mirror of
https://github.com/andrewyng/openworker.git
synced 2026-11-20 20:37:44 +00:00
Phase 1 of agent observability, engine items E7 and E21 (ocw-context docs/observability/phase-1/technical-design.md v1.0). E7: coworker/observability/recorder.py is the one sink for the five recording events. It folds them into turns and steps by recording format 1.0 and keeps the canonical step order. coworker/observability/ store.py saves them in recordings.db next to coworker.db: pending writes are merged by id and written by one background thread, so a turn never waits and nothing is dropped. A daily sweep deletes recordings older than 30 days in small batches, and deleting a session deletes its recordings and the exchange pieces only it used. E21: every message appended or inserted into the engine's history gets a `mid` (a ULID) through one _add_message() helper. `mid` is a sidecar like `ts`: stripped before any provider sees it. Older transcripts without mids keep working. The shared golden test (30 scenarios, copied into tests/corpora/ observability) passes 30 of 30, folded in memory and read back from recordings.db.
116 lines
5.1 KiB
Python
116 lines
5.1 KiB
Python
"""ConversationStore.save when the message list changed in the MIDDLE, not only at the end.
|
|
|
|
`save()` used to persist by count: if the in-memory list was longer than the file it
|
|
appended `messages[existing:]`. Two code paths change the list in the middle:
|
|
|
|
* `load()` heals tool-call/result pairing (`_repair_tool_pairing`): it synthesises a
|
|
placeholder result right after an orphaned call and moves a misplaced result up.
|
|
* `TurnEngine._repair_dangling_tool_calls` inserts a stub result right after the
|
|
assistant message whose call never got one.
|
|
|
|
After either, appending by count wrote a shifted tail: the inserted message was never
|
|
saved and the message after it was written twice, again on every load-and-save cycle.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from coworker.conversations import ConversationStore
|
|
from coworker.sessions import SessionRecord
|
|
|
|
|
|
def _assistant_call(call_id="c1"):
|
|
return {"role": "assistant", "content": None, "tool_calls": [
|
|
{"id": call_id, "type": "function", "function": {"name": "run_shell", "arguments": "{}"}}]}
|
|
|
|
|
|
def _record(sid, messages):
|
|
return SessionRecord(session_id=sid, workspace="/w", model="m", mode="interactive", messages=messages)
|
|
|
|
|
|
def _file_lines(store, sid):
|
|
return [json.loads(line) for line in store._file(sid).read_text(encoding="utf-8").splitlines() if line.strip()]
|
|
|
|
|
|
def test_save_after_load_repair_persists_the_placeholder_and_does_not_duplicate(tmp_path):
|
|
store = ConversationStore(tmp_path / "state")
|
|
sid = "abc123def456"
|
|
# An interrupted turn left a call with no result, followed by the next user message.
|
|
store.save(_record(sid, [{"role": "user", "content": "go"}, _assistant_call("c1"),
|
|
{"role": "user", "content": "next"}]))
|
|
|
|
rec = store.load(sid) # heals: placeholder inserted after the call
|
|
assert [m["role"] for m in rec.messages] == ["user", "assistant", "tool", "user"]
|
|
rec.messages.append({"role": "assistant", "content": "answer"})
|
|
store.save(rec)
|
|
|
|
assert _file_lines(store, sid) == rec.messages
|
|
# And it stays correct across another load-and-save cycle.
|
|
again = store.load(sid)
|
|
assert again.messages == rec.messages
|
|
store.save(again)
|
|
assert _file_lines(store, sid) == rec.messages
|
|
assert [m.get("content") for m in _file_lines(store, sid)].count("next") == 1
|
|
|
|
|
|
def test_save_after_mid_history_insert_writes_the_inserted_message(tmp_path):
|
|
store = ConversationStore(tmp_path / "state")
|
|
sid = "abc123def457"
|
|
msgs = [{"role": "user", "content": "go"}, _assistant_call("c1"),
|
|
{"role": "tool", "tool_call_id": "c1", "content": "{}"}, {"role": "user", "content": "more"}]
|
|
store.save(_record(sid, msgs))
|
|
# Something inserts a message in the middle (as the engine's dangling-call repair does),
|
|
# then a new message is appended.
|
|
msgs.insert(1, {"role": "notice", "kind": "note", "text": "inserted"})
|
|
msgs.append({"role": "assistant", "content": "done"})
|
|
store.save(_record(sid, msgs))
|
|
assert _file_lines(store, sid) == msgs
|
|
|
|
|
|
def test_restart_mid_approval_then_mode_switch_then_new_turn(tmp_path):
|
|
"""The sequence seen in the app: a turn parks on an approval and is saved, the app is
|
|
quit, the reopened session switches mode (a notice is saved after the parked call),
|
|
then a new message runs `_repair_dangling_tool_calls`, which inserts the stub between
|
|
the call and the notice."""
|
|
from types import SimpleNamespace
|
|
|
|
from coworker.engine import TurnEngine
|
|
|
|
store = ConversationStore(tmp_path / "state")
|
|
sid = "abc123def459"
|
|
store.save(_record(sid, [{"role": "user", "content": "run it"}, _assistant_call("c1")]))
|
|
|
|
rec = store.load(sid) # trailing call: pending, left as is
|
|
rec.messages.append({"role": "notice", "kind": "mode_switch", "text": "Discuss is on."})
|
|
store.save(rec)
|
|
|
|
engine = SimpleNamespace(messages=rec.messages)
|
|
engine._add_message = lambda m, at=None: TurnEngine._add_message(engine, m, at=at)
|
|
TurnEngine._repair_dangling_tool_calls(engine)
|
|
rec.messages.append({"role": "user", "content": "never mind"})
|
|
rec.messages.append({"role": "assistant", "content": "OK"})
|
|
store.save(rec)
|
|
|
|
assert _file_lines(store, sid) == rec.messages
|
|
assert [m["role"] for m in _file_lines(store, sid)] == [
|
|
"user", "assistant", "tool", "notice", "user", "assistant"]
|
|
|
|
|
|
def test_plain_append_still_appends(tmp_path):
|
|
"""The common path stays an append: the existing lines are not rewritten."""
|
|
store = ConversationStore(tmp_path / "state")
|
|
sid = "abc123def458"
|
|
msgs = [{"role": "user", "content": "go"}, {"role": "assistant", "content": "ok"}]
|
|
store.save(_record(sid, msgs))
|
|
path = store._file(sid)
|
|
before = path.stat().st_ino if hasattr(path.stat(), "st_ino") else None
|
|
head = path.read_text(encoding="utf-8")
|
|
msgs.append({"role": "user", "content": "again"})
|
|
store.save(_record(sid, msgs))
|
|
text = path.read_text(encoding="utf-8")
|
|
assert text.startswith(head)
|
|
assert _file_lines(store, sid) == msgs
|
|
if before is not None and before != 0:
|
|
assert path.stat().st_ino == before # same file, appended to, not replaced
|