From 3456d93af07402a67c504a50692717918f3ed99a Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Sat, 7 Nov 2026 11:17:31 -0600 Subject: [PATCH] Implement organization setup commands --- packages/business/README.md | 33 ++++ packages/business/src/business.mjs | 12 +- packages/business/src/org-registry.mjs | 10 +- packages/business/src/org-service.mjs | 147 ++++++++++++++++-- packages/business/tests/org-registry.test.mjs | 7 +- packages/business/tests/org-service.test.mjs | 14 ++ packages/cli/README.md | 19 +++ packages/cli/src/cli.mjs | 10 ++ packages/cli/src/org.mjs | 31 ++++ packages/cli/tests/org.test.mjs | 25 +++ scripts/mosaic | 2 +- 11 files changed, 284 insertions(+), 26 deletions(-) create mode 100644 packages/cli/src/org.mjs create mode 100644 packages/cli/tests/org.test.mjs diff --git a/packages/business/README.md b/packages/business/README.md index f4f9c6687..9838285fd 100644 --- a/packages/business/README.md +++ b/packages/business/README.md @@ -522,3 +522,36 @@ A refusal writes nothing and logs nothing. The answer is the read with the change: the running broker keeps the roles it loaded at start. The writer doesn't restart anything. The CLI (`mosaic business instance add|set`) and the Console Settings page send the patch. + +### Organizations (development release) + +`org-runtime.json` beside the instance config is private runtime metadata. It +reserves each organization ID and its derived `/org-runtime/` +root, which carries an instance-and-ID marker. It is not a system config. +The organization service writes the existing `businesses/.json` format so +`loadBusiness` can read it. Organization updates require the current revision; +a conflicting revision refuses. A document or marker that disagrees with the +registry refuses rather than falling back to another organization. + +Use `mosaic org template render ID --fixture local --output FILE`, edit the +non-secret JSON if needed, then `mosaic org validate FILE` and +`mosaic org create ID --from FILE`. Show and list use `mosaic org show ID +[--json]` and `mosaic org list [--json]`; update with `mosaic org update ID +--from FILE --if-revision N`. The development template identifies the local +owner, references reviewed roles, and declares the `work` project. Its path is +replaced with a root derived from the selected instance and organization. + +Initialize an organization checkout with `mosaic org queue init ID --local-only +--accept-local-loss-risk` and a project repository with `mosaic org project repo +init ID PROJECT --local-only --accept-local-loss-risk`. The explicit acceptance +records that this machine may hold the only copy. Alternatively use `--remote +https://HOST/OWNER/REPO` in place of both local-only flags. Both modes create a +local repository and genesis commit; remote mode records the URL without a +push. Existing paths are never overwritten. `mosaic org queue show ID --json` +and `mosaic org project repo show ID PROJECT --json` read the receipts back. +Queue review transports and transitions are not provided by this setup. + +The local development owner is accepted only through the existing named-instance +selection on this release's throwaway development instances. Receipt subjects +name that principal, not Jason. The dev-purpose bootstrap fence is a separate +follow-up before person-used deployment. diff --git a/packages/business/src/business.mjs b/packages/business/src/business.mjs index ee03a5687..084bcf07e 100644 --- a/packages/business/src/business.mjs +++ b/packages/business/src/business.mjs @@ -276,7 +276,7 @@ export function validateBusinessDocument(document, file, { rolesDir }) { projects[id] = checkMapping(entry, `${file} projects.${id}`, { fail: (code, message) => refuse(`${code}: ${message}`) }); continue; } - rejectUnknownKeys(entry, ["root", "repo", "remote"], `${file} projects.${id}`); + rejectUnknownKeys(entry, ["root", "repo", "remote", "git"], `${file} projects.${id}`); if (typeof entry.root !== "string" || !isAbsolute(entry.root) || normalize(entry.root) !== entry.root || entry.root.includes("\0")) { refuse(`${file} projects.${id}.root must be a normalized absolute path`); } @@ -286,8 +286,16 @@ export function validateBusinessDocument(document, file, { rolesDir }) { } // Row 199 (#1697, design D9 step 2): the upstream; a granted agent's work goes to its fork. if (Object.hasOwn(entry, "remote")) httpsUrl(entry.remote, `${file} projects.${id}.remote`, { repo: true }); + if (Object.hasOwn(entry, "git")) { + requireObject(entry.git, `${file} projects.${id}.git`); + rejectUnknownKeys(entry.git, ["mode", "remote"], `${file} projects.${id}.git`); + if (!['local-only', 'remote'].includes(entry.git.mode)) refuse(`${file} projects.${id}.git.mode must be local-only or remote`); + if (entry.git.mode === 'remote') httpsUrl(entry.git.remote, `${file} projects.${id}.git.remote`, { repo: true }); + else if (Object.hasOwn(entry.git, 'remote')) refuse(`${file} projects.${id}.git.remote is not allowed for local-only`); + } projects[id] = { root: entry.root, ...(Object.hasOwn(entry, "repo") ? { repo: entry.repo } : {}), - ...(Object.hasOwn(entry, "remote") ? { remote: entry.remote } : {}) }; + ...(Object.hasOwn(entry, "remote") ? { remote: entry.remote } : {}), + ...(Object.hasOwn(entry, "git") ? { git: { ...entry.git } } : {}) }; } for (const [instance, role] of Object.entries(roles)) { for (const project of role.publish ?? []) { diff --git a/packages/business/src/org-registry.mjs b/packages/business/src/org-registry.mjs index 61409c277..144dd4126 100644 --- a/packages/business/src/org-registry.mjs +++ b/packages/business/src/org-registry.mjs @@ -23,8 +23,9 @@ async function readPrivate(file) { const fd = await fs.open(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); 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); const file = join(configDir, 'org-runtime.json'); let doc; + 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); } 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); } @@ -32,7 +33,7 @@ export async function readRegistry({ configDir }) { } export async function listOrgs(ctx) { const doc = await readRegistry(ctx); return doc ? Object.values(doc.orgs) : []; } export async function withRegistry(ctx, fn) { - await safeDir(ctx.configDir); const lock = join(ctx.configDir, 'org-runtime.lock'); let fd; + 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); try { @@ -49,7 +50,8 @@ export async function reserveOrg(ctx, id) { return withRegistry(ctx, async registry => { if (Object.hasOwn(registry.orgs, id)) fail('org-exists', 'organization already reserved', root); for (const entry of Object.values(registry.orgs)) if (entry.root === root || root.startsWith(entry.root + '/') || entry.root.startsWith(root + '/')) fail('org-overlap', 'organization roots overlap', root); - await safeDir(dirname(root), true); + await safeDir(ctx.dataRoot); await privateDir(ctx.dataRoot); + await safeDir(dirname(root), true); await privateDir(dirname(root)); try { await fs.mkdir(root, { mode: 0o700 }); } catch (e) { if (e.code === 'EEXIST') fail('org-exists', 'organization root already exists', root); throw e; } await fs.writeFile(join(root, '.mosaic-org.json'), JSON.stringify({ instance: ctx.instance, id }) + '\n', { mode: 0o600, flag: 'wx' }); const entry = { id, root, state: 'reserved', created: new Date().toISOString(), revision: 1 }; registry.orgs[id] = entry; return { ...entry }; @@ -59,7 +61,7 @@ export async function resolveOrg(ctx, id) { const root = orgRoot(ctx.dataRoot, id); const registry = await readRegistry(ctx); const entry = registry?.orgs?.[id]; if (!entry) fail('org-unknown', 'unknown organization'); if (registry.instance !== ctx.instance || entry.root !== root) fail('org-mismatch', 'organization binding mismatch', root); - try { await safeDir(root); const st = await fs.lstat(root); if (st.uid !== process.getuid() || (st.mode & 0o777) !== 0o700) fail('org-mismatch', 'unsafe organization root', root); const marker = await readPrivate(join(root, '.mosaic-org.json')); if (marker.instance !== ctx.instance || marker.id !== id || Object.keys(marker).sort().join(',') !== 'id,instance') fail('org-mismatch', 'organization marker mismatch', root); } + try { await safeDir(root); await privateDir(ctx.dataRoot); await privateDir(dirname(root)); const st = await fs.lstat(root); if (st.uid !== process.getuid() || (st.mode & 0o777) !== 0o700) fail('org-mismatch', 'unsafe organization root', root); const marker = await readPrivate(join(root, '.mosaic-org.json')); if (marker.instance !== ctx.instance || marker.id !== id || Object.keys(marker).sort().join(',') !== 'id,instance') fail('org-mismatch', 'organization marker mismatch', root); } catch (e) { if (e instanceof OrgError) throw e; fail('org-mismatch', 'cannot verify organization root or marker', root); } return { id, root, state: entry.state }; } diff --git a/packages/business/src/org-service.mjs b/packages/business/src/org-service.mjs index a73f77c3b..8e767eaec 100644 --- a/packages/business/src/org-service.mjs +++ b/packages/business/src/org-service.mjs @@ -1,24 +1,137 @@ -import { readFile } from 'node:fs/promises'; +import { constants } from 'node:fs'; +import * as fs from 'node:fs/promises'; +import { spawn } from 'node:child_process'; +import { createHash, randomBytes } from 'node:crypto'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; -import { validateBusinessDocument } from './business.mjs'; -import { OrgError } from './org-registry.mjs'; +import { validateBusinessDocument, httpsUrl } from './business.mjs'; +import { ID_PATTERN } from './vocabulary.mjs'; +import { OrgError, orgRoot, readRegistry, reserveOrg, resolveOrg, withRegistry } from './org-registry.mjs'; const rolesDir = resolve(dirname(fileURLToPath(import.meta.url)), '../../../roles'); +const owner = Object.freeze({ provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' }); +const fail = (code, message, path) => { throw new OrgError(code, message, path); }; +const same = (a, b) => !!a && !!b && ['provider', 'iss', 'sub'].every(k => a[k] === b[k]); +const digest = text => createHash('sha256').update(text).digest('hex'); +const encode = doc => JSON.stringify(doc, null, 2) + '\n'; +const projectPath = (root, project) => join(root, 'projects', project); +function authorize(ctx) { if (!ctx || typeof ctx.instance !== 'string' || !/^[a-z][a-z0-9]{1,15}$/.test(ctx.instance) || ctx.instance === 'dev' || !same(ctx.principal, owner)) fail('org-forbidden', 'authenticated local development owner required'); } +function checkId(id) { if (typeof id !== 'string' || !ID_PATTERN.test(id)) fail('org-invalid', 'invalid id'); } +async function privateRead(file) { const fd = await fs.open(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); try { const st = await fd.stat(); if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o077)) fail('org-mismatch', 'unsafe file', file); return await fd.readFile('utf8'); } finally { await fd.close(); } } +async function atomic(file, content, exclusive = false) { + const tmp = join(dirname(file), `.org-write-${randomBytes(12).toString('hex')}`); + try { await fs.writeFile(tmp, content, { mode: 0o600, flag: 'wx' }); if (exclusive) await fs.link(tmp, file); else await fs.rename(tmp, file); } finally { await fs.rm(tmp, { force: true }); } +} +async function ensurePrivateDir(path) { try { await fs.mkdir(path, { mode: 0o700 }); } catch (e) { if (e.code !== 'EEXIST') throw e; } const st = await fs.lstat(path); if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o077)) fail('org-mismatch', 'unsafe directory', path); } +async function receipt(root, op, org, principal, fields = {}) { + const result = { op, org, at: new Date().toISOString(), subject: { ...principal }, ...fields }; + const file = join(root, 'receipts.jsonl'); const fd = await fs.open(file, constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW, 0o600); + try { const st = await fd.stat(); if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o077)) fail('org-mismatch', 'unsafe receipts', file); await fd.writeFile(JSON.stringify(result) + '\n'); } finally { await fd.close(); } return result; +} +async function receipts(root) { + try { const text = await privateRead(join(root, 'receipts.jsonl')); return text.trim().split('\n').map(line => JSON.parse(line)); } + catch { fail('org-mismatch', 'organization receipts cannot be read'); } +} +function normalizeDoc(ctx, id, input, existing) { + if (!input || input.id !== id) fail('org-invalid', 'document id must match organization id', '$.id'); + const doc = structuredClone(input); const root = orgRoot(ctx.dataRoot, id); + for (const [project, entry] of Object.entries(doc.projects ?? {})) { + checkId(project); if (!entry || entry.kind === 'library') fail('org-invalid', 'configured projects must be Git projects', `$.projects.${project}`); + const expected = projectPath(root, project), placeholder = `/__mosaic_org__/${id}/projects/${project}`; + if (entry.root !== expected && entry.root !== placeholder) fail('org-invalid', 'project root must be derived from the organization', `$.projects.${project}.root`); + if (entry.repo !== undefined && entry.repo !== expected) fail('org-invalid', 'project repo must be derived from the organization', `$.projects.${project}.repo`); + const previous = existing?.projects?.[project]; + if (entry.git && JSON.stringify(entry.git) !== JSON.stringify(previous?.git)) fail('org-invalid', 'project git binding is established by repo init', `$.projects.${project}.git`); + if (previous?.git && JSON.stringify(entry.git) !== JSON.stringify(previous.git)) fail('org-invalid', 'initialized git binding cannot be changed', `$.projects.${project}.git`); + entry.root = expected; if (entry.repo !== undefined) entry.repo = expected; + } + if (existing) for (const [p, entry] of Object.entries(existing.projects)) if (entry.git && !doc.projects?.[p]) fail('org-invalid', 'initialized project cannot be removed', `$.projects.${p}`); + return doc; +} export async function templateRender(id, { fixture } = {}) { - if (fixture !== undefined && fixture !== 'local') throw new OrgError('org-invalid', 'unknown fixture'); - const doc = JSON.parse(await readFile(new URL('./org-template.json', import.meta.url), 'utf8')); - doc.id = id; doc.projects.work.root = `/__mosaic_org__/${id}/projects/work`; - const result = await validateOrg(doc); if (!result.ok) throw new OrgError('org-invalid', result.errors[0].message); return doc; + if (fixture !== undefined && fixture !== 'local') fail('org-invalid', 'unknown fixture'); checkId(id); + const doc = JSON.parse(await fs.readFile(new URL('./org-template.json', import.meta.url), 'utf8')); + doc.id = id; doc.projects.work.root = `/__mosaic_org__/${id}/projects/work`; return doc; } export async function validateOrg(doc) { - try { validateBusinessDocument(doc, join('/businesses', `${doc?.id}.json`), { rolesDir }); return { ok: true, errors: [] }; } - catch (e) { return { ok: false, errors: [{ path: '$', message: e.message }] }; } + try { + checkId(doc?.id); validateBusinessDocument(doc, join('/businesses', `${doc.id}.json`), { rolesDir }); + for (const [project, entry] of Object.entries(doc.projects)) { + const suffix = `/org-runtime/${doc.id}/projects/${project}`; + const placeholder = `/__mosaic_org__/${doc.id}/projects/${project}`; + if (entry.kind === 'library' || (entry.root !== placeholder && !entry.root.endsWith(suffix))) fail('org-invalid', 'project root must be derived from the organization', `$.projects.${project}.root`); + if (entry.repo !== undefined && entry.repo !== entry.root) fail('org-invalid', 'project repo must match its root', `$.projects.${project}.repo`); + } + return { ok: true, errors: [] }; + } + catch (e) { + const match = /(?:^|[ /])(?:projects|roles|arbiters)\.([a-z0-9._-]+(?:\.[a-z0-9._-]+)*)/.exec(e.message); + const field = /(?:business file requires |business file ")"?([a-zA-Z][a-zA-Z0-9]*)/.exec(e.message); + const path = match ? '$.' + match[0].trim().replace(/^.*\//, '') : field ? '$.' + field[1] : /business id/.test(e.message) ? '$.id' : /business human/.test(e.message) ? '$.human' : '$'; + return { ok: false, errors: [{ path: e.path ?? path, message: e.message }] }; + } } -const pending = () => { throw new OrgError('org-unavailable', 'organization service operation not implemented yet'); }; -export async function createOrg(ctx, id, doc) { return pending(); } -export async function showOrg(ctx, id) { return pending(); } -export async function updateOrg(ctx, id, doc, { ifRevision } = {}) { return pending(); } -export async function queueInit(ctx, id, { mode, remote, acceptLocalLossRisk } = {}) { return pending(); } -export async function projectRepoInit(ctx, id, project, { mode, remote, acceptLocalLossRisk } = {}) { return pending(); } -export async function queueShow(ctx, id) { return pending(); } -export async function projectRepoShow(ctx, id, project) { return pending(); } +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) { + 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 }; +} +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); + await readRegistry(ctx); + const businessDir = join(ctx.configDir, 'businesses'); await ensurePrivateDir(businessDir); + const file = join(businessDir, `${id}.json`); try { await fs.lstat(file); fail('org-exists', 'business file already exists', file); } catch (e) { if (e.code !== 'ENOENT') throw e; } + const entry = await reserveOrg(ctx, id); const raw = encode(doc); + await atomic(join(entry.root, 'owner.json'), encode(ctx.principal), true); await atomic(file, raw, true); + const result = await receipt(entry.root, 'org.create', id, ctx.principal, { root: entry.root, revision: entry.revision }); + await withRegistry(ctx, async registry => { const found = await resolveOrg(ctx, id); if (found.state !== 'reserved') fail('org-mismatch', 'reservation changed'); registry.orgs[id].digest = digest(raw); registry.orgs[id].state = 'active'; }); return result; +} +export async function showOrg(ctx, id) { authorize(ctx); const { root, revision, doc, principal } = await readCurrent(ctx, id); return { id, root, revision, doc, principal }; } +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 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 }); + }); +} +function options({ mode, remote, acceptLocalLossRisk }) { + if (mode === 'local-only') { if (acceptLocalLossRisk !== true || remote !== undefined) fail('org-risk', 'local-only requires explicit loss-risk acceptance and no remote'); return { mode, riskAccepted: true }; } + if (mode === 'remote') { if (acceptLocalLossRisk || typeof remote !== 'string') fail('org-invalid', 'remote mode requires a remote URL'); try { httpsUrl(remote, 'remote', { repo: true }); } catch (e) { fail('org-invalid', e.message); } return { mode, remote }; } + fail('org-invalid', 'select local-only or remote'); +} +async function git(root, args) { + const env = { PATH: process.env.PATH ?? '/usr/bin:/bin', HOME: '/nonexistent', GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: '/dev/null', GIT_TERMINAL_PROMPT: '0', LC_ALL: 'C' }; + return new Promise((resolve, reject) => { const p = spawn('git', ['-c', 'user.name=Mosaic Local', '-c', 'user.email=mosaic@localhost.invalid', '-c', 'core.hooksPath=/dev/null', '-C', root, ...args], { env, stdio: ['ignore', 'pipe', 'pipe'] }); let out = '', err = ''; p.stdout.on('data', d => out += d); p.stderr.on('data', d => err += d); p.on('error', reject); p.on('close', code => code === 0 ? resolve(out.trim()) : reject(new OrgError('org-git', `git failed: ${err.trim()}`))); }); +} +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 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); + }); +} +export async function queueInit(ctx, id, opts = {}) { return setup(ctx, id, null, opts); } +export async function projectRepoInit(ctx, id, project, opts = {}) { return setup(ctx, id, project, opts); } +async function showSetup(ctx, id, project) { + authorize(ctx); const current = await readCurrent(ctx, id); if (project !== null) { checkId(project); if (!Object.hasOwn(current.doc.projects, project)) fail('org-unknown', 'unknown project'); } + const records = await receipts(current.root); + const op = project === null ? 'org.queue.init' : 'org.project.repo.init'; const item = records.findLast(r => r.op === op && (project === null || r.project === project)); + const root = project === null ? join(current.root, 'checkout') : projectPath(current.root, project); + if (!item || item.root !== root || !same(item.subject, current.principal)) fail('org-unavailable', 'setup receipt unavailable'); + if (project !== null) { const parent = await fs.lstat(join(current.root, 'projects')); if (!parent.isDirectory() || parent.isSymbolicLink() || parent.uid !== process.getuid() || (parent.mode & 0o077)) fail('org-mismatch', 'unsafe projects directory'); } + const st = await fs.lstat(root); const dotgit = await fs.lstat(join(root, '.git')); if (!st.isDirectory() || st.isSymbolicLink() || !dotgit.isDirectory() || dotgit.isSymbolicLink()) fail('org-mismatch', 'unsafe git repository'); + await git(root, ['rev-parse', '--verify', 'HEAD']); return item; +} +export async function queueShow(ctx, id) { return showSetup(ctx, id, null); } +export async function projectRepoShow(ctx, id, project) { return showSetup(ctx, id, project); } diff --git a/packages/business/tests/org-registry.test.mjs b/packages/business/tests/org-registry.test.mjs index 8605fd064..6e191d7d2 100644 --- a/packages/business/tests/org-registry.test.mjs +++ b/packages/business/tests/org-registry.test.mjs @@ -6,7 +6,10 @@ import { tmpdir } from 'node:os'; import { spawn } from 'node:child_process'; import { reserveOrg, activateOrg, resolveOrg, orgRoot } from '../src/org-registry.mjs'; -function fixture() { const root = mkdtempSync(join(tmpdir(), 'org-')); const configDir = join(root, 'config'); const dataRoot = join(root, 'data'); mkdirSync(configDir); mkdirSync(dataRoot); return { configDir, dataRoot, instance: 'test' }; } +function fixture() { const root = mkdtempSync(join(tmpdir(), 'org-')); const configDir = join(root, 'config'); const dataRoot = join(root, 'data'); mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 }); return { configDir, dataRoot, instance: 'test' }; } test('reserves and verifies an organization marker', async () => { const c = fixture(); await reserveOrg(c, 'alpha'); await activateOrg(c, 'alpha'); assert.equal((await resolveOrg(c, 'alpha')).root, orgRoot(c.dataRoot, 'alpha')); writeFileSync(join(orgRoot(c.dataRoot, 'alpha'), '.mosaic-org.json'), '{"instance":"other","id":"alpha"}'); await assert.rejects(resolveOrg(c, 'alpha'), { code: 'org-mismatch' }); }); -test('refuses existing and symlink roots', async () => { const c = fixture(); mkdirSync(join(c.dataRoot, 'org-runtime')); mkdirSync(orgRoot(c.dataRoot, 'alpha')); await assert.rejects(reserveOrg(c, 'alpha')); symlinkSync(orgRoot(c.dataRoot, 'alpha'), orgRoot(c.dataRoot, 'beta')); await assert.rejects(reserveOrg(c, 'beta')); }); +test('refuses existing and symlink roots', async () => { const c = fixture(); mkdirSync(join(c.dataRoot, 'org-runtime'), { mode: 0o700 }); mkdirSync(orgRoot(c.dataRoot, 'alpha')); await assert.rejects(reserveOrg(c, 'alpha')); symlinkSync(orgRoot(c.dataRoot, 'alpha'), orgRoot(c.dataRoot, 'beta')); await assert.rejects(reserveOrg(c, 'beta')); }); test('two processes cannot reserve the same id', async () => { const c = fixture(); const url = new URL('../src/org-registry.mjs', import.meta.url).href; const code = `import {reserveOrg} from ${JSON.stringify(url)}; await reserveOrg(${JSON.stringify(c)}, 'alpha');`; const run = () => new Promise(resolve => { const p = spawn(process.execPath, ['--input-type=module', '-e', code], { stdio: 'ignore' }); p.on('close', resolve); }); const results = await Promise.all([run(), run()]); assert.deepEqual(results.sort(), [0, 1]); }); +test('rejects a symlink in the data root path', async () => { const c = fixture(); symlinkSync(c.dataRoot, c.dataRoot + '-link'); c.dataRoot += '-link'; await assert.rejects(reserveOrg(c, 'alpha'), { code: 'org-mismatch' }); }); +test('rejects an overlapping registry root', async () => { const c = fixture(); writeFileSync(join(c.configDir, 'org-runtime.json'), JSON.stringify({version: 1, instance: c.instance, orgs: { other: { id: 'other', root: join(c.dataRoot, 'org-runtime'), state: 'reserved', created: new Date().toISOString(), revision: 1 } } }), { mode: 0o600 }); await assert.rejects(reserveOrg(c, 'alpha'), { code: 'org-overlap' }); }); +test('refuses loose ownership modes and a missing data root', async () => { const c = fixture(); const { chmodSync, rmSync } = await import('node:fs'); chmodSync(c.dataRoot, 0o755); await assert.rejects(reserveOrg(c, 'alpha'), { code: 'org-mismatch' }); chmodSync(c.dataRoot, 0o700); rmSync(c.dataRoot, { recursive: true }); await assert.rejects(reserveOrg(c, 'alpha')); }); diff --git a/packages/business/tests/org-service.test.mjs b/packages/business/tests/org-service.test.mjs index 3ad9dec41..0654ae9d3 100644 --- a/packages/business/tests/org-service.test.mjs +++ b/packages/business/tests/org-service.test.mjs @@ -3,3 +3,17 @@ import assert from 'node:assert/strict'; import { templateRender, validateOrg } from '../src/org-service.mjs'; test('local template validates with the shipped business loader', async () => { const doc = await templateRender('alpha', { fixture: 'local' }); assert.equal(doc.human, 'local-dev-owner'); assert.deepEqual(await validateOrg(doc), { ok: true, errors: [] }); }); + +import { mkdtempSync, mkdirSync } from 'node:fs'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; +import { createOrg, showOrg, updateOrg, queueInit, queueShow, projectRepoInit, projectRepoShow } from '../src/org-service.mjs'; +import { loadBusiness } from '../src/business.mjs'; +import { REPO_ROLES } from './helpers.mjs'; +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('create loads using existing loader and revisions fence updates', async () => { const c = context(); const doc = await templateRender('alpha', { fixture: 'local' }); await createOrg(c, 'alpha', doc); assert.equal(loadBusiness('alpha', { dir: c.configDir, rolesDir: REPO_ROLES }).projects.work.root, join(c.dataRoot, 'org-runtime/alpha/projects/work')); const current = await showOrg(c, 'alpha'); await updateOrg(c, 'alpha', current.doc, { ifRevision: current.revision }); await assert.rejects(updateOrg(c, 'alpha', current.doc, { ifRevision: current.revision }), { code: 'revision-conflict' }); }); +test('git setup requires explicit risk and refuses overwrite', async () => { const c = context(); await createOrg(c, 'alpha', await templateRender('alpha')); await assert.rejects(queueInit(c, 'alpha', { mode: 'local-only' })); const q = await queueInit(c, 'alpha', { mode: 'local-only', acceptLocalLossRisk: true }); assert.equal(q.riskAccepted, true); assert.equal((await queueShow(c, 'alpha')).root, q.root); await assert.rejects(queueInit(c, 'alpha', { mode: 'local-only', acceptLocalLossRisk: true })); const p = await projectRepoInit(c, 'alpha', 'work', { mode: 'local-only', acceptLocalLossRisk: true }); assert.equal((await projectRepoShow(c, 'alpha', 'work')).root, p.root); await assert.rejects(projectRepoInit(c, 'alpha', 'work', { mode: 'local-only', acceptLocalLossRisk: true })); }); +test('invalid paths and unrecognized principals refuse', async () => { const c = context(); const doc = await templateRender('alpha'); doc.projects.work.root = '/etc'; await assert.rejects(createOrg(c, 'alpha', doc)); c.principal = { provider: 'x', iss: 'x', sub: 'someone' }; await assert.rejects(createOrg(c, 'beta', await templateRender('beta'))); }); +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 })); }); diff --git a/packages/cli/README.md b/packages/cli/README.md index c2056c975..5a18643d4 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -1232,3 +1232,22 @@ and require confirmation. Cancellation adds no force-stop or kill-switch authori Both before/after outcomes appear as `cancelled`, with `retired: null` or `true` in JSON. If a routed message is stored but cancelled because its session is clearing/retired, the CLI prints its ID, reports the cancellation and exits 3 without waiting for a reply. + +### Organization commands + +On a fresh named development instance, `mosaic org template render ID --fixture +local --output FILE`, `mosaic org validate FILE`, and `mosaic org create ID +--from FILE` prepare an organization. `mosaic org show ID [--json]` and +`mosaic org list [--json]` read it; `mosaic org update ID --from FILE +--if-revision N` requires the revision from show. Each configured project gets +its own derived root. + +`mosaic org queue init ID --local-only --accept-local-loss-risk` initializes +its checkout; `mosaic org project repo init ID PROJECT --local-only +--accept-local-loss-risk` initializes a project. For either command `--remote +URL` selects remote mode instead. These commands never push or overwrite an +existing repository. Local-only acceptance records the risk of losing the only +copy. Read back with `mosaic org queue show ID [--json]` or `mosaic org project +repo show ID PROJECT [--json]`. See the Business README for the layout and +scope. On existing CLI commands, `--org ID` is an alias for `--business ID`; +if both are supplied they must name the same ID. diff --git a/packages/cli/src/cli.mjs b/packages/cli/src/cli.mjs index e786aa5e3..20eb7d2ab 100644 --- a/packages/cli/src/cli.mjs +++ b/packages/cli/src/cli.mjs @@ -40,8 +40,10 @@ import { readSessions, stopSession } from "../../seat/src/session.mjs"; import { SeatError } from "../../seat/src/seat.mjs"; import { usageCommand, USAGE_USAGE } from "./usage.mjs"; +import { orgCommand, ORG_USAGE } from "./org.mjs"; export const USAGE = `usage: + ${ORG_USAGE} ${USAGE_USAGE} ${LIMITS_USAGE} ${PLUGINS_USAGE} @@ -642,7 +644,15 @@ async function app(io, deps) { export async function main(argv, io = { env: process.env, stdin: process.stdin, stdout: process.stdout, stderr: process.stderr }, deps = {}) { if (argv.length === 0) return app(io, deps); + if (argv[0] !== 'org') { + const org = argv.indexOf('--org'), business = argv.indexOf('--business'); + if (org !== -1 && business !== -1) { + if (!argv[org + 1] || !argv[business + 1] || argv[org + 1] !== argv[business + 1]) throw usage('--org and --business conflict'); + argv = argv.filter((_, i) => i !== org && i !== org + 1); + } else if (org !== -1) argv = argv.map((arg, i) => i === org ? '--business' : arg); + } const [verb, ...rest] = argv; + if (verb === 'org') return orgCommand(rest, io, deps); if (["inbox", "decide", "tasks", "agents", "trail"].includes(verb)) return human(verb, rest, io, deps); if (verb === "project") return ["create", "list", "archive", "unarchive"].includes(rest[0]) ? projectBusCommand(rest, io, (p) => channelCallFor(p, io, deps)) : projectCommand(rest, io, deps, (p) => callFor(p, io, deps)); if (verb === "threads") return threadsCommand(rest, io, (p) => channelCallFor(p, io, deps)); diff --git a/packages/cli/src/org.mjs b/packages/cli/src/org.mjs new file mode 100644 index 000000000..bed61615b --- /dev/null +++ b/packages/cli/src/org.mjs @@ -0,0 +1,31 @@ +import { readFile, writeFile } from 'node:fs/promises'; +import { configDir } from '../../business/src/business.mjs'; +import { listOrgs } from '../../business/src/org-registry.mjs'; +import { templateRender, validateOrg, createOrg, showOrg, updateOrg, queueInit, queueShow, projectRepoInit, projectRepoShow } from '../../business/src/org-service.mjs'; +import { DEV_OWNER, devOwnerInstance } from './dev-local-owner.mjs'; +import { loadSystem } from './config.mjs'; +import { CliError } from './errors.mjs'; +export const ORG_USAGE = 'mosaic org template render ID [--fixture local] --output FILE | validate FILE | create ID --from FILE | show ID [--json] | list [--json] | update ID --from FILE --if-revision N | queue init ID (--local-only --accept-local-loss-risk | --remote URL) | queue show ID [--json] | project repo init ID PROJECT (--local-only --accept-local-loss-risk | --remote URL) | project repo show ID PROJECT [--json]'; +function parse(args) { const pos = [], values = {}, flags = new Set(); for (let i = 0; i < args.length; i++) { const a = args[i]; if (['--json', '--local-only', '--accept-local-loss-risk'].includes(a)) { if (flags.has(a)) throw new CliError(`duplicate ${a}`, 4); flags.add(a); } else if (['--fixture', '--output', '--from', '--if-revision', '--remote'].includes(a)) { if (values[a] !== undefined || !args[i+1] || args[i+1].startsWith('--')) throw new CliError(`${a} needs one value`, 4); values[a] = args[++i]; } else if (a.startsWith('--')) throw new CliError(`unknown option ${a}`, 4); else pos.push(a); } return { pos, values, flags }; } +async function document(file) { try { return JSON.parse(await readFile(file, 'utf8')); } catch (e) { throw new CliError(`cannot read document ${file}: ${e.message}`, 2); } } +export async function orgCommand(args, io, deps = {}) { + try { + const p = parse(args); const words = p.pos; let result; let accepted = []; + const allowed = (flags, values) => { for (const f of p.flags) if (!flags.includes(f)) throw new CliError(`unexpected ${f}`, 4); for (const f of Object.keys(p.values)) if (!values.includes(f)) throw new CliError(`unexpected ${f}`, 4); }; + const need = key => { if (!p.values[key]) throw new CliError(`${key} is required`, 4); return p.values[key]; }; + if (words[0] === 'template' && words[1] === 'render' && words.length === 3) { allowed([], ['--fixture', '--output']); result = await templateRender(words[2], { fixture: p.values['--fixture'] }); await writeFile(need('--output'), JSON.stringify(result, null, 2) + '\n', { flag: 'wx', mode: 0o600 }); io.stdout.write(`rendered ${words[2]} to ${p.values['--output']}\n`); return; } + if (words[0] === 'validate' && words.length === 2) { allowed([], []); result = await validateOrg(await document(words[1])); if (!result.ok) throw new CliError(result.errors.map(e => `${e.path}: ${e.message}`).join('\n'), 2); io.stdout.write('valid\n'); return; } + const instance = devOwnerInstance(io.env); const system = deps.system ?? loadSystem({ env: io.env, instance }); const ctx = { configDir: configDir(io.env), dataRoot: system.dataRoot, instance, principal: DEV_OWNER }; + if (words[0] === 'create' && words.length === 2) { allowed([], ['--from']); result = await createOrg(ctx, words[1], await document(need('--from'))); } + else if (words[0] === 'update' && words.length === 2) { allowed([], ['--from', '--if-revision']); const r = need('--if-revision'); if (!/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid revision', 4); result = await updateOrg(ctx, words[1], await document(need('--from')), { ifRevision: Number(r) }); } + else if (words[0] === 'show' && words.length === 2) { allowed(['--json'], []); result = await showOrg(ctx, words[1]); } + else if (words[0] === 'list' && words.length === 1) { allowed(['--json'], []); for (const entry of await listOrgs(ctx)) if (entry.state === 'active') accepted.push(await showOrg(ctx, entry.id)); result = accepted; } + else if ((words[0] === 'queue' && words.length === 3) || (words[0] === 'project' && words[1] === 'repo' && words.length === 5)) { + const project = words[0] === 'project', op = words[project ? 2 : 1], id = words[project ? 3 : 2], name = project ? words[4] : null; + if (op === 'show') { allowed(['--json'], []); result = project ? await projectRepoShow(ctx, id, name) : await queueShow(ctx, id); } + else if (op === 'init') { allowed(['--local-only', '--accept-local-loss-risk'], ['--remote']); if (p.flags.has('--local-only') === Boolean(p.values['--remote'])) throw new CliError('select exactly one of --local-only or --remote', 4); const opts = { mode: p.flags.has('--local-only') ? 'local-only' : 'remote', remote: p.values['--remote'], acceptLocalLossRisk: p.flags.has('--accept-local-loss-risk') }; result = project ? await projectRepoInit(ctx, id, name, opts) : await queueInit(ctx, id, opts); } + else throw new CliError(ORG_USAGE, 4); + } else throw new CliError(ORG_USAGE, 4); + io.stdout.write(JSON.stringify(result, null, 2) + '\n'); + } catch (e) { if (e instanceof CliError) throw e; throw new CliError(`${e.code ? e.code + ': ' : ''}${e.message}`, 2); } +} diff --git a/packages/cli/tests/org.test.mjs b/packages/cli/tests/org.test.mjs new file mode 100644 index 000000000..68dbd0702 --- /dev/null +++ b/packages/cli/tests/org.test.mjs @@ -0,0 +1,25 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, readFileSync } from 'node:fs'; +import { join, resolve } from 'node:path'; +import { tmpdir } from 'node:os'; +import { spawnSync } from 'node:child_process'; +const repo = resolve(import.meta.dirname, '../../..'); +function run(env, ...args) { return spawnSync(join(repo, 'scripts/mosaic'), args, { cwd: repo, env, encoding: 'utf8' }); } +test('organization CLI renders, validates, creates, updates and initializes git', () => { + const home = mkdtempSync(join(tmpdir(), 'org-cli-')); const env = { ...process.env, HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-orgtest/config.json') }; + const boot = spawnSync(join(repo, 'scripts/bootstrap.sh'), ['--instance', 'orgtest'], { cwd: repo, env, encoding: 'utf8' }); assert.equal(boot.status, 0, boot.stderr); + const file = join(home, 'alpha.json'); const invoke = (...args) => { const r = run(env, ...args); assert.equal(r.status, 0, r.stderr); return r.stdout; }; + invoke('org', 'template', 'render', 'alpha', '--fixture', 'local', '--output', file); + invoke('org', 'validate', file); invoke('org', 'create', 'alpha', '--from', file); + const bootCheck = spawnSync(process.execPath, ['--input-type=module', '-e', `import {loadSystem, bootConfig} from './packages/cli/src/config.mjs'; const system=loadSystem({env:process.env, instance:'orgtest'}); bootConfig({system,businessId:'alpha',env:process.env});`], { cwd: repo, env, encoding: 'utf8' }); assert.equal(bootCheck.status, 0, bootCheck.stderr); + let info = JSON.parse(invoke('org', 'show', 'alpha', '--json')); assert.equal(info.id, 'alpha'); assert.equal(JSON.parse(invoke('org', 'list', '--json')).length, 1); + invoke('org', 'update', 'alpha', '--from', file, '--if-revision', String(info.revision)); + invoke('org', 'queue', 'init', 'alpha', '--local-only', '--accept-local-loss-risk'); assert.equal(JSON.parse(invoke('org', 'queue', 'show', 'alpha', '--json')).riskAccepted, true); + invoke('org', 'project', 'repo', 'init', 'alpha', 'work', '--local-only', '--accept-local-loss-risk'); assert.equal(JSON.parse(invoke('org', 'project', 'repo', 'show', 'alpha', 'work', '--json')).project, 'work'); assert.equal(JSON.parse(readFileSync(file)).id, 'alpha'); +}); +test('business and org aliases conflict in either order', () => { for (const args of [['--business','alpha','--org','beta'], ['--org','beta','--business','alpha']]) { const r = run(process.env, 'tasks', ...args); assert.notEqual(r.status, 0); assert.match(r.stderr, /conflict/i); } }); + +import { main } from '../src/cli.mjs'; +import { io } from './helpers.mjs'; +test('org alias preserves existing command output and matching aliases are accepted', async () => { const seen = []; const deps = { system: { dataRoot: '/unused' }, transport: business => async () => { seen.push(business); return []; } }; const outputs = []; for (const options of [['--business','alpha'], ['--org','alpha'], ['--org','alpha','--business','alpha'], ['--business','alpha','--org','alpha']]) { const x = io(); await main(['tasks', '--json', ...options], x, deps); outputs.push(x.out.text); } assert.deepEqual(seen, ['alpha','alpha','alpha','alpha']); assert.equal(new Set(outputs).size, 1); }); diff --git a/scripts/mosaic b/scripts/mosaic index bb1cb8971..ee9c5f4c2 100755 --- a/scripts/mosaic +++ b/scripts/mosaic @@ -41,6 +41,6 @@ if [ "${1:-}" = library ] && [ "${2:-}" = survey ]; then exec node "$REPO/packages/library/src/survey.mjs" "$@" fi case "${1:-}" in - proxy|halt|resume|processing|usage|project|limits|inbox|decide|tasks|agents|trail|talk|stop|launches|bus|plugins|credentials|hosts|grants|name|names|follow|threads|thread|access) exec node "$REPO/packages/cli/src/cli.mjs" "$@" ;; + org|proxy|halt|resume|processing|usage|project|limits|inbox|decide|tasks|agents|trail|talk|stop|launches|bus|plugins|credentials|hosts|grants|name|names|follow|threads|thread|access) exec node "$REPO/packages/cli/src/cli.mjs" "$@" ;; esac exec node "$REPO/packages/seat/src/cli.mjs" "$@"