index.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /** Confined Node programs with host-owned bindings, output limits, and managed process cleanup. */
  2. import { stripTypeScriptTypes } from 'node:module'
  3. import { isAbsolute } from 'node:path'
  4. import { Context } from '@deepseek-ai/cordis'
  5. import z from '@deepseek-ai/schemastery'
  6. import { PtcRuntime } from '@deepseek-ai/dsh-ptc-runtime'
  7. import type { PtcBindingNamespace, PtcJsonValue, PtcRunFailure, PtcRunRequest, PtcRunResult, PtcRunSandbox, PtcRunSpec } from '@deepseek-ai/dsh-ptc-runtime'
  8. import { MAX_TIMER_DELAY_MS, clampTimeout } from '@deepseek-ai/dsh-timeout'
  9. import { SandboxUnavailableError, classifyRunnerFailure, isRunnerSpawnFailure } from '@deepseek-ai/dsh-sandbox'
  10. import type { ConfinedArgv, SandboxExecutionPolicy, SandboxMode } from '@deepseek-ai/dsh-sandbox'
  11. import type { SubprocessHandle, SubprocessOutcome } from '@deepseek-ai/dsh-subprocess'
  12. import type {} from '@deepseek-ai/dsh-sandbox-policy'
  13. import type {} from '@deepseek-ai/dsh-fs'
  14. import { snapshotJsonValue } from '@deepseek-ai/dsh-util-values'
  15. import { validateBindings } from './bindings.ts'
  16. import { JsonChannel } from './channel.ts'
  17. import { bootstrapArgs } from './launch.ts'
  18. import type { LaunchConfig } from './launch.ts'
  19. import { OutputLedger } from './output-ledger.ts'
  20. import { drainOutput } from './output-stream.ts'
  21. import { STARTUP_ENVIRONMENT_NAMES } from './environment.ts'
  22. import { decodePtcJsonWire, encodePtcJsonWire } from './json-wire.ts'
  23. import type { ProgramBootData } from './protocol.ts'
  24. /** Deployment-varying runtime bounds and launch choices. */
  25. export interface Config extends LaunchConfig {
  26. /** Default elapsed deadline, including nested tool and approval waits. */
  27. timeoutMs?: number
  28. /** Maximum numeric elapsed budget accepted by resolve. */
  29. maxTimeoutMs?: number
  30. /** Combined serialized logs, completion and diagnostic byte cap. */
  31. maxOutputBytes?: number
  32. /** V8 old-generation heap limit in MiB; native allocations are excluded. */
  33. maxOldGenerationSizeMb?: number
  34. /** Maximum control frame, outstanding argument and queued control-output bytes. */
  35. maxMessageBytes?: number
  36. /** Maximum simultaneous host binding calls accepted from a program. */
  37. maxPendingCalls?: number
  38. /** Managed process termination and output-drain grace in milliseconds. */
  39. graceMs?: number
  40. }
  41. type ResolvedConfig = Required<Omit<Config, 'bootstrapPath'>> & Pick<Config, 'bootstrapPath'>
  42. interface LiveRun { controller: AbortController; finished: Promise<void> }
  43. const STRIP_PREFIX = 'async function __dsh_program__() {\n'
  44. const STRIP_SUFFIX = '\n}'
  45. function messageOf(error: unknown): string { return error instanceof Error ? error.message : String(error) }
  46. function record(value: unknown): value is Record<string, unknown> { return typeof value === 'object' && value !== null && !Array.isArray(value) }
  47. /** Node provider; direct file effects use the same sandbox service as Bash. */
  48. export class NodePtcRuntime extends PtcRuntime {
  49. static inject = ['fs', 'subprocess', 'sandbox', 'sandboxPolicy']
  50. static Config: z<Config> = z.object({
  51. timeoutMs: z.number().default(120_000),
  52. maxTimeoutMs: z.number().default(600_000),
  53. maxOutputBytes: z.number().default(67_108_864),
  54. maxOldGenerationSizeMb: z.number().default(512),
  55. maxMessageBytes: z.number().default(134_217_728),
  56. maxPendingCalls: z.number().default(128),
  57. graceMs: z.number().default(3_000),
  58. nodeExecutable: z.string(),
  59. bootstrapPath: z.string(),
  60. })
  61. readonly language = 'typescript'
  62. readonly isolation = 'process'
  63. override get executionInstructions(): string {
  64. return 'Each call runs in a fresh Node process. Node APIs are available through await import(...). Relative paths use the supplied working directory; process.env starts empty. Direct filesystem access follows this execution\'s sandbox policy.'
  65. }
  66. private readonly config: ResolvedConfig
  67. private readonly live = new Set<LiveRun>()
  68. private disposed = false
  69. constructor(ctx: Context, config: Config) {
  70. super(ctx)
  71. this.config = { ...config, nodeExecutable: config.nodeExecutable ?? process.execPath } as ResolvedConfig
  72. for (const [key, value] of Object.entries(this.config)) {
  73. if (typeof value === 'number' && (!Number.isFinite(value) || value <= 0)) throw new Error(`ptc-runtime-node: ${key} must be positive and finite`)
  74. }
  75. for (const key of ['timeoutMs', 'maxTimeoutMs', 'graceMs'] as const) {
  76. if (this.config[key] > MAX_TIMER_DELAY_MS) throw new Error(`ptc-runtime-node: ${key} exceeds the supported timer range`)
  77. }
  78. if (!Number.isSafeInteger(this.config.maxOutputBytes) || this.config.maxOutputBytes < 4) throw new Error('ptc-runtime-node: maxOutputBytes must be an integer of at least 4')
  79. if (!Number.isSafeInteger(this.config.maxMessageBytes) || this.config.maxMessageBytes > 0xffff_ffff) throw new Error('ptc-runtime-node: maxMessageBytes must fit an unsigned 32-bit frame length')
  80. if (!Number.isSafeInteger(this.config.maxPendingCalls)) throw new Error('ptc-runtime-node: maxPendingCalls must be an integer')
  81. if (!Number.isSafeInteger(this.config.maxOldGenerationSizeMb)) throw new Error('ptc-runtime-node: maxOldGenerationSizeMb must be an integer')
  82. if (this.config.nodeExecutable.length === 0) throw new Error('ptc-runtime-node: nodeExecutable must be non-empty')
  83. if (this.config.bootstrapPath !== undefined && !isAbsolute(this.config.bootstrapPath)) throw new Error('ptc-runtime-node: bootstrapPath must be absolute')
  84. ctx.effect(() => async () => {
  85. this.disposed = true
  86. const active = [...this.live]
  87. for (const run of active) run.controller.abort('runtime disposed')
  88. await Promise.all(active.map(run => run.finished))
  89. }, 'Node ptc-runtime cleanup')
  90. }
  91. override get sandboxMode(): SandboxMode { return this.ctx.sandboxPolicy.defaultMode }
  92. override get timeout(): { defaultMs: number; maxMs: number } {
  93. return { defaultMs: Math.min(this.config.timeoutMs, this.config.maxTimeoutMs), maxMs: this.config.maxTimeoutMs }
  94. }
  95. /**
  96. * Resolve an execution under explicit or deployment policy.
  97. * @param request - Program, bindings, optional cwd/deadline, and resolved authority.
  98. * @returns Complete execution inputs with a capped numeric budget or an explicit null deadline.
  99. */
  100. resolve(request: PtcRunRequest): PtcRunSpec {
  101. if (this.disposed) throw new Error('ptc-runtime-node: resolve after disposal')
  102. const sandboxPolicy = request.sandboxPolicy ?? this.ctx.sandboxPolicy.resolve()
  103. const cwd = request.cwd ?? sandboxPolicy.workspaceRoot
  104. if (!isAbsolute(cwd)) throw new Error('ptc-runtime-node: cwd must be absolute')
  105. return {
  106. ...request,
  107. cwd,
  108. timeoutMs: request.timeoutMs === null ? null : clampTimeout(request.timeoutMs, this.config.timeoutMs, this.config.maxTimeoutMs, 'ptc-runtime-node: timeoutMs'),
  109. sandboxPolicy,
  110. }
  111. }
  112. /**
  113. * Run a resolved program in a fresh managed and confined Node process.
  114. * @param spec - Inputs returned by resolve; missing authority is caller misuse.
  115. * @returns Output and file-confinement facts after managed cleanup.
  116. */
  117. async run(spec: PtcRunSpec): Promise<PtcRunResult> {
  118. if (this.disposed) throw new Error('ptc-runtime-node: run after disposal')
  119. if (spec.sandboxPolicy === undefined) throw new Error('ptc-runtime-node: run requires a resolved sandbox policy')
  120. if (!isAbsolute(spec.cwd) || (spec.timeoutMs !== null && (!Number.isFinite(spec.timeoutMs) || spec.timeoutMs <= 0 || spec.timeoutMs > this.config.maxTimeoutMs))) throw new Error('ptc-runtime-node: run requires resolved cwd and timeout')
  121. const bindings = validateBindings(spec)
  122. const controller = new AbortController()
  123. const completion = Promise.withResolvers<void>()
  124. const live = { controller, finished: completion.promise }
  125. this.live.add(live)
  126. try {
  127. return await this.execute(spec, spec.sandboxPolicy, bindings, controller)
  128. } finally {
  129. this.live.delete(live)
  130. completion.resolve()
  131. }
  132. }
  133. private async execute(
  134. spec: PtcRunSpec,
  135. policy: SandboxExecutionPolicy,
  136. bindings: Map<string, PtcBindingNamespace>,
  137. controller: AbortController,
  138. ): Promise<PtcRunResult> {
  139. const output = new OutputLedger(this.config.maxOutputBytes)
  140. const logs: string[] = []
  141. const sandbox: PtcRunSandbox = { mode: policy.mode, denied: false }
  142. const result = Promise.withResolvers<PtcRunResult>()
  143. const signal = spec.signal === undefined ? controller.signal : AbortSignal.any([spec.signal, controller.signal])
  144. let handle: SubprocessHandle | undefined
  145. let channel: JsonChannel | undefined
  146. let confined: ConfinedArgv | undefined
  147. let settled = false
  148. let timedOut = false
  149. let outputOverflow = false
  150. let overflowResult: PtcRunResult | undefined
  151. let stderr = ''
  152. let parsing = true
  153. const wallTimer = spec.timeoutMs === null ? undefined
  154. : setTimeout(() => { timedOut = true; controller.abort('execution deadline reached') }, spec.timeoutMs)
  155. const finish = (failure?: PtcRunFailure, value?: PtcJsonValue): void => {
  156. if (settled) return
  157. settled = true
  158. clearTimeout(wallTimer)
  159. signal.removeEventListener('abort', onAbort)
  160. channel?.close()
  161. void (async () => {
  162. if (handle !== undefined) {
  163. try {
  164. handle.terminate()
  165. await Promise.all([handle.done.catch(() => {}), handle.waitForExit()])
  166. const drained = await Promise.all([
  167. drainOutput(handle.stdout, this.config.graceMs),
  168. drainOutput(handle.stderr, this.config.graceMs),
  169. ])
  170. if (drained.includes(false) && failure === undefined) {
  171. failure = { kind: 'worker-exit', message: 'Node process output did not close cleanly' }
  172. }
  173. } catch (error: unknown) {
  174. failure = { kind: 'worker-exit', message: `managed process cleanup failed: ${messageOf(error)}` }
  175. } finally {
  176. handle.stdout?.destroy()
  177. handle.stderr?.destroy()
  178. }
  179. }
  180. const outcome = outputOverflow ? overflowResult ?? output.limit(logs)
  181. : failure === undefined ? output.success(logs, value) : output.failure(logs, failure)
  182. result.resolve({ ...outcome, sandbox: { ...sandbox } })
  183. })()
  184. }
  185. const onAbort = (): void => {
  186. finish(timedOut
  187. ? { kind: 'timeout', message: `execution deadline reached (${spec.timeoutMs}ms)` }
  188. : { kind: 'abort', message: messageOf(signal.reason) })
  189. }
  190. signal.addEventListener('abort', onAbort, { once: true })
  191. if (signal.aborted) onAbort()
  192. try {
  193. // Abort callbacks can settle execution before or during an awaited operation.
  194. // oxlint-disable-next-line typescript/no-unnecessary-condition
  195. if (settled) return await result.promise
  196. const stripped = stripTypeScriptTypes(STRIP_PREFIX + spec.program + STRIP_SUFFIX)
  197. parsing = false
  198. const data: ProgramBootData = {
  199. code: stripped.slice(STRIP_PREFIX.length, stripped.length - STRIP_SUFFIX.length),
  200. namespaces: [...bindings.values()].map(binding => ({
  201. global: binding.global,
  202. names: Object.keys(binding.functions),
  203. ...binding.errorClass ? { errorClass: binding.errorClass } : {},
  204. })),
  205. maxOutputBytes: this.config.maxOutputBytes,
  206. }
  207. const executable = await this.ctx.subprocess.resolveExecutable(this.config.nodeExecutable, undefined, signal)
  208. // Abort callbacks can settle execution before or during an awaited operation.
  209. // oxlint-disable-next-line typescript/no-unnecessary-condition
  210. if (settled) return await result.promise
  211. const packaged = 'pkg' in process && this.config.bootstrapPath === undefined
  212. const heapFlag = `--max-old-space-size=${this.config.maxOldGenerationSizeMb}`
  213. const argv = [executable, ...packaged ? [] : [heapFlag], ...bootstrapArgs(this.ctx.fs, this.config, this.config.maxMessageBytes)]
  214. confined = policy.mode === 'danger-full-access' ? undefined : await this.ctx.sandbox.confine(argv, { ...policy, mode: policy.mode }, signal)
  215. // oxlint-disable-next-line typescript/no-unnecessary-condition -- Cancellation can settle during awaited confinement.
  216. if (settled) return await result.promise
  217. if (confined !== undefined) sandbox.enforcement = confined.enforcement
  218. // Electron needs its Node-mode selector until bootstrap; the child then removes it with other ambient values.
  219. const env: NodeJS.ProcessEnv = Object.fromEntries(Object.keys(process.env)
  220. .filter(key => !STARTUP_ENVIRONMENT_NAMES.has(key.toUpperCase()) && key.toUpperCase() !== 'ELECTRON_RUN_AS_NODE')
  221. .map(key => [key, undefined]))
  222. if (packaged) {
  223. env.DSH_PTC_RUNTIME_NODE = '1'
  224. env.NODE_OPTIONS = heapFlag
  225. }
  226. handle = this.ctx.subprocess.spawn({ argv: confined?.argv ?? argv, cwd: spec.cwd, env, stdio: { stdin: 'ignore', stdout: 'pipe', stderr: 'pipe', control: 'pipe' }, graceMs: this.config.graceMs, signal })
  227. const launched = handle
  228. if (launched.control === undefined || launched.stdout === undefined || launched.stderr === undefined) {
  229. throw new Error('subprocess provider did not supply the requested control and output pipes')
  230. }
  231. const admit = (text: string): void => {
  232. if (outputOverflow) return
  233. if (!output.admit(text, logs)) {
  234. outputOverflow = true
  235. overflowResult = output.limit([...logs, text])
  236. finish({ kind: 'output-limit', message: `outer output exceeded ${this.config.maxOutputBytes} bytes` })
  237. }
  238. }
  239. const stdoutDecoder = new TextDecoder('utf-8', { ignoreBOM: true })
  240. const stderrDecoder = new TextDecoder('utf-8', { ignoreBOM: true })
  241. handle.stdout?.on('data', (chunk: Buffer) => {
  242. const text = stdoutDecoder.decode(chunk, { stream: true })
  243. if (text.length > 0) admit(text)
  244. })
  245. handle.stdout?.on('end', () => {
  246. const text = stdoutDecoder.decode()
  247. if (text.length > 0) admit(text)
  248. })
  249. handle.stdout?.on('error', (error: Error) => { finish({ kind: 'worker-exit', message: messageOf(error) }) })
  250. handle.stderr?.on('data', (chunk: Buffer) => {
  251. const text = stderrDecoder.decode(chunk, { stream: true })
  252. stderr = (stderr + text).slice(-this.config.maxOutputBytes)
  253. if (text.length > 0) admit(text)
  254. })
  255. handle.stderr?.on('end', () => {
  256. const text = stderrDecoder.decode()
  257. if (text.length > 0) admit(text)
  258. })
  259. handle.stderr?.on('error', (error: Error) => { finish({ kind: 'worker-exit', message: messageOf(error) }) })
  260. let ready = false
  261. let nextId = 1
  262. let pending = 0
  263. let pendingBytes = 0
  264. const protocolFailure = (message: string): void => { finish({ kind: 'protocol', message }) }
  265. const processFinished = (outcome: SubprocessOutcome): void => {
  266. if (settled) return
  267. finish((confined !== undefined && classifyRunnerFailure(outcome.exitCode, stderr, confined.runnerFailureRules) !== undefined)
  268. ? { kind: 'sandbox-unavailable', message: stderr }
  269. : { kind: 'worker-exit', message: `Node process exited before completing (${String(outcome.exitCode)})${stderr ? `: ${stderr}` : ''}` })
  270. }
  271. const transport: JsonChannel = new JsonChannel(launched.control, this.config.maxMessageBytes, (raw, bytes) => {
  272. if (!record(raw)) { protocolFailure('invalid control frame'); return }
  273. if (!ready) {
  274. if (raw.type !== 'ready') { protocolFailure('program frame arrived before bootstrap readiness'); return }
  275. ready = true
  276. void transport.send({ type: 'boot', data }).catch((error: unknown) => { protocolFailure(messageOf(error)) })
  277. return
  278. }
  279. switch (raw.type) {
  280. case 'log':
  281. if (typeof raw.text !== 'string') { protocolFailure('invalid log frame'); return }
  282. admit(raw.text)
  283. return
  284. case 'output-limit': outputOverflow = true; finish(); return
  285. case 'done': {
  286. if (raw.error !== undefined) {
  287. if (!record(raw.error) || typeof raw.error.message !== 'string' || (raw.error.kind !== 'exception' && raw.error.kind !== 'invalid-output' && raw.error.kind !== 'output-limit')) { protocolFailure('invalid terminal error'); return }
  288. const failure = { kind: raw.error.kind, message: raw.error.message } as PtcRunFailure
  289. if (confined !== undefined) {
  290. sandbox.denied = confined.denialSignatures.some(signature =>
  291. failure.message.toLowerCase().includes(signature.toLowerCase()))
  292. }
  293. if (failure.kind === 'output-limit') outputOverflow = true
  294. finish(failure)
  295. return
  296. }
  297. const value = raw.value === undefined ? undefined : decodePtcJsonWire(raw.value)
  298. if (raw.value !== undefined && value === undefined) finish({ kind: 'invalid-output', message: 'program completion must be lossless JSON' })
  299. else finish(undefined, value)
  300. return
  301. }
  302. case 'call': {
  303. if (!Number.isSafeInteger(raw.id) || raw.id !== nextId || typeof raw.global !== 'string' || typeof raw.name !== 'string') { protocolFailure('invalid binding call identity'); return }
  304. nextId += 1
  305. const functions = bindings.get(raw.global)?.functions
  306. const fn = functions !== undefined && Object.hasOwn(functions, raw.name) ? functions[raw.name] : undefined
  307. if (typeof fn !== 'function') { protocolFailure('program requested an undeclared binding'); return }
  308. const args = decodePtcJsonWire(raw.args)
  309. if (args === undefined) { protocolFailure('binding arguments must be lossless JSON'); return }
  310. if (++pending > this.config.maxPendingCalls || (pendingBytes += bytes) > this.config.maxMessageBytes) { protocolFailure('pending binding calls exceed configured limits'); return }
  311. const id = raw.id
  312. void (async () => {
  313. let reply: unknown
  314. try {
  315. const value = snapshotJsonValue(await fn(args))
  316. if (value === undefined) throw new Error('binding resolution must be lossless JSON')
  317. reply = { type: 'reply', id, ok: true, value: encodePtcJsonWire(value) }
  318. } catch (error: unknown) {
  319. reply = { type: 'reply', id, ok: false, message: messageOf(error) }
  320. } finally {
  321. pending -= 1
  322. pendingBytes -= bytes
  323. }
  324. if (!settled) await transport.send(reply)
  325. })().catch((error: unknown) => { protocolFailure(messageOf(error)) })
  326. return
  327. }
  328. default: protocolFailure('unknown control message')
  329. }
  330. }, (error, kind) => {
  331. if (kind === 'protocol') protocolFailure(messageOf(error))
  332. else if (ready) finish({ kind: 'worker-exit', message: messageOf(error) })
  333. else void launched.done.then(processFinished, (failure: unknown) => { finish({ kind: 'worker-exit', message: messageOf(failure) }) })
  334. })
  335. channel = transport
  336. void launched.done.then((outcome) => {
  337. // Allow queued control-frame callbacks to run before classifying a command exit.
  338. setImmediate(() => { processFinished(outcome) })
  339. }, (error: unknown) => { finish({ kind: confined !== undefined && isRunnerSpawnFailure(error, confined.argv[0], spec.cwd) ? 'sandbox-unavailable' : 'worker-exit', message: messageOf(error) }) })
  340. } catch (error: unknown) {
  341. finish({ kind: error instanceof SandboxUnavailableError ? 'sandbox-unavailable' : parsing ? 'exception' : 'worker-exit', message: messageOf(error) })
  342. }
  343. return await result.promise
  344. }
  345. }
  346. export default NodePtcRuntime