feat(brain): keep how each answer was made (records, tools, plan, thinking, tool calls)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-16 03:00:10 +09:00
parent 2a140d8490
commit a20ebe4eae
8 changed files with 265 additions and 8 deletions

View File

@@ -51,6 +51,8 @@ class _LlmPaused(Exception):
# The asker's standing instructions the app sends with a reply (personal_notes.spec.md).
MAX_NOTES = 20
MAX_NOTE_CHARS = 300
# How long answer traces stay (history.spec.md, "Traces"); removed when the worker starts.
TRACE_KEEP_DAYS = 30
class VoiceWorker:
@@ -116,6 +118,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.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")
if self.transcriber is not None:
self.scheduler = SttScheduler(self.transcriber, partials_held=self._partials_held)
self.scheduler.start()
@@ -603,6 +608,23 @@ class Session:
waiting[user] = waiting.get(user, 0) + 1
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:
"""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")
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
@@ -622,12 +644,14 @@ class Session:
from jarvis.memory.history import HistoryContext
history = HistoryContext()
try:
steps = self._trace_start(history, notes)
async with self._llm():
answer = await asyncio.to_thread(w.brain.reply, user, text, history, task=True,
notes=notes or [])
notes=notes or [], trace=steps)
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()
answer = (answer or "").strip()
self._trace_end("task", None, user, text, answer, steps, now)
self.send({"type": "reply_text", "id": rid, "text": answer, "failed": not answer})
def _on_llm(self, msg: dict) -> None:
@@ -834,13 +858,15 @@ 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)
try:
async with self._llm(rid if speak else None):
w.answering += 1
try:
answer = await asyncio.to_thread(w.brain.reply, user, text, history, chat=chat,
schedule=schedule, notes=notes or [],
notes_backend=notes_backend, screen=screen)
notes_backend=notes_backend, screen=screen,
trace=steps)
finally:
w.answering -= 1
if not w.answering and w.scheduler:
@@ -848,7 +874,8 @@ class Session:
except Exception as e: # the brain must never take the session down
debug_log(f"brain reply {rid} failed: {type(e).__name__}", "llm")
answer = None
answer = (answer or "").strip()
answer = (answer or "").strip()
self._trace_end(via, channel if via == "voice" else chat_channel, user, text, answer, steps, now)
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})