2026-05-30 09:15:35 +00:00
|
|
|
import os
|
|
|
|
|
|
2026-05-11 07:08:51 +00:00
|
|
|
from api.turn_journal import (
|
|
|
|
|
append_turn_journal_event,
|
|
|
|
|
append_turn_journal_event_for_stream,
|
|
|
|
|
derive_turn_journal_states,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_append_turn_journal_event_for_stream_reuses_submitted_turn_id(tmp_path):
|
|
|
|
|
submitted = append_turn_journal_event(
|
|
|
|
|
"sid-1",
|
|
|
|
|
{"event": "submitted", "turn_id": "turn-1", "stream_id": "stream-1", "content": "hello"},
|
|
|
|
|
session_dir=tmp_path,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
worker = append_turn_journal_event_for_stream(
|
|
|
|
|
"sid-1",
|
|
|
|
|
"stream-1",
|
|
|
|
|
{"event": "worker_started"},
|
|
|
|
|
session_dir=tmp_path,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert submitted["turn_id"] == "turn-1"
|
|
|
|
|
assert worker["turn_id"] == "turn-1"
|
2026-05-12 12:20:06 +00:00
|
|
|
states, _ = derive_turn_journal_states([submitted, worker])
|
2026-05-11 07:08:51 +00:00
|
|
|
assert states["turn-1"]["event"] == "worker_started"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_append_turn_journal_event_for_stream_falls_back_to_new_turn_for_missing_stream(tmp_path):
|
|
|
|
|
event = append_turn_journal_event_for_stream(
|
|
|
|
|
"sid-1",
|
|
|
|
|
"stream-missing",
|
|
|
|
|
{"event": "interrupted", "reason": "no submitted event found"},
|
|
|
|
|
session_dir=tmp_path,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert event["stream_id"] == "stream-missing"
|
|
|
|
|
assert event["turn_id"]
|
|
|
|
|
assert event["event"] == "interrupted"
|
2026-05-30 09:15:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_append_turn_journal_event_skips_directory_fsync_without_o_directory(tmp_path, monkeypatch):
|
|
|
|
|
monkeypatch.delattr(os, "O_DIRECTORY", raising=False)
|
|
|
|
|
|
|
|
|
|
event = append_turn_journal_event(
|
|
|
|
|
"sid-windows",
|
|
|
|
|
{"event": "submitted", "content": "hello"},
|
|
|
|
|
session_dir=tmp_path,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert event["event"] == "submitted"
|
2026-06-04 18:24:25 +00:00
|
|
|
journal_dir = tmp_path / "_turn_journal"
|
|
|
|
|
shards = list(journal_dir.glob(f"sid-windows~{os.getpid()}.jsonl"))
|
|
|
|
|
assert len(shards) == 1, f"expected one pid-scoped shard, found: {list(journal_dir.iterdir())}"
|