Coordinate organization document writers

This commit is contained in:
jason.woltje committed 2026-11-07 14:47:27 -06:00
1 parent fa2a61a0e7
commit e342af7697
4 files changed
+91 -18

No files matched your search

+35 -7
View File
@@ -1,4 +1,4 @@
import { constants } from 'node:fs';
import { constants, openSync, closeSync, fstatSync, lstatSync, readFileSync, writeFileSync, renameSync, unlinkSync } from 'node:fs';
import * as fs from 'node:fs/promises';
import { dirname, isAbsolute, join, normalize, parse, resolve } from 'node:path';
import { randomBytes } from 'node:crypto';
@@ -24,15 +24,18 @@ async function readPrivate(file) {
try { const st = await fd.stat(); if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600) fail('org-mismatch', 'unsafe metadata file', file); return JSON.parse(await fd.readFile('utf8')); } finally { await fd.close(); }
}
async function privateDir(path) { const st = await fs.lstat(path); if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o077)) fail('org-mismatch', 'unsafe private directory', path); }
export async function readRegistry({ configDir }) {
await safeDir(configDir); await privateDir(configDir); const file = join(configDir, 'org-runtime.json'); let doc;
try { doc = await readPrivate(file); } catch (e) { if (e.code === 'ENOENT') return null; if (e instanceof OrgError) throw e; fail('org-mismatch', `invalid registry: ${e.message}`, file); }
function validateRegistry(doc, file) {
if (!doc || doc.version !== 1 || typeof doc.instance !== 'string' || !doc.orgs || typeof doc.orgs !== 'object' || Array.isArray(doc.orgs)) fail('org-mismatch', 'invalid registry', file);
for (const [id, entry] of Object.entries(doc.orgs)) { idCheck(id); if (!entry || entry.id !== id || typeof entry.root !== 'string' || !['reserved', 'active'].includes(entry.state) || !Number.isSafeInteger(entry.revision) || entry.revision < 1 || typeof entry.created !== 'string') fail('org-mismatch', 'invalid registry entry', file); }
return doc;
}
export async function readRegistry({ configDir }) {
await safeDir(configDir); await privateDir(configDir); const file = join(configDir, 'org-runtime.json'); let doc;
try { doc = await readPrivate(file); } catch (e) { if (e.code === 'ENOENT') return null; if (e instanceof OrgError) throw e; fail('org-mismatch', `invalid registry: ${e.message}`, file); }
return validateRegistry(doc, file);
}
export async function listOrgs(ctx) { const doc = await readRegistry(ctx); return doc ? Object.values(doc.orgs) : []; }
export async function withRegistry(ctx, fn) {
export async function withRegistry(ctx, fn, { write = true } = {}) {
await safeDir(ctx.configDir); await privateDir(ctx.configDir); const lock = join(ctx.configDir, 'org-runtime.lock'); let fd;
for (let n = 0; n < 200; n++) { try { fd = await fs.open(lock, 'wx', 0o600); break; } catch (e) { if (e.code !== 'EEXIST') throw e; await new Promise(r => setTimeout(r, 10)); } }
if (!fd) fail('org-locked', 'registry lock is held', lock);
@@ -40,11 +43,36 @@ export async function withRegistry(ctx, fn) {
const registry = await readRegistry(ctx) ?? { version: 1, instance: ctx.instance, orgs: {} };
if (registry.instance !== ctx.instance) fail('org-mismatch', 'registry instance mismatch');
const result = await fn(registry);
const file = join(ctx.configDir, 'org-runtime.json'); const temp = join(ctx.configDir, `.org-runtime-${randomBytes(12).toString('hex')}`);
try { await fs.writeFile(temp, JSON.stringify(registry, null, 2) + '\n', { mode: 0o600, flag: 'wx' }); await fs.rename(temp, file); } finally { await fs.rm(temp, { force: true }); }
if (write) {
const file = join(ctx.configDir, 'org-runtime.json'); const temp = join(ctx.configDir, `.org-runtime-${randomBytes(12).toString('hex')}`);
try { await fs.writeFile(temp, JSON.stringify(registry, null, 2) + '\n', { mode: 0o600, flag: 'wx' }); await fs.rename(temp, file); } finally { await fs.rm(temp, { force: true }); }
}
return result;
} finally { await fd.close(); await fs.unlink(lock); }
}
// The broker's existing organization writer is synchronous. It shares the
// registry lock with the async service and refuses contention rather than
// blocking the event loop. Legacy configs without a registry stay unchanged.
export function withRegistryWriter(configDir, fn) {
const file = join(configDir, 'org-runtime.json');
try { lstatSync(file); } catch (e) { if (e.code === 'ENOENT') return fn(null, () => {}); throw e; }
const lock = join(configDir, 'org-runtime.lock'); let lockFd;
try { lockFd = openSync(lock, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); }
catch (e) { if (e.code === 'EEXIST') fail('org-locked', 'registry lock is held', lock); throw e; }
try {
const dir = lstatSync(configDir);
if (!dir.isDirectory() || dir.isSymbolicLink() || dir.uid !== process.getuid() || (dir.mode & 0o077)) fail('org-mismatch', 'unsafe private directory', configDir);
const fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); let doc;
try { const st = fstatSync(fd); if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600) fail('org-mismatch', 'unsafe metadata file', file); doc = JSON.parse(readFileSync(fd, 'utf8')); } finally { closeSync(fd); }
const registry = validateRegistry(doc, file);
const publish = () => {
const temp = join(configDir, `.org-runtime-${randomBytes(12).toString('hex')}`);
try { writeFileSync(temp, JSON.stringify(registry, null, 2) + '\n', { mode: 0o600, flag: 'wx' }); renameSync(temp, file); }
finally { try { unlinkSync(temp); } catch (e) { if (e.code !== 'ENOENT') throw e; } }
};
return fn(registry, publish);
} finally { closeSync(lockFd); unlinkSync(lock); }
}
export async function reserveOrg(ctx, id) {
const root = orgRoot(ctx.dataRoot, id);
return withRegistry(ctx, async registry => {
+15 -9
View File
@@ -79,13 +79,14 @@ export async function validateOrg(doc) {
}
}
async function checked(doc) { const result = await validateOrg(doc); if (!result.ok) fail('org-invalid', result.errors[0].message, result.errors[0].path); }
async function readCurrent(ctx, id) {
async function readCurrentUnlocked(ctx, id) {
const found = await resolveOrg(ctx, id); if (found.state !== 'active') fail('org-unavailable', 'organization is not active');
const principal = JSON.parse(await privateRead(join(found.root, 'owner.json'))); if (!same(principal, ctx.principal)) fail('org-forbidden', 'organization owner required');
const entry = (await readRegistry(ctx)).orgs[id]; const raw = await privateRead(join(ctx.configDir, 'businesses', `${id}.json`));
if (!entry.digest || entry.digest !== digest(raw)) fail('org-mismatch', 'organization document and registry disagree');
const doc = JSON.parse(raw); await checked(doc); return { ...found, revision: entry.revision, doc, principal };
}
async function readCurrent(ctx, id) { return withRegistry(ctx, () => readCurrentUnlocked(ctx, id), { write: false }); }
export async function createOrg(ctx, id, input) {
authorize(ctx); checkId(id); if (input?.human !== ctx.principal.sub) fail('org-forbidden', 'document human must name the local owner', '$.human');
const doc = normalizeDoc(ctx, id, input); await checked(doc);
@@ -118,7 +119,7 @@ export async function notificationShow(ctx, id) {
}
export async function updateOrg(ctx, id, input, { ifRevision } = {}) {
authorize(ctx); return withRegistry(ctx, async registry => {
const current = await readCurrent(ctx, id); if (!Number.isSafeInteger(ifRevision) || ifRevision !== current.revision) fail('revision-conflict', 'organization revision changed');
const current = await readCurrentUnlocked(ctx, id); if (!Number.isSafeInteger(ifRevision) || ifRevision !== current.revision) fail('revision-conflict', 'organization revision changed');
const doc = normalizeDoc(ctx, id, input, current.doc); if (doc.human !== ctx.principal.sub) fail('org-forbidden', 'document human must name the local owner'); await checked(doc);
const raw = encode(doc), revision = current.revision + 1; await atomic(join(ctx.configDir, 'businesses', `${id}.json`), raw); registry.orgs[id].digest = digest(raw); registry.orgs[id].revision = revision;
return receipt(current.root, 'org.update', id, ctx.principal, { root: current.root, revision });
@@ -136,15 +137,20 @@ async function git(root, args) {
async function initGit(root) { await fs.mkdir(root, { mode: 0o700 }); await git(root, ['init', '--quiet', '--template=']); await git(root, ['commit', '--quiet', '--allow-empty', '-m', 'Genesis']); return git(root, ['rev-parse', 'HEAD']); }
async function setup(ctx, id, project, opts) {
authorize(ctx); const selection = options(opts); if (project !== null) checkId(project);
return withRegistry(ctx, async registry => {
const current = await readCurrent(ctx, id); if (project !== null && !Object.hasOwn(current.doc.projects, project)) fail('org-unknown', 'unknown project');
const current = await withRegistry(ctx, async () => {
const current = await readCurrentUnlocked(ctx, id); if (project !== null && !Object.hasOwn(current.doc.projects, project)) fail('org-unknown', 'unknown project');
const op = project === null ? 'org.queue.init' : 'org.project.repo.init';
if ((await receipts(current.root)).some(r => r.op === op && (project === null || r.project === project))) fail('org-exists', 'repository was already initialized; inspect its receipt');
const parent = project === null ? current.root : join(current.root, 'projects'); if (project !== null) await ensurePrivateDir(parent);
const root = project === null ? join(current.root, 'checkout') : projectPath(current.root, project);
const commit = await initGit(root); const fields = { ...selection, root, commit, ...(project === null ? {} : { project }) };
if (project !== null) { current.doc.projects[project].git = selection.mode === 'remote' ? { mode: 'remote', remote: selection.remote } : { mode: 'local-only' }; await checked(current.doc); const raw = encode(current.doc); await atomic(join(ctx.configDir, 'businesses', `${id}.json`), raw); registry.orgs[id].digest = digest(raw); registry.orgs[id].revision++; }
return receipt(current.root, op, id, ctx.principal, fields);
return current;
}, { write: false });
const parent = project === null ? current.root : join(current.root, 'projects'); if (project !== null) await ensurePrivateDir(parent);
const root = project === null ? join(current.root, 'checkout') : projectPath(current.root, project);
const commit = await initGit(root); const fields = { ...selection, root, commit, ...(project === null ? {} : { project }) };
return withRegistry(ctx, async registry => {
const latest = await readCurrentUnlocked(ctx, id);
if (latest.revision !== current.revision) fail('revision-conflict', 'organization changed during setup; inspect the initialized repository before retrying');
if (project !== null) { latest.doc.projects[project].git = selection.mode === 'remote' ? { mode: 'remote', remote: selection.remote } : { mode: 'local-only' }; await checked(latest.doc); const raw = encode(latest.doc); await atomic(join(ctx.configDir, 'businesses', `${id}.json`), raw); registry.orgs[id].digest = digest(raw); registry.orgs[id].revision++; }
return receipt(current.root, project === null ? 'org.queue.init' : 'org.project.repo.init', id, ctx.principal, fields);
});
}
export async function queueInit(ctx, id, opts = {}) { return setup(ctx, id, null, opts); }
+8 -2
View File
@@ -12,6 +12,7 @@ import { isDailyModelQuota } from "../../plugins/src/model-quota.mjs";
import { checkReplyLimits } from "../../library/src/limits.mjs";
import { ADDITION_VERB, additionProblem } from "./project-bootstrap.mjs";
import { remoteProblem, remoteTarget } from "./project-remotes.mjs";
import { withRegistryWriter } from "./org-registry.mjs";
export class OrganizationError extends Error {
constructor(code, message = code) {
@@ -263,11 +264,15 @@ export function organizationWriter({ dir, rolesDir, barrier = () => {} }) {
requireId(id, "organization id");
let revoked = false;
try {
return withLock(id, ({ home, parent }) => {
return withRegistryWriter(dir, (registry, publishRegistry) => withLock(id, ({ home, parent }) => {
recover(id);
const file = businessFilePath(id, dir);
const oldBytes = regular(file);
const before = sha256(oldBytes);
const entry = registry?.orgs?.[id];
if (entry && (entry.state !== 'active' || entry.digest !== before || !Number.isSafeInteger(entry.revision + 1))) {
fail('org-mismatch', 'organization document and registry disagree');
}
if (revision !== before) fail("revision-conflict", "organization revision changed; read again");
const old = JSON.parse(oldBytes);
if (typeof document === "function") document = document(old);
@@ -312,10 +317,11 @@ export function organizationWriter({ dir, rolesDir, barrier = () => {} }) {
}
renameSync(candidate, file);
syncDir(parent);
if (entry) { entry.digest = after; entry.revision++; publishRegistry(); }
barrier("renamed");
log(id, { ...event, state: "committed" });
return { revision: after, transaction, previous };
});
}));
} catch (e) {
if (revoked && e instanceof Error) e.revoked = true;
throw e;
@@ -10,6 +10,9 @@ import { tmpdir } from 'node:os';
import { createOrg, showOrg, updateOrg, queueInit, queueShow, projectRepoInit, projectRepoShow, notificationShow } from '../src/org-service.mjs';
import { loadBusiness } from '../src/business.mjs';
import { REPO_ROLES } from './helpers.mjs';
import { organizationWriter } from '../src/organization.mjs';
import { watch } from 'node:fs';
import { writeFile } from 'node:fs/promises';
function context() { const root = mkdtempSync(join(tmpdir(), 'org-service-')); const configDir = join(root, 'config'), dataRoot = join(root, 'data'); mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 }); return { configDir, dataRoot, instance: 'test', principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } }; }
test('fixture template includes the runnable local model route', async () => {
const doc = await templateRender('alpha', { fixture: 'local' });
@@ -37,3 +40,33 @@ test('invalid paths and unrecognized principals refuse', async () => { const c =
test('validation identifies an arbitrary project root', async () => { const doc = await templateRender('alpha'); doc.projects.work.root = '/etc'; const result = await validateOrg(doc); assert.equal(result.ok, false); assert.equal(result.errors[0].path, '$.projects.work.root'); });
test('remote setup records the URL and never pushes', async () => { const c = context(); await createOrg(c, 'alpha', await templateRender('alpha')); const remote = 'https://example.invalid/team/repo'; const q = await queueInit(c, 'alpha', { mode: 'remote', remote }); assert.equal((await queueShow(c, 'alpha')).remote, remote); assert.equal(q.mode, 'remote'); const p = await projectRepoInit(c, 'alpha', 'work', { mode: 'remote', remote }); assert.equal((await projectRepoShow(c, 'alpha', 'work')).remote, remote); assert.equal(p.mode, 'remote'); });
test('a prior receipt fences reinitialization even when the repository disappears', async () => { const c = context(); await createOrg(c, 'alpha', await templateRender('alpha')); const created = await queueInit(c, 'alpha', { mode: 'local-only', acceptLocalLossRisk: true }); const { rmSync } = await import('node:fs'); rmSync(created.root, { recursive: true }); await assert.rejects(queueInit(c, 'alpha', { mode: 'local-only', acceptLocalLossRisk: true })); });
test('broker organization writer updates the registry revision and remains readable', async () => {
const c = context(); await createOrg(c, 'alpha', await templateRender('alpha', { fixture: 'local' }));
const before = await showOrg(c, 'alpha');
const writer = organizationWriter({ dir: c.configDir, rolesDir: REPO_ROLES });
const { document, revision } = writer.read('alpha'); document.plugins.channel = ['cli'];
assert.ok(writer.write('alpha', document, { revision, actor: c.principal.sub }).transaction);
const after = await showOrg(c, 'alpha');
assert.deepEqual(after.doc.plugins.channel, ['cli']); assert.equal(after.revision, before.revision + 1);
});
test('setup never overwrites a broker write committed while git initializes', async () => {
const c = context(); await createOrg(c, 'alpha', await templateRender('alpha', { fixture: 'local' }));
const writer = organizationWriter({ dir: c.configDir, rolesDir: REPO_ROLES });
const dir = join(c.dataRoot, 'git-wrapper'); mkdirSync(dir, { mode: 0o700 });
const ready = join(dir, 'ready'), release = join(dir, 'release');
const wrapper = `#!${process.execPath}\nimport {writeFileSync,existsSync,watch} from 'node:fs';\nimport {spawnSync} from 'node:child_process';\nif(process.argv.slice(2).includes('init')){writeFileSync(${JSON.stringify(ready)},'ready');if(!existsSync(${JSON.stringify(release)}))await new Promise(resolve=>{const w=watch(${JSON.stringify(dir)},()=>{if(existsSync(${JSON.stringify(release)})){w.close();resolve()}});if(existsSync(${JSON.stringify(release)})){w.close();resolve()}})}\nconst p=spawnSync('/usr/bin/git',process.argv.slice(2),{stdio:'inherit'});process.exit(p.status??1);\n`;
writeFileSync(join(dir, 'git'), wrapper, { mode: 0o700 });
const oldPath = process.env.PATH; process.env.PATH = `${dir}:${oldPath}`;
let watcher; const waiting = new Promise(resolve => { watcher = watch(dir, () => { try { readFileSync(ready); watcher.close(); resolve(); } catch {} }); });
const pending = projectRepoInit(c, 'alpha', 'work', { mode: 'local-only', acceptLocalLossRisk: true });
try {
await Promise.race([waiting, new Promise((_, reject) => setTimeout(() => reject(Error('git init timeout')), 10000).unref())]);
const { document, revision } = writer.read('alpha'); document.plugins.channel = ['cli'];
assert.ok(writer.write('alpha', document, { revision, actor: c.principal.sub }).transaction);
await writeFile(release, 'release');
await pending.catch(e => { if (e.code !== 'revision-conflict') throw e; });
assert.deepEqual((await showOrg(c, 'alpha')).doc.plugins.channel, ['cli']);
} finally { process.env.PATH = oldPath; watcher?.close(); await writeFile(release, 'release'); await pending.catch(() => {}); }
});