Files
deepseek-harness/packages/bash/bash/src/index.ts
Yichen Jiang 184e164091 feat(tasks): background task runtime, generic task_* control tools, bash/subagent producers
One shared ctx.tasks registry (branded <kind>-N ids, owner-fenced
read/kill/wait/list, attachSurface misconfiguration fence, reported-flag
notice dedup, atomic register) + dsh-tool-tasks (task_output/task_list/
task_kill, completion-notice injection, background prompt habit).
Producers opt in via their own enableRunInBackground config: bash
(stream kind; seam slimmed to resolve/run/start returning a BashProcess
handle, bash_output/bash_kill deleted) and subagent (final-output kind;
done settles after run.dispose()). Owner disposal drains tasks through
the new awaited ctx.agents.onCleanup seam in the loop's disposal chain.
Both RFCs moved to implemented/; docs, catalogs, snapshots re-pinned.
2026-07-09 21:22:54 +08:00

100 lines
4.3 KiB
TypeScript

/**
* The bash executor seam (`ctx.bash`): an abstract service defining WHAT a
* bash backend does — run foreground commands, start background processes —
* without saying HOW. Implementations subclass {@link BashExecutor} and
* register themselves as the `bash` service; `@deepseek-ai/dsh-bash-local`
* (local subprocesses) is the first. Future implementations swap in
* sandboxes, containers, or remote exec servers without touching the tool
* schemas that consume them (`@deepseek-ai/dsh-tool-bash`).
*
* The split mirrors the LLM seam (`LlmService`/`LlmAdapter`) and the
* surveyed agents: pi hides execution behind a `BashOperations` interface
* (local shell / SSH / VM backends), Codex behind an exec-server protocol.
*
* The seam is deliberately TASK-FREE: `start()` hands back a
* {@link BashProcess} handle (incremental reads, kill, a quiescence promise)
* and nothing else. Task ids, owner isolation, polling tools, and completion
* notices are the generic `ctx.tasks` runtime's job (`@deepseek-ai/dsh-tasks`)
* — the tool layer adapts the handle into a task registration. This keeps a
* remote/sandbox executor free of any session or registry dependency.
*
* @module @deepseek-ai/dsh-bash
*/
import { Context, Service } from 'cordis'
import type { BashExecRequest, BashExecSpec, BashProcess, BashRunResult } from './types.ts'
export type {
BashExecRequest,
BashExecSpec,
BashProcess,
BashProcessRead,
BashProcessStatus,
BashRunResult,
CollectedOutput,
} from './types.ts'
declare module 'cordis' {
interface Context {
bash: BashExecutor
}
}
/**
* Abstract bash execution service. Subclass, implement the abstract methods,
* and load the subclass as a plugin — it registers as `ctx.bash` (one
* implementation per context; loading a second throws, which is cordis'
* standard duplicate-service behavior).
*
* Semantics every implementation must honor:
* - {@link run} REJECTS only for infrastructure failures (unusable workdir,
* missing shell, pre-aborted signal). Nonzero exits, timeout kills, and
* abort kills RESOLVE with a descriptive {@link BashRunResult} — reporting
* a failed command is the tool layer's job, not an exception.
* - {@link start} returns immediately; no timeout applies to background
* processes (callers stop them via {@link BashProcess.kill} or the spec's
* AbortSignal). The handle's `done` settles at process close and never
* rejects (a spawn failure settles as `killed` with the error readable on
* stderr).
* - {@link BashProcess.readOutput} is incremental: consecutive reads never
* re-deliver output. Implementations bound their buffers; reads that lost
* data flag `lossy` and point at full-stream spill files when available.
* - Disposal kills every running background process and awaits their exit
* (no orphan processes survive `fiber.dispose()`).
*/
export abstract class BashExecutor extends Service {
constructor(ctx: Context) {
super(ctx, 'bash')
}
/**
* Resolve a caller's {@link BashExecRequest} into a fully-specified
* {@link BashExecSpec}, applying this implementation's config defaults and
* caps (working directory, default/max timeout). Consumers (tool layer)
* call this, then pass the result to {@link run}/{@link start} — keeping
* defaulting in the implementation that owns the config while the seam type
* stays explicit (no hidden `?? default` inside run/start).
* @param request - the caller's request; omitted fields get this
* implementation's defaults, capped fields are clamped.
* @returns the fully-specified spec to hand to {@link run}/{@link start}.
*/
abstract resolve(request: BashExecRequest): BashExecSpec
/**
* Run a command in the foreground; resolves when it finishes.
* @param spec - a resolved spec from {@link resolve}, never a raw request.
* @returns the outcome; nonzero exits, timeout kills, and abort kills
* resolve with a descriptive result rather than reject.
*/
abstract run(spec: BashExecSpec): Promise<BashRunResult>
/**
* Start a background process and return its handle immediately.
* @param spec - a resolved spec from {@link resolve}, never a raw request.
* @returns the live process handle (reads, kill, quiescence promise).
*/
abstract start(spec: BashExecSpec): BashProcess
}
export default BashExecutor