fix(agent): a model that writes its calls out hands the retry on

Agent check run on b33cf5d: a text-protocol model in the free chain wrote
update_matter and note_fact as JSON blocks and, corrected, answered
'(called web_search, web_search)'. Correcting the same model with no
format to use cannot work. Now a narrated answer moves the serving link
to the back of the chain for the rest of the turn, and the correction
alone carries TEXT_TOOL_PROTOCOL_HINT (in the system prompt it broke the
native callers). Calls written as JSON naming the tool count as narrated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Lem7GyhyyjKhmwjG8G7Ud
This commit is contained in:
Claude committed 2026-10-22 07:50:28 +00:00
1 parent b33cf5db8d
commit 7decfe84e5
4 files changed
+31 -6

No files matched your search

+3 -2
View File
@@ -19,8 +19,9 @@ brackets, it is the pull request to read for the detail.
- **The agent researches before it states the law.** A real question on the
live site was answered from memory: no search, no source, and the tool calls
it should have made written into the answer as text. Every answer is now
checked: one that cites law without a source, or describes calls instead of
making them, is sent back once; if it still fails it is shown marked
checked: one that cites law without a source, describes calls instead of
making them, or gives a deadline without recording it, is sent back once
(a model that wrote its calls out hands the retry to the next model); if it still fails it is shown marked
"unverified". {#no-unsourced-law}
## 2026-10-21
+16 -2
View File
@@ -5,6 +5,7 @@ import {
type ChatMessage,
type Env,
type Link,
TEXT_TOOL_PROTOCOL_HINT,
} from "@bitbaum/ai-kit";
import { byokChain, isByokConfig } from "@bitbaum/ai-kit/byok";
import {
@@ -587,13 +588,14 @@ async function runTurnLocked(input: {
let repaired = false;
let unverified = false;
let recorded = 0;
let chain = models.chain;
for (let round = 0; round < MAX_ROUNDS; round++) {
const last = round === MAX_ROUNDS - 1;
let turn;
try {
turn = await complete({
chain: models.chain,
chain,
env: models.env,
extraHeaders: models.extraHeaders,
health: models.own ? undefined : health,
@@ -634,9 +636,21 @@ async function runTurnLocked(input: {
});
if (problems.length && !repaired && round < MAX_ROUNDS - 2) {
repaired = true;
const narrated = problems.includes("narrated");
if (narrated) {
// Half the free chain cannot call tools natively. The model that just
// wrote its calls out goes to the back for the rest of the turn, and
// the correction alone carries the text protocol: in the system
// prompt it made native callers switch to text and fail (run 5).
const served = (l: Link) => `${l.provider.id}/${l.model}` === turn.id;
chain = [...chain.filter((l) => !served(l)), ...chain.filter(served)];
}
conversation.push(
{ role: "assistant", content: turn.text },
{ role: "user", content: repairPrompt(problems) },
{
role: "user",
content: narrated ? `${repairPrompt(problems)}\n\n${TEXT_TOOL_PROTOCOL_HINT}` : repairPrompt(problems),
},
);
continue;
}
+6
View File
@@ -23,6 +23,12 @@ describe("answer check", () => {
expect(answerProblems("(called web_search, read_page)", none)).toEqual(["narrated"]);
});
it("catches calls written as JSON, as one model did on 2026-10-22", () => {
const text = '**Matter update**\n```json\n{ "action": "update_matter", "title": "Einsprache" }\n```';
expect(answerProblems(text, none)).toEqual(["narrated"]);
expect(answerProblems('I used "web_search" earlier.', none)).toEqual([]);
});
it("catches a TOOL line left in the answer", () => {
expect(answerProblems('TOOL: web_search\nARGS: {"query":"x"}', none)).toContain("narrated");
});
+6 -2
View File
@@ -9,7 +9,8 @@
* no source. Every sentence looked finished. That is the fabrication path the
* product promises not to take, so it is caught here, mechanically:
*
* narrated — tool calls written out instead of made, or an answer that
* narrated — tool calls written out instead of made (as name(…), as a
* TOOL line, or as JSON naming the tool), or an answer that
* is only the loop's own note of a call ("(called web_search)"),
* which a model copies from the conversation as its answer
*
@@ -48,7 +49,10 @@ export function answerProblems(
): AnswerProblem[] {
const problems: AnswerProblem[] = [];
const names = turn.toolNames.map((n) => n.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|");
const narrated = new RegExp(`\\b(?:${names})\\s*\\(|^\\s*(?:\\*\\*)?TOOL(?:\\*\\*)?\\s*[:=]`, "m");
const narrated = new RegExp(
`\\b(?:${names})\\s*\\(|[:=]\\s*"(?:${names})"|^\\s*(?:\\*\\*)?TOOL(?:\\*\\*)?\\s*[:=]`,
"m",
);
if ((names && narrated.test(text)) || /^\s*\(called [\w, ]+\)\s*$/.test(text)) problems.push("narrated");
if (turn.sources === 0 && CITES_LAW.test(text)) problems.push("unsourced");
if (turn.deadlines === 0 && DEADLINE_WORD.test(text) && DATE.test(text)) problems.push("unrecorded");