import type { QueryResult } from './query/utils.js'; import type { QueryRegistry } from './query/registry.js'; import type { TransportMode } from './gsd-transport-policy.js'; import { toFailureSignal } from './query-failure-classification.js'; export interface TransportRequest { legacyCommand: string; legacyArgs: string[]; registryCommand: string; registryArgs: string[]; mode: TransportMode; projectDir: string; workstream?: string; } export interface TransportAdapters { dispatchNative: (request: TransportRequest) => Promise; execSubprocessJson: (legacyCommand: string, legacyArgs: string[]) => Promise; execSubprocessRaw: (legacyCommand: string, legacyArgs: string[]) => Promise; formatNativeRaw?: (registryCommand: string, data: unknown) => string; } export interface TransportPolicyLike { preferNative: boolean; allowFallbackToSubprocess: boolean; } export class GSDTransport { constructor( private readonly registry: QueryRegistry, private readonly adapters: TransportAdapters, ) {} async run(request: TransportRequest, policy: TransportPolicyLike): Promise { if (this.shouldUseNative(request, policy)) { try { const native = await this.adapters.dispatchNative(request); return this.projectNativeOutput(request, native.data); } catch (error) { if (this.shouldRethrowNativeError(error, policy)) throw error; } } return this.dispatchSubprocess(request); } private shouldUseNative(request: TransportRequest, policy: TransportPolicyLike): boolean { const forceSubprocess = Boolean(request.workstream); return !forceSubprocess && policy.preferNative && this.registry.has(request.registryCommand); } private shouldRethrowNativeError(error: unknown, policy: TransportPolicyLike): boolean { if (!policy.allowFallbackToSubprocess) return true; // Do not subprocess-fallback after a timed-out native dispatch: // the timeout does not cancel the native handler, so falling through // would run the same command twice (double-execution race). return toFailureSignal(error).kind === 'timeout'; } private dispatchSubprocess(request: TransportRequest): Promise { if (request.mode === 'raw') { return this.adapters.execSubprocessRaw(request.legacyCommand, request.legacyArgs); } return this.adapters.execSubprocessJson(request.legacyCommand, request.legacyArgs); } private projectNativeOutput(request: TransportRequest, data: unknown): unknown { if (request.mode === 'raw') { if (this.adapters.formatNativeRaw) { return this.adapters.formatNativeRaw(request.registryCommand, data).trim(); } return this.toRaw(data); } return data; } private toRaw(data: unknown): string { if (typeof data === 'string') return data.trim(); const json = JSON.stringify(data, null, 2); if (json == null) return ''; return json.trim(); } }