config-audit/tests/scanners/subtraction-write.test.mjs
Kjell Tore Guttormsen 000e47f9d2 feat(scanners): the subtraction axis can now remove what it proposes (SUB-WRITE)
`optimize --subtract` has only ever proposed. `--apply` executes the blocks the
operator picks, behind a backup whose coverage is verified and a scope gate the
engine enforces rather than describes.

The open design decision from plan §C6 was settled by two measurements, not by
taste. It is NOT a fix-engine action: the subtraction axis appears nowhere in
scan-orchestrator or optimization-lens-scanner, so verifyFixes' re-scan would
mark every removal `verified` whether or not it happened -- a success-shaped
no-op, the same shape that made restoreBackup silently do nothing. It is NOT a
plan/implement step either: that pipeline needs a finding code, and OPT declares
exactly one, for the deterministic check.

The approval artifact is written by main context, not by the lens agent. That is
where the operator's decision actually happens, and it keeps the feature off the
still-unmeasured agent write surface (M-BUG-18 lists optimize as open).

Three properties are load-bearing, and each was seen red against its own defect:
removals validate against the ORIGINAL content and apply in descending line
order; the range check is not redundant with the text check (`line: 0` makes
`slice(-1, 0)` empty, so an empty text MATCHES and `splice(-1, 1)` deletes the
file's last line); and createBackup skips a nonexistent path while still
returning an id, so manifest coverage is asserted before a byte changes.

Two guards were green on their own defect and were fixed after measuring:
`/\b80\s*%\b/` never matches "80% of the file" -- `%` is a non-word character, so
the trailing `\b` demands a word character next. And the caller-arm sweep passed
vacuously against HEAD, iterating an empty list; only the added non-emptiness
assertion caught it.

The floor is repeated, not moved: floor-exclusion still vetoes before anything is
proposed, and the engine refuses a load-bearing block again so a hand-built
approval cannot route around it. `mv` to `_archive/` is a file-level rule and
does not apply to a block excision -- the timestamped backup is the recovery
artifact, and a second copy with no restorer would be worse than none.

strongestGate moves into write-scope.mjs so the gate ordering has one owner.

Dogfooded DRY-RUN against the real ~/.claude/CLAUDE.md: 29 candidates, gate
refused all 29 with exit 0 until the scope was approved, then 29/29 spans
validated with nothing written. ~789 tokens, ~18% of the file -- corroborating
the #40 fasit's ~850, and well short of what a deletion feature is tempted to
promise.

Suite 1625 -> 1659/0. Frozen v5.0.0 and default-output baselines untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017A6vrtPKsVuM4DJ27p7jzw
2026-08-10 06:10:42 +02:00

265 lines
12 KiB
JavaScript

/**
* SUB-WRITE engine — the write half of `optimize --subtract` (#63).
*
* The judgement half is an agent's; this half must be byte-deterministic,
* because it is the only path in the plugin that REMOVES configuration. Each
* test below corresponds to a numbered prediction in
* `docs/subwrite-fasit.local.md` §3, written before the engine existed.
*
* Two measurements settled the design and are re-asserted here by construction:
* the subtraction axis is absent from the orchestrated envelope (so
* `fix-engine.verifyFixes` would have marked every removal `verified` whether
* or not it happened), and `OPT` declares exactly one finding code, for the
* deterministic check. This engine therefore stands on its own.
*/
import { describe, it, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { join } from 'node:path';
import { mkdtemp, readFile, writeFile, rm, mkdir } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { exciseBlocks, applySubtraction } from '../../scanners/lib/subtraction-write.mjs';
import { parseManifest } from '../../scanners/lib/backup.mjs';
// Every backup this file creates stays in a temp root — otherwise the suite
// writes into the operator's real ~/.claude/config-audit/backups, where
// cleanupOldBackups() would start deleting genuine backups past MAX_BACKUPS.
const TEST_BACKUP_ROOT = join(tmpdir(), `config-audit-subwrite-backups-${process.pid}`);
process.env.CONFIG_AUDIT_BACKUP_ROOT = TEST_BACKUP_ROOT;
const BLOCK_A = '- Always write tests before code, and never skip the failing step.';
const BLOCK_FLOOR = '- Push to `git.example.test` after every commit.';
const BLOCK_B = '- Be concise and avoid unnecessary explanation in your answers.';
/** Line numbers are 1-based: BLOCK_A = 5, BLOCK_FLOOR = 7, BLOCK_B = 9. */
const FIXTURE = [
'# Project', // 1
'', // 2
'## Rules', // 3
'', // 4
BLOCK_A, // 5
'', // 6
BLOCK_FLOOR, // 7
'', // 8
BLOCK_B, // 9
'', // 10
'## End', // 11
'', // 12
].join('\n');
let dir;
let repo;
let file;
beforeEach(async () => {
dir = await mkdtemp(join(tmpdir(), 'config-audit-subwrite-'));
// A repo root of its own, so classification lands on `in-repo` unless a test
// deliberately targets somewhere else.
repo = join(dir, 'repo');
await mkdir(join(repo, '.git'), { recursive: true });
file = join(repo, 'CLAUDE.md');
await writeFile(file, FIXTURE, 'utf-8');
});
afterEach(async () => {
await rm(dir, { recursive: true, force: true });
});
/** An approval entry as `optimize.md` writes it from the lens payload. */
const removal = (line, endLine, text) => ({ file, line, endLine, text });
describe('exciseBlocks (pure)', () => {
it('P1 — removes a block whose text matches the file at line..endLine', () => {
const result = exciseBlocks(FIXTURE, [removal(5, 5, BLOCK_A)]);
assert.equal(result.refused.length, 0);
assert.equal(result.applied.length, 1);
assert.equal(result.applied[0].text, BLOCK_A);
assert.ok(!result.content.includes(BLOCK_A), 'block A must be gone');
assert.ok(result.content.includes(BLOCK_B), 'block B must survive');
assert.ok(result.content.includes(BLOCK_FLOOR), 'the floor block must survive');
});
it('P2 — refuses when the file no longer matches the approved text', () => {
const drifted = FIXTURE.replace(BLOCK_A, '- Always write tests before code, and never skip it.');
const result = exciseBlocks(drifted, [removal(5, 5, BLOCK_A)]);
assert.equal(result.applied.length, 0);
assert.equal(result.refused.length, 1);
assert.equal(result.refused[0].reason, 'block-mismatch');
assert.equal(result.content, drifted, 'the content must come back byte-identical');
});
it('P3 — refuses an out-of-range span without throwing', () => {
const result = exciseBlocks(FIXTURE, [removal(400, 402, BLOCK_A)]);
assert.equal(result.applied.length, 0);
assert.equal(result.refused[0].reason, 'block-mismatch');
assert.equal(result.content, FIXTURE);
});
it('P3 — a non-positive line is refused by the RANGE check, not by luck', () => {
// Measured (#63): with the range check disabled, the P3 case above stays
// green — `lines.slice(399, 402)` is empty, so the text check refuses it
// anyway and the test passes for the wrong reason. This is the case only
// the range check can catch: `slice(-1, 0)` is also empty, so an empty
// `text` MATCHES, and `splice(-1, 1)` then deletes the file's LAST line.
const result = exciseBlocks(FIXTURE, [removal(0, 0, '')]);
assert.equal(result.applied.length, 0);
assert.equal(result.refused[0].reason, 'block-mismatch');
assert.equal(result.content, FIXTURE, 'no line may be removed from the far end');
});
it('P4 — refuses a load-bearing block fed straight to the engine', () => {
// The pre-filter's veto never ran: this is the engine's own red line, so a
// caller that hand-builds an approval cannot route around the floor.
const result = exciseBlocks(FIXTURE, [removal(7, 7, BLOCK_FLOOR)]);
assert.equal(result.applied.length, 0);
assert.equal(result.refused.length, 1);
assert.equal(result.refused[0].reason, 'floor');
assert.ok(result.content.includes(BLOCK_FLOOR));
});
it('P5 — two blocks in one file: the second span is not shifted by the first removal', () => {
const result = exciseBlocks(FIXTURE, [removal(5, 5, BLOCK_A), removal(9, 9, BLOCK_B)]);
assert.equal(result.refused.length, 0);
assert.equal(result.applied.length, 2);
// Assert the exact surviving text, not merely "does not include": a naive
// ascending implementation removes block A and then whatever slid into
// lines 9..9, which is easy to mistake for success.
assert.equal(
result.content,
['# Project', '', '## Rules', '', BLOCK_FLOOR, '', '## End', ''].join('\n'),
);
});
it('P6 — collapses the double blank line a removal leaves at the seam', () => {
const result = exciseBlocks(FIXTURE, [removal(5, 5, BLOCK_A)]);
assert.ok(!/\n\n\n/.test(result.content), 'no run of two blank lines may survive');
});
it('P12 — a mismatch alongside a valid removal refuses only the mismatch', () => {
const result = exciseBlocks(FIXTURE, [
removal(5, 5, BLOCK_A),
removal(9, 9, '- Something that is not in this file at all.'),
]);
assert.equal(result.applied.length, 1);
assert.equal(result.refused.length, 1);
assert.equal(result.refused[0].reason, 'block-mismatch');
assert.ok(!result.content.includes(BLOCK_A));
assert.ok(result.content.includes(BLOCK_B), 'the refused block stays put');
});
});
describe('applySubtraction (filesystem + gate)', () => {
it('P1 — writes the file and reports the removed text', async () => {
const result = await applySubtraction([removal(5, 5, BLOCK_A)], { repoRoot: repo });
assert.equal(result.applied.length, 1);
assert.equal(result.filesWritten.length, 1);
const after = await readFile(file, 'utf-8');
assert.ok(!after.includes(BLOCK_A));
assert.ok(after.includes(BLOCK_B));
});
it('P7 — a dry run writes nothing and creates no backup', async () => {
const result = await applySubtraction([removal(5, 5, BLOCK_A)], {
repoRoot: repo,
dryRun: true,
});
assert.equal(result.dryRun, true);
assert.equal(result.backupId, null);
assert.equal(result.filesWritten.length, 0);
assert.equal(result.applied.length, 1, 'it still reports what WOULD be removed');
assert.equal(await readFile(file, 'utf-8'), FIXTURE, 'the file must be untouched');
});
it('P8 — a require-ok target without approval writes nothing, and that is exit-0 territory', async () => {
// `~/.claude/CLAUDE.md` under a fake home: the subtraction axis's primary
// target, and the one whose cost lands in every repo on every turn.
const home = join(dir, 'home');
const userConfig = join(home, '.claude');
await mkdir(userConfig, { recursive: true });
const userFile = join(userConfig, 'CLAUDE.md');
await writeFile(userFile, FIXTURE, 'utf-8');
const result = await applySubtraction(
[{ file: userFile, line: 5, endLine: 5, text: BLOCK_A }],
{ repoRoot: repo, home },
);
assert.equal(result.gate, 'require-ok');
assert.equal(result.requiresApproval, true);
assert.ok(result.disclosures.length >= 1, 'the gate must say why, not just refuse');
assert.equal(result.applied.length, 0);
assert.equal(result.filesWritten.length, 0);
assert.equal(result.refused[0].reason, 'scope-gate');
assert.equal(await readFile(userFile, 'utf-8'), FIXTURE);
});
it('P9 — the same target proceeds once the scope is explicitly approved', async () => {
const home = join(dir, 'home');
const userConfig = join(home, '.claude');
await mkdir(userConfig, { recursive: true });
const userFile = join(userConfig, 'CLAUDE.md');
await writeFile(userFile, FIXTURE, 'utf-8');
const result = await applySubtraction(
[{ file: userFile, line: 5, endLine: 5, text: BLOCK_A }],
{ repoRoot: repo, home, approveScope: true },
);
assert.equal(result.requiresApproval, true, 'the gate still reports what it classified');
assert.equal(result.applied.length, 1);
assert.ok(!(await readFile(userFile, 'utf-8')).includes(BLOCK_A));
});
it('P10 — the backup must cover the file that is actually written, not merely exist', async () => {
const result = await applySubtraction([removal(5, 5, BLOCK_A)], { repoRoot: repo });
// createBackup() skips a path that does not exist and still returns a
// manifest and an id, so "a backup was made" is not evidence (M-BUG-31).
assert.ok(result.backupId, 'a backup id is expected');
const manifest = parseManifest(
await readFile(join(TEST_BACKUP_ROOT, result.backupId, 'manifest.yaml'), 'utf-8'),
);
const covered = manifest.files.map((f) => f.originalPath);
for (const written of result.filesWritten) {
assert.ok(covered.includes(written), `backup does not cover ${written}`);
}
// …and the copy holds the PRE-removal bytes, which is what makes rollback real.
const copy = manifest.files.find((f) => f.originalPath === file);
assert.equal(
await readFile(join(TEST_BACKUP_ROOT, result.backupId, 'files', copy.backupPath.replace('./files/', '')), 'utf-8'),
FIXTURE,
);
});
it('P10 — aborts before any write when the backup cannot cover a target', async () => {
// A target that vanishes between approval and write: createBackup() would
// skip it silently, so the engine must refuse rather than write unbacked.
const ghost = join(repo, 'GONE.md');
const result = await applySubtraction(
[removal(5, 5, BLOCK_A), { file: ghost, line: 1, endLine: 1, text: 'x' }],
{ repoRoot: repo },
);
assert.equal(result.filesWritten.length, 0, 'nothing may be written');
assert.equal(await readFile(file, 'utf-8'), FIXTURE, 'the healthy file must be untouched too');
assert.ok(result.refused.some((r) => r.reason === 'unreadable'));
});
it('refuses everything when nothing survives validation, and leaves no backup behind', async () => {
const result = await applySubtraction([removal(7, 7, BLOCK_FLOOR)], { repoRoot: repo });
assert.equal(result.applied.length, 0);
assert.equal(result.backupId, null, 'no backup for a run that writes nothing');
assert.equal(await readFile(file, 'utf-8'), FIXTURE);
});
});