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(approval): cache stable row attribution on reads [S1,N26]

Corey Olson3d1ab9644bda
diff --git a/src/git.js b/src/git.js
index b09c3d83..de73fbb1 100644
--- a/src/git.js
+++ b/src/git.js
@@ -6,6 +6,7 @@ import {
spawnSync,
} from 'node:child_process';
import { AsyncLocalStorage } from 'node:async_hooks';
+import { createHash } from 'node:crypto';
import {
existsSync,
lstatSync,
@@ -19,6 +20,7 @@ import {
dirname,
join,
resolve,
+ sep,
} from 'node:path';
import { fileURLToPath } from 'node:url';
import { Refused } from './refused.js';
@@ -98,18 +100,20 @@ function fileSignature(path, includeContents = true) {
}
/** Refuse caching for config includes or any explicit ref backend selection this signature cannot track. */
-function configNeedsFallback(path) {
+function configNeedsFallback(path, { attribution = false } = {}) {
try {
const source = readFileSync(path, 'utf8');
return /^\s*\[\s*include(?:if\b|[\s\]])/imu.test(source)
- || /(?:^|\])\s*refstorage\s*=/imu.test(source);
+ || /(?:^|\])\s*refstorage\s*=/imu.test(source)
+ || (attribution && (/^\s*\[\s*(?:blame|mailmap|filter|diff)(?:\s|\])/imu.test(source)
+ || /(?:^|\])\s*attributesfile\s*=/imu.test(source)));
} catch (error) {
return error.code !== 'ENOENT' && error.code !== 'ENOTDIR';
}
}
/** Describe worktree discovery and ref files without invoking Git, so a warm read can validate facts. */
-function repositorySignature(cwd, info = null, includeHead = false) {
+function repositorySignature(cwd, info = null, includeHead = false, { attribution = false } = {}) {
const markers = [];
try { markers.push(['physical-cwd', realpathSync.native(cwd)]); }
catch (error) { markers.push(['physical-cwd', error.code ?? 'ERROR']); }
@@ -165,10 +169,10 @@ function repositorySignature(cwd, info = null, includeHead = false) {
const commonDir = info?.commonDir ?? discoveredCommonDir;
if (!gitDir || !commonDir) return includeHead ? null : JSON.stringify(markers);
const configPaths = [join(gitDir, 'config'), join(commonDir, 'config'), join(gitDir, 'config.worktree')];
- if (configPaths.some(configNeedsFallback)) return null;
+ if (configPaths.some(path => configNeedsFallback(path, { attribution }))) return null;
for (const path of [rootPath, gitDir, commonDir]) markers.push([path, fileSignature(path, false)]);
for (const path of [join(gitDir, 'commondir'), ...configPaths]) {
- markers.push([path, fileSignature(path, basename(path) === 'commondir')]);
+ markers.push([path, fileSignature(path, attribution || basename(path) === 'commondir')]);
}
if (includeHead) {
const headPath = join(gitDir, 'HEAD');
@@ -187,12 +191,20 @@ function repositorySignature(cwd, info = null, includeHead = false) {
}
}
}
+ if (attribution) {
+ const index = process.env.GIT_INDEX_FILE ? resolve(cwd, process.env.GIT_INDEX_FILE) : join(gitDir, 'index');
+ markers.push([index, fileSignature(index, false)]);
+ for (const path of [join(commonDir, 'shallow'), join(commonDir, 'info', 'grafts'), join(commonDir, 'info', 'attributes')]) {
+ markers.push([path, fileSignature(path)]);
+ }
+ }
return JSON.stringify(markers);
}
/** Describe relevant Git environment/config so harmless process settings can still share read facts. */
-function readCacheContext() {
+function readCacheContext({ attribution = false } = {}) {
const env = process.env;
+ if (attribution && (env.GIT_NAMESPACE || Object.keys(env).some(name => name.startsWith('GIT_ATTR_')))) return null;
const discoverySelectors = ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_COMMON_DIR', 'GIT_CEILING_DIRECTORIES',
'GIT_DISCOVERY_ACROSS_FILESYSTEM', 'GIT_IMPLICIT_WORK_TREE', 'GIT_OBJECT_DIRECTORY',
'GIT_ALTERNATE_OBJECT_DIRECTORIES', 'GIT_SHALLOW_FILE', 'GIT_GRAFT_FILE'];
@@ -224,12 +236,37 @@ function readCacheContext() {
if (env.GIT_CONFIG_SYSTEM) files.add(env.GIT_CONFIG_SYSTEM);
else if (process.platform !== 'win32') files.add('/etc/gitconfig');
}
+ if (attribution) {
+ const xdg = env.XDG_CONFIG_HOME || (env.HOME ? join(env.HOME, '.config') : null);
+ if (xdg) files.add(join(xdg, 'git', 'attributes'));
+ for (const path of ['/etc/gitattributes', '/usr/local/etc/gitattributes', '/opt/homebrew/etc/gitattributes']) files.add(path);
+ }
const paths = [...files].sort();
- if (paths.some(configNeedsFallback)) return null;
- const fileMarkers = paths.map((path) => [path, fileSignature(path, false)]);
+ if (paths.some(path => configNeedsFallback(path, { attribution }))) return null;
+ const fileMarkers = paths.map((path) => [path, fileSignature(path, attribution)]);
return JSON.stringify({ cwd: process.cwd(), values, home: env.HOME ?? null, xdg: env.XDG_CONFIG_HOME ?? null, files: fileMarkers });
}
+/** Describe a stable read input without Git processes; unsupported attribution settings disable reuse. */
+export function gitReadSignature(root, files, { attribution = false } = {}) {
+ const context = readCacheContext({ attribution });
+ const repository = repositorySignature(root, null, true, { attribution });
+ if (context === null || repository === null) return null;
+ const paths = new Set(files.map(file => join(root, file)));
+ if (attribution) {
+ for (const file of files) {
+ let folder = dirname(resolve(root, file));
+ while (folder === resolve(root) || folder.startsWith(resolve(root) + sep)) {
+ paths.add(join(folder, '.gitattributes'));
+ if (folder === resolve(root)) break;
+ folder = dirname(folder);
+ }
+ }
+ }
+ const inputs = [...paths].map(path => [path, fileSignature(path)]);
+ return createHash('sha256').update(JSON.stringify([context, repository, inputs])).digest('hex');
+}
+
/** Return a validated cached read fact, refreshing it when its filesystem signature changes. */
function readFact(key, signature, read) {
if (gitReadFacts.getStore() !== true) return read();
diff --git a/src/spec-approval.js b/src/spec-approval.js
index 13c0c1d9..c1b9d78d 100644
--- a/src/spec-approval.js
+++ b/src/spec-approval.js
@@ -2,7 +2,10 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import * as store from './board.js';
-import { tryGit } from './git.js';
+import {
+ gitReadSignature,
+ tryGit,
+} from './git.js';
import { personNeeds } from './needs.js';
import { Refused } from './refused.js';
import {
@@ -14,8 +17,20 @@ import {
verifyRowDecision,
} from './signature.js';
+const ORIGIN_CACHE_LIMIT = 32;
+const originCache = new Map();
+
/** Read each line's actual commit and Git author; neither identifies a person approval. */
function rowOrigins(root, file) {
+ const signature = () => gitReadSignature(root, [file, '.mailmap'], { attribution: true });
+ const before = signature();
+ const key = before === null ? null : JSON.stringify([root, file, before]);
+ if (key !== null && originCache.has(key)) {
+ const cached = originCache.get(key);
+ originCache.delete(key);
+ originCache.set(key, cached);
+ return cached;
+ }
const result = tryGit(root, ['blame', '--line-porcelain', '--', file]);
const origins = new Map();
let line;
@@ -27,6 +42,11 @@ function rowOrigins(root, file) {
else if (text.startsWith('author ')) author = text.slice(7);
else if (text.startsWith('\t') && line) origins.set(line, { commit, agent: author || 'unknown', attribution: 'Git author, seat unknown' });
}
+ // Only origins are reused: live board decisions and signature trust are checked on every projection.
+ if (key !== null && result.status === 0 && before === signature()) {
+ originCache.set(key, origins);
+ while (originCache.size > ORIGIN_CACHE_LIMIT) originCache.delete(originCache.keys().next().value);
+ }
return origins;
}
diff --git a/test/spec-approval.test.js b/test/spec-approval.test.js
index 0ac6bc3b..89676173 100644
--- a/test/spec-approval.test.js
+++ b/test/spec-approval.test.js
@@ -7,6 +7,7 @@ import { test } from 'node:test';
import { createE2eHelpers } from './e2e-helpers.js';
import { runFixtureChild } from './fixture-child.js';
import { configFromSource } from '../src/config.js';
+import { gitReadSignature } from '../src/git.js';
import * as store from '../src/board.js';
import { captureSpecApprovalBaseline, specApprovalState } from '../src/spec-approval.js';
import { SPEC_APPROVAL_ENGINE } from '../src/spec-approval-engine.js';
@@ -549,3 +550,65 @@ test('an authenticated first baseline cannot grandfather an absent future row [H
assert.ok(projected.rows.some(row => row.id === 'G99'),
'a later approved row absent from the captured source must remain agent-approved until a person confirms it');
});
+
+
+test('cached origins follow dirty lines and changed history while person decisions stay live [S1,B26,N26]', t => {
+ const helpers = createE2eHelpers();
+ t.after(helpers.cleanup);
+ const box = helpers.project('true');
+ const board = store.openBoard(join(box.repo, '.git', 'pullboard', 'board.sqlite'));
+ t.after(() => store.closeBoard(board));
+ const config = configFromSource(readFileSync(join(box.repo, 'pullboard.json'), 'utf8'));
+ const read = (root = box.repo) => specApprovalState(root, { board, config }).rows.find(row => row.id === 'G1');
+ const original = readFileSync(join(box.repo, 'SPEC.md'), 'utf8');
+ const first = read();
+ assert.equal(first.commit, box.git(box.repo, 'rev-parse', 'HEAD'));
+ assert.deepEqual(read(), first, 'a stable origin map preserves the full attribution');
+ assert.deepEqual(read(box.web), first, 'a linked worktree starts with the same historical attribution');
+ writeFileSync(join(box.repo, 'SPEC.md'), '\n' + original.replace('The page renders.', 'The changed page renders.'));
+ const dirty = read();
+ assert.equal(dirty.commit, null, 'dirty moved lines cannot reuse the old committed attribution');
+ assert.equal(dirty.text, 'The changed page renders.');
+ writeFileSync(join(box.repo, 'SPEC.md'), original);
+ box.git(box.repo, 'commit', '-q', '--amend', '--no-edit', '--author', 'Replacement Author <[email protected]>');
+ const rewritten = read();
+ assert.notEqual(rewritten.commit, first.commit, 'identical spec bytes under a new history require new origins');
+ assert.equal(rewritten.agent, 'Replacement Author');
+ assert.deepEqual(read(box.web), first, 'a linked worktree keeps its own unchanged revision after main moves');
+ assert.deepEqual(read(), rewritten);
+ document(box, box.repo, 'spec', 'approve', 'G1');
+ assert.equal(read(), undefined, 'a person decision is re-evaluated independently of cached Git origins');
+});
+
+
+test('attribution signatures bypass namespaced refs and external blame configuration [S1,N26,C7]', t => {
+ for (const [name, value] of [['GIT_CONFIG_GLOBAL', '/dev/null'], ['GIT_CONFIG_NOSYSTEM', '1']]) {
+ const previous = process.env[name];
+ process.env[name] = value;
+ t.after(() => {
+ if (previous === undefined) delete process.env[name];
+ else process.env[name] = previous;
+ });
+ }
+ const helpers = createE2eHelpers();
+ t.after(helpers.cleanup);
+ const box = helpers.project('true');
+ const signature = () => gitReadSignature(box.repo, ['SPEC.md', '.mailmap'], { attribution: true });
+ assert.match(signature(), /^[a-f0-9]{64}$/u);
+ const previousNamespace = process.env.GIT_NAMESPACE;
+ try {
+ process.env.GIT_NAMESPACE = 'private-attribution-fixture';
+ assert.equal(signature(), null, 'namespaced refs need a real Git read');
+ } finally {
+ if (previousNamespace === undefined) delete process.env.GIT_NAMESPACE;
+ else process.env.GIT_NAMESPACE = previousNamespace;
+ }
+ const beforeAttributes = signature();
+ writeFileSync(join(box.repo, '.gitattributes'), 'SPEC.md ident\n');
+ assert.notEqual(signature(), beforeAttributes, 'working-tree conversion attributes invalidate cached origins');
+ const beforeMailmap = signature();
+ writeFileSync(join(box.repo, '.mailmap'), 'Other Name <[email protected]> <[email protected]>\n');
+ assert.notEqual(signature(), beforeMailmap, 'mailmap contents belong to the attribution signature');
+ box.git(box.repo, 'config', 'blame.ignoreRevsFile', join(box.dir, 'external-ignore-list'));
+ assert.equal(signature(), null, 'external revision lists are unsupported rather than cached incompletely');
+});