agy-cli 0.1.44: a turn keeps only its current request body, and tool-result images are hashed in the history key (#1137)

A Codex tool loop with view_image screenshots grew the plugin host to
0.5-1.5 GiB after GC, so magpie recycled it every ~10 minutes and the
mid-turn conversation restarted without its tool outputs (tungloong's
report).

- Turn.registerResponse drops the previous step's body and
  contentSnapshot. Only the current step's are read again (a late
  prompt's estimate); the usage reducer reads settled/usage only.
- historyKey keys a tool_result image by mimeType + sha256, as it
  already keys a user's image, instead of copying its base64 into the
  key on every comparison.

Old data: a conversation parked before the update with an image inside
a tool result has a history hash taken over the raw bytes, so it starts
a new agy session once instead of resuming. Others are unchanged.

Run (mote, sandbox HOME): node --test conversation-key, step-usage,
usage-settlement, repeat-batch, compat: 128 pass. bun test: 21 pass.
scripts/check.mjs ok. Broken by hand: with origin/main's index.mjs both
new tests in conversation-key.mjs fail (2 of 33).
Not tried: a real Codex Desktop screenshot loop; heap size not measured.
This commit is contained in:
yetone
2026-11-14 17:11:03 +08:00
parent 0a2cf4734c
commit dac9726736
3 changed files with 57 additions and 3 deletions
+15 -1
View File
@@ -1180,6 +1180,14 @@ class Turn {
}
registerResponse(body) {
// Only the current step's body is read again (a late prompt's estimate).
// An earlier one is dropped here: a tool loop sending screenshots kept
// every request's parsed body, 20MB+ each, for the whole turn (plugins#1137).
const previous = this.currentResponse
if (previous) {
previous.body = null
previous.contentSnapshot = null
}
const estimate = this.proc.estimatePromptTokens(body)
const pending = this.pendingPromptStep
const step = {
@@ -3567,7 +3575,13 @@ function historyKey(messages) {
ids.set(block.id, ordinal)
value = { type: "tool_use", id: ordinal, name: block.name, input: block.input ?? {} }
} else if (block.type === "tool_result") {
value = { type: "tool_result", id: ids.get(block.tool_use_id) ?? block.tool_use_id, ...toolResult(block) }
const result = toolResult(block)
// An image a tool returned is keyed by its hash, as a user's image
// is, not copied whole into the key on every round-trip (plugins#1137).
const content = result.content.map((part) => part.type === "image"
? { type: "image", mimeType: part.mimeType, sha256: digest(part.data) }
: part)
value = { type: "tool_result", id: ids.get(block.tool_use_id) ?? block.tool_use_id, ...result, content }
} else if (block.type === "server_tool_use" || block.type === "mcp_tool_use" ||
block.type === "container_upload" || SERVER_TOOL_RESULT_BLOCK_TYPES.has(block.type)) {
value = { type: block.type, name: block.name || "", content: renderServerAssistantBlock(block) }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-agy-cli-auth",
"version": "0.1.43",
"version": "0.1.44",
"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",
+41 -1
View File
@@ -15,7 +15,7 @@ process.env.AGY_BRIDGE_STATE_FILE = path.join(workspace, "sessions.json")
process.env.HOME = workspace
fs.writeFileSync(
path.join(workspace, "bridge.mjs"),
source + "\nexport { conversationKey, stripVolatileOpening, stripVolatileSystem, isContinuedOpening, openingTextOf, sameInstructions, historyKey, takeParked, park, instructionsIdentity };\n",
source + "\nexport { conversationKey, stripVolatileOpening, stripVolatileSystem, isContinuedOpening, openingTextOf, sameInstructions, historyKey, takeParked, park, instructionsIdentity, Turn };\n",
)
const {
conversationKey,
@@ -28,6 +28,7 @@ const {
takeParked,
park,
instructionsIdentity,
Turn,
} = await import(pathToFileURL(path.join(workspace, "bridge.mjs")).href)
after(() => fs.rmSync(workspace, { recursive: true, force: true }))
@@ -723,3 +724,42 @@ test("a reused flattened session keeps a new document on the grown first message
assert.equal(text.includes("step one"), false, `resent the opening: ${JSON.stringify(text)}`)
})
})
// plugins#1137: a tool loop of screenshots kept every request body and
// copied each image whole into the history key on every round-trip.
function screenshotHistory(data) {
return [
{ role: "user", content: "look" },
{ role: "assistant", content: [{ type: "tool_use", id: "t1", name: "view_image", input: {} }] },
{ role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: [
{ type: "text", text: "shot" },
{ type: "image", source: { type: "base64", media_type: "image/png", data } },
] }] },
]
}
test("historyKey keys a tool_result image by its hash, not its bytes", () => {
const data = "A".repeat(200_000)
const key = historyKey(screenshotHistory(data))
assert.ok(key.length < 2_000, `key is ${key.length} chars`)
assert.ok(!key.includes("AAAAAAAAAAAAAAAA"))
assert.notEqual(key, historyKey(screenshotHistory("B".repeat(200_000))))
assert.equal(key, historyKey(screenshotHistory(data)))
})
test("a turn lets go of each earlier request body once the next one arrives", () => {
const proc = { estimatePromptTokens: () => 100 }
const turn = new Turn(proc, [{ role: "user", content: "hi" }])
const bodies = [1, 2, 3].map((n) => ({ messages: screenshotHistory("C".repeat(n)) }))
const steps = bodies.map((body) => {
const step = turn.registerResponse(body)
step.contentSnapshot = () => []
return step
})
assert.equal(steps[0].body, null)
assert.equal(steps[0].contentSnapshot, null)
assert.equal(steps[1].body, null)
assert.equal(steps[2].body, bodies[2], "the current step keeps its body for a late prompt")
assert.equal(typeof steps[2].contentSnapshot, "function")
assert.equal(turn.responseSteps.length, 3)
})