mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 10:44:52 +01:00
fix(runner): stop a turn that keeps resending a refused tool call and continue fresh (#21734)
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Native agent runs call Paperclip tools, and
`RepeatedToolFailureGuard` refuses an exact repeat of a call that
already failed validation twice
> - The refusals are not counted and never end the turn, so a model
whose host drops an argument can resend the same refused call until the
run's wall clock
> - In a recorded restart-chaos wave, an opencode/DeepSeek agent sent
the same `PATCH /api/issues/{id}` through `call_api` 431 times in 40
minutes without `body`; 427 were refused. The run held an agent slot and
delayed other tasks
> - The system knows the next step (stop the turn and continue on a
fresh session), so the harness must own it, with no Retry banner
> - This pull request counts refusals, stops the provider turn after a
bounded number with the classified cause `agent_tool_call_loop`, and
queues an automatic fresh-session continuation that tells the model to
use a dedicated tool
> - It also records the declared argument names that each opencode tool
call delivered, so we can tell whether the bridge or the model drops
`body`
> - The benefit is that a looping agent frees its slot in seconds
instead of 40 minutes, and the task continues by itself
## Linked Issues or Issue Description
**What happened?**
An opencode agent on DeepSeek created two subtasks and then tried to set
their descriptions with `call_api` `PATCH /api/issues/{id}`. Every call
arrived with `operationId`, `pathParams`, `query` and `responseText`,
but without `body`. The guard refused each repeat. The model said "I
need to include body" more than 20 times and kept sending the same call:
431 calls, 427 refused, over 40 minutes. The run stayed active and its
agent slot delayed other queued work.
**Expected behavior**
After a small, bounded number of refusals, Paperclip stops the provider
turn and continues the task automatically on a fresh session, with a
hint to use a dedicated Paperclip tool. The board shows no Retry banner
because the system owns this recovery.
**Steps to reproduce**
1. Run a native agent whose model resends one tool call with a missing
required argument.
2. The guard refuses the third and later identical calls.
3. On master, the refusals never end the turn.
**Paperclip version or commit**
`master` at 6400698a8e.
## What Changed
- `repeated-tool-failure-guard.ts`: counts refusals per exact call and
per run. At 6 refusals of one call, or 20 refusals in the run, it throws
`ToolCallLoopError` (`code: "agent_tool_call_loop"`, tool, counts and
argument names, never values). The message tells the model the turn
stops and gives a short hint.
- `paperclip-runner-tool-authority.ts`:
`scheduleToolCallLoopContinuation` writes one idempotent native
status-wake outbox row (key `agent_tool_call_loop:<runId>`) with
`forceFreshSession: true` and the hint as the wake instruction. The
outbox dispatcher admits it with `allowRunCoalescing: false`, so it
never merges into a deferred comment wake or the active run. An assignee
run uses `issue_continuation_needed`. A completion-review run uses
`native_completion_review` with its review assignment, which the
dispatcher revalidates, so the fresh reviewer session keeps the review.
- `native-session-executor.ts`: the session authority epoch queues that
continuation when the guard raises the loop error. The governed-wait
lookup finds the wake on the next tool event and ends the provider turn
as `yielded` with a `same_agent` continuation, the same way the
connection-tools refresh ends a turn. If the wake cannot be queued, the
refusal still reaches the model and the error is logged.
- `status-decision-committer.ts`: a yielded continuation with that key
binds to the queued wake instead of creating a second one.
- `status-arbiter.ts` and `native-run-finalizer.ts`: a pending review
run that ended through this continuation skips the
`native_review_unresolved` resume recovery, because the queued review
wake owns the next reviewer run.
- `mcp-bridge.ts` and `opencode-proxy-finish-diagnostics.ts`: the
opencode tool-delivery diagnostic records `argumentNames` (only names
declared in the tool's input schema, sorted, at most 1,024 characters of
JSON), `argumentNamesTruncated` and `undeclaredArgumentCount`. Values
and undeclared names are never recorded. The forwarder's closed key
allowlist accepts the new fields and validates them, so a record stays
under its 2,048-character limit.
### Threshold choice
- **6 refusals of one exact call.** The guard already runs a call twice
and then refuses with an explicit correction. Six more refusals (eight
identical attempts in total) give a model that reads errors many chances
to change its arguments. In the recorded run the model never changed
them in 427 refusals, so six refusals end that loop within seconds.
- **20 refusals in the run.** A model can alternate between a few
refused variants and keep each one under 6. Refusals happen only for
exact repeats of a certain validation failure, so a healthy run almost
never reaches 20.
### Previous-release story
The guard counters live in memory for one run's tool authority, as
before. A deploy or restart during a run starts the counters again at
zero on the new process; that only delays the stop. The continuation is
an ordinary wake row. If a server on the previous release finalizes a
run that a new server parked, it does not know the key and queues its
own `same_agent` continuation too; the issue execution lock lets one run
and the other coalesces.
## Verification
- Red then green, `repeated-tool-failure-guard.test.ts`: "after N
refusals the guard raises a turn-stopping loop error" and "stops the
turn once refusals across different calls reach the run limit". Both
fail on master.
- Red then green, `native-session-executor.test.ts`: "a turn whose model
repeats a refused call N times ends with agent_tool_call_loop and
schedules a continuation". With the epoch handling disabled it fails (13
calls instead of 8, nothing parked). Also "keeps the refused call's
error when the loop continuation cannot be queued".
- `paperclip-runner-tool-authority.test.ts`: "queues one fresh-session
continuation outbox for the assignee / a completion reviewer stopped by
a tool-call loop" (real Postgres; idempotent).
- `native-tool-call-loop-continuation.test.ts`: with a comment wake
already deferred behind the looping run, the continuation keeps its own
keyed deferred wake, the comment wake is unchanged, and after release a
run starts with `forceFreshSession` and the loop cause.
- `status-arbiter.test.ts`: a pending reviewer stopped by a loop
releases its checkout without a resume recovery.
- `native-status-arbiter-corpus.test.ts`: the existing wake-binding test
now also covers the `agent_tool_call_loop` key.
- `mcp-bridge-delivery.test.ts` and
`opencode-proxy-finish-diagnostics.test.ts`: argument names are
recorded, values and undeclared names are not, invalid records are
dropped, and a boundary test with 32 names of 64 characters stays
forwardable with `argumentNamesTruncated: true`.
- Server: the six touched test files pass (1755 tests). Runner:
`src/drivers/opencode` and `src/cli` pass (569 tests). `tsc --noEmit`
passes for `server` and `packages/paperclip-runner`.
- `pnpm verify` exits 2 (runner package changes need broad coverage), so
required CI supplies the broad run.
## Risks
- A run that hits the limit ends its turn early. The limits are far
above what a model that reads its errors needs, and the task continues
automatically on a fresh session.
- The fresh session loses the in-turn conversation. The task, its
comments, documents and workspace stay, and the continuation names the
cause and the hint.
- The diagnostic now carries declared argument names. These are schema
names, not values, and the forwarder validates the shape before it
forwards a record.
## Model Used
- Anthropic Claude Opus 5.5 (`claude-opus-5-5`), used through Claude
Code with tool use and code execution.
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
Refs #21360
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c7efca1eaf
commit
01c1e708e9
16 files changed
+493
-15
No files matched your search
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
||||
import { Agent, request } from "node:http";
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import { startOpenCodeMcpBridge, type OpenCodeMcpBridge } from "./mcp-bridge.js";
|
||||
import { createOpenCodeFinishDiagnosticForwarder } from "../../cli/opencode-proxy-finish-diagnostics.js";
|
||||
|
||||
const bridges: OpenCodeMcpBridge[] = [];
|
||||
const agents: Agent[] = [];
|
||||
@@ -88,6 +89,9 @@ it("answers each tool call, including a parallel pair on a pooled keep-alive cli
|
||||
socketRequestIndex: expect.any(Number),
|
||||
socketIdleMs: delivery.socketRequestIndex === 1 ? null : expect.any(Number),
|
||||
handlerMs: expect.any(Number),
|
||||
argumentNames: [],
|
||||
argumentNamesTruncated: false,
|
||||
undeclaredArgumentCount: 0,
|
||||
outcome: "result",
|
||||
written: true,
|
||||
});
|
||||
@@ -140,3 +144,56 @@ it("records a rejected call as an answered error", async () => {
|
||||
callIdSha256: createHash("sha256").update("11").digest("hex"), outcome: "error", written: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("records the declared argument names a call delivered, never their values or undeclared names", async () => {
|
||||
const messages: string[] = [];
|
||||
const bridge = await startOpenCodeMcpBridge({
|
||||
tools: [{ name: "call_api", inputSchema: { type: "object", properties: {
|
||||
operationId: { type: "string" }, pathParams: { type: "object" }, query: { type: "object" }, body: {}, responseText: { type: "object" },
|
||||
} } }],
|
||||
handler: async () => ({ ok: false, status: 400 }),
|
||||
onDiagnostic: message => { if (message.includes("tool-delivery-diagnostic")) messages.push(message); },
|
||||
});
|
||||
bridges.push(bridge);
|
||||
await (await fetch(bridge.url, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${bridge.secret}`, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ jsonrpc: "2.0", id: 12, method: "tools/call", params: { name: "call_api", arguments: {
|
||||
operationId: "PATCH /api/issues/{id}", pathParams: { id: "secret-issue" }, query: {}, responseText: { limitBytes: 8192 }, PRIVATE_FIELD: 1,
|
||||
} } }),
|
||||
})).json();
|
||||
await expect.poll(() => messages.length).toBe(1);
|
||||
expect(JSON.parse(messages[0]!)).toMatchObject({ argumentNames: ["operationId", "pathParams", "query", "responseText"],
|
||||
argumentNamesTruncated: false, undeclaredArgumentCount: 1, outcome: "result" });
|
||||
expect(messages[0]).not.toMatch(/PATCH|secret-issue|8192|PRIVATE_FIELD/);
|
||||
});
|
||||
|
||||
it("bounds many long argument names so the forwarder keeps the whole delivery record", async () => {
|
||||
const names = Array.from({ length: 32 }, (_, n) => `${String(n).padStart(2, "0")}${"a".repeat(62)}`);
|
||||
const messages: string[] = [];
|
||||
const bridge = await startOpenCodeMcpBridge({
|
||||
tools: [{ name: "wide_tool", inputSchema: { type: "object", properties: Object.fromEntries(names.map(name => [name, {}])) } }],
|
||||
handler: async () => ({ ok: true }),
|
||||
onDiagnostic: message => { if (message.includes("tool-delivery-diagnostic")) messages.push(message); },
|
||||
});
|
||||
bridges.push(bridge);
|
||||
await (await fetch(bridge.url, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${bridge.secret}`, "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ jsonrpc: "2.0", id: 13, method: "tools/call", params: { name: "wide_tool",
|
||||
arguments: Object.fromEntries(names.map(name => [name, 1])) } }),
|
||||
})).json();
|
||||
await expect.poll(() => messages.length).toBe(1);
|
||||
const record = JSON.parse(messages[0]!);
|
||||
expect(record.argumentNamesTruncated).toBe(true);
|
||||
expect(record.argumentNames.length).toBeGreaterThan(0);
|
||||
expect(record.argumentNames.length).toBeLessThan(names.length);
|
||||
expect(record.argumentNames).toEqual(names.slice(0, record.argumentNames.length));
|
||||
expect(JSON.stringify(record.argumentNames).length).toBeLessThanOrEqual(1_024);
|
||||
expect(messages[0]!.length).toBeLessThanOrEqual(2_048);
|
||||
const frames: unknown[] = [];
|
||||
const forward = createOpenCodeFinishDiagnosticForwarder(frame => frames.push(frame));
|
||||
forward.bind("session");
|
||||
forward.diagnostic(messages[0]!);
|
||||
expect(frames).toHaveLength(1);
|
||||
});
|
||||
@@ -65,6 +65,8 @@ const MAX_RESULT_BYTES = 1_048_576;
|
||||
const TOOL_DELIVERY_DIAGNOSTIC_SCHEMA = "paperclip.opencode.tool-delivery-diagnostic/v1";
|
||||
const MAX_TOOL_DELIVERY_DIAGNOSTICS = 256;
|
||||
const MAX_TOOL_INPUT_COERCION_DIAGNOSTICS = 64;
|
||||
const MAX_TOOL_DELIVERY_ARGUMENT_NAMES_CHARS = 1_024;
|
||||
const TOOL_DELIVERY_ARGUMENT_NAME = /^[A-Za-z0-9_.-]{1,64}$/;
|
||||
|
||||
type SocketUse = { requests: number; lastResponseAt: number | null };
|
||||
type ToolDelivery = {
|
||||
@@ -73,6 +75,7 @@ type ToolDelivery = {
|
||||
receivedAt: number;
|
||||
};
|
||||
type ToolDeliveryOutcome = "result" | "error" | "unanswered";
|
||||
type ToolDeliveryInput = { argumentNames: string[] | null; argumentNamesTruncated: boolean; undeclaredArgumentCount: number };
|
||||
|
||||
export function canonicalOpenCodeMcpToolName(name: string): string {
|
||||
const trimmed = name.trim();
|
||||
@@ -131,11 +134,14 @@ export async function startOpenCodeMcpBridge(
|
||||
delivery: ToolDelivery,
|
||||
outcome: ToolDeliveryOutcome,
|
||||
written: boolean,
|
||||
input?: ToolDeliveryInput,
|
||||
) => {
|
||||
if (!options.onDiagnostic || deliveryDiagnostics++ >= MAX_TOOL_DELIVERY_DIAGNOSTICS) return;
|
||||
const diagnostic = {
|
||||
schema: TOOL_DELIVERY_DIAGNOSTIC_SCHEMA,
|
||||
callIdSha256: createHash("sha256").update(callId).digest("hex"),
|
||||
...(input ? { argumentNames: input.argumentNames, argumentNamesTruncated: input.argumentNamesTruncated,
|
||||
undeclaredArgumentCount: input.undeclaredArgumentCount } : {}),
|
||||
socketRequestIndex: delivery.socketRequestIndex,
|
||||
socketIdleMs: delivery.socketIdleMs,
|
||||
handlerMs: Date.now() - delivery.receivedAt,
|
||||
@@ -200,6 +206,18 @@ export async function startOpenCodeMcpBridge(
|
||||
};
|
||||
}
|
||||
|
||||
function boundedArgumentNames(names: string[] | null): Pick<ToolDeliveryInput, "argumentNames" | "argumentNamesTruncated"> {
|
||||
if (!names) return { argumentNames: null, argumentNamesTruncated: false };
|
||||
const kept: string[] = [];
|
||||
let size = 2;
|
||||
for (const name of [...names].sort()) {
|
||||
size += JSON.stringify(name).length + (kept.length ? 1 : 0);
|
||||
if (size > MAX_TOOL_DELIVERY_ARGUMENT_NAMES_CHARS) return { argumentNames: kept, argumentNamesTruncated: true };
|
||||
kept.push(name);
|
||||
}
|
||||
return { argumentNames: kept, argumentNamesTruncated: false };
|
||||
}
|
||||
|
||||
function inputShape(input: unknown): "omitted" | "null" | "array" | "object" | "string" | "number" | "boolean" | "other" {
|
||||
if (input === undefined) return "omitted";
|
||||
if (input === null) return "null";
|
||||
@@ -250,7 +268,7 @@ async function handleRequest(
|
||||
response: ServerResponse,
|
||||
context: {
|
||||
delivery: ToolDelivery;
|
||||
diagnoseToolDelivery: (callId: string, delivery: ToolDelivery, outcome: ToolDeliveryOutcome, written: boolean) => void;
|
||||
diagnoseToolDelivery: (callId: string, delivery: ToolDelivery, outcome: ToolDeliveryOutcome, written: boolean, input?: ToolDeliveryInput) => void;
|
||||
secret: string;
|
||||
tools: OpenCodeMcpToolDefinition[];
|
||||
admittedTools: OpenCodeMcpToolDefinition[];
|
||||
@@ -345,8 +363,9 @@ async function handleRequest(
|
||||
// client that never consumes a written answer can be told apart from a
|
||||
// bridge that never answered it.
|
||||
let outcome: ToolDeliveryOutcome = "unanswered";
|
||||
let deliveredInput: ToolDeliveryInput | undefined;
|
||||
response.once("close", () => {
|
||||
context.diagnoseToolDelivery(callId, context.delivery, outcome, response.writableFinished);
|
||||
context.diagnoseToolDelivery(callId, context.delivery, outcome, response.writableFinished, deliveredInput);
|
||||
});
|
||||
const rawName = typeof params.name === "string" ? params.name : "";
|
||||
// A declared operation may itself start with paperclip_. Preserve that
|
||||
@@ -361,6 +380,14 @@ async function handleRequest(
|
||||
return;
|
||||
}
|
||||
const received = params.arguments ?? {};
|
||||
const declaredSchema = context.admittedTools.find(entry => entry.name === tool)?.inputSchema.properties;
|
||||
const declaredArguments = isRecord(declaredSchema) ? declaredSchema : {};
|
||||
const receivedNames = isRecord(received) ? Object.keys(received) : null;
|
||||
const declaredNames = receivedNames?.filter(name => Object.hasOwn(declaredArguments, name) && TOOL_DELIVERY_ARGUMENT_NAME.test(name)) ?? null;
|
||||
deliveredInput = {
|
||||
...boundedArgumentNames(declaredNames),
|
||||
undeclaredArgumentCount: (receivedNames?.length ?? 0) - (declaredNames?.length ?? 0),
|
||||
};
|
||||
const validator = context.validators.get(tool);
|
||||
const admitted = admitToolInput(validator, received);
|
||||
const { args, schemaValid } = admitted;
|
||||
|
||||
Reference in new issue
Block a user