Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(spec): preserve legacy collision history on rename [D4,A5]

Corey Olson8dd1236134fb
diff --git a/src/cli.js b/src/cli.js
index 9e449ee5..9ef0b993 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -9,7 +9,7 @@ import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:pat
import { pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
import * as store from './board.js';
-import { CONFIG_FILE, COORDINATOR, loadConfig } from './config.js';
+import { CONFIG_FILE, COORDINATOR, DOCTRINE_FILE, LEGACY_DOCTRINE_FILE, loadConfig } from './config.js';
import { doctrineHistory, loadDoctrine } from './doctrine.js';
import { agentSessionDigest, requirePersonChannel } from './person.js';
import { bindLocalSession } from './agent-session.js';
@@ -38,6 +38,7 @@ import {
idProblems,
lintSpec,
loadSpec,
+ legacyDoctrineRenameBase,
permanenceProblems,
parseSpec,
readSignoffs,
@@ -78,9 +79,10 @@ const CHECKOUT_LEASES = new WeakMap();
*
* @param {string} root
* @param {string} path
+ * @param {string} [revision]
* @returns {{ status: number, stdout: string }}
*/
-function coordinatorFile(root, path) {
+function coordinatorFile(root, path, revision) {
const commonDir = git(root, ['rev-parse', '--path-format=absolute', '--git-common-dir']);
const coordinatorRoot = dirname(commonDir);
const env = { ...gitChildEnv(root), GIT_NO_REPLACE_OBJECTS: '1' };
@@ -94,7 +96,7 @@ function coordinatorFile(root, path) {
'the primary checkout must be on its attached local branch to establish the collision baseline; the coordinator returns it to its branch',
);
}
- const result = spawnSync('git', ['--no-replace-objects', 'show', `${ref}:${path}`], {
+ const result = spawnSync('git', ['--no-replace-objects', 'show', `${revision ?? ref}:${path}`], {
cwd: coordinatorRoot, env, encoding: 'utf8',
});
return { status: result.status ?? 1, stdout: result.stdout ?? '' };
@@ -2250,8 +2252,14 @@ async function specCommand(io, { first, second, rest, values }) {
const files = [[ctx.config.spec, spec], ...(practice.exists ? [[practice.name, practice]] : [])];
for (const [, parsed] of files) assertRequiredSigners(ctx.info.root, parsed.rows);
const collisionFiles = [[ctx.config.spec, spec], ...(practice.repoExists ? [[practice.name, practice.repo]] : [])];
+ const renameBase = collisionFiles.some(([name]) => name === DOCTRINE_FILE)
+ ? legacyDoctrineRenameBase(ctx.info.root, DOCTRINE_FILE)
+ : null;
const previousCounts = new Map(collisionFiles.map(([name]) => {
- const previous = coordinatorFile(ctx.info.root, name);
+ const baselineName = renameBase && name === DOCTRINE_FILE ? LEGACY_DOCTRINE_FILE : name;
+ const previous = name === DOCTRINE_FILE && !renameBase
+ ? { status: 1, stdout: '' }
+ : coordinatorFile(ctx.info.root, baselineName, renameBase ?? undefined);
const rows = previous.status === 0 ? parseSpec(previous.stdout, { strictGrammarVersion: false }).rows : [];
const counts = new Map();
for (const row of rows) counts.set(row.id, (counts.get(row.id) ?? 0) + 1);
diff --git a/src/spec.js b/src/spec.js
index c74476b6..00ac4d4b 100644
--- a/src/spec.js
+++ b/src/spec.js
@@ -8,8 +8,11 @@
* that a row is met, kept with the text they read (S5).
*/
import { createHash } from 'node:crypto';
+import { spawnSync } from 'node:child_process';
import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
+import { DOCTRINE_FILE, LEGACY_DOCTRINE_FILE } from './config.js';
+import { cleanGitEnvironment } from './git.js';
import { Refused } from './refused.js';
import { hasSignerFile, isVerifiedSignoff, signRows, verifySignedRecords } from './signature.js';
@@ -118,6 +121,41 @@ export function loadSpec(root, config) {
return { ...parseSpec(readFileSync(file, 'utf8')), file, name, exists: true };
}
+/**
+ * Return the commit immediately before the canonical doctrine file was renamed from its legacy
+ * path. A staged rename uses HEAD; a committed rename uses that rename commit's first parent.
+ * Reading both files at this revision prevents later coordinator edits from becoming grandfathered.
+ *
+ * @param {string} root
+ * @param {string} name
+ * @returns {string | null}
+ */
+export function legacyDoctrineRenameBase(root, name) {
+ if (name !== DOCTRINE_FILE) return null;
+ const options = {
+ cwd: root,
+ env: cleanGitEnvironment(),
+ encoding: 'utf8',
+ maxBuffer: 8 * 1024 * 1024,
+ stdio: 'pipe',
+ };
+ const changed = spawnSync('git', [
+ '--no-replace-objects', '-c', 'core.quotepath=false',
+ 'diff', '--name-status', '--find-renames', 'HEAD', '--', LEGACY_DOCTRINE_FILE, DOCTRINE_FILE,
+ ], options);
+ if (changed.status === 0 && /^R\d*\tPRACTICE\.md\tDOCTRINE\.md$/mu.test(changed.stdout)) {
+ const head = spawnSync('git', ['rev-parse', '--verify', 'HEAD'], options);
+ return head.status === 0 ? head.stdout.trim() : null;
+ }
+ const history = spawnSync('git', [
+ '--no-replace-objects', '-c', 'core.quotepath=false',
+ 'log', '--follow', '--name-status', '--find-renames', '--format=%H %P', '--', DOCTRINE_FILE,
+ ], options);
+ if (history.status !== 0) return null;
+ const rename = history.stdout.match(/^([0-9a-f]{40}) ([0-9a-f]{40})(?: [0-9a-f]{40})?\n\nR\d*\tPRACTICE\.md\tDOCTRINE\.md$/mu);
+ return rename?.[2] ?? null;
+}
+
/**
* Findings for one row on its own: status, tier, text, gate, fields.
*
diff --git a/test/doctrine-name.test.js b/test/doctrine-name.test.js
index a187b782..1bc6f71e 100644
--- a/test/doctrine-name.test.js
+++ b/test/doctrine-name.test.js
@@ -198,3 +198,75 @@ test('[D1,D2,A5] explicit custom practice paths remain authoritative beside DOCT
assert.equal(rows.some((row) => row.id === 'L1'), false, 'custom path remains the configured source');
assert.ok(projectState(repo.root).practice.some((row) => row.id === 'C1'));
});
+
+/** Make a committed pre-init rename fixture, optionally adding a later collision on main. */
+function renamedDoctrineFixture(box, name, { laterCollision = false, commitRename = true } = {}) {
+ const root = join(box.dir, name);
+ mkdirSync(root);
+ box.git(root, 'init', '-q', '-b', 'main');
+ const originalSpec = '# Requirements\n\n## G · Goals\n- G1 [draft, aim] Existing requirement.\n';
+ const originalPractice = '# Legacy rules\n\n## G · Goals\n- G1 [draft] Existing collision.\n- G2 [draft] Legacy id without a collision.\n';
+ writeFileSync(join(root, 'SPEC.md'), originalSpec);
+ writeFileSync(join(root, PRACTICE), originalPractice);
+ box.git(root, 'add', 'SPEC.md', PRACTICE);
+ box.git(root, 'commit', '-q', '-m', 'docs: seed legacy doctrine collision');
+ box.git(root, 'mv', PRACTICE, DOCTRINE);
+ if (commitRename) box.git(root, 'commit', '-q', '-m', 'docs: rename practice to doctrine');
+ if (laterCollision) {
+ const updatedSpec = `${originalSpec.trimEnd()}\n- G2 [draft, aim] Added after the doctrine rename.\n`;
+ writeFileSync(join(root, 'SPEC.md'), updatedSpec);
+ box.git(root, 'add', 'SPEC.md');
+ box.git(root, 'commit', '-q', '-m', 'docs: add post-rename collision');
+ }
+ return root;
+}
+
+test('[D4,A5] rename keeps known duplicates while a later collision remains an error', function renameKeepsKnownDuplicates() {
+ const box = sandbox();
+ const beforeCollision = renamedDoctrineFixture(box, 'legacy-before-collision');
+ const initialized = box.run(beforeCollision, 'init');
+ assert.equal(initialized.status, 0, `${initialized.stdout}${initialized.stderr}`);
+ const knownOnly = box.run(beforeCollision, 'spec', 'check');
+ assert.equal(knownOnly.status, 0, `${knownOnly.stdout}${knownOnly.stderr}`);
+ assert.match(knownOnly.stdout, /SPEC\.md:4 G1 warning: known duplicate id; also appears at DOCTRINE\.md:4/u);
+ assert.match(knownOnly.stdout, /DOCTRINE\.md:4 G1 warning: known duplicate id; also appears at SPEC\.md:4/u);
+
+ const afterCollision = renamedDoctrineFixture(box, 'legacy-after-collision', { laterCollision: true });
+ const laterInitialized = box.run(afterCollision, 'init');
+ assert.equal(laterInitialized.status, 0, `${laterInitialized.stdout}${laterInitialized.stderr}`);
+ const later = box.run(afterCollision, 'spec', 'check');
+ assert.equal(later.status, 1, later.stdout);
+ assert.match(later.stdout, /SPEC\.md:4 G1 warning: known duplicate id; also appears at DOCTRINE\.md:4/u);
+ assert.match(later.stdout, /DOCTRINE\.md:4 G1 warning: known duplicate id; also appears at SPEC\.md:4/u);
+ assert.match(later.stdout, /SPEC\.md:5 G2 error: duplicate id; also appears at DOCTRINE\.md:5/u);
+ assert.match(later.stdout, /DOCTRINE\.md:5 G2 error: duplicate id; also appears at SPEC\.md:5/u);
+});
+
+test('[D4,A5] canonical collisions stay errors without legacy rename history', function canonicalCollisionNeedsLegacyHistory() {
+ const box = sandbox();
+ const root = join(box.dir, 'never-legacy');
+ mkdirSync(root);
+ box.git(root, 'init', '-q', '-b', 'main');
+ writeFileSync(join(root, 'SPEC.md'), '# Requirements\n\n## G · Goals\n- G1 [draft, aim] Existing requirement.\n');
+ writeFileSync(join(root, DOCTRINE), '# Doctrine\n\n## G · Goals\n- G1 [draft] Existing collision.\n');
+ box.git(root, 'add', 'SPEC.md', DOCTRINE);
+ box.git(root, 'commit', '-q', '-m', 'docs: add canonical doctrine collision');
+ const initialized = box.run(root, 'init');
+ assert.equal(initialized.status, 0, `${initialized.stdout}${initialized.stderr}`);
+ const check = box.run(root, 'spec', 'check');
+ assert.equal(check.status, 1, check.stdout);
+ assert.match(check.stdout, /SPEC\.md:4 G1 error: duplicate id; also appears at DOCTRINE\.md:4/u);
+ assert.match(check.stdout, /DOCTRINE\.md:4 G1 error: duplicate id; also appears at SPEC\.md:4/u);
+});
+
+
+test('[D4,A5] a staged rename keeps known duplicates before its rename commit', function stagedRenameKeepsKnownDuplicates() {
+ const box = sandbox();
+ const root = renamedDoctrineFixture(box, 'staged-legacy-rename', { commitRename: false });
+ const initialized = box.run(root, 'init');
+ assert.equal(initialized.status, 0, `${initialized.stdout}${initialized.stderr}`);
+ const check = box.run(root, 'spec', 'check');
+ assert.equal(check.status, 0, `${check.stdout}${check.stderr}`);
+ assert.match(check.stdout, /SPEC\.md:4 G1 warning: known duplicate id; also appears at DOCTRINE\.md:4/u);
+ assert.match(check.stdout, /DOCTRINE\.md:4 G1 warning: known duplicate id; also appears at SPEC\.md:4/u);
+});