add basic clock functions (timer, alarm, date, current time)

This commit is contained in:
gabby committed 2026-10-05 05:30:15 +00:00
1 parent 780c9592ed
commit 07b0e2ff7e
28 files changed
+979 -8

No files matched your search

+12 -4
View File
@@ -34,6 +34,10 @@ def test_expected_seeds_exist():
assert "calendar.create_event" in ids
assert "simplex.next_message" in ids
assert "simplex.connect_link" in ids
assert "time.now" in ids
assert "time.date" in ids
assert "time.set_timer" in ids
assert "time.set_alarm" in ids
@pytest.mark.parametrize("seed", SEED_DIRS, ids=[seed_id(s) for s in SEED_DIRS])
@@ -49,14 +53,18 @@ def test_seed_declares_a_real_integration(seed):
def test_seed_contract_is_flat_and_covered_by_declaration(seed):
code = (seed / "skill.py").read_text()
contract = parse_contract(code)
assert contract, "the seed must declare a CONTRACT constant"
for name, description in contract.items():
assert name.islower() and " " not in name, name
assert isinstance(description, str) and description.strip()
integration = parse_integration(code)
assert set(integration["config_vars"]) == set(contract)
mirror = json.loads((seed / "contract.json").read_text())
assert mirror == contract, "contract.json must mirror the CONTRACT constant"
if integration["transport"] == "compute":
# A local compute skill acts on the machine itself and needs no
# configuration; an empty contract is legitimate.
return
assert contract, "an external-integration seed must declare a CONTRACT constant"
for name, description in contract.items():
assert name.islower() and " " not in name, name
assert isinstance(description, str) and description.strip()
@pytest.mark.parametrize("seed", SEED_DIRS, ids=[seed_id(s) for s in SEED_DIRS])
+96
View File
@@ -0,0 +1,96 @@
"""Pure-stdlib tests for the in-process timer/alarm service.
The service is real local compute: a background thread fires on the host clock.
These tests use sub-second durations so they stay fast, and they assert the
mechanics (fire, drain, cancel, trace, honest message) rather than any external
service. Skill bodies that schedule timers are covered by the seed tests.
"""
import time
from datetime import datetime, timedelta, timezone
from semif_agent.timers import TimerService, format_duration
from semif_agent.trace import TraceLog
def wait_for(predicate, timeout=3.0):
deadline = time.time() + timeout
while time.time() < deadline:
if predicate():
return True
time.sleep(0.02)
return predicate()
def test_format_duration():
assert format_duration(1) == "1 second"
assert format_duration(30) == "30 seconds"
assert format_duration(60) == "1 minute"
assert format_duration(90) == "1 minute 30 seconds"
assert format_duration(3600) == "1 hour"
assert format_duration(5400) == "1 hour 30 minutes"
def test_timer_fires_and_drains_once(tmp_path):
trace = TraceLog(str(tmp_path / "runs.jsonl"))
service = TimerService(trace=trace)
try:
timer = service.set_timer(0.1, "0.1 seconds", run_id="r1", source="typed")
assert [t["id"] for t in service.pending()] == [timer.id]
assert wait_for(lambda: service.fired()), "the timer must fire"
fired = service.drain()
assert len(fired) == 1
assert fired[0].run_id == "r1"
assert fired[0].kind == "timer"
assert "0.1 seconds" in fired[0].message
assert service.drain() == [], "drain is one-shot"
assert service.pending() == []
events = [e for e in trace.read() if e["kind"] == "timer_fired"]
assert events, "firing must be traced"
assert events[0]["timer_id"] == timer.id
assert events[0]["timer_kind"] == "timer"
assert events[0]["run_id"] == "r1"
finally:
service.stop()
def test_alarm_fires():
service = TimerService()
try:
at = datetime.now(timezone.utc) + timedelta(seconds=0.1)
service.set_alarm(at, "5:00", run_id="r2")
assert wait_for(lambda: service.fired()), "the alarm must fire"
fired = service.fired()
assert fired[0]["kind"] == "alarm"
assert "Alarm" in fired[0]["message"]
finally:
service.stop()
def test_cancel_prevents_firing():
service = TimerService()
try:
timer = service.set_timer(0.2, "cancel me")
assert service.cancel(timer.id) is True
assert service.cancel(timer.id) is False
time.sleep(0.3)
assert service.drain() == []
assert service.pending() == []
finally:
service.stop()
def test_non_positive_duration_rejected():
service = TimerService()
try:
try:
service.set_timer(0, "nope")
except ValueError:
pass
else:
raise AssertionError("expected ValueError for a non-positive duration")
finally:
service.stop()