237 lines
10 KiB
TypeScript
237 lines
10 KiB
TypeScript
/**
|
|
* Local Service Provider for the subprocess capability seam. Each spawn owns a
|
|
* platform-selected managed range with the spec's per-stream stdio dispositions.
|
|
* Normal disposal terminates and joins live ranges; Node's synchronous exit
|
|
* phase force-stops any ranges the service still owns. It has no config: every
|
|
* disposition and limit arrives on the spec, so deployment-varying choices
|
|
* stay with the caller's config (the bash executor's, the LSP host's, …).
|
|
* @module @deepseek-ai/dsh-subprocess-local
|
|
*/
|
|
|
|
import { constants } from 'node:fs'
|
|
import { access, stat } from 'node:fs/promises'
|
|
import { delimiter, extname, isAbsolute, resolve } from 'node:path'
|
|
import { Context } from '@deepseek-ai/cordis'
|
|
import * as nodePty from 'node-pty'
|
|
import type { IPtyForkOptions } from 'node-pty'
|
|
import { SubprocessRuntime } from '@deepseek-ai/dsh-subprocess'
|
|
import type {
|
|
SubprocessHandle,
|
|
SubprocessSpawnSpec,
|
|
SubprocessTerminalHandle,
|
|
SubprocessTerminalSpawnSpec,
|
|
} from '@deepseek-ai/dsh-subprocess'
|
|
import {
|
|
bindManagedProcess,
|
|
childEnv,
|
|
prepareManagedProcessBinding,
|
|
spawnSubprocess,
|
|
validateSubprocessSpec,
|
|
} from './spawn.ts'
|
|
import type { LocalSubprocessHandle, SpawnInternals } from './spawn.ts'
|
|
import { launchLinuxScope, probeLinuxScope } from './linux-scope.ts'
|
|
import { launchWindowsJob, probeWindowsJob } from './windows-job.ts'
|
|
import { createProcessInspector } from './process-inspector.ts'
|
|
import type { ProcessInspector } from './process-inspector.ts'
|
|
import { LocalTerminalHandle } from './terminal.ts'
|
|
|
|
/**
|
|
* Local subprocess service: platform-selected managed ranges, Node-shaped stdio
|
|
* dispositions (raw pipes, inherit, bounded tail-keep collection with spill
|
|
* files), credential-scrubbed environment, and provider-owned range signalling.
|
|
* POSIX paths stage TERM before KILL; Windows paths terminate immediately.
|
|
* JavaScript-observable host exit also performs synchronous final termination.
|
|
*/
|
|
export class LocalSubprocessRuntime extends SubprocessRuntime {
|
|
/** Live handles retained for normal disposal and synchronous host-exit finalization. */
|
|
private live = new Set<LocalSubprocessHandle>()
|
|
/** Live terminals retained through normal quiescence or host-exit finalization. */
|
|
private terminals = new Set<LocalTerminalHandle>()
|
|
/** Test hook: spill and platform knobs forwarded to spawnSubprocess. */
|
|
internals: SpawnInternals = {}
|
|
/** Ordinary native containment mode, selected once before its first user command. */
|
|
private ordinaryMode: 'linux-scope' | 'windows-job' | 'fallback' | undefined
|
|
/** Test hook for platform process inspection; production resolves lazily on terminal spawn. */
|
|
terminalInspector: ProcessInspector | undefined
|
|
|
|
constructor(ctx: Context) {
|
|
super(ctx)
|
|
ctx.effect(() => {
|
|
const onHostExit = (): void => { this.terminateForHostExit() }
|
|
process.prependListener('exit', onHostExit)
|
|
return async () => {
|
|
try {
|
|
await this.disposeManagedProcesses()
|
|
} finally {
|
|
process.off('exit', onHostExit)
|
|
}
|
|
}
|
|
}, 'local subprocess teardown')
|
|
}
|
|
|
|
private terminateForHostExit(): void {
|
|
for (const handle of this.live) {
|
|
try {
|
|
handle.terminateForHostExit()
|
|
} catch (_ordinaryRangeTerminationFailed) {
|
|
// Host exit cannot await or report one target; continue with the rest.
|
|
}
|
|
}
|
|
for (const terminal of this.terminals) {
|
|
try {
|
|
terminal.terminateForHostExit()
|
|
} catch (_terminalTerminationFailed) {
|
|
// One terminal must not prevent final termination of another target.
|
|
}
|
|
}
|
|
}
|
|
|
|
private async disposeManagedProcesses(): Promise<void> {
|
|
// Request termination, then await MANAGED-RANGE exit — not just the
|
|
// direct command's settlement — so even a surviving descendant cannot
|
|
// outlive the fiber. Keep both sets authoritative while these waits are
|
|
// pending so a shorter process-level exit bound can still force-kill them.
|
|
const pending: Promise<unknown>[] = []
|
|
for (const handle of this.live) {
|
|
handle.terminate()
|
|
// Spawn-failure rejections already settled and left the live set.
|
|
pending.push(handle.done.catch(() => {}).then(() => handle.waitForExit()))
|
|
}
|
|
for (const terminal of this.terminals) {
|
|
pending.push(terminal.terminate())
|
|
}
|
|
const outcomes = await Promise.allSettled(pending)
|
|
const failures = outcomes.flatMap<unknown>(outcome => outcome.status === 'rejected'
|
|
? [outcome.reason as unknown]
|
|
: [])
|
|
if (failures.length > 0) this.terminateForHostExit()
|
|
this.live.clear()
|
|
this.terminals.clear()
|
|
if (failures.length === 1) throw failures[0]
|
|
if (failures.length > 1) throw new AggregateError(failures, 'local subprocess teardown failed')
|
|
}
|
|
|
|
async resolveExecutable(
|
|
command: string,
|
|
env?: Readonly<Record<string, string>>,
|
|
signal?: AbortSignal,
|
|
): Promise<string> {
|
|
if (command.length === 0) throw new Error('subprocess-local: executable must be non-empty')
|
|
signal?.throwIfAborted()
|
|
const environment = childEnv(env)
|
|
const absolute = isAbsolute(command)
|
|
if (!absolute && (command.includes('/') || (process.platform === 'win32' && command.includes('\\')))) {
|
|
throw new Error(
|
|
`subprocess-local: command ${JSON.stringify(command)} is a relative path; use an absolute path or a bare PATH name`,
|
|
)
|
|
}
|
|
const candidates = absolute ? [command] : this.executableCandidates(command, environment)
|
|
for (const candidate of candidates) {
|
|
signal?.throwIfAborted()
|
|
try {
|
|
const info = await stat(candidate)
|
|
if (!info.isFile()) continue
|
|
await access(candidate, constants.X_OK)
|
|
signal?.throwIfAborted()
|
|
return candidate
|
|
} catch {
|
|
// Try the next PATH candidate; the final miss receives one stable error.
|
|
}
|
|
}
|
|
signal?.throwIfAborted()
|
|
throw new Error(absolute
|
|
? `subprocess-local: command ${JSON.stringify(command)} is not an executable file`
|
|
: `subprocess-local: command ${JSON.stringify(command)} was not found on PATH`)
|
|
}
|
|
|
|
private executableCandidates(command: string, env: NodeJS.ProcessEnv): string[] {
|
|
const path = environmentValue(env, 'PATH') ?? ''
|
|
const extensions = process.platform === 'win32' && extname(command) === ''
|
|
? (environmentValue(env, 'PATHEXT') ?? '.COM;.EXE;.BAT;.CMD').split(';')
|
|
: ['']
|
|
return path.split(delimiter).flatMap(directory =>
|
|
extensions.map(extension => resolve(process.cwd(), directory, command + extension)))
|
|
}
|
|
|
|
spawn(spec: SubprocessSpawnSpec): SubprocessHandle {
|
|
validateSubprocessSpec(spec)
|
|
const mode = this.selectOrdinaryMode()
|
|
let handle: LocalSubprocessHandle
|
|
if (mode === 'fallback') {
|
|
handle = spawnSubprocess(spec, this.internals)
|
|
} else {
|
|
const binding = prepareManagedProcessBinding(this.internals)
|
|
const launch = mode === 'linux-scope' ? launchLinuxScope(spec) : launchWindowsJob(spec)
|
|
handle = bindManagedProcess(spec, launch, binding)
|
|
}
|
|
this.live.add(handle)
|
|
// Release ownership only once the managed range is empty, not at spawned-
|
|
// command settlement. A surviving helper remains owned until provider
|
|
// termination and observation reach quiescence.
|
|
const release = (): Promise<void> =>
|
|
handle.waitForExit().then(() => { this.live.delete(handle) })
|
|
void handle.done.then(release, release).catch(() => {})
|
|
return handle
|
|
}
|
|
|
|
private selectOrdinaryMode(): 'linux-scope' | 'windows-job' | 'fallback' {
|
|
if (this.ordinaryMode !== undefined) return this.ordinaryMode
|
|
const platform = this.internals.platform ?? process.platform
|
|
if (platform === 'linux' && probeLinuxScope()) this.ordinaryMode = 'linux-scope'
|
|
else if (platform === 'win32' && probeWindowsJob()) this.ordinaryMode = 'windows-job'
|
|
else this.ordinaryMode = 'fallback'
|
|
if (this.ordinaryMode === 'fallback') this.warnFallback(platform)
|
|
return this.ordinaryMode
|
|
}
|
|
|
|
private warnFallback(platform: NodeJS.Platform): void {
|
|
const reason = platform === 'darwin'
|
|
? 'macOS has no supported persistent process-range owner'
|
|
: platform === 'linux'
|
|
? 'a modern readable user-systemd scope is unavailable'
|
|
: platform === 'win32'
|
|
? 'the Win32 Job runner is unavailable'
|
|
: `platform ${platform} has no native managed range`
|
|
this.ctx.logger.warn(
|
|
`subprocess-local is using weaker process-tree containment because ${reason}; descendants that escape the process group or direct-parent tree are not guaranteed to terminate or delay waitForExit()`,
|
|
)
|
|
}
|
|
|
|
// Local PTY allocation is synchronous, but the provider contract permits remote asynchronous allocation.
|
|
// oxlint-disable-next-line typescript/require-await -- Preserve promise rejection semantics at the async provider contract.
|
|
async spawnTerminal(spec: SubprocessTerminalSpawnSpec): Promise<SubprocessTerminalHandle> {
|
|
const file = spec.argv[0]
|
|
if (file === undefined || file.length === 0) {
|
|
throw new Error('subprocess-local: terminal argv must contain a program')
|
|
}
|
|
spec.signal?.throwIfAborted()
|
|
const options: IPtyForkOptions = {
|
|
name: 'dumb',
|
|
rows: spec.rows,
|
|
cols: spec.cols,
|
|
cwd: spec.cwd,
|
|
env: childEnv(spec.env),
|
|
}
|
|
const inspector = this.terminalInspector ?? createProcessInspector()
|
|
const terminal = nodePty.spawn(file, [...spec.argv.slice(1)], options)
|
|
const handle = new LocalTerminalHandle(terminal, inspector, spec.graceMs)
|
|
this.terminals.add(handle)
|
|
const release = async (): Promise<void> => {
|
|
await handle.terminate()
|
|
this.terminals.delete(handle)
|
|
}
|
|
void handle.done.then(release, release).catch(() => {})
|
|
return handle
|
|
}
|
|
}
|
|
|
|
/** Read a Windows environment key using the platform's case-insensitive semantics. */
|
|
function environmentValue(env: NodeJS.ProcessEnv, name: 'PATH' | 'PATHEXT'): string | undefined {
|
|
const exact = env[name]
|
|
if (exact !== undefined || process.platform !== 'win32') return exact
|
|
const normalized = name.toUpperCase()
|
|
return Object.entries(env).find(([key]) => key.toUpperCase() === normalized)?.[1]
|
|
}
|
|
|
|
export default LocalSubprocessRuntime
|