Files
msd-core/tests/issue-498-update-context.test.cjs
Tom Boucher 9faacc0c15 test(#3148): bound the long tail and delete the unbounded-spawn allowlist (#3192)
* test(#3148): bound the long tail and delete the allowlist

Migrates the final 170 unbounded sync spawn sites across 49 files, then
removes the allowlist entirely. local/no-unbounded-spawn now runs with no
exemption surface across tests/**: there is no file to add a name to.

drift-detection's throw-native git() helper routes to gitOrThrow -- bare
runGit would have taken 16 call sites quiet on failure. commands.test.cjs
has two independently-scoped runGsdTools/runCli helpers, one already bounded
and one not; they are kept distinct rather than unified, the same trap as the
two same-named git() helpers in Wave 1.

runNpm's bound was erasable. Its options spread callerOptions after the
defaults, so an explicit timeout:undefined silently dropped the 180000ms
bound -- the rule flagged it and was right; it was not a false positive. Fixed
by destructuring with a default, with a test that fails when the default is
removed.

Two sites stay on a raw spawn with an explicit timeout because the seam
cannot express them: one needs shell:true for npm.cmd on Windows, one
redirects stdout to a real fd. Both are the rule's own documented second
option, not an escape from it.

Closure verified rather than asserted: the derivation scan reports 0 unbounded
spawn helpers and 0 unbounded direct git call sites, and a temporary file
carrying an unbounded spawn still errors with the allowlist gone.

Closes #3064.

* test(#3148): close a hole in the guard's own eslint-disable ban

The ban listed only the top level of tests/, so it was blind to 37 .cjs
files under tests/helpers, qa, observability, fixtures and dispatch. With the
allowlist deleted this test is the sole remaining way to detect someone
silencing the rule inline, so the gap was load-bearing: a nested file could
carry an unbounded spawn plus an eslint-disable and pass everything.

Proven before and after. A probe planted under tests/helpers with both was
invisible to the guard and clean under eslint; after making the listing
recursive the guard fails on it. The scanned set goes from 771 files to 808.

Pre-existing since the guard shipped, but this wave is what promoted it to
sole defense, so it is fixed here rather than filed.

Also converts the last hand-rolled throw check to throwIfFailed and the last
re-derived legacy shape to compose toLegacyResult, which makes the epic's
none-remain claim true rather than nearly true. toLegacyResult itself is not
widened -- eight callers depend on its shape and one consumer does not
justify changing a shared contract.

* fix(#3148): correct seam incoherence at the bound and a slow review-lane error path

Two real failures from the remote runner, both fixed at the cause.

The seam could return outcome TIMED_OUT together with exitCode 0. At the
exact bound spawnSync reports ETIMEDOUT while the child has already exited
with a real status, and toSeamResult classified on the error code while
passing status straight through -- an incoherent pair its own boundary test
was written to catch, and did. A status that is not null is direct evidence
the child exited on its own, so it now decides the outcome before the
error-code branches run. process-seam.cjs was deliberately untouched by every
earlier wave; this is a defect in the module itself, kept surgical, with a
unit test that fails against the old logic.

review-lane with an unknown subcommand fell through to its usage error only
after loading the capability registry and building a per-lane plan, which
spawns one child process per lane -- up to twelve. The error path took
~1288ms instead of ~119ms, and under bench load it outran a caller's spawn
timeout and was killed before writing anything, which is the empty stdout and
stderr CI saw. It now fails fast before any of that work begins.

This is the epic's first production change. It is user-facing, so it carries
a changeset rather than a no-changelog label.

* test(#3148): replace a real-race timeout test with a deterministic one

E9 raced git rev-parse against a 1ms bound and assumed git always lost. On a
warm container git finishes first, spawnSync returns status 0 with no error
at all, the seam correctly classifies EXITED, and gitOrThrow correctly does
not throw -- so the test failed on both lanes. A probe confirms a genuine
timeout always carries status null, so this was never the seam misbehaving.

Raising the bound would only lengthen the odds, which is the same defect with
better luck. The test now drives gitOrThrow against a stubbed runGit that
returns a synthetic TIMED_OUT result, so it asserts exactly what it always
meant to -- that a timeout propagates as a throw -- with no timing
dependence. Five consecutive runs are identical where the old one varied.

I wrote this test in Wave 0; it is a real-race test by construction and
CLAUDE.md says to replace those rather than re-run them.

* chore(#3148): backfill changeset PR number 3192

---------

Co-authored-by: sim <sim@local>
2026-08-07 21:03:50 -04:00

199 lines
9.2 KiB
JavaScript

'use strict';
process.env.GSD_TEST_MODE = '1';
// Issue #498 (candidate 3): resolveUpdateContext ports update.md's ~280-line
// get_installed_version bash into a pure, injected-fs function. It returns the
// same 4-field contract the workflow emits: { installedVersion, scope, runtime,
// gsdDir }. The fs is injected (exists/readFile) so the precedence cascade is
// finally testable without a live multi-runtime install.
const { test, describe } = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const nodeFs = require('node:fs');
const os = require('node:os');
const { runNode } = require('./helpers/process-seam.cjs');
const { throwIfFailed } = require('./helpers/git-fixture.cjs');
const { PROBE_TIMEOUT_MS } = require('./helpers/timeouts.cjs');
const ROOT = path.join(__dirname, '..');
const GSD_TOOLS = path.join(ROOT, 'gsd-core', 'bin', 'gsd-tools.cjs');
const { cleanup } = require('./helpers.cjs');
const { resolveUpdateContext } = require(
path.join(ROOT, 'gsd-core', 'bin', 'lib', 'update-context.cjs'),
);
// Normalize a path to a platform-agnostic key: resolve to absolute, then
// lowercase forward-slash form. This makes the fake fs match the resolver's
// path.join/path.resolve lookups on Windows (backslash + drive letter) as well
// as POSIX, so these unit tests are not OS-coupled.
function normKey(p) { return path.resolve(p).replace(/\\/g, '/').toLowerCase(); }
// Build an injected fs from a map of absolute path -> contents. Marker files
// (VERSION, workflows/update.md) just need to "exist".
function fakeFs(files) {
const set = new Map();
for (const [k, v] of Object.entries(files)) set.set(normKey(k), v);
return {
exists: (p) => set.has(normKey(p)),
readFile: (p) => { const k = normKey(p); return set.has(k) ? set.get(k) : null; },
};
}
// Compare resolved-dir results without coupling to OS path style.
function sameDir(a, b) { return normKey(a) === normKey(b); }
const HOME = '/home/u';
const CWD = '/work/proj';
function ver(dir) { return `${dir}/gsd-core/VERSION`; }
function marker(dir) { return `${dir}/gsd-core/workflows/update.md`; }
describe('resolveUpdateContext: scope cascade', () => {
test('GLOBAL claude install under $HOME/.claude', () => {
const fs = fakeFs({ [ver(`${HOME}/.claude`)]: '1.40.0\n', [marker(`${HOME}/.claude`)]: 'x' });
const r = resolveUpdateContext({ home: HOME, cwd: CWD, env: {}, fs });
assert.equal(r.installedVersion, '1.40.0');
assert.equal(r.scope, 'GLOBAL');
assert.equal(r.runtime, 'claude');
assert.ok(sameDir(r.gsdDir, `${HOME}/.claude`), `gsdDir was ${r.gsdDir}`);
});
test('LOCAL install under ./.claude takes priority over global', () => {
const fs = fakeFs({
[ver(`${CWD}/.claude`)]: '1.39.0\n', [marker(`${CWD}/.claude`)]: 'x',
[ver(`${HOME}/.claude`)]: '1.40.0\n', [marker(`${HOME}/.claude`)]: 'x',
});
const r = resolveUpdateContext({ home: HOME, cwd: CWD, env: {}, fs });
assert.equal(r.scope, 'LOCAL');
assert.equal(r.installedVersion, '1.39.0');
assert.ok(sameDir(r.gsdDir, `${CWD}/.claude`), `gsdDir was ${r.gsdDir}`);
});
test('cwd === home does NOT misdetect as LOCAL (dedup)', () => {
const fs = fakeFs({ [ver(`${HOME}/.claude`)]: '1.40.0\n', [marker(`${HOME}/.claude`)]: 'x' });
const r = resolveUpdateContext({ home: HOME, cwd: HOME, env: {}, fs });
assert.equal(r.scope, 'GLOBAL');
});
test('runtime detected but VERSION missing -> 0.0.0, keep scope/runtime', () => {
const fs = fakeFs({ [marker(`${HOME}/.codex`)]: 'x' });
const r = resolveUpdateContext({ home: HOME, cwd: CWD, env: {}, fs });
assert.equal(r.installedVersion, '0.0.0');
assert.equal(r.scope, 'GLOBAL');
assert.equal(r.runtime, 'codex');
});
test('no install anywhere -> UNKNOWN / claude / empty gsdDir', () => {
const r = resolveUpdateContext({ home: HOME, cwd: CWD, env: {}, fs: fakeFs({}) });
assert.deepEqual(r, { installedVersion: '0.0.0', scope: 'UNKNOWN', runtime: 'claude', gsdDir: '' });
});
});
describe('resolveUpdateContext: runtime probing + env overrides', () => {
test('opencode global under $HOME/.config/opencode', () => {
const dir = `${HOME}/.config/opencode`;
const fs = fakeFs({ [ver(dir)]: '1.40.0\n', [marker(dir)]: 'x' });
const r = resolveUpdateContext({ home: HOME, cwd: CWD, env: {}, fs });
assert.equal(r.runtime, 'opencode');
assert.ok(sameDir(r.gsdDir, dir), `gsdDir was ${r.gsdDir}`);
});
test('CLAUDE_CONFIG_DIR env override locates a custom global dir', () => {
const custom = '/opt/claude-home';
const fs = fakeFs({ [ver(custom)]: '1.40.0\n', [marker(custom)]: 'x' });
const r = resolveUpdateContext({ home: HOME, cwd: CWD, env: { CLAUDE_CONFIG_DIR: custom }, fs });
assert.equal(r.scope, 'GLOBAL');
assert.equal(r.runtime, 'claude');
assert.ok(sameDir(r.gsdDir, custom), `gsdDir was ${r.gsdDir}`);
});
test('preferredConfigDir fast-path: trusts a validated custom dir as GLOBAL', () => {
const custom = '/opt/gsd-x';
const fs = fakeFs({ [ver(custom)]: '1.41.0\n', [marker(custom)]: 'x' });
const r = resolveUpdateContext({
home: HOME, cwd: CWD, env: {}, fs,
preferredConfigDir: custom, preferredRuntime: 'kilo',
});
assert.equal(r.scope, 'GLOBAL');
assert.equal(r.runtime, 'kilo');
assert.ok(sameDir(r.gsdDir, custom), `gsdDir was ${r.gsdDir}`);
assert.equal(r.installedVersion, '1.41.0');
});
});
describe('gsd-tools update-context (CLI): emits the JSON contract', () => {
test('--config-dir fixture resolves to the documented 4-field JSON', () => {
const tmp = nodeFs.mkdtempSync(path.join(os.tmpdir(), 'gsd-uc-'));
try {
nodeFs.mkdirSync(path.join(tmp, 'gsd-core', 'workflows'), { recursive: true });
nodeFs.writeFileSync(path.join(tmp, 'gsd-core', 'VERSION'), '1.42.0\n');
nodeFs.writeFileSync(path.join(tmp, 'gsd-core', 'workflows', 'update.md'), 'x');
const r = runNode(
[GSD_TOOLS, 'update-context', '--config-dir', tmp, '--runtime', 'kilo', '--json'],
{ env: { ...process.env, GSD_TEST_MODE: '1' }, timeoutMs: PROBE_TIMEOUT_MS },
);
throwIfFailed(r, 'gsd-tools update-context --json');
const ctx = JSON.parse(r.stdout);
assert.deepEqual(Object.keys(ctx).sort(), ['gsdDir', 'installedVersion', 'runtime', 'scope']);
assert.equal(ctx.installedVersion, '1.42.0');
assert.equal(ctx.scope, 'GLOBAL');
assert.equal(ctx.runtime, 'kilo');
} finally {
cleanup(tmp);
}
});
});
describe('resolveUpdateContext: parity with the old inline bash (adversarial-review)', () => {
test('preferredConfigDir with a leading ~/ is expanded before the fast path', () => {
// The old inline bash ran `expand_home "$PREFERRED_CONFIG_DIR"` first, so a
// custom --config-dir like ~/custom-gsd must resolve, not fall to UNKNOWN.
const fs = fakeFs({
[ver(`${HOME}/custom-gsd`)]: '1.41.0\n',
[marker(`${HOME}/custom-gsd`)]: 'x',
});
const r = resolveUpdateContext({
home: HOME, cwd: CWD, env: {}, fs, preferredConfigDir: '~/custom-gsd',
});
assert.equal(r.installedVersion, '1.41.0');
assert.equal(r.scope, 'GLOBAL');
assert.ok(sameDir(r.gsdDir, `${HOME}/custom-gsd`), `gsdDir was ${r.gsdDir}`);
});
test('a VERSION-only dir (no update.md marker) is NOT trusted as a real version', () => {
// The old cascade required BOTH VERSION and the update.md marker before
// trusting the version; a partial dir falls to 0.0.0 but keeps scope.
const fs = fakeFs({ [ver(`${HOME}/.claude`)]: '1.40.0\n' }); // marker absent
const r = resolveUpdateContext({ home: HOME, cwd: CWD, env: {}, fs });
assert.equal(r.installedVersion, '0.0.0', 'VERSION-only dir must not be trusted');
assert.equal(r.scope, 'GLOBAL');
assert.equal(r.runtime, 'claude');
assert.ok(sameDir(r.gsdDir, `${HOME}/.claude`), `gsdDir was ${r.gsdDir}`);
});
test('fast path also requires the marker: VERSION-only preferredConfigDir -> 0.0.0', () => {
// The "trust = VERSION + marker" rule is consistent across every path, not
// just the cascade. A custom --config-dir with VERSION but no marker is a
// partial install: keep the dir/scope, report 0.0.0.
const custom = '/opt/gsd-partial';
const fs = fakeFs({ [ver(custom)]: '1.41.0\n' }); // marker absent
const r = resolveUpdateContext({
home: HOME, cwd: CWD, env: {}, fs, preferredConfigDir: custom, preferredRuntime: 'kilo',
});
assert.equal(r.installedVersion, '0.0.0', 'VERSION-only fast path must not be trusted');
assert.equal(r.scope, 'GLOBAL');
assert.ok(sameDir(r.gsdDir, custom), `gsdDir was ${r.gsdDir}`);
});
test('partial install with cwd===home does NOT misdetect as LOCAL (fallback dedup)', () => {
// Same same-path dedup the trusted path uses must apply to the 0.0.0
// fallback: a VERSION-only ~/.claude probed from cwd===home is GLOBAL.
const fs = fakeFs({ [ver(`${HOME}/.claude`)]: '1.40.0\n' }); // marker absent
const r = resolveUpdateContext({ home: HOME, cwd: HOME, env: {}, fs });
assert.equal(r.installedVersion, '0.0.0');
assert.equal(r.scope, 'GLOBAL', 'cwd===home partial must be GLOBAL, not LOCAL');
assert.ok(sameDir(r.gsdDir, `${HOME}/.claude`), `gsdDir was ${r.gsdDir}`);
});
});