agy-cli: a tool loaded mid-turn starts a new prompt with the round replayed (#942)

agy checks each call against the tools it listed before the prompt began.
After ToolSearch loads a deferred tool, the client's next request declares
it, and the bridge sent tools/list_changed and then continued the same
session/prompt. agy's validator still answered `unknown tool: "<name>" —
check spelling` for the rest of that prompt.

When the tool-result request declares a tool the process has not listed
(Proc.toolsGrew), the bridge now takes the existing changed-context path:
it drops the process without resolving its calls and replays the history,
the ToolSearch tool_use and its tool_result included, in a new
session/prompt, where agy lists the full catalog first. The user's turn is
in the replayed history once. The new process starts with the grown
catalog, so the next result continues it live and nothing loops. A catalog
that only loses or changes tools still continues the live turn.

Tests (test/smoke.mjs, fake agy): TOOLSEARCHGROW checks calls against the
tools listed at prompt start, like agy 1.5.3. The new block checks that the
grown catalog starts a new session/prompt, that it replays the ToolSearch
round with the user's ask once, that the old prompt never reaches the
validator with the new tool, that the loaded tool's call reaches the client,
and that its result continues the new session with no second replay. The
"live tools change" case now removes a tool instead of adding one.

Run on mote (Bun 1.4.2), at this tree:
- packages/agy-cli `bun test`: 8 pass, 0 fail; smoke.mjs ALL OK (319 checks).
- `bun scripts/check.mjs agy-cli`: registry.json and agy-cli ok.
Broken by hand: sed removed `|| proc.toolsGrew(tools)` from the condition;
smoke.mjs went 5 FAILURES (319 checks), the five new checks; restored, ALL OK.

Not run: the root `bun test`. It waited behind about 20 queued suites on
mote's suite.lock for over an hour, and WSL answered E_UNEXPECTED.
Not tried: a real agy 1.5.3 run.

Version 0.1.4.

Reported-by: chrisjian <chrisjian@users.noreply.github.com>
This commit is contained in:
yetone
2026-11-11 03:11:42 +08:00
parent 9696b6f37a
commit 60acd62bee
3 changed files with 70 additions and 10 deletions
+14 -6
View File
@@ -1313,6 +1313,14 @@ class Proc {
socket.once("close", () => this.watchers.delete(socket))
}
// Whether the client now declares a tool agy has not listed. agy checks a
// call against the tools it listed before the prompt began, so a tool added
// mid-turn (ToolSearch) stays unknown until the next session/prompt (#942).
toolsGrew(tools) {
const known = new Set(this.tools.map((tool) => tool.name))
return tools.some((tool) => !known.has(tool.name))
}
// Swap the client tool catalog in place; agy re-lists on tools/list_changed.
async updateTools(tools) {
const signature = JSON.stringify(tools)
@@ -4332,18 +4340,18 @@ export const AntigravityBridgePlugin = async () => {
const historyMatches = directIdsMatch
? proc.matchesPendingTurnHistory(turn, trailingMessages(body).history)
: proc.matchesHistory(trailingMessages(body).history)
if (interrupted || !historyMatches ||
if (interrupted || !historyMatches || proc.toolsGrew(tools) ||
proc.workDir !== workDir || !sameInstructions(proc.instructions, instructions)) {
// A changed context or user-interrupted tool turn needs a new
// session. Replay the real results without resolving the old
// process's calls.
// A changed context, a tool catalog that grew, or a
// user-interrupted tool turn needs a new session. Replay the
// real results without resolving the old process's calls.
dropFromPool(proc)
proc.abandonTurn(turn)
await proc.stop()
} else {
if (!proc.dead && !turn.done) {
// A changed tool catalog applies to the rest of this turn;
// a model change applies from the next prompt.
// A tool catalog that lost or changed tools applies to the
// rest of this turn; a model change applies from the next prompt.
await proc.updateTools(tools)
proc.continueWith(results)
proc.recordToolResults(turn, body.messages)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-agy-cli-auth",
"version": "0.1.3",
"version": "0.1.4",
"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",
+55 -3
View File
@@ -298,6 +298,20 @@ async function prompt(params) {
chunk("After tool search.\n")
return finish("After tool search.\n")
}
if (text.includes("TOOLSEARCHGROW")) {
// Like agy 1.5.3: a call is checked against the tools listed before this
// prompt began; a tools/list_changed during the prompt does not reach it.
const known = new Set(tools.map(tool => tool.name))
if (!text.includes("<conversation_history>")) await invoke("ToolSearch", { query: "select:loaded_tool" })
if (!known.has("loaded_tool")) {
log({ type: "unknownTool", convId: session.id, name: "loaded_tool" })
chunk('Encountered error in tool validation: unknown tool: "loaded_tool" — check spelling\n')
return finish("unknown tool")
}
await invoke("loaded_tool", { text: "grown" })
chunk("After loaded tool.\n")
return finish("After loaded tool.\n")
}
if (text.includes("ESCCALL") && !text.includes("<conversation_history>")) {
await invoke("echo", { text: "probe" })
chunk("After esc.\n")
@@ -1156,19 +1170,22 @@ let nativeConv
}
for (const change of ["tools", "model"]) {
const first = { model, messages: [{ role: "user", content: `TOOL live configuration ${change}` }], tools: [echoTool] }
const other = { name: "other", input_schema: { type: "object" } }
const first = { model, messages: [{ role: "user", content: `TOOL live configuration ${change}` }], tools: change === "tools" ? [echoTool, other] : [echoTool] }
const events = (await readSSE(await callFetch(first))).events
const old = inits().at(-1)
const second = continuation(first, events, "completed-before-change")
if (change === "tools") {
second.tools = [...second.tools, { name: "other", input_schema: { type: "object" } }]
// A catalog that only loses tools keeps the live turn; one that grows
// starts a new prompt (the #942 block below).
second.tools = [echoTool]
second.messages.push({ role: "user", content: [{ type: "text", text: "<system-update>tool catalog changed</system-update>" }] })
} else second.model = legacyModel
const final = (await readSSE(await callFetch(second))).events
const delivered = logs().filter(event => event.type === "mcpResult" && event.convId === old.convId).at(-1)
check(`live ${change} change continues the pending turn`, stopOf(final) === "end_turn" && inits().at(-1).convId === old.convId && delivered?.result?.content?.[0]?.text === "completed-before-change")
if (change === "tools") {
check("live tool change reaches agy", logs().some(event => event.type === "toolsList" && event.convId === old.convId && event.tools.includes("other")))
check("live tool change reaches agy", logs().some(event => event.type === "toolsList" && event.reason === "list_changed" && event.convId === old.convId && event.tools.includes("echo") && !event.tools.includes("other")))
check("system update reaches the pending MCP result", delivered?.result?.content?.some(part => part.text?.includes("tool catalog changed")))
} else {
const next = (await readSSE(await callFetch({ ...nextTurn(second, final, "MODEL next prompt"), model: legacyModel }))).events
@@ -1750,6 +1767,41 @@ for (const location of ["inline", "system", "developer", "user"]) {
afterSearchRes.status === 200 && inits().length === beforeSearch)
}
{
// #942: a tool ToolSearch loads mid-turn is unknown to agy for the rest of
// that prompt, so the bridge replays the round in a new session/prompt.
const searchTool = { name: "ToolSearch", description: "Search deferred tools", input_schema: { type: "object" } }
const loadedTool = { name: "loaded_tool", description: "Loaded by ToolSearch", input_schema: { type: "object", properties: { text: { type: "string" } } } }
const ask = "TOOLSEARCHGROW load a deferred tool"
const first = { model, messages: [{ role: "user", content: ask }], tools: [searchTool] }
const events = (await readSSE(await callFetch(first))).events
const old = inits().at(-1)
const before = inits().length
const second = continuation(first, events, [{ type: "tool_reference", tool_name: "loaded_tool" }])
second.tools = [searchTool, loadedTool]
const secondRes = await callFetch(second)
const secondEvents = (await readSSE(secondRes)).events
const replay = lastOf("user")
check("a tool loaded mid-turn starts a new session/prompt",
secondRes.status === 200 && inits().length === before + 1 && replay.convId !== old.convId, `got ${secondRes.status}`)
check("the new prompt replays the ToolSearch round and sends the user's turn once",
replay.text.includes("<conversation_history>") && replay.text.includes("Loaded tool: loaded_tool") &&
replay.text.split(ask).length === 2, replay.text)
check("no call to the loaded tool reaches agy's validator in the old prompt",
!logs().some(event => event.type === "unknownTool"))
const calls = toolCalls(secondEvents)
check("a call to the loaded tool reaches the client",
stopOf(secondEvents) === "tool_use" && calls.length === 1 && calls[0].name === "loaded_tool", JSON.stringify(calls))
const third = continuation(second, secondEvents, "loaded-tool-result")
const thirdRes = await callFetch(third)
const final = (await readSSE(thirdRes)).events
const delivered = logs().filter(event => event.type === "mcpResult" && event.convId === replay.convId).at(-1)
check("the loaded tool's result continues the new session with no second replay",
thirdRes.status === 200 && stopOf(final) === "end_turn" && inits().length === before + 1 &&
lastOf("user").convId === replay.convId && delivered?.name === "loaded_tool" &&
delivered?.result?.content?.[0]?.text === "loaded-tool-result")
}
{
// Pressing Esc during a client tool run appends [Request interrupted by user] + the new user prompt.
const first = { model, messages: [{ role: "user", content: "ESCCALL interrupted by user" }], tools: [echoTool] }