fix(bus): confine the working repo, serialize freshness with protected writes, fence readiness on publication (#802)

This commit is contained in:
jason.woltje committed 2026-11-04 10:38:54 -06:00
1 parent 1031686140
commit 7f5bbbf747
9 files changed
+414 -71

No files matched your search

+76 -32
View File
@@ -6,25 +6,28 @@
//
// reserve re-verify the plan against the world (#800), then reserve the product paths and a marker
// seed clone the source commit into a claimed temporary checkout, drop its remote, commit the one project-file change
// working seed the working repository from that commit
// working confine the working path, then seed the working repository from that commit
// mapping publish the one organization mapping through the private exact-add writer
// records initialize the records repository under that mapping
// intent save the original displayed intent as the owner's edit of spec/north-star.md
// chat associate the chat project
// activate admit the project to the running broker, then mark the product complete
// activate mark the product complete, then admit the project to the running broker
//
// Every inspection compares deterministic paths, the stored commits and exact bytes. What it cannot prove
// is kept as it is and reported as `bootstrap-uncertain`; nothing unrelated is removed and no commit is rewound.
// Nothing is ever deleted recursively, and every write goes through an exclusively created file and a rename,
// inside directories that are verified not to be links, so a link in a source checkout cannot redirect a write.
// Before every effect the organization revision and the chat association are checked again (#800).
// Before every effect the organization revision and the chat association are checked again (#800), and the check
// and the start of the effect are one synchronous run under the organization lock (`guarded`). A project is ready
// only with both a complete marker and a committed activate receipt, so the marker goes first and nothing is
// admitted until it is published.
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
import { closeSync, constants, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync, writeSync } from 'node:fs';
import { dirname, join, sep } from 'node:path';
import { dirname, join, relative, sep } from 'node:path';
import { digest, refuse, validatePlan } from './request-store.mjs';
import { gitEnv, runGit } from '../../seat/src/checkout.mjs';
import { initializeWorking } from '../../seat/src/working.mjs';
import { initializeWorking, workingRepo } from '../../seat/src/working.mjs';
import { initProject, writeHumanFile } from '../../project/src/index.mjs';
import { SAFE, identity } from '../../project/src/git.mjs';
import { loadProject } from '../../business/src/project.mjs';
@@ -139,16 +142,23 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
const publish = (file, temp, bytes) => stage(file, temp, bytes)();
// #800 before every effect, not only the first: the organization is still at the revision this plan expects (the
// confirmed one before the mapping, this plan's own post-add one after it), the mapping is the plan's, and the chat
// association is not another's. Synchronous and under the organization lock, so no write lands between the check
// and the effect after it. A mismatch keeps what is done and performs nothing further.
const fresh = () => writer.hold(business, ({ revision, document }) => {
// association is not another's. `fresh` runs under the organization lock, so a bare `fresh()` proves the world
// matched at that instant and nothing more: the lock is released when it returns. What keeps a write from landing
// under a stale plan is `guarded(effect)`: the caller has finished every asynchronous read the effect depends on,
// and the check and the start of the effect run in one synchronous run with the lock held, so no organization
// write lands between them. An effect that is a git child is started inside the lock; the child's own running
// time is not covered, and the next step's check catches a change made meanwhile. A mismatch keeps what is done
// and performs nothing further. `effect` must be synchronous and must not take the organization lock itself.
const fresh = effect => writer.hold(business, ({ revision, document }) => {
const stale = () => refuse('request-plan-stale');
if (document.id !== business || document.human !== owner) stale();
const added = writer.findAddition(business, { request, plan_digest: planDigest, project });
if (added) { if (revision !== added.after || canonicalJson(document.projects?.[project]) !== canonicalJson(entry)) stale(); }
else if (revision !== plan.organization_revision || Object.hasOwn(document.projects ?? {}, project)) stale();
if (services.chat.state({ business, project, name: plan.name, id: chatId }) === 'conflict') stale();
effect?.();
});
const guarded = effect => { let started; fresh(() => { started = effect(); }); return started; };
const git = (cwd, args, extra = {}, file = false) => runGit([...SAFE, ...(file ? ['-c', 'protocol.file.allow=always'] : []), ...(cwd ? ['-C', cwd] : []), ...args],
{ ...gitEnv(), ...(cwd ? { GIT_CEILING_DIRECTORIES: dirname(cwd) } : {}), ...extra });
@@ -160,6 +170,7 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
const op = base(name), last = journal.effects(business, request).filter(r => r.op === op).at(-1);
if (last?.stage === 'committed') return last.result;
if (last?.stage === 'refused') refuse(last.code ?? 'bootstrap-refused');
// An early refusal, before anything is recorded. The effects below each check again through `guarded`.
if (name !== 'reserve') fresh();
const header = { version: 1, business, request, op, kind: 'bootstrap', digest: planDigest };
journal.prepareEffect({ ...header, stage: 'prepared', result: null, code: null });
@@ -226,12 +237,11 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
// Cloned, or seeded but not yet committed. The only change allowed is the exact project file.
const onlyFile = status === '' || (/^( M|M ) \.mosaic\/project\.json$/.test(status) && readFileSync(seedFile, 'utf8') === seedBytes);
if (!onlyFile) uncertain();
fresh();
if (remotes === 'origin') await out(code, ['remote', 'remove', 'origin']);
if (status === '') publish(seedFile, seedTemp, seedBytes);
await out(code, ['add', '--', PROJECT_FILE]);
fresh();
await out(code, ['commit', '-q', '-m', `Bootstrap ${project} from ${plan.source.project}`], identity(human()));
const who = identity(human());
if (remotes === 'origin') await guarded(() => out(code, ['remote', 'remove', 'origin']));
if (status === '') guarded(() => publish(seedFile, seedTemp, seedBytes));
await guarded(() => out(code, ['add', '--', PROJECT_FILE]));
await guarded(() => out(code, ['commit', '-q', '-m', `Bootstrap ${project} from ${plan.source.project}`], who));
return { commit: await out(code, ['rev-parse', 'HEAD']), base: plan.source.commit };
}
const names = await tryOut(code, ['diff', '--name-status', `${head}~1`, head]);
@@ -256,12 +266,14 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
if (held && (!claim || held.isSymbolicLink() || !held.isDirectory())) uncertain();
if (!held) {
try {
if (!claim) writeFileSync(claimFile, claimBytes, { flag: 'wx', mode: 0o600 });
mkdirSync(tmp, { mode: 0o700 });
guarded(() => {
if (!claim) writeFileSync(claimFile, claimBytes, { flag: 'wx', mode: 0o600 });
mkdirSync(tmp, { mode: 0o700 });
});
} catch (e) { if (e.code === 'EEXIST') uncertain(); throw e; }
}
if (!held || readdirSync(tmp).length === 0) {
await out(null, ['clone', '-q', '--no-hardlinks', '--template=', '--', source.root, tmp], {}, true);
await guarded(() => out(null, ['clone', '-q', '--no-hardlinks', '--template=', '--', source.root, tmp], {}, true));
}
await services.barrier?.('clone');
// A clean clone of exactly the source commit, with at most the remote the clone itself added.
@@ -270,10 +282,11 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
if (head === null || remotes === null || status !== '' || !['', 'origin'].includes(remotes)) uncertain();
if (head !== plan.source.commit) refuse('request-plan-stale');
if (metadataProblem(tmp)) refuse('bootstrap-metadata-refused');
if (remotes === 'origin') await out(tmp, ['remote', 'remove', 'origin']);
fresh();
if (exists(here.root) || !confined(product)) uncertain();
renameSync(tmp, here.root);
if (remotes === 'origin') await guarded(() => out(tmp, ['remote', 'remove', 'origin']));
guarded(() => {
if (exists(here.root) || !confined(product)) uncertain();
renameSync(tmp, here.root);
});
await services.barrier?.('cloned');
}
const seed = async ({ recovering }) => {
@@ -282,13 +295,37 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
if (!code.isDirectory() || code.isSymbolicLink() || !markerOwns()) uncertain();
} else await clone(recovering);
const seededAt = await seeded();
dropClaim();
guarded(dropClaim);
return seededAt;
};
// 3. working. The working repository's main follows the checkout's HEAD, which is the seed commit.
const working = async () => {
const seedCommit = done('seed').commit, w = await initializeWorking({ dataRoot, business, project, root: here.root });
// 3. working. The working repository's main follows the checkout's HEAD, which is the seed commit. Nothing is made
// while a directory between the data root and the repository is a link or a file (N1: `working` linked to a
// sibling directory would take the repository and every file in it out of the data root). A repository that is
// already there is this plan's only on a retry, and only if it is a bare repository holding no ref but root and
// main, both at the seed commit; on a first attempt it is someone else's. Either way it is kept as it is.
const workingPath = workingRepo(dataRoot, business, project);
const workingExists = () => {
let at = dataRoot;
for (const name of relative(dataRoot, workingPath).split(sep)) {
at = join(at, name);
const st = exists(at);
if (!st) return false;
if (st.isSymbolicLink() || !st.isDirectory()) uncertain();
}
return true;
};
const working = async ({ recovering }) => {
const seedCommit = done('seed').commit;
if (workingExists()) {
if (!recovering) uncertain();
const at = [`--git-dir=${workingPath}`];
const refs = await tryOut(null, [...at, 'for-each-ref', '--format=%(refname) %(objectname)']);
const bare = await tryOut(null, [...at, 'rev-parse', '--is-bare-repository']);
const expected = new Set(['refs/heads/root', 'refs/heads/main'].map(ref => `${ref} ${seedCommit}`));
if (bare !== 'true' || refs === null || refs.split('\n').filter(Boolean).some(line => !expected.has(line))) uncertain();
}
const w = await initializeWorking({ dataRoot, business, project, root: here.root, around: guarded });
if (w.commit !== seedCommit) uncertain();
return { commit: w.commit };
};
@@ -312,7 +349,11 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
const records = async () => {
const repo = here.repo, st = exists(repo);
if (!st || !st.isDirectory() || st.isSymbolicLink()) uncertain();
if (readdirSync(repo).length === 0) { const made = await initProject(context(), { author: human() }); return { commit: made.commit }; }
if (readdirSync(repo).length === 0) {
const where = context(), author = human();
const made = await guarded(() => initProject(where, { author }));
return { commit: made.commit };
}
const count = await tryOut(repo, ['rev-list', '--count', 'HEAD']);
const head = await tryOut(repo, ['log', '-1', '--format=%H|%an|%s', 'HEAD']);
if (count !== '1' || head === null) uncertain();
@@ -326,7 +367,8 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
const repo = here.repo, init = done('records').commit;
const count = await tryOut(repo, ['rev-list', '--count', 'HEAD']), head = await tryOut(repo, ['rev-parse', 'HEAD']);
if (count === '1' && head === init) {
const saved = await writeHumanFile({ path: INTENT_PATH, content: plan.intent.text, base: init, ...context() });
// Every read above and inside the writer is finished before `around` checks and publishes the file.
const saved = await writeHumanFile({ path: INTENT_PATH, content: plan.intent.text, base: init, around: guarded, ...context() });
return { commit: saved.commit };
}
const line = await tryOut(repo, ['log', '-1', '--format=%P|%an|%s', 'HEAD']);
@@ -338,7 +380,8 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
return { commit: head };
};
// 7. chat. The association is one row; a row that is not this plan's is a conflict, never replaced.
// 7. chat. The association is one row; a row that is not this plan's is a conflict, never replaced. No await sits
// between the step's own check and the association.
const chat = () => {
const args = { business, project, name: plan.name, id: chatId }, state = services.chat.state(args);
if (state === 'conflict') uncertain();
@@ -346,15 +389,16 @@ export async function executeBootstrap({ binding, owner, journal, services }) {
return { chat_project: chatId };
};
// 8. activate. Synchronous from the activation to the receipt, so the broker and the marker agree.
// 8. activate. The complete marker is published first and the broker admits the project second, so a project is
// never admitted without it: a failed marker write ends the step before the broker is touched. A complete marker
// with no committed receipt admits nothing, since readiness at runtime and at startup needs both, and a revision
// that moved before the admission leaves the marker as it is and the project pending.
const activate = async () => {
await services.barrier?.('activate');
const mapped = done('mapping');
// The marker is staged before the broker admits anything, so a refusal leaves no half-admitted product.
const place = stage(marker, `${marker}.tmp`, markerBytes({ state: 'complete', request, plan_digest: planDigest, project }));
guarded(() => publish(marker, `${marker}.tmp`, markerBytes({ state: 'complete', request, plan_digest: planDigest, project })));
try { services.activate({ business, project, receipt, organization_revision: mapped.organization_revision }); }
catch (e) { if (e?.code === 'bootstrap-revision-stale') refuse('request-plan-stale'); throw e; }
place();
return { project, organization_revision: mapped.organization_revision };
};
+25 -10
View File
@@ -283,15 +283,20 @@ export async function startBroker({
let store, credentials, server, adapter, processing, syncTimer = null, syncPass = null, accessTimer = null;
try {
// Lifecycle Task 2 (#797): a product whose mapping is published but not yet activated is not part of
// this process. The request journal says which are activated; every other managed entry waits for the
// bootstrap executor's activateManagedProject, so the broker, the Credentials roots and the boot copy
// never see a half-built product. `businesses` becomes this process's own copy.
// this process. A managed product is part of it only with both a committed activate receipt in the
// request journal and a complete marker on disk (#802: a receipt alone, with the marker gone or never
// published, is not enough); every other managed entry waits for the bootstrap executor's
// activateManagedProject, so the broker, the Credentials roots and the boot copy never see a
// half-built product. `businesses` becomes this process's own copy.
store = new Store(dataRoot);
const requestJournal = createRequestStore(store);
const activatedProjects = business => new Set(requestJournal.committed(business, 'bootstrap')
.filter(r => r.op.endsWith(':activate') && typeof r.result?.project === 'string').map(r => r.result.project));
const pendingProject = (business, project, entry, activated) => entry?.kind !== 'library' && !activated.has(project)
&& managedState({ dataRoot, business, project, entry }) !== 'unmanaged';
const pendingProject = (business, project, entry, activated) => {
if (entry?.kind === 'library') return false;
const state = managedState({ dataRoot, business, project, entry });
return state !== 'unmanaged' && !(state === 'complete' && activated.has(project));
};
const withoutPending = (business, b, admit = []) => {
const activated = new Set([...activatedProjects(business), ...admit]);
const projects = Object.fromEntries(Object.entries(b.projects ?? {}).filter(([project, entry]) => !pendingProject(business, project, entry, activated)));
@@ -514,9 +519,11 @@ export async function startBroker({
}
}
// Lifecycle Task 2 (#797): the only way a project joins a running bus. The bootstrap executor calls it
// synchronously after every earlier step passed. It admits exactly the receipt-backed addition under the
// organization lock: the file must still be this business plus that entry, with the credentials, bots,
// libraries, roles and prior mappings the bus booted with. Anything else leaves the product pending.
// synchronously after every earlier step passed and the complete marker is published. It admits exactly the
// receipt-backed addition under the organization lock: the file must still be this business plus that entry,
// with the credentials, bots, libraries, roles and prior mappings the bus booted with. Anything else leaves
// the product pending. The executor commits the activate receipt right after this returns; admission() and a
// restart treat the product as ready only once that receipt exists.
const writer = organization ? organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir }) : null;
const known = new Map(Object.entries(businesses).map(([id, b]) => [id, canonicalJson(b.projects ?? {})]));
function activateManagedProject({ business: id, project, receipt, organization_revision }) {
@@ -536,6 +543,7 @@ export async function startBroker({
const here = managedPaths({ dataRoot, business: id, project });
if (!entry || entry.kind === 'library' || entry.root !== here.root || entry.repo !== here.repo
|| Object.keys(entry).some(k => !['root', 'repo', 'remote'].includes(k))) refuse('bootstrap-mapping-mismatch');
if (managedState({ dataRoot, business: id, project, entry }) !== 'complete') refuse('bootstrap-marker-incomplete');
const next = withoutPending(id, full, [project]);
const { [project]: _added, ...before } = next.projects;
if (canonicalJson(before) !== known.get(id)) refuse('bootstrap-mapping-mismatch');
@@ -549,7 +557,10 @@ export async function startBroker({
});
}
// Host IPC request.admission: may a project's code checkout be cloned and its tools used. A project the
// broker does not hold is never ready, and a declared one that is not yet held says it is pending.
// broker does not hold is never ready, and a declared one that is not yet held says it is pending. A managed
// product the broker holds is ready only while it has a complete marker and a committed activate receipt: the
// answer is read from both on every call, so a marker removed after the activation closes it again, and the
// broker's admission, which comes before the receipt commits, is not yet readiness (#802).
function admission({ business: id, project }) {
const no = code => ({ ready: false, revision: null, code });
if (typeof id !== 'string' || typeof project !== 'string' || !Object.hasOwn(businesses, id)) return no('project-unknown');
@@ -560,7 +571,11 @@ export async function startBroker({
revision = now.revision; declared = Object.hasOwn(now.document.projects ?? {}, project);
}
} catch { return no('organization-unreadable'); }
if (Object.hasOwn(businesses[id].projects ?? {}, project)) return { ready: true, revision, code: null };
if (Object.hasOwn(businesses[id].projects ?? {}, project)) {
try { if (pendingProject(id, project, businesses[id].projects[project], activatedProjects(id))) return no('project-pending'); }
catch { return no('project-pending'); }
return { ready: true, revision, code: null };
}
return no(declared ? 'project-pending' : 'project-unknown');
}
broker = new Broker({