/** * Low-level JSON-RPC client for a DeepSeek Harness SDK runtime subprocess. * {@link HarnessClient} owns the child process: it spawns the runtime, speaks * the `@deepseek-ai/dsh-sdk-protocol` wire over the child's stdio, fans * server notifications out to subscriptions, and tears the child down to * quiescence through a private EOF → SIGTERM → SIGKILL ladder. The design * twin is the Python SDK's `HarnessClient` (`python/sdk`); both drive the * same runtime protocol. This client runs OUTSIDE any harness context, so it * spawns directly rather than through the `dsh-subprocess` service — the * seam's documented exception for SDK-managed transports. * * @module @deepseek-ai/dsh-sdk-client/client */ import { spawn, type ChildProcess } from 'node:child_process' import { JsonRpcLineTransport, JsonRpcResponseError, type CommandExecution, type InitializeParams, type InitializeResult, type SessionPromptParams, type SessionQueryParams, type SessionQueryResult, type SdkPromptContentBlock, type SdkUserQuestionRequest, } from '@deepseek-ai/dsh-sdk-protocol' import { SessionId, SessionSeq } from '@deepseek-ai/dsh-session' import { disposeRuntimeProcess } from './dispose.ts' import { resolveDshLaunch, type RuntimeProcessOptions } from './launch.ts' import type { HarnessClientOptions, HarnessNotification, NotificationFilter } from './types.ts' /** Retained stderr lines used to diagnose an unexpected runtime death. */ const STDERR_TAIL_LIMIT = 400 /** Grace for the runtime's stdio streams to settle after its exit edge. */ const STREAM_SETTLE_MS = 100 /** * The runtime subprocess is gone or unusable: it exited, its stdio closed, or * it was never launchable. The message carries the exit code and a stderr * tail when available. */ export class TransportClosedError extends Error { /** @param message - the failure description, including any stderr tail. */ constructor(message: string) { super(message) this.name = 'TransportClosedError' } } /** A request exceeded {@link HarnessClientOptions.requestTimeoutMs}. */ export class RequestTimeoutError extends Error { /** @param message - which method timed out. */ constructor(message: string) { super(message) this.name = 'RequestTimeoutError' } } /** * The runtime answered outside its documented protocol (for example a * `session/prompt` response without `accepted: true`). */ export class SdkProtocolError extends Error { /** @param message - the protocol violation description. */ constructor(message: string) { super(message) this.name = 'SdkProtocolError' } } interface SubscriptionState { readonly queue: HarnessNotification[] readonly waiters: { resolve: (item: HarnessNotification) => void; reject: (error: Error) => void }[] readonly filter: NotificationFilter | undefined failure: Error | undefined } /** One client-side notification stream returned by {@link HarnessClient.subscribe}. */ export interface NotificationSubscription extends AsyncIterable { /** * Await the next matching notification. * @returns the notification; after the runtime died, drains what was * already delivered and then rejects; after {@link close}, rejects * immediately (the queue is dropped). */ next(): Promise /** * Drain one already-delivered notification without waiting. * @returns the next queued notification, or `undefined` when none is queued. */ tryNext(): HarnessNotification | undefined /** Detach from the client; queued items drop and pending waiters reject. */ close(): void } /** Internal producer side of a public notification subscription. */ class NotificationSubscriptionImpl implements NotificationSubscription { constructor( private readonly state: SubscriptionState, private readonly unsubscribe: () => void, ) {} /** * Await the next matching notification. * @returns the notification; after the runtime died, drains what was * already delivered and then rejects; after {@link close}, rejects * immediately (the queue is dropped). */ next(): Promise { const queued = this.state.queue.shift() if (queued !== undefined) return Promise.resolve(queued) if (this.state.failure !== undefined) return Promise.reject(this.state.failure) return new Promise((resolve, reject) => { this.state.waiters.push({ resolve, reject }) }) } /** * Drain one already-delivered notification without waiting. * @returns the next queued notification, or `undefined` when none is queued. */ tryNext(): HarnessNotification | undefined { return this.state.queue.shift() } /** Detach from the client; queued items drop and pending waiters reject. */ close(): void { this.unsubscribe() // The drop is part of this method's contract; a runtime-death fail() keeps // the queue so already-delivered notifications remain drainable. this.state.queue.length = 0 this.fail(new TransportClosedError('notification subscription closed')) } /** * Reject pending and future waits (delivery stops; the first failure wins). * Already-queued notifications remain drainable via {@link next}/{@link tryNext}. * @param error - the terminal failure delivered to waiters. */ fail(error: Error): void { this.state.failure ??= error for (const waiter of this.state.waiters.splice(0)) waiter.reject(this.state.failure) } /** * Deliver one notification to a waiter or the queue when the filter * matches. A throwing filter fails only THIS subscription (detached, the * throw becomes its terminal error) — it never disturbs sibling * subscriptions or the transport's read loop. * @param notification - the wire notification to deliver. */ push(notification: HarnessNotification): void { let matches: boolean try { matches = this.state.filter === undefined || this.state.filter(notification) } catch (error) { this.unsubscribe() this.fail(error instanceof Error ? error : new Error(String(error))) return } if (!matches) return const waiter = this.state.waiters.shift() if (waiter !== undefined) waiter.resolve(notification) else this.state.queue.push(notification) } /** * Iterate notifications until the subscription or runtime closes (the * terminating rejection propagates). * @returns an async iterator over {@link next} results. */ async * [Symbol.asyncIterator](): AsyncIterator { for (;;) yield await this.next() } } /** * JSON-RPC client for the DeepSeek Harness SDK runtime over subprocess stdio. * * The subprocess starts lazily on {@link start} and is owned by this instance * until {@link close}, which requests protocol `shutdown` and then walks the * shared EOF → SIGTERM → SIGKILL dispose ladder to quiescence. There is no * wire-level cancel: a timed-out request stays running server-side until the * runtime is closed. */ export class HarnessClient { /** Original public dsh launch and timeout options for this client. */ readonly options: HarnessClientOptions private readonly runtime: RuntimeProcessOptions private child: ChildProcess | undefined private transport: JsonRpcLineTransport | undefined private readonly stderrTail: string[] = [] private readonly subscriptions = new Map() private readonly sessionParents = new Map() private subscriptionSerial = 0 private exitCode: number | null | undefined private spawnError: Error | undefined private streamsSettled: Promise = Promise.resolve() private closeTask: Promise | undefined private readonly userQuestions = new Map() private userQuestionHandler: HarnessClientOptions['onUserQuestion'] private requestHandler: ((method: string, params: Record) => Promise) | undefined /** @param options - dsh profile, patch, home, process, environment, and timeout options. */ constructor(options?: HarnessClientOptions) constructor(options: HarnessClientOptions = {}, runtime?: RuntimeProcessOptions) { this.options = options this.runtime = runtime ?? resolveDshLaunch(options) } /** * Spawn the runtime subprocess and start reading frames. Idempotent while * the process is live; rejects reuse after {@link close}. */ start(): void { if (this.closeTask !== undefined) throw new TransportClosedError('DeepSeek Harness runtime client is closed') if (this.child !== undefined) return this.userQuestionHandler = this.options.onUserQuestion const child = spawn(this.runtime.command, this.runtime.args, { cwd: this.runtime.cwd, env: this.runtime.environment(), stdio: ['pipe', 'pipe', 'pipe'], }) this.child = child child.once('error', (error) => { this.spawnError = error // A spawn failure destroys the pipes without an input 'end' edge, so the // transport's pending requests must be failed here. this.transport?.close() this.failSubscriptions(this.closedError('DeepSeek Harness runtime failed to start')) }) // Writes racing the runtime's death EPIPE on stdin; the exit edge below is // the real signal, so the stream-level error only needs to be non-fatal. // The timing of that race is not deterministically reproducible. /* v8 ignore next */ child.stdin.on('error', () => {}) let stderrBuffer = '' child.stderr.setEncoding('utf8') child.stderr.on('data', (chunk: string) => { stderrBuffer += chunk const newline = stderrBuffer.lastIndexOf('\n') if (newline >= 0) { this.appendStderr(stderrBuffer.slice(0, newline).split('\n')) stderrBuffer = stderrBuffer.slice(newline + 1) } }) let signalStreamsSettled!: () => void this.streamsSettled = new Promise((resolve) => { signalStreamsSettled = resolve }) const settled = { stderr: false, exited: false } const maybeSettle = (): void => { if (settled.stderr && settled.exited) signalStreamsSettled() } child.stderr.once('close', () => { if (stderrBuffer.length > 0) this.appendStderr([stderrBuffer]) settled.stderr = true maybeSettle() }) child.once('exit', (code) => { this.exitCode = code settled.exited = true maybeSettle() this.failSubscriptions(this.closedError('DeepSeek Harness runtime exited')) }) child.once('close', () => { // All stdio has settled: stdout 'end' already drained every tail frame, // so closing now cannot drop responses — it only fails requests that // will never be answered. this.transport?.close() }) const transport = new JsonRpcLineTransport(child.stdout, child.stdin) transport.onRequest(async (method, params) => { if (method !== 'user-question/ask') { if (this.requestHandler !== undefined) return this.requestHandler(method, params) throw new SdkProtocolError(`unknown runtime request: ${method}`) } const handler = this.userQuestionHandler if (handler === undefined) throw new SdkProtocolError('user-question/ask requires an onUserQuestion callback') if (!isUserQuestionRequest(params)) { throw new SdkProtocolError('user-question/ask requires requestId, sessionId, and questions') } const requestId = params.requestId if (this.userQuestions.has(requestId)) throw new SdkProtocolError('duplicate user-question requestId') const controller = new AbortController() this.userQuestions.set(requestId, controller) const cancelled = Promise.withResolvers() const onAbort = (): void => { cancelled.reject(controller.signal.reason) } controller.signal.addEventListener('abort', onAbort, { once: true }) try { return await Promise.race([ Promise.resolve().then(() => { controller.signal.throwIfAborted() return handler(params, { signal: controller.signal }) }), cancelled.promise, ]) } finally { controller.signal.removeEventListener('abort', onAbort) this.userQuestions.delete(requestId) } }) transport.onNotification((method, params) => { if (method === 'user-question.cancelled' && typeof params.requestId === 'string') { this.userQuestions.get(params.requestId)?.abort(new Error('SDK runtime cancelled the user question')) } this.dispatchNotification({ method, params }) }) transport.start() this.transport = transport } /** * Install the additional-request handler; native questions retain their onUserQuestion callback. * @param handler - receives wire parameters and returns the JSON-RPC result; rejection sends an error. * @throws when the client has not started or has already closed. */ onRequest(handler: (method: string, params: Record) => Promise): void { if (this.closeTask !== undefined) { throw new TransportClosedError('DeepSeek Harness runtime client is closed; create a new client to install a request handler') } if (this.transport === undefined) { throw new TransportClosedError('start the SDK client before installing a request handler') } this.requestHandler = handler } /** * Start the runtime if needed, then perform the process-wide handshake. * @param params - workspace cwd plus the provider/model route. * @returns the runtime's wire identity. */ async initialize(params: InitializeParams): Promise { this.start() const result = await this.request('initialize', { ...params, ...this.userQuestionHandler === undefined ? {} : { capabilities: { ...params.capabilities, userQuestions: true } }, }, this.runtime.initializeTimeoutMs) if (!isRecord(result) || !isRecord(result.serverInfo) || typeof result.serverInfo.name !== 'string' || typeof result.serverInfo.version !== 'string') { throw new SdkProtocolError(`initialize returned no server identity: ${JSON.stringify(result)}`) } return { serverInfo: { name: result.serverInfo.name, version: result.serverInfo.version } } } /** * Queue one prompt without native command routing and return its durable inbox identity. * @param sessionId - target session; an unknown id creates it. * @param contentBlocks - the user message, sent verbatim. * @returns the queued message id. */ async prompt(sessionId: string, contentBlocks: SdkPromptContentBlock[]): Promise { const params: SessionPromptParams = { sessionId, contentBlocks } const result = await this.request('session/prompt', { ...params }) if (!isRecord(result) || typeof result.messageId !== 'string') { throw new SdkProtocolError(`session/prompt returned no message id: ${JSON.stringify(result)}`) } return result.messageId } /** * Restore persisted history without sending a prompt; failures propagate without creating a session. * @param sessionId - persisted identity in this runtime's configured persistence store. * @returns after the session is ready for prompts. */ async resumeSession(sessionId: string): Promise { await this.request('session/resume', { sessionId }) } /** * Read a Session's effective working directory, recovering it if missing. * @param sessionId - target Session; an unknown id creates it. * @returns the absolute effective directory. */ getWorkingDirectory(sessionId: string): Promise { return this.workingDirectoryRequest('session/working-directory/get', { sessionId }) } /** * Change a Session's effective directory without changing origin or permissions. * @param sessionId - target Session; an unknown id creates it. * @param path - absolute or Session-current-directory-relative path. * @returns the validated absolute directory. */ setWorkingDirectory(sessionId: string, path: string): Promise { return this.workingDirectoryRequest('session/working-directory/set', { sessionId, path }) } private async workingDirectoryRequest(method: string, params: object): Promise { const result = await this.request(method, params) if (!isRecord(result) || typeof result.cwd !== 'string') { throw new SdkProtocolError(`${method} returned no working directory: ${JSON.stringify(result)}`) } return result.cwd } /** * Send one JSON-RPC request and await its result. * @param method - the wire method name. * @param params - the params object; omitted params send `{}`. * @param timeoutMs - per-call override of {@link HarnessClientOptions.requestTimeoutMs}. * @returns the raw result; rejects with {@link JsonRpcResponseError} on a * protocol error response, {@link RequestTimeoutError} on timeout, and * {@link TransportClosedError} when the runtime is gone. */ async request(method: string, params?: object, timeoutMs?: number): Promise { this.start() // A dead runtime cannot answer; fail with process context instead of // writing into a destroyed pipe and hanging until the timeout. if (this.exitCode !== undefined || this.spawnError !== undefined) { await this.settleStreams() throw this.closedError('DeepSeek Harness runtime is not running') } const transport = this.transport /* v8 ignore next -- start() either sets the transport or throws */ if (transport === undefined) throw new TransportClosedError('DeepSeek Harness runtime is not running') const timeout = timeoutMs ?? this.runtime.requestTimeoutMs try { if (timeout === undefined) return await transport.request(method, params ?? {}) // The abort signal makes the timeout an abandonment: the transport drops // its pending entry, so repeated bounded requests against a hung method // retain no per-call state (the server-side work still runs to close). const abandon = new AbortController() const timer = setTimeout(() => { const stderr = this.stderrTail.length === 0 ? '' : `; stderr tail:\n${this.stderrTail.join('\n')}` abandon.abort(new RequestTimeoutError(`${method} timed out after ${timeout}ms waiting for ${this.runtime.description}${stderr}`)) }, timeout) try { return await transport.request(method, params ?? {}, abandon.signal) } finally { clearTimeout(timer) } } catch (error) { if (error instanceof JsonRpcResponseError || error instanceof RequestTimeoutError) throw error // Transport-level failures gain process context: exit code + stderr tail. await this.settleStreams() throw this.closedError(errorMessage(error)) } } /** * Subscribe to server notifications. * @param filter - optional predicate; omitted means every notification. * @returns the subscription handle; close it to stop delivery. After * {@link close} or runtime death the handle is born failed — there is no * producer left, so `next()` rejects instead of waiting forever. */ subscribe(filter?: NotificationFilter): NotificationSubscription { const id = String(this.subscriptionSerial++) const state: SubscriptionState = { queue: [], waiters: [], filter, failure: undefined } const subscription = new NotificationSubscriptionImpl(state, () => { this.subscriptions.delete(id) }) if (this.closeTask !== undefined || this.exitCode !== undefined || this.spawnError !== undefined) { subscription.fail(this.closedError('DeepSeek Harness runtime closed')) return subscription } this.subscriptions.set(id, subscription) return subscription } /** * Subscribe to one session and the descendants discovered from * `subagent.started` lineage edges. The runtime notifies for every session * in its context, so this client applies the scope. * @param sessionId - the root session id. * @returns the filtered subscription handle. */ subscribeSessionTree(sessionId: string): NotificationSubscription { return this.subscribe((notification) => { const params = notification.params if (notification.method === 'subagent.started' || notification.method === 'subagent.finished') { const parentId = params.parentSessionId if (typeof parentId === 'string' && this.isDescendantOf(parentId, sessionId)) return true return params.childSessionId === sessionId } const relatedId = params.sessionId return typeof relatedId === 'string' && this.isDescendantOf(relatedId, sessionId) }) } /** * Shut the runtime down and reap it: a best-effort protocol `shutdown` * bounded by `shutdownTimeoutMs`, then the shared stdin-EOF → SIGTERM → * SIGKILL ladder until the process actually exited. Idempotent. * @returns settlement of the complete teardown. */ close(): Promise { this.closeTask ??= this.performClose() return this.closeTask } private async performClose(): Promise { this.abortUserQuestions(new TransportClosedError('SDK client is closing')) const child = this.child if (child === undefined) return try { await this.request('shutdown', undefined, this.runtime.shutdownTimeoutMs ?? 1_000) } catch (error) { // Diagnostic only: the dispose ladder below is the authoritative teardown // for a runtime that cannot answer shutdown anymore. this.appendStderr([`shutdown request failed: ${errorMessage(error)}`]) } await disposeRuntimeProcess(child, { disposeEofGraceMs: this.runtime.disposeEofGraceMs ?? 6_000, disposeGraceMs: this.runtime.disposeGraceMs ?? 3_000, }) this.transport?.close() this.failSubscriptions(this.closedError('DeepSeek Harness runtime closed')) } private dispatchNotification(notification: HarnessNotification): void { this.recordSessionRelationship(notification) for (const subscription of this.subscriptions.values()) subscription.push(notification) } private recordSessionRelationship(notification: HarnessNotification): void { if (notification.method !== 'subagent.started') return const parentId = notification.params.parentSessionId const childId = notification.params.childSessionId if (typeof parentId === 'string' && parentId !== '' && typeof childId === 'string' && childId !== '' && parentId !== childId) { this.sessionParents.set(childId, parentId) } } private isDescendantOf(sessionId: string, rootSessionId: string): boolean { const visited = new Set() let current = sessionId while (!visited.has(current)) { if (current === rootSessionId) return true visited.add(current) const parent = this.sessionParents.get(current) if (parent === undefined) return false current = parent } // The parent map only ever extends chains upward, so a cycle cannot form. /* v8 ignore next */ return false } private failSubscriptions(error: Error): void { this.abortUserQuestions(error) for (const subscription of this.subscriptions.values()) subscription.fail(error) } private abortUserQuestions(error: Error): void { for (const controller of this.userQuestions.values()) controller.abort(error) } private appendStderr(lines: string[]): void { const kept = lines.filter(line => line.length > 0) this.stderrTail.push(...kept) if (this.stderrTail.length > STDERR_TAIL_LIMIT) { this.stderrTail.splice(0, this.stderrTail.length - STDERR_TAIL_LIMIT) } } private settleStreams(): Promise { return Promise.race([ this.streamsSettled, new Promise((resolve) => { setTimeout(resolve, STREAM_SETTLE_MS) }), ]) } private closedError(reason: string): TransportClosedError { const parts = [`${this.runtime.description}: ${reason}`] if (this.spawnError !== undefined) parts.push(`spawn error: ${this.spawnError.message}`) if (this.exitCode !== undefined) parts.push(`exit code: ${String(this.exitCode)}`) if (this.stderrTail.length > 0) parts.push(`stderr tail:\n${this.stderrTail.join('\n')}`) return new TransportClosedError(parts.join('\n')) } } /** Construct the transport against a generic process for package-local fake-runtime tests. */ export function createProcessHarnessClient(options: RuntimeProcessOptions): HarnessClient { const Constructor = HarnessClient as new ( publicOptions: HarnessClientOptions, runtime: RuntimeProcessOptions, ) => HarnessClient return new Constructor({}, options) } /** * Send routed input for the package's high-level run API; absent from the package exports. * @param client - client owning the initialized runtime connection. * @param sessionId - target Session; an unknown id creates it. * @param input - text or content blocks, without client-side command interpretation. * @returns the validated prompt receipt or native command execution. */ export async function queryInput( client: HarnessClient, sessionId: string, input: string | SdkPromptContentBlock[], ): Promise { const params: SessionQueryParams = { sessionId: SessionId(sessionId), contentBlocks: normalizeInput(input), } const value = await client.request('session/query', params) if (isRecord(value)) { if (value.kind === 'prompt' && typeof value.messageId === 'string' && value.messageId.length > 0) { return { kind: 'prompt', messageId: value.messageId } } if (value.kind === 'command') { return { kind: 'command', execution: commandExecution(value.execution) } } } throw new SdkProtocolError(`session/query returned an invalid routing result: ${JSON.stringify(value)}`) } /** * Normalize routed input: a string becomes one text block; blocks pass verbatim. * @param input - prompt or command text, or content blocks. * @returns the content blocks to send. */ export function normalizeInput(input: string | SdkPromptContentBlock[]): SdkPromptContentBlock[] { return typeof input === 'string' ? [{ type: 'text', text: input }] : input } /** * Whether `value` is a plain JSON object (the wire-boundary shape probe). * @param value - the wire value to probe. * @returns `true` iff `value` is a non-null, non-array object. */ export function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value) } /** Decode the native handler result returned by session/query. */ function commandExecution(value: unknown): CommandExecution { if (isRecord(value) && typeof value.commandId === 'string' && value.commandId.length > 0 && isRecord(value.result)) { const commandId = value.commandId as CommandExecution['commandId'] const { kind, text, sourceEventSeq } = value.result if (kind === 'success' && (text === undefined || typeof text === 'string') && (sourceEventSeq === undefined || (typeof sourceEventSeq === 'number' && Number.isSafeInteger(sourceEventSeq) && sourceEventSeq >= 0))) { return { commandId, result: { kind, ...text === undefined ? {} : { text }, ...sourceEventSeq === undefined ? {} : { sourceEventSeq: SessionSeq(sourceEventSeq) }, } } } if (kind === 'error' && typeof text === 'string' && text.trim().length > 0) { return { commandId, result: { kind, text } } } } throw new SdkProtocolError(`session/query returned an invalid command result: ${JSON.stringify(value)}`) } /** The message of a thrown value (the transport only throws `Error`s; `String` covers the rest). */ function errorMessage(error: unknown): string { /* v8 ignore next -- the transport and dispose ladder reject only with Errors */ return error instanceof Error ? error.message : String(error) } /** Check the question envelope before handing runtime input to application code. */ function isUserQuestionRequest(value: unknown): value is SdkUserQuestionRequest { return isRecord(value) && typeof value.requestId === 'string' && value.requestId.length > 0 && typeof value.sessionId === 'string' && Array.isArray(value.questions) && value.questions.length > 0 && value.questions.every((question: unknown) => isRecord(question) && typeof question.id === 'string' && typeof question.question === 'string') }