Files
deepseek-harness/packages/shell/tool-bash/src/index.ts
T
涂轶翔 101f26de4e fix(system-prompt): 保留 section API 的增量系统指令 (#6941)
* feat(system-prompt): admit section updates incrementally

* docs(system-prompt): accept retirement restart gap

* test(system-prompt): cover retirement and reentry before admission

* test(system-prompt): align rebase fixtures and persistence history

* fix: align section prompt consumers and CI fixtures

* test: pin credentials for section snapshot fixtures

* test: align remaining snapshots with section admission

* test: preserve section metadata in Web and PowerShell replays

* refactor(system-prompt): retire sections through Cordis cleanup

* chore: refresh section admission history after rebase

* fix(system-prompt): advance admission head after compaction

* fix(system-prompt): trim sections before admission hashing

* docs: move system upgrade guides to current version

* docs: add system message contribution cookbook

* perf(system-prompt): avoid indexing history for admission

* refactor(system-prompt): derive admission from logged sections

* test(system-prompt): update lifecycle and compaction consumers

* test(system-prompt): refresh CLI section event expectations

* test(system-prompt): refresh question composer section metadata

* feat(system-prompt): resolve same-name sections by scope

* refactor(persona): use ordinary section registrations

* refactor(system-prompt): clarify assembly and related control flow

* test(system-prompt): reconcile rebased section snapshots

* test(python): use current system text helper after rebase

* test(system-prompt): refresh CLI expected section blocks

* test(system-prompt): reconcile latest master integration

* test(agent-loop): seed tools before deferred addition assertion

* refactor(compaction): replace system heads when compacting prefixes

* refactor(system-prompt): separate source text and notice attribution

* fix(system-prompt): preserve legacy instructions in compaction snapshots

* refactor(compaction): build system snapshots while reading the summary prefix

* refactor(compaction): rebuild current system instructions

* refactor(system-prompt): defer compaction rebuild to admission

* refactor(system-prompt): remove redundant fixtures and setup

* fix(plan-mode): announce committed user transitions

* docs(system-prompt): reconcile section lifecycle and compaction design

* refactor(system-prompt): consolidate projection version increments

* fix(system-prompt): reconcile master integration and replay fixtures

* docs(system-prompt): align upgrade guide with current release

* refactor(system-prompt): make admission operations explicit

* fix(system-prompt): reconcile master API and release changes

* docs(system-prompt): regenerate catalogs after master refresh

* refactor(llm-pi-ai): delegate text rendering to pi-ai

* fix(llm-pi-ai): preserve blank lines between system text parts

* refactor(system-prompt): remove redundant retirement metadata

* refactor(system-prompt): own admission and use canonical surface targets

* refactor(system-prompt): compare captured sections at commitment

Assembly fixes section text and lifecycle; PromptAssembly.systemUpdates() compares them with the committed projection when the loop commits. This replaces SystemPrompt.admit(), needsRebuild() and isCurrent(), the SystemAdmission export, the loop's reassembly after compaction, and SessionSurface.systemHeadResetSeq. The projection records a pending rebuild as a boolean that an attributed reset clears.

After a compaction inside a step, the rebuilt head uses the sections captured for that step; later source changes arrive as ordinary updates at the next step.

* test: await the session read fixture and type a matcher for lint

* test(tool-executor-ptc): describe same-name section override on executor replacement

Duplicate section names no longer reject a scoped executor; the earlier registration is overridden and returns as an ordinary update when the executor leaves.

* docs(upgrade-guide): move system-prompt integrations to v0.2.2-rc.3

The root version on master is 0.2.2-rc.3; verify-upgrade-guides rejects a guide added under an earlier version directory.

* refactor(system-prompt): rebuild over system messages without sections

A system message without source.sections now marks rebuilding pending, like a compaction summary. The next model attempt replaces the head with current sections and clears later system messages. This removes the separate legacy flag and its appended whole-prompt replacement and notice.

Sessions written by earlier releases, migrated logs, and imported DeepSeek conversations take the same path, so the importer and its tests return to their master versions.

* test(web): refresh the imported-conversation recording and drop a stray bytecode file

The Web recording for a resumed DeepSeek import was recorded while the prompt was appended after the imported history; the head is replaced again, as on master. The .pyc under the macOS signing skill was staged by accident in an earlier merge.

* refactor(session): scan system nodes on read

surface.systemNodes is read only when a system rebuild is written. Scanning the current surface nodes on each read restores the surface fold's plan types and state to their master definitions, which no longer carry the system message.

* refactor(session): rename SystemSurfaceNode.hasContent to isEmpty

The flag reports that the logged content array has no blocks. isEmpty matches the documented "empty head" wording and covers a head that was written empty without ever being cleared.

* refactor(llm): join system text through extractTextContent

extractTextContent gains a trim option that trims each text block and omits blocks left empty. The DeepSeek serializer, the pi-ai converter, and the prompt inspector use it with a blank-line separator in place of three hand-written joins.

The pi-ai converter now trims system and developer text like the other two, so whitespace-only blocks are omitted there as well.

* test(snapshots): refresh the PowerShell and cordis-inspect recordings

The two PowerShell scenarios still held the initial system message as one unattributed block; replay now writes one block per section with its attribution. The cordis-inspect-jsdoc recording embeds the API catalog, which names SystemSurfaceNode.isEmpty since the rename.

* test(plan-mode,harness-dev-context): cover a rejected pre-step and a repeated legacy prompt

A rejected pre-step must leave a queued plan-mode switch pending and unannounced until the next accepted step. A second unattributed system message with the same legacy paragraphs must keep the admitted paragraphs.

* test(web): refresh Web recordings for section-attributed system messages

Twenty-seven Web recordings still held the initial system message as one unattributed block. Replay now writes one text block per section with its attribution; twenty-six recordings differ only in that line.

plan-review also records the plan-mode switch notice before the first prompt and appends a plan:status update on exit in place of a head replacement and a series header, so its expanded golden gains the notice row.

The Creator persona test joins the system text blocks with blank lines, as the adapters do, before matching the persona line.

* refactor(system-prompt): require the retry argument of systemUpdates

The agent loop passes it on every attempt. A custom loop that omitted it on a retry received a full comparison in place of the pending rebuild, with no diagnostic; the type now makes each caller state which attempt it is.

* docs(upgrade-guide): move the system prompt integrations guide to v0.2.2-rc.5

The root version is 0.2.2-rc.5 after the master merge, and verify-upgrade-guides rejects a guide added under an earlier version directory.

* test(web): refresh the mention path presence recording for section-attributed system messages

* test: align two specs that master left failing

The DeepSeek settings spec waited for a saved notice carrying the provider label from before the rename to DeepSeek API. The Windows packaging workflow spec seeded one of the two helpers that the Stage workflow helpers step copies.

* chore: remove a Python cache file committed by mistake
2026-11-19 21:11:38 +08:00

603 lines
28 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Model-facing Consumer of the `ctx.shell` capability seam. While a job
* registry is composed, every call registers its process with `ctx.jobs` as
* it starts: `run_in_background` returns the id at once, and a foreground call
* waits on its job until the command finishes or the wait times out, at which
* point it returns the same id. Without a registry the tool is foreground-only
* and the executor's deadline kills the command.
*
* TODO(permissions): deployment policy belongs in `tools/pre-execute` and
* sandboxing executors; see docs/architecture.md § Where new behavior goes.
* @module @deepseek-ai/dsh-tool-bash
*/
import type {} from '@deepseek-ai/dsh-working-directory'
import { FiberState } from '@deepseek-ai/cordis'
import type { Context } from '@deepseek-ai/cordis'
import z from '@deepseek-ai/schemastery'
import { isAbsolute, sep } from 'node:path'
import { defineTool, TOOL_ABORTED, ToolArgsError, ToolContextUnavailableError } from '@deepseek-ai/dsh-tools'
import type { GenericCallView, TerminalCallView, ToolDefinition, ToolExecution, ToolResult, ToolResultView } from '@deepseek-ai/dsh-tools'
import { HarnessError } from '@deepseek-ai/dsh-llm'
import type { JobId, JobRegistry, JobView } from '@deepseek-ai/dsh-jobs'
import type {} from '@deepseek-ai/dsh-user-approval'
import type {} from '@deepseek-ai/dsh-shell-env'
import type { SandboxExecutionPolicy, SandboxMode } from '@deepseek-ai/dsh-sandbox'
import { ESCALATION_TARGETS, approveEscalation, sandboxPermissionsDescription, validateEscalationArgs } from '@deepseek-ai/dsh-sandbox'
import { subprocessSpawnFailure } from '@deepseek-ai/dsh-subprocess'
import type { SandboxPolicyService } from '@deepseek-ai/dsh-sandbox-policy'
import { DSH_ENV_PREFIX } from '@deepseek-ai/dsh-shell'
import type { ShellExecRequest, ShellExecSpec, ShellExecution, ShellRunResult } from '@deepseek-ai/dsh-shell'
import { processJob, processOutcome, processSources, ringDelta } from './background.ts'
import type { ProcessJobHooks } from './background.ts'
import { parseExitStatus, renderJobRead, renderPromoted, renderResult } from './render.ts'
export const name = 'tool-bash'
export const inject = ['tools', 'shell', 'systemPrompt', 'shellEnv', 'workingDirectory']
/** Configuration for the bash tool. */
export interface Config {
/**
* Expose `run_in_background` while a job registry is composed (default
* true); disabled calls are also rejected. Without a registry the tool is
* foreground-only regardless.
*/
enableRunInBackground?: boolean
/**
* Keep a foreground command that reaches its timeout running as a
* background job instead of killing it (default true). Applies only while
* background execution is available: with `enableRunInBackground` false or
* no job registry, the executor's deadline kills the command. A foreground
* command the registry refuses at its start (admission or a missing
* controller) also runs under the deadline kill.
*/
promoteOnTimeout?: boolean
}
/** Runtime configuration schema for the bash tool plugin. */
export const Config: z<Config> = z.object({
enableRunInBackground: z.boolean().default(true),
promoteOnTimeout: z.boolean().default(true),
})
/** Parsed tool args; execute validates value constraints absent from ParameterSchemaSpec. */
interface BashToolArgs {
command: string
description: string
timeoutMs?: number
workdir?: string
run_in_background?: boolean
sandbox_permissions?: string
/** Optional for an omitted or repeated effective mode; widening requires a non-empty reason. */
justification?: string
}
/** Which optional argument groups the registered schema advertises in this composition. */
interface AdvertisedArgs {
/** `sandbox_permissions` and `justification`; advertised only with a sandboxing executor. */
escalation: boolean
/** `run_in_background`; advertised unless `enableRunInBackground: false`. */
background: boolean
}
/**
* Reject arguments the schema does not advertise. Schema validation checks
* advertised keys only, so these still reach `execute`; the model fixes the call
* by dropping them, which makes them argument errors. Runs before escalation
* pairing checks, approval, and working-directory setup.
* @param args - the call's arguments.
* @param advertised - the argument groups this composition advertises.
*/
function rejectUnadvertisedArgs(
args: { sandbox_permissions?: string; justification?: string; run_in_background?: boolean },
advertised: AdvertisedArgs,
): void {
if (!advertised.escalation && (args.sandbox_permissions !== undefined || (args.justification ?? '').trim() !== '')) {
throw new ToolArgsError(['sandbox_permissions and justification are not available in this composition (no sandboxing executor to escalate)'])
}
if (!advertised.background && args.run_in_background === true) {
throw new ToolArgsError(['run_in_background is disabled for this deployment (enableRunInBackground: false)'])
}
}
function validateBashArgs(args: BashToolArgs, effectiveMode: SandboxMode | undefined, advertised: AdvertisedArgs): void {
if (args.command.trim().length === 0) {
throw new ToolArgsError(['invalid command: expected a non-empty string'])
}
if (args.description.trim().length === 0) {
throw new ToolArgsError(['invalid description: expected a non-empty string'])
}
if (args.timeoutMs !== undefined && (!Number.isFinite(args.timeoutMs) || args.timeoutMs <= 0)) {
throw new ToolArgsError([`invalid timeoutMs: expected a positive number, got ${JSON.stringify(args.timeoutMs)}`])
}
rejectUnadvertisedArgs(args, advertised)
if (args.sandbox_permissions !== undefined && args.sandbox_permissions === effectiveMode) return
const justification = args.sandbox_permissions === undefined && args.justification?.trim() === ''
? undefined
: args.justification
validateEscalationArgs(args.sandbox_permissions, justification)
}
function bashDescription(): string {
return 'Execute a bash command (`bash -c`) and return its stdout/stderr. '
+ 'Each call runs in a fresh shell; pass `workdir` instead of using `cd`. '
+ `Managed \`$${DSH_ENV_PREFIX}*\` variables expose current harness environment facts. `
+ 'Long output is truncated to its tail; the full output is saved to a file whose path is reported when available. '
+ 'Provide `description` before `command` in the arguments. '
+ 'Before any delete or move, verify that the resolved absolute target path is the intended one; never run it against a computed path you have not checked. '
+ 'An unset variable expands to an empty string, so guard variables in such paths with `${VAR:?}`. '
+ 'Commands may run under a file sandbox; a blocked file operation is reported as `[sandbox: file access denied under <mode> mode]`, a policy denial: do not retry another way.'
}
/**
* Present foreground calls as terminals and background starts as generic cards.
* The command remains the title on both paths; foreground cwd is passed through
* for the bridge to resolve, while background descriptions remain card content.
*/
type BashCallArgs = { command: string; description: string; workdir?: string; run_in_background?: boolean }
function presentBashCall(args: BashCallArgs): GenericCallView | TerminalCallView {
if (args.run_in_background === true) {
return {
card: 'generic',
title: args.command,
kind: 'execute',
rawInput: args.command,
content: [{ type: 'text', text: args.description }],
}
}
return {
card: 'terminal',
title: args.command,
description: args.description,
...args.workdir !== undefined ? { cwd: args.workdir } : {},
}
}
/**
* Present completed foreground output as a terminal; background acknowledgements
* and execution errors use generic fenced output without an exit-status pill.
*/
function presentBashResult(args: unknown, result: ToolResult): ToolResultView | undefined {
const block = result.content.length === 1 ? result.content[0] : undefined
if (block === undefined || block.type !== 'text') return undefined
const raw = block.text
const isBackground = typeof args === 'object' && args !== null && (args as { run_in_background?: unknown }).run_in_background === true
const isPromoted = (result as { value?: { kind?: unknown } }).value?.kind === 'promoted'
// Background acknowledgements, promotions, and errors have no terminal exit status.
if (isBackground || isPromoted || result.isError) {
return { card: 'generic', content: [{ type: 'text', text: `\`\`\`console\n${raw.replace(/\n+$/, '')}\n\`\`\`` }] }
}
// The exit marker becomes the card's exit pill, so it leaves the output body.
const { body, ...exit } = parseExitStatus(raw)
return { card: 'terminal', output: body, ...exit }
}
/** Resolve a relative per-call override against the Session's current directory. */
function resolveWorkdir(modelWorkdir: string | undefined, sessionCwd: string | undefined): string | undefined {
if (modelWorkdir === undefined) return sessionCwd
if (sessionCwd !== undefined && !isAbsolute(modelWorkdir)) {
return `${sessionCwd}${sep}${modelWorkdir}`
}
return modelWorkdir
}
/** Detach the executor DTO from readonly Service Definition types into plain JSON data. */
function canonicalBashResult(result: ShellRunResult, cwd: string) {
const output = (stream: ShellRunResult['stdout']) => ({
text: stream.text,
truncated: stream.truncated,
...stream.spillPath !== undefined ? { spillPath: stream.spillPath } : {},
})
return {
cwd,
exitCode: result.exitCode,
signal: result.signal,
timedOut: result.timedOut,
aborted: result.aborted,
timeoutMs: result.timeoutMs,
stdout: output(result.stdout),
stderr: output(result.stderr),
...result.sandbox !== undefined ? {
sandbox: {
mode: result.sandbox.mode,
denied: result.sandbox.denied,
...result.sandbox.enforcement !== undefined ? { enforcement: result.sandbox.enforcement } : {},
...result.sandbox.runnerFailed !== undefined ? { runnerFailed: result.sandbox.runnerFailed } : {},
},
} : {},
}
}
/** The structured abort the foreground paths throw when the caller cancels the call. */
function toolAborted(): HarnessError {
const error = new HarnessError('tool call aborted', TOOL_ABORTED)
error.name = 'AbortError'
return error
}
/**
* Report a launch or preparation rejection as this tool's failure. This
* call's cancellation reason stays as thrown so the registry records it as
* aborted; a platform spawn error is classified as a subprocess spawn failure
* with the original as `cause`; any other value, including a coded error,
* keeps its own identity.
*/
function launchFailure(error: unknown, signal: AbortSignal): unknown {
return signal.aborted && error === signal.reason ? error : subprocessSpawnFailure(error, 'bash')
}
/** A registered command: its id, the process once the starter spawned it, and the reason an outside kill gave. */
interface StartedJob {
readonly id: JobId
readonly process: () => ShellExecution | undefined
readonly stopped: () => string | undefined
/** The original error of a registration whose preparation failed before the process published. */
readonly failure: () => { error: unknown } | undefined
}
/** Canonical background-handle properties shared by the bash output union. */
const BACKGROUND_OUTPUT_PROPERTIES = {
kind: { type: 'string', required: true, const: 'background' },
jobId: { type: 'string', required: true },
cwd: { type: 'string', required: true },
} as const
export function apply(ctx: Context, config: Config = {}): void {
const backgroundEnabled = config.enableRunInBackground ?? true
// Keeping a timed-out command needs the whole background surface: the job
// tools to collect and stop it, and the registry itself at execution time.
const promoteOnTimeout = (config.promoteOnTimeout ?? true) && backgroundEnabled
const defaultMode = ctx.shell.sandboxMode
const escalationModes: readonly SandboxMode[] = defaultMode === undefined ? [] : ESCALATION_TARGETS
const sandboxPolicy: SandboxPolicyService | undefined = defaultMode === undefined ? undefined : ctx.get('sandboxPolicy')
if (defaultMode !== undefined && sandboxPolicy === undefined) {
throw new Error('tool-bash: the mounted bash executor confines but ctx.sandboxPolicy is missing')
}
/** Resolve the complete standing policy for this call when a confining executor is mounted. */
const resolveSandboxPolicy = (exec: ToolExecution): SandboxExecutionPolicy | undefined =>
sandboxPolicy?.resolve(exec.agent === undefined ? {} : { session: exec.agent.session })
/**
* Resolve a sandbox-escalation request through `ctx.approval` BEFORE
* anything executes, delegating the shared fail-closed sequence (strict
* widening, channel resolution, outcome mapping) to
* {@link approveEscalation}. This tool contributes only the approval
* ingredients; {@link validateBashArgs} has already rejected the fields when
* no sandboxing executor advertises them. The shared policy resolver is
* required whenever the executor advertises confinement, so a split
* composition fails at tool-plugin load.
*/
const approveBashEscalation = (
mode: string,
justification: string,
exec: ToolExecution,
standingPolicy: SandboxExecutionPolicy | undefined,
): Promise<SandboxMode> => {
const effectiveMode = (standingPolicy as SandboxExecutionPolicy).mode
return approveEscalation(
{ requestedMode: mode, justification, effectiveMode, subject: 'command' },
{
approver: ctx.get('approval'),
agent: exec.agent,
callId: exec.callId,
toolName: 'bash',
signal: exec.signal,
},
)
}
// Cross-call guidance belongs in the prompt rather than one-call schema prose.
ctx.systemPrompt.section({
name: 'tool:bash',
order: ctx.systemPrompt.getSectionOrder('TOOL_BASH'),
text: 'Check the [exit code: N] marker on every bash result; investigate failures before moving on.',
retirement: 'The bash tool is no longer available. Its guidance no longer applies.',
})
/**
* One registration of the `bash` tool. With a registry, every call
* registers its process as a job at its start; without one the tool is
* foreground-only and the executor's deadline kills the command.
*/
const bashTool = (jobs: JobRegistry | undefined): ToolDefinition => {
const background = jobs !== undefined
const promote = background && promoteOnTimeout
/** Register the command as a job; the process spawns inside the starter, after admission. */
const startJob = (registry: JobRegistry, args: BashToolArgs, exec: ToolExecution, spec: ShellExecSpec): StartedJob => {
let proc: ShellExecution | undefined
let stopped: string | undefined
let hooks: ProcessJobHooks | undefined
const id = registry.start({
kind: 'bash',
label: args.command,
toolCall: { callId: exec.callId, rootCallId: exec.rootCallId },
...exec.agent ? { owner: exec.agent.id } : {},
output: processSources(() => proc),
run: () => {
const job = processJob(
async (signal) => {
proc = await ctx.shell.execute({ ...spec, signal })
return proc
},
started => processOutcome(started, escalationModes),
)
hooks = job
return {
done: job.done,
// A kill from outside this call (the human's, a parallel job_kill)
// reaches the process here; its reason is what the model reads.
cancel: (reason) => {
stopped = reason
job.cancel(reason)
},
}
},
})
return { id, process: () => proc, stopped: () => stopped, failure: () => hooks?.failure() }
}
/** Wait on a registered foreground command until it settles or the timeout passes. */
const waitOnJob = async (registry: JobRegistry, attached: StartedJob, exec: ToolExecution, spec: ShellExecSpec) => {
const owner = exec.agent?.id
const timeoutMs = spec.timeoutMs
/**
* Stop the job on this call's own account and stay on it until it
* settles, so the settlement is `awaited` and no completion notice
* follows a result this call already carries; the record then leaves
* with the call, as the model never saw the id.
*/
const stop = async (reason: string): Promise<JobView> => {
registry.kill(attached.id, owner, reason)
const settled = await registry.wait(attached.id, timeoutMs, owner)
if (settled.status !== 'running' && settled.status !== 'stopping') registry.remove(attached.id, owner)
return settled
}
let view: JobView
try {
view = await registry.wait(attached.id, timeoutMs, owner, exec.signal)
} catch {
// A wait on the caller's own live job rejects only for the caller's
// abort: the model's call is over, so the command goes with it, as
// under the deadline kill.
await stop('tool call aborted')
throw toolAborted()
}
if ((view.status === 'running' || view.status === 'stopping') && attached.process() === undefined) {
// The wait expired while preparation still ran: nothing is running
// to keep, so this is the deadline's preparation timeout, with the
// settled empty result the executor's own deadline would have given.
await stop('timed out during preparation')
return {
kind: 'foreground' as const,
cwd: spec.workdir,
exitCode: null,
signal: null,
timedOut: true,
aborted: false,
timeoutMs,
stdout: { text: '', truncated: false },
stderr: { text: '', truncated: false },
...spec.sandboxPolicy !== undefined ? { sandbox: { mode: spec.sandboxPolicy.mode, denied: false } } : {},
}
}
if (view.status === 'running' || view.status === 'stopping') {
// The wait timed out: the job keeps running. One consuming read seeds
// the result with the output so far, so `job_output` continues exactly
// where this result stops.
const read = registry.read(attached.id, owner)
return {
kind: 'promoted' as const,
cwd: spec.workdir,
jobId: attached.id,
timeoutMs,
output: renderJobRead(
ringDelta(read.chunks), read.lossy, read.job.output.spillPaths ?? [], attached.process()?.sandbox, escalationModes,
),
}
}
// Settled while the call waited: the model never saw the id, so the
// record leaves the registry with this result. The process handle keeps
// the split streams and classification the foreground result needs.
registry.remove(attached.id, owner)
const process = attached.process()
// No process was published: the starter failed before the spawn. The
// original error keeps its class and code, exactly as the foreground
// projection would have rejected; a kill before the spawn has only its detail.
if (process === undefined) {
const failure = attached.failure()
throw failure === undefined ? new Error(view.detail) : launchFailure(failure.error, exec.signal)
}
const result = await process.result().catch((error: unknown) => { throw launchFailure(error, exec.signal) })
const stopped = attached.stopped()
return { kind: 'foreground' as const, ...canonicalBashResult(result, spec.workdir), ...stopped !== undefined ? { stopped } : {} }
}
return defineTool({
name: 'bash',
description: bashDescription(),
parameters: {
description: {
type: 'string',
required: true,
description: 'Clear, concise description of what this command does in active voice, '
+ '5-10 words (shown in the UI). Examples: "ls" → "List files in current directory"; '
+ '"git status" → "Show working tree status"; "npm install" → "Install package dependencies".',
},
command: { type: 'string', required: true, description: 'The bash command to execute.' },
timeoutMs: {
type: 'number',
description: promote
? 'Timeout in milliseconds. The executor applies its configured default and cap; on expiry the command moves to the background as a job instead of being killed.'
: 'Timeout in milliseconds. The executor applies its configured default and cap, and kills the command on expiry.',
},
workdir: { type: 'string', description: 'Working directory for this command. Defaults to the session workspace; a relative path is resolved against it.' },
...background ? {
run_in_background: { type: 'boolean' as const, description: 'Run in the background and return a job id immediately (collect with job_output, stop with job_kill). No timeout applies.' },
} : {},
...escalationModes.length > 0 ? {
sandbox_permissions: {
type: 'string' as const,
enum: [...escalationModes],
description: sandboxPermissionsDescription('command'),
},
justification: {
type: 'string' as const,
description: 'Required with sandbox_permissions: one sentence for the user explaining why this exact command needs the wider access. '
+ 'Use the language of the user’s current request.',
},
} : {},
},
output: {
schema: {
oneOf: [
{
type: 'object',
additionalProperties: false,
properties: BACKGROUND_OUTPUT_PROPERTIES,
},
{
type: 'object',
additionalProperties: false,
properties: {
kind: { type: 'string', required: true, const: 'promoted' },
cwd: { type: 'string', required: true },
jobId: { type: 'string', required: true },
timeoutMs: { type: 'number', required: true },
output: { type: 'string', required: true },
},
},
{
type: 'object',
additionalProperties: false,
properties: {
kind: { type: 'string', required: true, const: 'foreground' },
cwd: { type: 'string', required: true },
exitCode: { required: true, oneOf: [{ type: 'integer' }, { type: 'null' }] },
signal: { required: true, oneOf: [{ type: 'string' }, { type: 'null' }] },
timedOut: { type: 'boolean', required: true },
aborted: { type: 'boolean', required: true },
stopped: { type: 'string' },
timeoutMs: { type: 'number', required: true },
stdout: {
type: 'object',
additionalProperties: false,
required: true,
properties: {
text: { type: 'string', required: true },
truncated: { type: 'boolean', required: true },
spillPath: { type: 'string' },
},
},
stderr: {
type: 'object',
additionalProperties: false,
required: true,
properties: {
text: { type: 'string', required: true },
truncated: { type: 'boolean', required: true },
spillPath: { type: 'string' },
},
},
sandbox: {
type: 'object',
additionalProperties: false,
properties: {
mode: { type: 'string', required: true },
denied: { type: 'boolean', required: true },
enforcement: { type: 'string' },
runnerFailed: { type: 'boolean' },
},
},
},
},
],
},
render: (_args, value) => [{
type: 'text',
text: value.kind === 'background'
? `started background job ${value.jobId}`
: value.kind === 'promoted'
? renderPromoted(value)
: renderResult(value as { kind: 'foreground' } & ShellRunResult, escalationModes),
}],
presentationMeta: (_args, value) => ({ cwd: value.cwd }),
},
async execute(args: BashToolArgs, exec) {
// Description is display metadata; workdir defaults to the caller's session.
const standingPolicy = resolveSandboxPolicy(exec)
validateBashArgs(args, standingPolicy?.mode, { escalation: escalationModes.length > 0, background: backgroundEnabled })
const approvedMode = args.sandbox_permissions !== undefined && args.justification !== undefined
? await approveBashEscalation(args.sandbox_permissions, args.justification, exec, standingPolicy)
: undefined
const policy = approvedMode === undefined
? standingPolicy
: { ...(standingPolicy as SandboxExecutionPolicy), mode: approvedMode }
const cwd = exec.agent === undefined ? undefined : await ctx.workingDirectory.ensure(exec.agent, exec.signal)
const workdir = resolveWorkdir(args.workdir, cwd)
const dshEnv = ctx.shellEnv.collect(exec)
const request: ShellExecRequest = {
command: args.command,
...workdir !== undefined ? { workdir } : {},
...args.timeoutMs !== undefined ? { timeoutMs: args.timeoutMs } : {},
dshEnv,
...policy !== undefined ? { sandboxPolicy: policy } : {},
}
if (args.run_in_background === true) {
if (jobs === undefined) {
throw new ToolContextUnavailableError('background jobs unavailable: load @deepseek-ai/dsh-jobs and @deepseek-ai/dsh-tool-jobs')
}
// The caller owns cancellation until ctx.jobs commits detached ownership.
if (exec.signal.aborted) throw toolAborted()
const spec = ctx.shell.resolve({ ...request, onExpiry: 'none' })
return { kind: 'background' as const, jobId: startJob(jobs, args, exec, spec).id, cwd: spec.workdir }
}
// A foreground call is a job the tool waits on, so the command is
// visible and killable from the moment it starts and outlives the wait
// when the timeout passes. Admission refused at the start (the owner's
// job limit, no controller) runs the command under the deadline kill
// instead, exactly as a composition without a registry does.
if (jobs !== undefined && promote) {
const spec = ctx.shell.resolve({ ...request, onExpiry: 'none' })
let attached: StartedJob | undefined
try {
attached = startJob(jobs, args, exec, spec)
} catch (error) {
ctx.logger.warn(`bash: job registration refused, running in the foreground with the timeout kill instead: ${String(error)}`)
}
if (attached !== undefined) return waitOnJob(jobs, attached, exec, spec)
}
const spec = ctx.shell.resolve({ ...request, signal: exec.signal })
let result: ShellRunResult
try {
result = await (await ctx.shell.execute(spec)).result()
} catch (error) {
throw launchFailure(error, exec.signal)
}
if (result.aborted) throw toolAborted()
return { kind: 'foreground' as const, ...canonicalBashResult(result, spec.workdir) }
},
presentCall: presentBashCall,
presentResult: presentBashResult,
})
}
// The background surface follows the registry. The foreground-only variant
// registers now unless a registry is already composed, the job-backed
// variant replaces it for as long as `ctx.jobs` is present, and the
// foreground-only one returns when the registry unloads while this plugin
// stays. Both registrations belong to this plugin's own fiber.
if (!backgroundEnabled) {
ctx.tools.register(bashTool(undefined))
return
}
let foregroundOnly = ctx.get('jobs') === undefined ? ctx.tools.register(bashTool(undefined)) : undefined
ctx.inject(['jobs'], (jobCtx) => {
foregroundOnly?.()
foregroundOnly = undefined
const unregister = ctx.tools.register(bashTool(jobCtx.jobs))
jobCtx.effect(() => () => {
unregister()
if (ctx.fiber.state === FiberState.ACTIVE) foregroundOnly = ctx.tools.register(bashTool(undefined))
})
})
}