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

@@ -215,11 +215,13 @@ pub fn query(path: &Path, bot: Option<&str>, q: &Query) -> Result<Value, String>
let (id, ts): (i64, f64) = (r.get(0)?, r.get(1)?);
let user: String = r.get(4)?;
let steps: String = r.get(8)?;
let ms: i64 = r.get(7)?;
Ok(((ts, 1, id), json!({
"key": format!("t{id}"), "type": "thought", "at": ts, "via": r.get::<_, String>(2)?,
"channel": r.get::<_, Option<String>>(3)?,
"who": {"id": user.clone(), "name": name_of(&user)},
"question": r.get::<_, String>(5)?, "answer": r.get::<_, String>(6)?, "ms": r.get::<_, i64>(7)?,
"question": r.get::<_, String>(5)?, "answer": r.get::<_, String>(6)?, "ms": ms.max(0),
"pending": ms < 0,
"steps": serde_json::from_str::<Value>(&steps).unwrap_or(json!([])),
})))
}).map_err(err)?;
@@ -384,6 +386,21 @@ mod tests {
assert!(Cursor::of(&json!({"at": 1.0, "key": "x1"})).is_none());
}
#[test]
fn a_trace_still_being_made_reads_as_pending_with_its_steps_so_far() {
let (path, c) = db("pending");
rec(&c, 10.0, "voice", "vc", "1", "Alice", None, "서울 날씨 어때");
c.execute("INSERT INTO traces(ts, via, channel_id, user_id, question, answer, ms, steps) VALUES (?,?,?,?,?,?,?,?)",
rusqlite::params![10.5, "voice", "vc", "1", "서울 날씨 어때", "", -1, r#"[{"kind":"tools","names":["getWeather"]}]"#])
.unwrap();
let v = query(&path, None, &Query::default()).unwrap();
let t = &v["items"][1];
assert_eq!((t["type"].as_str(), t["pending"].as_bool()), (Some("thought"), Some(true)));
assert_eq!(t["steps"][0]["names"], json!(["getWeather"]));
let done = query(&sample("pending-done"), None, &Query::default()).unwrap();
assert_eq!(done["items"][1]["pending"], false);
}
#[test]
fn the_bot_id_comes_from_its_token() {
assert_eq!(bot_id_of_token("MTIzNDU2Nzg5MDEyMzQ1Njc4.GxYzAb.abcdef").as_deref(), Some("123456789012345678"));

View File

@@ -44,13 +44,13 @@ CREATE TABLE IF NOT EXISTS presence (
CREATE INDEX IF NOT EXISTS presence_user ON presence(user_id, channel_id);
CREATE TABLE IF NOT EXISTS traces (
id INTEGER PRIMARY KEY,
ts REAL NOT NULL, -- when the answer was ready
ts REAL NOT NULL, -- when it was asked
via TEXT NOT NULL, -- voice | chat | task
channel_id TEXT, -- where it was asked; none for a task
user_id TEXT NOT NULL,
question TEXT NOT NULL,
answer TEXT NOT NULL, -- empty when the brain gave none
ms INTEGER NOT NULL,
ms INTEGER NOT NULL, -- -1 while the answer is being made
steps TEXT NOT NULL -- JSON list (history.spec.md, "Traces")
);
CREATE INDEX IF NOT EXISTS traces_ts ON traces(ts);
@@ -107,6 +107,10 @@ class HistoryContext:
matches: List[str] = field(default_factory=list)
def _steps_json(steps: list) -> str:
return json.dumps(steps, ensure_ascii=False, default=str)
def _fold(text: str) -> str:
"""One Unicode folding for stored text and query words alike (NFKC, then case folding),
so matching does not depend on SQLite's ASCII-only lower()."""
@@ -225,16 +229,34 @@ class HistoryStore:
# -- maintenance --------------------------------------------------------
def add_trace(self, via: str, channel_id: Optional[str], user_id: str, question: str, answer: str,
steps: list, ms: int, ts: float) -> int:
def start_trace(self, via: str, channel_id: Optional[str], user_id: str, question: str, ts: float) -> int:
"""A trace from the moment it was asked: no answer yet and ``ms`` -1."""
with self._lock:
cur = self._conn.execute(
"INSERT INTO traces(ts, via, channel_id, user_id, question, answer, ms, steps) VALUES (?,?,?,?,?,?,?,?)",
(ts, via, channel_id, user_id, question, answer or "", int(ms),
json.dumps(steps, ensure_ascii=False, default=str)))
"INSERT INTO traces(ts, via, channel_id, user_id, question, answer, ms, steps) VALUES (?,?,?,?,?,'',-1,'[]')",
(ts, via, channel_id, user_id, question))
self._conn.commit()
return int(cur.lastrowid)
def trace_steps(self, trace_id: int, steps: list) -> None:
"""The steps so far of a trace still being made."""
with self._lock:
self._conn.execute("UPDATE traces SET steps=? WHERE id=?", (_steps_json(steps), trace_id))
self._conn.commit()
def finish_trace(self, trace_id: int, answer: str, steps: list, ms: int) -> None:
with self._lock:
self._conn.execute("UPDATE traces SET answer=?, steps=?, ms=? WHERE id=?",
(answer or "", _steps_json(steps), max(0, int(ms)), trace_id))
self._conn.commit()
def close_unfinished_traces(self) -> int:
"""End, with no answer, the traces a killed run left being made (the host calls it at start)."""
with self._lock:
n = self._conn.execute("UPDATE traces SET ms=0 WHERE ms < 0").rowcount
self._conn.commit()
return n
def traces_of(self, person: str) -> List[Trace]:
with self._lock:
rows = self._conn.execute(

View File

@@ -73,10 +73,13 @@ Setting `since` (default none, meaning the whole history) drops records older th
How each answer was made, for the management app's 대화 screen (`app/src/admin.spec.md`,
"Conversations"). A trace is never a record: no read above returns it and it never reaches a prompt.
- `add_trace(via, channel_id, user_id, question, answer, steps, ms, ts)`: one per `reply` the brain
ran (`via` `voice`, `chat` or `task`; `channel_id` where it was asked, none for a task; `answer`
empty when the brain gave none; `ts` when it was ready; `ms` how long it took). `steps` is a JSON
list in the order they happened:
- One per `reply` the brain ran, kept from the moment it was asked so the screen can show it while
the answer is made: `start_trace(via, channel_id, user_id, question, ts)` (`via` `voice`, `chat` or
`task`; `channel_id` where it was asked, none for a task; `ts` when it was asked) stores it with no
answer and `ms` -1, `trace_steps(id, steps)` stores the steps so far, and
`finish_trace(id, answer, steps, ms)` the end (`answer` empty when the brain gave none; `ms` how
long it took). `close_unfinished_traces()`, which the host calls at start, ends with no answer the
traces a killed run left at -1. `steps` is a JSON list in the order they happened:
- `{"kind": "records", "recent": [...], "matches": [...], "notes": n}`: the history lines and the
number of personal notes the brain was given (the voice worker adds it first),
- `{"kind": "tools", "names": [...]}`: the tools the turn was offered after routing,
@@ -87,7 +90,7 @@ How each answer was made, for the management app's 대화 screen (`app/src/admin
error (at most 2,000 characters), or `"skipped": "not_allowed" | "duplicate"` with `ok` false
for a call the engine refused.
The engine writes them through `turn_state.trace` (`reply/turn_state.py`), which does nothing when
no trace list was set (v1, evals).
no trace list was set (v1, evals); the voice worker's list stores the steps as each is added.
- `traces_of(person)`: the person's traces, oldest first. `forget_person` deletes them too.
- `purge_traces_older_than(ts)`: the voice worker keeps traces 30 days (`TRACE_KEEP_DAYS`) and
removes older ones when it starts. `purge_older_than` removes old traces with the records.

View File

@@ -55,6 +55,37 @@ MAX_NOTE_CHARS = 300
TRACE_KEEP_DAYS = 30
class _LiveTrace(list):
"""A reply's trace (history.spec.md, "Traces"): stored as soon as the request arrives and again
as each step is added, so the 대화 screen fills it in while the answer is made. The engine adds
steps from the brain's thread. Storing never stops the answer."""
def __init__(self, store, via: str, channel: str | None, user: str, question: str):
super().__init__()
self.store, self.began, self.id = store, time.time(), None
if store is not None:
self.id = self._keep(store.start_trace, via, channel, user, question, ts=self.began)
@staticmethod
def _keep(write, *args, **kw):
try:
return write(*args, **kw)
except Exception as e: # noqa: BLE001 - a trace is a view, not part of the answer
debug_log(f"trace not kept: {type(e).__name__}", "llm")
return None
def append(self, step) -> None:
super().append(step)
if self.id is not None:
self._keep(self.store.trace_steps, self.id, list(self))
def finish(self, answer: str) -> None:
if self.id is not None:
self._keep(self.store.finish_trace, self.id, answer, list(self),
ms=round((time.time() - self.began) * 1000))
self.id = None
class VoiceWorker:
def _partials_held(self) -> bool:
return self.llm_game and self.answering > 0
@@ -118,6 +149,9 @@ class VoiceWorker:
n = self.history.close_open_presence(time.time())
if n:
debug_log(f"closed {n} presence intervals left open by a previous run", "voice_worker")
n = self.history.close_unfinished_traces()
if n:
debug_log(f"closed {n} answer traces left unfinished by a previous run", "voice_worker")
n = self.history.purge_traces_older_than(time.time() - TRACE_KEEP_DAYS * 86400)
if n:
debug_log(f"removed {n} answer traces older than {TRACE_KEEP_DAYS} days", "voice_worker")
@@ -609,26 +643,22 @@ class Session:
asyncio.get_running_loop().create_task(self._reply(rid, user, text, via, speak, early, channel, notes))
@staticmethod
def _trace_start(history, notes: list | None) -> list:
def _records_step(history, notes: list | None) -> dict:
"""The first step of an answer's trace: the records and notes it was given."""
return [{"kind": "records", "recent": list(history.recent), "matches": list(history.matches),
"notes": len(notes or [])}]
def _trace_end(self, via: str, channel: str | None, user: str, question: str, answer: str,
steps: list, began: float) -> None:
"""Keep how this answer was made (history.spec.md, "Traces"); never stops the answer."""
if self.worker.history is None:
return
try:
self.worker.history.add_trace(via, channel, user, question, answer, steps,
ms=round((time.time() - began) * 1000), ts=time.time())
except Exception as e: # noqa: BLE001 - a trace is a view, not part of the answer
debug_log(f"trace not kept: {type(e).__name__}", "llm")
return {"kind": "records", "recent": list(history.recent), "matches": list(history.matches),
"notes": len(notes or [])}
async def _task(self, rid: str, user: str, text: str, notes: list | None = None) -> None:
"""A scheduled task (schedule.spec.md, "Tasks"): answered in the chat lane with the read-only
tools, its own short-term memory and the person's history scope. It is not a turn of their
conversation, so their turn count and delete confirmations stay as they are."""
trace = _LiveTrace(self.worker.history, "task", None, user, text)
try:
await self._task_inner(rid, user, text, notes, trace)
finally:
trace.finish("") # only when it did not get to its answer
async def _task_inner(self, rid: str, user: str, text: str, notes: list | None, trace: _LiveTrace) -> None:
w = self.worker
answer = None
async with w.brain_people.setdefault(user, asyncio.Lock()), w.brain_lanes["chat"]:
@@ -644,14 +674,14 @@ class Session:
from jarvis.memory.history import HistoryContext
history = HistoryContext()
try:
steps = self._trace_start(history, notes)
trace.append(self._records_step(history, notes))
async with self._llm():
answer = await asyncio.to_thread(w.brain.reply, user, text, history, task=True,
notes=notes or [], trace=steps)
notes=notes or [], trace=trace)
except Exception as e: # the brain must never take the session down
debug_log(f"brain task {rid} failed: {type(e).__name__}", "llm")
answer = (answer or "").strip()
self._trace_end("task", None, user, text, answer, steps, now)
trace.finish(answer)
self.send({"type": "reply_text", "id": rid, "text": answer, "failed": not answer})
def _on_llm(self, msg: dict) -> None:
@@ -791,9 +821,11 @@ class Session:
async def _reply(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool = False,
channel: str | None = None, notes: list | None = None) -> None:
trace = _LiveTrace(self.worker.history, via, self.voice_channel if via == "voice" else channel, user, text)
try:
await self._reply_inner(rid, user, text, via, speak, early, channel, notes or [])
await self._reply_inner(rid, user, text, via, speak, early, channel, notes or [], trace=trace)
finally:
trace.finish("") # only when it did not get to its answer
self.thinking_replies.discard(rid)
self.cancelled_replies.discard(rid)
if not self._handed_over.pop(rid, False): # else _delivered counts it
@@ -830,7 +862,8 @@ class Session:
self._delivered(rid, ok)
async def _reply_inner(self, rid: str, user: str, text: str, via: str, speak: bool, early: bool,
chat_channel: str | None = None, notes: list | None = None) -> None:
chat_channel: str | None = None, notes: list | None = None, *,
trace: _LiveTrace) -> None:
w = self.worker
channel = self.voice_channel
# Person first, then lane: a reply waiting for its asker's other answer holds no lane.
@@ -858,7 +891,7 @@ class Session:
schedule = AppBridge(self, asyncio.get_running_loop(), "schedule", *origin)
notes_backend = AppBridge(self, asyncio.get_running_loop(), "note", *origin)
screen = AppBridge(self, asyncio.get_running_loop(), "screen", *origin)
steps = self._trace_start(history, notes)
trace.append(self._records_step(history, notes))
try:
async with self._llm(rid if speak else None):
w.answering += 1
@@ -866,7 +899,7 @@ class Session:
answer = await asyncio.to_thread(w.brain.reply, user, text, history, chat=chat,
schedule=schedule, notes=notes or [],
notes_backend=notes_backend, screen=screen,
trace=steps)
trace=trace)
finally:
w.answering -= 1
if not w.answering and w.scheduler:
@@ -875,7 +908,7 @@ class Session:
debug_log(f"brain reply {rid} failed: {type(e).__name__}", "llm")
answer = None
answer = (answer or "").strip()
self._trace_end(via, channel if via == "voice" else chat_channel, user, text, answer, steps, now)
trace.finish(answer)
if not answer and rid in self.tts_requests:
self._on_tts({"type": "tts", "id": rid, "text": "", "more": False}) # ends the notice's speech
self.send({"type": "reply_text", "id": rid, "text": answer, "failed": not answer})

View File

@@ -248,8 +248,11 @@ which carry Discord's own time.
still answering means the answer is not spoken: `reply_text` still comes, then `tts_done` with
`cancelled: true` and no audio. Every reply, a task's too and one that failed, leaves a trace of
how it was made (`history.spec.md`, "Traces"): the history lines and number of notes it was given,
then what the engine wrote down; a trace that cannot be stored never stops the answer. Traces older
than 30 days are removed when the worker starts. Each user keeps their own short-term dialogue
then what the engine wrote down. It is stored as soon as the request arrives (before it waits for
the asker's previous answer) and again as each step is added, then ends with the answer, or with
none when the reply fails, the session closes or the reply is cancelled; a trace that cannot be
stored never stops the answer. At start the worker ends the traces a killed run left unfinished
and removes those older than 30 days. Each user keeps their own short-term dialogue
memory (tool carry-over) across replies. The brain runs the reply engine with v1's single-user
memory off (`legacy_memory=False`: no warm profile, diary or graph), because several people share
this assistant and only the asker's scoped history may reach the prompt. It answers in Korean

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