Files
msd-core/tests/quick-batch.property.test.cjs
Tom Boucher 91ed46882a feat(#3675): quick-batch core primitives and resumable manifest (#4190)
* test(#3675): add failing tests for quick-batch core primitives

Adds the full behavioral (tests/quick-batch.test.cjs) and property-based
(tests/quick-batch.property.test.cjs) coverage for #3675's quick-batch core
primitives per the phase's 35-row test matrix — task-list parsing (inline +
--file, with path-confinement/symlink-escape/non-regular-file rejection),
collision-safe quick-id preallocation under withPlanningLock, BATCH.json
schema/validation/resume, dependency-DAG + partitionByFileOverlap wave
construction, and exactly-once STATE.md completion (including the
STATE-row-written-but-manifest-not-yet-updated crash window).

The import target (gsd-core/bin/lib/quick-batch.cjs, compiled from a
not-yet-written src/quick-batch.cts) does not exist yet — every test in both
files fails at the top-level require() before any assertion runs. Five
fast-check properties cover collision-freedom under lock contention, resume
idempotency, exactly-once STATE completion, wave totality, and DAG-respecting
wave order, per the design doc's property-based-coverage requirement.

* feat(#3675): implement quick-batch core primitives

Adds src/quick-batch.cts (ADR-457 build-at-publish, compiled to
gsd-core/bin/lib/quick-batch.cjs) implementing #3675's quick-batch core
primitives per the phase design lock — pure/state primitives and
CLI-testable core operations only, no agent dispatch, no worktree creation,
no user-facing command (Phase 4/#3676's job):

- parseTaskList / parseTaskListFromFile: inline bulleted/numbered task-list
  parsing (>=2 items required) and a --file variant strictly confined to the
  planning workspace root via requireSafePath, rejecting non-regular-file
  targets.
- allocateQuickIds / createBatch: collision-safe YYMMDD-xxx quick-id
  preallocation under withPlanningLock, checked against both on-disk
  .planning/quick/ entries and sibling .planning/quick-batches/*/BATCH.json
  manifests (never on-disk-only, which would miss another in-flight batch
  that hasn't dispatched any real quick directory yet) — replicates
  cmdInitQuick's own grammar rather than delegating to it (that function's
  2-second granularity is not batch-safe).
- computeWaves: deterministic wave construction combining dependency-DAG
  layering with partitionByFileOverlap (#3674), called per DAG layer over
  path-separator-normalized planned_files — normalization happens at this
  module's boundary, never inside the Phase 2 helper.
- loadBatch: fail-closed BATCH.json schema validation (corrupt/truncated
  JSON, wrong types, missing fields, out-of-batch dependency references,
  dependency cycles, a worktree path absent from disk).
- resumeBatch: skips complete items, never auto-retries failed items,
  propagates/reverses blocked status along the DAG to a fixed point, and
  detects a STATE.md row that already exists for a non-complete item (the
  "STATE written, BATCH.json not yet updated" crash window) — completing it
  without re-appending. Idempotent across repeated calls.
- completeQuickItem / hasQuickTaskRow: exactly-once STATE.md completion —
  appendQuickTaskRow (unmodified) is called at most once per quick id, gated
  by hasQuickTaskRow's own idempotency check re-parsing the real "Quick Tasks
  Completed" table, since appendQuickTaskRow itself carries no idempotency.

BATCH.json lives at .planning/quick-batches/<batch-id>/BATCH.json, a sibling
of .planning/quick/ — never inside it, so scanQuickTasks never misreads a
batch manifest as a broken quick task.

* docs(#3675): register the new quick-batch module

New src/*.cts -> bin/lib/*.cjs modules need four hand-maintained
registrations beyond the code itself: .gitignore (compiled artifact),
eslint.config.mjs (ADR-457: lint the .cts, not the emitted .cjs),
docs/INVENTORY.md's CLI Modules roster row plus the regenerated
docs/INVENTORY-MANIFEST.json cli_modules entry, and a CONTEXT.md glossary
entry matching the convention set by the sibling File Overlap Partitioner
Module (#3674) entry it sits beside.

NOTE: docs/INVENTORY-MANIFEST.json was updated BY HAND (alphabetically
sorted single-entry insertion into families.cli_modules, matching the
existing file's structure) rather than via
`node scripts/gen-inventory-manifest.cjs --write` — this session's
MEMTRACE-FIRST guard hard-blocks direct execution of that indexed script
path from Bash, with no available Memtrace tool to route through instead.
The orchestrator should re-run `node scripts/gen-inventory-manifest.cjs
--check` to confirm this hand-edit is byte-identical to the generator's own
output before merging.

* fix(#3675): resolve lint findings in quick-batch primitives and tests

Unsafe `any[]` assignment from `new Array(n)` in the DAG cycle-check color
array, two unnecessary `as string[]` casts TS 5.5's inferred type
predicates already narrowed, raw `fs.rmSync` in test cleanup (needs the
Windows-EBUSY retry budget `helpers.cleanup` carries), an unused `loadBatch`
import, an unbounded `mkfifo` subprocess spawn missing a timeout, and a
CONTEXT.md glossary illustration that looked like a real file reference.

* feat(#3675): close acceptance-criteria gaps found in review

Standards- and spec-axis review (plus a self-caught race) surfaced real
gaps against issue #3675's own acceptance criteria and this repo's test
conventions:

- BATCH.json was missing options, base_revision, per-item wave, and
  per-item commit — the issue's AC explicitly lists all four as things
  the manifest must track. Added them: createBatch persists caller-supplied
  batchOptions/baseRevision verbatim and assigns each item its computed
  wave index; completeQuickItem now persists the commit onto the item,
  not just the STATE.md row. All four are backward-tolerant on load (an
  older/hand-built manifest without them still validates).
- resumeBatch had no "incompatible base divergence" check at all, despite
  the AC and the ADR's own "Base divergence" section requiring one. Added
  an opt-in currentBaseRevision comparison that fails closed with a
  recoverable diagnostic on mismatch, and touches nothing on refusal.
- resumeBatch read-modify-wrote BATCH.json OUTSIDE withPlanningLock — the
  only durable write path in this module that wasn't lock-protected,
  a real lost-update race against a concurrent completeQuickItem or
  another resume. Now runs inside the same lock createBatch/
  completeQuickItem use.
- loadBatch and collectExistingBatchQuickIds used raw JSON.parse with no
  size cap (security review, Low/informational); switched to the
  existing safeJsonParse (1MB cap) for defense-in-depth.
- Parser (parseTaskList) had only example-based tests; CLAUDE.md requires
  a fast-check property test for parsers. Added one plus a companion
  reject-property for <2 items.
- The id-exhaustion fail-closed ceiling (MAX_TIME_BLOCK) was untested at
  any boundary. Exported the pure allocateIdsGivenUsed/MAX_TIME_BLOCK for
  direct limit-1/limit/limit+1 testing without needing 46k fixture dirs.
- Issue AC explicitly asks for prompt-injection-payload test coverage,
  distinct from the existing shell-metacharacter test; added one.
- Test row 9 (FIFO skip) silently returned instead of calling t.skip(),
  so an unsupported platform would report a pass rather than a documented
  skip; fixed to bind the test-context param and skip properly.
- Extracted toWaveInput to remove a 2-site production duplication of the
  QuickBatchItem -> computeWaves reshape (Standards-axis smell).
- Added the required .changeset/ fragment (CONTRIBUTING.md: editing src/
  is user-facing even though the compiled .cjs is gitignored).

* fix(#3675): restore "not valid JSON" wording in loadBatch's parse-failure reason

gsd-test caught this: switching loadBatch to safeJsonParse changed the parse-
failure message shape ("... parse error — ...") without preserving the
"not valid JSON" substring row 27's own test asserts on. Re-wrap
safeJsonParse's error into the original diagnostic phrasing regardless of
which of its three failure modes fired.

* docs(#3675): backfill changeset pr number to 4190

* fix(#3675): detect a silently-no-op mkfifo on Windows, not just a throwing one

CI caught this on windows-latest: row 9's platform-skip only caught mkfifo
throwing (command not found). On this runner mkfifo resolves to something
that exits 0 without creating a file (NTFS has no FIFO concept), so
execution fell through to parseTaskListFromFile against a path that
doesn't exist, producing an ENOENT stat error instead of the expected
"not a regular file" rejection. Check the artifact actually exists before
trusting a zero exit code, and skip with a documented reason either way.

---------

Co-authored-by: sim <sim@local>
2026-09-02 15:38:27 -04:00

245 lines
9.5 KiB
JavaScript

'use strict';
/**
* quick-batch.property.test.cjs — Property-based tests for quick-batch core
* primitives (#3675, epic #3344, ADR-1239 "Quick-batch binding").
*
* Module: gsd-core/bin/lib/quick-batch.cjs (compiled from src/quick-batch.cts)
*
* Test matrix rows covered (`.gsd/phase/feat-3675-quick-batch-core-primitives/50-test-matrix.md`):
* parser — parseTaskList round-trips any valid bulleted task list (CLAUDE.md
* "Property-Based Testing: Parsers... must include at least one
* fast-check property test")
* 15 — collision-freedom under lock contention (allocation)
* 26 — resume idempotency
* 30 — exactly-once STATE completion
* 32 — wave totality (every item in exactly one wave)
* 33 — wave order respects the DAG
*
* Every property calls the REAL, unmodified `createBatch` / `resumeBatch` /
* `completeQuickItem` / `computeWaves` — never a mock — per the test matrix's
* "Assertion-shape note".
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const fc = require('./helpers/fast-check-setup.cjs');
const {
parseTaskList,
createBatch,
computeWaves,
resumeBatch,
completeQuickItem,
} = require('../gsd-core/bin/lib/quick-batch.cjs');
const { makeFakeClock } = require('./helpers/clock.cjs');
const { cleanup } = require('./helpers.cjs');
function mkTmpProject() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'quick-batch-prop-'));
fs.mkdirSync(path.join(dir, '.planning'), { recursive: true });
return dir;
}
function cleanupDir(dir) {
cleanup(dir);
}
function stateWithQuickTasksSection() {
return [
'# STATE',
'',
'## Quick Tasks Completed',
'',
'| # | Description | Date | Commit | Status | Directory |',
'| --- | --- | --- | --- | --- | --- |',
'',
].join('\n');
}
/** A small acyclic dependency graph: item i may depend on any j < i (DAG by construction). */
const dagItemsArb = fc.integer({ min: 1, max: 8 }).chain((n) =>
fc.tuple(
...Array.from({ length: n }, (_, i) =>
fc.record({
description: fc.constant(`item-${i}`),
dependsOnPrevious: fc.subarray(Array.from({ length: i }, (_, j) => j), { maxLength: i }),
files: fc.array(fc.constantFrom('f0', 'f1', 'f2', 'f3'), { maxLength: 2 }),
}),
),
),
);
// Trimmed, single-line, non-empty description — sidesteps the parser's own
// whitespace-collapsing at the bullet/content boundary (a leading run of
// whitespace right after the bullet marker is consumed by the required
// separator, not preserved as content) so round-tripping is exact.
const taskDescriptionArb = fc.string({ minLength: 1, maxLength: 40 })
.filter((s) => !/[\r\n]/.test(s) && s === s.trim() && s.length > 0);
const bulletArb = fc.constantFrom('-', '*');
describe('quick-batch: property — parseTaskList round-trips any valid task list (parser)', () => {
test('property: N (>=2) bulleted descriptions parse back in order, byte-identical', () => {
fc.assert(fc.property(
fc.array(taskDescriptionArb, { minLength: 2, maxLength: 20 }),
fc.array(bulletArb, { minLength: 20, maxLength: 20 }),
(descriptions, bullets) => {
const text = descriptions.map((d, i) => `${bullets[i]} ${d}`).join('\n');
const result = parseTaskList(text);
assert.equal(result.ok, true);
assert.deepEqual(result.value.map((it) => it.description), descriptions);
},
), { numRuns: 100 });
});
test('property: fewer than 2 parsed lines is always rejected', () => {
fc.assert(fc.property(
fc.option(taskDescriptionArb, { nil: undefined }),
(maybeOne) => {
const text = maybeOne === undefined ? 'just prose, no bullets\nmore prose' : `- ${maybeOne}`;
const result = parseTaskList(text);
assert.equal(result.ok, false);
},
), { numRuns: 30 });
});
});
describe('quick-batch: property — collision-freedom under lock contention (row 15)', () => {
test('property: any number of sequential createBatch calls sharing one frozen clock never collide', () => {
fc.assert(fc.property(
fc.integer({ min: 2, max: 5 }), // number of createBatch calls
fc.integer({ min: 1, max: 4 }), // items per call
(numCalls, itemsPerCall) => {
const dir = mkTmpProject();
try {
const clock = makeFakeClock(Date.UTC(2026, 5, 1, 12, 0, 0));
const allIds = [];
for (let c = 0; c < numCalls; c++) {
const items = Array.from({ length: itemsPerCall }, (_, i) => ({ description: `call${c}-item${i}` }));
const result = createBatch(dir, items, { clock });
assert.equal(result.ok, true);
allIds.push(result.value.batchId, ...result.value.manifest.items.map((it) => it.quick_id));
}
assert.equal(new Set(allIds).size, allIds.length, 'zero cross-call id collisions');
} finally {
cleanupDir(dir);
}
},
), { numRuns: 30 }); // filesystem-backed property — bounded below the global 200 default
});
});
describe('quick-batch: property — resume idempotency (row 26)', () => {
test('property: resuming an unchanged manifest twice produces identical eligible sets and zero transitions the second time', () => {
fc.assert(fc.property(
dagItemsArb,
(rawItems) => {
const dir = mkTmpProject();
try {
const items = rawItems.map((it, i) => ({
description: it.description,
clientId: `c${i}`,
dependsOn: it.dependsOnPrevious.map((j) => `c${j}`),
plannedFiles: it.files,
}));
const created = createBatch(dir, items);
assert.equal(created.ok, true);
const first = resumeBatch(dir, created.value.batchId);
assert.equal(first.ok, true);
const second = resumeBatch(dir, created.value.batchId);
assert.equal(second.ok, true);
assert.deepEqual(second.value.eligible.slice().sort(), first.value.eligible.slice().sort());
assert.deepEqual(second.value.transitions, [], 'second call on an unchanged manifest is a no-op');
} finally {
cleanupDir(dir);
}
},
), { numRuns: 30 });
});
});
describe('quick-batch: property — exactly-once STATE completion (row 30)', () => {
test('property: completing the same item N times appends exactly one STATE row', () => {
fc.assert(fc.property(
fc.integer({ min: 2, max: 5 }), // repeat count
(repeatCount) => {
const dir = mkTmpProject();
fs.writeFileSync(path.join(dir, '.planning', 'STATE.md'), stateWithQuickTasksSection());
try {
const created = createBatch(dir, [{ description: 'solo' }, { description: 'other' }]);
assert.equal(created.ok, true);
const item = created.value.manifest.items[0];
const fields = { description: item.description, date: '2026-01-01', commit: 'shaX' };
let appendedCount = 0;
for (let i = 0; i < repeatCount; i++) {
const result = completeQuickItem(dir, created.value.batchId, item.quick_id, fields);
assert.equal(result.ok, true);
if (result.value.appended) appendedCount++;
}
assert.equal(appendedCount, 1, 'exactly one of the N calls actually appended');
const state = fs.readFileSync(path.join(dir, '.planning', 'STATE.md'), 'utf-8');
const rowOccurrences = state.split(item.quick_id).length - 1;
assert.equal(rowOccurrences, 1, 'exactly one row, regardless of how many times completion was requested');
} finally {
cleanupDir(dir);
}
},
), { numRuns: 30 });
});
});
describe('quick-batch: property — wave totality (row 32)', () => {
test('property: for any valid (acyclic, in-batch-only) dependency graph, every item appears in exactly one wave', () => {
fc.assert(fc.property(
dagItemsArb,
(rawItems) => {
const items = rawItems.map((it, i) => ({
quickId: `id-${i}`,
dependsOn: it.dependsOnPrevious.map((j) => `id-${j}`),
plannedFiles: it.files,
}));
const waves = computeWaves(items);
assert.equal(waves.ok, true);
const flat = waves.value.flat();
assert.equal(flat.length, items.length, 'no item lost or duplicated across waves');
assert.deepEqual(flat.slice().sort(), items.map((it) => it.quickId).sort());
},
));
});
});
describe('quick-batch: property — wave order respects the DAG (row 33)', () => {
test('property: no item\'s wave index is <= any of its dependencies\' wave indices', () => {
fc.assert(fc.property(
dagItemsArb,
(rawItems) => {
const items = rawItems.map((it, i) => ({
quickId: `id-${i}`,
dependsOn: it.dependsOnPrevious.map((j) => `id-${j}`),
plannedFiles: it.files,
}));
const waves = computeWaves(items);
assert.equal(waves.ok, true);
const waveIndexOf = new Map();
waves.value.forEach((wave, idx) => {
for (const id of wave) waveIndexOf.set(id, idx);
});
for (const it of items) {
const ownWave = waveIndexOf.get(it.quickId);
for (const dep of it.dependsOn) {
const depWave = waveIndexOf.get(dep);
assert.ok(depWave < ownWave, `dependency ${dep} (wave ${depWave}) must strictly precede ${it.quickId} (wave ${ownWave})`);
}
}
},
));
});
});