index.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * Local implementation of the bash executor seam over the subprocess
  3. * seam. Public commands run as `bash -c` in a managed process group spawned
  4. * through `ctx.subprocess`; subclasses may reuse the same mechanics with an
  5. * explicit argv. This executor owns command defaulting, deadlines and cause
  6. * classification, the model-friendly terminal environment, and the model-facing
  7. * stdout/stderr merge for background reads. Execution policy belongs in
  8. * `tools/pre-execute` or a sandboxing executor.
  9. * @module @deepseek-ai/dsh-bash-local
  10. */
  11. import { Context } from 'cordis'
  12. import z from 'schemastery'
  13. import { BashExecutor } from '@deepseek-ai/dsh-bash'
  14. import type { BashExecRequest, BashExecSpec, BashProcess, BashProcessRead, BashRunResult, CollectedOutput } from '@deepseek-ai/dsh-bash'
  15. import type { SubprocessCollect, SubprocessHandle, SubprocessOutputReader, SubprocessSpawnSpec } from '@deepseek-ai/dsh-subprocess'
  16. import { clampTimeout, deadline, MAX_TIMER_DELAY_MS, timeoutOf } from '@deepseek-ai/dsh-timeout'
  17. /**
  18. * Model-friendly environment overrides: disable colors, pagers, and
  19. * interactive terminal features that would garble tool output (the same set
  20. * Codex hardcodes; Claude Code achieves it via TERM=dumb). Bash-tool policy —
  21. * merged first into the spawn's explicit env, so a trusted caller's own entry
  22. * still wins; the subprocess service applies its credential scrub independently.
  23. */
  24. export const ENV_OVERRIDES = {
  25. NO_COLOR: '1',
  26. TERM: 'dumb',
  27. PAGER: 'cat',
  28. GIT_PAGER: 'cat',
  29. } as const
  30. /** Default SIGTERM→SIGKILL grace period (the `graceMs` config; matches OpenCode's 3s). */
  31. const DEFAULT_GRACE_MS = 3_000
  32. /** Default per-stream spill cap (the `maxSpillBytes` config). */
  33. const DEFAULT_MAX_SPILL_BYTES = 64 * 1024 * 1024
  34. /** Plugin config (all optional — `static Config` supplies the defaults). */
  35. export interface Config {
  36. /** Default working directory for commands (default: process.cwd()). */
  37. cwd?: string
  38. /** Default foreground timeout in milliseconds. */
  39. timeoutMs?: number
  40. /** Upper bound for per-call timeout overrides. */
  41. maxTimeoutMs?: number
  42. /** Per-stream in-memory output cap; overflow spills to a temp file. */
  43. maxOutputBytes?: number
  44. /** Per-stream spill-file cap; larger streams retain only their in-memory tail. */
  45. maxSpillBytes?: number
  46. /** Grace period for kill escalation and inherited pipes; at most `MAX_TIMER_DELAY_MS`. */
  47. graceMs?: number
  48. }
  49. /** The shape after schemastery applied the defaults (cwd has none). */
  50. type ResolvedConfig = Required<Omit<Config, 'cwd'>> & Pick<Config, 'cwd'>
  51. /** Project a settled collect-mode reader into the final CollectedOutput shape. */
  52. function finalOutput(reader: SubprocessOutputReader): CollectedOutput {
  53. const read = reader.readFrom(0)
  54. return {
  55. text: read.text,
  56. truncated: read.lossy,
  57. ...read.spillPath !== undefined ? { spillPath: read.spillPath } : {},
  58. }
  59. }
  60. function assertPositiveFinite(name: string, value: number): void {
  61. if (!Number.isFinite(value) || value <= 0) {
  62. throw new Error(`bash-local: ${name} must be a positive finite number`)
  63. }
  64. }
  65. /**
  66. * Local bash executor over `ctx.subprocess`. Bounded output, spill files, and
  67. * process-group SIGTERM→SIGKILL escalation are the subprocess service's
  68. * mechanics; this executor supplies their configured budgets per spawn, so a
  69. * still-running background process stays managed (killed and joined at
  70. * composition teardown) even across an executor reload.
  71. */
  72. export class LocalBashExecutor extends BashExecutor {
  73. static inject = ['subprocess']
  74. static Config: z<Config> = z.object({
  75. cwd: z.string(),
  76. timeoutMs: z.number().default(120_000),
  77. maxTimeoutMs: z.number().default(600_000),
  78. maxOutputBytes: z.number().default(64_000),
  79. maxSpillBytes: z.number().default(DEFAULT_MAX_SPILL_BYTES),
  80. graceMs: z.number().default(DEFAULT_GRACE_MS),
  81. })
  82. /** Validated config (schemastery applied the defaults before construction). */
  83. readonly config: ResolvedConfig
  84. constructor(ctx: Context, config: Config) {
  85. super(ctx)
  86. // Schemastery fills these fields before construction; the type does not encode that step.
  87. this.config = config as ResolvedConfig
  88. assertPositiveFinite('timeoutMs', this.config.timeoutMs)
  89. assertPositiveFinite('maxTimeoutMs', this.config.maxTimeoutMs)
  90. assertPositiveFinite('maxOutputBytes', this.config.maxOutputBytes)
  91. assertPositiveFinite('maxSpillBytes', this.config.maxSpillBytes)
  92. assertPositiveFinite('graceMs', this.config.graceMs)
  93. if (this.config.graceMs > MAX_TIMER_DELAY_MS) {
  94. throw new Error(`bash-local: graceMs must be no greater than ${MAX_TIMER_DELAY_MS}`)
  95. }
  96. }
  97. /**
  98. * Resolve a request into a fully-specified spec: fill `workdir` from
  99. * `config.cwd` (else `process.cwd()`), and `timeoutMs` from
  100. * `config.timeoutMs`, capped at `config.maxTimeoutMs`. The tool layer calls
  101. * this before {@link run}/{@link start}, so those methods receive explicit
  102. * values and never re-default.
  103. */
  104. resolve(request: BashExecRequest): BashExecSpec {
  105. const timeoutMs = clampTimeout(
  106. request.timeoutMs,
  107. this.config.timeoutMs,
  108. this.config.maxTimeoutMs,
  109. 'bash-local: request.timeoutMs',
  110. )
  111. const stdoutMaxBytes = request.stdoutMaxBytes ?? this.config.maxOutputBytes
  112. assertPositiveFinite('request.stdoutMaxBytes', stdoutMaxBytes)
  113. return {
  114. command: request.command,
  115. workdir: request.workdir ?? this.config.cwd ?? process.cwd(),
  116. timeoutMs,
  117. stdoutMaxBytes,
  118. ...request.signal ? { signal: request.signal } : {},
  119. // Carry stdin/ordinary env/trusted dshEnv through verbatim — optional,
  120. // no config default. The subprocess service owns the scrub and merge order.
  121. ...request.stdin !== undefined ? { stdin: request.stdin } : {},
  122. ...request.env !== undefined ? { env: request.env } : {},
  123. ...request.dshEnv !== undefined ? { dshEnv: request.dshEnv } : {},
  124. // Carry a sandbox policy through verbatim: this executor never
  125. // confines, so the field is inert here (the seam contract) — a
  126. // sandboxing subclass overrides resolve() to stamp its default instead.
  127. sandboxPolicy: request.sandboxPolicy,
  128. }
  129. }
  130. /** Map one resolved bash spec and explicit argv onto a fully-specified subprocess spawn. */
  131. // XXX(stateful-shell): evaluate persistent cwd or PTY sessions when workflows require shell state.
  132. private spawnSpec(
  133. spec: BashExecSpec,
  134. argv: readonly string[],
  135. stdoutMaxBytes: number,
  136. signal: AbortSignal | undefined,
  137. ): SubprocessSpawnSpec {
  138. const collect = (maxBytes: number): SubprocessCollect =>
  139. ({ maxBytes, spill: { maxBytes: this.config.maxSpillBytes } })
  140. return {
  141. argv,
  142. cwd: spec.workdir,
  143. stdio: {
  144. stdin: spec.stdin !== undefined ? { data: spec.stdin } : 'ignore',
  145. stdout: collect(stdoutMaxBytes),
  146. stderr: collect(this.config.maxOutputBytes),
  147. },
  148. graceMs: this.config.graceMs,
  149. signal,
  150. // One explicit env map for the seam, layered so the trusted dshEnv
  151. // snapshot beats both the caller's env and the terminal overrides; the
  152. // subprocess service merges the whole map after its ambient scrub.
  153. env: { ...ENV_OVERRIDES, ...spec.env, ...spec.dshEnv },
  154. }
  155. }
  156. /** The collect-mode readers the executor itself requested (present by construction). */
  157. private static collected(handle: SubprocessHandle): { stdout: SubprocessOutputReader; stderr: SubprocessOutputReader } {
  158. const { stdout, stderr } = handle.collected
  159. /* v8 ignore start -- collect dispositions expose both readers by the seam contract; defensive. */
  160. if (stdout === undefined || stderr === undefined) {
  161. throw new Error('bash-local: subprocess implementation dropped a requested collect stream')
  162. }
  163. /* v8 ignore stop */
  164. return { stdout, stderr }
  165. }
  166. async run(spec: BashExecSpec): Promise<BashRunResult> {
  167. return this.runArgv(spec, ['bash', '-c', spec.command])
  168. }
  169. /**
  170. * Run an explicit argv with the foreground lifecycle, environment, output,
  171. * timeout, and cancellation semantics of this executor. Subclasses use this
  172. * after replacing the public command's shell argv at an execution boundary.
  173. * @param spec - resolved execution settings and caller-owned command metadata.
  174. * @param argv - exact executable and arguments to hand to `ctx.subprocess`.
  175. * @returns the settled foreground result with collected output and cause facts.
  176. */
  177. protected async runArgv(spec: BashExecSpec, argv: readonly string[]): Promise<BashRunResult> {
  178. // One deadline combines timeout and upstream cancellation; disposal clears its timer.
  179. using d = deadline(spec.signal, spec.timeoutMs, 'BASH_TIMEOUT')
  180. const handle = this.ctx.subprocess.spawn(this.spawnSpec(spec, argv, spec.stdoutMaxBytes, d.signal))
  181. const outcome = await handle.done
  182. const collected = LocalBashExecutor.collected(handle)
  183. // Only this executor's timeout reason counts as timedOut; outer deadlines count as aborts.
  184. const timedOut = timeoutOf(d.signal, 'BASH_TIMEOUT') !== undefined
  185. const aborted = d.signal.aborted && !timedOut
  186. return {
  187. ...outcome,
  188. timedOut,
  189. aborted,
  190. timeoutMs: spec.timeoutMs,
  191. stdout: finalOutput(collected.stdout),
  192. stderr: finalOutput(collected.stderr),
  193. }
  194. }
  195. start(spec: BashExecSpec): BashProcess {
  196. return this.startArgv(spec, ['bash', '-c', spec.command])
  197. }
  198. /**
  199. * Start an explicit argv with the background lifecycle, environment, output,
  200. * cancellation, and process-tree ownership semantics of this executor.
  201. * Subclasses use this after replacing the public command's shell argv at an
  202. * execution boundary.
  203. * @param spec - resolved execution settings and caller-owned command metadata.
  204. * @param argv - exact executable and arguments to hand to `ctx.subprocess`.
  205. * @returns the live background handle; spawn rejection settles it as killed.
  206. */
  207. protected startArgv(spec: BashExecSpec, argv: readonly string[]): BashProcess {
  208. // Background runs ignore timeoutMs; callers stop them through kill() or spec.signal.
  209. const running = this.ctx.subprocess.spawn(this.spawnSpec(spec, argv, this.config.maxOutputBytes, spec.signal))
  210. const collected = LocalBashExecutor.collected(running)
  211. // A spawn failure produces no process output, so the subprocess service has nothing
  212. // to buffer; the note is delivered exactly once through the read path.
  213. let spawnFailureNote: string | undefined
  214. const consumeSpawnFailure = (): string => {
  215. const note = spawnFailureNote ?? ''
  216. spawnFailureNote = undefined
  217. return note
  218. }
  219. let stdoutOffset = 0
  220. let stderrOffset = 0
  221. const proc: BashProcess = {
  222. status: 'running',
  223. exitCode: null,
  224. signal: null,
  225. done: running.done.then((outcome) => {
  226. // Any signal termination is killed, including a command signaling itself.
  227. if (proc.status === 'running') {
  228. proc.status = spec.signal?.aborted === true || outcome.signal !== null ? 'killed' : 'completed'
  229. }
  230. proc.exitCode = outcome.exitCode
  231. proc.signal = outcome.signal
  232. this.onProcessDone(proc, collected.stderr.readFrom(0).text, false)
  233. }, (error: unknown) => {
  234. // Background spawn failures settle as killed and surface through the read path.
  235. proc.status = 'killed'
  236. spawnFailureNote = `spawn failed: ${String(error)}`
  237. this.onProcessDone(proc, spawnFailureNote, true, error)
  238. }),
  239. readOutput: (): BashProcessRead => {
  240. const out = collected.stdout.readFrom(stdoutOffset)
  241. const err = collected.stderr.readFrom(stderrOffset)
  242. stdoutOffset = out.nextOffset
  243. stderrOffset = err.nextOffset
  244. // A failed spawn never produced process output, so the note and real
  245. // stderr text are mutually exclusive.
  246. const errText = err.text.length > 0 ? err.text : consumeSpawnFailure()
  247. // Single newline between sections: stdout chunks usually end with one
  248. // already; add it only when missing.
  249. const separator = out.text.length > 0 && !out.text.endsWith('\n') ? '\n' : ''
  250. const delta = out.text
  251. + (errText.length > 0 ? `${separator}[stderr]\n${errText}` : '')
  252. return {
  253. delta,
  254. lossy: out.lossy || err.lossy,
  255. ...out.spillPath !== undefined ? { stdoutSpillPath: out.spillPath } : {},
  256. ...err.spillPath !== undefined ? { stderrSpillPath: err.spillPath } : {},
  257. }
  258. },
  259. kill: (): boolean => {
  260. if (proc.status !== 'running') return false
  261. proc.status = 'killed'
  262. running.terminate()
  263. return true
  264. },
  265. }
  266. return proc
  267. }
  268. /**
  269. * Settlement hook for subclasses that attach execution facts to a process.
  270. * Called after exit facts or spawn-failure output are stamped and before
  271. * {@link BashProcess.done} resolves. The base implementation is intentionally
  272. * empty.
  273. * @param _proc - the settled process handle.
  274. * @param _stderr - the process's retained stderr tail used by subclasses for settlement classification.
  275. * @param _spawnFailed - whether the subprocess promise rejected before a process started.
  276. * @param _spawnError - the original spawn rejection reason, which may itself be undefined.
  277. */
  278. protected onProcessDone(_proc: BashProcess, _stderr: string, _spawnFailed: boolean, _spawnError?: unknown): void {}
  279. }
  280. export default LocalBashExecutor