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:
@@ -4,6 +4,7 @@ registered chat channel messages, searched per asker. Contract:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
import sqlite3
|
||||
@@ -41,6 +42,19 @@ CREATE TABLE IF NOT EXISTS presence (
|
||||
left_at REAL
|
||||
);
|
||||
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
|
||||
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,
|
||||
steps TEXT NOT NULL -- JSON list (history.spec.md, "Traces")
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS traces_ts ON traces(ts);
|
||||
CREATE INDEX IF NOT EXISTS traces_user ON traces(user_id);
|
||||
"""
|
||||
|
||||
_WORD = re.compile(r"\w{2,}", re.UNICODE)
|
||||
@@ -70,6 +84,21 @@ class Record:
|
||||
text: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Trace:
|
||||
"""How one answer was made: never read back into a prompt (``history.spec.md``, "Traces")."""
|
||||
|
||||
id: int
|
||||
ts: float
|
||||
via: str
|
||||
channel_id: Optional[str]
|
||||
user_id: str
|
||||
question: str
|
||||
answer: str
|
||||
ms: int
|
||||
steps: list
|
||||
|
||||
|
||||
@dataclass
|
||||
class HistoryContext:
|
||||
"""Lines for the prompt: the recent window and the search matches, kept apart for labelling."""
|
||||
@@ -196,6 +225,29 @@ 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:
|
||||
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)))
|
||||
self._conn.commit()
|
||||
return int(cur.lastrowid)
|
||||
|
||||
def traces_of(self, person: str) -> List[Trace]:
|
||||
with self._lock:
|
||||
rows = self._conn.execute(
|
||||
"SELECT id, ts, via, channel_id, user_id, question, answer, ms, steps FROM traces"
|
||||
" WHERE user_id=? ORDER BY ts, id", (person,)).fetchall()
|
||||
return [Trace(**{**dict(r), "steps": json.loads(r["steps"])}) for r in rows]
|
||||
|
||||
def purge_traces_older_than(self, ts: float) -> int:
|
||||
with self._lock:
|
||||
n = self._conn.execute("DELETE FROM traces WHERE ts < ?", (ts,)).rowcount
|
||||
self._conn.commit()
|
||||
return n
|
||||
|
||||
def records_of(self, person: str) -> List[Record]:
|
||||
with self._lock:
|
||||
rows = self._conn.execute(
|
||||
@@ -207,6 +259,7 @@ class HistoryStore:
|
||||
with self._lock:
|
||||
n = self._conn.execute("DELETE FROM history WHERE author_id=? OR to_id=?", (person, person)).rowcount
|
||||
self._conn.execute("DELETE FROM presence WHERE user_id=?", (person,))
|
||||
self._conn.execute("DELETE FROM traces WHERE user_id=?", (person,))
|
||||
self._conn.commit()
|
||||
debug_log(f"history: forgot a person ({n} records)", "memory")
|
||||
|
||||
@@ -214,6 +267,7 @@ class HistoryStore:
|
||||
with self._lock:
|
||||
n = self._conn.execute("DELETE FROM history WHERE ts < ?", (ts,)).rowcount
|
||||
self._conn.execute("DELETE FROM presence WHERE left_at IS NOT NULL AND left_at < ?", (ts,))
|
||||
self._conn.execute("DELETE FROM traces WHERE ts < ?", (ts,))
|
||||
self._conn.commit()
|
||||
debug_log(f"history: retention removed {n} records", "memory")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user