Files
plugins/packages/agy-cli/test/compat.mjs
T
DaWei LiuandGuuuu233 9a96799efb agy-cli: user text beside a live tool result replays the round in a new prompt (#909, #951)
A user message that carried ordinary text beside a live tool_result, before
or after the result block, was refused with 400 "text beside a live
tool_result; send it after the tool response completes" (maxleebeta's
DeepSeek-harness agent, #909; Claude Science does the same). inspectToolTurn
now marks such a turn userSteered, so the bridge takes the existing replay
path: the old session is stopped, its pending calls are not resolved, and a
new session/prompt carries the round, the real tool result included. The
user's text is never spliced into an MCP function response. The same goes
for plain text after a Skill result, and for a hook line that carries "The
user sent a new message while you were working".

Still refused: an unclosed protocol notice (<system-update>... with no end
tag) on either side of the result, a companion (ARGUMENTS:, [Image: ...],
Tool loaded.) with no matching preceding tool, and an image before the first
tool_result. A turn whose tools also grew (#942) takes the same single
replay.

Changed behaviour beyond #909: a protocol notice, token line or hook-success
line placed before the tool_result (previously 400) now replays in a new
session, where the same line after the result is forwarded in-session.

Tests: compat.mjs checks both placements are userSteered, the MCP result
is unchanged, and the replay carries the instruction and the result. smoke.mjs
drives both placements and the Skill case through the fake agy and checks a
new convId whose prompt carries both. Five smoke checks that asserted 400
for text the bridge now replays were removed; the unclosed-notice 400 check
stays.

Run at head 04692ce0a74d (merged onto origin/main d5f4214e) on mote, bun 1.4.2:
- bun test packages/agy-cli: 8 pass, 0 fail, at the head and merged
- bun scripts/check.mjs agy-cli: ok, at the head and merged
- Broken by hand (index.mjs reverted, tests kept): compat 1 fail (the new
  test); smoke 3 FAILURES of 321 (before, after, Skill: status 400)
- Probe of inspectToolTurn merged: the user text appears once in the replay
  and the result once; MCP content stays "file contents"

Not run: the root bun test suite (mote's suite queue was long); the
reporter's own request bytes (never obtained, #909 stays open for them).

Co-authored-by: Guuuu233 <308638291+Guuuu233@users.noreply.github.com>
2026-11-11 04:33:33 +08:00

219 lines
11 KiB
JavaScript

import assert from "node:assert/strict"
import childProcess from "node:child_process"
import { EventEmitter } from "node:events"
import { syncBuiltinESMExports } from "node:module"
import { PassThrough } from "node:stream"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { pathToFileURL } from "node:url"
import { after, test } from "node:test"
// Expose private seams from an isolated copy, without adding production exports.
// The smoke suite covers the fetch/ACP/MCP path; these cases cover batched
// client payloads that the sequential fake ACP server cannot emit itself.
const workspace = fs.mkdtempSync(path.join(os.tmpdir(), "agy-compat-test-"))
const entry = new URL("../index.mjs", import.meta.url)
let source = fs.readFileSync(entry, "utf8")
source = source.replace(/from "(\.\/[^"\n]+)"/g, (_, target) => `from ${JSON.stringify(new URL(target, entry).href)}`)
fs.writeFileSync(path.join(workspace, "bridge.mjs"), source + "\nexport { inspectToolTurn, matchToolResults, historyKey, allProcs, toolCalls, renderInitialPromptParts, withStateLock, execOnce };\n")
const { inspectToolTurn, matchToolResults, historyKey, allProcs, toolCalls, renderInitialPromptParts, withStateLock, execOnce } = await import(pathToFileURL(path.join(workspace, "bridge.mjs")).href)
after(() => fs.rmSync(workspace, { recursive: true, force: true }))
const image = { type: "image", source: { type: "base64", media_type: "image/png", data: "aW1hZ2VB" } }
const result = (id, name, text) => ({
block: { type: "tool_result", tool_use_id: id, content: text },
call: { id, name },
result: { content: [{ type: "text", text }], isError: false },
})
const bodyFor = content => ({ messages: [{ role: "assistant", content: [] }, { role: "user", content }] })
test("images and dimension notes belong to their preceding result in a batch", () => {
const a = result("call_a", "Read", "first")
const b = result("call_b", "Read", "second")
const body = bodyFor([a.block, image, { type: "text", text: "[Image: first screenshot]" }, b.block])
const { results } = inspectToolTurn([a, b], body)
assert.deepEqual(results[0].result.content.map(part => part.type), ["text", "image", "text"])
assert.equal(results[0].result.content[1].data, image.source.data)
assert.deepEqual(results[1].result.content, [{ type: "text", text: "second" }])
})
test("a companion cannot attach to a different preceding tool", () => {
const skill = result("call_skill", "Skill", "Launching skill: review")
const read = result("call_read", "Read", "file text")
for (const text of ["ARGUMENTS: review", "Tool loaded.", "[Image: screenshot]"]) {
assert.throws(() => inspectToolTurn([skill, read], bodyFor([skill.block, read.block, { type: "text", text }])), /text beside a live tool_result/)
}
})
test("ordinary user text beside live tool results steers a replay without becoming a tool response", () => {
const call = result("call_read", "Read", "file contents")
const instruction = { type: "text", text: "Continue reviewing the image pipeline" }
for (const blocks of [[call.block, instruction], [instruction, call.block]]) {
const body = bodyFor(blocks)
const inspected = inspectToolTurn([call], body)
assert.equal(inspected.userSteered, true, "mixed user text requires a fresh user prompt")
assert.deepEqual(inspected.results[0].result.content, call.result.content,
"user instructions must not be attached to an MCP function response")
const replay = renderInitialPromptParts(body, null, true).map(part => part.text || "").join("")
assert.ok(replay.includes("Continue reviewing the image pipeline"),
"the replay must carry the original user instruction")
assert.ok(replay.includes("file contents"), "the replay must retain the real tool result")
}
})
test("a partial ID rename maps by full history without discarding the live ID anchor", () => {
const original = [
{ type: "tool_use", id: "original_a", name: "Read", input: { path: "a" } },
{ type: "tool_use", id: "original_b", name: "Read", input: { path: "b" } },
]
const expectedMessages = [{ role: "user", content: "read both" }, { role: "assistant", content: original }]
const history = structuredClone(expectedMessages)
history[1].content[1].id = "renamed_b"
const turn = { finalized: false }
const proc = { turn, expectedMessages, matchesHistory: value => historyKey(value) === historyKey(expectedMessages) }
const calls = original.map(call => ({ ...call, mcpResponded: false }))
allProcs.add(proc)
calls.forEach(call => toolCalls.set(call.id, { proc, turn, call }))
try {
const blocks = ["original_a", "renamed_b"].map(id => ({ type: "tool_result", tool_use_id: id, content: "done" }))
const matched = matchToolResults({ messages: [...history, { role: "user", content: blocks }] }, blocks)
assert.deepEqual(matched.map(item => item.match?.call?.id), ["original_a", "original_b"])
} finally {
allProcs.delete(proc)
calls.forEach(call => toolCalls.delete(call.id))
}
})
test("history replay keeps image bytes between their original surrounding text", () => {
const parts = renderInitialPromptParts({ messages: [
{ role: "user", content: [{ type: "text", text: "before first image" }, image, { type: "text", text: "after first image" }] },
{ role: "assistant", content: "old reply" },
{ role: "user", content: "current question" },
] }, null, true)
assert.deepEqual(parts.map(part => part.type), ["text", "image", "text"])
assert.ok(parts[0].text.endsWith("Human: before first image"))
assert.ok(parts[2].text.startsWith("after first image\nAssistant: old reply"))
assert.ok(parts[2].text.endsWith("<current_turn>\ncurrent question\n</current_turn>"))
})
test("state lock recovers a stale lock only after its owning process has exited", () => {
const file = path.join(workspace, "dead-owner-state.json")
fs.writeFileSync(file + ".lock", JSON.stringify({ pid: 2147483647 }))
const old = new Date(Date.now() - 10_000)
fs.utimesSync(file + ".lock", old, old)
let wrote = false
withStateLock(file, () => { wrote = true })
assert.equal(wrote, true)
assert.equal(fs.existsSync(file + ".lock"), false)
})
async function captureExec(callback, mode = "close") {
const originalSpawn = childProcess.spawn
const originalEnv = process.env
const originalSetTimeout = globalThis.setTimeout
const calls = []
const delays = []
const inherited = { PATH: "parent-path", AGY_CLI_DISABLE_AUTO_UPDATE: "false" }
try {
process.env = inherited
globalThis.setTimeout = (callback, delay, ...args) => {
delays.push(delay)
return originalSetTimeout(callback, mode === "timeout" ? 0 : delay, ...args)
}
childProcess.spawn = (file, args, options) => {
calls.push({ file, args, options })
if (mode === "throw") throw new Error("fixture spawn failure")
const child = new EventEmitter()
child.stdout = new PassThrough()
child.stderr = new PassThrough()
child.kill = signal => {
calls.at(-1).signal = signal
queueMicrotask(() => child.emit("close", null))
}
queueMicrotask(() => {
child.stdout.write("fixture output")
child.stderr.write("fixture error output")
if (mode === "close") child.emit("close", 7)
if (mode === "error") child.emit("error", new Error("fixture async failure"))
})
return child
}
syncBuiltinESMExports()
await callback(calls, delays, inherited)
assert.equal(inherited.AGY_CLI_DISABLE_AUTO_UPDATE, "false")
} finally {
childProcess.spawn = originalSpawn
syncBuiltinESMExports()
process.env = originalEnv
globalThis.setTimeout = originalSetTimeout
}
}
test("execOnce agy branch forces hidden windows and keeps a user-set update value without mutating options", async () => {
await captureExec(async (calls, delays) => {
const env = Object.freeze({ CALLER_ONLY: "kept", AGY_CLI_DISABLE_AUTO_UPDATE: "false" })
const opts = Object.freeze({ env, windowsHide: false, cwd: "workspace", timeoutMs: 1234 })
const args = Object.freeze(["models"])
const result = await execOnce("agy", args, opts)
assert.deepEqual(result, { err: null, stdout: "fixture output", stderr: "fixture error output", code: 7, timedOut: false })
assert.equal(calls[0].file, "agy")
assert.equal(calls[0].args, args)
assert.equal(calls[0].options.windowsHide, true)
assert.equal(calls[0].options.env.AGY_CLI_DISABLE_AUTO_UPDATE, "false")
assert.equal(calls[0].options.env.CALLER_ONLY, "kept")
assert.equal(calls[0].options.env.PATH, "parent-path")
assert.equal(calls[0].options.cwd, "workspace")
assert.deepEqual(calls[0].options.stdio, ["ignore", "pipe", "pipe"])
assert.deepEqual(delays, [1234])
assert.equal(env.AGY_CLI_DISABLE_AUTO_UPDATE, "false")
assert.equal(opts.windowsHide, false)
})
})
test("execOnce taskkill uses generic spawn, keeps caller env and cannot expose a window", async () => {
await captureExec(async (calls, delays) => {
const args = Object.freeze(["/T", "/F", "/PID", "12345"])
for (const env of [Object.freeze({ CALLER_ONLY: "kept" }), Object.freeze({ AGY_CLI_DISABLE_AUTO_UPDATE: "false" })]) {
const opts = Object.freeze({ env, windowsHide: false, timeoutMs: 3000 })
const result = await execOnce("taskkill", args, opts)
const actual = calls.at(-1)
assert.equal(actual.file, "taskkill")
assert.equal(actual.args, args)
assert.equal(actual.options.windowsHide, true, "taskkill must remain hidden after caller overrides")
assert.equal(actual.options.env, env, "taskkill must use generic spawn without agy env injection")
assert.deepEqual(actual.options.env, env)
assert.equal(opts.windowsHide, false)
assert.equal(result.code, 7)
assert.equal(result.stdout, "fixture output")
assert.equal(result.stderr, "fixture error output")
assert.equal(result.timedOut, false)
}
await execOnce("taskkill", args)
assert.equal(calls.at(-1).options.env, undefined, "taskkill without caller env must inherit the parent environment")
assert.deepEqual(delays, [3000, 3000, 30000])
})
})
test("execOnce taskkill retains its three-second deadline and kills only its child", async () => {
await captureExec(async (calls, delays) => {
const result = await execOnce("taskkill", ["/T", "/F", "/PID", "12345"], { timeoutMs: 3000 })
assert.deepEqual(delays, [3000])
assert.equal(calls[0].signal, "SIGKILL")
assert.equal(result.timedOut, true)
assert.equal(result.code, null)
assert.equal(result.stdout, "fixture output")
}, "timeout")
})
test("execOnce still collects synchronous and asynchronous spawn failures", async () => {
for (const mode of ["throw", "error"]) {
await captureExec(async () => {
const result = await execOnce("taskkill", [])
assert.match(result.err.message, /fixture/)
assert.equal(result.code, null)
assert.equal(result.timedOut, false)
}, mode)
}
})