Skip to content

Commit 7f35bb1

Browse files
salman1993jamadeogus
authored
fix(goose): enable deferred tool loading for goose agents (#3227)
Replaces #3143 (by @jamadeo), with the latest `main` merged in and squashed into one commit. ## What - Turns on goose's lookup tool deferral (aaif-goose/goose#13378): `GOOSE_TOOL_DEFERRAL=lookup` plus `GOOSE_TOOL_DEFERRAL_ALWAYS_LOAD`, so Buzz tools load only when they're needed. - Pins `goose-acp` to v1.59.0, the first release that includes it. - Leaves mesh sessions out: `meshGooseEnv` unsets both settings, with a TODO, since mesh hasn't been tested with deferral. - Adds a live test, `enables a deferred Buzz tool before executing it`, that checks the recorded provider requests. ## Notes - The known MCP credential non-persistence failure is still there. aaif-goose/goose#12908 was closed without merging, so 1.59.0 doesn't fix it. The note in `docs/releases.md` and the comment in `goose.ts` stay, updated to v1.59.0, using the same wording as #3209. - #3209 also pins 1.59.0. Whichever PR merges second will get small conflicts on the same lines. ## Testing - `tsc` and `biome` pass. - The `src/bundled/goose` and `src/buzz-mcp` tests pass (750). - Before the main merge, the live tests passed against the real 1.59.0 binary. They haven't been rerun since. --------- Signed-off-by: Salman Mohammed <[email protected]> Co-authored-by: Jack Amadeo <[email protected]> Co-authored-by: gus <09b93b3b4074c080c50c23964a0cd36f95bdf1446c55342394f1ac8386fae44a@buzz.block.builderlab.xyz>
1 parent 9fa3524 commit 7f35bb1

5 files changed

Lines changed: 183 additions & 3 deletions

File tree

‎src/bundled/goose/goose.live.test.ts‎

Lines changed: 164 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ type Send = {
180180

181181
/** A goose whose Buzz tool calls and own requests are recorded; sends
182182
* succeed without reaching a relay. */
183-
async function live() {
183+
async function live(env?: Readonly<Record<string, string | null>>) {
184184
const calls: Call[] = [];
185185
const tools: ToolServer = async (conversation, message) => {
186186
calls.push({
@@ -197,7 +197,11 @@ async function live() {
197197
return respond(undefined, {}, message);
198198
};
199199
const cwd = mkdtempSync(join(tmpdir(), "buzz-goose-"));
200-
const goose = await GooseProcess.start(spawn, { cwd, serve });
200+
const goose = await GooseProcess.start(spawn, {
201+
cwd,
202+
serve,
203+
...(env && { env }),
204+
});
201205
const requests: { method: string; ok?: boolean }[] = [];
202206
/** What each request that succeeded answered, in order. */
203207
const results: { method: string; result: unknown }[] = [];
@@ -231,6 +235,164 @@ const homeSetup = () =>
231235
});
232236

233237
describe.skipIf(!BIN)("goose-acp", () => {
238+
it("enables a deferred Buzz tool before executing it", {
239+
timeout: 180_000,
240+
}, async () => {
241+
const catalogs: string[][] = [];
242+
const provider = createServer(async (request, response) => {
243+
if (request.method === "GET") {
244+
response.writeHead(200, { "Content-Type": "application/json" });
245+
response.end(
246+
JSON.stringify({
247+
object: "list",
248+
data: [{ id: "gpt-4o", object: "model" }],
249+
}),
250+
);
251+
return;
252+
}
253+
let body = "";
254+
for await (const chunk of request) body += chunk;
255+
const input = JSON.parse(body) as {
256+
tools?: { function: { name: string } }[];
257+
};
258+
catalogs.push((input.tools ?? []).map((tool) => tool.function.name));
259+
const step = catalogs.length;
260+
const call =
261+
step === 1
262+
? {
263+
id: "enable-interest",
264+
type: "function",
265+
function: {
266+
name: "enable_tools",
267+
arguments: JSON.stringify({
268+
tool_names: ["buzz__interest_list"],
269+
}),
270+
},
271+
}
272+
: {
273+
id: "list-interests",
274+
type: "function",
275+
function: {
276+
name: "buzz__interest_list",
277+
arguments: "{}",
278+
},
279+
};
280+
const delta =
281+
step <= 2
282+
? { role: "assistant", tool_calls: [{ index: 0, ...call }] }
283+
: { role: "assistant", content: "done" };
284+
response.writeHead(200, { "Content-Type": "text/event-stream" });
285+
for (const choice of [
286+
{ index: 0, delta, finish_reason: null },
287+
{
288+
index: 0,
289+
delta: {},
290+
finish_reason: step <= 2 ? "tool_calls" : "stop",
291+
},
292+
]) {
293+
response.write(
294+
`data: ${JSON.stringify({
295+
id: `response-${step}`,
296+
object: "chat.completion.chunk",
297+
created: 1,
298+
model: "gpt-4o",
299+
choices: [choice],
300+
})}\n\n`,
301+
);
302+
}
303+
response.end("data: [DONE]\n\n");
304+
});
305+
await new Promise<void>((resolve) =>
306+
provider.listen(0, "127.0.0.1", resolve),
307+
);
308+
const address = provider.address();
309+
if (!address || typeof address === "string")
310+
throw new Error("No provider address");
311+
const { goose, cwd, tools, calls } = await live({
312+
OPENAI_API_KEY: "local-test-only",
313+
OPENAI_HOST: `http://127.0.0.1:${address.port}`,
314+
OPENAI_BASE_PATH: "v1/chat/completions",
315+
OPENAI_BASE_URL: null,
316+
GOOSE_DISABLE_SESSION_NAMING: "true",
317+
});
318+
try {
319+
const session = await goose.open({
320+
cwd,
321+
tools,
322+
choice: () => ({ provider: "openai", model: "gpt-4o" }),
323+
systemPrompt: "You are a Buzz agent.",
324+
});
325+
const result = await session.send("List my interests, then reply done.");
326+
expect(result.ok).toBe(true);
327+
expect(catalogs).toHaveLength(3);
328+
expect(catalogs[0]).toContain("enable_tools");
329+
expect(catalogs[0]).toContain("buzz__send");
330+
expect(catalogs[0]).not.toContain("buzz__interest_list");
331+
expect(catalogs[1]).toContain("buzz__interest_list");
332+
expect(
333+
calls.some(
334+
(call) =>
335+
call.method === "tools/call" &&
336+
(call.params as Send).name === "interest_list",
337+
),
338+
).toBe(true);
339+
const exported = (await goose.request("_goose/unstable/session/export", {
340+
sessionId: session.id,
341+
format: "json",
342+
})) as { data: string };
343+
type Content = {
344+
type: string;
345+
id?: string;
346+
toolCall?: { status: string; value: { name: string } };
347+
toolResult?: {
348+
status: string;
349+
value: {
350+
isError?: boolean;
351+
structuredContent?: { enabled: string[] };
352+
};
353+
};
354+
};
355+
const history = JSON.parse(exported.data) as {
356+
conversation: { role: string; content: Content[] }[];
357+
};
358+
const events = history.conversation.flatMap((message) =>
359+
message.content.map((content) => ({ role: message.role, ...content })),
360+
);
361+
const enabledAt = events.findIndex(
362+
(event) =>
363+
event.role === "assistant" &&
364+
event.type === "toolRequest" &&
365+
event.toolCall?.status === "success" &&
366+
event.toolCall.value.name === "enable_tools",
367+
);
368+
expect(enabledAt).toBeGreaterThanOrEqual(0);
369+
const activation = events.findIndex(
370+
(event) =>
371+
event.type === "toolResponse" &&
372+
event.id === events[enabledAt]?.id &&
373+
event.toolResult?.status === "success" &&
374+
event.toolResult.value.isError !== true &&
375+
event.toolResult.value.structuredContent?.enabled.includes(
376+
"buzz__interest_list",
377+
),
378+
);
379+
expect(activation).toBeGreaterThan(enabledAt);
380+
const executedAt = events.findIndex(
381+
(event) =>
382+
event.role === "assistant" &&
383+
event.type === "toolRequest" &&
384+
event.toolCall?.status === "success" &&
385+
event.toolCall.value.name === "buzz__interest_list",
386+
);
387+
expect(executedAt).toBeGreaterThan(activation);
388+
} finally {
389+
await goose.kill();
390+
await new Promise<void>((resolve, reject) =>
391+
provider.close((error) => (error ? reject(error) : resolve())),
392+
);
393+
}
394+
});
395+
234396
it("answers with the Buzz tools, the Buzz prompt and a title", {
235397
timeout: 180_000,
236398
}, async () => {

‎src/bundled/goose/goose.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { sessionTools } from "../../buzz-mcp/session-tools";
12
import { afterEach, describe, expect, it, vi } from "vitest";
23
import {
34
GooseProcess,
@@ -98,6 +99,11 @@ describe("GooseProcess", () => {
9899
expect(process.options.cwd).toBe("/work");
99100
expect(process.options.env).toMatchObject({
100101
GOOSE_MODE: "auto",
102+
GOOSE_TOOL_DEFERRAL: "lookup",
103+
GOOSE_TOOL_DEFERRAL_ALWAYS_LOAD: sessionTools()
104+
.filter((tool) => tool.load === "always")
105+
.map((tool) => `buzz__${tool.name}`)
106+
.join(","),
101107
BUZZ_PRIVATE_KEY: null,
102108
BUZZ_AUTH_TAG: null,
103109
BUZZ_RELAY_URL: null,

‎src/bundled/goose/goose.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// goose on the shared ACP client: one `goose-acp` process, with goose's
22
// extensions for the system prompt, steering, and per-turn provider and model
33
// picks. Each session's Buzz tools reach goose over HTTP MCP, never a Buzz key.
4+
import { sessionTools } from "../../buzz-mcp/session-tools";
45
import {
56
AcpProcess,
67
REQUEST_TIMEOUT_MS,
@@ -56,6 +57,11 @@ const CONFIGURED = "goose";
5657
* model, which goose would prefer to each session's. */
5758
export const ENV: Readonly<Record<string, string | null>> = {
5859
GOOSE_MODE: "auto",
60+
GOOSE_TOOL_DEFERRAL: "lookup",
61+
GOOSE_TOOL_DEFERRAL_ALWAYS_LOAD: sessionTools()
62+
.filter((tool) => tool.load === "always")
63+
.map((tool) => `buzz__${tool.name}`)
64+
.join(","),
5965
BUZZ_PRIVATE_KEY: null,
6066
BUZZ_AUTH_TAG: null,
6167
BUZZ_RELAY_URL: null,

‎src/bundled/mesh-compute/meshGoose.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ describe("meshGooseEnv", () => {
4949
OPENAI_HOST: "http://127.0.0.1:4111",
5050
OPENAI_BASE_PATH: "lease-1/v1/chat/completions",
5151
OPENAI_API_KEY: "mesh-local",
52+
GOOSE_TOOL_DEFERRAL: null,
53+
GOOSE_TOOL_DEFERRAL_ALWAYS_LOAD: null,
5254
});
5355
});
5456
});

‎src/bundled/mesh-compute/meshGoose.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const MESH_EXTENSIONS: readonly PlatformExtension[] = [
4747
export function meshGooseEnv(
4848
baseUrl: string,
4949
root: string,
50-
): Record<string, string> {
50+
): Record<string, string | null> {
5151
const url = new URL(baseUrl);
5252
const path = url.pathname.replace(/^\/+|\/+$/g, "");
5353
return {
@@ -57,6 +57,10 @@ export function meshGooseEnv(
5757
OPENAI_BASE_PATH: `${path}/chat/completions`,
5858
// The loopback route takes no key; goose needs one to start the provider.
5959
OPENAI_API_KEY: "mesh-local",
60+
// TODO: enable goose's tool deferral for mesh once it is tested there.
61+
// Until then, unset the lookup deferral that `ENV` turns on for goose.
62+
GOOSE_TOOL_DEFERRAL: null,
63+
GOOSE_TOOL_DEFERRAL_ALWAYS_LOAD: null,
6064
};
6165
}
6266

0 commit comments

Comments
 (0)