singularity-forge/src/web/hooks-service.ts
Tom Boucher 7a413bb84f fix(web): resolve compiled .js modules for all subprocess calls under node_modules (#2320)
Node v24 unconditionally refuses .ts files under node_modules/ — even
with --experimental-transform-types. When GSD is installed globally via
npm, every web service subprocess that loads a .ts extension module
crashes with ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING.

Add resolveSubprocessModule() and buildSubprocessPrefixArgs() to
ts-subprocess-flags.ts. When packageRoot is under node_modules/ and the
compiled dist/*.js file exists, subprocess calls use the compiled JS
directly without TS flags or the resolve-ts.mjs loader.

Updated all 14 web service files: auto-dashboard, bridge, captures,
cleanup, doctor, export, forensics, history, hooks, recovery-diagnostics,
settings, skill-health, undo, and visualizer.

Fixes #2279

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 07:34:41 -06:00

88 lines
3.4 KiB
TypeScript

import { execFile } from "node:child_process"
import { existsSync } from "node:fs"
import { join } from "node:path"
import { pathToFileURL } from "node:url"
import { resolveBridgeRuntimeConfig } from "./bridge-service.ts"
import { resolveTypeStrippingFlag, resolveSubprocessModule, buildSubprocessPrefixArgs } from "./ts-subprocess-flags.ts"
import type { HooksData } from "../../web/lib/remaining-command-types.ts"
const HOOKS_MAX_BUFFER = 512 * 1024
const HOOKS_MODULE_ENV = "GSD_HOOKS_MODULE"
function resolveTsLoaderPath(packageRoot: string): string {
return join(packageRoot, "src", "resources", "extensions", "gsd", "tests", "resolve-ts.mjs")
}
/**
* Collects hook configuration and status via a child process.
* Runtime state (active cycles, hook queue) is not available in a cold child
* process, so activeCycles will be empty. The child calls getHookStatus() which
* reads from preferences to build entries, then formatHookStatus() for display.
*/
export async function collectHooksData(projectCwdOverride?: string): Promise<HooksData> {
const config = resolveBridgeRuntimeConfig(undefined, projectCwdOverride)
const { packageRoot, projectCwd } = config
const resolveTsLoader = resolveTsLoaderPath(packageRoot)
const moduleResolution = resolveSubprocessModule(packageRoot, "resources/extensions/gsd/post-unit-hooks.ts")
const hooksModulePath = moduleResolution.modulePath
if (!moduleResolution.useCompiledJs && (!existsSync(resolveTsLoader) || !existsSync(hooksModulePath))) {
throw new Error(
`hooks data provider not found; checked=${resolveTsLoader},${hooksModulePath}`,
)
}
if (moduleResolution.useCompiledJs && !existsSync(hooksModulePath)) {
throw new Error(`hooks data provider not found; checked=${hooksModulePath}`)
}
// getHookStatus() internally calls resolvePostUnitHooks() and resolvePreDispatchHooks()
// from preferences.ts, which read from process.cwd()/.gsd/preferences.md.
// We set cwd to projectCwd so preferences resolution finds the right files.
// In a cold child process, cycleCounts is empty, so activeCycles will be {}.
const script = [
'const { pathToFileURL } = await import("node:url");',
`const mod = await import(pathToFileURL(process.env.${HOOKS_MODULE_ENV}).href);`,
'const entries = mod.getHookStatus();',
'const formattedStatus = mod.formatHookStatus();',
'process.stdout.write(JSON.stringify({ entries, formattedStatus }));',
].join(" ")
const prefixArgs = buildSubprocessPrefixArgs(packageRoot, moduleResolution, pathToFileURL(resolveTsLoader).href)
return await new Promise<HooksData>((resolveResult, reject) => {
execFile(
process.execPath,
[
...prefixArgs,
"--eval",
script,
],
{
cwd: projectCwd,
env: {
...process.env,
[HOOKS_MODULE_ENV]: hooksModulePath,
},
maxBuffer: HOOKS_MAX_BUFFER,
},
(error, stdout, stderr) => {
if (error) {
reject(new Error(`hooks data subprocess failed: ${stderr || error.message}`))
return
}
try {
resolveResult(JSON.parse(stdout) as HooksData)
} catch (parseError) {
reject(
new Error(
`hooks data subprocess returned invalid JSON: ${parseError instanceof Error ? parseError.message : String(parseError)}`,
),
)
}
},
)
})
}