fix(agent): a runaway reply runs 8 calls, a turn 12 law reads; point the agent at read_law

Agent check on d13d436: one model reply asked for read_law ~150 times and
the turn failed; in the tax case the model searched instead of reading
Zürich's StG and answered too late to be corrected.

- One reply runs at most 8 tool calls; the rest, and any exact repeat,
  are answered 'not run' so the model can ask again.
- Per turn: read_law 12, web_search 8, read_page 8 (with the writes).
- The Swiss and Liechtenstein market notes now tell the agent read_law
  reads federal, cantonal and Liechtenstein acts, with an example.

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 16:30:15 +00:00
1 parent d13d436c36
commit 51ac8c7d44
4 files changed
+48 -7

No files matched your search

+2 -2
View File
@@ -77,7 +77,7 @@ const SWITZERLAND: Market = {
"CH-GE": ["Geneva", "Genf", "Ginevra"],
},
agentNote:
"Switzerland: federal law and the law of the 26 cantons differ; a cantonal tax, permit or procedure is governed by the canton's law, not only by the federal act. Official texts are on fedlex.admin.ch and on each canton's own collection.",
"Switzerland: federal law and the law of the 26 cantons differ; a cantonal tax, permit or procedure is governed by the canton's law, not only by the federal act. read_law reads both from the official texts: a federal act by its abbreviation (OR, DBG), a cantonal act with the canton and its abbreviation, number or title (canton ZH, act StG, § 140).",
};
const LIECHTENSTEIN: Market = {
@@ -85,7 +85,7 @@ const LIECHTENSTEIN: Market = {
regions: {},
languages: ["de"],
agentNote:
"Liechtenstein has its own legal order (official texts on gesetze.li), close to Swiss and Austrian law in places and part of the EEA: do not apply Swiss or Austrian law there unless a Liechtenstein source says it applies.",
"Liechtenstein has its own legal order, close to Swiss and Austrian law in places and part of the EEA: do not apply Swiss or Austrian law there unless a Liechtenstein source says it applies. read_law reads its acts from gesetze.li (ABGB, PGR, LV, SteG…).",
};
/** The markets that are switched on, in the order they are shown. */
+3 -2
View File
@@ -144,7 +144,8 @@ describe("text from outside is data, not instructions", () => {
"note_fact",
{ fact: `fact ${i}` },
]);
const model = scriptedModel([{ tools: flood }, { text: "Done." }]);
// Over two replies: one reply runs at most 8 calls, the turn's limit is 10.
const model = scriptedModel([{ tools: flood.slice(0, 8) }, { tools: flood.slice(8) }, { text: "Done." }]);
await runTurn({
db,
master: MASTER,
@@ -157,7 +158,7 @@ describe("text from outside is data, not instructions", () => {
expect((await loadMatter(db, MASTER, id, OWNER))!.core.facts).toHaveLength(
10,
);
expect(model.seen[1]).toContain("limit reached");
expect(model.seen[2]).toContain("limit reached");
});
});
+22
View File
@@ -308,3 +308,25 @@ describe("reading the law", () => {
expect(view.events.some((e) => e.kind === "step" && e.tool === "unverified")).toBe(false);
});
});
describe("a runaway reply", () => {
it("runs at most 8 calls of one reply, and a repeated call once", async () => {
const id = await matter();
let reads = 0;
const model = scriptedModel([
{ tools: Array.from({ length: 30 }, () => ["read_law", { act: "OR", article: "270b" }] as [string, unknown]) },
{ text: "Done [1]." },
]);
await runTurn({
db, master: MASTER, matterId: id, sub: OWNER, env: ENV, fetchImpl: model.impl,
readLaw: async () => {
reads++;
return { cite: "OR (SR 220)", collection: "Fedlex", result: { ok: true, article: { heading: "Art. 270b", text: "x", url: "https://www.fedlex.admin.ch/x", inForceSince: "2026-10-01" } } };
},
text: "Rent?",
});
expect(reads).toBe(1);
expect(model.seen[1]).toContain("at most 8 tool calls are run from one reply");
expect(model.seen[1]).toContain("the same call was already made in this reply");
});
});
+21 -3
View File
@@ -77,6 +77,8 @@ const health = createHealthTracker();
const MAX_ROUNDS = 8;
/** Corrections per turn (answer-check.ts); each goes to the next model in the chain. */
const MAX_CORRECTIONS = 2;
/** Tool calls run from one model reply; the rest are answered "not run" so the model can ask again. */
const MAX_CALLS_PER_ROUND = 8;
const HISTORY = 30;
const DOC_BUDGET = 24_000;
@@ -387,8 +389,16 @@ export function untrusted(source: string, text: string): string {
/** Results whose text comes from outside Causius, and so can carry instructions. */
const UNTRUSTED_RESULTS = new Set(["web_search", "read_page", "read_law", "read_document"]);
/** At most this many writes of each kind in one turn: an injected loop cannot flood a matter. */
/**
* At most this many calls of each kind in one turn: an injected loop cannot
* flood a matter, and a model cannot read the same statute a hundred times
* (Agent check, 2026-10-22: one round asked for read_law ~150 times and the
* turn failed).
*/
const WRITE_LIMIT: Partial<Record<string, number>> = {
read_law: 12,
web_search: 8,
read_page: 8,
note_fact: 10,
record_deadline: 8,
add_task: 8,
@@ -716,8 +726,16 @@ async function runTurnLocked(input: {
trace([]);
const results: string[] = [];
for (const call of turn.toolCalls) {
const result = await runTool(call.name, call.args);
const ran = new Set<string>();
for (const [i, call] of turn.toolCalls.entries()) {
const key = `${call.name}:${call.args}`;
const result =
i >= MAX_CALLS_PER_ROUND
? `not run: at most ${MAX_CALLS_PER_ROUND} tool calls are run from one reply; call again for what is still needed`
: ran.has(key)
? "not run: the same call was already made in this reply; its result is above"
: await runTool(call.name, call.args);
ran.add(key);
const shownResult = hide(result);
results.push(
`[tool ${call.name}] ${UNTRUSTED_RESULTS.has(call.name) ? untrusted(call.name, shownResult) : shownResult}`,