Files
get-shit-done/tests/pattern-mapper.test.cjs
Tom Boucher 2acb38c918 fix(pattern-mapper): prevent redundant file reads and add early-stop rule (#2312) (#2327)
* feat: add /gsd-spec-phase — Socratic spec refinement with ambiguity scoring (#2213)

Introduces `/gsd-spec-phase <phase>` as an optional pre-step before discuss-phase.
Clarifies WHAT a phase delivers (requirements, boundaries, acceptance criteria) with
quantitative ambiguity scoring before discuss-phase handles HOW to implement.

- `commands/gsd/spec-phase.md` — slash command routing to workflow
- `get-shit-done/workflows/spec-phase.md` — full Socratic interview loop (up to 6
  rounds, 5 rotating perspectives: Researcher, Simplifier, Boundary Keeper, Failure
  Analyst, Seed Closer) with weighted 4-dimension ambiguity gate (≤ 0.20 to write SPEC.md)
- `get-shit-done/templates/spec.md` — SPEC.md template with falsifiable requirements
  (Current/Target/Acceptance per requirement), Boundaries, Acceptance Criteria,
  Ambiguity Report, and Interview Log; includes two full worked examples
- `get-shit-done/workflows/discuss-phase.md` — new `check_spec` step detects
  `{padded_phase}-SPEC.md` at startup; displays "Found SPEC.md — N requirements
  locked. Focusing on implementation decisions."; `analyze_phase` respects `spec_loaded`
  flag to skip "what/why" gray areas; `write_context` emits `<spec_lock>` section
  with boundary summary and canonical ref to SPEC.md
- `docs/ARCHITECTURE.md` — update command/workflow counts (74→75, 71→72)

Closes #2213

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(pattern-mapper): prevent redundant file reads and add early-stop rule (#2312)

Adds three explicit constraints to the agent prompt:
1. Read each analog file EXACTLY ONCE (no re-reads from context)
2. For files > 2,000 lines, use Grep + Read with offset/limit instead of full load
3. Stop analog search after 3–5 strong matches

Also adds <critical_rules> block to surface these constraints at high salience.
Adds regression tests READS-01, READS-02, READS-03.

Closes #2312

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(pattern-mapper): clarify re-read rule allows non-overlapping targeted reads (CR feedback)

"Read each file EXACTLY ONCE" conflicted with the large-file targeted-read
strategy. Rewrites both the Step 4 guidance and the <critical_rules> block to
make the rule precise: re-reading the same range is forbidden; multiple
non-overlapping targeted reads for large files are permitted.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-16 17:15:29 -04:00

143 lines
5.1 KiB
JavaScript

/**
* Tests for Pattern Mapper feature (#1861, #2312)
*
* Covers:
* - Config key workflow.pattern_mapper in VALID_CONFIG_KEYS
* - Default value is true
* - Config round-trip (set/get)
* - init plan-phase output includes patterns_path (null when missing, path when present)
* - Agent prompt contains no-re-read and early-stop constraints (#2312)
*/
const { describe, test, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const { runGsdTools, createTempProject, cleanup } = require('./helpers.cjs');
describe('pattern-mapper config key', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempProject();
});
afterEach(() => {
cleanup(tmpDir);
});
test('workflow.pattern_mapper is a valid config key', () => {
// Setting an invalid key produces an error; a valid key succeeds
const result = runGsdTools('config-set workflow.pattern_mapper true', tmpDir, { HOME: tmpDir });
assert.ok(result.success, `Expected success but got error: ${result.error}`);
});
test('default value is true in CONFIG_DEFAULTS', () => {
// Create a new project config and verify the default
const result = runGsdTools('config-new-project', tmpDir, { HOME: tmpDir });
assert.ok(result.success, `config-new-project failed: ${result.error}`);
const configPath = path.join(tmpDir, '.planning', 'config.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
assert.strictEqual(config.workflow.pattern_mapper, true);
});
test('config round-trip set/get', () => {
// Ensure config exists first
runGsdTools('config-new-project', tmpDir, { HOME: tmpDir });
// Set to false
const setResult = runGsdTools('config-set workflow.pattern_mapper false', tmpDir, { HOME: tmpDir });
assert.ok(setResult.success, `config-set failed: ${setResult.error}`);
// Get should return false
const getResult = runGsdTools('config-get workflow.pattern_mapper', tmpDir, { HOME: tmpDir });
assert.ok(getResult.success, `config-get failed: ${getResult.error}`);
assert.strictEqual(getResult.output, 'false');
});
});
describe('init plan-phase patterns_path', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempProject();
// Create minimal planning structure for init plan-phase
const planningDir = path.join(tmpDir, '.planning');
fs.writeFileSync(path.join(planningDir, 'STATE.md'), [
'# State',
'',
'## Current Phase',
'Phase 1 — Foundation',
].join('\n'));
fs.writeFileSync(path.join(planningDir, 'ROADMAP.md'), [
'# Roadmap',
'',
'## Phase 1: Foundation',
'Build the foundation.',
'**Status:** Planning',
'**Requirements:** [FOUND-01]',
].join('\n'));
// Create phase directory
const phaseDir = path.join(planningDir, 'phases', '01-foundation');
fs.mkdirSync(phaseDir, { recursive: true });
});
afterEach(() => {
cleanup(tmpDir);
});
test('patterns_path is null when no PATTERNS.md exists', () => {
const result = runGsdTools('init plan-phase 1', tmpDir, { HOME: tmpDir });
assert.ok(result.success, `init plan-phase failed: ${result.error}`);
const data = JSON.parse(result.output);
assert.strictEqual(data.patterns_path, null);
});
test('patterns_path contains correct path when PATTERNS.md exists', () => {
// Create a PATTERNS.md in the phase directory
const phaseDir = path.join(tmpDir, '.planning', 'phases', '01-foundation');
fs.writeFileSync(path.join(phaseDir, '01-PATTERNS.md'), '# Patterns\n');
const result = runGsdTools('init plan-phase 1', tmpDir, { HOME: tmpDir });
assert.ok(result.success, `init plan-phase failed: ${result.error}`);
const data = JSON.parse(result.output);
assert.ok(data.patterns_path, 'patterns_path should not be null');
assert.ok(data.patterns_path.includes('PATTERNS.md'), `Expected path to contain PATTERNS.md, got: ${data.patterns_path}`);
assert.ok(data.patterns_path.includes('01-foundation'), `Expected path to include phase dir, got: ${data.patterns_path}`);
});
});
describe('gsd-pattern-mapper agent prompt efficiency constraints (#2312)', () => {
const agentPath = path.join(__dirname, '..', 'agents', 'gsd-pattern-mapper.md');
let agentContent;
beforeEach(() => {
agentContent = fs.readFileSync(agentPath, 'utf-8');
});
test('READS-01: prompt contains no-re-read constraint', () => {
assert.ok(
/read each.*file.*once/i.test(agentContent) || /never re-read/i.test(agentContent),
'Agent prompt must instruct the model to read each analog file only once'
);
});
test('READS-02: prompt contains early-stop instruction', () => {
assert.ok(
/stop.*analog|3.?5.*analog|early.stop/i.test(agentContent),
'Agent prompt must instruct the model to stop after finding 3-5 analogs'
);
});
test('READS-03: prompt contains large-file strategy', () => {
assert.ok(
/2[,.]?000.*line|offset.*limit|large file/i.test(agentContent),
'Agent prompt must include guidance for reading large files with offset/limit'
);
});
});