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.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
fix(core): refresh Git facts per request [C7]
Corey Olson146e584965a3
diff --git a/src/api-http.js b/src/api-http.jsindex fa4518d3..8027fd6a 100644--- a/src/api-http.js+++ b/src/api-http.js@@ -1,6 +1,7 @@/** API v1's shared HTTP boundary; local and relay adapters supply the same board operations (A2). */import { Refused } from './refused.js';import { refusalDocument } from './json.js';+import { withGitFacts } from './git.js';const BODY_BYTES = 100_000;@@ -70,27 +71,29 @@ export function createApiHandler(adapter, { pollMs = 200 } = {}) {}/** Deliver only records after the latest delivered sequence, with no overlapping polls. */async function send() {- if (closed || busy || blocked) return;- busy = true;- try {- who = await adapter.authenticate(req, { board: board.id, write: false });- const warning = await adapter.warning?.(board, who);- const serialized = warning ? JSON.stringify({ version: 1, warning }) : null;- if (serialized !== lastWarning) {- lastWarning = serialized;- if (serialized && !res.write('event: warning\ndata: ' + serialized + '\n\n')) { blocked = true; return; }- }- const records = initial ?? await adapter.events(board, after, who);- initial = null;- for (const event of records) {- if (closed) break;- after = event.event_id;- if (!res.write('id: ' + after + '\ndata: ' + JSON.stringify({ version: 1, event }) + '\n\n')) { blocked = true; break; }- }- } catch (error) {- if (!closed) res.write('event: error\ndata: ' + JSON.stringify(refusal(error)) + '\n\n');- stop();- } finally { busy = false; }+ return withGitFacts(async () => {+ if (closed || busy || blocked) return;+ busy = true;+ try {+ who = await adapter.authenticate(req, { board: board.id, write: false });+ const warning = await adapter.warning?.(board, who);+ const serialized = warning ? JSON.stringify({ version: 1, warning }) : null;+ if (serialized !== lastWarning) {+ lastWarning = serialized;+ if (serialized && !res.write('event: warning\ndata: ' + serialized + '\n\n')) { blocked = true; return; }+ }+ const records = initial ?? await adapter.events(board, after, who);+ initial = null;+ for (const event of records) {+ if (closed) break;+ after = event.event_id;+ if (!res.write('id: ' + after + '\ndata: ' + JSON.stringify({ version: 1, event }) + '\n\n')) { blocked = true; break; }+ }+ } catch (error) {+ if (!closed) res.write('event: error\ndata: ' + JSON.stringify(refusal(error)) + '\n\n');+ stop();+ } finally { busy = false; }+ });}res.once('close', stop);res.on('drain', () => { blocked = false; void send(); });@@ -101,6 +104,11 @@ export function createApiHandler(adapter, { pollMs = 200 } = {}) {/** Route a request through the adapter and keep every HTTP response inside API v1. */async function handle(req, res) {+ return withGitFacts(() => route(req, res));+ }++ /** Handle one request under a fresh Git-fact scope so a long-lived server observes later refs. */+ async function route(req, res) {try {let url;try { url = new URL(req.url ?? '/', 'http://127.0.0.1'); } catch { throw new Refused('BAD_REQUEST', 'use a valid path under /api/v1/boards'); }diff --git a/src/run.js b/src/run.jsindex cc7760be..8552fe11 100644--- a/src/run.js+++ b/src/run.js@@ -203,16 +203,20 @@ function commitHeader(item, rules, plain) {* @param {any} item* @param {any} config* @param {string[]} paths- * @returns {{ ok: boolean, output: string }}+ * @returns {{ ok: boolean, output: string, commit?: string }}*/-function commitWork(root, item, config, paths) {+export function commitWork(root, item, config, paths) {git(root, ['add', '-A', '--', ...paths]);+ const previousHead = headCommit(root);let output = '';for (const plain of [false, true]) {const result = spawnSync('git', ['commit', '-q', '-m', commitHeader(item, config.commits, plain)], { cwd: root, encoding: 'utf8' });invalidateGitFacts();if (result.status === 0) {- return { ok: true, output: '' };+ const commit = headCommit(root);+ return commit === previousHead+ ? { ok: false, output: 'git commit succeeded without advancing HEAD' }+ : { ok: true, output: '', commit };}output = `${result.stdout}${result.stderr}`;}diff --git a/test/git-calls.test.js b/test/git-calls.test.jsindex 13d2df70..966fea40 100644--- a/test/git-calls.test.js+++ b/test/git-calls.test.js@@ -5,9 +5,15 @@ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, wrimport { delimiter, join, resolve } from 'node:path';import { tmpdir } from 'node:os';import { test } from 'node:test';+import { main } from '../src/cli.js';import { headCommit, withGitFacts } from '../src/git.js';+import { serveApi } from '../src/api.js';+import { pathToFileURL } from 'node:url';const BIN = resolve(import.meta.dirname, '../bin/pullboard.js');+const RUN_MODULE = pathToFileURL(resolve(import.meta.dirname, '../src/run.js')).href;+const CONFIG_MODULE = pathToFileURL(resolve(import.meta.dirname, '../src/config.js')).href;+const GIT_MODULE = pathToFileURL(resolve(import.meta.dirname, '../src/git.js')).href;/** Find the real Git executable before the fixture adds its counting shim to PATH. */function realGitPath() {@@ -115,16 +121,72 @@ function deferred() {}/** Advance the fixture branch using real Git plumbing without running its checkout hooks. */-function advanceHead(box, label) {+function advanceHead(box, label, branch = 'main') {const parent = box.git(box.repo, 'rev-parse', 'HEAD');const tree = box.git(box.repo, 'rev-parse', 'HEAD^{tree}');const commit = execFileSync(box.env.PULLBOARD_REAL_GIT, ['commit-tree', tree, '-p', parent], {cwd: box.repo, env: box.env, input: label, encoding: 'utf8',}).trim();- box.git(box.repo, 'update-ref', 'refs/heads/main', commit);+ box.git(box.repo, 'update-ref', `refs/heads/${branch}`, commit);return commit;}+/** Read a code reference through the running local API using a SHA-shaped current branch name. */+async function readServedBranchCode(api, branch) {+ const address = new URL(api.url);+ const headers = { 'x-pullboard-key': address.searchParams.get('k') };+ const listing = await fetch(address, { headers });+ assert.equal(listing.status, 200);+ const board = (await listing.json()).boards[0];+ assert.ok(board?.id, 'the fixture board is listed');+ const path = `${address.pathname}/${encodeURIComponent(board.id)}/code?ref=${encodeURIComponent(`SPEC.md:1@${branch}`)}`;+ const response = await fetch(address.origin + path, { headers });+ const document = await response.json();+ assert.equal(response.status, 200, JSON.stringify(document));+ return document.code.commit;+}++test('a live serve request sees a commit made while the server stays open [C7]', async (t) => {+ const box = fixture(t);+ const branch = 'a1b2c3d4e5f6';+ box.git(box.repo, 'branch', '-m', branch);+ const original = box.git(box.repo, 'rev-parse', 'HEAD');+ await withGitFacts(async () => {+ assert.equal(headCommit(box.repo), original, 'the long-lived command has an initial cached HEAD');+ const api = await serveApi({ secret: 'git-facts-test-key', runCommand: main, projects: () => [{ root: box.repo, name: 'Git facts fixture' }] });+ try {+ assert.equal(await readServedBranchCode(api, branch), original);+ const advanced = advanceHead(box, 'advance the served branch', branch);+ assert.notEqual(advanced, original);+ assert.equal(await readServedBranchCode(api, branch), advanced, 'the next HTTP request resolves the branch after its commit');+ } finally {+ await api.close();+ }+ });+});++test('the runner invalidates cached HEAD immediately after its own commit [C7]', (t) => {+ const box = fixture(t);+ const before = box.git(box.worker, 'rev-parse', 'HEAD');+ mkdirSync(join(box.worker, 'src'));+ writeFileSync(join(box.worker, 'src', 'runner-result.txt'), 'built\n');+ const item = { item_id: 1, item_title: 'runner commit refreshes HEAD', item_lane: 'core', item_spec_ids: 'C7' };+ const script = `import { commitWork } from ${JSON.stringify(RUN_MODULE)};+import { loadConfig } from ${JSON.stringify(CONFIG_MODULE)};+import { withGitFacts } from ${JSON.stringify(GIT_MODULE)};+const root = process.cwd();+const item = JSON.parse(process.env.PULLBOARD_TEST_ITEM);+const result = await withGitFacts(() => commitWork(root, item, loadConfig(root), ['src/runner-result.txt']));+console.log(JSON.stringify(result));`;+ const env = { ...box.env, PULLBOARD_TEST_ITEM: JSON.stringify(item) };+ const result = spawnSync(process.execPath, ['--input-type=module', '-e', script], { cwd: box.worker, env, encoding: 'utf8' });+ assert.equal(result.status, 0, `${result.stdout}${result.stderr}`);+ const committed = JSON.parse(result.stdout.trim());+ assert.equal(committed.ok, true, committed.output);+ assert.notEqual(committed.commit, before, 'the runner reports the HEAD created by its commit');+ assert.equal(committed.commit, box.git(box.worker, 'rev-parse', 'HEAD'));+});+test('settled and concurrent commands do not share Git-fact caches [C7]', async (t) => {const box = fixture(t);const settledRead = deferred();