feat(worker): answer traces stored from the question on and filled in as the answer is made

A trace is now kept the moment the request arrives (at the time it was
asked, ms -1), its steps are stored as the engine adds each one, and it
ends with the answer. Traces a killed run left unfinished are closed at
start. The 대화 screen reads a trace still being made as pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-16 12:03:45 +09:00
parent fd7ccab55b
commit 6fc3ad3d57
7 changed files with 171 additions and 44 deletions

View File

@@ -243,10 +243,11 @@ def test_chat_tool_reads_and_searches_one_channels_messages(store):
def test_how_an_answer_was_made_is_kept_per_person_and_never_searched(store):
steps = [{"kind": "tools", "names": ["getWeather"]},
{"kind": "tool", "name": "getWeather", "args": {"location": "서울"}, "ok": True, "result": "맑음"}]
store.add_trace("voice", VC, "a", "서울 날씨 어때", "맑습니다", steps, ms=1200, ts=T0)
store.add_trace("chat", CHAT, "b", "안녕", "", [], ms=50, ts=T0 + 1)
store.finish_trace(store.start_trace("voice", VC, "a", "서울 날씨 어때", ts=T0), "맑습니다", steps, ms=1200)
store.finish_trace(store.start_trace("chat", CHAT, "b", "안녕", ts=T0 + 1), "", [], ms=50)
[t] = store.traces_of("a")
assert (t.via, t.channel_id, t.question, t.answer, t.ms, t.steps) == ("voice", VC, "서울 날씨 어때", "맑습니다", 1200, steps)
assert t.ts == T0 # when it was asked, not when the answer was ready
# A trace is no record: the brain never reads it back as history.
assert store.search("a", "날씨", scope=Scope()) == []
assert store.recent("a", T0 + 2, scope=Scope()) == []
@@ -255,9 +256,30 @@ def test_how_an_answer_was_made_is_kept_per_person_and_never_searched(store):
def test_traces_have_their_own_retention_and_follow_the_history_retention(store):
store.add_trace("voice", VC, "a", "옛 질문", "옛 답", [], ms=1, ts=T0)
store.add_trace("voice", VC, "a", "새 질문", "새 답", [], ms=1, ts=T0 + 100)
store.finish_trace(store.start_trace("voice", VC, "a", "옛 질문", ts=T0), "옛 답", [], ms=1)
store.finish_trace(store.start_trace("voice", VC, "a", "새 질문", ts=T0 + 100), "새 답", [], ms=1)
store.purge_traces_older_than(T0 + 50)
assert [t.question for t in store.traces_of("a")] == ["새 질문"]
store.purge_older_than(T0 + 200)
assert store.traces_of("a") == []
def test_a_trace_is_kept_from_the_question_on_and_filled_in_as_the_answer_is_made(store):
tid = store.start_trace("voice", VC, "a", "서울 날씨 어때", ts=T0)
[t] = store.traces_of("a")
assert (t.question, t.answer, t.ms, t.steps) == ("서울 날씨 어때", "", -1, []) # -1: still being made
store.trace_steps(tid, [{"kind": "tools", "names": ["getWeather"]}])
assert store.traces_of("a")[0].steps == [{"kind": "tools", "names": ["getWeather"]}]
assert store.traces_of("a")[0].ms == -1
store.finish_trace(tid, "맑습니다", [{"kind": "tools", "names": ["getWeather"]}], ms=900)
[t] = store.traces_of("a")
assert (t.answer, t.ms, t.ts) == ("맑습니다", 900, T0)
def test_traces_a_killed_run_left_unfinished_are_closed_without_an_answer(store):
store.start_trace("voice", VC, "a", "끊긴 질문", ts=T0)
store.finish_trace(store.start_trace("voice", VC, "a", "끝난 질문", ts=T0 + 1), "답", [], ms=5)
assert store.close_unfinished_traces() == 1
cut, done = store.traces_of("a")
assert (cut.answer, cut.ms >= 0) == ("", True)
assert (done.answer, done.ms) == ("답", 5)

View File

@@ -244,6 +244,32 @@ def test_each_answer_keeps_how_it_was_made_with_the_records_it_was_given(store):
assert (task.via, task.channel_id, task.answer) == ("task", None, "answer for task")
@pytest.mark.unit
def test_a_trace_is_stored_from_the_request_on_and_fills_in_while_the_answer_is_made(store):
import time
during = []
def answer(user, text):
during.append((store.traces_of(user), time.time())) # the engine has added its steps
return "answer"
async def body(url):
async with connect(url) as ws:
await hello(ws)
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="reply", id="r1", user="100", text="q", via="voice", speak=False)
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r1")
asyncio.run(with_worker(body, store=store, brain=FakeBrain(answer)))
[(traces, answering_at)] = during
[t] = traces
assert (t.question, t.answer, t.ms) == ("q", "", -1) # -1: still being made
assert [s["kind"] for s in t.steps] == ["records", "tools"]
assert t.ts <= answering_at
[t] = store.traces_of("100")
assert (t.answer, t.ms >= 0, t.ts) == ("answer", True, traces[0].ts)
@pytest.mark.unit
def test_without_a_brain_reply_is_an_error():
async def body(url):
@@ -337,15 +363,16 @@ def test_presence_left_open_by_a_previous_run_is_closed_at_start(store):
def test_traces_older_than_thirty_days_are_removed_at_start(store):
import time
now = time.time()
store.add_trace("voice", "vc1", "100", "old", "a", [], ms=1, ts=now - 31 * 86400)
store.add_trace("voice", "vc1", "100", "new", "a", [], ms=1, ts=now - 29 * 86400)
store.finish_trace(store.start_trace("voice", "vc1", "100", "old", ts=now - 31 * 86400), "a", [], ms=1)
store.finish_trace(store.start_trace("voice", "vc1", "100", "new", ts=now - 29 * 86400), "a", [], ms=1)
store.start_trace("voice", "vc1", "100", "cut off", ts=now - 60) # a killed run never finished it
async def body(url):
async with connect(url) as ws:
await hello(ws)
asyncio.run(with_worker(body, store=store))
assert [t.question for t in store.traces_of("100")] == ["new"]
assert [(t.question, t.ms >= 0) for t in store.traces_of("100")] == [("new", True), ("cut off", True)]
@pytest.mark.unit