agy-cli 0.1.74: a turn the conversation went on without is replayed at once, not after two 409s (#1051)

NieiR's logs on #1051 (agy-cli 0.1.69, agy 1.5.9, OpenCode through a
routing group of google, devin, neko and agy-cli): agy-cli left a turn
detached with 5 pending calls at 13:51:47, the group served the next 16
requests on google and neko, and at 14:07:36 google's 429 failed back
to agy-cli with google's own tool round at the tail. None of those ids
were agy's, the held turn was neither stale nor expired, so the request
got 409 "no active MCP call matches these tool results" twice and was
replayed only at the third try (NO_PROGRESS_REPEAT_LIMIT).

movedOnVerdict counts such a turn as stale ("moved_on") on the first
request: its calls are pending, at least one of its calls is in the
client's history, and after the last of them comes an assistant tool
round whose ids don't have the bridge's shape (toolu_ + 24 hex). It is
asked in the unmatched-results path and again under the lock in
recoverStaleTurn, so the reclaim and replay are the existing ones.

Left as before: a sibling request that only leaves the turn's calls out
(a side request, a compaction fork) still gets 409 and needs three, as
smoke.mjs's "unmatched results alone cannot supersede" and A7 check; a
later round with ids of the bridge's shape is not taken as moving on.

Run (mote, HOME=$(mktemp -d)):
- packages/agy-cli bun test (all suites, smoke included): 36 pass,
  0 fail; repeat-batch.mjs alone: 48 pass.
- bun scripts/check.mjs agy-cli: ok.

Broken by hand: origin/main's index.mjs with the new tests: the moved-on
request answers 409 (the first new test fails); the own-shape test
passes on both.

Not tried: a real routing group failing over to real agy.
This commit is contained in:
yetone
2026-11-16 13:37:11 +08:00
parent 369643183f
commit 3d9ac97f91
3 changed files with 70 additions and 3 deletions
+26 -2
View File
@@ -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.
+1 -1
View File
@@ -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",
+43
View File
@@ -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)