diff --git a/packages/agy-cli/README.en.md b/packages/agy-cli/README.en.md index fb06caa..45cdc66 100644 --- a/packages/agy-cli/README.en.md +++ b/packages/agy-cli/README.en.md @@ -375,6 +375,15 @@ off unless you turn it on. account's own quota is used up) ends the turn as a rate limit (`rate_limit_error`, HTTP 429 without streaming) carrying agy's words and reset, so magpie rests the account until then and asks the next one. +- **A reply with only thinking, no text and no tool call** (billion-context's + "the turn degenerated again after the continuation nudge"): when one of the + model's replies comes back empty, agy writes "⚠ Model produced invalid + output." and tries again; when that try is empty too, agy ends the turn as + finished. The plugin doesn't hand such an ending over as the reply: it asks + once more in the same session, where the model still has the whole context. + Still empty, the turn ends with an error, so magpie asks the next account, + and the client sending the request again continues in that session. An + empty ending with no thinking either is returned as it is. - **HTTP 409 saying tool results do not match the history**: usually the client rewrote the history or tool IDs mid-conversation. When the plugin can work out the correct match it rebuilds the session; when it cannot, it refuses rather diff --git a/packages/agy-cli/README.md b/packages/agy-cli/README.md index 0a2bb7f..5b09746 100644 --- a/packages/agy-cli/README.md +++ b/packages/agy-cli/README.md @@ -266,6 +266,12 @@ agy 自己的系统提示词,只剩插件的一行工具说明和客户端的 结尾是 "⚠ Individual quota reached. … Resets in 1h18m40s."(这个账号自己的额度用完)时, 按额度不足(`rate_limit_error`,非流式为 HTTP 429)结束本轮,原文和重置时间一并带上, magpie 据此让这个账号歇到重置,并改问下一个账号。 +- **回复只有思考、没有正文也没有工具调用**(billion-context 报 "the turn degenerated + again after the continuation nudge"):模型的一次回复空了时,agy 会写一行 + "⚠ Model produced invalid output." 并重试;重试也空时,agy 以正常结束收尾。插件不把这样 + 的空结尾当作回复交出去,而是在同一个会话里再问一次(模型手上还是完整的上下文); + 还是空的话以错误结束本轮,magpie 会改问下一个账号,客户端重发同一请求也会接着这个会话。 + 一个连思考都没有的空结尾照常返回。 - **HTTP 409,提示工具结果与历史对不上**:通常是客户端在对话中途改写了历史或工具 ID。 能确定对应关系时插件会重建会话;无法确定时直接拒绝,不做猜测。 Hermes Agent 会把一次批量的 `tool_call({"calls": [...]})` 在自己的历史里拆成每项一次调用, diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index a15831d..1bd7606 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -162,6 +162,14 @@ const refused429 = (detail) => /code 429\b/i.test(detail) && /resource has been // It is agy's, not the model's, and the turn goes on (#1124). const RETRY_MARKER = "\n⚠ Model produced invalid output.\n" const RETRY_CORE = RETRY_MARKER.trim() +// When agy's retry comes back empty too, agy ends the turn on end_turn with +// nothing but thinking, and a client takes that as a finished reply that +// said nothing: billion-context's "the turn degenerated again after the +// continuation nudge" (fnckerpoi on X, DSH through billion-context, 11-18). +// The bridge asks once more in the same session, where the model still has +// the whole context, and a reply still empty after that is an error the +// next account answers. The words go upstream, so they name nobody. +const EMPTY_REPLY_PROMPT = "Your last reply was empty. Continue: make the tool call you were about to make, or give your answer." // agy's own output-cap retry reaches the reply as three lines of its own: // what happened, its instruction to continue, and the retries left. The // model's answer goes on after them in the same turn, so the notice is agy's @@ -1535,6 +1543,14 @@ class Turn { this.openBanner = -1 this.capPending = false this.attemptDetail = "" + // What the model gave since the turn began or since its last tool + // call: answer text, thinking, and agy's "invalid output" retry. An + // ending with thinking or that retry and no text is empty (emptyEnding). + this.saidSinceCall = false + this.thoughtSinceCall = false + this.invalidOutput = false + this.invalidOutputSeen = false + this.emptyAsked = false this.refusedDetail = "" this.agentErrorCore = "" this.timer = null @@ -1823,6 +1839,7 @@ class Turn { this.openBanner = start return text } + if (!attempt) this.invalidOutput = this.invalidOutputSeen = true if (attempt) this.attemptDetail = ATTEMPT_LINE.exec(text.slice(at, end))?.[1]?.trim() || this.attemptDetail if (attempt && refused429(this.attemptDetail)) this.refusedDetail ||= this.attemptDetail if (text[end] === "\n") end++ @@ -1833,6 +1850,7 @@ class Turn { emitText(text) { if (!text) return + if (text.trim()) this.saidSinceCall = true this.textTail = text.slice(-1) this.push({ type: "text", text }) } @@ -1852,6 +1870,15 @@ class Turn { } } + // The model ended its reply having thought, or after agy's empty-output + // retry, with no text and no tool call since the last one. A bare empty + // ending (no thinking either) is left as it is: that is a reply. + emptyEnding(stopReason) { + if (this.steering || this.saidSinceCall) return false + if (stopReason != null && stopReason !== "end_turn") return false + return this.thoughtSinceCall || this.invalidOutput + } + flushText() { if (!this.heldText || this.agentError) return const text = this.dropRetryBanner(this.heldText, true) @@ -2413,6 +2440,7 @@ class Proc { turn.notifyAcpTools() return } + if (text.trim()) turn.thoughtSinceCall = true turn.push({ type: "thinking", text }) } } @@ -2585,6 +2613,19 @@ class Proc { // agy ended the turn without waiting for a long-running client tool: // deliver its real result in a follow-up prompt of the same response. const late = turn.calls.filter((call) => call.deferred && !call.lateDelivered) + if (!late.length && turn.emptyEnding(result?.stopReason)) { + if (!turn.emptyAsked) { + turn.emptyAsked = true + turn.thoughtSinceCall = turn.invalidOutput = false + turn.arm() + promptParts = [{ type: "text", text: EMPTY_REPLY_PROMPT }] + lateCalls = null + continue + } + throw new AgentTurnError("agy bridge: the model ended its reply twice with no text and no tool call" + + (turn.invalidOutputSeen ? " (agy: Model produced invalid output)" : "") + + "; send the request again, or try another model") + } if (!late.length) { turn.complete({ ...(result ?? {}), usage }) return @@ -2749,6 +2790,7 @@ class Proc { clientSeen: false, } turn.flushText() + turn.saidSinceCall = turn.thoughtSinceCall = turn.invalidOutput = turn.emptyAsked = false turn.calls.push(call) toolCalls.set(call.id, { proc: this, turn, call }) socket.once("close", () => { diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 76f895a..ce2a5dd 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.93", + "version": "0.1.94", "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 5baa576..98ea1ca 100644 --- a/packages/agy-cli/test/repeat-batch.mjs +++ b/packages/agy-cli/test/repeat-batch.mjs @@ -990,7 +990,6 @@ test("#1124: agy's retry banner after an empty first call never reaches the clie [[["thought", "stay quiet"], ["text", banner], ["thought", "suggest one"], ["text", "梳理一下事件完整时间线"]], "梳理一下事件完整时间线"], [[["text", banner + "\n"], ["text", "the retry's answer"]], "the retry's answer"], [[["text", "\n⚠ Model produced "], ["text", "invalid output."], ["text", "\nretry"]], "retry"], - [[["text", banner]], ""], ]) { await withKit({}, async (kit) => { kit.defaultPrompt = async (agent) => { @@ -1006,6 +1005,103 @@ test("#1124: agy's retry banner after an empty first call never reaches the clie } }) +// fnckerpoi on X (DSH 0.2.2-rc.3 through billion-context, agy-cli 0.1.91): +// agy's empty-output retry came back empty too, agy ended the turn on +// end_turn with only thinking, and billion-context's continuation nudge got +// the same, "the turn degenerated again after the continuation nudge". +const EMPTY_BANNER = "\n⚠ Model produced invalid output." + +function sessionsOpened(kit) { + return kit.acpLog.filter((entry) => entry.method === "session/new" || entry.method === "session/load").length +} + +test("an empty ending is asked again in the same session, and its answer is the reply", async () => { + for (const empty of [ + [["thought", "the next step is to read the file"]], + [["thought", "stay quiet"], ["text", EMPTY_BANNER], ["thought", "still nothing"]], + [["text", EMPTY_BANNER]], + [["thought", "thinking"], ["text", " \n"]], + ]) { + await withKit({}, async (kit) => { + kit.defaultPrompt = async (agent) => { + if (promptText({ prompt: agent.prompt }).includes("Your last reply was empty")) { + await agent.text("the answer after asking again") + return { stopReason: "end_turn" } + } + for (const [kind, text] of empty) await agent[kind](text) + return { stopReason: "end_turn" } + } + const parsed = await readSse(await kit.request(baseBody({ metadata: { user_id: "empty-ending" }, + messages: [{ role: "user", content: "fix the bug" }] }))) + assert.equal(parsed.error, null) + assert.equal(parsed.stopReason, "end_turn") + assert.equal(replyText(parsed).trim(), "the answer after asking again") + const sent = prompts(kit) + assert.equal(sent.length, 2, "asked once more") + assert.ok(!/magpie|agy|antigravity|gemini/i.test(sent[1]), "the words sent upstream name nobody") + assert.equal(sessionsOpened(kit), 1, "in the same session, not a replay") + }) + } +}) + +test("an ending still empty after asking again is an error, and the session is kept", async () => { + await withKit({}, async (kit) => { + let empty = true + kit.defaultPrompt = async (agent) => { + if (!empty) { + await agent.text("answered on the client's retry") + return { stopReason: "end_turn" } + } + await agent.thought("hmm") + await agent.text(EMPTY_BANNER) + await agent.thought("hmm again") + return { stopReason: "end_turn" } + } + // a conversation under way: a first message sent again opens a new session anyway + const body = baseBody({ metadata: { user_id: "empty-twice" }, messages: [ + { role: "user", content: "look at the bug" }, + { role: "assistant", content: [{ type: "text", text: "it is in parse()" }] }, + { role: "user", content: "fix the bug" }, + ] }) + const parsed = await readSse(await kit.request(body)) + assert.ok(parsed.error, "an error, not a finished reply that said nothing") + assert.match(parsed.error.message, /no text and no tool call/) + assert.match(parsed.error.message, /Model produced invalid output/) + assert.notEqual(parsed.stopReason, "end_turn") + assert.equal(prompts(kit).length, 2, "asked once more, not again and again") + empty = false + const again = await readSse(await kit.request(body)) + assert.equal(again.error, null) + assert.equal(replyText(again), "answered on the client's retry") + assert.equal(sessionsOpened(kit), 1, "the client's retry continues in the kept session") + }) +}) + +test("a bare empty ending, with no thinking, is a reply and is not asked again", async () => { + await withKit({}, async (kit) => { + kit.defaultPrompt = async () => ({ stopReason: "end_turn" }) + const parsed = await readSse(await kit.request(baseBody({ metadata: { user_id: "bare-empty" }, + messages: [{ role: "user", content: "say nothing" }] }))) + assert.equal(parsed.error, null) + assert.equal(parsed.stopReason, "end_turn") + assert.equal(prompts(kit).length, 1) + }) +}) + +test("a max_tokens ending with only thinking is passed through, not asked again", async () => { + await withKit({}, async (kit) => { + kit.defaultPrompt = async (agent) => { + await agent.thought("a long thought") + return { stopReason: "max_tokens" } + } + const parsed = await readSse(await kit.request(baseBody({ metadata: { user_id: "max-tokens-thought" }, + messages: [{ role: "user", content: "think hard" }] }))) + assert.equal(parsed.error, null) + assert.equal(parsed.stopReason, "max_tokens") + assert.equal(prompts(kit).length, 1) + }) +}) + test("#1124: the model quoting the retry banner mid-line keeps it", async () => { const chunks = ["agy printed `⚠ Model produced invalid output.` and", " then retried."] await withKit({}, async (kit) => {