| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399 |
- import { once } from 'node:events'
- import { Context } from 'cordis'
- import {
- CommandExitError,
- FileNotFoundError,
- type CommandHandle,
- type CommandResult,
- type Sandbox,
- } from '@deepseek-ai/dsh-e2b'
- import type E2BSandboxService from '@deepseek-ai/dsh-e2b'
- import type { SubprocessSpawnSpec } from '@deepseek-ai/dsh-subprocess'
- import E2BSubprocessService from '@deepseek-ai/dsh-subprocess-e2b'
- import * as E2BSubprocessInvariant from '../src/invariant.ts'
- import { E2BBase64Decoder, E2B_OUTPUT_COMPLETE_FRAME, E2BOutputReader } from '../src/output.ts'
- import { E2BSubprocessHandle } from '../src/process.ts'
- import InvariantService from '@deepseek-ai/dsh-invariants'
- import { describe, expect, it, vi } from 'vitest'
- function commandError(exitCode: number): CommandExitError {
- return new CommandExitError({ exitCode, stdout: '', stderr: '', error: `exit ${exitCode}` })
- }
- interface StartOptions {
- background: true
- cwd: string
- stdin: boolean
- timeoutMs: number
- signal?: AbortSignal
- envs?: Record<string, string>
- onStdout?: (data: string) => void | Promise<void>
- onStderr?: (data: string) => void | Promise<void>
- }
- class FakeCommandHandle {
- pid = 4242
- readonly sent: Array<string | Uint8Array> = []
- closes = 0
- kills = 0
- disconnects = 0
- killError: unknown
- disconnectError: unknown
- private readonly result = Promise.withResolvers<CommandResult>()
- private settled = false
- constructor(private readonly onKill: () => void = () => {}) {}
- wait(): Promise<CommandResult> {
- return this.result.promise
- }
- async sendStdin(data: string | Uint8Array): Promise<void> {
- this.sent.push(data)
- }
- async closeStdin(): Promise<void> {
- this.closes += 1
- }
- async kill(): Promise<boolean> {
- this.kills += 1
- if (this.killError !== undefined) throw this.killError
- this.onKill()
- return true
- }
- async disconnect(): Promise<void> {
- this.disconnects += 1
- if (this.disconnectError !== undefined) throw this.disconnectError
- }
- succeed(exitCode = 0): void {
- if (this.settled) return
- this.settled = true
- this.result.resolve({ exitCode, stdout: '', stderr: '' })
- }
- fail(exitCode: number): void {
- if (this.settled) return
- this.settled = true
- this.result.reject(commandError(exitCode))
- }
- crash(error: unknown): void {
- if (this.settled) return
- this.settled = true
- this.result.reject(error)
- }
- }
- class FakeSandbox {
- readonly handle: FakeCommandHandle
- readonly commandsSeen: string[] = []
- readonly writtenFiles: string[][] = []
- readonly writtenFileData = new Map<string, string>()
- readonly removed: string[] = []
- readonly directories: string[] = []
- startOptions: StartOptions | undefined
- backgroundError: unknown
- envError: unknown
- statusError: unknown
- nextRemoveError: unknown
- probeError: unknown
- signalError: unknown
- readonly signalErrors: unknown[] = []
- trapsTerm = false
- delaysKill = false
- delaysKillCompletion = false
- sdkKillStops = true
- alive = true
- zombieOnly = false
- ambient = 'PATH=/ambient/bin\0KEEP=safe\0NPM_TOKEN=secret\0DSH_STALE=old\0BROKEN\0=bad\0'
- processGroupId = '4242\n'
- exitStatus = ''
- readonly processGroupReads: string[] = []
- afterStatusRead: (() => void) | undefined
- beforeProbe: (() => void) | undefined
- afterProbe: (() => void) | undefined
- private startGate: Promise<void> | undefined
- private openStart: (() => void) | undefined
- private processGroupReadGate: Promise<void> | undefined
- private openProcessGroupRead: (() => void) | undefined
- private signalGate: Promise<void> | undefined
- private openSignal: (() => void) | undefined
- constructor() {
- this.handle = new FakeCommandHandle(() => {
- if (this.sdkKillStops) {
- this.alive = false
- this.handle.fail(137)
- }
- })
- }
- deferStart(): void {
- const gate = Promise.withResolvers<undefined>()
- this.startGate = gate.promise
- this.openStart = () => { gate.resolve(undefined) }
- }
- releaseStart(): void {
- this.openStart?.()
- }
- deferProcessGroupRead(): void {
- const gate = Promise.withResolvers<undefined>()
- this.processGroupReadGate = gate.promise
- this.openProcessGroupRead = () => { gate.resolve(undefined) }
- }
- releaseProcessGroupRead(): void {
- this.openProcessGroupRead?.()
- }
- deferSignals(): void {
- const gate = Promise.withResolvers<undefined>()
- this.signalGate = gate.promise
- this.openSignal = () => { gate.resolve(undefined) }
- }
- releaseSignals(): void {
- this.openSignal?.()
- }
- finish(exitCode = 0): void {
- this.alive = false
- void this.completeOutput().then(
- () => {
- if (exitCode === 0) this.handle.succeed(0)
- else this.handle.fail(exitCode)
- },
- (error: unknown) => { this.handle.crash(error) },
- )
- }
- async completeOutput(): Promise<void> {
- await Promise.all([
- this.stdoutWire(`${E2B_OUTPUT_COMPLETE_FRAME}\n`),
- this.stderrWire(`${E2B_OUTPUT_COMPLETE_FRAME}\n`),
- ])
- }
- async stdout(data: string): Promise<void> {
- await this.stdoutWire(data.length === 0 ? '' : `${Buffer.from(data).toString('base64')}\n`)
- }
- async stderr(data: string): Promise<void> {
- await this.stderrWire(data.length === 0 ? '' : `${Buffer.from(data).toString('base64')}\n`)
- }
- async stdoutWire(data: string): Promise<void> {
- await this.startOptions?.onStdout?.(data)
- }
- async stderrWire(data: string): Promise<void> {
- await this.startOptions?.onStderr?.(data)
- }
- readonly sandbox = {
- sandboxId: 'fake',
- files: {
- makeDir: async (path: string): Promise<boolean> => {
- this.directories.push(path)
- return true
- },
- write: async (files: Array<{ path: string; data: string }>): Promise<object[]> => {
- this.writtenFiles.push(files.map(file => file.path))
- for (const file of files) this.writtenFileData.set(file.path, file.data)
- return files.map(() => ({}))
- },
- read: async (path: string): Promise<string> => {
- if (!path.endsWith('/exit-code')) {
- await this.processGroupReadGate
- return this.processGroupReads.shift() ?? this.processGroupId
- }
- if (this.statusError !== undefined) {
- const error = this.statusError
- this.statusError = undefined
- throw error
- }
- this.afterStatusRead?.()
- return this.exitStatus
- },
- remove: async (path: string): Promise<void> => {
- this.removed.push(path)
- if (this.nextRemoveError !== undefined) {
- const error = this.nextRemoveError
- this.nextRemoveError = undefined
- throw error
- }
- },
- },
- commands: {
- run: async (command: string, options?: StartOptions | { signal?: AbortSignal }): Promise<CommandHandle | CommandResult> => {
- this.commandsSeen.push(command)
- if (command === 'env -0') {
- if (this.envError !== undefined) throw this.envError
- return { exitCode: 0, stdout: this.ambient, stderr: '' }
- }
- if (command.startsWith('set -o pipefail; ps -eo pgid=,stat=')) {
- this.beforeProbe?.()
- if (options?.signal?.aborted === true) throw new DOMException('aborted', 'AbortError')
- if (this.probeError !== undefined) {
- const error = this.probeError
- this.probeError = undefined
- throw error
- }
- const stdout = this.alive && !this.zombieOnly ? 'live\n' : ''
- this.afterProbe?.()
- return { exitCode: 0, stdout, stderr: '' }
- }
- if (command.startsWith('kill -TERM ')) {
- await this.signalGate
- const error = this.signalErrors.shift() ?? this.signalError
- if (error !== undefined) {
- if (this.signalErrors.length === 0) this.signalError = undefined
- throw error
- }
- if (!this.trapsTerm) {
- this.alive = false
- this.handle.fail(143)
- }
- return { exitCode: 0, stdout: '', stderr: '' }
- }
- if (command.startsWith('kill -KILL ')) {
- await this.signalGate
- const error = this.signalErrors.shift() ?? this.signalError
- if (error !== undefined) {
- if (this.signalErrors.length === 0) this.signalError = undefined
- throw error
- }
- if (!this.delaysKill) this.alive = false
- if (!this.delaysKillCompletion) this.handle.fail(137)
- return { exitCode: 0, stdout: '', stderr: '' }
- }
- if ((options as StartOptions | undefined)?.background === true) {
- this.startOptions = options as StartOptions
- await this.startGate
- if (this.backgroundError !== undefined) throw this.backgroundError
- return this.handle as unknown as CommandHandle
- }
- return { exitCode: 0, stdout: '', stderr: '' }
- },
- },
- } as unknown as Sandbox
- }
- function spec(overrides: Partial<SubprocessSpawnSpec> = {}): SubprocessSpawnSpec {
- return {
- argv: ['bash', '-c', 'printf ok'],
- cwd: '/workspace',
- stdio: {
- stdin: 'ignore',
- stdout: { maxBytes: 4, spill: { maxBytes: 16 } },
- stderr: { maxBytes: 4 },
- },
- graceMs: 5,
- ...overrides,
- }
- }
- function runtime(fake: FakeSandbox, getSandbox: () => Promise<Sandbox> = async () => fake.sandbox): E2BSandboxService {
- return {
- cwd: '/workspace',
- runtimeRoot: '/workspace/.dsh-e2b',
- disposeMode: 'kill',
- getSandbox,
- } as unknown as E2BSandboxService
- }
- async function flush(): Promise<void> {
- await new Promise(resolve => setTimeout(resolve, 0))
- }
- describe('E2BOutputReader', () => {
- it('decodes base64 across arbitrary callback boundaries and rejects malformed framing', () => {
- const decoder = new E2BBase64Decoder()
- expect(decoder.push('')).toEqual(Buffer.alloc(0))
- expect(decoder.push('5')).toEqual(Buffer.alloc(0))
- expect(decoder.push('L2')).toEqual(Buffer.alloc(0))
- expect(decoder.push('g\n').toString()).toBe('你')
- expect(decoder.push('YQ==\nYg==\n').toString()).toBe('ab')
- expect(decoder.push(`${Buffer.from([0, 255]).toString('base64')}\n`)).toEqual(Buffer.from([0, 255]))
- expect(decoder.push(`${E2B_OUTPUT_COMPLETE_FRAME}\n`)).toEqual(Buffer.alloc(0))
- decoder.finish()
- expect(() => new E2BBase64Decoder().push('%\n')).toThrow('invalid base64')
- expect(() => new E2BBase64Decoder().push('AB==\n')).toThrow('invalid base64')
- expect(() => decoder.push(`${E2B_OUTPUT_COMPLETE_FRAME}\n`)).toThrow('duplicate output transport completion')
- expect(() => decoder.push('YQ==\n')).toThrow('continued after completion')
- const truncated = new E2BBase64Decoder()
- truncated.push('YQ')
- expect(() => { truncated.finish() }).toThrow('truncated base64')
- expect(() => { new E2BBase64Decoder().finish() }).toThrow('incomplete output transport')
- const interrupted = new E2BBase64Decoder()
- interrupted.push('YQ')
- expect(() => { interrupted.finish(false) }).not.toThrow()
- })
- it('keeps a byte-exact tail with independent whole-stream cursors', () => {
- const reader = new E2BOutputReader(4, 10, '/remote/spill')
- reader.push(Buffer.alloc(0))
- reader.push(Buffer.from('ab'))
- reader.push(Buffer.from('cdef'))
- expect(reader.size).toBe(6)
- expect(reader.readFrom(0)).toEqual({ text: 'cdef', nextOffset: 6, lossy: true, spillPath: '/remote/spill' })
- expect(reader.readFrom(2)).toEqual({ text: 'cdef', nextOffset: 6, lossy: false })
- expect(reader.readFrom(5)).toEqual({ text: 'f', nextOffset: 6, lossy: false })
- expect(reader.readFrom(99)).toEqual({ text: '', nextOffset: 6, lossy: false })
- reader.invalidateSpill()
- expect(reader.readFrom(0)).toEqual({ text: 'cdef', nextOffset: 6, lossy: true })
- })
- it('drops whole head chunks and withholds absent or over-cap spills', () => {
- const withoutSpill = new E2BOutputReader(2, undefined, '/unused')
- withoutSpill.push(Buffer.from('ab'))
- withoutSpill.push(Buffer.from('cd'))
- expect(withoutSpill.readFrom(0)).toEqual({ text: 'cd', nextOffset: 4, lossy: true })
- const overCap = new E2BOutputReader(2, 3, '/too-small')
- overCap.push(Buffer.from('abcd'))
- expect(overCap.readFrom(0)).toEqual({ text: 'cd', nextOffset: 4, lossy: true })
- expect(() => overCap.readFrom(-1)).toThrow(/non-negative safe integer/)
- expect(() => overCap.readFrom(1.5)).toThrow(/non-negative safe integer/)
- })
- })
- describe('E2BSubprocessHandle', () => {
- it('starts asynchronously, keeps secrets out of the command, and supports deferred piped stdin/output', async () => {
- const fake = new FakeSandbox()
- fake.processGroupId = '4343\n'
- fake.deferStart()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- argv: ['tool', 'argument with spaces'],
- stdio: { stdin: 'pipe', stdout: 'pipe', stderr: { maxBytes: 8, spill: { maxBytes: 32 } } },
- env: { PATH: '/bin', 'FOO-BAR': 'hyphen-value', DEEPSEEK_API_KEY: 'explicit-secret', DSH_MODE: 'test' },
- }), '/workspace/.dsh-e2b/processes/one')
- expect(handle.pid).toBe(-1)
- handle.stdin!.write('hello')
- handle.stdin!.end()
- fake.releaseStart()
- await flush()
- expect(handle.pid).toBe(4343)
- expect(fake.handle.sent.map(value => String(value))).toEqual(['hello'])
- expect(fake.handle.closes).toBe(1)
- expect(fake.startOptions?.envs).toBeUndefined()
- const command = fake.commandsSeen.find(value => value.includes('exec "$dsh_e2b_env_bin" -i'))!
- expect(command).toContain('"$dsh_e2b_setsid" --wait -- "$dsh_e2b_bash" -c')
- expect(command).not.toContain('DEEPSEEK_API_KEY')
- expect(command).not.toContain('DSH_MODE')
- expect(command).not.toContain('FOO-BAR')
- expect(command).not.toContain('explicit-secret')
- expect(command).not.toContain('hyphen-value')
- expect(command).not.toContain('${!dsh_e2b_name}')
- expect(fake.commandsSeen).toContain('env -0')
- expect(command).toContain('mapfile -d')
- expect(command).toContain('dsh_e2b_node="$(command -v node)"')
- expect(command).toContain('"$dsh_e2b_env_bin" -i "$dsh_e2b_node" -e')
- expect(command).toContain('exec "$dsh_e2b_env_bin" -i "${dsh_e2b_env[@]}"')
- expect(command).toContain('>&2 2>/dev/null')
- expect(command).not.toContain('2>/dev/null >&2')
- expect(command).toContain('base64')
- expect(fake.writtenFiles[0]).toEqual([
- '/workspace/.dsh-e2b/processes/one/pid',
- '/workspace/.dsh-e2b/processes/one/exit-code',
- '/workspace/.dsh-e2b/processes/one/environment',
- '/workspace/.dsh-e2b/processes/one/stderr.log',
- ])
- expect(fake.writtenFileData.get('/workspace/.dsh-e2b/processes/one/environment')).toBe(
- 'PATH=/bin\0KEEP=safe\0FOO-BAR=hyphen-value\0DEEPSEEK_API_KEY=explicit-secret\0DSH_MODE=test\0',
- )
- let piped = ''
- handle.stdout!.on('data', (chunk) => { piped += String(chunk) })
- await fake.stdout('pipe-data')
- await fake.stderr('err')
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- expect(piped).toBe('pipe-data')
- expect(handle.collected.stderr!.readFrom(0)).toMatchObject({ text: 'err', lossy: false })
- expect(fake.removed).toContain('/workspace/.dsh-e2b/processes/one/stderr.log')
- await expect(handle.waitForExit()).resolves.toBe(true)
- })
- it('rejects malformed environment entries before command start', async () => {
- for (const env of [{ 'BAD=NAME': 'x' }, { BAD: 'x\0INJECTED=1' }]) {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ env }), '/runtime/invalid-environment')
- await expect(handle.done).rejects.toThrow('environment entries')
- expect(fake.startOptions).toBeUndefined()
- expect(fake.removed).toContain('/runtime/invalid-environment')
- }
- })
- it('preserves UTF-8 bytes when the ASCII transport is split across callbacks', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- stdio: { stdin: 'ignore', stdout: 'pipe', stderr: { maxBytes: 4 } },
- }), '/runtime/split-utf8')
- await flush()
- const chunks: Buffer[] = []
- handle.stdout!.on('data', (chunk: Buffer) => { chunks.push(chunk) })
- for (const character of `${Buffer.from('A你好B').toString('base64')}\n`) {
- await fake.stdoutWire(character)
- }
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- expect(Buffer.concat(chunks).toString('utf8')).toBe('A你好B')
- })
- it('rejects malformed output transport without confusing it with a consumer sink failure', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/malformed-output')
- await flush()
- await fake.stdoutWire('%\n')
- fake.finish()
- await expect(handle.done).rejects.toThrow('invalid base64 output transport')
- const stderrFake = new FakeSandbox()
- const stderrHandle = new E2BSubprocessHandle(runtime(stderrFake), spec(), '/runtime/malformed-stderr')
- await flush()
- await stderrFake.stderrWire('%\n')
- stderrFake.finish()
- await expect(stderrHandle.done).rejects.toThrow('invalid base64 output transport')
- })
- it('rejects a naturally completed command whose encoder omits its completion frame', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/incomplete-output')
- await flush()
- fake.alive = false
- fake.handle.succeed(0)
- await expect(handle.done).rejects.toThrow('incomplete output transport')
- })
- it('bounds descendant-held output draining and withholds the incomplete spill', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ graceMs: 5 }), '/runtime/drain-bound')
- await flush()
- await fake.stdout('leader-output')
- fake.exitStatus = '0\n'
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- expect(fake.handle.disconnects).toBe(1)
- expect(handle.collected.stdout?.readFrom(0)).toEqual({
- text: 'tput',
- nextOffset: 13,
- lossy: true,
- })
- expect(fake.removed).toContain('/runtime/drain-bound/stdout.log')
- handle.terminate()
- await expect(handle.waitForExit()).resolves.toBe(true)
- })
- it('waits for lossless raw-pipe output after the direct status is published', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- graceMs: 1,
- stdio: { stdin: 'ignore', stdout: 'pipe', stderr: { maxBytes: 4 } },
- }), '/runtime/pipe-drain')
- let output = ''
- handle.stdout!.on('data', (chunk) => { output += String(chunk) })
- await flush()
- fake.exitStatus = '0\n'
- let settled = false
- void handle.done.then(() => { settled = true })
- await new Promise(resolve => setTimeout(resolve, 50))
- expect(settled).toBe(false)
- expect(fake.handle.disconnects).toBe(0)
- await fake.stdout('complete protocol frame')
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- expect(output).toBe('complete protocol frame')
- })
- it('accepts clean encoder completion inside the output-drain grace', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ graceMs: 100 }), '/runtime/drain-complete')
- await flush()
- fake.exitStatus = '0\n'
- fake.afterStatusRead = () => {
- fake.afterStatusRead = undefined
- setTimeout(() => { fake.finish() }, 0)
- }
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- expect(fake.handle.disconnects).toBe(0)
- })
- it('preserves a published exit code when requested termination outlives output draining', async () => {
- const fake = new FakeSandbox()
- fake.trapsTerm = true
- fake.delaysKill = true
- fake.delaysKillCompletion = true
- fake.sdkKillStops = false
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ graceMs: 5 }), '/runtime/drain-signal')
- await flush()
- handle.terminate()
- await vi.waitFor(() => { expect(fake.commandsSeen).toContain('kill -KILL -- -4242') })
- fake.exitStatus = '0\n'
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- expect(fake.handle.disconnects).toBe(1)
- fake.alive = false
- await expect(handle.waitForExit()).resolves.toBe(true)
- })
- it('rejects an invalid direct-command exit status', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/invalid-status')
- await flush()
- fake.exitStatus = '999\n'
- await expect(handle.done).rejects.toThrow('invalid exit code')
- handle.terminate()
- await expect(handle.waitForExit()).resolves.toBe(true)
- })
- it('rolls back a published process group before rejecting a monitoring failure', async () => {
- const fake = new FakeSandbox()
- fake.statusError = new Error('status transport failed')
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/status-failure')
- await expect(handle.done).rejects.toThrow('status transport failed')
- expect(fake.commandsSeen).toContain('kill -TERM -- -4242')
- expect(fake.alive).toBe(false)
- await expect(handle.waitForExit()).resolves.toBe(true)
- const failed = new FakeSandbox()
- failed.statusError = new Error('status transport failed')
- failed.signalErrors.push(new Error('TERM transport failed'), new Error('KILL transport failed'))
- failed.handle.killError = new Error('SDK kill failed')
- const retained = new E2BSubprocessHandle(runtime(failed), spec({ graceMs: 1 }), '/runtime/status-cleanup-failure')
- await expect(retained.done).rejects.toThrow(
- 'command monitoring failed and process-group rollback did not reach quiescence',
- )
- expect(failed.alive).toBe(true)
- failed.handle.killError = undefined
- retained.terminate()
- await expect(retained.waitForExit()).resolves.toBe(true)
- })
- it('surfaces deferred piped-stdin write and close failures as stream errors', async () => {
- const writeFake = new FakeSandbox()
- writeFake.deferStart()
- vi.spyOn(writeFake.handle, 'sendStdin').mockRejectedValueOnce('stdin rejected')
- const writeHandle = new E2BSubprocessHandle(runtime(writeFake), spec({
- stdio: { stdin: 'pipe', stdout: { maxBytes: 4 }, stderr: { maxBytes: 4 } },
- }), '/runtime/stdin-write-error')
- const writeError = once(writeHandle.stdin!, 'error')
- writeHandle.stdin!.write('input')
- writeFake.releaseStart()
- await expect(writeError).resolves.toMatchObject([{ message: 'stdin rejected' }])
- writeFake.finish()
- await writeHandle.done
- const closeFake = new FakeSandbox()
- vi.spyOn(closeFake.handle, 'closeStdin').mockRejectedValueOnce(new Error('close rejected'))
- const closeHandle = new E2BSubprocessHandle(runtime(closeFake), spec({
- stdio: { stdin: 'pipe', stdout: { maxBytes: 4 }, stderr: { maxBytes: 4 } },
- }), '/runtime/stdin-close-error')
- await flush()
- const closeError = once(closeHandle.stdin!, 'error')
- closeHandle.stdin!.end()
- await expect(closeError).resolves.toMatchObject([{ message: 'close rejected' }])
- closeFake.finish()
- await closeHandle.done
- })
- it('collects bounded tails, retains valid spills, and maps natural nonzero exits', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- stdio: {
- stdin: { data: 'batch' },
- stdout: { maxBytes: 4, spill: { maxBytes: 16 } },
- stderr: { maxBytes: 3 },
- },
- }), '/runtime/two')
- await flush()
- await fake.stdout('abcdef')
- await fake.stderr('12345')
- fake.finish(7)
- await expect(handle.done).resolves.toEqual({ exitCode: 7, signal: null })
- expect(fake.handle.sent).toEqual(['batch'])
- expect(fake.handle.closes).toBe(1)
- expect(handle.collected.stdout!.readFrom(0)).toEqual({
- text: 'cdef',
- nextOffset: 6,
- lossy: true,
- spillPath: '/runtime/two/stdout.log',
- })
- expect(handle.collected.stderr!.readFrom(0)).toEqual({ text: '345', nextOffset: 5, lossy: true })
- expect(fake.removed).not.toContain('/runtime/two/stdout.log')
- })
- it('removes a spill once the complete stream exceeds its cap', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- stdio: { stdin: 'ignore', stdout: { maxBytes: 2, spill: { maxBytes: 3 } }, stderr: 'inherit' },
- }), '/runtime/oversize')
- await flush()
- await fake.stdout('abcd')
- await fake.stderr('')
- fake.finish()
- await handle.done
- expect(handle.collected.stdout!.readFrom(0)).toEqual({ text: 'cd', nextOffset: 4, lossy: true })
- expect(fake.removed).toContain('/runtime/oversize/stdout.log')
- const command = fake.commandsSeen.find(value => value.includes('dsh_e2b_tee='))!
- expect(command).toContain('"$dsh_e2b_head" -c 3')
- expect(command).toContain('/runtime/oversize/stdout.log')
- expect(command).toContain('"$dsh_e2b_tee" --output-error=warn-nopipe')
- expect(command).not.toContain('tee -a')
- })
- it('contains remote spill-removal failures and routes empty inherited output', async () => {
- const fake = new FakeSandbox()
- fake.nextRemoveError = new Error('already removed')
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- stdio: { stdin: 'ignore', stdout: 'inherit', stderr: { maxBytes: 4, spill: { maxBytes: 8 } } },
- }), '/runtime/remove-error')
- await flush()
- await fake.stdout('')
- await fake.stderr('')
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- expect(fake.removed).toContain('/runtime/remove-error/stderr.log')
- })
- it('terminates a process group with TERM and reports the signal outcome', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/term')
- await flush()
- handle.terminate()
- handle.terminate()
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGTERM' })
- await expect(handle.waitForExit()).resolves.toBe(true)
- expect(fake.commandsSeen).toContain('kill -TERM -- -4242')
- expect(fake.commandsSeen).not.toContain('kill -KILL -- -4242')
- const signals = fake.commandsSeen.filter(command => command.startsWith('kill -')).length
- fake.alive = true
- handle.terminate()
- await flush()
- expect(fake.alive).toBe(true)
- expect(fake.commandsSeen.filter(command => command.startsWith('kill -'))).toHaveLength(signals)
- })
- it('makes termination a permanent no-op after natural quiescence is observed', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/natural-quiescence')
- await flush()
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- await expect(handle.waitForExit()).resolves.toBe(true)
- const signals = fake.commandsSeen.filter(command => command.startsWith('kill -')).length
- fake.alive = true
- handle.terminate()
- await flush()
- expect(fake.alive).toBe(true)
- expect(fake.commandsSeen.filter(command => command.startsWith('kill -'))).toHaveLength(signals)
- })
- it('treats a zombie-only process group as quiescent', async () => {
- const fake = new FakeSandbox()
- fake.zombieOnly = true
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/zombie-quiescence')
- await flush()
- await expect(handle.waitForExit()).resolves.toBe(true)
- expect(fake.commandsSeen).toContain(
- 'set -o pipefail; ps -eo pgid=,stat= | awk \'$1 == 4242 && $2 !~ /^[ZXx]/ { live=1 } END { if (live) print "live" }\'',
- )
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- })
- it('keeps proven quiescence after a concurrent termination transport fails', async () => {
- const fake = new FakeSandbox()
- fake.signalErrors.push(new Error('TERM transport failed'), new Error('KILL transport failed'))
- fake.handle.killError = new Error('SDK kill failed')
- fake.deferSignals()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/quiescent-race')
- await flush()
- handle.terminate()
- await vi.waitFor(() => { expect(fake.commandsSeen).toContain('kill -TERM -- -4242') })
- fake.alive = false
- await expect(handle.waitForExit()).resolves.toBe(true)
- fake.probeError = new Error('post-quiescence probe failed')
- fake.releaseSignals()
- await vi.waitFor(() => { expect(fake.handle.kills).toBe(1) })
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- const signals = fake.commandsSeen.filter(command => command.startsWith('kill -')).length
- handle.terminate()
- await expect(handle.waitForExit()).resolves.toBe(true)
- expect(fake.commandsSeen.filter(command => command.startsWith('kill -'))).toHaveLength(signals)
- })
- it('escalates a TERM-trapping process group to KILL and uses the SDK kill as fallback', async () => {
- const fake = new FakeSandbox()
- fake.trapsTerm = true
- fake.handle.killError = new Error('already gone')
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ graceMs: 1 }), '/runtime/kill')
- await flush()
- handle.terminate()
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGKILL' })
- await expect(handle.waitForExit()).resolves.toBe(true)
- expect(fake.commandsSeen).toContain('kill -KILL -- -4242')
- expect(fake.handle.kills).toBe(1)
- })
- it('does not treat successful termination transport as observed quiescence', async () => {
- const fake = new FakeSandbox()
- fake.trapsTerm = true
- fake.delaysKill = true
- fake.delaysKillCompletion = true
- fake.sdkKillStops = false
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ graceMs: 1 }), '/runtime/termination-fence')
- await flush()
- handle.terminate()
- await vi.waitFor(() => { expect(fake.handle.kills).toBe(1) })
- let quiescent = false
- const waiting = handle.waitForExit().then((value) => { quiescent = value })
- await new Promise(resolve => setTimeout(resolve, 10))
- expect(quiescent).toBe(false)
- fake.alive = false
- fake.finish()
- await waiting
- expect(quiescent).toBe(true)
- })
- it('honors termination requested before asynchronous startup finishes', async () => {
- const fake = new FakeSandbox()
- fake.deferStart()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/deferred-kill')
- handle.terminate()
- fake.releaseStart()
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGTERM' })
- })
- it('kills through the provisional SDK handle before process-group publication', async () => {
- const fake = new FakeSandbox()
- fake.deferProcessGroupRead()
- fake.signalErrors.push(commandError(1), commandError(1))
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/pre-publication-kill')
- await vi.waitFor(() => { expect(fake.startOptions).toBeDefined() })
- handle.terminate()
- await vi.waitFor(() => { expect(fake.handle.kills).toBe(1) })
- expect(fake.alive).toBe(false)
- await expect(handle.waitForExit()).resolves.toBe(true)
- fake.releaseProcessGroupRead()
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGKILL' })
- })
- it('bounds a quiescence observer while provisional termination is awaiting the controller', async () => {
- const fake = new FakeSandbox()
- fake.deferProcessGroupRead()
- const reconnect = Promise.withResolvers<Sandbox>()
- let calls = 0
- const delayedRuntime = runtime(fake, async () => {
- calls += 1
- return calls === 1 ? fake.sandbox : await reconnect.promise
- })
- const handle = new E2BSubprocessHandle(delayedRuntime, spec(), '/runtime/pre-publication-observer')
- await vi.waitFor(() => { expect(fake.startOptions).toBeDefined() })
- handle.terminate()
- const controller = new AbortController()
- const waiting = handle.waitForExit(controller.signal)
- await flush()
- controller.abort()
- await expect(waiting).resolves.toBe(false)
- reconnect.resolve(fake.sandbox)
- await expect(handle.waitForExit()).resolves.toBe(true)
- fake.releaseProcessGroupRead()
- await handle.done
- })
- it('proves a provisional group exit when the SDK kill fallback fails', async () => {
- const fake = new FakeSandbox()
- fake.deferProcessGroupRead()
- fake.trapsTerm = true
- fake.handle.killError = new Error('SDK kill unavailable')
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ graceMs: 1 }), '/runtime/pre-publication-group-kill')
- await vi.waitFor(() => { expect(fake.startOptions).toBeDefined() })
- handle.terminate()
- await expect(handle.waitForExit()).resolves.toBe(true)
- fake.releaseProcessGroupRead()
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGKILL' })
- })
- it('reports failed provisional group and SDK force transports', async () => {
- const fake = new FakeSandbox()
- fake.deferProcessGroupRead()
- fake.signalErrors.push(new Error('TERM transport failed'), new Error('KILL transport failed'))
- fake.handle.killError = new Error('SDK kill failed')
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ graceMs: 1 }), '/runtime/pre-publication-failure')
- await vi.waitFor(() => { expect(fake.startOptions).toBeDefined() })
- handle.terminate()
- await expect(handle.waitForExit()).rejects.toThrow('force termination failed through both')
- fake.handle.killError = undefined
- handle.terminate()
- await expect(handle.waitForExit()).resolves.toBe(true)
- fake.releaseProcessGroupRead()
- await handle.done
- const absentGroup = new FakeSandbox()
- absentGroup.deferProcessGroupRead()
- absentGroup.signalErrors.push(commandError(1), commandError(1))
- absentGroup.handle.killError = new Error('SDK kill failed without a provisional group')
- const absentHandle = new E2BSubprocessHandle(
- runtime(absentGroup),
- spec({ graceMs: 1 }),
- '/runtime/pre-publication-absent-group',
- )
- await vi.waitFor(() => { expect(absentGroup.startOptions).toBeDefined() })
- absentHandle.terminate()
- await expect(absentHandle.waitForExit()).rejects.toThrow('force termination failed through both')
- absentGroup.handle.killError = undefined
- absentHandle.terminate()
- await expect(absentHandle.waitForExit()).resolves.toBe(true)
- absentGroup.releaseProcessGroupRead()
- await absentHandle.done
- })
- it('honors an already-aborted signal when constructing the asynchronous handle directly', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ signal: AbortSignal.abort('stop') }), '/runtime/pre-aborted')
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGTERM' })
- })
- it('reacts to a signal that aborts after the remote command has started', async () => {
- const fake = new FakeSandbox()
- const controller = new AbortController()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ signal: controller.signal }), '/runtime/live-abort')
- await flush()
- controller.abort('stop')
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGTERM' })
- })
- it('can terminate a surviving process group after the command leader settles', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/surviving-group')
- await flush()
- await fake.completeOutput()
- fake.handle.succeed(0)
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- expect(fake.alive).toBe(true)
- handle.terminate()
- await flush()
- const signaled = fake.commandsSeen.includes('kill -TERM -- -4242')
- if (!signaled) fake.finish()
- await expect(handle.waitForExit()).resolves.toBe(true)
- expect(signaled).toBe(true)
- })
- it('bounds waitForExit while startup or a live group is pending', async () => {
- const fake = new FakeSandbox()
- fake.deferStart()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/wait')
- const beforeStart = new AbortController()
- const pending = handle.waitForExit(beforeStart.signal)
- beforeStart.abort()
- await expect(pending).resolves.toBe(false)
- await expect(handle.waitForExit(AbortSignal.abort())).resolves.toBe(false)
- fake.releaseStart()
- await flush()
- const live = new AbortController()
- const liveWait = handle.waitForExit(live.signal)
- live.abort()
- await expect(liveWait).resolves.toBe(false)
- fake.finish()
- await handle.done
- const terminatingFake = new FakeSandbox()
- terminatingFake.deferStart()
- const terminating = new E2BSubprocessHandle(runtime(terminatingFake), spec(), '/runtime/wait-termination-start')
- terminating.terminate()
- const beforeHandle = new AbortController()
- const handlePending = terminating.waitForExit(beforeHandle.signal)
- beforeHandle.abort()
- await expect(handlePending).resolves.toBe(false)
- terminatingFake.releaseStart()
- await terminating.done
- })
- it('bounds both sides of the liveness-poll abort race', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/poll-abort')
- await flush()
- const beforeTick = new AbortController()
- fake.afterProbe = () => { beforeTick.abort(); fake.afterProbe = undefined }
- await expect(handle.waitForExit(beforeTick.signal)).resolves.toBe(false)
- const duringTick = new AbortController()
- fake.afterProbe = () => {
- fake.afterProbe = undefined
- setTimeout(() => { duringTick.abort() }, 0)
- }
- await expect(handle.waitForExit(duringTick.signal)).resolves.toBe(false)
- const duringProbe = new AbortController()
- fake.beforeProbe = () => { duringProbe.abort(); fake.beforeProbe = undefined }
- await expect(handle.waitForExit(duringProbe.signal)).resolves.toBe(false)
- let racedAbort = false
- const raceSignal = {
- get aborted() { return racedAbort },
- addEventListener: () => { racedAbort = true },
- removeEventListener: () => {},
- } as unknown as AbortSignal
- await expect(handle.waitForExit(raceSignal)).resolves.toBe(false)
- fake.finish()
- await handle.done
- })
- it('observes a live group across one successful bounded poll', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/poll-success')
- await flush()
- setTimeout(() => { fake.finish() }, 1)
- await expect(handle.waitForExit(new AbortController().signal)).resolves.toBe(true)
- await handle.done
- })
- it('treats startup failure as no live tree and contains readiness rejection', async () => {
- const fake = new FakeSandbox()
- fake.backgroundError = new Error('start failed')
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/fail')
- await expect(handle.done).rejects.toThrow('start failed')
- expect(handle.pid).toBe(-1)
- expect(fake.removed).toContain('/runtime/fail/environment')
- expect(fake.removed).toContain('/runtime/fail')
- await expect(handle.waitForExit()).resolves.toBe(true)
- handle.terminate()
- const unavailableHandle = new E2BSubprocessHandle(
- runtime(new FakeSandbox(), async () => { throw new Error('sandbox unavailable') }),
- spec(),
- '/runtime/unavailable-start',
- )
- await expect(unavailableHandle.done).rejects.toThrow('sandbox unavailable')
- await expect(unavailableHandle.waitForExit()).resolves.toBe(true)
- const envFailure = new FakeSandbox()
- envFailure.envError = new Error('ambient lookup failed')
- const envHandle = new E2BSubprocessHandle(runtime(envFailure), spec(), '/runtime/env-failure')
- await expect(envHandle.done).rejects.toThrow('ambient lookup failed')
- expect(envFailure.removed).toEqual([])
- const cleanupFailure = new FakeSandbox()
- cleanupFailure.backgroundError = new Error('start failed before credential consumption')
- cleanupFailure.nextRemoveError = new Error('credential cleanup failed')
- const cleanupHandle = new E2BSubprocessHandle(runtime(cleanupFailure), spec(), '/runtime/cleanup-failure')
- await expect(cleanupHandle.done).rejects.toThrow('command failed and private state cleanup failed')
- const absentState = new FakeSandbox()
- absentState.backgroundError = new Error('start failed after external cleanup')
- absentState.nextRemoveError = new FileNotFoundError('already removed')
- const absentHandle = new E2BSubprocessHandle(runtime(absentState), spec(), '/runtime/absent-state')
- await expect(absentHandle.done).rejects.toThrow('start failed after external cleanup')
- })
- it('bounds a readiness rejection with a still-live caller signal', async () => {
- const fake = new FakeSandbox()
- fake.deferStart()
- fake.backgroundError = new Error('start failed')
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/fail-with-signal')
- const waiting = handle.waitForExit(new AbortController().signal)
- fake.releaseStart()
- await expect(handle.done).rejects.toThrow('start failed')
- await expect(waiting).resolves.toBe(true)
- })
- it('propagates an unavailable sandbox unless the caller aborts the wait', async () => {
- const fake = new FakeSandbox()
- let calls = 0
- const unavailable = runtime(fake, async () => {
- calls += 1
- if (calls === 1) return fake.sandbox
- throw new Error('connection unavailable')
- })
- const handle = new E2BSubprocessHandle(unavailable, spec(), '/runtime/unavailable')
- await flush()
- await expect(handle.waitForExit()).rejects.toThrow('connection unavailable')
- fake.finish()
- await handle.done
- })
- it('returns false when the caller aborts while reconnecting for liveness', async () => {
- const fake = new FakeSandbox()
- const reconnect = Promise.withResolvers<Sandbox>()
- let calls = 0
- const unavailable = runtime(fake, async () => {
- calls += 1
- return calls === 1 ? fake.sandbox : await reconnect.promise
- })
- const handle = new E2BSubprocessHandle(unavailable, spec(), '/runtime/reconnect-abort')
- await flush()
- const controller = new AbortController()
- const waiting = handle.waitForExit(controller.signal)
- await flush()
- controller.abort()
- reconnect.reject(new Error('connection unavailable'))
- await expect(waiting).resolves.toBe(false)
- fake.finish()
- await handle.done
- })
- it('returns false when a liveness request itself is aborted and surfaces other probe failures', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/probe')
- await flush()
- const controller = new AbortController()
- controller.abort()
- await expect(handle.waitForExit(controller.signal)).resolves.toBe(false)
- fake.probeError = new Error('probe failed')
- await expect(handle.waitForExit()).rejects.toThrow('probe failed')
- fake.finish()
- await handle.done
- })
- it('makes batch stdin close failures best-effort', async () => {
- const fake = new FakeSandbox()
- vi.spyOn(fake.handle, 'sendStdin').mockRejectedValueOnce(new Error('closed'))
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- stdio: { stdin: { data: 'ignored' }, stdout: { maxBytes: 4 }, stderr: { maxBytes: 4 } },
- }), '/runtime/stdin-closed')
- await flush()
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- })
- it('rejects malformed SDK process ids and non-command settlement failures', async () => {
- const invalidPid = new FakeSandbox()
- invalidPid.handle.pid = 0
- const invalid = new E2BSubprocessHandle(runtime(invalidPid), spec(), '/runtime/invalid-pid')
- await expect(invalid.done).rejects.toThrow(/invalid command pid 0/)
- expect(invalidPid.handle.kills).toBe(1)
- expect(invalidPid.removed).toContain('/runtime/invalid-pid/environment')
- await expect(invalid.waitForExit()).resolves.toBe(true)
- const failedRollback = new FakeSandbox()
- failedRollback.handle.pid = 0
- failedRollback.handle.killError = new Error('invalid handle kill failed')
- const retained = new E2BSubprocessHandle(runtime(failedRollback), spec(), '/runtime/invalid-pid-retained')
- await expect(retained.done).rejects.toThrow('invalid command pid rollback did not reach quiescence')
- await expect(retained.waitForExit()).rejects.toThrow('invalid handle kill failed')
- failedRollback.handle.killError = undefined
- retained.terminate()
- await expect(retained.waitForExit()).resolves.toBe(true)
- const crashedFake = new FakeSandbox()
- const crashed = new E2BSubprocessHandle(runtime(crashedFake), spec(), '/runtime/crashed')
- await flush()
- crashedFake.alive = false
- crashedFake.handle.crash(new Error('command transport failed'))
- await expect(crashed.done).rejects.toThrow('command transport failed')
- })
- it('rejects invalid or absent process-group publication', async () => {
- const invalidGroup = new FakeSandbox()
- invalidGroup.processGroupId = 'not-a-pid\n'
- invalidGroup.delaysKill = true
- invalidGroup.sdkKillStops = false
- invalidGroup.afterProbe = () => { invalidGroup.alive = false }
- const invalid = new E2BSubprocessHandle(runtime(invalidGroup), spec(), '/runtime/invalid-group')
- await expect(invalid.done).rejects.toThrow(/invalid process-group id/)
- expect(invalidGroup.handle.kills).toBe(1)
- expect(invalidGroup.commandsSeen).toContain('kill -KILL -- -4242')
- await expect(invalid.waitForExit()).resolves.toBe(true)
- const absentGroup = new FakeSandbox()
- absentGroup.processGroupId = ''
- const absent = new E2BSubprocessHandle(runtime(absentGroup), spec(), '/runtime/absent-group')
- await flush()
- absentGroup.finish()
- await expect(absent.done).rejects.toThrow(/exited before publishing/)
- expect(absentGroup.handle.kills).toBe(1)
- expect(absentGroup.commandsSeen).toContain('kill -KILL -- -4242')
- await expect(absent.waitForExit()).resolves.toBe(true)
- })
- it('preserves publication and rollback failures when cleanup cannot be verified', async () => {
- const fake = new FakeSandbox()
- fake.processGroupId = 'not-a-pid\n'
- fake.signalError = new Error('rollback signal failed')
- fake.handle.killError = new Error('SDK kill failed')
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/failed-rollback')
- let failure: unknown
- try {
- await handle.done
- } catch (error: unknown) {
- failure = error
- }
- expect(failure).toBeInstanceOf(AggregateError)
- if (!(failure instanceof AggregateError)) throw new Error('expected AggregateError')
- expect(failure.message).toBe('subprocess-e2b: process-group publication failed and rollback did not reach quiescence')
- const failures = Array.from(failure.errors as Iterable<unknown>)
- expect(failures).toHaveLength(2)
- expect(failures[0]).toBeInstanceOf(Error)
- expect(failures[1]).toBeInstanceOf(Error)
- if (!(failures[0] instanceof Error) || !(failures[1] instanceof Error)) throw new Error('expected nested errors')
- expect(failures[0].message).toContain('invalid process-group id')
- expect(failures[1].message).toBe('rollback signal failed')
- expect(fake.handle.kills).toBe(1)
- const bounded = new AbortController()
- const waiting = handle.waitForExit(bounded.signal)
- bounded.abort()
- await expect(waiting).resolves.toBe(false)
- handle.terminate()
- await expect(handle.waitForExit()).resolves.toBe(true)
- expect(fake.commandsSeen).toContain('kill -TERM -- -4242')
- })
- it('waits for delayed process-group publication', async () => {
- const fake = new FakeSandbox()
- fake.processGroupReads.push('', '4242\n')
- const handle = new E2BSubprocessHandle(runtime(fake), spec(), '/runtime/delayed-group')
- await vi.waitFor(() => { expect(handle.pid).toBe(4242) })
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- })
- it('handles output backpressure and contains a stderr sink failure', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- stdio: { stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' },
- }), '/runtime/backpressure')
- await flush()
- handle.stdout!.on('error', () => {})
- const stdoutWrite = vi.spyOn(handle.stdout!, 'write').mockReturnValueOnce(false)
- const stdoutPending = fake.stdout('blocked')
- queueMicrotask(() => { handle.stdout!.emit('drain') })
- await stdoutPending
- stdoutWrite.mockRestore()
- handle.stderr!.on('error', () => {})
- const stderrWrite = vi.spyOn(handle.stderr!, 'write').mockReturnValueOnce(false)
- const stderrPending = fake.stderr('broken')
- queueMicrotask(() => { handle.stderr!.emit('error', new Error('sink failed')) })
- await stderrPending
- stderrWrite.mockRestore()
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- })
- it('settles output backpressure when the consumer closes the pipe', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- stdio: { stdin: 'ignore', stdout: 'pipe', stderr: { maxBytes: 4 } },
- }), '/runtime/backpressure-close')
- await flush()
- const stdoutWrite = vi.spyOn(handle.stdout!, 'write').mockReturnValueOnce(false)
- const pending = fake.stdout('discarded')
- queueMicrotask(() => { handle.stdout!.destroy() })
- await pending
- stdoutWrite.mockRestore()
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- })
- it('contains a pipe callback failure instead of rejecting command settlement', async () => {
- const fake = new FakeSandbox()
- const handle = new E2BSubprocessHandle(runtime(fake), spec({
- stdio: { stdin: 'ignore', stdout: 'pipe', stderr: { maxBytes: 4 } },
- }), '/runtime/pipe-error')
- await flush()
- const emitted = once(handle.stdout!, 'error')
- handle.stdout!.destroy(new Error('consumer failed'))
- await emitted
- await fake.stdout('late output')
- fake.finish()
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- })
- it('contains an already-gone group signal and escalates after a TERM transport failure', async () => {
- const gone = new FakeSandbox()
- gone.trapsTerm = true
- gone.signalError = commandError(1)
- const goneHandle = new E2BSubprocessHandle(runtime(gone), spec({ graceMs: 1 }), '/runtime/gone-signal')
- await flush()
- goneHandle.terminate()
- await expect(goneHandle.done).resolves.toEqual({ exitCode: null, signal: 'SIGKILL' })
- const failed = new FakeSandbox()
- failed.signalError = new Error('signal transport failed')
- const failedHandle = new E2BSubprocessHandle(runtime(failed), spec(), '/runtime/failed-signal')
- await flush()
- failedHandle.terminate()
- await expect(failedHandle.done).resolves.toEqual({ exitCode: null, signal: 'SIGKILL' })
- expect(failed.commandsSeen).toContain('kill -KILL -- -4242')
- })
- it('allows termination retry after both force transports fail', async () => {
- const fake = new FakeSandbox()
- fake.signalErrors.push(new Error('TERM transport failed'), new Error('KILL transport failed'))
- fake.handle.killError = new Error('SDK kill failed')
- const handle = new E2BSubprocessHandle(runtime(fake), spec({ graceMs: 1 }), '/runtime/retry-signal')
- await flush()
- handle.terminate()
- await expect(handle.waitForExit()).rejects.toThrow('force termination failed through both')
- fake.handle.killError = undefined
- handle.terminate()
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGTERM' })
- expect(fake.commandsSeen.filter(command => command.startsWith('kill -TERM '))).toHaveLength(2)
- const missingGroup = new FakeSandbox()
- missingGroup.trapsTerm = true
- missingGroup.signalErrors.push(undefined, commandError(1))
- missingGroup.handle.killError = new Error('SDK kill failed after group exit race')
- const raced = new E2BSubprocessHandle(runtime(missingGroup), spec({ graceMs: 1 }), '/runtime/group-exit-race')
- await flush()
- raced.terminate()
- await expect(raced.waitForExit()).rejects.toThrow('force termination failed through both')
- missingGroup.handle.killError = undefined
- raced.terminate()
- await expect(raced.waitForExit()).resolves.toBe(true)
- })
- })
- describe('E2BSubprocessService', () => {
- async function service(
- fake = new FakeSandbox(),
- providedRuntime: E2BSandboxService = runtime(fake),
- ): Promise<{ ctx: Context; fiber: Awaited<ReturnType<Context['plugin']>> }> {
- const ctx = new Context()
- ctx.provide('e2b', providedRuntime)
- const fiber = await ctx.plugin(E2BSubprocessService)
- return { ctx, fiber }
- }
- it('registers handles and disposal terminates and joins live remote groups regardless of sandbox policy', async () => {
- const fake = new FakeSandbox()
- fake.trapsTerm = true
- const { ctx, fiber } = await service(fake)
- const handle = ctx.subprocess.spawn(spec({ graceMs: 1 }))
- await flush()
- await fiber.dispose()
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGKILL' })
- expect(fake.alive).toBe(false)
- })
- it('awaits SDK settlement after the remote process group becomes quiescent', async () => {
- const fake = new FakeSandbox()
- fake.trapsTerm = true
- const { ctx, fiber } = await service(fake)
- const handle = ctx.subprocess.spawn(spec())
- await flush()
- fake.alive = false
- let disposed = false
- const disposing = fiber.dispose().then(() => { disposed = true })
- await flush()
- expect(disposed).toBe(false)
- fake.finish()
- await disposing
- await expect(handle.done).resolves.toEqual({ exitCode: 0, signal: null })
- })
- it('reports a failed termination transaction from disposal instead of waiting on done', async () => {
- const fake = new FakeSandbox()
- fake.signalErrors.push(new Error('TERM transport failed'), new Error('KILL transport failed'))
- fake.handle.killError = new Error('SDK kill failed')
- const { ctx, fiber } = await service(fake)
- const handle = ctx.subprocess.spawn(spec({ graceMs: 1 }))
- await flush()
- await expect(fiber.dispose()).resolves.toBeUndefined()
- await expect(handle.waitForExit()).rejects.toThrow('force termination failed through both')
- fake.handle.killError = undefined
- handle.terminate()
- await expect(handle.waitForExit()).resolves.toBe(true)
- await expect(handle.done).resolves.toEqual({ exitCode: null, signal: 'SIGTERM' })
- })
- it('releases naturally settled handles before later service disposal', async () => {
- const fake = new FakeSandbox()
- const { ctx, fiber } = await service(fake)
- const handle = ctx.subprocess.spawn(spec())
- await flush()
- fake.finish()
- await handle.done
- await flush()
- const signalsBefore = fake.commandsSeen.filter(command => command.startsWith('kill -')).length
- await fiber.dispose()
- expect(fake.commandsSeen.filter(command => command.startsWith('kill -')).length).toBe(signalsBefore)
- })
- it('contains a release liveness failure and retries quiescence during disposal', async () => {
- const fake = new FakeSandbox()
- let calls = 0
- const reconnecting = runtime(fake, async () => {
- calls += 1
- if (calls === 2) throw new Error('transient liveness failure')
- return fake.sandbox
- })
- const { ctx, fiber } = await service(fake, reconnecting)
- const handle = ctx.subprocess.spawn(spec())
- await flush()
- fake.finish()
- await handle.done
- await flush()
- await fiber.dispose()
- expect(calls).toBeGreaterThanOrEqual(3)
- })
- it('contains spawn rejection while disposal is joining the pending handle', async () => {
- const fake = new FakeSandbox()
- fake.deferStart()
- fake.backgroundError = new Error('start failed during disposal')
- const { ctx, fiber } = await service(fake)
- const subprocess = ctx.subprocess
- const handle = subprocess.spawn(spec())
- const disposing = fiber.dispose()
- await flush()
- expect(() => subprocess.spawn(spec())).toThrow('service is disposing')
- fake.releaseStart()
- await expect(disposing).resolves.toBeUndefined()
- await expect(handle.done).rejects.toThrow('start failed during disposal')
- })
- it('validates synchronous spawn preconditions', async () => {
- const { ctx } = await service()
- expect(() => ctx.subprocess.spawn(spec({ argv: [] }))).toThrow(/non-empty program/)
- expect(() => ctx.subprocess.spawn(spec({ graceMs: 0 }))).toThrow(/positive finite/)
- expect(() => ctx.subprocess.spawn(spec({ signal: AbortSignal.abort('stop') }))).toThrow(/aborted before spawn/)
- expect(() => ctx.subprocess.spawn(spec({ signal: { aborted: true, reason: undefined } as AbortSignal }))).toThrow(/aborted$/)
- })
- it('registers the package-owned empty invariant installer', async () => {
- const ctx = new Context()
- await ctx.plugin(InvariantService, { enabled: true })
- const fiber = await ctx.plugin(E2BSubprocessInvariant).await()
- await fiber.dispose()
- })
- })
|