agy-cli 0.1.26: a quoted error marker no longer ends the turn; a trimmed tool loop keeps its prompt (#1052, #1053)

#1052: Turn.pushText failed the turn at the first "⚠ Agent execution
terminated due to error." anywhere in the reply, so a model quoting it
(inline, in a code block) was cut off and reported as an agy error. agy's
own marker ends the prompt, so the marker is now held, and the turn fails
only when the prompt ends with it still held (Turn.endText, before the
agentError check). Text or a tool call after it sends it on as text.

#1053: when a replay went over the prompt budget mid tool loop,
renderInitialPromptParts kept only the opening message and the newest
history, and dropped the user prompt the loop works on. Now, when the
current turn carries tool_results, the latest user message without one is
kept too if it fits, in order, with an omission note per gap.

Not changed: #1052's retry banners (nothing shows them reaching the
client as message chunks) and #1051 (the supersede rule is deliberate,
covered by A15 and the lineage sibling test; evidence asked for).

Run on WSL (Bun 1.4.2, temp HOME):
- bun test packages/agy-cli: 15 pass, 0 fail.
- test/repeat-batch.mjs: 17 pass, 0 fail (3 new).
- bun scripts/check.mjs agy-cli: ok.
Broken by hand, each 16 pass 1 fail, then restored: the task prompt not
pinned; pushText back to the first-indexOf rule; turn.endText() removed.
Not tried: a real agy (no install, no Google endpoints).

Reported-by: hiee3
This commit is contained in:
yetone
2026-11-12 21:47:42 +08:00
parent fc87cc8855
commit bb3f68a47a
3 changed files with 118 additions and 9 deletions
+39 -8
View File
@@ -1075,15 +1075,18 @@ class Turn {
})
}
// Withholds a possible prefix of agy's error marker until it can be classified.
// Withholds agy's error marker, or a possible prefix of it, until it can be
// classified. agy's own marker ends the prompt; one that more text follows
// was the model quoting it (#1052).
pushText(chunk) {
if (this.done || this.agentError) return
let text = this.heldText + chunk
this.heldText = ""
const marker = text.indexOf(AGENT_ERROR_CORE)
if (marker >= 0) {
this.agentError = true
text = text.slice(0, marker).replace(/\n$/, "")
const marker = text.lastIndexOf(AGENT_ERROR_CORE)
if (marker >= 0 && !text.slice(marker + AGENT_ERROR_CORE.length).trim()) {
const start = text[marker - 1] === "\n" ? marker - 1 : marker
this.heldText = text.slice(start)
text = text.slice(0, start)
if (text) this.push({ type: "text", text })
return
}
@@ -1097,6 +1100,14 @@ class Turn {
if (text) this.push({ type: "text", text })
}
// The prompt ended: a marker still held was agy's own.
endText() {
if (this.heldText.includes(AGENT_ERROR_CORE)) {
this.agentError = true
this.heldText = ""
}
}
flushText() {
if (!this.heldText || this.agentError) return
const text = this.heldText
@@ -1623,6 +1634,7 @@ class Proc {
turn.complete({ stopReason: "cancelled" })
return
}
turn.endText()
if (turn.agentError) {
throw await this.#agentError(turn)
}
@@ -3817,13 +3829,32 @@ function renderInitialPromptParts(body, callerWorkDir, instructionsInSession, re
room -= utf8Bytes(HISTORY_OMITTED_NOTE)
const anchor = entries[0] && entries[0].bytes <= room ? entries[0] : null
if (anchor) room -= anchor.bytes
// Mid tool loop, the user's prompt that started it says what the loop is
// for: it stays too, after the anchor, with a note for each gap (#1053).
let task = -1
if (current.some((message) => Array.isArray(message.content) &&
message.content.some((block) => block?.type === "tool_result"))) {
task = history.findLastIndex((message) => message.role === "user" && (typeof message.content === "string"
? !!message.content.trim()
: Array.isArray(message.content) && !message.content.some((block) => block?.type === "tool_result")))
if (task <= 0 || entries[task].bytes + utf8Bytes(HISTORY_OMITTED_NOTE) > room) task = -1
else room -= entries[task].bytes + utf8Bytes(HISTORY_OMITTED_NOTE)
}
const keep = [...(anchor ? [0] : []), ...(task > 0 ? [task] : [])]
const tail = []
for (let i = entries.length - 1; i > (anchor ? 0 : -1); i--) {
for (let i = entries.length - 1; i > Math.max(task, anchor ? 0 : -1); i--) {
if (entries[i].bytes > room) break
room -= entries[i].bytes
tail.unshift(entries[i])
tail.unshift(i)
}
kept = anchor ? [anchor, null, ...tail] : [null, ...tail]
kept = []
let next = 0
for (const i of [...keep, ...tail]) {
if (i > next) kept.push(null)
kept.push(entries[i])
next = i + 1
}
if (next < entries.length) kept.push(null)
if (room < 0) kept = []
}
const out = []
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-agy-cli-auth",
"version": "0.1.25",
"version": "0.1.26",
"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",
+78
View File
@@ -415,3 +415,81 @@ test("A16: a short replay is rendered unchanged", async () => {
await sleep(0)
})
})
test("A16: mid tool loop, a trimmed replay keeps the user prompt that started the loop (#1053)", async () => {
await withKit({}, async (kit) => {
scriptedAgent(kit)
const big = (label) => `${label} head ` + "tool output line with some words. ".repeat(560) + ` ${label} tail`
const messages = [
{ role: "user", content: "ANCHOR opening request" },
{ role: "assistant", content: "ANCHOR reply" },
{ role: "user", content: "TASK START: rename parseConfig to loadConfig everywhere" },
]
// 16 tool steps of about 19 KiB each: about 130 KiB even after each result is clipped to 8 KiB.
for (let index = 0; index < 16; index++) {
messages.push({ role: "assistant", content: [{ type: "tool_use", id: `toolu_step${index}`, name: "Read", input: { path: `/repo/${index}.txt` } }] })
messages.push({ role: "user", content: [{ type: "tool_result", tool_use_id: `toolu_step${index}`, content: big(`step-${index}`) }] })
}
messages.push({ role: "assistant", content: [{ type: "tool_use", id: "toolu_last", name: "Read", input: { path: "/repo/last.txt" } }] })
messages.push({ role: "user", content: [{ type: "tool_result", tool_use_id: "toolu_last", content: "LAST RESULT" }] })
const parsed = await readSse(await kit.request(baseBody({ metadata: { user_id: "budget-task" }, messages })))
assert.equal(parsed.stopReason, "end_turn")
const prompt = prompts(kit).at(-1)
assert.ok(bytes(prompt) <= 112 * 1024, `initial prompt is ${bytes(prompt)} bytes`)
assert.ok(prompt.includes("ANCHOR opening request"), "the anchor message is kept")
assert.ok(prompt.includes("Human: TASK START: rename parseConfig to loadConfig everywhere"), "the loop's prompt is kept")
assert.ok(prompt.includes("step-15 tail") && prompt.includes("LAST RESULT"), "the newest steps are kept")
assert.ok(!prompt.includes("step-0 head"), "the oldest step gave way")
const note = "[... earlier conversation history omitted to fit ACP 112 KiB prompt budget ...]"
assert.equal(prompt.split(note).length - 1, 2, "one note before the pinned prompt, one after it")
assert.ok(prompt.indexOf("ANCHOR opening request") < prompt.indexOf("TASK START") &&
prompt.indexOf("TASK START") < prompt.indexOf("step-15 tail"), "the kept history stays in order")
})
})
// ----------------------------------------------- agy's error marker (#1052)
function markerAgent(kit, chunks) {
kit.defaultPrompt = async (agent) => {
for (const chunk of chunks) await agent.text(chunk)
return { stopReason: "end_turn" }
}
}
function replyText(parsed) {
return parsed.content.filter((block) => block.type === "text").map((block) => block.text).join("")
}
test("#1052: the model quoting agy's error marker mid-reply does not end a healthy turn", async () => {
for (const chunks of [
["agy prints `⚠ Agent execution terminated due to error.` when a turn fails;", " the real cause is in its log."],
["The log ends with:\n\n```\n⚠ Agent execution terminated due to error.\n", "```\n\nThat line comes from agy."],
["Quoted: \n⚠ Agent execution ", "terminated due to error.\n", "and the reply goes on."],
]) {
await withKit({}, async (kit) => {
markerAgent(kit, chunks)
const parsed = await readSse(await kit.request(baseBody({ metadata: { user_id: "marker-quoted" },
messages: [{ role: "user", content: "why did my agy turn stop?" }] })))
assert.equal(parsed.error, null)
assert.equal(parsed.stopReason, "end_turn")
assert.equal(replyText(parsed), chunks.join(""))
})
}
})
test("#1052: agy's own marker at the end of the prompt still fails the turn", async () => {
for (const chunks of [
["partial before failure", "\n⚠ Agent execution terminated due to error.\n"],
["partial before failure\n⚠ Agent execution ", "terminated due to error.", "\n"],
]) {
await withKit({}, async (kit) => {
markerAgent(kit, chunks)
const response = await kit.request(baseBody({ metadata: { user_id: "marker-real" },
messages: [{ role: "user", content: "hello" }] }))
const text = await response.text()
assert.match(text, /agy stopped the turn because of an error/)
assert.ok(!text.includes("Agent execution terminated"), "the marker never reaches the client")
assert.ok(text.includes("partial before failure"), "text before the marker is kept")
})
}
})