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:
@@ -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(
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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})
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user