| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366 |
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- HookEventName,
- SessionEventType,
- TASK_OUTPUT_PROVIDER_DESCRIPTION,
- TASK_OUTPUT_TOOL_NAME,
- createRootTraceContext,
- createSessionId,
- createToolCallId,
- createTurnId,
- type BackgroundExecutionSnapshot,
- type ExecutionResult,
- } from "@zcode/contracts";
- import { createInMemoryHookRunner } from "../src/hooks/index.js";
- import { PermissionService, defaultPermissionConfig } from "../src/permission/service.js";
- import {
- InMemoryRuntimeTaskRegistry,
- type RuntimeTaskSnapshot,
- } from "../src/runtime-task/registry.js";
- import { createToolExecutor } from "../src/tool/executor.js";
- import { claimRuntimeBackgroundTaskNotification } from "../src/tool/executor/background-task-registry.js";
- import { BackgroundTaskTracker } from "../src/tool/executor/background-tasks.js";
- import {
- formatTaskOutputModelContent,
- resolveTaskOutputLength,
- taskOutputToolEntry,
- truncateTaskOutput,
- } from "../src/tool/handlers/task-output.js";
- import { createToolRegistry } from "../src/tool/registry.js";
- import type { ToolExecutionContext } from "../src/tool/types.js";
- afterEach(() => {
- vi.useRealTimers();
- vi.unstubAllEnvs();
- });
- describe("TaskOutput tool", () => {
- it("exposes the canonical provider contract and local-only aliases", () => {
- const registry = createToolRegistry();
- registry.register(taskOutputToolEntry);
- expect(registry.toContracts()).toEqual([
- expect.objectContaining({
- name: TASK_OUTPUT_TOOL_NAME,
- description: TASK_OUTPUT_PROVIDER_DESCRIPTION,
- inputSchema: taskOutputToolEntry.inputSchema,
- readOnly: true,
- concurrentSafe: true,
- needsApproval: false,
- sideEffectScope: "none",
- }),
- ]);
- expect(registry.toContracts()).toHaveLength(1);
- for (const alias of ["AgentOutputTool", "BashOutputTool", "AgentOutput", "BashOutput"]) {
- expect(registry.get(alias)?.handler).toBe(taskOutputToolEntry.handler);
- }
- });
- it("returns not_ready for a running Agent even when its output file is absent", async () => {
- const registry = registryWithTask(runtimeTask());
- const output = await taskOutputToolEntry.handler(
- { task_id: "agent_1", block: false },
- toolContext(registry),
- );
- expect(output).toMatchObject({
- retrieval_status: "not_ready",
- task: {
- task_id: "agent_1",
- task_type: "local_agent",
- status: "running",
- output: "",
- },
- });
- });
- it("marks terminal tasks notified and emits the provider result XML", async () => {
- const registry = registryWithTask(
- runtimeTask({
- status: "completed",
- output: completedAgentOutput("answer <raw>&\n"),
- }),
- );
- const output = await taskOutputToolEntry.handler(
- { task_id: "agent_1", block: false },
- toolContext(registry),
- );
- expect(registry.get("agent_1")?.notified).toBe(true);
- expect(formatTaskOutputModelContent(output)).toBe(
- [
- "<retrieval_status>success</retrieval_status>",
- "<task_id>agent_1</task_id>",
- "<task_type>local_agent</task_type>",
- "<status>completed</status>",
- "<output>\nanswer <raw>&\n</output>",
- ].join("\n\n"),
- );
- });
- it.each([false, true])(
- "does not consume the completion claim when terminal projection fails (block=%s)",
- async (block) => {
- const taskId = `bash_projection_failure_${String(block)}`;
- const registry = registryWithTask(
- runtimeTask({
- agentId: taskId,
- agentType: "local_bash",
- taskId,
- taskType: "local_bash",
- type: "local_bash",
- status: "completed",
- }),
- );
- await expect(
- taskOutputToolEntry.handler(
- { task_id: taskId, block, timeout: 100 },
- toolContext(registry, {
- executionPort: {
- getBackgroundTask: async () => {
- throw new Error("background snapshot unavailable");
- },
- } as never,
- }),
- ),
- ).rejects.toThrow("background snapshot unavailable");
- expect(registry.get(taskId)?.notified).not.toBe(true);
- },
- );
- it.each([false, true])(
- "does not consume the completion claim when aborted during terminal projection (block=%s)",
- async (block) => {
- const taskId = `bash_projection_abort_${String(block)}`;
- const registry = registryWithTask(
- runtimeTask({
- agentId: taskId,
- agentType: "local_bash",
- taskId,
- taskType: "local_bash",
- type: "local_bash",
- status: "completed",
- }),
- );
- const controller = new AbortController();
- const result = executionResult("finished", "", 0);
- let resolveSnapshot!: (snapshot: BackgroundExecutionSnapshot | undefined) => void;
- const snapshotPromise = new Promise<BackgroundExecutionSnapshot | undefined>((resolve) => {
- resolveSnapshot = resolve;
- });
- const outputPromise = taskOutputToolEntry.handler(
- { task_id: taskId, block, timeout: 100 },
- toolContext(registry, {
- abortSignal: controller.signal,
- executionPort: {
- getBackgroundTask: () => snapshotPromise,
- } as never,
- }),
- );
- controller.abort();
- resolveSnapshot({
- taskId,
- status: "completed",
- startedAt: result.startedAt,
- completedAt: result.completedAt,
- result,
- });
- await expect(outputPromise).rejects.toMatchObject({ name: "AbortError" });
- expect(registry.get(taskId)?.notified).not.toBe(true);
- },
- );
- it("omits empty optional tags and formats task:null as retrieval status only", () => {
- expect(
- formatTaskOutputModelContent({
- retrieval_status: "not_ready",
- task: {
- task_id: "agent_1",
- task_type: "local_agent",
- status: "running",
- description: "Research task",
- output: " \n\t",
- exitCode: null,
- },
- }),
- ).toBe(
- [
- "<retrieval_status>not_ready</retrieval_status>",
- "<task_id>agent_1</task_id>",
- "<task_type>local_agent</task_type>",
- "<status>running</status>",
- ].join("\n\n"),
- );
- expect(formatTaskOutputModelContent({ retrieval_status: "timeout", task: null })).toBe(
- "<retrieval_status>timeout</retrieval_status>",
- );
- });
- it("prefers the completed Agent result over disk output and joins blocks with one newline", async () => {
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-agent-"));
- const outputFile = join(outputRoot, "task.output");
- await writeFile(outputFile, "raw transcript", "utf8");
- const registry = registryWithTask(
- runtimeTask({
- status: "completed",
- outputFile,
- output: completedAgentOutput("first", "second"),
- }),
- );
- try {
- const output = (await taskOutputToolEntry.handler(
- { task_id: "agent_1", block: false },
- toolContext(registry),
- )) as any;
- expect(output.task.output).toBe("first\nsecond");
- expect(output.task.result).toBe("first\nsecond");
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("falls back to Bash snapshot stdout then stderr and includes exit code", async () => {
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "completed",
- }),
- );
- const result = executionResult("stdout", "stderr", 7);
- const output = await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: false },
- toolContext(registry, {
- executionPort: {
- async run() {
- return result;
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "completed",
- startedAt: result.startedAt,
- completedAt: result.completedAt,
- outputPath: "/tmp/bash_1.output",
- result,
- };
- },
- },
- }),
- );
- expect(output).toMatchObject({
- retrieval_status: "success",
- task: {
- task_id: "bash_1",
- task_type: "local_bash",
- output: "stdout\nstderr",
- exitCode: 7,
- },
- });
- expect((output as any).task.outputFile).toBeUndefined();
- expect(formatTaskOutputModelContent(output)).toContain("<exit_code>7</exit_code>");
- });
- /**
- * dwf run 的产物回投(桌面实测 bug)。dwf 从不写 outputFile,所以泛型投影只会回空串——
- * 产物必须从 registry 条目的 `resultText` 上取。
- */
- function dwfRunTask(overrides: Partial<RuntimeTaskSnapshot> = {}): RuntimeTaskSnapshot {
- return runtimeTask({
- agentId: "dwfrun_1",
- agentType: "local_dynamic_workflow",
- description: "Dynamic workflow run",
- status: "completed",
- taskId: "dwfrun_1",
- taskType: "local_dynamic_workflow",
- type: "local_dynamic_workflow",
- ...overrides,
- });
- }
- it("returns the stored artifact text for a settled dynamic workflow run", async () => {
- const registry = registryWithTask(dwfRunTask({ resultText: "the final answer" }));
- const output = await taskOutputToolEntry.handler(
- { task_id: "dwfrun_1", block: false },
- toolContext(registry),
- );
- expect(output).toMatchObject({
- retrieval_status: "success",
- task: {
- task_id: "dwfrun_1",
- task_type: "local_dynamic_workflow",
- status: "completed",
- output: "the final answer",
- result: "the final answer",
- },
- });
- expect(formatTaskOutputModelContent(output)).toContain("<output>\nthe final answer\n</output>");
- });
- it("returns an empty output for a dynamic workflow run with no stored artifact", async () => {
- const registry = registryWithTask(dwfRunTask());
- const output = await taskOutputToolEntry.handler(
- { task_id: "dwfrun_1", block: false },
- toolContext(registry),
- );
- expect(output).toMatchObject({
- retrieval_status: "success",
- task: { task_id: "dwfrun_1", output: "" },
- });
- expect((output as { task: Record<string, unknown> }).task.result).toBeUndefined();
- });
- it("passes a dynamic workflow run failure through as error", async () => {
- const registry = registryWithTask(
- dwfRunTask({ status: "failed", error: "script threw TypeError" }),
- );
- const output = await taskOutputToolEntry.handler(
- { task_id: "dwfrun_1", block: false },
- toolContext(registry),
- );
- expect(output).toMatchObject({
- task: { status: "failed", error: "script threw TypeError" },
- });
- });
- it("reads the canonical Bash output file when the execution snapshot has no streams", async () => {
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-bash-"));
- const outputFile = join(outputRoot, "bash.output");
- await writeFile(outputFile, "persisted bash output", "utf8");
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "completed",
- outputFile,
- exitCode: 9,
- }),
- );
- try {
- const output = await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: false },
- toolContext(registry, {
- executionPort: {
- async run() {
- return executionResult("", "", 0);
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "completed",
- startedAt: new Date("2026-07-25T00:00:00.000Z"),
- completedAt: new Date("2026-07-25T00:00:01.000Z"),
- outputPath: outputFile,
- };
- },
- },
- }),
- );
- expect(output).toMatchObject({
- task: {
- output: "persisted bash output",
- exitCode: 9,
- outputFile,
- },
- });
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("reads running Bash output from the canonical file instead of progress tails", async () => {
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-bash-running-"));
- const outputFile = join(outputRoot, "bash.output");
- await writeFile(outputFile, "stdout-1\nstderr-1\nstdout-2\n", "utf8");
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "running",
- outputFile,
- }),
- );
- try {
- const output = await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: false },
- toolContext(registry, {
- executionPort: {
- async run() {
- return executionResult("", "", 0);
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "running",
- startedAt: new Date("2026-07-25T00:00:00.000Z"),
- outputPath: outputFile,
- stdoutPersistedOutputPath: outputFile,
- stdoutTail: "stale tail",
- };
- },
- },
- }),
- );
- expect(output).toMatchObject({
- retrieval_status: "not_ready",
- task: {
- output: "stdout-1\nstderr-1\nstdout-2\n",
- outputFile,
- status: "running",
- },
- });
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("reads a fixed 30000-byte prefix from a running Bash output file", async () => {
- vi.stubEnv("BASH_MAX_OUTPUT_LENGTH", "1");
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-bash-running-prefix-"));
- const outputFile = join(outputRoot, "bash.output");
- const expectedPrefix = "a".repeat(30_000);
- await writeFile(outputFile, `${expectedPrefix}${"z".repeat(10_000)}`, "utf8");
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "running",
- outputFile,
- }),
- );
- try {
- const output = (await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: false },
- toolContext(registry, {
- executionPort: {
- async run() {
- return executionResult("", "", 0);
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "running",
- startedAt: new Date("2026-07-25T00:00:00.000Z"),
- outputPath: outputFile,
- stdoutPersistedOutputPath: outputFile,
- stdoutTail: "stale tail",
- };
- },
- },
- }),
- )) as any;
- expect(output).toMatchObject({
- retrieval_status: "not_ready",
- task: {
- output: expectedPrefix,
- outputFile,
- status: "running",
- },
- });
- expect(formatTaskOutputModelContent(output)).not.toContain("[Truncated. Full output:");
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("applies the Bash prefix limit before TaskOutput truncation after a blocking timeout", async () => {
- vi.stubEnv("TASK_MAX_OUTPUT_LENGTH", "20000");
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-bash-timeout-prefix-"));
- const outputFile = join(outputRoot, "bash.output");
- const expectedPrefix = `${"a".repeat(29_999)}P`;
- await writeFile(outputFile, `${expectedPrefix}${"z".repeat(10_000)}`, "utf8");
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "running",
- outputFile,
- }),
- );
- try {
- const output = (await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: true, timeout: 0 },
- toolContext(registry, {
- executionPort: {
- async run() {
- return executionResult("", "", 0);
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "running",
- startedAt: new Date("2026-07-25T00:00:00.000Z"),
- outputPath: outputFile,
- stdoutPersistedOutputPath: outputFile,
- };
- },
- },
- }),
- )) as any;
- expect(output).toMatchObject({
- retrieval_status: "timeout",
- task: {
- output: expectedPrefix,
- outputFile,
- status: "running",
- },
- });
- const modelContent = formatTaskOutputModelContent(output);
- expect(modelContent).toContain(`[Truncated. Full output: ${outputFile}]`);
- expect(modelContent).toMatch(/P\n<\/output>$/u);
- expect(modelContent).not.toMatch(/z\n<\/output>$/u);
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("falls back to Bash snapshot tails when the canonical output file is unavailable", async () => {
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "running",
- }),
- );
- const output = await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: false },
- toolContext(registry, {
- executionPort: {
- async run() {
- return executionResult("", "", 0);
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "running",
- startedAt: new Date("2026-07-25T00:00:00.000Z"),
- outputPath: "/missing/bash.output",
- stdoutPersistedOutputPath: "/missing/bash.output",
- stdoutTail: "merged tail",
- stderrTail: "framework warning",
- };
- },
- },
- }),
- );
- expect(output).toMatchObject({
- retrieval_status: "not_ready",
- task: {
- output: "merged tail\nframework warning",
- },
- });
- expect((output as any).task.outputFile).toBeUndefined();
- });
- it("keeps legacy split Bash files readable without advertising either as complete output", async () => {
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-bash-legacy-split-"));
- const stdoutPath = join(outputRoot, "stdout.log");
- const stderrPath = join(outputRoot, "stderr.log");
- const stdout = `stdout-${"s".repeat(20_000)}`;
- const stderr = `stderr-${"e".repeat(20_000)}`;
- await Promise.all([
- writeFile(stdoutPath, stdout, "utf8"),
- writeFile(stderrPath, stderr, "utf8"),
- ]);
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "completed",
- outputFile: stdoutPath,
- }),
- );
- const result = executionResult("capped stdout", "capped stderr", 7);
- result.stdout.truncated = true;
- result.stdout.artifactPath = stdoutPath;
- result.stderr.truncated = true;
- result.stderr.artifactPath = stderrPath;
- try {
- const output = (await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: false },
- toolContext(registry, {
- executionPort: {
- async run() {
- return result;
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "completed",
- startedAt: result.startedAt,
- completedAt: result.completedAt,
- outputPath: stdoutPath,
- stdoutPersistedOutputPath: stdoutPath,
- stderrPersistedOutputPath: stderrPath,
- result,
- };
- },
- },
- }),
- )) as any;
- expect(output).toMatchObject({
- retrieval_status: "success",
- task: {
- exitCode: 7,
- output: `${stdout}\n${stderr}`,
- },
- });
- expect(output.task.outputFile).toBeUndefined();
- const modelContent = formatTaskOutputModelContent(output);
- expect(modelContent).toContain(`${stdout}\n${stderr}`);
- expect(modelContent).not.toContain("[Truncated. Full output:");
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("uses the canonical Bash file as the truthful Full output target", async () => {
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-bash-full-output-"));
- const outputFile = join(outputRoot, "bash.output");
- const fullOutput = `first-line\n${"x".repeat(40_000)}\nlast-line`;
- await writeFile(outputFile, fullOutput, "utf8");
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "completed",
- outputFile,
- }),
- );
- try {
- const output = (await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: false },
- toolContext(registry, {
- executionPort: {
- async run() {
- return executionResult("", "", 0);
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "completed",
- startedAt: new Date("2026-07-25T00:00:00.000Z"),
- completedAt: new Date("2026-07-25T00:00:01.000Z"),
- outputPath: outputFile,
- stdoutPersistedOutputPath: outputFile,
- };
- },
- },
- }),
- )) as any;
- expect(output.task.output).toBe(fullOutput);
- expect(output.task.outputFile).toBe(outputFile);
- expect(formatTaskOutputModelContent(output)).toContain(
- `[Truncated. Full output: ${outputFile}]`,
- );
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("does not advertise the canonical file when framework stderr is appended", async () => {
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-bash-framework-error-"));
- const outputFile = join(outputRoot, "bash.output");
- await writeFile(outputFile, "merged command output", "utf8");
- const registry = registryWithTask(
- runtimeTask({
- agentId: "bash_1",
- agentType: "local_bash",
- taskId: "bash_1",
- taskType: "local_bash",
- type: "local_bash",
- status: "completed",
- outputFile,
- }),
- );
- const result = executionResult("", "framework warning", 1);
- try {
- const output = (await taskOutputToolEntry.handler(
- { task_id: "bash_1", block: false },
- toolContext(registry, {
- executionPort: {
- async run() {
- return result;
- },
- async getBackgroundTask() {
- return {
- taskId: "bash_1",
- status: "completed",
- startedAt: result.startedAt,
- completedAt: result.completedAt,
- outputPath: outputFile,
- stdoutPersistedOutputPath: outputFile,
- result,
- };
- },
- },
- }),
- )) as any;
- expect(output.task.output).toBe("merged command output\nframework warning");
- expect(output.task.outputFile).toBeUndefined();
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("uses the Agent output file and error when no completed result is available", async () => {
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-agent-failed-"));
- const outputFile = join(outputRoot, "agent.output");
- await writeFile(outputFile, "partial agent output", "utf8");
- const registry = registryWithTask(
- runtimeTask({
- status: "failed",
- error: "agent failed",
- outputFile,
- }),
- );
- try {
- const output = await taskOutputToolEntry.handler(
- { task_id: "agent_1", block: false },
- toolContext(registry),
- );
- expect(output).toMatchObject({
- retrieval_status: "success",
- task: {
- status: "failed",
- output: "partial agent output",
- error: "agent failed",
- },
- });
- expect(formatTaskOutputModelContent(output)).toContain("<error>agent failed</error>");
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("waits in 100ms steps and emits one progress event before returning success", async () => {
- vi.useFakeTimers();
- const registry = registryWithTask(runtimeTask());
- const events: Array<{ type: string; payload: unknown }> = [];
- const promise = taskOutputToolEntry.handler(
- { task_id: "agent_1", block: true, timeout: 1_000 },
- toolContext(registry, {
- emitEvent: async (event) => {
- events.push({ type: event.type, payload: event.payload });
- },
- }),
- );
- await vi.advanceTimersByTimeAsync(0);
- registry.update("agent_1", (task) => ({
- ...task,
- status: "completed",
- output: completedAgentOutput("finished"),
- }));
- await vi.advanceTimersByTimeAsync(100);
- await expect(promise).resolves.toMatchObject({
- retrieval_status: "success",
- task: { status: "completed", output: "finished" },
- });
- expect(events).toEqual([
- expect.objectContaining({
- type: SessionEventType.ToolCallProgress,
- payload: expect.objectContaining({
- toolName: TASK_OUTPUT_TOOL_NAME,
- elapsedMs: 0,
- }),
- }),
- ]);
- });
- it("returns timeout with the latest task, and task:null when the task disappears", async () => {
- const activeRegistry = registryWithTask(runtimeTask());
- await expect(
- taskOutputToolEntry.handler(
- { task_id: "agent_1", block: true, timeout: 0 },
- toolContext(activeRegistry),
- ),
- ).resolves.toMatchObject({
- retrieval_status: "timeout",
- task: { status: "running" },
- });
- vi.useFakeTimers();
- const removedRegistry = registryWithTask(runtimeTask());
- const removedPromise = taskOutputToolEntry.handler(
- { task_id: "agent_1", block: true, timeout: 1_000 },
- toolContext(removedRegistry),
- );
- await vi.advanceTimersByTimeAsync(0);
- removedRegistry.remove("agent_1");
- await vi.advanceTimersByTimeAsync(100);
- await expect(removedPromise).resolves.toEqual({
- retrieval_status: "timeout",
- task: null,
- });
- });
- it("throws AbortError while blocking", async () => {
- vi.useFakeTimers();
- const registry = registryWithTask(runtimeTask());
- const controller = new AbortController();
- const promise = taskOutputToolEntry.handler(
- { task_id: "agent_1", block: true, timeout: 1_000 },
- toolContext(registry, { abortSignal: controller.signal }),
- );
- const rejection = expect(promise).rejects.toMatchObject({ name: "AbortError" });
- await vi.advanceTimersByTimeAsync(0);
- controller.abort();
- await vi.advanceTimersByTimeAsync(100);
- await rejection;
- });
- it("returns the exact empty and unknown task failures from the handler", async () => {
- const registry = registryWithTask(runtimeTask());
- const context = toolContext(registry);
- await expect(taskOutputToolEntry.handler({ task_id: "" }, context)).resolves.toEqual({
- result: false,
- errorCode: 1,
- message: "Task ID is required",
- });
- await expect(taskOutputToolEntry.handler({ task_id: "missing" }, context)).resolves.toEqual({
- result: false,
- errorCode: 2,
- message: "No task found with ID: missing",
- });
- });
- it("projects schema and TaskOutput handler failures through the executor", async () => {
- const sessionId = createSessionId("task-output-validation");
- const turnId = createTurnId("task-output-validation");
- const traceContext = createRootTraceContext({ sessionId, turnId });
- const registry = createToolRegistry();
- registry.register(taskOutputToolEntry);
- const executor = createToolExecutor({
- emitEvent: async () => undefined,
- permissionService: new PermissionService(defaultPermissionConfig),
- registry,
- runtimeTaskRegistry: new InMemoryRuntimeTaskRegistry(),
- sessionId,
- turnId,
- traceContext,
- });
- const missingField = await executor.execute(
- {
- id: createToolCallId("task-output-missing-field"),
- input: {},
- name: TASK_OUTPUT_TOOL_NAME,
- },
- { traceContext },
- );
- expect(missingField.success).toBe(false);
- expect(missingField.error?.message).toBe("Tool input failed inputSchema validation");
- expect(missingField.modelContent).toBe(
- [
- "<tool_use_error>InputValidationError: TaskOutput failed due to the following issue:",
- "The required parameter `task_id` is missing</tool_use_error>",
- ].join("\n"),
- );
- for (const testCase of [
- {
- id: "task-output-empty-id",
- input: { task_id: "" },
- message: "Task ID is required",
- },
- {
- id: "task-output-unknown-id",
- input: { task_id: "missing" },
- message: "No task found with ID: missing",
- },
- ] as const) {
- const result = await executor.execute(
- {
- id: createToolCallId(testCase.id),
- input: testCase.input,
- name: TASK_OUTPUT_TOOL_NAME,
- },
- { traceContext },
- );
- expect(result.success).toBe(false);
- expect(result.error?.message).toBe(testCase.message);
- expect(result.modelContent).toBe(`<tool_use_error>${testCase.message}</tool_use_error>`);
- }
- });
- it("rejects empty and unknown task IDs before tool hooks and execution start", async () => {
- const sessionId = createSessionId("task-output-pre-hook-validation");
- const turnId = createTurnId("task-output-pre-hook-validation");
- const traceContext = createRootTraceContext({ sessionId, turnId });
- const registry = createToolRegistry();
- registry.register(taskOutputToolEntry);
- const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry();
- const events: SessionEventType[] = [];
- let preToolUseCalls = 0;
- let postToolUseFailureCalls = 0;
- const hookRunner = createInMemoryHookRunner({
- hooks: [
- {
- event: HookEventName.PreToolUse,
- matcher: TASK_OUTPUT_TOOL_NAME,
- callback: async () => {
- preToolUseCalls += 1;
- return {};
- },
- },
- {
- event: HookEventName.PostToolUseFailure,
- matcher: TASK_OUTPUT_TOOL_NAME,
- callback: async () => {
- postToolUseFailureCalls += 1;
- return {};
- },
- },
- ],
- });
- const executor = createToolExecutor({
- emitEvent: async (event) => {
- events.push(event.type);
- },
- hookRunner,
- permissionService: new PermissionService(defaultPermissionConfig),
- registry,
- runtimeTaskRegistry,
- sessionId,
- turnId,
- traceContext,
- });
- for (const testCase of [
- { input: { task_id: "" }, message: "Task ID is required" },
- { input: { task_id: "missing" }, message: "No task found with ID: missing" },
- ]) {
- const result = await executor.execute(
- {
- id: createToolCallId(`task-output-pre-hook-${testCase.input.task_id || "empty"}`),
- input: testCase.input,
- name: TASK_OUTPUT_TOOL_NAME,
- },
- { traceContext },
- );
- expect(result.success).toBe(false);
- expect(result.modelContent).toBe(`<tool_use_error>${testCase.message}</tool_use_error>`);
- }
- expect(preToolUseCalls).toBe(0);
- expect(postToolUseFailureCalls).toBe(0);
- expect(events).toEqual([SessionEventType.ToolCallError, SessionEventType.ToolCallError]);
- });
- it("tails generic task output at 8 MiB with the omitted-output prefix", async () => {
- const outputRoot = await mkdtemp(join(tmpdir(), "zcode-task-output-tail-"));
- const outputFile = join(outputRoot, "workflow.output");
- await writeFile(outputFile, `${"a".repeat(1024)}${"b".repeat(8 * 1024 * 1024)}`, "utf8");
- const registry = registryWithTask(
- runtimeTask({
- agentId: "workflow_1",
- agentType: "local_workflow",
- taskId: "workflow_1",
- taskType: "local_workflow",
- type: "local_workflow",
- status: "completed",
- outputFile,
- }),
- );
- try {
- const output = (await taskOutputToolEntry.handler(
- { task_id: "workflow_1", block: false },
- toolContext(registry),
- )) as any;
- expect(output.task.output).toHaveLength(
- 8 * 1024 * 1024 + "[1KB of earlier output omitted]\n".length,
- );
- expect(output.task.output).toMatch(/^\[1KB of earlier output omitted\]\n/u);
- expect(output.task.output.endsWith("b".repeat(1024))).toBe(true);
- } finally {
- await rm(outputRoot, { recursive: true, force: true });
- }
- });
- it("applies the source-aligned TASK_MAX_OUTPUT_LENGTH rules and truncation prefix", () => {
- expect(resolveTaskOutputLength(undefined)).toBe(32_000);
- expect(resolveTaskOutputLength("invalid")).toBe(32_000);
- expect(resolveTaskOutputLength("0")).toBe(32_000);
- expect(resolveTaskOutputLength("100suffix")).toBe(100);
- expect(resolveTaskOutputLength("200000")).toBe(160_000);
- const output = "x".repeat(40_000);
- const truncated = truncateTaskOutput(output, "/tmp/agent_1.output");
- expect(truncated).toHaveLength(32_000);
- expect(truncated).toMatch(/^\[Truncated\. Full output: \/tmp\/agent_1\.output\]\n\n/u);
- expect(truncated.endsWith("x".repeat(100))).toBe(true);
- const maximum = truncateTaskOutput("x".repeat(200_000), "/tmp/agent_1.output", "160000");
- expect(maximum).toHaveLength(160_000);
- const shortLimitPrefix = "[Truncated. Full output: /tmp/agent_1.output]\n\n";
- expect(truncateTaskOutput("x".repeat(100), "/tmp/agent_1.output", "10")).toBe(
- shortLimitPrefix + "x".repeat(100).slice(shortLimitPrefix.length - 10),
- );
- });
- it("persists mapped XML above 100000 characters with the persisted-output envelope", async () => {
- vi.stubEnv("TASK_MAX_OUTPUT_LENGTH", "160000");
- const sessionId = createSessionId("task-output-persist");
- const turnId = createTurnId("task-output-persist");
- const traceContext = createRootTraceContext({ sessionId, turnId });
- const registry = createToolRegistry();
- registry.register(taskOutputToolEntry);
- const runtimeTaskRegistry = registryWithTask(
- runtimeTask({
- status: "completed",
- output: completedAgentOutput("x".repeat(120_000)),
- outputFile: "/tmp/agent_1.output",
- }),
- );
- const persisted: Array<{ content: string; contentType?: string }> = [];
- const executor = createToolExecutor({
- artifactStore: {
- async writeToolResultArtifact(request) {
- persisted.push({
- content: request.content,
- contentType: request.contentType,
- });
- return {
- id: "artifact_task_output",
- uri: "artifact://task-output",
- path: "/tmp/tool-results/task-output.txt",
- bytes: Buffer.byteLength(request.content),
- contentType: request.contentType ?? "text/plain",
- createdAt: new Date(),
- };
- },
- async readToolResultArtifact() {
- throw new Error("not used");
- },
- },
- emitEvent: async () => undefined,
- permissionService: new PermissionService(defaultPermissionConfig),
- registry,
- runtimeTaskRegistry,
- sessionId,
- turnId,
- traceContext,
- });
- const result = await executor.execute(
- {
- id: createToolCallId("task-output-persist"),
- input: { task_id: "agent_1", block: false },
- name: TASK_OUTPUT_TOOL_NAME,
- },
- { traceContext },
- );
- expect(result.success).toBe(true);
- expect(persisted).toHaveLength(1);
- expect(persisted[0]?.contentType).toBe("text/plain");
- expect(persisted[0]?.content).toMatch(/^<retrieval_status>success<\/retrieval_status>/u);
- expect(persisted[0]?.content).toContain("<output>\n");
- const persistedContent = persisted[0]?.content ?? "";
- const formattedSize = `${(persistedContent.length / 1024).toFixed(1).replace(/\.0$/u, "")}KB`;
- expect(String(result.modelContent)).toMatch(
- new RegExp(
- `^<persisted-output>\\nOutput too large \\(${formattedSize.replace(".", "\\.")}\\)\\. Full output saved to: `,
- "u",
- ),
- );
- expect(String(result.modelContent)).toContain("\n...\n</persisted-output>");
- expect(String(result.modelContent)).not.toContain("Tool output truncated by resultBudget");
- });
- it("keeps the original XML when 100000-character artifact persistence fails", async () => {
- vi.stubEnv("TASK_MAX_OUTPUT_LENGTH", "160000");
- const sessionId = createSessionId("task-output-persist-failure");
- const turnId = createTurnId("task-output-persist-failure");
- const traceContext = createRootTraceContext({ sessionId, turnId });
- const registry = createToolRegistry();
- registry.register(taskOutputToolEntry);
- const runtimeTaskRegistry = registryWithTask(
- runtimeTask({
- status: "completed",
- output: completedAgentOutput("\u0800".repeat(150_000)),
- outputFile: "/tmp/agent_1.output",
- }),
- );
- const executor = createToolExecutor({
- artifactStore: {
- async writeToolResultArtifact() {
- throw new Error("disk full");
- },
- async readToolResultArtifact() {
- throw new Error("not used");
- },
- },
- emitEvent: async () => undefined,
- permissionService: new PermissionService(defaultPermissionConfig),
- registry,
- runtimeTaskRegistry,
- sessionId,
- turnId,
- traceContext,
- });
- const result = await executor.execute(
- {
- id: createToolCallId("task-output-persist-failure"),
- input: { task_id: "agent_1", block: false },
- name: TASK_OUTPUT_TOOL_NAME,
- },
- { traceContext },
- );
- expect(result.success).toBe(true);
- expect(String(result.modelContent)).toMatch(/^<retrieval_status>success<\/retrieval_status>/u);
- expect(String(result.modelContent)).toContain("\u0800".repeat(1_000));
- expect(String(result.modelContent)).not.toContain("<persisted-output>");
- expect(String(result.modelContent)).not.toContain("Tool output truncated by resultBudget");
- });
- it("uses notified as the single completion notification claim in both orderings", async () => {
- const taskOutputFirst = registryWithTask(runtimeTask({ status: "completed" }));
- await taskOutputToolEntry.handler(
- { task_id: "agent_1", block: false },
- toolContext(taskOutputFirst),
- );
- expect(
- claimRuntimeBackgroundTaskNotification(
- { runtimeTaskRegistry: taskOutputFirst } as never,
- { id: "tool_bash", input: {}, name: "Bash" },
- "agent_1",
- ),
- ).toBe(false);
- const completionFirst = registryWithTask(runtimeTask({ status: "completed" }));
- expect(
- claimRuntimeBackgroundTaskNotification(
- { runtimeTaskRegistry: completionFirst } as never,
- { id: "tool_bash", input: {}, name: "Bash" },
- "agent_1",
- ),
- ).toBe(true);
- await taskOutputToolEntry.handler(
- { task_id: "agent_1", block: false },
- toolContext(completionFirst),
- );
- expect(completionFirst.get("agent_1")?.notified).toBe(true);
- });
- it("does not enqueue a Bash completion after TaskOutput claimed the terminal task", async () => {
- const taskId = "bash_task_output_first";
- const runtimeTaskRegistry = registryWithTask(
- runtimeTask({
- agentId: taskId,
- agentType: "local_bash",
- taskId,
- taskType: "local_bash",
- type: "local_bash",
- status: "completed",
- exitCode: 0,
- }),
- );
- await taskOutputToolEntry.handler(
- { task_id: taskId, block: false },
- toolContext(runtimeTaskRegistry),
- );
- const sessionId = createSessionId("task-output-notification-race");
- const turnId = createTurnId("task-output-notification-race");
- const traceContext = createRootTraceContext({ sessionId, turnId });
- const notifications: string[] = [];
- let completeEvent = () => undefined;
- const completedEvent = new Promise<void>((resolve) => {
- completeEvent = resolve;
- });
- const tracker = new BackgroundTaskTracker({
- emitEvent: async (event: { type: SessionEventType }) => {
- if (event.type === SessionEventType.BackgroundTaskCompleted) completeEvent();
- },
- enqueueBackgroundTaskNotification: (notification: { text: string }) => {
- notifications.push(notification.text);
- },
- executionPort: {
- async run() {
- throw new Error("run should not be called");
- },
- async waitForBackgroundTask() {
- return {
- taskId,
- status: "completed" as const,
- startedAt: new Date("2026-07-25T00:00:00.000Z"),
- completedAt: new Date("2026-07-25T00:00:01.000Z"),
- result: executionResult("done", "", 0),
- };
- },
- },
- runtimeTaskRegistry,
- runtimeScope: "main",
- sessionId,
- } as never);
- await tracker.trackBackgroundTask(
- {
- id: "tool_bash_task_output_first",
- input: { command: "echo done" },
- name: "Bash",
- } as never,
- { backgroundTaskId: taskId, status: "backgrounded" },
- traceContext,
- turnId,
- );
- await completedEvent;
- expect(notifications).toEqual([]);
- expect(runtimeTaskRegistry.get(taskId)).toMatchObject({
- notified: true,
- status: "completed",
- exitCode: 0,
- });
- });
- });
- function runtimeTask(overrides: Partial<RuntimeTaskSnapshot> = {}): RuntimeTaskSnapshot {
- return {
- taskId: "agent_1",
- agentId: "agent_1",
- agentType: "general-purpose",
- description: "Research task",
- status: "running",
- startedAt: new Date("2026-07-25T00:00:00.000Z"),
- type: "local_agent",
- taskType: "local_agent",
- isBackgrounded: true,
- ...overrides,
- };
- }
- function registryWithTask(task: RuntimeTaskSnapshot): InMemoryRuntimeTaskRegistry {
- const registry = new InMemoryRuntimeTaskRegistry();
- registry.register(task);
- return registry;
- }
- function toolContext(
- registry: InMemoryRuntimeTaskRegistry,
- overrides: Partial<ToolExecutionContext> = {},
- ): ToolExecutionContext {
- return {
- abortSignal: new AbortController().signal,
- runtimeTaskRegistry: registry,
- sessionId: "sess_task_output" as never,
- toolCallId: "tool_task_output",
- traceId: "trace_task_output" as never,
- workingDirectory: "/tmp",
- workspaceRoot: "/tmp",
- ...overrides,
- };
- }
- function completedAgentOutput(...texts: string[]) {
- return {
- status: "completed" as const,
- agentId: "agent_1",
- agentType: "general-purpose",
- description: "Research task",
- prompt: "Research this",
- content: texts.map((text) => ({ type: "text" as const, text })),
- totalToolUseCount: 1,
- totalDurationMs: 10,
- totalTokens: 5,
- };
- }
- function executionResult(stdout: string, stderr: string, exitCode: number): ExecutionResult {
- const startedAt = new Date("2026-07-25T00:00:00.000Z");
- const completedAt = new Date("2026-07-25T00:00:01.000Z");
- return {
- status: "completed",
- exitCode,
- stdout: {
- text: stdout,
- bytes: Buffer.byteLength(stdout),
- truncated: false,
- },
- stderr: {
- text: stderr,
- bytes: Buffer.byteLength(stderr),
- truncated: false,
- },
- durationMs: 1_000,
- timedOut: false,
- cancelled: false,
- startedAt,
- completedAt,
- };
- }
|