* feat(#3970): per-task external-tracker content-resolution seam Implements ADR-3646 (Phase 1, #3970): a `<task tracker-id="...">` attribute plus a new optional `taskContentResolver` capability-manifest field let a capability resolve a task's action/verify/acceptance-criteria/read_first/done content from an external issue tracker instead of PLAN.md's inline body. - src/plan-document.cts: parses the `tracker-id` attribute into `PlanTask.trackerId` - src/task-content-resolution.cts: new leaf module — split/find/build/resolve, with a hard-halt (throw) contract on ambiguous/failed/timeout/malformed resolution, never a silent fallback to possibly-stale inline text - src/task-command-router.cts: new `task resolve-content --plan --task-id --raw` CLI verb wiring the module into a real process exit code - gsd-core/bin/lib/capability-validator.cjs: validates the new `taskContentResolver` manifest field (feature-role only, cross-capability trackerPrefix uniqueness) - gsd-core/workflows/execute-plan.md, gsd-core/references/loop-hook-dispatch.md, docs/reference/capability-manifest.md: wire the seam into the per-task loop and document it as a new `execute:task` point outside the existing contribution/step/gate vocabulary (unconditional in autonomous mode) Closes #3970 * fix(#3970): gate checkpoint tasks out of content resolution, close trackerPrefix grammar parity gap, cover path-traversal guard Standards/Spec code-review pass on the task-content-resolution seam (ADR-3646 Phase 1) found three defects: 1. execute-plan.md's task-content-resolution bullet fired on any tracker-id-bearing task with no check that it wasn't type="checkpoint:*", contradicting ADR-3646 Decision 1 (a checkpoint task must never enter resolve-content). plan-document.cts already parses trackerId: null unconditionally for checkpoint tasks; only the workflow prose needed the fix, so the bullet now explicitly excludes checkpoint tasks. 2. task-content-resolution.cts's parseResolverDeclaration accepted any non-empty trackerPrefix with no grammar check, while capability- validator.cjs's KEBAB_RE enforces kebab-case at install time — a Generative Fix Divergence gap. Added the same grammar (as a literal regex, documented as intentionally not shared across the .cts/.cjs build boundary) plus a parity test asserting the two surfaces agree across a valid/invalid trackerPrefix table. 3. task-command-router.cts's routeResolveContent path-traversal guard on --plan had zero test coverage. Added a test exercising a ../../../etc/passwit-shaped path and asserting the USAGE rejection names the offending path. * fix(#3970): sanitize resolver diagnostics and cap resolver timeoutMs Two findings caught by an isolated security-review pass on the task content resolution seam: - ResolverFailedError/ResolverMalformedOutputError embedded raw, unsanitized subprocess stderr/stdout (attacker/model-influenced via the tracker-id argv token) into .message. A hostile or buggy resolver could smuggle a newline plus a forged "Error: " line, or terminal escape sequences, into a diagnostic io.cjs's error() writes verbatim to stderr. Fixed at the constructor (task-content-resolution.cts) via io.cjs's existing formatDiagnosticToken(), so every caller of resolveTaskContent gets a safe .message by construction. - capability-validator.cjs's validateTaskContentResolverFields had no upper bound on taskContentResolver.invoke.timeoutMs, letting a manifest declare an effectively unbounded value and defeat the "bounded subprocess" design intent. Added a 120000ms ceiling specific to this field, without touching the shared isPositiveIntegerMs() helper (still used unbounded by the reviewer lane's timeoutFloorMs and probe timeoutMs). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(#3970): fix gsd-test failures — stale prose allowlist line and stderr-vs-message assertion gsd-test (remote dockerized matrix) came back red with 5 failures on this PR; all five are real defects, fixed here. - tests/no-bare-gsd-tools-command-position.test.cjs: PROSE_ALLOWLIST's execute-plan.md entry pointed at line 415, which ffc190df4's checkpoint-exclusion caveat (added near line 221) shifted down by one line. The actual "validated downstream by gsd-tools uat classify-coverage" descriptive mention now sits at line 416. Updated the allowlist entry's line number to match. - tests/task-command-router-resolve-content.test.cjs: the path-traversal test asserted the outside-project-scope diagnostic against the thrown ExitError's own .message. io.cts's error() (ADR-3889) writes its human-readable message to fd 2 via writeAllSync and then throws a bare `new ExitError(1)` with no message argument — by design, so the exception carries no duplicate text and the thrown ExitError's message defaults to "process exit 1" (cli-exit.cts's ExitError constructor). Root cause was the test, not the source: task-command-router.cjs's outside-project-scope rejection already calls error() correctly and the diagnostic text is genuinely emitted, just on fd 2, not on the exception. Fixed the test to capture fd-2 writes (mirroring tests/estimate-calibrate.test.cjs's runCalibrateExpectError and this same file's own captureStdout for fd 1) and assert against the captured stderr text instead of err.message. This was masked locally because a manual `node -e` sanity check that only inspects the caught exception's .message cannot see what the real node:test run actually failed on. Emitted-Drift-Ack-Growth: execute-plan.md — adds the ADR-3646 task-content-resolution bullet and checkpoint-exclusion caveat to the per-task execute loop; a real behavioral prose addition, not incidental bloat. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * docs(#3970): backfill changeset PR number (pr:0 -> pr:4000) --------- Co-authored-by: sim <sim@local> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
316
tests/capability-validator-task-content-resolver.test.cjs
Normal file
316
tests/capability-validator-task-content-resolver.test.cjs
Normal file
@@ -0,0 +1,316 @@
|
||||
// docs-guard-exempt: no docs/ file reads in this test.
|
||||
'use strict';
|
||||
process.env.GSD_TEST_MODE = '1';
|
||||
|
||||
/**
|
||||
* capability-validator-task-content-resolver.test.cjs — behavioral tests for
|
||||
* the OPTIONAL `taskContentResolver` body on `role: "feature"` capability
|
||||
* manifests (ADR-3646, #3970).
|
||||
*
|
||||
* Implements test-matrix rows 20–24 of
|
||||
* `.gsd/phase/feat-3970-task-content-resolution-seam/50-test-matrix.md`.
|
||||
* See `docs/adr/3646-per-task-content-resolution-seam.md` Decision 3 for the
|
||||
* shape this validates.
|
||||
*/
|
||||
|
||||
const { test, describe } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const {
|
||||
validateCapability,
|
||||
validateTaskContentResolver,
|
||||
validateCrossCapability,
|
||||
} = require('../gsd-core/bin/lib/capability-validator.cjs');
|
||||
|
||||
// ─── Fixture builders ──────────────────────────────────────────────────────
|
||||
// House convention (tests/capability-manifest-version.test.cjs): builder
|
||||
// functions return a VALID fixture, which each test then mutates. Every call
|
||||
// returns a FRESH object — no shared mutable state, no execution-order
|
||||
// dependence.
|
||||
|
||||
function validResolver() {
|
||||
return {
|
||||
trackerPrefix: 'beads',
|
||||
invoke: {
|
||||
binary: 'bd',
|
||||
args: ['show', '{{id}}', '--json'],
|
||||
timeoutMs: 10000,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function featureCap(overrides) {
|
||||
return {
|
||||
id: 'demo',
|
||||
role: 'feature',
|
||||
version: '1.2.3',
|
||||
title: 'Demo',
|
||||
description: 'A demo capability.',
|
||||
tier: 'standard',
|
||||
requires: [],
|
||||
engines: { gsd: '>=1.6.0' },
|
||||
runtimeCompat: { supported: ['*'], unsupported: [] },
|
||||
skills: [],
|
||||
agents: [],
|
||||
hooks: [],
|
||||
config: {},
|
||||
steps: [],
|
||||
contributions: [],
|
||||
gates: [],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function runtimeCap(overrides) {
|
||||
return {
|
||||
id: 'demo-rt',
|
||||
role: 'runtime',
|
||||
version: '1.2.3',
|
||||
title: 'Demo RT',
|
||||
description: 'A demo runtime.',
|
||||
tier: 'standard',
|
||||
requires: [],
|
||||
engines: { gsd: '>=1.6.0' },
|
||||
runtime: {
|
||||
configHome: { kind: 'dot-home', name: '.demo', env: [] },
|
||||
localConfigDir: '.demo',
|
||||
configFormat: 'settings-json',
|
||||
artifactLayout: { global: [], local: [] },
|
||||
commandStyle: 'slash-hyphen',
|
||||
hooksSurface: 'settings-json',
|
||||
sandboxTier: 'none',
|
||||
supportTier: 2,
|
||||
installSurface: 'settings-json',
|
||||
writesSharedSettings: false,
|
||||
permissionWriter: null,
|
||||
extendedHookEvents: [],
|
||||
hostIntegration: {
|
||||
embeddingMode: 'imperative',
|
||||
commandSurface: 'slash-file',
|
||||
dispatch: { namedDispatch: true, nested: true, maxDepth: -1, background: true, subagentToolkit: 'full', backgroundDispatch: false },
|
||||
modelMode: 'passive',
|
||||
hookBus: 'host',
|
||||
stateIO: 'filesystem',
|
||||
effortSurface: 'none',
|
||||
isolation: 'process',
|
||||
},
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function reviewerCap(overrides) {
|
||||
return {
|
||||
id: 'demo-reviewer',
|
||||
role: 'reviewer',
|
||||
version: '1.2.3',
|
||||
title: 'Demo Reviewer',
|
||||
description: 'A demo reviewer lane.',
|
||||
tier: 'standard',
|
||||
requires: [],
|
||||
engines: { gsd: '>=1.6.0' },
|
||||
reviewer: {
|
||||
slug: 'demo-reviewer',
|
||||
flags: ['--demo-reviewer'],
|
||||
transport: 'spawn',
|
||||
probe: { kind: 'command-exists', binary: 'demo-reviewer' },
|
||||
invoke: {
|
||||
binary: 'demo-reviewer',
|
||||
args: [],
|
||||
promptChannel: 'stdin',
|
||||
outputChannel: 'stdout',
|
||||
modelArg: null,
|
||||
effortChannel: 'none',
|
||||
},
|
||||
timeoutFloorMs: 5000,
|
||||
emptyOutput: 'stub-with-stderr',
|
||||
reviewsSection: 'Demo Reviewer',
|
||||
evidenceClass: 'source-grounded',
|
||||
requiresBinaries: [],
|
||||
promptBudgetKey: null,
|
||||
handler: null,
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// ─── Row 20: valid taskContentResolver on a feature manifest ───────────────
|
||||
|
||||
describe('row 20 — valid taskContentResolver body', () => {
|
||||
test('valid taskContentResolver body passes', () => {
|
||||
const cap = featureCap({ taskContentResolver: validResolver() });
|
||||
const errs = validateCapability(cap, cap.id);
|
||||
assert.deepEqual(errs, [], `expected no errors, got: ${JSON.stringify(errs)}`);
|
||||
});
|
||||
|
||||
test('omitting taskContentResolver entirely on an otherwise-valid feature manifest yields zero errors', () => {
|
||||
const cap = featureCap();
|
||||
const errs = validateCapability(cap, cap.id);
|
||||
assert.deepEqual(errs, [], `expected no errors, got: ${JSON.stringify(errs)}`);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Row 21: feature-only field ────────────────────────────────────────────
|
||||
|
||||
describe('row 21 — taskContentResolver on non-feature role is rejected', () => {
|
||||
test('role:runtime declaring taskContentResolver is rejected', () => {
|
||||
const cap = runtimeCap({ taskContentResolver: validResolver() });
|
||||
const errs = validateCapability(cap, cap.id);
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('taskContentResolver') && e.includes('feature-only')),
|
||||
`expected a feature-only rejection, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
|
||||
test('role:reviewer declaring taskContentResolver is rejected', () => {
|
||||
const cap = reviewerCap({ taskContentResolver: validResolver() });
|
||||
const errs = validateCapability(cap, cap.id);
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('taskContentResolver') && e.includes('feature-only')),
|
||||
`expected a feature-only rejection, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Row 22: malformed trackerPrefix grammar ───────────────────────────────
|
||||
|
||||
describe('row 22 — malformed trackerPrefix is rejected', () => {
|
||||
test('capital-cased trackerPrefix violates KEBAB_RE', () => {
|
||||
const cap = featureCap({
|
||||
taskContentResolver: { ...validResolver(), trackerPrefix: 'Beads' },
|
||||
});
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('trackerPrefix') && e.includes('kebab-case')),
|
||||
`expected a grammar error naming trackerPrefix, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
|
||||
test('empty-string trackerPrefix is rejected', () => {
|
||||
const cap = featureCap({
|
||||
taskContentResolver: { ...validResolver(), trackerPrefix: '' },
|
||||
});
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('trackerPrefix')),
|
||||
`expected a trackerPrefix error, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Row 23: invoke.timeoutMs boundary ─────────────────────────────────────
|
||||
|
||||
describe('row 23 — invoke.timeoutMs must be a positive integer', () => {
|
||||
for (const bad of [0, -1, 1.5, undefined]) {
|
||||
test(`timeoutMs ${JSON.stringify(bad)} is rejected`, () => {
|
||||
const resolver = validResolver();
|
||||
resolver.invoke = { ...resolver.invoke, timeoutMs: bad };
|
||||
const cap = featureCap({ taskContentResolver: resolver });
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('timeoutMs')),
|
||||
`expected a timeoutMs error for ${JSON.stringify(bad)}, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
test('a legitimate positive integer timeoutMs (10000) is accepted', () => {
|
||||
const cap = featureCap({ taskContentResolver: validResolver() });
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
assert.ok(
|
||||
!errs.some((e) => e.includes('timeoutMs')),
|
||||
`expected no timeoutMs error, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── invoke.timeoutMs upper ceiling (security review finding, #3970) ───────
|
||||
// A manifest declaring an unbounded-in-practice timeoutMs (e.g.
|
||||
// Number.MAX_SAFE_INTEGER) would let resolve-content hang near-indefinitely
|
||||
// on a stuck/malicious resolver, defeating the "bounded subprocess" design
|
||||
// intent. Boundary-inclusive per CLAUDE.md's limit-1/limit/limit+1 rule.
|
||||
|
||||
describe('invoke.timeoutMs upper ceiling (120000ms)', () => {
|
||||
test('timeoutMs 120000 (exactly at the ceiling) is accepted', () => {
|
||||
const resolver = validResolver();
|
||||
resolver.invoke = { ...resolver.invoke, timeoutMs: 120000 };
|
||||
const cap = featureCap({ taskContentResolver: resolver });
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
assert.ok(
|
||||
!errs.some((e) => e.includes('timeoutMs')),
|
||||
`expected no timeoutMs error at the boundary, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
|
||||
test('timeoutMs 120001 (one past the ceiling) is rejected', () => {
|
||||
const resolver = validResolver();
|
||||
resolver.invoke = { ...resolver.invoke, timeoutMs: 120001 };
|
||||
const cap = featureCap({ taskContentResolver: resolver });
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('timeoutMs') && e.includes('120000')),
|
||||
`expected a ceiling timeoutMs error, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
|
||||
test('an unbounded-in-practice timeoutMs (Number.MAX_SAFE_INTEGER) is rejected', () => {
|
||||
const resolver = validResolver();
|
||||
resolver.invoke = { ...resolver.invoke, timeoutMs: Number.MAX_SAFE_INTEGER };
|
||||
const cap = featureCap({ taskContentResolver: resolver });
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('timeoutMs')),
|
||||
`expected a timeoutMs error, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── invoke.args must carry the {{id}} placeholder ─────────────────────────
|
||||
|
||||
describe('invoke.args without {{id}} placeholder', () => {
|
||||
test('args missing the {{id}} placeholder is rejected', () => {
|
||||
const resolver = validResolver();
|
||||
resolver.invoke = { ...resolver.invoke, args: ['show', 'GSD-42', '--json'] };
|
||||
const cap = featureCap({ taskContentResolver: resolver });
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('{{id}}')),
|
||||
`expected a placeholder error, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Row 24: cross-capability trackerPrefix uniqueness ─────────────────────
|
||||
|
||||
describe('row 24 — duplicate trackerPrefix across capabilities fails cross-capability validation', () => {
|
||||
test('two manifests declaring the same trackerPrefix collide', () => {
|
||||
const capA = featureCap({ id: 'resolver-a', taskContentResolver: validResolver() });
|
||||
const capB = featureCap({ id: 'resolver-b', taskContentResolver: validResolver() });
|
||||
const capMap = new Map([
|
||||
[capA.id, capA],
|
||||
[capB.id, capB],
|
||||
]);
|
||||
const errs = validateCrossCapability(capMap, new Set());
|
||||
assert.ok(
|
||||
errs.some((e) => e.includes('trackerPrefix') && e.includes('resolver-a') && e.includes('resolver-b')),
|
||||
`expected a collision error naming both ids, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
|
||||
test('two manifests declaring different trackerPrefix values do not collide', () => {
|
||||
const capA = featureCap({ id: 'resolver-a', taskContentResolver: validResolver() });
|
||||
const capB = featureCap({
|
||||
id: 'resolver-b',
|
||||
taskContentResolver: { ...validResolver(), trackerPrefix: 'linear' },
|
||||
});
|
||||
const capMap = new Map([
|
||||
[capA.id, capA],
|
||||
[capB.id, capB],
|
||||
]);
|
||||
const errs = validateCrossCapability(capMap, new Set());
|
||||
assert.ok(
|
||||
!errs.some((e) => e.includes('trackerPrefix')),
|
||||
`expected no trackerPrefix collision, got: ${JSON.stringify(errs)}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -107,7 +107,7 @@ const PROSE_ALLOWLIST = [
|
||||
{ file: 'agents/gsd-phase-researcher.md', line: 33, reason: 'package-legitimacy provenance rule names the command as the source of an OK verdict; descriptive' },
|
||||
{ file: 'agents/gsd-roadmapper.md', line: 642, reason: 'parenthetical "e.g." naming SDK queries a user *could* run; not an agent instruction' },
|
||||
{ file: 'agents/gsd-intel-updater.md', line: 40, reason: 'cross-platform note names the `gsd-tools intel <subcommand>` CLI surface descriptively ("CLI invocations go through..."); not an agent instruction' },
|
||||
{ file: 'gsd-core/workflows/execute-plan.md', line: 415, reason: 'describes the downstream SDK validation step (`validated downstream by ...`); names the mechanism, does not instruct the agent to type it' },
|
||||
{ file: 'gsd-core/workflows/execute-plan.md', line: 416, reason: 'describes the downstream SDK validation step (`validated downstream by ...`); names the mechanism, does not instruct the agent to type it' },
|
||||
];
|
||||
|
||||
// Resolver-snippet definition lines / probes that must never be flagged. A line
|
||||
|
||||
108
tests/plan-document.test.cjs
Normal file
108
tests/plan-document.test.cjs
Normal file
@@ -0,0 +1,108 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Unit tests for plan-document.cjs
|
||||
*
|
||||
* Module: gsd-core/bin/lib/plan-document.cjs
|
||||
*
|
||||
* Covers the `tracker-id` attribute (ADR-3646 Phase 1, #3970) added to the
|
||||
* `<task>` element grammar, plus regression coverage proving the addition
|
||||
* does not alter pre-existing task-parsing behaviour.
|
||||
*
|
||||
* Matrix rows referenced below are from
|
||||
* .gsd/phase/feat-3970-task-content-resolution-seam/50-test-matrix.md
|
||||
*/
|
||||
|
||||
const { describe, test } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { parsePlanDocument } = require('../gsd-core/bin/lib/plan-document.cjs');
|
||||
|
||||
describe('plan-document: tracker-id attribute', () => {
|
||||
test('row 1 — no tracker-id attribute yields trackerId: null', () => {
|
||||
const doc = parsePlanDocument(`
|
||||
<task type="auto">
|
||||
<name>Do a thing</name>
|
||||
</task>
|
||||
`);
|
||||
assert.equal(doc.tasks.length, 1);
|
||||
assert.equal(doc.tasks[0].trackerId, null);
|
||||
});
|
||||
|
||||
test('row 2 — tracker-id is read verbatim, never split', () => {
|
||||
const doc = parsePlanDocument(`
|
||||
<task type="auto" tracker-id="beads:GSD-42">
|
||||
<name>Do a thing</name>
|
||||
</task>
|
||||
`);
|
||||
assert.equal(doc.tasks.length, 1);
|
||||
assert.equal(doc.tasks[0].trackerId, 'beads:GSD-42');
|
||||
});
|
||||
|
||||
test('row 3 — tracker-id="" (empty string) normalises to null', () => {
|
||||
const doc = parsePlanDocument(`
|
||||
<task type="auto" tracker-id="">
|
||||
<name>Do a thing</name>
|
||||
</task>
|
||||
`);
|
||||
assert.equal(doc.tasks.length, 1);
|
||||
assert.equal(doc.tasks[0].trackerId, null);
|
||||
});
|
||||
|
||||
test('row 4 — checkpoint tasks never read tracker-id, even when present', () => {
|
||||
const doc = parsePlanDocument(`
|
||||
<task type="checkpoint:decision" tracker-id="beads:GSD-99">
|
||||
<decision>Ship it</decision>
|
||||
</task>
|
||||
`);
|
||||
assert.equal(doc.tasks.length, 1);
|
||||
assert.equal(doc.tasks[0].kind, 'checkpoint');
|
||||
assert.equal(doc.tasks[0].trackerId, null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('plan-document: regression — legacy behaviour unchanged', () => {
|
||||
test('legacy `## Task N` markdown fallback still parses with trackerId: null', () => {
|
||||
const doc = parsePlanDocument(`
|
||||
## Task 1: Do a thing
|
||||
|
||||
Some body text.
|
||||
|
||||
## Task 2: Do another thing
|
||||
`);
|
||||
assert.equal(doc.tasks.length, 2);
|
||||
for (const t of doc.tasks) {
|
||||
assert.equal(t.kind, 'auto');
|
||||
assert.equal(t.type, null);
|
||||
assert.equal(t.trackerId, null);
|
||||
assert.deepEqual(t.plannedFiles, []);
|
||||
assert.deepEqual(t.acceptanceCriteria, []);
|
||||
assert.equal(t.done, null);
|
||||
}
|
||||
assert.equal(doc.tasks[0].name, 'Task 1: Do a thing');
|
||||
assert.equal(doc.tasks[1].name, 'Task 2: Do another thing');
|
||||
});
|
||||
|
||||
test('ordinary task with name/files/acceptance_criteria still parses correctly alongside trackerId', () => {
|
||||
const doc = parsePlanDocument(`
|
||||
<task type="auto" tracker-id="beads:GSD-7">
|
||||
<name>Implement the seam</name>
|
||||
<files>src/a.cts, src/b.cts</files>
|
||||
<acceptance_criteria>
|
||||
- criterion one
|
||||
- criterion two
|
||||
</acceptance_criteria>
|
||||
<done>Merged.</done>
|
||||
</task>
|
||||
`);
|
||||
assert.equal(doc.tasks.length, 1);
|
||||
const t = doc.tasks[0];
|
||||
assert.equal(t.kind, 'auto');
|
||||
assert.equal(t.type, 'auto');
|
||||
assert.equal(t.name, 'Implement the seam');
|
||||
assert.deepEqual(t.plannedFiles, ['src/a.cts', 'src/b.cts']);
|
||||
assert.deepEqual(t.acceptanceCriteria, ['criterion one', 'criterion two']);
|
||||
assert.equal(t.done, 'Merged.');
|
||||
assert.equal(t.trackerId, 'beads:GSD-7');
|
||||
});
|
||||
});
|
||||
279
tests/task-command-router-resolve-content.test.cjs
Normal file
279
tests/task-command-router-resolve-content.test.cjs
Normal file
@@ -0,0 +1,279 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Tests for `task resolve-content` (ADR-3646 Decision 2, issue #3970).
|
||||
* Covers test matrix rows 17-19:
|
||||
* 17 — plan exists, task-id not found in it -> USAGE, non-zero exit.
|
||||
* 18 — end-to-end happy path via injected `resolveTaskContentFn`.
|
||||
* 19 — missing --plan and/or --task-id -> USAGE, before any filesystem access.
|
||||
* Plus the hard-halt path: a thrown resolver-error class must surface as a
|
||||
* non-zero CLI exit, never a swallowed `{resolved:false}` JSON answer.
|
||||
*
|
||||
* In-process style (routeResolveContent's own `_`-prefixed-equivalent
|
||||
* `deps` injection seam), mirroring `refactor-trigger-command-router.cts`'s
|
||||
* injection convention — no subprocess spawn needed for these rows.
|
||||
*/
|
||||
|
||||
const { test, describe, mock } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const { createTempDir, cleanup } = require('./helpers.cjs');
|
||||
|
||||
const { routeResolveContent } = require('../gsd-core/bin/lib/task-command-router.cjs');
|
||||
const { ExitError } = require('../gsd-core/bin/lib/cli-exit.cjs');
|
||||
const {
|
||||
ResolverFailedError,
|
||||
} = require('../gsd-core/bin/lib/task-content-resolution.cjs');
|
||||
|
||||
function writePlan(dir, taskXml) {
|
||||
const planPath = path.join(dir, '01-PLAN.md');
|
||||
fs.writeFileSync(planPath, `# Plan\n\n${taskXml}\n`, 'utf8');
|
||||
return planPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* `io.cjs`'s `output()` writes to fd 1 via `fs.writeSync` directly (never
|
||||
* `process.stdout.write`) — see `writeAllSync` in
|
||||
* `gsd-core/bin/lib/io.cjs`. Capture fd-1 writes by mocking `fs.writeSync`
|
||||
* itself; every other fd passes through to the real implementation
|
||||
* unmocked (so the plan-file reads and any other fs traffic inside a test
|
||||
* body still work).
|
||||
*/
|
||||
function captureStdout(fn) {
|
||||
const chunks = [];
|
||||
const origWriteSync = fs.writeSync.bind(fs);
|
||||
const writeMock = mock.method(fs, 'writeSync', (fd, buffer, ...rest) => {
|
||||
if (fd === 1) {
|
||||
chunks.push(Buffer.isBuffer(buffer) ? buffer.toString('utf8') : String(buffer));
|
||||
return Buffer.isBuffer(buffer) ? buffer.length : Buffer.byteLength(String(buffer));
|
||||
}
|
||||
return origWriteSync(fd, buffer, ...rest);
|
||||
});
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
writeMock.mock.restore();
|
||||
}
|
||||
return chunks.join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* `io.cjs`'s `error()` (ADR-3889) writes its human-readable message to fd 2
|
||||
* via `writeAllSync`, then throws a bare `new ExitError(1)` with NO message
|
||||
* — the stderr write already happened, so the thrown Error's own `.message`
|
||||
* defaults to `"process exit ${code}"` (see `cli-exit.cjs`'s `ExitError`
|
||||
* constructor) and never carries the diagnostic text. Asserting against
|
||||
* `err.message` therefore can never see the "outside project scope" text;
|
||||
* the diagnostic must be read off the captured fd-2 bytes instead. Mirrors
|
||||
* the same fd-mock idiom `captureStdout` above uses for fd 1, and the
|
||||
* established repo pattern in `tests/estimate-calibrate.test.cjs`'s
|
||||
* `runCalibrateExpectError`.
|
||||
*/
|
||||
function captureStderr(fn) {
|
||||
const chunks = [];
|
||||
const origWriteSync = fs.writeSync.bind(fs);
|
||||
const writeMock = mock.method(fs, 'writeSync', (fd, buffer, ...rest) => {
|
||||
if (fd === 2) {
|
||||
chunks.push(Buffer.isBuffer(buffer) ? buffer.toString('utf8') : String(buffer));
|
||||
return Buffer.isBuffer(buffer) ? buffer.length : Buffer.byteLength(String(buffer));
|
||||
}
|
||||
return origWriteSync(fd, buffer, ...rest);
|
||||
});
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
writeMock.mock.restore();
|
||||
}
|
||||
return chunks.join('');
|
||||
}
|
||||
|
||||
const RESOLVABLE_TASK = '<task type="auto" tracker-id="test:1"><name>x</name><action>do the thing</action></task>';
|
||||
|
||||
describe('task resolve-content (rows 17-19)', () => {
|
||||
test('row 19: missing --plan and --task-id -> USAGE, no filesystem access', (t) => {
|
||||
const dir = createTempDir('gsd-resolve-content-19-');
|
||||
t.after(() => cleanup(dir));
|
||||
const readMock = require('node:test').mock.method(fs, 'readFileSync', () => {
|
||||
throw new Error('must not read the filesystem before usage validation');
|
||||
});
|
||||
t.after(() => readMock.mock.restore());
|
||||
|
||||
assert.throws(
|
||||
() => routeResolveContent({ args: ['task', 'resolve-content'], cwd: dir, raw: false }),
|
||||
ExitError,
|
||||
);
|
||||
});
|
||||
|
||||
test('row 19: missing --task-id only -> USAGE', (t) => {
|
||||
const dir = createTempDir('gsd-resolve-content-19b-');
|
||||
t.after(() => cleanup(dir));
|
||||
writePlan(dir, RESOLVABLE_TASK);
|
||||
assert.throws(
|
||||
() =>
|
||||
routeResolveContent({
|
||||
args: ['task', 'resolve-content', '--plan', '01-PLAN.md'],
|
||||
cwd: dir,
|
||||
raw: false,
|
||||
}),
|
||||
ExitError,
|
||||
);
|
||||
});
|
||||
|
||||
test('row 17: plan exists, task-id not found -> USAGE, non-zero exit', (t) => {
|
||||
const dir = createTempDir('gsd-resolve-content-17-');
|
||||
t.after(() => cleanup(dir));
|
||||
writePlan(dir, RESOLVABLE_TASK);
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
routeResolveContent({
|
||||
args: ['task', 'resolve-content', '--plan', '01-PLAN.md', '--task-id', 'nope:999'],
|
||||
cwd: dir,
|
||||
raw: false,
|
||||
}),
|
||||
(err) => {
|
||||
assert.ok(err instanceof ExitError, `expected ExitError, got ${err}`);
|
||||
assert.strictEqual(err.code, 1);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('row 18: end-to-end happy path, resolved:true content shape', (t) => {
|
||||
const dir = createTempDir('gsd-resolve-content-18-');
|
||||
t.after(() => cleanup(dir));
|
||||
writePlan(dir, RESOLVABLE_TASK);
|
||||
|
||||
const fakeCapabilities = [
|
||||
{
|
||||
id: 'fake-tracker',
|
||||
taskContentResolver: {
|
||||
trackerPrefix: 'test',
|
||||
invoke: { binary: 'fake-cli', args: ['show', '{{id}}'], timeoutMs: 5000 },
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
let capturedInvocation = null;
|
||||
const resolveTaskContentFn = (input) => {
|
||||
capturedInvocation = input;
|
||||
return {
|
||||
kind: 'resolved',
|
||||
content: {
|
||||
action: 'do the resolved thing',
|
||||
verify: 'run the resolved verify',
|
||||
acceptanceCriteria: ['criterion a'],
|
||||
readFirst: ['README.md'],
|
||||
done: 'done marker',
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const stdout = captureStdout(() => {
|
||||
routeResolveContent(
|
||||
{
|
||||
args: ['task', 'resolve-content', '--plan', '01-PLAN.md', '--task-id', 'test:1'],
|
||||
cwd: dir,
|
||||
raw: true,
|
||||
},
|
||||
{ loadCapabilities: () => fakeCapabilities, resolveTaskContentFn },
|
||||
);
|
||||
});
|
||||
|
||||
assert.strictEqual(capturedInvocation.trackerId, 'test:1');
|
||||
assert.deepStrictEqual(capturedInvocation.capabilities, fakeCapabilities);
|
||||
|
||||
const printed = JSON.parse(stdout);
|
||||
assert.strictEqual(printed.resolved, true);
|
||||
assert.strictEqual(printed.content.action, 'do the resolved thing');
|
||||
assert.strictEqual(printed.content.verify, 'run the resolved verify');
|
||||
assert.deepStrictEqual(printed.content.acceptanceCriteria, ['criterion a']);
|
||||
assert.deepStrictEqual(printed.content.readFirst, ['README.md']);
|
||||
assert.strictEqual(printed.content.done, 'done marker');
|
||||
});
|
||||
|
||||
test('hard-halt: a thrown ResolverFailedError becomes a non-zero exit, never {resolved:false}', (t) => {
|
||||
const dir = createTempDir('gsd-resolve-content-hardhalt-');
|
||||
t.after(() => cleanup(dir));
|
||||
writePlan(dir, RESOLVABLE_TASK);
|
||||
|
||||
const fakeCapabilities = [
|
||||
{
|
||||
id: 'fake-tracker',
|
||||
taskContentResolver: {
|
||||
trackerPrefix: 'test',
|
||||
invoke: { binary: 'fake-cli', args: ['show', '{{id}}'], timeoutMs: 5000 },
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
let thrown = null;
|
||||
const stdout = captureStdout(() => {
|
||||
try {
|
||||
routeResolveContent(
|
||||
{
|
||||
args: ['task', 'resolve-content', '--plan', '01-PLAN.md', '--task-id', 'test:1'],
|
||||
cwd: dir,
|
||||
raw: true,
|
||||
},
|
||||
{
|
||||
loadCapabilities: () => fakeCapabilities,
|
||||
resolveTaskContentFn: () => {
|
||||
throw new ResolverFailedError('fake-cli', 1, 'boom');
|
||||
},
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
thrown = err;
|
||||
}
|
||||
});
|
||||
assert.ok(thrown instanceof ExitError, `expected ExitError, got ${thrown}`);
|
||||
assert.strictEqual(stdout, '', 'resolver failure must never write a JSON {resolved:false} answer to stdout');
|
||||
});
|
||||
|
||||
test('path traversal: --plan escaping the project root -> USAGE, non-zero exit, no filesystem read', (t) => {
|
||||
const dir = createTempDir('gsd-resolve-content-traversal-');
|
||||
t.after(() => cleanup(dir));
|
||||
|
||||
const escapingPath = '../../../etc/passwit';
|
||||
let thrown = null;
|
||||
const stderr = captureStderr(() => {
|
||||
try {
|
||||
routeResolveContent({
|
||||
args: ['task', 'resolve-content', '--plan', escapingPath, '--task-id', 'test:1'],
|
||||
cwd: dir,
|
||||
raw: false,
|
||||
});
|
||||
} catch (err) {
|
||||
thrown = err;
|
||||
}
|
||||
});
|
||||
assert.ok(thrown instanceof ExitError, `expected ExitError, got ${thrown}`);
|
||||
assert.strictEqual(thrown.code, 1);
|
||||
assert.ok(
|
||||
stderr.includes('outside project scope') && stderr.includes(escapingPath),
|
||||
`expected an outside-project-scope rejection naming the path on stderr, got: ${stderr}`,
|
||||
);
|
||||
});
|
||||
|
||||
test('not-applicable: task-id with no ":" -> {resolved:false}, no reason field', (t) => {
|
||||
const dir = createTempDir('gsd-resolve-content-na-');
|
||||
t.after(() => cleanup(dir));
|
||||
writePlan(dir, '<task type="auto" tracker-id="notrackerprefix"><name>y</name></task>');
|
||||
|
||||
const stdout = captureStdout(() => {
|
||||
routeResolveContent(
|
||||
{
|
||||
args: ['task', 'resolve-content', '--plan', '01-PLAN.md', '--task-id', 'notrackerprefix'],
|
||||
cwd: dir,
|
||||
raw: true,
|
||||
},
|
||||
{ loadCapabilities: () => [] },
|
||||
);
|
||||
});
|
||||
const printed = JSON.parse(stdout);
|
||||
assert.deepStrictEqual(printed, { resolved: false });
|
||||
});
|
||||
});
|
||||
343
tests/task-content-resolution.test.cjs
Normal file
343
tests/task-content-resolution.test.cjs
Normal file
@@ -0,0 +1,343 @@
|
||||
'use strict';
|
||||
|
||||
// Task Content Resolution Module tests (ADR-3646 Phase 1, #3970).
|
||||
// Covers test matrix rows 5-16 (.gsd/phase/feat-3970-task-content-resolution-seam/50-test-matrix.md).
|
||||
// Every subprocess call is a fake execFn — never a real spawn, never a real
|
||||
// timeout wait (CLAUDE.md's clock-seam rule).
|
||||
|
||||
const { test } = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
const fc = require('fast-check');
|
||||
|
||||
const m = require('../gsd-core/bin/lib/task-content-resolution.cjs');
|
||||
const {
|
||||
TASK_CONTENT_RESULT,
|
||||
splitTrackerId,
|
||||
findResolver,
|
||||
buildInvocation,
|
||||
resolveTaskContent,
|
||||
ResolverAmbiguousError,
|
||||
ResolverFailedError,
|
||||
ResolverTimeoutError,
|
||||
ResolverMalformedOutputError,
|
||||
} = m;
|
||||
|
||||
function beadsCapability(overrides = {}) {
|
||||
return {
|
||||
id: 'beads-capability',
|
||||
taskContentResolver: {
|
||||
trackerPrefix: 'beads',
|
||||
invoke: {
|
||||
binary: 'bd',
|
||||
args: ['show', '{{id}}', '--json'],
|
||||
timeoutMs: 10000,
|
||||
},
|
||||
...overrides,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function timeoutError() {
|
||||
const e = new Error('spawnSync bd ETIMEDOUT');
|
||||
e.code = 'ETIMEDOUT';
|
||||
return e;
|
||||
}
|
||||
|
||||
// ─── splitTrackerId — pure ─────────────────────────────────────────────────────
|
||||
|
||||
test('splitTrackerId returns null for null and empty input', () => {
|
||||
assert.strictEqual(splitTrackerId(null), null);
|
||||
assert.strictEqual(splitTrackerId(''), null);
|
||||
});
|
||||
|
||||
test('splitTrackerId splits on the first colon', () => {
|
||||
assert.deepStrictEqual(splitTrackerId('beads:GSD-42'), { prefix: 'beads', id: 'GSD-42' });
|
||||
});
|
||||
|
||||
test('splitTrackerId with no colon returns null (row 16 boundary partner)', () => {
|
||||
assert.strictEqual(splitTrackerId('noprefix'), null);
|
||||
});
|
||||
|
||||
test('id containing colons splits on first colon only (row 16)', () => {
|
||||
assert.deepStrictEqual(
|
||||
splitTrackerId('beads:issue:GSD-1'),
|
||||
{ prefix: 'beads', id: 'issue:GSD-1' },
|
||||
);
|
||||
});
|
||||
|
||||
// ─── buildInvocation — pure ─────────────────────────────────────────────────────
|
||||
|
||||
test('buildInvocation replaces every {{id}} entry with the literal id', () => {
|
||||
const resolver = { invoke: { binary: 'bd', args: ['show', '{{id}}', '--json'], timeoutMs: 10000 } };
|
||||
assert.deepStrictEqual(buildInvocation(resolver, 'GSD-42'), {
|
||||
binary: 'bd',
|
||||
args: ['show', 'GSD-42', '--json'],
|
||||
timeoutMs: 10000,
|
||||
});
|
||||
});
|
||||
|
||||
test('buildInvocation does not touch args that merely contain {{id}} as a substring', () => {
|
||||
const resolver = { invoke: { binary: 'bd', args: ['prefix-{{id}}-suffix'], timeoutMs: 1000 } };
|
||||
assert.deepStrictEqual(buildInvocation(resolver, 'X').args, ['prefix-{{id}}-suffix']);
|
||||
});
|
||||
|
||||
// ─── findResolver — pure ─────────────────────────────────────────────────────
|
||||
|
||||
test('findResolver returns null when zero capabilities match the prefix', () => {
|
||||
assert.strictEqual(findResolver('beads', []), null);
|
||||
assert.strictEqual(findResolver('beads', [{ id: 'other', taskContentResolver: undefined }]), null);
|
||||
});
|
||||
|
||||
test('findResolver returns the single well-formed match', () => {
|
||||
const cap = beadsCapability();
|
||||
const result = findResolver('beads', [cap]);
|
||||
assert.strictEqual(result.capabilityId, 'beads-capability');
|
||||
assert.strictEqual(result.trackerPrefix, 'beads');
|
||||
});
|
||||
|
||||
test('findResolver returns "ambiguous" for two matching capabilities (row 7)', () => {
|
||||
const capA = beadsCapability();
|
||||
const capB = { ...beadsCapability(), id: 'other-capability' };
|
||||
assert.strictEqual(findResolver('beads', [capA, capB]), 'ambiguous');
|
||||
});
|
||||
|
||||
test('findResolver ignores a declaration missing the {{id}} placeholder (row 15)', () => {
|
||||
const bad = {
|
||||
id: 'bad-capability',
|
||||
taskContentResolver: { trackerPrefix: 'beads', invoke: { binary: 'bd', args: ['show'], timeoutMs: 1000 } },
|
||||
};
|
||||
assert.strictEqual(findResolver('beads', [bad]), null);
|
||||
});
|
||||
|
||||
test('findResolver ignores garbage taskContentResolver shapes without throwing', () => {
|
||||
const garbage = [
|
||||
{ id: 'a', taskContentResolver: null },
|
||||
{ id: 'b', taskContentResolver: 'not-an-object' },
|
||||
{ id: 'c', taskContentResolver: [] },
|
||||
{ id: 'd', taskContentResolver: { trackerPrefix: 'beads' } }, // no invoke
|
||||
{ id: 'e', taskContentResolver: { trackerPrefix: 'beads', invoke: { binary: '', args: ['{{id}}'], timeoutMs: 1000 } } },
|
||||
{ id: 'f', taskContentResolver: { trackerPrefix: 'beads', invoke: { binary: 'bd', args: ['{{id}}'], timeoutMs: 0 } } },
|
||||
{ id: 'g', taskContentResolver: { trackerPrefix: 'beads', invoke: { binary: 'bd', args: ['{{id}}'], timeoutMs: -5 } } },
|
||||
{ id: 'h', taskContentResolver: { trackerPrefix: 'beads', invoke: { binary: 'bd', args: ['{{id}}'], timeoutMs: 1.5 } } },
|
||||
];
|
||||
assert.strictEqual(findResolver('beads', garbage), null);
|
||||
});
|
||||
|
||||
// ─── resolveTaskContent — orchestration ─────────────────────────────────────────
|
||||
|
||||
test('row 5: no tracker-id resolves not-applicable', () => {
|
||||
const result = resolveTaskContent({ trackerId: null, capabilities: [beadsCapability()] });
|
||||
assert.deepStrictEqual(result, { kind: TASK_CONTENT_RESULT.NOT_APPLICABLE });
|
||||
});
|
||||
|
||||
test('row 6: unmatched prefix resolves no-resolver', () => {
|
||||
const result = resolveTaskContent({ trackerId: 'unknownprefix:1', capabilities: [] });
|
||||
assert.deepStrictEqual(result, { kind: TASK_CONTENT_RESULT.NO_RESOLVER });
|
||||
});
|
||||
|
||||
test('row 7: ambiguous prefix registration throws, never silently picks one', () => {
|
||||
const capA = beadsCapability();
|
||||
const capB = { ...beadsCapability(), id: 'other-capability' };
|
||||
assert.throws(
|
||||
() => resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [capA, capB], execFn: () => { throw new Error('must not be called'); } }),
|
||||
ResolverAmbiguousError,
|
||||
);
|
||||
});
|
||||
|
||||
test('row 8: non-empty description resolves true with mapped content', () => {
|
||||
const execFn = () => ({
|
||||
status: 0,
|
||||
stdout: JSON.stringify({
|
||||
description: 'do X',
|
||||
verify: 'run tests',
|
||||
acceptance_criteria: ['a', 'b'],
|
||||
read_first: ['docs/x.md'],
|
||||
done: 'X is done',
|
||||
}),
|
||||
stderr: '',
|
||||
});
|
||||
const result = resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn });
|
||||
assert.deepStrictEqual(result, {
|
||||
kind: TASK_CONTENT_RESULT.RESOLVED,
|
||||
content: {
|
||||
action: 'do X',
|
||||
verify: 'run tests',
|
||||
acceptanceCriteria: ['a', 'b'],
|
||||
readFirst: ['docs/x.md'],
|
||||
done: 'X is done',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('row 9: empty description string resolves empty', () => {
|
||||
const execFn = () => ({ status: 0, stdout: JSON.stringify({ description: '' }), stderr: '' });
|
||||
const result = resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn });
|
||||
assert.deepStrictEqual(result, { kind: TASK_CONTENT_RESULT.EMPTY });
|
||||
});
|
||||
|
||||
test('row 9b: whitespace-only description resolves empty', () => {
|
||||
const execFn = () => ({ status: 0, stdout: JSON.stringify({ description: ' ' }), stderr: '' });
|
||||
const result = resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn });
|
||||
assert.deepStrictEqual(result, { kind: TASK_CONTENT_RESULT.EMPTY });
|
||||
});
|
||||
|
||||
test('row 10: absent description resolves empty, same as empty string', () => {
|
||||
const execFn = () => ({ status: 0, stdout: JSON.stringify({}), stderr: '' });
|
||||
const result = resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn });
|
||||
assert.deepStrictEqual(result, { kind: TASK_CONTENT_RESULT.EMPTY });
|
||||
});
|
||||
|
||||
test('row 11: non-zero resolver exit throws ResolverFailedError, never falls back silently', () => {
|
||||
const execFn = () => ({ status: 1, stdout: '', stderr: 'no such issue GSD-42' });
|
||||
assert.throws(
|
||||
() => resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn }),
|
||||
(err) => {
|
||||
assert.ok(err instanceof ResolverFailedError);
|
||||
assert.strictEqual(err.exitCode, 1);
|
||||
assert.match(err.stderrTail, /no such issue/);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('row 11b: a spawn error (e.g. ENOENT) also throws ResolverFailedError', () => {
|
||||
const enoent = new Error('spawnSync bd ENOENT');
|
||||
enoent.code = 'ENOENT';
|
||||
const execFn = () => ({ status: null, stdout: '', stderr: '', error: enoent });
|
||||
assert.throws(
|
||||
() => resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn }),
|
||||
ResolverFailedError,
|
||||
);
|
||||
});
|
||||
|
||||
test('row 12: resolver exceeding timeoutMs throws ResolverTimeoutError (simulated, no real wait)', () => {
|
||||
const execFn = () => ({ status: null, stdout: '', stderr: '', error: timeoutError() });
|
||||
assert.throws(
|
||||
() => resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn }),
|
||||
(err) => {
|
||||
assert.ok(err instanceof ResolverTimeoutError);
|
||||
assert.strictEqual(err.timeoutMs, 10000);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('row 13: malformed JSON stdout throws, is not conflated with empty content', () => {
|
||||
const execFn = () => ({ status: 0, stdout: 'not json', stderr: '' });
|
||||
assert.throws(
|
||||
() => resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn }),
|
||||
ResolverMalformedOutputError,
|
||||
);
|
||||
});
|
||||
|
||||
test('row 14: valid JSON non-object stdout (null/array/string/number/bool) throws', () => {
|
||||
const cases = [null, [], 'x', 0, true];
|
||||
for (const value of cases) {
|
||||
const execFn = () => ({ status: 0, stdout: JSON.stringify(value), stderr: '' });
|
||||
assert.throws(
|
||||
() => resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn }),
|
||||
ResolverMalformedOutputError,
|
||||
`expected throw for JSON value ${JSON.stringify(value)}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('row 15: invoke.args without {{id}} placeholder is rejected — resolves no-resolver, never spawns', () => {
|
||||
const badCapability = {
|
||||
id: 'bad-capability',
|
||||
taskContentResolver: { trackerPrefix: 'beads', invoke: { binary: 'bd', args: ['show'], timeoutMs: 1000 } },
|
||||
};
|
||||
const execFn = () => { throw new Error('must not spawn a rejected declaration'); };
|
||||
const result = resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [badCapability], execFn });
|
||||
assert.deepStrictEqual(result, { kind: TASK_CONTENT_RESULT.NO_RESOLVER });
|
||||
});
|
||||
|
||||
test('row 16: id containing colons splits on first colon only, end to end', () => {
|
||||
let capturedArgs = null;
|
||||
const execFn = (binary, args) => {
|
||||
capturedArgs = args;
|
||||
return { status: 0, stdout: JSON.stringify({ description: 'do X' }), stderr: '' };
|
||||
};
|
||||
const result = resolveTaskContent({ trackerId: 'beads:issue:GSD-1', capabilities: [beadsCapability()], execFn });
|
||||
assert.deepStrictEqual(capturedArgs, ['show', 'issue:GSD-1', '--json']);
|
||||
assert.strictEqual(result.kind, TASK_CONTENT_RESULT.RESOLVED);
|
||||
});
|
||||
|
||||
// ─── stderr-on-success is not an error (design.md negative space) ──────────────
|
||||
|
||||
test('stderr output on a successful (exit 0) run is not treated as a failure', () => {
|
||||
const execFn = () => ({ status: 0, stdout: JSON.stringify({ description: 'do X' }), stderr: 'informational log line' });
|
||||
const result = resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn });
|
||||
assert.strictEqual(result.kind, TASK_CONTENT_RESULT.RESOLVED);
|
||||
});
|
||||
|
||||
// ─── fast-check property test (row 14, gauntlet enumeration) ───────────────────
|
||||
|
||||
test('property: any valid-JSON-but-not-an-object stdout always throws ResolverMalformedOutputError, never resolves silently', () => {
|
||||
fc.assert(
|
||||
fc.property(
|
||||
fc.oneof(
|
||||
fc.constant(null),
|
||||
fc.array(fc.anything()),
|
||||
fc.string(),
|
||||
fc.double(),
|
||||
fc.boolean(),
|
||||
),
|
||||
(value) => {
|
||||
const execFn = () => ({ status: 0, stdout: JSON.stringify(value), stderr: '' });
|
||||
try {
|
||||
resolveTaskContent({ trackerId: 'beads:GSD-42', capabilities: [beadsCapability()], execFn });
|
||||
return false;
|
||||
} catch (err) {
|
||||
return err instanceof ResolverMalformedOutputError;
|
||||
}
|
||||
},
|
||||
),
|
||||
{ numRuns: 100 },
|
||||
);
|
||||
});
|
||||
|
||||
// ─── stderr/stdout sanitization in .message (security review, #3970) ───────
|
||||
// `stderrTail`/`stdoutSample` are UNTRUSTED subprocess-sourced text (the
|
||||
// resolver binary and its argv `{{id}}` token come from a PLAN.md
|
||||
// `tracker-id` attribute, which is often LLM/agent-authored). Embedding them
|
||||
// raw into `.message` would let a hostile or buggy resolver smuggle its own
|
||||
// `\n` and forge a second `Error: ` line when a caller (task-command-
|
||||
// router.cts's routeResolveContent) writes `.message` to stderr via
|
||||
// io.cjs's error(). `formatDiagnosticToken()` (io.cts) is `JSON.stringify`
|
||||
// under the hood: it wraps the value in quotes and escapes control
|
||||
// characters, including `\n` -> the two-character sequence `\n`, so the
|
||||
// result can never span more than one line.
|
||||
|
||||
test('ResolverFailedError.message has no raw literal newline even when stderrTail smuggles one', () => {
|
||||
const hostileStderr = 'real failure text\nError: fake message forged by a hostile resolver';
|
||||
const err = new ResolverFailedError('bd', 1, hostileStderr);
|
||||
assert.ok(
|
||||
!err.message.includes('\n'),
|
||||
`expected no raw newline in .message, got: ${JSON.stringify(err.message)}`,
|
||||
);
|
||||
// The raw field is left untouched for programmatic callers — only the
|
||||
// rendered .message is sanitized.
|
||||
assert.strictEqual(err.stderrTail, hostileStderr);
|
||||
// formatDiagnosticToken === JSON.stringify: the escaped '\n' (backslash-n,
|
||||
// two characters) survives inside the JSON-quoted substring.
|
||||
assert.ok(err.message.includes('\\n'));
|
||||
assert.ok(err.message.includes(JSON.stringify(hostileStderr)));
|
||||
});
|
||||
|
||||
test('ResolverMalformedOutputError.message has no raw literal newline even when stdoutSample smuggles one', () => {
|
||||
const hostileStdout = '{"description":"x"\nError: fake message forged by a hostile resolver';
|
||||
const err = new ResolverMalformedOutputError('bd', 'stdout is not valid JSON', hostileStdout);
|
||||
assert.ok(
|
||||
!err.message.includes('\n'),
|
||||
`expected no raw newline in .message, got: ${JSON.stringify(err.message)}`,
|
||||
);
|
||||
assert.strictEqual(err.stdoutSample, hostileStdout);
|
||||
assert.ok(err.message.includes('\\n'));
|
||||
assert.ok(err.message.includes(JSON.stringify(hostileStdout)));
|
||||
});
|
||||
|
||||
test('ResolverFailedError.message with an empty stderrTail omits the trailing colon segment', () => {
|
||||
const err = new ResolverFailedError('bd', 1, '');
|
||||
assert.strictEqual(err.message, "resolver command 'bd' exited 1");
|
||||
});
|
||||
84
tests/task-content-resolver-grammar-parity.test.cjs
Normal file
84
tests/task-content-resolver-grammar-parity.test.cjs
Normal file
@@ -0,0 +1,84 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Parity test: `capability-validator.cjs`'s install-time `KEBAB_RE` grammar
|
||||
* check on `taskContentResolver.trackerPrefix` (`validateTaskContentResolver`)
|
||||
* MUST agree with `task-content-resolution.cts`'s resolve-time re-validation
|
||||
* inside `parseResolverDeclaration` (exercised here via `findResolver`) on
|
||||
* every `trackerPrefix` value.
|
||||
*
|
||||
* This is the "Generative Fix Divergence" guard CLAUDE.md requires whenever
|
||||
* two surfaces share a rule with no single source of truth: the grammar is
|
||||
* duplicated as a literal regex in both files (see `task-content-
|
||||
* resolution.cts`'s `TRACKER_PREFIX_RE` docstring for why it is not a shared
|
||||
* import), so this test is what actually keeps them from drifting apart. If a
|
||||
* future change to either regex loosens or tightens it without mirroring the
|
||||
* change in the other file, this test fails.
|
||||
*/
|
||||
|
||||
const { test, describe } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { validateTaskContentResolver } = require('../gsd-core/bin/lib/capability-validator.cjs');
|
||||
const { findResolver } = require('../gsd-core/bin/lib/task-content-resolution.cjs');
|
||||
|
||||
function validInvoke() {
|
||||
return { binary: 'bd', args: ['show', '{{id}}', '--json'], timeoutMs: 10000 };
|
||||
}
|
||||
|
||||
function featureCapWithResolver(trackerPrefix) {
|
||||
return {
|
||||
id: 'demo',
|
||||
role: 'feature',
|
||||
taskContentResolver: { trackerPrefix, invoke: validInvoke() },
|
||||
};
|
||||
}
|
||||
|
||||
/** True when the validator accepts this trackerPrefix (no trackerPrefix-naming error). */
|
||||
function validatorAccepts(trackerPrefix) {
|
||||
const cap = featureCapWithResolver(trackerPrefix);
|
||||
const errs = validateTaskContentResolver(cap);
|
||||
return !errs.some((e) => e.includes('trackerPrefix'));
|
||||
}
|
||||
|
||||
/** True when the resolve-time seam accepts this trackerPrefix (finds a match, not `null`). */
|
||||
function resolverAccepts(trackerPrefix) {
|
||||
const capabilities = [featureCapWithResolver(trackerPrefix)];
|
||||
const result = findResolver(trackerPrefix, capabilities);
|
||||
return result !== null && result !== 'ambiguous';
|
||||
}
|
||||
|
||||
const TABLE = [
|
||||
{ trackerPrefix: 'beads', valid: true },
|
||||
{ trackerPrefix: 'my-tracker', valid: true },
|
||||
{ trackerPrefix: 'Beads', valid: false },
|
||||
{ trackerPrefix: 'has_underscore', valid: false },
|
||||
{ trackerPrefix: 'UPPER', valid: false },
|
||||
{ trackerPrefix: '', valid: false },
|
||||
{ trackerPrefix: '1leading-digit', valid: false },
|
||||
];
|
||||
|
||||
describe('trackerPrefix grammar parity — capability-validator.cjs vs task-content-resolution.cts', () => {
|
||||
for (const { trackerPrefix, valid } of TABLE) {
|
||||
test(`'${trackerPrefix}' — validator and resolver agree (expected valid: ${valid})`, () => {
|
||||
const validatorResult = validatorAccepts(trackerPrefix);
|
||||
const resolverResult = resolverAccepts(trackerPrefix);
|
||||
assert.strictEqual(
|
||||
validatorResult,
|
||||
valid,
|
||||
`validator disagreed with expected table value for '${trackerPrefix}'`,
|
||||
);
|
||||
assert.strictEqual(
|
||||
resolverResult,
|
||||
valid,
|
||||
`resolver disagreed with expected table value for '${trackerPrefix}'`,
|
||||
);
|
||||
assert.strictEqual(
|
||||
validatorResult,
|
||||
resolverResult,
|
||||
`PARITY BREAK: validator and resolver disagree for trackerPrefix '${trackerPrefix}' ` +
|
||||
`(validator: ${validatorResult}, resolver: ${resolverResult})`,
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user