Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/buzz-help.md
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ available rather than silently discarding it.
agent’s activity or stop an individual request. Expand recorded work for its
activity controls. Stop availability
depends on the agent type.
For native Claude and Codex, Stop also cancels pending plugin tool calls
For native Claude, Codex and Goose, Stop also cancels pending plugin tool calls
that use the host signal. It cannot undo a write already sent.
- Agent-to-agent messages stay in the conversation as collapsed history after
work finishes or you reopen a thread. Expand them to read dispatches, handoffs,
Expand Down
7 changes: 5 additions & 2 deletions docs/plugin-architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -897,8 +897,11 @@ lowercase and cannot be a Buzz tool's. The tool is removed when its plugin's
scope ends, so safe mode removes installed plugins' tools.

`call(client, context, args)` can read `client.signal`, a host-owned
`AbortSignal` captured for that call. Native Claude and Codex cancel it on
Stop. Keep that exact signal across waits. Pass it to work that supports
`AbortSignal` captured for that call. Native Claude, Codex and Goose cancel it on
Stop. Goose captures the current ACP work burst: steers share its signal, and
Stop, eligible final-message settlement, normal completion, failure or session close abort
it. Calls before a Goose session starts work or while idle receive an aborted
signal; agent retirement also aborts captured calls. Keep that exact signal across waits. Pass it to work that supports
cancellation, then call `signal.throwIfAborted()` before an external write.
A new turn does not restore a canceled call. The supplied client also checks
cancellation before it publishes. Stop cannot undo a write already sent.
Expand Down
10 changes: 5 additions & 5 deletions src/bundled/goose/goose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,11 @@ describe("GooseProcess", () => {
const session = await open();
session.conversation = "channel/root";
const response = await process.mcp(session.id, "tools/list");
expect(tools).toHaveBeenCalledWith("channel/root", {
id: 1,
method: "tools/list",
params: {},
});
expect(tools).toHaveBeenCalledWith(
"channel/root",
{ id: 1, method: "tools/list", params: {} },
expect.objectContaining({ aborted: true }),
);
// goose matches the reply to its request by the id it sent.
expect(response).toEqual({
jsonrpc: "2.0",
Expand Down
156 changes: 155 additions & 1 deletion src/bundled/goose/runtime.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import type { AddedTool } from "../../buzz-mcp/session-tools";
import type { BuzzClient, Memories } from "../../buzz-mcp/client";
import { callTool, memoryVersion } from "../../buzz-mcp/tools";
import { createAgentMemories } from "../../features/agents/memory";
Expand Down Expand Up @@ -66,6 +67,7 @@ function setup(
options: Readonly<{
/** Channels that are the owner's own Solo chats; the owner is the viewer. */
solo?: Set<string>;
added?: readonly AddedTool[];
hold?: boolean;
reply?: string;
thread?: readonly EventData[];
Expand Down Expand Up @@ -189,7 +191,7 @@ function setup(
() => relay,
core,
storage,
undefined,
() => options.added ?? [],
options.repositories,
);
const published: { kind: number; content: string; tags: string[][] }[] = [];
Expand Down Expand Up @@ -855,6 +857,158 @@ describe("GooseRuntime", () => {
expect(process?.cancelled).toEqual([process?.turns[0]?.sessionId]);
});

it("lists added tools and cancels a held write across Stop without reviving it in the next turn", async () => {
let signal: AbortSignal | undefined;
let enter!: () => void;
let release!: () => void;
const entered = new Promise<void>((resolve) => {
enter = resolve;
});
const held = new Promise<void>((resolve) => {
release = resolve;
});
const write = vi.fn();
const tool: AddedTool = {
name: "fixture_save",
description: "Fixture write",
inputSchema: { type: "object" },
call: async (client) => {
signal = client.signal;
if (!signal) throw new Error("Cancellation required");
signal.throwIfAborted();
enter();
await held;
signal.throwIfAborted();
write();
return "saved";
},
};
const f = setup({ hold: true, scope: "channel", added: [tool] });
const interruptible = vi.fn<NonNullable<Delivery["interruptible"]>>();
await f.deliver(message("1", "save"), { interruptible });
await vi.waitFor(() => expect(f.goose()?.turns).toHaveLength(1));
const process = f.goose();
const sessionId = process?.turns[0]?.sessionId;
if (!process || !sessionId) throw new Error("Turn missing");
expect(await process.mcp(sessionId, "tools/list")).toMatchObject({
result: {
tools: expect.arrayContaining([
expect.objectContaining({ name: tool.name }),
]),
},
});
const pending = process.mcp(sessionId, "tools/call", {
name: tool.name,
arguments: {},
});
try {
await entered;
expect(signal?.aborted).toBe(false);
await interruptible.mock.calls[0]?.[0]();
expect(signal?.aborted).toBe(true);
await f.deliver(message("2", "next"));
await vi.waitFor(() => expect(process.turns).toHaveLength(2));
expect(signal?.aborted).toBe(true);
} finally {
release();
}
expect(await pending).toMatchObject({ result: { isError: true } });
expect(write).not.toHaveBeenCalled();
const old = signal;
expect(
await process.mcp(sessionId, "tools/call", {
name: tool.name,
arguments: {},
}),
).not.toMatchObject({ result: { isError: true } });
expect(signal).not.toBe(old);
expect(signal?.aborted).toBe(false);
expect(write).toHaveBeenCalledTimes(1);
process.turns[1]?.finish();
await vi.waitFor(() => expect(signal?.aborted).toBe(true));
expect(
await process.mcp(sessionId, "tools/call", {
name: tool.name,
arguments: {},
}),
).toMatchObject({ result: { isError: true } });
expect(write).toHaveBeenCalledTimes(1);
});

it("keeps steered work live, then cancels its plugin calls on a successful final send or retirement", async () => {
const signals: AbortSignal[] = [];
const f = setup({
hold: true,
scope: "channel",
added: [
{
name: "fixture_signal",
description: "Capture cancellation",
inputSchema: { type: "object" },
call: async (client) => {
if (!client.signal) throw new Error("Cancellation required");
client.signal.throwIfAborted();
signals.push(client.signal);
return "live";
},
},
],
});
try {
await f.deliver(message("1", "first"));
await vi.waitFor(() => expect(f.goose()?.turns).toHaveLength(1));
const process = f.goose();
const id = process?.turns[0]?.sessionId;
if (!process || !id) throw new Error("Missing turn");
const capture = () =>
process.mcp(id, "tools/call", { name: "fixture_signal" });
await capture();
await f.deliver(message("2", "steer"));
await vi.waitFor(() => expect(process.turns[0]?.steered).toHaveLength(1));
expect(signals[0]?.aborted).toBe(false);
expect(
await process.mcp(id, "tools/call", {
name: "send",
arguments: { text: "Finished", audience: "everyone", final: true },
_meta: { "agent-tool-call-request-id": "final-call" },
}),
).not.toMatchObject({ result: { isError: true } });
// A burst with steered messages is not finished by a single final send.
expect(signals[0]?.aborted).toBe(false);
process.turns[0]?.finish();
await vi.waitFor(() => expect(signals[0]?.aborted).toBe(true));
await f.deliver(message("3", "new work"));
await vi.waitFor(() => expect(process.turns).toHaveLength(2));
await capture();
expect(signals[1]?.aborted).toBe(false);
expect(
await process.mcp(id, "tools/call", {
name: "send",
arguments: { text: "Done", audience: "everyone", final: true },
_meta: { "agent-tool-call-request-id": "final-call" },
}),
).not.toMatchObject({ result: { isError: true } });
expect(signals[1]?.aborted).toBe(true);
// The final tool result still reaches Goose before ACP cancel is sent.
expect(process.cancelled).toHaveLength(0);
process.update(id, {
sessionUpdate: "tool_call_update",
toolCallId: "final-call",
status: "completed",
});
await vi.waitFor(() => expect(process.cancelled).toHaveLength(1));
await f.deliver(message("4", "retire"));
await vi.waitFor(() => expect(process.turns).toHaveLength(3));
await capture();
expect(signals[2]?.aborted).toBe(false);
const retired = f.runtime.retire(self);
expect(signals[2]?.aborted).toBe(true);
await retired;
} finally {
f.runtime.dispose();
}
});

it("returns no read that the owner's Stop overtook", async () => {
let release!: () => void;
let entered!: () => void;
Expand Down
9 changes: 7 additions & 2 deletions src/bundled/goose/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,14 +824,19 @@ export class GooseRuntime {
};
// Every session of the agent shares them, so they read the settings, the
// handle and the conversation's place as they are when called.
const tools: ToolServer = async (conversation, message) => {
const tools: ToolServer = async (conversation, message, workSignal) => {
const handle = entry.handle;
// A call in a conversation answers only while its turn runs unstopped.
const current = conversation && entry.sessions.current(conversation);
const signal = AbortSignal.any([
entry.lifetime.signal,
workSignal ?? AbortSignal.abort(new Error("The turn has ended")),
]);
const client =
handle &&
appClient(handle, {
signal: entry.lifetime.signal,
signal,
callSignal: signal,
...(current && {
check: () => {
if (!current()) throw new Error("The Goose turn has ended");
Expand Down
10 changes: 5 additions & 5 deletions src/bundled/goose/sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ describe("AgentSessions", () => {
]);
await goose?.mcp("session-2", "tools/list");
expect(ada).not.toHaveBeenCalled();
expect(bo).toHaveBeenCalledWith("chan/root", {
id: expect.anything(),
method: "tools/list",
params: {},
});
expect(bo).toHaveBeenCalledWith(
"chan/root",
{ id: expect.anything(), method: "tools/list", params: {} },
expect.objectContaining({ aborted: true }),
);
await goose?.mcp("session-1", "tools/list");
expect(ada).toHaveBeenCalledOnce();
expect(bo).toHaveBeenCalledOnce();
Expand Down
90 changes: 90 additions & 0 deletions src/features/agent-harness/acp/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,96 @@ describe("a session without hooks", () => {
await second;
});

it("cancels captured work on Stop, final settlement, failure and close, independently per session", async () => {
const harness = fakeHarness({ hold: true });
const acp = await start(harness);
const session = await acp.open({ cwd: "/work", systemPrompt: "" });
const other = await acp.open({ cwd: "/work", systemPrompt: "" });
expect(session.signal.aborted).toBe(true);
const first = session.send("first");
const parallel = other.send("other");
await flush();
const old = session.signal;
const isolated = other.signal;
session.interrupt();
expect(old.aborted).toBe(true);
expect(isolated.aborted).toBe(false);
const second = session.send("second");
expect(session.signal.aborted).toBe(true);
harness.turns[0]?.finish();
await flush();
const fresh = session.signal;
expect(fresh).not.toBe(old);
expect(fresh.aborted).toBe(false);
expect(old.aborted).toBe(true);
session.finished("final-call");
expect(fresh.aborted).toBe(true);
expect(session.working).toBe(true);
await second;
harness.turns[2]?.finish();
await first;
await acp.close(other);
expect(isolated.aborted).toBe(true);
await parallel;
const failed = session.send("failure");
await flush();
const failing = session.signal;
session.fail("failure");
expect(failing.aborted).toBe(true);
await expect(failed).resolves.toMatchObject({ ok: false });
});

it("aborts work when a prompt fails, without waiting for a process close", async () => {
const harness = fakeHarness({
promptError: { code: -32000, message: "failed" },
});
const acp = await start(harness);
const session = await acp.open({ cwd: "/work", systemPrompt: "" });
const done = session.send("first");
const signal = session.signal;
expect(signal.aborted).toBe(false);
await expect(done).resolves.toMatchObject({ ok: false });
expect(signal.aborted).toBe(true);
expect(session.open).toBe(true);
});

it("forwards an aborted signal before session/new and while idle, and aborts active calls on process exit", async () => {
const fake = fakeGoose({ hold: true });
const signals: AbortSignal[] = [];
const handler: ToolServer = async (_conversation, message, signal) => {
if (!signal) throw new Error("Missing cancellation");
signals.push(signal);
return { jsonrpc: "2.0", id: message.id, result: {} };
};
const acp = await AcpProcess.start(fake.spawn, GOOSE, {
cwd: "/work",
serve: async (dispatch) => {
await dispatch({ id: 1, method: "tools/list" });
return fake.serve(dispatch);
},
});
const session = await acp.open({
cwd: "/work",
systemPrompt: "",
tools: handler,
});
expect(signals).toHaveLength(1);
expect(signals[0]?.aborted).toBe(true);
const process = fake.geese[0];
if (!process) throw new Error("Missing process");
await process.mcp(session.id, "tools/list");
expect(signals.every((signal) => signal.aborted)).toBe(true);
const done = session.send("first");
await vi.waitFor(() => expect(process.turns).toHaveLength(1));
await process.mcp(session.id, "tools/call", { name: "fixture" });
const active = signals.at(-1);
expect(active?.aborted).toBe(false);
process.exit(1, "failed");
await acp.exited;
expect(active?.aborted).toBe(true);
await expect(done).resolves.toMatchObject({ ok: false });
});

it("keeps the harness's error code on a failed turn", async () => {
const harness = fakeHarness({
promptError: { code: -32000, message: "Authentication required" },
Expand Down
10 changes: 5 additions & 5 deletions src/features/agent-harness/acp/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ export class AcpProcess {
): Promise<Wire | undefined> {
const { id, method = "", params } = (message ?? {}) as Wire;
if (id === undefined || id === null) return undefined;
const response = (await tools(session?.conversation, {
id,
method,
params,
})) as Wire;
const response = (await tools(
session?.conversation,
{ id, method, params },
session?.signal ?? AbortSignal.abort(new Error("The turn has ended")),
)) as Wire;
const call = (params ?? {}) as {
name?: unknown;
arguments?: { final?: unknown };
Expand Down
Loading
Loading