refactor(#190): collapse SDK sync bridge and remove synckit seam
This commit is contained in:
@@ -199,19 +199,10 @@ const { routePhasesCommand } = require('./lib/phases-command-router.cjs');
|
||||
const { routeValidateCommand } = require('./lib/validate-command-router.cjs');
|
||||
const { routeRoadmapCommand } = require('./lib/roadmap-command-router.cjs');
|
||||
|
||||
// ─── SDK bridge (Phase 6 inline family / non-family delegation) ───────────────
|
||||
// For inline case blocks that have SDK counterparts (frontmatter, config, and
|
||||
// non-family commands), we attempt to dispatch via executeForCjs (the sync
|
||||
// bridge). CJS handlers are retained as fallback when SDK is unavailable.
|
||||
//
|
||||
// NOTE: migrate-config, detect-custom-files, config-path, and find-phase
|
||||
// are CJS-native special cases; see comments inline.
|
||||
|
||||
// Shared loader for the synchronous SDK runtime bridge; see
|
||||
// `bin/lib/cjs-sdk-bridge.cjs`. All canonical-command CJS dispatchers (the
|
||||
// per-family routers and the non-family helper below) consume the same loader
|
||||
// so a change to the SDK-load contract lands in one place.
|
||||
const { tryLoadSdk: _tryLoadSdkBridge, getExecuteForCjs } = require('./lib/cjs-sdk-bridge.cjs');
|
||||
// ─── Bridge collapsed (Phase 4) ────────────────────────────────────────────────
|
||||
// Non-family commands now run through their CJS handlers directly. Keep the
|
||||
// helper contract so existing call sites remain unchanged during the phase
|
||||
// sequence; it always returns false so callers fall through to CJS.
|
||||
|
||||
/**
|
||||
* Attempt SDK dispatch for a non-family command.
|
||||
@@ -231,55 +222,15 @@ const { tryLoadSdk: _tryLoadSdkBridge, getExecuteForCjs } = require('./lib/cjs-s
|
||||
* @param {Function} opts.output - output emitter (core.output)
|
||||
*/
|
||||
function _dispatchNonFamily({ registryCommand, registryArgs, legacyCommand, legacyArgs, cwd, raw, error, output }) {
|
||||
if (!_tryLoadSdkBridge()) return false;
|
||||
let result;
|
||||
try {
|
||||
result = getExecuteForCjs()({
|
||||
registryCommand,
|
||||
registryArgs,
|
||||
legacyCommand,
|
||||
legacyArgs,
|
||||
// Always request typed JSON from the bridge; CJS `output(data, raw)` handles
|
||||
// user-facing rendering. Passing `mode: 'raw'` would make the bridge
|
||||
// pre-render result.data to a JSON string that the CJS output path then
|
||||
// double-stringifies (returning a JSON string of a JSON string).
|
||||
mode: 'json',
|
||||
projectDir: cwd,
|
||||
workstream: process.env.GSD_WORKSTREAM || undefined,
|
||||
});
|
||||
} catch {
|
||||
// Bridge threw (e.g. synckit worker crash, Atomics failure on Windows).
|
||||
// Return false so the caller falls through to the CJS handler.
|
||||
return false;
|
||||
}
|
||||
if (!result.ok) {
|
||||
const message = (result.errorDetails && result.errorDetails.message)
|
||||
|| `${legacyCommand} (${registryCommand}) failed (${result.errorKind})`;
|
||||
// Propagate the structured reason code through to CJS `error()` so the
|
||||
// `--json-errors` JSON-shaped stderr carries the typed reason (e.g.
|
||||
// 'config_key_not_found') instead of the generic 'unknown'. Handlers
|
||||
// tag the GSDError with `.reason` and the worker forwards it via
|
||||
// errorDetails.reason. (Bugs #2943, #3086.)
|
||||
const reason = result.errorDetails && result.errorDetails.reason;
|
||||
if (reason) {
|
||||
error(message, reason);
|
||||
} else {
|
||||
error(message);
|
||||
}
|
||||
return true; // handled (error reported)
|
||||
}
|
||||
// CJS parity for --raw output (config.cjs:525 `output(value, raw, String(value))`):
|
||||
// when the caller asked for --raw and the SDK returned a scalar, pass that
|
||||
// scalar through as `rawValue` so core.output() emits the bare string
|
||||
// representation instead of JSON-stringifying it. Non-scalar shapes fall
|
||||
// through to the structured JSON path, matching `output(obj, raw)`.
|
||||
const data = result.data;
|
||||
if (raw && (typeof data === 'string' || typeof data === 'number' || typeof data === 'boolean')) {
|
||||
output(data, raw, String(data));
|
||||
} else {
|
||||
output(data, raw);
|
||||
}
|
||||
return true;
|
||||
void registryCommand;
|
||||
void registryArgs;
|
||||
void legacyCommand;
|
||||
void legacyArgs;
|
||||
void cwd;
|
||||
void raw;
|
||||
void error;
|
||||
void output;
|
||||
return false;
|
||||
}
|
||||
|
||||
// ─── Arg parsing helpers ──────────────────────────────────────────────────────
|
||||
|
||||
@@ -1,136 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* CJS↔SDK Sync Runtime Bridge Adapter — Phase 5/6 of #3524.
|
||||
*
|
||||
* Single shared loader for the synchronous SDK runtime bridge that every CJS
|
||||
* command-router family file and `gsd-tools.cjs` non-family dispatcher
|
||||
* delegates through. Centralizing the load prevents the seven-fold duplicated
|
||||
* `tryLoadSdk` blocks that existed across the routers from drifting against
|
||||
* each other (the exact anti-pattern the Phase 6 hand-sync lint is meant to
|
||||
* stop, applied to the SDK-load logic itself).
|
||||
*
|
||||
* Load path policy: the bridge resolves the bundled SDK by package-relative
|
||||
* filesystem path, NOT by the `@opengsd/gsd-sdk` package name. The package name
|
||||
* is not installed in the root `node_modules` (it lives as a sibling workspace
|
||||
* package, not a dependency), and the SDK's public entry doesn't re-export
|
||||
* `executeForCjs` or `formatStateLoadRawStdout` anyway. Using the relative
|
||||
* path means the loader works identically in (a) the development checkout
|
||||
* (`<repo>/sdk/dist/...`) and (b) the published package layout
|
||||
* (`node_modules/get-shit-done-redux/sdk/dist/...`) because the `files` array in
|
||||
* `package.json` keeps `sdk/dist` at the same path inside the published
|
||||
* tarball.
|
||||
*
|
||||
* The previous implementation used `require('@opengsd/gsd-sdk')`, which always
|
||||
* failed because the package was unresolvable from the consumer location.
|
||||
* That cached `_loadFailed = true` for the lifetime of the process and made
|
||||
* every router silently fall through to CJS — defeating Phase 5/6's entire
|
||||
* goal. The integration test at `tests/cjs-sdk-bridge-integration.test.cjs`
|
||||
* locks the load-success invariant so this regression cannot recur.
|
||||
*
|
||||
* Usage:
|
||||
* const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
* if (tryLoadSdk()) {
|
||||
* const result = getExecuteForCjs()({ ... });
|
||||
* }
|
||||
*
|
||||
* Plus `getFormatStateLoadRawStdout()` for the `state load --raw` adapter and
|
||||
* `getSdkModule()` for routers that need the raw runtime-bridge-sync module.
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
|
||||
// Computed once at module load. Resolves the bundled SDK relative to this
|
||||
// file's on-disk location, so both dev and post-install layouts work.
|
||||
// <root>/get-shit-done/bin/lib/cjs-sdk-bridge.cjs
|
||||
// <root>/sdk/dist/runtime-bridge-sync/index.js
|
||||
// <root>/sdk/dist/query/state-project-load.js
|
||||
const RUNTIME_BRIDGE_PATH = path.resolve(
|
||||
__dirname,
|
||||
'..',
|
||||
'..',
|
||||
'..',
|
||||
'sdk',
|
||||
'dist',
|
||||
'runtime-bridge-sync',
|
||||
'index.js',
|
||||
);
|
||||
const STATE_PROJECT_LOAD_PATH = path.resolve(
|
||||
__dirname,
|
||||
'..',
|
||||
'..',
|
||||
'..',
|
||||
'sdk',
|
||||
'dist',
|
||||
'query',
|
||||
'state-project-load.js',
|
||||
);
|
||||
|
||||
let _runtimeBridge = null;
|
||||
let _formatStateLoadRawStdout = null;
|
||||
let _loadFailed = false;
|
||||
|
||||
/**
|
||||
* Load the bundled SDK runtime bridge once and cache the result. Returns true
|
||||
* on success, false if the dist artifacts are missing (e.g. `npm run
|
||||
* build:sdk` has not been executed in a fresh dev checkout) or if the
|
||||
* expected `executeForCjs` export is absent. Cached result is reused on
|
||||
* subsequent calls.
|
||||
*/
|
||||
function tryLoadSdk() {
|
||||
if (_runtimeBridge) return true;
|
||||
if (_loadFailed) return false;
|
||||
try {
|
||||
// eslint-disable-next-line global-require
|
||||
const bridge = require(RUNTIME_BRIDGE_PATH);
|
||||
if (typeof bridge.executeForCjs !== 'function') {
|
||||
_loadFailed = true;
|
||||
return false;
|
||||
}
|
||||
// eslint-disable-next-line global-require
|
||||
const stateProjectLoad = require(STATE_PROJECT_LOAD_PATH);
|
||||
if (typeof stateProjectLoad.formatStateLoadRawStdout !== 'function') {
|
||||
_loadFailed = true;
|
||||
return false;
|
||||
}
|
||||
_runtimeBridge = bridge;
|
||||
_formatStateLoadRawStdout = stateProjectLoad.formatStateLoadRawStdout;
|
||||
return true;
|
||||
} catch {
|
||||
_loadFailed = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cached `executeForCjs` function, or null if `tryLoadSdk()` has
|
||||
* not been called or returned false. Callers must check `tryLoadSdk()` first.
|
||||
*/
|
||||
function getExecuteForCjs() {
|
||||
return _runtimeBridge ? _runtimeBridge.executeForCjs : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cached `formatStateLoadRawStdout` function, or null. Used by
|
||||
* the state command router for the `state load --raw` adapter that projects
|
||||
* SDK return data into the legacy key=value lines format.
|
||||
*/
|
||||
function getFormatStateLoadRawStdout() {
|
||||
return _formatStateLoadRawStdout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cached runtime-bridge-sync module object after a successful
|
||||
* `tryLoadSdk()`, or null. Provided for callers that need additional named
|
||||
* exports beyond `executeForCjs`.
|
||||
*/
|
||||
function getSdkModule() {
|
||||
return _runtimeBridge;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
tryLoadSdk,
|
||||
getExecuteForCjs,
|
||||
getFormatStateLoadRawStdout,
|
||||
getSdkModule,
|
||||
};
|
||||
@@ -2,10 +2,6 @@
|
||||
|
||||
const { INIT_SUBCOMMANDS } = require('./command-aliases.cjs');
|
||||
const { routeCjsCommandFamily } = require('./cjs-command-router-adapter.cjs');
|
||||
const { output } = require('./core.cjs');
|
||||
|
||||
// ─── SDK bridge (Phase 6) — shared loader via cjs-sdk-bridge.cjs ──────────────
|
||||
const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
|
||||
/**
|
||||
* Manifest-backed init subcommand router.
|
||||
@@ -20,44 +16,8 @@ const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
* SDK-only (unsupported in CJS router): none.
|
||||
*/
|
||||
function routeInitCommand({ init, args, cwd, raw, parseNamedArgs, error }) {
|
||||
const activeWorkstream = process.env.GSD_WORKSTREAM;
|
||||
const sdkAvailable = !activeWorkstream && tryLoadSdk();
|
||||
|
||||
function sdkHandler(registryCommand, registryArgs, legacyArgs, cjsFallback) {
|
||||
if (!sdkAvailable) return cjsFallback;
|
||||
return () => {
|
||||
let result;
|
||||
try {
|
||||
result = getExecuteForCjs()({
|
||||
registryCommand,
|
||||
registryArgs,
|
||||
legacyCommand: 'init',
|
||||
legacyArgs,
|
||||
// #3631: under --raw, request mode:'raw' so the bridge runs the SDK's
|
||||
// raw projection (formatQueryRawOutput) and returns the scalar string
|
||||
// CJS callers used to print. We then bypass output()'s JSON-stringify
|
||||
// path by passing rawValue (the third positional). With mode:'json',
|
||||
// output() emits the JSON IR as before.
|
||||
mode: raw ? 'raw' : 'json',
|
||||
projectDir: cwd,
|
||||
});
|
||||
} catch {
|
||||
// Bridge threw (e.g. synckit worker crash, Atomics failure on Windows).
|
||||
// Fall through to CJS handler — the CJS path is the designed safety net.
|
||||
return cjsFallback();
|
||||
}
|
||||
if (!result.ok) {
|
||||
error(result.errorDetails && result.errorDetails.message
|
||||
? result.errorDetails.message
|
||||
: `init ${registryCommand} failed (${result.errorKind})`);
|
||||
return;
|
||||
}
|
||||
if (raw) {
|
||||
output(null, true, typeof result.data === 'string' ? result.data : String(result.data ?? ''));
|
||||
} else {
|
||||
output(result.data);
|
||||
}
|
||||
};
|
||||
function sdkHandler(_registryCommand, _registryArgs, _legacyArgs, cjsFallback) {
|
||||
return cjsFallback;
|
||||
}
|
||||
|
||||
routeCjsCommandFamily({
|
||||
|
||||
@@ -2,10 +2,6 @@
|
||||
|
||||
const { PHASES_SUBCOMMANDS } = require('./command-aliases.cjs');
|
||||
const { routeCjsCommandFamily } = require('./cjs-command-router-adapter.cjs');
|
||||
const { output } = require('./core.cjs');
|
||||
|
||||
// ─── SDK bridge (Phase 6) — shared loader via cjs-sdk-bridge.cjs ──────────────
|
||||
const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
|
||||
/**
|
||||
* Manifest-backed phases subcommand router.
|
||||
@@ -25,37 +21,8 @@ const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
* CJS-only subcommands: none.
|
||||
*/
|
||||
function routePhasesCommand({ phase, milestone, args, cwd, raw, error }) {
|
||||
const activeWorkstream = process.env.GSD_WORKSTREAM;
|
||||
const sdkAvailable = !activeWorkstream && tryLoadSdk();
|
||||
|
||||
function sdkHandler(registryCommand, registryArgs, legacyArgs, cjsFallback) {
|
||||
if (!sdkAvailable) return cjsFallback;
|
||||
return () => {
|
||||
const result = getExecuteForCjs()({
|
||||
registryCommand,
|
||||
registryArgs,
|
||||
legacyCommand: 'phases',
|
||||
legacyArgs,
|
||||
// #3631: under --raw, request mode:'raw' so the bridge runs the SDK's
|
||||
// raw projection (formatQueryRawOutput) and returns the scalar string
|
||||
// CJS callers used to print. We then bypass output()'s JSON-stringify
|
||||
// path by passing rawValue (the third positional). With mode:'json',
|
||||
// output() emits the JSON IR as before.
|
||||
mode: raw ? 'raw' : 'json',
|
||||
projectDir: cwd,
|
||||
});
|
||||
if (!result.ok) {
|
||||
error(result.errorDetails && result.errorDetails.message
|
||||
? result.errorDetails.message
|
||||
: `phases ${registryCommand} failed (${result.errorKind})`);
|
||||
return;
|
||||
}
|
||||
if (raw) {
|
||||
output(null, true, typeof result.data === 'string' ? result.data : String(result.data ?? ''));
|
||||
} else {
|
||||
output(result.data);
|
||||
}
|
||||
};
|
||||
function sdkHandler(_registryCommand, _registryArgs, _legacyArgs, cjsFallback) {
|
||||
return cjsFallback;
|
||||
}
|
||||
|
||||
routeCjsCommandFamily({
|
||||
|
||||
@@ -2,10 +2,6 @@
|
||||
|
||||
const { ROADMAP_SUBCOMMANDS } = require('./command-aliases.cjs');
|
||||
const { routeCjsCommandFamily } = require('./cjs-command-router-adapter.cjs');
|
||||
const { output } = require('./core.cjs');
|
||||
|
||||
// ─── SDK bridge (Phase 6) — shared loader via cjs-sdk-bridge.cjs ──────────────
|
||||
const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
|
||||
/**
|
||||
* Manifest-backed roadmap subcommand router.
|
||||
@@ -20,50 +16,8 @@ const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
* SDK-only (unsupported in CJS router): none.
|
||||
*/
|
||||
function routeRoadmapCommand({ roadmap, args, cwd, raw, error }) {
|
||||
const activeWorkstream = process.env.GSD_WORKSTREAM;
|
||||
// GSD_SDK_NESTED is set by SDK handlers that spawn gsd-tools.cjs as a
|
||||
// child process (e.g. roadmapAnnotateDependencies). Without this guard
|
||||
// the child process re-dispatches through the SDK bridge, which spawns
|
||||
// again, ad infinitum until the synckit 15s timeout fires. Bug #3537
|
||||
// annotate-dependencies parity.
|
||||
const nested = process.env.GSD_SDK_NESTED === '1';
|
||||
const sdkAvailable = !activeWorkstream && !nested && tryLoadSdk();
|
||||
|
||||
function sdkHandler(registryCommand, registryArgs, legacyArgs, cjsFallback) {
|
||||
if (!sdkAvailable) return cjsFallback;
|
||||
return () => {
|
||||
let result;
|
||||
try {
|
||||
result = getExecuteForCjs()({
|
||||
registryCommand,
|
||||
registryArgs,
|
||||
legacyCommand: 'roadmap',
|
||||
legacyArgs,
|
||||
// #3631: under --raw, request mode:'raw' so the bridge runs the SDK's
|
||||
// raw projection (formatQueryRawOutput) and returns the scalar string
|
||||
// CJS callers used to print. We then bypass output()'s JSON-stringify
|
||||
// path by passing rawValue (the third positional). With mode:'json',
|
||||
// output() emits the JSON IR as before.
|
||||
mode: raw ? 'raw' : 'json',
|
||||
projectDir: cwd,
|
||||
});
|
||||
} catch {
|
||||
// Bridge threw (e.g. synckit worker crash, Atomics failure on Windows).
|
||||
// Fall through to CJS handler — the CJS path is the designed safety net.
|
||||
return cjsFallback();
|
||||
}
|
||||
if (!result.ok) {
|
||||
error(result.errorDetails && result.errorDetails.message
|
||||
? result.errorDetails.message
|
||||
: `roadmap ${registryCommand} failed (${result.errorKind})`);
|
||||
return;
|
||||
}
|
||||
if (raw) {
|
||||
output(null, true, typeof result.data === 'string' ? result.data : String(result.data ?? ''));
|
||||
} else {
|
||||
output(result.data);
|
||||
}
|
||||
};
|
||||
function sdkHandler(_registryCommand, _registryArgs, _legacyArgs, cjsFallback) {
|
||||
return cjsFallback;
|
||||
}
|
||||
|
||||
routeCjsCommandFamily({
|
||||
|
||||
@@ -2,138 +2,6 @@
|
||||
|
||||
const { STATE_SUBCOMMANDS } = require('./command-aliases.cjs');
|
||||
const { routeCjsCommandFamily } = require('./cjs-command-router-adapter.cjs');
|
||||
const { output } = require('./core.cjs');
|
||||
const {
|
||||
tryLoadSdk,
|
||||
getExecuteForCjs,
|
||||
getFormatStateLoadRawStdout,
|
||||
} = require('./cjs-sdk-bridge.cjs');
|
||||
|
||||
// Subcommands whose CJS contract is exit-non-zero (stderr) ONLY when the
|
||||
// underlying STATE.md is missing — not for in-state errors like
|
||||
// "field not found". CJS `cmdStateGet` calls `error('STATE.md not found')` →
|
||||
// exit 1 for the missing-file case but `output({ error: 'Section or field
|
||||
// "X" not found' }, raw)` → exit 0 for the missing-field case. Mutation
|
||||
// commands always use output() (exit 0) even when STATE.md is missing, so
|
||||
// they are absent from this set entirely.
|
||||
const EXIT_ON_STATE_MD_MISSING = new Set(['state.get']);
|
||||
const STATE_MD_MISSING_MESSAGE = 'STATE.md not found';
|
||||
|
||||
// Subcommands whose CJS contract is always exit-0 — they emit
|
||||
// { error: '...' } JSON via output() for every failure case (missing STATE.md,
|
||||
// missing required args, validation failures). When the SDK returns
|
||||
// result.ok === false for these subcommands we must NOT call error() (exit 1);
|
||||
// instead we surface the SDK error message as exit-0 JSON so callers can
|
||||
// JSON.parse the response and branch on the error field.
|
||||
const OUTPUT_ON_SDK_ERROR = new Set([
|
||||
'state.record-metric',
|
||||
'state.advance-plan',
|
||||
'state.record-session',
|
||||
'state.add-decision',
|
||||
'state.add-blocker',
|
||||
'state.resolve-blocker',
|
||||
'state.update-progress',
|
||||
]);
|
||||
|
||||
// The bridge loader verifies both `executeForCjs` and `formatStateLoadRawStdout`
|
||||
// are present before returning success, so this router can call `tryLoadSdk()`
|
||||
// directly without an additional capability check.
|
||||
|
||||
/**
|
||||
* Dispatch a subcommand via the SDK sync bridge.
|
||||
*
|
||||
* Returns true if dispatched successfully, false if the SDK is unavailable.
|
||||
* The caller must still handle result.ok=false as a hard error.
|
||||
*
|
||||
* @param {string} registryCommand - Registry command name (e.g. 'state.json')
|
||||
* @param {string[]} registryArgs - Args for the registry handler
|
||||
* @param {string} cwd - Project directory
|
||||
* @param {boolean} raw - Raw output mode
|
||||
* @param {Function} error - Error reporter
|
||||
* @param {Function} [rawFormatter] - Optional raw output formatter (for state.load)
|
||||
* @returns {boolean} true if handled, false to fall through to CJS
|
||||
*/
|
||||
function dispatchViaSdk(registryCommand, registryArgs, legacyArgs, cwd, raw, error, rawFormatter) {
|
||||
if (!tryLoadSdk()) return false;
|
||||
|
||||
// When a CJS-side rawFormatter is supplied (e.g. state.load --raw → key=value
|
||||
// lines), always request 'json' from the bridge so the SDK returns the typed
|
||||
// data object. Passing mode: 'raw' would make the bridge pre-render to a
|
||||
// string and the formatter would no-op. For subcommands without a rawFormatter,
|
||||
// honor the user's --raw flag and let the bridge do default rendering.
|
||||
const bridgeMode = rawFormatter ? 'json' : (raw ? 'raw' : 'json');
|
||||
|
||||
let result;
|
||||
try {
|
||||
result = getExecuteForCjs()({
|
||||
registryCommand,
|
||||
registryArgs,
|
||||
legacyCommand: 'state',
|
||||
legacyArgs,
|
||||
mode: bridgeMode,
|
||||
projectDir: cwd,
|
||||
// Phase 6 fix: workstream is now threaded through to the native handler.
|
||||
// GSDTransport no longer forces subprocess for workstream-scoped requests —
|
||||
// the worker's dispatchNative closure correctly passes workstream to
|
||||
// registry.dispatch() (Phase 5.1 fix), enabling native workstream dispatch.
|
||||
workstream: process.env.GSD_WORKSTREAM || undefined,
|
||||
});
|
||||
} catch {
|
||||
// Bridge threw (e.g. synckit worker crash, Atomics failure on Windows).
|
||||
// Return false so the caller falls through to the CJS handler.
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!result.ok) {
|
||||
// Mutation subcommands whose CJS contract is always exit-0: surface the SDK
|
||||
// error as JSON output (exit 0) rather than calling error() (exit 1). This
|
||||
// preserves the CJS contract for callers that JSON.parse stdout and branch on
|
||||
// the error field — particularly important on Windows/Node 24 where the SDK
|
||||
// bridge returns result.ok===false for validation failures (e.g. missing
|
||||
// required args) instead of propagating them as result.data.error objects.
|
||||
if (OUTPUT_ON_SDK_ERROR.has(registryCommand)) {
|
||||
const msg = result.errorDetails && result.errorDetails.message
|
||||
? result.errorDetails.message
|
||||
: `state ${registryCommand} failed (${result.errorKind})`;
|
||||
output({ error: msg });
|
||||
return true;
|
||||
}
|
||||
error(result.errorDetails && result.errorDetails.message
|
||||
? result.errorDetails.message
|
||||
: `state ${registryCommand} failed (${result.errorKind})`);
|
||||
return true; // handled (error was reported)
|
||||
}
|
||||
|
||||
// Surface STATE.md-missing as a CJS-style fatal error (exit non-zero,
|
||||
// stderr) for the specific subcommands whose CJS contract uses error() not
|
||||
// output() for that case. The exact "STATE.md not found" message is the
|
||||
// canonical signal both CJS and SDK use — other "error" shapes (e.g.
|
||||
// "Section or field X not found" from state.get with present STATE.md)
|
||||
// stay as exit-0 JSON output so shell-script consumers JSON.parse the
|
||||
// output and branch on the error field without process-exit handling.
|
||||
if (
|
||||
EXIT_ON_STATE_MD_MISSING.has(registryCommand)
|
||||
&& result.data
|
||||
&& typeof result.data === 'object'
|
||||
&& result.data.error === STATE_MD_MISSING_MESSAGE
|
||||
) {
|
||||
error(result.data.error);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (raw && rawFormatter) {
|
||||
const rawText = rawFormatter(result.data);
|
||||
const fs = require('fs');
|
||||
fs.writeSync(1, rawText);
|
||||
} else if (raw) {
|
||||
// #3631: bridge was called with mode:'raw', so result.data is the scalar
|
||||
// string the CJS path would have printed. Bypass output()'s JSON path.
|
||||
output(null, true, typeof result.data === 'string' ? result.data : String(result.data ?? ''));
|
||||
} else {
|
||||
output(result.data);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manifest-backed state subcommand router.
|
||||
@@ -156,22 +24,8 @@ function routeStateCommand({ state, args, cwd, raw, parseNamedArgs, error }) {
|
||||
return parsedPlans;
|
||||
};
|
||||
|
||||
// Phase 6 fix: workstream commands are now handled natively in the sync bridge
|
||||
// worker. GSDTransport no longer forces subprocess for workstream-scoped requests;
|
||||
// the worker threads workstream through to registry.dispatch() correctly.
|
||||
const sdkAvailable = tryLoadSdk();
|
||||
|
||||
// Helper: build SDK-backed handler that falls through to CJS on SDK failure.
|
||||
// cjsFallback is called when SDK is unavailable or when the subcommand has no
|
||||
// SDK counterpart.
|
||||
function sdkHandler(registryCommand, registryArgs, legacyArgs, rawFormatter, cjsFallback) {
|
||||
if (!sdkAvailable) return cjsFallback;
|
||||
return () => {
|
||||
const handled = dispatchViaSdk(
|
||||
registryCommand, registryArgs, legacyArgs, cwd, raw, error, rawFormatter,
|
||||
);
|
||||
if (!handled) cjsFallback();
|
||||
};
|
||||
function sdkHandler(_registryCommand, _registryArgs, _legacyArgs, _rawFormatter, cjsFallback) {
|
||||
return cjsFallback;
|
||||
}
|
||||
|
||||
routeCjsCommandFamily({
|
||||
@@ -188,11 +42,7 @@ function routeStateCommand({ state, args, cwd, raw, parseNamedArgs, error }) {
|
||||
'state.load',
|
||||
[],
|
||||
args.slice(1),
|
||||
// Resolved lazily — the formatter getter returns null until
|
||||
// tryLoadSdk() runs inside dispatchViaSdk. sdkHandler only invokes
|
||||
// this formatter when SDK dispatch succeeds, so by then the bridge
|
||||
// has cached the formatter and the getter returns the real function.
|
||||
(...formatterArgs) => getFormatStateLoadRawStdout()(...formatterArgs),
|
||||
null,
|
||||
() => state.cmdStateLoad(cwd, raw),
|
||||
),
|
||||
json: sdkHandler(
|
||||
|
||||
@@ -3,10 +3,6 @@
|
||||
const { VALIDATE_SUBCOMMANDS } = require('./command-aliases.cjs');
|
||||
const { formatGsdSlash, resolveRuntime } = require('./runtime-slash.cjs');
|
||||
const { routeCjsCommandFamily } = require('./cjs-command-router-adapter.cjs');
|
||||
const { output } = require('./core.cjs');
|
||||
|
||||
// ─── SDK bridge (Phase 6) — shared loader via cjs-sdk-bridge.cjs ──────────────
|
||||
const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
|
||||
/**
|
||||
* Manifest-backed validate subcommand router.
|
||||
@@ -25,44 +21,8 @@ const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
* SDK-only (unsupported in CJS router): none.
|
||||
*/
|
||||
function routeValidateCommand({ verify, args, cwd, raw, parseNamedArgs, output: outputFn, error }) {
|
||||
const activeWorkstream = process.env.GSD_WORKSTREAM;
|
||||
const sdkAvailable = !activeWorkstream && tryLoadSdk();
|
||||
|
||||
function sdkHandler(registryCommand, registryArgs, legacyArgs, cjsFallback) {
|
||||
if (!sdkAvailable) return cjsFallback;
|
||||
return () => {
|
||||
let result;
|
||||
try {
|
||||
result = getExecuteForCjs()({
|
||||
registryCommand,
|
||||
registryArgs,
|
||||
legacyCommand: 'validate',
|
||||
legacyArgs,
|
||||
// #3631: under --raw, request mode:'raw' so the bridge runs the SDK's
|
||||
// raw projection (formatQueryRawOutput) and returns the scalar string
|
||||
// CJS callers used to print. We then bypass output()'s JSON-stringify
|
||||
// path by passing rawValue (the third positional). With mode:'json',
|
||||
// output() emits the JSON IR as before.
|
||||
mode: raw ? 'raw' : 'json',
|
||||
projectDir: cwd,
|
||||
});
|
||||
} catch {
|
||||
// Bridge threw (e.g. synckit worker crash, Atomics failure on Windows).
|
||||
// Fall through to CJS handler — the CJS path is the designed safety net.
|
||||
return cjsFallback();
|
||||
}
|
||||
if (!result.ok) {
|
||||
error(result.errorDetails && result.errorDetails.message
|
||||
? result.errorDetails.message
|
||||
: `validate ${registryCommand} failed (${result.errorKind})`);
|
||||
return;
|
||||
}
|
||||
if (raw) {
|
||||
output(null, true, typeof result.data === 'string' ? result.data : String(result.data ?? ''));
|
||||
} else {
|
||||
output(result.data);
|
||||
}
|
||||
};
|
||||
function sdkHandler(_registryCommand, _registryArgs, _legacyArgs, cjsFallback) {
|
||||
return cjsFallback;
|
||||
}
|
||||
|
||||
routeCjsCommandFamily({
|
||||
|
||||
@@ -2,10 +2,6 @@
|
||||
|
||||
const { VERIFY_SUBCOMMANDS } = require('./command-aliases.cjs');
|
||||
const { routeCjsCommandFamily } = require('./cjs-command-router-adapter.cjs');
|
||||
const { output } = require('./core.cjs');
|
||||
|
||||
// ─── SDK bridge (Phase 6) — shared loader via cjs-sdk-bridge.cjs ──────────────
|
||||
const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
|
||||
/**
|
||||
* Manifest-backed verify subcommand router.
|
||||
@@ -20,44 +16,8 @@ const { tryLoadSdk, getExecuteForCjs } = require('./cjs-sdk-bridge.cjs');
|
||||
* SDK-only (unsupported in CJS router): none.
|
||||
*/
|
||||
function routeVerifyCommand({ verify, args, cwd, raw, error }) {
|
||||
const activeWorkstream = process.env.GSD_WORKSTREAM;
|
||||
const sdkAvailable = !activeWorkstream && tryLoadSdk();
|
||||
|
||||
function sdkHandler(registryCommand, registryArgs, legacyArgs, cjsFallback) {
|
||||
if (!sdkAvailable) return cjsFallback;
|
||||
return () => {
|
||||
let result;
|
||||
try {
|
||||
result = getExecuteForCjs()({
|
||||
registryCommand,
|
||||
registryArgs,
|
||||
legacyCommand: 'verify',
|
||||
legacyArgs,
|
||||
// #3631: under --raw, request mode:'raw' so the bridge runs the SDK's
|
||||
// raw projection (formatQueryRawOutput) and returns the scalar string
|
||||
// CJS callers used to print. We then bypass output()'s JSON-stringify
|
||||
// path by passing rawValue (the third positional). With mode:'json',
|
||||
// output() emits the JSON IR as before.
|
||||
mode: raw ? 'raw' : 'json',
|
||||
projectDir: cwd,
|
||||
});
|
||||
} catch {
|
||||
// Bridge threw (e.g. synckit worker crash, Atomics failure on Windows).
|
||||
// Fall through to CJS handler — the CJS path is the designed safety net.
|
||||
return cjsFallback();
|
||||
}
|
||||
if (!result.ok) {
|
||||
error(result.errorDetails && result.errorDetails.message
|
||||
? result.errorDetails.message
|
||||
: `verify ${registryCommand} failed (${result.errorKind})`);
|
||||
return;
|
||||
}
|
||||
if (raw) {
|
||||
output(null, true, typeof result.data === 'string' ? result.data : String(result.data ?? ''));
|
||||
} else {
|
||||
output(result.data);
|
||||
}
|
||||
};
|
||||
function sdkHandler(_registryCommand, _registryArgs, _legacyArgs, cjsFallback) {
|
||||
return cjsFallback;
|
||||
}
|
||||
|
||||
routeCjsCommandFamily({
|
||||
|
||||
Reference in New Issue
Block a user