Keep provider adapters private

This commit is contained in:
pku-xht
2026-08-05 00:26:39 +08:00
parent 2cf637480c
commit 34b6cb91ed
5 changed files with 28 additions and 33 deletions

View File

@@ -76,11 +76,8 @@ export class ManagedClaudeCodeProcess implements SpawnedProcess {
* @param child - shared handle that remains the process-tree authority.
*/
constructor(private readonly child: SubprocessHandle) {
if (child.stdin === undefined || child.stdout === undefined) {
throw new Error('subagent-claude-code: SDK child requires piped stdin and stdout')
}
this.stdin = child.stdin
this.stdout = child.stdout
this.stdin = child.stdin as NonNullable<SubprocessHandle['stdin']>
this.stdout = child.stdout as NonNullable<SubprocessHandle['stdout']>
// EventEmitter gives `error` special throw semantics without a listener.
// The SDK attaches its listener synchronously after custom spawn returns,
// while this no-op also contains an already-rejected spawn handle.

View File

@@ -20,7 +20,6 @@ import { SessionId } from '@deepseek-ai/dsh-session'
import {
settleRunResult,
subprocessRunHandle,
thrownError,
type SubagentResult,
type SubagentRun,
type SubagentStartRequest,
@@ -39,6 +38,8 @@ import {
/** Default POSIX grace between subprocess termination tiers. */
export const DEFAULT_DISPOSE_GRACE_MS = 3_000
/* jscpd:ignore-start -- sibling providers intentionally keep product-private
* run inputs and error normalization instead of adding a shared lifecycle owner. */
/** Fully resolved inputs for one official Claude Agent SDK query. */
export interface ClaudeCodeRunSpec {
/** Parent Session workspace supplied to the SDK and real CLI. */
@@ -53,6 +54,12 @@ export interface ClaudeCodeRunSpec {
readonly onError?: (error: Error, stopReason: SubagentStopReason) => void
}
function thrown(value: unknown): Error {
/* v8 ignore next -- typed SDK and subprocess failures reject with Error. */
return value instanceof Error ? value : new Error(String(value))
}
/* jscpd:ignore-end */
/**
* Validate and preserve the one-shot task before crossing the SDK boundary.
* @param prompt - task content accepted from the shared subagent service.
@@ -131,7 +138,7 @@ export async function disposeClaudeCodeChild(
try {
query?.close()
} catch (error: unknown) {
failures.push(thrownError(error))
failures.push(thrown(error))
}
if (child.pid > 0) {
@@ -139,13 +146,13 @@ export async function disposeClaudeCodeChild(
try {
await child.waitForExit()
} catch (error: unknown) {
failures.push(thrownError(error))
failures.push(thrown(error))
}
}
try {
await child.done
} catch (error: unknown) {
failures.push(thrownError(error))
failures.push(thrown(error))
}
const firstFailure = failures[0]
@@ -234,7 +241,7 @@ export async function startClaudeCodeRun(
await disposeClaudeCodeChild(query, child)
} catch (disposeError: unknown) {
throw new AggregateError(
[thrownError(error), thrownError(disposeError)],
[thrown(error), thrown(disposeError)],
'subagent-claude-code: startup failed and CLI cleanup also failed',
)
}
@@ -243,7 +250,7 @@ export async function startClaudeCodeRun(
query.close()
} catch (disposeError: unknown) {
throw new AggregateError(
[thrownError(error), thrownError(disposeError)],
[thrown(error), thrown(disposeError)],
'subagent-claude-code: startup failed and query cleanup also failed',
)
}
@@ -252,7 +259,7 @@ export async function startClaudeCodeRun(
if (cancelledBeforeCleanup || request.signal.aborted) {
throw new Error('subagent-claude-code: request was aborted before SDK startup')
}
throw thrownError(error)
throw thrown(error)
}
const publishedQuery = query

View File

@@ -445,7 +445,7 @@ describe('official spawn projection', () => {
expect(process.kill('SIGTERM')).toBe(false)
})
it('emits spawn errors and rejects handles without the required pipes', async () => {
it('emits spawn errors', async () => {
const child = fakeChild()
const process = new ManagedClaudeCodeProcess(child.handle)
const errorListener = vi.fn()
@@ -459,15 +459,6 @@ describe('official spawn projection', () => {
message: 'spawn boom',
}))
expect(removed).not.toHaveBeenCalled()
const missingStdin = fakeChild({ stdin: undefined })
Object.defineProperty(missingStdin.handle, 'stdin', { value: undefined })
expect(() => new ManagedClaudeCodeProcess(missingStdin.handle))
.toThrow('requires piped stdin and stdout')
const missingStdout = fakeChild({ stdout: undefined })
Object.defineProperty(missingStdout.handle, 'stdout', { value: undefined })
expect(() => new ManagedClaudeCodeProcess(missingStdout.handle))
.toThrow('requires piped stdin and stdout')
})
it('exposes a settled direct-child exit code', async () => {