From 4a64252bd7c85c9fd94a38e5ff0ee82166606352 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 15 Nov 2026 04:31:05 +0800 Subject: [PATCH] agy-cli 0.1.51: one opening sent to several models at once gets an agy each swang20201 on X: Test models on an agy-cli account gave a check for the first model and a cross for the other three. magpie's Test sends the same "hi" opening to up to four models at once. agy-cli keys its agy process by user, system and first message, not the model, so all four shared one key. The first started a turn; the others found it unfinished and got 503 "agy bridge: this conversation still has an unfinished response". Not the account's limits, and not the model ids. Two changes: - A fresh opening that finds the key's process busy with a turn for another model gets a process of its own (key + request id). The same model sent twice while the first runs is still refused as a resend, so smoke's "simultaneous startup allows only one live response" holds. - acquireProc starts the turn (begin) inside the same locks, so a request waiting on them sees the earlier turn and is never handed a process about to be busy. The turn records its model. Run: - New test in test/repeat-batch.mjs, magpie's exact probe body to four models at once: red on origin/main 9126c46f (1 x 200, 3 x 503), green after. - Broken by hand: with begin moved back outside the lock the new test is red again. - packages/agy-cli bun test under a temp HOME on mote: 21/21, smoke included. - Whole repo, HOME=$(mktemp -d) bun test and bun scripts/check.mjs on mica at this tree: 8849 pass, 0 fail (8869 tests, 617 files), check.mjs ok. - Real agy 1.5.9 (agy acp), four models at once through the plugin: old code 1 x 200 + 3 x 503; this code 4 x 200 at 3.7, 6.0, 9.1 and 11.2s, inside magpie's 20s test wait. Not tried: the reporter's models (Gemini 4 Argon/Hydrogen, Claude 5.5). The account used offers Gemini 3.8 Flash, Claude 4.6 and GPT-OSS only, so the model id mapping wasn't checked against a list holding those ids. --- packages/agy-cli/index.mjs | 29 ++++++++++++++++----- packages/agy-cli/package.json | 2 +- packages/agy-cli/test/repeat-batch.mjs | 35 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index c552d24..44a776d 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -1938,6 +1938,7 @@ class Proc { throw new UnsupportedContent("image input with this agy ACP server; update to an image-capable agy version") } const turn = new Turn(this, messages) + turn.model = model this.turn = turn this.expectedMessages = messages.map((message) => ({ ...message })) this.historyInvalid = false @@ -2835,7 +2836,9 @@ async function withAcquireLocks(keys, fn) { return run(0) } -async function acquireProc(agyPath, convKey, opts, isFreshConversation, forceNew = false) { +// begin, when given, starts the turn inside the same locks, so a request that +// waits on them sees this one's turn and is never handed a process about to be busy. +async function acquireProc(agyPath, convKey, opts, isFreshConversation, forceNew = false, begin = null) { // Discover a live continued session's current key, lock it together with // the new key (sorted, to avoid deadlock), then pick again inside those // locks. A process is re-keyed only when its current key is already held. @@ -2849,7 +2852,8 @@ async function acquireProc(agyPath, convKey, opts, isFreshConversation, forceNew const needed = candidate?.convKey if (needed && !locked.has(needed) && attempt < 2) return { retry: needed } } - return { value: await acquireProcLocked(agyPath, convKey, opts, isFreshConversation, forceNew, locked) } + const value = await acquireProcLocked(agyPath, convKey, opts, isFreshConversation, forceNew, locked) + return { value: begin ? { ...value, turn: await begin(value.proc, value.history) } : value } }) if (outcome.retry) { extra = outcome.retry @@ -2893,6 +2897,19 @@ async function acquireProcLocked(agyPath, convKey, opts, isFreshConversation, fo if (candidate && lockedKeys.has(candidate.convKey)) existing = rekeyProc(candidate, convKey) } let why = "no_live" + let key = convKey + if (existing && !existing.dead && existing.turn && isFreshConversation && !forceNew && + !opts.hasToolResults && !existing.turn.cancelRequested && existing.turn.model !== opts.model) { + // A new conversation that opens as a running one did (one system prompt and + // one first message), for another model, is a conversation of its own, not + // a resend of the running one: magpie's Test models sends every model the + // same "hi" at once (swang20201 on X). It gets an agy of its own, under a + // key of its own; its follow-ups key as the first. The same request for + // the same model while the first runs is still refused as a resend. + key = `${convKey}#${opts.reqId || randomBytes(4).toString("hex")}` + existing = null + why = "concurrent_opening" + } if (existing) { if (!existing.dead && existing.turn) { throw new BusyError("agy bridge: this conversation still has an unfinished response") @@ -2936,14 +2953,14 @@ async function acquireProcLocked(agyPath, convKey, opts, isFreshConversation, fo } } const resume = forceNew || isFreshConversation ? null : takeParked(convKey, opts) - const proc = new Proc(agyPath, convKey, { ...opts, resume }) + const proc = new Proc(agyPath, key, { ...opts, resume }) try { await proc.start() } catch (error) { await proc.stop().catch(() => {}) throw error } - pool.set(convKey, proc) + pool.set(key, proc) // Why this request got a new agy session, so a cache that never hits can be // traced to the check that failed (#1058). Counts only, never content. logSessionEvent(proc, why, { @@ -5984,14 +6001,14 @@ export const AntigravityBridgePlugin = async () => { } } - const { proc, history } = await acquireProc( + const { proc, turn } = await acquireProc( agy, convKey, acquireOpts, isFresh, resultBlocks.length > 0 && !keptSession, + begin, ) - const turn = await begin(proc, history) return streamResponse(proc, turn, body, init.signal) } catch (error) { return jsonError(error.statusCode ?? 502, error.message, error.code, { noRetry: error.noRetry }) diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index a51f852..15d2095 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.50", + "version": "0.1.51", "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 a48b28d..d7a93ee 100644 --- a/packages/agy-cli/test/repeat-batch.mjs +++ b/packages/agy-cli/test/repeat-batch.mjs @@ -985,3 +985,38 @@ test("Lmohn39: a try that fails another way still waits for agy's next one", asy assert.equal(replyText(parsed), "answer") }) }) + +// ---------------------------------------------------------------- one opening, several models at once + +// magpie's "Test models" sends every model the same smallest request, four at a +// time: {"model":…,"max_tokens":16,"messages":[{"role":"user","content":"hi"}]}. +// Those share one conversation key, so all but the first were turned away with +// "this conversation still has an unfinished response" (swang20201 on X: one ✓, +// three ✗). Each is a conversation of its own and gets an agy of its own. +test("the same opening sent to several models at once is answered for each", async () => { + const ids = ["claude-opus-5-5", "gemini-4-argon", "claude-sonnet-5-5", "gemini-3.8-flash"] + await withKit({ offeredModels: [ + ["gemini-4-argon-high", "Gemini 4 Argon (High)"], ["gemini-3.8-flash-high", "Gemini 3.8 Flash (High)"], + ["claude-opus-5-5-high", "Claude Opus 5.5 (High)"], ["claude-sonnet-5-5-high", "Claude Sonnet 5.5 (High)"], + ] }, async (kit) => { + let arrived = 0 + let release + const allIn = new Promise((resolve) => { release = resolve }) + kit.defaultPrompt = async (agent) => { + // every prompt is held until the last one is in: they overlap, as on a real agy + if (++arrived === ids.length) release() + await Promise.race([allIn, sleep(2_000)]) + await agent.text(`answer ${agent.index}`) + return { stopReason: "end_turn" } + } + const responses = await Promise.all(ids.map((model) => + kit.request({ model, max_tokens: 16, messages: [{ role: "user", content: "hi" }] }))) + const results = await Promise.all(responses.map(async (response) => + response.status === 200 ? readSse(response) : { status: response.status, text: await response.text() })) + for (const [i, result] of results.entries()) { + assert.equal(result.status, undefined, `${ids[i]}: ${result.text}`) + assert.equal(result.stopReason, "end_turn", ids[i]) + } + assert.equal(arrived, ids.length) + }) +})