Python
test(observability): Phase 3 tests use recent turn dates, not 1970
Devika Verma7a087f9d0c65
diff --git a/tests/test_observability_phase3_engine.py b/tests/test_observability_phase3_engine.pyindex 4ca11d42..da15f23c 100644--- a/tests/test_observability_phase3_engine.py+++ b/tests/test_observability_phase3_engine.py@@ -9,6 +9,7 @@ from __future__ import annotationsimport asyncioimport hashlibimport json+import timefrom pathlib import Pathimport jsonschema@@ -29,6 +30,10 @@ from coworker.tools import ToolRegistrySCHEMA = json.loads((Path(__file__).parent / "corpora/observability/recording-format.schema.json").read_text("utf-8"))+# A turn an hour old, not from 1970: a store with the background writer sweeps on its first tick, and+# a 1970 turn is past the 30 days, so it could vanish between the write and the read.+RECENT = int(time.time() * 1000) - 3_600_000+class _Scripted(ProviderClient):def __init__(self, turns):@@ -231,7 +236,7 @@ def test_lead_polling_is_stored_once_on_the_last_turn_of_the_run(tmp_path):manager = SessionManager(workspace=tmp_path, provider=_Scripted([]))store = manager.recordings_storeteam = {"team_id": "t1", "role": "lead"}- turns = [_turn(f"L{i}", "lead-s", i, 1_000_000 + i * 60_000, {"reason": "timer", "board_seq": 5}, team)+ turns = [_turn(f"L{i}", "lead-s", i, RECENT + i * 60_000, {"reason": "timer", "board_seq": 5}, team)for i in range(1, 5)]_write(store, turns, [_step(t["turn_id"], "lead-s", 1, t["started_at"]) for t in turns])manager._scope_lead_polling("lead-s")@@ -331,8 +336,8 @@ def test_run_health_counts_a_stuck_run_as_failed():def test_turns_recorded_before_the_ledger_are_filled_in(tmp_path):manager = SessionManager(workspace=tmp_path, provider=_Scripted([]))store = manager.recordings_store- t = _turn("T0", "s-old", 1, 1_000, {"reason": "user"})- _write(store, [t], [_step("T0", "s-old", 1, 1_000)])+ t = _turn("T0", "s-old", 1, RECENT, {"reason": "user"})+ _write(store, [t], [_step("T0", "s-old", 1, RECENT)])with store._lock, store._conn:store._conn.execute("DELETE FROM turn_usage") # as a Phase 2 store has noneassert store.turns_without_usage() == ["T0"]diff --git a/tests/test_observability_phase3_review_fixes.py b/tests/test_observability_phase3_review_fixes.pyindex 5827a1b7..730bd917 100644--- a/tests/test_observability_phase3_review_fixes.py+++ b/tests/test_observability_phase3_review_fixes.py@@ -8,6 +8,7 @@ from __future__ import annotationsimport asyncioimport json+import timefrom types import SimpleNamespacefrom coworker.observability import RecordingStore, rollups, scopes, signals, usage_api@@ -17,6 +18,10 @@ from coworker.sessions import SessionRecordfrom coworker.teams.model import Actor, Rolefrom coworker.teams.registry import TeamWorker+# A turn an hour old, not from 1970: a store with the background writer sweeps on its first tick, and+# a 1970 turn is past the 30 days, so it could vanish between the write and the read.+RECENT = int(time.time() * 1000) - 3_600_000+def _turn(tid, sid, seq, at, *, automation=None, status="completed"):t = {"turn_id": tid, "session_id": sid, "seq": seq, "kind": "run", "trigger": "user", "started_at": at,@@ -141,9 +146,9 @@ def test_a_team_summary_reads_once_and_works_out_again_only_on_a_change(tmp_pathlead_actor="lead",workers=[TeamWorker(actor="sam", persona="swe-worker", session_id="worker-session")])store = manager.recordings_store- _write(store, [_turn("W1", "worker-session", 1, 1_000)],- [_step("W1", "worker-session", 1, 1_000),- _step("W1", "worker-session", 2, 1_100, kind="tool", name="transition", call_id="t1", attrs={})])+ _write(store, [_turn("W1", "worker-session", 1, RECENT + 1_000)],+ [_step("W1", "worker-session", 1, RECENT + 1_000),+ _step("W1", "worker-session", 2, RECENT + 1_100, kind="tool", name="transition", call_id="t1", attrs={})])reads = {"team_turns": 0, "load_recording": 0}for name in reads:@@ -155,8 +160,8 @@ def test_a_team_summary_reads_once_and_works_out_again_only_on_a_change(tmp_pathmonkeypatch.setattr(store, name, counted)# the hand-off step is one indexed read, not the whole recording- assert manager._handoff_step("worker-session", 2_000) == {"step_id": "W1-s2", "seq": 2, "turn_id": "W1"}- assert manager._handoff_step("worker-session", 0) is None # before the step+ assert manager._handoff_step("worker-session", RECENT + 2_000) == {"step_id": "W1-s2", "seq": 2, "turn_id": "W1"}+ assert manager._handoff_step("worker-session", RECENT) is None # before the stepassert reads["load_recording"] == 0first = manager.team_summary(team.team_id)@@ -167,7 +172,7 @@ def test_a_team_summary_reads_once_and_works_out_again_only_on_a_change(tmp_pathassert reads["team_turns"] == after_first # nothing changed: the kept answerassert again["wakes"] == first["wakes"] and again["events"] == first["events"]# a member's new turn is a change- _write(store, [_turn("W2", "worker-session", 2, 5_000)], [_step("W2", "worker-session", 1, 5_000)])+ _write(store, [_turn("W2", "worker-session", 2, RECENT + 5_000)], [_step("W2", "worker-session", 1, RECENT + 5_000)])manager.team_summary(team.team_id)assert reads["team_turns"] == after_first + 1# so is a board change@@ -208,14 +213,14 @@ def test_a_resumed_automation_turn_is_tagged_with_its_run(tmp_path, monkeypatch)store = manager.recordings_storerun = {"task_id": "task-1", "run_id": "run-1", "trigger": "schedule"}# the run's own turn stopped at an approval; the app restarted; the card is answered- _write(store, [_turn("R1", "__run__run-1", 1, 1_000, automation=run, status="unknown")],- [_step("R1", "__run__run-1", 1, 1_000)])+ _write(store, [_turn("R1", "__run__run-1", 1, RECENT + 1_000, automation=run, status="unknown")],+ [_step("R1", "__run__run-1", 1, RECENT + 1_000)])got = _resume(manager, "__run__run-1")assert got["automation"] == run and got["wake"]["reason"] == "answer"# a follow-up typed into the run's session later is not the run's, nor is its resume (TD3-23)- _write(store, [_turn("R2", "__run__run-1", 2, 9_000, status="unknown")], [_step("R2", "__run__run-1", 1, 9_000)])+ _write(store, [_turn("R2", "__run__run-1", 2, RECENT + 9_000, status="unknown")], [_step("R2", "__run__run-1", 1, RECENT + 9_000)])assert _resume(manager, "__run__run-1")["automation"] is None# an ordinary session's resume carries none- _write(store, [_turn("S1", "plain", 1, 1_000, automation=run)], [_step("S1", "plain", 1, 1_000)])+ _write(store, [_turn("S1", "plain", 1, RECENT + 1_000, automation=run)], [_step("S1", "plain", 1, RECENT + 1_000)])assert _resume(manager, "plain")["automation"] is Nonemanager.observability.close()