@dreb/coding-agent 2.4.5 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -337,7 +337,7 @@ The `search` tool provides natural language queries over the codebase using embe
337
337
  **How it works:** The first query builds a project index (typically 10–60s, longer for very large repos). Subsequent queries use the cached index, with incremental re-indexing for changed files (mtime-based). Each unique `projectDir` gets its own independent index.
338
338
 
339
339
  **Indexing pipeline:**
340
- - AST-aware code chunking via tree-sitter (TypeScript, JavaScript, Python, Go, Rust, Java, C, C++) — extracts functions, classes, methods, and exports as individual chunks
340
+ - AST-aware code chunking via tree-sitter (TypeScript, JavaScript, Python, Go, Rust, Java, C, C++, GDScript) — extracts functions, classes, methods, and exports as individual chunks
341
341
  - Format-aware text chunking for non-code files (Markdown by heading, YAML/JSON/TOML by top-level key)
342
342
  - Local embeddings via all-MiniLM-L6-v2 (~23MB model, auto-downloaded on first use, cached at `~/.dreb/agent/models/`)
343
343
 
@@ -2,7 +2,7 @@
2
2
  name: code-reviewer
3
3
  description: Reviews code changes for correctness, idiomatic patterns, and maintainability
4
4
  tools: read, grep, find, ls, bash, search
5
- model: glm-5-turbo, sonnet
5
+ model: zai/glm-5-turbo, anthropic/sonnet
6
6
  ---
7
7
 
8
8
  You are a code reviewer. Your single question is: **"Does this code do what it should, correctly and idiomatically?"**
@@ -2,7 +2,7 @@
2
2
  name: completeness-checker
3
3
  description: Verifies a PR fully implements what the linked issue requires
4
4
  tools: read, grep, find, ls, bash, search
5
- model: glm-5-turbo, sonnet
5
+ model: zai/glm-5-turbo, anthropic/sonnet
6
6
  ---
7
7
 
8
8
  You are a completeness checker. Your single question is: **"Does this PR deliver everything the linked issue requires?"**
@@ -2,7 +2,7 @@
2
2
  name: error-auditor
3
3
  description: Audits code changes for silent runtime failures, missing error handling, and unsafe fallbacks
4
4
  tools: read, grep, find, ls, bash, search
5
- model: glm-5-turbo, sonnet
5
+ model: zai/glm-5-turbo, anthropic/sonnet
6
6
  ---
7
7
 
8
8
  You are an error auditor. Your single question is: **"What can go wrong silently at runtime?"**
package/agents/explore.md CHANGED
@@ -2,7 +2,7 @@
2
2
  name: Explore
3
3
  description: Codebase exploration — find files, search code, answer questions. Read-only.
4
4
  tools: read, grep, find, ls, bash, search
5
- model: glm-5-turbo, sonnet
5
+ model: zai/glm-5-turbo, anthropic/sonnet
6
6
  ---
7
7
 
8
8
  You are a codebase exploration agent. Your job is to quickly find information in the codebase and report back concisely.
@@ -2,7 +2,7 @@
2
2
  name: feature-dev
3
3
  description: Strong general-purpose coder for targeted implementation, fixes, and refactoring
4
4
  tools: read, write, edit, grep, find, ls, bash, search
5
- model: glm-5.1, opus
5
+ model: zai/glm-5.1, anthropic/opus
6
6
  ---
7
7
 
8
8
  You are a focused implementation agent. You receive specific direction — a plan, a finding to fix, or a task to implement — and you execute it precisely.
@@ -2,7 +2,7 @@
2
2
  name: independent-assessor
3
3
  description: Independently verifies review findings against actual source code — requires strongest available model
4
4
  tools: read, grep, find, ls, bash, search
5
- model: glm-5.1, opus
5
+ model: zai/glm-5.1, anthropic/opus
6
6
  ---
7
7
 
8
8
  You are an independent assessor. Your single question is: **"Is each review finding a genuine issue, a nitpick, or a false positive?"**
package/agents/sandbox.md CHANGED
@@ -2,7 +2,7 @@
2
2
  name: Sandbox
3
3
  description: Sandboxed analysis agent restricted to /tmp files only (no codebase access).
4
4
  tools: tmp_read
5
- model: glm-5-turbo, sonnet
5
+ model: zai/glm-5-turbo, anthropic/sonnet
6
6
  ---
7
7
 
8
8
  You are a sandboxed analysis agent. You have NO access to the project codebase.
@@ -2,7 +2,7 @@
2
2
  name: simplifier
3
3
  description: Identifies opportunities to simplify code without changing behavior
4
4
  tools: read, grep, find, ls, bash, search
5
- model: glm-5-turbo, sonnet
5
+ model: zai/glm-5-turbo, anthropic/sonnet
6
6
  ---
7
7
 
8
8
  You are a simplifier. Your single question is: **"Can this be expressed more clearly without changing behavior?"**
@@ -2,7 +2,7 @@
2
2
  name: test-reviewer
3
3
  description: Reviews test coverage and quality for changed code, identifying untested or poorly tested behaviors
4
4
  tools: read, grep, find, ls, bash, search
5
- model: glm-5-turbo, sonnet
5
+ model: zai/glm-5-turbo, anthropic/sonnet
6
6
  ---
7
7
 
8
8
  You are a test reviewer. Your single question is: **"What behaviors are untested or poorly tested?"**
@@ -1 +1 @@
1
- {"version":3,"file":"bash-executor.d.ts","sourceRoot":"","sources":["../../src/core/bash-executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,OAAO,EAAE,KAAK,cAAc,EAA6B,MAAM,iBAAiB,CAAC;AAOjF,MAAM,WAAW,mBAAmB;IACnC,+DAA+D;IAC/D,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,mCAAmC;IACnC,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,UAAU;IAC1B,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,mDAAmD;IACnD,SAAS,EAAE,OAAO,CAAC;IACnB,uCAAuC;IACvC,SAAS,EAAE,OAAO,CAAC;IACnB,yFAAyF;IACzF,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB;AAMD;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,UAAU,CAAC,CAE/F;AAED;;;GAGG;AACH,wBAAsB,yBAAyB,CAC9C,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,cAAc,EAC1B,OAAO,CAAC,EAAE,mBAAmB,GAC3B,OAAO,CAAC,UAAU,CAAC,CAqGrB","sourcesContent":["/**\n * Bash command execution with streaming support and cancellation.\n *\n * This module provides a unified bash execution implementation used by:\n * - AgentSession.executeBash() for interactive and RPC modes\n * - Direct calls from modes that need bash execution\n */\n\nimport { randomBytes } from \"node:crypto\";\nimport { createWriteStream, type WriteStream, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport stripAnsi from \"strip-ansi\";\nimport { sanitizeBinaryOutput } from \"../utils/shell.js\";\nimport { type BashOperations, createLocalBashOperations } from \"./tools/bash.js\";\nimport { DEFAULT_MAX_BYTES, truncateTail } from \"./tools/truncate.js\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface BashExecutorOptions {\n\t/** Callback for streaming output chunks (already sanitized) */\n\tonChunk?: (chunk: string) => void;\n\t/** AbortSignal for cancellation */\n\tsignal?: AbortSignal;\n}\n\nexport interface BashResult {\n\t/** Combined stdout + stderr output (sanitized, possibly truncated) */\n\toutput: string;\n\t/** Process exit code (undefined if killed/cancelled) */\n\texitCode: number | undefined;\n\t/** Whether the command was cancelled via signal */\n\tcancelled: boolean;\n\t/** Whether the output was truncated */\n\ttruncated: boolean;\n\t/** Path to temp file containing full output (if output exceeded truncation threshold) */\n\tfullOutputPath?: string;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * Execute a bash command with optional streaming and cancellation support.\n *\n * Uses the same local BashOperations backend as createBashTool() so interactive\n * user bash and tool-invoked bash share the same process spawning behavior.\n * Sanitization, newline normalization, temp-file capture, and truncation still\n * happen in executeBashWithOperations(), so reusing the local backend does not\n * change output processing behavior.\n *\n * @param command - The bash command to execute\n * @param options - Optional streaming callback and abort signal\n * @returns Promise resolving to execution result\n */\nexport function executeBash(command: string, options?: BashExecutorOptions): Promise<BashResult> {\n\treturn executeBashWithOperations(command, process.cwd(), createLocalBashOperations(), options);\n}\n\n/**\n * Execute a bash command using custom BashOperations.\n * Used for remote execution (SSH, containers, etc.).\n */\nexport async function executeBashWithOperations(\n\tcommand: string,\n\tcwd: string,\n\toperations: BashOperations,\n\toptions?: BashExecutorOptions,\n): Promise<BashResult> {\n\tconst outputChunks: string[] = [];\n\tlet outputBytes = 0;\n\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\n\n\tlet tempFilePath: string | undefined;\n\tlet tempFileStream: WriteStream | undefined;\n\tlet totalBytes = 0;\n\n\tconst decoder = new TextDecoder();\n\n\tconst onData = (data: Buffer) => {\n\t\ttotalBytes += data.length;\n\n\t\t// Sanitize: strip ANSI, replace binary garbage, normalize newlines\n\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\n\n\t\t// Start writing to temp file if exceeds threshold\n\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\tfor (const chunk of outputChunks) {\n\t\t\t\ttempFileStream.write(chunk);\n\t\t\t}\n\t\t}\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.write(text);\n\t\t}\n\n\t\t// Keep rolling buffer\n\t\toutputChunks.push(text);\n\t\toutputBytes += text.length;\n\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\n\t\t\tconst removed = outputChunks.shift()!;\n\t\t\toutputBytes -= removed.length;\n\t\t}\n\n\t\t// Stream to callback\n\t\tif (options?.onChunk) {\n\t\t\toptions.onChunk(text);\n\t\t}\n\t};\n\n\ttry {\n\t\tconst result = await operations.exec(command, cwd, {\n\t\t\tonData,\n\t\t\tsignal: options?.signal,\n\t\t});\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\tconst fullOutput = outputChunks.join(\"\");\n\t\tconst truncationResult = truncateTail(fullOutput);\n\n\t\t// If truncation occurred (e.g. by line count) but no temp file was\n\t\t// created (output was under the byte-streaming threshold), write the\n\t\t// full output to a temp file so the truncation message can reference it.\n\t\tif (truncationResult.truncated && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t}\n\n\t\tconst cancelled = options?.signal?.aborted ?? false;\n\n\t\treturn {\n\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\texitCode: cancelled ? undefined : (result.exitCode ?? undefined),\n\t\t\tcancelled,\n\t\t\ttruncated: truncationResult.truncated,\n\t\t\tfullOutputPath: tempFilePath,\n\t\t};\n\t} catch (err) {\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\t// Check if it was an abort\n\t\tif (options?.signal?.aborted) {\n\t\t\tconst fullOutput = outputChunks.join(\"\");\n\t\t\tconst truncationResult = truncateTail(fullOutput);\n\t\t\tif (truncationResult.truncated && !tempFilePath) {\n\t\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t\t}\n\t\t\treturn {\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\t\texitCode: undefined,\n\t\t\t\tcancelled: true,\n\t\t\t\ttruncated: truncationResult.truncated,\n\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t};\n\t\t}\n\n\t\tthrow err;\n\t}\n}\n"]}
1
+ {"version":3,"file":"bash-executor.d.ts","sourceRoot":"","sources":["../../src/core/bash-executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,OAAO,EAAE,KAAK,cAAc,EAA6B,MAAM,iBAAiB,CAAC;AAOjF,MAAM,WAAW,mBAAmB;IACnC,+DAA+D;IAC/D,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,mCAAmC;IACnC,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,UAAU;IAC1B,sEAAsE;IACtE,MAAM,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,mDAAmD;IACnD,SAAS,EAAE,OAAO,CAAC;IACnB,uCAAuC;IACvC,SAAS,EAAE,OAAO,CAAC;IACnB,yFAAyF;IACzF,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB;AAMD;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,UAAU,CAAC,CAE/F;AAED;;;GAGG;AACH,wBAAsB,yBAAyB,CAC9C,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,cAAc,EAC1B,OAAO,CAAC,EAAE,mBAAmB,GAC3B,OAAO,CAAC,UAAU,CAAC,CAuGrB","sourcesContent":["/**\n * Bash command execution with streaming support and cancellation.\n *\n * This module provides a unified bash execution implementation used by:\n * - AgentSession.executeBash() for interactive and RPC modes\n * - Direct calls from modes that need bash execution\n */\n\nimport { randomBytes } from \"node:crypto\";\nimport { createWriteStream, type WriteStream, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport stripAnsi from \"strip-ansi\";\nimport { sanitizeBinaryOutput } from \"../utils/shell.js\";\nimport { type BashOperations, createLocalBashOperations } from \"./tools/bash.js\";\nimport { DEFAULT_MAX_BYTES, truncateTail } from \"./tools/truncate.js\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface BashExecutorOptions {\n\t/** Callback for streaming output chunks (already sanitized) */\n\tonChunk?: (chunk: string) => void;\n\t/** AbortSignal for cancellation */\n\tsignal?: AbortSignal;\n}\n\nexport interface BashResult {\n\t/** Combined stdout + stderr output (sanitized, possibly truncated) */\n\toutput: string;\n\t/** Process exit code (undefined if killed/cancelled) */\n\texitCode: number | undefined;\n\t/** Whether the command was cancelled via signal */\n\tcancelled: boolean;\n\t/** Whether the output was truncated */\n\ttruncated: boolean;\n\t/** Path to temp file containing full output (if output exceeded truncation threshold) */\n\tfullOutputPath?: string;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * Execute a bash command with optional streaming and cancellation support.\n *\n * Uses the same local BashOperations backend as createBashTool() so interactive\n * user bash and tool-invoked bash share the same process spawning behavior.\n * Sanitization, newline normalization, temp-file capture, and truncation still\n * happen in executeBashWithOperations(), so reusing the local backend does not\n * change output processing behavior.\n *\n * @param command - The bash command to execute\n * @param options - Optional streaming callback and abort signal\n * @returns Promise resolving to execution result\n */\nexport function executeBash(command: string, options?: BashExecutorOptions): Promise<BashResult> {\n\treturn executeBashWithOperations(command, process.cwd(), createLocalBashOperations(), options);\n}\n\n/**\n * Execute a bash command using custom BashOperations.\n * Used for remote execution (SSH, containers, etc.).\n */\nexport async function executeBashWithOperations(\n\tcommand: string,\n\tcwd: string,\n\toperations: BashOperations,\n\toptions?: BashExecutorOptions,\n): Promise<BashResult> {\n\tconst outputChunks: string[] = [];\n\tlet outputBytes = 0;\n\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\n\n\tlet tempFilePath: string | undefined;\n\tlet tempFileStream: WriteStream | undefined;\n\tlet totalBytes = 0;\n\n\tconst decoder = new TextDecoder();\n\n\tconst onData = (data: Buffer) => {\n\t\ttotalBytes += data.length;\n\n\t\t// Sanitize: strip ANSI, replace binary garbage, normalize newlines\n\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\n\n\t\t// Start writing to temp file if exceeds threshold\n\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\t// Create the file synchronously so it exists immediately for downstream checks.\n\t\t\twriteFileSync(tempFilePath, \"\");\n\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\tfor (const chunk of outputChunks) {\n\t\t\t\ttempFileStream.write(chunk);\n\t\t\t}\n\t\t}\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.write(text);\n\t\t}\n\n\t\t// Keep rolling buffer\n\t\toutputChunks.push(text);\n\t\toutputBytes += text.length;\n\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\n\t\t\tconst removed = outputChunks.shift()!;\n\t\t\toutputBytes -= removed.length;\n\t\t}\n\n\t\t// Stream to callback\n\t\tif (options?.onChunk) {\n\t\t\toptions.onChunk(text);\n\t\t}\n\t};\n\n\ttry {\n\t\tconst result = await operations.exec(command, cwd, {\n\t\t\tonData,\n\t\t\tsignal: options?.signal,\n\t\t});\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\tconst fullOutput = outputChunks.join(\"\");\n\t\tconst truncationResult = truncateTail(fullOutput);\n\n\t\t// If truncation occurred (e.g. by line count) but no temp file was\n\t\t// created (output was under the byte-streaming threshold), write the\n\t\t// full output to a temp file so the truncation message can reference it.\n\t\tif (truncationResult.truncated && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t}\n\n\t\tconst cancelled = options?.signal?.aborted ?? false;\n\n\t\treturn {\n\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\texitCode: cancelled ? undefined : (result.exitCode ?? undefined),\n\t\t\tcancelled,\n\t\t\ttruncated: truncationResult.truncated,\n\t\t\tfullOutputPath: tempFilePath,\n\t\t};\n\t} catch (err) {\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\t// Check if it was an abort\n\t\tif (options?.signal?.aborted) {\n\t\t\tconst fullOutput = outputChunks.join(\"\");\n\t\t\tconst truncationResult = truncateTail(fullOutput);\n\t\t\tif (truncationResult.truncated && !tempFilePath) {\n\t\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t\t}\n\t\t\treturn {\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\t\texitCode: undefined,\n\t\t\t\tcancelled: true,\n\t\t\t\ttruncated: truncationResult.truncated,\n\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t};\n\t\t}\n\n\t\tthrow err;\n\t}\n}\n"]}
@@ -52,6 +52,8 @@ export async function executeBashWithOperations(command, cwd, operations, option
52
52
  if (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {
53
53
  const id = randomBytes(8).toString("hex");
54
54
  tempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);
55
+ // Create the file synchronously so it exists immediately for downstream checks.
56
+ writeFileSync(tempFilePath, "");
55
57
  tempFileStream = createWriteStream(tempFilePath);
56
58
  for (const chunk of outputChunks) {
57
59
  tempFileStream.write(chunk);
@@ -1 +1 @@
1
- {"version":3,"file":"bash-executor.js","sourceRoot":"","sources":["../../src/core/bash-executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAoB,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAuB,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACjF,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AA0BtE,+EAA+E;AAC/E,iBAAiB;AACjB,+E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,YAAY;aAC5B,CAAC;QACH,CAAC;QAED,MAAM,GAAG,CAAC;IACX,CAAC;AAAA,CACD","sourcesContent":["/**\n * Bash command execution with streaming support and cancellation.\n *\n * This module provides a unified bash execution implementation used by:\n * - AgentSession.executeBash() for interactive and RPC modes\n * - Direct calls from modes that need bash execution\n */\n\nimport { randomBytes } from \"node:crypto\";\nimport { createWriteStream, type WriteStream, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport stripAnsi from \"strip-ansi\";\nimport { sanitizeBinaryOutput } from \"../utils/shell.js\";\nimport { type BashOperations, createLocalBashOperations } from \"./tools/bash.js\";\nimport { DEFAULT_MAX_BYTES, truncateTail } from \"./tools/truncate.js\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface BashExecutorOptions {\n\t/** Callback for streaming output chunks (already sanitized) */\n\tonChunk?: (chunk: string) => void;\n\t/** AbortSignal for cancellation */\n\tsignal?: AbortSignal;\n}\n\nexport interface BashResult {\n\t/** Combined stdout + stderr output (sanitized, possibly truncated) */\n\toutput: string;\n\t/** Process exit code (undefined if killed/cancelled) */\n\texitCode: number | undefined;\n\t/** Whether the command was cancelled via signal */\n\tcancelled: boolean;\n\t/** Whether the output was truncated */\n\ttruncated: boolean;\n\t/** Path to temp file containing full output (if output exceeded truncation threshold) */\n\tfullOutputPath?: string;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * Execute a bash command with optional streaming and cancellation support.\n *\n * Uses the same local BashOperations backend as createBashTool() so interactive\n * user bash and tool-invoked bash share the same process spawning behavior.\n * Sanitization, newline normalization, temp-file capture, and truncation still\n * happen in executeBashWithOperations(), so reusing the local backend does not\n * change output processing behavior.\n *\n * @param command - The bash command to execute\n * @param options - Optional streaming callback and abort signal\n * @returns Promise resolving to execution result\n */\nexport function executeBash(command: string, options?: BashExecutorOptions): Promise<BashResult> {\n\treturn executeBashWithOperations(command, process.cwd(), createLocalBashOperations(), options);\n}\n\n/**\n * Execute a bash command using custom BashOperations.\n * Used for remote execution (SSH, containers, etc.).\n */\nexport async function executeBashWithOperations(\n\tcommand: string,\n\tcwd: string,\n\toperations: BashOperations,\n\toptions?: BashExecutorOptions,\n): Promise<BashResult> {\n\tconst outputChunks: string[] = [];\n\tlet outputBytes = 0;\n\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\n\n\tlet tempFilePath: string | undefined;\n\tlet tempFileStream: WriteStream | undefined;\n\tlet totalBytes = 0;\n\n\tconst decoder = new TextDecoder();\n\n\tconst onData = (data: Buffer) => {\n\t\ttotalBytes += data.length;\n\n\t\t// Sanitize: strip ANSI, replace binary garbage, normalize newlines\n\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\n\n\t\t// Start writing to temp file if exceeds threshold\n\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\tfor (const chunk of outputChunks) {\n\t\t\t\ttempFileStream.write(chunk);\n\t\t\t}\n\t\t}\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.write(text);\n\t\t}\n\n\t\t// Keep rolling buffer\n\t\toutputChunks.push(text);\n\t\toutputBytes += text.length;\n\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\n\t\t\tconst removed = outputChunks.shift()!;\n\t\t\toutputBytes -= removed.length;\n\t\t}\n\n\t\t// Stream to callback\n\t\tif (options?.onChunk) {\n\t\t\toptions.onChunk(text);\n\t\t}\n\t};\n\n\ttry {\n\t\tconst result = await operations.exec(command, cwd, {\n\t\t\tonData,\n\t\t\tsignal: options?.signal,\n\t\t});\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\tconst fullOutput = outputChunks.join(\"\");\n\t\tconst truncationResult = truncateTail(fullOutput);\n\n\t\t// If truncation occurred (e.g. by line count) but no temp file was\n\t\t// created (output was under the byte-streaming threshold), write the\n\t\t// full output to a temp file so the truncation message can reference it.\n\t\tif (truncationResult.truncated && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t}\n\n\t\tconst cancelled = options?.signal?.aborted ?? false;\n\n\t\treturn {\n\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\texitCode: cancelled ? undefined : (result.exitCode ?? undefined),\n\t\t\tcancelled,\n\t\t\ttruncated: truncationResult.truncated,\n\t\t\tfullOutputPath: tempFilePath,\n\t\t};\n\t} catch (err) {\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\t// Check if it was an abort\n\t\tif (options?.signal?.aborted) {\n\t\t\tconst fullOutput = outputChunks.join(\"\");\n\t\t\tconst truncationResult = truncateTail(fullOutput);\n\t\t\tif (truncationResult.truncated && !tempFilePath) {\n\t\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t\t}\n\t\t\treturn {\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\t\texitCode: undefined,\n\t\t\t\tcancelled: true,\n\t\t\t\ttruncated: truncationResult.truncated,\n\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t};\n\t\t}\n\n\t\tthrow err;\n\t}\n}\n"]}
1
+ {"version":3,"file":"bash-executor.js","sourceRoot":"","sources":["../../src/core/bash-executor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAoB,aAAa,EAAE,MA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WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;gBAC1C,YAAY,GAAG,IAAI,CAAC,MAAM,EAAE,EAAE,aAAa,EAAE,MAAM,CAAC,CAAC;gBACrD,aAAa,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;YACzC,CAAC;YACD,OAAO;gBACN,MAAM,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU;gBAC1E,QAAQ,EAAE,SAAS;gBACnB,SAAS,EAAE,IAAI;gBACf,SAAS,EAAE,gBAAgB,CAAC,SAAS;gBACrC,cAAc,EAAE,YAAY;aAC5B,CAAC;QACH,CAAC;QAED,MAAM,GAAG,CAAC;IACX,CAAC;AAAA,CACD","sourcesContent":["/**\n * Bash command execution with streaming support and cancellation.\n *\n * This module provides a unified bash execution implementation used by:\n * - AgentSession.executeBash() for interactive and RPC modes\n * - Direct calls from modes that need bash execution\n */\n\nimport { randomBytes } from \"node:crypto\";\nimport { createWriteStream, type WriteStream, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport stripAnsi from \"strip-ansi\";\nimport { sanitizeBinaryOutput } from \"../utils/shell.js\";\nimport { type BashOperations, createLocalBashOperations } from \"./tools/bash.js\";\nimport { DEFAULT_MAX_BYTES, truncateTail } from \"./tools/truncate.js\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface BashExecutorOptions {\n\t/** Callback for streaming output chunks (already sanitized) */\n\tonChunk?: (chunk: string) => void;\n\t/** AbortSignal for cancellation */\n\tsignal?: AbortSignal;\n}\n\nexport interface BashResult {\n\t/** Combined stdout + stderr output (sanitized, possibly truncated) */\n\toutput: string;\n\t/** Process exit code (undefined if killed/cancelled) */\n\texitCode: number | undefined;\n\t/** Whether the command was cancelled via signal */\n\tcancelled: boolean;\n\t/** Whether the output was truncated */\n\ttruncated: boolean;\n\t/** Path to temp file containing full output (if output exceeded truncation threshold) */\n\tfullOutputPath?: string;\n}\n\n// ============================================================================\n// Implementation\n// ============================================================================\n\n/**\n * Execute a bash command with optional streaming and cancellation support.\n *\n * Uses the same local BashOperations backend as createBashTool() so interactive\n * user bash and tool-invoked bash share the same process spawning behavior.\n * Sanitization, newline normalization, temp-file capture, and truncation still\n * happen in executeBashWithOperations(), so reusing the local backend does not\n * change output processing behavior.\n *\n * @param command - The bash command to execute\n * @param options - Optional streaming callback and abort signal\n * @returns Promise resolving to execution result\n */\nexport function executeBash(command: string, options?: BashExecutorOptions): Promise<BashResult> {\n\treturn executeBashWithOperations(command, process.cwd(), createLocalBashOperations(), options);\n}\n\n/**\n * Execute a bash command using custom BashOperations.\n * Used for remote execution (SSH, containers, etc.).\n */\nexport async function executeBashWithOperations(\n\tcommand: string,\n\tcwd: string,\n\toperations: BashOperations,\n\toptions?: BashExecutorOptions,\n): Promise<BashResult> {\n\tconst outputChunks: string[] = [];\n\tlet outputBytes = 0;\n\tconst maxOutputBytes = DEFAULT_MAX_BYTES * 2;\n\n\tlet tempFilePath: string | undefined;\n\tlet tempFileStream: WriteStream | undefined;\n\tlet totalBytes = 0;\n\n\tconst decoder = new TextDecoder();\n\n\tconst onData = (data: Buffer) => {\n\t\ttotalBytes += data.length;\n\n\t\t// Sanitize: strip ANSI, replace binary garbage, normalize newlines\n\t\tconst text = sanitizeBinaryOutput(stripAnsi(decoder.decode(data, { stream: true }))).replace(/\\r/g, \"\");\n\n\t\t// Start writing to temp file if exceeds threshold\n\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\t// Create the file synchronously so it exists immediately for downstream checks.\n\t\t\twriteFileSync(tempFilePath, \"\");\n\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\tfor (const chunk of outputChunks) {\n\t\t\t\ttempFileStream.write(chunk);\n\t\t\t}\n\t\t}\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.write(text);\n\t\t}\n\n\t\t// Keep rolling buffer\n\t\toutputChunks.push(text);\n\t\toutputBytes += text.length;\n\t\twhile (outputBytes > maxOutputBytes && outputChunks.length > 1) {\n\t\t\tconst removed = outputChunks.shift()!;\n\t\t\toutputBytes -= removed.length;\n\t\t}\n\n\t\t// Stream to callback\n\t\tif (options?.onChunk) {\n\t\t\toptions.onChunk(text);\n\t\t}\n\t};\n\n\ttry {\n\t\tconst result = await operations.exec(command, cwd, {\n\t\t\tonData,\n\t\t\tsignal: options?.signal,\n\t\t});\n\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\tconst fullOutput = outputChunks.join(\"\");\n\t\tconst truncationResult = truncateTail(fullOutput);\n\n\t\t// If truncation occurred (e.g. by line count) but no temp file was\n\t\t// created (output was under the byte-streaming threshold), write the\n\t\t// full output to a temp file so the truncation message can reference it.\n\t\tif (truncationResult.truncated && !tempFilePath) {\n\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t}\n\n\t\tconst cancelled = options?.signal?.aborted ?? false;\n\n\t\treturn {\n\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\texitCode: cancelled ? undefined : (result.exitCode ?? undefined),\n\t\t\tcancelled,\n\t\t\ttruncated: truncationResult.truncated,\n\t\t\tfullOutputPath: tempFilePath,\n\t\t};\n\t} catch (err) {\n\t\tif (tempFileStream) {\n\t\t\ttempFileStream.end();\n\t\t}\n\n\t\t// Check if it was an abort\n\t\tif (options?.signal?.aborted) {\n\t\t\tconst fullOutput = outputChunks.join(\"\");\n\t\t\tconst truncationResult = truncateTail(fullOutput);\n\t\t\tif (truncationResult.truncated && !tempFilePath) {\n\t\t\t\tconst id = randomBytes(8).toString(\"hex\");\n\t\t\t\ttempFilePath = join(tmpdir(), `dreb-bash-${id}.log`);\n\t\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t\t}\n\t\t\treturn {\n\t\t\t\toutput: truncationResult.truncated ? truncationResult.content : fullOutput,\n\t\t\t\texitCode: undefined,\n\t\t\t\tcancelled: true,\n\t\t\t\ttruncated: truncationResult.truncated,\n\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t};\n\t\t}\n\n\t\tthrow err;\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"bash.d.ts","sourceRoot":"","sources":["../../../src/core/tools/bash.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,KAAK,MAAM,EAAQ,MAAM,mBAAmB,CAAC;AAOtD,OAAO,KAAK,EAAE,cAAc,EAA2B,MAAM,wBAAwB,CAAC;AAItF,OAAO,EAAoD,KAAK,gBAAgB,EAAgB,MAAM,eAAe,CAAC;AAUtH,QAAA,MAAM,UAAU;;;EAGd,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC;AAEtD,MAAM,WAAW,eAAe;IAC/B,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC9B;;;;;;OAMG;IACH,IAAI,EAAE,CACL,OAAO,EAAE,MAAM,EACf,GAAG,EAAE,MAAM,EACX,OAAO,EAAE;QACR,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;QAC/B,MAAM,CAAC,EAAE,WAAW,CAAC;QACrB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;KACxB,KACG,OAAO,CAAC;QAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC,CAAC;CAC1C;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,IAAI,cAAc,CA2D1D;AAED,MAAM,WAAW,gBAAgB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC,UAAU,CAAC;CACvB;AAED,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,gBAAgB,KAAK,gBAAgB,CAAC;AAO5E,MAAM,WAAW,eAAe;IAC/B,oEAAoE;IACpE,UAAU,CAAC,EAAE,cAAc,CAAC;IAC5B,mFAAmF;IACnF,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,2DAA2D;IAC3D,SAAS,CAAC,EAAE,aAAa,CAAC;CAC1B;AAID,KAAK,eAAe,GAAG;IACtB,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC,OAAO,GAAG,SAAS,CAAC;CACrC,CAAC;AAwGF,wBAAgB,wBAAwB,CACvC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,eAAe,GACvB,cAAc,CAAC,OAAO,UAAU,EAAE,eAAe,GAAG,SAAS,EAAE,eAAe,CAAC,CAuKjF;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,SAAS,CAAC,OAAO,UAAU,CAAC,CAEnG;AAED,yEAAyE;AACzE,eAAO,MAAM,kBAAkB;;;iDAA0C,CAAC;AAC1E,eAAO,MAAM,QAAQ;;;QAAgC,CAAC","sourcesContent":["import { randomBytes } from \"node:crypto\";\nimport { createWriteStream, existsSync, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { AgentTool } from \"@dreb/agent-core\";\nimport { Container, Text, truncateToWidth } from \"@dreb/tui\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { spawn } from \"child_process\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.js\";\nimport { truncateToVisualLines } from \"../../modes/interactive/components/visual-truncate.js\";\nimport { theme } from \"../../modes/interactive/theme/theme.js\";\nimport { waitForChildProcess } from \"../../utils/child-process.js\";\nimport { getShellConfig, getShellEnv, killProcessTree } from \"../../utils/shell.js\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.js\";\nimport { getTextOutput, invalidArgText, str } from \"./render-utils.js\";\nimport { renderTerminalOutput } from \"./terminal-render.js\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.js\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateTail } from \"./truncate.js\";\n\n/**\n * Generate a unique temp file path for bash output.\n */\nfunction getTempFilePath(): string {\n\tconst id = randomBytes(8).toString(\"hex\");\n\treturn join(tmpdir(), `dreb-bash-${id}.log`);\n}\n\nconst bashSchema = Type.Object({\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n});\n\nexport type BashToolInput = Static<typeof bashSchema>;\n\nexport interface BashToolDetails {\n\ttruncation?: TruncationResult;\n\tfullOutputPath?: string;\n}\n\n/**\n * Pluggable operations for the bash tool.\n * Override these to delegate command execution to remote systems (for example SSH).\n */\nexport interface BashOperations {\n\t/**\n\t * Execute a command and stream output.\n\t * @param command The command to execute\n\t * @param cwd Working directory\n\t * @param options Execution options\n\t * @returns Promise resolving to exit code (null if killed)\n\t */\n\texec: (\n\t\tcommand: string,\n\t\tcwd: string,\n\t\toptions: {\n\t\t\tonData: (data: Buffer) => void;\n\t\t\tsignal?: AbortSignal;\n\t\t\ttimeout?: number;\n\t\t\tenv?: NodeJS.ProcessEnv;\n\t\t},\n\t) => Promise<{ exitCode: number | null }>;\n}\n\n/**\n * Create bash operations using dreb's built-in local shell execution backend.\n *\n * This is useful for extensions that intercept user_bash and still want dreb's\n * standard local shell behavior while wrapping or rewriting commands.\n */\nexport function createLocalBashOperations(): BashOperations {\n\treturn {\n\t\texec: (command, cwd, { onData, signal, timeout, env }) => {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tconst { shell, args } = getShellConfig();\n\t\t\t\tif (!existsSync(cwd)) {\n\t\t\t\t\treject(new Error(`Working directory does not exist: ${cwd}\\nCannot execute bash commands.`));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst child = spawn(shell, [...args, command], {\n\t\t\t\t\tcwd,\n\t\t\t\t\tdetached: true,\n\t\t\t\t\tenv: env ?? getShellEnv(),\n\t\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t\t});\n\t\t\t\tlet timedOut = false;\n\t\t\t\tlet timeoutHandle: NodeJS.Timeout | undefined;\n\t\t\t\t// Set timeout if provided.\n\t\t\t\tif (timeout !== undefined && timeout > 0) {\n\t\t\t\t\ttimeoutHandle = setTimeout(() => {\n\t\t\t\t\t\ttimedOut = true;\n\t\t\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t\t\t}, timeout * 1000);\n\t\t\t\t}\n\t\t\t\t// Stream stdout and stderr.\n\t\t\t\tchild.stdout?.on(\"data\", onData);\n\t\t\t\tchild.stderr?.on(\"data\", onData);\n\t\t\t\t// Handle abort signal by killing the entire process tree.\n\t\t\t\tconst onAbort = () => {\n\t\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t\t};\n\t\t\t\tif (signal) {\n\t\t\t\t\tif (signal.aborted) onAbort();\n\t\t\t\t\telse signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t\t// Handle shell spawn errors and wait for the process to terminate without hanging\n\t\t\t\t// on inherited stdio handles held by detached descendants.\n\t\t\t\twaitForChildProcess(child)\n\t\t\t\t\t.then((code) => {\n\t\t\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\t\t\treject(new Error(\"aborted\"));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (timedOut) {\n\t\t\t\t\t\t\treject(new Error(`timeout:${timeout}`));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tresolve({ exitCode: code });\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err) => {\n\t\t\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\treject(err);\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t};\n}\n\nexport interface BashSpawnContext {\n\tcommand: string;\n\tcwd: string;\n\tenv: NodeJS.ProcessEnv;\n}\n\nexport type BashSpawnHook = (context: BashSpawnContext) => BashSpawnContext;\n\nfunction resolveSpawnContext(command: string, cwd: string, spawnHook?: BashSpawnHook): BashSpawnContext {\n\tconst baseContext: BashSpawnContext = { command, cwd, env: { ...getShellEnv() } };\n\treturn spawnHook ? spawnHook(baseContext) : baseContext;\n}\n\nexport interface BashToolOptions {\n\t/** Custom operations for command execution. Default: local shell */\n\toperations?: BashOperations;\n\t/** Command prefix prepended to every command (for example shell setup commands) */\n\tcommandPrefix?: string;\n\t/** Hook to adjust command, cwd, or env before execution */\n\tspawnHook?: BashSpawnHook;\n}\n\nconst BASH_PREVIEW_LINES = 5;\n\ntype BashRenderState = {\n\tstartedAt: number | undefined;\n\tendedAt: number | undefined;\n\tinterval: NodeJS.Timeout | undefined;\n};\n\ntype BashResultRenderState = {\n\tcachedWidth: number | undefined;\n\tcachedLines: string[] | undefined;\n\tcachedSkipped: number | undefined;\n};\n\nclass BashResultRenderComponent extends Container {\n\tstate: BashResultRenderState = {\n\t\tcachedWidth: undefined,\n\t\tcachedLines: undefined,\n\t\tcachedSkipped: undefined,\n\t};\n}\n\nfunction formatDuration(ms: number): string {\n\treturn `${(ms / 1000).toFixed(1)}s`;\n}\n\nfunction formatBashCall(args: { command?: string; timeout?: number } | undefined): string {\n\tconst command = str(args?.command);\n\tconst timeout = args?.timeout as number | undefined;\n\tconst timeoutSuffix = timeout ? theme.fg(\"muted\", ` (timeout ${timeout}s)`) : \"\";\n\tconst commandDisplay = command === null ? invalidArgText(theme) : command ? command : theme.fg(\"toolOutput\", \"...\");\n\treturn theme.fg(\"toolTitle\", theme.bold(`$ ${commandDisplay}`)) + timeoutSuffix;\n}\n\nfunction rebuildBashResultRenderComponent(\n\tcomponent: BashResultRenderComponent,\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\t\tdetails?: BashToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\tshowImages: boolean,\n\tstartedAt: number | undefined,\n\tendedAt: number | undefined,\n): void {\n\tconst state = component.state;\n\tcomponent.clear();\n\n\tconst output = getTextOutput(result as any, showImages).trim();\n\n\tif (output) {\n\t\tconst styledOutput = output\n\t\t\t.split(\"\\n\")\n\t\t\t.map((line) => theme.fg(\"toolOutput\", line))\n\t\t\t.join(\"\\n\");\n\n\t\tif (options.expanded) {\n\t\t\tcomponent.addChild(new Text(`\\n${styledOutput}`, 0, 0));\n\t\t} else {\n\t\t\tcomponent.addChild({\n\t\t\t\trender: (width: number) => {\n\t\t\t\t\tif (state.cachedLines === undefined || state.cachedWidth !== width) {\n\t\t\t\t\t\tconst preview = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, width);\n\t\t\t\t\t\tstate.cachedLines = preview.visualLines;\n\t\t\t\t\t\tstate.cachedSkipped = preview.skippedCount;\n\t\t\t\t\t\tstate.cachedWidth = width;\n\t\t\t\t\t}\n\t\t\t\t\tif (state.cachedSkipped && state.cachedSkipped > 0) {\n\t\t\t\t\t\tconst hint =\n\t\t\t\t\t\t\ttheme.fg(\"muted\", `... (${state.cachedSkipped} earlier lines,`) +\n\t\t\t\t\t\t\t` ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t\t\t\t\treturn [\"\", truncateToWidth(hint, width, \"...\"), ...(state.cachedLines ?? [])];\n\t\t\t\t\t}\n\t\t\t\t\treturn [\"\", ...(state.cachedLines ?? [])];\n\t\t\t\t},\n\t\t\t\tinvalidate: () => {\n\t\t\t\t\tstate.cachedWidth = undefined;\n\t\t\t\t\tstate.cachedLines = undefined;\n\t\t\t\t\tstate.cachedSkipped = undefined;\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t}\n\n\tconst truncation = result.details?.truncation;\n\tconst fullOutputPath = result.details?.fullOutputPath;\n\tif (truncation?.truncated || fullOutputPath) {\n\t\tconst warnings: string[] = [];\n\t\tif (fullOutputPath) {\n\t\t\twarnings.push(`Full output: ${fullOutputPath}`);\n\t\t}\n\t\tif (truncation?.truncated) {\n\t\t\tif (truncation.truncatedBy === \"lines\") {\n\t\t\t\twarnings.push(`Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines`);\n\t\t\t} else {\n\t\t\t\twarnings.push(\n\t\t\t\t\t`Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\tcomponent.addChild(new Text(`\\n${theme.fg(\"warning\", `[${warnings.join(\". \")}]`)}`, 0, 0));\n\t}\n\n\tif (startedAt !== undefined) {\n\t\tconst label = options.isPartial ? \"Elapsed\" : \"Took\";\n\t\tconst endTime = endedAt ?? Date.now();\n\t\tcomponent.addChild(new Text(`\\n${theme.fg(\"muted\", `${label} ${formatDuration(endTime - startedAt)}`)}`, 0, 0));\n\t}\n}\n\nexport function createBashToolDefinition(\n\tcwd: string,\n\toptions?: BashToolOptions,\n): ToolDefinition<typeof bashSchema, BashToolDetails | undefined, BashRenderState> {\n\tconst ops = options?.operations ?? createLocalBashOperations();\n\tconst commandPrefix = options?.commandPrefix;\n\tconst spawnHook = options?.spawnHook;\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription: `Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`,\n\t\tpromptSnippet: \"Execute bash commands (ls, grep, find, etc.)\",\n\t\tparameters: bashSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{ command, timeout }: { command: string; timeout?: number },\n\t\t\tsignal?: AbortSignal,\n\t\t\tonUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\tconst resolvedCommand = commandPrefix ? `${commandPrefix}\\n${command}` : command;\n\t\t\tconst spawnContext = resolveSpawnContext(resolvedCommand, cwd, spawnHook);\n\t\t\tif (onUpdate) {\n\t\t\t\tonUpdate({ content: [], details: undefined });\n\t\t\t}\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tlet tempFilePath: string | undefined;\n\t\t\t\tlet tempFileStream: ReturnType<typeof createWriteStream> | undefined;\n\t\t\t\tlet totalBytes = 0;\n\t\t\t\tconst chunks: Buffer[] = [];\n\t\t\t\tlet chunksBytes = 0;\n\t\t\t\tconst maxChunksBytes = DEFAULT_MAX_BYTES * 2;\n\n\t\t\t\tconst handleData = (data: Buffer) => {\n\t\t\t\t\ttotalBytes += data.length;\n\t\t\t\t\t// Start writing to a temp file once output exceeds the in-memory threshold.\n\t\t\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\t\t\t\t// Write all buffered chunks to the file.\n\t\t\t\t\t\tfor (const chunk of chunks) tempFileStream.write(chunk);\n\t\t\t\t\t}\n\t\t\t\t\t// Write to temp file if we have one.\n\t\t\t\t\tif (tempFileStream) tempFileStream.write(data);\n\t\t\t\t\t// Keep a rolling buffer of recent output for tail truncation.\n\t\t\t\t\tchunks.push(data);\n\t\t\t\t\tchunksBytes += data.length;\n\t\t\t\t\t// Trim old chunks if the rolling buffer grows too large.\n\t\t\t\t\twhile (chunksBytes > maxChunksBytes && chunks.length > 1) {\n\t\t\t\t\t\tconst removed = chunks.shift()!;\n\t\t\t\t\t\tchunksBytes -= removed.length;\n\t\t\t\t\t}\n\t\t\t\t\t// Stream partial output using the rolling tail buffer.\n\t\t\t\t\tif (onUpdate) {\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\tconst fullText = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\tconst truncation = truncateTail(fullText);\n\t\t\t\t\t\tonUpdate({\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: truncation.content || \"\" }],\n\t\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\t\ttruncation: truncation.truncated ? truncation : undefined,\n\t\t\t\t\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tops.exec(spawnContext.command, spawnContext.cwd, {\n\t\t\t\t\tonData: handleData,\n\t\t\t\t\tsignal,\n\t\t\t\t\ttimeout,\n\t\t\t\t\tenv: spawnContext.env,\n\t\t\t\t})\n\t\t\t\t\t.then(({ exitCode }) => {\n\t\t\t\t\t\t// Close temp file stream before building the final result.\n\t\t\t\t\t\tif (tempFileStream) tempFileStream.end();\n\t\t\t\t\t\t// Combine the rolling buffer chunks.\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\t// Render terminal output to collapse progress bars, handle \\r overwrites,\n\t\t\t\t\t\t// and process ANSI cursor movement — producing what a terminal would display.\n\t\t\t\t\t\tconst fullOutput = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\t// Apply tail truncation for the final display payload.\n\t\t\t\t\t\tconst truncation = truncateTail(fullOutput);\n\t\t\t\t\t\t// If truncation occurred (e.g. by line count) but no temp file was\n\t\t\t\t\t\t// created (output was under the byte-streaming threshold), write the\n\t\t\t\t\t\t// full output to a temp file so the truncation message can reference it.\n\t\t\t\t\t\tif (truncation.truncated && !tempFilePath) {\n\t\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlet outputText = truncation.content || \"(no output)\";\n\t\t\t\t\t\tlet details: BashToolDetails | undefined;\n\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t// Build truncation details and an actionable notice.\n\t\t\t\t\t\t\tdetails = { truncation, fullOutputPath: tempFilePath };\n\t\t\t\t\t\t\tconst startLine = truncation.totalLines - truncation.outputLines + 1;\n\t\t\t\t\t\t\tconst endLine = truncation.totalLines;\n\t\t\t\t\t\t\tif (truncation.lastLinePartial) {\n\t\t\t\t\t\t\t\t// Edge case: the last line alone is larger than the byte limit.\n\t\t\t\t\t\t\t\tconst lastLineSize = formatSize(Buffer.byteLength(fullOutput.split(\"\\n\").pop() || \"\", \"utf-8\"));\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else if (truncation.truncatedBy === \"lines\") {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (exitCode !== 0 && exitCode !== null) {\n\t\t\t\t\t\t\toutputText += `\\n\\nCommand exited with code ${exitCode}`;\n\t\t\t\t\t\t\treject(new Error(outputText));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: outputText }], details });\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err: Error) => {\n\t\t\t\t\t\t// Close temp file stream and include buffered output in the error message.\n\t\t\t\t\t\tif (tempFileStream) tempFileStream.end();\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\tlet output = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\tif (err.message === \"aborted\") {\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += \"Command aborted\";\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else if (err.message.startsWith(\"timeout:\")) {\n\t\t\t\t\t\t\tconst timeoutSecs = err.message.split(\":\")[1];\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += `Command timed out after ${timeoutSecs} seconds`;\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treject(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t\trenderCall(args, _theme, context) {\n\t\t\tconst state = context.state;\n\t\t\tif (context.executionStarted && state.startedAt === undefined) {\n\t\t\t\tstate.startedAt = Date.now();\n\t\t\t\tstate.endedAt = undefined;\n\t\t\t}\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatBashCall(args));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, _theme, context) {\n\t\t\tconst state = context.state;\n\t\t\tif (state.startedAt !== undefined && options.isPartial && !state.interval) {\n\t\t\t\tstate.interval = setInterval(() => context.invalidate(), 1000);\n\t\t\t}\n\t\t\tif (!options.isPartial || context.isError) {\n\t\t\t\tstate.endedAt ??= Date.now();\n\t\t\t\tif (state.interval) {\n\t\t\t\t\tclearInterval(state.interval);\n\t\t\t\t\tstate.interval = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst component =\n\t\t\t\t(context.lastComponent as BashResultRenderComponent | undefined) ?? new BashResultRenderComponent();\n\t\t\trebuildBashResultRenderComponent(\n\t\t\t\tcomponent,\n\t\t\t\tresult as any,\n\t\t\t\toptions,\n\t\t\t\tcontext.showImages,\n\t\t\t\tstate.startedAt,\n\t\t\t\tstate.endedAt,\n\t\t\t);\n\t\t\tcomponent.invalidate();\n\t\t\treturn component;\n\t\t},\n\t};\n}\n\nexport function createBashTool(cwd: string, options?: BashToolOptions): AgentTool<typeof bashSchema> {\n\treturn wrapToolDefinition(createBashToolDefinition(cwd, options));\n}\n\n/** Default bash tool using process.cwd() for backwards compatibility. */\nexport const bashToolDefinition = createBashToolDefinition(process.cwd());\nexport const bashTool = createBashTool(process.cwd());\n"]}
1
+ {"version":3,"file":"bash.d.ts","sourceRoot":"","sources":["../../../src/core/tools/bash.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,KAAK,MAAM,EAAQ,MAAM,mBAAmB,CAAC;AAOtD,OAAO,KAAK,EAAE,cAAc,EAA2B,MAAM,wBAAwB,CAAC;AAItF,OAAO,EAAoD,KAAK,gBAAgB,EAAgB,MAAM,eAAe,CAAC;AAUtH,QAAA,MAAM,UAAU;;;EAGd,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC;AAEtD,MAAM,WAAW,eAAe;IAC/B,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC9B;;;;;;OAMG;IACH,IAAI,EAAE,CACL,OAAO,EAAE,MA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E;AACzE,eAAO,MAAM,kBAAkB;;;iDAA0C,CAAC;AAC1E,eAAO,MAAM,QAAQ;;;QAAgC,CAAC","sourcesContent":["import { randomBytes } from \"node:crypto\";\nimport { createWriteStream, existsSync, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { AgentTool } from \"@dreb/agent-core\";\nimport { Container, Text, truncateToWidth } from \"@dreb/tui\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { spawn } from \"child_process\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.js\";\nimport { truncateToVisualLines } from \"../../modes/interactive/components/visual-truncate.js\";\nimport { theme } from \"../../modes/interactive/theme/theme.js\";\nimport { waitForChildProcess } from \"../../utils/child-process.js\";\nimport { getShellConfig, getShellEnv, killProcessTree } from \"../../utils/shell.js\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.js\";\nimport { getTextOutput, invalidArgText, str } from \"./render-utils.js\";\nimport { renderTerminalOutput } from \"./terminal-render.js\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.js\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateTail } from \"./truncate.js\";\n\n/**\n * Generate a unique temp file path for bash output.\n */\nfunction getTempFilePath(): string {\n\tconst id = randomBytes(8).toString(\"hex\");\n\treturn join(tmpdir(), `dreb-bash-${id}.log`);\n}\n\nconst bashSchema = Type.Object({\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n});\n\nexport type BashToolInput = Static<typeof bashSchema>;\n\nexport interface BashToolDetails {\n\ttruncation?: TruncationResult;\n\tfullOutputPath?: string;\n}\n\n/**\n * Pluggable operations for the bash tool.\n * Override these to delegate command execution to remote systems (for example SSH).\n */\nexport interface BashOperations {\n\t/**\n\t * Execute a command and stream output.\n\t * @param command The command to execute\n\t * @param cwd Working directory\n\t * @param options Execution options\n\t * @returns Promise resolving to exit code (null if killed)\n\t */\n\texec: (\n\t\tcommand: string,\n\t\tcwd: string,\n\t\toptions: {\n\t\t\tonData: (data: Buffer) => void;\n\t\t\tsignal?: AbortSignal;\n\t\t\ttimeout?: number;\n\t\t\tenv?: NodeJS.ProcessEnv;\n\t\t},\n\t) => Promise<{ exitCode: number | null }>;\n}\n\n/**\n * Create bash operations using dreb's built-in local shell execution backend.\n *\n * This is useful for extensions that intercept user_bash and still want dreb's\n * standard local shell behavior while wrapping or rewriting commands.\n */\nexport function createLocalBashOperations(): BashOperations {\n\treturn {\n\t\texec: (command, cwd, { onData, signal, timeout, env }) => {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tconst { shell, args } = getShellConfig();\n\t\t\t\tif (!existsSync(cwd)) {\n\t\t\t\t\treject(new Error(`Working directory does not exist: ${cwd}\\nCannot execute bash commands.`));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst child = spawn(shell, [...args, command], {\n\t\t\t\t\tcwd,\n\t\t\t\t\tdetached: true,\n\t\t\t\t\tenv: env ?? getShellEnv(),\n\t\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t\t});\n\t\t\t\tlet timedOut = false;\n\t\t\t\tlet timeoutHandle: NodeJS.Timeout | undefined;\n\t\t\t\t// Set timeout if provided.\n\t\t\t\tif (timeout !== undefined && timeout > 0) {\n\t\t\t\t\ttimeoutHandle = setTimeout(() => {\n\t\t\t\t\t\ttimedOut = true;\n\t\t\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t\t\t}, timeout * 1000);\n\t\t\t\t}\n\t\t\t\t// Stream stdout and stderr.\n\t\t\t\tchild.stdout?.on(\"data\", onData);\n\t\t\t\tchild.stderr?.on(\"data\", onData);\n\t\t\t\t// Handle abort signal by killing the entire process tree.\n\t\t\t\tconst onAbort = () => {\n\t\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t\t};\n\t\t\t\tif (signal) {\n\t\t\t\t\tif (signal.aborted) onAbort();\n\t\t\t\t\telse signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t\t// Handle shell spawn errors and wait for the process to terminate without hanging\n\t\t\t\t// on inherited stdio handles held by detached descendants.\n\t\t\t\twaitForChildProcess(child)\n\t\t\t\t\t.then((code) => {\n\t\t\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\t\t\treject(new Error(\"aborted\"));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (timedOut) {\n\t\t\t\t\t\t\treject(new Error(`timeout:${timeout}`));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tresolve({ exitCode: code });\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err) => {\n\t\t\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\treject(err);\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t};\n}\n\nexport interface BashSpawnContext {\n\tcommand: string;\n\tcwd: string;\n\tenv: NodeJS.ProcessEnv;\n}\n\nexport type BashSpawnHook = (context: BashSpawnContext) => BashSpawnContext;\n\nfunction resolveSpawnContext(command: string, cwd: string, spawnHook?: BashSpawnHook): BashSpawnContext {\n\tconst baseContext: BashSpawnContext = { command, cwd, env: { ...getShellEnv() } };\n\treturn spawnHook ? spawnHook(baseContext) : baseContext;\n}\n\nexport interface BashToolOptions {\n\t/** Custom operations for command execution. Default: local shell */\n\toperations?: BashOperations;\n\t/** Command prefix prepended to every command (for example shell setup commands) */\n\tcommandPrefix?: string;\n\t/** Hook to adjust command, cwd, or env before execution */\n\tspawnHook?: BashSpawnHook;\n}\n\nconst BASH_PREVIEW_LINES = 5;\n\ntype BashRenderState = {\n\tstartedAt: number | undefined;\n\tendedAt: number | undefined;\n\tinterval: NodeJS.Timeout | undefined;\n};\n\ntype BashResultRenderState = {\n\tcachedWidth: number | undefined;\n\tcachedLines: string[] | undefined;\n\tcachedSkipped: number | undefined;\n};\n\nclass BashResultRenderComponent extends Container {\n\tstate: BashResultRenderState = {\n\t\tcachedWidth: undefined,\n\t\tcachedLines: undefined,\n\t\tcachedSkipped: undefined,\n\t};\n}\n\nfunction formatDuration(ms: number): string {\n\treturn `${(ms / 1000).toFixed(1)}s`;\n}\n\nfunction formatBashCall(args: { command?: string; timeout?: number } | undefined): string {\n\tconst command = str(args?.command);\n\tconst timeout = args?.timeout as number | undefined;\n\tconst timeoutSuffix = timeout ? theme.fg(\"muted\", ` (timeout ${timeout}s)`) : \"\";\n\tconst commandDisplay = command === null ? invalidArgText(theme) : command ? command : theme.fg(\"toolOutput\", \"...\");\n\treturn theme.fg(\"toolTitle\", theme.bold(`$ ${commandDisplay}`)) + timeoutSuffix;\n}\n\nfunction rebuildBashResultRenderComponent(\n\tcomponent: BashResultRenderComponent,\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\t\tdetails?: BashToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\tshowImages: boolean,\n\tstartedAt: number | undefined,\n\tendedAt: number | undefined,\n): void {\n\tconst state = component.state;\n\tcomponent.clear();\n\n\tconst output = getTextOutput(result as any, showImages).trim();\n\n\tif (output) {\n\t\tconst styledOutput = output\n\t\t\t.split(\"\\n\")\n\t\t\t.map((line) => theme.fg(\"toolOutput\", line))\n\t\t\t.join(\"\\n\");\n\n\t\tif (options.expanded) {\n\t\t\tcomponent.addChild(new Text(`\\n${styledOutput}`, 0, 0));\n\t\t} else {\n\t\t\tcomponent.addChild({\n\t\t\t\trender: (width: number) => {\n\t\t\t\t\tif (state.cachedLines === undefined || state.cachedWidth !== width) {\n\t\t\t\t\t\tconst preview = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, width);\n\t\t\t\t\t\tstate.cachedLines = preview.visualLines;\n\t\t\t\t\t\tstate.cachedSkipped = preview.skippedCount;\n\t\t\t\t\t\tstate.cachedWidth = width;\n\t\t\t\t\t}\n\t\t\t\t\tif (state.cachedSkipped && state.cachedSkipped > 0) {\n\t\t\t\t\t\tconst hint =\n\t\t\t\t\t\t\ttheme.fg(\"muted\", `... (${state.cachedSkipped} earlier lines,`) +\n\t\t\t\t\t\t\t` ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t\t\t\t\treturn [\"\", truncateToWidth(hint, width, \"...\"), ...(state.cachedLines ?? [])];\n\t\t\t\t\t}\n\t\t\t\t\treturn [\"\", ...(state.cachedLines ?? [])];\n\t\t\t\t},\n\t\t\t\tinvalidate: () => {\n\t\t\t\t\tstate.cachedWidth = undefined;\n\t\t\t\t\tstate.cachedLines = undefined;\n\t\t\t\t\tstate.cachedSkipped = undefined;\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t}\n\n\tconst truncation = result.details?.truncation;\n\tconst fullOutputPath = result.details?.fullOutputPath;\n\tif (truncation?.truncated || fullOutputPath) {\n\t\tconst warnings: string[] = [];\n\t\tif (fullOutputPath) {\n\t\t\twarnings.push(`Full output: ${fullOutputPath}`);\n\t\t}\n\t\tif (truncation?.truncated) {\n\t\t\tif (truncation.truncatedBy === \"lines\") {\n\t\t\t\twarnings.push(`Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines`);\n\t\t\t} else {\n\t\t\t\twarnings.push(\n\t\t\t\t\t`Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\tcomponent.addChild(new Text(`\\n${theme.fg(\"warning\", `[${warnings.join(\". \")}]`)}`, 0, 0));\n\t}\n\n\tif (startedAt !== undefined) {\n\t\tconst label = options.isPartial ? \"Elapsed\" : \"Took\";\n\t\tconst endTime = endedAt ?? Date.now();\n\t\tcomponent.addChild(new Text(`\\n${theme.fg(\"muted\", `${label} ${formatDuration(endTime - startedAt)}`)}`, 0, 0));\n\t}\n}\n\nexport function createBashToolDefinition(\n\tcwd: string,\n\toptions?: BashToolOptions,\n): ToolDefinition<typeof bashSchema, BashToolDetails | undefined, BashRenderState> {\n\tconst ops = options?.operations ?? createLocalBashOperations();\n\tconst commandPrefix = options?.commandPrefix;\n\tconst spawnHook = options?.spawnHook;\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription: `Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`,\n\t\tpromptSnippet: \"Execute bash commands (ls, grep, find, etc.)\",\n\t\tparameters: bashSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{ command, timeout }: { command: string; timeout?: number },\n\t\t\tsignal?: AbortSignal,\n\t\t\tonUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\tconst resolvedCommand = commandPrefix ? `${commandPrefix}\\n${command}` : command;\n\t\t\tconst spawnContext = resolveSpawnContext(resolvedCommand, cwd, spawnHook);\n\t\t\tif (onUpdate) {\n\t\t\t\tonUpdate({ content: [], details: undefined });\n\t\t\t}\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tlet tempFilePath: string | undefined;\n\t\t\t\tlet tempFileStream: ReturnType<typeof createWriteStream> | undefined;\n\t\t\t\tlet totalBytes = 0;\n\t\t\t\tconst chunks: Buffer[] = [];\n\t\t\t\tlet chunksBytes = 0;\n\t\t\t\tconst maxChunksBytes = DEFAULT_MAX_BYTES * 2;\n\n\t\t\t\tconst handleData = (data: Buffer) => {\n\t\t\t\t\ttotalBytes += data.length;\n\t\t\t\t\t// Start writing to a temp file once output exceeds the in-memory threshold.\n\t\t\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\t// Create the file synchronously so it exists immediately for downstream checks.\n\t\t\t\t\t\twriteFileSync(tempFilePath, \"\");\n\t\t\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\t\t\t\t// Write all buffered chunks to the file.\n\t\t\t\t\t\tfor (const chunk of chunks) tempFileStream.write(chunk);\n\t\t\t\t\t}\n\t\t\t\t\t// Write to temp file if we have one.\n\t\t\t\t\tif (tempFileStream) tempFileStream.write(data);\n\t\t\t\t\t// Keep a rolling buffer of recent output for tail truncation.\n\t\t\t\t\tchunks.push(data);\n\t\t\t\t\tchunksBytes += data.length;\n\t\t\t\t\t// Trim old chunks if the rolling buffer grows too large.\n\t\t\t\t\twhile (chunksBytes > maxChunksBytes && chunks.length > 1) {\n\t\t\t\t\t\tconst removed = chunks.shift()!;\n\t\t\t\t\t\tchunksBytes -= removed.length;\n\t\t\t\t\t}\n\t\t\t\t\t// Stream partial output using the rolling tail buffer.\n\t\t\t\t\tif (onUpdate) {\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\tconst fullText = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\tconst truncation = truncateTail(fullText);\n\t\t\t\t\t\tonUpdate({\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: truncation.content || \"\" }],\n\t\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\t\ttruncation: truncation.truncated ? truncation : undefined,\n\t\t\t\t\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tops.exec(spawnContext.command, spawnContext.cwd, {\n\t\t\t\t\tonData: handleData,\n\t\t\t\t\tsignal,\n\t\t\t\t\ttimeout,\n\t\t\t\t\tenv: spawnContext.env,\n\t\t\t\t})\n\t\t\t\t\t.then(({ exitCode }) => {\n\t\t\t\t\t\t// Close temp file stream before building the final result.\n\t\t\t\t\t\tif (tempFileStream) tempFileStream.end();\n\t\t\t\t\t\t// Combine the rolling buffer chunks.\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\t// Render terminal output to collapse progress bars, handle \\r overwrites,\n\t\t\t\t\t\t// and process ANSI cursor movement — producing what a terminal would display.\n\t\t\t\t\t\tconst fullOutput = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\t// Apply tail truncation for the final display payload.\n\t\t\t\t\t\tconst truncation = truncateTail(fullOutput);\n\t\t\t\t\t\t// If truncation occurred (e.g. by line count) but no temp file was\n\t\t\t\t\t\t// created (output was under the byte-streaming threshold), write the\n\t\t\t\t\t\t// full output to a temp file so the truncation message can reference it.\n\t\t\t\t\t\tif (truncation.truncated && !tempFilePath) {\n\t\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlet outputText = truncation.content || \"(no output)\";\n\t\t\t\t\t\tlet details: BashToolDetails | undefined;\n\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t// Build truncation details and an actionable notice.\n\t\t\t\t\t\t\tdetails = { truncation, fullOutputPath: tempFilePath };\n\t\t\t\t\t\t\tconst startLine = truncation.totalLines - truncation.outputLines + 1;\n\t\t\t\t\t\t\tconst endLine = truncation.totalLines;\n\t\t\t\t\t\t\tif (truncation.lastLinePartial) {\n\t\t\t\t\t\t\t\t// Edge case: the last line alone is larger than the byte limit.\n\t\t\t\t\t\t\t\tconst lastLineSize = formatSize(Buffer.byteLength(fullOutput.split(\"\\n\").pop() || \"\", \"utf-8\"));\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else if (truncation.truncatedBy === \"lines\") {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (exitCode !== 0 && exitCode !== null) {\n\t\t\t\t\t\t\toutputText += `\\n\\nCommand exited with code ${exitCode}`;\n\t\t\t\t\t\t\treject(new Error(outputText));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: outputText }], details });\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err: Error) => {\n\t\t\t\t\t\t// Close temp file stream and include buffered output in the error message.\n\t\t\t\t\t\tif (tempFileStream) tempFileStream.end();\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\tlet output = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\tif (err.message === \"aborted\") {\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += \"Command aborted\";\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else if (err.message.startsWith(\"timeout:\")) {\n\t\t\t\t\t\t\tconst timeoutSecs = err.message.split(\":\")[1];\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += `Command timed out after ${timeoutSecs} seconds`;\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treject(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t\trenderCall(args, _theme, context) {\n\t\t\tconst state = context.state;\n\t\t\tif (context.executionStarted && state.startedAt === undefined) {\n\t\t\t\tstate.startedAt = Date.now();\n\t\t\t\tstate.endedAt = undefined;\n\t\t\t}\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatBashCall(args));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, _theme, context) {\n\t\t\tconst state = context.state;\n\t\t\tif (state.startedAt !== undefined && options.isPartial && !state.interval) {\n\t\t\t\tstate.interval = setInterval(() => context.invalidate(), 1000);\n\t\t\t}\n\t\t\tif (!options.isPartial || context.isError) {\n\t\t\t\tstate.endedAt ??= Date.now();\n\t\t\t\tif (state.interval) {\n\t\t\t\t\tclearInterval(state.interval);\n\t\t\t\t\tstate.interval = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst component =\n\t\t\t\t(context.lastComponent as BashResultRenderComponent | undefined) ?? new BashResultRenderComponent();\n\t\t\trebuildBashResultRenderComponent(\n\t\t\t\tcomponent,\n\t\t\t\tresult as any,\n\t\t\t\toptions,\n\t\t\t\tcontext.showImages,\n\t\t\t\tstate.startedAt,\n\t\t\t\tstate.endedAt,\n\t\t\t);\n\t\t\tcomponent.invalidate();\n\t\t\treturn component;\n\t\t},\n\t};\n}\n\nexport function createBashTool(cwd: string, options?: BashToolOptions): AgentTool<typeof bashSchema> {\n\treturn wrapToolDefinition(createBashToolDefinition(cwd, options));\n}\n\n/** Default bash tool using process.cwd() for backwards compatibility. */\nexport const bashToolDefinition = createBashToolDefinition(process.cwd());\nexport const bashTool = createBashTool(process.cwd());\n"]}
@@ -208,6 +208,8 @@ export function createBashToolDefinition(cwd, options) {
208
208
  // Start writing to a temp file once output exceeds the in-memory threshold.
209
209
  if (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {
210
210
  tempFilePath = getTempFilePath();
211
+ // Create the file synchronously so it exists immediately for downstream checks.
212
+ writeFileSync(tempFilePath, "");
211
213
  tempFileStream = createWriteStream(tempFilePath);
212
214
  // Write all buffered chunks to the file.
213
215
  for (const chunk of chunks)
@@ -1 +1 @@
1
- {"version":3,"file":"bash.js","sourceRoot":"","sources":["../../../src/core/tools/bash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACvE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAe,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,wDAAwD,CAAC;AACjF,OAAO,EAAE,qBAAqB,EAAE,MAAM,uDAAuD,CAAC;AAC9F,OAAO,EAAE,KAAK,EAAE,MAAM,wCAAwC,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEpF,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,UAAU,EAAyB,YAAY,EAAE,MAAM,eAAe,CAAC;AAEtH;;GAEG;AACH,SAAS,eAAe,GAAW;IAClC,MAAM,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC1C,OAAO,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s\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { AgentTool } from \"@dreb/agent-core\";\nimport { Container, Text, truncateToWidth } from \"@dreb/tui\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { spawn } from \"child_process\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.js\";\nimport { truncateToVisualLines } from \"../../modes/interactive/components/visual-truncate.js\";\nimport { theme } from \"../../modes/interactive/theme/theme.js\";\nimport { waitForChildProcess } from \"../../utils/child-process.js\";\nimport { getShellConfig, getShellEnv, killProcessTree } from \"../../utils/shell.js\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.js\";\nimport { getTextOutput, invalidArgText, str } from \"./render-utils.js\";\nimport { renderTerminalOutput } from \"./terminal-render.js\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.js\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateTail } from \"./truncate.js\";\n\n/**\n * Generate a unique temp file path for bash output.\n */\nfunction getTempFilePath(): string {\n\tconst id = randomBytes(8).toString(\"hex\");\n\treturn join(tmpdir(), `dreb-bash-${id}.log`);\n}\n\nconst bashSchema = Type.Object({\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n});\n\nexport type BashToolInput = Static<typeof bashSchema>;\n\nexport interface BashToolDetails {\n\ttruncation?: TruncationResult;\n\tfullOutputPath?: string;\n}\n\n/**\n * Pluggable operations for the bash tool.\n * Override these to delegate command execution to remote systems (for example SSH).\n */\nexport interface BashOperations {\n\t/**\n\t * Execute a command and stream output.\n\t * @param command The command to execute\n\t * @param cwd Working directory\n\t * @param options Execution options\n\t * @returns Promise resolving to exit code (null if killed)\n\t */\n\texec: (\n\t\tcommand: string,\n\t\tcwd: string,\n\t\toptions: {\n\t\t\tonData: (data: Buffer) => void;\n\t\t\tsignal?: AbortSignal;\n\t\t\ttimeout?: number;\n\t\t\tenv?: NodeJS.ProcessEnv;\n\t\t},\n\t) => Promise<{ exitCode: number | null }>;\n}\n\n/**\n * Create bash operations using dreb's built-in local shell execution backend.\n *\n * This is useful for extensions that intercept user_bash and still want dreb's\n * standard local shell behavior while wrapping or rewriting commands.\n */\nexport function createLocalBashOperations(): BashOperations {\n\treturn {\n\t\texec: (command, cwd, { onData, signal, timeout, env }) => {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tconst { shell, args } = getShellConfig();\n\t\t\t\tif (!existsSync(cwd)) {\n\t\t\t\t\treject(new Error(`Working directory does not exist: ${cwd}\\nCannot execute bash commands.`));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst child = spawn(shell, [...args, command], {\n\t\t\t\t\tcwd,\n\t\t\t\t\tdetached: true,\n\t\t\t\t\tenv: env ?? getShellEnv(),\n\t\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t\t});\n\t\t\t\tlet timedOut = false;\n\t\t\t\tlet timeoutHandle: NodeJS.Timeout | undefined;\n\t\t\t\t// Set timeout if provided.\n\t\t\t\tif (timeout !== undefined && timeout > 0) {\n\t\t\t\t\ttimeoutHandle = setTimeout(() => {\n\t\t\t\t\t\ttimedOut = true;\n\t\t\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t\t\t}, timeout * 1000);\n\t\t\t\t}\n\t\t\t\t// Stream stdout and stderr.\n\t\t\t\tchild.stdout?.on(\"data\", onData);\n\t\t\t\tchild.stderr?.on(\"data\", onData);\n\t\t\t\t// Handle abort signal by killing the entire process tree.\n\t\t\t\tconst onAbort = () => {\n\t\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t\t};\n\t\t\t\tif (signal) {\n\t\t\t\t\tif (signal.aborted) onAbort();\n\t\t\t\t\telse signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t\t// Handle shell spawn errors and wait for the process to terminate without hanging\n\t\t\t\t// on inherited stdio handles held by detached descendants.\n\t\t\t\twaitForChildProcess(child)\n\t\t\t\t\t.then((code) => {\n\t\t\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\t\t\treject(new Error(\"aborted\"));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (timedOut) {\n\t\t\t\t\t\t\treject(new Error(`timeout:${timeout}`));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tresolve({ exitCode: code });\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err) => {\n\t\t\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\treject(err);\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t};\n}\n\nexport interface BashSpawnContext {\n\tcommand: string;\n\tcwd: string;\n\tenv: NodeJS.ProcessEnv;\n}\n\nexport type BashSpawnHook = (context: BashSpawnContext) => BashSpawnContext;\n\nfunction resolveSpawnContext(command: string, cwd: string, spawnHook?: BashSpawnHook): BashSpawnContext {\n\tconst baseContext: BashSpawnContext = { command, cwd, env: { ...getShellEnv() } };\n\treturn spawnHook ? spawnHook(baseContext) : baseContext;\n}\n\nexport interface BashToolOptions {\n\t/** Custom operations for command execution. Default: local shell */\n\toperations?: BashOperations;\n\t/** Command prefix prepended to every command (for example shell setup commands) */\n\tcommandPrefix?: string;\n\t/** Hook to adjust command, cwd, or env before execution */\n\tspawnHook?: BashSpawnHook;\n}\n\nconst BASH_PREVIEW_LINES = 5;\n\ntype BashRenderState = {\n\tstartedAt: number | undefined;\n\tendedAt: number | undefined;\n\tinterval: NodeJS.Timeout | undefined;\n};\n\ntype BashResultRenderState = {\n\tcachedWidth: number | undefined;\n\tcachedLines: string[] | undefined;\n\tcachedSkipped: number | undefined;\n};\n\nclass BashResultRenderComponent extends Container {\n\tstate: BashResultRenderState = {\n\t\tcachedWidth: undefined,\n\t\tcachedLines: undefined,\n\t\tcachedSkipped: undefined,\n\t};\n}\n\nfunction formatDuration(ms: number): string {\n\treturn `${(ms / 1000).toFixed(1)}s`;\n}\n\nfunction formatBashCall(args: { command?: string; timeout?: number } | undefined): string {\n\tconst command = str(args?.command);\n\tconst timeout = args?.timeout as number | undefined;\n\tconst timeoutSuffix = timeout ? theme.fg(\"muted\", ` (timeout ${timeout}s)`) : \"\";\n\tconst commandDisplay = command === null ? invalidArgText(theme) : command ? command : theme.fg(\"toolOutput\", \"...\");\n\treturn theme.fg(\"toolTitle\", theme.bold(`$ ${commandDisplay}`)) + timeoutSuffix;\n}\n\nfunction rebuildBashResultRenderComponent(\n\tcomponent: BashResultRenderComponent,\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\t\tdetails?: BashToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\tshowImages: boolean,\n\tstartedAt: number | undefined,\n\tendedAt: number | undefined,\n): void {\n\tconst state = component.state;\n\tcomponent.clear();\n\n\tconst output = getTextOutput(result as any, showImages).trim();\n\n\tif (output) {\n\t\tconst styledOutput = output\n\t\t\t.split(\"\\n\")\n\t\t\t.map((line) => theme.fg(\"toolOutput\", line))\n\t\t\t.join(\"\\n\");\n\n\t\tif (options.expanded) {\n\t\t\tcomponent.addChild(new Text(`\\n${styledOutput}`, 0, 0));\n\t\t} else {\n\t\t\tcomponent.addChild({\n\t\t\t\trender: (width: number) => {\n\t\t\t\t\tif (state.cachedLines === undefined || state.cachedWidth !== width) {\n\t\t\t\t\t\tconst preview = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, width);\n\t\t\t\t\t\tstate.cachedLines = preview.visualLines;\n\t\t\t\t\t\tstate.cachedSkipped = preview.skippedCount;\n\t\t\t\t\t\tstate.cachedWidth = width;\n\t\t\t\t\t}\n\t\t\t\t\tif (state.cachedSkipped && state.cachedSkipped > 0) {\n\t\t\t\t\t\tconst hint =\n\t\t\t\t\t\t\ttheme.fg(\"muted\", `... (${state.cachedSkipped} earlier lines,`) +\n\t\t\t\t\t\t\t` ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t\t\t\t\treturn [\"\", truncateToWidth(hint, width, \"...\"), ...(state.cachedLines ?? [])];\n\t\t\t\t\t}\n\t\t\t\t\treturn [\"\", ...(state.cachedLines ?? [])];\n\t\t\t\t},\n\t\t\t\tinvalidate: () => {\n\t\t\t\t\tstate.cachedWidth = undefined;\n\t\t\t\t\tstate.cachedLines = undefined;\n\t\t\t\t\tstate.cachedSkipped = undefined;\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t}\n\n\tconst truncation = result.details?.truncation;\n\tconst fullOutputPath = result.details?.fullOutputPath;\n\tif (truncation?.truncated || fullOutputPath) {\n\t\tconst warnings: string[] = [];\n\t\tif (fullOutputPath) {\n\t\t\twarnings.push(`Full output: ${fullOutputPath}`);\n\t\t}\n\t\tif (truncation?.truncated) {\n\t\t\tif (truncation.truncatedBy === \"lines\") {\n\t\t\t\twarnings.push(`Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines`);\n\t\t\t} else {\n\t\t\t\twarnings.push(\n\t\t\t\t\t`Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\tcomponent.addChild(new Text(`\\n${theme.fg(\"warning\", `[${warnings.join(\". \")}]`)}`, 0, 0));\n\t}\n\n\tif (startedAt !== undefined) {\n\t\tconst label = options.isPartial ? \"Elapsed\" : \"Took\";\n\t\tconst endTime = endedAt ?? Date.now();\n\t\tcomponent.addChild(new Text(`\\n${theme.fg(\"muted\", `${label} ${formatDuration(endTime - startedAt)}`)}`, 0, 0));\n\t}\n}\n\nexport function createBashToolDefinition(\n\tcwd: string,\n\toptions?: BashToolOptions,\n): ToolDefinition<typeof bashSchema, BashToolDetails | undefined, BashRenderState> {\n\tconst ops = options?.operations ?? createLocalBashOperations();\n\tconst commandPrefix = options?.commandPrefix;\n\tconst spawnHook = options?.spawnHook;\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription: `Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`,\n\t\tpromptSnippet: \"Execute bash commands (ls, grep, find, etc.)\",\n\t\tparameters: bashSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{ command, timeout }: { command: string; timeout?: number },\n\t\t\tsignal?: AbortSignal,\n\t\t\tonUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\tconst resolvedCommand = commandPrefix ? `${commandPrefix}\\n${command}` : command;\n\t\t\tconst spawnContext = resolveSpawnContext(resolvedCommand, cwd, spawnHook);\n\t\t\tif (onUpdate) {\n\t\t\t\tonUpdate({ content: [], details: undefined });\n\t\t\t}\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tlet tempFilePath: string | undefined;\n\t\t\t\tlet tempFileStream: ReturnType<typeof createWriteStream> | undefined;\n\t\t\t\tlet totalBytes = 0;\n\t\t\t\tconst chunks: Buffer[] = [];\n\t\t\t\tlet chunksBytes = 0;\n\t\t\t\tconst maxChunksBytes = DEFAULT_MAX_BYTES * 2;\n\n\t\t\t\tconst handleData = (data: Buffer) => {\n\t\t\t\t\ttotalBytes += data.length;\n\t\t\t\t\t// Start writing to a temp file once output exceeds the in-memory threshold.\n\t\t\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\t\t\t\t// Write all buffered chunks to the file.\n\t\t\t\t\t\tfor (const chunk of chunks) tempFileStream.write(chunk);\n\t\t\t\t\t}\n\t\t\t\t\t// Write to temp file if we have one.\n\t\t\t\t\tif (tempFileStream) tempFileStream.write(data);\n\t\t\t\t\t// Keep a rolling buffer of recent output for tail truncation.\n\t\t\t\t\tchunks.push(data);\n\t\t\t\t\tchunksBytes += data.length;\n\t\t\t\t\t// Trim old chunks if the rolling buffer grows too large.\n\t\t\t\t\twhile (chunksBytes > maxChunksBytes && chunks.length > 1) {\n\t\t\t\t\t\tconst removed = chunks.shift()!;\n\t\t\t\t\t\tchunksBytes -= removed.length;\n\t\t\t\t\t}\n\t\t\t\t\t// Stream partial output using the rolling tail buffer.\n\t\t\t\t\tif (onUpdate) {\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\tconst fullText = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\tconst truncation = truncateTail(fullText);\n\t\t\t\t\t\tonUpdate({\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: truncation.content || \"\" }],\n\t\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\t\ttruncation: truncation.truncated ? truncation : undefined,\n\t\t\t\t\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tops.exec(spawnContext.command, spawnContext.cwd, {\n\t\t\t\t\tonData: handleData,\n\t\t\t\t\tsignal,\n\t\t\t\t\ttimeout,\n\t\t\t\t\tenv: spawnContext.env,\n\t\t\t\t})\n\t\t\t\t\t.then(({ exitCode }) => {\n\t\t\t\t\t\t// Close temp file stream before building the final result.\n\t\t\t\t\t\tif (tempFileStream) tempFileStream.end();\n\t\t\t\t\t\t// Combine the rolling buffer chunks.\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\t// Render terminal output to collapse progress bars, handle \\r overwrites,\n\t\t\t\t\t\t// and process ANSI cursor movement — producing what a terminal would display.\n\t\t\t\t\t\tconst fullOutput = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\t// Apply tail truncation for the final display payload.\n\t\t\t\t\t\tconst truncation = truncateTail(fullOutput);\n\t\t\t\t\t\t// If truncation occurred (e.g. by line count) but no temp file was\n\t\t\t\t\t\t// created (output was under the byte-streaming threshold), write the\n\t\t\t\t\t\t// full output to a temp file so the truncation message can reference it.\n\t\t\t\t\t\tif (truncation.truncated && !tempFilePath) {\n\t\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlet outputText = truncation.content || \"(no output)\";\n\t\t\t\t\t\tlet details: BashToolDetails | undefined;\n\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t// Build truncation details and an actionable notice.\n\t\t\t\t\t\t\tdetails = { truncation, fullOutputPath: tempFilePath };\n\t\t\t\t\t\t\tconst startLine = truncation.totalLines - truncation.outputLines + 1;\n\t\t\t\t\t\t\tconst endLine = truncation.totalLines;\n\t\t\t\t\t\t\tif (truncation.lastLinePartial) {\n\t\t\t\t\t\t\t\t// Edge case: the last line alone is larger than the byte limit.\n\t\t\t\t\t\t\t\tconst lastLineSize = formatSize(Buffer.byteLength(fullOutput.split(\"\\n\").pop() || \"\", \"utf-8\"));\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else if (truncation.truncatedBy === \"lines\") {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (exitCode !== 0 && exitCode !== null) {\n\t\t\t\t\t\t\toutputText += `\\n\\nCommand exited with code ${exitCode}`;\n\t\t\t\t\t\t\treject(new Error(outputText));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: outputText }], details });\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err: Error) => {\n\t\t\t\t\t\t// Close temp file stream and include buffered output in the error message.\n\t\t\t\t\t\tif (tempFileStream) tempFileStream.end();\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\tlet output = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\tif (err.message === \"aborted\") {\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += \"Command aborted\";\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else if (err.message.startsWith(\"timeout:\")) {\n\t\t\t\t\t\t\tconst timeoutSecs = err.message.split(\":\")[1];\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += `Command timed out after ${timeoutSecs} seconds`;\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treject(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t\trenderCall(args, _theme, context) {\n\t\t\tconst state = context.state;\n\t\t\tif (context.executionStarted && state.startedAt === undefined) {\n\t\t\t\tstate.startedAt = Date.now();\n\t\t\t\tstate.endedAt = undefined;\n\t\t\t}\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatBashCall(args));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, _theme, context) {\n\t\t\tconst state = context.state;\n\t\t\tif (state.startedAt !== undefined && options.isPartial && !state.interval) {\n\t\t\t\tstate.interval = setInterval(() => context.invalidate(), 1000);\n\t\t\t}\n\t\t\tif (!options.isPartial || context.isError) {\n\t\t\t\tstate.endedAt ??= Date.now();\n\t\t\t\tif (state.interval) {\n\t\t\t\t\tclearInterval(state.interval);\n\t\t\t\t\tstate.interval = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst component =\n\t\t\t\t(context.lastComponent as BashResultRenderComponent | undefined) ?? new BashResultRenderComponent();\n\t\t\trebuildBashResultRenderComponent(\n\t\t\t\tcomponent,\n\t\t\t\tresult as any,\n\t\t\t\toptions,\n\t\t\t\tcontext.showImages,\n\t\t\t\tstate.startedAt,\n\t\t\t\tstate.endedAt,\n\t\t\t);\n\t\t\tcomponent.invalidate();\n\t\t\treturn component;\n\t\t},\n\t};\n}\n\nexport function createBashTool(cwd: string, options?: BashToolOptions): AgentTool<typeof bashSchema> {\n\treturn wrapToolDefinition(createBashToolDefinition(cwd, options));\n}\n\n/** Default bash tool using process.cwd() for backwards compatibility. */\nexport const bashToolDefinition = createBashToolDefinition(process.cwd());\nexport const bashTool = createBashTool(process.cwd());\n"]}
1
+ {"version":3,"file":"bash.js","sourceRoot":"","sources":["../../../src/core/tools/bash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACvE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAe,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;A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AK,CAAC,QAAQ,GAAG,SAAS,CAAC;gBAC5B,CAAC;YACF,CAAC;YACD,MAAM,SAAS,GACb,OAAO,CAAC,aAAuD,IAAI,IAAI,yBAAyB,EAAE,CAAC;YACrG,gCAAgC,CAC/B,SAAS,EACT,MAAa,EACb,OAAO,EACP,OAAO,CAAC,UAAU,EAClB,KAAK,CAAC,SAAS,EACf,KAAK,CAAC,OAAO,CACb,CAAC;YACF,SAAS,CAAC,UAAU,EAAE,CAAC;YACvB,OAAO,SAAS,CAAC;QAAA,CACjB;KACD,CAAC;AAAA,CACF;AAED,MAAM,UAAU,cAAc,CAAC,GAAW,EAAE,OAAyB,EAAgC;IACpG,OAAO,kBAAkB,CAAC,wBAAwB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AAAA,CAClE;AAED,yEAAyE;AACzE,MAAM,CAAC,MAAM,kBAAkB,GAAG,wBAAwB,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;AAC1E,MAAM,CAAC,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC","sourcesContent":["import { randomBytes } from \"node:crypto\";\nimport { createWriteStream, existsSync, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { AgentTool } from \"@dreb/agent-core\";\nimport { Container, Text, truncateToWidth } from \"@dreb/tui\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { spawn } from \"child_process\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.js\";\nimport { truncateToVisualLines } from \"../../modes/interactive/components/visual-truncate.js\";\nimport { theme } from \"../../modes/interactive/theme/theme.js\";\nimport { waitForChildProcess } from \"../../utils/child-process.js\";\nimport { getShellConfig, getShellEnv, killProcessTree } from \"../../utils/shell.js\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.js\";\nimport { getTextOutput, invalidArgText, str } from \"./render-utils.js\";\nimport { renderTerminalOutput } from \"./terminal-render.js\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.js\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateTail } from \"./truncate.js\";\n\n/**\n * Generate a unique temp file path for bash output.\n */\nfunction getTempFilePath(): string {\n\tconst id = randomBytes(8).toString(\"hex\");\n\treturn join(tmpdir(), `dreb-bash-${id}.log`);\n}\n\nconst bashSchema = Type.Object({\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n});\n\nexport type BashToolInput = Static<typeof bashSchema>;\n\nexport interface BashToolDetails {\n\ttruncation?: TruncationResult;\n\tfullOutputPath?: string;\n}\n\n/**\n * Pluggable operations for the bash tool.\n * Override these to delegate command execution to remote systems (for example SSH).\n */\nexport interface BashOperations {\n\t/**\n\t * Execute a command and stream output.\n\t * @param command The command to execute\n\t * @param cwd Working directory\n\t * @param options Execution options\n\t * @returns Promise resolving to exit code (null if killed)\n\t */\n\texec: (\n\t\tcommand: string,\n\t\tcwd: string,\n\t\toptions: {\n\t\t\tonData: (data: Buffer) => void;\n\t\t\tsignal?: AbortSignal;\n\t\t\ttimeout?: number;\n\t\t\tenv?: NodeJS.ProcessEnv;\n\t\t},\n\t) => Promise<{ exitCode: number | null }>;\n}\n\n/**\n * Create bash operations using dreb's built-in local shell execution backend.\n *\n * This is useful for extensions that intercept user_bash and still want dreb's\n * standard local shell behavior while wrapping or rewriting commands.\n */\nexport function createLocalBashOperations(): BashOperations {\n\treturn {\n\t\texec: (command, cwd, { onData, signal, timeout, env }) => {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tconst { shell, args } = getShellConfig();\n\t\t\t\tif (!existsSync(cwd)) {\n\t\t\t\t\treject(new Error(`Working directory does not exist: ${cwd}\\nCannot execute bash commands.`));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst child = spawn(shell, [...args, command], {\n\t\t\t\t\tcwd,\n\t\t\t\t\tdetached: true,\n\t\t\t\t\tenv: env ?? getShellEnv(),\n\t\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t\t});\n\t\t\t\tlet timedOut = false;\n\t\t\t\tlet timeoutHandle: NodeJS.Timeout | undefined;\n\t\t\t\t// Set timeout if provided.\n\t\t\t\tif (timeout !== undefined && timeout > 0) {\n\t\t\t\t\ttimeoutHandle = setTimeout(() => {\n\t\t\t\t\t\ttimedOut = true;\n\t\t\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t\t\t}, timeout * 1000);\n\t\t\t\t}\n\t\t\t\t// Stream stdout and stderr.\n\t\t\t\tchild.stdout?.on(\"data\", onData);\n\t\t\t\tchild.stderr?.on(\"data\", onData);\n\t\t\t\t// Handle abort signal by killing the entire process tree.\n\t\t\t\tconst onAbort = () => {\n\t\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t\t};\n\t\t\t\tif (signal) {\n\t\t\t\t\tif (signal.aborted) onAbort();\n\t\t\t\t\telse signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t\t// Handle shell spawn errors and wait for the process to terminate without hanging\n\t\t\t\t// on inherited stdio handles held by detached descendants.\n\t\t\t\twaitForChildProcess(child)\n\t\t\t\t\t.then((code) => {\n\t\t\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\t\t\treject(new Error(\"aborted\"));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (timedOut) {\n\t\t\t\t\t\t\treject(new Error(`timeout:${timeout}`));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tresolve({ exitCode: code });\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err) => {\n\t\t\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\t\t\tif (signal) signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\treject(err);\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t};\n}\n\nexport interface BashSpawnContext {\n\tcommand: string;\n\tcwd: string;\n\tenv: NodeJS.ProcessEnv;\n}\n\nexport type BashSpawnHook = (context: BashSpawnContext) => BashSpawnContext;\n\nfunction resolveSpawnContext(command: string, cwd: string, spawnHook?: BashSpawnHook): BashSpawnContext {\n\tconst baseContext: BashSpawnContext = { command, cwd, env: { ...getShellEnv() } };\n\treturn spawnHook ? spawnHook(baseContext) : baseContext;\n}\n\nexport interface BashToolOptions {\n\t/** Custom operations for command execution. Default: local shell */\n\toperations?: BashOperations;\n\t/** Command prefix prepended to every command (for example shell setup commands) */\n\tcommandPrefix?: string;\n\t/** Hook to adjust command, cwd, or env before execution */\n\tspawnHook?: BashSpawnHook;\n}\n\nconst BASH_PREVIEW_LINES = 5;\n\ntype BashRenderState = {\n\tstartedAt: number | undefined;\n\tendedAt: number | undefined;\n\tinterval: NodeJS.Timeout | undefined;\n};\n\ntype BashResultRenderState = {\n\tcachedWidth: number | undefined;\n\tcachedLines: string[] | undefined;\n\tcachedSkipped: number | undefined;\n};\n\nclass BashResultRenderComponent extends Container {\n\tstate: BashResultRenderState = {\n\t\tcachedWidth: undefined,\n\t\tcachedLines: undefined,\n\t\tcachedSkipped: undefined,\n\t};\n}\n\nfunction formatDuration(ms: number): string {\n\treturn `${(ms / 1000).toFixed(1)}s`;\n}\n\nfunction formatBashCall(args: { command?: string; timeout?: number } | undefined): string {\n\tconst command = str(args?.command);\n\tconst timeout = args?.timeout as number | undefined;\n\tconst timeoutSuffix = timeout ? theme.fg(\"muted\", ` (timeout ${timeout}s)`) : \"\";\n\tconst commandDisplay = command === null ? invalidArgText(theme) : command ? command : theme.fg(\"toolOutput\", \"...\");\n\treturn theme.fg(\"toolTitle\", theme.bold(`$ ${commandDisplay}`)) + timeoutSuffix;\n}\n\nfunction rebuildBashResultRenderComponent(\n\tcomponent: BashResultRenderComponent,\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\t\tdetails?: BashToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\tshowImages: boolean,\n\tstartedAt: number | undefined,\n\tendedAt: number | undefined,\n): void {\n\tconst state = component.state;\n\tcomponent.clear();\n\n\tconst output = getTextOutput(result as any, showImages).trim();\n\n\tif (output) {\n\t\tconst styledOutput = output\n\t\t\t.split(\"\\n\")\n\t\t\t.map((line) => theme.fg(\"toolOutput\", line))\n\t\t\t.join(\"\\n\");\n\n\t\tif (options.expanded) {\n\t\t\tcomponent.addChild(new Text(`\\n${styledOutput}`, 0, 0));\n\t\t} else {\n\t\t\tcomponent.addChild({\n\t\t\t\trender: (width: number) => {\n\t\t\t\t\tif (state.cachedLines === undefined || state.cachedWidth !== width) {\n\t\t\t\t\t\tconst preview = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, width);\n\t\t\t\t\t\tstate.cachedLines = preview.visualLines;\n\t\t\t\t\t\tstate.cachedSkipped = preview.skippedCount;\n\t\t\t\t\t\tstate.cachedWidth = width;\n\t\t\t\t\t}\n\t\t\t\t\tif (state.cachedSkipped && state.cachedSkipped > 0) {\n\t\t\t\t\t\tconst hint =\n\t\t\t\t\t\t\ttheme.fg(\"muted\", `... (${state.cachedSkipped} earlier lines,`) +\n\t\t\t\t\t\t\t` ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t\t\t\t\treturn [\"\", truncateToWidth(hint, width, \"...\"), ...(state.cachedLines ?? [])];\n\t\t\t\t\t}\n\t\t\t\t\treturn [\"\", ...(state.cachedLines ?? [])];\n\t\t\t\t},\n\t\t\t\tinvalidate: () => {\n\t\t\t\t\tstate.cachedWidth = undefined;\n\t\t\t\t\tstate.cachedLines = undefined;\n\t\t\t\t\tstate.cachedSkipped = undefined;\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t}\n\n\tconst truncation = result.details?.truncation;\n\tconst fullOutputPath = result.details?.fullOutputPath;\n\tif (truncation?.truncated || fullOutputPath) {\n\t\tconst warnings: string[] = [];\n\t\tif (fullOutputPath) {\n\t\t\twarnings.push(`Full output: ${fullOutputPath}`);\n\t\t}\n\t\tif (truncation?.truncated) {\n\t\t\tif (truncation.truncatedBy === \"lines\") {\n\t\t\t\twarnings.push(`Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines`);\n\t\t\t} else {\n\t\t\t\twarnings.push(\n\t\t\t\t\t`Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\tcomponent.addChild(new Text(`\\n${theme.fg(\"warning\", `[${warnings.join(\". \")}]`)}`, 0, 0));\n\t}\n\n\tif (startedAt !== undefined) {\n\t\tconst label = options.isPartial ? \"Elapsed\" : \"Took\";\n\t\tconst endTime = endedAt ?? Date.now();\n\t\tcomponent.addChild(new Text(`\\n${theme.fg(\"muted\", `${label} ${formatDuration(endTime - startedAt)}`)}`, 0, 0));\n\t}\n}\n\nexport function createBashToolDefinition(\n\tcwd: string,\n\toptions?: BashToolOptions,\n): ToolDefinition<typeof bashSchema, BashToolDetails | undefined, BashRenderState> {\n\tconst ops = options?.operations ?? createLocalBashOperations();\n\tconst commandPrefix = options?.commandPrefix;\n\tconst spawnHook = options?.spawnHook;\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription: `Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`,\n\t\tpromptSnippet: \"Execute bash commands (ls, grep, find, etc.)\",\n\t\tparameters: bashSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{ command, timeout }: { command: string; timeout?: number },\n\t\t\tsignal?: AbortSignal,\n\t\t\tonUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\tconst resolvedCommand = commandPrefix ? `${commandPrefix}\\n${command}` : command;\n\t\t\tconst spawnContext = resolveSpawnContext(resolvedCommand, cwd, spawnHook);\n\t\t\tif (onUpdate) {\n\t\t\t\tonUpdate({ content: [], details: undefined });\n\t\t\t}\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tlet tempFilePath: string | undefined;\n\t\t\t\tlet tempFileStream: ReturnType<typeof createWriteStream> | undefined;\n\t\t\t\tlet totalBytes = 0;\n\t\t\t\tconst chunks: Buffer[] = [];\n\t\t\t\tlet chunksBytes = 0;\n\t\t\t\tconst maxChunksBytes = DEFAULT_MAX_BYTES * 2;\n\n\t\t\t\tconst handleData = (data: Buffer) => {\n\t\t\t\t\ttotalBytes += data.length;\n\t\t\t\t\t// Start writing to a temp file once output exceeds the in-memory threshold.\n\t\t\t\t\tif (totalBytes > DEFAULT_MAX_BYTES && !tempFilePath) {\n\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\t// Create the file synchronously so it exists immediately for downstream checks.\n\t\t\t\t\t\twriteFileSync(tempFilePath, \"\");\n\t\t\t\t\t\ttempFileStream = createWriteStream(tempFilePath);\n\t\t\t\t\t\t// Write all buffered chunks to the file.\n\t\t\t\t\t\tfor (const chunk of chunks) tempFileStream.write(chunk);\n\t\t\t\t\t}\n\t\t\t\t\t// Write to temp file if we have one.\n\t\t\t\t\tif (tempFileStream) tempFileStream.write(data);\n\t\t\t\t\t// Keep a rolling buffer of recent output for tail truncation.\n\t\t\t\t\tchunks.push(data);\n\t\t\t\t\tchunksBytes += data.length;\n\t\t\t\t\t// Trim old chunks if the rolling buffer grows too large.\n\t\t\t\t\twhile (chunksBytes > maxChunksBytes && chunks.length > 1) {\n\t\t\t\t\t\tconst removed = chunks.shift()!;\n\t\t\t\t\t\tchunksBytes -= removed.length;\n\t\t\t\t\t}\n\t\t\t\t\t// Stream partial output using the rolling tail buffer.\n\t\t\t\t\tif (onUpdate) {\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\tconst fullText = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\tconst truncation = truncateTail(fullText);\n\t\t\t\t\t\tonUpdate({\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: truncation.content || \"\" }],\n\t\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\t\ttruncation: truncation.truncated ? truncation : undefined,\n\t\t\t\t\t\t\t\tfullOutputPath: tempFilePath,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tops.exec(spawnContext.command, spawnContext.cwd, {\n\t\t\t\t\tonData: handleData,\n\t\t\t\t\tsignal,\n\t\t\t\t\ttimeout,\n\t\t\t\t\tenv: spawnContext.env,\n\t\t\t\t})\n\t\t\t\t\t.then(({ exitCode }) => {\n\t\t\t\t\t\t// Close temp file stream before building the final result.\n\t\t\t\t\t\tif (tempFileStream) tempFileStream.end();\n\t\t\t\t\t\t// Combine the rolling buffer chunks.\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\t// Render terminal output to collapse progress bars, handle \\r overwrites,\n\t\t\t\t\t\t// and process ANSI cursor movement — producing what a terminal would display.\n\t\t\t\t\t\tconst fullOutput = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\t// Apply tail truncation for the final display payload.\n\t\t\t\t\t\tconst truncation = truncateTail(fullOutput);\n\t\t\t\t\t\t// If truncation occurred (e.g. by line count) but no temp file was\n\t\t\t\t\t\t// created (output was under the byte-streaming threshold), write the\n\t\t\t\t\t\t// full output to a temp file so the truncation message can reference it.\n\t\t\t\t\t\tif (truncation.truncated && !tempFilePath) {\n\t\t\t\t\t\t\ttempFilePath = getTempFilePath();\n\t\t\t\t\t\t\twriteFileSync(tempFilePath, fullOutput);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tlet outputText = truncation.content || \"(no output)\";\n\t\t\t\t\t\tlet details: BashToolDetails | undefined;\n\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t// Build truncation details and an actionable notice.\n\t\t\t\t\t\t\tdetails = { truncation, fullOutputPath: tempFilePath };\n\t\t\t\t\t\t\tconst startLine = truncation.totalLines - truncation.outputLines + 1;\n\t\t\t\t\t\t\tconst endLine = truncation.totalLines;\n\t\t\t\t\t\t\tif (truncation.lastLinePartial) {\n\t\t\t\t\t\t\t\t// Edge case: the last line alone is larger than the byte limit.\n\t\t\t\t\t\t\t\tconst lastLineSize = formatSize(Buffer.byteLength(fullOutput.split(\"\\n\").pop() || \"\", \"utf-8\"));\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else if (truncation.truncatedBy === \"lines\") {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${tempFilePath}]`;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (exitCode !== 0 && exitCode !== null) {\n\t\t\t\t\t\t\toutputText += `\\n\\nCommand exited with code ${exitCode}`;\n\t\t\t\t\t\t\treject(new Error(outputText));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: outputText }], details });\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((err: Error) => {\n\t\t\t\t\t\t// Close temp file stream and include buffered output in the error message.\n\t\t\t\t\t\tif (tempFileStream) tempFileStream.end();\n\t\t\t\t\t\tconst fullBuffer = Buffer.concat(chunks);\n\t\t\t\t\t\tlet output = renderTerminalOutput(fullBuffer.toString(\"utf-8\"));\n\t\t\t\t\t\tif (err.message === \"aborted\") {\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += \"Command aborted\";\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else if (err.message.startsWith(\"timeout:\")) {\n\t\t\t\t\t\t\tconst timeoutSecs = err.message.split(\":\")[1];\n\t\t\t\t\t\t\tif (output) output += \"\\n\\n\";\n\t\t\t\t\t\t\toutput += `Command timed out after ${timeoutSecs} seconds`;\n\t\t\t\t\t\t\treject(new Error(output));\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\treject(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t});\n\t\t},\n\t\trenderCall(args, _theme, context) {\n\t\t\tconst state = context.state;\n\t\t\tif (context.executionStarted && state.startedAt === undefined) {\n\t\t\t\tstate.startedAt = Date.now();\n\t\t\t\tstate.endedAt = undefined;\n\t\t\t}\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatBashCall(args));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, _theme, context) {\n\t\t\tconst state = context.state;\n\t\t\tif (state.startedAt !== undefined && options.isPartial && !state.interval) {\n\t\t\t\tstate.interval = setInterval(() => context.invalidate(), 1000);\n\t\t\t}\n\t\t\tif (!options.isPartial || context.isError) {\n\t\t\t\tstate.endedAt ??= Date.now();\n\t\t\t\tif (state.interval) {\n\t\t\t\t\tclearInterval(state.interval);\n\t\t\t\t\tstate.interval = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst component =\n\t\t\t\t(context.lastComponent as BashResultRenderComponent | undefined) ?? new BashResultRenderComponent();\n\t\t\trebuildBashResultRenderComponent(\n\t\t\t\tcomponent,\n\t\t\t\tresult as any,\n\t\t\t\toptions,\n\t\t\t\tcontext.showImages,\n\t\t\t\tstate.startedAt,\n\t\t\t\tstate.endedAt,\n\t\t\t);\n\t\t\tcomponent.invalidate();\n\t\t\treturn component;\n\t\t},\n\t};\n}\n\nexport function createBashTool(cwd: string, options?: BashToolOptions): AgentTool<typeof bashSchema> {\n\treturn wrapToolDefinition(createBashToolDefinition(cwd, options));\n}\n\n/** Default bash tool using process.cwd() for backwards compatibility. */\nexport const bashToolDefinition = createBashToolDefinition(process.cwd());\nexport const bashTool = createBashTool(process.cwd());\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"subagent.d.ts","sourceRoot":"","sources":["../../../src/core/tools/subagent.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,KAAK,MAAM,EAAQ,MAAM,mBAAmB,CAAC;AAItD,OAAO,KAAK,EAAE,cAAc,EAA2B,MAAM,wBAAwB,CAAC;AACtF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAI1D,OAAO,EAAiC,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAMrF,UAAU,eAAe;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,6EAA6E;IAC7E,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;CACrB;AAID,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,MAAM,GACb;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,eAAe,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAiDtE;AAqDD,MAAM,WAAW,cAAc;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,I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E,mBAAmB,GAAG,SAAS,CAAC,OAAO,cAAc,CAAC,CAE/G;AAED,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;yCAA8C,CAAC;AAClF,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;QAAoC,CAAC","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport { randomBytes } from \"node:crypto\";\nimport { existsSync, readdirSync, readFileSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, resolve } from \"node:path\";\nimport type { AgentTool } from \"@dreb/agent-core\";\nimport { Text } from \"@dreb/tui\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { CONFIG_DIR_NAME, getPackageDir, getSubagentSessionsDir } from \"../../config.js\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.js\";\nimport { attachJsonlLineReader } from \"../../modes/rpc/jsonl.js\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.js\";\nimport type { ModelRegistry } from \"../model-registry.js\";\nimport { resolveCliModel } from \"../model-resolver.js\";\nimport { getTextOutput, invalidArgText, str } from \"./render-utils.js\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.js\";\nimport { DEFAULT_MAX_BYTES, formatSize, type TruncationResult } from \"./truncate.js\";\n\n// ---------------------------------------------------------------------------\n// Agent type system\n// ---------------------------------------------------------------------------\n\ninterface AgentTypeConfig {\n\tname: string;\n\tdescription: string;\n\ttools?: string;\n\t/** Single model ID or ordered fallback list. First resolvable model wins. */\n\tmodel?: string | string[];\n\tsystemPrompt: string;\n}\n\nconst DEFAULT_AGENT = \"Explore\";\n\nexport function parseAgentFrontmatter(\n\tcontent: string,\n): { ok: true; config: AgentTypeConfig } | { ok: false; error: string } {\n\tconst fmMatch = content.match(/^---\\n([\\s\\S]*?)\\n---\\n?([\\s\\S]*)$/);\n\tif (!fmMatch) return { ok: false, error: \"missing --- frontmatter delimiters\" };\n\n\tconst frontmatter = fmMatch[1];\n\tconst body = fmMatch[2].trim();\n\n\tconst get = (key: string): string | undefined => {\n\t\tconst match = frontmatter.match(new RegExp(`^${key}:\\\\s*(.+)$`, \"m\"));\n\t\treturn match?.[1].trim();\n\t};\n\n\t/** Parse `model` field — supports single string, comma-separated, or YAML list syntax. */\n\tconst getModel = (): string | string[] | undefined => {\n\t\t// First check for YAML list syntax (indented lines starting with \"- \")\n\t\tconst listMatch = frontmatter.match(/^model:\\s*\\n((?:\\s+-\\s+.+\\n?)+)/m);\n\t\tif (listMatch) {\n\t\t\tconst items = listMatch[1]\n\t\t\t\t.split(\"\\n\")\n\t\t\t\t.map((line) => line.replace(/^\\s+-\\s+/, \"\").trim())\n\t\t\t\t.filter(Boolean);\n\t\t\treturn items.length > 1 ? items : items[0];\n\t\t}\n\t\t// Inline value — check for comma-separated list\n\t\tconst value = get(\"model\");\n\t\tif (!value) return undefined;\n\t\tif (value.includes(\",\")) {\n\t\t\tconst items = value\n\t\t\t\t.split(\",\")\n\t\t\t\t.map((s) => s.trim())\n\t\t\t\t.filter(Boolean);\n\t\t\treturn items.length > 1 ? items : items[0];\n\t\t}\n\t\treturn value;\n\t};\n\n\tconst name = get(\"name\");\n\tif (!name) return { ok: false, error: \"missing required 'name' field in frontmatter\" };\n\n\treturn {\n\t\tok: true,\n\t\tconfig: {\n\t\t\tname,\n\t\t\tdescription: get(\"description\") || \"\",\n\t\t\ttools: get(\"tools\"),\n\t\t\tmodel: getModel(),\n\t\t\tsystemPrompt: body,\n\t\t},\n\t};\n}\n\nfunction discoverAgentTypes(cwd: string): Map<string, AgentTypeConfig> {\n\tconst agents = new Map<string, AgentTypeConfig>();\n\n\t// Package-bundled agents (shipped with dreb — the canonical source of truth for built-in agents)\n\tconst packageAgentsDir = join(getPackageDir(), \"agents\");\n\tloadAgentsFromDir(packageAgentsDir, agents);\n\n\t// User-level agents (~/.dreb/agents/*.md)\n\tconst userDir = join(homedir(), CONFIG_DIR_NAME, \"agents\");\n\tloadAgentsFromDir(userDir, agents);\n\n\t// Project-level agents (.dreb/agents/*.md)\n\t// TODO: Security gate — prompt user for confirmation before loading agents from untrusted repos\n\tconst projectDir = join(cwd, \".dreb\", \"agents\");\n\tloadAgentsFromDir(projectDir, agents);\n\n\treturn agents;\n}\n\nfunction loadAgentsFromDir(dir: string, agents: Map<string, AgentTypeConfig>): void {\n\tif (!existsSync(dir)) return;\n\ttry {\n\t\tfor (const file of readdirSync(dir)) {\n\t\t\tif (!file.endsWith(\".md\")) continue;\n\t\t\ttry {\n\t\t\t\tconst content = readFileSync(join(dir, file), \"utf-8\");\n\t\t\t\tconst parsed = parseAgentFrontmatter(content);\n\t\t\t\tif (!parsed.ok) {\n\t\t\t\t\tconsole.error(`[subagent] Skipping agent file ${join(dir, file)}: ${parsed.error}`);\n\t\t\t\t} else {\n\t\t\t\t\tagents.set(parsed.config.name, parsed.config);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[subagent] Could not read agent file ${join(dir, file)}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t} catch (err) {\n\t\tif ((err as NodeJS.ErrnoException).code !== \"ENOENT\") {\n\t\t\tconsole.error(\n\t\t\t\t`[subagent] Could not read agents directory ${dir}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Subagent process spawning\n// ---------------------------------------------------------------------------\n\nexport interface SubagentResult {\n\tagent: string;\n\ttask: string;\n\tmodel?: string;\n\texitCode: number;\n\toutput: string;\n\tstderr: string;\n\terrorMessage: string | null;\n\t/** Path to the persisted session JSONL file, if available */\n\tsessionFile?: string;\n}\n\n// Capture at module load before process.title overwrites argv memory on Linux.\n// After process.title = \"dreb\" (in cli.ts), the original argv area is overwritten\n// and process.argv[1] may return corrupted or truncated data.\nconst DREB_SCRIPT = process.argv[1] || \"dreb\";\nconst NODE_EXEC = process.execPath;\n\n// TODO: Support PATH-based binary discovery.\n// Currently returns the captured argv[1].\nfunction findDrebBinary(): string {\n\treturn DREB_SCRIPT;\n}\n\nasync function spawnSubagent(\n\tagentConfig: AgentTypeConfig,\n\ttask: string,\n\tcwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tparentProvider?: string,\n\tsessionDir?: string,\n): Promise<SubagentResult> {\n\tconst drebBin = findDrebBinary();\n\tconsole.error(`[subagent] spawn: agent=${agentConfig.name} cwd=${cwd}`);\n\n\t// Validate cwd exists — spawn() throws a misleading ENOENT blaming the\n\t// binary when the cwd is invalid, making the real cause hard to diagnose\n\tif (!existsSync(cwd)) {\n\t\treturn {\n\t\t\tagent: agentConfig.name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Working directory does not exist: ${cwd}`,\n\t\t};\n\t}\n\n\tconst args: string[] = [\"--mode\", \"json\", \"--ui\", \"agent\"];\n\tif (sessionDir) {\n\t\targs.push(\"--session-dir\", sessionDir);\n\t} else {\n\t\targs.push(\"--no-session\");\n\t}\n\t// By spawn time, model should be a resolved single string (fallback resolution\n\t// happens in executeSingle). Handle string[] defensively by taking the first entry.\n\tconst modelStr = Array.isArray(agentConfig.model) ? agentConfig.model[0] : agentConfig.model;\n\tif (modelStr) {\n\t\targs.push(\"--model\", modelStr);\n\t\t// When the model string doesn't already specify a provider (no \"/\"),\n\t\t// inherit the parent's provider to prevent fuzzy matching from picking\n\t\t// an unauthenticated provider (e.g. Bedrock instead of Anthropic).\n\t\tif (parentProvider && !modelStr.includes(\"/\")) {\n\t\t\targs.push(\"--provider\", parentProvider);\n\t\t}\n\t}\n\tif (agentConfig.tools) {\n\t\targs.push(\"--tools\", agentConfig.tools);\n\t}\n\tif (agentConfig.systemPrompt) {\n\t\targs.push(\"--append-system-prompt\", agentConfig.systemPrompt);\n\t}\n\targs.push(\"-p\", task);\n\n\t// Early abort check — if the signal is already aborted (e.g. queued task whose\n\t// AbortController was aborted while waiting on bgAcquire), bail out before\n\t// spawning a child process that can never be killed. addEventListener(\"abort\")\n\t// on an already-aborted signal does NOT fire the callback in Node.js.\n\tif (signal?.aborted) {\n\t\treturn {\n\t\t\tagent: agentConfig.name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: \"Aborted before spawn\",\n\t\t};\n\t}\n\n\treturn new Promise<SubagentResult>((resolvePromise, rejectPromise) => {\n\t\tlet proc: ChildProcess;\n\t\ttry {\n\t\t\tproc = spawn(NODE_EXEC, [drebBin, ...args], {\n\t\t\t\tcwd,\n\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t\tenv: { ...process.env },\n\t\t\t});\n\t\t} catch (err) {\n\t\t\trejectPromise(new Error(`Failed to spawn subagent: ${err instanceof Error ? err.message : String(err)}`));\n\t\t\treturn;\n\t\t}\n\n\t\tlet settled = false;\n\t\tlet killTimer: ReturnType<typeof setTimeout> | null = null;\n\t\tconst collectedMessages: Array<{ role: string; content: any[] }> = [];\n\t\tconst stderrChunks: string[] = [];\n\t\tlet stderrSize = 0;\n\t\tconst MAX_STDERR_BYTES = 8192;\n\t\tconst plainStdoutLines: string[] = [];\n\t\tlet lastToolName = \"\";\n\t\tlet resolvedModel: string | undefined;\n\n\t\t// Drain stderr concurrently to avoid pipe deadlock (capped to prevent OOM from verbose subagents)\n\t\tproc.stderr?.on(\"data\", (chunk: Buffer) => {\n\t\t\tif (stderrSize < MAX_STDERR_BYTES) {\n\t\t\t\tconst str = chunk.toString();\n\t\t\t\tstderrChunks.push(str);\n\t\t\t\tstderrSize += str.length;\n\t\t\t}\n\t\t});\n\t\tproc.stderr?.on(\"error\", (err) => {\n\t\t\tconsole.error(`[subagent] stderr stream error (agent=${agentConfig.name}): ${err.message}`);\n\t\t});\n\n\t\t// Parse JSONL events from stdout\n\t\tif (proc.stdout) {\n\t\t\tproc.stdout.on(\"error\", (err) => {\n\t\t\t\tconsole.error(`[subagent] stdout stream error (agent=${agentConfig.name}): ${err.message}`);\n\t\t\t});\n\t\t\tattachJsonlLineReader(proc.stdout, (line) => {\n\t\t\t\tif (!line.trim()) return;\n\t\t\t\t// Separate JSON.parse from event handling so only parse failures\n\t\t\t\t// are caught as non-JSON lines — errors in handling propagate normally\n\t\t\t\tlet event: any;\n\t\t\t\ttry {\n\t\t\t\t\tevent = JSON.parse(line);\n\t\t\t\t} catch {\n\t\t\t\t\t// Capture non-JSON lines — on failure these often contain the real error\n\t\t\t\t\t// (e.g. startup errors printed before JSONL mode begins)\n\t\t\t\t\tplainStdoutLines.push(line.trim());\n\t\t\t\t\tif (line.trim().startsWith(\"{\")) {\n\t\t\t\t\t\tconsole.error(`[subagent] Failed to parse JSONL event: ${line.slice(0, 200)}`);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (event.type === \"agent_start\" && event.model) {\n\t\t\t\t\tresolvedModel = event.model.id;\n\t\t\t\t}\n\t\t\t\tif (event.type === \"message_end\" && event.message?.role === \"assistant\") {\n\t\t\t\t\tcollectedMessages.push(event.message);\n\t\t\t\t}\n\t\t\t\tif (event.type === \"tool_execution_start\" && onProgress) {\n\t\t\t\t\tlastToolName = event.toolName || \"\";\n\t\t\t\t\tonProgress(`Using ${lastToolName}...`);\n\t\t\t\t}\n\t\t\t\tif (event.type === \"tool_execution_end\" && onProgress) {\n\t\t\t\t\tonProgress(`${lastToolName} done`);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\n\t\t// Handle abort signal (guard kill() against ESRCH race if process already exited)\n\t\tconst onAbort = () => {\n\t\t\ttry {\n\t\t\t\tproc.kill(\"SIGTERM\");\n\t\t\t} catch {\n\t\t\t\t/* process already exited */\n\t\t\t}\n\t\t\tkillTimer = setTimeout(() => {\n\t\t\t\ttry {\n\t\t\t\t\tif (!proc.killed) proc.kill(\"SIGKILL\");\n\t\t\t\t} catch {\n\t\t\t\t\t/* process already exited */\n\t\t\t\t}\n\t\t\t}, 5000);\n\t\t};\n\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\n\t\tproc.on(\"error\", (err) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (killTimer) clearTimeout(killTimer);\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\trejectPromise(new Error(`Subagent process error: ${err.message}`));\n\t\t});\n\n\t\tproc.on(\"close\", (code) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (killTimer) clearTimeout(killTimer);\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\tconst exitCode = code ?? 1;\n\t\t\tconst stderr = stderrChunks.join(\"\");\n\t\t\tconsole.error(\n\t\t\t\t`[subagent] close: agent=${agentConfig.name} exit=${exitCode} messages=${collectedMessages.length}${exitCode !== 0 ? ` stderr=${stderr.slice(0, 200)} stdout=${plainStdoutLines.join(\"|\").slice(0, 200)}` : \"\"}`,\n\t\t\t);\n\n\t\t\t// Extract final text output from collected assistant messages\n\t\t\tconst outputParts: string[] = [];\n\t\t\tfor (const msg of collectedMessages) {\n\t\t\t\tif (Array.isArray(msg.content)) {\n\t\t\t\t\tfor (const part of msg.content) {\n\t\t\t\t\t\tif (part.type === \"text\" && part.text) {\n\t\t\t\t\t\t\toutputParts.push(part.text);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst output = outputParts.join(\"\\n\\n\");\n\n\t\t\t// Build error message from best available source: stderr, plain stdout lines, or generic\n\t\t\tlet errorMessage: string | null = null;\n\t\t\tif (exitCode !== 0) {\n\t\t\t\tconst stderrTrimmed = stderr.trim();\n\t\t\t\tconst plainOutput = plainStdoutLines.join(\"\\n\").trim();\n\t\t\t\terrorMessage =\n\t\t\t\t\tstderrTrimmed.slice(0, 500) || plainOutput.slice(0, 500) || `Subagent exited with code ${exitCode}`;\n\t\t\t}\n\n\t\t\t// Discover the session file written by the child process\n\t\t\tconst sessionFile = sessionDir ? discoverSessionFile(sessionDir, agentConfig.name) : undefined;\n\n\t\t\tresolvePromise({\n\t\t\t\tagent: agentConfig.name,\n\t\t\t\ttask,\n\t\t\t\tmodel:\n\t\t\t\t\tresolvedModel ??\n\t\t\t\t\t(exitCode === 0\n\t\t\t\t\t\t? Array.isArray(agentConfig.model)\n\t\t\t\t\t\t\t? agentConfig.model[0]\n\t\t\t\t\t\t\t: agentConfig.model\n\t\t\t\t\t\t: undefined),\n\t\t\t\texitCode,\n\t\t\t\toutput,\n\t\t\t\tstderr: stderr.slice(0, 2000), // cap stderr\n\t\t\t\terrorMessage,\n\t\t\t\tsessionFile,\n\t\t\t});\n\t\t});\n\t});\n}\n\n// ---------------------------------------------------------------------------\n// Session file discovery and cleanup\n// ---------------------------------------------------------------------------\n\n/**\n * Find the most recently modified .jsonl file in a session directory.\n * Returns the full path, or undefined if no session file was written\n * (e.g., subagent was killed before the first assistant message).\n */\nexport function discoverSessionFile(sessionDir: string, agentName: string): string | undefined {\n\ttry {\n\t\tif (!existsSync(sessionDir)) return undefined;\n\t\tconst files = readdirSync(sessionDir).filter((f) => f.endsWith(\".jsonl\"));\n\t\tif (files.length === 0) return undefined;\n\t\t// Pick the most recently modified file (typically there's only one per subagent dir)\n\t\tlet best: { path: string; mtime: number } | undefined;\n\t\tfor (const f of files) {\n\t\t\ttry {\n\t\t\t\tconst fullPath = join(sessionDir, f);\n\t\t\t\tconst mtime = statSync(fullPath).mtime.getTime();\n\t\t\t\tif (!best || mtime > best.mtime) {\n\t\t\t\t\tbest = { path: fullPath, mtime };\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// File disappeared or is a bad symlink — skip it, keep any valid candidate\n\t\t\t}\n\t\t}\n\t\tif (best) {\n\t\t\tconsole.error(`[subagent] session file: ${best.path} (agent=${agentName})`);\n\t\t\treturn best.path;\n\t\t}\n\t} catch (err) {\n\t\tconsole.error(\n\t\t\t`[subagent] failed to discover session file (agent=${agentName}): ${err instanceof Error ? err.message : String(err)}`,\n\t\t);\n\t}\n\treturn undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Execution modes\n// ---------------------------------------------------------------------------\n\n/**\n * Resolve a model fallback list against the registry. Tries each model in order,\n * returns the first one that resolves successfully. If all fail, returns the\n * last error. Single strings are treated as a one-element list.\n */\nexport function resolveModelWithFallbacks(\n\tmodels: string | string[],\n\tparentProvider: string | undefined,\n\tregistry: ModelRegistry | undefined,\n): { ok: true; modelId: string; provider?: string } | { ok: false; error: string } {\n\tconst modelList = Array.isArray(models) ? models : [models];\n\tlet lastError = \"\";\n\tfor (const modelStr of modelList) {\n\t\tconst result = resolveModelStringSingle(modelStr, parentProvider, registry);\n\t\tif (result.ok) return result;\n\t\tlastError = result.error;\n\t}\n\tif (modelList.length > 1) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `None of the fallback models resolved: ${modelList.join(\", \")}. Last error: ${lastError}`,\n\t\t};\n\t}\n\treturn { ok: false, error: lastError };\n}\n\nexport function resolveModelStringSingle(\n\tmodelStr: string,\n\tparentProvider: string | undefined,\n\tregistry: ModelRegistry | undefined,\n): { ok: true; modelId: string; provider?: string } | { ok: false; error: string } {\n\tif (!registry) {\n\t\treturn { ok: true, modelId: modelStr };\n\t}\n\n\t// If the model string contains \"/\" the user already specified a provider\n\tconst hasProvider = modelStr.includes(\"/\");\n\tconst resolved = resolveCliModel({\n\t\tcliProvider: hasProvider ? undefined : parentProvider,\n\t\tcliModel: modelStr,\n\t\tmodelRegistry: registry,\n\t});\n\n\tif (resolved.error) {\n\t\treturn { ok: false, error: resolved.error };\n\t}\n\tif (!resolved.model) {\n\t\treturn { ok: false, error: `Model \"${modelStr}\" not found. Use --list-models to see available models.` };\n\t}\n\n\t// resolveCliModel creates a synthetic model for any unknown ID when a\n\t// provider is specified (designed for custom/self-hosted models like Ollama).\n\t// For subagents this causes silent failures — reject synthetic fallbacks\n\t// so the next model in the fallback list is tried instead.\n\tif (resolved.isSyntheticFallback) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `Model \"${modelStr}\" not found for provider \"${resolved.model.provider}\". Use --list-models to see available models.`,\n\t\t};\n\t}\n\n\treturn { ok: true, modelId: resolved.model.id, provider: resolved.model.provider };\n}\n\nconst MAX_PARALLEL_TASKS = 8;\nconst MAX_CONCURRENCY = 4;\nconst MAX_TASK_LENGTH = 32_768; // 32 KB — prevent E2BIG from oversized argv\n\n// Semaphore for background task concurrency — shared across all background launches\nlet bgRunning = 0;\nconst bgWaiters: Array<() => void> = [];\n\nasync function bgAcquire(): Promise<void> {\n\tif (bgRunning < MAX_CONCURRENCY) {\n\t\tbgRunning++;\n\t\treturn;\n\t}\n\treturn new Promise<void>((resolve) => {\n\t\tbgWaiters.push(() => {\n\t\t\tbgRunning++;\n\t\t\tresolve();\n\t\t});\n\t});\n}\n\nfunction bgRelease(): void {\n\tbgRunning--;\n\tconst next = bgWaiters.shift();\n\tif (next) next();\n}\n\n/**\n * Resolve a per-task cwd relative to the parent cwd.\n * Rejects absolute paths and relative paths that escape the parent directory.\n * Returns a result object with ok=false and an error string on rejection, so callers can surface it to the model.\n */\nfunction clampCwd(defaultCwd: string, itemCwd?: string): { ok: true; cwd: string } | { ok: false; error: string } {\n\tif (!itemCwd) return { ok: true, cwd: defaultCwd };\n\tif (itemCwd.startsWith(\"/\")) {\n\t\treturn { ok: false, error: `Rejected absolute cwd \"${itemCwd}\" — must be relative to parent cwd` };\n\t}\n\tconst resolved = resolve(defaultCwd, itemCwd);\n\tif (resolved !== defaultCwd && !resolved.startsWith(`${defaultCwd}/`)) {\n\t\treturn { ok: false, error: `Rejected cwd \"${itemCwd}\" — resolves outside parent cwd` };\n\t}\n\treturn { ok: true, cwd: resolved };\n}\n\nasync function executeSingle(\n\tagents: Map<string, AgentTypeConfig>,\n\tagentName: string | undefined,\n\ttask: string,\n\tcwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tmodelOverride?: string,\n\tparentProvider?: string,\n\tregistry?: ModelRegistry,\n\tsessionDir?: string,\n): Promise<SubagentResult> {\n\tconst name = agentName || DEFAULT_AGENT;\n\tconst config = agents.get(name);\n\tif (!config) {\n\t\treturn {\n\t\t\tagent: name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Unknown agent type \"${name}\". Available: ${[...agents.keys()].join(\", \")}. If you expected \"${name}\" to exist, check the .md file in ~/.dreb/agents/ or .dreb/agents/ for syntax errors.`,\n\t\t};\n\t}\n\t// Validate task length for all modes (single, parallel items, chain steps)\n\tif (task.length > MAX_TASK_LENGTH) {\n\t\treturn {\n\t\t\tagent: name,\n\t\t\ttask: `${task.slice(0, 200)}...`,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Task prompt too long (${task.length} chars, max ${MAX_TASK_LENGTH}). Shorten the prompt.`,\n\t\t};\n\t}\n\t// Per-invocation model override takes precedence over agent definition model.\n\t// Override is always a single string; agent config may be a string or fallback list.\n\tconst modelSpec = modelOverride || config.model;\n\tlet effectiveConfig: AgentTypeConfig = modelOverride ? { ...config, model: modelOverride } : config;\n\tlet resolvedProvider = parentProvider;\n\n\t// Resolve and validate the model against the registry before spawning.\n\t// This catches typos and invalid model names immediately instead of failing\n\t// silently in the child process. Also passes the canonical model ID to the\n\t// child, avoiding fuzzy matching entirely.\n\tif (modelSpec) {\n\t\tconst resolved = resolveModelWithFallbacks(modelSpec, parentProvider, registry);\n\t\tif (!resolved.ok) {\n\t\t\treturn {\n\t\t\t\tagent: name,\n\t\t\t\ttask,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: resolved.error,\n\t\t\t};\n\t\t}\n\t\teffectiveConfig = { ...effectiveConfig, model: resolved.modelId };\n\t\tif (resolved.provider) {\n\t\t\tresolvedProvider = resolved.provider;\n\t\t}\n\t}\n\n\tonProgress?.(`Running ${name} agent...`);\n\treturn spawnSubagent(effectiveConfig, task, cwd, signal, onProgress, resolvedProvider, sessionDir);\n}\n\nasync function executeChain(\n\tagents: Map<string, AgentTypeConfig>,\n\tchain: Array<{ agent?: string; task: string; cwd?: string; model?: string }>,\n\tdefaultCwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tparentProvider?: string,\n\tregistry?: ModelRegistry,\n\tsessionBaseDir?: string,\n): Promise<SubagentResult[]> {\n\tconst results: SubagentResult[] = [];\n\tlet previousOutput = \"\";\n\n\tfor (let i = 0; i < chain.length; i++) {\n\t\tif (signal?.aborted) break;\n\t\tconst step = chain[i];\n\t\tconst task = step.task.replace(/\\{previous\\}/g, previousOutput);\n\t\tonProgress?.(`Chain step ${i + 1}/${chain.length}`);\n\n\t\t// Validate task length after {previous} substitution (can compound across steps)\n\t\tif (task.length > MAX_TASK_LENGTH) {\n\t\t\tresults.push({\n\t\t\t\tagent: step.agent || DEFAULT_AGENT,\n\t\t\t\ttask: `${task.slice(0, 200)}...`,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: `Task prompt too long after {previous} substitution (${task.length} chars, max ${MAX_TASK_LENGTH}). Shorten the prompt or summarize previous output.`,\n\t\t\t});\n\t\t\tbreak;\n\t\t}\n\n\t\tconst cwdResult = clampCwd(defaultCwd, step.cwd);\n\t\tif (!cwdResult.ok) {\n\t\t\tresults.push({\n\t\t\t\tagent: step.agent || DEFAULT_AGENT,\n\t\t\t\ttask,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: cwdResult.error,\n\t\t\t});\n\t\t\tbreak;\n\t\t}\n\n\t\t// Each chain step gets its own session subdirectory\n\t\tconst stepSessionDir = sessionBaseDir ? join(sessionBaseDir, `step-${i + 1}`) : undefined;\n\t\tconst result = await executeSingle(\n\t\t\tagents,\n\t\t\tstep.agent,\n\t\t\ttask,\n\t\t\tcwdResult.cwd,\n\t\t\tsignal,\n\t\t\tonProgress,\n\t\t\tstep.model,\n\t\t\tparentProvider,\n\t\t\tregistry,\n\t\t\tstepSessionDir,\n\t\t);\n\t\tresults.push(result);\n\n\t\tif (result.exitCode !== 0) {\n\t\t\tbreak; // stop chain on error\n\t\t}\n\t\tpreviousOutput = result.output;\n\t}\n\n\treturn results;\n}\n\n// ---------------------------------------------------------------------------\n// Background execution\n// ---------------------------------------------------------------------------\n\nfunction generateAgentId(): string {\n\treturn randomBytes(6).toString(\"hex\");\n}\n\n// ---------------------------------------------------------------------------\n// Background agent registry — queryable by TUI / Telegram frontends\n// ---------------------------------------------------------------------------\n\nexport interface BackgroundAgentInfo {\n\tagentId: string;\n\tagentType: string;\n\ttaskSummary: string;\n\tstartedAt: number;\n\tstatus: \"running\" | \"completed\" | \"failed\";\n}\n\nconst backgroundAgentRegistry = new Map<string, BackgroundAgentInfo>();\nconst backgroundAbortControllers = new Map<string, AbortController>();\n\n/** Get a snapshot of all tracked background agents (running and recently completed). Returns readonly clones. */\nexport function getBackgroundAgents(): readonly Readonly<BackgroundAgentInfo>[] {\n\treturn [...backgroundAgentRegistry.values()].map((a) => ({ ...a }));\n}\n\n/** Get only currently running background agents. Returns readonly clones. */\nexport function getRunningBackgroundAgents(): readonly Readonly<BackgroundAgentInfo>[] {\n\treturn [...backgroundAgentRegistry.values()].filter((a) => a.status === \"running\").map((a) => ({ ...a }));\n}\n\n/** Abort all running background agents. */\nexport function abortBackgroundAgents(): void {\n\tfor (const [id, controller] of backgroundAbortControllers) {\n\t\tcontroller.abort();\n\t\tconst entry = backgroundAgentRegistry.get(id);\n\t\tif (entry && entry.status === \"running\") {\n\t\t\tentry.status = \"failed\";\n\t\t}\n\t}\n\tbackgroundAbortControllers.clear();\n}\n\n/** Remove completed/failed entries older than the given age (ms). Default: 5 minutes. */\nexport function pruneBackgroundAgents(maxAgeMs = 5 * 60 * 1000): void {\n\tconst now = Date.now();\n\tfor (const [id, info] of backgroundAgentRegistry) {\n\t\tif (info.status !== \"running\" && now - info.startedAt > maxAgeMs) {\n\t\t\tbackgroundAgentRegistry.delete(id);\n\t\t\tbackgroundAbortControllers.delete(id);\n\t\t}\n\t}\n}\n\nexport interface SubagentToolOptions {\n\t/** Called when a background subagent starts. Used by TUI to show status indicators. */\n\tonBackgroundStart?: (agentId: string, agentType: string, taskSummary: string) => void;\n\t/** Called when a background subagent completes with its result. `cancelled` is true if the user aborted it. */\n\tonBackgroundComplete?: (agentId: string, result: SubagentResult, cancelled: boolean) => void;\n\t/** Parent session's current provider (e.g. \"anthropic\"). Called at each invocation to get the live value after mid-session model switches. */\n\tparentProvider?: () => string | undefined;\n\t/** Model registry for validating model names before spawning child processes. */\n\tmodelRegistry?: ModelRegistry;\n}\n\n// ---------------------------------------------------------------------------\n// Tool schema and definition\n// ---------------------------------------------------------------------------\n\nconst taskItemSchema = Type.Object({\n\tagent: Type.Optional(Type.String({ description: \"Agent type name (default: 'Explore')\" })),\n\ttask: Type.String({ description: \"The task prompt for this subagent\" }),\n\tcwd: Type.Optional(Type.String({ description: \"Working directory (defaults to parent's cwd)\" })),\n\tmodel: Type.Optional(\n\t\tType.String({\n\t\t\tdescription:\n\t\t\t\t\"Model override for this task. Takes precedence over agent definition model. Note: a single-string override discards the agent's fallback list.\",\n\t\t}),\n\t),\n});\n\nconst subagentSchema = Type.Object({\n\tagent: Type.Optional(Type.String({ description: \"Agent type name (default: 'Explore')\" })),\n\ttask: Type.Optional(Type.String({ description: \"Task prompt (single mode)\", minLength: 1 })),\n\tmodel: Type.Optional(\n\t\tType.String({\n\t\t\tdescription:\n\t\t\t\t\"Model override. Takes precedence over agent definition model. Note: a single-string override discards the agent's fallback list. For parallel/chain, set per-task instead.\",\n\t\t}),\n\t),\n\ttasks: Type.Optional(\n\t\tType.Array(taskItemSchema, {\n\t\t\tdescription: \"Array of tasks to run in parallel (max 8)\",\n\t\t\tminItems: 1,\n\t\t\tmaxItems: MAX_PARALLEL_TASKS,\n\t\t}),\n\t),\n\tchain: Type.Optional(\n\t\tType.Array(taskItemSchema, {\n\t\t\tdescription: \"Sequential pipeline — each step can use {previous} for prior output\",\n\t\t\tminItems: 1,\n\t\t}),\n\t),\n\t// background parameter removed — all subagents run in background mode.\n\t// Kept in schema for backward compatibility (silently ignored if passed).\n\tbackground: Type.Optional(\n\t\tType.Boolean({ description: \"Deprecated — all subagents run in background mode. This parameter is ignored.\" }),\n\t),\n});\n\nexport type SubagentToolInput = Static<typeof subagentSchema>;\n\nexport interface SubagentToolDetails {\n\ttruncation?: TruncationResult;\n\tmode: \"single\" | \"parallel\" | \"chain\";\n\tagentCount: number;\n}\n\nfunction formatSubagentCall(\n\targs: SubagentToolInput | undefined,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.js\").theme,\n\targsComplete = true,\n): string {\n\tconst invalidArg = invalidArgText(theme);\n\n\tif (args?.tasks) {\n\t\treturn (\n\t\t\ttheme.fg(\"toolTitle\", theme.bold(\"subagent\")) +\n\t\t\t\" \" +\n\t\t\ttheme.fg(\"accent\", `parallel (${args.tasks.length} tasks)`)\n\t\t);\n\t}\n\tif (args?.chain) {\n\t\treturn `${theme.fg(\"toolTitle\", theme.bold(\"subagent\"))} ${theme.fg(\"accent\", `chain (${args.chain.length} steps)`)}`;\n\t}\n\n\tconst agent = str(args?.agent) || DEFAULT_AGENT;\n\tconst model = str(args?.model);\n\tconst task = str(args?.task);\n\tconst taskPreview = task ? (task.length > 60 ? `${task.slice(0, 57)}...` : task) : null;\n\tconst modelSuffix = model ? ` ${theme.fg(\"muted\", `(${model})`)}` : \"\";\n\treturn (\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"subagent\")) +\n\t\t\" \" +\n\t\ttheme.fg(\"accent\", agent) +\n\t\tmodelSuffix +\n\t\t\" \" +\n\t\t(taskPreview === null\n\t\t\t? argsComplete\n\t\t\t\t? invalidArg\n\t\t\t\t: theme.fg(\"muted\", \"…\")\n\t\t\t: theme.fg(\"toolOutput\", `\"${taskPreview}\"`))\n\t);\n}\n\nfunction formatSubagentResult(\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string }>;\n\t\tdetails?: SubagentToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.js\").theme,\n\tshowImages: boolean,\n): string {\n\tconst output = getTextOutput(result, showImages).trim();\n\tlet text = \"\";\n\tif (output) {\n\t\tconst lines = output.split(\"\\n\");\n\t\tconst maxLines = options.expanded ? lines.length : 25;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n${displayLines.map((line) => theme.fg(\"toolOutput\", line)).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines,`)} ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t}\n\t}\n\tconst truncation = result.details?.truncation;\n\tif (truncation?.truncated) {\n\t\ttext += `\\n${theme.fg(\"warning\", `[Truncated: ${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit]`)}`;\n\t}\n\treturn text;\n}\n\nfunction formatSingleResult(result: SubagentResult): string {\n\tlet text = `## Agent: ${result.agent}${result.model ? ` (model: ${result.model})` : \"\"}\\n`;\n\tif (result.exitCode !== 0) {\n\t\ttext += `**Error** (exit ${result.exitCode}): ${result.errorMessage || \"Unknown error\"}\\n`;\n\t\tif (result.stderr) {\n\t\t\ttext += `\\nStderr:\\n${result.stderr}\\n`;\n\t\t}\n\t}\n\tif (result.output) {\n\t\ttext += `\\n${result.output}`;\n\t} else if (result.exitCode === 0) {\n\t\ttext += \"\\n(No output)\";\n\t}\n\tif (result.sessionFile) {\n\t\ttext += `\\n\\nSession log: ${result.sessionFile}`;\n\t}\n\treturn text;\n}\n\nexport function createSubagentToolDefinition(\n\tcwd: string,\n\toptions?: SubagentToolOptions,\n): ToolDefinition<typeof subagentSchema, SubagentToolDetails | undefined> {\n\tconst onBackgroundStart = options?.onBackgroundStart;\n\tconst onBackgroundComplete = options?.onBackgroundComplete;\n\tconst getParentProvider = options?.parentProvider ?? (() => undefined);\n\tconst modelRegistry = options?.modelRegistry;\n\n\t// Discover agents at definition time to build the prompt guidelines.\n\t// This is cheap (reads .md files) and the same call happens on every execute().\n\tconst knownAgents = discoverAgentTypes(cwd);\n\tconst agentListParts: string[] = [];\n\tfor (const [name, config] of knownAgents) {\n\t\tconst defaultTag = name === DEFAULT_AGENT ? \" (default)\" : \"\";\n\t\tconst desc = config.description || name;\n\t\tagentListParts.push(`'${name}'${defaultTag} — ${desc}`);\n\t}\n\tconst builtInAgentsLine = `Built-in agents: ${agentListParts.join(\"; \")}`;\n\n\treturn {\n\t\tname: \"subagent\",\n\t\tlabel: \"subagent\",\n\t\tdescription:\n\t\t\t\"Delegate tasks to independent subagents (Explore for codebase research, Sandbox for isolated /tmp-only analysis). \" +\n\t\t\t\"Supports single task, parallel (up to 8, max 4 concurrent), \" +\n\t\t\t\"and chain (sequential pipeline with {previous} substitution) modes. \" +\n\t\t\t\"All subagents run in background — returns immediately, notifies on completion.\",\n\t\tpromptSnippet: \"Delegate tasks to independent subagents\",\n\t\tpromptGuidelines: [\n\t\t\t\"Use `subagent` to delegate focused, independent tasks to child agents\",\n\t\t\t\"Available agent types can be discovered from ~/.dreb/agents/ and .dreb/agents/ markdown files\",\n\t\t\tbuiltInAgentsLine,\n\t\t\t\"Use parallel mode for independent tasks that can run concurrently\",\n\t\t\t\"Use chain mode when each step depends on the previous step's output (reference with {previous})\",\n\t\t\t\"All subagents run in background — the tool returns immediately and you are notified when each agent completes.\",\n\t\t\t\"Subagents have their own context window — provide enough context in the task prompt\",\n\t\t\t\"Each agent notifies independently when done — completion messages include a list of any still-running agents. If you need their results before proceeding, stop generating — do not output anything, do not launch filler work. Your turn ends, and when an agent completes, its result arrives as a new message that resumes your turn automatically.\",\n\t\t\t\"Agent definitions specify a `model` field with a provider fallback list (comma-separated or YAML list). The spawner tries each in order and uses the first one that resolves for the current provider. This makes agents portable across providers.\",\n\t\t\t\"Per-invocation `model` overrides take precedence but **discard the entire fallback list** — if the single override model isn't available on the current provider, the agent fails. Only override when you have a specific reason (e.g. escalating to a stronger tier for a complex task).\",\n\t\t\t\"**Model routing** — agent definitions already specify the right tier for their role. Most subagent tasks (exploration, file discovery, grep, navigation, summarization) are handled well by the defaults. Do not override the model unless the task genuinely requires a different capability tier than what the agent definition provides.\",\n\t\t],\n\t\tparameters: subagentSchema,\n\n\t\tasync execute(_toolCallId, params: SubagentToolInput, _signal, _onUpdate) {\n\t\t\tconst agents = discoverAgentTypes(cwd);\n\n\t\t\t// Determine mode\n\t\t\tconst modeCount = (params.task ? 1 : 0) + (params.tasks ? 1 : 0) + (params.chain ? 1 : 0);\n\t\t\tif (modeCount === 0) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{ type: \"text\", text: \"Error: provide one of `task` (single), `tasks` (parallel), or `chain`.\" },\n\t\t\t\t\t],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\t\t\tif (modeCount > 1) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: \"Error: modes are mutually exclusive — provide only one of `task`, `tasks`, or `chain`.\",\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// All subagents run in background mode — return immediately, notify on completion\n\t\t\t{\n\t\t\t\tif (!onBackgroundComplete) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: \"Subagent execution requires background support, which is not available in this session.\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t/**\n\t\t\t\t * Shared lifecycle for all background launches: generates agent ID,\n\t\t\t\t * sets up registry/abort/notification, gates on the concurrency\n\t\t\t\t * semaphore, and handles errors. The caller provides the actual\n\t\t\t\t * work via `runFn(signal)` which must return a SubagentResult.\n\t\t\t\t */\n\t\t\t\tconst launchBackgroundLifecycle = (\n\t\t\t\t\tagentName: string,\n\t\t\t\t\ttaskSummary: string,\n\t\t\t\t\trunFn: (signal: AbortSignal) => Promise<SubagentResult>,\n\t\t\t\t): string => {\n\t\t\t\t\tconst agentId = generateAgentId();\n\t\t\t\t\tconst bgAbort = new AbortController();\n\t\t\t\t\tbackgroundAgentRegistry.set(agentId, {\n\t\t\t\t\t\tagentId,\n\t\t\t\t\t\tagentType: agentName,\n\t\t\t\t\t\ttaskSummary,\n\t\t\t\t\t\tstartedAt: Date.now(),\n\t\t\t\t\t\tstatus: \"running\",\n\t\t\t\t\t});\n\t\t\t\t\tbackgroundAbortControllers.set(agentId, bgAbort);\n\t\t\t\t\tonBackgroundStart?.(agentId, agentName, taskSummary);\n\n\t\t\t\t\tconst bgSignal = bgAbort.signal;\n\n\t\t\t\t\tconst safeNotify = (result: SubagentResult) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tonBackgroundComplete(agentId, result, bgSignal.aborted);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t`[subagent] onBackgroundComplete threw for agent ${agentId}: ${err instanceof Error ? err.message : String(err)}. Background result lost.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tconst run = async () => {\n\t\t\t\t\t\tawait bgAcquire();\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await runFn(bgSignal);\n\t\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\t\tif (entry && !bgSignal.aborted) entry.status = result.exitCode === 0 ? \"completed\" : \"failed\";\n\t\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\t\tsafeNotify(result);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\t\tif (entry && !bgSignal.aborted) entry.status = \"failed\";\n\t\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\t\tsafeNotify({\n\t\t\t\t\t\t\t\tagent: agentName,\n\t\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\t\texitCode: 1,\n\t\t\t\t\t\t\t\toutput: \"\",\n\t\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\t\terrorMessage: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\tbgRelease();\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t\trun().catch((err) => {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[subagent] Unhandled background error (${agentId}): ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\tif (entry && entry.status === \"running\") entry.status = \"failed\";\n\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tonBackgroundComplete(\n\t\t\t\t\t\t\t\tagentId,\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tagent: agentName,\n\t\t\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\t\t\texitCode: 1,\n\t\t\t\t\t\t\t\t\toutput: \"\",\n\t\t\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\t\t\terrorMessage: `Internal error: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tbgSignal.aborted,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} catch (notifyErr) {\n\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t`[subagent] CRITICAL: Last-resort notification failed for ${agentId}: ${notifyErr instanceof Error ? notifyErr.message : String(notifyErr)}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\treturn agentId;\n\t\t\t\t};\n\n\t\t\t\t// Helper to launch a single background task\n\t\t\t\tconst subagentSessionsBase = getSubagentSessionsDir();\n\t\t\t\tconst launchBackgroundTask = (\n\t\t\t\t\tagentName: string,\n\t\t\t\t\ttask: string,\n\t\t\t\t\ttaskLabel: string,\n\t\t\t\t\ttaskCwd?: string,\n\t\t\t\t\tmodelOverride?: string,\n\t\t\t\t) => {\n\t\t\t\t\tconst resolvedCwd = taskCwd ?? cwd;\n\t\t\t\t\t// Each background agent gets its own session subdirectory\n\t\t\t\t\tconst sessionId = generateAgentId();\n\t\t\t\t\tconst sessionDir = join(subagentSessionsBase, sessionId);\n\t\t\t\t\treturn launchBackgroundLifecycle(agentName, taskLabel, (signal) =>\n\t\t\t\t\t\texecuteSingle(\n\t\t\t\t\t\t\tagents,\n\t\t\t\t\t\t\tagentName === DEFAULT_AGENT ? undefined : agentName,\n\t\t\t\t\t\t\ttask,\n\t\t\t\t\t\t\tresolvedCwd,\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tmodelOverride,\n\t\t\t\t\t\t\tgetParentProvider(),\n\t\t\t\t\t\t\tmodelRegistry,\n\t\t\t\t\t\t\tsessionDir,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t};\n\n\t\t\t\tif (params.task) {\n\t\t\t\t\t// Single background task\n\t\t\t\t\tconst agentName = params.agent || DEFAULT_AGENT;\n\t\t\t\t\tconst agentId = launchBackgroundTask(\n\t\t\t\t\t\tagentName,\n\t\t\t\t\t\tparams.task,\n\t\t\t\t\t\t`${agentName} task`,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\tparams.model,\n\t\t\t\t\t);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: `Background agent ${agentId} started (${agentName}). You will be notified when it completes.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"single\", agentCount: 1 } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: true,\n\t\t\t\t\t};\n\t\t\t\t} else if (params.tasks) {\n\t\t\t\t\t// Parallel background tasks — each gets its own agent ID and notifies independently\n\t\t\t\t\tconst launched: Array<{ id: string; taskText: string }> = [];\n\t\t\t\t\tconst skipped: Array<{ taskText: string; error: string }> = [];\n\t\t\t\t\tfor (let i = 0; i < params.tasks.length; i++) {\n\t\t\t\t\t\tconst item = params.tasks[i];\n\t\t\t\t\t\tconst agentName = item.agent || DEFAULT_AGENT;\n\t\t\t\t\t\tconst cwdResult = clampCwd(cwd, item.cwd);\n\t\t\t\t\t\tif (!cwdResult.ok) {\n\t\t\t\t\t\t\tskipped.push({ taskText: item.task, error: cwdResult.error });\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst agentId = launchBackgroundTask(\n\t\t\t\t\t\t\tagentName,\n\t\t\t\t\t\t\titem.task,\n\t\t\t\t\t\t\t`${agentName} task ${i + 1}/${params.tasks.length}`,\n\t\t\t\t\t\t\tcwdResult.cwd,\n\t\t\t\t\t\t\titem.model,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tlaunched.push({ id: agentId, taskText: item.task });\n\t\t\t\t\t}\n\t\t\t\t\tconst listing = launched.map(({ id, taskText }) => ` ${id}: ${taskText.slice(0, 80)}`).join(\"\\n\");\n\t\t\t\t\tconst skippedListing = skipped\n\t\t\t\t\t\t.map(({ taskText, error }) => ` SKIPPED: ${taskText.slice(0, 60)} — ${error}`)\n\t\t\t\t\t\t.join(\"\\n\");\n\t\t\t\t\tconst parts = [`${launched.length} background agents started:\\n${listing}`];\n\t\t\t\t\tif (skipped.length > 0) {\n\t\t\t\t\t\tparts.push(`\\n${skipped.length} task(s) failed to launch:\\n${skippedListing}`);\n\t\t\t\t\t}\n\t\t\t\t\tif (launched.length > 0) {\n\t\t\t\t\t\tparts.push(\"\\nEach will notify independently when complete.\");\n\t\t\t\t\t} else {\n\t\t\t\t\t\tparts.push(\"\\nNo agents were launched.\");\n\t\t\t\t\t}\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: parts.join(\"\\n\"),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"parallel\", agentCount: launched.length } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: launched.length > 0,\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\t// Chain mode — sequential, stays as one agent since steps depend on each other\n\t\t\t\t\tconst agentName = params.chain![0].agent || DEFAULT_AGENT;\n\t\t\t\t\tconst taskSummary = `${params.chain!.length}-step chain`;\n\t\t\t\t\tconst chainSteps = params.chain!;\n\n\t\t\t\t\tconst chainSessionDir = join(subagentSessionsBase, `chain-${generateAgentId()}`);\n\t\t\t\t\tconst agentId = launchBackgroundLifecycle(agentName, taskSummary, async (signal) => {\n\t\t\t\t\t\tconst results = await executeChain(\n\t\t\t\t\t\t\tagents,\n\t\t\t\t\t\t\tchainSteps,\n\t\t\t\t\t\t\tcwd,\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tgetParentProvider(),\n\t\t\t\t\t\t\tmodelRegistry,\n\t\t\t\t\t\t\tchainSessionDir,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst resultText = results\n\t\t\t\t\t\t\t.map((r, i) => `### Step ${i + 1}\\n${formatSingleResult(r)}`)\n\t\t\t\t\t\t\t.join(\"\\n\\n---\\n\\n\");\n\t\t\t\t\t\tconst failed = results.filter((r) => r.exitCode !== 0);\n\t\t\t\t\t\t// Per-step session logs are already embedded in resultText via formatSingleResult\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tagent: \"chain\",\n\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\texitCode: failed.length > 0 ? 1 : 0,\n\t\t\t\t\t\t\toutput: resultText,\n\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\terrorMessage:\n\t\t\t\t\t\t\t\tfailed.length > 0\n\t\t\t\t\t\t\t\t\t? `Chain stopped at step ${results.length} of ${chainSteps.length}: ${results[results.length - 1]?.errorMessage}`\n\t\t\t\t\t\t\t\t\t: null,\n\t\t\t\t\t\t};\n\t\t\t\t\t});\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: `Background chain ${agentId} started (${taskSummary}). You will be notified when it completes.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"chain\", agentCount: chainSteps.length } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: true,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\trenderCall(args, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatSubagentCall(args, theme, context.argsComplete));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatSubagentResult(result as any, options, theme, context.showImages));\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createSubagentTool(cwd: string, options?: SubagentToolOptions): AgentTool<typeof subagentSchema> {\n\treturn wrapToolDefinition(createSubagentToolDefinition(cwd, options));\n}\n\nexport const subagentToolDefinition = createSubagentToolDefinition(process.cwd());\nexport const subagentTool = createSubagentTool(process.cwd());\n"]}
1
+ {"version":3,"file":"subagent.d.ts","sourceRoot":"","sources":["../../../src/core/tools/subagent.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,KAAK,MAAM,EAAQ,MAAM,mBAAmB,CAAC;AAItD,OAAO,KAAK,EAAE,cAAc,EAA2B,MAAM,wBAAwB,CAAC;AACtF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAI1D,OAAO,EAAiC,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAMrF,UAAU,eAAe;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,6EAA6E;IAC7E,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;CACrB;AAID,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,MAAM,GACb;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,eAAe,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAiDtE;AAqDD,MAAM,WAAW,cAAc;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;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;;;;;;;;QAAoC,CAAC","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport { randomBytes } from \"node:crypto\";\nimport { existsSync, readdirSync, readFileSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, resolve } from \"node:path\";\nimport type { AgentTool } from \"@dreb/agent-core\";\nimport { Text } from \"@dreb/tui\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { CONFIG_DIR_NAME, getPackageDir, getSubagentSessionsDir } from \"../../config.js\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.js\";\nimport { attachJsonlLineReader } from \"../../modes/rpc/jsonl.js\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.js\";\nimport type { ModelRegistry } from \"../model-registry.js\";\nimport { resolveCliModel } from \"../model-resolver.js\";\nimport { getTextOutput, invalidArgText, str } from \"./render-utils.js\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.js\";\nimport { DEFAULT_MAX_BYTES, formatSize, type TruncationResult } from \"./truncate.js\";\n\n// ---------------------------------------------------------------------------\n// Agent type system\n// ---------------------------------------------------------------------------\n\ninterface AgentTypeConfig {\n\tname: string;\n\tdescription: string;\n\ttools?: string;\n\t/** Single model ID or ordered fallback list. First resolvable model wins. */\n\tmodel?: string | string[];\n\tsystemPrompt: string;\n}\n\nconst DEFAULT_AGENT = \"Explore\";\n\nexport function parseAgentFrontmatter(\n\tcontent: string,\n): { ok: true; config: AgentTypeConfig } | { ok: false; error: string } {\n\tconst fmMatch = content.match(/^---\\n([\\s\\S]*?)\\n---\\n?([\\s\\S]*)$/);\n\tif (!fmMatch) return { ok: false, error: \"missing --- frontmatter delimiters\" };\n\n\tconst frontmatter = fmMatch[1];\n\tconst body = fmMatch[2].trim();\n\n\tconst get = (key: string): string | undefined => {\n\t\tconst match = frontmatter.match(new RegExp(`^${key}:\\\\s*(.+)$`, \"m\"));\n\t\treturn match?.[1].trim();\n\t};\n\n\t/** Parse `model` field — supports single string, comma-separated, or YAML list syntax. */\n\tconst getModel = (): string | string[] | undefined => {\n\t\t// First check for YAML list syntax (indented lines starting with \"- \")\n\t\tconst listMatch = frontmatter.match(/^model:\\s*\\n((?:\\s+-\\s+.+\\n?)+)/m);\n\t\tif (listMatch) {\n\t\t\tconst items = listMatch[1]\n\t\t\t\t.split(\"\\n\")\n\t\t\t\t.map((line) => line.replace(/^\\s+-\\s+/, \"\").trim())\n\t\t\t\t.filter(Boolean);\n\t\t\treturn items.length > 1 ? items : items[0];\n\t\t}\n\t\t// Inline value — check for comma-separated list\n\t\tconst value = get(\"model\");\n\t\tif (!value) return undefined;\n\t\tif (value.includes(\",\")) {\n\t\t\tconst items = value\n\t\t\t\t.split(\",\")\n\t\t\t\t.map((s) => s.trim())\n\t\t\t\t.filter(Boolean);\n\t\t\treturn items.length > 1 ? items : items[0];\n\t\t}\n\t\treturn value;\n\t};\n\n\tconst name = get(\"name\");\n\tif (!name) return { ok: false, error: \"missing required 'name' field in frontmatter\" };\n\n\treturn {\n\t\tok: true,\n\t\tconfig: {\n\t\t\tname,\n\t\t\tdescription: get(\"description\") || \"\",\n\t\t\ttools: get(\"tools\"),\n\t\t\tmodel: getModel(),\n\t\t\tsystemPrompt: body,\n\t\t},\n\t};\n}\n\nfunction discoverAgentTypes(cwd: string): Map<string, AgentTypeConfig> {\n\tconst agents = new Map<string, AgentTypeConfig>();\n\n\t// Package-bundled agents (shipped with dreb — the canonical source of truth for built-in agents)\n\tconst packageAgentsDir = join(getPackageDir(), \"agents\");\n\tloadAgentsFromDir(packageAgentsDir, agents);\n\n\t// User-level agents (~/.dreb/agents/*.md)\n\tconst userDir = join(homedir(), CONFIG_DIR_NAME, \"agents\");\n\tloadAgentsFromDir(userDir, agents);\n\n\t// Project-level agents (.dreb/agents/*.md)\n\t// TODO: Security gate — prompt user for confirmation before loading agents from untrusted repos\n\tconst projectDir = join(cwd, \".dreb\", \"agents\");\n\tloadAgentsFromDir(projectDir, agents);\n\n\treturn agents;\n}\n\nfunction loadAgentsFromDir(dir: string, agents: Map<string, AgentTypeConfig>): void {\n\tif (!existsSync(dir)) return;\n\ttry {\n\t\tfor (const file of readdirSync(dir)) {\n\t\t\tif (!file.endsWith(\".md\")) continue;\n\t\t\ttry {\n\t\t\t\tconst content = readFileSync(join(dir, file), \"utf-8\");\n\t\t\t\tconst parsed = parseAgentFrontmatter(content);\n\t\t\t\tif (!parsed.ok) {\n\t\t\t\t\tconsole.error(`[subagent] Skipping agent file ${join(dir, file)}: ${parsed.error}`);\n\t\t\t\t} else {\n\t\t\t\t\tagents.set(parsed.config.name, parsed.config);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[subagent] Could not read agent file ${join(dir, file)}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t} catch (err) {\n\t\tif ((err as NodeJS.ErrnoException).code !== \"ENOENT\") {\n\t\t\tconsole.error(\n\t\t\t\t`[subagent] Could not read agents directory ${dir}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Subagent process spawning\n// ---------------------------------------------------------------------------\n\nexport interface SubagentResult {\n\tagent: string;\n\ttask: string;\n\tmodel?: string;\n\texitCode: number;\n\toutput: string;\n\tstderr: string;\n\terrorMessage: string | null;\n\t/** Path to the persisted session JSONL file, if available */\n\tsessionFile?: string;\n}\n\n// Capture at module load before process.title overwrites argv memory on Linux.\n// After process.title = \"dreb\" (in cli.ts), the original argv area is overwritten\n// and process.argv[1] may return corrupted or truncated data.\nconst DREB_SCRIPT = process.argv[1] || \"dreb\";\nconst NODE_EXEC = process.execPath;\n\n// TODO: Support PATH-based binary discovery.\n// Currently returns the captured argv[1].\nfunction findDrebBinary(): string {\n\treturn DREB_SCRIPT;\n}\n\nasync function spawnSubagent(\n\tagentConfig: AgentTypeConfig,\n\ttask: string,\n\tcwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tparentProvider?: string,\n\tsessionDir?: string,\n): Promise<SubagentResult> {\n\tconst drebBin = findDrebBinary();\n\tconsole.error(`[subagent] spawn: agent=${agentConfig.name} cwd=${cwd}`);\n\n\t// Validate cwd exists — spawn() throws a misleading ENOENT blaming the\n\t// binary when the cwd is invalid, making the real cause hard to diagnose\n\tif (!existsSync(cwd)) {\n\t\treturn {\n\t\t\tagent: agentConfig.name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Working directory does not exist: ${cwd}`,\n\t\t};\n\t}\n\n\tconst args: string[] = [\"--mode\", \"json\", \"--ui\", \"agent\"];\n\tif (sessionDir) {\n\t\targs.push(\"--session-dir\", sessionDir);\n\t} else {\n\t\targs.push(\"--no-session\");\n\t}\n\t// By spawn time, model should be a resolved single string (fallback resolution\n\t// happens in executeSingle). Handle string[] defensively by taking the first entry.\n\tconst modelStr = Array.isArray(agentConfig.model) ? agentConfig.model[0] : agentConfig.model;\n\tif (modelStr) {\n\t\targs.push(\"--model\", modelStr);\n\t\t// When the model string doesn't already specify a provider (no \"/\"),\n\t\t// inherit the parent's provider to prevent fuzzy matching from picking\n\t\t// an unauthenticated provider (e.g. Bedrock instead of Anthropic).\n\t\tif (parentProvider && !modelStr.includes(\"/\")) {\n\t\t\targs.push(\"--provider\", parentProvider);\n\t\t}\n\t}\n\tif (agentConfig.tools) {\n\t\targs.push(\"--tools\", agentConfig.tools);\n\t}\n\tif (agentConfig.systemPrompt) {\n\t\targs.push(\"--append-system-prompt\", agentConfig.systemPrompt);\n\t}\n\targs.push(\"-p\", task);\n\n\t// Early abort check — if the signal is already aborted (e.g. queued task whose\n\t// AbortController was aborted while waiting on bgAcquire), bail out before\n\t// spawning a child process that can never be killed. addEventListener(\"abort\")\n\t// on an already-aborted signal does NOT fire the callback in Node.js.\n\tif (signal?.aborted) {\n\t\treturn {\n\t\t\tagent: agentConfig.name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: \"Aborted before spawn\",\n\t\t};\n\t}\n\n\treturn new Promise<SubagentResult>((resolvePromise, rejectPromise) => {\n\t\tlet proc: ChildProcess;\n\t\ttry {\n\t\t\tproc = spawn(NODE_EXEC, [drebBin, ...args], {\n\t\t\t\tcwd,\n\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t\tenv: { ...process.env },\n\t\t\t});\n\t\t} catch (err) {\n\t\t\trejectPromise(new Error(`Failed to spawn subagent: ${err instanceof Error ? err.message : String(err)}`));\n\t\t\treturn;\n\t\t}\n\n\t\tlet settled = false;\n\t\tlet killTimer: ReturnType<typeof setTimeout> | null = null;\n\t\tconst collectedMessages: Array<{ role: string; content: any[] }> = [];\n\t\tconst stderrChunks: string[] = [];\n\t\tlet stderrSize = 0;\n\t\tconst MAX_STDERR_BYTES = 8192;\n\t\tconst plainStdoutLines: string[] = [];\n\t\tlet lastToolName = \"\";\n\t\tlet resolvedModel: string | undefined;\n\n\t\t// Drain stderr concurrently to avoid pipe deadlock (capped to prevent OOM from verbose subagents)\n\t\tproc.stderr?.on(\"data\", (chunk: Buffer) => {\n\t\t\tif (stderrSize < MAX_STDERR_BYTES) {\n\t\t\t\tconst str = chunk.toString();\n\t\t\t\tstderrChunks.push(str);\n\t\t\t\tstderrSize += str.length;\n\t\t\t}\n\t\t});\n\t\tproc.stderr?.on(\"error\", (err) => {\n\t\t\tconsole.error(`[subagent] stderr stream error (agent=${agentConfig.name}): ${err.message}`);\n\t\t});\n\n\t\t// Parse JSONL events from stdout\n\t\tif (proc.stdout) {\n\t\t\tproc.stdout.on(\"error\", (err) => {\n\t\t\t\tconsole.error(`[subagent] stdout stream error (agent=${agentConfig.name}): ${err.message}`);\n\t\t\t});\n\t\t\tattachJsonlLineReader(proc.stdout, (line) => {\n\t\t\t\tif (!line.trim()) return;\n\t\t\t\t// Separate JSON.parse from event handling so only parse failures\n\t\t\t\t// are caught as non-JSON lines — errors in handling propagate normally\n\t\t\t\tlet event: any;\n\t\t\t\ttry {\n\t\t\t\t\tevent = JSON.parse(line);\n\t\t\t\t} catch {\n\t\t\t\t\t// Capture non-JSON lines — on failure these often contain the real error\n\t\t\t\t\t// (e.g. startup errors printed before JSONL mode begins)\n\t\t\t\t\tplainStdoutLines.push(line.trim());\n\t\t\t\t\tif (line.trim().startsWith(\"{\")) {\n\t\t\t\t\t\tconsole.error(`[subagent] Failed to parse JSONL event: ${line.slice(0, 200)}`);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (event.type === \"agent_start\" && event.model) {\n\t\t\t\t\tresolvedModel = event.model.id;\n\t\t\t\t}\n\t\t\t\tif (event.type === \"message_end\" && event.message?.role === \"assistant\") {\n\t\t\t\t\tcollectedMessages.push(event.message);\n\t\t\t\t}\n\t\t\t\tif (event.type === \"tool_execution_start\" && onProgress) {\n\t\t\t\t\tlastToolName = event.toolName || \"\";\n\t\t\t\t\tonProgress(`Using ${lastToolName}...`);\n\t\t\t\t}\n\t\t\t\tif (event.type === \"tool_execution_end\" && onProgress) {\n\t\t\t\t\tonProgress(`${lastToolName} done`);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\n\t\t// Handle abort signal (guard kill() against ESRCH race if process already exited)\n\t\tconst onAbort = () => {\n\t\t\ttry {\n\t\t\t\tproc.kill(\"SIGTERM\");\n\t\t\t} catch {\n\t\t\t\t/* process already exited */\n\t\t\t}\n\t\t\tkillTimer = setTimeout(() => {\n\t\t\t\ttry {\n\t\t\t\t\tif (!proc.killed) proc.kill(\"SIGKILL\");\n\t\t\t\t} catch {\n\t\t\t\t\t/* process already exited */\n\t\t\t\t}\n\t\t\t}, 5000);\n\t\t};\n\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\n\t\tproc.on(\"error\", (err) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (killTimer) clearTimeout(killTimer);\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\trejectPromise(new Error(`Subagent process error: ${err.message}`));\n\t\t});\n\n\t\tproc.on(\"close\", (code) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (killTimer) clearTimeout(killTimer);\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\tconst exitCode = code ?? 1;\n\t\t\tconst stderr = stderrChunks.join(\"\");\n\t\t\tconsole.error(\n\t\t\t\t`[subagent] close: agent=${agentConfig.name} exit=${exitCode} messages=${collectedMessages.length}${exitCode !== 0 ? ` stderr=${stderr.slice(0, 200)} stdout=${plainStdoutLines.join(\"|\").slice(0, 200)}` : \"\"}`,\n\t\t\t);\n\n\t\t\t// Extract final text output from collected assistant messages\n\t\t\tconst outputParts: string[] = [];\n\t\t\tfor (const msg of collectedMessages) {\n\t\t\t\tif (Array.isArray(msg.content)) {\n\t\t\t\t\tfor (const part of msg.content) {\n\t\t\t\t\t\tif (part.type === \"text\" && part.text) {\n\t\t\t\t\t\t\toutputParts.push(part.text);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst output = outputParts.join(\"\\n\\n\");\n\n\t\t\t// Build error message from best available source: stderr, plain stdout lines, or generic\n\t\t\tlet errorMessage: string | null = null;\n\t\t\tif (exitCode !== 0) {\n\t\t\t\tconst stderrTrimmed = stderr.trim();\n\t\t\t\tconst plainOutput = plainStdoutLines.join(\"\\n\").trim();\n\t\t\t\terrorMessage =\n\t\t\t\t\tstderrTrimmed.slice(0, 500) || plainOutput.slice(0, 500) || `Subagent exited with code ${exitCode}`;\n\t\t\t}\n\n\t\t\t// Discover the session file written by the child process\n\t\t\tconst sessionFile = sessionDir ? discoverSessionFile(sessionDir, agentConfig.name) : undefined;\n\n\t\t\tresolvePromise({\n\t\t\t\tagent: agentConfig.name,\n\t\t\t\ttask,\n\t\t\t\tmodel:\n\t\t\t\t\tresolvedModel ??\n\t\t\t\t\t(exitCode === 0\n\t\t\t\t\t\t? Array.isArray(agentConfig.model)\n\t\t\t\t\t\t\t? agentConfig.model[0]\n\t\t\t\t\t\t\t: agentConfig.model\n\t\t\t\t\t\t: undefined),\n\t\t\t\texitCode,\n\t\t\t\toutput,\n\t\t\t\tstderr: stderr.slice(0, 2000), // cap stderr\n\t\t\t\terrorMessage,\n\t\t\t\tsessionFile,\n\t\t\t});\n\t\t});\n\t});\n}\n\n// ---------------------------------------------------------------------------\n// Session file discovery and cleanup\n// ---------------------------------------------------------------------------\n\n/**\n * Find the most recently modified .jsonl file in a session directory.\n * Returns the full path, or undefined if no session file was written\n * (e.g., subagent was killed before the first assistant message).\n */\nexport function discoverSessionFile(sessionDir: string, agentName: string): string | undefined {\n\ttry {\n\t\tif (!existsSync(sessionDir)) return undefined;\n\t\tconst files = readdirSync(sessionDir).filter((f) => f.endsWith(\".jsonl\"));\n\t\tif (files.length === 0) return undefined;\n\t\t// Pick the most recently modified file (typically there's only one per subagent dir)\n\t\tlet best: { path: string; mtime: number } | undefined;\n\t\tfor (const f of files) {\n\t\t\ttry {\n\t\t\t\tconst fullPath = join(sessionDir, f);\n\t\t\t\tconst mtime = statSync(fullPath).mtime.getTime();\n\t\t\t\tif (!best || mtime > best.mtime) {\n\t\t\t\t\tbest = { path: fullPath, mtime };\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// File disappeared or is a bad symlink — skip it, keep any valid candidate\n\t\t\t}\n\t\t}\n\t\tif (best) {\n\t\t\tconsole.error(`[subagent] session file: ${best.path} (agent=${agentName})`);\n\t\t\treturn best.path;\n\t\t}\n\t} catch (err) {\n\t\tconsole.error(\n\t\t\t`[subagent] failed to discover session file (agent=${agentName}): ${err instanceof Error ? err.message : String(err)}`,\n\t\t);\n\t}\n\treturn undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Execution modes\n// ---------------------------------------------------------------------------\n\n/**\n * Resolve a model fallback list against the registry. Tries each model in order,\n * returns the first one that resolves successfully. If all fail, returns the\n * last error. Single strings are treated as a one-element list.\n */\nexport function resolveModelWithFallbacks(\n\tmodels: string | string[],\n\tparentProvider: string | undefined,\n\tregistry: ModelRegistry | undefined,\n): { ok: true; modelId: string; provider?: string } | { ok: false; error: string } {\n\tconst modelList = Array.isArray(models) ? models : [models];\n\tlet lastError = \"\";\n\tfor (const modelStr of modelList) {\n\t\tconst result = resolveModelStringSingle(modelStr, parentProvider, registry);\n\t\tif (result.ok) return result;\n\t\tlastError = result.error;\n\t}\n\tif (modelList.length > 1) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `None of the fallback models resolved: ${modelList.join(\", \")}. Last error: ${lastError}`,\n\t\t};\n\t}\n\treturn { ok: false, error: lastError };\n}\n\nexport function resolveModelStringSingle(\n\tmodelStr: string,\n\tparentProvider: string | undefined,\n\tregistry: ModelRegistry | undefined,\n): { ok: true; modelId: string; provider?: string } | { ok: false; error: string } {\n\tif (!registry) {\n\t\treturn { ok: true, modelId: modelStr };\n\t}\n\n\t// If the model string contains \"/\" the user already specified a provider\n\tconst hasProvider = modelStr.includes(\"/\");\n\tconst resolved = resolveCliModel({\n\t\tcliProvider: hasProvider ? undefined : parentProvider,\n\t\tcliModel: modelStr,\n\t\tmodelRegistry: registry,\n\t});\n\n\tif (resolved.error) {\n\t\treturn { ok: false, error: resolved.error };\n\t}\n\tif (!resolved.model) {\n\t\treturn { ok: false, error: `Model \"${modelStr}\" not found. Use --list-models to see available models.` };\n\t}\n\n\t// resolveCliModel creates a synthetic model for any unknown ID when a\n\t// provider is specified (designed for custom/self-hosted models like Ollama).\n\t// For subagents this causes silent failures — reject synthetic fallbacks\n\t// so the next model in the fallback list is tried instead.\n\tif (resolved.isSyntheticFallback) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `Model \"${modelStr}\" not found for provider \"${resolved.model.provider}\". Use --list-models to see available models.`,\n\t\t};\n\t}\n\n\t// Verify the resolved provider has authentication configured.\n\t// resolveCliModel uses getAll() (all models, not just authenticated ones)\n\t// so a model can resolve successfully to a provider with no API key.\n\t// Reject early so the fallback list can continue to the next model.\n\tif (!registry.authStorage.hasAuth(resolved.model.provider)) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `No authentication configured for provider \"${resolved.model.provider}\". Model \"${modelStr}\" cannot be used.`,\n\t\t};\n\t}\n\n\treturn { ok: true, modelId: resolved.model.id, provider: resolved.model.provider };\n}\n\nconst MAX_PARALLEL_TASKS = 8;\nconst MAX_CONCURRENCY = 4;\nconst MAX_TASK_LENGTH = 32_768; // 32 KB — prevent E2BIG from oversized argv\n\n// Semaphore for background task concurrency — shared across all background launches\nlet bgRunning = 0;\nconst bgWaiters: Array<() => void> = [];\n\nasync function bgAcquire(): Promise<void> {\n\tif (bgRunning < MAX_CONCURRENCY) {\n\t\tbgRunning++;\n\t\treturn;\n\t}\n\treturn new Promise<void>((resolve) => {\n\t\tbgWaiters.push(() => {\n\t\t\tbgRunning++;\n\t\t\tresolve();\n\t\t});\n\t});\n}\n\nfunction bgRelease(): void {\n\tbgRunning--;\n\tconst next = bgWaiters.shift();\n\tif (next) next();\n}\n\n/**\n * Resolve a per-task cwd relative to the parent cwd.\n * Rejects absolute paths and relative paths that escape the parent directory.\n * Returns a result object with ok=false and an error string on rejection, so callers can surface it to the model.\n */\nfunction clampCwd(defaultCwd: string, itemCwd?: string): { ok: true; cwd: string } | { ok: false; error: string } {\n\tif (!itemCwd) return { ok: true, cwd: defaultCwd };\n\tif (itemCwd.startsWith(\"/\")) {\n\t\treturn { ok: false, error: `Rejected absolute cwd \"${itemCwd}\" — must be relative to parent cwd` };\n\t}\n\tconst resolved = resolve(defaultCwd, itemCwd);\n\tif (resolved !== defaultCwd && !resolved.startsWith(`${defaultCwd}/`)) {\n\t\treturn { ok: false, error: `Rejected cwd \"${itemCwd}\" — resolves outside parent cwd` };\n\t}\n\treturn { ok: true, cwd: resolved };\n}\n\nasync function executeSingle(\n\tagents: Map<string, AgentTypeConfig>,\n\tagentName: string | undefined,\n\ttask: string,\n\tcwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tmodelOverride?: string,\n\tparentProvider?: string,\n\tregistry?: ModelRegistry,\n\tsessionDir?: string,\n): Promise<SubagentResult> {\n\tconst name = agentName || DEFAULT_AGENT;\n\tconst config = agents.get(name);\n\tif (!config) {\n\t\treturn {\n\t\t\tagent: name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Unknown agent type \"${name}\". Available: ${[...agents.keys()].join(\", \")}. If you expected \"${name}\" to exist, check the .md file in ~/.dreb/agents/ or .dreb/agents/ for syntax errors.`,\n\t\t};\n\t}\n\t// Validate task length for all modes (single, parallel items, chain steps)\n\tif (task.length > MAX_TASK_LENGTH) {\n\t\treturn {\n\t\t\tagent: name,\n\t\t\ttask: `${task.slice(0, 200)}...`,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Task prompt too long (${task.length} chars, max ${MAX_TASK_LENGTH}). Shorten the prompt.`,\n\t\t};\n\t}\n\t// Per-invocation model override takes precedence over agent definition model.\n\t// Override is always a single string; agent config may be a string or fallback list.\n\tconst modelSpec = modelOverride || config.model;\n\tlet effectiveConfig: AgentTypeConfig = modelOverride ? { ...config, model: modelOverride } : config;\n\tlet resolvedProvider = parentProvider;\n\n\t// Resolve and validate the model against the registry before spawning.\n\t// This catches typos and invalid model names immediately instead of failing\n\t// silently in the child process. Also passes the canonical model ID to the\n\t// child, avoiding fuzzy matching entirely.\n\tif (modelSpec) {\n\t\tconst resolved = resolveModelWithFallbacks(modelSpec, parentProvider, registry);\n\t\tif (!resolved.ok) {\n\t\t\treturn {\n\t\t\t\tagent: name,\n\t\t\t\ttask,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: resolved.error,\n\t\t\t};\n\t\t}\n\t\teffectiveConfig = { ...effectiveConfig, model: resolved.modelId };\n\t\tif (resolved.provider) {\n\t\t\tresolvedProvider = resolved.provider;\n\t\t}\n\t}\n\n\tonProgress?.(`Running ${name} agent...`);\n\treturn spawnSubagent(effectiveConfig, task, cwd, signal, onProgress, resolvedProvider, sessionDir);\n}\n\nasync function executeChain(\n\tagents: Map<string, AgentTypeConfig>,\n\tchain: Array<{ agent?: string; task: string; cwd?: string; model?: string }>,\n\tdefaultCwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tparentProvider?: string,\n\tregistry?: ModelRegistry,\n\tsessionBaseDir?: string,\n): Promise<SubagentResult[]> {\n\tconst results: SubagentResult[] = [];\n\tlet previousOutput = \"\";\n\n\tfor (let i = 0; i < chain.length; i++) {\n\t\tif (signal?.aborted) break;\n\t\tconst step = chain[i];\n\t\tconst task = step.task.replace(/\\{previous\\}/g, previousOutput);\n\t\tonProgress?.(`Chain step ${i + 1}/${chain.length}`);\n\n\t\t// Validate task length after {previous} substitution (can compound across steps)\n\t\tif (task.length > MAX_TASK_LENGTH) {\n\t\t\tresults.push({\n\t\t\t\tagent: step.agent || DEFAULT_AGENT,\n\t\t\t\ttask: `${task.slice(0, 200)}...`,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: `Task prompt too long after {previous} substitution (${task.length} chars, max ${MAX_TASK_LENGTH}). Shorten the prompt or summarize previous output.`,\n\t\t\t});\n\t\t\tbreak;\n\t\t}\n\n\t\tconst cwdResult = clampCwd(defaultCwd, step.cwd);\n\t\tif (!cwdResult.ok) {\n\t\t\tresults.push({\n\t\t\t\tagent: step.agent || DEFAULT_AGENT,\n\t\t\t\ttask,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: cwdResult.error,\n\t\t\t});\n\t\t\tbreak;\n\t\t}\n\n\t\t// Each chain step gets its own session subdirectory\n\t\tconst stepSessionDir = sessionBaseDir ? join(sessionBaseDir, `step-${i + 1}`) : undefined;\n\t\tconst result = await executeSingle(\n\t\t\tagents,\n\t\t\tstep.agent,\n\t\t\ttask,\n\t\t\tcwdResult.cwd,\n\t\t\tsignal,\n\t\t\tonProgress,\n\t\t\tstep.model,\n\t\t\tparentProvider,\n\t\t\tregistry,\n\t\t\tstepSessionDir,\n\t\t);\n\t\tresults.push(result);\n\n\t\tif (result.exitCode !== 0) {\n\t\t\tbreak; // stop chain on error\n\t\t}\n\t\tpreviousOutput = result.output;\n\t}\n\n\treturn results;\n}\n\n// ---------------------------------------------------------------------------\n// Background execution\n// ---------------------------------------------------------------------------\n\nfunction generateAgentId(): string {\n\treturn randomBytes(6).toString(\"hex\");\n}\n\n// ---------------------------------------------------------------------------\n// Background agent registry — queryable by TUI / Telegram frontends\n// ---------------------------------------------------------------------------\n\nexport interface BackgroundAgentInfo {\n\tagentId: string;\n\tagentType: string;\n\ttaskSummary: string;\n\tstartedAt: number;\n\tstatus: \"running\" | \"completed\" | \"failed\";\n}\n\nconst backgroundAgentRegistry = new Map<string, BackgroundAgentInfo>();\nconst backgroundAbortControllers = new Map<string, AbortController>();\n\n/** Get a snapshot of all tracked background agents (running and recently completed). Returns readonly clones. */\nexport function getBackgroundAgents(): readonly Readonly<BackgroundAgentInfo>[] {\n\treturn [...backgroundAgentRegistry.values()].map((a) => ({ ...a }));\n}\n\n/** Get only currently running background agents. Returns readonly clones. */\nexport function getRunningBackgroundAgents(): readonly Readonly<BackgroundAgentInfo>[] {\n\treturn [...backgroundAgentRegistry.values()].filter((a) => a.status === \"running\").map((a) => ({ ...a }));\n}\n\n/** Abort all running background agents. */\nexport function abortBackgroundAgents(): void {\n\tfor (const [id, controller] of backgroundAbortControllers) {\n\t\tcontroller.abort();\n\t\tconst entry = backgroundAgentRegistry.get(id);\n\t\tif (entry && entry.status === \"running\") {\n\t\t\tentry.status = \"failed\";\n\t\t}\n\t}\n\tbackgroundAbortControllers.clear();\n}\n\n/** Remove completed/failed entries older than the given age (ms). Default: 5 minutes. */\nexport function pruneBackgroundAgents(maxAgeMs = 5 * 60 * 1000): void {\n\tconst now = Date.now();\n\tfor (const [id, info] of backgroundAgentRegistry) {\n\t\tif (info.status !== \"running\" && now - info.startedAt > maxAgeMs) {\n\t\t\tbackgroundAgentRegistry.delete(id);\n\t\t\tbackgroundAbortControllers.delete(id);\n\t\t}\n\t}\n}\n\nexport interface SubagentToolOptions {\n\t/** Called when a background subagent starts. Used by TUI to show status indicators. */\n\tonBackgroundStart?: (agentId: string, agentType: string, taskSummary: string) => void;\n\t/** Called when a background subagent completes with its result. `cancelled` is true if the user aborted it. */\n\tonBackgroundComplete?: (agentId: string, result: SubagentResult, cancelled: boolean) => void;\n\t/** Parent session's current provider (e.g. \"anthropic\"). Called at each invocation to get the live value after mid-session model switches. */\n\tparentProvider?: () => string | undefined;\n\t/** Model registry for validating model names before spawning child processes. */\n\tmodelRegistry?: ModelRegistry;\n}\n\n// ---------------------------------------------------------------------------\n// Tool schema and definition\n// ---------------------------------------------------------------------------\n\nconst taskItemSchema = Type.Object({\n\tagent: Type.Optional(Type.String({ description: \"Agent type name (default: 'Explore')\" })),\n\ttask: Type.String({ description: \"The task prompt for this subagent\" }),\n\tcwd: Type.Optional(Type.String({ description: \"Working directory (defaults to parent's cwd)\" })),\n\tmodel: Type.Optional(\n\t\tType.String({\n\t\t\tdescription:\n\t\t\t\t\"Model override for this task. Takes precedence over agent definition model. Note: a single-string override discards the agent's fallback list.\",\n\t\t}),\n\t),\n});\n\nconst subagentSchema = Type.Object({\n\tagent: Type.Optional(Type.String({ description: \"Agent type name (default: 'Explore')\" })),\n\ttask: Type.Optional(Type.String({ description: \"Task prompt (single mode)\", minLength: 1 })),\n\tmodel: Type.Optional(\n\t\tType.String({\n\t\t\tdescription:\n\t\t\t\t\"Model override. Takes precedence over agent definition model. Note: a single-string override discards the agent's fallback list. For parallel/chain, set per-task instead.\",\n\t\t}),\n\t),\n\ttasks: Type.Optional(\n\t\tType.Array(taskItemSchema, {\n\t\t\tdescription: \"Array of tasks to run in parallel (max 8)\",\n\t\t\tminItems: 1,\n\t\t\tmaxItems: MAX_PARALLEL_TASKS,\n\t\t}),\n\t),\n\tchain: Type.Optional(\n\t\tType.Array(taskItemSchema, {\n\t\t\tdescription: \"Sequential pipeline — each step can use {previous} for prior output\",\n\t\t\tminItems: 1,\n\t\t}),\n\t),\n\t// background parameter removed — all subagents run in background mode.\n\t// Kept in schema for backward compatibility (silently ignored if passed).\n\tbackground: Type.Optional(\n\t\tType.Boolean({ description: \"Deprecated — all subagents run in background mode. This parameter is ignored.\" }),\n\t),\n});\n\nexport type SubagentToolInput = Static<typeof subagentSchema>;\n\nexport interface SubagentToolDetails {\n\ttruncation?: TruncationResult;\n\tmode: \"single\" | \"parallel\" | \"chain\";\n\tagentCount: number;\n}\n\nfunction formatSubagentCall(\n\targs: SubagentToolInput | undefined,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.js\").theme,\n\targsComplete = true,\n): string {\n\tconst invalidArg = invalidArgText(theme);\n\n\tif (args?.tasks) {\n\t\treturn (\n\t\t\ttheme.fg(\"toolTitle\", theme.bold(\"subagent\")) +\n\t\t\t\" \" +\n\t\t\ttheme.fg(\"accent\", `parallel (${args.tasks.length} tasks)`)\n\t\t);\n\t}\n\tif (args?.chain) {\n\t\treturn `${theme.fg(\"toolTitle\", theme.bold(\"subagent\"))} ${theme.fg(\"accent\", `chain (${args.chain.length} steps)`)}`;\n\t}\n\n\tconst agent = str(args?.agent) || DEFAULT_AGENT;\n\tconst model = str(args?.model);\n\tconst task = str(args?.task);\n\tconst taskPreview = task ? (task.length > 60 ? `${task.slice(0, 57)}...` : task) : null;\n\tconst modelSuffix = model ? ` ${theme.fg(\"muted\", `(${model})`)}` : \"\";\n\treturn (\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"subagent\")) +\n\t\t\" \" +\n\t\ttheme.fg(\"accent\", agent) +\n\t\tmodelSuffix +\n\t\t\" \" +\n\t\t(taskPreview === null\n\t\t\t? argsComplete\n\t\t\t\t? invalidArg\n\t\t\t\t: theme.fg(\"muted\", \"…\")\n\t\t\t: theme.fg(\"toolOutput\", `\"${taskPreview}\"`))\n\t);\n}\n\nfunction formatSubagentResult(\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string }>;\n\t\tdetails?: SubagentToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.js\").theme,\n\tshowImages: boolean,\n): string {\n\tconst output = getTextOutput(result, showImages).trim();\n\tlet text = \"\";\n\tif (output) {\n\t\tconst lines = output.split(\"\\n\");\n\t\tconst maxLines = options.expanded ? lines.length : 25;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n${displayLines.map((line) => theme.fg(\"toolOutput\", line)).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines,`)} ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t}\n\t}\n\tconst truncation = result.details?.truncation;\n\tif (truncation?.truncated) {\n\t\ttext += `\\n${theme.fg(\"warning\", `[Truncated: ${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit]`)}`;\n\t}\n\treturn text;\n}\n\nfunction formatSingleResult(result: SubagentResult): string {\n\tlet text = `## Agent: ${result.agent}${result.model ? ` (model: ${result.model})` : \"\"}\\n`;\n\tif (result.exitCode !== 0) {\n\t\ttext += `**Error** (exit ${result.exitCode}): ${result.errorMessage || \"Unknown error\"}\\n`;\n\t\tif (result.stderr) {\n\t\t\ttext += `\\nStderr:\\n${result.stderr}\\n`;\n\t\t}\n\t}\n\tif (result.output) {\n\t\ttext += `\\n${result.output}`;\n\t} else if (result.exitCode === 0) {\n\t\ttext += \"\\n(No output)\";\n\t}\n\tif (result.sessionFile) {\n\t\ttext += `\\n\\nSession log: ${result.sessionFile}`;\n\t}\n\treturn text;\n}\n\nexport function createSubagentToolDefinition(\n\tcwd: string,\n\toptions?: SubagentToolOptions,\n): ToolDefinition<typeof subagentSchema, SubagentToolDetails | undefined> {\n\tconst onBackgroundStart = options?.onBackgroundStart;\n\tconst onBackgroundComplete = options?.onBackgroundComplete;\n\tconst getParentProvider = options?.parentProvider ?? (() => undefined);\n\tconst modelRegistry = options?.modelRegistry;\n\n\t// Discover agents at definition time to build the prompt guidelines.\n\t// This is cheap (reads .md files) and the same call happens on every execute().\n\tconst knownAgents = discoverAgentTypes(cwd);\n\tconst agentListParts: string[] = [];\n\tfor (const [name, config] of knownAgents) {\n\t\tconst defaultTag = name === DEFAULT_AGENT ? \" (default)\" : \"\";\n\t\tconst desc = config.description || name;\n\t\tagentListParts.push(`'${name}'${defaultTag} — ${desc}`);\n\t}\n\tconst builtInAgentsLine = `Built-in agents: ${agentListParts.join(\"; \")}`;\n\n\treturn {\n\t\tname: \"subagent\",\n\t\tlabel: \"subagent\",\n\t\tdescription:\n\t\t\t\"Delegate tasks to independent subagents (Explore for codebase research, Sandbox for isolated /tmp-only analysis). \" +\n\t\t\t\"Supports single task, parallel (up to 8, max 4 concurrent), \" +\n\t\t\t\"and chain (sequential pipeline with {previous} substitution) modes. \" +\n\t\t\t\"All subagents run in background — returns immediately, notifies on completion.\",\n\t\tpromptSnippet: \"Delegate tasks to independent subagents\",\n\t\tpromptGuidelines: [\n\t\t\t\"Use `subagent` to delegate focused, independent tasks to child agents\",\n\t\t\t\"Available agent types can be discovered from ~/.dreb/agents/ and .dreb/agents/ markdown files\",\n\t\t\tbuiltInAgentsLine,\n\t\t\t\"Use parallel mode for independent tasks that can run concurrently\",\n\t\t\t\"Use chain mode when each step depends on the previous step's output (reference with {previous})\",\n\t\t\t\"All subagents run in background — the tool returns immediately and you are notified when each agent completes.\",\n\t\t\t\"Subagents have their own context window — provide enough context in the task prompt\",\n\t\t\t\"Each agent notifies independently when done — completion messages include a list of any still-running agents. If you need their results before proceeding, stop generating — do not output anything, do not launch filler work. Your turn ends, and when an agent completes, its result arrives as a new message that resumes your turn automatically.\",\n\t\t\t\"Agent definitions specify a `model` field with a provider fallback list (comma-separated or YAML list). The spawner tries each in order and uses the first one that resolves for the current provider. This makes agents portable across providers.\",\n\t\t\t\"Per-invocation `model` overrides take precedence but **discard the entire fallback list** — if the single override model isn't available on the current provider, the agent fails. Only override when you have a specific reason (e.g. escalating to a stronger tier for a complex task).\",\n\t\t\t\"**Model routing** — agent definitions already specify the right tier for their role. Most subagent tasks (exploration, file discovery, grep, navigation, summarization) are handled well by the defaults. Do not override the model unless the task genuinely requires a different capability tier than what the agent definition provides.\",\n\t\t],\n\t\tparameters: subagentSchema,\n\n\t\tasync execute(_toolCallId, params: SubagentToolInput, _signal, _onUpdate) {\n\t\t\tconst agents = discoverAgentTypes(cwd);\n\n\t\t\t// Determine mode\n\t\t\tconst modeCount = (params.task ? 1 : 0) + (params.tasks ? 1 : 0) + (params.chain ? 1 : 0);\n\t\t\tif (modeCount === 0) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{ type: \"text\", text: \"Error: provide one of `task` (single), `tasks` (parallel), or `chain`.\" },\n\t\t\t\t\t],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\t\t\tif (modeCount > 1) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: \"Error: modes are mutually exclusive — provide only one of `task`, `tasks`, or `chain`.\",\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// All subagents run in background mode — return immediately, notify on completion\n\t\t\t{\n\t\t\t\tif (!onBackgroundComplete) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: \"Subagent execution requires background support, which is not available in this session.\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t/**\n\t\t\t\t * Shared lifecycle for all background launches: generates agent ID,\n\t\t\t\t * sets up registry/abort/notification, gates on the concurrency\n\t\t\t\t * semaphore, and handles errors. The caller provides the actual\n\t\t\t\t * work via `runFn(signal)` which must return a SubagentResult.\n\t\t\t\t */\n\t\t\t\tconst launchBackgroundLifecycle = (\n\t\t\t\t\tagentName: string,\n\t\t\t\t\ttaskSummary: string,\n\t\t\t\t\trunFn: (signal: AbortSignal) => Promise<SubagentResult>,\n\t\t\t\t): string => {\n\t\t\t\t\tconst agentId = generateAgentId();\n\t\t\t\t\tconst bgAbort = new AbortController();\n\t\t\t\t\tbackgroundAgentRegistry.set(agentId, {\n\t\t\t\t\t\tagentId,\n\t\t\t\t\t\tagentType: agentName,\n\t\t\t\t\t\ttaskSummary,\n\t\t\t\t\t\tstartedAt: Date.now(),\n\t\t\t\t\t\tstatus: \"running\",\n\t\t\t\t\t});\n\t\t\t\t\tbackgroundAbortControllers.set(agentId, bgAbort);\n\t\t\t\t\tonBackgroundStart?.(agentId, agentName, taskSummary);\n\n\t\t\t\t\tconst bgSignal = bgAbort.signal;\n\n\t\t\t\t\tconst safeNotify = (result: SubagentResult) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tonBackgroundComplete(agentId, result, bgSignal.aborted);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t`[subagent] onBackgroundComplete threw for agent ${agentId}: ${err instanceof Error ? err.message : String(err)}. Background result lost.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tconst run = async () => {\n\t\t\t\t\t\tawait bgAcquire();\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await runFn(bgSignal);\n\t\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\t\tif (entry && !bgSignal.aborted) entry.status = result.exitCode === 0 ? \"completed\" : \"failed\";\n\t\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\t\tsafeNotify(result);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\t\tif (entry && !bgSignal.aborted) entry.status = \"failed\";\n\t\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\t\tsafeNotify({\n\t\t\t\t\t\t\t\tagent: agentName,\n\t\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\t\texitCode: 1,\n\t\t\t\t\t\t\t\toutput: \"\",\n\t\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\t\terrorMessage: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\tbgRelease();\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t\trun().catch((err) => {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[subagent] Unhandled background error (${agentId}): ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\tif (entry && entry.status === \"running\") entry.status = \"failed\";\n\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tonBackgroundComplete(\n\t\t\t\t\t\t\t\tagentId,\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tagent: agentName,\n\t\t\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\t\t\texitCode: 1,\n\t\t\t\t\t\t\t\t\toutput: \"\",\n\t\t\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\t\t\terrorMessage: `Internal error: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tbgSignal.aborted,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} catch (notifyErr) {\n\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t`[subagent] CRITICAL: Last-resort notification failed for ${agentId}: ${notifyErr instanceof Error ? notifyErr.message : String(notifyErr)}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\treturn agentId;\n\t\t\t\t};\n\n\t\t\t\t// Helper to launch a single background task\n\t\t\t\tconst subagentSessionsBase = getSubagentSessionsDir();\n\t\t\t\tconst launchBackgroundTask = (\n\t\t\t\t\tagentName: string,\n\t\t\t\t\ttask: string,\n\t\t\t\t\ttaskLabel: string,\n\t\t\t\t\ttaskCwd?: string,\n\t\t\t\t\tmodelOverride?: string,\n\t\t\t\t) => {\n\t\t\t\t\tconst resolvedCwd = taskCwd ?? cwd;\n\t\t\t\t\t// Each background agent gets its own session subdirectory\n\t\t\t\t\tconst sessionId = generateAgentId();\n\t\t\t\t\tconst sessionDir = join(subagentSessionsBase, sessionId);\n\t\t\t\t\treturn launchBackgroundLifecycle(agentName, taskLabel, (signal) =>\n\t\t\t\t\t\texecuteSingle(\n\t\t\t\t\t\t\tagents,\n\t\t\t\t\t\t\tagentName === DEFAULT_AGENT ? undefined : agentName,\n\t\t\t\t\t\t\ttask,\n\t\t\t\t\t\t\tresolvedCwd,\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tmodelOverride,\n\t\t\t\t\t\t\tgetParentProvider(),\n\t\t\t\t\t\t\tmodelRegistry,\n\t\t\t\t\t\t\tsessionDir,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t};\n\n\t\t\t\tif (params.task) {\n\t\t\t\t\t// Single background task\n\t\t\t\t\tconst agentName = params.agent || DEFAULT_AGENT;\n\t\t\t\t\tconst agentId = launchBackgroundTask(\n\t\t\t\t\t\tagentName,\n\t\t\t\t\t\tparams.task,\n\t\t\t\t\t\t`${agentName} task`,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\tparams.model,\n\t\t\t\t\t);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: `Background agent ${agentId} started (${agentName}). You will be notified when it completes.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"single\", agentCount: 1 } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: true,\n\t\t\t\t\t};\n\t\t\t\t} else if (params.tasks) {\n\t\t\t\t\t// Parallel background tasks — each gets its own agent ID and notifies independently\n\t\t\t\t\tconst launched: Array<{ id: string; taskText: string }> = [];\n\t\t\t\t\tconst skipped: Array<{ taskText: string; error: string }> = [];\n\t\t\t\t\tfor (let i = 0; i < params.tasks.length; i++) {\n\t\t\t\t\t\tconst item = params.tasks[i];\n\t\t\t\t\t\tconst agentName = item.agent || DEFAULT_AGENT;\n\t\t\t\t\t\tconst cwdResult = clampCwd(cwd, item.cwd);\n\t\t\t\t\t\tif (!cwdResult.ok) {\n\t\t\t\t\t\t\tskipped.push({ taskText: item.task, error: cwdResult.error });\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst agentId = launchBackgroundTask(\n\t\t\t\t\t\t\tagentName,\n\t\t\t\t\t\t\titem.task,\n\t\t\t\t\t\t\t`${agentName} task ${i + 1}/${params.tasks.length}`,\n\t\t\t\t\t\t\tcwdResult.cwd,\n\t\t\t\t\t\t\titem.model,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tlaunched.push({ id: agentId, taskText: item.task });\n\t\t\t\t\t}\n\t\t\t\t\tconst listing = launched.map(({ id, taskText }) => ` ${id}: ${taskText.slice(0, 80)}`).join(\"\\n\");\n\t\t\t\t\tconst skippedListing = skipped\n\t\t\t\t\t\t.map(({ taskText, error }) => ` SKIPPED: ${taskText.slice(0, 60)} — ${error}`)\n\t\t\t\t\t\t.join(\"\\n\");\n\t\t\t\t\tconst parts = [`${launched.length} background agents started:\\n${listing}`];\n\t\t\t\t\tif (skipped.length > 0) {\n\t\t\t\t\t\tparts.push(`\\n${skipped.length} task(s) failed to launch:\\n${skippedListing}`);\n\t\t\t\t\t}\n\t\t\t\t\tif (launched.length > 0) {\n\t\t\t\t\t\tparts.push(\"\\nEach will notify independently when complete.\");\n\t\t\t\t\t} else {\n\t\t\t\t\t\tparts.push(\"\\nNo agents were launched.\");\n\t\t\t\t\t}\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: parts.join(\"\\n\"),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"parallel\", agentCount: launched.length } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: launched.length > 0,\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\t// Chain mode — sequential, stays as one agent since steps depend on each other\n\t\t\t\t\tconst agentName = params.chain![0].agent || DEFAULT_AGENT;\n\t\t\t\t\tconst taskSummary = `${params.chain!.length}-step chain`;\n\t\t\t\t\tconst chainSteps = params.chain!;\n\n\t\t\t\t\tconst chainSessionDir = join(subagentSessionsBase, `chain-${generateAgentId()}`);\n\t\t\t\t\tconst agentId = launchBackgroundLifecycle(agentName, taskSummary, async (signal) => {\n\t\t\t\t\t\tconst results = await executeChain(\n\t\t\t\t\t\t\tagents,\n\t\t\t\t\t\t\tchainSteps,\n\t\t\t\t\t\t\tcwd,\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tgetParentProvider(),\n\t\t\t\t\t\t\tmodelRegistry,\n\t\t\t\t\t\t\tchainSessionDir,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst resultText = results\n\t\t\t\t\t\t\t.map((r, i) => `### Step ${i + 1}\\n${formatSingleResult(r)}`)\n\t\t\t\t\t\t\t.join(\"\\n\\n---\\n\\n\");\n\t\t\t\t\t\tconst failed = results.filter((r) => r.exitCode !== 0);\n\t\t\t\t\t\t// Per-step session logs are already embedded in resultText via formatSingleResult\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tagent: \"chain\",\n\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\texitCode: failed.length > 0 ? 1 : 0,\n\t\t\t\t\t\t\toutput: resultText,\n\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\terrorMessage:\n\t\t\t\t\t\t\t\tfailed.length > 0\n\t\t\t\t\t\t\t\t\t? `Chain stopped at step ${results.length} of ${chainSteps.length}: ${results[results.length - 1]?.errorMessage}`\n\t\t\t\t\t\t\t\t\t: null,\n\t\t\t\t\t\t};\n\t\t\t\t\t});\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: `Background chain ${agentId} started (${taskSummary}). You will be notified when it completes.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"chain\", agentCount: chainSteps.length } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: true,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\trenderCall(args, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatSubagentCall(args, theme, context.argsComplete));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatSubagentResult(result as any, options, theme, context.showImages));\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createSubagentTool(cwd: string, options?: SubagentToolOptions): AgentTool<typeof subagentSchema> {\n\treturn wrapToolDefinition(createSubagentToolDefinition(cwd, options));\n}\n\nexport const subagentToolDefinition = createSubagentToolDefinition(process.cwd());\nexport const subagentTool = createSubagentTool(process.cwd());\n"]}
@@ -408,6 +408,16 @@ export function resolveModelStringSingle(modelStr, parentProvider, registry) {
408
408
  error: `Model "${modelStr}" not found for provider "${resolved.model.provider}". Use --list-models to see available models.`,
409
409
  };
410
410
  }
411
+ // Verify the resolved provider has authentication configured.
412
+ // resolveCliModel uses getAll() (all models, not just authenticated ones)
413
+ // so a model can resolve successfully to a provider with no API key.
414
+ // Reject early so the fallback list can continue to the next model.
415
+ if (!registry.authStorage.hasAuth(resolved.model.provider)) {
416
+ return {
417
+ ok: false,
418
+ error: `No authentication configured for provider "${resolved.model.provider}". Model "${modelStr}" cannot be used.`,
419
+ };
420
+ }
411
421
  return { ok: true, modelId: resolved.model.id, provider: resolved.model.provider };
412
422
  }
413
423
  const MAX_PARALLEL_TASKS = 8;
@@ -1 +1 @@
1
- {"version":3,"file":"subagent.js","sourceRoot":"","sources":["../../../src/core/tools/subagent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,I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C,GAAG,EAAE,CAAC,CAAC;AAClF,MAAM,CAAC,MAAM,YAAY,GAAG,kBAAkB,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport { randomBytes } from \"node:crypto\";\nimport { existsSync, readdirSync, readFileSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, resolve } from \"node:path\";\nimport type { AgentTool } from \"@dreb/agent-core\";\nimport { Text } from \"@dreb/tui\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { CONFIG_DIR_NAME, getPackageDir, getSubagentSessionsDir } from \"../../config.js\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.js\";\nimport { attachJsonlLineReader } from \"../../modes/rpc/jsonl.js\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.js\";\nimport type { ModelRegistry } from \"../model-registry.js\";\nimport { resolveCliModel } from \"../model-resolver.js\";\nimport { getTextOutput, invalidArgText, str } from \"./render-utils.js\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.js\";\nimport { DEFAULT_MAX_BYTES, formatSize, type TruncationResult } from \"./truncate.js\";\n\n// ---------------------------------------------------------------------------\n// Agent type system\n// ---------------------------------------------------------------------------\n\ninterface AgentTypeConfig {\n\tname: string;\n\tdescription: string;\n\ttools?: string;\n\t/** Single model ID or ordered fallback list. First resolvable model wins. */\n\tmodel?: string | string[];\n\tsystemPrompt: string;\n}\n\nconst DEFAULT_AGENT = \"Explore\";\n\nexport function parseAgentFrontmatter(\n\tcontent: string,\n): { ok: true; config: AgentTypeConfig } | { ok: false; error: string } {\n\tconst fmMatch = content.match(/^---\\n([\\s\\S]*?)\\n---\\n?([\\s\\S]*)$/);\n\tif (!fmMatch) return { ok: false, error: \"missing --- frontmatter delimiters\" };\n\n\tconst frontmatter = fmMatch[1];\n\tconst body = fmMatch[2].trim();\n\n\tconst get = (key: string): string | undefined => {\n\t\tconst match = frontmatter.match(new RegExp(`^${key}:\\\\s*(.+)$`, \"m\"));\n\t\treturn match?.[1].trim();\n\t};\n\n\t/** Parse `model` field — supports single string, comma-separated, or YAML list syntax. */\n\tconst getModel = (): string | string[] | undefined => {\n\t\t// First check for YAML list syntax (indented lines starting with \"- \")\n\t\tconst listMatch = frontmatter.match(/^model:\\s*\\n((?:\\s+-\\s+.+\\n?)+)/m);\n\t\tif (listMatch) {\n\t\t\tconst items = listMatch[1]\n\t\t\t\t.split(\"\\n\")\n\t\t\t\t.map((line) => line.replace(/^\\s+-\\s+/, \"\").trim())\n\t\t\t\t.filter(Boolean);\n\t\t\treturn items.length > 1 ? items : items[0];\n\t\t}\n\t\t// Inline value — check for comma-separated list\n\t\tconst value = get(\"model\");\n\t\tif (!value) return undefined;\n\t\tif (value.includes(\",\")) {\n\t\t\tconst items = value\n\t\t\t\t.split(\",\")\n\t\t\t\t.map((s) => s.trim())\n\t\t\t\t.filter(Boolean);\n\t\t\treturn items.length > 1 ? items : items[0];\n\t\t}\n\t\treturn value;\n\t};\n\n\tconst name = get(\"name\");\n\tif (!name) return { ok: false, error: \"missing required 'name' field in frontmatter\" };\n\n\treturn {\n\t\tok: true,\n\t\tconfig: {\n\t\t\tname,\n\t\t\tdescription: get(\"description\") || \"\",\n\t\t\ttools: get(\"tools\"),\n\t\t\tmodel: getModel(),\n\t\t\tsystemPrompt: body,\n\t\t},\n\t};\n}\n\nfunction discoverAgentTypes(cwd: string): Map<string, AgentTypeConfig> {\n\tconst agents = new Map<string, AgentTypeConfig>();\n\n\t// Package-bundled agents (shipped with dreb — the canonical source of truth for built-in agents)\n\tconst packageAgentsDir = join(getPackageDir(), \"agents\");\n\tloadAgentsFromDir(packageAgentsDir, agents);\n\n\t// User-level agents (~/.dreb/agents/*.md)\n\tconst userDir = join(homedir(), CONFIG_DIR_NAME, \"agents\");\n\tloadAgentsFromDir(userDir, agents);\n\n\t// Project-level agents (.dreb/agents/*.md)\n\t// TODO: Security gate — prompt user for confirmation before loading agents from untrusted repos\n\tconst projectDir = join(cwd, \".dreb\", \"agents\");\n\tloadAgentsFromDir(projectDir, agents);\n\n\treturn agents;\n}\n\nfunction loadAgentsFromDir(dir: string, agents: Map<string, AgentTypeConfig>): void {\n\tif (!existsSync(dir)) return;\n\ttry {\n\t\tfor (const file of readdirSync(dir)) {\n\t\t\tif (!file.endsWith(\".md\")) continue;\n\t\t\ttry {\n\t\t\t\tconst content = readFileSync(join(dir, file), \"utf-8\");\n\t\t\t\tconst parsed = parseAgentFrontmatter(content);\n\t\t\t\tif (!parsed.ok) {\n\t\t\t\t\tconsole.error(`[subagent] Skipping agent file ${join(dir, file)}: ${parsed.error}`);\n\t\t\t\t} else {\n\t\t\t\t\tagents.set(parsed.config.name, parsed.config);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[subagent] Could not read agent file ${join(dir, file)}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t} catch (err) {\n\t\tif ((err as NodeJS.ErrnoException).code !== \"ENOENT\") {\n\t\t\tconsole.error(\n\t\t\t\t`[subagent] Could not read agents directory ${dir}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Subagent process spawning\n// ---------------------------------------------------------------------------\n\nexport interface SubagentResult {\n\tagent: string;\n\ttask: string;\n\tmodel?: string;\n\texitCode: number;\n\toutput: string;\n\tstderr: string;\n\terrorMessage: string | null;\n\t/** Path to the persisted session JSONL file, if available */\n\tsessionFile?: string;\n}\n\n// Capture at module load before process.title overwrites argv memory on Linux.\n// After process.title = \"dreb\" (in cli.ts), the original argv area is overwritten\n// and process.argv[1] may return corrupted or truncated data.\nconst DREB_SCRIPT = process.argv[1] || \"dreb\";\nconst NODE_EXEC = process.execPath;\n\n// TODO: Support PATH-based binary discovery.\n// Currently returns the captured argv[1].\nfunction findDrebBinary(): string {\n\treturn DREB_SCRIPT;\n}\n\nasync function spawnSubagent(\n\tagentConfig: AgentTypeConfig,\n\ttask: string,\n\tcwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tparentProvider?: string,\n\tsessionDir?: string,\n): Promise<SubagentResult> {\n\tconst drebBin = findDrebBinary();\n\tconsole.error(`[subagent] spawn: agent=${agentConfig.name} cwd=${cwd}`);\n\n\t// Validate cwd exists — spawn() throws a misleading ENOENT blaming the\n\t// binary when the cwd is invalid, making the real cause hard to diagnose\n\tif (!existsSync(cwd)) {\n\t\treturn {\n\t\t\tagent: agentConfig.name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Working directory does not exist: ${cwd}`,\n\t\t};\n\t}\n\n\tconst args: string[] = [\"--mode\", \"json\", \"--ui\", \"agent\"];\n\tif (sessionDir) {\n\t\targs.push(\"--session-dir\", sessionDir);\n\t} else {\n\t\targs.push(\"--no-session\");\n\t}\n\t// By spawn time, model should be a resolved single string (fallback resolution\n\t// happens in executeSingle). Handle string[] defensively by taking the first entry.\n\tconst modelStr = Array.isArray(agentConfig.model) ? agentConfig.model[0] : agentConfig.model;\n\tif (modelStr) {\n\t\targs.push(\"--model\", modelStr);\n\t\t// When the model string doesn't already specify a provider (no \"/\"),\n\t\t// inherit the parent's provider to prevent fuzzy matching from picking\n\t\t// an unauthenticated provider (e.g. Bedrock instead of Anthropic).\n\t\tif (parentProvider && !modelStr.includes(\"/\")) {\n\t\t\targs.push(\"--provider\", parentProvider);\n\t\t}\n\t}\n\tif (agentConfig.tools) {\n\t\targs.push(\"--tools\", agentConfig.tools);\n\t}\n\tif (agentConfig.systemPrompt) {\n\t\targs.push(\"--append-system-prompt\", agentConfig.systemPrompt);\n\t}\n\targs.push(\"-p\", task);\n\n\t// Early abort check — if the signal is already aborted (e.g. queued task whose\n\t// AbortController was aborted while waiting on bgAcquire), bail out before\n\t// spawning a child process that can never be killed. addEventListener(\"abort\")\n\t// on an already-aborted signal does NOT fire the callback in Node.js.\n\tif (signal?.aborted) {\n\t\treturn {\n\t\t\tagent: agentConfig.name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: \"Aborted before spawn\",\n\t\t};\n\t}\n\n\treturn new Promise<SubagentResult>((resolvePromise, rejectPromise) => {\n\t\tlet proc: ChildProcess;\n\t\ttry {\n\t\t\tproc = spawn(NODE_EXEC, [drebBin, ...args], {\n\t\t\t\tcwd,\n\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t\tenv: { ...process.env },\n\t\t\t});\n\t\t} catch (err) {\n\t\t\trejectPromise(new Error(`Failed to spawn subagent: ${err instanceof Error ? err.message : String(err)}`));\n\t\t\treturn;\n\t\t}\n\n\t\tlet settled = false;\n\t\tlet killTimer: ReturnType<typeof setTimeout> | null = null;\n\t\tconst collectedMessages: Array<{ role: string; content: any[] }> = [];\n\t\tconst stderrChunks: string[] = [];\n\t\tlet stderrSize = 0;\n\t\tconst MAX_STDERR_BYTES = 8192;\n\t\tconst plainStdoutLines: string[] = [];\n\t\tlet lastToolName = \"\";\n\t\tlet resolvedModel: string | undefined;\n\n\t\t// Drain stderr concurrently to avoid pipe deadlock (capped to prevent OOM from verbose subagents)\n\t\tproc.stderr?.on(\"data\", (chunk: Buffer) => {\n\t\t\tif (stderrSize < MAX_STDERR_BYTES) {\n\t\t\t\tconst str = chunk.toString();\n\t\t\t\tstderrChunks.push(str);\n\t\t\t\tstderrSize += str.length;\n\t\t\t}\n\t\t});\n\t\tproc.stderr?.on(\"error\", (err) => {\n\t\t\tconsole.error(`[subagent] stderr stream error (agent=${agentConfig.name}): ${err.message}`);\n\t\t});\n\n\t\t// Parse JSONL events from stdout\n\t\tif (proc.stdout) {\n\t\t\tproc.stdout.on(\"error\", (err) => {\n\t\t\t\tconsole.error(`[subagent] stdout stream error (agent=${agentConfig.name}): ${err.message}`);\n\t\t\t});\n\t\t\tattachJsonlLineReader(proc.stdout, (line) => {\n\t\t\t\tif (!line.trim()) return;\n\t\t\t\t// Separate JSON.parse from event handling so only parse failures\n\t\t\t\t// are caught as non-JSON lines — errors in handling propagate normally\n\t\t\t\tlet event: any;\n\t\t\t\ttry {\n\t\t\t\t\tevent = JSON.parse(line);\n\t\t\t\t} catch {\n\t\t\t\t\t// Capture non-JSON lines — on failure these often contain the real error\n\t\t\t\t\t// (e.g. startup errors printed before JSONL mode begins)\n\t\t\t\t\tplainStdoutLines.push(line.trim());\n\t\t\t\t\tif (line.trim().startsWith(\"{\")) {\n\t\t\t\t\t\tconsole.error(`[subagent] Failed to parse JSONL event: ${line.slice(0, 200)}`);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (event.type === \"agent_start\" && event.model) {\n\t\t\t\t\tresolvedModel = event.model.id;\n\t\t\t\t}\n\t\t\t\tif (event.type === \"message_end\" && event.message?.role === \"assistant\") {\n\t\t\t\t\tcollectedMessages.push(event.message);\n\t\t\t\t}\n\t\t\t\tif (event.type === \"tool_execution_start\" && onProgress) {\n\t\t\t\t\tlastToolName = event.toolName || \"\";\n\t\t\t\t\tonProgress(`Using ${lastToolName}...`);\n\t\t\t\t}\n\t\t\t\tif (event.type === \"tool_execution_end\" && onProgress) {\n\t\t\t\t\tonProgress(`${lastToolName} done`);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\n\t\t// Handle abort signal (guard kill() against ESRCH race if process already exited)\n\t\tconst onAbort = () => {\n\t\t\ttry {\n\t\t\t\tproc.kill(\"SIGTERM\");\n\t\t\t} catch {\n\t\t\t\t/* process already exited */\n\t\t\t}\n\t\t\tkillTimer = setTimeout(() => {\n\t\t\t\ttry {\n\t\t\t\t\tif (!proc.killed) proc.kill(\"SIGKILL\");\n\t\t\t\t} catch {\n\t\t\t\t\t/* process already exited */\n\t\t\t\t}\n\t\t\t}, 5000);\n\t\t};\n\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\n\t\tproc.on(\"error\", (err) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (killTimer) clearTimeout(killTimer);\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\trejectPromise(new Error(`Subagent process error: ${err.message}`));\n\t\t});\n\n\t\tproc.on(\"close\", (code) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (killTimer) clearTimeout(killTimer);\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\tconst exitCode = code ?? 1;\n\t\t\tconst stderr = stderrChunks.join(\"\");\n\t\t\tconsole.error(\n\t\t\t\t`[subagent] close: agent=${agentConfig.name} exit=${exitCode} messages=${collectedMessages.length}${exitCode !== 0 ? ` stderr=${stderr.slice(0, 200)} stdout=${plainStdoutLines.join(\"|\").slice(0, 200)}` : \"\"}`,\n\t\t\t);\n\n\t\t\t// Extract final text output from collected assistant messages\n\t\t\tconst outputParts: string[] = [];\n\t\t\tfor (const msg of collectedMessages) {\n\t\t\t\tif (Array.isArray(msg.content)) {\n\t\t\t\t\tfor (const part of msg.content) {\n\t\t\t\t\t\tif (part.type === \"text\" && part.text) {\n\t\t\t\t\t\t\toutputParts.push(part.text);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst output = outputParts.join(\"\\n\\n\");\n\n\t\t\t// Build error message from best available source: stderr, plain stdout lines, or generic\n\t\t\tlet errorMessage: string | null = null;\n\t\t\tif (exitCode !== 0) {\n\t\t\t\tconst stderrTrimmed = stderr.trim();\n\t\t\t\tconst plainOutput = plainStdoutLines.join(\"\\n\").trim();\n\t\t\t\terrorMessage =\n\t\t\t\t\tstderrTrimmed.slice(0, 500) || plainOutput.slice(0, 500) || `Subagent exited with code ${exitCode}`;\n\t\t\t}\n\n\t\t\t// Discover the session file written by the child process\n\t\t\tconst sessionFile = sessionDir ? discoverSessionFile(sessionDir, agentConfig.name) : undefined;\n\n\t\t\tresolvePromise({\n\t\t\t\tagent: agentConfig.name,\n\t\t\t\ttask,\n\t\t\t\tmodel:\n\t\t\t\t\tresolvedModel ??\n\t\t\t\t\t(exitCode === 0\n\t\t\t\t\t\t? Array.isArray(agentConfig.model)\n\t\t\t\t\t\t\t? agentConfig.model[0]\n\t\t\t\t\t\t\t: agentConfig.model\n\t\t\t\t\t\t: undefined),\n\t\t\t\texitCode,\n\t\t\t\toutput,\n\t\t\t\tstderr: stderr.slice(0, 2000), // cap stderr\n\t\t\t\terrorMessage,\n\t\t\t\tsessionFile,\n\t\t\t});\n\t\t});\n\t});\n}\n\n// ---------------------------------------------------------------------------\n// Session file discovery and cleanup\n// ---------------------------------------------------------------------------\n\n/**\n * Find the most recently modified .jsonl file in a session directory.\n * Returns the full path, or undefined if no session file was written\n * (e.g., subagent was killed before the first assistant message).\n */\nexport function discoverSessionFile(sessionDir: string, agentName: string): string | undefined {\n\ttry {\n\t\tif (!existsSync(sessionDir)) return undefined;\n\t\tconst files = readdirSync(sessionDir).filter((f) => f.endsWith(\".jsonl\"));\n\t\tif (files.length === 0) return undefined;\n\t\t// Pick the most recently modified file (typically there's only one per subagent dir)\n\t\tlet best: { path: string; mtime: number } | undefined;\n\t\tfor (const f of files) {\n\t\t\ttry {\n\t\t\t\tconst fullPath = join(sessionDir, f);\n\t\t\t\tconst mtime = statSync(fullPath).mtime.getTime();\n\t\t\t\tif (!best || mtime > best.mtime) {\n\t\t\t\t\tbest = { path: fullPath, mtime };\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// File disappeared or is a bad symlink — skip it, keep any valid candidate\n\t\t\t}\n\t\t}\n\t\tif (best) {\n\t\t\tconsole.error(`[subagent] session file: ${best.path} (agent=${agentName})`);\n\t\t\treturn best.path;\n\t\t}\n\t} catch (err) {\n\t\tconsole.error(\n\t\t\t`[subagent] failed to discover session file (agent=${agentName}): ${err instanceof Error ? err.message : String(err)}`,\n\t\t);\n\t}\n\treturn undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Execution modes\n// ---------------------------------------------------------------------------\n\n/**\n * Resolve a model fallback list against the registry. Tries each model in order,\n * returns the first one that resolves successfully. If all fail, returns the\n * last error. Single strings are treated as a one-element list.\n */\nexport function resolveModelWithFallbacks(\n\tmodels: string | string[],\n\tparentProvider: string | undefined,\n\tregistry: ModelRegistry | undefined,\n): { ok: true; modelId: string; provider?: string } | { ok: false; error: string } {\n\tconst modelList = Array.isArray(models) ? models : [models];\n\tlet lastError = \"\";\n\tfor (const modelStr of modelList) {\n\t\tconst result = resolveModelStringSingle(modelStr, parentProvider, registry);\n\t\tif (result.ok) return result;\n\t\tlastError = result.error;\n\t}\n\tif (modelList.length > 1) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `None of the fallback models resolved: ${modelList.join(\", \")}. Last error: ${lastError}`,\n\t\t};\n\t}\n\treturn { ok: false, error: lastError };\n}\n\nexport function resolveModelStringSingle(\n\tmodelStr: string,\n\tparentProvider: string | undefined,\n\tregistry: ModelRegistry | undefined,\n): { ok: true; modelId: string; provider?: string } | { ok: false; error: string } {\n\tif (!registry) {\n\t\treturn { ok: true, modelId: modelStr };\n\t}\n\n\t// If the model string contains \"/\" the user already specified a provider\n\tconst hasProvider = modelStr.includes(\"/\");\n\tconst resolved = resolveCliModel({\n\t\tcliProvider: hasProvider ? undefined : parentProvider,\n\t\tcliModel: modelStr,\n\t\tmodelRegistry: registry,\n\t});\n\n\tif (resolved.error) {\n\t\treturn { ok: false, error: resolved.error };\n\t}\n\tif (!resolved.model) {\n\t\treturn { ok: false, error: `Model \"${modelStr}\" not found. Use --list-models to see available models.` };\n\t}\n\n\t// resolveCliModel creates a synthetic model for any unknown ID when a\n\t// provider is specified (designed for custom/self-hosted models like Ollama).\n\t// For subagents this causes silent failures — reject synthetic fallbacks\n\t// so the next model in the fallback list is tried instead.\n\tif (resolved.isSyntheticFallback) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `Model \"${modelStr}\" not found for provider \"${resolved.model.provider}\". Use --list-models to see available models.`,\n\t\t};\n\t}\n\n\treturn { ok: true, modelId: resolved.model.id, provider: resolved.model.provider };\n}\n\nconst MAX_PARALLEL_TASKS = 8;\nconst MAX_CONCURRENCY = 4;\nconst MAX_TASK_LENGTH = 32_768; // 32 KB — prevent E2BIG from oversized argv\n\n// Semaphore for background task concurrency — shared across all background launches\nlet bgRunning = 0;\nconst bgWaiters: Array<() => void> = [];\n\nasync function bgAcquire(): Promise<void> {\n\tif (bgRunning < MAX_CONCURRENCY) {\n\t\tbgRunning++;\n\t\treturn;\n\t}\n\treturn new Promise<void>((resolve) => {\n\t\tbgWaiters.push(() => {\n\t\t\tbgRunning++;\n\t\t\tresolve();\n\t\t});\n\t});\n}\n\nfunction bgRelease(): void {\n\tbgRunning--;\n\tconst next = bgWaiters.shift();\n\tif (next) next();\n}\n\n/**\n * Resolve a per-task cwd relative to the parent cwd.\n * Rejects absolute paths and relative paths that escape the parent directory.\n * Returns a result object with ok=false and an error string on rejection, so callers can surface it to the model.\n */\nfunction clampCwd(defaultCwd: string, itemCwd?: string): { ok: true; cwd: string } | { ok: false; error: string } {\n\tif (!itemCwd) return { ok: true, cwd: defaultCwd };\n\tif (itemCwd.startsWith(\"/\")) {\n\t\treturn { ok: false, error: `Rejected absolute cwd \"${itemCwd}\" — must be relative to parent cwd` };\n\t}\n\tconst resolved = resolve(defaultCwd, itemCwd);\n\tif (resolved !== defaultCwd && !resolved.startsWith(`${defaultCwd}/`)) {\n\t\treturn { ok: false, error: `Rejected cwd \"${itemCwd}\" — resolves outside parent cwd` };\n\t}\n\treturn { ok: true, cwd: resolved };\n}\n\nasync function executeSingle(\n\tagents: Map<string, AgentTypeConfig>,\n\tagentName: string | undefined,\n\ttask: string,\n\tcwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tmodelOverride?: string,\n\tparentProvider?: string,\n\tregistry?: ModelRegistry,\n\tsessionDir?: string,\n): Promise<SubagentResult> {\n\tconst name = agentName || DEFAULT_AGENT;\n\tconst config = agents.get(name);\n\tif (!config) {\n\t\treturn {\n\t\t\tagent: name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Unknown agent type \"${name}\". Available: ${[...agents.keys()].join(\", \")}. If you expected \"${name}\" to exist, check the .md file in ~/.dreb/agents/ or .dreb/agents/ for syntax errors.`,\n\t\t};\n\t}\n\t// Validate task length for all modes (single, parallel items, chain steps)\n\tif (task.length > MAX_TASK_LENGTH) {\n\t\treturn {\n\t\t\tagent: name,\n\t\t\ttask: `${task.slice(0, 200)}...`,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Task prompt too long (${task.length} chars, max ${MAX_TASK_LENGTH}). Shorten the prompt.`,\n\t\t};\n\t}\n\t// Per-invocation model override takes precedence over agent definition model.\n\t// Override is always a single string; agent config may be a string or fallback list.\n\tconst modelSpec = modelOverride || config.model;\n\tlet effectiveConfig: AgentTypeConfig = modelOverride ? { ...config, model: modelOverride } : config;\n\tlet resolvedProvider = parentProvider;\n\n\t// Resolve and validate the model against the registry before spawning.\n\t// This catches typos and invalid model names immediately instead of failing\n\t// silently in the child process. Also passes the canonical model ID to the\n\t// child, avoiding fuzzy matching entirely.\n\tif (modelSpec) {\n\t\tconst resolved = resolveModelWithFallbacks(modelSpec, parentProvider, registry);\n\t\tif (!resolved.ok) {\n\t\t\treturn {\n\t\t\t\tagent: name,\n\t\t\t\ttask,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: resolved.error,\n\t\t\t};\n\t\t}\n\t\teffectiveConfig = { ...effectiveConfig, model: resolved.modelId };\n\t\tif (resolved.provider) {\n\t\t\tresolvedProvider = resolved.provider;\n\t\t}\n\t}\n\n\tonProgress?.(`Running ${name} agent...`);\n\treturn spawnSubagent(effectiveConfig, task, cwd, signal, onProgress, resolvedProvider, sessionDir);\n}\n\nasync function executeChain(\n\tagents: Map<string, AgentTypeConfig>,\n\tchain: Array<{ agent?: string; task: string; cwd?: string; model?: string }>,\n\tdefaultCwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tparentProvider?: string,\n\tregistry?: ModelRegistry,\n\tsessionBaseDir?: string,\n): Promise<SubagentResult[]> {\n\tconst results: SubagentResult[] = [];\n\tlet previousOutput = \"\";\n\n\tfor (let i = 0; i < chain.length; i++) {\n\t\tif (signal?.aborted) break;\n\t\tconst step = chain[i];\n\t\tconst task = step.task.replace(/\\{previous\\}/g, previousOutput);\n\t\tonProgress?.(`Chain step ${i + 1}/${chain.length}`);\n\n\t\t// Validate task length after {previous} substitution (can compound across steps)\n\t\tif (task.length > MAX_TASK_LENGTH) {\n\t\t\tresults.push({\n\t\t\t\tagent: step.agent || DEFAULT_AGENT,\n\t\t\t\ttask: `${task.slice(0, 200)}...`,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: `Task prompt too long after {previous} substitution (${task.length} chars, max ${MAX_TASK_LENGTH}). Shorten the prompt or summarize previous output.`,\n\t\t\t});\n\t\t\tbreak;\n\t\t}\n\n\t\tconst cwdResult = clampCwd(defaultCwd, step.cwd);\n\t\tif (!cwdResult.ok) {\n\t\t\tresults.push({\n\t\t\t\tagent: step.agent || DEFAULT_AGENT,\n\t\t\t\ttask,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: cwdResult.error,\n\t\t\t});\n\t\t\tbreak;\n\t\t}\n\n\t\t// Each chain step gets its own session subdirectory\n\t\tconst stepSessionDir = sessionBaseDir ? join(sessionBaseDir, `step-${i + 1}`) : undefined;\n\t\tconst result = await executeSingle(\n\t\t\tagents,\n\t\t\tstep.agent,\n\t\t\ttask,\n\t\t\tcwdResult.cwd,\n\t\t\tsignal,\n\t\t\tonProgress,\n\t\t\tstep.model,\n\t\t\tparentProvider,\n\t\t\tregistry,\n\t\t\tstepSessionDir,\n\t\t);\n\t\tresults.push(result);\n\n\t\tif (result.exitCode !== 0) {\n\t\t\tbreak; // stop chain on error\n\t\t}\n\t\tpreviousOutput = result.output;\n\t}\n\n\treturn results;\n}\n\n// ---------------------------------------------------------------------------\n// Background execution\n// ---------------------------------------------------------------------------\n\nfunction generateAgentId(): string {\n\treturn randomBytes(6).toString(\"hex\");\n}\n\n// ---------------------------------------------------------------------------\n// Background agent registry — queryable by TUI / Telegram frontends\n// ---------------------------------------------------------------------------\n\nexport interface BackgroundAgentInfo {\n\tagentId: string;\n\tagentType: string;\n\ttaskSummary: string;\n\tstartedAt: number;\n\tstatus: \"running\" | \"completed\" | \"failed\";\n}\n\nconst backgroundAgentRegistry = new Map<string, BackgroundAgentInfo>();\nconst backgroundAbortControllers = new Map<string, AbortController>();\n\n/** Get a snapshot of all tracked background agents (running and recently completed). Returns readonly clones. */\nexport function getBackgroundAgents(): readonly Readonly<BackgroundAgentInfo>[] {\n\treturn [...backgroundAgentRegistry.values()].map((a) => ({ ...a }));\n}\n\n/** Get only currently running background agents. Returns readonly clones. */\nexport function getRunningBackgroundAgents(): readonly Readonly<BackgroundAgentInfo>[] {\n\treturn [...backgroundAgentRegistry.values()].filter((a) => a.status === \"running\").map((a) => ({ ...a }));\n}\n\n/** Abort all running background agents. */\nexport function abortBackgroundAgents(): void {\n\tfor (const [id, controller] of backgroundAbortControllers) {\n\t\tcontroller.abort();\n\t\tconst entry = backgroundAgentRegistry.get(id);\n\t\tif (entry && entry.status === \"running\") {\n\t\t\tentry.status = \"failed\";\n\t\t}\n\t}\n\tbackgroundAbortControllers.clear();\n}\n\n/** Remove completed/failed entries older than the given age (ms). Default: 5 minutes. */\nexport function pruneBackgroundAgents(maxAgeMs = 5 * 60 * 1000): void {\n\tconst now = Date.now();\n\tfor (const [id, info] of backgroundAgentRegistry) {\n\t\tif (info.status !== \"running\" && now - info.startedAt > maxAgeMs) {\n\t\t\tbackgroundAgentRegistry.delete(id);\n\t\t\tbackgroundAbortControllers.delete(id);\n\t\t}\n\t}\n}\n\nexport interface SubagentToolOptions {\n\t/** Called when a background subagent starts. Used by TUI to show status indicators. */\n\tonBackgroundStart?: (agentId: string, agentType: string, taskSummary: string) => void;\n\t/** Called when a background subagent completes with its result. `cancelled` is true if the user aborted it. */\n\tonBackgroundComplete?: (agentId: string, result: SubagentResult, cancelled: boolean) => void;\n\t/** Parent session's current provider (e.g. \"anthropic\"). Called at each invocation to get the live value after mid-session model switches. */\n\tparentProvider?: () => string | undefined;\n\t/** Model registry for validating model names before spawning child processes. */\n\tmodelRegistry?: ModelRegistry;\n}\n\n// ---------------------------------------------------------------------------\n// Tool schema and definition\n// ---------------------------------------------------------------------------\n\nconst taskItemSchema = Type.Object({\n\tagent: Type.Optional(Type.String({ description: \"Agent type name (default: 'Explore')\" })),\n\ttask: Type.String({ description: \"The task prompt for this subagent\" }),\n\tcwd: Type.Optional(Type.String({ description: \"Working directory (defaults to parent's cwd)\" })),\n\tmodel: Type.Optional(\n\t\tType.String({\n\t\t\tdescription:\n\t\t\t\t\"Model override for this task. Takes precedence over agent definition model. Note: a single-string override discards the agent's fallback list.\",\n\t\t}),\n\t),\n});\n\nconst subagentSchema = Type.Object({\n\tagent: Type.Optional(Type.String({ description: \"Agent type name (default: 'Explore')\" })),\n\ttask: Type.Optional(Type.String({ description: \"Task prompt (single mode)\", minLength: 1 })),\n\tmodel: Type.Optional(\n\t\tType.String({\n\t\t\tdescription:\n\t\t\t\t\"Model override. Takes precedence over agent definition model. Note: a single-string override discards the agent's fallback list. For parallel/chain, set per-task instead.\",\n\t\t}),\n\t),\n\ttasks: Type.Optional(\n\t\tType.Array(taskItemSchema, {\n\t\t\tdescription: \"Array of tasks to run in parallel (max 8)\",\n\t\t\tminItems: 1,\n\t\t\tmaxItems: MAX_PARALLEL_TASKS,\n\t\t}),\n\t),\n\tchain: Type.Optional(\n\t\tType.Array(taskItemSchema, {\n\t\t\tdescription: \"Sequential pipeline — each step can use {previous} for prior output\",\n\t\t\tminItems: 1,\n\t\t}),\n\t),\n\t// background parameter removed — all subagents run in background mode.\n\t// Kept in schema for backward compatibility (silently ignored if passed).\n\tbackground: Type.Optional(\n\t\tType.Boolean({ description: \"Deprecated — all subagents run in background mode. This parameter is ignored.\" }),\n\t),\n});\n\nexport type SubagentToolInput = Static<typeof subagentSchema>;\n\nexport interface SubagentToolDetails {\n\ttruncation?: TruncationResult;\n\tmode: \"single\" | \"parallel\" | \"chain\";\n\tagentCount: number;\n}\n\nfunction formatSubagentCall(\n\targs: SubagentToolInput | undefined,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.js\").theme,\n\targsComplete = true,\n): string {\n\tconst invalidArg = invalidArgText(theme);\n\n\tif (args?.tasks) {\n\t\treturn (\n\t\t\ttheme.fg(\"toolTitle\", theme.bold(\"subagent\")) +\n\t\t\t\" \" +\n\t\t\ttheme.fg(\"accent\", `parallel (${args.tasks.length} tasks)`)\n\t\t);\n\t}\n\tif (args?.chain) {\n\t\treturn `${theme.fg(\"toolTitle\", theme.bold(\"subagent\"))} ${theme.fg(\"accent\", `chain (${args.chain.length} steps)`)}`;\n\t}\n\n\tconst agent = str(args?.agent) || DEFAULT_AGENT;\n\tconst model = str(args?.model);\n\tconst task = str(args?.task);\n\tconst taskPreview = task ? (task.length > 60 ? `${task.slice(0, 57)}...` : task) : null;\n\tconst modelSuffix = model ? ` ${theme.fg(\"muted\", `(${model})`)}` : \"\";\n\treturn (\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"subagent\")) +\n\t\t\" \" +\n\t\ttheme.fg(\"accent\", agent) +\n\t\tmodelSuffix +\n\t\t\" \" +\n\t\t(taskPreview === null\n\t\t\t? argsComplete\n\t\t\t\t? invalidArg\n\t\t\t\t: theme.fg(\"muted\", \"…\")\n\t\t\t: theme.fg(\"toolOutput\", `\"${taskPreview}\"`))\n\t);\n}\n\nfunction formatSubagentResult(\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string }>;\n\t\tdetails?: SubagentToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.js\").theme,\n\tshowImages: boolean,\n): string {\n\tconst output = getTextOutput(result, showImages).trim();\n\tlet text = \"\";\n\tif (output) {\n\t\tconst lines = output.split(\"\\n\");\n\t\tconst maxLines = options.expanded ? lines.length : 25;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n${displayLines.map((line) => theme.fg(\"toolOutput\", line)).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines,`)} ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t}\n\t}\n\tconst truncation = result.details?.truncation;\n\tif (truncation?.truncated) {\n\t\ttext += `\\n${theme.fg(\"warning\", `[Truncated: ${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit]`)}`;\n\t}\n\treturn text;\n}\n\nfunction formatSingleResult(result: SubagentResult): string {\n\tlet text = `## Agent: ${result.agent}${result.model ? ` (model: ${result.model})` : \"\"}\\n`;\n\tif (result.exitCode !== 0) {\n\t\ttext += `**Error** (exit ${result.exitCode}): ${result.errorMessage || \"Unknown error\"}\\n`;\n\t\tif (result.stderr) {\n\t\t\ttext += `\\nStderr:\\n${result.stderr}\\n`;\n\t\t}\n\t}\n\tif (result.output) {\n\t\ttext += `\\n${result.output}`;\n\t} else if (result.exitCode === 0) {\n\t\ttext += \"\\n(No output)\";\n\t}\n\tif (result.sessionFile) {\n\t\ttext += `\\n\\nSession log: ${result.sessionFile}`;\n\t}\n\treturn text;\n}\n\nexport function createSubagentToolDefinition(\n\tcwd: string,\n\toptions?: SubagentToolOptions,\n): ToolDefinition<typeof subagentSchema, SubagentToolDetails | undefined> {\n\tconst onBackgroundStart = options?.onBackgroundStart;\n\tconst onBackgroundComplete = options?.onBackgroundComplete;\n\tconst getParentProvider = options?.parentProvider ?? (() => undefined);\n\tconst modelRegistry = options?.modelRegistry;\n\n\t// Discover agents at definition time to build the prompt guidelines.\n\t// This is cheap (reads .md files) and the same call happens on every execute().\n\tconst knownAgents = discoverAgentTypes(cwd);\n\tconst agentListParts: string[] = [];\n\tfor (const [name, config] of knownAgents) {\n\t\tconst defaultTag = name === DEFAULT_AGENT ? \" (default)\" : \"\";\n\t\tconst desc = config.description || name;\n\t\tagentListParts.push(`'${name}'${defaultTag} — ${desc}`);\n\t}\n\tconst builtInAgentsLine = `Built-in agents: ${agentListParts.join(\"; \")}`;\n\n\treturn {\n\t\tname: \"subagent\",\n\t\tlabel: \"subagent\",\n\t\tdescription:\n\t\t\t\"Delegate tasks to independent subagents (Explore for codebase research, Sandbox for isolated /tmp-only analysis). \" +\n\t\t\t\"Supports single task, parallel (up to 8, max 4 concurrent), \" +\n\t\t\t\"and chain (sequential pipeline with {previous} substitution) modes. \" +\n\t\t\t\"All subagents run in background — returns immediately, notifies on completion.\",\n\t\tpromptSnippet: \"Delegate tasks to independent subagents\",\n\t\tpromptGuidelines: [\n\t\t\t\"Use `subagent` to delegate focused, independent tasks to child agents\",\n\t\t\t\"Available agent types can be discovered from ~/.dreb/agents/ and .dreb/agents/ markdown files\",\n\t\t\tbuiltInAgentsLine,\n\t\t\t\"Use parallel mode for independent tasks that can run concurrently\",\n\t\t\t\"Use chain mode when each step depends on the previous step's output (reference with {previous})\",\n\t\t\t\"All subagents run in background — the tool returns immediately and you are notified when each agent completes.\",\n\t\t\t\"Subagents have their own context window — provide enough context in the task prompt\",\n\t\t\t\"Each agent notifies independently when done — completion messages include a list of any still-running agents. If you need their results before proceeding, stop generating — do not output anything, do not launch filler work. Your turn ends, and when an agent completes, its result arrives as a new message that resumes your turn automatically.\",\n\t\t\t\"Agent definitions specify a `model` field with a provider fallback list (comma-separated or YAML list). The spawner tries each in order and uses the first one that resolves for the current provider. This makes agents portable across providers.\",\n\t\t\t\"Per-invocation `model` overrides take precedence but **discard the entire fallback list** — if the single override model isn't available on the current provider, the agent fails. Only override when you have a specific reason (e.g. escalating to a stronger tier for a complex task).\",\n\t\t\t\"**Model routing** — agent definitions already specify the right tier for their role. Most subagent tasks (exploration, file discovery, grep, navigation, summarization) are handled well by the defaults. Do not override the model unless the task genuinely requires a different capability tier than what the agent definition provides.\",\n\t\t],\n\t\tparameters: subagentSchema,\n\n\t\tasync execute(_toolCallId, params: SubagentToolInput, _signal, _onUpdate) {\n\t\t\tconst agents = discoverAgentTypes(cwd);\n\n\t\t\t// Determine mode\n\t\t\tconst modeCount = (params.task ? 1 : 0) + (params.tasks ? 1 : 0) + (params.chain ? 1 : 0);\n\t\t\tif (modeCount === 0) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{ type: \"text\", text: \"Error: provide one of `task` (single), `tasks` (parallel), or `chain`.\" },\n\t\t\t\t\t],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\t\t\tif (modeCount > 1) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: \"Error: modes are mutually exclusive — provide only one of `task`, `tasks`, or `chain`.\",\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// All subagents run in background mode — return immediately, notify on completion\n\t\t\t{\n\t\t\t\tif (!onBackgroundComplete) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: \"Subagent execution requires background support, which is not available in this session.\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t/**\n\t\t\t\t * Shared lifecycle for all background launches: generates agent ID,\n\t\t\t\t * sets up registry/abort/notification, gates on the concurrency\n\t\t\t\t * semaphore, and handles errors. The caller provides the actual\n\t\t\t\t * work via `runFn(signal)` which must return a SubagentResult.\n\t\t\t\t */\n\t\t\t\tconst launchBackgroundLifecycle = (\n\t\t\t\t\tagentName: string,\n\t\t\t\t\ttaskSummary: string,\n\t\t\t\t\trunFn: (signal: AbortSignal) => Promise<SubagentResult>,\n\t\t\t\t): string => {\n\t\t\t\t\tconst agentId = generateAgentId();\n\t\t\t\t\tconst bgAbort = new AbortController();\n\t\t\t\t\tbackgroundAgentRegistry.set(agentId, {\n\t\t\t\t\t\tagentId,\n\t\t\t\t\t\tagentType: agentName,\n\t\t\t\t\t\ttaskSummary,\n\t\t\t\t\t\tstartedAt: Date.now(),\n\t\t\t\t\t\tstatus: \"running\",\n\t\t\t\t\t});\n\t\t\t\t\tbackgroundAbortControllers.set(agentId, bgAbort);\n\t\t\t\t\tonBackgroundStart?.(agentId, agentName, taskSummary);\n\n\t\t\t\t\tconst bgSignal = bgAbort.signal;\n\n\t\t\t\t\tconst safeNotify = (result: SubagentResult) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tonBackgroundComplete(agentId, result, bgSignal.aborted);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t`[subagent] onBackgroundComplete threw for agent ${agentId}: ${err instanceof Error ? err.message : String(err)}. Background result lost.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tconst run = async () => {\n\t\t\t\t\t\tawait bgAcquire();\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await runFn(bgSignal);\n\t\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\t\tif (entry && !bgSignal.aborted) entry.status = result.exitCode === 0 ? \"completed\" : \"failed\";\n\t\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\t\tsafeNotify(result);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\t\tif (entry && !bgSignal.aborted) entry.status = \"failed\";\n\t\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\t\tsafeNotify({\n\t\t\t\t\t\t\t\tagent: agentName,\n\t\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\t\texitCode: 1,\n\t\t\t\t\t\t\t\toutput: \"\",\n\t\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\t\terrorMessage: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\tbgRelease();\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t\trun().catch((err) => {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[subagent] Unhandled background error (${agentId}): ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\tif (entry && entry.status === \"running\") entry.status = \"failed\";\n\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tonBackgroundComplete(\n\t\t\t\t\t\t\t\tagentId,\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tagent: agentName,\n\t\t\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\t\t\texitCode: 1,\n\t\t\t\t\t\t\t\t\toutput: \"\",\n\t\t\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\t\t\terrorMessage: `Internal error: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tbgSignal.aborted,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} catch (notifyErr) {\n\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t`[subagent] CRITICAL: Last-resort notification failed for ${agentId}: ${notifyErr instanceof Error ? notifyErr.message : String(notifyErr)}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\treturn agentId;\n\t\t\t\t};\n\n\t\t\t\t// Helper to launch a single background task\n\t\t\t\tconst subagentSessionsBase = getSubagentSessionsDir();\n\t\t\t\tconst launchBackgroundTask = (\n\t\t\t\t\tagentName: string,\n\t\t\t\t\ttask: string,\n\t\t\t\t\ttaskLabel: string,\n\t\t\t\t\ttaskCwd?: string,\n\t\t\t\t\tmodelOverride?: string,\n\t\t\t\t) => {\n\t\t\t\t\tconst resolvedCwd = taskCwd ?? cwd;\n\t\t\t\t\t// Each background agent gets its own session subdirectory\n\t\t\t\t\tconst sessionId = generateAgentId();\n\t\t\t\t\tconst sessionDir = join(subagentSessionsBase, sessionId);\n\t\t\t\t\treturn launchBackgroundLifecycle(agentName, taskLabel, (signal) =>\n\t\t\t\t\t\texecuteSingle(\n\t\t\t\t\t\t\tagents,\n\t\t\t\t\t\t\tagentName === DEFAULT_AGENT ? undefined : agentName,\n\t\t\t\t\t\t\ttask,\n\t\t\t\t\t\t\tresolvedCwd,\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tmodelOverride,\n\t\t\t\t\t\t\tgetParentProvider(),\n\t\t\t\t\t\t\tmodelRegistry,\n\t\t\t\t\t\t\tsessionDir,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t};\n\n\t\t\t\tif (params.task) {\n\t\t\t\t\t// Single background task\n\t\t\t\t\tconst agentName = params.agent || DEFAULT_AGENT;\n\t\t\t\t\tconst agentId = launchBackgroundTask(\n\t\t\t\t\t\tagentName,\n\t\t\t\t\t\tparams.task,\n\t\t\t\t\t\t`${agentName} task`,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\tparams.model,\n\t\t\t\t\t);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: `Background agent ${agentId} started (${agentName}). You will be notified when it completes.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"single\", agentCount: 1 } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: true,\n\t\t\t\t\t};\n\t\t\t\t} else if (params.tasks) {\n\t\t\t\t\t// Parallel background tasks — each gets its own agent ID and notifies independently\n\t\t\t\t\tconst launched: Array<{ id: string; taskText: string }> = [];\n\t\t\t\t\tconst skipped: Array<{ taskText: string; error: string }> = [];\n\t\t\t\t\tfor (let i = 0; i < params.tasks.length; i++) {\n\t\t\t\t\t\tconst item = params.tasks[i];\n\t\t\t\t\t\tconst agentName = item.agent || DEFAULT_AGENT;\n\t\t\t\t\t\tconst cwdResult = clampCwd(cwd, item.cwd);\n\t\t\t\t\t\tif (!cwdResult.ok) {\n\t\t\t\t\t\t\tskipped.push({ taskText: item.task, error: cwdResult.error });\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst agentId = launchBackgroundTask(\n\t\t\t\t\t\t\tagentName,\n\t\t\t\t\t\t\titem.task,\n\t\t\t\t\t\t\t`${agentName} task ${i + 1}/${params.tasks.length}`,\n\t\t\t\t\t\t\tcwdResult.cwd,\n\t\t\t\t\t\t\titem.model,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tlaunched.push({ id: agentId, taskText: item.task });\n\t\t\t\t\t}\n\t\t\t\t\tconst listing = launched.map(({ id, taskText }) => ` ${id}: ${taskText.slice(0, 80)}`).join(\"\\n\");\n\t\t\t\t\tconst skippedListing = skipped\n\t\t\t\t\t\t.map(({ taskText, error }) => ` SKIPPED: ${taskText.slice(0, 60)} — ${error}`)\n\t\t\t\t\t\t.join(\"\\n\");\n\t\t\t\t\tconst parts = [`${launched.length} background agents started:\\n${listing}`];\n\t\t\t\t\tif (skipped.length > 0) {\n\t\t\t\t\t\tparts.push(`\\n${skipped.length} task(s) failed to launch:\\n${skippedListing}`);\n\t\t\t\t\t}\n\t\t\t\t\tif (launched.length > 0) {\n\t\t\t\t\t\tparts.push(\"\\nEach will notify independently when complete.\");\n\t\t\t\t\t} else {\n\t\t\t\t\t\tparts.push(\"\\nNo agents were launched.\");\n\t\t\t\t\t}\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: parts.join(\"\\n\"),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"parallel\", agentCount: launched.length } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: launched.length > 0,\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\t// Chain mode — sequential, stays as one agent since steps depend on each other\n\t\t\t\t\tconst agentName = params.chain![0].agent || DEFAULT_AGENT;\n\t\t\t\t\tconst taskSummary = `${params.chain!.length}-step chain`;\n\t\t\t\t\tconst chainSteps = params.chain!;\n\n\t\t\t\t\tconst chainSessionDir = join(subagentSessionsBase, `chain-${generateAgentId()}`);\n\t\t\t\t\tconst agentId = launchBackgroundLifecycle(agentName, taskSummary, async (signal) => {\n\t\t\t\t\t\tconst results = await executeChain(\n\t\t\t\t\t\t\tagents,\n\t\t\t\t\t\t\tchainSteps,\n\t\t\t\t\t\t\tcwd,\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tgetParentProvider(),\n\t\t\t\t\t\t\tmodelRegistry,\n\t\t\t\t\t\t\tchainSessionDir,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst resultText = results\n\t\t\t\t\t\t\t.map((r, i) => `### Step ${i + 1}\\n${formatSingleResult(r)}`)\n\t\t\t\t\t\t\t.join(\"\\n\\n---\\n\\n\");\n\t\t\t\t\t\tconst failed = results.filter((r) => r.exitCode !== 0);\n\t\t\t\t\t\t// Per-step session logs are already embedded in resultText via formatSingleResult\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tagent: \"chain\",\n\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\texitCode: failed.length > 0 ? 1 : 0,\n\t\t\t\t\t\t\toutput: resultText,\n\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\terrorMessage:\n\t\t\t\t\t\t\t\tfailed.length > 0\n\t\t\t\t\t\t\t\t\t? `Chain stopped at step ${results.length} of ${chainSteps.length}: ${results[results.length - 1]?.errorMessage}`\n\t\t\t\t\t\t\t\t\t: null,\n\t\t\t\t\t\t};\n\t\t\t\t\t});\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: `Background chain ${agentId} started (${taskSummary}). You will be notified when it completes.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"chain\", agentCount: chainSteps.length } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: true,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\trenderCall(args, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatSubagentCall(args, theme, context.argsComplete));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatSubagentResult(result as any, options, theme, context.showImages));\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createSubagentTool(cwd: string, options?: SubagentToolOptions): AgentTool<typeof subagentSchema> {\n\treturn wrapToolDefinition(createSubagentToolDefinition(cwd, options));\n}\n\nexport const subagentToolDefinition = createSubagentToolDefinition(process.cwd());\nexport const subagentTool = createSubagentTool(process.cwd());\n"]}
1
+ {"version":3,"file":"subagent.js","sourceRoot":"","sources":["../../../src/core/tools/subagent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAe,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AACzF,OAAO,EAAE,OAAO,EAAE,MAAM,wDAAwD,CAAC;AACjF,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AAGjE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAyB,MAAM,eAAe,CAAC;AAerF,MAAM,aAAa,GAAG,SAAS,CAAC;AAEhC,MAAM,UAAU,qBAAqB,CACpC,OAAe,EACwD;IACvE,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACp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AC;QAAA,CACD;QAED,UAAU,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE;YAChC,MAAM,IAAI,GAAI,OAAO,CAAC,aAAkC,IAAI,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;YAC/E,IAAI,CAAC,OAAO,CAAC,kBAAkB,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC;YACpE,OAAO,IAAI,CAAC;QAAA,CACZ;QACD,YAAY,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE;YAC7C,MAAM,IAAI,GAAI,OAAO,CAAC,aAAkC,IAAI,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;YAC/E,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC,MAAa,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;YACtF,OAAO,IAAI,CAAC;QAAA,CACZ;KACD,CAAC;AAAA,CACF;AAED,MAAM,UAAU,kBAAkB,CAAC,GAAW,EAAE,OAA6B,EAAoC;IAChH,OAAO,kBAAkB,CAAC,4BAA4B,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AAAA,CACtE;AAED,MAAM,CAAC,MAAM,sBAAsB,GAAG,4BAA4B,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;AAClF,MAAM,CAAC,MAAM,YAAY,GAAG,kBAAkB,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC","sourcesContent":["import { type ChildProcess, spawn } from \"node:child_process\";\nimport { randomBytes } from \"node:crypto\";\nimport { existsSync, readdirSync, readFileSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, resolve } from \"node:path\";\nimport type { AgentTool } from \"@dreb/agent-core\";\nimport { Text } from \"@dreb/tui\";\nimport { type Static, Type } from \"@sinclair/typebox\";\nimport { CONFIG_DIR_NAME, getPackageDir, getSubagentSessionsDir } from \"../../config.js\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.js\";\nimport { attachJsonlLineReader } from \"../../modes/rpc/jsonl.js\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.js\";\nimport type { ModelRegistry } from \"../model-registry.js\";\nimport { resolveCliModel } from \"../model-resolver.js\";\nimport { getTextOutput, invalidArgText, str } from \"./render-utils.js\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.js\";\nimport { DEFAULT_MAX_BYTES, formatSize, type TruncationResult } from \"./truncate.js\";\n\n// ---------------------------------------------------------------------------\n// Agent type system\n// ---------------------------------------------------------------------------\n\ninterface AgentTypeConfig {\n\tname: string;\n\tdescription: string;\n\ttools?: string;\n\t/** Single model ID or ordered fallback list. First resolvable model wins. */\n\tmodel?: string | string[];\n\tsystemPrompt: string;\n}\n\nconst DEFAULT_AGENT = \"Explore\";\n\nexport function parseAgentFrontmatter(\n\tcontent: string,\n): { ok: true; config: AgentTypeConfig } | { ok: false; error: string } {\n\tconst fmMatch = content.match(/^---\\n([\\s\\S]*?)\\n---\\n?([\\s\\S]*)$/);\n\tif (!fmMatch) return { ok: false, error: \"missing --- frontmatter delimiters\" };\n\n\tconst frontmatter = fmMatch[1];\n\tconst body = fmMatch[2].trim();\n\n\tconst get = (key: string): string | undefined => {\n\t\tconst match = frontmatter.match(new RegExp(`^${key}:\\\\s*(.+)$`, \"m\"));\n\t\treturn match?.[1].trim();\n\t};\n\n\t/** Parse `model` field — supports single string, comma-separated, or YAML list syntax. */\n\tconst getModel = (): string | string[] | undefined => {\n\t\t// First check for YAML list syntax (indented lines starting with \"- \")\n\t\tconst listMatch = frontmatter.match(/^model:\\s*\\n((?:\\s+-\\s+.+\\n?)+)/m);\n\t\tif (listMatch) {\n\t\t\tconst items = listMatch[1]\n\t\t\t\t.split(\"\\n\")\n\t\t\t\t.map((line) => line.replace(/^\\s+-\\s+/, \"\").trim())\n\t\t\t\t.filter(Boolean);\n\t\t\treturn items.length > 1 ? items : items[0];\n\t\t}\n\t\t// Inline value — check for comma-separated list\n\t\tconst value = get(\"model\");\n\t\tif (!value) return undefined;\n\t\tif (value.includes(\",\")) {\n\t\t\tconst items = value\n\t\t\t\t.split(\",\")\n\t\t\t\t.map((s) => s.trim())\n\t\t\t\t.filter(Boolean);\n\t\t\treturn items.length > 1 ? items : items[0];\n\t\t}\n\t\treturn value;\n\t};\n\n\tconst name = get(\"name\");\n\tif (!name) return { ok: false, error: \"missing required 'name' field in frontmatter\" };\n\n\treturn {\n\t\tok: true,\n\t\tconfig: {\n\t\t\tname,\n\t\t\tdescription: get(\"description\") || \"\",\n\t\t\ttools: get(\"tools\"),\n\t\t\tmodel: getModel(),\n\t\t\tsystemPrompt: body,\n\t\t},\n\t};\n}\n\nfunction discoverAgentTypes(cwd: string): Map<string, AgentTypeConfig> {\n\tconst agents = new Map<string, AgentTypeConfig>();\n\n\t// Package-bundled agents (shipped with dreb — the canonical source of truth for built-in agents)\n\tconst packageAgentsDir = join(getPackageDir(), \"agents\");\n\tloadAgentsFromDir(packageAgentsDir, agents);\n\n\t// User-level agents (~/.dreb/agents/*.md)\n\tconst userDir = join(homedir(), CONFIG_DIR_NAME, \"agents\");\n\tloadAgentsFromDir(userDir, agents);\n\n\t// Project-level agents (.dreb/agents/*.md)\n\t// TODO: Security gate — prompt user for confirmation before loading agents from untrusted repos\n\tconst projectDir = join(cwd, \".dreb\", \"agents\");\n\tloadAgentsFromDir(projectDir, agents);\n\n\treturn agents;\n}\n\nfunction loadAgentsFromDir(dir: string, agents: Map<string, AgentTypeConfig>): void {\n\tif (!existsSync(dir)) return;\n\ttry {\n\t\tfor (const file of readdirSync(dir)) {\n\t\t\tif (!file.endsWith(\".md\")) continue;\n\t\t\ttry {\n\t\t\t\tconst content = readFileSync(join(dir, file), \"utf-8\");\n\t\t\t\tconst parsed = parseAgentFrontmatter(content);\n\t\t\t\tif (!parsed.ok) {\n\t\t\t\t\tconsole.error(`[subagent] Skipping agent file ${join(dir, file)}: ${parsed.error}`);\n\t\t\t\t} else {\n\t\t\t\t\tagents.set(parsed.config.name, parsed.config);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[subagent] Could not read agent file ${join(dir, file)}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t} catch (err) {\n\t\tif ((err as NodeJS.ErrnoException).code !== \"ENOENT\") {\n\t\t\tconsole.error(\n\t\t\t\t`[subagent] Could not read agents directory ${dir}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t);\n\t\t}\n\t}\n}\n\n// ---------------------------------------------------------------------------\n// Subagent process spawning\n// ---------------------------------------------------------------------------\n\nexport interface SubagentResult {\n\tagent: string;\n\ttask: string;\n\tmodel?: string;\n\texitCode: number;\n\toutput: string;\n\tstderr: string;\n\terrorMessage: string | null;\n\t/** Path to the persisted session JSONL file, if available */\n\tsessionFile?: string;\n}\n\n// Capture at module load before process.title overwrites argv memory on Linux.\n// After process.title = \"dreb\" (in cli.ts), the original argv area is overwritten\n// and process.argv[1] may return corrupted or truncated data.\nconst DREB_SCRIPT = process.argv[1] || \"dreb\";\nconst NODE_EXEC = process.execPath;\n\n// TODO: Support PATH-based binary discovery.\n// Currently returns the captured argv[1].\nfunction findDrebBinary(): string {\n\treturn DREB_SCRIPT;\n}\n\nasync function spawnSubagent(\n\tagentConfig: AgentTypeConfig,\n\ttask: string,\n\tcwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tparentProvider?: string,\n\tsessionDir?: string,\n): Promise<SubagentResult> {\n\tconst drebBin = findDrebBinary();\n\tconsole.error(`[subagent] spawn: agent=${agentConfig.name} cwd=${cwd}`);\n\n\t// Validate cwd exists — spawn() throws a misleading ENOENT blaming the\n\t// binary when the cwd is invalid, making the real cause hard to diagnose\n\tif (!existsSync(cwd)) {\n\t\treturn {\n\t\t\tagent: agentConfig.name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Working directory does not exist: ${cwd}`,\n\t\t};\n\t}\n\n\tconst args: string[] = [\"--mode\", \"json\", \"--ui\", \"agent\"];\n\tif (sessionDir) {\n\t\targs.push(\"--session-dir\", sessionDir);\n\t} else {\n\t\targs.push(\"--no-session\");\n\t}\n\t// By spawn time, model should be a resolved single string (fallback resolution\n\t// happens in executeSingle). Handle string[] defensively by taking the first entry.\n\tconst modelStr = Array.isArray(agentConfig.model) ? agentConfig.model[0] : agentConfig.model;\n\tif (modelStr) {\n\t\targs.push(\"--model\", modelStr);\n\t\t// When the model string doesn't already specify a provider (no \"/\"),\n\t\t// inherit the parent's provider to prevent fuzzy matching from picking\n\t\t// an unauthenticated provider (e.g. Bedrock instead of Anthropic).\n\t\tif (parentProvider && !modelStr.includes(\"/\")) {\n\t\t\targs.push(\"--provider\", parentProvider);\n\t\t}\n\t}\n\tif (agentConfig.tools) {\n\t\targs.push(\"--tools\", agentConfig.tools);\n\t}\n\tif (agentConfig.systemPrompt) {\n\t\targs.push(\"--append-system-prompt\", agentConfig.systemPrompt);\n\t}\n\targs.push(\"-p\", task);\n\n\t// Early abort check — if the signal is already aborted (e.g. queued task whose\n\t// AbortController was aborted while waiting on bgAcquire), bail out before\n\t// spawning a child process that can never be killed. addEventListener(\"abort\")\n\t// on an already-aborted signal does NOT fire the callback in Node.js.\n\tif (signal?.aborted) {\n\t\treturn {\n\t\t\tagent: agentConfig.name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: \"Aborted before spawn\",\n\t\t};\n\t}\n\n\treturn new Promise<SubagentResult>((resolvePromise, rejectPromise) => {\n\t\tlet proc: ChildProcess;\n\t\ttry {\n\t\t\tproc = spawn(NODE_EXEC, [drebBin, ...args], {\n\t\t\t\tcwd,\n\t\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t\t\tenv: { ...process.env },\n\t\t\t});\n\t\t} catch (err) {\n\t\t\trejectPromise(new Error(`Failed to spawn subagent: ${err instanceof Error ? err.message : String(err)}`));\n\t\t\treturn;\n\t\t}\n\n\t\tlet settled = false;\n\t\tlet killTimer: ReturnType<typeof setTimeout> | null = null;\n\t\tconst collectedMessages: Array<{ role: string; content: any[] }> = [];\n\t\tconst stderrChunks: string[] = [];\n\t\tlet stderrSize = 0;\n\t\tconst MAX_STDERR_BYTES = 8192;\n\t\tconst plainStdoutLines: string[] = [];\n\t\tlet lastToolName = \"\";\n\t\tlet resolvedModel: string | undefined;\n\n\t\t// Drain stderr concurrently to avoid pipe deadlock (capped to prevent OOM from verbose subagents)\n\t\tproc.stderr?.on(\"data\", (chunk: Buffer) => {\n\t\t\tif (stderrSize < MAX_STDERR_BYTES) {\n\t\t\t\tconst str = chunk.toString();\n\t\t\t\tstderrChunks.push(str);\n\t\t\t\tstderrSize += str.length;\n\t\t\t}\n\t\t});\n\t\tproc.stderr?.on(\"error\", (err) => {\n\t\t\tconsole.error(`[subagent] stderr stream error (agent=${agentConfig.name}): ${err.message}`);\n\t\t});\n\n\t\t// Parse JSONL events from stdout\n\t\tif (proc.stdout) {\n\t\t\tproc.stdout.on(\"error\", (err) => {\n\t\t\t\tconsole.error(`[subagent] stdout stream error (agent=${agentConfig.name}): ${err.message}`);\n\t\t\t});\n\t\t\tattachJsonlLineReader(proc.stdout, (line) => {\n\t\t\t\tif (!line.trim()) return;\n\t\t\t\t// Separate JSON.parse from event handling so only parse failures\n\t\t\t\t// are caught as non-JSON lines — errors in handling propagate normally\n\t\t\t\tlet event: any;\n\t\t\t\ttry {\n\t\t\t\t\tevent = JSON.parse(line);\n\t\t\t\t} catch {\n\t\t\t\t\t// Capture non-JSON lines — on failure these often contain the real error\n\t\t\t\t\t// (e.g. startup errors printed before JSONL mode begins)\n\t\t\t\t\tplainStdoutLines.push(line.trim());\n\t\t\t\t\tif (line.trim().startsWith(\"{\")) {\n\t\t\t\t\t\tconsole.error(`[subagent] Failed to parse JSONL event: ${line.slice(0, 200)}`);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (event.type === \"agent_start\" && event.model) {\n\t\t\t\t\tresolvedModel = event.model.id;\n\t\t\t\t}\n\t\t\t\tif (event.type === \"message_end\" && event.message?.role === \"assistant\") {\n\t\t\t\t\tcollectedMessages.push(event.message);\n\t\t\t\t}\n\t\t\t\tif (event.type === \"tool_execution_start\" && onProgress) {\n\t\t\t\t\tlastToolName = event.toolName || \"\";\n\t\t\t\t\tonProgress(`Using ${lastToolName}...`);\n\t\t\t\t}\n\t\t\t\tif (event.type === \"tool_execution_end\" && onProgress) {\n\t\t\t\t\tonProgress(`${lastToolName} done`);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\n\t\t// Handle abort signal (guard kill() against ESRCH race if process already exited)\n\t\tconst onAbort = () => {\n\t\t\ttry {\n\t\t\t\tproc.kill(\"SIGTERM\");\n\t\t\t} catch {\n\t\t\t\t/* process already exited */\n\t\t\t}\n\t\t\tkillTimer = setTimeout(() => {\n\t\t\t\ttry {\n\t\t\t\t\tif (!proc.killed) proc.kill(\"SIGKILL\");\n\t\t\t\t} catch {\n\t\t\t\t\t/* process already exited */\n\t\t\t\t}\n\t\t\t}, 5000);\n\t\t};\n\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\n\t\tproc.on(\"error\", (err) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (killTimer) clearTimeout(killTimer);\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\trejectPromise(new Error(`Subagent process error: ${err.message}`));\n\t\t});\n\n\t\tproc.on(\"close\", (code) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (killTimer) clearTimeout(killTimer);\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\tconst exitCode = code ?? 1;\n\t\t\tconst stderr = stderrChunks.join(\"\");\n\t\t\tconsole.error(\n\t\t\t\t`[subagent] close: agent=${agentConfig.name} exit=${exitCode} messages=${collectedMessages.length}${exitCode !== 0 ? ` stderr=${stderr.slice(0, 200)} stdout=${plainStdoutLines.join(\"|\").slice(0, 200)}` : \"\"}`,\n\t\t\t);\n\n\t\t\t// Extract final text output from collected assistant messages\n\t\t\tconst outputParts: string[] = [];\n\t\t\tfor (const msg of collectedMessages) {\n\t\t\t\tif (Array.isArray(msg.content)) {\n\t\t\t\t\tfor (const part of msg.content) {\n\t\t\t\t\t\tif (part.type === \"text\" && part.text) {\n\t\t\t\t\t\t\toutputParts.push(part.text);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst output = outputParts.join(\"\\n\\n\");\n\n\t\t\t// Build error message from best available source: stderr, plain stdout lines, or generic\n\t\t\tlet errorMessage: string | null = null;\n\t\t\tif (exitCode !== 0) {\n\t\t\t\tconst stderrTrimmed = stderr.trim();\n\t\t\t\tconst plainOutput = plainStdoutLines.join(\"\\n\").trim();\n\t\t\t\terrorMessage =\n\t\t\t\t\tstderrTrimmed.slice(0, 500) || plainOutput.slice(0, 500) || `Subagent exited with code ${exitCode}`;\n\t\t\t}\n\n\t\t\t// Discover the session file written by the child process\n\t\t\tconst sessionFile = sessionDir ? discoverSessionFile(sessionDir, agentConfig.name) : undefined;\n\n\t\t\tresolvePromise({\n\t\t\t\tagent: agentConfig.name,\n\t\t\t\ttask,\n\t\t\t\tmodel:\n\t\t\t\t\tresolvedModel ??\n\t\t\t\t\t(exitCode === 0\n\t\t\t\t\t\t? Array.isArray(agentConfig.model)\n\t\t\t\t\t\t\t? agentConfig.model[0]\n\t\t\t\t\t\t\t: agentConfig.model\n\t\t\t\t\t\t: undefined),\n\t\t\t\texitCode,\n\t\t\t\toutput,\n\t\t\t\tstderr: stderr.slice(0, 2000), // cap stderr\n\t\t\t\terrorMessage,\n\t\t\t\tsessionFile,\n\t\t\t});\n\t\t});\n\t});\n}\n\n// ---------------------------------------------------------------------------\n// Session file discovery and cleanup\n// ---------------------------------------------------------------------------\n\n/**\n * Find the most recently modified .jsonl file in a session directory.\n * Returns the full path, or undefined if no session file was written\n * (e.g., subagent was killed before the first assistant message).\n */\nexport function discoverSessionFile(sessionDir: string, agentName: string): string | undefined {\n\ttry {\n\t\tif (!existsSync(sessionDir)) return undefined;\n\t\tconst files = readdirSync(sessionDir).filter((f) => f.endsWith(\".jsonl\"));\n\t\tif (files.length === 0) return undefined;\n\t\t// Pick the most recently modified file (typically there's only one per subagent dir)\n\t\tlet best: { path: string; mtime: number } | undefined;\n\t\tfor (const f of files) {\n\t\t\ttry {\n\t\t\t\tconst fullPath = join(sessionDir, f);\n\t\t\t\tconst mtime = statSync(fullPath).mtime.getTime();\n\t\t\t\tif (!best || mtime > best.mtime) {\n\t\t\t\t\tbest = { path: fullPath, mtime };\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// File disappeared or is a bad symlink — skip it, keep any valid candidate\n\t\t\t}\n\t\t}\n\t\tif (best) {\n\t\t\tconsole.error(`[subagent] session file: ${best.path} (agent=${agentName})`);\n\t\t\treturn best.path;\n\t\t}\n\t} catch (err) {\n\t\tconsole.error(\n\t\t\t`[subagent] failed to discover session file (agent=${agentName}): ${err instanceof Error ? err.message : String(err)}`,\n\t\t);\n\t}\n\treturn undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Execution modes\n// ---------------------------------------------------------------------------\n\n/**\n * Resolve a model fallback list against the registry. Tries each model in order,\n * returns the first one that resolves successfully. If all fail, returns the\n * last error. Single strings are treated as a one-element list.\n */\nexport function resolveModelWithFallbacks(\n\tmodels: string | string[],\n\tparentProvider: string | undefined,\n\tregistry: ModelRegistry | undefined,\n): { ok: true; modelId: string; provider?: string } | { ok: false; error: string } {\n\tconst modelList = Array.isArray(models) ? models : [models];\n\tlet lastError = \"\";\n\tfor (const modelStr of modelList) {\n\t\tconst result = resolveModelStringSingle(modelStr, parentProvider, registry);\n\t\tif (result.ok) return result;\n\t\tlastError = result.error;\n\t}\n\tif (modelList.length > 1) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `None of the fallback models resolved: ${modelList.join(\", \")}. Last error: ${lastError}`,\n\t\t};\n\t}\n\treturn { ok: false, error: lastError };\n}\n\nexport function resolveModelStringSingle(\n\tmodelStr: string,\n\tparentProvider: string | undefined,\n\tregistry: ModelRegistry | undefined,\n): { ok: true; modelId: string; provider?: string } | { ok: false; error: string } {\n\tif (!registry) {\n\t\treturn { ok: true, modelId: modelStr };\n\t}\n\n\t// If the model string contains \"/\" the user already specified a provider\n\tconst hasProvider = modelStr.includes(\"/\");\n\tconst resolved = resolveCliModel({\n\t\tcliProvider: hasProvider ? undefined : parentProvider,\n\t\tcliModel: modelStr,\n\t\tmodelRegistry: registry,\n\t});\n\n\tif (resolved.error) {\n\t\treturn { ok: false, error: resolved.error };\n\t}\n\tif (!resolved.model) {\n\t\treturn { ok: false, error: `Model \"${modelStr}\" not found. Use --list-models to see available models.` };\n\t}\n\n\t// resolveCliModel creates a synthetic model for any unknown ID when a\n\t// provider is specified (designed for custom/self-hosted models like Ollama).\n\t// For subagents this causes silent failures — reject synthetic fallbacks\n\t// so the next model in the fallback list is tried instead.\n\tif (resolved.isSyntheticFallback) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `Model \"${modelStr}\" not found for provider \"${resolved.model.provider}\". Use --list-models to see available models.`,\n\t\t};\n\t}\n\n\t// Verify the resolved provider has authentication configured.\n\t// resolveCliModel uses getAll() (all models, not just authenticated ones)\n\t// so a model can resolve successfully to a provider with no API key.\n\t// Reject early so the fallback list can continue to the next model.\n\tif (!registry.authStorage.hasAuth(resolved.model.provider)) {\n\t\treturn {\n\t\t\tok: false,\n\t\t\terror: `No authentication configured for provider \"${resolved.model.provider}\". Model \"${modelStr}\" cannot be used.`,\n\t\t};\n\t}\n\n\treturn { ok: true, modelId: resolved.model.id, provider: resolved.model.provider };\n}\n\nconst MAX_PARALLEL_TASKS = 8;\nconst MAX_CONCURRENCY = 4;\nconst MAX_TASK_LENGTH = 32_768; // 32 KB — prevent E2BIG from oversized argv\n\n// Semaphore for background task concurrency — shared across all background launches\nlet bgRunning = 0;\nconst bgWaiters: Array<() => void> = [];\n\nasync function bgAcquire(): Promise<void> {\n\tif (bgRunning < MAX_CONCURRENCY) {\n\t\tbgRunning++;\n\t\treturn;\n\t}\n\treturn new Promise<void>((resolve) => {\n\t\tbgWaiters.push(() => {\n\t\t\tbgRunning++;\n\t\t\tresolve();\n\t\t});\n\t});\n}\n\nfunction bgRelease(): void {\n\tbgRunning--;\n\tconst next = bgWaiters.shift();\n\tif (next) next();\n}\n\n/**\n * Resolve a per-task cwd relative to the parent cwd.\n * Rejects absolute paths and relative paths that escape the parent directory.\n * Returns a result object with ok=false and an error string on rejection, so callers can surface it to the model.\n */\nfunction clampCwd(defaultCwd: string, itemCwd?: string): { ok: true; cwd: string } | { ok: false; error: string } {\n\tif (!itemCwd) return { ok: true, cwd: defaultCwd };\n\tif (itemCwd.startsWith(\"/\")) {\n\t\treturn { ok: false, error: `Rejected absolute cwd \"${itemCwd}\" — must be relative to parent cwd` };\n\t}\n\tconst resolved = resolve(defaultCwd, itemCwd);\n\tif (resolved !== defaultCwd && !resolved.startsWith(`${defaultCwd}/`)) {\n\t\treturn { ok: false, error: `Rejected cwd \"${itemCwd}\" — resolves outside parent cwd` };\n\t}\n\treturn { ok: true, cwd: resolved };\n}\n\nasync function executeSingle(\n\tagents: Map<string, AgentTypeConfig>,\n\tagentName: string | undefined,\n\ttask: string,\n\tcwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tmodelOverride?: string,\n\tparentProvider?: string,\n\tregistry?: ModelRegistry,\n\tsessionDir?: string,\n): Promise<SubagentResult> {\n\tconst name = agentName || DEFAULT_AGENT;\n\tconst config = agents.get(name);\n\tif (!config) {\n\t\treturn {\n\t\t\tagent: name,\n\t\t\ttask,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Unknown agent type \"${name}\". Available: ${[...agents.keys()].join(\", \")}. If you expected \"${name}\" to exist, check the .md file in ~/.dreb/agents/ or .dreb/agents/ for syntax errors.`,\n\t\t};\n\t}\n\t// Validate task length for all modes (single, parallel items, chain steps)\n\tif (task.length > MAX_TASK_LENGTH) {\n\t\treturn {\n\t\t\tagent: name,\n\t\t\ttask: `${task.slice(0, 200)}...`,\n\t\t\texitCode: 1,\n\t\t\toutput: \"\",\n\t\t\tstderr: \"\",\n\t\t\terrorMessage: `Task prompt too long (${task.length} chars, max ${MAX_TASK_LENGTH}). Shorten the prompt.`,\n\t\t};\n\t}\n\t// Per-invocation model override takes precedence over agent definition model.\n\t// Override is always a single string; agent config may be a string or fallback list.\n\tconst modelSpec = modelOverride || config.model;\n\tlet effectiveConfig: AgentTypeConfig = modelOverride ? { ...config, model: modelOverride } : config;\n\tlet resolvedProvider = parentProvider;\n\n\t// Resolve and validate the model against the registry before spawning.\n\t// This catches typos and invalid model names immediately instead of failing\n\t// silently in the child process. Also passes the canonical model ID to the\n\t// child, avoiding fuzzy matching entirely.\n\tif (modelSpec) {\n\t\tconst resolved = resolveModelWithFallbacks(modelSpec, parentProvider, registry);\n\t\tif (!resolved.ok) {\n\t\t\treturn {\n\t\t\t\tagent: name,\n\t\t\t\ttask,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: resolved.error,\n\t\t\t};\n\t\t}\n\t\teffectiveConfig = { ...effectiveConfig, model: resolved.modelId };\n\t\tif (resolved.provider) {\n\t\t\tresolvedProvider = resolved.provider;\n\t\t}\n\t}\n\n\tonProgress?.(`Running ${name} agent...`);\n\treturn spawnSubagent(effectiveConfig, task, cwd, signal, onProgress, resolvedProvider, sessionDir);\n}\n\nasync function executeChain(\n\tagents: Map<string, AgentTypeConfig>,\n\tchain: Array<{ agent?: string; task: string; cwd?: string; model?: string }>,\n\tdefaultCwd: string,\n\tsignal?: AbortSignal,\n\tonProgress?: (event: string) => void,\n\tparentProvider?: string,\n\tregistry?: ModelRegistry,\n\tsessionBaseDir?: string,\n): Promise<SubagentResult[]> {\n\tconst results: SubagentResult[] = [];\n\tlet previousOutput = \"\";\n\n\tfor (let i = 0; i < chain.length; i++) {\n\t\tif (signal?.aborted) break;\n\t\tconst step = chain[i];\n\t\tconst task = step.task.replace(/\\{previous\\}/g, previousOutput);\n\t\tonProgress?.(`Chain step ${i + 1}/${chain.length}`);\n\n\t\t// Validate task length after {previous} substitution (can compound across steps)\n\t\tif (task.length > MAX_TASK_LENGTH) {\n\t\t\tresults.push({\n\t\t\t\tagent: step.agent || DEFAULT_AGENT,\n\t\t\t\ttask: `${task.slice(0, 200)}...`,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: `Task prompt too long after {previous} substitution (${task.length} chars, max ${MAX_TASK_LENGTH}). Shorten the prompt or summarize previous output.`,\n\t\t\t});\n\t\t\tbreak;\n\t\t}\n\n\t\tconst cwdResult = clampCwd(defaultCwd, step.cwd);\n\t\tif (!cwdResult.ok) {\n\t\t\tresults.push({\n\t\t\t\tagent: step.agent || DEFAULT_AGENT,\n\t\t\t\ttask,\n\t\t\t\texitCode: 1,\n\t\t\t\toutput: \"\",\n\t\t\t\tstderr: \"\",\n\t\t\t\terrorMessage: cwdResult.error,\n\t\t\t});\n\t\t\tbreak;\n\t\t}\n\n\t\t// Each chain step gets its own session subdirectory\n\t\tconst stepSessionDir = sessionBaseDir ? join(sessionBaseDir, `step-${i + 1}`) : undefined;\n\t\tconst result = await executeSingle(\n\t\t\tagents,\n\t\t\tstep.agent,\n\t\t\ttask,\n\t\t\tcwdResult.cwd,\n\t\t\tsignal,\n\t\t\tonProgress,\n\t\t\tstep.model,\n\t\t\tparentProvider,\n\t\t\tregistry,\n\t\t\tstepSessionDir,\n\t\t);\n\t\tresults.push(result);\n\n\t\tif (result.exitCode !== 0) {\n\t\t\tbreak; // stop chain on error\n\t\t}\n\t\tpreviousOutput = result.output;\n\t}\n\n\treturn results;\n}\n\n// ---------------------------------------------------------------------------\n// Background execution\n// ---------------------------------------------------------------------------\n\nfunction generateAgentId(): string {\n\treturn randomBytes(6).toString(\"hex\");\n}\n\n// ---------------------------------------------------------------------------\n// Background agent registry — queryable by TUI / Telegram frontends\n// ---------------------------------------------------------------------------\n\nexport interface BackgroundAgentInfo {\n\tagentId: string;\n\tagentType: string;\n\ttaskSummary: string;\n\tstartedAt: number;\n\tstatus: \"running\" | \"completed\" | \"failed\";\n}\n\nconst backgroundAgentRegistry = new Map<string, BackgroundAgentInfo>();\nconst backgroundAbortControllers = new Map<string, AbortController>();\n\n/** Get a snapshot of all tracked background agents (running and recently completed). Returns readonly clones. */\nexport function getBackgroundAgents(): readonly Readonly<BackgroundAgentInfo>[] {\n\treturn [...backgroundAgentRegistry.values()].map((a) => ({ ...a }));\n}\n\n/** Get only currently running background agents. Returns readonly clones. */\nexport function getRunningBackgroundAgents(): readonly Readonly<BackgroundAgentInfo>[] {\n\treturn [...backgroundAgentRegistry.values()].filter((a) => a.status === \"running\").map((a) => ({ ...a }));\n}\n\n/** Abort all running background agents. */\nexport function abortBackgroundAgents(): void {\n\tfor (const [id, controller] of backgroundAbortControllers) {\n\t\tcontroller.abort();\n\t\tconst entry = backgroundAgentRegistry.get(id);\n\t\tif (entry && entry.status === \"running\") {\n\t\t\tentry.status = \"failed\";\n\t\t}\n\t}\n\tbackgroundAbortControllers.clear();\n}\n\n/** Remove completed/failed entries older than the given age (ms). Default: 5 minutes. */\nexport function pruneBackgroundAgents(maxAgeMs = 5 * 60 * 1000): void {\n\tconst now = Date.now();\n\tfor (const [id, info] of backgroundAgentRegistry) {\n\t\tif (info.status !== \"running\" && now - info.startedAt > maxAgeMs) {\n\t\t\tbackgroundAgentRegistry.delete(id);\n\t\t\tbackgroundAbortControllers.delete(id);\n\t\t}\n\t}\n}\n\nexport interface SubagentToolOptions {\n\t/** Called when a background subagent starts. Used by TUI to show status indicators. */\n\tonBackgroundStart?: (agentId: string, agentType: string, taskSummary: string) => void;\n\t/** Called when a background subagent completes with its result. `cancelled` is true if the user aborted it. */\n\tonBackgroundComplete?: (agentId: string, result: SubagentResult, cancelled: boolean) => void;\n\t/** Parent session's current provider (e.g. \"anthropic\"). Called at each invocation to get the live value after mid-session model switches. */\n\tparentProvider?: () => string | undefined;\n\t/** Model registry for validating model names before spawning child processes. */\n\tmodelRegistry?: ModelRegistry;\n}\n\n// ---------------------------------------------------------------------------\n// Tool schema and definition\n// ---------------------------------------------------------------------------\n\nconst taskItemSchema = Type.Object({\n\tagent: Type.Optional(Type.String({ description: \"Agent type name (default: 'Explore')\" })),\n\ttask: Type.String({ description: \"The task prompt for this subagent\" }),\n\tcwd: Type.Optional(Type.String({ description: \"Working directory (defaults to parent's cwd)\" })),\n\tmodel: Type.Optional(\n\t\tType.String({\n\t\t\tdescription:\n\t\t\t\t\"Model override for this task. Takes precedence over agent definition model. Note: a single-string override discards the agent's fallback list.\",\n\t\t}),\n\t),\n});\n\nconst subagentSchema = Type.Object({\n\tagent: Type.Optional(Type.String({ description: \"Agent type name (default: 'Explore')\" })),\n\ttask: Type.Optional(Type.String({ description: \"Task prompt (single mode)\", minLength: 1 })),\n\tmodel: Type.Optional(\n\t\tType.String({\n\t\t\tdescription:\n\t\t\t\t\"Model override. Takes precedence over agent definition model. Note: a single-string override discards the agent's fallback list. For parallel/chain, set per-task instead.\",\n\t\t}),\n\t),\n\ttasks: Type.Optional(\n\t\tType.Array(taskItemSchema, {\n\t\t\tdescription: \"Array of tasks to run in parallel (max 8)\",\n\t\t\tminItems: 1,\n\t\t\tmaxItems: MAX_PARALLEL_TASKS,\n\t\t}),\n\t),\n\tchain: Type.Optional(\n\t\tType.Array(taskItemSchema, {\n\t\t\tdescription: \"Sequential pipeline — each step can use {previous} for prior output\",\n\t\t\tminItems: 1,\n\t\t}),\n\t),\n\t// background parameter removed — all subagents run in background mode.\n\t// Kept in schema for backward compatibility (silently ignored if passed).\n\tbackground: Type.Optional(\n\t\tType.Boolean({ description: \"Deprecated — all subagents run in background mode. This parameter is ignored.\" }),\n\t),\n});\n\nexport type SubagentToolInput = Static<typeof subagentSchema>;\n\nexport interface SubagentToolDetails {\n\ttruncation?: TruncationResult;\n\tmode: \"single\" | \"parallel\" | \"chain\";\n\tagentCount: number;\n}\n\nfunction formatSubagentCall(\n\targs: SubagentToolInput | undefined,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.js\").theme,\n\targsComplete = true,\n): string {\n\tconst invalidArg = invalidArgText(theme);\n\n\tif (args?.tasks) {\n\t\treturn (\n\t\t\ttheme.fg(\"toolTitle\", theme.bold(\"subagent\")) +\n\t\t\t\" \" +\n\t\t\ttheme.fg(\"accent\", `parallel (${args.tasks.length} tasks)`)\n\t\t);\n\t}\n\tif (args?.chain) {\n\t\treturn `${theme.fg(\"toolTitle\", theme.bold(\"subagent\"))} ${theme.fg(\"accent\", `chain (${args.chain.length} steps)`)}`;\n\t}\n\n\tconst agent = str(args?.agent) || DEFAULT_AGENT;\n\tconst model = str(args?.model);\n\tconst task = str(args?.task);\n\tconst taskPreview = task ? (task.length > 60 ? `${task.slice(0, 57)}...` : task) : null;\n\tconst modelSuffix = model ? ` ${theme.fg(\"muted\", `(${model})`)}` : \"\";\n\treturn (\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"subagent\")) +\n\t\t\" \" +\n\t\ttheme.fg(\"accent\", agent) +\n\t\tmodelSuffix +\n\t\t\" \" +\n\t\t(taskPreview === null\n\t\t\t? argsComplete\n\t\t\t\t? invalidArg\n\t\t\t\t: theme.fg(\"muted\", \"…\")\n\t\t\t: theme.fg(\"toolOutput\", `\"${taskPreview}\"`))\n\t);\n}\n\nfunction formatSubagentResult(\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string }>;\n\t\tdetails?: SubagentToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.js\").theme,\n\tshowImages: boolean,\n): string {\n\tconst output = getTextOutput(result, showImages).trim();\n\tlet text = \"\";\n\tif (output) {\n\t\tconst lines = output.split(\"\\n\");\n\t\tconst maxLines = options.expanded ? lines.length : 25;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n${displayLines.map((line) => theme.fg(\"toolOutput\", line)).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines,`)} ${keyHint(\"app.tools.expand\", \"to expand\")})`;\n\t\t}\n\t}\n\tconst truncation = result.details?.truncation;\n\tif (truncation?.truncated) {\n\t\ttext += `\\n${theme.fg(\"warning\", `[Truncated: ${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit]`)}`;\n\t}\n\treturn text;\n}\n\nfunction formatSingleResult(result: SubagentResult): string {\n\tlet text = `## Agent: ${result.agent}${result.model ? ` (model: ${result.model})` : \"\"}\\n`;\n\tif (result.exitCode !== 0) {\n\t\ttext += `**Error** (exit ${result.exitCode}): ${result.errorMessage || \"Unknown error\"}\\n`;\n\t\tif (result.stderr) {\n\t\t\ttext += `\\nStderr:\\n${result.stderr}\\n`;\n\t\t}\n\t}\n\tif (result.output) {\n\t\ttext += `\\n${result.output}`;\n\t} else if (result.exitCode === 0) {\n\t\ttext += \"\\n(No output)\";\n\t}\n\tif (result.sessionFile) {\n\t\ttext += `\\n\\nSession log: ${result.sessionFile}`;\n\t}\n\treturn text;\n}\n\nexport function createSubagentToolDefinition(\n\tcwd: string,\n\toptions?: SubagentToolOptions,\n): ToolDefinition<typeof subagentSchema, SubagentToolDetails | undefined> {\n\tconst onBackgroundStart = options?.onBackgroundStart;\n\tconst onBackgroundComplete = options?.onBackgroundComplete;\n\tconst getParentProvider = options?.parentProvider ?? (() => undefined);\n\tconst modelRegistry = options?.modelRegistry;\n\n\t// Discover agents at definition time to build the prompt guidelines.\n\t// This is cheap (reads .md files) and the same call happens on every execute().\n\tconst knownAgents = discoverAgentTypes(cwd);\n\tconst agentListParts: string[] = [];\n\tfor (const [name, config] of knownAgents) {\n\t\tconst defaultTag = name === DEFAULT_AGENT ? \" (default)\" : \"\";\n\t\tconst desc = config.description || name;\n\t\tagentListParts.push(`'${name}'${defaultTag} — ${desc}`);\n\t}\n\tconst builtInAgentsLine = `Built-in agents: ${agentListParts.join(\"; \")}`;\n\n\treturn {\n\t\tname: \"subagent\",\n\t\tlabel: \"subagent\",\n\t\tdescription:\n\t\t\t\"Delegate tasks to independent subagents (Explore for codebase research, Sandbox for isolated /tmp-only analysis). \" +\n\t\t\t\"Supports single task, parallel (up to 8, max 4 concurrent), \" +\n\t\t\t\"and chain (sequential pipeline with {previous} substitution) modes. \" +\n\t\t\t\"All subagents run in background — returns immediately, notifies on completion.\",\n\t\tpromptSnippet: \"Delegate tasks to independent subagents\",\n\t\tpromptGuidelines: [\n\t\t\t\"Use `subagent` to delegate focused, independent tasks to child agents\",\n\t\t\t\"Available agent types can be discovered from ~/.dreb/agents/ and .dreb/agents/ markdown files\",\n\t\t\tbuiltInAgentsLine,\n\t\t\t\"Use parallel mode for independent tasks that can run concurrently\",\n\t\t\t\"Use chain mode when each step depends on the previous step's output (reference with {previous})\",\n\t\t\t\"All subagents run in background — the tool returns immediately and you are notified when each agent completes.\",\n\t\t\t\"Subagents have their own context window — provide enough context in the task prompt\",\n\t\t\t\"Each agent notifies independently when done — completion messages include a list of any still-running agents. If you need their results before proceeding, stop generating — do not output anything, do not launch filler work. Your turn ends, and when an agent completes, its result arrives as a new message that resumes your turn automatically.\",\n\t\t\t\"Agent definitions specify a `model` field with a provider fallback list (comma-separated or YAML list). The spawner tries each in order and uses the first one that resolves for the current provider. This makes agents portable across providers.\",\n\t\t\t\"Per-invocation `model` overrides take precedence but **discard the entire fallback list** — if the single override model isn't available on the current provider, the agent fails. Only override when you have a specific reason (e.g. escalating to a stronger tier for a complex task).\",\n\t\t\t\"**Model routing** — agent definitions already specify the right tier for their role. Most subagent tasks (exploration, file discovery, grep, navigation, summarization) are handled well by the defaults. Do not override the model unless the task genuinely requires a different capability tier than what the agent definition provides.\",\n\t\t],\n\t\tparameters: subagentSchema,\n\n\t\tasync execute(_toolCallId, params: SubagentToolInput, _signal, _onUpdate) {\n\t\t\tconst agents = discoverAgentTypes(cwd);\n\n\t\t\t// Determine mode\n\t\t\tconst modeCount = (params.task ? 1 : 0) + (params.tasks ? 1 : 0) + (params.chain ? 1 : 0);\n\t\t\tif (modeCount === 0) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{ type: \"text\", text: \"Error: provide one of `task` (single), `tasks` (parallel), or `chain`.\" },\n\t\t\t\t\t],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\t\t\tif (modeCount > 1) {\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\ttext: \"Error: modes are mutually exclusive — provide only one of `task`, `tasks`, or `chain`.\",\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// All subagents run in background mode — return immediately, notify on completion\n\t\t\t{\n\t\t\t\tif (!onBackgroundComplete) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: \"Subagent execution requires background support, which is not available in this session.\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t/**\n\t\t\t\t * Shared lifecycle for all background launches: generates agent ID,\n\t\t\t\t * sets up registry/abort/notification, gates on the concurrency\n\t\t\t\t * semaphore, and handles errors. The caller provides the actual\n\t\t\t\t * work via `runFn(signal)` which must return a SubagentResult.\n\t\t\t\t */\n\t\t\t\tconst launchBackgroundLifecycle = (\n\t\t\t\t\tagentName: string,\n\t\t\t\t\ttaskSummary: string,\n\t\t\t\t\trunFn: (signal: AbortSignal) => Promise<SubagentResult>,\n\t\t\t\t): string => {\n\t\t\t\t\tconst agentId = generateAgentId();\n\t\t\t\t\tconst bgAbort = new AbortController();\n\t\t\t\t\tbackgroundAgentRegistry.set(agentId, {\n\t\t\t\t\t\tagentId,\n\t\t\t\t\t\tagentType: agentName,\n\t\t\t\t\t\ttaskSummary,\n\t\t\t\t\t\tstartedAt: Date.now(),\n\t\t\t\t\t\tstatus: \"running\",\n\t\t\t\t\t});\n\t\t\t\t\tbackgroundAbortControllers.set(agentId, bgAbort);\n\t\t\t\t\tonBackgroundStart?.(agentId, agentName, taskSummary);\n\n\t\t\t\t\tconst bgSignal = bgAbort.signal;\n\n\t\t\t\t\tconst safeNotify = (result: SubagentResult) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tonBackgroundComplete(agentId, result, bgSignal.aborted);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t`[subagent] onBackgroundComplete threw for agent ${agentId}: ${err instanceof Error ? err.message : String(err)}. Background result lost.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tconst run = async () => {\n\t\t\t\t\t\tawait bgAcquire();\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst result = await runFn(bgSignal);\n\t\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\t\tif (entry && !bgSignal.aborted) entry.status = result.exitCode === 0 ? \"completed\" : \"failed\";\n\t\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\t\tsafeNotify(result);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\t\tif (entry && !bgSignal.aborted) entry.status = \"failed\";\n\t\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\t\tsafeNotify({\n\t\t\t\t\t\t\t\tagent: agentName,\n\t\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\t\texitCode: 1,\n\t\t\t\t\t\t\t\toutput: \"\",\n\t\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\t\terrorMessage: err instanceof Error ? err.message : String(err),\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\tbgRelease();\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t\trun().catch((err) => {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[subagent] Unhandled background error (${agentId}): ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst entry = backgroundAgentRegistry.get(agentId);\n\t\t\t\t\t\tif (entry && entry.status === \"running\") entry.status = \"failed\";\n\t\t\t\t\t\tbackgroundAbortControllers.delete(agentId);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tonBackgroundComplete(\n\t\t\t\t\t\t\t\tagentId,\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tagent: agentName,\n\t\t\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\t\t\texitCode: 1,\n\t\t\t\t\t\t\t\t\toutput: \"\",\n\t\t\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\t\t\terrorMessage: `Internal error: ${err instanceof Error ? err.message : String(err)}`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tbgSignal.aborted,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} catch (notifyErr) {\n\t\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\t`[subagent] CRITICAL: Last-resort notification failed for ${agentId}: ${notifyErr instanceof Error ? notifyErr.message : String(notifyErr)}`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\treturn agentId;\n\t\t\t\t};\n\n\t\t\t\t// Helper to launch a single background task\n\t\t\t\tconst subagentSessionsBase = getSubagentSessionsDir();\n\t\t\t\tconst launchBackgroundTask = (\n\t\t\t\t\tagentName: string,\n\t\t\t\t\ttask: string,\n\t\t\t\t\ttaskLabel: string,\n\t\t\t\t\ttaskCwd?: string,\n\t\t\t\t\tmodelOverride?: string,\n\t\t\t\t) => {\n\t\t\t\t\tconst resolvedCwd = taskCwd ?? cwd;\n\t\t\t\t\t// Each background agent gets its own session subdirectory\n\t\t\t\t\tconst sessionId = generateAgentId();\n\t\t\t\t\tconst sessionDir = join(subagentSessionsBase, sessionId);\n\t\t\t\t\treturn launchBackgroundLifecycle(agentName, taskLabel, (signal) =>\n\t\t\t\t\t\texecuteSingle(\n\t\t\t\t\t\t\tagents,\n\t\t\t\t\t\t\tagentName === DEFAULT_AGENT ? undefined : agentName,\n\t\t\t\t\t\t\ttask,\n\t\t\t\t\t\t\tresolvedCwd,\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tmodelOverride,\n\t\t\t\t\t\t\tgetParentProvider(),\n\t\t\t\t\t\t\tmodelRegistry,\n\t\t\t\t\t\t\tsessionDir,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t};\n\n\t\t\t\tif (params.task) {\n\t\t\t\t\t// Single background task\n\t\t\t\t\tconst agentName = params.agent || DEFAULT_AGENT;\n\t\t\t\t\tconst agentId = launchBackgroundTask(\n\t\t\t\t\t\tagentName,\n\t\t\t\t\t\tparams.task,\n\t\t\t\t\t\t`${agentName} task`,\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\tparams.model,\n\t\t\t\t\t);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: `Background agent ${agentId} started (${agentName}). You will be notified when it completes.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"single\", agentCount: 1 } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: true,\n\t\t\t\t\t};\n\t\t\t\t} else if (params.tasks) {\n\t\t\t\t\t// Parallel background tasks — each gets its own agent ID and notifies independently\n\t\t\t\t\tconst launched: Array<{ id: string; taskText: string }> = [];\n\t\t\t\t\tconst skipped: Array<{ taskText: string; error: string }> = [];\n\t\t\t\t\tfor (let i = 0; i < params.tasks.length; i++) {\n\t\t\t\t\t\tconst item = params.tasks[i];\n\t\t\t\t\t\tconst agentName = item.agent || DEFAULT_AGENT;\n\t\t\t\t\t\tconst cwdResult = clampCwd(cwd, item.cwd);\n\t\t\t\t\t\tif (!cwdResult.ok) {\n\t\t\t\t\t\t\tskipped.push({ taskText: item.task, error: cwdResult.error });\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst agentId = launchBackgroundTask(\n\t\t\t\t\t\t\tagentName,\n\t\t\t\t\t\t\titem.task,\n\t\t\t\t\t\t\t`${agentName} task ${i + 1}/${params.tasks.length}`,\n\t\t\t\t\t\t\tcwdResult.cwd,\n\t\t\t\t\t\t\titem.model,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tlaunched.push({ id: agentId, taskText: item.task });\n\t\t\t\t\t}\n\t\t\t\t\tconst listing = launched.map(({ id, taskText }) => ` ${id}: ${taskText.slice(0, 80)}`).join(\"\\n\");\n\t\t\t\t\tconst skippedListing = skipped\n\t\t\t\t\t\t.map(({ taskText, error }) => ` SKIPPED: ${taskText.slice(0, 60)} — ${error}`)\n\t\t\t\t\t\t.join(\"\\n\");\n\t\t\t\t\tconst parts = [`${launched.length} background agents started:\\n${listing}`];\n\t\t\t\t\tif (skipped.length > 0) {\n\t\t\t\t\t\tparts.push(`\\n${skipped.length} task(s) failed to launch:\\n${skippedListing}`);\n\t\t\t\t\t}\n\t\t\t\t\tif (launched.length > 0) {\n\t\t\t\t\t\tparts.push(\"\\nEach will notify independently when complete.\");\n\t\t\t\t\t} else {\n\t\t\t\t\t\tparts.push(\"\\nNo agents were launched.\");\n\t\t\t\t\t}\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: parts.join(\"\\n\"),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"parallel\", agentCount: launched.length } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: launched.length > 0,\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\t// Chain mode — sequential, stays as one agent since steps depend on each other\n\t\t\t\t\tconst agentName = params.chain![0].agent || DEFAULT_AGENT;\n\t\t\t\t\tconst taskSummary = `${params.chain!.length}-step chain`;\n\t\t\t\t\tconst chainSteps = params.chain!;\n\n\t\t\t\t\tconst chainSessionDir = join(subagentSessionsBase, `chain-${generateAgentId()}`);\n\t\t\t\t\tconst agentId = launchBackgroundLifecycle(agentName, taskSummary, async (signal) => {\n\t\t\t\t\t\tconst results = await executeChain(\n\t\t\t\t\t\t\tagents,\n\t\t\t\t\t\t\tchainSteps,\n\t\t\t\t\t\t\tcwd,\n\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tgetParentProvider(),\n\t\t\t\t\t\t\tmodelRegistry,\n\t\t\t\t\t\t\tchainSessionDir,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst resultText = results\n\t\t\t\t\t\t\t.map((r, i) => `### Step ${i + 1}\\n${formatSingleResult(r)}`)\n\t\t\t\t\t\t\t.join(\"\\n\\n---\\n\\n\");\n\t\t\t\t\t\tconst failed = results.filter((r) => r.exitCode !== 0);\n\t\t\t\t\t\t// Per-step session logs are already embedded in resultText via formatSingleResult\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tagent: \"chain\",\n\t\t\t\t\t\t\ttask: taskSummary,\n\t\t\t\t\t\t\texitCode: failed.length > 0 ? 1 : 0,\n\t\t\t\t\t\t\toutput: resultText,\n\t\t\t\t\t\t\tstderr: \"\",\n\t\t\t\t\t\t\terrorMessage:\n\t\t\t\t\t\t\t\tfailed.length > 0\n\t\t\t\t\t\t\t\t\t? `Chain stopped at step ${results.length} of ${chainSteps.length}: ${results[results.length - 1]?.errorMessage}`\n\t\t\t\t\t\t\t\t\t: null,\n\t\t\t\t\t\t};\n\t\t\t\t\t});\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: `Background chain ${agentId} started (${taskSummary}). You will be notified when it completes.`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: { mode: \"chain\", agentCount: chainSteps.length } as SubagentToolDetails,\n\t\t\t\t\t\tendTurn: true,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\trenderCall(args, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatSubagentCall(args, theme, context.argsComplete));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatSubagentResult(result as any, options, theme, context.showImages));\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createSubagentTool(cwd: string, options?: SubagentToolOptions): AgentTool<typeof subagentSchema> {\n\treturn wrapToolDefinition(createSubagentToolDefinition(cwd, options));\n}\n\nexport const subagentToolDefinition = createSubagentToolDefinition(process.cwd());\nexport const subagentTool = createSubagentTool(process.cwd());\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dreb/coding-agent",
3
- "version": "2.4.5",
3
+ "version": "2.5.1",
4
4
  "description": "Coding agent CLI with read, bash, edit, write tools and session management",
5
5
  "type": "module",
6
6
  "drebConfig": {