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

@@ -29,8 +29,10 @@ class FakeBrain:
self.calls = []
def reply(self, user, text, history, chat=None, schedule=None, task=False, notes=None, notes_backend=None,
screen=None):
screen=None, trace=None):
self.calls.append((user, text, history))
if trace is not None:
trace.append({"kind": "tools", "names": ["getWeather"]})
self.chat, self.schedule, self.task = chat, schedule, task
self.notes, self.notes_backend, self.screen = notes, notes_backend, screen
return self.answer(user, text)
@@ -211,6 +213,37 @@ def test_a_failed_brain_answer_is_reported(store, answer):
assert seen[-1] == {"type": "reply_text", "id": "r1", "text": "", "failed": True}
@pytest.mark.unit
def test_each_answer_keeps_how_it_was_made_with_the_records_it_was_given(store):
async def body(url):
async with connect(url) as ws:
await hello(ws, stt={"partials": False})
await send(ws, type="voice_join", channel="vc1")
await send(ws, type="member_join", user="100", name="Alice")
await say(ws, "100", 0.9)
await send(ws, type="reply", id="r1", user="100", text="what did I say", via="voice", speak=False,
notes=["반말로 해"])
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "r1")
await send(ws, type="reply", id="c1", user="100", text="hi", via="chat", speak=False, channel="ch1")
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "c1")
await send(ws, type="reply", id="t1", user="100", text="task", via="task")
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("id") == "t1")
brain = FakeBrain(lambda u, t: None if t == "hi" else f"answer for {t}")
asyncio.run(with_worker(body, store=store, brain=brain))
voice, chat, task = store.traces_of("100")
assert (voice.via, voice.channel_id, voice.question, voice.answer) == \
("voice", "vc1", "what did I say", "answer for what did I say")
assert voice.ms >= 0
records = voice.steps[0]
assert records["kind"] == "records" and records["notes"] == 1
assert any("Alice: level 0.9" in x for x in records["recent"])
assert voice.steps[1:] == [{"kind": "tools", "names": ["getWeather"]}]
# A failed answer is kept too, with no answer; a task has no channel.
assert (chat.via, chat.channel_id, chat.answer) == ("chat", "ch1", "")
assert (task.via, task.channel_id, task.answer) == ("task", None, "answer for task")
@pytest.mark.unit
def test_without_a_brain_reply_is_an_error():
async def body(url):
@@ -300,6 +333,21 @@ def test_presence_left_open_by_a_previous_run_is_closed_at_start(store):
assert store.recent("old", now=4_000_000_000, scope=Scope(), minutes=10 ** 9) == []
@pytest.mark.unit
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)
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"]
@pytest.mark.unit
def test_the_brain_prints_no_text_from_its_thread(tmp_path, monkeypatch, capfd):
pytest.importorskip("mcp") # the real brain needs the jarvis dependencies (--brain)