From 2a4221ed0aaa7d2e89bc4995d01d69125f08a10f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 22 Oct 2026 16:42:25 +0000 Subject: [PATCH] fix(agent): keep the turn's real answer when a correction ends in a call note Agent check on 51ac8c7: the tax answer (a table with the deadline) was flagged for narrating a call; two corrections later the last reply was only '(called add_task)', so the person saw 'could not finish' although the deadline was recorded. The turn now keeps its best real answer, narration removed, and stores that when the last reply has nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018Lem7GyhyyjKhmwjG8G7Ud --- lib/matters/agent.test.ts | 15 +++++++++++++++ lib/matters/agent.ts | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/matters/agent.test.ts b/lib/matters/agent.test.ts index 9168e75..90f6533 100644 --- a/lib/matters/agent.test.ts +++ b/lib/matters/agent.test.ts @@ -330,3 +330,18 @@ describe("a runaway reply", () => { expect(model.seen[1]).toContain("the same call was already made in this reply"); }); }); + +describe("an answer lost to its correction", () => { + it("keeps the turn's real answer when the last reply is only a call note", async () => { + const id = await matter(); + const model = scriptedModel([ + { text: "Your objection is due 4 November 2026. update_matter(title)" }, + { text: "(called add_task)" }, + { text: "(called add_task)" }, + ]); + await runTurn({ db, master: MASTER, matterId: id, sub: OWNER, env: ENV, fetchImpl: model.impl, text: "When?" }); + const view = (await loadMatter(db, MASTER, id, OWNER))!; + const answer = view.events.filter((e) => e.kind === "message" && e.role === "assistant").at(-1); + expect(answer && answer.kind === "message" ? answer.text : "").toContain("Your objection is due 4 November 2026"); + }); +}); diff --git a/lib/matters/agent.ts b/lib/matters/agent.ts index c5abb98..6c3b4a5 100644 --- a/lib/matters/agent.ts +++ b/lib/matters/agent.ts @@ -637,6 +637,7 @@ async function runTurnLocked(input: { const readLaw = input.readLaw ?? readLawDefault; let servedBy = ""; let finalText = ""; + let bestAnswer = ""; let corrections = 0; let unverified = false; let recorded = 0; @@ -695,6 +696,10 @@ async function runTurnLocked(input: { deadlines: recorded + view.items.filter((i) => i.kind === "deadline").length, }); trace(problems); + // The best real answer of the turn so far, without any narration: what + // the person gets if a later correction ends in nothing but a call note. + const substance = withoutNarration(turn.text, TOOL_NAMES); + if (substance.length > bestAnswer.length) bestAnswer = substance; // Asking for a source is pointless once neither search nor Fedlex can // be reached; the answer is then stored marked unverified instead. const correctable = problems.filter((p) => canSearch || canReadLaw || p !== "unsourced"); @@ -720,7 +725,7 @@ async function runTurnLocked(input: { continue; } unverified = problems.includes("unsourced") || problems.includes("narrated"); - finalText = problems.includes("narrated") ? withoutNarration(turn.text, TOOL_NAMES) : turn.text; + finalText = problems.includes("narrated") ? substance || bestAnswer : turn.text; break; }