diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index 7665e9c..fa220ca 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -3427,6 +3427,30 @@ function staleTurnVerdict(proc, turn, now = Date.now()) { return { stale: false, reason: pendingCallCount(turn) ? "pending_tool" : "running" } } +// A detached turn the conversation went on without: after its last call in +// the client's history comes a tool round of calls the bridge never made (an +// id of another shape than its own). A sibling request that only leaves the +// turn's calls out (a side request, a compaction fork) is not this and still +// gets 409. A routing +// group that moved the conversation to another provider and failed back to +// agy-cli with that provider's tool results does this (#1051): no later +// request can answer the held turn, so waiting for three unmatched requests +// before replaying only costs the client two 409s. +function movedOnVerdict(turn, messages) { + if (!turn || turn.attached || turn.steering || turn.cancelRequested || !Array.isArray(messages)) return null + if (!pendingCallCount(turn)) return null + const own = new Set(turn.calls.map((call) => call.id)) + let last = -1 + messages.forEach((message, index) => { + if (Array.isArray(message?.content) && message.content.some((block) => + (block?.type === "tool_use" && own.has(block.id)) || (block?.type === "tool_result" && own.has(block.tool_use_id)))) last = index + }) + if (last < 0) return null + const foreign = messages.slice(last + 1).some((message) => message?.role === "assistant" && Array.isArray(message.content) && + message.content.some((block) => block?.type === "tool_use" && typeof block.id === "string" && block.id && !BRIDGE_CALL_ID.test(block.id))) + return foreign ? { stale: true, reason: "moved_on" } : null +} + // A pending tool turn only owns its own line of history, not a sibling request // that continues its base without carrying any of the emitted tool calls. function lineageVerdict(proc, turn, opts) { @@ -3522,7 +3546,7 @@ async function recoverStaleTurn(ctx) { return { kind: "conflict", turnId } } if (held.cancelRequested) return { kind: "fallthrough" } - const verdict = staleTurnVerdict(current, held) + const verdict = movedOnVerdict(held, ctx.body?.messages) ?? staleTurnVerdict(current, held) // Lineage supersession belongs to acquisition of a new prompt. Unmatched // results alone reclaim an unexpired owner only at the repeat limit. if (!verdict.stale && (!forced || verdict.reason === "attached" || verdict.reason === "steering")) { @@ -6553,7 +6577,7 @@ export const AntigravityBridgePlugin = async () => { } const held = pool.get(convKey) if (held?.turn && !held.turn.cancelRequested) { - const verdict = staleTurnVerdict(held, held.turn) + const verdict = movedOnVerdict(held.turn, body.messages) ?? staleTurnVerdict(held, held.turn) let attempts if (!verdict.stale) { // An attached stream is live progress; only a quiet turn counts. diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 1d864bf..037f376 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-agy-cli-auth", - "version": "0.1.73", + "version": "0.1.74", "description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI", "type": "module", "main": "./opencode.mjs", diff --git a/packages/agy-cli/test/repeat-batch.mjs b/packages/agy-cli/test/repeat-batch.mjs index ccc3089..72eaf84 100644 --- a/packages/agy-cli/test/repeat-batch.mjs +++ b/packages/agy-cli/test/repeat-batch.mjs @@ -185,6 +185,49 @@ test("A7: the third unmatched repeat against a quiet turn replays the request's }) }) +// A routing group served the held call's result on another provider, whose +// own tool round then failed back to agy-cli (#1051). +function movedOnRound(body, first, foreignId) { + const use = first.content.find((block) => block.type === "tool_use") + return { + ...body, + messages: [ + ...body.messages, + { role: "assistant", content: [use] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: use.id, content: "answered elsewhere" }] }, + { role: "assistant", content: [{ type: "text", text: "another provider's step" }, + { type: "tool_use", id: foreignId, name: "Read", input: { path: "/other" } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: foreignId, content: "FOREIGN RESULT" }] }, + ], + } +} + +test("A7: a turn the conversation went on without is replayed at the first unmatched request (#1051)", async () => { + await withKit({}, async (kit) => { + scriptedAgent(kit, { "FOREIGN RESULT": "replayed after the group came back" }) + const body = baseBody({ metadata: { user_id: "moved-on" }, messages: [{ role: "user", content: "TOOL start" }] }) + const first = await pendingToolTurn(kit, body) + const response = await kit.request(movedOnRound(body, first, "toolu_01AbCdEfGhIjKlMnOpQrStUv")) + assert.equal(response.status, 200, response.status === 200 ? "" : await response.text()) + const parsed = await readSse(response) + assert.equal(parsed.stopReason, "end_turn") + assert.equal(parsed.content.find((block) => block.type === "text")?.text, "replayed after the group came back") + assert.equal(kit.acpSpawns.length, 2, "the replay runs on a fresh process") + }) +}) + +test("A7: a later round with the bridge's own id shape is not taken as the conversation moving on (#1051)", async () => { + await withKit({}, async (kit) => { + scriptedAgent(kit) + const body = baseBody({ metadata: { user_id: "moved-on-own-shape" }, messages: [{ role: "user", content: "TOOL start" }] }) + const first = await pendingToolTurn(kit, body) + const response = await kit.request(movedOnRound(body, first, "toolu_0123456789abcdef01234567")) + assert.equal(response.status, 409) + await response.text() + assert.equal(kit.acpSpawns.length, 1) + }) +}) + test("A7: a third repeat that cannot be replayed ends in a terminal 400 with attempts and turn_id", async () => { await withKit({}, async (kit) => { scriptedAgent(kit)