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,UAAU,EAAE,eAAe,GAAG,SAAS,CAAC,CA4F5E","sourcesContent":["import { type Static, Type } from \"typebox\";\nimport type { Context } from \"../context.ts\";\nimport type { AgentHarnessTool, ShellOutputTruncation, ShellOutputView } from \"../types.ts\";\nimport { applyShellOutputUpdate } from \"../utils/output-capture.ts\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize } from \"../utils/truncate.ts\";\nimport type { ExecutionToolContext } from \"./tool-context.ts\";\n\nconst MAX_TIMEOUT_SECONDS = 2_147_483_647 / 1000;\nconst BASH_CHECKPOINT_INTERVAL_MS = 2_000;\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?: ShellOutputTruncation;\n\tfullOutputPath?: string;\n}\n\nexport interface BashExecution {\n\tcommand: string;\n\tcwd: string;\n\tenv: Record<string, string>;\n\tinheritEnv: boolean;\n}\n\nexport type BashPrepare<TContext extends ExecutionToolContext = ExecutionToolContext> = (\n\texecution: BashExecution,\n\ttoolContext: TContext,\n\tcontext: Context,\n) => void | Promise<void>;\n\nexport interface BashToolOptions<TContext extends ExecutionToolContext = ExecutionToolContext> {\n\tcommandPrefix?: string;\n\tprepare?: BashPrepare<TContext>;\n}\n\nfunction validateTimeout(timeout: number | undefined): void {\n\tif (timeout === undefined) return;\n\tif (!Number.isFinite(timeout) || timeout <= 0) {\n\t\tthrow new Error(\"Invalid timeout: must be a finite number of seconds\");\n\t}\n\tif (timeout > MAX_TIMEOUT_SECONDS) {\n\t\tthrow new Error(`Invalid timeout: maximum is ${MAX_TIMEOUT_SECONDS} seconds`);\n\t}\n}\n\nexport function createBashTool<TContext extends ExecutionToolContext = ExecutionToolContext>(\n\toptions?: BashToolOptions<TContext>,\n): AgentHarnessTool<TContext, typeof bashSchema, BashToolDetails | undefined> {\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription: `Execute a bash command in the current working directory. Returns combined 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\tparameters: bashSchema,\n\t\tasync execute(_toolCallId, { command, timeout }, onUpdate, toolContext, _invocation, context) {\n\t\t\tvalidateTimeout(timeout);\n\t\t\tconst { env } = toolContext;\n\t\t\tconst execution: BashExecution = {\n\t\t\t\tcommand: options?.commandPrefix ? `${options.commandPrefix}\\n${command}` : command,\n\t\t\t\tcwd: env.cwd,\n\t\t\t\tenv: {},\n\t\t\t\tinheritEnv: true,\n\t\t\t};\n\t\t\tawait options?.prepare?.(execution, toolContext, context);\n\t\t\tlet view: ShellOutputView | undefined;\n\t\t\tlet lastCheckpointAt = Date.now();\n\t\t\tlet lastCheckpoint: string | undefined;\n\t\t\tlet acceptingUpdates = true;\n\n\t\t\tonUpdate({ content: [], details: undefined });\n\t\t\tconst result = await env.exec(\n\t\t\t\texecution.command,\n\t\t\t\t{\n\t\t\t\t\tcwd: execution.cwd,\n\t\t\t\t\tenv: execution.env,\n\t\t\t\t\tinheritEnv: execution.inheritEnv,\n\t\t\t\t\ttimeout,\n\t\t\t\t\tcapture: {\n\t\t\t\t\t\tlimits: { maxBytes: DEFAULT_MAX_BYTES, maxLines: DEFAULT_MAX_LINES, retain: \"tail\" },\n\t\t\t\t\t\tspill: true,\n\t\t\t\t\t},\n\t\t\t\t\tonUpdate: (update) => {\n\t\t\t\t\t\tif (!acceptingUpdates) return;\n\t\t\t\t\t\tview = applyShellOutputUpdate(view, update);\n\t\t\t\t\t\tconst snapshot = {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: view.text }],\n\t\t\t\t\t\t\tdetails: {\n\t\t\t\t\t\t\t\ttruncation: view.truncation.truncated ? view.truncation : undefined,\n\t\t\t\t\t\t\t\tfullOutputPath: view.spillPath,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst now = Date.now();\n\t\t\t\t\t\tconst encoded = JSON.stringify(snapshot);\n\t\t\t\t\t\tconst checkpoint =\n\t\t\t\t\t\t\tnow - lastCheckpointAt >= BASH_CHECKPOINT_INTERVAL_MS && encoded !== lastCheckpoint;\n\t\t\t\t\t\tonUpdate(snapshot, checkpoint ? { checkpoint: true } : undefined);\n\t\t\t\t\t\tif (checkpoint) {\n\t\t\t\t\t\t\tlastCheckpointAt = now;\n\t\t\t\t\t\t\tlastCheckpoint = encoded;\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tcontext,\n\t\t\t);\n\t\t\tacceptingUpdates = false;\n\n\t\t\tlet outputText = view?.text ?? \"\";\n\t\t\tconst capture = result.ok ? { text: outputText, ...result.value } : view;\n\t\t\tlet details: BashToolDetails | undefined;\n\t\t\tif (capture?.truncation.truncated) {\n\t\t\t\tdetails = { truncation: capture.truncation, fullOutputPath: capture.spillPath };\n\t\t\t\tconst startLine = capture.truncation.totalLines - capture.truncation.outputLines + 1;\n\t\t\t\tconst endLine = capture.truncation.totalLines;\n\t\t\t\tif (capture.truncation.lastLinePartial) {\n\t\t\t\t\tconst lastLineSize = formatSize(capture.lastLineBytes ?? capture.truncation.outputBytes);\n\t\t\t\t\toutputText += `\\n\\n[Showing last ${formatSize(capture.truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${capture.spillPath}]`;\n\t\t\t\t} else if (capture.truncation.truncatedBy === \"lines\") {\n\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${capture.truncation.totalLines}. Full output: ${capture.spillPath}]`;\n\t\t\t\t} else {\n\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${capture.truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${capture.spillPath}]`;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!result.ok) {\n\t\t\t\tconst status =\n\t\t\t\t\tresult.error.code === \"timeout\"\n\t\t\t\t\t\t? `Command timed out after ${timeout} seconds`\n\t\t\t\t\t\t: result.error.code === \"aborted\"\n\t\t\t\t\t\t\t? \"Command aborted\"\n\t\t\t\t\t\t\t: result.error.message;\n\t\t\t\tthrow new Error(outputText ? `${outputText}\\n\\n${status}` : status, { cause: result.error });\n\t\t\t}\n\t\t\tif (result.value.exitCode !== 0) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`${outputText ? `${outputText}\\n\\n` : \"\"}Command exited with code ${result.value.exitCode}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn { content: [{ type: \"text\", text: outputText || \"(no output)\" }], details };\n\t\t},\n\t};\n}\n"]}