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; }