HERMES-GIELIE/tests/test_run_journal_routes.py

186 lines
5.8 KiB
Python
Raw Normal View History

2026-05-15 01:37:54 +00:00
from pathlib import Path
from types import SimpleNamespace
import io
ROOT = Path(__file__).resolve().parents[1]
ROUTES_SRC = (ROOT / "api" / "routes.py").read_text()
def test_stream_status_exposes_replay_summary():
status_pos = ROUTES_SRC.index('parsed.path == "/api/chat/stream/status"')
block = ROUTES_SRC[status_pos : status_pos + 900]
assert "find_run_summary(stream_id)" in block
assert '"replay_available"' in block
assert '"journal"' in block
assert "_run_journal_status_payload" in block
def test_dead_stream_sse_replays_journal_before_404_fallback():
handler_pos = ROUTES_SRC.index("def _handle_sse_stream")
block = ROUTES_SRC[handler_pos : handler_pos + 1800]
assert "find_run_summary(stream_id)" in block
assert "stream not found" in block
assert "_replay_run_journal" in block
2026-05-30 02:37:04 +00:00
assert "_parse_run_journal_after_seq(qs, stream_id)" in block
2026-05-15 01:37:54 +00:00
assert 'Content-Type", "text/event-stream; charset=utf-8"' in block
def test_replay_emits_event_ids_and_stale_restart_diagnostic():
replay_pos = ROUTES_SRC.index("def _replay_run_journal")
block = ROUTES_SRC[replay_pos : replay_pos + 1200]
assert "read_run_events" in block
assert "_sse_with_id" in block
assert "stale_interrupted_event" in block
def test_session_payload_exposes_runtime_journal_for_stale_streams():
assert "original_stream_id = getattr(s, \"active_stream_id\", None)" in ROUTES_SRC
assert '"runtime_journal"' in ROUTES_SRC
assert 'terminal_state = "lost-worker-bookkeeping"' in ROUTES_SRC
Release v0.51.237 — Release HE (stage-q8): reconcile early-cancel against live worker state (#3492) ## Release v0.51.237 — Release HE (stage-q8) Phase 3 MEDIUM-ring pick (3-factor framework): a real concurrency/state-consistency fix from a regular contributor (@franksong2702, ★★★ 38 merges). The hardest review of the sweep — the dual gate caught **two** silent data-loss bugs across two review rounds. ### Fixed | PR | Author | Fix | |----|--------|-----| | #3475 | @franksong2702 | Cancelling a live turn immediately after send now reliably stops the worker and settles the session to a cancelled state (was: spinner over a blank page). `cancel_stream()` falls back to the live active-run registry (`ACTIVE_RUNS`) + session agent cache when `STREAMS` has already detached, so the worker still receives `interrupt("Cancelled by user")`. `/api/session` reports run-journal active state from the live registry instead of trusting a persisted `active_stream_id`. | ### Pre-release dual gate caught TWO silent data-loss bugs (both fixed + regression-tested) The PR's refactor moved `agent.interrupt()` ahead of the partial-text/reasoning/tool-call snapshot, and that snapshot was no longer under `streams_lock`: 1. **Codex round 1** — the worker's `finally` (which pops `STREAM_PARTIAL_TEXT`/`STREAM_REASONING_TEXT`/`STREAM_LIVE_TOOL_CALLS` under `STREAMS_LOCK`) could clear those buffers the instant `interrupt()` wakes it, so a cancelled turn **silently lost its already-streamed text**. (Opus reviewed the original and said ship — it assumed the snapshot was still lock-protected; the stale comment claimed so but the code wasn't. Verified against the actual code → Codex was right.) 2. **Codex round 2** — my first fix only snapshotted on the `STREAMS`-present path; the detached `ACTIVE_RUNS`-only path (the case this PR adds) still lost text. Fixed by hoisting the snapshot above the `if stream_present` branch so it runs unconditionally under the lock. Both fixes have regression tests **verified to fail against the buggy versions** (`test_cancel_preserves_partial_text_when_interrupt_pops_buffers` + `test_cancel_preserves_partial_text_on_detached_active_run_path`). ### Gate results - **Full pytest suite**: 7481 passed, 9 skipped, 3 xpassed, **0 failed** - **ruff forward gate**: CLEAN - **browser-smoke gate**: CLEAN - **Codex (regression)**: SHIP ONLY WITH FIXES (×2 rounds) → both MUST-FIXes applied → re-reviewed **SAFE TO SHIP** - **Opus (correctness)**: reviewed the original (SAFE); the shipped version is strictly safer (adds the under-lock snapshot Opus deemed unnecessary) - Deadlock concern cleared: no path takes `ACTIVE_RUNS_LOCK` then `STREAMS_LOCK`. Rebased onto current master (streaming.py/routes.py merged clean — no overlap with the #3468 dedup change shipped in HC). Closes #3475. Co-authored-by: franksong2702 <franksong2702@users.noreply.github.com>
2026-06-03 18:26:12 +00:00
assert "active=journal_active" in ROUTES_SRC
assert "journal_active = bool(original_stream_id in active_stream_ids)" in ROUTES_SRC
2026-05-15 01:37:54 +00:00
def test_status_payload_marks_non_terminal_dead_journal_as_stale():
import api.routes as routes
payload = routes._run_journal_status_payload(
{
"session_id": "session_1",
"run_id": "run_1",
"last_seq": 3,
"last_event_id": "run_1:3",
"last_event": "token",
"terminal": False,
"terminal_state": "running",
},
active=False,
)
assert payload["terminal"] is False
assert payload["terminal_state"] == "lost-worker-bookkeeping"
2026-05-15 01:37:54 +00:00
assert payload["last_event_id"] == "run_1:3"
def test_status_payload_preserves_terminal_error_state():
import api.routes as routes
payload = routes._run_journal_status_payload(
{
"session_id": "session_1",
"run_id": "run_1",
"terminal": True,
"terminal_state": "interrupted-by-crash",
"last_event": "apperror",
},
active=False,
)
assert payload["terminal"] is True
assert payload["terminal_state"] == "interrupted-by-crash"
def test_replay_run_journal_writes_replayed_events_and_synthetic_terminal(monkeypatch):
import api.routes as routes
handler = SimpleNamespace(wfile=io.BytesIO())
monkeypatch.setattr(
routes,
"find_run_summary",
lambda stream_id: {
"session_id": "session_1",
"run_id": stream_id,
"terminal": False,
},
)
monkeypatch.setattr(
routes,
"read_run_events",
lambda session_id, run_id, after_seq=None: {
"events": [
{
"event": "token",
"payload": {"text": "hello"},
"event_id": f"{run_id}:1",
}
]
},
)
monkeypatch.setattr(
routes,
"stale_interrupted_event",
lambda session_id, run_id, after_seq=None: {
"event": "apperror",
"payload": {"type": "interrupted"},
"event_id": f"{run_id}:2",
},
)
assert routes._replay_run_journal(handler, "run_1", 0) is True
body = handler.wfile.getvalue().decode("utf-8")
assert "id: run_1:1\n" in body
assert "event: token\n" in body
assert "id: run_1:2\n" in body
assert "event: apperror\n" in body
def test_replay_run_journal_honors_after_seq_cursor(monkeypatch):
import api.routes as routes
captured = {}
handler = SimpleNamespace(wfile=io.BytesIO())
monkeypatch.setattr(
routes,
"find_run_summary",
lambda stream_id: {
"session_id": "session_1",
"run_id": stream_id,
"terminal": True,
},
)
def fake_read_run_events(session_id, run_id, after_seq=None):
captured["after_seq"] = after_seq
return {
"events": [
{
"event": "done",
"payload": {"session": {"session_id": session_id}},
"event_id": f"{run_id}:4",
}
]
}
monkeypatch.setattr(routes, "read_run_events", fake_read_run_events)
assert routes._replay_run_journal(handler, "run_1", 3) is True
assert captured["after_seq"] == 3
body = handler.wfile.getvalue().decode("utf-8")
assert "id: run_1:4\n" in body
assert "event: done\n" in body
2026-05-30 02:37:04 +00:00
def test_parse_run_journal_after_seq_is_run_aware():
import api.routes as routes
assert routes._parse_run_journal_after_seq(
{"after_event_id": ["new-run:7"], "after_seq": ["7"]},
"new-run",
) == 7
assert routes._parse_run_journal_after_seq(
{"after_event_id": ["old-run:7"], "after_seq": ["7"]},
"new-run",
) is None
assert routes._parse_run_journal_after_seq({"after_seq": ["3"]}, "new-run") == 3
def test_frontend_sends_run_aware_replay_cursor():
messages_src = (ROOT / "static" / "messages.js").read_text(encoding="utf-8")
assert "let _lastRunJournalEventId=''" in messages_src
assert "_lastRunJournalEventId=raw" in messages_src
assert "after_event_id=${encodeURIComponent(_lastRunJournalEventId||'')}" in messages_src