From ca3a438a4e1f05d96c75854ddc3936fe96bdbbd9 Mon Sep 17 00:00:00 2001 From: creatixchu Date: Fri, 25 Sep 2026 15:38:45 +0800 Subject: [PATCH] test(ptc): give nested tool fixtures an owned lifecycle context --- .../ui-tool/tests/spill-policy-terminal.client.spec.ts | 4 ++-- packages/fs/tool-fs/tests/read-image.spec.ts | 5 +++-- packages/spill/spill-policy/tests/multimodal.spec.ts | 2 +- packages/spill/spill-policy/tests/spill-policy.spec.ts | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/client/ui-tool/tests/spill-policy-terminal.client.spec.ts b/packages/client/ui-tool/tests/spill-policy-terminal.client.spec.ts index 26c1b6edb4..6c90a09c6c 100644 --- a/packages/client/ui-tool/tests/spill-policy-terminal.client.spec.ts +++ b/packages/client/ui-tool/tests/spill-policy-terminal.client.spec.ts @@ -70,8 +70,8 @@ async function executeShell(text: string, nested: boolean, name = 'bash', maxInl async execute() { return [{ type: 'text', text }] }, }))) const session = Session.create(SessionId('spill-terminal')) - // The tool pipeline reads only the owner session; no agent loop runs here. - const agent = { session } as Agent + // Nested PTC registers retained-environment cleanup on the owning context. + const agent = { ctx, session } as Agent const callId = ToolCallId('shell-call') const result = await ctx.tools.execute({ callId, diff --git a/packages/fs/tool-fs/tests/read-image.spec.ts b/packages/fs/tool-fs/tests/read-image.spec.ts index 8b1378fb7f..625b727737 100644 --- a/packages/fs/tool-fs/tests/read-image.spec.ts +++ b/packages/fs/tool-fs/tests/read-image.spec.ts @@ -6,7 +6,7 @@ * regression that `read` keeps its text-only contract. */ -import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, onTestFinished } from 'vitest' import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -105,6 +105,7 @@ interface SetupOptions { async function setup(options: SetupOptions = {}) { const ctx = new Context() + onTestFinished(async () => { await ctx.fiber.dispose() }) await ctx.plugin(SystemPrompt) await ctx.plugin(ToolRuntime, { mode: options.toolMode ?? 'native' }) if (options.toolMode === 'ptc' || options.toolMode === 'both') { @@ -299,7 +300,7 @@ describe('read_image happy path', () => { const result = await call(ctx, RUN_CODE_NAME, { code: 'return await tools.read_image({ file_path: "red.png" })', description: 'Read the image through PTC mode', - }, agentOn('vision-model')) + }, { ...agentOn('vision-model'), ctx }) expect(result.isError).toBe(false) expect(result.content.every(block => block.type === 'text')).toBe(true) diff --git a/packages/spill/spill-policy/tests/multimodal.spec.ts b/packages/spill/spill-policy/tests/multimodal.spec.ts index f75d26ddb1..88da0cccf9 100644 --- a/packages/spill/spill-policy/tests/multimodal.spec.ts +++ b/packages/spill/spill-policy/tests/multimodal.spec.ts @@ -80,7 +80,7 @@ async function setup(content: JsonValue[], maxInlineTokens: number) { inputSchema: { type: 'object' }, call: async () => ({ content }), })) const session = Session.create(SessionId('mixed-spill')) - const agent = { session, options: { provider: 'vision', model: 'vision' } } as Agent + const agent = { ctx, session, options: { provider: 'vision', model: 'vision' } } as Agent const execute = (name = 'inspect') => ctx.tools.execute({ signal: new AbortController().signal, callId: ToolCallId('mixed-call'), name, arguments: name === 'run_code' ? { code: 'return await tools.inspect({})', description: 'Inspect a window' } : {}, agent, diff --git a/packages/spill/spill-policy/tests/spill-policy.spec.ts b/packages/spill/spill-policy/tests/spill-policy.spec.ts index 5b69856578..bb357a8d1f 100644 --- a/packages/spill/spill-policy/tests/spill-policy.spec.ts +++ b/packages/spill/spill-policy/tests/spill-policy.spec.ts @@ -50,7 +50,7 @@ async function mountRuntime(ctx: Context, config: NodeRuntimeConfig = {}): Promi function observedAgent(ctx: Context, id: string, observe: (type: string, data: unknown) => void) { const session = ctx.sessions.create(SessionId(id), { meta: { cwd: process.cwd() } }) ctx.on('session/event', (owner, event) => { if (owner === session) observe(event.type, event.data) }) - return { session } + return { ctx, session } } const testToolSignal = new AbortController().signal