Files
msd-core/tests/fix-2658-trae-runtime-detection-and-instruction-path.test.cjs
Tom Boucher 342590c70e refactor(#3184): milestone windowing has one owner and a decidable failure signal (#3209)
* test(#3184): failing-first milestone-window single-owner suite

Covers the 50 input classes in the phase test matrix: scope classification
(genuinely-empty vs truncated vs unscoped vs unreadable), the section-end
owner's level boundaries, consumer-output identity per ADR-3180 Decision 4(c),
the milestone.complete refusal with negative proof that no directory moved,
the version-token boundary defect, drift-guard behavior, and three fast-check
properties over document-shaped generators.

Committed alone so the remote runner records the failure before the fix lands.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

* refactor(#3184): milestone windowing routes through one owner

Three copies of the milestone section-end walk lived in roadmap-parser.cts —
two distinct computeSectionEnd function nodes plus an inline third in
getMilestonePhaseFilter's versionOverride branch. computeMilestoneSectionEnd is
now the sole owner and the other two are deleted, not kept in sync by comment.

The whole-repo drift guard found what the epic did not: state.cts held three
more re-derivations of the same vocabulary — two byte-identical milestone
bounding checks carrying a defect neither reported copy has (no boundary after
the version token, so v2.0 matched inside v2.0.1), and a milestone-sectioning
predicate. All three route through the owner now.

A composition-level duplicate appeared inside this change's own first pass:
getMilestonePhaseFilter and cmdMilestoneComplete each re-assembled a window out
of the owner's primitives, and had already diverged on whether to skip a closed
milestone heading. sliceMilestoneWindow is the one composition.

Windows now carry the ADR-3180 SCOPE discriminator, so a truncated window is
distinguishable from a genuinely empty milestone — those were output-identical,
which is the whole failure class. roadmap analyze emits it (#3165), and
milestone complete refuses to archive on anything but COMPLETE rather than
pass-all moving every phase directory on disk (#3166). The pass-all degrade is
preserved where its premise holds: making the filter deny-all would trade a
silent over-inclusive answer for a silent under-inclusive one on the read paths
that count with it.

extractCurrentMilestone keeps its signature — 200+ affected symbols across 41
files and 25 process flows — and is a one-line wrapper over the scoped owner.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

* fix(#3184): fence-aware phase detection and one heading-selection owner

Review fixes from the two orthogonal passes.

The blocker: hasPhaseEntries matched ATX phase headings fence-aware via
tokenizeHeadings but tested the #2199 bullet form against un-stripped markdown,
so a fenced EXAMPLE of the bullet syntax counted as a real phase. A genuinely
empty milestone then classified TRUNCATED and milestone complete refused a
legitimate archive — a false positive in the destructive direction, worse than
the defect this phase set out to fix. Both that path and getMilestonePhaseFilter
own pre-existing bullet scan now run on stripFencedCode, since leaving one meant
the owner file gave two different answers to the same question.

The selection rule — locate, prefer the non-closed heading, else the first — had
been written three more times inside the file whose thesis is single ownership.
selectMilestoneHeading owns it; all three sites route through it. The copies were
behaviorally identical, so this is de-duplication with no observable change,
verified by probing that all three paths select the same heading.

roadmap analyze emitting a scope no consumer read left #3165's actual symptom
alive, so Route 0 in next.md now treats a non-complete scope as scan-failed
rather than as a clean empty scan, and the ADR amendment no longer overstates
what shipped.

Also: the scope refusal moved above the archive-directory create, so a refusal
leaves nothing on disk; the versionOverride comment names all four consumers;
COMMANDS.md documents the new guard beside its sibling.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

* test(#2658): exclude the changelog from the malformed-path scan

The gate walks every emitted .md/.js/.cjs file in an installed tree and asserts
none contains `.claude/.trae/rules` or `.trae/.trae/rules`. CHANGELOG.md ships
into that tree, and its #2658 entry quotes both malformed paths while describing
the fix that removed them — so the release note documenting the fix trips the
fix's own regression test. Red on next before this branch.

The installer is correct: a probe over a real --trae --local install found 621
emitted files, exactly one hit, and it was gsd-core/CHANGELOG.md. The scan scope
was the defect, not the product.

Excluded by exact relative path rather than by loosening the patterns or skipping
all markdown — the emitted agent and command markdown is precisely what #2658 was
about, so the gate stays strong everywhere it matters.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

* test(#3184): regenerate install-tree fixtures for the shared drift scanner

scripts/lib/ ships in the npm package and installer, so extracting the shared
tree-walk into scripts/lib/drift-scan.cjs adds one path to every runtime's
install tree. Regenerated via npm run gen:install-tree; the delta is exactly
that one path per fixture.

The two drift guards themselves do not ship (scripts/lint-*.cjs is excluded),
so only the extracted library moves. This matches the existing
scripts/lib/allowlist-ratchet.cjs precedent, which is likewise a lint-only
helper carried in the shipped tree.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

* fix(#3184): restore the #730 sub-milestone boundary and narrow the refusal

The remote runner caught two regressions this branch introduced. Both were mine,
and neither review pass found them — only running the existing suite did.

The version-token boundary. I replaced locateMilestoneHeadings' \b with
(?![\w.-]), reasoning that v2.0 matching inside v2.0.1 was the same defect #2562
fixed in isMilestoneShippedInRoadmap. It is not the same question. A milestone
state of v8.0 legitimately selects the '## v8.0-B' sub-milestone section over a
closed v8.0-A sibling (#730), and \b is what allows it while the stricter
boundary forbids it — nine tests in roadmap-phase-fallback said so. Reverted to
\b; the state.cts consolidation is now a straight merge with no behavior change,
and the v2.0/v2.0.1 ambiguity is left exactly as it was. The ADR amendment and
the design doc no longer claim otherwise.

The refusal scope. I refused whenever the window was not COMPLETE, but #3166 is
about the TRUNCATED window specifically — the heading is found and the section
closes before the phase region, so pass-all archives everything. UNREADABLE and
UNSCOPED are pre-existing, legitimately handled states, and refusing on them
broke 'handles missing ROADMAP.md gracefully' and three archive tests. Narrowed
to TRUNCATED; docs corrected to match.

One of the new tests was also wrong: its fixture gave the shipped and current
milestones' phases the same numeric id, and the filter matches on that id, so it
could not have distinguished the two windows. Fixture corrected to exercise what
it claims to.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

* fix(#3184): enumerate drift-scan.cjs for uninstall

The installer copies scripts/lib/ wholesale, but uninstall removes an explicit
set — deliberately, so a user's own helpers in that directory survive. The
extracted drift-scan.cjs was copied in and never enumerated, so it outlived
uninstall, left the directory non-empty, and the rmdir that follows failed.

Added to GSD_SCRIPTS_LIB_FILES, following allowlist-ratchet.cjs, which is
likewise a lint-only helper that ships there and is enumerated. Verified with a
real install-then-uninstall into a temp target: scripts/lib/ held exactly the
three GSD files and was gone afterwards.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

* test(#3184): assert install and uninstall agree on scripts/lib and scripts/changeset

Found while shipping this phase, and fixed here rather than noted.

install() copies scripts/lib/ and scripts/changeset/ into the target WHOLESALE —
the comment at the copy site literally says "and any future lib helpers".
uninstall() removes them by hardcoded enumeration, deliberately, so a user's own
helpers in those directories survive. A wholesale writer paired with an
enumerated remover cannot stay in sync by construction: any file added to either
directory ships to every user and is then orphaned in their repo forever, since
it survives uninstall, leaves the directory non-empty, and the rmdir that follows
fails. Nothing reported this. 31,225 tests were green over it.

That is the same divergence class this epic exists to delete, sitting in the
installer, so it gets the same remedy CLAUDE.md prescribes for it: a parity
assertion that fails the moment the two surfaces disagree. The test compares each
directory's real contents against its enumeration and names the offending file
plus the constant to add it to.

Both enumerations are hoisted to module scope and exported, so the test asserts
on the actual arrays rather than pattern-matching the installer's source — no
allow-test-rule annotation needed. Proven non-vacuous both ways: empty diff on
the current tree, correct report when an unenumerated file is injected.

scripts/changeset/ turned out to carry the identical defect and is covered too.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

* chore(#3184): backfill changeset PR number

Also narrows the wording to match the shipped behavior: the refusal fires on a
truncated window specifically, not on any non-complete scope.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015kfkRFNUESoBspUYcAQaT3

---------

Co-authored-by: sim <sim@local>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-08 09:50:35 -04:00

266 lines
13 KiB
JavaScript

'use strict';
/**
* Regression tests for #2658 — Trae runtime not detected in workflow
* runtime-detection blocks (falls back to claude), and the install-time
* `CLAUDE.md` path rewrite mutilates the claude fallback into a malformed
* path instead of resolving to the Trae rules file.
*
* Defects collided (see
* .gsd/bug/fix-2658-trae-runtime-not-detected-falls-back-to-/10-diagnosis.md):
*
* 1. `gsd-core/workflows/new-project.md` AND `gsd-core/workflows/ingest-docs.md`
* (found during this remediation — same pattern, same gap, not just
* new-project.md as originally reported) never recognized trae (path
* `/.trae/` or env `TRAE_CONFIG_DIR`) in their runtime-detection blocks —
* fell through to `RUNTIME=claude`.
* 2. The `trae.js` entry in `bin/install.js`'s `RUNTIME_CONTENT_DISPATCH`
* replaced bare `CLAUDE.md` first, leaving a stale `.claude/` prefix:
* `.claude/CLAUDE.md` -> `.claude/.trae/rules/`.
* 3. `convertClaudeToTraeMarkdown` (mirrored in `bin/install.js` and
* `src/runtime-artifact-conversion.cts`) had the same class of bug but a
* DIFFERENT wrong output (`.trae/.trae/rules/`), because its generic
* `.claude/` -> `.trae/` rewrite ran after the bare `CLAUDE.md` rewrite
* and re-mutated the leftover prefix.
* 4. `capabilities/trae/capability.json` didn't declare
* `hostBehaviors.projectInstructionFile`, so even a correctly-detected
* trae runtime resolved to the generic `AGENTS.md` default via
* `getProjectInstructionFile`.
* 5. Found by the end-to-end install test below, one level deeper than the
* static trace: `copyWithPathReplacement` (bin/install.js) runs a
* GENERIC `~/.claude/` / `$HOME/.claude/` / `./.claude/` -> runtime-dir
* rewrite on every .md file BEFORE calling `convertClaudeToTraeMarkdown`,
* substituting a `pathPrefix` the converter is never given (it differs
* per install: relative for a project-local install, an arbitrary
* absolute path for a local install rooted elsewhere, `~/.trae/` for a
* global one). The converter's `.claude/CLAUDE.md`-specific patterns
* (defect 3's fix) never fire on that already-rewritten text, and the
* bare fallback still doubles the prefix — a first attempt at fixing
* this handled only the `./.trae/CLAUDE.md` shape and missed the
* `~/.claude/` / `$HOME/.claude/` forms `gsd-core/workflows/profile-user.md`
* actually uses, caught by row 12 (the real spawned install) below on a
* second run. Fixed with a prefix-preserving pattern (capture whatever
* precedes a `.trae/` tail, keep it, fix only the filename suffix)
* instead of assuming one fixed shape.
*/
const { test, describe } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const fc = require('fast-check');
process.env['GSD_TEST_MODE'] = '1';
const { getProjectInstructionFile } = require('../gsd-core/bin/lib/runtime-name-policy.cjs');
const { convertClaudeToTraeMarkdown } = require('../bin/install.js');
const runtimeArtifactConversion = require('../gsd-core/bin/lib/runtime-artifact-conversion.cjs');
const { runMinimalInstall, walk } = require('./helpers/install-shared.cjs');
const { cleanup } = require('./helpers.cjs');
const REPO_ROOT = path.join(__dirname, '..');
const MALFORMED_SINGLE = '.claude/.trae/rules';
const MALFORMED_DOUBLE = '.trae/.trae/rules';
const EXPECTED_PATH = '.trae/rules/rules.md';
describe('#2658 acceptance criterion 2: getProjectInstructionFile resolves trae to a concrete file', () => {
test('trae maps to .trae/rules/rules.md (not the generic AGENTS.md default)', () => {
assert.strictEqual(getProjectInstructionFile('trae'), EXPECTED_PATH);
});
test('capability descriptor declares the same path getProjectInstructionFile returns', () => {
const cap = JSON.parse(
fs.readFileSync(path.join(REPO_ROOT, 'capabilities', 'trae', 'capability.json'), 'utf8'),
);
assert.strictEqual(cap.runtime.hostBehaviors.projectInstructionFile, EXPECTED_PATH);
assert.strictEqual(getProjectInstructionFile('trae'), cap.runtime.hostBehaviors.projectInstructionFile);
});
test('the declared path is a concrete file, not a bare directory (acceptance criterion 2)', () => {
assert.ok(!EXPECTED_PATH.endsWith('/'), 'must not be directory-terminated');
assert.ok(/\.md$/.test(EXPECTED_PATH), 'must name a concrete markdown file');
});
});
describe('#2658: convertClaudeToTraeMarkdown never mutilates the CLAUDE.md path (bin/install.js)', () => {
const cases = [
['bare CLAUDE.md', 'See CLAUDE.md for details.'],
['./CLAUDE.md', 'Read ./CLAUDE.md before starting.'],
['backtick-wrapped `CLAUDE.md`', 'The file `CLAUDE.md` is authoritative.'],
['the exact reported-bug input: .claude/CLAUDE.md', 'Fallback path is .claude/CLAUDE.md by default.'],
['backtick-wrapped .claude/CLAUDE.md', 'Fallback: `.claude/CLAUDE.md`.'],
['./.claude/CLAUDE.md', 'From root: ./.claude/CLAUDE.md'],
];
for (const [label, input] of cases) {
test(`${label} -> ${EXPECTED_PATH}, no malformed output`, () => {
const out = convertClaudeToTraeMarkdown(input);
assert.ok(!out.includes(MALFORMED_SINGLE), `output must not contain "${MALFORMED_SINGLE}": ${out}`);
assert.ok(!out.includes(MALFORMED_DOUBLE), `output must not contain "${MALFORMED_DOUBLE}": ${out}`);
assert.ok(out.includes(EXPECTED_PATH), `output must contain "${EXPECTED_PATH}": ${out}`);
});
}
test('fast-check property: any surrounding text around .claude/CLAUDE.md never yields a malformed path', () => {
fc.assert(
fc.property(
fc.string({ maxLength: 40 }),
fc.string({ maxLength: 40 }),
(prefix, suffix) => {
const content = `${prefix}.claude/CLAUDE.md${suffix}`;
const out = convertClaudeToTraeMarkdown(content);
assert.ok(!out.includes(MALFORMED_SINGLE));
assert.ok(!out.includes(MALFORMED_DOUBLE));
},
),
{ numRuns: 200 },
);
});
});
describe('#2658 defect 5: post-generic-rewrite ".trae/"-prefixed forms preserve their prefix instead of doubling it', () => {
// These simulate the text `copyWithPathReplacement`'s generic `~/.claude/` /
// `$HOME/.claude/` / `./.claude/` -> runtime-dir pass hands to
// convertClaudeToTraeMarkdown — the converter never sees the original
// `.claude/`-prefixed source in this pipeline, only these already-rewritten
// shapes. A fixed-shape patch that only handled the local relative form
// left the local-install-absolute-path and global tilde forms broken.
const cases = [
['local relative (post "./.claude/" -> "./.trae/" rewrite)', './.trae/CLAUDE.md', './.trae/rules/rules.md'],
[
'local install absolute path (post "./.claude/" -> "<tmp-root>/.trae/" rewrite)',
'/private/var/folders/xx/gsd-trae-local-abc123/.trae/CLAUDE.md',
'/private/var/folders/xx/gsd-trae-local-abc123/.trae/rules/rules.md',
],
['global tilde (post "~/.claude/" -> "~/.trae/" rewrite)', '~/.trae/CLAUDE.md', '~/.trae/rules/rules.md'],
['backtick-wrapped local relative', '`./.trae/CLAUDE.md`', '`./.trae/rules/rules.md`'],
];
for (const [label, input, expected] of cases) {
test(`${label} -> prefix preserved, no malformed path`, () => {
const out = convertClaudeToTraeMarkdown(input);
assert.ok(!out.includes(MALFORMED_SINGLE), `output must not contain "${MALFORMED_SINGLE}": ${out}`);
assert.ok(!out.includes(MALFORMED_DOUBLE), `output must not contain "${MALFORMED_DOUBLE}": ${out}`);
assert.strictEqual(out, expected);
});
}
test('fast-check property: any arbitrary path ending in .trae/ never yields a doubled prefix', () => {
fc.assert(
fc.property(
fc.string({ maxLength: 30 }).filter((s) => !s.includes('`') && !/\s/.test(s)),
(prefix) => {
const content = `${prefix}.trae/CLAUDE.md`;
const out = convertClaudeToTraeMarkdown(content);
assert.ok(!out.includes(MALFORMED_SINGLE));
assert.ok(!out.includes(MALFORMED_DOUBLE));
},
),
{ numRuns: 200 },
);
});
});
describe('#2658 output parity: bin/install.js vs runtime-artifact-conversion.cjs convertClaudeToTraeMarkdown (#2094 mirror)', () => {
// Parity must hold for the pre-existing reported-bug input AND for every
// arbitrary-prefix ".trae/"-tail shape the prefix-preserving regex
// (bin/install.js:2747-2748, mirrored byte-for-byte at
// src/runtime-artifact-conversion.cts:1357-1358) was added to handle. A
// change to only one copy of that regex would otherwise pass every other
// test in this file — none of the defect-5 cases above call the mirror —
// while silently diverging from the other copy.
const parityCases = [
['the reported-bug input (bare .claude/ prefix)', 'Fallback path is .claude/CLAUDE.md by default.'],
['local relative prefix (post "./.claude/" -> "./.trae/" rewrite)', './.trae/CLAUDE.md'],
[
'nested project-path absolute prefix (post "./.claude/" -> "<tmp-root>/.trae/" rewrite)',
'/private/var/folders/xx/gsd-trae-local-abc123/.trae/CLAUDE.md',
],
['global tilde prefix (post "~/.claude/" -> "~/.trae/" rewrite)', '~/.trae/CLAUDE.md'],
['$HOME-variable prefix (post "$HOME/.claude/" -> "$HOME/.trae/" rewrite)', '$HOME/.trae/CLAUDE.md'],
['backtick-wrapped local relative prefix', '`./.trae/CLAUDE.md`'],
];
for (const [label, input] of parityCases) {
test(`identical output for ${label}`, () => {
assert.strictEqual(
convertClaudeToTraeMarkdown(input),
runtimeArtifactConversion.convertClaudeToTraeMarkdown(input),
);
});
}
test('fast-check property: any arbitrary ".trae/"-tail path produces identical output in both implementations', () => {
fc.assert(
fc.property(
fc.string({ maxLength: 30 }).filter((s) => !s.includes('`') && !/\s/.test(s)),
(prefix) => {
const content = `${prefix}.trae/CLAUDE.md`;
assert.strictEqual(
convertClaudeToTraeMarkdown(content),
runtimeArtifactConversion.convertClaudeToTraeMarkdown(content),
);
},
),
{ numRuns: 200 },
);
});
});
describe('#2658: end-to-end --trae install never emits the malformed path (acceptance criterion 1)', () => {
test('local install: no emitted .md/.js/.cjs file contains the malformed strings; the rules file is concrete', () => {
const { configDir, root } = runMinimalInstall({ runtime: 'trae', scope: 'local' });
try {
const files = walk(configDir)
.filter((f) => /\.(md|js|cjs)$/.test(f))
// gsd-core/CHANGELOG.md is excluded by exact relative path (not a blanket
// .md skip — the emitted agent/command/workflow markdown this gate exists
// to guard stays fully scanned). CHANGELOG.md legitimately QUOTES the
// malformed `.claude/.trae/rules` / `.trae/.trae/rules` strings while
// documenting the #2658 fix itself (#3006) — that historical-value
// citation is not a regression of the installer's actual output. Verified
// empirically: excluding only this one file drops the hit count to zero
// across all 620 other emitted files.
.filter((f) => f.split(path.sep).join('/').indexOf('gsd-core/CHANGELOG.md') === -1);
assert.ok(files.length > 0, 'expected at least one emitted .md/.js/.cjs file');
for (const file of files) {
const content = fs.readFileSync(file, 'utf8');
assert.ok(!content.includes(MALFORMED_SINGLE), `${file} must not contain "${MALFORMED_SINGLE}"`);
assert.ok(!content.includes(MALFORMED_DOUBLE), `${file} must not contain "${MALFORMED_DOUBLE}"`);
}
} finally {
cleanup(root);
}
});
});
describe('#2658 acceptance criterion 3: new-project.md / ingest-docs.md detect trae before falling back to claude', () => {
const workflowsDir = path.join(REPO_ROOT, 'gsd-core', 'workflows');
test('new-project.md recognizes /.trae/ path and TRAE_CONFIG_DIR before the claude fallback', () => {
const content = fs.readFileSync(path.join(workflowsDir, 'new-project.md'), 'utf8');
const pathBlock = content.match(/Derive `RUNTIME`[\s\S]*?Otherwise → `RUNTIME=claude`/);
assert.ok(pathBlock, 'runtime-detection path block must exist');
assert.ok(
/Path contains `\/\.trae\/` → `RUNTIME=trae`/.test(pathBlock[0]),
'path-based detection must recognize /.trae/ before the claude fallback',
);
const envBlock = content.match(/if \[ -n "\$CODEX_HOME" \][\s\S]*?else RUNTIME="claude"; fi/);
assert.ok(envBlock, 'env-var fallback block must exist');
assert.ok(
/TRAE_CONFIG_DIR/.test(envBlock[0]),
'env-var fallback must recognize TRAE_CONFIG_DIR before the claude fallback',
);
});
test('ingest-docs.md carries the same trae detection (found during this remediation, not just new-project.md)', () => {
const content = fs.readFileSync(path.join(workflowsDir, 'ingest-docs.md'), 'utf8');
const block = content.match(/\*\*Detect runtime\*\*[\s\S]*?else → `RUNTIME=claude`/);
assert.ok(block, 'runtime-detection block must exist');
assert.ok(
/`\/\.trae\/` → `RUNTIME=trae`/.test(block[0]),
'ingest-docs.md must also recognize /.trae/ before the claude fallback',
);
assert.ok(/TRAE_CONFIG_DIR/.test(content), 'env-var fallback mention must include TRAE_CONFIG_DIR');
});
});