refactor(query): deepen runtime context/native adapter/output seams (#3076)

* refactor(query): deepen runtime context, native adapter, and cli output seams

* chore(changeset): add fragment for query seam deepening continuation

* refactor(query): converge internal command-resolution imports on canonical seam

* refactor(query): remove dead seam wrappers and converge on canonical modules

* docs(architecture): update context and adr for query seam completion

* fix(query): preserve gsd-tools stderr in cli output and clarify static ws test scope

* test(query): cover whitespace stderr and null exitCode fallback
This commit is contained in:
Tom Boucher
2026-05-03 16:31:48 -04:00
committed by GitHub
parent 5c9f34bd31
commit 9c92c32f6e
28 changed files with 196 additions and 115 deletions

View File

@@ -1,9 +1,9 @@
import { findProjectRoot } from './helpers.js';
import { createRegistry } from './index.js';
import { runQueryDispatch } from './query-dispatch.js';
import { resolveGsdToolsPath, GSDToolsError } from '../gsd-tools.js';
import { GSDError, exitCodeFor } from '../errors.js';
import { validateWorkstreamName } from '../workstream-utils.js';
import { resolveGsdToolsPath } from '../gsd-tools.js';
import { resolveQueryRuntimeContext } from './query-runtime-context.js';
import { createQueryNativeDispatchAdapter } from './query-native-dispatch-adapter.js';
import { buildQueryCliOutputFromDispatch, buildQueryCliOutputFromError, type QueryCliAdapterOutput } from './query-cli-output.js';
export interface QueryCliAdapterInput {
projectDir: string;
@@ -11,11 +11,6 @@ export interface QueryCliAdapterInput {
queryArgv?: string[];
}
export interface QueryCliAdapterOutput {
exitCode: number;
stdoutChunks: string[];
stderrLines: string[];
}
function queryFallbackToCjsEnabled(): boolean {
const v = process.env.GSD_QUERY_FALLBACK?.toLowerCase();
@@ -23,49 +18,21 @@ function queryFallbackToCjsEnabled(): boolean {
return true;
}
function resolveQueryWorkstream(ws: string | undefined): string | undefined {
if (ws !== undefined) {
return validateWorkstreamName(ws) ? ws : undefined;
}
const envWs = process.env.GSD_WORKSTREAM;
if (!envWs) return undefined;
return validateWorkstreamName(envWs) ? envWs : undefined;
}
export async function runQueryCliCommand(input: QueryCliAdapterInput): Promise<QueryCliAdapterOutput> {
const stderrLines: string[] = [];
const stdoutChunks: string[] = [];
const ws = resolveQueryWorkstream(input.ws);
try {
const projectDir = findProjectRoot(input.projectDir);
const runtime = resolveQueryRuntimeContext({ projectDir: input.projectDir, ws: input.ws });
const registry = createRegistry();
const out = await runQueryDispatch({
registry,
projectDir,
ws,
projectDir: runtime.projectDir,
ws: runtime.ws,
cjsFallbackEnabled: queryFallbackToCjsEnabled(),
resolveGsdToolsPath,
dispatchNative: (cmd, argv) => registry.dispatch(cmd, argv, projectDir, ws),
nativeAdapter: createQueryNativeDispatchAdapter(registry, runtime.projectDir, runtime.ws),
}, input.queryArgv ?? []);
stderrLines.push(...out.stderr);
if (!out.ok) {
stderrLines.push(out.error.message);
return { exitCode: out.exit_code, stdoutChunks, stderrLines };
}
if (out.stdout) stdoutChunks.push(out.stdout);
return { exitCode: 0, stdoutChunks, stderrLines };
return buildQueryCliOutputFromDispatch(out);
} catch (err) {
if (err instanceof GSDError) {
stderrLines.push(`Error: ${err.message}`);
return { exitCode: exitCodeFor(err.classification), stdoutChunks, stderrLines };
}
if (err instanceof GSDToolsError) {
stderrLines.push(`Error: ${err.message}`);
return { exitCode: err.exitCode ?? 1, stdoutChunks, stderrLines };
}
stderrLines.push(`Error: ${err instanceof Error ? err.message : String(err)}`);
return { exitCode: 1, stdoutChunks, stderrLines };
return buildQueryCliOutputFromError(err);
}
}