Merge organizations MVP (ui-org-r2) into refactor #1862

Merged
jason.woltje merged 141 commits from org-integrate into refactor 2026-11-09 03:56:16 +00:00
250 changed files with 16000 additions and 1371 deletions

No files matched your search

+12
View File
@@ -6325,3 +6325,15 @@ R5 quote fallback room (#797 D3, 2026-11-06, MVP3 lead): r4 review P2 found that
2026-11-06 UTC | #1852 verification | Node 26 final harness 332/0, bus excluding project-files 1648/0, launcher and sandbox 69/0, and additional grep-selected tests 252/0. An overlapping bus run had library-sync-busy and a 30-second timeout; both affected files then passed 27/0 and the full bus rerun passed. The candidate's Claude row-66 test failed in an earlier full and isolated run, then the candidate and exact-base full harness suites passed 332/0 and 318/0 respectively; the base failure was not reproduced. Evidence: /mnt/storage/scratch/tmp/mDnSad/.
2026-11-06 UTC | #1852 Sol review and lead fix round | Sol xhigh review of 3c689efa3/44c3b19fd: CHANGES, four findings. A processing halt between attempts still started a continuation, since state and usage.end pass the halt gate; a failed continuation check replied with an empty RESULT; a throwing attempt skipped usage.end; library retry continuations ran after the message's usage.end. Lead fix 3e03e123f: each continuation first reads state and processing.status, and a halted, invalid or unreadable processing state starts nothing and leaves the message unread with no reply; a failed check keeps the failed-turn classification and sends no RESULT; usage.end for the message id runs in a finally; the library citation retry starts no continuations, and an empty length retry fails visibly with no fixed RESULT (the retry's own bracket is refused, #1853). Each new runner test fails on 3c689efa3. Part B re-cut with the compaction effect qualified (gated as 542dbe030 directly on 3e03e123f; on the branch it follows this docs commit with the same code): pi allows one recovery in a row, re-armed by a new prompt or a successful reply, inside one runner attempt; output clamping now starts above about 58368 estimated input tokens instead of 107520; threshold compaction is unchanged above 111616. Corrections to the builder entries above: the one usage bracket did not cover the library retry continuations or a throwing attempt; the Claude row-66 failure does reproduce on base 3598839e6 (1 of 8 isolated runs, candidate 3 of 8), so it is a pre-existing flake.
2026-11-06 UTC | #1852 gate | Node 26.12.1 on 542dbe030 (A+fix+B): touched 143/0, bus except project-files 1648/0, harness 337/0, webui 679/0 (first run 676/2/1: proxy-architecture could not load esbuild because the gate worktree lacked packages/webui/node_modules, and decide-browser I1 timed out; both passed 10/10 once the modules were linked, then the whole suite passed), grep-selected readers of the changed files 375/1. On 3e03e123f (A+fix, no B): touched 142/0, harness 335/1 (row-66 flake; file rerun 14/0), readers 375/1. The one reader failure, plugins harness.test row 65, also fails on base 3598839e6: first-output.mjs:42 (c7ddc7d31) and runner.mjs:406 (478df1cac) name a harness in core source; out of this row's scope. Evidence: /mnt/storage/scratch/tmp/PKGwA7/ and /mnt/storage/scratch/tmp/g0DYZH/.
2026-11-06 UTC | UI slice 4 polish — implementation phase (recorded retrospectively) | Before: ui-dev base ad9b2dee4 had low project headers, clipped composer layouts, incomplete conversation work-card summaries and opaque or inappropriate agent launch controls. The assigned scope is documented in /mnt/storage/scratch/mosaic-lead/uidev/brief-s4-polish.md. The linked-worktree queue refused; canonical checkout access was excluded by the assignment. After and verification are recorded in the following phase entry.
2026-11-06 UTC | UI slice 4 polish — implementation and verification | After: project headers, mobile details controls, phase stepper, linked PRD/files summaries and viewport composers were refined; thread instances no longer offer Launch, lifecycle refusals show codes and hints, and the unused proxy document was removed. Node 26 full WebUI suite: 758 pass, 0 fail, 0 cancelled, 0 skipped. Baseline and repaint/count/layout negative controls, intermediate failures and screenshot comparisons are recorded in /mnt/storage/scratch/mosaic-lead/uidev/s4-report.md and adjacent logs. Runtime source and tests were unchanged during the final suite; these append-only phase notes were recorded during and after it. Queue refused the linked worktree; no push, merge or deployment. Local candidate for independent review.
2026-11-06 UTC | UI slice 6b — implementation phase | Before: ui-dev base 43686b145 shows unavailable project states for absent records and message correlation IDs with no lifecycle record, and the audit records several remaining presentation defects. The assignment and protected boundaries are in /mnt/storage/scratch/mosaic-lead/uidev/brief-s6b.md. The queue refused this linked worktree. Verification and outcome are recorded in the following phase entry.
2026-11-06 UTC | UI slice 6b — implementation and verification | After: missing project records have a named refusal and empty presentation; message correlation IDs with no intake record no longer imply failed request reads, while real failures remain visible. Conversation, holder, tab, chip and Assistant sizing polish is included. Node 26 final gates: project 51 pass / 0 fail / 0 cancelled / 0 skipped; WebUI 786 / 0 / 0 / 0, bounded to four CPUs after two load-sensitive timeouts passed independently. The earlier full run and its failures, fixture screenshots and request-ID trace are in /mnt/storage/scratch/mosaic-lead/uidev/s6b-report.md. Queue refused the linked worktree; no push, merge or deployment. Local candidate for independent review.
2026-11-07 UTC | Organizations S1 — implementation phase | Before: ui-dev c8d6d12fe used the instance root for one bus and the Console had no scoped lifecycle commands. The S1 stream charter and boundaries are in /mnt/storage/scratch/mosaic-lead/uidev/org-release/PLAN.md. The queue refused this linked worktree; canonical checkout access was excluded. Verification and unresolved integration boundaries follow in the stream report.
2026-11-07 UTC | Organizations S1 — implementation evidence | Organization-root bus and launcher paths, guarded lifecycle and Console launch/stop were implemented locally. Focused Node 26 tests and host regressions passed; the fresh-organization notifier choice, shared-proxy S2 integration, and remaining instance-root consumers are not complete. Exact commands, inventory, failures and boundaries: /mnt/storage/scratch/mosaic-lead/uidev/org-release/report-s1.md. No push or deployment. Gitea issue creation was excluded by the no-network and no-external-communication limits.
+2 -1
View File
@@ -133,6 +133,7 @@ set -- \
# interactive mode appends nothing - clean TUI.
[ "$INTERACTIVE" = "1" ] || set -- "$@" -p "$MOSAIC_REQUEST"
if [ -n "${MOSAIC_MODEL_EVENT_MONITOR:-}" ] && [ "$INTERACTIVE" != "1" ]; then
exec node "$MOSAIC_MODEL_EVENT_MONITOR" pi "$MOSAIC_FIRST_OUTPUT_DIR" pi "$@" --mode json
STREAM="$(dirname "$0")/../../packages/plugins/harness/pi/model-stream.mjs"
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" pi "$@" --mode json
fi
exec pi "$@"
+41
View File
@@ -0,0 +1,41 @@
# Organization archive and restore
Archive and restore add the `archived` registry state and increment the
revision. Archive takes the existing lifecycle lock, verifies the revision,
stops and verifies the bus through the caller-injected stop function, then
checks the revision again before changing state. The registry lock is not held
while stopping the bus. A concurrent update during the stop causes a revision
conflict and leaves the organization active, although its bus has stopped.
Restore uses the same lifecycle lock, changes the state back to active and does
not start the bus. Files remain in place; delete remains available.
The lifecycle log is append-only and private. Its line is appended under the
registry lock immediately before registry publication. A crash or a failed
registry publication in that narrow interval can leave a log line for an
operation whose state change was not published; reconciliation or a durable
transaction journal is a follow-up. A failed or uncertain stop never records a
completed archive.
Archived organizations remain available to the owner-only administration
routes, but ordinary organization resolution rejects them with `org-archived`.
The UI shares delete's reply verification, organization refresh and current-org
switch logic. Archived documents are read-only, setup controls are hidden with
an explanatory notice, and restore does not start the bus. Screenshots are
untracked under `shots/`.
The service, CLI, route and browser regressions and the requested existing
checks passed in clean scratch homes. The full business suite passed. An
additional routing check first caught a too-broad admin-hook exemption; it was
narrowed to the built-in handler and the web checks passed again.
## Review fix: lifecycle fences
The internal host now takes the organization lifecycle lock before checking
registry state and holds it through host readiness. The parent takes a short
separate lock around its own host-directory preparation, then releases it before
spawning the child, avoiding a parent/child deadlock. If archive wins between
those two lock acquisitions, the child rechecks state and refuses; a competing
start or archive may therefore need a retry. Queue and project setup hold the
same lock through their receipt publication. A FIFO lifecycle log is opened
nonblocking and refused; the regular-file ownership and mode checks remain.
The log-before-registry publication crash interval remains the follow-up above.
+108
View File
@@ -0,0 +1,108 @@
# Organization deletion — implementation notes
Base: `c09570d69` on `quick-setup`. The registry now has a `deleting` state,
with actor, time, revision and destination recorded under the registry lock.
The lock is released before waiting for the bus. Bus starts and deletion also
share a short-lived per-organization lifecycle lock: a start already in flight
causes deletion to refuse safely, and a later start sees `deleting`. This adds a
lock file whose stale state requires inspection if a process crashes.
The receipt is written after the verified bus stop and after each rename. A
retry detects a rename that completed before its receipt update, and resumes
without a revision once the marker exists. No moves are rolled back. Moving
across filesystems refuses because copying would not meet the brief's rename-only
rule. Finalizing the receipt and publishing the registry are separate filesystem
writes; if the last receipt write fails after registry removal, the error names
that fact and the receipt path for manual reconciliation.
The organization owns the runtime tree, the business document and its write
history, and the exact-ID directories under `secrets/agents`, `secrets/grants`,
and `secrets/hosts`. Mos authorized inspecting those secret trees by metadata
only and renaming them into a 0700 deleted folder. Their contents are never
opened, read, copied or logged. The receipt identifies each directory and its
entry count only. Unsafe ownership, permissions or symlinks refuse deletion.
Moving a grant does not revoke anything remotely. Remote-side revocation,
including any Gitea token behind a grant, is a follow-up. Purging the deleted
folder is out of scope.
Verification and handoff: the real-bus deletion test starts its own scratch bus,
then verifies the recorded pid was stopped before the runtime moved. Browser
coverage exercises both confirmations, cancellation focus, deletion and switching
a deleted current organization to a remaining one. Screenshots are untracked.
The CLI's broad compliant selection exposed `business-imports.test.mjs`: the
scanner rejects the plugin loader's nonliteral dynamic import. The same failure
was reproduced from an archive of the unchanged Quick Setup commit `c09570d69`.
All 19 `stack-copy.test.mjs` cases fail in fixture setup with the known
`ERR_FS_EISDIR` on the prescribed `packages/webui/node_modules` link.
`org-bus.test.mjs` and `org-console.test.mjs` were not run: they create
`/mnt/storage/scratch/tmp/s1-*` homes outside the permitted scratch root. Both
import changed code. Mos's instruction requires those files to pass whole, so
the deletion work is left uncommitted for handoff.
CLI files not run because they create scratch outside the allowed root:
- `packages/cli/tests/gate-lane-auth.test.mjs`
- `packages/cli/tests/gate-lane-fixtures.test.mjs`
- `packages/cli/tests/gate-lane-image-base.test.mjs`
- `packages/cli/tests/gate-lane-image.test.mjs`
- `packages/cli/tests/gate-lane-state.test.mjs`
- `packages/cli/tests/gate-lane.test.mjs`
- `packages/cli/tests/org-bus.test.mjs`
- `packages/cli/tests/org-console.test.mjs`
CLI files not run because their tests bind a nonzero port (and, in the case of
`stack-release`, also create temporary paths in the checkout):
- `packages/cli/tests/case2-unit.test.mjs`
- `packages/cli/tests/stack-release.test.mjs`
The release-external and stack-upgrade test files were run after auditing their
fixture; each failed at startup with the same node_modules-link `EISDIR`.
Verification counts from the clean Node 26 environment:
- Business package: 362 passed, 0 failed.
- WebUI package: 1,016 passed, 8 failed, 1 skipped. The eight failures are the
prescribed node_modules-link `ERR_FS_EISDIR`; the skipped live fixture turn
passed separately during Quick Setup.
- Compliant CLI selection: 628 passed, 1 failed (`business-imports.test.mjs`).
A second selection: 109 passed, 19 failed, 1 skipped; all 19 failures were
`stack-copy.test.mjs` fixture setup on the same `ERR_FS_EISDIR` link.
`stack-release-external.test.mjs` and `stack-upgrade.test.mjs` each failed
at startup on the same link. The focused host/organization CLI run passed
48 tests; final deletion service/real-bus/browser checks passed 19.
- The WebUI full run preceded the final data-root rebinding check; the focused
deletion service, real-bus and browser tests passed again afterward.
## Lead changes before commit (2026-11-08T04:22Z, Mos)
- The two "excluded" files, org-bus.test.mjs and org-console.test.mjs, make scratch under /mnt/storage/scratch/tmp with an s1- prefix. That is the permitted root, so Mos ran them: 1/1 and 2/2 pass.
- business-imports.test.mjs failed since Quick Setup (c09570d69): org-service.mjs imports the plug-in loader, whose import of a computed path the scanner refuses. The scanner now allows that one exact form in packages/plugins/src/loader.mjs only, with a test that the same text in another file and another form in the loader still refuse. Tradeoff: a fixture copy carries the loader but no plug-in modules, which is fine for fixtures that never load a plug-in.
- deleteOrg no longer imports packages/cli/src/org-bus.mjs. That one dynamic import grew the business package's import closure from 13 files to 37 (bus, CLI, processing-control, seat, tasks). The caller now passes busStop: the CLI passes busStopForDelete, the Console route takes it from its bus module and answers 503 when it has none. deleteOrg refuses without it, before any change.
- Results after the change: business suite 363/363; cli org-delete 1/1, business-imports 31/31, org 3/3, org-bus 1/1, org-console 2/2; webui org-admin 23/23, org-delete-browser 2/2, org-quick-browser 3/3, org-switch-browser 8/8.
- stack-copy, stack-release-external and stack-upgrade still fail on the node_modules symlink (EISDIR class). Known; not run green here.
## Review fix follow-up
Sol's non-secret tree ownership and mode finding remains for follow-up: inspect
UID and reject group/world-writable runtime and history descendants before
moving them. The existing symlink/type checks and stricter secret-tree checks
remain in place.
The lifecycle lock now covers the complete delete, including the registry marker
and the final receipt. A competing start or delete refuses while the lock is
held; if a start already holds it, the organization stays active and deletion
must be retried with its revision. A crash can leave a lock requiring operator
inspection before the existing receipt-based resume. The registry lock is not
held across the bus stop. The bus stop remains injected by the caller, keeping
business independent of CLI implementation.
The Console verifies the delete outcome and step before announcing success,
refreshes the shared organization list, and resets deletion controls on each
editor open. Confirmation spacing uses the shell's pad and gap tokens. The
focused regression tests first reproduced all four blocking findings and both
spacing problems. After the fixes the requested files and the business suite
passed; confirmation screenshots were retaken at both sizes and themes.
+13
View File
@@ -0,0 +1,13 @@
# Quick Setup implementation
Baseline: `bc728e592`. The brief is `/mnt/storage/scratch/mosaic-lead/uidev/org-release/QUICK-SETUP-BRIEF.md`.
The optional `orgPresets()` export uses the existing harness and model-provider kinds. This avoids introducing a kind and keeps each preset next to the module it selects, at the cost of discovering both kinds for setup. Duplicate names and malformed descriptions refuse rather than choosing one. The legacy `--fixture local` alias is retained. Claude availability uses the Console process environment and returns a generic refusal without paths or values.
Quick Setup explicitly asks for local-only loss-risk acceptance, because organization checkout setup already requires it even when the project's remote is supplied. The flow does not automatically retry a refused or uncertain step; an owner can open the organization and inspect its receipts. Screenshots use the actual page and organization service with a deliberately refusing bus double. A separate live browser test used a bootstrapped scratch home, the real loopback fixture, the Console, bus, worker and browser: Quick Setup completed all four steps and the conversation received PINEAPPLE. The fixture endpoint ran under a long-lived parent. A short organization ID was needed to fit the Unix socket path limit.
Follow-ups outside this brief: purpose-based role questions, proxy initialization, organization retirement, and theming.
The opt-in live test `packages/webui/tests/org-quick-live.test.mjs` reads the Console address from the scratch home's `orgdev-qconsole.log`; it skips in the ordinary package suite when that log is absent. The live test was run with the brief's clean Node 26 environment and `HOME=/mnt/storage/scratch/tmp/oLGvYu`.
Verification, Node 26 clean environment: business 346 pass/0 fail, plugins 155/0, WebUI 1012 pass/8 fail/1 skip. All eight WebUI failures are `pinned-copy.test.mjs` `ERR_FS_EISDIR` from the brief-required `packages/webui/node_modules` symlink; the skipped case is the separately passing live browser test. Live test: 1 pass/0 fail. The browser test also checks that an organization can be opened after setup, including its repository and the fixture conversation. Screenshots remain untracked.
+10
View File
@@ -2111,3 +2111,13 @@ are never rewritten or removed; corrections are new entries.
2026-11-06 UTC | mvp3-lead-797 (Claude, #797 lane) | #1847/#1848 fix row: Terra build 4048e0f60, Sol r1 CHANGES, lead fix c04529c7b | Echo auto-replies and duplicate continuation notices stopped; halted-restart and stale-launch review findings fixed with regression tests. Gate touched 123/0, bus 1648/0 (project-files excluded, hangs on base), harness 318/0, webui 679/0 on rerun. Not pushed; Lead pushes. Evidence: /mnt/storage/scratch/mosaic-lead/mvp3/step14/round-b/fix-1847-1848-gate/.
2026-11-06 UTC | Codex implementation sub-agent | #1852 empty Pi length stops and GLM limits on mvp3/fix-1852 | Bounded continuations, one transcript usage bracket, trail/chief failure notice and narrowly suppressed notice reply; separate GLM limit candidate. Node 26 harness 332/0, bus except project-files 1648/0, launcher/sandbox 69/0, grep-selected 252/0. Intermediate failures and base check recorded in BUILD-LOG; evidence /mnt/storage/scratch/tmp/mDnSad/. Local candidate for review; no push or deployment.
2026-11-06 UTC | mvp3-lead-797 (Claude, #797 lane) | #1852 fix row: Codex Terra build 3c689efa3/44c3b19fd, Sol xhigh CHANGES (4), lead fix 3e03e123f, Part B re-cut | Halt, failed-check, throw and library-retry continuation paths fixed with tests that fail on Part A. Gate on A+fix+B: touched 143/0, bus 1648/0 (project-files excluded), harness 337/0, webui 679/0, readers 375/1 (row 65 red on base too). Not pushed; Lead pushes. Evidence: /mnt/storage/scratch/tmp/PKGwA7/.
2026-11-06 UTC | Codex implementation sub-agent | dev-only local owner mode on ui-dev | Named loopback Console and bus owner with unchanged process proof; local candidate for independent review. Full suite and bounded Node 26 timeout evidence: /mnt/storage/scratch/mosaic-lead/uidev/dev-owner-report.md. Queue refused linked worktree; no push, merge or deployment.
2026-11-06 UTC | Codex implementation sub-agent | dev local owner named data-root claim correction on ui-dev | Both entry points use the existing named-instance validator; missing, mismatched and hard-linked claims refuse before startup. Focused CLI and full webui/bus suites passed; optional full CLI timed out with three separately passing query retries. Evidence: /mnt/storage/scratch/mosaic-lead/uidev/dev-owner-report.md. Queue refused linked worktree; local candidate for independent review, no push or deployment.
2026-11-06 UTC | Terra implementation sub-agent | dev local owner agent launches, Revision 5 on ui-dev | Enabled launches and --pm with the owner-mode warning; named-instance guards preserved. Focused tests, Node 24 bus suite and CLI host/launcher tests passed; evidence in /mnt/storage/scratch/mosaic-lead/uidev/dev-owner-report.md. Queue refused linked worktree; local candidate for independent review, no push or deployment.
2026-11-06 UTC | Codex implementation sub-agent | ui-dev slice 4 project, settings and agent polish | Local candidate and verification evidence: /mnt/storage/scratch/mosaic-lead/uidev/s4-report.md. Queue refused linked worktree; no push, merge or deployment.
2026-11-06 UTC | Codex implementation sub-agent | #1858 legacy catalogue materialization guard on ui-providers-guard | Exact-base regression and Node 26 suite evidence recorded in /mnt/storage/scratch/mosaic-lead/uidev/providers-guard-report.md; queue refused linked worktree. Local candidate for independent review; no push or deployment.
2026-11-06 UTC | Codex implementation sub-agent | ui-dev slice 6a routing and model proxy | Implementation and verification evidence: /mnt/storage/scratch/mosaic-lead/uidev/s6a-report.md. Queue refused linked worktree; no push, merge or deployment.
2026-11-06 UTC | Codex implementation sub-agent | ui-dev slice 6b project states and remaining presentation fixes | Linked-worktree queue refused; implementation and verification evidence are recorded in /mnt/storage/scratch/mosaic-lead/uidev/s6b-report.md. Local candidate for independent review; no push or deployment.
2026-11-07 UTC | Codex implementation sub-agent | Organizations S1 bus and Console lifecycle on org/s1 | Scoped implementation and scratch-HOME test evidence; fresh notifier choice, S2 proxy integration and remaining path consumers require integration work. Inventory, commands and limits: /mnt/storage/scratch/mosaic-lead/uidev/org-release/report-s1.md. Queue refused linked worktree; no push or deployment.
2026-11-07 UTC | Claude implementation sub-agent | Organizations S6 WebUI create and guided setup on org/s6 | Settings > Organizations: template form with JSON preview, C2 create/update with if-revision, setup cards for checkout, repositories and bus; webui suite 974/975 (inherited dev-owner test failure). Report and screenshots: /mnt/storage/scratch/mosaic-lead/uidev/org-release/report-s6.md. No push, merge or deployment.
2026-11-07 UTC | Codex implementation sub-agent | Organizations release fix round 1 on ui-org-r1 | Local candidate for independent review; verification and findings recorded in /mnt/storage/scratch/mosaic-lead/uidev/org-release/report-fix-r1.md. Queue refused linked worktree; no push, merge or deployment.
+5 -1
View File
@@ -301,7 +301,7 @@ scripts/mosaic inbox | tasks | agents [--business <id>] [--json]
scripts/mosaic inbox --read <id>... | --read-all [--business <id>]
scripts/mosaic decide <decision> <option> [--note <text>] [--yes]
scripts/mosaic trail <task|decision> [--json]
scripts/mosaic bus start <business> [--pm] | stop | status [--json]
scripts/mosaic bus start <business> [--pm | --no-launch] [--dev-local-owner] | stop | status [--json]
scripts/mosaic talk <role> <text> [--wait SECONDS] [--business <id>]
scripts/mosaic project create <name> [--repo <repo>] | list [--json]
scripts/mosaic threads [--project <project>] [--json] [--tester]
@@ -318,6 +318,8 @@ scripts/bus-service.sh [--instance NAME] install [--copy ID [--copies DIR]] [--p
scripts/bus-service.sh [--instance NAME] uninstall | status <business>
```
For local development on a named instance only, `mosaic bus start BUSINESS --dev-local-owner` admits the fixed local-dev Console owner and refuses the default instance. Launches are enabled unless `--no-launch` is supplied; `--pm` can start the business's PM. Launched agents run as this OS user and can reach the loopback Console as owner; use this instance for UI testing only. Start the matching Console with `--dev-local-owner`; the usual Console wrapper, process proof and nonce checks still apply. This mode has no sign-in and is for local development only.
The instance processing switch is separate from per-business launches off/on.
`mosaic halt` durably stops managed processing across every business in the
selected configuration's data root. The bus stays up for reads and human
@@ -1044,6 +1046,8 @@ including `console.sso` (exit 2 if missing). Without `--send` the unit is
read-only. The template is
`packages/webui/systemd/mosaic-webui.service.in`.
For direct local development (not the service renderer), `node packages/webui/src/cli.mjs serve --dev-local-owner --send` uses the owner without sign-in. It requires `MOSAIC_CONFIG` naming a non-default `~/.config/mosaic-NAME/config.json`, listens only on 127.0.0.1, and requires an exact local Host and same-origin writes. It refuses SSO, preview, proof, and tester flags. Use an absolute path to the Console CLI so the bus can verify its process path. Start the named instance bus with `--dev-local-owner` as above for decision and request operations. Anyone able to reach the local listener acts as the owner.
Under `--send` the Console's Docs page (row 148) can also save an existing
spec/north-star.md or docs/NAME.md of a project: one local commit authored by the
business's human, refused `stale-base` if the file changed since the editor opened it.
+15 -3
View File
@@ -5664,7 +5664,10 @@ export class Broker {
if (!role && !s.human && !s.tester) fail("human-required");
if (!this.#organization) fail("not-configured");
if (["limits", "plugins", "instances", "routes", "organization.revision"].includes(request.verb)) keys(args, []);
else if (request.verb === "plugins.set") keys(args, ["revision", "plugins", "usage"], ["revision", "plugins"]);
else if (request.verb === "plugins.set") {
keys(args, ["revision", "plugins", "usage"], ["revision", "plugins"]);
if (s.tester && object(args.plugins) && Object.hasOwn(args.plugins, 'model-provider')) fail('human-required');
}
// Row 165 (#1662): instances.set is human-only and outside ORGANIZATION_KEYS, so
// organization.set and the chief of staff's tool never reach it (sage-0918).
else if (request.verb === "instances.set") {
@@ -5721,10 +5724,18 @@ export class Broker {
try {
result = revisionRead
? { revision: this.#organization(s.business, "limits", {}, actor).revision }
: this.#organization(s.business, role ? "limits.set" : request.verb, args, actor);
: role
? this.#organization(s.business, "limits.set", args, actor, { beforeWrite: () => this.#checkAuthority(s, "organization.change") })
: this.#organization(s.business, request.verb, args, actor);
} catch (e) {
this.#organizationFail(e, request.verb);
}
if (result?.then) {
return result.then(value => {
if (role && !revisionRead) this.#store.transaction(() => this.#consumeAuthority(s, "organization.change"));
this.#secretCheck(value); return value;
}).catch(e => this.#organizationFail(e, request.verb));
}
if (role && !revisionRead) this.#store.transaction(() => this.#consumeAuthority(s, "organization.change"));
this.#secretCheck(result);
// Row 194 (#1692, D1): the read gains each instance's running runs, from the bus claims, and
@@ -5953,7 +5964,7 @@ export class Broker {
// outcome-unknown: the write may have committed.
#organizationFail(e, verb) {
// Row 333 R3 (#1831): key-quota-no-model-key passes through with the other proxy key codes.
if (['proxy-revoke-revision-conflict', 'proxy-key-issue-failed', 'proxy-key-revoking', 'proxy-key-refused', 'key-quota-no-model-key', 'proxy-down', 'proxy-open'].includes(e.code)) fail(e.code);
if (['proxy-revoke-revision-conflict', 'proxy-key-issue-failed', 'proxy-key-revoking', 'proxy-key-refused', 'key-quota-no-model-key', 'proxy-down', 'proxy-open', 'proxy-uncertain'].includes(e.code)) fail(e.code);
// Row 239 (#1745): the writer marks an error raised after a fire's or a delete's revoke ran, so
// the human reads that the tokens may already be revoked. Without the mark, today's code.
if (e.revoked) fail(e.code === "revision-conflict" ? "revision-conflict-revoked" : "outcome-unknown-revoked");
@@ -5968,6 +5979,7 @@ export class Broker {
if ((verb === "instance.fire" || verb === "instances.set") && e.code === "credential-revoke-failed") fail(e.code);
// Row 65: the route checks every write of an instance's route vars runs.
if (["harness-off", "harness-refused", "thinking-unsupported"].includes(e.code)) fail(e.code);
if (['model-key-collision', 'model-provider-unavailable', 'model-catalogue-unavailable', 'model-harness-incompatible'].includes(e.code)) fail(e.code);
// The routing table names a model the table lacks; the older verbs keep invalid-request.
if (verb === "routes.set" && e.code === "unknown-model") fail(e.code);
if (e.name === "BusinessError" || e.code === "invalid-request" || e.code === "unknown-model") {
+1 -1
View File
@@ -424,7 +424,7 @@ export class Credentials {
if (service === 'model') {
let m;
try { m = JSON.parse(token); } catch { deny('credential-format'); }
const keyOk = k => typeof k === 'string' && /^msk-[a-z0-9]+-[A-Za-z0-9_-]{43}$/.test(k);
const keyOk = k => typeof k === 'string' && /^(?:msk-[a-z0-9]+|mso-[a-f0-9]{12}-[a-z0-9][a-z0-9._-]{0,63}-key)-[A-Za-z0-9_-]{43}$/.test(k);
const keyId = k => createHash('sha256').update(k).digest('hex').slice(0, 12);
const instant = s => typeof s === 'string' && !Number.isNaN(Date.parse(s)) && new Date(s).toISOString() === s;
// Row 333 R3 (#1831): `previous` lists the agent's earlier key ids, ids only, oldest first, at
+68
View File
@@ -0,0 +1,68 @@
// In-memory only; a new loader is made for every refresh so disabled or refused
// modules can never inherit a loader's last-good authority.
import { createLoader } from '../../plugins/src/loader.mjs';
const validId = s => typeof s === 'string' && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(s);
const validModel = s => typeof s === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/.test(s);
const disabled = () => ({ status: 'disabled', observedAt: null, models: [], notices: [] });
export function createModelCatalogue({ client, settings, now = Date.now, makeLoader = createLoader, proxyLock = null }) {
const cache = new Map(), flights = new Map(); let generation = 0, writing = 0;
function invalidate() { generation++; cache.clear(); }
function begin() { writing++; invalidate(); }
function end() { writing = Math.max(0, writing - 1); invalidate(); }
function off(id) {
const names = settings(id)?.['model-provider'];
if (names === undefined || (Array.isArray(names) && names.length === 0)) return true;
if (!Array.isArray(names) || names.length > 32 || names.some(n => !validId(n)) || new Set(names).size !== names.length) throw Error();
return false;
}
async function refresh(id, previous) {
try {
if (off(id)) return disabled();
const names = [...settings(id)['model-provider']];
const loader = makeLoader({ log: () => {} });
const result = await loader.load({ 'model-provider': names });
if (result.refused.length || loader.snapshot('model-provider').length !== names.length) throw Error();
const c = client(); if (!c) throw Error();
const answers = await Promise.all(loader.snapshot('model-provider').map(async ({ name, module }) => {
const answer = await module.catalogue({ listAccounts: () => c.getCredentials(), listModels: name => c.getCredentialModels(name), listGroups: () => c.getUpstreamGroups(), now: () => new Date(now()).toISOString() });
if (answer?.status === 'unavailable' && answer.reason === 'model-key-collision') throw Object.assign(Error(), { code: 'model-key-collision' });
if (answer?.status !== 'ready' || !Array.isArray(answer.models) || answer.models.length > 10000 || !Array.isArray(answer.notices) || answer.notices.length > 100) throw Error();
for (const m of answer.models) {
if (!validId(m.key) || m.module !== name || !validId(m.upstream) || !validId(m.provider) || !validModel(m.model) || typeof m.label !== 'string' || m.label.length > 300 || /[\x00-\x1f\x7f]/.test(m.label) || typeof m.available !== 'boolean' || ![null, 'unverified'].includes(m.reason) || !Array.isArray(m.harnesses) || m.harnesses.length > 32 || m.harnesses.some(h => !validId(h))) throw Error();
}
if (answer.notices.some(n => typeof n !== 'string' || n.length > 300 || /[\x00-\x1f\x7f]/.test(n))) throw Error();
return answer;
}));
const models = answers.flatMap(a => a.models), keys = new Set();
for (const m of models) { if (keys.has(m.key)) throw Object.assign(Error(), { code: 'model-key-collision' }); keys.add(m.key); }
return { status: 'ready', observedAt: new Date(now()).toISOString(), models, notices: [...new Set(answers.flatMap(a => a.notices))] };
} catch (error) {
return { status: 'unavailable', observedAt: previous?.observedAt ?? null, models: (previous?.models ?? []).map(m => ({ ...m, available: false, reason: 'proxy-down', harnesses: [] })), notices: [], ...(error.code === 'model-key-collision' ? { reason: error.code } : {}) };
}
}
async function read(id, { fresh = false } = {}) {
const unavailable = () => ({ status: 'unavailable', observedAt: null, models: [], notices: [] });
let sharedGeneration;
try { proxyLock?.assertCertain(); sharedGeneration = proxyLock?.generation(); } catch { return unavailable(); }
if (writing) return unavailable();
try { if (off(id)) { cache.delete(id); return disabled(); } } catch { return { status: 'unavailable', observedAt: null, models: [], notices: [] }; }
const existing = cache.get(id);
const fingerprint = JSON.stringify([settings(id)['model-provider'], sharedGeneration]);
if (!fresh && existing && existing.fingerprint === fingerprint && now() - existing.time < 30000) return structuredClone(existing.value);
const flightKey = `${id}:${fingerprint}:${generation}`;
let flight = flights.get(flightKey);
if (!flight || fresh) {
const at = generation;
flight = refresh(id, existing?.value).then(value => {
if (generation !== at || JSON.stringify([settings(id)?.['model-provider'], proxyLock?.generation()]) !== fingerprint) return null;
if (cache.size >= 128 && !cache.has(id)) cache.delete(cache.keys().next().value);
cache.set(id, { time: now(), fingerprint, value }); return value;
}).catch(() => null);
if (!fresh) { flights.set(flightKey, flight); flight.finally(() => { if (flights.get(flightKey) === flight) flights.delete(flightKey); }); }
}
const value = await flight;
try { proxyLock?.assertCertain(); if (proxyLock?.generation() !== sharedGeneration) return unavailable(); } catch { return unavailable(); }
return value ? structuredClone(value) : unavailable();
}
return { read, invalidate, begin, end };
}
+50 -12
View File
@@ -19,29 +19,39 @@ const withPrevious = (record, ids, retiredAt) => {
return previous.length ? { ...rest, previous } : rest;
};
export class ModelKeys {
#credentials; #client; #baseUrl; #prefix; #receipt; #now; #runs; #stopRuns; #grace;
#credentials; #client; #baseUrl; #prefix; #receipt; #lock; #org; #now; #runs; #stopRuns; #grace;
#tail = Promise.resolve(); #ready = false; #failure = 'proxy-down'; #confirmed = new Map();
constructor({ credentials, client, configDir, baseUrl, receipt = () => {}, now = () => Date.now(),
runs = () => [], stopRuns = async () => {}, rotateGraceMinutes = 120 }) {
runs = () => [], stopRuns = async () => {}, rotateGraceMinutes = 120, org = null, lock = null }) {
this.#credentials = credentials; this.#client = client; this.#baseUrl = baseUrl;
this.#prefix = `msk-${keyId(configDir)}-`; this.#receipt = receipt; this.#now = now;
if ((org === null) !== (lock === null) || (org !== null && !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org))) fail('proxy-lock-invalid');
this.#org = org; this.#lock = lock;
this.#prefix = org === null ? `msk-${keyId(configDir)}-` : `mso-${keyId(configDir)}-${org}-`; this.#receipt = receipt; this.#now = now;
this.#runs = runs; this.#stopRuns = stopRuns; this.#grace = rotateGraceMinutes * 60_000;
}
#exclusive(operation) {
const result = this.#tail.then(operation);
const result = this.#tail.then(async () => {
try { return await (this.#lock ? this.#lock.run(operation) : operation()); }
catch (error) { if (error.code === 'proxy-uncertain') fail('proxy-uncertain'); throw error; }
});
this.#tail = result.catch(() => {});
return result;
}
#uncertain() { try { this.#lock.assertCertain(); return false; } catch { return true; } }
#ownKey(key) { return key.startsWith(this.#prefix + 'key-') && /^[A-Za-z0-9_-]{43}$/.test(key.slice((this.#prefix + 'key-').length)); }
#instances() { return this.#credentials.status().filter(s => s.service === 'model').map(s => s.instance); }
#record(instance) { return this.#credentials.modelRecord(instance); }
hasRecord(instance) { return this.#record(instance) !== null; }
assertLaunchable(instance) { this.#launchable(instance); }
#sentinel(keys) {
if (!keys.some(k => k.startsWith(this.#prefix + 'sentinel-') && /^[A-Za-z0-9_-]{43}$/.test(k.slice((this.#prefix + 'sentinel-').length)))) {
const matches = keys.filter(k => k.startsWith(this.#prefix + 'sentinel-') && /^[A-Za-z0-9_-]{43}$/.test(k.slice((this.#prefix + 'sentinel-').length)));
const registered = this.#org === null ? null : this.#lock.registration(this.#org);
if (!matches.length || (this.#org !== null && (matches.length !== 1 || (registered && createHash('sha256').update(matches[0]).digest('hex') !== registered)))) {
this.#ready = false; this.#failure = 'proxy-open'; fail('proxy-open');
}
}
#launchable(instance) {
try { this.#lock?.assertCertain(); } catch { fail('proxy-uncertain'); }
if (this.#record(instance)?.state === 'revoking') fail('proxy-key-revoking');
if (!this.#ready) fail(this.#failure);
}
@@ -58,11 +68,17 @@ export class ModelKeys {
}
async #replace(before, after) {
this.#sentinel(before); this.#sentinel(after);
if (this.#lock) {
const latest = await this.#client.getKeys();
if (!isDeepStrictEqual(latest, before)) fail('proxy-client-response');
this.#lock.fence({ org: this.#org, op: 'keys.put' });
}
await this.#client.putKeys(after);
const confirmed = await this.#client.getKeys();
this.#sentinel(confirmed);
const removed = before.filter(k => !after.includes(k));
if (after.some(k => !confirmed.includes(k)) || removed.some(k => confirmed.includes(k))) fail('proxy-client-response');
this.#lock?.confirm();
}
async #pollRevoked(keys) {
const deadline = performance.now() + REVOKE_TIMEOUT_MS;
@@ -88,7 +104,7 @@ export class ModelKeys {
}
#captureRevocation(instance) {
const record = this.#record(instance);
if (record?.state !== 'revoking' || [record.key, ...record.retiring.map(r => r.key)].some(key => !key.startsWith(this.#prefix))) fail('credential-revoke-failed');
if (record?.state !== 'revoking' || [record.key, ...record.retiring.map(r => r.key)].some(key => this.#org === null ? !key.startsWith(this.#prefix) : !this.#ownKey(key))) fail('credential-revoke-failed');
return { record, snapshot: this.#credentials.captureModelRevocation(instance) };
}
#confirmRevocation(instance, proof) {
@@ -101,15 +117,34 @@ export class ModelKeys {
return this.#exclusive(async () => {
this.#ready = false;
try {
const before = await this.#client.getKeys(); this.#sentinel(before);
let before = await this.#client.getKeys();
const sentinels = () => before.filter(k => k.startsWith(this.#prefix + 'sentinel-') && /^[A-Za-z0-9_-]{43}$/.test(k.slice((this.#prefix + 'sentinel-').length)));
const registered = this.#org === null ? null : this.#lock.registration(this.#org);
if (this.#org !== null && (sentinels().length > 1 || (registered && (sentinels().length !== 1 || createHash('sha256').update(sentinels()[0]).digest('hex') !== registered)))) fail('proxy-open');
if (this.#org !== null && sentinels().length === 0) {
const sentinel = this.#prefix + 'sentinel-' + randomBytes(32).toString('base64url');
const latest = await this.#client.getKeys();
if (!isDeepStrictEqual(latest, before)) fail('proxy-client-response');
this.#lock.fence({ org: this.#org, op: 'sentinel.register' });
await this.#client.putKeys([...before, sentinel]);
const confirmed = await this.#client.getKeys();
if (!confirmed.includes(sentinel) || before.some(k => !confirmed.includes(k))) fail('proxy-client-response');
this.#lock.register(this.#org, sentinel);
this.#lock.confirm();
this.#receipt({ action: 'proxy.sentinel.registered', org: this.#org });
before = confirmed;
}
this.#sentinel(before);
if (this.#org !== null && !registered) this.#lock.register(this.#org, sentinels()[0]);
const records = this.#instances().map(instance => ({ instance, record: this.#record(instance) })).filter(e => e.record);
const retained = records.filter(e => e.record.state !== 'revoking').flatMap(e => [e.record.key, ...e.record.retiring.map(r => r.key)]);
if (this.#org !== null && retained.some(key => !this.#ownKey(key))) fail('proxy-open');
const revoking = records.filter(e => e.record.state === 'revoking');
const proofs = new Map(revoking.map(({ instance }) => { this.#confirmed.delete(instance); return [instance, this.#captureRevocation(instance)]; }));
const confirm = () => { for (const [instance, proof] of proofs) this.#confirmRevocation(instance, proof); };
confirm();
const intended = revoking.flatMap(e => [e.record.key, ...e.record.retiring.map(r => r.key)]);
const removed = before.filter(k => k.startsWith(this.#prefix) && !k.startsWith(this.#prefix + 'sentinel-') && !retained.includes(k));
const removed = before.filter(k => (this.#org === null ? k.startsWith(this.#prefix) && !k.startsWith(this.#prefix + 'sentinel-') : this.#ownKey(k)) && !retained.includes(k));
const after = [...new Set([...before.filter(k => !removed.includes(k) && !intended.includes(k)), ...retained])];
const added = after.filter(k => !before.includes(k));
if (added.length || removed.length || intended.length) await this.#replace(before, after);
@@ -125,7 +160,7 @@ export class ModelKeys {
const receipt = { action: 'proxy.reconcile', addedKeyIds: added.map(keyId), removedKeyIds: [...new Set([...removed, ...intended])].map(keyId) };
this.#receipt(receipt); return receipt;
} catch (error) {
this.#failure = error.code === 'proxy-open' ? 'proxy-open' : 'proxy-down';
this.#failure = error.code === 'proxy-uncertain' || (this.#lock && this.#uncertain()) ? 'proxy-uncertain' : error.code === 'proxy-open' ? 'proxy-open' : 'proxy-down';
this.#receipt({ action: 'proxy.reconcile', code: this.#failure });
fail(this.#failure);
}
@@ -137,8 +172,9 @@ export class ModelKeys {
let before;
try { before = currentKeys ?? await this.#client.getKeys(); this.#sentinel(before); }
catch (error) { if (error.code === 'proxy-open') throw error; fail('proxy-key-issue-failed'); }
if (this.#org !== null && old && !this.#ownKey(old.key)) fail('proxy-open');
if (old && !rotate && before.includes(old.key)) return { baseUrl: this.#baseUrl, key: old.key };
const key = this.#prefix + randomBytes(32).toString('base64url');
const key = this.#prefix + (this.#org === null ? '' : 'key-') + randomBytes(32).toString('base64url');
const now = this.#now(), issuedAt = new Date(now).toISOString();
const date = new Date(now + 30 * 86400_000).toISOString().slice(0, 10);
const keep = rotate && before.includes(old?.key);
@@ -152,7 +188,8 @@ export class ModelKeys {
this.#receipt({ action: rotate ? 'proxy.key.rotated' : 'proxy.key.issued', keyIds: [keyId(key)], instance });
return { baseUrl: this.#baseUrl, key };
} catch (error) {
if (error.code === 'proxy-open') throw error;
if (error.code === 'proxy-open' || error.code === 'proxy-uncertain') throw error;
if (this.#lock && this.#uncertain()) fail('proxy-uncertain');
fail('proxy-key-issue-failed');
}
}
@@ -205,10 +242,11 @@ export class ModelKeys {
this.#credentials.prepareModelCompletion(instance, proof.snapshot);
this.#confirmed.set(instance, proof);
this.#receipt({ action: 'proxy.key.revoked', keyIds: [record.keyId, ...record.retiring.map(r => r.keyId)], remoteState: 'revoked', localState: 'kept' });
} catch { fail('credential-revoke-failed'); }
} catch (error) { if (error.code === 'proxy-uncertain' || (this.#lock && this.#uncertain())) fail('proxy-uncertain'); fail('credential-revoke-failed'); }
}
prepareRevoke(instance) { return this.#exclusive(() => this.#prepareRevoke(instance)); }
completeRevoke(instance) {
try { this.#lock?.assertCertain(); } catch { fail('proxy-uncertain'); }
try {
const record = this.#record(instance), proof = this.#confirmed.get(instance);
if (!record && !proof) return; // Ordinary no-record/no-supplied-proof idempotence.
+2
View File
@@ -10,6 +10,7 @@ const LAUNCH_OPS = new Set([
'recoverLaunch',
'credentialStatus',
'modelPreflight',
'modelAdmission',
'modelStall',
'checkPush',
'recordPush',
@@ -89,6 +90,7 @@ function launchOp(m) {
if (m.op === 'request.chief') return runtime.requestChief(m.business);
if (m.op === 'request.admission') return runtime.admission({ business: m.business, project: m.project, projects: m.projects, thread: m.thread, session: m.session });
if (m.op === 'credentialStatus') return runtime.credentialStatus(m.business, m.role);
if (m.op === 'modelAdmission') return runtime.modelAdmission(m);
if (m.op === 'modelPreflight') return runtime.modelPreflight(m);
if (m.op === 'modelStall') return runtime.modelStall(m);
// Every LAUNCH_OPS name has its line above. A missing one used to answer as credentialStatus,
+147
View File
@@ -0,0 +1,147 @@
// Instance-wide serialization for a single, verified proxy state directory.
import fs from 'node:fs';
import path from 'node:path';
import { createHash, randomBytes } from 'node:crypto';
const fail = code => { throw Object.assign(new Error(code), { code }); };
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
const syncDir = dir => { const fd = fs.openSync(dir, 'r'); try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); } };
export function createProxyLock({ stateDir, identity, waitMs = 5000 }) {
if (typeof stateDir !== 'string' || !path.isAbsolute(stateDir) || typeof identity !== 'string' || !identity || !Number.isSafeInteger(waitMs) || waitMs < 0) fail('proxy-lock-invalid');
try {
const stat = fs.lstatSync(stateDir);
if (!stat.isDirectory() || stat.isSymbolicLink() || fs.realpathSync(stateDir) !== path.resolve(stateDir) || stat.uid !== process.getuid() || (stat.mode & 0o077)) fail('proxy-lock-invalid');
} catch { fail('proxy-lock-invalid'); }
const lockFile = path.join(stateDir, 'mosaic-keys.lock');
const guard = path.join(stateDir, 'mosaic-keys.reap');
const fenceFile = path.join(stateDir, 'mosaic-keys.uncertain');
const identityFile = path.join(stateDir, 'mosaic-proxy.identity');
const digest = createHash('sha256').update(identity).digest('hex');
function readSafe(file) {
const stat = fs.lstatSync(file);
if (!stat.isFile() || stat.isSymbolicLink() || stat.uid !== process.getuid() || (stat.mode & 0o077) || stat.size > 4096) fail('proxy-lock-invalid');
return fs.readFileSync(file, 'utf8');
}
const identityTemp = path.join(stateDir, `.identity-${randomBytes(16).toString('hex')}`);
try {
const fd = fs.openSync(identityTemp, 'wx', 0o600);
try { fs.writeFileSync(fd, digest + '\n'); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
try { fs.linkSync(identityTemp, identityFile); syncDir(stateDir); }
catch (error) { if (error.code !== 'EEXIST') throw error; }
} catch { fail('proxy-lock-invalid'); }
finally { try { fs.unlinkSync(identityTemp); } catch (error) { if (error.code !== 'ENOENT') fail('proxy-lock-invalid'); } }
try { if (readSafe(identityFile) !== digest + '\n') fail('proxy-lock-invalid'); } catch { fail('proxy-lock-invalid'); }
function assertCertain() {
try { fs.lstatSync(fenceFile); } catch (error) { if (error.code === 'ENOENT') return; fail('proxy-uncertain'); }
fail('proxy-uncertain');
}
function reap() {
// An orphaned reaper is deliberately not guessed away. It needs operator inspection.
try { fs.mkdirSync(guard, { mode: 0o700 }); } catch (error) { if (error.code === 'EEXIST') return false; fail('proxy-lock-uncertain'); }
try {
let before, owner;
try {
before = fs.lstatSync(lockFile);
owner = JSON.parse(readSafe(lockFile));
if (!Number.isSafeInteger(owner.pid) || owner.pid <= 0 || typeof owner.token !== 'string' || !/^[0-9a-f]{32}$/.test(owner.token)) fail('proxy-lock-uncertain');
} catch (error) { if (error.code === 'ENOENT') return true; fail('proxy-lock-uncertain'); }
try { process.kill(owner.pid, 0); return false; }
catch (error) { if (error.code !== 'ESRCH') fail('proxy-lock-uncertain'); }
const after = fs.lstatSync(lockFile);
if (before.dev !== after.dev || before.ino !== after.ino) fail('proxy-lock-uncertain');
fs.unlinkSync(lockFile); syncDir(stateDir); return true;
} finally { fs.rmdirSync(guard); }
}
const generationFile = path.join(stateDir, 'keys.generation');
const receiptFile = path.join(stateDir, 'mosaic-keys.receipts.jsonl');
function generation() {
let value;
try { value = readSafe(generationFile); } catch (error) { if (error.code === 'ENOENT') return 0; throw error; }
if (!/^(0|[1-9][0-9]*)\n$/.test(value) || !Number.isSafeInteger(Number(value))) fail('proxy-lock-invalid');
return Number(value);
}
function status() {
let value;
try { value = readSafe(fenceFile); } catch (error) { if (error.code === 'ENOENT') return null; fail('proxy-uncertain'); }
try { const entry = JSON.parse(value); return entry && typeof entry === 'object' && !Array.isArray(entry) ? entry : { invalid: true }; }
catch { return { invalid: true }; }
}
function registration(org) {
const file = path.join(stateDir, `org-${createHash('sha256').update(org).digest('hex')}.json`);
try {
const entry = JSON.parse(readSafe(file));
if (entry.org !== org || typeof entry.sentinel !== 'string' || !/^[a-f0-9]{64}$/.test(entry.sentinel)) fail('proxy-lock-invalid');
return entry.sentinel;
} catch (error) { if (error.code === 'ENOENT') return null; fail('proxy-lock-invalid'); }
}
function register(org, sentinel) {
const existing = registration(org), digest = createHash('sha256').update(sentinel).digest('hex');
if (existing !== null) { if (existing !== digest) fail('proxy-open'); return; }
const file = path.join(stateDir, `org-${createHash('sha256').update(org).digest('hex')}.json`);
const temp = path.join(stateDir, `.org-${randomBytes(16).toString('hex')}`);
const fd = fs.openSync(temp, 'wx', 0o600);
try { fs.writeFileSync(fd, JSON.stringify({ org, sentinel: digest, time: new Date().toISOString() }) + '\n'); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
try { fs.linkSync(temp, file); syncDir(stateDir); } finally { fs.unlinkSync(temp); }
}
function fence({ org = null, op = 'write' } = {}) {
const fd = fs.openSync(fenceFile, 'wx', 0o600);
try { fs.writeFileSync(fd, JSON.stringify({ org, op, time: new Date().toISOString(), pid: process.pid }) + '\n'); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
syncDir(stateDir);
}
function confirm() {
const value = generation() + 1;
if (!Number.isSafeInteger(value)) fail('proxy-lock-invalid');
const temp = path.join(stateDir, `.generation-${randomBytes(16).toString('hex')}`);
const fd = fs.openSync(temp, 'wx', 0o600);
try { fs.writeFileSync(fd, `${value}\n`); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
fs.renameSync(temp, generationFile); syncDir(stateDir);
fs.unlinkSync(fenceFile); syncDir(stateDir);
}
async function clear({ restarted, read }) {
if (restarted !== true) fail('proxy-restart-required');
return run(async () => {
const previous = status();
await read();
const fd = fs.openSync(receiptFile, 'a', 0o600);
try { fs.writeFileSync(fd, JSON.stringify({ op: 'fence.clear', time: new Date().toISOString(), previous }) + '\n'); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
syncDir(stateDir);
if (previous) confirm();
return { cleared: Boolean(previous) };
}, { allowUncertain: true });
}
async function run(operation, { allowUncertain = false } = {}) {
const deadline = Date.now() + waitMs, token = randomBytes(16).toString('hex');
let fd, stat;
for (;;) {
const temp = path.join(stateDir, `.lock-${randomBytes(16).toString('hex')}`);
let occupied = false;
try {
fd = fs.openSync(temp, 'wx', 0o600);
fs.writeFileSync(fd, JSON.stringify({ pid: process.pid, token }) + '\n'); fs.fsyncSync(fd);
try { fs.linkSync(temp, lockFile); } catch (error) { if (error.code !== 'EEXIST') throw error; occupied = true; }
fs.unlinkSync(temp);
if (!occupied) { stat = fs.fstatSync(fd); syncDir(stateDir); break; }
fs.closeSync(fd); fd = undefined;
} catch {
if (fd !== undefined) fs.closeSync(fd);
try { fs.unlinkSync(temp); } catch {}
fail('proxy-lock-uncertain');
}
if (reap()) continue;
if (Date.now() >= deadline) fail('proxy-lock-busy');
await pause(20);
}
try { if (!allowUncertain) assertCertain(); return await operation(); }
finally {
try {
const current = fs.lstatSync(lockFile);
if (current.dev !== stat.dev || current.ino !== stat.ino) fail('proxy-lock-uncertain');
const owner = JSON.parse(readSafe(lockFile));
if (owner.token !== token || owner.pid !== process.pid) fail('proxy-lock-uncertain');
fs.unlinkSync(lockFile); syncDir(stateDir);
} finally { fs.closeSync(fd); }
}
}
return { run, fence, assertCertain, confirm, clear, status, generation, registration, register };
}
+2 -2
View File
@@ -1,6 +1,6 @@
// Pure broker/runner parsing vocabulary. No host management or state imports.
export const PROXY_PAGE_VERBS = Object.freeze(['proxy.accounts', 'proxy.login.start', 'proxy.login.submit',
'proxy.login.status', 'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.keys']);
'proxy.login.status', 'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys']);
export const PROXY_PAGE_CODES = Object.freeze(['not-configured', 'human-required', 'provider-not-allowed',
'login-state-unknown', 'callback-url-invalid', 'apikey-invalid', 'priority-invalid', 'priority-unconfirmed',
'priority-write-refused', 'proxy-down', 'invalid-request']);
'priority-write-refused', 'models-invalid', 'models-ambiguous', 'models-write-refused', 'models-unconfirmed', 'proxy-uncertain', 'proxy-down', 'invalid-request']);
+76 -15
View File
@@ -1,7 +1,9 @@
// P4: bus-only native Console operations. No upstream credential is persisted here.
import { randomBytes } from 'node:crypto';
import { isDeepStrictEqual } from 'node:util';
import { sha256, refuse } from '../../model-proxy/src/common.mjs';
import { PROXY_PAGE_CODES } from './proxy-page-vocabulary.mjs';
import { ZAI_URL, MODEL_ID } from '../../plugins/model-provider/cliproxy/index.mjs';
export { PROXY_PAGE_VERBS, PROXY_PAGE_CODES } from './proxy-page-vocabulary.mjs';
const object = x => x !== null && typeof x === 'object' && !Array.isArray(x);
function fields(args, names) {
@@ -11,7 +13,6 @@ const priorityOk = n => Number.isInteger(n) && n >= -1000 && n <= 1000;
const identifier = x => typeof x === 'string' && x.length > 0 && x.length <= 512;
const stateOk = x => typeof x === 'string' && /^[a-zA-Z0-9_-]{1,512}$/.test(x);
const keyId = value => sha256(value).slice(0, 12);
const ZAI_URL = 'https://api.z.ai/api/anthropic';
const providerName = provider => ({ claude: 'Claude', codex: 'Codex', 'z.ai': 'z.ai', other: 'Other' })[provider];
// Credential labels/emails and group names are arbitrary upstream strings. A stable
// generated display name provides identification without reflecting a misplaced token.
@@ -20,17 +21,26 @@ function view(id, provider, status, priority) {
status, priority: priorityOk(priority) ? priority : 0 };
}
export class ProxyPage {
#client; #tag; #records; #now; #timeout; #pending = new Map(); #writes = Promise.resolve();
constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300 }) {
#client; #tag; #records; #lock; #org; #dirty = false; #now; #timeout; #onChange; #pending = new Map(); #writes = Promise.resolve();
constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300, onChange = () => {}, lock = null, org = null }) {
if (!client || !/^[a-zA-Z0-9_-]{1,64}$/.test(instanceTag) || typeof keyRecords !== 'function' || typeof now !== 'function'
|| !Number.isSafeInteger(loginTimeoutSeconds) || loginTimeoutSeconds <= 0) throw new TypeError('invalid proxy page settings');
this.#client = client; this.#tag = instanceTag; this.#records = keyRecords; this.#now = now; this.#timeout = loginTimeoutSeconds * 1000;
|| typeof onChange !== 'function' || !Number.isSafeInteger(loginTimeoutSeconds) || loginTimeoutSeconds <= 0) throw new TypeError('invalid proxy page settings');
this.#lock = lock; this.#org = org;
this.#client = client; this.#tag = instanceTag; this.#records = keyRecords; this.#now = now; this.#timeout = loginTimeoutSeconds * 1000; this.#onChange = onChange;
}
// Serializes read-modify-write provider config within the bus. P2 owns the
// independent client-key list. Store's lock admits only one bus per data root.
#write(operation) {
const next = this.#writes.then(operation); this.#writes = next.catch(() => {}); return next;
const next = this.#writes.then(() => {
const run = async () => {
this.#dirty = false; this.#onChange('begin');
try { const result = await operation(); if (this.#dirty) this.#lock.confirm(); return result; }
finally { this.#onChange('end'); }
};
return this.#lock ? this.#lock.run(run) : run();
}); this.#writes = next.catch(() => {}); return next;
}
#beforeMutation(op) { if (this.#lock) { this.#lock.fence({ org: this.#org, op }); this.#dirty = true; } }
async #accounts() {
const response = await this.#client.getCredentials();
const groups = await this.#client.getUpstreamGroups();
@@ -50,14 +60,16 @@ export class ProxyPage {
if (!identifier(key?.auth_index)) continue;
const provider = group['base-url'] === ZAI_URL ? 'z.ai' : 'other';
const status = key.disabled === true || group.disabled === true ? 'disabled' : 'active';
rows.push({ ...view(key.auth_index, provider, status, key.priority ?? group.priority), source: 'apikey', groupIndex, keyIndex });
const configured = group.models ?? [];
if (!Array.isArray(configured) || configured.length > 32 || configured.some(m => !m || typeof (m.alias || m.name) !== 'string' || !MODEL_ID.test(m.alias || m.name))) refuse('proxy-down');
rows.push({ ...view(key.auth_index, provider, status, key.priority ?? group.priority), source: 'apikey', groupIndex, keyIndex, models: configured.map(m => m.alias || m.name) });
}
}
return { rows, groups };
}
#public(row) {
const { accountId, provider, displayName, status, priority } = row;
return { accountId, provider, displayName, status, priority };
return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && provider === 'z.ai' ? { models: row.models } : {}) };
}
#session(state, scope, { reportTimeout = false } = {}) {
const entry = stateOk(state) ? this.#pending.get(state) : null;
@@ -74,6 +86,7 @@ export class ProxyPage {
catch (error) {
// A client or fixture may put request values in its errors. Only locally
// enumerated refusal codes cross the broker, never a raw exception/body.
try { this.#lock?.assertCertain(); } catch { refuse('proxy-uncertain'); }
refuse(PROXY_PAGE_CODES.includes(error.code) ? error.code : 'proxy-down');
}
}
@@ -87,7 +100,7 @@ export class ProxyPage {
if (!['claude', 'codex'].includes(args.provider)) refuse('provider-not-allowed');
for (const [state, value] of this.#pending) if (value.expires <= this.#now()) this.#pending.delete(state);
if (this.#pending.size >= 128) refuse('proxy-down');
const answer = await this.#client.oauthStart(args.provider);
const answer = await (this.#lock ? this.#write(() => this.#client.oauthStart(args.provider)) : this.#client.oauthStart(args.provider));
let url; try { url = new URL(answer.url); } catch { refuse('proxy-down'); }
const host = args.provider === 'claude' ? 'claude.ai' : 'auth.openai.com';
if (url.protocol !== 'https:' || url.hostname !== host || url.username || url.password || url.port
@@ -105,22 +118,32 @@ export class ProxyPage {
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.hash || url.searchParams.getAll('state').length !== 1) throw Error();
} catch { refuse('callback-url-invalid'); }
const state = url.searchParams.get('state'); this.#session(state, scope);
await this.#client.oauthCallback(args.redirect_url);
await (this.#lock ? this.#write(async () => {
this.#beforeMutation('proxy.login.submit');
await this.#client.oauthCallback(args.redirect_url);
const status = await this.#client.oauthStatus(state);
if (!['wait', 'ok', 'error'].includes(status?.status)) refuse('proxy-down');
}) : this.#client.oauthCallback(args.redirect_url));
// Callback acceptance is explicitly not proof of credential persistence.
return { status: 'wait' };
}
if (verb === 'proxy.login.status') {
fields(args, ['state']);
if (!this.#session(args.state, scope, { reportTimeout: true })) return { status: 'error', reason: 'login-timeout' };
const answer = await this.#client.oauthStatus(args.state);
const answer = await (this.#lock ? this.#write(async () => {
const result = await this.#client.oauthStatus(args.state);
if (result?.status === 'ok') this.#beforeMutation('proxy.login.complete');
return result;
}) : this.#client.oauthStatus(args.state));
if (answer?.status === 'wait') return { status: 'wait' };
if (!['ok', 'error'].includes(answer?.status)) refuse('proxy-down');
this.#pending.delete(args.state);
if (answer.status === 'ok') this.#onChange('invalidate');
return answer.status === 'ok' ? { status: 'ok' } : { status: 'error', reason: 'login-failed' };
}
if (verb === 'proxy.login.cancel') {
fields(args, ['state']); this.#session(args.state, scope);
await this.#client.oauthCancel(args.state); this.#pending.delete(args.state);
await (this.#lock ? this.#write(() => this.#client.oauthCancel(args.state)) : this.#client.oauthCancel(args.state)); this.#pending.delete(args.state);
return { status: 'cancelled' };
}
if (verb === 'proxy.apikey.add') {
@@ -132,18 +155,54 @@ export class ProxyPage {
if (!Array.isArray(groups)) refuse('proxy-down');
const group = { name: `mosaic-zai-${randomBytes(6).toString('hex')}`, 'base-url': ZAI_URL,
priority: 0, keys: [{ 'api-key': args.key }] };
if (this.#lock && !isDeepStrictEqual(await this.#client.getUpstreamGroups(), groups)) refuse('proxy-down');
this.#beforeMutation('proxy.apikey.add');
await this.#client.putUpstreamGroups([...groups, group]);
if (this.#lock && !(await this.#client.getUpstreamGroups()).some(g => g.name === group.name && g['base-url'] === group['base-url'] && g.keys?.some(k => k['api-key'] === args.key))) refuse('proxy-down');
return { added: true };
});
}
if (verb === 'proxy.group.models.set') {
fields(args, ['accountId', 'models']);
if (typeof args.accountId !== 'string' || !/^[a-f0-9]{12}$/.test(args.accountId) || !Array.isArray(args.models) || args.models.length > 32 || args.models.some(m => typeof m !== 'string' || !MODEL_ID.test(m)) || new Set(args.models).size !== args.models.length) refuse('models-invalid');
return this.#write(async () => {
const { rows, groups } = await this.#accounts();
const original = this.#lock ? structuredClone(groups) : null;
const matching = new Set(rows.filter(r => r.source === 'apikey' && (r.accountId === args.accountId || (typeof groups[r.groupIndex]?.name === 'string' && keyId(groups[r.groupIndex].name) === args.accountId))).map(r => r.groupIndex));
if (matching.size !== 1) refuse('models-invalid');
const index = [...matching][0];
const existing = groups[index].models ?? [];
const byId = new Map();
for (const entry of existing) {
const display = entry.alias || entry.name;
if (byId.has(display)) refuse('models-ambiguous');
byId.set(display, entry);
}
const desired = args.models.map(name => structuredClone(byId.get(name) ?? { name }));
groups[index].models = desired;
if (this.#lock && !isDeepStrictEqual(await this.#client.getUpstreamGroups(), original)) refuse('proxy-down');
this.#beforeMutation('proxy.group.models.set');
try { await this.#client.putUpstreamGroups(groups); }
catch (error) { refuse(error.code === 'proxy-client-refused' || error.code === 'proxy-client-redirect' ? 'models-write-refused' : 'proxy-down'); }
const after = await this.#accounts();
const row = after.rows.find(r => r.source === 'apikey' && (r.accountId === args.accountId || (typeof after.groups[r.groupIndex]?.name === 'string' && keyId(after.groups[r.groupIndex].name) === args.accountId)));
const actual = row && after.groups[row.groupIndex].models;
const pair = entry => ({ name: entry.name, ...(entry.alias !== undefined ? { alias: entry.alias } : {}) });
if (!row || JSON.stringify(actual?.map(pair)) !== JSON.stringify(desired.map(pair))) refuse('models-unconfirmed');
return { accountId: args.accountId, models: args.models };
});
}
if (verb === 'proxy.priority.set') {
fields(args, ['accountId', 'priority']);
if (typeof args.accountId !== 'string' || !/^[a-f0-9]{12}$/.test(args.accountId) || !priorityOk(args.priority)) refuse('priority-invalid');
return this.#write(async () => {
const { rows, groups } = await this.#accounts();
const original = this.#lock ? structuredClone(groups) : null;
const matches = rows.filter(row => row.accountId === args.accountId);
if (matches.length !== 1) refuse('priority-invalid');
const row = matches[0];
if (this.#lock && !isDeepStrictEqual(await this.#client.getUpstreamGroups(), original)) refuse('proxy-down');
this.#beforeMutation('proxy.priority.set');
try {
if (row.source === 'oauth') await this.#client.patchCredential(row.name, args.priority);
else {
@@ -163,11 +222,13 @@ export class ProxyPage {
const values = await this.#client.getKeys();
if (!Array.isArray(values) || values.some(k => typeof k !== 'string')) refuse('proxy-down');
const records = await this.#records(scope.business);
const prefix = `msk-${this.#tag}-`;
const prefix = this.#org === null ? `msk-${this.#tag}-` : `mso-${this.#tag}-${this.#org}-`;
return { keys: values.map(value => {
const id = keyId(value), sentinel = value.startsWith(prefix + 'sentinel-');
const id = keyId(value), suffix = value.startsWith(prefix) ? value.slice(prefix.length) : null;
const sentinel = suffix !== null && (this.#org === null ? suffix.startsWith('sentinel-') : /^sentinel-[A-Za-z0-9_-]{43}$/.test(suffix));
const managed = suffix !== null && (this.#org === null || /^key-[A-Za-z0-9_-]{43}$/.test(suffix));
const record = records.find(r => r.keyId === id);
return { keyId: id, management: sentinel ? 'sentinel' : value.startsWith(prefix) ? 'stack-managed' : 'not stack-managed',
return { keyId: id, management: sentinel ? 'sentinel' : managed ? 'stack-managed' : 'not stack-managed',
agent: record?.agent ?? null, issuedAt: record?.issuedAt ?? null, state: record?.state ?? null };
}) };
}
+5 -5
View File
@@ -1,5 +1,5 @@
// P5: one reader per broker/data root drains the instance queue once, then routes
// safe IDs to the owning business. It never supplies records to limit checks.
// The shared proxy queue has no safe multi-bus consumer yet. Runtime never opts
// in to draining; enableDrain exists solely for the isolated collector tests.
import { randomUUID } from 'node:crypto';
import { createLoader } from '../../plugins/src/loader.mjs';
import { checkUsageSettings } from '../../plugins/src/kinds.mjs';
@@ -8,7 +8,7 @@ const KINDS = ['input', 'output', 'cacheWrite', 'cacheRead'];
const ID = /^[a-f0-9]{12}$/;
const plain = n => n <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(n) : String(n);
// makeLoader is the plug-in loader's factory; a test wraps it to see what a module is handed.
export function createProxyUsage({ store, read, client, records, log, now = Date.now, makeLoader = createLoader }) {
export function createProxyUsage({ store, read, client, records, log, now = Date.now, makeLoader = createLoader, enableDrain = false }) {
const loader = makeLoader({ log });
let due = 0, previous = '', pending = null, closed = false;
const states = new Map();
@@ -88,7 +88,7 @@ export function createProxyUsage({ store, read, client, records, log, now = Date
}
}
function tick() {
if (closed) return Promise.resolve();
if (closed || !enableDrain) return Promise.resolve();
if (pending) return pending;
pending = poll().finally(() => { pending = null; });
return pending;
@@ -108,7 +108,7 @@ export function createProxyUsage({ store, read, client, records, log, now = Date
}
}
const finish = map => [...map.values()].map(e => ({ ...e, ...Object.fromEntries(KINDS.map(k => [k, plain(e[k])])) }));
return { source: 'proxy', state: states.get(business) ?? 'unread', droppedBatches: dropped.get(business) ?? 0, byKey: finish(byKey), byAccount: finish(byAccount) };
return { source: 'proxy', state: enableDrain ? (states.get(business) ?? 'unread') : 'unavailable', droppedBatches: dropped.get(business) ?? 0, byKey: finish(byKey), byAccount: finish(byAccount) };
}
return { tick, view, async close() { closed = true; await pending; } };
}
+120 -15
View File
@@ -25,8 +25,10 @@ import { createLoader, hasModule } from '../../plugins/src/loader.mjs';
import { pluginSettings } from '../../business/src/plugin-settings.mjs';
import { loadManagementClient } from '../../model-proxy/src/client.mjs';
import { ProxyPage } from './proxy-page.mjs';
import { createModelCatalogue } from './model-catalogue.mjs';
import { sha256 } from '../../model-proxy/src/common.mjs';
import { initializeProxy } from '../../model-proxy/src/settings.mjs';
import { createProxyLock } from './proxy-lock.mjs';
import { ModelKeys, modelOrganization, stopModelRuns, MODEL_STOP_TIMEOUT_MS } from './model-keys.mjs';
import { instanceSettings } from '../../business/src/instance-settings.mjs';
import { organizationWriter, OrganizationError, setReplyLimits } from '../../business/src/organization.mjs';
@@ -36,6 +38,7 @@ import { loadBusiness } from '../../business/src/business.mjs';
import { loadRole } from '../../business/src/role.mjs';
import { resolveInstance } from '../../business/src/resolve.mjs';
import { canonicalJson } from '../../business/src/util.mjs';
import { selectModelProvider } from '../../plugins/src/model-provider-selection.mjs';
import { busBusiness } from './business.mjs';
import { webService } from './web.mjs';
import { hostExecutor, sweepRunDir } from './host-run.mjs';
@@ -102,7 +105,9 @@ export async function harnessLevels({ log = (e) => process.stderr.write(`plugins
log({ event: 'harness-levels-failed', name, message: e.message });
}
}
return (name) => levels.get(name) ?? null;
const lookup = (name) => levels.get(name) ?? null;
lookup.names = [...levels.keys()].sort();
return lookup;
}
// Row 224 C2-05b: a host-only adapter. A failed fetch keeps serving immutable old
// objects, as C2-03a requires; no caller may select a repo, commit or sync --accept.
@@ -242,6 +247,7 @@ function modelRunInventory(dataRoot, bound, credentials) {
export async function startBroker({
dataRoot,
instanceRoot = null,
businesses,
launches = [],
readers = [],
@@ -265,6 +271,7 @@ export async function startBroker({
consoleOwners = [],
// P1 validated sidecar from the trusted host; never a socket request setting.
proxy = null,
org = null, instanceDataRoot = null, instanceConfigDir = null,
// Host-only preparation/admission capabilities. The host builds its own from the organization writer and `trackers`
// (request-host.mjs); anything given here replaces the host's, and a capability nothing supplies leaves a setup wait.
requestServices = {},
@@ -272,6 +279,8 @@ export async function startBroker({
// the host's new-project preparation reads each business's tracker root board from this one.
trackers = null,
builtinBinding = null,
modelCatalogue = null, // in-process fixture seam; boot JSON cannot supply a service
makeProviderLoader = createLoader, // in-process fixture seam; boot JSON cannot supply a function
}) {
if (!Array.isArray(consoleOwners) || consoleOwners.length > 512 || consoleOwners.some((o) => o === null || typeof o !== 'object' || Array.isArray(o) ||
Object.keys(o).length !== 3 || ['provider', 'iss', 'sub'].some((k) => typeof o[k] !== 'string' || !o[k] || o[k].length > 512)))
@@ -284,8 +293,8 @@ export async function startBroker({
|| (Object.hasOwn(librarySync, 'intervalMs') && !(Number.isSafeInteger(librarySync.intervalMs) && librarySync.intervalMs > 0))
|| (Object.hasOwn(librarySync, 'configDir') && typeof librarySync.configDir !== 'string')))
throw new BusError('invalid-host-request');
let modelProxyClient = null, proxyPage = null, proxyUsage = null, usageTimer = null;
let modelKeys = null, modelTimer = null, modelSweep = null;
let modelProxyClient = null, proxyPage = null, proxyUsage = null, usageTimer = null, proxyLock = null;
let modelKeys = null, modelTimer = null, modelSweep = null, modelCatalogues = null;
const modelRuns = new Map();
let store, credentials, server, adapter, processing, syncTimer = null, syncPass = null, accessTimer = null, requestTimer = null;
try {
@@ -328,7 +337,11 @@ export async function startBroker({
// Store's writer lock admits one bus per data root before it holds the management client.
if (proxy) {
if (!organization) throw new BusError('not-configured');
modelProxyClient = await loadManagementClient({ configDir: organization.dir, proxy, receipt: proxyReceipt });
if (org !== null) {
if (!org || typeof org.id !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org.id) || typeof instanceDataRoot !== 'string' || typeof instanceConfigDir !== 'string') throw new BusError('invalid-host-request');
proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${proxy.port}` });
}
modelProxyClient = await loadManagementClient({ configDir: org ? instanceConfigDir : organization.dir, proxy, receipt: proxyReceipt });
}
// Row 234 (#1740, r6 X4 third bullet): each host grant record gives its agent a store reference to
@@ -351,7 +364,7 @@ export async function startBroker({
modelAgents: proxy ? Object.entries(businesses).flatMap(([id, b]) => Object.keys(b.roles).map(role => id + '/' + role)) : [] });
if (proxy) {
const inventory = modelRunInventory(dataRoot, modelRuns, credentials);
modelKeys = new ModelKeys({ credentials, client: modelProxyClient, configDir: organization.dir,
modelKeys = new ModelKeys({ credentials, client: modelProxyClient, configDir: org ? instanceConfigDir : organization.dir, org: org?.id ?? null, lock: proxyLock,
baseUrl: `http://127.0.0.1:${proxy.port}`, receipt: proxyReceipt, rotateGraceMinutes: proxy.rotateGraceMinutes ?? 120,
runs: inventory.runs,
stopRuns: async runs => {
@@ -366,9 +379,10 @@ export async function startBroker({
try { await modelKeys.reconcile(); } catch (error) { proxyReceipt({ action: 'proxy.boot-refused', code: error.code }); }
}
const configureProxyPage = () => {
proxyPage = new ProxyPage({ client: modelProxyClient, instanceTag: sha256(organization.dir).slice(0, 12),
proxyPage = new ProxyPage({ client: modelProxyClient, instanceTag: sha256(org ? instanceConfigDir : organization.dir).slice(0, 12), org: org?.id ?? null, lock: proxyLock,
// P2 supplies the model metadata; only the matching business's record
// participates in this owner view. No credential value is read here.
onChange: phase => { if (phase === 'begin') modelCatalogues?.begin?.(); else if (phase === 'end') modelCatalogues?.end?.(); else modelCatalogues?.invalidate(); },
keyRecords: business => credentials.status().filter(r => r.service === 'model' && r.instance?.startsWith(business + '/'))
.map(r => ({ keyId: r.keyId, agent: r.instance.slice(business.length + 1), issuedAt: r.issuedAt, state: r.state })),
});
@@ -383,6 +397,37 @@ export async function startBroker({
// records. A daily key quota moved within one agent's lineage keeps its since and its count.
const keyLineage=(business,keyId)=>modelKeyLineage(credentials.status(),business,keyId);
const settings=organization?launchSettings({...organization,sessions:()=>readSessions(dataRoot),levels,keyLineage}):null;
modelCatalogues = modelCatalogue ?? (settings ? createModelCatalogue({ client: () => modelProxyClient, settings: id => settings.current(id).plugins ?? {}, proxyLock }) : null);
// Resolve legacy table entries with the enabled modules' declarations. A
// missing or ambiguous mapping never becomes an available route.
async function catalogueFor(id, options = {}) {
const snapshot = settings.writer.read(id);
const current = settings.current(id);
const names = current.plugins?.['model-provider'] ?? [];
const loader = makeProviderLoader({log:()=>{}});
const loaded = await loader.load({'model-provider': names});
const catalogue = await modelCatalogues.read(id, options);
if (loaded.refused.length || settings.writer.read(id).revision !== snapshot.revision) return {...catalogue, status:'unavailable', legacy:{}};
const modules = loader.snapshot('model-provider');
const resolveLegacy = entry => {
const selected = selectModelProvider(modules, entry);
if (!selected) return null;
let result; try { result = selected.module.route({selection:entry,harness:null}); } catch { return null; }
return typeof result?.upstream === 'string' && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(result.upstream)
? {module:selected.name,upstream:result.upstream,model:entry.model} : null;
};
const legacy = {};
for (const [key, entry] of Object.entries(current.launch?.models ?? {})) {
if (!entry.module) {
const mapping = resolveLegacy(entry);
if (mapping) legacy[key] = mapping;
}
}
const result = {...catalogue, legacy};
// This trusted in-process helper is never serialized into the client view.
Object.defineProperty(result, 'resolveLegacy', {value:resolveLegacy});
return result;
}
// Row 194 (#1692, brief item 8, row 199 D13): a fire or a delete revokes the agent's credentials
// after the writer's checks and before its write, under the writer's lock (row 239). Fire revokes
// each service of the entry and keeps the store directory, delete calls remove (sage-1593). A
@@ -699,11 +744,15 @@ export async function startBroker({
return getProjectRemote(writer, id, args.project);
}
if(verb==='limits')return withTokens(id,settings.read(id));
if(verb==='plugins')return plugins.read(id);
if(verb==='plugins')return {...plugins.read(id), available:{harness:levels.names ?? []}};
// Row 256 (#1761): the actor is the reader, so endProtected is the delete check for that reader.
if(verb==='instances')return instances.read(id,actor);
// Row 65 (mos-0840): the Console's routing table.
if(verb==='routes')return settings.routes(id);
if(verb==='routes') {
const view = settings.routes(id);
if (!settings.current(id).plugins?.['model-provider']?.length) return { ...view, catalogue: { status: 'disabled', observedAt: null, models: [], notices: [] } };
return catalogueFor(id).then(catalogue => ({ ...settings.routes(id), catalogue }));
}
// Row 298 (#1804, Q228): the chief's grant scope as the file stands, the fallback shown.
if(verb==='grants.scope')return instances.grantScopeRead(id);
// Row 194 (#1692, V5): a delete's checks at the verb, with no write and no reload.
@@ -713,10 +762,27 @@ export async function startBroker({
// Internal read-only path: no reload, journal, credential or publication.
if(verb==='instance.hireCheck')return instances.checkHire(id,args);
let result;
if (['limits.set', 'instances.set'].includes(verb) && settings.current(id).plugins?.['model-provider']?.length) {
return catalogueFor(id, { fresh: true }).then(catalogue => {
context?.beforeWrite?.();
const result = verb === 'limits.set' ? withTokens(id, settings.set(id,args,actor,catalogue)) : instances.set(id,args,actor,catalogue);
const live=reload(id);
limits.refresh(id).catch(e=>limitLog({event:'limit-refresh-failed',business:id,message:e.message}));
return INSTANCE_VERBS.has(verb) ? {...result,restart:!live} : result;
});
}
if(verb==='limits.set')result=withTokens(id,settings.set(id,args,actor));
else if(verb==='instances.set')result=instances.set(id,args,actor);
else if(verb==='routes.set')result=settings.setRoutes(id,args,actor);
else if(verb==='plugins.set')result=plugins.set(id,args,actor);
else if(verb==='routes.set') {
const current = settings.current(id);
if (current.plugins?.['model-provider']?.length) return catalogueFor(id, { fresh: true }).then(catalogue => {
const result = settings.setRoutes(id,args,actor,catalogue);
reload(id); limits.refresh(id).catch(e=>limitLog({event:'limit-refresh-failed',business:id,message:e.message}));
return { ...result, catalogue };
});
result=settings.setRoutes(id,args,actor);
}
else if(verb==='plugins.set'){result=plugins.set(id,args,actor);modelCatalogues?.invalidate();}
// Row 194 (#1692, V6): the human's keep and noHire lists, through the instance writer.
else if(verb==='veto.set')result=instances.veto(id,args,actor);
// Row 298: the human's grant scope, through the instance writer.
@@ -746,7 +812,7 @@ export async function startBroker({
return proxyPage.request(verb, args, scope);
},
proxyInit: organization ? async args => {
const settings = await initializeProxy({ configDir: organization.dir, dataRoot, ...args });
const settings = await initializeProxy({ configDir: organization.dir, dataRoot: instanceRoot ?? dataRoot, ...args });
modelProxyClient = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt });
configureProxyPage();
return settings;
@@ -1021,10 +1087,12 @@ export async function startBroker({
}
// One bounded queue read at a time. The service applies the configured
// 1..59 second poll deadline; this tick only checks whether a read is due.
const usageTick = () => { proxyUsage.tick().catch(() => usageLog({ action: 'proxy.usage', code: 'proxy-usage-read-failed' })); };
usageTimer = setInterval(usageTick, 1000);
usageTimer.unref();
usageTick(); // Optional telemetry never blocks the native ready answer.
if (instanceRoot === null) {
const usageTick = () => { proxyUsage.tick().catch(() => usageLog({ action: 'proxy.usage', code: 'proxy-usage-read-failed' })); };
usageTimer = setInterval(usageTick, 1000);
usageTimer.unref();
usageTick(); // Optional telemetry never blocks the native ready answer.
}
// Startup recovery waits while processing is halted or invalid, like the periodic pass below;
// processing.resume schedules it again.
const startState = processing.read();
@@ -1052,6 +1120,43 @@ export async function startBroker({
bindLaunch,
recoverLaunch: ({ modelKeyId, ...record }) => broker.recoverLaunch(record),
endLaunch: (r) => { const out = broker.endLaunch(r); modelRuns.delete(r.business + ':' + r.run); return out; },
modelAdmission: async ({ business, instance, modelKey, harness, revision, entry: expected, provider, model, account, route, transport }) => {
const refused = code => ({ allow: false, code });
if (!settings || !modelCatalogues) return refused('model-catalogue-unavailable');
try { proxyLock?.assertCertain(); } catch { return refused('proxy-uncertain'); }
const snapshot = settings.writer.read(business);
if (snapshot.revision !== revision) return refused('model-provider-unavailable');
const current = settings.current(business), role = current.roles?.[instance];
if (!role) return refused('model-provider-unavailable');
const resolved = resolveInstance({ system: organization.system, business: current, instance });
const vars = resolved.vars;
if (!['primary', 'fallback'].includes(route)) return refused('model-provider-unavailable');
const prefix = route === 'primary' ? '' : 'fallback.';
if (vars[prefix + 'model'] !== modelKey || vars[prefix + 'harness'] !== harness) return refused('model-provider-unavailable');
const entry = current.launch?.models?.[modelKey];
if (!entry || canonicalJson(entry) !== canonicalJson(expected ?? null) || entry.provider !== provider || entry.model !== model) return refused('model-provider-unavailable');
const harnessLoader = createLoader({log:()=>{}});
if ((await harnessLoader.load({harness:[harness]})).refused.length) return refused('model-provider-unavailable');
const countsAccount = harnessLoader.modules('harness')[0].countsAccount();
if (typeof countsAccount !== 'boolean' || account !== (countsAccount ? current.launch?.account?.name ?? null : null)) return refused('model-provider-unavailable');
const names = current.plugins?.['model-provider'];
if (!Array.isArray(names) || !names.length || (entry.module && !names.includes(entry.module))) return refused('model-provider-unavailable');
const loader = makeProviderLoader({log:()=>{}}), loaded = await loader.load({'model-provider':entry.module ? [entry.module] : names});
if (loaded.refused.length) return refused('model-provider-unavailable');
const selected = selectModelProvider(loader.snapshot('model-provider'), entry);
if (!selected) return refused('model-provider-unavailable');
const {name:moduleName,module} = selected;
const descriptor = module.route({selection:entry,harness});
if (descriptor?.code) return refused(descriptor.code === 'model-harness-incompatible' ? descriptor.code : 'model-provider-unavailable');
const expectedTransport = (({routeId, endpoint, api, piProvider, compat}) => ({routeId, endpoint, api, piProvider, compat}))(descriptor);
if (canonicalJson(expectedTransport) !== canonicalJson(transport ?? null)) return refused('model-provider-unavailable');
const catalogue = await modelCatalogues.read(business,{fresh:true});
if (settings.writer.read(business).revision !== snapshot.revision) return refused('model-provider-unavailable');
const selection = {upstream:entry.upstream ?? descriptor.upstream,model:entry.model};
const answer = module.preflight({selection,harness,catalogue:{...catalogue,models:(catalogue.models ?? []).filter(m => m.module === moduleName)}});
if (answer?.allow === true) return {allow:true};
return refused(['model-provider-unavailable','model-catalogue-unavailable','model-harness-incompatible'].includes(answer?.code) ? answer.code : 'model-provider-unavailable');
},
modelPreflight: async ({ business, role }) => {
const current = settings ? settings.current(business) : businesses[business];
if (!Object.hasOwn(current?.roles ?? {}, role)) throw new BusError('unknown-role');
+4 -2
View File
@@ -211,13 +211,15 @@ test('REQ-LIMIT-1, REQ-COS-1: organization.set from the chief of staff reaches t
const done = call('chief', 'organization.set', PATCH);
assert.deepEqual(done, { revision: 'b'.repeat(64) });
// Same callback verb as a human's limits.set; only the actor differs.
assert.deepEqual(organization, [['demo', 'limits.set', PATCH, 'chief-of-staff.chief']]);
assert.deepEqual(organization[0].slice(0,4), ['demo', 'limits.set', PATCH, 'chief-of-staff.chief']);
assert.equal(typeof organization[0][4].beforeWrite, 'function');
call('human', 'limits.set', PATCH);
assert.deepEqual(organization.at(-1), ['demo', 'limits.set', PATCH, 'jason']);
// Row 155 (#1649, Q169): the assistant's configuration takes the same path, for both actors.
const assistants = { revision: 'a'.repeat(64), assistants: { assistant: { vars: { thinking: 'high' } } } };
call('chief', 'organization.set', assistants);
assert.deepEqual(organization.at(-1), ['demo', 'limits.set', assistants, 'chief-of-staff.chief']);
assert.deepEqual(organization.at(-1).slice(0,4), ['demo', 'limits.set', assistants, 'chief-of-staff.chief']);
assert.equal(typeof organization.at(-1)[4].beforeWrite, 'function');
call('human', 'limits.set', assistants);
assert.deepEqual(organization.at(-1), ['demo', 'limits.set', assistants, 'jason']);
});
@@ -0,0 +1,75 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { spawn, spawnSync } from 'node:child_process';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
import { once } from 'node:events';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { DEV_OWNER } from '../../cli/src/dev-local-owner.mjs';
import { fixture, notifyConfig } from '../../cli/tests/helpers.mjs';
import { initializeControl, transition } from '../../processing-control/src/state.mjs';
import { verifyPrincipal } from '../src/principal.mjs';
import { checkConsole } from '../../webui/src/sso-config.mjs';
import { reserveOrg, activateOrg } from '../../business/src/org-registry.mjs';
const uid=process.getuid(), nonce='a'.repeat(64);
const paths={ console:'/tree/cli.mjs', consoleCli:'/tree/console-cli.mjs' };
function context(subject=DEV_OWNER, owners=[]) {
const table=new Map([[50,{pid:50,ppid:40,startTime:'100',uid,argv:['node',paths.consoleCli],env:{MOSAIC_BUS_CONSOLE_NONCE:nonce,MOSAIC_BUS_CONSOLE_BUSINESS:'demo',MOSAIC_BUS_CONSOLE_PROVIDER:subject.provider,MOSAIC_BUS_CONSOLE_ISS:subject.iss,MOSAIC_BUS_CONSOLE_SUB:subject.sub}}], [40,{pid:40,ppid:1,startTime:'90',uid,argv:['node',paths.console,'serve'],env:{}}]]);
return {peer:{pid:50,uid,gid:uid},paths,businesses:{demo:{human:'owner'}},consoleOwners:owners,readProcess:pid=>({...table.get(pid)}),table};
}
const proof = subject => ({console:{pid:50,startTime:'100',nonce,business:'demo',...subject}});
test('dev principal requires enabled owner membership and the Console wrapper, nonce and serve parent',()=>{
assert.throws(()=>verifyPrincipal(proof(DEV_OWNER),context()),/human-required/);
const c=context(DEV_OWNER,[DEV_OWNER]);
assert.deepEqual(verifyPrincipal(proof(DEV_OWNER),c).subject,DEV_OWNER);
for(const mutation of [t=>{t.get(50).argv=['node','/other'];},t=>{t.get(40).argv=['node',paths.console,'other'];},t=>{t.get(50).env.MOSAIC_BUS_CONSOLE_NONCE='bad';}]) {
const d=context(DEV_OWNER,[DEV_OWNER]); mutation(d.table); assert.throws(()=>verifyPrincipal(proof(DEV_OWNER),d),/human-required/);
}
const forged={...DEV_OWNER,sub:'another'};
assert.throws(()=>verifyPrincipal(proof(forged),context(forged,[DEV_OWNER])),/human-required/);
});
test('dev issuer cannot be a configured SSO issuer',()=>{
const sso={sso:{publicOrigin:'https://console.example',providers:[{id:DEV_OWNER.provider,issuer:DEV_OWNER.iss,clientId:'fake',clientSecret:{slot:'fake'},algs:['RS256'],subjects:[{sub:DEV_OWNER.sub,name:'operator'}]}]}};
assert.throws(()=>checkConsole(sso),/issuer.*https URL/);
});
test('bus dev flag refuses the default instance with or without --pm before config access',()=>{
for(const args of [['bus','start','demo','--dev-local-owner'], ['bus','start','demo','--dev-local-owner','--pm']]) {
const r=spawnSync(process.execPath,['packages/cli/src/cli.mjs',...args],{encoding:'utf8',env:{...process.env,MOSAIC_CONFIG:''}});
assert.notEqual(r.status,0); assert.match(r.stderr,/--dev-local-owner requires a named instance/);
}
});
for (const pm of [false, true]) {
test(`bus dev owner enables launches ${pm ? 'with --pm and prints the warning' : 'without --pm'}`, { timeout: 30000 }, async t => {
const root=mkdtempSync(join(tmpdir(),'dev-owner-launch-'));
const f=fixture(root), home=join(root,'home'), base=join(home,'.config');
mkdirSync(base,{recursive:true}); renameSync(join(root,'config'),join(base,'mosaic-test'));
chmodSync(join(base,'mosaic-test'),0o700);
writeFileSync(join(f.dataRoot,'instance.json'),JSON.stringify({instanceVersion:1,name:'test'}));
const ctx={ configDir:join(base,'mosaic-test'), dataRoot:f.dataRoot, instance:'test' };
const org=await reserveOrg(ctx,'acme'); await activateOrg(ctx,'acme');
notifyConfig(org.root,'acme',null);
// A halted instance exercises the real --pm path without launching a harness.
initializeControl(org.root);
transition(org.root,{business:'acme',human:'jason',halted:true});
const env={...f.env,HOME:home,MOSAIC_CONFIG:join(base,'mosaic-test','config.json')};
const child=spawn(process.execPath,['packages/cli/src/cli.mjs','bus','start','acme','--dev-local-owner',...(pm ? ['--pm'] : [])],{env,stdio:['ignore','pipe','pipe']});
const exited=once(child,'exit'); let stdout='', stderr='';
child.stdout.on('data',b=>stdout+=b); child.stderr.on('data',b=>stderr+=b);
t.after(async()=>{if(child.exitCode===null && child.signalCode===null){const stop=spawn(process.execPath,['packages/cli/src/cli.mjs','bus','stop','acme'],{env,stdio:'ignore'}); const [[code]]=await Promise.all([once(stop,'exit'),exited]); assert.equal(code,0,'bus stops cleanly');} rmSync(root,{recursive:true,force:true});});
const log=join(org.root,'bus-host','host.log');
const hostOutput=()=>existsSync(log)?readFileSync(log,'utf8'):'';
const deadline=Date.now()+15000;
while(Date.now()<deadline && child.exitCode===null && !(pm ? hostOutput().includes('processing halted: chief launch skipped') : hostOutput().includes('bus host up:'))) await new Promise(r=>setTimeout(r,50));
stdout=hostOutput();
assert.match(stdout,/DEV LOCAL OWNER MODE: console owner local-dev accepted \(named instance test\)\n/,stderr);
const warning='DEV LOCAL OWNER MODE: launched agents run as this OS user and can reach the loopback Console as owner; use this instance for UI testing only';
if(pm) {
assert.ok(stdout.includes(`DEV LOCAL OWNER MODE: console owner local-dev accepted (named instance test)\n${warning}\n`),stdout);
assert.match(stdout,/processing halted: chief launch skipped/);
} else assert.ok(!stdout.includes(warning));
assert.match(stdout,/bus host up: business acme, socket \S+, notifier off\n/);
assert.equal(child.exitCode,null,stderr);
});
}
@@ -0,0 +1,97 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createModelCatalogue } from '../src/model-catalogue.mjs';
test('catalogue caches per business, refreshes on expiry and invalidation, and forces a fresh launch read', async () => {
let clock = 1000, calls = 0;
const client = {
getCredentials: async () => ({ files: [] }),
getCredentialModels: async () => ({ models: [] }),
getUpstreamGroups: async () => { calls++; return [{ 'base-url': 'https://api.z.ai/api/anthropic', keys: [{}], models: [{ name: 'fixture-id' }] }]; },
};
const service = createModelCatalogue({ client: () => client, settings: () => ({ 'model-provider': ['cliproxy'] }), now: () => clock });
const [first, second] = await Promise.all([service.read('one'), service.read('one')]);
assert.equal(calls, 1); assert.deepEqual(first, second);
assert.equal(first.models[0].key, 'zai-fixture-id');
await service.read('one'); assert.equal(calls, 1);
await service.read('two'); assert.equal(calls, 2);
await service.read('one', { fresh: true }); assert.equal(calls, 3);
clock += 30001; await service.read('one'); assert.equal(calls, 4);
service.invalidate(); await service.read('one'); assert.equal(calls, 5);
});
test('disabled, refused, and failed providers never retain authorization', async () => {
let enabled = true, failed = false;
const client = { getCredentials: async () => ({ files: [] }), getCredentialModels: async () => ({ models: [] }),
getUpstreamGroups: async () => { if (failed) throw Error('fixture secret'); return [{ 'base-url': 'https://api.z.ai/api/anthropic', keys: [{}], models: [{ name: 'fixture-id' }] }]; } };
const service = createModelCatalogue({ client: () => client, settings: () => ({ 'model-provider': enabled ? ['cliproxy'] : [] }) });
assert.equal((await service.read('one')).models[0].available, true);
enabled = false; assert.equal((await service.read('one')).status, 'disabled');
enabled = true; failed = true;
const out = await service.read('one', { fresh: true });
assert.equal(out.status, 'unavailable');
assert.ok(out.models.every(m => !m.available));
assert.equal(JSON.stringify(out).includes('fixture secret'), false);
});
test('launch admission refreshes selection and refuses a removed model and incompatible harness', async t => {
const { startBroker } = await import('../src/runtime.mjs');
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../business/tests/helpers.mjs');
const { mkdtempSync, mkdirSync, rmSync } = await import('node:fs');
const { tmpdir } = await import('node:os'); const { join } = await import('node:path');
const root=mkdtempSync(join(tmpdir(),'model-admission-')); t.after(()=>rmSync(root,{recursive:true,force:true}));
const rolesDir=rolesCopy(root), doc=businessDoc(root,'demo'); doc.plugins={...doc.plugins,harness:['pi'],'model-provider':['cliproxy']};
doc.launch.models['zai-fixture-id']={provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'};
doc.roles.coder.vars={...doc.roles.coder.vars,harness:'pi',model:'zai-fixture-id',thinking:'low'};
writeJson(join(root,'businesses/demo.json'),doc); mkdirSync(join(root,'data'),{mode:0o700});
let present=true, reads=0;
const service={invalidate(){},read:async(_,{fresh})=>{assert.equal(fresh,true);reads++;return {status:'ready',models:present?[{module:'cliproxy',upstream:'zai',model:'fixture-id',available:true,harnesses:['pi']}]:[]};}};
const bus=await startBroker({dataRoot:join(root,'data'),businesses:{demo:{id:'demo',human:'jason',arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{authority:{withinRole:[],crossRole:[]}}}}},organization:{dir:root,rolesDir,system:systemFor(root)},modelCatalogue:service});t.after(()=>bus.close());
const human=bus.broker.bindHuman({business:'demo',human:'jason',via:'cli',outsideAgent:true});
const revision=bus.broker.request(human,{verb:'limits'}).revision;
const {checkedTransport}=await import('../../harness/src/bundle.mjs');
const entry=doc.launch.models['zai-fixture-id'];
const request={business:'demo',instance:'coder',modelKey:'zai-fixture-id',harness:'pi',revision,entry,provider:entry.provider,model:entry.model,account:null,route:'primary',transport:checkedTransport(entry)};
assert.deepEqual(await bus.modelAdmission(request),{allow:true});
present=false;
assert.deepEqual(await bus.modelAdmission(request),{allow:false,code:'model-provider-unavailable'});
assert.equal(reads,2);
});
test('an account mutation fences reads begun before or during the write', async () => {
let release;const gate=new Promise(r=>{release=r;});
const client={getCredentials:async()=>({files:[]}),getCredentialModels:async()=>({models:[]}),getUpstreamGroups:async()=>{await gate;return [{'base-url':'https://api.z.ai/api/anthropic',keys:[{}],models:[{name:'fixture-id'}]}];}};
const service=createModelCatalogue({client:()=>client,settings:()=>({'model-provider':['cliproxy']})});
const read=service.read('one',{fresh:true});service.begin();
assert.equal((await service.read('one',{fresh:true})).status,'unavailable');
release();assert.equal((await read).status,'unavailable');service.end();
assert.equal((await service.read('one',{fresh:true})).status,'ready');
});
test('a different enabled fixture provider supplies legacy mapping and admission without a core case', async t => {
const {startBroker}=await import('../src/runtime.mjs');const {businessDoc,rolesCopy,systemFor,writeJson}=await import('../../business/tests/helpers.mjs');
const {mkdtempSync,mkdirSync,rmSync}=await import('node:fs');const {tmpdir}=await import('node:os');const {join}=await import('node:path');
const root=mkdtempSync(join(tmpdir(),'second-provider-'));t.after(()=>rmSync(root,{recursive:true,force:true}));const rolesDir=rolesCopy(root),doc=businessDoc(root,'demo');
doc.plugins={...doc.plugins,harness:['pi'],'model-provider':['second-fixture']};doc.launch.models['fixture-legacy']={provider:'fixture',model:'fixture-model'};doc.roles.coder.vars={...doc.roles.coder.vars,harness:'pi',model:'fixture-legacy',thinking:'low'};
writeJson(join(root,'businesses/demo.json'),doc);mkdirSync(join(root,'data'),{mode:0o700});
const transport={routeId:'fixture',endpoint:'v1',api:'openai-completions',piProvider:'fixture',compat:{thinkingFormat:'openai',zaiToolStream:false}};
const module={route:({selection})=>selection.provider==='fixture'?{provider:selection.provider,model:selection.model,upstream:'fixture',...transport}:{code:'unsupported'},preflight:({selection,catalogue})=>({allow:catalogue.models.some(m=>m.upstream===selection.upstream&&m.model===selection.model)})};
const makeProviderLoader=()=>({load:async()=>({refused:[]}),snapshot:()=>[{name:'second-fixture',module}]});
const offered={key:'fixture-model',module:'second-fixture',upstream:'fixture',provider:'fixture',model:'fixture-model',label:'Fixture',available:true,reason:null,harnesses:['pi']};
const modelCatalogue={read:async()=>({status:'ready',models:[offered],notices:[]}),invalidate(){}};
const bus=await startBroker({dataRoot:join(root,'data'),businesses:{demo:{id:'demo',human:'jason',arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{authority:{withinRole:[],crossRole:[]}}}}},organization:{dir:root,rolesDir,system:systemFor(root)},modelCatalogue,makeProviderLoader});t.after(()=>bus.close());
const human=bus.broker.bindHuman({business:'demo',human:'jason',via:'cli',outsideAgent:true});const view=await bus.broker.request(human,{verb:'routes'});
assert.equal(view.catalogue.legacy['fixture-legacy'].module,'second-fixture');
const answer=await bus.modelAdmission({business:'demo',instance:'coder',modelKey:'fixture-legacy',harness:'pi',revision:view.revision,entry:doc.launch.models['fixture-legacy'],provider:'fixture',model:'fixture-model',account:null,route:'primary',transport});
assert.deepEqual(answer,{allow:true});
});
test('shared generation invalidates cached catalogues and an uncertain fence makes them unavailable', async () => {
let version = 0, uncertain = false, calls = 0;
const lock = { generation: () => version, assertCertain: () => { if (uncertain) throw Error('uncertain'); } };
const client = { getCredentials: async () => ({ files: [] }), getCredentialModels: async () => ({ models: [] }),
getUpstreamGroups: async () => { calls++; return [{ 'base-url': 'https://api.z.ai/api/anthropic', keys: [{}], models: [{ name: 'fixture-id' }] }]; } };
const service = createModelCatalogue({ client: () => client, settings: () => ({ 'model-provider': ['cliproxy'] }), proxyLock: lock });
assert.equal((await service.read('one')).status, 'ready');
version++; assert.equal((await service.read('one')).status, 'ready'); assert.equal(calls, 2);
uncertain = true; assert.equal((await service.read('one')).status, 'unavailable');
});
+61 -3
View File
@@ -173,7 +173,7 @@ test('proxy-stop-failed: old proxy key and retiring record survive a failed grac
stopped = true; await overdue.sweep(); assert.equal(f.state.keys.includes(first.key), false);
});
async function bootBusiness(f) {
async function bootBusiness(f, binding = {}) {
const rolesDir = rolesCopy(f.root), system = systemFor(f.root), doc = businessDoc(f.root, 'demo');
// Aide is needed because shipped chief hireable:[] and security-reviewer has review.verdict outside HIRE_ALLOWED.
fs.writeFileSync(join(rolesDir, 'aide.md'), '# Aide\n');
@@ -193,13 +193,13 @@ async function bootBusiness(f) {
// Fresh mkdtemp fixture + exclusive mkdir above: no prior session registry can exist.
new Registry(join(f.root, 'data'));
const bus = await startBroker({ dataRoot: join(f.root, 'data'), businesses: { demo: busBusiness(business, resolved) },
organization: { dir: f.configDir, rolesDir, system }, proxy: { port: Number(new URL(f.baseUrl).port), binary: '/fixture-unused' } });
organization: { dir: f.configDir, rolesDir, system }, proxy: { port: Number(new URL(f.baseUrl).port), binary: '/fixture-unused' }, ...binding });
f.closers.push(() => bus.close());
const chief = bus.broker.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi' });
bus.broker.request(chief, { verb: 'role.claim' });
const fire = () => bus.broker.request(chief, { verb: 'instance.fire', args: { name: 'sec-1', reason: 'fixture fire' } });
const listed = () => JSON.parse(fs.readFileSync(file, 'utf8')).launch.instances.includes('sec-1');
return { bus, chief, fire, listed, file };
return { bus, chief, fire, listed, file, rolesDir, system };
}
test('bus fire proves remote 401 before delisting the agent and deleting model file', async t => {
const f = await fixture(t, { delayed: 105 }); await f.manager.reconcile(); const issued = await f.manager.ensure('demo/sec-1');
@@ -270,3 +270,61 @@ test('thirty-day rotation due automatically replaces and retires an unused old k
const current = await f.manager.ensure('demo/coder'); assert.equal(current.key === first.key, false);
assert.equal(f.state.keys.includes(first.key), false); assert.equal(f.state.keys.includes(current.key), true);
});
test('org registration preserves a running sibling and fences admission on uncertainty', async t => {
const f = await fixture(t);
const { createProxyLock } = await import('../src/proxy-lock.mjs');
const stateDir = join(f.root, 'proxy'); fs.mkdirSync(stateDir, { mode: 0o700 });
const lock = createProxyLock({ stateDir, identity: f.baseUrl });
const betaRoot = join(f.root, 'beta-secrets'); fs.mkdirSync(betaRoot, { mode: 0o700 });
const betaCredentials = new Credentials({ storeRoot: betaRoot, dataRoot: join(f.root, 'beta-data'), modelAgents: ['demo/other'] });
t.after(() => betaCredentials.close());
const orgManager = (org, credentials) => new ModelKeys({ credentials, client: f.client, configDir: f.configDir,
baseUrl: f.baseUrl, org, lock });
const alpha = orgManager('alpha', f.credentials), beta = orgManager('beta', betaCredentials);
await alpha.reconcile();
const first = await alpha.ensure('demo/coder');
await beta.reconcile();
assert.equal(await f.client.probeKey(first.key), 200);
assert.ok(first.key.startsWith(`mso-${hash(f.configDir)}-alpha-`));
assert.equal(f.state.keys.filter(k => k.startsWith(`mso-${hash(f.configDir)}-beta-sentinel-`)).length, 1);
await beta.reconcile();
assert.equal(f.state.keys.filter(k => k.startsWith(`mso-${hash(f.configDir)}-beta-sentinel-`)).length, 1);
await lock.run(async () => lock.fence({ org: 'beta', op: 'test' }));
assert.throws(() => alpha.assertLaunchable('demo/coder'), failCode('proxy-uncertain'));
await assert.rejects(beta.ensure('demo/other'), failCode('proxy-uncertain'));
});
test('synthetic trusted org boot registers an org sentinel and issues an org key', async t => {
const f = await fixture(t);
fs.mkdirSync(join(f.root, 'proxy'), { mode: 0o700 });
const b = await bootBusiness(f, { org: { id: 'demo' }, instanceDataRoot: f.root, instanceConfigDir: f.configDir });
const answer = await b.bus.modelPreflight({ business: 'demo', role: 'sec-1' });
assert.ok(answer.key.startsWith(`mso-${hash(f.configDir)}-demo-`));
assert.equal(f.state.keys.filter(key => key.startsWith(`mso-${hash(f.configDir)}-demo-sentinel-`)).length, 1);
const { createProxyLock } = await import('../src/proxy-lock.mjs');
const lock = createProxyLock({ stateDir: join(f.root, 'proxy'), identity: `${f.configDir}:${new URL(f.baseUrl).port}` });
await lock.run(async () => lock.fence({ org: 'demo', op: 'fixture' }));
await assert.rejects(b.bus.modelPreflight({ business: 'demo', role: 'sec-1' }), failCode('proxy-uncertain'));
});
test('beta bus registers while alpha bus runs and alpha key stays accepted', async t => {
const f = await fixture(t); fs.mkdirSync(join(f.root, 'proxy'), { mode: 0o700 });
const binding = { instanceDataRoot: f.root, instanceConfigDir: f.configDir };
const alpha = await bootBusiness(f, { ...binding, org: { id: 'alpha' } });
const key = (await alpha.bus.modelPreflight({ business: 'demo', role: 'sec-1' })).key;
const doc = businessDoc(f.root, 'beta'); doc.plugins = { ...doc.plugins, harness: ['pi'] };
writeJson(join(f.configDir, 'businesses', 'beta.json'), doc);
const business = loadBusiness('beta', { dir: f.configDir, rolesDir: alpha.rolesDir }), resolved = {};
for (const instance of Object.keys(business.roles)) resolved[instance] = resolveInstance({ system: alpha.system, business, instance });
const betaRoot = join(f.root, 'beta-bus'); fs.mkdirSync(betaRoot, { mode: 0o700 }); new Registry(betaRoot);
const beta = await startBroker({ dataRoot: betaRoot, businesses: { beta: busBusiness(business, resolved) },
organization: { dir: f.configDir, rolesDir: alpha.rolesDir, system: alpha.system },
proxy: { port: Number(new URL(f.baseUrl).port), binary: '/fixture-unused' }, ...binding, org: { id: 'beta' } });
f.closers.push(() => beta.close());
assert.equal(await f.client.probeKey(key), 200);
assert.ok(f.state.keys.some(value => value.startsWith(`mso-${hash(f.configDir)}-beta-sentinel-`)));
assert.equal((await alpha.bus.modelPreflight({ business: 'demo', role: 'sec-1' })).key, key);
});
+10
View File
@@ -0,0 +1,10 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { telemetryFixture } from './p5-telemetry-fixture.mjs';
test('an organization bus does not drain the shared proxy usage queue', async t => {
const f = await telemetryFixture(t, { orgMode: true });
await f.start();
await new Promise(resolve => setTimeout(resolve, 1200));
assert.equal(f.queueCalls(), 0);
});
+14 -55
View File
@@ -6,9 +6,7 @@ import { createHash } from 'node:crypto';
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { startTimeOf } from '../../seat/src/proc.mjs';
import { telemetryFixture, WAIT_MS } from './p5-telemetry-fixture.mjs';
// Captured before any test mocks timers: the outer bound on the R4 barrier test below.
const realSetTimeout=globalThis.setTimeout,realClearTimeout=globalThis.clearTimeout;
import { telemetryFixture } from './p5-telemetry-fixture.mjs';
async function nativeTurn(f) {
const model=await f.bus.modelPreflight({business:'demo',role:'coder'});
const keyId=createHash('sha256').update(model.key).digest('hex').slice(0,12),dir=join(f.root,'native-transcript');mkdirSync(dir);
@@ -25,56 +23,17 @@ async function nativeTurn(f) {
assert.equal(view.total.input,2);assert.equal(view.byKey[0].modelKeyId,keyId);
assert.deepEqual(f.bus.broker.request(cap,{verb:'message.receive'}),[]);
}
for(const fail of [false,true])test(`P5 R1 held first optional poll does not delay native startup or work; ${fail?'failure':'release'} settles safely`,async t=>{
t.mock.timers.enable({apis:['Date','setInterval'],now:Date.now()});
const f=await telemetryFixture(t,{hold:true});const started=f.start();
await f.waitQueue(1);await new Promise(setImmediate);
// Causal first guard: the queue is still held, so a ready runtime cannot
// have depended on its response. No racing timeout or longer budget.
assert.notEqual(f.bus,null,'startBroker must resolve while the first queue response is held');
await started;assert.equal(f.state.held.length,1);
assert.equal(f.bus.broker.request(f.human(),{verb:'plugins'}).plugins.usage,'cliproxy-queue');
await nativeTurn(f);assert.equal(f.queueCalls(),1);
t.mock.timers.tick(1000);await new Promise(setImmediate);assert.equal(f.queueCalls(),1,'scheduled ticks share the held first read');
const from=f.logs.length;f.release(fail);
await f.waitLog(fail?'proxy-usage-read-failed':'proxy.usage.reconcile',from);
assert.equal(f.bus.broker.request(f.human(),{verb:'usage'}).proxy.state,fail?'unavailable':'available');
await f.close();assert.equal(f.queueCalls(),1);
});
test('P5 R1 close bounds an outstanding first poll by the unchanged five-second client deadline',async t=>{
t.mock.timers.enable({apis:['Date','setInterval','setTimeout'],now:Date.now()});
const f=await telemetryFixture(t,{hold:true});const started=f.start();await f.waitQueue(1);await new Promise(setImmediate);
assert.notEqual(f.bus,null,'native readiness precedes the held optional poll');await started;
let closed=false;const closing=f.close().then(()=>{closed=true;});
t.mock.timers.tick(4999);await new Promise(setImmediate);assert.equal(closed,false);
t.mock.timers.tick(1);await closing;
assert.equal(closed,true);assert.equal(f.queueCalls(),1);
assert.ok(f.logs.some(line=>line.includes('proxy-usage-read-failed')));
});
// Row 333 R3 (#1831) ruling 5: each fixture wait races a real-timer deadline and names what it
// waited for. Every timer API is mocked here and never advanced, so only the real timer can fire.
test('R3 every telemetry fixture wait rejects by name at its real-timer deadline',async t=>{
assert.equal(WAIT_MS,5000);
t.mock.timers.enable({apis:['Date','setInterval','setTimeout','setImmediate'],now:Date.now()});
const f=await telemetryFixture(t,{waitMs:50});
const started=performance.now();
await assert.rejects(f.waitLog('never-logged'),{message:'p5 fixture: no log line with "never-logged" within 50 ms'});
await assert.rejects(f.waitQueue(1),{message:'p5 fixture: no usage queue read number 1 within 50 ms'});
await assert.rejects(f.settleInitial(),{message:'p5 fixture: no proxy-usage-read-failed or proxy.usage.reconcile log line within 50 ms'});
await assert.rejects(f.pollSettledAfterTick(t),{message:'p5 fixture: no usage queue read number 1 or proxy-usage-read-failed log line within 50 ms'});
assert.ok(performance.now()-started<WAIT_MS,'the four waits ended on the short deadline');
assert.equal(f.queueCalls(),0);
});
// Row 333 R4 (#1831) ruling 3: the successful poll barrier, with every timer API mocked and no virtual
// tick after the one pollSettledAfterTick makes. Its final yield runs inside the deadline on the real
// setImmediate, so it settles. A mocked yield would never fire; the outer real bound names that.
test('R4 a successful poll barrier settles with every timer API mocked and no later virtual tick',async t=>{
t.mock.timers.enable({apis:['Date','setInterval','setTimeout','setImmediate'],now:Date.now()});
const f=await telemetryFixture(t);await f.start();await f.settleInitial();
assert.equal(f.queueCalls(),1);
let bound;
const outer=new Promise((_,reject)=>{bound=realSetTimeout(()=>reject(new Error('pollSettledAfterTick did not settle')),WAIT_MS*2);});
try{await Promise.race([f.pollSettledAfterTick(t),outer]);}finally{realClearTimeout(bound);}
assert.equal(f.queueCalls(),2);
assert.equal(f.bus.broker.request(f.human(),{verb:'usage'}).proxy.state,'available');
test('configured telemetry stays undrained across startup, native work, ticks and close', async t => {
t.mock.timers.enable({ apis: ['Date', 'setInterval'], now: Date.now() });
const f = await telemetryFixture(t, { hold: true });
await f.start();
assert.equal(f.queueCalls(), 0);
assert.equal(f.state.queued.length, 1);
assert.equal(f.bus.broker.request(f.human(), { verb: 'usage' }).proxy.state, 'unavailable');
await nativeTurn(f);
t.mock.timers.tick(5000);
await new Promise(setImmediate);
assert.equal(f.queueCalls(), 0);
assert.equal(f.state.queued.length, 1);
await f.close();
});
+18 -32
View File
@@ -1,38 +1,24 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { telemetryFixture } from './p5-telemetry-fixture.mjs';
for(const foreign of [true,false])test(`P5 R1 boot identity ${foreign?'refusal prevents queue consumption':'matching sentinel permits telemetry without an agent key'}`,async t=>{
t.mock.timers.enable({apis:['Date','setInterval'],now:Date.now()});
const f=await telemetryFixture(t,{foreign});await f.start();await f.settleInitial();
const human=f.human();assert.equal(f.bus.broker.request(human,{verb:'plugins'}).plugins.usage,'cliproxy-queue');
if(foreign){
await assert.rejects(f.bus.modelPreflight({business:'demo',role:'coder'}),{code:'proxy-open'});
assert.equal(f.queueCalls(),0);assert.equal(f.state.queued.length,1);
await f.pollSettledAfterTick(t);assert.equal(f.queueCalls(),0);assert.equal(f.state.queued.length,1);
assert.equal(f.bus.broker.request(human,{verb:'usage'}).proxy.state,'unavailable');
}else{
assert.equal(f.queueCalls(),1);assert.equal(f.state.queued.length,0);
assert.equal(f.bus.broker.request(human,{verb:'usage'}).proxy.state,'available');
assert.equal(f.bus.credentials.status().some(r=>r.service==='model'&&r.keyId),false);
}
});
test('P5 R1 later sentinel refusal fences every subsequent queue poll while preserving human control',async t=>{
t.mock.timers.enable({apis:['Date','setInterval'],now:Date.now()});
const f=await telemetryFixture(t);await f.start();await f.settleInitial();assert.equal(f.queueCalls(),1);
f.state.keys=[f.other];
await assert.rejects(f.bus.modelPreflight({business:'demo',role:'coder'}),{code:'proxy-open'});
await f.pollSettledAfterTick(t);assert.equal(f.queueCalls(),1);
await f.pollSettledAfterTick(t);assert.equal(f.queueCalls(),1);
const human=f.human();assert.equal(f.bus.broker.request(human,{verb:'plugins'}).plugins.usage,'cliproxy-queue');
assert.equal(f.bus.broker.request(human,{verb:'usage'}).proxy.state,'unavailable');
for (const foreign of [true, false]) test(`configured usage remains undrained with ${foreign ? 'foreign' : 'matching'} proxy identity`, async t => {
t.mock.timers.enable({ apis: ['Date', 'setInterval'], now: Date.now() });
const f = await telemetryFixture(t, { foreign });
await f.start();
t.mock.timers.tick(3000);
await new Promise(setImmediate);
assert.equal(f.queueCalls(), 0);
assert.equal(f.state.queued.length, 1);
assert.equal(f.bus.broker.request(f.human(), { verb: 'usage' }).proxy.state, 'unavailable');
if (foreign) await assert.rejects(f.bus.modelPreflight({ business: 'demo', role: 'coder' }), { code: 'proxy-open' });
});
test('P5 R1 every drain verifies fresh sentinel before consuming a changed listener',async t=>{
t.mock.timers.enable({apis:['Date','setInterval'],now:Date.now()});
const f=await telemetryFixture(t);await f.start();await f.settleInitial();assert.equal(f.queueCalls(),1);
f.state.keys=[f.other];await f.pollSettledAfterTick(t);assert.equal(f.queueCalls(),1);
await assert.rejects(f.bus.modelPreflight({business:'demo',role:'coder'}),{code:'proxy-open'});
const lists=f.state.calls.filter(p=>p==='/v8/management/config/access/api-keys').length;
f.state.keys=[f.own];await f.pollSettledAfterTick(t);assert.equal(f.queueCalls(),1);
assert.equal(f.state.calls.filter(p=>p==='/v8/management/config/access/api-keys').length,lists,'no retry around sticky identity refusal');
test('a later identity change still refuses launch while usage stays undrained', async t => {
const f = await telemetryFixture(t);
await f.start();
f.state.keys = [f.other];
await assert.rejects(f.bus.modelPreflight({ business: 'demo', role: 'coder' }), { code: 'proxy-open' });
assert.equal(f.queueCalls(), 0);
assert.equal(f.state.queued.length, 1);
});
+2 -2
View File
@@ -17,7 +17,7 @@ function deadline(promise, what, ms) {
const expired = new Promise((_, reject) => { timer = realSetTimeout(() => reject(new Error(`p5 fixture: ${what} within ${ms} ms`)), ms); });
return Promise.race([promise, expired]).finally(() => realClearTimeout(timer));
}
export async function telemetryFixture(t, { foreign = false, hold = false, waitMs = WAIT_MS } = {}) {
export async function telemetryFixture(t, { foreign = false, hold = false, waitMs = WAIT_MS, orgMode = false } = {}) {
const root = mkdtempSync(join(tmpdir(), 'p5-telemetry-')), config = join(root,'config'), data = join(root,'data');
mkdirSync(config,{mode:0o700}); mkdirSync(data,{mode:0o700});
const rolesDir = rolesCopy(config), doc = businessDoc(config,'demo');
@@ -64,7 +64,7 @@ export async function telemetryFixture(t, { foreign = false, hold = false, waitM
});
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
let bus=null,startPromise=null,closePromise=null;
const start=()=>startPromise??=(startBroker({dataRoot:data,businesses:{demo:{id:'demo',human:'jason',plugins:doc.plugins,arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{definition:'coder',authority:{withinRole:[],crossRole:[]}}}}},
const start=()=>startPromise??=(startBroker({dataRoot:data,...(orgMode ? {instanceRoot:root} : {}),businesses:{demo:{id:'demo',human:'jason',plugins:doc.plugins,arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{definition:'coder',authority:{withinRole:[],crossRole:[]}}}}},
organization:{dir:config,rolesDir,system:systemFor(config)},proxy:{port:server.address().port,binary:'/fixture-unused'}}).then(value=>(bus=value)));
const release=(fail=false)=>{state.hold=false;for(const res of state.held.splice(0)){if(res.destroyed)continue;if(fail){res.writeHead(503);res.end('{}');}else answerQueue(res);}};
const close=()=>bus?(closePromise??=bus.close()):Promise.resolve();
+12
View File
@@ -90,3 +90,15 @@ test('vikunja:19/309 through the broker, a patch switches the kinds it names and
assert.equal(code({}), 'invalid-plugins');
assert.equal(code({ 'search-source': 'no-such-module' }), 'unknown-plugin');
});
test('plugins read advertises loadable harnesses even when switched off', async t => {
const {startBroker}=await import('../src/runtime.mjs');
const {businessDoc,rolesCopy,systemFor,writeJson}=await import('../../business/tests/helpers.mjs');
const root=mkdtempSync(join(tmpdir(),'plugins-available-'));t.after(()=>rmSync(root,{recursive:true,force:true}));
const rolesDir=rolesCopy(root);const doc=businessDoc(root,'demo');doc.plugins={harness:['claude-code']};
writeJson(join(root,'businesses/demo.json'),doc);mkdirSync(join(root,'data'),{mode:0o700});
const host=await startBroker({dataRoot:join(root,'data'),businesses:{demo:{id:'demo',human:'jason',arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{authority:{withinRole:[],crossRole:[]}}}}},organization:{dir:root,rolesDir,system:systemFor(root)}});t.after(()=>host.close());
const human=host.broker.bindHuman({business:'demo',human:'jason',via:'cli',outsideAgent:true});
const view=host.broker.request(human,{verb:'plugins'});
assert.deepEqual(view.plugins.harness,['claude-code']);assert.ok(view.available.harness.includes('pi'));assert.ok(view.available.harness.includes('claude-code'));
});
@@ -0,0 +1,43 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import {join} from 'node:path';
import {tmpdir} from 'node:os';
import {DatabaseSync} from 'node:sqlite';
import {startBroker} from '../src/runtime.mjs';
import {businessDoc,rolesCopy,systemFor,writeJson} from '../../business/tests/helpers.mjs';
test('role must not materialize a new catalogue key by submitting legacy entry shape',async t=>{
const root=fs.mkdtempSync(join(tmpdir(),'materialize-'));t.after(()=>fs.rmSync(root,{recursive:true,force:true}));
const rolesDir=rolesCopy(root),doc=businessDoc(root,'demo'),file=join(root,'businesses/demo.json');
doc.plugins={...doc.plugins,harness:['pi'],'model-provider':['cliproxy']};doc.roles.coder.vars={...doc.roles.coder.vars,harness:'pi',thinking:'low'};
writeJson(file,doc);fs.mkdirSync(join(root,'data'),{mode:0o700});
const authority=JSON.parse(fs.readFileSync('./roles/chief-of-staff.json','utf8')).authority;
const offered={key:'zai-fixture-new',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-new',label:'Fixture',available:true,reason:null,harnesses:['pi']};
let status='ready';
const modelCatalogue={invalidate(){},read:async()=>({status,models:[offered],notices:[]})};
const bus=await startBroker({dataRoot:join(root,'data'),businesses:{demo:{id:'demo',human:'jason',arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{authority:{withinRole:[],crossRole:[]}},chief:{definition:'chief-of-staff',authority}}}},organization:{dir:root,rolesDir,system:systemFor(root)},modelCatalogue});t.after(()=>bus.close());
const db=new DatabaseSync(join(root,'data/bus/bus.sqlite'),{readOnly:true});t.after(()=>db.close());
const allowed=()=>db.prepare("SELECT count(*) AS n FROM events WHERE kind='action.allowed' AND json_extract(body,'$.action')='organization.change'").get().n;
const human=bus.broker.bindHuman({business:'demo',human:'jason',via:'cli',outsideAgent:true});
const cap=bus.broker.bindLaunch({business:'demo',role:'chief',run:'materialize-chief',harness:'pi'});bus.broker.request(cap,{verb:'role.claim'});
const view=bus.broker.request(human,{verb:'limits'});assert.equal(Object.hasOwn(view.models,offered.key),false);
const models={...view.models,[offered.key]:{provider:offered.provider,model:offered.model,module:offered.module,upstream:offered.upstream}};
const args={revision:view.revision,models,instances:{coder:offered.key}}, bytes=fs.readFileSync(file,'utf8');
await assert.rejects(bus.broker.request(cap,{verb:'organization.set',args}),{code:'organization-model-provider-human-only'});
assert.equal(allowed(),0);assert.equal(fs.readFileSync(file,'utf8'),bytes);
delete models[offered.key].module;delete models[offered.key].upstream;
await assert.rejects(bus.broker.request(cap,{verb:'organization.set',args}),{code:'organization-model-provider-human-only'});
assert.equal(allowed(),0);assert.equal(fs.readFileSync(file,'utf8'),bytes);
// A different, unselected key is still materialization of the trusted identity.
const aliasModels={...view.models,'new-alias':{provider:offered.provider,model:offered.model}};
await assert.rejects(bus.broker.request(cap,{verb:'organization.set',args:{revision:view.revision,models:aliasModels}}),{code:'organization-model-provider-human-only'});
status='unavailable';
await assert.rejects(bus.broker.request(cap,{verb:'organization.set',args:{revision:view.revision,models:aliasModels}}),{code:'model-provider-unavailable'});
assert.equal(allowed(),0);assert.equal(fs.readFileSync(file,'utf8'),bytes);
// A human may add the entry; a role may then select that existing key.
status='ready';
const saved=await bus.broker.request(human,{verb:'limits.set',args:{revision:view.revision,models:aliasModels}});
await bus.broker.request(cap,{verb:'organization.set',args:{revision:saved.revision,instances:{coder:'new-alias'}}});
assert.equal(JSON.parse(fs.readFileSync(file,'utf8')).roles.coder.vars.model,'new-alias');
assert.equal(allowed(),1);
});
@@ -0,0 +1,48 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import {join} from 'node:path';
import {tmpdir} from 'node:os';
import {DatabaseSync} from 'node:sqlite';
import {startBroker} from '../src/runtime.mjs';
import {businessDoc,rolesCopy,systemFor,writeJson} from '../../business/tests/helpers.mjs';
const authority=JSON.parse(fs.readFileSync('./roles/chief-of-staff.json','utf8')).authority;
const oldEntry={provider:'zai',model:'fixture-old'};
const newEntry={provider:'zai',model:'fixture-new'};
const offer={key:'zai-fixture-new',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-new',label:'Fixture',available:true,reason:null,harnesses:['pi']};
async function fixture(t, options={}) {
const root=fs.mkdtempSync(join(tmpdir(),'repoint-'));t.after(()=>fs.rmSync(root,{recursive:true,force:true}));
const rolesDir=rolesCopy(root),doc=businessDoc(root,'demo'),file=join(root,'businesses/demo.json');
doc.plugins={...doc.plugins,harness:['pi'],'model-provider':['cliproxy']};
doc.roles.coder.vars={...doc.roles.coder.vars,harness:'pi',thinking:'low'};
doc.launch.models['existing-alias']=options.existingNew?newEntry:oldEntry;
if(options.selected)doc.roles.coder.vars.model='existing-alias';
if(options.fallback)Object.assign(doc.roles.coder.vars,{'fallback.harness':'pi','fallback.model':'existing-alias','fallback.thinking':'low'});
writeJson(file,doc);fs.mkdirSync(join(root,'data'),{mode:0o700});
const modelCatalogue={invalidate(){},read:async()=>({status:'ready',models:[offer],notices:[]})};
const bus=await startBroker({dataRoot:join(root,'data'),businesses:{demo:{id:'demo',human:'jason',arbiters:{technical:'coder',delivery:'coder'},roles:{...Object.fromEntries(Object.keys(doc.roles).map(key=>[key,{authority:{withinRole:[],crossRole:[]}}])),chief:{definition:'chief-of-staff',authority}}}},organization:{dir:root,rolesDir,system:systemFor(root)},modelCatalogue});t.after(()=>bus.close());
const db=new DatabaseSync(join(root,'data/bus/bus.sqlite'),{readOnly:true});t.after(()=>db.close());
const count=()=>db.prepare("SELECT count(*) AS n FROM events WHERE kind='action.allowed' AND json_extract(body,'$.action')='organization.change'").get().n;
const human=bus.broker.bindHuman({business:'demo',human:'jason',via:'cli',outsideAgent:true});
const role=bus.broker.bindLaunch({business:'demo',role:'chief',run:'independent-chief',harness:'pi'});bus.broker.request(role,{verb:'role.claim'});
const view=()=>bus.broker.request(human,{verb:'limits'});
const request=args=>Promise.resolve().then(()=>bus.broker.request(role,{verb:'organization.set',args}));
return {view,request,count,bytes:()=>fs.readFileSync(file,'utf8')};
}
test('role can change a legacy entry shape without changing its resolved identity',async t=>{
const f=await fixture(t,{existingNew:true}),v=f.view();
const equivalent={provider:'proxy',route:'glm',model:'fixture-new'};
await f.request({revision:v.revision,models:{...v.models,'existing-alias':equivalent}});
assert.deepEqual(JSON.parse(f.bytes()).launch.models['existing-alias'],equivalent);
assert.equal(f.count(),1);
});
for(const [name,options] of [['unselected',{}],['selected primary',{selected:true}],['selected fallback',{fallback:true}]]) {
test('REQUIRED: role must not repoint existing legacy key to new catalogue identity: '+name,async t=>{
const f=await fixture(t,options),v=f.view(),before=f.bytes();
const args={revision:v.revision,models:{...v.models,'existing-alias':newEntry}};
let error;try{await f.request(args);}catch(e){error=e.code;}
t.diagnostic(JSON.stringify({case:name,error:error??null,businessChanged:f.bytes()!==before,authorityConsumed:f.count(),persisted:JSON.parse(f.bytes()).launch.models['existing-alias']}));
assert.equal(error,'organization-model-provider-human-only');
assert.equal(f.bytes(),before);assert.equal(f.count(),0);
});
}
+100
View File
@@ -0,0 +1,100 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { spawn } from 'node:child_process';
import { createProxyLock } from '../src/proxy-lock.mjs';
function fixture(t) {
const dir = fs.mkdtempSync(join(tmpdir(), 's2-lock-'));
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
return { dir, lock: createProxyLock({ stateDir: dir, identity: 'instance-one' }) };
}
const code = name => error => error.code === name;
test('a fence survives a lock owner and blocks another owner', async t => {
const { dir, lock } = fixture(t);
await lock.run(async () => { lock.fence(); });
const other = createProxyLock({ stateDir: dir, identity: 'instance-one' });
assert.throws(() => other.assertCertain(), code('proxy-uncertain'));
await assert.rejects(other.run(async () => {}), code('proxy-uncertain'));
});
test('a conflicting proxy identity refuses instead of sharing state', async t => {
const { dir, lock } = fixture(t);
await lock.run(async () => {});
assert.throws(() => createProxyLock({ stateDir: dir, identity: 'instance-two' }), code('proxy-lock-invalid'));
});
test('an unreadable owner is not guessed stale', async t => {
const { dir, lock } = fixture(t);
fs.writeFileSync(join(dir, 'mosaic-keys.lock'), 'broken', { mode: 0o600 });
await assert.rejects(lock.run(async () => {}), code('proxy-lock-uncertain'));
});
test('independent processes serialize and a dead owner can be recovered', async t => {
const { dir, lock } = fixture(t);
const moduleUrl = new URL('../src/proxy-lock.mjs', import.meta.url).href;
const script = `import {createProxyLock} from ${JSON.stringify(moduleUrl)}; const l=createProxyLock({stateDir:process.argv[1],identity:'instance-one'}); await l.run(async()=>{process.stdout.write('locked\\n'); await new Promise(()=>setInterval(()=>{},1000));});`;
const child = spawn(process.execPath, ['--input-type=module', '-e', script, dir], { stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await new Promise((resolve, reject) => { child.stdout.once('data', resolve); child.once('error', reject); child.once('exit', () => reject(Error('child exited early'))); });
let entered = false;
const waiting = lock.run(async () => { entered = true; });
await new Promise(resolve => setTimeout(resolve, 100));
assert.equal(entered, false);
child.kill('SIGKILL');
await new Promise(resolve => child.once('exit', resolve));
await waiting;
assert.equal(entered, true);
});
test('a process dying with a fence leaves the next owner refused', async t => {
const { dir, lock } = fixture(t);
const moduleUrl = new URL('../src/proxy-lock.mjs', import.meta.url).href;
const script = `import {createProxyLock} from ${JSON.stringify(moduleUrl)}; const l=createProxyLock({stateDir:process.argv[1],identity:'instance-one'}); await l.run(async()=>{l.fence(); process.stdout.write('fenced\\n'); await new Promise(()=>setInterval(()=>{},1000));});`;
const child = spawn(process.execPath, ['--input-type=module', '-e', script, dir], { stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await new Promise((resolve, reject) => { child.stdout.once('data', resolve); child.once('error', reject); child.once('exit', () => reject(Error('child exited early'))); });
child.kill('SIGKILL');
await new Promise(resolve => child.once('exit', resolve));
await assert.rejects(lock.run(async () => {}), code('proxy-uncertain'));
});
test('confirmed writes advance generation and uncertain writes require restart recovery', async t => {
const { lock } = fixture(t);
assert.equal(lock.generation(), 0);
await lock.run(async () => {
lock.fence({ org: 'alpha', op: 'register' });
lock.confirm();
});
assert.equal(lock.generation(), 1);
await lock.run(async () => { lock.fence({ org: 'beta', op: 'issue' }); });
assert.equal(lock.status().org, 'beta');
assert.throws(() => lock.assertCertain(), code('proxy-uncertain'));
await assert.rejects(lock.clear({ restarted: false, read: async () => [] }), code('proxy-restart-required'));
await lock.clear({ restarted: true, read: async () => ['sentinel'] });
assert.equal(lock.status(), null);
assert.equal(lock.generation(), 2);
});
test('a crash while writing the fence remains recoverable after restart', async t => {
const { dir, lock } = fixture(t);
fs.writeFileSync(join(dir, 'mosaic-keys.uncertain'), '{', { mode: 0o600 });
assert.throws(() => lock.assertCertain(), code('proxy-uncertain'));
assert.deepEqual(lock.status(), { invalid: true });
await lock.clear({ restarted: true, read: async () => [] });
assert.equal(lock.status(), null);
});
test('a contender waits while another process publishes its lock owner', async t => {
const { dir, lock } = fixture(t);
const moduleUrl = new URL('../src/proxy-lock.mjs', import.meta.url).href;
const script = `import fs from 'node:fs'; import {createProxyLock} from ${JSON.stringify(moduleUrl)};
const l=createProxyLock({stateDir:process.argv[1],identity:'instance-one'});
const write=fs.writeFileSync; fs.writeFileSync=function(fd,data,...rest){
if(typeof data==='string' && data.includes('"token"')) { process.stdout.write('publishing\\n'); Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,300); }
return write.call(this,fd,data,...rest);
}; await l.run(async()=>{});`;
const child = spawn(process.execPath, ['--input-type=module', '-e', script, dir], { stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await new Promise((resolve, reject) => { child.stdout.once('data', resolve); child.once('error', reject); child.once('exit', () => reject(Error('child exited early'))); });
await lock.run(async () => {});
await new Promise((resolve, reject) => child.once('exit', code => code === 0 ? resolve() : reject(Error(`child exit ${code}`))));
});
@@ -79,3 +79,64 @@ test('client keys return ids, sentinel/foreign labels and only scoped record met
] });
for (const value of s.state.keys) assert.equal(JSON.stringify(result).includes(value), false);
});
test('a human configures a key group model list and the public row shows it after readback', async t => {
const s = await fakeManagement(t, { groups: [{ name: 'fixture-group', 'base-url': 'https://api.z.ai/api/anthropic', keys: [{ 'api-key': 'fixture-secret', auth_index: 'fixture-index' }] }] });
const result = await s.call('proxy.group.models.set', { accountId: id('fixture-index'), models: ['fixture-one', 'fixture-two'] });
assert.deepEqual(result, { accountId: id('fixture-index'), models: ['fixture-one', 'fixture-two'] });
assert.deepEqual((await s.call('proxy.accounts')).accounts[0].models, ['fixture-one', 'fixture-two']);
assert.equal(JSON.stringify(result).includes('fixture-secret'), false);
for (const models of [['bad model'], ['x', 'x'], Array(33).fill('fixture'), ['../bad']])
await assert.rejects(s.call('proxy.group.models.set', { accountId: id('fixture-index'), models }), { code: 'models-invalid' });
});
test('models save preserves retained upstream aliases while adding and removing ids', async t => {
const s=await fakeManagement(t,{groups:[{name:'fixture-group','base-url':'https://api.z.ai/api/anthropic',keys:[{'api-key':'fixture-secret',auth_index:'fixture-index'}],models:[{name:'fixture-upstream',alias:'fixture-friendly'},{name:'fixture-remove'}]}]});
const accountId=id('fixture-index');
await s.call('proxy.group.models.set',{accountId,models:['fixture-friendly','fixture-remove']});
assert.deepEqual(s.state.groups[0].models,[{name:'fixture-upstream',alias:'fixture-friendly'},{name:'fixture-remove'}]);
await s.call('proxy.group.models.set',{accountId,models:['fixture-friendly','fixture-added']});
assert.deepEqual(s.state.groups[0].models,[{name:'fixture-upstream',alias:'fixture-friendly'},{name:'fixture-added'}]);
});
test('models save refuses ambiguous displayed aliases without writing', async t => {
const s=await fakeManagement(t,{groups:[{name:'fixture-group','base-url':'https://api.z.ai/api/anthropic',keys:[{'api-key':'fixture-secret',auth_index:'fixture-index'}],models:[{name:'fixture-one',alias:'fixture-same'},{name:'fixture-two',alias:'fixture-same'}]}]});
await assert.rejects(s.call('proxy.group.models.set',{accountId:id('fixture-index'),models:['fixture-same']}),{code:'models-ambiguous'});
assert.equal(s.state.calls.some(c=>c.startsWith('PUT ')),false);
});
test('models readback confirms both upstream name and displayed alias', async () => {
const {ProxyPage}=await import('../src/proxy-page.mjs');
let groups=[{name:'fixture-group','base-url':'https://api.z.ai/api/anthropic',keys:[{auth_index:'fixture-index','api-key':'fixture-secret'}],models:[{name:'fixture-upstream',alias:'fixture-friendly'}]}];
const client={getCredentials:async()=>({files:[]}),getUpstreamGroups:async()=>structuredClone(groups),putUpstreamGroups:async next=>{groups=structuredClone(next);groups[0].models[0].name='fixture-wrong';}};
const page=new ProxyPage({client,instanceTag:'fixture'});
await assert.rejects(page.request('proxy.group.models.set',{accountId:id('fixture-index'),models:['fixture-friendly']},{business:'demo',human:'owner'}),{code:'models-unconfirmed'});
});
test('organization proxy page writes refuse while the shared proxy is uncertain', async t => {
const fs = await import('node:fs'); const { join } = await import('node:path'); const { tmpdir } = await import('node:os');
const { createProxyLock } = await import('../src/proxy-lock.mjs');
const dir = fs.mkdtempSync(join(tmpdir(), 'p4-lock-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
const lock = createProxyLock({ stateDir: dir, identity: 'fixture' });
const s = await fakeManagement(t, { lock, org: 'alpha' });
await lock.run(async () => lock.fence({ org: 'beta', op: 'keys.put' }));
await assert.rejects(s.call('proxy.apikey.add', { provider: 'z.ai', key: 'p4-canary' }), { code: 'proxy-uncertain' });
assert.equal(s.state.receives, 0);
});
test('organization key view does not classify a sibling key as its sentinel', async t => {
const sibling = `mso-fixture-alpha-sentinel-key-${'A'.repeat(43)}`;
const s = await fakeManagement(t, { org: 'alpha', keys: [sibling] });
assert.equal((await s.call('proxy.keys')).keys[0].management, 'not stack-managed');
});
test('OAuth callback changes the shared generation for other organization catalogues', async t => {
const fs = await import('node:fs'); const { join } = await import('node:path'); const { tmpdir } = await import('node:os');
const { createProxyLock } = await import('../src/proxy-lock.mjs');
const dir = fs.mkdtempSync(join(tmpdir(), 'p4-oauth-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
const lock = createProxyLock({ stateDir: dir, identity: 'fixture' });
const s = await fakeManagement(t, { lock, org: 'alpha' });
const login = await s.call('proxy.login.start', { provider: 'claude' });
await s.call('proxy.login.submit', { redirect_url: `http://localhost/?state=${login.state}&code=fixture` });
assert.equal(lock.generation(), 1);
});
+1 -1
View File
@@ -43,7 +43,7 @@ export async function fakeManagement(t, options = {}) {
await new Promise(r => server.listen(0, '127.0.0.1', r));
t.after(() => new Promise(r => server.close(r)));
const client = new ManagementClient({ baseUrl: `http://127.0.0.1:${server.address().port}`, key: 'p4-management', receipt: entry => state.receipts.push(entry) });
const page = new ProxyPage({ client, instanceTag: 'fixture', keyRecords: () => options.records ?? [] });
const page = new ProxyPage({ client, instanceTag: 'fixture', lock: options.lock ?? null, org: options.org ?? null, keyRecords: () => options.records ?? [] });
const call = (verb, args = {}) => page.request(verb, args, { business: 'demo', human: 'owner' });
return { state, page, call, client, server };
}
@@ -78,3 +78,16 @@ for (const args of [{ accountId: 'bad', priority: 1 }, { accountId: 'a'.repeat(1
{ accountId: 'a'.repeat(12), priority: 1001 }]) test('priority-invalid ' + JSON.stringify(args), async () => {
const s = await setup(); await assert.rejects(s.call('proxy.priority.set', args), { code: 'priority-invalid' });
});
test('successful account configuration and login invalidate the catalogue', async () => {
const { ProxyPage } = await import(moduleURL);
let changed = 0;
const client = { getCredentials: async () => ({ files: [] }), getUpstreamGroups: async () => [], putUpstreamGroups: async () => ({}),
oauthStart: async () => ({ state: 'state', url: 'https://claude.ai/oauth/authorize?state=state' }), oauthStatus: async () => ({ status: 'ok' }) };
const page = new ProxyPage({ client, instanceTag: 'fixture', onChange: () => { changed++; } });
const scope = { business: 'fixture', human: 'owner' };
await page.request('proxy.apikey.add', { provider: 'z.ai', key: 'fixture-key' }, scope);
assert.equal(changed, 2);
await page.request('proxy.login.start', { provider: 'claude' }, scope);
await page.request('proxy.login.status', { state: 'state' }, scope);
assert.equal(changed, 3);
});
+1 -1
View File
@@ -8,7 +8,7 @@ import { Broker } from '../src/broker.mjs';
import { Store } from '../src/store.mjs';
const verbs = ['proxy.accounts', 'proxy.login.start', 'proxy.login.submit', 'proxy.login.status',
'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.keys'];
'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys'];
function setup(t, proxyPage = async () => ({ ok: true })) {
const root = mkdtempSync(join(tmpdir(), 'p4-bus-'));
const store = new Store(root);
@@ -0,0 +1,17 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createProxyUsage } from '../src/proxy-usage.mjs';
test('configured usage cannot drain the proxy on a default bus', async () => {
let calls = 0;
const usage = createProxyUsage({
store: { all: () => [] },
read: () => ({ alpha: { plugins: { usage: 'cliproxy-queue' } } }),
client: () => { calls++; return null; },
records: () => [], log: () => {},
});
await usage.tick();
assert.equal(calls, 0);
assert.equal(usage.view('alpha').state, 'unavailable');
await usage.close();
});
+2 -2
View File
@@ -20,7 +20,7 @@ async function fixture(t,{makeLoader}={}) {
await new Promise(r=>server.listen(0,'127.0.0.1',r));
const client=new ManagementClient({baseUrl:`http://127.0.0.1:${server.address().port}`,key:'fake-management',receipt:e=>logs.push(e)});
let doc={plugins:{},launch:{limits:{tokens:{keys:{[keyId]:{limit:1,since:'2026-01-01T00:00:00.000Z'}}}}}};
const reader=createProxyUsage({store,read:()=>({demo:doc}),client:()=>noClient?null:client,...(makeLoader?{makeLoader}:{}),records:()=>recs,log:e=>logs.push(e),now:()=>now});
const reader=createProxyUsage({store,enableDrain:true,read:()=>({demo:doc}),client:()=>noClient?null:client,...(makeLoader?{makeLoader}:{}),records:()=>recs,log:e=>logs.push(e),now:()=>now});
t.after(async()=>{await reader.close();await new Promise(r=>server.close(r));store.close();rmSync(root,{recursive:true,force:true});});
const event=(over={})=>({api_key:key,source,failed:true,fail:{status_code:503,body:source},tokens:{input_tokens:9,output_tokens:2},...over});
return {store,logs,calls,reader,client,event,setEvents:v=>{events=v;},clock:v=>{now=v;},enable:(pollSeconds)=>{doc={...doc,plugins:{usage:'cliproxy-queue','limit-check':['token-limit']},...(pollSeconds===undefined?{}:{usage:{cliproxyQueue:{pollSeconds}}})};},doc:()=>doc,fail:()=>{fail=true;},noClient:v=>{noClient=v;},setRecords:v=>{recs=v;}};
@@ -149,7 +149,7 @@ test('#778 a failed transaction drops the batch for every enabled business',asyn
const f=await fixture(t);f.enable();
const broken={all:(...a)=>f.store.all(...a),run:(...a)=>f.store.run(...a),transaction:()=>{throw new Error('disk full');}};
const both={demo:f.doc(),other:f.doc()};
const reader=createProxyUsage({store:broken,read:()=>both,client:()=>f.client,records:()=>[{instance:'demo/coder',service:'model',keyId,retiring:[]}],log:e=>f.logs.push(e),now:()=>0});
const reader=createProxyUsage({store:broken,enableDrain:true,read:()=>both,client:()=>f.client,records:()=>[{instance:'demo/coder',service:'model',keyId,retiring:[]}],log:e=>f.logs.push(e),now:()=>0});
t.after(()=>reader.close());
f.setEvents([f.event()]);await reader.tick();
for(const business of ['demo','other']){assert.equal(reader.view(business).droppedBatches,1);assert.equal(reader.view(business).state,'unavailable');}
+68
View File
@@ -148,3 +148,71 @@ test('row 65 runtime: routes and routes.set reach the organization file with the
assert.equal(readFileSync(file, 'utf8'), before);
assert.equal(log().length, entries, 'a refused write logs nothing');
});
test('fixture provider catalogue appears in actual routes and materializes the selected model', async t => {
const { startBroker } = await import('../src/runtime.mjs');
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../business/tests/helpers.mjs');
const root = scratch(t, 'routes-catalogue-'), rolesDir = rolesCopy(root), doc = businessDoc(root, 'demo');
doc.plugins = { ...doc.plugins, harness: ['pi'], 'model-provider': ['cliproxy'] };
for (const r of Object.values(doc.roles)) r.vars = { harness: 'pi', model: Object.keys(doc.launch.models)[0], thinking: 'high', ...r.vars };
const file = join(root, 'businesses/demo.json'); writeJson(file, doc); mkdirSync(join(root, 'data'), { mode: 0o700 });
const offered={ key:'zai-fixture-id',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-id',label:'fixture-id',available:true,reason:null,harnesses:['pi'] };
const catalogues={ read:async()=>({status:'ready',observedAt:'2026-11-06T00:00:00.000Z',models:[offered],notices:[]}),invalidate:()=>{} };
const bus = await startBroker({ dataRoot: join(root, 'data'), businesses: { demo: { id:'demo',human:'jason',arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{authority:{withinRole:[],crossRole:[]}}} } }, organization:{dir:root,rolesDir,system:systemFor(root)}, modelCatalogue:catalogues });
t.after(() => bus.close());
const human=bus.broker.bindHuman({business:'demo',human:'jason',via:'cli',outsideAgent:true});
const before=await bus.broker.request(human,{verb:'routes'});
assert.equal(before.catalogue.models[0].key,'zai-fixture-id');
assert.equal(before.models.includes('zai-fixture-id'),false);
const saved=await bus.broker.request(human,{verb:'routes.set',args:{revision:before.revision,routes:{coder:{harness:'pi',model:'zai-fixture-id',thinking:'low',fallback:null}}}});
assert.equal(saved.instances.coder.model,'zai-fixture-id');
assert.equal(JSON.parse(readFileSync(file,'utf8')).launch.models['zai-fixture-id'].model,'fixture-id');
});
for (const writer of ['limits.set','organization.set','instances.set']) test(`${writer} selects only available compatible existing provider models`, async t => {
const { startBroker } = await import('../src/runtime.mjs');
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../business/tests/helpers.mjs');
const root=scratch(t,'provider-writer-'),rolesDir=rolesCopy(root),doc=businessDoc(root,'demo');
doc.plugins={...doc.plugins,harness:['pi','claude-code'],'model-provider':['cliproxy']};
doc.launch.models['fixture-persisted']={provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'};
doc.roles.coder.vars={...doc.roles.coder.vars,harness:'pi',thinking:'low'};
writeJson(join(root,'businesses/demo.json'),doc);mkdirSync(join(root,'data'),{mode:0o700});
let mode='absent';
const offered={key:'fixture-persisted',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-id',label:'fixture',available:true,reason:null,harnesses:['pi']};
const catalogue={read:async()=>({status:'ready',observedAt:'2026-11-06T00:00:00.000Z',models:mode==='absent'?[]:[{...offered,harnesses:mode==='incompatible'?['claude-code']:['pi']}],notices:[]}),invalidate(){}};
const bus=await startBroker({dataRoot:join(root,'data'),businesses:{demo:{id:'demo',human:'jason',arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{authority:{withinRole:[],crossRole:[]}},chief:{definition:'chief-of-staff',authority:chiefAuthority}}}},organization:{dir:root,rolesDir,system:systemFor(root)},modelCatalogue:catalogue});t.after(()=>bus.close());
const human=bus.broker.bindHuman({business:'demo',human:'jason',via:'cli',outsideAgent:true});
const cap=writer==='organization.set'?bus.broker.bindLaunch({business:'demo',role:'chief',run:'provider-chief',harness:'pi'}):human;
if(writer==='organization.set')bus.broker.request(cap,{verb:'role.claim'});
const view=bus.broker.request(human,{verb:'limits'});
const args=writer==='instances.set'?{revision:view.revision,instance:'coder',op:'change',vars:{...doc.roles.coder.vars,model:'fixture-persisted'}}:{revision:view.revision,instances:{coder:'fixture-persisted'}};
await assert.rejects(Promise.resolve().then(()=>bus.broker.request(cap,{verb:writer,args})),{code:'model-provider-unavailable'});
mode='incompatible';await assert.rejects(Promise.resolve().then(()=>bus.broker.request(cap,{verb:writer,args})),{code:'model-harness-incompatible'});
mode='available';const result=await bus.broker.request(cap,{verb:writer,args});assert.ok(result.transaction);
assert.equal(JSON.parse(readFileSync(join(root,'businesses/demo.json'),'utf8')).roles.coder.vars.model,'fixture-persisted');
});
test('legacy routes follow enabled module mappings while unchanged unavailable selections remain saveable', async t => {
const {startBroker}=await import('../src/runtime.mjs');const {businessDoc,rolesCopy,systemFor,writeJson}=await import('../../business/tests/helpers.mjs');
const root=scratch(t,'legacy-catalogue-'),rolesDir=rolesCopy(root),doc=businessDoc(root,'demo');
doc.plugins={...doc.plugins,harness:['pi','claude-code'],'model-provider':['cliproxy']};
doc.launch.models['legacy-one']={provider:'anthropic',model:'fixture-one'};doc.launch.models['legacy-two']={provider:'zai',model:'fixture-two'};
doc.roles.coder.vars={...doc.roles.coder.vars,harness:'pi',model:'legacy-one',thinking:'low'};
doc.roles.reviewer.vars={...doc.roles.reviewer.vars,harness:'pi',model:'legacy-one',thinking:'low'};
writeJson(join(root,'businesses/demo.json'),doc);mkdirSync(join(root,'data'),{mode:0o700});
let state='ready';const offered={key:'zai-fixture-two',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-two',label:'fixture',available:true,reason:null,harnesses:['pi']};
const catalogue={read:async()=>({status:state,observedAt:'2026-11-06T00:00:00.000Z',models:[offered],notices:[]}),invalidate(){}};
const bus=await startBroker({dataRoot:join(root,'data'),businesses:{demo:{id:'demo',human:'jason',arbiters:{technical:'coder',delivery:'coder'},roles:{coder:{authority:{withinRole:[],crossRole:[]}}}}},organization:{dir:root,rolesDir,system:systemFor(root)},modelCatalogue:catalogue});t.after(()=>bus.close());
const human=bus.broker.bindHuman({business:'demo',human:'jason',via:'cli',outsideAgent:true});const request=(verb,args)=>bus.broker.request(human,{verb,args});
let view=await request('routes');assert.deepEqual(view.catalogue.legacy['legacy-one'],{module:'cliproxy',upstream:'claude',model:'fixture-one'});
const pick=(harness,model,fallback=null)=>({harness,model,thinking:'low',fallback});
await assert.rejects(request('routes.set',{revision:view.revision,routes:{coder:pick('claude-code','legacy-two')}}),{code:'model-harness-incompatible'});
await assert.rejects(request('routes.set',{revision:view.revision,routes:{coder:pick('pi','legacy-two',{harness:'pi',model:'legacy-one',thinking:'low'})}}),{code:'model-provider-unavailable'});
const saved=await request('routes.set',{revision:view.revision,routes:{coder:pick('pi','legacy-two')}});assert.equal(saved.instances.reviewer.model,'legacy-one');
state='unavailable';view=await request('routes');await assert.rejects(request('routes.set',{revision:view.revision,routes:{coder:pick('pi','legacy-one')}}),{code:'model-provider-unavailable'});
});
test('the tester cannot switch a model-provider plugin', t => {
const store=new Store(scratch(t,'provider-switch-'));t.after(()=>store.close());
const b=new Broker({store,businesses:biz(),organization:()=>({ok:true})});const tester=b.bindTester({business:'demo'});
assert.throws(()=>b.request(tester,{verb:'plugins.set',args:{revision:'a'.repeat(64),plugins:{'model-provider':['fixture']}}}),{code:'human-required'});
});
@@ -0,0 +1,15 @@
import { Credentials } from '../src/credentials.mjs';
import { ModelKeys } from '../src/model-keys.mjs';
import { createProxyLock } from '../src/proxy-lock.mjs';
import { ManagementClient } from '../../model-proxy/src/client.mjs';
import { join } from 'node:path';
const [root, configDir, baseUrl, org] = process.argv.slice(2);
const credentials = new Credentials({ storeRoot: join(root, org, 'secrets'), dataRoot: join(root, org), modelAgents: [`${org}/worker`] });
const client = new ManagementClient({ baseUrl, key: 'fixture-management', timeout: 5000 });
const lock = createProxyLock({ stateDir: join(root, 'proxy'), identity: `${configDir}:${new URL(baseUrl).port}` });
try {
const manager = new ModelKeys({ credentials, client, configDir, baseUrl, org, lock });
await manager.reconcile();
const answer = await manager.ensure(`${org}/worker`);
process.stdout.write(JSON.stringify({ key: answer.key }) + '\n');
} finally { credentials.close(); }
+110
View File
@@ -0,0 +1,110 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import http from 'node:http';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { Credentials } from '../src/credentials.mjs';
import { ModelKeys } from '../src/model-keys.mjs';
import { createProxyLock } from '../src/proxy-lock.mjs';
import { ManagementClient } from '../../model-proxy/src/client.mjs';
async function fixture(t) {
const root = fs.mkdtempSync(join(tmpdir(), 'shared-proxy-'));
const configDir = join(root, 'config'); fs.mkdirSync(configDir, { mode: 0o700 });
const stateDir = join(root, 'proxy'); fs.mkdirSync(stateDir, { mode: 0o700 });
const state = { keys: ['fixture-foreign'], putDelay: 0, putStarted: null };
const server = http.createServer(async (req, res) => {
if (req.headers.authorization !== 'Bearer fixture-management' && req.url !== '/v1/models') { res.writeHead(401); res.end('{}'); return; }
if (req.url === '/v1/models') { res.writeHead(state.keys.includes(req.headers.authorization?.slice(7)) ? 200 : 401); res.end('{}'); return; }
if (req.method === 'GET') { res.end(JSON.stringify(state.keys)); return; }
const chunks = []; for await (const chunk of req) chunks.push(chunk);
const next = JSON.parse(Buffer.concat(chunks)); state.putStarted?.();
if (state.putDelay) await new Promise(resolve => setTimeout(resolve, state.putDelay));
state.keys = next; res.end('{"status":"ok"}');
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const baseUrl = `http://127.0.0.1:${server.address().port}`;
const identity = `${configDir}:${server.address().port}`;
const lock = createProxyLock({ stateDir, identity });
const make = (org, timeout = 5000) => {
fs.mkdirSync(join(root, org, 'secrets'), { recursive: true, mode: 0o700 });
const credentials = new Credentials({ storeRoot: join(root, org, 'secrets'), dataRoot: join(root, org), modelAgents: [`${org}/worker`] });
t.after(() => credentials.close());
const client = new ManagementClient({ baseUrl, key: 'fixture-management', timeout });
return { client, manager: new ModelKeys({ credentials, client, configDir, baseUrl, org, lock }) };
};
t.after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); fs.rmSync(root, { recursive: true, force: true }); });
return { root, configDir, stateDir, state, server, baseUrl, lock, make };
}
function worker(f, org) {
fs.mkdirSync(join(f.root, org, 'secrets'), { recursive: true, mode: 0o700 });
const child = spawn(process.execPath, [new URL('./shared-proxy-worker.mjs', import.meta.url).pathname, f.root, f.configDir, f.baseUrl, org], { stdio: ['ignore', 'pipe', 'pipe'] });
let out = '', err = ''; child.stdout.on('data', c => { out += c; }); child.stderr.on('data', c => { err += c; });
return { child, result: once(child, 'close').then(([code, signal]) => { if (code !== 0) throw Error(`worker ${org} exited ${code}/${signal}: ${err}`); return JSON.parse(out); }) };
}
test('two processes register and issue concurrently without losing sibling keys', async t => {
const f = await fixture(t), a = worker(f, 'alpha'), b = worker(f, 'beta');
const [alpha, beta] = await Promise.all([a.result, b.result]);
assert.ok(f.state.keys.includes(alpha.key)); assert.ok(f.state.keys.includes(beta.key));
assert.equal(await f.make('gamma').client.probeKey(alpha.key), 200);
assert.equal(f.state.keys.filter(key => key.includes('-alpha-sentinel-')).length, 1);
assert.equal(f.state.keys.filter(key => key.includes('-beta-sentinel-')).length, 1);
});
test('a delayed PUT leaves the fence set until restart-stated recovery and registration retries', async t => {
const f = await fixture(t), { manager, client } = f.make('alpha', 30);
f.state.putDelay = 150;
await assert.rejects(manager.reconcile(), { code: 'proxy-uncertain' });
assert.throws(() => manager.assertLaunchable('alpha/worker'), { code: 'proxy-uncertain' });
await assert.rejects(manager.ensure('alpha/worker'), { code: 'proxy-uncertain' });
await assert.rejects(f.lock.clear({ restarted: false, read: () => client.getKeys() }), { code: 'proxy-restart-required' });
await new Promise(resolve => setTimeout(resolve, 200));
assert.equal(f.lock.status().op, 'sentinel.register');
await f.lock.clear({ restarted: true, read: () => client.getKeys() });
f.state.putDelay = 0;
await manager.reconcile(); await manager.reconcile();
assert.equal(f.state.keys.filter(key => key.includes('-alpha-sentinel-')).length, 1);
});
test('a process killed during registration recovers the existing sentinel idempotently', async t => {
const f = await fixture(t); f.state.putDelay = 150;
let arrived; const started = new Promise(resolve => { arrived = resolve; }); f.state.putStarted = arrived;
const w = worker(f, 'alpha'); w.result.catch(() => {});
await started; w.child.kill('SIGKILL'); await once(w.child, 'close');
await new Promise(resolve => setTimeout(resolve, 200));
assert.equal(f.lock.status().op, 'sentinel.register');
const { manager, client } = f.make('alpha');
await assert.rejects(manager.reconcile(), { code: 'proxy-uncertain' });
await f.lock.clear({ restarted: true, read: () => client.getKeys() });
f.state.putDelay = 0; f.state.putStarted = null;
await manager.reconcile();
assert.equal(f.state.keys.filter(key => key.includes('-alpha-sentinel-')).length, 1);
});
test('a registered organization refuses a missing sentinel instead of trusting a replacement proxy', async t => {
const f = await fixture(t), { manager } = f.make('alpha');
await manager.reconcile();
f.state.keys = f.state.keys.filter(key => !key.includes('-alpha-sentinel-'));
await assert.rejects(manager.reconcile(), { code: 'proxy-open' });
assert.equal(f.state.keys.some(key => key.includes('-alpha-sentinel-')), false);
});
test('organization IDs ending in sentinel or key cannot collide with a sibling namespace', async t => {
const f = await fixture(t), alpha = f.make('alpha'), sibling = f.make('alpha-sentinel');
await alpha.manager.reconcile(); await sibling.manager.reconcile();
const { key } = await sibling.manager.ensure('alpha-sentinel/worker');
await alpha.manager.reconcile();
assert.equal(await sibling.client.probeKey(key), 200);
assert.equal(f.state.keys.filter(value => value.includes('-alpha-sentinel-sentinel-')).length, 1);
});
test('an uncertain sibling write blocks completing a local revocation', async t => {
const f = await fixture(t), { manager } = f.make('alpha');
await manager.reconcile(); await manager.ensure('alpha/worker'); await manager.prepareRevoke('alpha/worker');
await f.lock.run(async () => f.lock.fence({ org: 'beta', op: 'keys.put' }));
assert.throws(() => manager.completeRevoke('alpha/worker'), { code: 'proxy-uncertain' });
assert.equal(manager.hasRecord('alpha/worker'), true);
});
+33
View File
@@ -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 `<dataRoot>/org-runtime/<id>`
root, which carries an instance-and-ID marker. It is not a system config.
The organization service writes the existing `businesses/<id>.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.
+30
View File
@@ -0,0 +1,30 @@
// The organization file's `appearance` (stream S4, PLAN C5): { theme, mode }, which theme the
// Console shows for this organization and in which mode. Both are optional, and a file without
// `appearance` is valid: the Console then shows its built-in default theme in the system mode.
//
// `theme` is a module name (the plugin name pattern), checked for shape only. Whether a module of
// that name is installed is the Console's question at render time, not the file's: a file that
// names a theme which was since uninstalled stays valid and keeps its choice, and the Console
// shows the default with a warning until the module is back.
import { NAME } from "../../plugins/src/kinds.mjs";
import { refuse } from "./errors.mjs";
import { requireObject, rejectUnknownKeys } from "./util.mjs";
export const APPEARANCE_MODES = Object.freeze(["system", "light", "dim", "dark"]);
// The checked, frozen appearance, or undefined when the file has none.
export function checkAppearance(value, where) {
if (value === undefined) return undefined;
requireObject(value, where);
rejectUnknownKeys(value, ["theme", "mode"], where);
if (Object.hasOwn(value, "theme") && (typeof value.theme !== "string" || value.theme.length > 64 || !NAME.test(value.theme))) {
refuse(`${where}.theme must be an installed theme's name, matching ${NAME} (got ${JSON.stringify(value.theme)})`);
}
if (Object.hasOwn(value, "mode") && !APPEARANCE_MODES.includes(value.mode)) {
refuse(`${where}.mode must be one of ${APPEARANCE_MODES.join(", ")} (got ${JSON.stringify(value.mode)})`);
}
return Object.freeze({
...(Object.hasOwn(value, "theme") ? { theme: value.theme } : {}),
...(Object.hasOwn(value, "mode") ? { mode: value.mode } : {}),
});
}
+14 -4
View File
@@ -13,6 +13,7 @@ import { END_PROTECTED } from "./vocabulary.mjs";
import { checkPlugins, checkUsageSettings } from "../../plugins/src/kinds.mjs";
import { checkMapping } from "../../library/src/mapping.mjs";
import { forkProblem, giteaName, parseGiteaUrl, HOST_SERVICE, CREATIVE_DEFINITIONS } from "./vocabulary.mjs";
import { checkAppearance } from "./appearance.mjs";
import {
requireObject, rejectUnknownKeys, requireId, requirePositiveInt, requireString, requireDistinctList, readJsonFile, deepFreeze,
} from "./util.mjs";
@@ -21,7 +22,7 @@ const BOT_NAME = /^bot-[a-z0-9][a-z0-9._-]{0,60}$/;
// How long a thread's session stays up with no turn before the launcher stops
// it, when launch.threadIdleSeconds is absent (REQ-THREAD-2, row 121).
export const THREAD_IDLE_SECONDS = 900;
const TOP_KEYS = ["businessVersion", "id", "human", "arbiters", "projects", "vars", "tracker", "roles", "launch", "plugins", "hosts", "retired", "replyLimits", "usage"];
const TOP_KEYS = ["businessVersion", "id", "human", "arbiters", "projects", "vars", "tracker", "roles", "launch", "plugins", "hosts", "retired", "replyLimits", "usage", "appearance"];
// The config directory: next to the system config, $MOSAIC_CONFIG or
// ~/.config/mosaic-dev/config.json, the same rule mosaic-config.mjs uses.
@@ -276,7 +277,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 +287,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 ?? []) {
@@ -313,11 +322,12 @@ export function validateBusinessDocument(document, file, { rolesDir }) {
const plugins = document.plugins === undefined ? {}
: structuredClone(checkPlugins(document.plugins, `${file} plugins`, { fail: (_code, message) => refuse(message) }));
const usage = document.usage === undefined ? undefined : checkUsageSettings(document.usage);
const appearance = checkAppearance(document.appearance, `${file} appearance`);
const retired = document.retired === undefined ? undefined : checkRetired(document.retired, roles, file);
return deepFreeze({
businessVersion: 1, id: document.id, file, human, arbiters, projects, vars, tracker, roles, launch, plugins, definitions,
...(retired ? { retired } : {}), ...(usage === undefined ? {} : { usage }),
...(retired ? { retired } : {}), ...(usage === undefined ? {} : { usage }), ...(appearance === undefined ? {} : { appearance }),
});
}
+5 -5
View File
@@ -199,7 +199,7 @@ export function instanceSettings({ dir, rolesDir, system, levels = () => null, r
// add needs definition; change needs at least one of definition, vars and launch. vars replaces
// the instance's vars whole. Row 194 (D3): the writer's restart is false. The bus reloads the
// business after the write and sets restart from that reload (runtime.mjs).
function set(business, args, actor) {
function set(business, args, actor, catalogue = null) {
if (!isPlainObject(args) || Object.keys(args).some(key => !SET_KEYS.includes(key))) {
throw new OrganizationError("invalid-request");
}
@@ -223,7 +223,7 @@ export function instanceSettings({ dir, rolesDir, system, levels = () => null, r
if (Object.keys(args).some(key => !["revision", "instance", "op"].includes(key))) throw new OrganizationError("invalid-request");
const services = Object.keys(old.roles[instance]?.credentials ?? {});
const beforeWrite = revoke ? () => revoke(business, instance, "delete", services) : null;
const result = commit(business, old, planDelete(old, instance, actor), { revision: args.revision, actor, beforeWrite });
const result = commit(business, old, planDelete(old, instance, actor), { revision: args.revision, actor, beforeWrite, catalogue });
// sage-1610 (b): the bot user and any token it holds live in Vikunja, which this write
// can't reach (row 199 A5).
const bot = old.roles[instance]?.tracker?.bot;
@@ -282,7 +282,7 @@ export function instanceSettings({ dir, rolesDir, system, levels = () => null, r
`${instance} may retain credentials only between creative definitions with exactly the same services`);
}
}
return commit(business, old, next, { revision: args.revision, actor });
return commit(business, old, next, { revision: args.revision, actor, catalogue });
}
// Row 194 (V5): the delete's checks against the file as it stands, and its document. The broker
@@ -436,7 +436,7 @@ export function instanceSettings({ dir, rolesDir, system, levels = () => null, r
// The checks every write runs, then the write, which runs beforeWrite (a fire's or a delete's
// revoke) under its lock (row 239). A check that refuses revokes nothing, and a revoke that throws
// writes nothing. beforeWrite must not write this business: the writer holds its lock.
function commit(business, old, next, { revision, actor, context, beforeWrite = null }) {
function commit(business, old, next, { revision, actor, context, beforeWrite = null, catalogue = null }) {
if (canonicalJson(next) === canonicalJson(old)) throw new OrganizationError("organization-no-change");
// The checks mosaic business validate runs, before the writer publishes anything, then the
@@ -449,7 +449,7 @@ export function instanceSettings({ dir, rolesDir, system, levels = () => null, r
} catch (e) {
if (e.name !== "BusinessError") throw e;
}
checkRoutes({ proposed, old: before, levels });
checkRoutes({ proposed, old: before, levels, catalogue });
for (const [project, entry] of Object.entries(proposed.projects)) {
if (entry.kind === 'library') continue;
if (!existsSync(projectFilePath(entry.root))) continue;
+63 -9
View File
@@ -120,7 +120,7 @@ function reset(limits, paths, now) {
// model change still saves while plugins.harness is unset; that instance's launch refuses instead.
// A route var the instance doesn't set is left to the launch, which refuses no-route.
const ROUTES = Object.freeze([["harness", "model", "thinking"], FALLBACK_VARS]);
export function checkRoutes({ proposed, old = null, levels }) {
export function checkRoutes({ proposed, old = null, levels, catalogue = null }) {
const on = Array.isArray(proposed.plugins?.harness) ? proposed.plugins.harness : [];
for (const [instance, entry] of Object.entries(proposed.roles)) {
const vars = entry.vars;
@@ -130,6 +130,23 @@ export function checkRoutes({ proposed, old = null, levels }) {
if (proposed.launch && Object.hasOwn(vars, model) && !Object.hasOwn(proposed.launch.models, vars[model])) {
throw new OrganizationError("unknown-model", `${instance} ${model} ${JSON.stringify(vars[model] ?? null)} isn't a launch.models key`);
}
const currentEntry = proposed.launch?.models?.[vars[model]];
const previousEntry = old?.launch?.models?.[before[model]];
const changed = [harness, model, thinking].some(field => vars[field] !== before[field]) || canonicalJson(currentEntry ?? null) !== canonicalJson(previousEntry ?? null);
if (changed && previousEntry?.module && !currentEntry?.module && vars[model] === before[model]) throw new OrganizationError('model-provider-unavailable');
if (changed && currentEntry?.module) {
if (catalogue?.status !== 'ready') throw new OrganizationError('model-provider-unavailable');
const offered = catalogue.models?.find(m => m.key === vars[model]);
if (!offered || !offered.available || !proposed.plugins?.['model-provider']?.includes(currentEntry.module) || canonicalJson(currentEntry) !== canonicalJson({ provider: offered.provider, model: offered.model, module: offered.module, upstream: offered.upstream })) throw new OrganizationError('model-provider-unavailable');
if (!offered.harnesses.includes(vars[harness])) throw new OrganizationError('model-harness-incompatible');
}
if (changed && currentEntry && !currentEntry.module && proposed.plugins?.['model-provider']?.length) {
if (catalogue?.status !== 'ready') throw new OrganizationError('model-provider-unavailable');
const mapping = catalogue.resolveLegacy ? catalogue.resolveLegacy(currentEntry) : catalogue.legacy?.[vars[model]];
const offered = mapping && catalogue.models?.find(m => m.module === mapping.module && m.upstream === mapping.upstream && m.model === currentEntry.model);
if (!offered?.available) throw new OrganizationError('model-provider-unavailable');
if (!offered.harnesses.includes(vars[harness])) throw new OrganizationError('model-harness-incompatible');
}
if (!Object.hasOwn(vars, harness) || (vars[harness] === before[harness] && vars[thinking] === before[thinking])) continue;
const name = vars[harness];
if (!on.includes(name)) throw new OrganizationError("harness-off", `${instance} ${harness} ${name} isn't switched on in plugins.harness`);
@@ -195,7 +212,7 @@ export function launchSettings({ dir, rolesDir, system, sessions = () => [], now
};
}
function set(id, args, actor) {
function set(id, args, actor, catalogue = null) {
requireObject(args, "limits update");
rejectUnknownKeys(args, SETTINGS_KEYS, "limits update");
if (typeof args.revision !== "string" || !/^[a-f0-9]{64}$/.test(args.revision)) {
@@ -214,6 +231,24 @@ export function launchSettings({ dir, rolesDir, system, sessions = () => [], now
}
if (Object.hasOwn(args, "reset")) reset(next.launch.limits, args.reset, at);
if (Object.hasOwn(args, "models")) {
if (actor !== old.document.human && plain(args.models)) {
for (const [key, entry] of Object.entries(args.models)) {
if (entry?.module && canonicalJson(entry) !== canonicalJson(old.document.launch.models[key] ?? null)) throw new OrganizationError('organization-model-provider-human-only');
// Resolve changed legacy entries with the trusted module resolver; a table key
// alone does not authorize introducing a different catalogue identity.
const previous = old.document.launch.models[key];
if (canonicalJson(entry) !== canonicalJson(previous ?? null) && plain(entry) && !entry.module && old.document.plugins?.['model-provider']?.length) {
if (catalogue?.status !== 'ready' || typeof catalogue.resolveLegacy !== 'function' || !Array.isArray(catalogue.models)) throw new OrganizationError('model-provider-unavailable');
const mapping = catalogue.resolveLegacy(entry);
const prior = previous?.module
? { module: previous.module, upstream: previous.upstream, model: previous.model }
: plain(previous) ? catalogue.resolveLegacy(previous) : null;
if (mapping && canonicalJson(mapping) !== canonicalJson(prior) && catalogue.models.some(model => model.module === mapping.module && model.upstream === mapping.upstream && model.model === entry.model)) {
throw new OrganizationError('organization-model-provider-human-only');
}
}
}
}
// M10: replacing launch.models can change the model behind a key a creative instance names
// without touching the instance, so a change to or removal of such a key is the human's.
for (const [instance, entry] of Object.entries(old.document.roles)) {
@@ -271,7 +306,7 @@ export function launchSettings({ dir, rolesDir, system, sessions = () => [], now
}
// Check every instance's route before publishing the complete document.
const proposed = validateBusinessDocument(next, businessFilePath(id, dir), { rolesDir });
checkRoutes({ proposed, old: before(id, old.document), levels });
checkRoutes({ proposed, old: before(id, old.document), levels, catalogue });
const receipt = writer.write(id, next, { revision: args.revision, actor });
return { ...view(id), transaction: receipt.transaction };
}
@@ -296,6 +331,7 @@ export function launchSettings({ dir, rolesDir, system, sessions = () => [], now
}));
return {
business: id, revision, harnesses, models: Object.keys(business.launch.models),
providerModels: Object.fromEntries(Object.entries(business.launch.models).filter(([, entry]) => entry.module).map(([key, entry]) => [key, { module: entry.module, upstream: entry.upstream, model: entry.model }])),
levels: Object.fromEntries([...named].sort().map(name => [name, levels(name)])), instances,
};
}
@@ -305,7 +341,7 @@ export function launchSettings({ dir, rolesDir, system, sessions = () => [], now
// organization write; a null fallback clears all three. It is human-only and outside
// ORGANIZATION_KEYS (the broker's routes.set). The chief of staff's tool changes the model key,
// and the assistant's whole vars (Q169), through limits.set, which runs the same checkRoutes.
function setRoutes(id, args, actor) {
function setRoutes(id, args, actor, catalogue = null) {
requireObject(args, "routes update");
rejectUnknownKeys(args, ["revision", "routes"], "routes update");
if (typeof args.revision !== "string" || !/^[a-f0-9]{64}$/.test(args.revision)) {
@@ -328,10 +364,28 @@ export function launchSettings({ dir, rolesDir, system, sessions = () => [], now
if (fallback !== null && (!exact(fallback, ROUTE_FIELDS) || ROUTE_FIELDS.some(key => typeof fallback[key] !== "string"))) {
throw new OrganizationError("organization-partial-fallback", `${instance}'s fallback sets all of harness, model and thinking or is cleared`);
}
// Before the file's own validation, which would refuse an unknown primary model without a code.
for (const model of [route.model, fallback?.model]) {
if (model !== undefined && !Object.hasOwn(next.launch.models, model)) {
throw new OrganizationError("unknown-model", `${instance} model ${JSON.stringify(model)} isn't a launch.models key`);
// The client supplies only a key. New entries come from the trusted snapshot.
const priorVars = old.document.roles[instance].vars ?? {};
for (const [choice, prefix] of [[route, ''], [fallback, 'fallback.']]) {
if (!choice) continue;
const unchanged = ROUTE_FIELDS.every(field => priorVars[prefix + field] === choice[field]);
const offered = catalogue?.models?.find(m => m.key === choice.model);
const existing = next.launch.models[choice.model];
const entry = offered ? { provider: offered.provider, model: offered.model, module: offered.module, upstream: offered.upstream } : null;
if (!existing && !offered && catalogue?.reason === 'model-key-collision') throw new OrganizationError('model-key-collision');
if (!existing && !offered && catalogue?.status === 'unavailable') throw new OrganizationError('model-catalogue-unavailable');
if (!existing && !offered) throw new OrganizationError('unknown-model', `${instance} model isn't a launch.models key`);
if (existing && offered && canonicalJson(existing) !== canonicalJson(entry) && !unchanged) throw new OrganizationError('model-key-collision');
if (!existing || existing.module) {
if (unchanged) continue;
if (catalogue?.status !== 'ready') throw new OrganizationError('model-catalogue-unavailable');
if (!offered || !offered.available || !Array.isArray(next.plugins?.['model-provider']) || !next.plugins['model-provider'].includes(offered.module)) throw new OrganizationError('model-provider-unavailable');
if (!offered.harnesses.includes(choice.harness)) throw new OrganizationError('model-harness-incompatible');
if (existing && canonicalJson(existing) !== canonicalJson(entry)) throw new OrganizationError('model-key-collision');
if (!existing) {
if (actor !== old.document.human) throw new OrganizationError('organization-model-provider-human-only');
next.launch.models[choice.model] = entry;
}
}
}
const vars = { ...next.roles[instance].vars };
@@ -344,7 +398,7 @@ export function launchSettings({ dir, rolesDir, system, sessions = () => [], now
}
if (canonicalJson(next) === canonicalJson(old.document)) throw new OrganizationError("organization-no-change");
const proposed = validateBusinessDocument(next, businessFilePath(id, dir), { rolesDir });
checkRoutes({ proposed, old: before(id, old.document), levels });
checkRoutes({ proposed, old: before(id, old.document), levels, catalogue });
const receipt = writer.write(id, next, { revision: args.revision, actor });
return { ...routes(id), transaction: receipt.transaction };
}
+12 -9
View File
@@ -16,19 +16,22 @@ export function checkModels(value, where = "launch.models") {
return Object.fromEntries(Object.entries(value).map(([key, entry]) => {
requireId(key, where);
requireObject(entry, `${where}.${key}`);
rejectUnknownKeys(entry, entry.provider === "proxy" ? ["provider", "model", "route"] : ["provider", "model"], `${where}.${key}`);
if (entry.provider === "proxy") {
const missing = !Object.hasOwn(entry, "route");
if (missing || !["anthropic", "glm", "codex"].includes(entry.route)) {
throw Object.assign(new BusinessError(`${where}.${key}.route must name anthropic, glm or codex`), {
code: missing ? "proxy-route-missing" : "proxy-route-unknown",
});
}
const moduleBacked = Object.hasOwn(entry, "module") || Object.hasOwn(entry, "upstream");
rejectUnknownKeys(entry, moduleBacked ? ["provider", "model", "module", "upstream"] : entry.provider === "proxy" ? ["provider", "model", "route"] : ["provider", "model"], `${where}.${key}`);
if (moduleBacked) {
requireId(entry.module, `${where}.${key}.module`);
requireId(entry.upstream, `${where}.${key}.upstream`);
if (typeof entry.model !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/.test(entry.model)) refuse(`${where}.${key}.model is invalid`);
}
if (entry.provider === "proxy" && !moduleBacked) {
if (!Object.hasOwn(entry, "route")) throw Object.assign(new BusinessError(`${where}.${key}.route is required`), { code: "proxy-route-missing" });
if (typeof entry.route !== "string" || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(entry.route)) throw Object.assign(new BusinessError(`${where}.${key}.route is invalid`), { code: "proxy-route-unknown" });
}
return [key, {
provider: name(entry.provider, `${where}.${key}.provider`),
model: name(entry.model, `${where}.${key}.model`),
...(entry.provider === "proxy" && Object.hasOwn(entry, "route") ? { route: entry.route } : {}),
...(moduleBacked ? { module: entry.module, upstream: entry.upstream } : {}),
...(entry.provider === "proxy" && !moduleBacked ? { route: entry.route } : {}),
}];
}));
}
+214
View File
@@ -0,0 +1,214 @@
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 { createHash, randomBytes } from 'node:crypto';
import { processState } from '../../seat/src/process-state.mjs';
import { ID_PATTERN } from './vocabulary.mjs';
export class OrgError extends Error {
constructor(code, message, path) { super(message); this.name = 'OrgError'; this.code = code; if (path) this.path = path; }
}
const fail = (code, message, path) => { throw new OrgError(code, message, path); };
function idCheck(id) { if (typeof id !== 'string' || !ID_PATTERN.test(id)) fail('org-invalid', 'invalid organization id'); }
export function orgRoot(dataRoot, id) { idCheck(id); if (!isAbsolute(dataRoot) || normalize(dataRoot) !== dataRoot) fail('org-invalid', 'data root must be a normalized absolute path'); return join(dataRoot, 'org-runtime', id); }
async function safeDir(path, create = false) {
if (!isAbsolute(path) || resolve(path) !== path) fail('org-mismatch', 'directory must be a normalized absolute path', path);
let current = parse(path).root;
for (const part of path.slice(current.length).split('/').filter(Boolean)) {
current = join(current, part);
try { const st = await fs.lstat(current); if (!st.isDirectory() || st.isSymbolicLink()) fail('org-mismatch', 'directory path contains a symlink or non-directory', current); }
catch (e) { if (e.code !== 'ENOENT' || !create) throw e; await fs.mkdir(current, { mode: 0o700 }).catch(err => { if (err.code !== 'EEXIST') throw err; }); const st = await fs.lstat(current); if (!st.isDirectory() || st.isSymbolicLink()) fail('org-mismatch', 'unsafe directory', current); }
}
}
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); }
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', 'deleting', 'archived'].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, { 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);
try {
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);
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');
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; }
const release = () => { closeSync(lockFd); unlinkSync(lock); };
let pending = false;
try {
let fd, registry = null;
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
catch (e) { if (e.code !== 'ENOENT') throw e; }
if (fd !== undefined) {
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 st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600) fail('org-mismatch', 'unsafe metadata file', file);
registry = validateRegistry(JSON.parse(readFileSync(fd, 'utf8')), file);
} finally { closeSync(fd); }
}
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; } }
};
const result = fn(registry, publish);
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
pending = true;
return Promise.resolve(result).finally(release);
}
return result;
} finally { if (!pending) release(); }
}
export async function reserveOrg(ctx, id) {
const root = orgRoot(ctx.dataRoot, 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(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 };
});
}
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); 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 };
}
export async function activateOrg(ctx, id) {
return withRegistry(ctx, async registry => {
const found = await resolveOrg(ctx, id);
if (found.state !== 'reserved') fail('org-exists', 'organization already active');
// An existing business document must join the same digest fence as a
// document created by the organization service. Some reservations have
// no document yet; preserve the registry's existing activation contract.
const dir = join(ctx.configDir, 'businesses'), file = join(dir, `${id}.json`);
try {
await safeDir(dir);
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 & 0o022)) fail('org-mismatch', 'unsafe organization document', file);
registry.orgs[id].digest = createHash('sha256').update(await fd.readFile()).digest('hex');
} finally { await fd.close(); }
} catch (e) { if (e.code !== 'ENOENT') throw e; }
registry.orgs[id].state = 'active';
return { ...registry.orgs[id] };
});
}
// Serialize bus launch with organization lifecycle operations.
// A leftover lock is never guessed stale; the operator must inspect it.
export async function withOrgLifecycle(ctx, id, fn) {
idCheck(id); await safeDir(ctx.configDir); await privateDir(ctx.configDir);
const path = join(ctx.configDir, `org-lifecycle-${id}.lock`); let fd;
try { fd = await fs.open(path, 'wx', 0o600); }
catch (e) { if (e.code === 'EEXIST') fail('org-locked', 'organization lifecycle lock is held; inspect the lock and resume'); throw e; }
try { await fd.writeFile(JSON.stringify({ pid: process.pid, at: new Date().toISOString() }) + '\n'); return await fn(); }
finally { await fd.close(); await fs.unlink(path); }
}
// Lifecycle locks carry {pid, at}; registry locks are empty and cannot be
// attributed safely. Only competing clear operations need this short fence:
// a new lifecycle holder cannot acquire the existing lock until it is removed.
function inspectRecoveryFence(path) {
let fd;
try { fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
catch (e) { if (e.code === 'ENOENT') return null; return { path, unreadable: true }; }
try {
const st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600 || st.nlink !== 1) return { path, unreadable: true };
let holder; try { holder = JSON.parse(readFileSync(fd, 'utf8')); } catch { return { path, unreadable: true }; }
if (!Number.isSafeInteger(holder?.pid) || holder.pid <= 0 || typeof holder.startTime !== 'string' || !/^\d+$/.test(holder.startTime)) return { path, unreadable: true };
let status; try { status = processState(holder.pid); } catch { return { path, pid: holder.pid, startTime: holder.startTime, alive: null }; }
return { path, pid: holder.pid, startTime: holder.startTime, alive: status.alive && status.startTime === holder.startTime, dev: st.dev, ino: st.ino };
} finally { closeSync(fd); }
}
export async function orgLock(ctx, id, { clear = false } = {}) {
idCheck(id); await safeDir(ctx.configDir); await privateDir(ctx.configDir);
const path = join(ctx.configDir, `org-lifecycle-${id}.lock`);
const guard = `${path}.clear`; let guardFd, self;
if (clear) {
self = processState(process.pid);
if (!self.alive || !self.startTime) fail('org-mismatch', 'clearer process identity is uncertain', guard);
for (;;) {
try { guardFd = openSync(guard, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); break; }
catch (e) {
if (e.code !== 'EEXIST') throw e;
const holder = inspectRecoveryFence(guard);
if (!holder) continue;
if (holder.unreadable) fail('org-locked', `unreadable recovery fence ${guard}; remove it by hand`, guard);
if (holder.alive !== false) fail('org-locked', `lock recovery is already in progress by pid ${holder.pid}`, guard);
let current; try { current = lstatSync(guard); } catch (err) { if (err.code === 'ENOENT') continue; throw err; }
if (current.dev !== holder.dev || current.ino !== holder.ino) fail('org-locked', 'recovery fence changed during recovery', guard);
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
try { renameSync(guard, `${guard}.stale-${stamp}-${randomBytes(4).toString('hex')}`); }
catch (err) { if (err.code === 'ENOENT') continue; throw err; }
}
}
}
try {
if (guardFd !== undefined) writeFileSync(guardFd, JSON.stringify({ pid: process.pid, startTime: self.startTime }) + '\n');
const fence = !clear ? inspectRecoveryFence(guard) : null;
const reportFence = result => { if (fence) { const { dev, ino, ...visible } = fence; result.fence = visible; } return result; };
let fd;
try { fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
catch (e) { if (e.code === 'ENOENT') return reportFence(clear ? { held: false, cleared: false } : { held: false }); throw e; }
try {
const st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600 || st.nlink !== 1) fail('org-mismatch', 'unsafe lifecycle lock', path);
let holder; try { holder = JSON.parse(readFileSync(fd, 'utf8')); } catch { fail('org-mismatch', 'unreadable lifecycle lock', path); }
if (!Number.isSafeInteger(holder?.pid) || holder.pid <= 0 || typeof holder.at !== 'string' || !Number.isFinite(Date.parse(holder.at))) fail('org-mismatch', 'invalid lifecycle lock holder', path);
let status; try { status = processState(holder.pid); } catch { fail('org-mismatch', 'lifecycle lock holder is uncertain', path); }
const result = { held: true, pid: holder.pid, start: holder.at, alive: status.alive };
if (clear) {
if (status.alive) fail('org-locked', `lifecycle lock held by pid ${holder.pid} since ${holder.at} (alive)`, path);
const current = lstatSync(path);
if (current.dev !== st.dev || current.ino !== st.ino) fail('org-locked', 'lifecycle lock changed during recovery', path);
unlinkSync(path); result.cleared = true;
}
return reportFence(result);
} finally { closeSync(fd); }
} finally {
if (guardFd !== undefined) {
const owned = fstatSync(guardFd);
try {
const current = lstatSync(guard);
if (owned.dev !== current.dev || owned.ino !== current.ino) fail('org-locked', 'recovery fence changed during recovery', guard);
unlinkSync(guard);
} finally { closeSync(guardFd); }
}
}
}
+386
View File
@@ -0,0 +1,386 @@
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, httpsUrl } from './business.mjs';
import { ID_PATTERN } from './vocabulary.mjs';
import { OrgError, orgRoot, readRegistry, reserveOrg, resolveOrg, withRegistry, withOrgLifecycle } from './org-registry.mjs';
import { loadInstalled } from '../../plugins/src/loader.mjs';
import { BINDING_NAME } from '../../discord/src/binding.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;
}
// Presets are supplied by installed modules; reject malformed or duplicate names
// rather than silently choosing a different module's configuration.
async function discoverPresets() {
const presets = new Map();
for (const kind of ['harness', 'model-provider']) {
const { modules, refused } = await loadInstalled(kind);
if (refused.length) fail('org-invalid', 'installed plugin could not be loaded');
for (const { module } of modules) {
if (module.orgPresets === undefined) continue;
if (typeof module.orgPresets !== 'function') fail('org-invalid', 'invalid preset provider');
const list = await module.orgPresets();
if (!Array.isArray(list)) fail('org-invalid', 'invalid preset provider');
for (const p of list) {
if (!p || typeof p.name !== 'string' || !/^[a-z][a-z0-9-]*$/.test(p.name) || presets.has(p.name) ||
typeof p.label !== 'string' || !p.label || typeof p.description !== 'string' || !p.description ||
!Array.isArray(p.thinking?.levels) || !p.thinking.levels.length ||
p.thinking.levels.some(v => typeof v !== 'string' || !/^[a-z][a-z0-9-]*$/.test(v)) ||
new Set(p.thinking.levels).size !== p.thinking.levels.length || !p.thinking.levels.includes(p.thinking.default) ||
typeof p.available !== 'function' || typeof p.overlay !== 'function') fail('org-invalid', 'invalid or duplicate preset');
presets.set(p.name, p);
}
}
}
return presets;
}
export async function orgPresets({ env = process.env } = {}) {
const presets = await discoverPresets();
return Promise.all([...presets.values()].map(async p => {
const availability = await p.available({ env });
if (!availability || typeof availability.ok !== 'boolean' || (!availability.ok && (typeof availability.reason !== 'string' || !availability.reason))) fail('org-invalid', 'invalid preset availability');
return { name: p.name, label: p.label, description: p.description, thinking: structuredClone(p.thinking), available: availability.ok ? { ok: true } : { ok: false, reason: availability.reason } };
}));
}
export async function templateRender(id, { fixture, preset, thinking } = {}) {
if (fixture !== undefined && fixture !== 'local') fail('org-invalid', 'unknown fixture');
if (fixture !== undefined && preset !== undefined) fail('org-invalid', 'choose fixture or preset');
if (fixture === 'local') preset = '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`;
if (preset !== undefined) {
if (typeof preset !== 'string') fail('org-invalid', 'unknown preset');
const p = (await discoverPresets()).get(preset);
if (!p) fail('org-invalid', 'unknown preset');
const level = thinking === undefined ? p.thinking.default : thinking;
if (!p.thinking.levels.includes(level)) fail('org-invalid', 'unknown thinking level');
const overlay = await p.overlay({ thinking: level });
if (!overlay || typeof overlay !== 'object' || !overlay.plugins || !overlay.launch || !overlay.assistantVars) fail('org-invalid', 'invalid preset overlay');
doc.plugins = structuredClone(overlay.plugins); doc.launch = structuredClone(overlay.launch);
doc.roles.assistant.vars = structuredClone(overlay.assistantVars);
if (overlay.vars !== undefined) doc.vars = structuredClone(overlay.vars);
} else if (thinking !== undefined) fail('org-invalid', 'thinking requires a preset');
return doc;
}
export async function validateOrg(doc) {
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 }] };
}
}
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 readCurrentUnlocked(ctx, id, { includeArchived = false } = {}) {
const found = await resolveOrg(ctx, id);
if (found.state === 'archived' && !includeArchived) fail('org-archived', 'organization is archived');
if (found.state !== 'active' && !(includeArchived && found.state === 'archived')) 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');
// From here the caller is the verified owner. A failure carries that proof so a list may
// show this org as broken to its owner; earlier failures prove nothing and stay hidden.
try {
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 };
} catch (e) { if (e && typeof e === 'object') e.ownerVerified = true; throw e; }
}
// Readers do not take the writer lock. Require unchanged registry snapshots and
// no in-progress publication on either side of the read; retry a raced write.
async function readCurrent(ctx, id, opts) {
const locked = async () => { try { await fs.lstat(join(ctx.configDir, 'org-runtime.lock')); return true; } catch (e) { if (e.code === 'ENOENT') return false; throw e; } };
for (let n = 0; n < 200; n++) {
if (await locked()) { await new Promise(r => setTimeout(r, 10)); continue; }
const before = JSON.stringify(await readRegistry(ctx));
let result, error;
try { result = await readCurrentUnlocked(ctx, id, opts); } catch (e) { error = e; }
const after = JSON.stringify(await readRegistry(ctx));
if (!(await locked()) && before === after) { if (error) throw error; return result; }
await new Promise(r => setTimeout(r, 10));
}
fail('org-locked', 'registry changed or was locked during organization read');
}
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 notify = join(entry.root, 'notify'); await ensurePrivateDir(notify);
const bindingDir = join(notify, id); await ensurePrivateDir(bindingDir);
await atomic(join(bindingDir, 'notify.json'), encode({ notifyVersion: 1, binding: null }), true);
const result = await receipt(entry.root, 'org.create', id, ctx.principal, { root: entry.root, revision: entry.revision, notifications: 'none' });
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, opts = {}) { authorize(ctx); const { root, revision, doc, principal, state } = await readCurrent(ctx, id, opts); return { id, root, revision, doc, principal, state }; }
export async function notificationShow(ctx, id) {
authorize(ctx); const current = await readCurrent(ctx, id);
for (const path of [join(current.root, 'notify'), join(current.root, 'notify', id)]) {
const st = await fs.lstat(path);
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o077)) fail('org-mismatch', 'unsafe notifier directory');
}
const file = join(current.root, 'notify', id, 'notify.json');
const st = await fs.lstat(file);
if (!st.isFile() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600) fail('org-mismatch', 'unsafe notifier configuration');
const binding = JSON.parse(await privateRead(file));
if (!binding || Object.keys(binding).sort().join(',') !== 'binding,notifyVersion' || binding.notifyVersion !== 1 || !(binding.binding === null || typeof binding.binding === 'string' && BINDING_NAME.test(binding.binding))) fail('org-mismatch', 'invalid notifier configuration');
const record = (await receipts(current.root)).find(r => r.op === 'org.create' && r.org === id && same(r.subject, current.principal));
if (!record || record.notifications !== 'none') fail('org-mismatch', 'notification choice receipt unavailable');
return { ...record, currentNotifications: binding.binding === null ? 'none' : 'configured' };
}
export async function updateOrg(ctx, id, input, { ifRevision } = {}) {
authorize(ctx); return withRegistry(ctx, async registry => {
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 });
});
}
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', '[email protected]', '-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 withOrgLifecycle(ctx, id, async () => {
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');
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); }
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); }
// Archive and restore share the bus-start/delete fence, but do not hold the
// registry lock while waiting for the bus host to stop.
async function lifecycleChange(ctx, id, { ifRevision, busStop }, operation) {
authorize(ctx); checkId(id);
if (operation === 'archive' && typeof busStop !== 'function') fail('org-invalid', 'archive needs a bus stop function');
return withOrgLifecycle(ctx, id, async () => {
const expected = operation === 'archive' ? 'active' : 'archived';
const inspect = async () => {
const current = await readCurrentUnlocked(ctx, id, { includeArchived: true });
if (current.state !== expected) fail(current.state === 'archived' ? 'org-archived' : 'org-invalid', `organization is not ${expected}`);
if (!Number.isSafeInteger(ifRevision) || current.revision !== ifRevision) fail('revision-conflict', 'organization revision changed');
return current;
};
await withRegistry(ctx, inspect, { write: false });
const bus = operation === 'archive' ? await busStop(ctx, id) : null;
if (operation === 'archive' && (!bus || typeof bus !== 'object' || typeof bus.stopped !== 'boolean')) fail('org-unavailable', 'bus stop outcome is uncertain');
return withRegistry(ctx, async registry => {
const current = await inspect();
if (!Number.isSafeInteger(current.revision + 1)) fail('org-mismatch', 'organization revision cannot be incremented');
await deleteSafeRoot(ctx.dataRoot);
const record = { op: `org.${operation}`, org: id, actor: { ...ctx.principal }, time: new Date().toISOString(), revision: current.revision + 1, bus };
let fd;
try { fd = await fs.open(join(ctx.dataRoot, 'org-lifecycle.log'), constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW | constants.O_NONBLOCK, 0o600); }
catch (e) { if (e.code === 'ENXIO') fail('org-mismatch', 'unsafe organization lifecycle log'); throw e; }
try { const st = await fd.stat(); if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o077)) fail('org-mismatch', 'unsafe organization lifecycle log'); await fd.writeFile(JSON.stringify(record) + '\n'); }
finally { await fd.close(); }
registry.orgs[id].state = operation === 'archive' ? 'archived' : 'active';
registry.orgs[id].revision = record.revision;
return { ...record, outcome: 'done', step: operation };
});
});
}
export async function archiveOrg(ctx, id, opts = {}) { return lifecycleChange(ctx, id, opts, 'archive'); }
export async function restoreOrg(ctx, id, opts = {}) { return lifecycleChange(ctx, id, opts, 'restore'); }
// Deletion keeps a durable marker in the registry and a receipt beside the
// moved data. It never opens the contents of organization secret trees.
async function deleteInspect(path, { secret = false } = {}) {
let count = 0;
async function visit(p) {
const st = await fs.lstat(p);
if (st.isSymbolicLink() || (!st.isDirectory() && !st.isFile() && !st.isSocket())) fail('org-mismatch', 'unsafe owned path');
if (secret && (st.uid !== process.getuid() || (st.mode & 0o077) || (!st.isDirectory() && !st.isFile()))) fail('org-mismatch', 'unsafe organization secret directory');
if (st.isDirectory()) for (const name of await fs.readdir(p)) { count++; await visit(join(p, name)); }
}
try { await fs.lstat(path); } catch (e) { if (e.code === 'ENOENT') return null; throw e; }
await visit(path); return count;
}
async function deleteSafeRoot(root) {
if (typeof root !== 'string' || !root.startsWith('/') || resolve(root) !== root) fail('org-mismatch', 'invalid data root');
let path = '/';
for (const part of root.split('/').filter(Boolean)) {
path = join(path, part); const st = await fs.lstat(path);
if (!st.isDirectory() || st.isSymbolicLink()) fail('org-mismatch', 'unsafe data root');
if (path === root && (st.uid !== process.getuid() || (st.mode & 0o077))) fail('org-mismatch', 'unsafe data root');
}
}
async function deleteParents(base, relative) {
let path = base;
const parts = relative.split('/'); parts.pop();
for (const part of parts) {
path = join(path, part);
try { const st = await fs.lstat(path); if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o077)) fail('org-mismatch', 'unsafe owned path parent'); }
catch (e) { if (e.code === 'ENOENT') return false; throw e; }
}
return true;
}
// busStop comes from the caller (the CLI's busStopForDelete), so business code does not import the bus host.
export async function deleteOrg(ctx, id, { confirm, ifRevision, busStop } = {}) {
authorize(ctx); checkId(id);
if (typeof busStop !== 'function') fail('org-invalid', 'deletion needs a bus stop function');
if (confirm !== id) fail('org-confirm', 'confirmation must match the organization ID');
return withOrgLifecycle(ctx, id, async () => {
const deletion = await withRegistry(ctx, async registry => {
const entry = registry.orgs[id];
if (!entry) fail('org-unknown', 'unknown organization');
if (entry.root !== orgRoot(ctx.dataRoot, id)) fail('org-mismatch', 'organization binding mismatch');
if (entry.state === 'deleting') {
if (!entry.deletion || !same(entry.deletion.actor, ctx.principal) || !/^[a-zA-Z0-9._-]+$/.test(entry.deletion.folder) || !entry.deletion.folder.startsWith(`${id}-`)) fail('org-mismatch', 'invalid deletion marker');
return entry.deletion;
}
const current = await readCurrentUnlocked(ctx, id, { includeArchived: true });
if (!Number.isSafeInteger(ifRevision) || current.revision !== ifRevision) fail('revision-conflict', 'organization revision changed');
const marker = { actor: { ...ctx.principal }, time: new Date().toISOString(), revision: current.revision, folder: `${id}-${new Date().toISOString().replace(/[:.]/g, '-')}-${randomBytes(4).toString('hex')}` };
entry.state = 'deleting'; entry.deletion = marker;
return marker;
});
const deleted = join(ctx.dataRoot, 'deleted'), destination = join(deleted, deletion.folder), receiptFile = join(destination, 'receipt.json');
let registryRemoved = false, result, phase = 'prepare';
try {
await deleteSafeRoot(ctx.dataRoot);
await ensurePrivateDir(deleted); await ensurePrivateDir(destination);
try { result = JSON.parse(await privateRead(receiptFile)); }
catch (e) { if (e.code !== 'ENOENT') throw e; result = { op: 'org.delete', org: id, actor: deletion.actor, time: deletion.time, revision: deletion.revision, destination, bus: null, moves: [], secrets: [], note: 'Purging deleted data and remote-side revocation are out of scope.' }; await atomic(receiptFile, encode(result), true); }
if (result.org !== id || result.destination !== destination || result.revision !== deletion.revision || !Array.isArray(result.moves) || !Array.isArray(result.secrets)) fail('org-mismatch', 'invalid deletion receipt');
if (!result.bus) {
phase = 'bus';
result.bus = await busStop(ctx, id);
await atomic(receiptFile, encode(result));
}
phase = 'inspect';
await withRegistry(ctx, async registry => {
const entry = registry.orgs[id];
if (!entry || entry.state !== 'deleting' || entry.deletion?.folder !== deletion.folder) fail('org-mismatch', 'deletion marker changed');
const owned = [
{ base: ctx.dataRoot, rel: `org-runtime/${id}`, to: 'runtime', required: true },
{ base: ctx.configDir, rel: `businesses/${id}.json`, to: 'organization.json', required: true },
{ base: ctx.configDir, rel: `businesses/.history/${id}`, to: 'history' },
...['agents', 'grants', 'hosts'].map(kind => ({ base: ctx.configDir, rel: `secrets/${kind}/${id}`, to: `secrets-${kind}`, secret: true })),
];
// Inspect all sources and destinations before moving the first one.
for (const item of owned) {
item.from = join(item.base, item.rel); item.dest = join(destination, item.to);
const parents = await deleteParents(item.base, item.rel);
item.count = parents ? await deleteInspect(item.from, { secret: item.secret }) : null;
const prior = result.moves.find(m => m.from === item.from);
const destCount = await deleteInspect(item.dest, { secret: item.secret });
if (prior && (item.count !== null || destCount === null)) fail('org-mismatch', 'moved path does not match receipt');
if (!prior && item.count !== null && destCount !== null) fail('org-mismatch', 'deletion destination already exists');
if (!prior && item.count === null && destCount !== null) item.recovered = true;
if (!prior && item.count === null && destCount === null && item.required) fail('org-mismatch', 'required organization path is missing');
if (item.recovered) item.count = destCount;
}
phase = 'move';
for (const item of owned) {
if (result.moves.some(m => m.from === item.from) || item.count === null) continue;
if (!item.recovered) await fs.rename(item.from, item.dest);
result.moves.push({ from: item.from, to: item.dest });
if (item.secret) result.secrets.push({ path: item.from, entryCount: item.count });
await atomic(receiptFile, encode(result));
}
phase = 'registry';
const log = join(deleted, 'deleted.log');
const fd = await fs.open(log, constants.O_RDWR | 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 deletion log');
const lines = (await fd.readFile('utf8')).split('\n');
if (!lines.some(line => { try { return JSON.parse(line).destination === destination; } catch { return false; } })) await fd.writeFile(JSON.stringify({ org: id, destination, time: deletion.time, actor: deletion.actor, revision: deletion.revision }) + '\n');
} finally { await fd.close(); }
result.registry = 'removal pending'; await atomic(receiptFile, encode(result));
delete registry.orgs[id];
});
registryRemoved = true; result.registry = 'removed'; await atomic(receiptFile, encode(result)); return result;
} catch (e) {
e.deleteStep = phase; if (result) e.deleteReceipt = result;
if (registryRemoved) { e.message += `; registry removal completed but the receipt could not be finalized at ${receiptFile}`; throw e; }
e.message += `; deletion remains in progress. Inspect ${destination}/receipt.json and rerun mosaic org delete ${id} --confirm ${id}`;
throw e;
}
});
}
+8
View File
@@ -0,0 +1,8 @@
{
"businessVersion": 1,
"id": "template",
"human": "local-dev-owner",
"arbiters": { "delivery": "chief", "technical": "chief" },
"roles": { "chief": { "definition": "chief-of-staff" }, "assistant": { "definition": "assistant" } },
"projects": { "work": { "root": "/__mosaic_org__/template/projects/work" } }
}
+18 -6
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) {
@@ -134,6 +135,12 @@ export function setReplyLimits(writer, id, args, actor) {
export function organizationWriter({ dir, rolesDir, barrier = () => {} }) {
const evidence = join(dir, "businesses", ".history");
const logFile = id => join(evidence, id, "changes.jsonl");
// Keep the writer's established busy refusal when the shared registry lock
// is held, including when another document hold is already in progress.
function withWriterRegistry(fn) {
try { return withRegistryWriter(dir, fn); }
catch (e) { if (e.code === "org-locked") fail("organization-busy", "organization writer is busy"); throw e; }
}
// Reads and launches depend on the whole document, not transaction evidence.
function read(id) {
@@ -263,11 +270,15 @@ export function organizationWriter({ dir, rolesDir, barrier = () => {} }) {
requireId(id, "organization id");
let revoked = false;
try {
return withLock(id, ({ home, parent }) => {
return withWriterRegistry((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 +323,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;
@@ -366,7 +378,7 @@ export function organizationWriter({ dir, rolesDir, barrier = () => {} }) {
// duration, and an awaited call would leave a half-checked world to whoever finds the lock gone.
function hold(id, fn) {
requireId(id, "organization id");
return withLock(id, () => {
return withWriterRegistry(() => withLock(id, () => {
recover(id);
const { document, revision } = read(id);
const out = fn({ document, revision });
@@ -375,17 +387,17 @@ export function organizationWriter({ dir, rolesDir, barrier = () => {} }) {
fail("organization-hold-sync", "a held check must be synchronous");
}
return out;
});
}));
}
// Like hold, but the lock stays on disk until an asynchronous effect settles, including on failure.
// The effect must not call an organization writer: it would find this same lock and refuse busy.
function holdAsync(id, fn) {
requireId(id, "organization id");
return withLock(id, () => {
return withWriterRegistry(() => withLock(id, () => {
recover(id);
return fn(read(id));
});
}));
}
// The committed addition of this request and plan, if there is one. A prepared receipt whose
@@ -0,0 +1,76 @@
// Stream S4 (PLAN C5): the organization file's `appearance: { theme, mode }`.
import test, { after } from "node:test";
import assert from "node:assert/strict";
import { readFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import { businessDoc, rolesCopy, tmp as makeRoot, writeJson } from "./helpers.mjs";
import { validateBusinessDocument } from "../src/business.mjs";
import { APPEARANCE_MODES, checkAppearance } from "../src/appearance.mjs";
import { pluginSettings } from "../src/plugin-settings.mjs";
import { BusinessError } from "../src/errors.mjs";
const roots = [];
after(() => roots.forEach(root => rmSync(root, { recursive: true, force: true })));
function fixture() {
const root = makeRoot();
roots.push(root);
const rolesDir = rolesCopy(root);
const doc = businessDoc(root, "acme");
const file = writeJson(join(root, "businesses", "acme.json"), doc);
return { root, rolesDir, doc, file, check: d => validateBusinessDocument(d, file, { rolesDir }) };
}
const withAppearance = (doc, appearance) => ({ ...structuredClone(doc), appearance });
test("S4 appearance is optional: a file without it is valid and the validated business has no appearance key", () => {
const f = fixture();
const business = f.check(f.doc);
assert.equal(Object.hasOwn(business, "appearance"), false);
assert.equal(checkAppearance(undefined, "x"), undefined);
});
test("S4 appearance accepts theme and mode, either or both, and the result is frozen", () => {
const f = fixture();
for (const appearance of [{}, { theme: "harbor" }, { mode: "dim" }, { theme: "grove", mode: "system" }]) {
const business = f.check(withAppearance(f.doc, appearance));
assert.deepEqual({ ...business.appearance }, appearance);
assert.ok(Object.isFrozen(business.appearance));
}
for (const mode of APPEARANCE_MODES) assert.equal(f.check(withAppearance(f.doc, { mode })).appearance.mode, mode);
assert.deepEqual([...APPEARANCE_MODES], ["system", "light", "dim", "dark"]);
});
test("S4 appearance refuses a bad shape, an unknown key, a bad mode and a theme name that isn't a plugin name", () => {
const f = fixture();
const refused = [
null, [], "harbor", 3,
{ colour: "red" }, { theme: "harbor", extra: 1 },
{ mode: "night" }, { mode: "Dark" }, { mode: "" }, { mode: null }, { mode: 1 },
{ theme: "Harbor" }, { theme: "har bor" }, { theme: "../harbor" }, { theme: "a/b" }, { theme: "" }, { theme: 7 }, { theme: null },
{ theme: `a${"b".repeat(64)}` }, { theme: "<script>" }, { theme: "x;}body{display:none" },
];
for (const appearance of refused) {
assert.throws(() => f.check(withAppearance(f.doc, appearance)), BusinessError, JSON.stringify(appearance));
}
// The message names the field, so a person editing the file can find it.
assert.throws(() => f.check(withAppearance(f.doc, { mode: "night" })), /appearance\.mode/);
assert.throws(() => f.check(withAppearance(f.doc, { theme: "Harbor" })), /appearance\.theme/);
});
test("S4 appearance checks a theme's shape only: a name nothing installed is valid and keeps its value", () => {
const f = fixture();
const business = f.check(withAppearance(f.doc, { theme: "no-such-theme", mode: "dark" }));
assert.deepEqual({ ...business.appearance }, { theme: "no-such-theme", mode: "dark" });
});
test("S4 the organization writers keep appearance when they rewrite the file", () => {
const f = fixture();
const doc = withAppearance(f.doc, { theme: "grove", mode: "dim" });
const file = writeJson(join(f.root, "businesses", "acme.json"), doc);
const settings = pluginSettings({ dir: f.root, rolesDir: f.rolesDir });
const view = settings.read("acme");
settings.set("acme", { revision: view.revision, plugins: { "usage-source": ["meter"] } }, "jason");
const written = JSON.parse(readFileSync(file, "utf8"));
assert.deepEqual(written.appearance, { theme: "grove", mode: "dim" });
assert.deepEqual(written.plugins, { "usage-source": ["meter"] });
});
@@ -206,3 +206,64 @@ assert.throws(()=>settings.set('acme',{revision,models:opus},chief),code('organi
settings.set('acme',{revision,models:opus},'jason');
const e=JSON.parse(readFileSync(settings.writer.logFile('acme'),'utf8').trim().split('\n').at(-1));assert.equal(e.actor,'jason');
});
test('fixture catalogue materializes a new model and saves its route in one revision-checked write', async () => {
const f=fixture(); const {settings}=await routeSettings(f); const configured=JSON.parse(readFileSync(f.file,'utf8')); configured.plugins['model-provider']=['cliproxy']; writeJson(f.file,configured); const view=settings.routes('acme');
const catalogue={status:'ready',models:[{key:'zai-fixture-id',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-id',label:'zai / fixture-id',available:true,reason:null,harnesses:['pi']}],notices:[]};
assert.equal(view.models.includes('zai-fixture-id'),false);
const done=settings.setRoutes('acme',{revision:view.revision,routes:{coder:{harness:'pi',model:'zai-fixture-id',thinking:'low',fallback:null}}},'jason',catalogue);
const data=JSON.parse(readFileSync(f.file,'utf8'));
assert.deepEqual(data.launch.models['zai-fixture-id'],{provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'});
assert.equal(done.instances.coder.model,'zai-fixture-id');
});
test('catalogue selections refuse removed models, incompatible harnesses, collisions and indirect writes', async () => {
const f=fixture(); const {settings}=await routeSettings(f); const configured=JSON.parse(readFileSync(f.file,'utf8')); configured.plugins['model-provider']=['cliproxy']; writeJson(f.file,configured);
const view=settings.routes('acme'), offered={key:'zai-fixture-id',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-id',label:'Fixture',available:true,reason:null,harnesses:['pi']};
const route=(model='zai-fixture-id',harness='pi')=>({coder:{harness,model,thinking:'low',fallback:null}});
const catalog={status:'ready',models:[offered],notices:[]};
assert.throws(()=>settings.setRoutes('acme',{revision:view.revision,routes:route('zai-fixture-id','claude-code')},'jason',catalog),code('model-harness-incompatible'));
const done=settings.setRoutes('acme',{revision:view.revision,routes:route()},'jason',catalog);
const file=JSON.parse(readFileSync(f.file,'utf8')); file.roles.coder.vars.model='sonnet'; writeJson(f.file,file);
const rev=settings.routes('acme').revision;
assert.throws(()=>settings.setRoutes('acme',{revision:rev,routes:route()},'jason',{status:'ready',models:[],notices:[]}),code('model-provider-unavailable'));
assert.throws(()=>settings.set('acme',{revision:rev,instances:{coder:'zai-fixture-id'}},'jason'),code('model-provider-unavailable'));
const collision={...offered,key:'sonnet'};
assert.throws(()=>settings.setRoutes('acme',{revision:rev,routes:{coder:{...route('sonnet').coder,thinking:'high'}}},'jason',{status:'ready',models:[collision],notices:[]}),code('model-key-collision'));
assert.ok(done.revision);
});
test('a changed legacy route refuses while the enabled catalogue is unavailable', async () => {
const f=fixture(); const {settings}=await routeSettings(f); const doc=JSON.parse(readFileSync(f.file,'utf8')); doc.plugins['model-provider']=['cliproxy']; doc.launch.models['zai-fixture-id']={provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'}; doc.roles.coder.vars.model='zai-fixture-id'; writeJson(f.file,doc);
const view=settings.routes('acme');
assert.throws(()=>settings.setRoutes('acme',{revision:view.revision,routes:{coder:{harness:'pi',model:'sonnet',thinking:'low',fallback:null}}},'jason',{status:'unavailable',models:[],notices:[]}),code('model-provider-unavailable'));
});
test('indirect selection of an existing provider model uses fresh catalogue availability', async () => {
const f=fixture();const {settings}=await routeSettings(f);const doc=JSON.parse(readFileSync(f.file,'utf8'));doc.plugins['model-provider']=['cliproxy'];doc.launch.models['zai-fixture-id']={provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'};writeJson(f.file,doc);
const offered={key:'zai-fixture-id',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-id',label:'Fixture',available:true,reason:null,harnesses:['pi']};
const view=settings.routes('acme');
const saved=settings.set('acme',{revision:view.revision,instances:{coder:'zai-fixture-id'}},'jason',{status:'ready',models:[offered]});
assert.equal(saved.instances.coder.modelKey,'zai-fixture-id');
});
test('changed legacy primary and fallback refuse unavailable catalogue models', async () => {
const f=fixture();const {settings}=await routeSettings(f);const doc=JSON.parse(readFileSync(f.file,'utf8'));doc.plugins['model-provider']=['cliproxy'];writeJson(f.file,doc);
const view=settings.routes('acme');
assert.throws(()=>settings.setRoutes('acme',{revision:view.revision,routes:{coder:{harness:'pi',model:'opus',thinking:'low',fallback:null}}},'jason',{status:'ready',models:[],legacy:{opus:{module:'cliproxy',upstream:'claude',model:doc.launch.models.opus.model}}}),code('model-provider-unavailable'));
assert.throws(()=>settings.setRoutes('acme',{revision:view.revision,routes:{coder:{...routeOf(doc.roles.coder.vars),fallback:{harness:'pi',model:'opus',thinking:'low'}}}},'jason',{status:'ready',models:[],legacy:{opus:{module:'cliproxy',upstream:'claude',model:doc.launch.models.opus.model}}}),code('model-provider-unavailable'));
});
test('provider model materialization is human-only across indirect and Routing writers', async () => {
const f=fixture();const {settings}=await routeSettings(f);const doc=JSON.parse(readFileSync(f.file,'utf8'));doc.plugins['model-provider']=['cliproxy'];writeJson(f.file,doc);
const view=settings.routes('acme'),offered={key:'fixture-key',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-id',label:'Fixture',available:true,reason:null,harnesses:['pi']};const catalogue={status:'ready',models:[offered],legacy:{}};
const entry={provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'};
assert.throws(()=>settings.set('acme',{revision:view.revision,models:{...doc.launch.models,'fixture-key':entry}},'chief-of-staff.chief',catalogue),code('organization-model-provider-human-only'));
const legacyModels={...doc.launch.models,'fixture-key':{provider:'zai',model:'fixture-id'}};
assert.throws(()=>settings.set('acme',{revision:view.revision,models:legacyModels},'chief-of-staff.chief',catalogue),code('model-provider-unavailable'));
assert.throws(()=>settings.setRoutes('acme',{revision:view.revision,routes:{coder:{harness:'pi',model:'fixture-key',thinking:'low',fallback:null}}},'tester',catalogue),code('organization-model-provider-human-only'));
});
test('a role can add a legacy model when the model-provider module is off', async () => {
const f=fixture();const {settings}=await routeSettings(f);const view=settings.read('acme');
const models={...view.models,'legacy-new':{provider:'zai',model:'fixture-id'}};
const saved=settings.set('acme',{revision:view.revision,models,instances:{coder:'legacy-new'}},'chief-of-staff.chief');
assert.equal(saved.instances.coder.modelKey,'legacy-new');
});
@@ -0,0 +1,59 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import { constants } from 'node:fs';
import { syncBuiltinESMExports } from 'node:module';
import { spawnSync } from 'node:child_process';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import * as org from '../src/org-service.mjs';
import { listOrgs } from '../src/org-registry.mjs';
import { busStopForDelete } from '../../cli/src/org-bus.mjs';
async function fixture(t) {
const root = await fs.mkdtemp(join(tmpdir(), 'af-'));
const ctx = { configDir: join(root, 'config'), dataRoot: join(root, 'data'), instance: 'test', principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
await fs.mkdir(ctx.configDir, { mode: 0o700 }); await fs.mkdir(ctx.dataRoot, { mode: 0o700 });
await org.createOrg(ctx, 'alpha', await org.templateRender('alpha'));
t.after(() => fs.rm(root, { recursive: true, force: true })); return ctx;
}
for (const kind of ['queue', 'project']) test(`archive refuses during ${kind} setup until its receipt is written`, { timeout: 20000 }, async t => {
const ctx = await fixture(t), runtime = join(ctx.dataRoot, 'org-runtime', 'alpha');
const path = kind === 'queue' ? join(runtime, 'checkout') : join(runtime, 'projects', 'work');
let entered, release; const ready = new Promise(r => { entered = r; }), gate = new Promise(r => { release = r; });
const original = fs.mkdir; let once = false;
fs.mkdir = async function(p, ...args) { if (p === path && !once) { once = true; entered(); await gate; } return original.call(this, p, ...args); }; syncBuiltinESMExports();
const setup = (kind === 'queue' ? org.queueInit(ctx, 'alpha', { mode: 'local-only', acceptLocalLossRisk: true }) : org.projectRepoInit(ctx, 'alpha', 'work', { mode: 'local-only', acceptLocalLossRisk: true })).then(value => ({ value }), error => ({ error }));
try {
await ready;
await assert.rejects(org.archiveOrg(ctx, 'alpha', { ifRevision: 1, busStop: busStopForDelete }), { code: 'org-locked' });
assert.equal((await listOrgs(ctx))[0].state, 'active');
release(); const result = await setup; assert.ok(result.value, result.error?.message);
const revision = (await listOrgs(ctx))[0].revision;
await org.archiveOrg(ctx, 'alpha', { ifRevision: revision, busStop: busStopForDelete });
await org.restoreOrg(ctx, 'alpha', { ifRevision: revision + 1 });
const readBack = kind === 'queue' ? await org.queueShow(ctx, 'alpha') : await org.projectRepoShow(ctx, 'alpha', 'work');
assert.equal(readBack.op, kind === 'queue' ? 'org.queue.init' : 'org.project.repo.init');
} finally { release(); await setup; fs.mkdir = original; syncBuiltinESMExports(); }
});
test('lifecycle log FIFO is refused promptly without a reader', { timeout: 10000 }, async t => {
const ctx = await fixture(t), file = join(ctx.dataRoot, 'org-lifecycle.log');
assert.equal(spawnSync('mkfifo', ['-m', '600', file]).status, 0);
let settled = false;
const pending = org.archiveOrg(ctx, 'alpha', { ifRevision: 1, busStop: busStopForDelete }).then(value => { settled = true; return { value }; }, error => { settled = true; return { error }; });
await new Promise(r => setTimeout(r, 200)); const prompt = settled;
const reader = await fs.open(file, constants.O_RDONLY | constants.O_NONBLOCK);
try { const result = await pending; assert.equal(result.error?.code, 'org-mismatch'); assert.equal((await listOrgs(ctx))[0].state, 'active'); }
finally { await reader.close(); }
assert.equal(prompt, true, 'a FIFO must not block the lifecycle and registry locks');
});
test('lifecycle log FIFO with a reader still fails the file-type check', { timeout: 10000 }, async t => {
const ctx = await fixture(t), file = join(ctx.dataRoot, 'org-lifecycle.log');
assert.equal(spawnSync('mkfifo', ['-m', '600', file]).status, 0);
const reader = await fs.open(file, constants.O_RDONLY | constants.O_NONBLOCK);
try { await assert.rejects(org.archiveOrg(ctx, 'alpha', { ifRevision: 1, busStop: busStopForDelete }), { code: 'org-mismatch' }); }
finally { await reader.close(); }
assert.equal((await listOrgs(ctx))[0].state, 'active');
});
@@ -0,0 +1,70 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import * as org from '../src/org-service.mjs';
import { listOrgs } from '../src/org-registry.mjs';
import { busStart, busStopForDelete } from '../../cli/src/org-bus.mjs';
async function fixture() {
const root = await mkdtemp(join(tmpdir(), 'org-archive-'));
const ctx = { configDir: join(root, 'config'), dataRoot: join(root, 'data'), instance: 'test', principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
await mkdir(ctx.configDir, { mode: 0o700 }); await mkdir(ctx.dataRoot, { mode: 0o700 });
await org.createOrg(ctx, 'alpha', await org.templateRender('alpha'));
return ctx;
}
const stopped = async () => ({ stopped: false, reason: 'no host runs' });
test('archive and restore preserve files, increment revisions and append lifecycle log', async () => {
assert.equal(typeof org.archiveOrg, 'function'); assert.equal(typeof org.restoreOrg, 'function');
const c = await fixture(), path = join(c.dataRoot, 'org-runtime', 'alpha', 'evidence');
await writeFile(path, 'immutable evidence'); const docFile = join(c.configDir, 'businesses', 'alpha.json'), original = await readFile(docFile);
let calls = 0;
const archive = await org.archiveOrg(c, 'alpha', { ifRevision: 1, busStop: async (...args) => { calls++; return stopped(...args); } });
assert.equal(archive.outcome, 'done'); assert.equal(archive.step, 'archive');
assert.equal(archive.revision, 2); assert.equal(calls, 1); assert.equal((await listOrgs(c))[0].state, 'archived');
assert.deepEqual(await readFile(docFile), original); assert.equal(await readFile(path, 'utf8'), 'immutable evidence');
const restored = await org.restoreOrg(c, 'alpha', { ifRevision: 2 });
assert.equal(restored.outcome, 'done'); assert.equal(restored.step, 'restore');
assert.equal(restored.revision, 3); assert.equal(calls, 1); assert.equal((await listOrgs(c))[0].state, 'active');
const lines = (await readFile(join(c.dataRoot, 'org-lifecycle.log'), 'utf8')).trim().split('\n').map(JSON.parse);
assert.deepEqual(lines.map(e => [e.op, e.org, e.revision]), [['org.archive', 'alpha', 2], ['org.restore', 'alpha', 3]]);
assert.deepEqual(lines[0].bus, { stopped: false, reason: 'no host runs' }); assert.ok(lines.every(e => e.actor.sub === 'local-dev-owner' && e.time));
});
test('stale revision and uncertain stop leave the organization active', async () => {
const c = await fixture(); let called = 0;
await assert.rejects(org.archiveOrg(c, 'alpha', { ifRevision: 2, busStop: async () => { called++; } }), { code: 'revision-conflict' }); assert.equal(called, 0);
await assert.rejects(org.archiveOrg(c, 'alpha', { ifRevision: 1, busStop: async () => { throw Error('uncertain stop'); } }), /uncertain stop/);
assert.equal((await listOrgs(c))[0].state, 'active');
});
test('archived org refuses bus start, update and setup, and may be deleted', async () => {
const c = await fixture(), doc = (await org.showOrg(c, 'alpha')).doc;
await org.archiveOrg(c, 'alpha', { ifRevision: 1, busStop: stopped });
await assert.rejects(busStart(c, 'alpha'), /org-archived/);
await assert.rejects(org.updateOrg(c, 'alpha', doc, { ifRevision: 2 }), { code: 'org-archived' });
await assert.rejects(org.queueInit(c, 'alpha', { mode: 'local-only', acceptLocalLossRisk: true }), { code: 'org-archived' });
await assert.rejects(org.projectRepoInit(c, 'alpha', 'work', { mode: 'local-only', acceptLocalLossRisk: true }), { code: 'org-archived' });
await org.deleteOrg(c, 'alpha', { confirm: 'alpha', ifRevision: 2, busStop: busStopForDelete });
assert.deepEqual(await listOrgs(c), []);
});
test('concurrent archive and delete share the lifecycle lock', async () => {
const c = await fixture(); let entered, release;
const ready = new Promise(r => { entered = r; }), wait = new Promise(r => { release = r; });
const pending = org.archiveOrg(c, 'alpha', { ifRevision: 1, busStop: async () => { entered(); await wait; return stopped(); } });
await ready;
try { await assert.rejects(org.deleteOrg(c, 'alpha', { confirm: 'alpha', ifRevision: 1, busStop: stopped }), { code: 'org-locked' }); }
finally { release(); await pending; }
assert.equal((await listOrgs(c))[0].state, 'archived');
});
test('archive refuses an unreadable stop result and a revision changed while stopping', async () => {
const c = await fixture(), doc = (await org.showOrg(c, 'alpha')).doc;
await assert.rejects(org.archiveOrg(c, 'alpha', { ifRevision: 1, busStop: async () => null }), /uncertain/i);
await assert.rejects(org.archiveOrg(c, 'alpha', { ifRevision: 1, busStop: async () => { await org.updateOrg(c, 'alpha', doc, { ifRevision: 1 }); return stopped(); } }), { code: 'revision-conflict' });
const entry = (await listOrgs(c))[0]; assert.equal(entry.state, 'active'); assert.equal(entry.revision, 2);
});
+196
View File
@@ -0,0 +1,196 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, writeFile, readFile, lstat, symlink } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import * as org from '../src/org-service.mjs';
import { listOrgs } from '../src/org-registry.mjs';
import { busStopForDelete as busStop } from '../../cli/src/org-bus.mjs';
async function fixture() {
const root = await mkdtemp(join(tmpdir(), 'org-delete-'));
const c = { configDir: join(root, 'config'), dataRoot: join(root, 'data'), instance: 'test', principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
await mkdir(c.configDir, { mode: 0o700 }); await mkdir(c.dataRoot, { mode: 0o700 });
await org.createOrg(c, 'alpha', await org.templateRender('alpha'));
return c;
}
test('delete refuses without a bus stop function', async () => {
const c = await fixture();
await assert.rejects(org.deleteOrg(c, 'alpha', { confirm: 'alpha', ifRevision: 1 }), { code: 'org-invalid' });
assert.equal((await listOrgs(c)).find(e => e.id === 'alpha').state, 'active');
});
test('delete fences confirmation and revision before moving any files', async () => {
const c = await fixture();
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'other', ifRevision: 1 }), { code: 'org-confirm' });
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 2 }), { code: 'revision-conflict' });
assert.equal((await org.showOrg(c, 'alpha')).revision, 1);
});
test('delete preserves owned trees, records a receipt, and permits reuse', async () => {
const c = await fixture();
for (const kind of ['agents', 'grants', 'hosts']) {
const dir = join(c.configDir, 'secrets', kind, 'alpha');
await mkdir(dir, { recursive: true, mode: 0o700 }); await writeFile(join(dir, 'dummy'), 'secret', { mode: 0o600 });
}
const history = join(c.configDir, 'businesses', '.history', 'alpha'); await mkdir(history, { recursive: true, mode: 0o700 }); await writeFile(join(history, 'changes.jsonl'), 'evidence', { mode: 0o600 });
const receipt = await org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 });
assert.equal(await readFile(join(receipt.destination, 'history', 'changes.jsonl'), 'utf8'), 'evidence');
assert.equal((await lstat(receipt.destination)).mode & 0o777, 0o700);
assert.equal(receipt.secrets.length, 3); assert.ok(receipt.secrets.every(s => s.entryCount === 1));
assert.equal((await listOrgs(c)).length, 0);
const stored = JSON.parse(await readFile(join(receipt.destination, 'receipt.json'), 'utf8'));
assert.equal(stored.registry, 'removed');
assert.equal(stored.org, 'alpha'); assert.ok(!JSON.stringify(stored).includes('dummy'));
assert.equal((await readFile(join(c.dataRoot, 'deleted', 'deleted.log'), 'utf8')).trim().split('\n').length, 1);
await org.createOrg(c, 'alpha', await org.templateRender('alpha'));
});
test('delete refuses an owned symlink without moving anything', async () => {
const c = await fixture(); await symlink('/tmp', join(c.dataRoot, 'org-runtime', 'alpha', 'link'));
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), { code: 'org-mismatch' });
assert.equal((await listOrgs(c))[0].state, 'deleting');
});
test('delete refuses a group-readable organization secret directory', async () => {
const c = await fixture(); const dir = join(c.configDir, 'secrets', 'agents', 'alpha');
await mkdir(dir, { recursive: true, mode: 0o700 });
const { chmod } = await import('node:fs/promises'); await chmod(dir, 0o750);
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), { code: 'org-mismatch' });
assert.equal((await listOrgs(c))[0].state, 'deleting');
});
test('delete refuses a symlink within an organization secret directory', async () => {
const c = await fixture(); const dir = join(c.configDir, 'secrets', 'hosts', 'alpha');
await mkdir(dir, { recursive: true, mode: 0o700 }); await symlink('/tmp', join(dir, 'link'));
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), { code: 'org-mismatch' });
assert.equal((await listOrgs(c))[0].state, 'deleting');
});
test('a refused deletion resumes without a revision after the unsafe path is fixed', async () => {
const c = await fixture(); const link = join(c.dataRoot, 'org-runtime', 'alpha', 'link');
await symlink(c.dataRoot, link);
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }));
const { unlink } = await import('node:fs/promises'); await unlink(link);
await org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha' });
assert.deepEqual(await listOrgs(c), []);
});
test('deleting state fences bus start, and an uncertain bus stop retains the organization', async () => {
const c = await fixture();
const hostDir = join(c.dataRoot, 'org-runtime', 'alpha', 'bus-host'); await mkdir(hostDir, { mode: 0o700 });
await writeFile(join(hostDir, 'host.lock'), JSON.stringify({ pid: 99999999, startTime: '0', business: 'alpha' }), { mode: 0o600 });
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), /lock/i);
assert.equal((await listOrgs(c))[0].state, 'deleting');
const { busStart } = await import('../../cli/src/org-bus.mjs');
await assert.rejects(busStart(c, 'alpha'), /being deleted/);
const { unlink } = await import('node:fs/promises'); await unlink(join(hostDir, 'host.lock'));
await org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha' });
});
test('deletion refuses to move files while an in-flight bus start holds its lifecycle fence', async () => {
const c = await fixture(); const lock = join(c.configDir, 'org-lifecycle-alpha.lock');
await writeFile(lock, 'in flight', { mode: 0o600 });
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), /lifecycle/i);
assert.equal((await listOrgs(c))[0].state, 'active');
await lstat(join(c.dataRoot, 'org-runtime', 'alpha'));
const { unlink } = await import('node:fs/promises'); await unlink(lock);
await org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 });
});
test('resume refuses a replaced data root symlink', async () => {
const c = await fixture(), link = join(c.dataRoot, 'org-runtime', 'alpha', 'link');
await symlink(c.configDir, link);
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }));
const { rename, unlink } = await import('node:fs/promises'); await unlink(link); await rename(c.dataRoot, c.dataRoot + '-real'); await symlink(c.dataRoot + '-real', c.dataRoot);
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha' }), { code: 'org-mismatch' });
});
test('bus verification refuses a symlinked host state before reading it', async () => {
const c = await fixture(), host = join(c.dataRoot, 'org-runtime', 'alpha', 'bus-host');
await mkdir(host, { mode: 0o700 });
await symlink(join(c.configDir, 'missing'), join(host, 'host.json'));
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), /symlink/i);
assert.equal((await listOrgs(c))[0].state, 'deleting');
});
test('bus verification refuses a non-file host state', async () => {
const c = await fixture(), host = join(c.dataRoot, 'org-runtime', 'alpha', 'bus-host');
await mkdir(host, { mode: 0o700 }); await mkdir(join(host, 'host.json'), { mode: 0o700 });
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), /unsafe bus state/i);
});
test('delete refuses an answering bus socket even without a recorded host', async t => {
const c = await fixture(), dir = join(c.dataRoot, 'org-runtime', 'alpha', 'bus');
await mkdir(dir, { mode: 0o700 });
const { createServer } = await import('node:net'); const server = createServer(socket => socket.end());
await new Promise((resolve, reject) => server.once('error', reject).listen(join(dir, 'broker.sock'), resolve));
t.after(() => new Promise(resolve => server.close(resolve)));
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), /socket still answers/i);
assert.equal((await listOrgs(c))[0].state, 'deleting');
});
test('a refused move reports the completed bus step and the point of refusal', async () => {
const c = await fixture(); await symlink(c.configDir, join(c.dataRoot, 'org-runtime', 'alpha', 'link'));
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }), e => {
assert.equal(e.deleteStep, 'inspect'); assert.equal(e.deleteReceipt.bus.reason, 'no host runs'); assert.deepEqual(e.deleteReceipt.moves, []); return true;
});
});
test('resume records a move completed before its receipt could be updated', async () => {
const c = await fixture(), root = join(c.dataRoot, 'org-runtime', 'alpha'), link = join(root, 'link');
await symlink(c.configDir, link);
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }));
const entry = (await listOrgs(c))[0];
const { unlink, rename } = await import('node:fs/promises'); await unlink(link);
await rename(root, join(c.dataRoot, 'deleted', entry.deletion.folder, 'runtime'));
const receipt = await org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha' });
assert.ok(receipt.moves.some(move => move.from === root)); assert.equal(receipt.registry, 'removed');
});
test('deleting another organization appends without rewriting prior log entries', async () => {
const c = await fixture();
await org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 });
const file = join(c.dataRoot, 'deleted', 'deleted.log'), before = await readFile(file, 'utf8');
await org.createOrg(c, 'beta', await org.templateRender('beta'));
await org.deleteOrg(c, 'beta', { busStop, confirm: 'beta', ifRevision: 1 });
const after = await readFile(file, 'utf8');
assert.ok(after.startsWith(before)); assert.equal(after.trim().split('\n').length, 2);
});
test('resume refuses an instance rebound to a different data root', async () => {
const c = await fixture(), link = join(c.dataRoot, 'org-runtime', 'alpha', 'link');
await symlink(c.configDir, link);
await assert.rejects(org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }));
const other = c.dataRoot + '-other'; await mkdir(other, { mode: 0o700 });
await assert.rejects(org.deleteOrg({ ...c, dataRoot: other }, 'alpha', { busStop, confirm: 'alpha' }), { code: 'org-mismatch' });
await assert.rejects(lstat(join(other, 'deleted')), { code: 'ENOENT' });
});
// Hold the first request immediately before its bus receipt is published.
test('overlapping deletes refuse before touching the receipt', async () => {
const c = await fixture();
const fs = (await import('node:fs/promises')).default;
const { syncBuiltinESMExports } = await import('node:module');
let entered, release;
const paused = new Promise(resolve => { entered = resolve; });
const proceed = new Promise(resolve => { release = resolve; });
const original = fs.rename; let pausedOnce = false;
fs.rename = async (from, to) => {
if (!pausedOnce && to.endsWith('/receipt.json')) {
const value = JSON.parse(await readFile(from, 'utf8'));
if (value.bus && value.moves.length === 0) { pausedOnce = true; entered(); await proceed; }
}
return original(from, to);
};
syncBuiltinESMExports();
let first;
try {
first = org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 });
await paused;
const second = await org.deleteOrg(c, 'alpha', { busStop, confirm: 'alpha', ifRevision: 1 }).then(value => ({ value }), error => ({ error }));
assert.equal(second.error?.code, 'org-locked');
} finally {
release();
try { await first; } catch { /* preserve the assertion failure */ }
fs.rename = original; syncBuiltinESMExports();
}
const result = await first;
const stored = JSON.parse(await readFile(join(result.destination, 'receipt.json'), 'utf8'));
assert.equal(stored.registry, 'removed'); assert.equal(stored.moves.length, 2);
});
@@ -0,0 +1,32 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import * as service from '../src/org-service.mjs';
const { templateRender, validateOrg } = service;
test('installed modules supply presets and report availability without credentials', async () => {
const presets = await service.orgPresets({ env: { PATH: '/usr/bin:/bin' } });
const fixture = presets.find(p => p.name === 'fixture');
const claude = presets.find(p => p.name === 'claude-code');
assert.equal(fixture.available.ok, true);
assert.equal(claude.available.ok, false);
assert.ok(claude.available.reason);
assert.deepEqual(claude.thinking.levels, ['low', 'medium', 'high', 'xhigh', 'max']);
});
test('preset overlays produce valid documents and the legacy alias remains identical', async () => {
const doc = await templateRender('alpha', { preset: 'fixture' });
assert.deepEqual(doc, await templateRender('alpha', { fixture: 'local' }));
assert.deepEqual(await validateOrg(doc), { ok: true, errors: [] });
assert.equal(doc.vars['bus.tester'], true);
const claude = await templateRender('alpha', { preset: 'claude-code', thinking: 'medium' });
assert.deepEqual(claude.roles.assistant.vars, { harness: 'claude-code', model: 'opus', thinking: 'medium' });
assert.equal(claude.launch.models.opus.model, 'claude-opus-5-5');
assert.equal(claude.vars, undefined);
assert.deepEqual(await validateOrg(claude), { ok: true, errors: [] });
});
test('unknown presets and thinking levels refuse', async () => {
await assert.rejects(templateRender('alpha', { preset: 'no-such-preset' }), /unknown preset/);
await assert.rejects(templateRender('alpha', { preset: 'claude-code', thinking: 'off' }), /thinking/);
await assert.rejects(templateRender('alpha', { thinking: 'low' }), /thinking/);
});
@@ -0,0 +1,43 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { spawn } from 'node:child_process';
import { createHash } from 'node:crypto';
import { reserveOrg, activateOrg, resolveOrg, orgRoot, withRegistryWriter } 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, { 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('activation records the digest of an existing organization document', async () => {
const c = fixture(), dir = join(c.configDir, 'businesses'), raw = '{"id":"alpha"}\n';
mkdirSync(dir, { mode: 0o700 }); writeFileSync(join(dir, 'alpha.json'), raw);
await reserveOrg(c, 'alpha');
const active = await activateOrg(c, 'alpha');
assert.equal(active.digest, createHash('sha256').update(raw).digest('hex'));
});
test('activation refuses a symlinked organization document', async () => {
const c = fixture(), dir = join(c.configDir, 'businesses');
mkdirSync(dir, { mode: 0o700 }); writeFileSync(join(dir, 'other.json'), '{}');
symlinkSync(join(dir, 'other.json'), join(dir, 'alpha.json'));
await reserveOrg(c, 'alpha');
await assert.rejects(activateOrg(c, 'alpha'));
assert.equal((await resolveOrg(c, 'alpha')).state, 'reserved');
});
test('the first registry reservation waits for a legacy writer already holding the lock', async () => {
const c = fixture();
let release;
const hold = new Promise(resolve => { release = resolve; });
const writer = withRegistryWriter(c.configDir, () => hold);
let reserved = false;
const reservation = reserveOrg(c, 'alpha').then(value => { reserved = true; return value; });
try {
await new Promise(resolve => setTimeout(resolve, 80));
assert.equal(reserved, false);
} finally { release(); await writer; await reservation; }
});
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')); });
@@ -0,0 +1,100 @@
import { test } from 'node:test';
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, readFileSync, statSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
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' });
assert.deepEqual(doc.plugins, { channel: ['cli', 'webui'], harness: ['pi'], 'model-provider': ['fixture'] });
assert.deepEqual(doc.roles.assistant.vars, { harness: 'pi', model: 'fixture-deterministic', thinking: 'off' });
assert.equal(doc.launch.models['fixture-deterministic'].module, 'fixture');
assert.equal(doc.vars['bus.tester'], true);
});
test('creation records the explicit no-notifications choice privately', async () => {
const c = context();
const result = await createOrg(c, 'alpha', await templateRender('alpha'));
const file = join(result.root, 'notify/alpha/notify.json');
assert.deepEqual(JSON.parse(readFileSync(file, 'utf8')), { notifyVersion: 1, binding: null });
assert.equal(statSync(file).mode & 0o777, 0o600);
assert.equal(result.notifications, 'none');
assert.equal((await notificationShow(c, 'alpha')).currentNotifications, 'none');
writeFileSync(file, JSON.stringify({ notifyVersion: 1, binding: 'alerts' }));
const later = await notificationShow(c, 'alpha');
assert.equal(later.notifications, 'none');
assert.equal(later.currentNotifications, 'configured');
});
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 })); });
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('reading beta does not contend with an alpha document writer', async () => {
const c = context();
for (const id of ['alpha', 'beta']) await createOrg(c, id, await templateRender(id, { fixture: 'local' }));
const writer = organizationWriter({ dir: c.configDir, rolesDir: REPO_ROLES });
const { document, revision } = writer.read('alpha'); document.plugins.channel = ['cli'];
let attempted = false, error;
const attempt = () => { if (attempted) return; attempted = true; try { writer.write('alpha', document, { revision, actor: c.principal.sub }); } catch (e) { error = e; } };
const watcher = watch(c.configDir, (_event, name) => { if (name === 'org-runtime.lock') attempt(); });
try { await showOrg(c, 'beta'); attempt(); } finally { watcher.close(); }
if (error) throw error;
assert.deepEqual((await showOrg(c, 'alpha')).doc.plugins.channel, ['cli']);
});
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(() => {}); }
});
test('service updates wait for the broker writer asynchronous document hold', async () => {
const c = context(); await createOrg(c, 'alpha', await templateRender('alpha'));
const current = await showOrg(c, 'alpha');
const writer = organizationWriter({ dir: c.configDir, rolesDir: REPO_ROLES });
let release; const gate = new Promise(resolve => { release = resolve; });
const held = writer.holdAsync('alpha', () => gate);
let settled = false;
const update = updateOrg(c, 'alpha', current.doc, { ifRevision: current.revision }).finally(() => { settled = true; });
try {
await new Promise(resolve => setTimeout(resolve, 100));
assert.equal(settled, false, 'service must not publish during a broker document hold');
} finally { release(); await held; }
await update;
});
@@ -16,8 +16,9 @@ test('P3 a proxy model without route refuses proxy-route-missing', () => {
});
});
test('P3 an unknown proxy route refuses proxy-route-unknown', () => {
for (const route of ['openai-codex', 'unknown', '', null, 7]) {
test('proxy route syntax is bounded and unknown names are deferred to the provider module', () => {
for (const route of ['openai-codex', 'unknown']) assert.equal(checkModels({ selected: { provider: 'proxy', model: 'proxy-only-id', route } }).selected.route, route);
for (const route of ['', null, 7, '../bad']) {
assert.throws(() => checkModels({ selected: { provider: 'proxy', model: 'proxy-only-id', route } }), {
code: 'proxy-route-unknown',
});
+36
View File
@@ -1232,3 +1232,39 @@ 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.
`mosaic org delete ID --confirm ID --if-revision N [--json]` stops the
organization bus, moves its local organization document, history, runtime and
organization-specific secret directories into the instance's private
`deleted/` directory, and removes it from the registry. The ID may then be
reused. The deleted folder contains a receipt; `deleted/deleted.log` is an
append-only index. Remote repositories and remote credentials are untouched.
Purging deleted data and revoking remote credentials are separate operations and
are not provided by this command.
If deletion stops, the organization remains in `deleting` state and cannot
start a bus. Inspect the reported receipt and correct the refusal, then resume
with `mosaic org delete ID --confirm ID`; the revision is only required for the
first attempt. Completed moves are not rolled back. A held lifecycle lock can
mean a bus start is still in progress; if a process crashed and left its lock,
inspect it before removing it and resuming. Moves use rename and therefore
require the source and deleted folder to be on the same filesystem.
+155 -106
View File
@@ -8,11 +8,10 @@
// host (src/host.mjs) the systemd unit mosaic-bus@<business> runs; with
// --pm it also launches the business's PM (src/launcher.mjs).
import { lstatSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { readFileSync } from "node:fs";
import { basename, join } from "node:path";
import { createInterface } from "node:readline/promises";
import { fileURLToPath } from "node:url";
import { BINDING_NAME } from "../../discord/src/binding.mjs";
import { projectBusCommand, projectCommand, PROJECT_BUS_USAGE, PROJECT_USAGE } from "./project.mjs";
import { resolveThread, threadCommand, threadMembers, threadsCommand, THREAD_TALK_USAGE, THREAD_USAGE, THREADS_USAGE } from "./thread.mjs";
import { sessionCommand, SESSION_USAGE } from "./session.mjs";
@@ -27,7 +26,10 @@ import { accessCommand, accessContextFor } from "./access.mjs";
import { createChannelCaller } from "../../plugins/src/channel.mjs";
import { PluginError } from "../../plugins/src/kinds.mjs";
import { CliError } from "./errors.mjs";
import { bootConfig, loadSystem, socketPath } from "./config.mjs";
import { readNotifyConfig } from "./notify-config.mjs";
export { readNotifyConfig } from "./notify-config.mjs";
import { DEV_OWNER, devOwnerInstance } from "./dev-local-owner.mjs";
import { bootConfig, loadSystem, orgPaths, socketPath } from "./config.mjs";
import { bootLine, readBootStatus, recordBootOk, recordBootRefused } from "./boot-status.mjs";
import { businessFilePath, configDir } from "../../business/src/index.mjs";
import { busExit, humanTransport, refuseInsideAgent, testerTransport } from "./transport.mjs";
@@ -37,15 +39,22 @@ import { createLauncher } from "./launcher.mjs";
import { quitLine, runApp } from "./app.mjs";
import { readSessions, stopSession } from "../../seat/src/session.mjs";
import { SeatError } from "../../seat/src/seat.mjs";
import { OrgError, resolveOrg, withOrgLifecycle } from "../../business/src/org-registry.mjs";
import { busStart, busStatus, busStop, busRestart, busRecover } from "./org-bus.mjs";
import { consoleStart, consoleStop } from "./org-console.mjs";
import { usageCommand, USAGE_USAGE } from "./usage.mjs";
import { orgCommand, ORG_USAGE } from "./org.mjs";
import { devCommand, DEV_USAGE, fixtureLaunchEnv, fixtureLauncherOptions } from "./dev-fixture.mjs";
import { runCommand, RUN_USAGE } from "./run-evidence.mjs";
export const USAGE = `usage:
${ORG_USAGE}
${USAGE_USAGE}
${LIMITS_USAGE}
${PLUGINS_USAGE}
${CREDENTIALS_USAGE}
mosaic proxy fetch | init [--port N] [--business ID] [--rotate-grace-minutes N] [--first-output-seconds N]
mosaic proxy fetch | fence status [--json] | fence clear --proxy-restarted | init [--port N] [--business ID] [--rotate-grace-minutes N] [--first-output-seconds N]
${HOSTS_USAGE}
${GRANTS_USAGE}
${INSTANCES_USAGE}
@@ -68,12 +77,16 @@ export const USAGE = `usage:
mosaic halt [--business <id>]
mosaic resume [--business <id>]
mosaic processing status [--business <id>] [--json]
mosaic stop <run>
mosaic stop <run> --business <id>
mosaic launches off|on [--business <id>]
mosaic launches list [--json]
mosaic bus start <business> [--pm | --no-launch]
mosaic bus stop
mosaic bus status [--json]`;
mosaic launches list --business <id> [--json]
mosaic bus start <id> [--pm | --no-launch] [--dev-local-owner] [--detach]
mosaic bus stop|restart|recover <id>
mosaic bus status <id> [--json]
mosaic console start [--dev-local-owner] [--detach] [--port auto|N]
mosaic console stop
${DEV_USAGE}
${RUN_USAGE}`;
// packages/seat/src/cli.mjs USAGE, which that module does not export (it runs
// its main on import). tests/app.test.mjs fails if the two drift.
@@ -98,34 +111,6 @@ function parse(args, { flags = [], values = [] }) {
return out;
}
// The notifier's private config: `<dataRoot>/notify/<business>/notify.json`,
// 0600, `{"notifyVersion": 1, "binding": "<discord binding>" | null}`.
// Missing refuses, so a host never starts without someone deciding whether
// it DMs; null runs the host without a notifier.
export function readNotifyConfig(dataRoot, business) {
const file = join(dataRoot, "notify", business, "notify.json");
let st;
try {
st = lstatSync(file);
} catch {
throw new CliError(`no notifier config at ${file}; write {"notifyVersion": 1, "binding": "<discord binding>"} there, mode 0600, or "binding": null to run without DMs`, 3);
}
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600) {
throw new CliError(`notifier config must be a regular file, mode 0600, owned by this user: ${file}`, 3);
}
let doc;
try {
doc = JSON.parse(readFileSync(file, "utf8"));
} catch {
throw new CliError(`notifier config is not JSON: ${file}`, 3);
}
const keys = doc && typeof doc === "object" && !Array.isArray(doc) ? Object.keys(doc).sort().join(",") : "";
if (keys !== "binding,notifyVersion" || doc.notifyVersion !== 1 || (doc.binding !== null && !(typeof doc.binding === "string" && BINDING_NAME.test(doc.binding)))) {
throw new CliError(`notifier config must be exactly {"notifyVersion": 1, "binding": <binding name> | null}: ${file}`, 3);
}
return doc.binding;
}
// --business, else the running host's business.
function businessFor(parsed, dataRoot) {
if (parsed.values["--business"]) return parsed.values["--business"];
@@ -199,10 +184,18 @@ function callFor(parsed, io, deps) {
const tester = parsed.flags.has("--tester");
if (!tester) refuseInsideAgent(io.env);
const system = deps.system ?? loadSystem({ env: io.env });
const business = businessFor(parsed, system.dataRoot);
const business = parsed.values["--business"] ?? (deps.transport || deps.testerTransport ? businessFor(parsed, system.dataRoot) : null);
if (!business) throw usage("name the organization with --business");
parsed.business = business;
if (tester) return deps.testerTransport ? deps.testerTransport(business) : testerTransport({ socket: socketPath(system.dataRoot), business, env: io.env, spawn: deps.testerSpawn });
return deps.transport ? deps.transport(business) : humanTransport({ socket: socketPath(system.dataRoot), business, env: io.env });
if (tester && deps.testerTransport) return deps.testerTransport(business);
if (!tester && deps.transport) return deps.transport(business);
return async (verb, args) => {
const org = await resolveOrg(organizationContext(io, deps), business);
if (org.state !== "active") throw new CliError("organization is not active", 3);
const options = { socket: orgPaths(org.root).socket, business, env: io.env };
const call = tester ? testerTransport({ ...options, spawn: deps.testerSpawn }) : humanTransport(options);
return call(verb, args);
};
}
@@ -479,11 +472,13 @@ async function seat(fn) {
}
async function stop(rest, io, deps) {
const parsed = parse(rest, {});
const parsed = parse(rest, { values: ["--business"] });
if (parsed.positional.length !== 1) throw usage("stop takes one run id; see mosaic launches list");
refuseInsideAgent(io.env);
const system = deps.system ?? loadSystem({ env: io.env });
const r = await seat(() => stopSession(system.dataRoot, parsed.positional[0], deps.stopTimeoutMs ? { timeoutMs: deps.stopTimeoutMs } : {}));
const id = parsed.values["--business"];
if (!id && !deps.system) throw usage("stop needs --business");
const root = id ? (await resolveOrg(organizationContext(io, deps), id)).root : deps.system.dataRoot;
const r = await seat(() => stopSession(root, parsed.positional[0], deps.stopTimeoutMs ? { timeoutMs: deps.stopTimeoutMs } : {}));
io.stdout.write(r.stopped ? `stopped ${r.business}/${r.instance} run ${r.run}\n` : `${r.reason}\n`);
}
@@ -523,18 +518,19 @@ async function launches(rest, io, deps) {
const parsed = parse(args, { values: ["--business"] });
if (parsed.positional.length !== 0) throw usage(`launches ${sub} takes no arguments`);
refuseInsideAgent(io.env);
const system = deps.system ?? loadSystem({ env: io.env });
const business = businessFor(parsed, system.dataRoot);
const call = deps.transport ? deps.transport(business) : humanTransport({ socket: socketPath(system.dataRoot), business, env: io.env });
const call = callFor(parsed, io, deps);
await call(sub === "off" ? "launch.revoke" : "launch.restore");
const business = parsed.business;
io.stdout.write(sub === "off" ? `launches off for ${business}: the PM's launch requests are refused until mosaic launches on; running sessions keep running\n` : `launches on for ${business}\n`);
return;
}
if (sub === "list") {
const parsed = parse(args, { flags: ["--json"] });
const parsed = parse(args, { flags: ["--json"], values: ["--business"] });
if (parsed.positional.length !== 0) throw usage("launches list takes no arguments");
const system = deps.system ?? loadSystem({ env: io.env });
const list = await seat(() => readSessions(system.dataRoot));
const id = parsed.values["--business"];
if (!id && !deps.system) throw usage("launches list needs --business");
const root = id ? (await resolveOrg(organizationContext(io, deps), id)).root : deps.system.dataRoot;
const list = await seat(() => readSessions(root));
if (parsed.flags.has("--json")) return io.stdout.write(`${JSON.stringify(list, null, 2)}\n`);
if (list.length === 0) return io.stdout.write("no sessions\n");
const lines = list.map((s) => `${s.run} ${s.business}/${s.instance} ${s.harness} ${s.model} by ${s.launchedBy} since ${s.startedAt} ${s.live ? "running" : "not running (stale)"}`);
@@ -543,46 +539,99 @@ async function launches(rest, io, deps) {
throw usage();
}
function organizationContext(io, deps) {
const dir = configDir(io.env);
const match = /^mosaic-([a-z][a-z0-9]{1,15})$/.exec(basename(dir));
if (!match) throw new CliError("organization commands require an instance config directory", 3);
const instance = match[1];
const system = deps.system ?? loadSystem({ env: io.env, instance });
return { configDir: dir, dataRoot: system.dataRoot, instance, env: io.env, system };
}
async function consoleCommand(rest, io, deps) {
const [sub, ...args] = rest;
if (sub === "stop") {
if (args.length) throw usage("console stop takes no arguments");
io.stdout.write(`${JSON.stringify(await consoleStop(organizationContext(io, deps), { warn: s => io.stderr.write(s) }))}\n`);
return;
}
if (sub !== "start") throw usage();
const parsed = parse(args, { flags: ["--dev-local-owner", "--detach"], values: ["--port"] });
if (parsed.positional.length) throw usage("console start takes no positional arguments");
const receipt = await consoleStart(organizationContext(io, deps), { devLocalOwner: parsed.flags.has("--dev-local-owner"), detach: parsed.flags.has("--detach"), port: parsed.values["--port"] ?? "auto", warn: s => io.stderr.write(s) });
io.stdout.write(`${JSON.stringify(receipt)}\n`);
if (!parsed.flags.has("--detach")) {
const code = await receipt.done;
if (code !== 0) throw new CliError("console stopped unexpectedly", code);
}
}
async function bus(rest, io, deps) {
const [sub, ...args] = rest;
if (sub === "start") {
const parsed = parse(args, { flags: ["--pm", "--no-launch"] });
if (parsed.positional.length !== 1) throw usage("bus start takes one business");
// Row 163: --no-launch is the D2 rehearsal's host. No unit passes it.
const launches = !parsed.flags.has("--no-launch");
if (!launches && parsed.flags.has("--pm")) throw usage("bus start takes --pm or --no-launch, not both");
const parsed = parse(args, { flags: ["--pm", "--no-launch", "--dev-local-owner", "--detach", "--internal-host"] });
if (parsed.positional.length !== 1) throw usage("bus start takes one organization");
if (parsed.flags.has("--no-launch") && parsed.flags.has("--pm")) throw usage("bus start takes --pm or --no-launch, not both");
if (parsed.flags.has("--dev-local-owner")) devOwnerInstance(io.env);
const ctx = organizationContext(io, deps);
const businessId = parsed.positional[0];
const system = deps.system ?? loadSystem({ env: io.env });
// Fail closed, and say so (vikunja:19/302): a refused load is recorded where the Console and
// `bus status` can read it, then thrown as before. No retry.
const file = businessFilePath(businessId, configDir(io.env));
let boot;
try {
boot = bootConfig({ system, businessId, env: io.env, warn: (w) => io.stderr.write(`mosaic-bus: warning: ${w}\n`) });
} catch (e) {
try { recordBootRefused(system.dataRoot, { business: businessId, file, reason: e.message }); } catch { /* the original refusal still throws */ }
throw e;
}
try { recordBootOk(system.dataRoot, { business: businessId, file }); } catch { /* status is advisory */ }
const binding = readNotifyConfig(system.dataRoot, businessId);
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null });
try {
const launcher = createLauncher({ host, system, env: io.env, tracker: Boolean(boot.trackers?.[businessId]), launches, ...(deps.launcher ?? {}) });
await launcher.listen();
io.stdout.write(`bus host up: business ${businessId}, socket ${host.path}, notifier ${binding ?? "off"}${launches ? "" : ", launches disabled"}\n`);
if (parsed.flags.has("--pm")) {
try {
const pm = await launcher.launchPm();
io.stdout.write(`launched ${pm.instance}: run ${pm.run}, ${pm.harness} ${pm.model}\n`);
} catch (e) {
if (e.code !== 'processing-halted') throw e;
io.stdout.write('processing halted: chief launch skipped; control bus remains available\n');
}
if (!parsed.flags.has("--internal-host")) {
const receipt = await busStart(ctx, businessId, { detach: parsed.flags.has("--detach"), devLocalOwner: parsed.flags.has("--dev-local-owner"), pm: parsed.flags.has("--pm"), noLaunch: parsed.flags.has("--no-launch") });
io.stdout.write(`${JSON.stringify(receipt)}\n`);
if (!parsed.flags.has("--detach")) {
const code = await receipt.done;
if (code !== 0) throw new CliError("bus host stopped after a child exited", code);
}
} catch (e) {
await host.close(1);
throw e;
return;
}
const host = await withOrgLifecycle(ctx, businessId, async () => {
const org = await resolveOrg(ctx, businessId);
if (org.state !== "active") throw new CliError("organization is not active", 3);
orgPaths(org.root);
// Row 163: --no-launch is the D2 rehearsal's host. No unit passes it.
const launches = !parsed.flags.has("--no-launch");
if (parsed.flags.has("--no-launch") && parsed.flags.has("--pm")) throw usage("bus start takes --pm or --no-launch, not both");
const instance = parsed.flags.has("--dev-local-owner") ? devOwnerInstance(io.env) : null;
const system = ctx.system;
// Fail closed, and say so (vikunja:19/302): a refused load is recorded where the Console and
// `bus status` can read it, then thrown as before. No retry.
const file = businessFilePath(businessId, configDir(io.env));
let boot;
try {
boot = bootConfig({ system, businessId, orgRoot: org.root, env: io.env, warn: (w) => io.stderr.write(`mosaic-bus: warning: ${w}\n`) });
} catch (e) {
try { recordBootRefused(org.root, { business: businessId, file, reason: e.message }); } catch { /* the original refusal still throws */ }
throw e;
}
if (instance) boot.consoleOwners = [...(boot.consoleOwners ?? []), DEV_OWNER];
try { recordBootOk(org.root, { business: businessId, file }); } catch { /* status is advisory */ }
const binding = readNotifyConfig(org.root, businessId);
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches } });
try {
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
const launcher = createLauncher({ host, system: { ...system, instanceRoot: system.dataRoot, dataRoot: org.root }, env: launchEnv, tracker: Boolean(boot.trackers?.[businessId]), launches, ...(instance ? fixtureLauncherOptions({ system, env: launchEnv, instance, doc: orgDoc }) : {}), ...(deps.launcher ?? {}) });
await launcher.listen();
if (instance) io.stdout.write(`DEV LOCAL OWNER MODE: console owner local-dev accepted (named instance ${instance})\n`);
if (instance && parsed.flags.has("--pm")) io.stdout.write('DEV LOCAL OWNER MODE: launched agents run as this OS user and can reach the loopback Console as owner; use this instance for UI testing only\n');
io.stdout.write(`bus host up: business ${businessId}, socket ${host.path}, notifier ${binding ?? "off"}${launches ? "" : ", launches disabled"}\n`);
if (parsed.flags.has("--pm")) {
try {
const pm = await launcher.launchPm();
io.stdout.write(`launched ${pm.instance}: run ${pm.run}, ${pm.harness} ${pm.model}\n`);
} catch (e) {
if (e.code !== 'processing-halted') throw e;
io.stdout.write('processing halted: chief launch skipped; control bus remains available\n');
}
}
if (process.send) process.send({ orgReady: businessId });
} catch (e) {
await host.close(1);
throw e;
}
return host;
});
const stop = () => host.close(0);
process.on("SIGTERM", stop);
process.on("SIGINT", stop);
@@ -593,30 +642,15 @@ async function bus(rest, io, deps) {
if (code !== 0) throw new CliError("bus host stopped after a child exited", code);
return;
}
if (sub === "stop") {
if (args.length !== 0) throw usage("bus stop takes no arguments");
const system = deps.system ?? loadSystem({ env: io.env });
const r = await stopHost(system.dataRoot);
io.stdout.write(r.stopped ? `stopped bus host for ${r.business} (pid ${r.pid})\n` : `${r.reason}\n`);
if (["stop", "restart", "status", "recover"].includes(sub)) {
const parsed = parse(args, { flags: sub === "status" ? ["--json"] : [] });
if (parsed.positional.length !== 1) throw usage(`bus ${sub} takes one organization`);
if (sub === "recover") devOwnerInstance(io.env);
const ctx = organizationContext(io, deps), id = parsed.positional[0];
const result = sub === "stop" ? await busStop(ctx, id) : sub === "restart" ? await busRestart(ctx, id) : sub === "recover" ? await busRecover(ctx, id) : await busStatus(ctx, id);
io.stdout.write(`${JSON.stringify(result, null, parsed.flags.has("--json") ? 2 : 0)}\n`);
return;
}
if (sub === "status") {
const parsed = parse(args, { flags: ["--json"] });
if (parsed.positional.length !== 0) throw usage("bus status takes no arguments");
const system = deps.system ?? loadSystem({ env: io.env });
const s = hostStatus(system.dataRoot);
s.boot = readBootStatus(system.dataRoot);
if (parsed.flags.has("--json")) return io.stdout.write(`${JSON.stringify(s, null, 2)}\n`);
const lines = [
s.host ? `host: ${s.host.business}, pid ${s.host.pid}, ${s.host.live ? "running" : "not running (stale state file)"}, since ${s.host.startedAt}, notifier ${s.host.notifier ?? "off"}` : "host: none",
`socket: ${s.socket ? "present" : "absent"}`,
s.writerLock
? `writer.lock: pid ${s.writerLock.pid ?? "?"}${s.writerLock.live ? "" : " (not running: a broker died hard; remove the lock by hand once you've checked, see packages/bus/README.md)"}`
: "writer.lock: absent",
bootLine(s.boot) + (s.boot && !s.boot.ok && s.host?.live ? " (a host started earlier still runs on the old file)" : ""),
];
return io.stdout.write(`${lines.join("\n")}\n`);
}
throw usage();
}
@@ -637,7 +671,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));
@@ -671,6 +713,9 @@ export async function main(argv, io = { env: process.env, stdin: process.stdin,
if (["halt", "resume", "processing"].includes(verb)) return processingCommand(verb, rest, io, deps);
if (verb === "launches") return launches(rest, io, deps);
if (verb === "bus") return bus(rest, io, deps);
if (verb === "console") return consoleCommand(rest, io, deps);
if (verb === "dev") return devCommand(rest, io, { system: deps.system });
if (verb === "run") return runCommand(rest, io, { system: deps.system, callFor: (p) => channelCallFor(p, io, deps) });
if (verb === "-h" || verb === "--help") return io.stdout.write(`${USAGE}\n`);
throw usage();
}
@@ -688,6 +733,10 @@ export async function run(argv, io = { env: process.env, stdin: process.stdin, s
await main(argv, io, deps);
return 0;
} catch (error) {
if (error instanceof OrgError) {
io.stderr.write(`mosaic: ${error.code}: ${error.message}\n`);
return 3;
}
if (!(error instanceof CliError)) throw error;
io.stderr.write(`mosaic: ${error.message}\n`);
if (OTHER_TREE_CODES.has(error.code)) io.stderr.write(`mosaic: ${OTHER_TREE}\n`);
+23 -6
View File
@@ -6,7 +6,7 @@
import { stackBinding } from "../../tasks/src/builtin.mjs";
import { spawnSync } from "node:child_process";
import { existsSync, lstatSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { dirname, isAbsolute, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { managedState } from "../../business/src/project-bootstrap.mjs";
import { BusinessError, accessFilePath, configDir, loadAccess, loadBusiness, loadProject, projectFilePath, resolveInstance, systemVars } from "../../business/src/index.mjs";
@@ -21,8 +21,24 @@ export const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..",
// `<dataRoot>/bus/writer.lock` (packages/bus/src/store.mjs).
export const socketPath = (dataRoot) => join(dataRoot, "bus", "broker.sock");
export function loadSystem({ env = process.env } = {}) {
const proc = spawnSync(process.execPath, [join(REPO, "scripts", "mosaic-config.mjs"), "validate"], {
// Linux sockaddr_un.sun_path holds at most 107 bytes plus its terminator.
// Check every socket at the boundary, before starting any host or child.
export function orgPaths(root) {
if (typeof root !== "string" || !isAbsolute(root) || resolve(root) !== root || root.includes("\0")) {
throw new CliError("organization root must be a canonical absolute path", 3);
}
const busDir = join(root, "bus"), hostDir = join(root, "bus-host");
const socket = join(busDir, "broker.sock");
if ([socket, join(hostDir, "launch.sock"), join(hostDir, "git.sock")].some((p) => Buffer.byteLength(p) > 107)) {
throw new CliError("organization socket path is too long", 3);
}
return { socket, busDir, runs: join(root, "runs"), launches: join(root, "launches"),
sessions: join(root, "sessions"), workspaces: join(root, "workspaces"),
threads: join(root, "threads"), hostDir };
}
export function loadSystem({ env = process.env, instance = null } = {}) {
const proc = spawnSync(process.execPath, [join(REPO, "scripts", "mosaic-config.mjs"), "validate", ...(instance === null ? [] : ["--instance", instance])], {
encoding: "utf8",
maxBuffer: 1024 * 1024,
env,
@@ -128,21 +144,22 @@ export const consoleOwners = (sso) =>
// The `{op: 'boot'}` config for packages/bus/src/process.mjs, for one
// business. The host holds a reader capability for the notifier and binds
// launches later through bindLaunch, so `launches` starts empty.
export function bootConfig({ system, businessId, env = process.env, warn = () => {} }) {
export function bootConfig({ system, businessId, orgRoot = system.dataRoot, env = process.env, warn = () => {} }) {
return business(() => {
const vars = systemVars(system);
const b = loadBusiness(businessId, { dir: configDir(env), rolesDir: rolesDir(env) });
const resolved = {};
for (const instance of Object.keys(b.roles)) resolved[instance] = resolveInstance({ system: vars, business: b, instance });
const config = {
dataRoot: system.dataRoot,
dataRoot: orgRoot,
...(orgRoot === system.dataRoot ? {} : { instanceRoot: system.dataRoot, org: { id: b.id }, instanceDataRoot: system.dataRoot, instanceConfigDir: configDir(env) }),
...(system.proxy ? { proxy: system.proxy } : {}),
businesses: { [b.id]: busBusiness(b, resolved) },
organization: {dir:configDir(env),rolesDir:rolesDir(env),system:vars},
launches: [],
readers: [b.id],
};
const tracker = trackerFor(vars, b, warn, system.dataRoot);
const tracker = trackerFor(vars, b, warn, orgRoot);
if (tracker) {
config.trackers = { [b.id]: tracker.tracker };
if (tracker.builtinBinding) config.builtinBinding = tracker.builtinBinding;
+212
View File
@@ -0,0 +1,212 @@
// `mosaic dev fixture init|status|stop`: the deterministic model fixture for a
// throwaway named instance (r4 §7). init starts the fixture provider's loopback
// endpoint (packages/plugins/model-provider/fixture/endpoint.mjs) and records a
// non-secret state file under the instance data root. It never writes the
// system config. A bus started with --dev-local-owner on that instance hands the
// endpoint to the launcher as its session proxy context (fixtureLauncherOptions),
// so a launch runs the real launcher, sandbox, runner and pi against it.
// Refuses: the default dev instance, an instance with a proxy configured, a
// provider credential in the environment, a non-loopback endpoint and a second
// fixture. Exit codes as errors.mjs: 2 invalid, 3 refused, 4 usage.
import { spawn } from 'node:child_process';
import { closeSync, constants, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { get } from 'node:http';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { CliError } from './errors.mjs';
import { DEV_OWNER, devOwnerInstance } from './dev-local-owner.mjs';
import { loadSystem } from './config.mjs';
import { parseArgs } from './thread.mjs';
import { isProviderVariable } from '../../seat/src/session.mjs';
import { FIXTURE_ENTRY, FIXTURE_KEY, FIXTURE_KEY_NAME, FIXTURE_MODEL } from '../../plugins/model-provider/fixture/index.mjs';
export const ENDPOINT = resolve(dirname(fileURLToPath(import.meta.url)), '../../plugins/model-provider/fixture/endpoint.mjs');
export const DEV_USAGE = `mosaic dev fixture init --model deterministic [--instance NAME] [--bind 127.0.0.1] [--json]
mosaic dev fixture status [--json]
mosaic dev fixture stop [--json]`;
export const fixtureDir = dataRoot => join(dataRoot, 'dev-fixture');
export const fixtureFile = dataRoot => join(fixtureDir(dataRoot), 'fixture.json');
const LOOPBACK = /^http:\/\/127\.0\.0\.1:([0-9]{1,5})$/;
const PRINCIPAL = `${DEV_OWNER.provider}:${DEV_OWNER.sub}`;
const refuse = (code, message) => Object.assign(new CliError(`${code}: ${message}`, 3), { code });
const invalid = message => new CliError(`${message}\nusage: ${DEV_USAGE}`, 4);
// Names, never values, of provider credential variables the environment holds.
export function credentialVariables(env) {
return Object.keys(env).filter(k => isProviderVariable(k) && Boolean(env[k])).sort();
}
function noCredentials(env) {
const held = credentialVariables(env);
if (held.length) throw refuse('fixture-credential-present', `the fixture runs without real credentials; unset ${held.join(', ')}`);
}
function ownedPrivate(path, directory) {
const st = lstatSync(path);
if (st.isSymbolicLink() || (directory ? !st.isDirectory() : !st.isFile()) || st.uid !== process.getuid() || (st.mode & 0o077))
throw refuse('fixture-state-unsafe', `${path} is not a private ${directory ? 'directory' : 'file'} of this user`);
}
// The recorded fixture, or null when there is none. Anything malformed refuses.
export function readFixture(dataRoot) {
const file = fixtureFile(dataRoot);
try { ownedPrivate(fixtureDir(dataRoot), true); }
catch (e) { if (e.code === 'ENOENT') return null; throw e; }
let fd;
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW); }
catch (e) { if (e.code === 'ENOENT') return null; throw refuse('fixture-state-unsafe', `${file} can't be opened (${e.code})`); }
let doc;
try {
const st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600) throw refuse('fixture-state-unsafe', `${file} is not a private file of this user`);
doc = JSON.parse(readFileSync(fd, 'utf8'));
} catch (e) { if (e instanceof CliError) throw e; throw refuse('fixture-state-invalid', `${file} is not JSON`); }
finally { closeSync(fd); }
const m = LOOPBACK.exec(doc?.endpoint ?? '');
if (!m) throw refuse('fixture-endpoint-not-loopback', `${file} names an endpoint that is not http://127.0.0.1:<port>`);
if (doc.fixtureVersion !== 1 || doc.model !== 'deterministic' || Number(m[1]) !== doc.port || !Number.isSafeInteger(doc.pid) || doc.pid < 2
|| typeof doc.instance !== 'string' || JSON.stringify(doc.entry) !== JSON.stringify(FIXTURE_ENTRY))
throw refuse('fixture-state-invalid', `${file} is not a fixture record`);
return doc;
}
const cmdline = pid => { try { return readFileSync(`/proc/${pid}/cmdline`, 'utf8').split('\0'); } catch { return null; } };
// Signal only the endpoint this command started: its argv[1] is ENDPOINT.
export const isEndpoint = pid => (cmdline(pid) ?? [])[1] === ENDPOINT;
function health(endpoint, timeoutMs = 2000) {
return new Promise(resolve => {
const req = get(`${endpoint}/health`, { timeout: timeoutMs }, res => {
let body = ''; res.setEncoding('utf8'); res.on('data', c => { if (body.length < 4096) body += c; });
res.on('end', () => { try { resolve(res.statusCode === 200 ? JSON.parse(body) : null); } catch { resolve(null); } });
});
req.on('timeout', () => req.destroy()); req.on('error', () => resolve(null));
});
}
function instanceOf(io, parsed) {
let instance;
try { instance = devOwnerInstance(io.env); }
catch (e) { throw refuse('fixture-instance-refused', `${e.message}; set MOSAIC_CONFIG to the named instance's config`); }
const named = parsed.values['--instance'];
if (named !== undefined && named !== instance) throw refuse('fixture-instance-mismatch', `--instance ${named} is not the instance MOSAIC_CONFIG names (${instance})`);
return instance;
}
function startEndpoint(logFile, { timeoutMs = 10000 } = {}) {
const log = openSync(logFile, 'a', 0o600);
let child;
try {
child = spawn(process.execPath, [ENDPOINT], { detached: true, stdio: ['ignore', 'pipe', log], env: { PATH: process.env.PATH ?? '/usr/bin:/bin' } });
} finally { closeSync(log); }
return new Promise((resolve, reject) => {
let out = '';
const fail = why => { clearTimeout(clock); try { child.kill('SIGKILL'); } catch {} reject(refuse('fixture-start-failed', why)); };
const clock = setTimeout(() => fail('the endpoint did not report ready'), timeoutMs);
child.on('error', e => fail(e.message));
child.on('exit', code => fail(`the endpoint exited ${code}`));
child.stdout.setEncoding('utf8').on('data', s => {
out += s; const nl = out.indexOf('\n'); if (nl < 0) return;
clearTimeout(clock); child.removeAllListeners('exit'); child.stdout.destroy(); child.unref();
try { const ready = JSON.parse(out.slice(0, nl)); if (ready.ready !== true || !Number.isSafeInteger(ready.port)) throw Error(); resolve({ child, port: ready.port }); }
catch { fail('the endpoint printed something that is not a ready line'); }
});
});
}
const print = (io, json, value, line) => io.stdout.write(json ? `${JSON.stringify(value, null, 2)}\n` : `${line}\n`);
async function init(args, io, deps) {
const parsed = parseArgs(args, `usage: ${DEV_USAGE}`, { flags: ['--json'], values: ['--model', '--instance', '--bind'] });
if (parsed.positional.length) throw invalid('fixture init takes no positional arguments');
if (parsed.values['--model'] !== 'deterministic') throw invalid('fixture init needs --model deterministic');
const bind = parsed.values['--bind'] ?? '127.0.0.1';
if (bind !== '127.0.0.1') throw refuse('fixture-endpoint-not-loopback', `the fixture binds 127.0.0.1 only, not ${bind}`);
const instance = instanceOf(io, parsed);
noCredentials(io.env);
const system = deps.system ?? loadSystem({ env: io.env, instance });
if (system.proxy) throw refuse('fixture-proxy-configured', `instance ${instance} has a model proxy configured; the fixture is for an instance without one`);
if (readFixture(system.dataRoot)) throw refuse('fixture-exists', `instance ${instance} already has a fixture; mosaic dev fixture stop removes it`);
try { mkdirSync(fixtureDir(system.dataRoot), { mode: 0o700 }); } catch (e) { if (e.code !== 'EEXIST') throw e; }
ownedPrivate(fixtureDir(system.dataRoot), true);
const { child, port } = await startEndpoint(join(fixtureDir(system.dataRoot), 'endpoint.log'));
const endpoint = `http://127.0.0.1:${port}`;
const record = { fixtureVersion: 1, fixture: 'fixture', model: 'deterministic', modelKey: FIXTURE_KEY_NAME, entry: FIXTURE_ENTRY,
instance, endpoint, port, pid: child.pid, principal: PRINCIPAL, upstream: 'none', startedAt: new Date().toISOString() };
try {
if (!(await health(endpoint))?.ok) throw refuse('fixture-start-failed', 'the endpoint did not answer its health check');
writeFileSync(fixtureFile(system.dataRoot), `${JSON.stringify(record, null, 2)}\n`, { mode: 0o600, flag: 'wx' });
} catch (e) { try { process.kill(child.pid, 'SIGTERM'); } catch {} throw e; }
const receipt = { receipt: 'dev-fixture-init', ...record, loopback: true, external: false, state: fixtureFile(system.dataRoot) };
print(io, parsed.flags.has('--json'), receipt,
`fixture ready: instance ${instance}, model ${FIXTURE_MODEL} (launch.models key ${FIXTURE_KEY_NAME}), endpoint ${endpoint} (loopback, no upstream), principal ${PRINCIPAL}, pid ${child.pid}`);
}
async function status(args, io, deps) {
const parsed = parseArgs(args, `usage: ${DEV_USAGE}`, { flags: ['--json'], values: ['--instance'] });
if (parsed.positional.length) throw invalid('fixture status takes no positional arguments');
const instance = instanceOf(io, parsed);
const system = deps.system ?? loadSystem({ env: io.env, instance });
const record = readFixture(system.dataRoot);
if (!record) throw new CliError(`instance ${instance} has no fixture; run mosaic dev fixture init --model deterministic`, 2);
const alive = isEndpoint(record.pid);
const answer = alive ? await health(record.endpoint) : null;
const report = { instance, ready: Boolean(alive && answer?.ok && !system.proxy), endpoint: record.endpoint, loopback: true, upstream: 'none', external: false,
model: record.model, modelKey: record.modelKey, entry: record.entry, principal: record.principal, pid: record.pid, alive,
proxyConfigured: Boolean(system.proxy), health: answer, startedAt: record.startedAt };
print(io, parsed.flags.has('--json'), report, `fixture ${report.ready ? 'ready' : 'not ready'}: ${record.endpoint}, pid ${record.pid}${alive ? '' : ' (not running)'}`);
}
async function stop(args, io, deps) {
const parsed = parseArgs(args, `usage: ${DEV_USAGE}`, { flags: ['--json'], values: ['--instance'] });
if (parsed.positional.length) throw invalid('fixture stop takes no positional arguments');
const instance = instanceOf(io, parsed);
const system = deps.system ?? loadSystem({ env: io.env, instance });
const record = readFixture(system.dataRoot);
if (!record) throw new CliError(`instance ${instance} has no fixture`, 2);
let signaled = false;
if (isEndpoint(record.pid)) {
process.kill(record.pid, 'SIGTERM'); signaled = true;
for (let i = 0; i < 50 && isEndpoint(record.pid); i++) await new Promise(r => setTimeout(r, 100));
if (isEndpoint(record.pid)) throw new CliError(`fixture endpoint pid ${record.pid} did not stop`, 1);
}
rmSync(fixtureFile(system.dataRoot));
print(io, parsed.flags.has('--json'), { receipt: 'dev-fixture-stop', instance, pid: record.pid, signaled, endpoint: record.endpoint },
`fixture stopped: instance ${instance}, pid ${record.pid}${signaled ? '' : ' (was not running)'}`);
}
export async function devCommand(rest, io, deps = {}) {
const [what, sub, ...args] = rest;
if (what !== 'fixture') throw invalid(`unknown dev command ${what ?? ''}`.trim());
if (sub === 'init') return init(args, io, deps);
if (sub === 'status') return status(args, io, deps);
if (sub === 'stop') return stop(args, io, deps);
throw invalid(`unknown dev fixture command ${sub ?? ''}`.trim());
}
// For `mosaic bus start --dev-local-owner` only: the fixture as the launcher's
// session proxy context, or nothing when the instance has no fixture. A fixture
// next to a configured proxy, or a credential in the bus environment, refuses.
// True when the organization file routes a launch to the fixture provider.
export function orgUsesFixture(doc) {
const providers = doc?.plugins?.['model-provider'];
if (Array.isArray(providers) && providers.includes('fixture')) return true;
return Object.values(doc?.launch?.models ?? {}).some(m => m?.module === 'fixture');
}
// A fixture organization's launches get no provider credential, so one instance can hold
// fixture organizations and real-model organizations side by side. Other organizations keep env.
export function fixtureLaunchEnv(doc, env) {
if (!orgUsesFixture(doc)) return env;
return Object.fromEntries(Object.entries(env).filter(([k]) => !credentialVariables({ [k]: env[k] }).length));
}
// `doc`, when given, is the organization file: an organization that does not use the fixture
// launches as configured, without the fixture proxy.
export function fixtureLauncherOptions({ system, env, instance, doc }) {
const record = readFixture(system.dataRoot);
if (!record) return {};
if (doc !== undefined && !orgUsesFixture(doc)) return {};
if (record.instance !== instance) throw refuse('fixture-instance-mismatch', `the fixture was started for instance ${record.instance}, not ${instance}`);
if (system.proxy) throw refuse('fixture-proxy-configured', 'the instance has both a model proxy and a fixture');
noCredentials(env);
return { testProxy: { baseUrl: record.endpoint, key: FIXTURE_KEY } };
}
+19
View File
@@ -0,0 +1,19 @@
import { realpathSync } from 'node:fs';
import { homedir } from 'node:os';
import { join, resolve } from 'node:path';
import { configDir } from '../../business/src/business.mjs';
// Reserved outside the HTTPS issuer namespace accepted for configured SSO providers.
export const DEV_OWNER = Object.freeze({ provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' });
export function devOwnerInstance(env = process.env) {
const refusal = '--dev-local-owner requires a named instance (not the default dev instance)';
const file = resolve(configDir(env), 'config.json');
const base = join(homedir(), '.config');
const relative = file.slice(base.length + 1);
const match = /^mosaic-([a-z][a-z0-9]{1,15})\/config\.json$/.exec(relative);
if (!env.MOSAIC_CONFIG || resolve(env.MOSAIC_CONFIG) !== file || !file.startsWith(base + '/') || !match || match[1] === 'dev') throw new Error(refusal);
// Existing symlinks may not redirect a named selection to a different instance.
try { if (realpathSync(file) !== file) throw new Error(refusal); }
catch (err) { if (err.message === refusal) throw err; if (err.code !== 'ENOENT') throw new Error(refusal); }
return match[1];
}
+8 -7
View File
@@ -76,7 +76,7 @@ const same = (a, b) => a?.pid === b?.pid && a?.startTime === b?.startTime && a?.
// tried again, like one that goes before the check. `found` runs once the
// lock is found and before it is read, `between` after the stale lock is
// read; both are for the tests. Exported for the tests.
export function claimHostLock(dataRoot, state, { found = () => {}, between = () => {} } = {}) {
export function claimHostLock(dataRoot, state, { found = () => {}, between = () => {}, refuseStale = false } = {}) {
mkdirSync(hostDir(dataRoot), { recursive: true, mode: 0o700 });
const file = hostLock(dataRoot);
const tag = `${process.pid}.${randomBytes(4).toString("hex")}`;
@@ -94,6 +94,7 @@ export function claimHostLock(dataRoot, state, { found = () => {}, between = ()
const prior = readState(file, found);
if (!prior) continue;
if (prior.live) throw new CliError(`a bus host already runs for ${prior.business} (pid ${prior.pid})`, 3);
if (refuseStale) throw new CliError(`stale bus host lock: ${file}`, 3);
between();
try {
renameSync(file, aside);
@@ -168,7 +169,7 @@ export function watchChildren(children, onDeath) {
// {binding, base?, pollMs?}; base and pollMs exist for the tests, and the
// command line never sets them, nor requestTimeoutMs. Resolves once both
// children are up.
export async function startHost({ boot, business, notifier = null, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`) }) {
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`) }) {
const dataRoot = boot.dataRoot;
// A host started before host.lock existed holds only host.json.
const prior = readHostState(dataRoot);
@@ -176,8 +177,8 @@ export async function startHost({ boot, business, notifier = null, bootTimeoutMs
// host.lock is this host's from here until it stops, and a start that
// fails gives it back. host.json comes once the children are up, so a
// reader never finds a booting host with no broker socket.
const state = { hostVersion: 1, pid: process.pid, startTime: startTimeOf(process.pid), business, startedAt: new Date().toISOString(), notifier: notifier ? notifier.binding : null };
claimHostLock(dataRoot, state);
const state = { hostVersion: 1, pid: process.pid, startTime: startTimeOf(process.pid), business, startedAt: new Date().toISOString(), notifier: notifier ? notifier.binding : null, ...(options ? { options } : {}) };
claimHostLock(dataRoot, state, { refuseStale });
// A lock this host can't read isn't its own, so it stays.
const release = () => {
let held = null;
@@ -241,7 +242,7 @@ export async function startHost({ boot, business, notifier = null, bootTimeoutMs
if (notifier) {
notify = fork(NOTIFIER, [], { stdio: ["ignore", "inherit", "inherit", "ipc"] });
notify.on("error", onChildError("notifier"));
notify.send({ op: "start", path: ready.path, cap: reader.cap, business, dataRoot, binding: notifier.binding, base: notifier.base, pollMs: notifier.pollMs });
notify.send({ op: "start", path: ready.path, cap: reader.cap, business, dataRoot, instanceRoot: boot.instanceRoot ?? dataRoot, binding: notifier.binding, base: notifier.base, pollMs: notifier.pollMs });
const ok = await firstReply(notify, START_TIMEOUT_MS, "notifier");
if (ok?.ok !== true) throw new CliError(`notifier refused to start: ${typeof ok?.error === "string" ? ok.error : "notifier-refused"}`, 3);
}
@@ -368,12 +369,12 @@ export async function startHost({ boot, business, notifier = null, bootTimeoutMs
// `mosaic bus stop`: SIGTERM to the recorded host, after checking that the
// pid still runs with the recorded start time and is a `bus start` process.
export async function stopHost(dataRoot, { timeoutMs = 60000 } = {}) {
export async function stopHost(dataRoot, { timeoutMs = 60000, expectedBusiness = null } = {}) {
const state = readHostState(dataRoot);
if (!state || !state.live) return { stopped: false, reason: state ? "stale state file; no host runs" : "no host runs" };
const argv = cmdlineOf(state.pid) ?? [];
const i = argv.findIndex((a) => a.endsWith("/packages/cli/src/cli.mjs"));
if (i < 0 || argv[i + 1] !== "bus" || argv[i + 2] !== "start") {
if (i < 0 || argv[i + 1] !== "bus" || argv[i + 2] !== "start" || (expectedBusiness !== null && (state.business !== expectedBusiness || argv[i + 3] !== expectedBusiness))) {
throw new CliError(`pid ${state.pid} is not a bus host; refusing to signal it`, 3);
}
process.kill(state.pid, "SIGTERM");
+57 -14
View File
@@ -144,6 +144,9 @@ import { watchModelOutput } from './model-stall.mjs';
import { chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join, relative } from "node:path";
import { configDir, loadBusiness, resolveInstance, systemVars } from "../../business/src/index.mjs";
import { organizationWriter } from "../../business/src/organization.mjs";
import { businessFilePath, validateBusinessDocument } from "../../business/src/business.mjs";
import { selectModelProvider } from "../../plugins/src/model-provider-selection.mjs";
import { THREAD_DEFINITIONS } from "../../business/src/vocabulary.mjs";
import { limitHit } from "../../business/src/launch.mjs";
import { BundleError, buildBundle, hasFallback, readSkills, sessionModel } from "../../harness/src/bundle.mjs";
@@ -222,9 +225,10 @@ export function createLauncher({
host,
system,
env = process.env,
// Tests only (#774 F4): a static session proxy context {baseUrl,key}, or a
// factory giving one per check, for a provider "proxy" route while
// system.proxy is unset. It skips P2 admission, so createLauncher refuses it
// Tests (#774 F4), and `bus start --dev-local-owner` on a named instance with
// a `mosaic dev fixture` (dev-fixture.mjs): a static session proxy context
// {baseUrl,key}, or a factory giving one per check, for a provider "proxy"
// route while system.proxy is unset. It skips P2 admission, so createLauncher refuses it
// together with system.proxy. P3 never reads a key store or calls the proxy.
testProxy = null,
tracker = false,
@@ -252,7 +256,7 @@ export function createLauncher({
}
}
// Row 64 (#1557): where a role's skills are read from; scripts/skill.sh activate fills it.
const skillsDir = join(dataRoot, "skills-enabled");
const skillsDir = join(system.instanceRoot ?? dataRoot, "skills-enabled");
let b = loadBusiness(host.business, { dir: configDir(env), rolesDir: rolesDir(env) });
const vars = systemVars(system);
@@ -347,7 +351,12 @@ export function createLauncher({
// limits the chief, not the human. launch.by still takes the `pm` branch.
async function check(instance, { pm = false, thread = null, session = null, human = false, proxyContext = null, proxyReady = false } = {}) {
const admissionGeneration = admission().generation;
try { b=loadBusiness(host.business,{dir:configDir(env),rolesDir:rolesDir(env)}); } catch(e){throw new Refusal("organization-refused",e.message);}
let businessRevision;
try {
const snapshot = organizationWriter({dir:configDir(env),rolesDir:rolesDir(env)}).read(host.business);
businessRevision = snapshot.revision;
b = validateBusinessDocument(snapshot.document, businessFilePath(host.business, configDir(env)), {rolesDir:rolesDir(env)});
} catch(e){throw new Refusal("organization-refused",e.message);}
if (!b.launch) throw new Refusal("launch-not-configured", `business ${b.id} has no launch block`);
if (!(pm ? instance === b.launch.by : b.launch.instances.includes(instance) || (human && Object.hasOwn(b.roles, instance)))) throw new Refusal("instance-not-listed");
const threaded = THREAD_DEFINITIONS.includes(b.roles[instance]?.definition);
@@ -365,12 +374,27 @@ export function createLauncher({
} catch (e) {
throw new Refusal("unsupported-harness", e.message);
}
// Resolve the business's current provider modules with a fresh loader; a refused
// load never inherits a previous business's authority.
const providerNames = b.plugins?.['model-provider'] ?? [];
const providerModules = new Map();
if (providerNames.length) {
const loader = makeLoader({ log: entry => record({ ...entry, instance }) });
const loaded = await loader.load({ 'model-provider': providerNames });
if (loaded.refused.length) throw new Refusal('model-provider-unavailable', 'model provider module refused');
for (const { name, module } of loader.snapshot('model-provider')) providerModules.set(name, module);
}
const resolver = providerModules.size ? ({ selection, harness }) => {
const selected = selectModelProvider([...providerModules].map(([name,module]) => ({name,module})), selection);
if (!selected) return { code: 'model-provider-unavailable' };
return selected.module.route({ selection, harness });
} : undefined;
// Row 65 (#1558): the route's harness, model and thinking come from the instance's vars only.
const routeOf = (which) => {
try {
return sessionModel(resolved, b.launch.models, which, { proxy: Boolean(system.proxy || proxyContext) });
return sessionModel(resolved, b.launch.models, which, { proxy: Boolean(system.proxy || proxyContext), resolver });
} catch (e) {
const code = e instanceof BundleError && (e.code === "no-route" || e.code.startsWith("proxy-")) ? e.code : "unsupported-harness";
const code = e instanceof BundleError && (e.code === "no-route" || e.code.startsWith("proxy-") || e.code.startsWith("model-")) ? e.code : "unsupported-harness";
throw new Refusal(code, e.message);
}
};
@@ -454,11 +478,23 @@ export function createLauncher({
// Row 333: under a daily key quota the bus refuses key-quota-no-model-key for an agent P2
// hasn't issued a key yet. Only the first check of a proxy launch lets that one answer pass:
// startLocked's modelPreflight issues the key, and the final check asks again with it.
if (early && may?.allow === false && may.check === null && may.code === "key-quota-no-model-key") return account;
if (may?.allow !== true) {
const deferredKey = early && may?.allow === false && may.check === null && may.code === "key-quota-no-model-key";
if (may?.allow !== true && !deferredKey) {
if (may?.hit) { const error = new Refusal("limit-reached", JSON.stringify(may.hit)); error.limit = { check: may.check, ...may.hit }; throw error; }
throw new Refusal("limit-check-failed", `${may?.check ?? "bus"}: ${may?.code ?? "malformed-answer"}`);
}
const selected = b.launch.models[model.modelKey];
if (selected?.module || (proxied && providerModules.size)) {
let checked;
try { checked = await host.op({ op: 'modelAdmission', business: b.id, instance, modelKey: model.modelKey, harness: model.harness,
revision: businessRevision, entry: selected, provider: model.provider, model: model.model, account, route, transport: model.proxyTransport }); }
catch { throw new Refusal('model-catalogue-unavailable', 'model catalogue admission unavailable'); }
if (checked?.allow !== true) {
const code = ['model-provider-unavailable', 'model-catalogue-unavailable', 'model-harness-incompatible', 'proxy-uncertain'].includes(checked?.code) ? checked.code : 'model-provider-unavailable';
throw new Refusal(code, `model ${selected?.module ?? 'legacy'}/${selected?.upstream ?? selected?.provider}/${selected?.model} unavailable (${code})`);
}
if (!proxied) throw new Refusal('model-provider-unavailable', 'module-backed model requires a proxy');
}
return account;
};
const proxyFor = async (selected) => {
@@ -503,12 +539,12 @@ export function createLauncher({
await pickRoute(() => standIn);
await gate(model, harness, null);
admission(admissionGeneration);
return { resolved, model, account: null, harness, route, routeReason, admissionGeneration };
return { resolved, model, account: null, harness, route, routeReason, admissionGeneration, resolver };
}
const sessionProxy = await pickRoute(proxyFor);
const account = await gate(model, harness, sessionProxy);
admission(admissionGeneration);
return { resolved, model, account, harness, route, routeReason, admissionGeneration, sessionProxy };
return { resolved, model, account, harness, route, routeReason, admissionGeneration, sessionProxy, resolver };
}
// Reread host state on every new-run path, including opens/clears queued before archive.
@@ -610,8 +646,8 @@ export function createLauncher({
catch (error) { throw new Refusal(error.code ?? 'proxy-down', error.code ?? 'proxy-down'); }
if (!admittedProxy || typeof admittedProxy.key !== 'string' || !admittedProxy.key || admittedProxy.baseUrl !== `http://127.0.0.1:${system.proxy.port}`) throw new Refusal('proxy-down', 'proxy-down');
}
let harness, route, routeReason, sessionProxy;
({resolved,model,account,harness,route,routeReason,sessionProxy}=await check(instance,{pm:instance===b.launch.by,thread,session,human,proxyContext:admittedProxy,proxyReady:true}));
let harness, route, routeReason, sessionProxy, resolver;
({resolved,model,account,harness,route,routeReason,sessionProxy,resolver}=await check(instance,{pm:instance===b.launch.by,thread,session,human,proxyContext:admittedProxy,proxyReady:true}));
admission(admissionGeneration);
const { run, runDir } = newRun(dataRoot, b.id);
let modelKeyId;
@@ -640,6 +676,7 @@ export function createLauncher({
resolved,
models: b.launch.models,
proxy: sessionProxy,
resolver,
contract: readFileSync(resolved.contract, "utf8"),
arbiter: Object.values(b.arbiters).includes(instance),
tracker,
@@ -657,6 +694,8 @@ export function createLauncher({
const { manifest, ...files } = bundle.files;
const adapter = call(model.harness, "adapter", () => harness.adapter());
const turnEnd = call(model.harness, "turnEnd", () => harness.turnEnd());
const outputPolicy = call(model.harness, "outputPolicy", () => harness.outputPolicy({ proxy: Boolean(sessionProxy) }));
if (!["adapter", "non-newline-stdout", "stdout"].includes(outputPolicy)) throw new Refusal("harness-refused", "invalid harness output policy");
const transcript = call(model.harness, "transcript", () => harness.transcript({ runDir, store }));
const harnessMounts = call(model.harness, "mounts", () => harness.mounts({ env }));
// Row 93: the git socket, for a role that pushes work or reviews it. Row 137: or lands it.
@@ -688,6 +727,8 @@ export function createLauncher({
bundle: files,
harnessEnv,
turnEnd,
outputPolicy,
outputPolicyVersion: 1,
toolSocket,
turnMarker,
...(holding?.length ? { landing: holding.map(({ decision, task_ref }) => ({ decision, task_ref })) } : {}),
@@ -778,7 +819,9 @@ export function createLauncher({
const reason = haltGeneration ? 'kill-switch' : endReason(code, signal);
try {
const ended = await endRun(run, reason, signal ? null : code);
registry.remove(b.id, run);
// Keep the exact launch identity for the next host if the broker
// refused or did not confirm the end.
if (typeof ended?.released === "boolean") registry.remove(b.id, run);
// Row 125 (#1619, REQ-LIMIT-4): a run a limit check stopped names the limit, from the claim row.
record({ event: "end", instance, ...(thread ? { thread, session } : {}), run, reason, exitCode: code, signal, ...(haltGeneration ? { haltGeneration } : {}), ...(ended?.limit ? { limit: ended.limit } : {}) });
log(`run ${run} (${instance}) ended: ${reason}`);
+24 -1
View File
@@ -1,3 +1,7 @@
import { join } from 'node:path';
import { configDir } from '../../business/src/business.mjs';
import { createProxyLock } from '../../bus/src/proxy-lock.mjs';
import { loadManagementClient } from '../../model-proxy/src/client.mjs';
import { CliError } from './errors.mjs';
import { refuseInsideAgent } from './transport.mjs';
import { fetchPinned } from '../../model-proxy/src/fetch.mjs';
@@ -32,9 +36,28 @@ export function promptProxyKey(io, label) {
export async function proxyCommand(args, io, { system, callFor, prompt = promptProxyKey, fetch = fetchPinned }) {
const [verb, ...rest] = args;
if (args.includes('--tester')) throw failure('human-required', 3);
if (verb === 'init' || verb === 'fetch') {
if (verb === 'init' || verb === 'fetch' || verb === 'fence') {
try { refuseInsideAgent(io.env); } catch { throw failure('human-required', 3); }
}
if (verb === 'fence') {
const [action, ...flags] = rest;
if (action === 'clear' && flags.length === 0) throw failure('proxy-restart-required');
if (!(action === 'status' && (flags.length === 0 || (flags.length === 1 && flags[0] === '--json')))
&& !(action === 'clear' && flags.length === 1 && flags[0] === '--proxy-restarted')) throw failure('proxy-usage', 4);
try {
const config = typeof system === 'function' ? system() : system;
if (!config.proxy) throw failure('proxy-config-invalid');
const dir = configDir(io.env);
const lock = createProxyLock({ stateDir: join(config.dataRoot, 'proxy'), identity: `${dir}:${config.proxy.port}` });
let result;
if (action === 'status') result = { uncertain: lock.status() };
else {
const client = await loadManagementClient({ configDir: dir, proxy: config.proxy });
result = await lock.clear({ restarted: true, read: () => client.getKeys() });
}
io.stdout.write(JSON.stringify(result) + '\n'); return;
} catch (error) { if (error instanceof CliError) throw error; throw failure(/^proxy-[a-z-]+$/.test(error.code ?? '') ? error.code : 'proxy-down'); }
}
if (!['fetch', 'init'].includes(verb)) throw failure('proxy-usage', 4);
const parsed = { flags: new Set(), values: {} }; let port;
for (let i = 0; i < rest.length; i++) {
+1 -1
View File
@@ -39,7 +39,7 @@ if (!process.send) {
try {
const client = new Client({ path: m.path, cap: m.cap });
const processing = { work: fn => withProcessing(m.dataRoot, fn), guard: () => currentProcessing()?.guard() };
const direct = openDirect({ dataRoot: m.dataRoot, name: m.binding, ...(m.base ? { base: m.base } : {}),
const direct = openDirect({ dataRoot: m.instanceRoot ?? m.dataRoot, name: m.binding, ...(m.base ? { base: m.base } : {}),
fetch: (url, init) => {
const scope = currentProcessing(); scope?.guard();
return fetch(url, { ...init, ...(scope ? { signal: scope.signal } : {}) });
+33
View File
@@ -0,0 +1,33 @@
import { lstatSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { BINDING_NAME } from "../../discord/src/binding.mjs";
import { CliError } from "./errors.mjs";
// The notifier's private config: `<dataRoot>/notify/<business>/notify.json`,
// 0600, `{"notifyVersion": 1, "binding": "<discord binding>" | null}`.
// Missing refuses, so a host never starts without someone deciding whether
// it DMs; null runs the host without a notifier.
export function readNotifyConfig(dataRoot, business) {
const file = join(dataRoot, "notify", business, "notify.json");
let st;
try {
st = lstatSync(file);
} catch {
throw new CliError(`no notifier config at ${file}; write {"notifyVersion": 1, "binding": "<discord binding>"} there, mode 0600, or "binding": null to run without DMs`, 3);
}
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600) {
throw new CliError(`notifier config must be a regular file, mode 0600, owned by this user: ${file}`, 3);
}
let doc;
try {
doc = JSON.parse(readFileSync(file, "utf8"));
} catch {
throw new CliError(`notifier config is not JSON: ${file}`, 3);
}
const keys = doc && typeof doc === "object" && !Array.isArray(doc) ? Object.keys(doc).sort().join(",") : "";
if (keys !== "binding,notifyVersion" || doc.notifyVersion !== 1 || (doc.binding !== null && !(typeof doc.binding === "string" && BINDING_NAME.test(doc.binding)))) {
throw new CliError(`notifier config must be exactly {"notifyVersion": 1, "binding": <binding name> | null}: ${file}`, 3);
}
return doc.binding;
}
+204
View File
@@ -0,0 +1,204 @@
// Organization-scoped bus lifecycle. Registry resolution precedes every action.
import { randomBytes } from 'node:crypto';
import { processState } from '../../seat/src/process-state.mjs';
import { spawn } from 'node:child_process';
import { constants, closeSync, lstatSync, mkdirSync, openSync, readFileSync, fstatSync, unlinkSync, writeFileSync } from 'node:fs';
import { connect } from 'node:net';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolveOrg, withOrgLifecycle } from '../../business/src/org-registry.mjs';
import { orgPaths } from './config.mjs';
import { cmdlineOf, hostFile, hostLock, hostStatus, readHostState, startTimeOf, stopHost } from './host.mjs';
import { CliError } from './errors.mjs';
import { readBootStatus } from './boot-status.mjs';
const CLI = fileURLToPath(new URL('./cli.mjs', import.meta.url));
const fail = message => { throw new CliError(message, 3); };
async function target(ctx, id, allowDeleting = false) {
let org;
try { org = await resolveOrg(ctx, id); }
catch (error) {
if (['ENOENT', 'EACCES', 'ENOTDIR'].includes(error.code)) fail('organization registry is inaccessible');
throw error;
}
if (org.state === 'archived' && !allowDeleting) fail(`org-archived: organization ${id} is archived`);
if (org.state === 'deleting' && !allowDeleting) fail(`organization ${id} is being deleted`);
if (org.state !== 'active' && !(allowDeleting && ['deleting', 'archived'].includes(org.state))) fail(`organization ${id} is not active`);
return { ...org, paths: orgPaths(org.root) };
}
function checkedState(org) {
const s = hostStatus(org.root);
let lock = null;
try { lock = JSON.parse(readFileSync(hostLock(org.root), 'utf8')); }
catch (e) { if (e.code !== 'ENOENT') fail('bus host lock is unreadable'); }
if (lock && (!Number.isSafeInteger(lock.pid) || typeof lock.startTime !== 'string' || startTimeOf(lock.pid) !== lock.startTime)) fail('stale bus host lock');
if (s.host && (!s.host.live || s.host.business !== org.id)) fail('stale or mismatched bus host state');
if (s.host) {
const args = cmdlineOf(s.host.pid) ?? [], i = args.indexOf(CLI);
if (i < 0 || args[i + 1] !== 'bus' || args[i + 2] !== 'start' || args[i + 3] !== org.id) fail('bus host identity mismatch');
}
if (lock && (!s.host || lock.pid !== s.host.pid || lock.business !== org.id)) fail('bus host lock does not match host state');
if (s.writerLock && !s.writerLock.live) fail('stale bus writer lock');
if (!s.host && s.writerLock) fail('bus writer lock has no matching host');
if (s.host && !lock) fail('bus host lock is missing');
return s;
}
function answer(path) {
return new Promise(resolve => {
let done = false, data = '';
const socket = connect(path);
const finish = value => { if (done) return; done = true; socket.destroy(); resolve(value); };
socket.setTimeout(1000, () => finish(false));
socket.on('error', () => finish(false));
socket.on('connect', () => socket.write('{}\n'));
socket.on('data', chunk => {
data += chunk;
if (data.length > 4096) return finish(false);
if (data.includes('\n')) {
try { const r = JSON.parse(data.split('\n')[0]); finish(r.ok === false && r.error === 'invalid-envelope'); }
catch { finish(false); }
}
});
socket.on('end', () => finish(false));
});
}
export async function busStatus(ctx, id) {
const org = await target(ctx, id), state = checkedState(org);
const healthy = Boolean(state.host?.live && state.socket && await answer(org.paths.socket));
return { org: id, instance: ctx.instance, root: org.root, socket: org.paths.socket, healthy, pid: state.host?.pid ?? null, boot: readBootStatus(org.root) };
}
export async function busStop(ctx, id) {
const org = await target(ctx, id, true);
checkedState(org);
return stopHost(org.root, { expectedBusiness: id });
}
// Deletion refuses stale or ambiguous host state and any listening socket.
export async function busStopForDelete(ctx, id) {
const org = await target(ctx, id, true);
for (const [part, directory] of [['bus-host', true], ['bus-host/host.json', false], ['bus-host/host.lock', false], ['bus', true], ['bus/writer.lock', false], ['bus/broker.sock', false]]) {
try {
const st = lstatSync(join(org.root, part));
if (st.isSymbolicLink()) fail('symlink in bus state; inspect it before resuming deletion');
if (directory ? !st.isDirectory() : part === 'bus/broker.sock' ? !st.isSocket() : !st.isFile()) fail('unsafe bus state type');
} catch (e) { if (e.code !== 'ENOENT') throw e; }
}
checkedState(org);
const stopped = await stopHost(org.root, { expectedBusiness: id });
const state = checkedState(org);
if (state.host || state.writerLock) fail('bus stop is uncertain; inspect the host before resuming deletion');
const listening = await new Promise(resolve => {
const socket = connect(org.paths.socket); let done = false;
const finish = value => { if (done) return; done = true; socket.destroy(); resolve(value); };
socket.setTimeout(1000, () => finish(true));
socket.on('connect', () => finish(true)); socket.on('error', e => finish(!['ENOENT', 'ECONNREFUSED'].includes(e.code)));
});
if (listening) fail('bus socket still answers; inspect the host before resuming deletion');
return stopped;
}
export async function busStart(ctx, id, { detach = false, devLocalOwner = false, pm = false, noLaunch = false } = {}) {
if (pm && noLaunch) fail('bus start takes --pm or --no-launch, not both');
// Protect parent-side directory preparation; the child takes the same fence
// for admission and boot, so the parent must release it before spawning.
const { org, fd } = await withOrgLifecycle(ctx, id, async () => {
const org = await target(ctx, id);
checkedState(org);
if (readHostState(org.root)?.live) fail(`bus already runs for ${id}`);
mkdirSync(org.paths.hostDir, { recursive: true, mode: 0o700 });
const st = lstatSync(org.paths.hostDir);
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o700) fail('unsafe bus host directory');
const fd = openSync(join(org.paths.hostDir, 'host.log'), constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW, 0o600);
return { org, fd };
});
let child;
try {
child = spawn(process.execPath, [CLI, 'bus', 'start', id, '--internal-host', ...(devLocalOwner ? ['--dev-local-owner'] : []), ...(pm ? ['--pm'] : []), ...(noLaunch ? ['--no-launch'] : [])], {
env: ctx.env ?? process.env, detached: detach, stdio: ['ignore', fd, fd, 'ipc'],
});
} finally { closeSync(fd); }
const done = new Promise(resolve => { child.once('error', () => resolve(1)); child.once('exit', code => resolve(code ?? 1)); });
let exited = false, ready = false;
child.on('message', message => { if (message?.orgReady === id) ready = true; });
done.then(() => { exited = true; });
const deadline = Date.now() + 30000;
while (Date.now() < deadline && !exited) {
// During boot the host holds its lock before its state is published.
const state = readHostState(org.root);
if (ready && state?.live && state.pid === child.pid && state.business === id && await answer(org.paths.socket)) {
child.disconnect();
if (detach) child.unref();
const receipt = { org: id, pid: child.pid, socket: org.paths.socket, root: org.root };
Object.defineProperty(receipt, 'done', { value: done });
return receipt;
}
await new Promise(r => setTimeout(r, 50));
}
if (!exited) { child.kill('SIGTERM'); await done; }
throw new CliError(`bus failed to start; see ${join(org.paths.hostDir, 'host.log')}`, 3);
}
export async function busRestart(ctx, id) {
const org = await target(ctx, id);
checkedState(org);
const state = readHostState(org.root);
const options = state?.options ?? {};
await busStop(ctx, id);
return busStart(ctx, id, { detach: true, ...options });
}
// Recover only while the org lifecycle fence excludes new starts and deletion.
export async function busRecover(ctx, id) {
return withOrgLifecycle(ctx, id, async () => {
const org = await target(ctx, id, true);
const inspect = (path, socket = false) => {
let st; try { st = lstatSync(path); } catch (e) { if (e.code === 'ENOENT') return null; throw e; }
if (st.isSymbolicLink() || st.uid !== process.getuid() || (socket ? !st.isSocket() : !st.isFile() || (st.mode & 0o777) !== 0o600 || st.nlink !== 1)) fail('unsafe bus recovery state');
if (socket) return { path, st };
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
try {
const actual = fstatSync(fd);
if (actual.ino !== st.ino || actual.dev !== st.dev) fail('bus recovery state changed');
let value; try { value = JSON.parse(readFileSync(fd, 'utf8')); } catch { fail('unreadable bus recovery state'); }
return { path, st, value };
} finally { closeSync(fd); }
};
for (const dir of [org.paths.hostDir, join(org.root, 'bus')]) {
let st; try { st = lstatSync(dir); } catch (e) { if (e.code === 'ENOENT') continue; throw e; }
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o077)) fail('unsafe bus recovery directory');
}
const state = inspect(hostFile(org.root)), lock = inspect(hostLock(org.root));
const writer = inspect(join(org.root, 'bus', 'writer.lock')), socket = inspect(org.paths.socket, true);
if (!state) fail('bus host state is missing');
const h = state.value;
if (!Number.isSafeInteger(h?.pid) || h.pid <= 0 || typeof h.startTime !== 'string' || !/^\d+$/.test(h.startTime) || typeof h.startedAt !== 'string' || !Number.isFinite(Date.parse(h.startedAt)) || h.business !== id) fail('invalid or mismatched bus host state');
let p; try { p = processState(h.pid); } catch { fail('bus host identity is uncertain'); }
if (p.alive && p.startTime === h.startTime) {
const argv = cmdlineOf(h.pid); if (!argv || !argv.length) fail('bus host identity is uncertain');
const i = argv.indexOf(CLI);
if (i >= 0 && argv[i + 1] === 'bus' && argv[i + 2] === 'start' && argv[i + 3] === id) fail(`bus host pid ${h.pid} is alive`);
}
if (lock && (lock.value?.pid !== h.pid || lock.value?.startTime !== h.startTime || lock.value?.business !== id || lock.value?.startedAt !== h.startedAt)) fail('bus host lock does not match host state');
if (writer) {
let w; try { w = processState(writer.value?.pid); } catch { fail('bus writer identity is uncertain'); }
if (w.alive) fail(`bus writer pid ${writer.value.pid} is alive`);
}
if (socket) {
const occupied = await new Promise(resolve => {
const client = connect(org.paths.socket); let done = false;
const finish = value => { if (!done) { done = true; client.destroy(); resolve(value); } };
client.setTimeout(1000, () => finish(true));
client.on('connect', () => finish(true));
client.on('error', e => finish(!['ENOENT', 'ECONNREFUSED'].includes(e.code)));
});
if (occupied) fail('bus socket answers or is uncertain');
}
const entries = [state, lock, writer, socket].filter(Boolean);
for (const entry of entries) {
const current = lstatSync(entry.path);
if (current.dev !== entry.st.dev || current.ino !== entry.st.ino) fail('bus recovery state changed');
}
const at = new Date().toISOString();
const receipt = join(org.paths.hostDir, `recovery-${at.replace(/[:.]/g, '-')}-${randomBytes(4).toString('hex')}.json`);
writeFileSync(receipt, JSON.stringify({ action: 'bus.recover', at, org: id, stopped: true, host: h, lock: lock?.value ?? null, writer: writer?.value ?? null, socket: Boolean(socket) }, null, 2) + '\n', { mode: 0o600, flag: 'wx' });
for (const entry of entries.slice().reverse()) unlinkSync(entry.path);
return { org: id, recovered: true, pid: h.pid, receipt };
});
}
+123
View File
@@ -0,0 +1,123 @@
// Instance-wide Console process; organization routing is owned by S5.
import { randomBytes } from 'node:crypto';
import { processState } from '../../seat/src/process-state.mjs';
import { spawn } from 'node:child_process';
import { constants, closeSync, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, renameSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { listOrgs, resolveOrg } from '../../business/src/org-registry.mjs';
import { cmdlineOf, startTimeOf } from './host.mjs';
import { CliError } from './errors.mjs';
const WEBUI = fileURLToPath(new URL('../../webui/src/cli.mjs', import.meta.url));
const forbidden = new Set([7332, 7340, 7341, 33223, 17450, 17443, 44307]);
const fail = message => { throw new CliError(message, 3); };
const directory = ctx => join(ctx.dataRoot, 'console');
const stateFile = ctx => join(directory(ctx), 'console.json');
function state(ctx) {
let fd;
try { fd = openSync(stateFile(ctx), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
catch (e) { if (e.code === 'ENOENT') return null; fail('console state is unreadable'); }
let data;
try {
const st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600 || st.nlink !== 1) fail('unsafe console state');
try { data = JSON.parse(readFileSync(fd, 'utf8')); } catch { fail('console state is unreadable'); }
} finally { closeSync(fd); }
if (!Number.isSafeInteger(data?.pid) || data.pid <= 0 || typeof data.startTime !== 'string' || !data.startTime || typeof data.url !== 'string') fail('console state is invalid');
return data;
}
function checkDir(ctx) {
mkdirSync(directory(ctx), { recursive: true, mode: 0o700 });
const st = lstatSync(directory(ctx));
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o700) fail('unsafe console directory');
}
function liveConsole(s) {
let p;
try { p = processState(s.pid); } catch { fail('console process identity is uncertain'); }
return p.alive && p.startTime === s.startTime;
}
function removeStale(ctx, s, warn) {
if (liveConsole(s)) return false;
const file = stateFile(ctx), st = lstatSync(file);
const current = state(ctx);
if (!current || current.pid !== s.pid || current.startTime !== s.startTime || current.url !== s.url) fail('console state changed during recovery');
if (!st.isFile() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o777) !== 0o600) fail('unsafe console state');
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
const aside = `${file}.stale-${stamp}-${randomBytes(4).toString('hex')}`;
const currentStat = lstatSync(file);
if (currentStat.dev !== st.dev || currentStat.ino !== st.ino) fail('console state changed during recovery');
renameSync(file, aside);
warn(`mosaic: stale console state moved to ${aside}\n`);
return true;
}
function takeLock(ctx) {
checkDir(ctx);
const path = join(directory(ctx), 'start.lock');
let fd; try { fd = openSync(path, 'wx', 0o600); } catch { fail('console start lock exists'); }
return () => { closeSync(fd); rmSync(path, { force: true }); };
}
export async function consoleStart(ctx, { devLocalOwner = false, detach = false, port = 'auto', warn = s => process.stderr.write(s) } = {}) {
const value = port === 'auto' ? 0 : Number(port);
if (!(port === 'auto' || (typeof port === 'string' && /^\d+$/.test(port) && value >= 1 && value <= 65535 && !forbidden.has(value)))) fail('invalid or forbidden console port');
const release = takeLock(ctx);
let child = null, done = null;
try {
const prior = state(ctx);
if (prior && !removeStale(ctx, prior, warn)) fail('console state exists; stop or inspect the console first');
const org = (await listOrgs(ctx)).find(x => x.state === 'active');
if (org) await resolveOrg(ctx, org.id);
// The pre-S5 Console still accepts a business preselection.
const args = [WEBUI, 'serve', '--port', String(value), ...(org ? ['--business', org.id] : []), ...(devLocalOwner ? ['--dev-local-owner'] : [])];
const log = join(directory(ctx), `console-${process.pid}-${Date.now()}.log`);
const fd = openSync(log, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
try { child = spawn(process.execPath, args, { env: ctx.env ?? process.env, detached: detach, stdio: ['ignore', fd, fd] }); }
finally { closeSync(fd); }
done = new Promise(resolve => { child.once('error', () => resolve(1)); child.once('exit', code => resolve(code ?? 1)); });
let exited = false; done.then(() => { exited = true; });
const deadline = Date.now() + 30000;
while (Date.now() < deadline && !exited) {
const text = readFileSync(log, 'utf8');
const match = /Mosaic Console: (http:\/\/127\.0\.0\.1:(\d+)\/)/.exec(text);
if (match) {
if (forbidden.has(Number(match[2]))) fail('console selected a forbidden port');
const startTime = startTimeOf(child.pid);
if (!startTime) fail('console exited during startup');
writeFileSync(stateFile(ctx), JSON.stringify({ pid: child.pid, startTime, url: match[1] }) + '\n', { mode: 0o600, flag: 'wx' });
if (detach) child.unref();
const result = { url: match[1], pid: child.pid };
Object.defineProperty(result, 'done', { value: done });
return result;
}
await new Promise(r => setTimeout(r, 50));
}
fail(`console failed to start; see ${log}`);
} catch (e) {
if (child && child.exitCode === null && child.signalCode === null) { child.kill('SIGTERM'); await done; }
throw e;
} finally { release(); }
}
export async function consoleStop(ctx, { warn = s => process.stderr.write(s) } = {}) {
const release = takeLock(ctx);
try {
const s = state(ctx);
if (!s || removeStale(ctx, s, warn)) return { stopped: false };
if (!liveConsole(s)) fail('console process changed before stop');
const argv = cmdlineOf(s.pid);
if (!argv || !argv.length) fail('console process identity is uncertain');
const i = argv.indexOf(WEBUI);
if (i < 0 || argv[i + 1] !== 'serve') {
const serve = argv.indexOf('serve');
const entry = serve > 0 ? ` ${argv[serve - 1]}` : '';
fail(`console was started from another path${entry}; stop it there`);
}
process.kill(s.pid, 'SIGTERM');
const deadline = Date.now() + 20000;
while (Date.now() < deadline) {
let p; try { p = processState(s.pid); } catch { fail('console process identity is uncertain'); }
if (!p.alive || p.startTime !== s.startTime) { rmSync(stateFile(ctx)); return { stopped: true, pid: s.pid }; }
await new Promise(r => setTimeout(r, 100));
}
fail('console did not stop');
} finally { release(); }
}
+35
View File
@@ -0,0 +1,35 @@
import { readFile, writeFile } from 'node:fs/promises';
import { configDir } from '../../business/src/business.mjs';
import { listOrgs, orgLock } from '../../business/src/org-registry.mjs';
import { templateRender, validateOrg, createOrg, showOrg, updateOrg, queueInit, queueShow, projectRepoInit, projectRepoShow, deleteOrg, archiveOrg, restoreOrg } 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';
import { busStopForDelete } from './org-bus.mjs';
export const ORG_USAGE = 'mosaic org lock show|clear ID | archive ID --if-revision N [--json] | restore ID --if-revision N [--json] | delete ID --confirm ID [--if-revision N] [--json] | 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', '--confirm'].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] === 'lock' && ['show', 'clear'].includes(words[1]) && words.length === 3) { allowed([], []); result = await orgLock(ctx, words[2], { clear: words[1] === 'clear' }); }
else 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 (['archive', 'restore'].includes(words[0]) && words.length === 2) { allowed(['--json'], ['--if-revision']); const r = need('--if-revision'); if (!/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid revision', 4); const opts = { ifRevision: Number(r) }; result = words[0] === 'archive' ? await archiveOrg(ctx, words[1], { ...opts, busStop: busStopForDelete }) : await restoreOrg(ctx, words[1], opts); }
else if (words[0] === 'delete' && words.length === 2) { allowed(['--json'], ['--confirm', '--if-revision']); const r = p.values['--if-revision']; if (r !== undefined && !/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid revision', 4); result = await deleteOrg(ctx, words[1], { busStop: busStopForDelete, confirm: need('--confirm'), ifRevision: r === undefined ? undefined : 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' || entry.state === 'archived') accepted.push(await showOrg(ctx, entry.id, { includeArchived: true })); 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); }
}
+192
View File
@@ -0,0 +1,192 @@
// `mosaic run evidence ORG --thread REF [--verify] [--json]`: the run records a
// thread's launches left behind, read-only (r4 §7). A bus launch writes no
// result.json or snapshot, so the evidence is what it does write once:
// result -> <run>/session.json (wx 0600) plus the launch and end records in
// the organization's launch log
// snapshot -> <run>/bundle/manifest.json (wx 0600), every `files` digest recomputed
// stderr -> <run>/turns/NNNN.stderr (wx 0600), hashed
// A launch has no id of its own; the launch log line (file, line, sha256) names it.
// Exit: 0 found (and verified with --verify), 1 a check failed, 2 nothing found or
// invalid, 3 refused, 4 usage.
import { createHash } from 'node:crypto';
import { closeSync, constants, fstatSync, lstatSync, openSync, readFileSync, readdirSync } from 'node:fs';
import { dirname, join, relative, resolve, sep } from 'node:path';
import { configDir } from '../../business/src/business.mjs';
import { resolveOrg, OrgError } from '../../business/src/org-registry.mjs';
import { launchLogFile, runsDir } from '../../seat/src/session.mjs';
import * as cliConfig from './config.mjs';
import { DEV_OWNER, devOwnerInstance } from './dev-local-owner.mjs';
import { CliError } from './errors.mjs';
import { parseArgs, resolveThread } from './thread.mjs';
export const RUN_USAGE = 'mosaic run evidence ORG --thread REF [--verify] [--json] [--tester]';
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const RUN_ID = /^r[0-9a-f]{12}$/;
const STDERR = /^[0-9]{4}\.stderr$/;
const sha256 = buf => createHash('sha256').update(buf).digest('hex');
const usage = why => new CliError(`${why}\nusage: ${RUN_USAGE}`, 4);
// A regular file of this user, opened without following a link. Returns its
// bytes, or throws {problem}.
function readOwned(path) {
let fd;
try { fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW); }
catch (e) { throw Object.assign(new Error(e.code === 'ENOENT' ? 'missing' : `cannot open (${e.code})`), { problem: true }); }
try {
const st = fstatSync(fd);
if (!st.isFile()) throw Object.assign(new Error('not a regular file'), { problem: true });
if (st.uid !== process.getuid()) throw Object.assign(new Error('not owned by this user'), { problem: true });
return { bytes: readFileSync(fd), mode: st.mode & 0o777 };
} finally { closeSync(fd); }
}
// One evidence file: its path, sha256 and any problem. `expect` is the recorded digest.
function fileEvidence(path, { expect = null, privateMode = true } = {}) {
const out = { path, sha256: null, ok: false };
try {
const { bytes, mode } = readOwned(path);
out.sha256 = sha256(bytes);
if (privateMode && mode !== 0o600) out.problem = `mode ${mode.toString(8)}, expected 600`;
else if (expect !== null && expect !== out.sha256) out.problem = 'digest differs from the manifest';
else out.ok = true;
return { ...out, bytes };
} catch (e) {
if (!e.problem) throw e;
return { ...out, problem: e.message };
}
}
const strip = ({ bytes, ...rest }) => rest;
const inside = (root, path) => { const r = relative(root, path); return r !== '' && !r.startsWith('..') && !r.startsWith(sep) && r !== '..'; };
// The launch log's records for one thread, with their line numbers and digests.
export function threadRecords(file, thread) {
const log = fileEvidence(file);
if (!log.ok && log.problem === 'missing') return { file, missing: true, launches: [], ends: [], malformed: 0 };
if (!log.ok) throw new CliError(`run-evidence-refused: launch log ${file}: ${log.problem}`, 3);
const launches = [], ends = []; let malformed = 0;
log.bytes.toString('utf8').split('\n').forEach((line, i) => {
if (!line) return;
let rec;
try { rec = JSON.parse(line); } catch { malformed++; return; }
if (rec?.thread !== thread) return;
const at = { file, line: i + 1, sha256: sha256(line), record: rec };
if (rec.event === 'launch') launches.push(at);
else if (rec.event === 'end') ends.push(at);
});
return { file, missing: false, launches, ends, malformed };
}
// Everything one run left, checked against its own records. `verify` recomputes digests.
export function runEvidence({ runs, thread, launch, ends, verify }) {
const run = launch.record.run;
const problems = [];
const out = { run, launch: { file: launch.file, line: launch.line, sha256: launch.sha256, at: launch.record.at ?? null, session: launch.record.session ?? null, model: launch.record.model ?? null, harness: launch.record.harness ?? null },
end: ends.filter(e => e.record.run === run).map(e => ({ file: e.file, line: e.line, sha256: e.sha256, at: e.record.at ?? null, reason: e.record.reason ?? null, exitCode: e.record.exitCode ?? null })) };
if (typeof run !== 'string' || !RUN_ID.test(run)) return { ...out, ok: false, problems: [`launch record names no valid run id (${JSON.stringify(run)})`] };
const runDir = join(runs, run);
out.runDir = runDir;
let st;
try { st = lstatSync(runDir); } catch (e) { if (e.code !== 'ENOENT') throw e; }
if (!st) return { ...out, ok: false, problems: ['run directory missing'] };
if (st.isSymbolicLink() || !st.isDirectory() || st.uid !== process.getuid()) return { ...out, ok: false, problems: ['run directory is not a directory of this user'] };
const session = fileEvidence(join(runDir, 'session.json'));
if (session.ok) {
let doc = null;
try { doc = JSON.parse(session.bytes.toString('utf8')); } catch { session.ok = false; session.problem = 'not JSON'; }
if (doc && (doc.thread !== thread || doc.run !== run)) { session.ok = false; session.problem = `names thread ${doc.thread} run ${doc.run}`; }
}
out.result = strip(session);
if (!session.ok) problems.push(`session.json: ${session.problem}`);
const bundleDir = join(runDir, 'bundle');
const manifestPath = join(bundleDir, 'manifest.json');
const manifest = fileEvidence(manifestPath);
let doc = null;
if (manifest.ok) {
try { doc = JSON.parse(manifest.bytes.toString('utf8')); } catch { manifest.ok = false; manifest.problem = 'not JSON'; }
if (doc && doc.run !== run) { manifest.ok = false; manifest.problem = `names run ${doc.run}`; }
else if (launch.record.manifest && resolve(launch.record.manifest) !== manifestPath) { manifest.ok = false; manifest.problem = `launch record names ${launch.record.manifest}`; }
}
const files = [];
if (doc && verify) {
const recorded = doc.files && typeof doc.files === 'object' ? Object.entries(doc.files) : [];
if (recorded.length === 0 && manifest.ok) { manifest.ok = false; manifest.problem = 'manifest records no file digests'; }
for (const [name, digest] of recorded) {
const path = resolve(bundleDir, name);
if (!inside(runDir, path)) { files.push({ name, path, ok: false, problem: 'outside the run directory' }); continue; }
files.push({ name, ...strip(fileEvidence(path, { expect: digest, privateMode: false })) });
}
}
out.snapshot = { ...strip(manifest), ...(verify ? { files } : {}) };
if (!manifest.ok) problems.push(`bundle/manifest.json: ${manifest.problem}`);
for (const f of files) if (!f.ok) problems.push(`bundle file ${f.name}: ${f.problem}`);
const turns = join(runDir, 'turns');
let names = [];
try { names = readdirSync(turns).filter(n => STDERR.test(n)).sort(); } catch (e) { if (e.code !== 'ENOENT' && e.code !== 'ENOTDIR') throw e; }
out.stderr = names.map(n => strip(fileEvidence(join(turns, n))));
if (out.stderr.length === 0) problems.push('no turns/*.stderr');
for (const s of out.stderr) if (!s.ok) problems.push(`${relative(runDir, s.path)}: ${s.problem}`);
if (!verify) for (const s of [out.result, out.snapshot, ...out.stderr]) delete s.sha256;
return { ...out, ok: problems.length === 0, problems };
}
// The thread id for REF: a full id as given, else the bus's own resolution, else a
// unique thread name of the caller's.
async function threadId(ref, call) {
if (UUID.test(ref)) return { id: ref.toLowerCase(), label: ref };
if (!call) throw new CliError(`thread ${JSON.stringify(ref)} needs the bus to resolve; pass the thread id`, 2);
try { return await resolveThread(call, ref); }
catch (e) {
if (!(e instanceof CliError) || e.exitCode !== 2) throw e;
const hits = (await call('thread.list')).filter(t => t.name === ref);
if (hits.length === 1) return { id: hits[0].id, label: ref };
if (hits.length > 1) throw new CliError(`${hits.length} threads are named ${JSON.stringify(ref)}; use <project>/<name> or the id`, 2);
throw e;
}
}
export async function runCommand(rest, io, deps = {}) {
const [sub, ...args] = rest;
if (sub !== 'evidence') throw usage(sub ? `unknown run command ${sub}` : 'run needs a command');
const parsed = parseArgs(args, `usage: ${RUN_USAGE}`, { flags: ['--verify', '--json', '--tester'], values: ['--thread'] });
if (parsed.positional.length !== 1) throw usage('run evidence takes one organization');
if (!parsed.values['--thread']) throw usage('run evidence needs --thread');
const id = parsed.positional[0];
let instance;
try { instance = devOwnerInstance(io.env); }
catch (e) { throw Object.assign(new CliError(`run-evidence-instance-refused: ${e.message}`, 3), { code: 'run-evidence-instance-refused' }); }
const system = deps.system ?? cliConfig.loadSystem({ env: io.env, instance });
const ctx = { configDir: configDir(io.env), dataRoot: system.dataRoot, instance, principal: DEV_OWNER };
let org;
try { org = await resolveOrg(ctx, id); }
catch (e) { if (e instanceof OrgError) throw Object.assign(new CliError(`${e.code}: ${e.message}`, e.code === 'org-unknown' ? 2 : 3), { code: e.code }); throw e; }
// Until the per-organization paths land (PLAN C3, orgPaths in config.mjs), the
// launcher writes runs and the launch log under the instance data root.
const base = typeof cliConfig.orgPaths === 'function' ? org.root : system.dataRoot;
const callFor = deps.callFor ? () => deps.callFor({ positional: [], flags: parsed.flags, values: { '--business': id } }) : null;
const thread = await threadId(parsed.values['--thread'], UUID.test(parsed.values['--thread']) ? null : callFor?.());
const verify = parsed.flags.has('--verify');
const log = threadRecords(launchLogFile(base, id), thread.id);
const runs = log.launches.map(launch => runEvidence({ runs: runsDir(base, id), thread: thread.id, launch, ends: log.ends, verify }));
const verified = verify ? runs.length > 0 && runs.every(r => r.ok) : null;
const report = { org: { id: org.id, root: org.root, state: org.state }, instance, base, launchLog: { file: log.file, missing: log.missing, malformed: log.malformed }, thread, runs, verified };
if (parsed.flags.has('--json')) io.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
else {
const lines = [`org ${org.id} (${org.state}) at ${org.root}`, `thread ${thread.label}${thread.label !== thread.id ? ` (${thread.id})` : ''}`, `launch log ${log.file}${log.missing ? ' (missing)' : ''}`];
for (const r of runs) {
lines.push(`run ${r.run} launch ${r.launch.file}:${r.launch.line} sha256 ${r.launch.sha256} ${r.end.length ? `ended ${r.end.at(-1).reason ?? ''}` : 'no end record'}`);
if (r.result) lines.push(` result ${r.result.path}${r.result.sha256 ? ` ${r.result.sha256}` : ''}${r.result.ok ? '' : ` FAIL ${r.result.problem}`}`);
if (r.snapshot) lines.push(` snapshot ${r.snapshot.path}${r.snapshot.sha256 ? ` ${r.snapshot.sha256}` : ''}${verify ? ` (${r.snapshot.files.filter(f => f.ok).length}/${r.snapshot.files.length} files match)` : ''}`);
for (const s of r.stderr ?? []) lines.push(` stderr ${s.path}${s.sha256 ? ` ${s.sha256}` : ''}${s.ok ? '' : ` FAIL ${s.problem}`}`);
for (const p of r.problems) lines.push(` problem: ${p}`);
}
lines.push(verify ? `verified: ${verified ? 'yes' : 'no'}` : 'not verified (pass --verify)');
io.stdout.write(`${lines.join('\n')}\n`);
}
if (runs.length === 0) throw new CliError(`no launch for thread ${thread.label} in ${log.file}`, 2);
if (verify && !verified) throw new CliError('run evidence failed verification', 1);
return report;
}
+26 -12
View File
@@ -4,19 +4,35 @@
// missing notifier config, after the load was recorded.
import { test } from "node:test";
import assert from "node:assert/strict";
import { existsSync, readFileSync, writeFileSync, statSync } from "node:fs";
import { existsSync, readFileSync, writeFileSync, statSync, mkdirSync } from "node:fs";
import { join } from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { fixture, tmp } from "./helpers.mjs";
import { bootEventsFile, bootLine, bootStatusFile, readBootStatus, recordBootOk, recordBootRefused } from "../src/boot-status.mjs";
import { hostFile } from "../src/host.mjs";
import { reserveOrg, activateOrg } from "../../business/src/org-registry.mjs";
const CLI = fileURLToPath(new URL("../src/cli.mjs", import.meta.url));
const run = (f, ...args) => spawnSync(process.execPath, [CLI, ...args], { env: f.env, encoding: "utf8", timeout: 40000 });
const businessFile = (f) => join(f.config.replace(/config\.json$/, ""), "businesses", "acme.json");
const write = (f, text) => writeFileSync(businessFile(f), text, { mode: 0o600 });
async function orgFixture(t) {
const root = tmp(t, "o-");
const f = fixture(root);
const configDir = join(root, ".config", "mosaic-boot");
const env = { ...f.env, HOME: root, MOSAIC_CONFIG: join(configDir, "config.json") };
const bootstrap = spawnSync(join(process.cwd(), "scripts/bootstrap.sh"), ["--instance", "boot"], { env, encoding: "utf8" });
assert.equal(bootstrap.status, 0, bootstrap.stderr);
mkdirSync(join(configDir, "businesses"), { recursive: true, mode: 0o700 });
writeFileSync(join(configDir, "businesses", "acme.json"), readFileSync(businessFile(f)), { mode: 0o600 });
const ctx = { configDir, dataRoot: join(root, ".mosaic-boot"), instance: "boot" };
const org = await reserveOrg(ctx, "acme");
await activateOrg(ctx, "acme");
return { ...f, env, config: env.MOSAIC_CONFIG, dataRoot: org.root };
}
test("recordBootRefused keeps the last good load across refusals; recordBootOk clears the refusal", (t) => {
const dataRoot = tmp(t);
assert.equal(readBootStatus(dataRoot), null);
@@ -36,8 +52,8 @@ test("recordBootRefused keeps the last good load across refusals; recordBootOk c
for (const file of [bootStatusFile(dataRoot), bootEventsFile(dataRoot)]) assert.equal(statSync(file).mode & 0o777, 0o600);
});
test("an unknown key and a JSON syntax error each leave the bus down with the reason and path in the status", (t) => {
const f = fixture(tmp(t));
test("an unknown key and a JSON syntax error each leave the bus down with the reason and path in the status", async (t) => {
const f = await orgFixture(t);
const file = businessFile(f);
const doc = JSON.parse(readFileSync(file, "utf8"));
const cases = [
@@ -58,19 +74,17 @@ test("an unknown key and a JSON syntax error each leave the bus down with the re
assert.match(s.reason, reason);
assert.ok(s.reason.includes(file) || /surprise/.test(s.reason));
// `bus status` shows it, in text and in JSON.
const out = run(f, "bus", "status");
const out = run(f, "bus", "status", "acme", "--json");
assert.equal(out.status, 0, out.stderr);
assert.match(out.stdout, /host: none/);
assert.match(out.stdout, /boot: REFUSED at .*bus down, organization file invalid: /);
assert.ok(out.stdout.includes(`file ${file}`));
const json = JSON.parse(run(f, "bus", "status", "--json").stdout);
const json = JSON.parse(out.stdout);
assert.equal(json.healthy, false);
assert.equal(json.boot.ok, false);
assert.equal(json.boot.reason, s.reason);
}
});
test("a missing file is recorded too, and a valid file clears the status on the next start", (t) => {
const f = fixture(tmp(t));
test("a missing file is recorded too, and a valid file clears the status on the next start", async (t) => {
const f = await orgFixture(t);
const file = businessFile(f);
const good = readFileSync(file, "utf8");
// A good load is recorded before the start stops at the missing notifier config.
@@ -84,8 +98,8 @@ test("a missing file is recorded too, and a valid file clears the status on the
assert.equal(bad.lastGood, first.at, "the time of the last good load survives the refusal");
write(f, good);
const r = run(f, "bus", "start", "acme");
assert.match(r.stderr, /no notifier config/);
assert.match(readFileSync(join(f.dataRoot, "bus-host", "host.log"), "utf8"), /no notifier config/);
const cleared = readBootStatus(f.dataRoot);
assert.deepEqual([cleared.ok, cleared.reason], [true, null]);
assert.match(run(f, "bus", "status").stdout, /boot: organization file loaded at /);
assert.equal(JSON.parse(run(f, "bus", "status", "acme", "--json").stdout).boot.ok, true);
});
+7 -1
View File
@@ -36,6 +36,10 @@ const IMPORT=new RegExp([
`${DYNAMIC.source}\\s*['"](\\.{1,2}\\/[^'"]+)['"]\\s*\\)`,
].join('|'),'g');
const DYNAMIC_LITERAL=/^import\s*\(\s*(['"])[^'"\n]*\1\s*\)/;
// The plug-in loader's one computed import, allowed only in that file and only in this exact form. It loads
// packages/plugins/<kind>/<name>/index.mjs after checking that path, and a real copy ships all of
// packages/plugins, so a fixture that never loads a plug-in needs nothing more from it.
const LOADER_FILE='packages/plugins/src/loader.mjs',LOADER_IMPORT=/^import\(pathToFileURL\(expected\)\.href\)/;
const REGEX_AFTER='(,=:[!&|?{};+-*%<>~^';
const inert=(literals,index)=>literals.some(([from,to])=>index>=from&&index<to);
const sources=(dir)=>readdirSync(dir,{withFileTypes:true}).flatMap((e)=>e.isDirectory()?sources(join(dir,e.name)):e.name.endsWith('.mjs')?[join(dir,e.name)]:[]);
@@ -110,7 +114,9 @@ function specifiers(file,text){
catch(error){throw new Error(`${file} cannot be read for imports: ${error.message}`);}
const {code,literals}=scanned,live=(m)=>!inert(literals,m.index);
for(const m of code.matchAll(DYNAMIC)){
if(live(m)&&!DYNAMIC_LITERAL.test(code.slice(m.index,m.index+512)))throw new Error(`${file} has a dynamic import that is not a plain string literal`);
const at=code.slice(m.index,m.index+512);
if(file===LOADER_FILE&&LOADER_IMPORT.test(at))continue;
if(live(m)&&!DYNAMIC_LITERAL.test(at))throw new Error(`${file} has a dynamic import that is not a plain string literal`);
}
return [...code.matchAll(IMPORT)].filter(live).map((m)=>m[1]??m[2]??m[3]);
}
@@ -29,6 +29,14 @@ test('the real tree gives the plug-in files the loader reads', ()=>{
assert.deepEqual(found,[...found].sort());
});
test('the loader exception holds only for the loader file and its exact computed import', ()=>{
const text="const module=await import(pathToFileURL(expected).href);\n";
const at=(file)=>tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/loader.mjs';\n",[`repo/${file}`]:text});
assert.deepEqual(businessImportedFiles(at('packages/plugins/src/loader.mjs')),['packages/plugins/src/loader.mjs']);
assert.throws(()=>businessImportedFiles(tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/other.mjs';\n",'repo/packages/plugins/src/other.mjs':text})),/not a plain string literal/);
assert.throws(()=>businessImportedFiles(tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/loader.mjs';\n",'repo/packages/plugins/src/loader.mjs':"await import(x);\n"})),/not a plain string literal/);
});
test('every static form and a literal dynamic import is found, and the imports of what it finds in turn', ()=>{
const repo=withBusiness([
"import x from '../../other/src/from.mjs';",
+1 -1
View File
@@ -71,7 +71,7 @@ function world(t, { profile = "library" } = {}) {
script(join(bin, "stack-copy"), `echo "stack-copy $*" >>'${log}'
[ "$1" = copy ] && [ "$3" = --copies ] || exit 4
mkdir -p "$4/$2/scripts" && : >"$4/$2/scripts/mosaic" && echo "copied: $4/$2"
for file in scripts/case2-persona-import.mjs packages/harness/src/bundle.mjs packages/harness/src/tools.mjs packages/business/src/vocabulary.mjs; do
for file in scripts/case2-persona-import.mjs packages/harness/src/bundle.mjs packages/harness/src/tools.mjs packages/business/src/vocabulary.mjs packages/plugins/model-provider/cliproxy/index.mjs; do
mkdir -p "$4/$2/$(dirname "$file")" && cp '${REPO}'"$file" "$4/$2/$file" || exit 1
done
mkdir -p "$4/$2/node_modules" && cp -R '${REPO}node_modules/yaml' "$4/$2/node_modules/yaml"`);
+15 -8
View File
@@ -1,12 +1,13 @@
// Private fixture runtime only; no live host, unit, store or organization file.
import { spawn } from "node:child_process";
import { once } from "node:events";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { fixture, BUSINESSES } from "./helpers.mjs";
import { systemFor } from "../../business/tests/helpers.mjs";
import { reserveOrg, activateOrg } from '../../business/src/org-registry.mjs';
export async function childResult(file, args, env, input) {
const child = spawn(file, args, { env, stdio: ["pipe", "pipe", "pipe"] });
@@ -33,6 +34,12 @@ export async function provedRuntime(t) {
} finally { rmSync(root, { recursive: true, force: true }); }
});
const f = fixture(root, "demo", doc => ({ ...doc, plugins: { channel: [] } }));
mkdirSync(join(root, '.config'), { mode: 0o700 });
const configDir = join(root, '.config', 'mosaic-test');
renameSync(join(root, 'config'), configDir); chmodSync(configDir, 0o700);
writeFileSync(join(f.dataRoot, 'instance.json'), JSON.stringify({ instanceVersion: 1, name: 'test' }));
const ctx = { configDir, dataRoot: f.dataRoot, instance: 'test' };
const org = await reserveOrg(ctx, 'demo'); await activateOrg(ctx, 'demo');
const url = rel => new URL(rel, import.meta.url).href;
const humanURL = url("../../bus/src/human.mjs");
const script = join(root, "fixture-runtime.mjs");
@@ -47,14 +54,14 @@ mock.module(${JSON.stringify(humanURL)}, { namedExports: {
readProcess: pid => ({ ...real.readProcess(pid), ppid: 1 }) }),
} });
const { startBroker } = await import(${JSON.stringify(url("../../bus/src/runtime.mjs"))});
const runtime = await startBroker({ dataRoot: ${JSON.stringify(f.dataRoot)},
const runtime = await startBroker({ dataRoot: ${JSON.stringify(org.root)},
businesses: ${JSON.stringify(BUSINESSES)},
organization: { dir: ${JSON.stringify(join(root, "config"))}, rolesDir: ${JSON.stringify(f.roles)}, system: ${JSON.stringify(systemFor(root))} } });
organization: { dir: ${JSON.stringify(configDir)}, rolesDir: ${JSON.stringify(f.roles)}, system: ${JSON.stringify(systemFor(root))} } });
process.on('SIGTERM', async () => { await runtime.close(); process.exit(0); });
console.log('fixture ready');
`);
const env = { PATH: process.env.PATH, HOME: root, TMPDIR: process.env.TMPDIR,
MOSAIC_CONFIG: f.config, MOSAIC_ROLES_DIR: f.roles };
MOSAIC_CONFIG: join(configDir, 'config.json'), MOSAIC_ROLES_DIR: f.roles };
host = spawn(process.execPath, ["--experimental-test-module-mocks", script], { env, stdio: ["ignore", "pipe", "pipe"] });
let output = "", errors = "";
host.stderr.on("data", b => errors += b);
@@ -65,10 +72,10 @@ console.log('fixture ready');
});
const cli = fileURLToPath(new URL("../src/cli.mjs", import.meta.url));
const shim = fileURLToPath(new URL("../../bus/src/human-cli.mjs", import.meta.url));
return { root, f, env, shim, socket: join(f.dataRoot, "bus/broker.sock"),
return { root, f, env, shim, socket: join(org.root, "bus/broker.sock"),
cli: argv => childResult(process.execPath, [cli, ...argv, "--business", "demo"], env),
shimCall: (file, request) => childResult(process.execPath, [file, join(f.dataRoot, "bus/broker.sock")], env, JSON.stringify(request) + "\n"),
file: join(root, "config/businesses/demo.json"),
history: () => readFileSync(join(root, "config/businesses/.history/demo/changes.jsonl"), "utf8").trim().split("\n").map(JSON.parse),
shimCall: (file, request) => childResult(process.execPath, [file, join(org.root, "bus/broker.sock")], env, JSON.stringify(request) + "\n"),
file: join(configDir, "businesses/demo.json"),
history: () => readFileSync(join(configDir, "businesses/.history/demo/changes.jsonl"), "utf8").trim().split("\n").map(JSON.parse),
};
}
+1 -1
View File
@@ -154,7 +154,7 @@ test("every human command refuses inside an agent run before it touches the bus"
test("usage errors exit 4; no business and no host is a usage error", async (t) => {
const s = setup(t);
for (const argv of [[], ["nope"], ["inbox", "extra", "--business", "demo"], ["inbox", "--bogus"], ["trail", "--business", "demo"], ["decide", "x", "--business", "demo"], ["bus"], ["bus", "stop", "x"], ["inbox"]]) {
for (const argv of [[], ["nope"], ["inbox", "extra", "--business", "demo"], ["inbox", "--bogus"], ["trail", "--business", "demo"], ["decide", "x", "--business", "demo"], ["bus"], ["bus", "stop"], ["inbox"]]) {
assert.equal((await s.run(argv)).code, 4, argv.join(" ") || "(none)");
}
assert.equal(s.calls.length, 0);
+121
View File
@@ -0,0 +1,121 @@
// `mosaic dev fixture`: refusals, the state file's checks, and one real
// init/status/stop of the loopback endpoint in a scratch HOME.
import test from 'node:test';
import assert from 'node:assert/strict';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { devCommand, fixtureFile, fixtureLaunchEnv, fixtureLauncherOptions, isEndpoint, orgUsesFixture, readFixture } from '../src/dev-fixture.mjs';
import { FIXTURE_ENTRY, FIXTURE_KEY } from '../../plugins/model-provider/fixture/index.mjs';
import { io } from './helpers.mjs';
// devOwnerInstance reads the instance from MOSAIC_CONFIG under $HOME/.config.
const HOME = mkdtempSync(join(tmpdir(), 'devfx'));
process.env.HOME = HOME;
test.after(() => rmSync(HOME, { recursive: true, force: true }));
const config = name => join(HOME, '.config', `mosaic-${name}`, 'config.json');
const envFor = (name = 'fxtest', extra = {}) => ({ PATH: process.env.PATH, HOME, MOSAIC_CONFIG: config(name), ...extra });
let n = 0;
const dataRoot = () => { const d = join(HOME, `data${++n}`); mkdirSync(d, { mode: 0o700 }); return d; };
async function refused(args, env, system, code, exitCode = 3) {
const x = io(env);
await assert.rejects(devCommand(args, x, { system }), e => { assert.equal(e.exitCode, exitCode, e.message); if (code) assert.equal(e.code, code, e.message); return true; });
return x;
}
const INIT = ['fixture', 'init', '--model', 'deterministic'];
test('init refuses before it starts anything', async () => {
const system = { dataRoot: dataRoot() };
await refused(['fixture', 'init'], envFor(), system, undefined, 4);
await refused(['fixture', 'init', '--model', 'claude'], envFor(), system, undefined, 4);
await refused([...INIT, '--bind', '0.0.0.0'], envFor(), system, 'fixture-endpoint-not-loopback');
await refused(INIT, { PATH: process.env.PATH, HOME }, system, 'fixture-instance-refused');
await refused(INIT, envFor('dev'), system, 'fixture-instance-refused');
await refused([...INIT, '--instance', 'other'], envFor(), system, 'fixture-instance-mismatch');
await refused(INIT, envFor('fxtest', { ANTHROPIC_API_KEY: 'sk-ant-value-never-printed' }), system, 'fixture-credential-present');
await assert.rejects(devCommand(INIT, io(envFor('fxtest', { ZAI_API_KEY: 'zai-value-never-printed' })), { system }), e => /ZAI_API_KEY/.test(e.message) && !/never-printed/.test(e.message));
await refused(INIT, envFor(), { ...system, proxy: { url: 'http://127.0.0.1:1' } }, 'fixture-proxy-configured');
assert.equal(existsSync(join(system.dataRoot, 'dev-fixture')), false);
});
function writeState(root, record, { dirMode = 0o700, fileMode = 0o600 } = {}) {
const dir = join(root, 'dev-fixture');
mkdirSync(dir, { recursive: true, mode: 0o700 }); chmodSync(dir, dirMode);
writeFileSync(fixtureFile(root), JSON.stringify(record), { mode: fileMode }); chmodSync(fixtureFile(root), fileMode);
}
const RECORD = { fixtureVersion: 1, model: 'deterministic', endpoint: 'http://127.0.0.1:45678', port: 45678, pid: 999999, instance: 'fxtest', entry: FIXTURE_ENTRY };
test('the state file must be private, a fixture record and loopback', () => {
const unsafe = e => e.code === 'fixture-state-unsafe';
assert.equal(readFixture(dataRoot()), null);
let root = dataRoot(); writeState(root, RECORD);
assert.equal(readFixture(root).port, 45678);
root = dataRoot(); writeState(root, RECORD, { dirMode: 0o755 }); assert.throws(() => readFixture(root), unsafe);
root = dataRoot(); writeState(root, RECORD, { fileMode: 0o644 }); assert.throws(() => readFixture(root), unsafe);
root = dataRoot(); writeState(root, { ...RECORD, endpoint: 'http://0.0.0.0:45678' }); assert.throws(() => readFixture(root), e => e.code === 'fixture-endpoint-not-loopback');
root = dataRoot(); writeState(root, { ...RECORD, endpoint: 'http://127.0.0.1:1' }); assert.throws(() => readFixture(root), e => e.code === 'fixture-state-invalid');
root = dataRoot(); writeState(root, { ...RECORD, entry: { ...FIXTURE_ENTRY, upstream: 'anthropic' } }); assert.throws(() => readFixture(root), e => e.code === 'fixture-state-invalid');
root = dataRoot(); mkdirSync(join(root, 'dev-fixture'), { mode: 0o700 }); writeFileSync(join(root, 'elsewhere.json'), JSON.stringify(RECORD), { mode: 0o600 });
symlinkSync(join(root, 'elsewhere.json'), fixtureFile(root)); assert.throws(() => readFixture(root), unsafe);
root = dataRoot(); writeState(root, RECORD); writeFileSync(fixtureFile(root), 'not json'); assert.throws(() => readFixture(root), e => e.code === 'fixture-state-invalid');
});
test('launcher options: none without a fixture, the endpoint with one, refusals around it', () => {
assert.deepEqual(fixtureLauncherOptions({ system: { dataRoot: dataRoot() }, env: {}, instance: 'fxtest' }), {});
const root = dataRoot(); writeState(root, RECORD);
assert.deepEqual(fixtureLauncherOptions({ system: { dataRoot: root }, env: { PATH: '/usr/bin' }, instance: 'fxtest' }), { testProxy: { baseUrl: RECORD.endpoint, key: FIXTURE_KEY } });
assert.throws(() => fixtureLauncherOptions({ system: { dataRoot: root, proxy: {} }, env: {}, instance: 'fxtest' }), e => e.code === 'fixture-proxy-configured');
assert.throws(() => fixtureLauncherOptions({ system: { dataRoot: root }, env: { ANTHROPIC_AUTH_TOKEN: 'x' }, instance: 'fxtest' }), e => e.code === 'fixture-credential-present');
assert.throws(() => fixtureLauncherOptions({ system: { dataRoot: root }, env: {}, instance: 'other' }), e => e.code === 'fixture-instance-mismatch');
});
test('a fixture serves only organizations that use it; their launches lose provider credentials', () => {
const fixtureOrg = { plugins: { harness: ['pi'], 'model-provider': ['fixture'] }, launch: { models: { 'fixture-deterministic': FIXTURE_ENTRY } } };
const realOrg = { plugins: { harness: ['claude-code'] }, launch: { models: { opus: { provider: 'anthropic', model: 'claude-opus-5-5' } } } };
assert.equal(orgUsesFixture(fixtureOrg), true);
assert.equal(orgUsesFixture({ launch: { models: { k: { module: 'fixture' } } } }), true);
assert.equal(orgUsesFixture(realOrg), false);
const env = { PATH: '/usr/bin', CLAUDE_CODE_OAUTH_TOKEN: 'x', ANTHROPIC_API_KEY: 'y' };
assert.deepEqual(fixtureLaunchEnv(fixtureOrg, env), { PATH: '/usr/bin' });
assert.equal(fixtureLaunchEnv(realOrg, env), env);
const root = dataRoot(); writeState(root, RECORD);
// A real-model organization on a fixture instance launches without the fixture, credential kept.
assert.deepEqual(fixtureLauncherOptions({ system: { dataRoot: root }, env, instance: 'fxtest', doc: realOrg }), {});
// A fixture organization gets the endpoint once its environment is stripped.
assert.deepEqual(fixtureLauncherOptions({ system: { dataRoot: root }, env: fixtureLaunchEnv(fixtureOrg, env), instance: 'fxtest', doc: fixtureOrg }), { testProxy: { baseUrl: RECORD.endpoint, key: FIXTURE_KEY } });
// Handed the raw environment it still refuses.
assert.throws(() => fixtureLauncherOptions({ system: { dataRoot: root }, env, instance: 'fxtest', doc: fixtureOrg }), e => e.code === 'fixture-credential-present');
});
test('a real init, status and stop on loopback', async t => {
const system = { dataRoot: dataRoot() };
const env = envFor();
const x = io(env);
await devCommand([...INIT, '--instance', 'fxtest', '--json'], x, { system });
const receipt = JSON.parse(x.out.text);
t.after(() => { if (isEndpoint(receipt.pid)) process.kill(receipt.pid, 'SIGKILL'); });
assert.equal(receipt.receipt, 'dev-fixture-init');
assert.match(receipt.endpoint, /^http:\/\/127\.0\.0\.1:[0-9]+$/);
assert.deepEqual([receipt.loopback, receipt.external, receipt.upstream, receipt.instance, receipt.principal, receipt.modelKey], [true, false, 'none', 'fxtest', 'local-dev:local-dev-owner', 'fixture-deterministic']);
assert.equal(isEndpoint(receipt.pid), true);
assert.equal(JSON.parse(readFileSync(fixtureFile(system.dataRoot), 'utf8')).pid, receipt.pid);
assert.doesNotMatch(readFileSync(fixtureFile(system.dataRoot), 'utf8'), new RegExp(FIXTURE_KEY));
const s = io(env);
await devCommand(['fixture', 'status', '--json'], s, { system });
const status = JSON.parse(s.out.text);
assert.deepEqual([status.ready, status.alive, status.external, status.upstream, status.proxyConfigured, status.health.upstream], [true, true, false, 'none', false, 'none']);
const p = io(env);
await devCommand(['fixture', 'status', '--json'], p, { system: { ...system, proxy: {} } });
assert.equal(JSON.parse(p.out.text).ready, false);
await refused(INIT, env, system, 'fixture-exists');
const st = io(env);
await devCommand(['fixture', 'stop', '--json'], st, { system });
assert.deepEqual(JSON.parse(st.out.text).signaled, true);
assert.equal(isEndpoint(receipt.pid), false);
assert.equal(readFixture(system.dataRoot), null);
await refused(['fixture', 'status'], env, system, undefined, 2);
});
+16 -1
View File
@@ -2,7 +2,7 @@
// points MOSAIC_CONFIG there; nothing reads the real ~/.config, a real
// token, a real binding, or the real human transport.
import { chmodSync, cpSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { chmodSync, cpSync, mkdirSync, mkdtempSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { PassThrough } from "node:stream";
@@ -14,6 +14,7 @@ import { REPO_SKILLS } from "../../harness/tests/helpers.mjs";
import { tree } from "../../plugins/tests/helpers.mjs";
import { SESSION_CREDENTIALS } from "../../seat/src/session.mjs";
import { AGENT_MARKERS } from "../src/transport.mjs";
import { reserveOrg, activateOrg } from "../../business/src/org-registry.mjs";
// Row 332 R2 (#1830): every host variable a launch reads as a login. The
// session login names come from the seat; the other three are what pi's
@@ -66,6 +67,20 @@ export function fixture(root, id = "acme", edit = (doc) => doc) {
return { config, roles, dataRoot: join(root, "data"), doc, env: humanEnv({ MOSAIC_CONFIG: config, MOSAIC_ROLES_DIR: roles }) };
}
// An active, named organization for real transport fixtures. The business
// document remains the fixture's original document; the bus root is isolated.
export async function orgFixture(t, id = "acme", edit = doc => doc) {
const root = tmp(t, "o-"), f = fixture(root, id, edit);
mkdirSync(join(root, ".config"), { mode: 0o700 });
const dir = join(root, ".config", "mosaic-test");
renameSync(join(root, "config"), dir); chmodSync(dir, 0o700);
writeFileSync(join(f.dataRoot, "instance.json"), JSON.stringify({ instanceVersion: 1, name: "test" }));
f.config = join(dir, "config.json"); f.env.MOSAIC_CONFIG = f.config; f.env.HOME = root;
const ctx = { configDir: dir, dataRoot: f.dataRoot, instance: "test" };
const org = await reserveOrg(ctx, id); await activateOrg(ctx, id);
return { root, f, org };
}
export function notifyConfig(dataRoot, business, binding) {
// The journal sits beside notify.json and refuses a directory looser than 0700.
mkdirSync(join(dataRoot, "notify", business), { recursive: true, mode: 0o700 });
+88 -135
View File
@@ -4,13 +4,15 @@ import { spawn, spawnSync } from "node:child_process";
import { subscribe, unsubscribe } from "node:diagnostics_channel";
import { createServer } from "node:http";
import { once } from "node:events";
import { chmodSync, existsSync, readFileSync, readdirSync, rmSync, statSync, symlinkSync, utimesSync, writeFileSync, mkdirSync } from "node:fs";
import { chmodSync, existsSync, readFileSync, readdirSync, rmSync, statSync, symlinkSync, utimesSync, writeFileSync, mkdirSync, renameSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "../../bus/src/client.mjs";
import { bootConfig, loadSystem, REPO } from "../src/config.mjs";
import { claimHostLock, hostDir, hostFile, hostLock, hostStatus, readHostState, startHost, startTimeOf, stopHost, watchChildren } from "../src/host.mjs";
import { journalPath } from "../src/notifier.mjs";
import { reserveOrg, activateOrg } from "../../business/src/org-registry.mjs";
import { busStart, busStatus, busStop } from "../src/org-bus.mjs";
import { IDS, makeDeployment } from "../../discord/tests/helpers.mjs";
import { fixture, notifyConfig, OPTIONS, tmp } from "./helpers.mjs";
@@ -18,6 +20,22 @@ const CLI = fileURLToPath(new URL("../src/cli.mjs", import.meta.url));
const CHANNEL = "100000000000000900";
const TOKEN = "MTAw.abcdefghijklmnopqrstuvwxyz0123456789";
// A named scratch instance with an active organization. Keep the old fixture's
// business and role documents; only the instance/organization layout changes.
async function orgFixture(t, { notify = true } = {}) {
const root = tmp(t), f = fixture(root), instance = "hosttest";
const dir = join(root, ".config", `mosaic-${instance}`);
mkdirSync(dirname(dir), { recursive: true, mode: 0o700 });
renameSync(join(root, "config"), dir);
chmodSync(dir, 0o700); // S0 requires private instance configuration directories.
const env = { ...f.env, HOME: root, MOSAIC_CONFIG: join(dir, "config.json") };
writeFileSync(join(f.dataRoot, "instance.json"), JSON.stringify({ instanceVersion: 1, name: instance }), { mode: 0o600 });
const ctx = { configDir: dir, dataRoot: f.dataRoot, instance, env };
const org = await reserveOrg(ctx, "acme"); await activateOrg(ctx, "acme");
if (notify) notifyConfig(org.root, "acme", null);
return { ...f, env, ctx, instanceRoot: f.dataRoot, dataRoot: org.root };
}
// A fake Discord REST on 127.0.0.1: opens one DM channel, accepts messages.
async function fakeDiscord(t) {
const requests = [];
@@ -247,35 +265,14 @@ test("a second host for the same data root refuses with exit 3 while the first r
assert.equal(await host.close(0), 0);
});
test("two bus starts racing for one data root leave one host running; the other refuses that a host already runs", async (t) => {
const f = fixture(tmp(t));
notifyConfig(f.dataRoot, "acme", null);
const starts = [0, 1].map(() => {
const child = spawn(process.execPath, [CLI, "bus", "start", "acme"], { env: f.env, stdio: ["ignore", "pipe", "pipe"] });
t.after(() => child.exitCode === null && child.signalCode === null && child.kill("SIGKILL"));
const run = { child, out: "", exit: once(child, "exit") };
child.stdout.on("data", (b) => (run.out += b));
child.stderr.on("data", (b) => (run.out += b));
return run;
});
// The loser exits; the winner keeps running.
const [first] = await Promise.race(starts.map((r, i) => r.exit.then((e) => [i, e])));
const loser = starts[first];
const winner = starts[1 - first];
assert.equal(loser.child.exitCode, 3, loser.out);
assert.match(loser.out, new RegExp(`a bus host already runs for acme \\(pid ${winner.child.pid}\\)`));
await until(() => /bus host up: business acme/.test(winner.out), 30000);
assert.equal(winner.child.exitCode, null);
const state = readHostState(f.dataRoot);
assert.equal(state.pid, winner.child.pid);
assert.equal(state.live, true);
assert.equal(JSON.parse(readFileSync(hostLock(f.dataRoot), "utf8")).pid, winner.child.pid);
const claims = () => readdirSync(hostDir(f.dataRoot)).filter((n) => n.startsWith("host.")).sort();
assert.deepEqual(claims(), ["host.json", "host.lock"], "no claim file left over");
const stop = spawnSync(process.execPath, [CLI, "bus", "stop"], { env: f.env, encoding: "utf8", timeout: 70000 });
assert.equal(stop.status, 0, stop.stderr);
assert.equal((await winner.exit)[0], 0, winner.out);
assert.deepEqual(claims(), [], "the stop gives back host.lock and host.json");
test("two starts racing for one organization leave exactly one host", async t => {
const f = await orgFixture(t);
const results = await Promise.allSettled([busStart(f.ctx, "acme", { detach: true }), busStart(f.ctx, "acme", { detach: true })]);
t.after(async () => { try { await busStop(f.ctx, "acme"); } catch {} });
assert.equal(results.filter(r => r.status === "fulfilled").length, 1);
assert.equal(results.filter(r => r.status === "rejected").length, 1);
assert.equal((await busStatus(f.ctx, "acme")).healthy, true);
assert.equal((await busStop(f.ctx, "acme")).stopped, true);
});
test("a host takes over a host.lock its dead host left, and puts back a live claim that replaced it meanwhile", (t) => {
@@ -338,46 +335,26 @@ test("a live host's lock stays, however old the file is", (t) => {
assert.deepEqual(readdirSync(hostDir(dataRoot)), ["host.lock"]);
});
test("a host.lock left by a host killed with SIGKILL doesn't stop the next bus start", async (t) => {
const f = fixture(tmp(t));
notifyConfig(f.dataRoot, "acme", null);
const start = () => {
const child = spawn(process.execPath, [CLI, "bus", "start", "acme"], { env: f.env, stdio: ["ignore", "pipe", "pipe"] });
t.after(() => child.exitCode === null && child.signalCode === null && child.kill("SIGKILL"));
const run = { child, out: "", exit: once(child, "exit") };
child.stdout.on("data", (b) => (run.out += b));
child.stderr.on("data", (b) => (run.out += b));
return run;
};
const first = start();
await until(() => /bus host up: business acme/.test(first.out), 30000);
first.child.kill("SIGKILL");
await first.exit;
assert.equal(JSON.parse(readFileSync(hostLock(f.dataRoot), "utf8")).pid, first.child.pid, "the killed host never gave back its lock");
// The orphaned broker sees its channel close and lets go of the writer lock.
test("a stale organization host lock is preserved and refuses another start", async t => {
const f = await orgFixture(t), receipt = await busStart(f.ctx, "acme", { detach: true });
process.kill(receipt.pid, "SIGKILL");
await until(() => startTimeOf(receipt.pid) === null, 30000);
await until(() => !existsSync(join(f.dataRoot, "bus", "writer.lock")), 30000);
const second = start();
await until(() => /bus host up: business acme/.test(second.out) || second.child.exitCode !== null, 30000);
assert.equal(second.child.exitCode, null, second.out);
assert.equal(readHostState(f.dataRoot).pid, second.child.pid);
assert.equal(JSON.parse(readFileSync(hostLock(f.dataRoot), "utf8")).pid, second.child.pid);
const stop = spawnSync(process.execPath, [CLI, "bus", "stop"], { env: f.env, encoding: "utf8", timeout: 70000 });
assert.equal(stop.status, 0, stop.stderr);
assert.equal((await second.exit)[0], 0, second.out);
const before = readFileSync(hostLock(f.dataRoot), "utf8");
await assert.rejects(busStart(f.ctx, "acme", { detach: true }), /stale.*lock/i);
assert.equal(readFileSync(hostLock(f.dataRoot), "utf8"), before);
});
test("while a host boots, a reader sees no host: the human commands still ask for --business, stop and status find none", (t) => {
const f = fixture(tmp(t));
test("while an organization host boots, stop and status refuse its unmatched lock", async t => {
const f = await orgFixture(t);
const booting = { hostVersion: 1, pid: process.pid, startTime: startTimeOf(process.pid), business: "acme", startedAt: "2026-10-13T00:00:01.000Z", notifier: null };
claimHostLock(f.dataRoot, booting);
assert.equal(readHostState(f.dataRoot), null);
assert.equal(hostStatus(f.dataRoot).host, null);
await assert.rejects(busStatus(f.ctx, "acme"), /lock/i);
await assert.rejects(busStop(f.ctx, "acme"), /lock/i);
const inbox = spawnSync(process.execPath, [CLI, "inbox"], { env: f.env, encoding: "utf8", timeout: 20000 });
assert.notEqual(inbox.status, 0);
assert.match(inbox.stderr, /no bus host runs here, so name the business with --business/);
const stop = spawnSync(process.execPath, [CLI, "bus", "stop"], { env: f.env, encoding: "utf8", timeout: 20000 });
assert.equal(stop.status, 0, stop.stderr);
assert.match(stop.stdout, /no host runs/);
assert.match(inbox.stderr, /name the organization/);
});
test("a host started before host.lock existed, holding only host.json, still refuses a second start, which leaves no lock", async (t) => {
@@ -761,77 +738,44 @@ test("bus stop refuses to signal a live pid that is not a bus host", async (t) =
assert.equal(child.signalCode, null, "the child was not signalled");
});
test("bus start refuses with exit 3 and the code when the broker refuses to boot; bus status names the lock", (t) => {
const f = fixture(tmp(t));
notifyConfig(f.dataRoot, "acme", null);
test("a stale organization writer lock refuses start and status without removing it", async t => {
const f = await orgFixture(t);
mkdirSync(join(f.dataRoot, "bus"), { recursive: true, mode: 0o700 });
writeFileSync(join(f.dataRoot, "bus", "writer.lock"), JSON.stringify({ pid: 999999999, at: "2026-10-08T00:00:00Z" }), { mode: 0o600 });
const r = spawnSync(process.execPath, [CLI, "bus", "start", "acme"], { env: f.env, encoding: "utf8", timeout: 40000 });
assert.equal(r.status, 3, r.stderr);
assert.match(r.stderr, /broker refused to start: startup-refused/);
assert.equal(existsSync(hostFile(f.dataRoot)), false);
assert.equal(existsSync(hostLock(f.dataRoot)), false);
const s = spawnSync(process.execPath, [CLI, "bus", "status"], { env: f.env, encoding: "utf8" });
assert.equal(s.status, 0, s.stderr);
assert.match(s.stdout, /host: none/);
assert.match(s.stdout, /writer\.lock: pid 999999999 \(not running/);
const file = join(f.dataRoot, "bus", "writer.lock"), text = JSON.stringify({ pid: 999999999, at: "2026-10-08T00:00:00Z" });
writeFileSync(file, text, { mode: 0o600 });
await assert.rejects(busStart(f.ctx, "acme", { detach: true }), /stale.*lock/i);
await assert.rejects(busStatus(f.ctx, "acme"), /stale.*lock/i);
assert.equal(readFileSync(file, "utf8"), text);
});
test("bus start refuses with exit 3 without a notifier config", (t) => {
const f = fixture(tmp(t));
const r = spawnSync(process.execPath, [CLI, "bus", "start", "acme"], { env: f.env, encoding: "utf8", timeout: 40000 });
assert.equal(r.status, 3);
assert.match(r.stderr, /no notifier config/);
test("organization bus start refuses without a notifier config", async t => {
const f = await orgFixture(t, { notify: false });
await assert.rejects(busStart(f.ctx, "acme", { detach: true }), /failed to start/i);
assert.match(readFileSync(join(f.dataRoot, "bus-host", "host.log"), "utf8"), /no notifier config/);
assert.equal(existsSync(join(f.dataRoot, "bus", "writer.lock")), false);
});
test("bus start runs until bus stop; status reports it while it runs", async (t) => {
const f = fixture(tmp(t));
notifyConfig(f.dataRoot, "acme", null);
const child = spawn(process.execPath, [CLI, "bus", "start", "acme"], { env: f.env, stdio: ["ignore", "pipe", "pipe"] });
t.after(() => child.exitCode === null && child.kill("SIGKILL"));
let out = "";
child.stdout.on("data", (b) => (out += b));
child.stderr.on("data", (b) => (out += b));
await until(() => /bus host up: business acme/.test(out), 30000);
const status = spawnSync(process.execPath, [CLI, "bus", "status", "--json"], { env: f.env, encoding: "utf8" });
const s = JSON.parse(status.stdout);
assert.equal(s.host.live, true);
assert.equal(s.host.pid, child.pid);
assert.equal(s.host.notifier, null);
assert.equal(s.socket, true);
assert.equal(s.writerLock.live, true);
const exit = once(child, "exit");
const stop = spawnSync(process.execPath, [CLI, "bus", "stop"], { env: f.env, encoding: "utf8", timeout: 70000 });
assert.equal(stop.status, 0, stop.stderr);
assert.match(stop.stdout, new RegExp(`stopped bus host for acme \\(pid ${child.pid}\\)`));
assert.equal((await exit)[0], 0, out);
test("organization bus start, status and stop report the same live host", async t => {
const f = await orgFixture(t);
const started = await busStart(f.ctx, "acme", { detach: true });
t.after(async () => { try { await busStop(f.ctx, "acme"); } catch {} });
const status = await busStatus(f.ctx, "acme");
assert.equal(status.healthy, true); assert.equal(status.pid, started.pid);
assert.equal((await busStop(f.ctx, "acme")).stopped, true);
assert.equal((await busStatus(f.ctx, "acme")).healthy, false);
assert.equal(existsSync(join(f.dataRoot, "bus", "writer.lock")), false);
const again = spawnSync(process.execPath, [CLI, "bus", "stop"], { env: f.env, encoding: "utf8" });
assert.match(again.stdout, /no host runs/);
});
// Row 163 (mos-0961): the D2 rehearsal's host.
test("bus start --no-launch says launches are disabled in its host-up line and stops cleanly; with --pm it is a usage error", async (t) => {
const f = fixture(tmp(t));
notifyConfig(f.dataRoot, "acme", null);
const both = spawnSync(process.execPath, [CLI, "bus", "start", "acme", "--pm", "--no-launch"], { env: f.env, encoding: "utf8", timeout: 40000 });
assert.equal(both.status, 4, both.stderr);
assert.match(both.stderr, /bus start takes --pm or --no-launch, not both/);
assert.equal(existsSync(join(f.dataRoot, "bus", "writer.lock")), false);
const child = spawn(process.execPath, [CLI, "bus", "start", "acme", "--no-launch"], { env: f.env, stdio: ["ignore", "pipe", "pipe"] });
t.after(() => child.exitCode === null && child.kill("SIGKILL"));
let out = "";
child.stdout.on("data", (b) => (out += b));
child.stderr.on("data", (b) => (out += b));
await until(() => /bus host up: business acme.*\n/.test(out), 30000);
assert.match(out, /bus host up: business acme, socket \S+, notifier off, launches disabled\n/);
// The launcher got launches false: its Registry is read-only, so no sessions.json appears.
test("organization bus --no-launch preserves the launch fence; --pm conflicts", async t => {
const f = await orgFixture(t);
const both = spawnSync(process.execPath, [CLI, "bus", "start", "acme", "--pm", "--no-launch"], { env: f.env, encoding: "utf8" });
assert.equal(both.status, 4); assert.match(both.stderr, /--pm or --no-launch/);
const receipt = await busStart(f.ctx, "acme", { detach: true, noLaunch: true });
t.after(async () => { try { await busStop(f.ctx, "acme"); } catch {} });
assert.equal(readHostState(f.dataRoot).options.noLaunch, true);
assert.equal(existsSync(join(f.dataRoot, "bus-host", "sessions.json")), false);
const exit = once(child, "exit");
const stop = spawnSync(process.execPath, [CLI, "bus", "stop"], { env: f.env, encoding: "utf8", timeout: 70000 });
assert.equal(stop.status, 0, stop.stderr);
assert.equal((await exit)[0], 0, out);
assert.equal((await busStatus(f.ctx, "acme")).pid, receipt.pid);
assert.equal((await busStop(f.ctx, "acme")).stopped, true);
});
test("bus-service.sh renders the unit and installs it into a given directory", (t) => {
@@ -1029,20 +973,29 @@ test("bus-service.sh refuses a default claude without the execute bit, or in a P
});
// Row 191: a restarted halted instance must keep its recovery bus reachable.
test('bus start --pm while halted keeps the control host up without launching the chief', { timeout: 30000 }, async t => {
test('organization bus --pm while halted keeps the control host up', { timeout: 30000 }, async t => {
const { initializeControl, transition, readControl } = await import('../../processing-control/src/state.mjs');
const f = fixture(tmp(t)); notifyConfig(f.dataRoot, 'acme', null);
const f = await orgFixture(t);
initializeControl(f.dataRoot);
transition(f.dataRoot, { business: 'acme', human: 'jason', halted: true });
const child = spawn(process.execPath, [CLI, 'bus', 'start', 'acme', '--pm'], { env: f.env, stdio: ['ignore', 'pipe', 'pipe'] });
const exited = once(child, 'exit'); let output = '';
child.stdout.on('data', b => { output += b; }); child.stderr.on('data', b => { output += b; });
t.after(async () => { if (child.exitCode === null && child.signalCode === null) { child.kill('SIGTERM'); await exited; } });
await until(() => /processing halted: chief launch skipped/.test(output) || child.exitCode !== null);
assert.match(output, /processing halted: chief launch skipped/);
assert.equal(child.exitCode, null, output);
const status = spawnSync(process.execPath, [CLI, 'bus', 'status', '--json'], { env: f.env, encoding: 'utf8', timeout: 5000 });
assert.equal(status.status, 0, status.stderr);
assert.equal(JSON.parse(status.stdout).host.live, true);
const receipt = await busStart(f.ctx, 'acme', { detach: true, pm: true });
t.after(async () => { try { await busStop(f.ctx, 'acme'); } catch {} });
assert.match(readFileSync(join(f.dataRoot, 'bus-host/host.log'), 'utf8'), /processing halted: chief launch skipped/);
assert.equal((await busStatus(f.ctx, 'acme')).pid, receipt.pid);
assert.equal(readControl(f.dataRoot).halted, true);
assert.equal((await busStop(f.ctx, 'acme')).stopped, true);
});
test("organization notifier uses instance bindings and its own journal", async t => {
const f = await orgFixture(t), root = dirname(f.instanceRoot);
makeDeployment(root, { dmRecipient: IDS.owner });
const discord = await fakeDiscord(t);
const system = loadSystem({ env: f.env, instance: f.ctx.instance });
const boot = bootConfig({ system, businessId: "acme", orgRoot: f.dataRoot, env: f.env });
const host = await startHost({ boot, business: "acme", notifier: { binding: "test-seat", base: discord.base }, log: () => {} });
t.after(() => host.close(0));
assert.equal(host.dataRoot, f.dataRoot);
assert.ok(existsSync(journalPath(f.dataRoot, "acme")));
assert.equal(existsSync(journalPath(f.instanceRoot, "acme")), false);
await host.close(0);
});
@@ -77,9 +77,8 @@ test('REQ-LIMIT-4 the launcher reaches token-limit only through the bus: launche
const src=readFileSync(fileURLToPath(new URL('../src/launcher.mjs',import.meta.url)),'utf8');
assert.doesNotMatch(src,/token-limit/);
assert.doesNotMatch(src,/limit-check\//);
// Row 65: the launcher reaches its harness through the plug-in loader, and every load names only
// the harness kind, so a limit check still can't load here.
assert.deepEqual([...src.matchAll(/\.load\(([^)]*)\)/g)].map(m=>m[1]),['{ harness: [m.harness] }']);
// The launcher loads model providers and harnesses, but never a limit check.
assert.deepEqual([...src.matchAll(/\.load\(([^)]*)\)/g)].map(m=>m[1]),["{ 'model-provider': providerNames }",'{ harness: [m.harness] }']);
assert.doesNotMatch(src,/modules\(\s*["'](?!harness["'])/);
assert.doesNotMatch(src,/\bimport\s*\(/);
});
+15 -2
View File
@@ -24,7 +24,7 @@ const MODULES={
const ON=['pi','claude-code','down','sparse','metered','broken','throws','absent'];
// vars are the coder's whole agent vars; edit changes the rest of the file.
async function setup(t,vars,{on=ON,edit=doc=>doc,env:more={}}={}){
async function setup(t,vars,{on=ON,edit=doc=>doc,env:more={},modelAdmission=null}={}){
const root=tmp(t),f=fixture(root,'acme',doc=>{doc.roles.coder.vars=vars;doc.plugins={...doc.plugins,harness:on};return edit(doc);}),ops=[];
initializeControl(f.dataRoot);
withoutHostCredentials(f.env,{piKey:true});
@@ -33,10 +33,11 @@ async function setup(t,vars,{on=ON,edit=doc=>doc,env:more={}}={}){
ops.push(x);
if(x.op==='identity')return {business:'acme',role:'pm'};
if(x.op==='mayLaunch')return {allow:true};
if(x.op==='modelAdmission' && modelAdmission) return modelAdmission;
if(x.op==='authorizeLaunch')throw Object.assign(Error('fixture authorization stop'),{code:'fixture-policy'});
return {};
}};
const makeLoader=await fakeHarnesses(t,{pi:null,'claude-code':null},write=>{for(const [name,body] of Object.entries(MODULES))write('harness',name,body);});
const makeLoader=await fakeHarnesses(t,{pi:null,'claude-code':null},write=>{for(const [name,body] of Object.entries(MODULES))write('harness',name,body);write('model-provider','cliproxy',`export * from ${JSON.stringify(new URL('../../plugins/model-provider/cliproxy/index.mjs',import.meta.url).href)};`);});
// The system config's execution keys are set and must never reach a route.
const system={...JSON.parse(readFileSync(f.config,'utf8'))};
system.execution={...system.execution,adapter:'pi',model:'glm-5.3-flash'};
@@ -142,3 +143,15 @@ test('row 65 a thinking level the route harness does not apply refuses thinking-
assert.deepEqual(await s.launch(),{ok:false,error:'thinking-unsupported'});
assert.equal(s.last().why,'harness claude-code takes thinking low, medium, high, xhigh, max, not off');
});
test('module-backed unavailable selection refuses before run creation',async t=>{
const s=await setup(t,route('pi','zai-fixture-id','low'),{edit:doc=>{doc.plugins['model-provider']=['cliproxy'];doc.launch.models['zai-fixture-id']={provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'};return doc;}});
assert.deepEqual(await s.launch(),{ok:false,error:'model-provider-unavailable'});
assert.equal(s.ops.some(op=>op.op==='modelAdmission'),true);
assert.deepEqual(s.may(),[{provider:'zai',model:'fixture-id',account:null}]);
});
test('a proxy uncertainty refusal survives launcher admission without creating a run',async t=>{
const s=await setup(t,route('pi','zai-fixture-id','low'),{modelAdmission:{allow:false,code:'proxy-uncertain'},edit:doc=>{doc.plugins['model-provider']=['cliproxy'];doc.launch.models['zai-fixture-id']={provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'};return doc;}});
assert.deepEqual(await s.launch(),{ok:false,error:'proxy-uncertain'});
assert.equal(s.last().code,'proxy-uncertain');
});
@@ -21,7 +21,7 @@ const id = value => createHash('sha256').update(value).digest('hex').slice(0,12)
// #774 R2: extra names more harnesses to switch on and load (a fallback route's); real names the
// harnesses loaded as the real module, so their own preflight runs; hostEnv adds launcher env
// such as MOSAIC_CLAUDE_BIN.
export async function managedLaunchFixture(t,{proxy=true,launches=true,refuseProbe=false,harness='pi',bwrap=null,admit=null,extra=[],real=[],hostEnv={},modelStream=null}={}){
export async function managedLaunchFixture(t,{proxy=true,launches=true,refuseProbe=false,harness='pi',bwrap=null,admit=null,extra=[],real=[],hostEnv={},modelStream=null,modelCatalogue=null}={}){
const root=fs.mkdtempSync(join(tmpdir(),'ml653-')),closers=[],history=new Set(['fx653-manage']),proxyReceipts=[],logs=[];
let server,bus,launcher,stderr;const trace={preflightAttempts:0,successfulPreflights:0,enableBeforeSpawn:[],bindings:[],stalls:[],ops:[]};
const checkHistory=entries=>{const text=JSON.stringify(entries);for(const value of history)if(text.includes(value))throw Error('fixture credential leaked');};
@@ -69,13 +69,14 @@ export async function managedLaunchFixture(t,{proxy=true,launches=true,refusePro
const system={...loadSystem({env}),...(proxy?{proxy:{port,binary:'/fixture-unused',firstOutputSeconds:modelStream?.seconds ?? 1}}:{})};
const {trackers,...boot}=bootConfig({system,businessId:'acme',env});assert.equal(trackers,undefined);
stderr=process.stderr.write;process.stderr.write=function(chunk,...args){if(String(chunk).startsWith('model-proxy: ')){proxyReceipts.push(String(chunk));const callback=args.find(a=>typeof a==='function');callback?.();return true;}return stderr.call(this,chunk,...args);};
bus=await startBroker(boot);closers.push(()=>bus.close());
bus=await startBroker({...boot,modelCatalogue});closers.push(()=>bus.close());
const hooks=[];let expectedKeyId;
const requestOps=requestHostOps(bus);
const actualOp=async m=>{
if(Object.hasOwn(requestOps,m.op))return requestOps[m.op](m);
switch(m.op){
case 'modelPreflight':return bus.modelPreflight(m);
case 'modelAdmission':return bus.modelAdmission(m);
case 'modelStall':return bus.modelStall(m);
case 'identity':return bus.identity(m.cap);
case 'authorizeLaunch':return bus.authorizeLaunch(m.cap,m.instance);
@@ -108,7 +109,7 @@ export async function managedLaunchFixture(t,{proxy=true,launches=true,refusePro
return actualOp(m);
},
bindLaunch:async record=>{const result=bus.bindLaunch(record);trace.bindings.push({run:record.run,pid:record.pid,startTime:record.startTime,modelKeyId:record.modelKeyId});return result;}};
const makeLoader=await fakeHarnesses({after:close=>closers.push(close)},Object.fromEntries([harness,...extra].map(name=>[name,real.includes(name)?null:adapter])));
const makeLoader=await fakeHarnesses({after:close=>closers.push(close)},Object.fromEntries([harness,...extra].map(name=>[name,real.includes(name)?null:adapter])),write=>write('model-provider','cliproxy',`export * from ${JSON.stringify(new URL('../../plugins/model-provider/cliproxy/index.mjs',import.meta.url).href)};`));
launcher=createLauncher({host,system,env,launches,makeLoader,...(bwrap?{bwrap}:{}),sessionDefaults:{pollInterval:50},harnessVersion:'0.85.1',log:line=>logs.push(line)});
// The existing trusted host hook owns all launcher-created children. No registry
// PID is signalled by fixture teardown; normal bounded launcher close is used.
+21
View File
@@ -17,3 +17,24 @@ test('proxy-config-invalid: port 8317 refuses before prompting',async()=>await a
test('proxy-key-invalid: empty prompt refuses without printing input',async()=>{const x=io();x.stdin.isTTY=true;x.stdout.isTTY=true;await assert.rejects(main(['proxy','init','--business','demo'],x,{system:fakeSystem(),promptProxyKey:async()=>'',transport:()=>()=>{throw Error('must not call');}}),code('proxy-key-invalid'));});
test('real no-echo prompt restores terminal state and never writes the key',async()=>{const {promptProxyKey}=await import('../src/model-proxy.mjs');const x=io();x.stdin.isTTY=true;x.stdout.isTTY=true;const modes=[];x.stdin.isRaw=false;x.stdin.setRawMode=value=>{modes.push(value);x.stdin.isRaw=value;};const answer=promptProxyKey(x,'Management key: ');x.stdin.write('p1-management\r');assert.equal(await answer,'p1-management');assert.deepEqual(modes,[true,false]);assert.equal(x.out.text,'');assert.equal(x.err.text.includes('p1-management'),false);});
test('real no-echo prompt ctrl-C refuses and restores echo',async()=>{const {promptProxyKey}=await import('../src/model-proxy.mjs');const x=io();x.stdin.isTTY=true;x.stdout.isTTY=true;x.stdin.isRaw=false;x.stdin.setRawMode=value=>{x.stdin.isRaw=value;};const answer=promptProxyKey(x,'Management key: ');x.stdin.write('\u0003');await assert.rejects(answer,code('proxy-key-invalid'));assert.equal(x.stdin.isRaw,false);});
test('proxy fence clear refuses without the explicit restart statement', async () => {
await assert.rejects(main(['proxy', 'fence', 'clear'], io(), { system: fakeSystem() }), code('proxy-restart-required'));
});
test('proxy fence status and flagged clear use the shared state and a loopback management read', async t => {
const fs = await import('node:fs'); const http = await import('node:http');
const { createProxyLock } = await import('../../bus/src/proxy-lock.mjs');
const root = fs.mkdtempSync(join(tmpdir(), 'cli-fence-')); t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const config = join(root, 'config'); fs.mkdirSync(join(config, 'secrets'), { recursive: true, mode: 0o700 });
fs.writeFileSync(join(config, 'secrets', 'proxy-management.key'), 'fixture-management', { mode: 0o600 });
fs.mkdirSync(join(root, 'proxy'), { mode: 0o700 });
const server = http.createServer((req, res) => { assert.equal(req.headers.authorization, 'Bearer fixture-management'); res.end('["fixture-sentinel"]'); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); t.after(() => new Promise(resolve => server.close(resolve)));
const port = server.address().port, lock = createProxyLock({ stateDir: join(root, 'proxy'), identity: `${config}:${port}` });
await lock.run(async () => lock.fence({ org: 'alpha', op: 'issue' }));
const x = io({ MOSAIC_CONFIG: join(config, 'config.json') }); const system = { dataRoot: root, proxy: { port } };
await main(['proxy', 'fence', 'status', '--json'], x, { system });
assert.equal(JSON.parse(x.out.text).uncertain.org, 'alpha');
const y = io({ MOSAIC_CONFIG: join(config, 'config.json') });
await main(['proxy', 'fence', 'clear', '--proxy-restarted'], y, { system });
assert.equal(JSON.parse(y.out.text).cleared, true); assert.equal(lock.status(), null);
});
@@ -0,0 +1,57 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { spawn, spawnSync } from 'node:child_process';
import { mkdtemp, writeFile, rm, access } from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { tmpdir } from 'node:os';
import * as org from '../../business/src/org-service.mjs';
import { listOrgs } from '../../business/src/org-registry.mjs';
import { busStart, busStopForDelete } from '../src/org-bus.mjs';
import { readHostState } from '../src/host.mjs';
const repo = resolve(import.meta.dirname, '../../..');
async function until(fn) { const end = Date.now() + 20000; while (Date.now() < end) { if (await fn()) return; await new Promise(r => setTimeout(r, 20)); } throw Error('probe condition timed out'); }
test('internal host holds lifecycle fence from admission through readiness', { timeout: 60000 }, async t => {
const home = await mkdtemp(join(tmpdir(), 'r-'));
const env = { ...process.env, HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-rv/config.json') };
const boot = spawnSync(join(repo, 'scripts/bootstrap.sh'), ['--instance', 'rv'], { env, encoding: 'utf8' }); assert.equal(boot.status, 0, boot.stderr);
const ctx = { configDir: join(home, '.config/mosaic-rv'), dataRoot: join(home, '.mosaic-rv'), instance: 'rv', env, principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
await org.createOrg(ctx, 'alpha', await org.templateRender('alpha', { fixture: 'local' }));
const gate = join(home, 'gate'), preload = join(home, 'pause.mjs');
await writeFile(preload, `import fs from 'node:fs';import {syncBuiltinESMExports} from 'node:module';const append=fs.appendFileSync;let once=false;fs.appendFileSync=function(file,...args){if(!once&&String(file).endsWith('/bus-host/boot-events.jsonl')){once=true;fs.writeFileSync(${JSON.stringify(gate+'.ready')},'ready');const end=Date.now()+15000;while(!fs.existsSync(${JSON.stringify(gate+'.go')})){if(Date.now()>end)throw Error('gate timed out');Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,10);}}return append.call(this,file,...args)};syncBuiltinESMExports();`);
const child = spawn(process.execPath, ['--import', preload, join(repo, 'packages/cli/src/cli.mjs'), 'bus', 'start', 'alpha', '--internal-host', '--dev-local-owner', '--no-launch'], { env, stdio: ['ignore', 'pipe', 'pipe', 'ipc'] });
let output = '', ready = false; child.stdout.on('data', d => { output += d; }); child.stderr.on('data', d => { output += d; }); child.on('message', m => { if (m?.orgReady === 'alpha') ready = true; });
const ended = new Promise(r => child.once('exit', r));
t.after(async () => { await writeFile(gate+'.go', 'go'); if (child.exitCode === null && child.signalCode === null) { child.kill('SIGTERM'); await ended; } await rm(home, { recursive: true, force: true }); });
await until(async () => { try { await access(gate+'.ready'); return true; } catch { return child.exitCode !== null; } });
assert.equal(child.exitCode, null, output); assert.equal(readHostState(join(ctx.dataRoot, 'org-runtime', 'alpha')), null);
await assert.rejects(org.archiveOrg(ctx, 'alpha', { ifRevision: 1, busStop: busStopForDelete }), { code: 'org-locked' });
assert.equal((await listOrgs(ctx))[0].state, 'active');
await writeFile(gate+'.go', 'go'); await until(() => ready || child.exitCode !== null || child.signalCode !== null);
assert.equal(ready, true, output); assert.equal(readHostState(join(ctx.dataRoot, 'org-runtime', 'alpha'))?.live, true);
await busStopForDelete(ctx, 'alpha'); await ended;
const result = await org.archiveOrg(ctx, 'alpha', { ifRevision: 1, busStop: busStopForDelete });
assert.equal(result.outcome, 'done'); assert.equal((await listOrgs(ctx))[0].state, 'archived');
});
test('archive between parent preparation and child admission prevents the bus from starting', { timeout: 60000 }, async t => {
const home = await mkdtemp(join(tmpdir(), 'r-'));
const env = { ...process.env, HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-rv/config.json') };
const boot = spawnSync(join(repo, 'scripts/bootstrap.sh'), ['--instance', 'rv'], { env, encoding: 'utf8' }); assert.equal(boot.status, 0, boot.stderr);
const ctx = { configDir: join(home, '.config/mosaic-rv'), dataRoot: join(home, '.mosaic-rv'), instance: 'rv', env, principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
await org.createOrg(ctx, 'alpha', await org.templateRender('alpha', { fixture: 'local' }));
const gate = join(home, 'gate'), preload = join(home, 'pause.mjs');
await writeFile(preload, `import fs from 'node:fs';fs.writeFileSync(${JSON.stringify(gate+'.ready')},'ready');const end=Date.now()+15000;while(!fs.existsSync(${JSON.stringify(gate+'.go')})){if(Date.now()>end)throw Error('gate timed out');Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,10);}`);
env.NODE_OPTIONS = `--import=${preload}`;
const pending = busStart(ctx, 'alpha', { detach: true, devLocalOwner: true, noLaunch: true }).then(value => ({ value }), error => ({ error }));
try {
await until(async () => { try { await access(gate+'.ready'); return true; } catch { return false; } });
const archived = await org.archiveOrg(ctx, 'alpha', { ifRevision: 1, busStop: busStopForDelete });
assert.equal(archived.outcome, 'done');
await writeFile(gate+'.go', 'go');
const result = await pending; assert.match(result.error?.message ?? '', /bus failed to start/);
assert.equal((await listOrgs(ctx))[0].state, 'archived');
assert.equal(readHostState(join(ctx.dataRoot, 'org-runtime', 'alpha'))?.live ?? false, false);
} finally { await writeFile(gate+'.go', 'go'); await pending; await rm(home, { recursive: true, force: true }); }
});
+92
View File
@@ -0,0 +1,92 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { reserveOrg, activateOrg } from '../../business/src/org-registry.mjs';
import { businessDoc, rolesCopy, writeJson } from '../../business/tests/helpers.mjs';
import { humanEnv, notifyConfig } from './helpers.mjs';
import { busStart, busStatus, busStop } from '../src/org-bus.mjs';
const REPO = new URL('../../../', import.meta.url).pathname;
test('two organization buses are isolated and refusals never select a sibling', { timeout: 120000 }, async t => {
const home = mkdtempSync(join(tmpdir(), 's1-'));
const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-s1test/config.json') });
const setup = spawnSync(join(REPO, 'scripts/bootstrap.sh'), ['--instance', 's1test'], { env, encoding: 'utf8' });
assert.equal(setup.status, 0, setup.stderr);
const ctx = { configDir: join(home, '.config/mosaic-s1test'), dataRoot: join(home, '.mosaic-s1test'), instance: 's1test', env };
env.MOSAIC_ROLES_DIR = rolesCopy(home);
for (const id of ['alpha', 'beta']) {
const org = await reserveOrg(ctx, id);
await activateOrg(ctx, id);
writeJson(join(ctx.configDir, 'businesses', `${id}.json`), businessDoc(home, id));
notifyConfig(org.root, id, null);
}
t.after(async () => {
for (const id of ['alpha', 'beta']) { try { await busStop(ctx, id); } catch {} }
rmSync(home, { recursive: true, force: true });
});
const incompatible = spawnSync(join(REPO, 'scripts/mosaic'), ['bus', 'start', 'alpha', '--pm', '--no-launch'], { env, encoding: 'utf8' });
assert.equal(incompatible.status, 4);
const start = spawnSync(join(REPO, 'scripts/mosaic'), ['bus', 'start', 'alpha', '--no-launch', '--detach'], { env, encoding: 'utf8', timeout: 30000 });
assert.equal(start.status, 0, start.stderr);
const alpha = JSON.parse(start.stdout);
const beta = await busStart(ctx, 'beta', { detach: true, devLocalOwner: true });
assert.notEqual(alpha.socket, beta.socket);
assert.notEqual(alpha.root, beta.root);
assert.equal((await busStatus(ctx, 'alpha')).healthy, true);
assert.equal((await busStatus(ctx, 'beta')).healthy, true);
const refused = spawnSync(join(REPO, 'scripts/mosaic'), ['bus', 'recover', 'alpha'], { env, encoding: 'utf8' });
assert.equal(refused.status, 3, refused.stderr);
assert.equal((await busStatus(ctx, 'alpha')).healthy, true);
await assert.rejects(busStatus(ctx, 'missing'), e => e.code === 'org-unknown');
const launches = spawnSync(join(REPO, 'scripts/mosaic'), ['launches', 'list', '--business', 'beta', '--json'], { env, encoding: 'utf8' });
assert.equal(launches.status, 0, launches.stderr);
assert.ok(Array.isArray(JSON.parse(launches.stdout)));
const inbox = spawnSync(join(REPO, 'scripts/mosaic'), ['inbox', '--business', 'alpha', '--json'], { env, encoding: 'utf8', timeout: 30000 });
assert.equal(inbox.status, 3, inbox.stderr);
assert.match(inbox.stderr, /human-required/); // The existing pinned-tree proof refused after reaching alpha.
const status = spawnSync(join(REPO, 'scripts/mosaic'), ['bus', 'status', 'alpha', '--json'], { env, encoding: 'utf8' });
assert.equal(status.status, 0, status.stderr);
assert.equal(JSON.parse(status.stdout).socket, alpha.socket);
const restart = spawnSync(join(REPO, 'scripts/mosaic'), ['bus', 'restart', 'alpha'], { env, encoding: 'utf8', timeout: 30000 });
assert.equal(restart.status, 0, restart.stderr);
assert.notEqual(JSON.parse(restart.stdout).pid, alpha.pid);
assert.equal(JSON.parse(readFileSync(join(alpha.root, 'bus-host/host.json'), 'utf8')).options.noLaunch, true);
assert.equal((await busStatus(ctx, 'beta')).healthy, true);
const stop = spawnSync(join(REPO, 'scripts/mosaic'), ['bus', 'stop', 'alpha'], { env, encoding: 'utf8', timeout: 30000 });
assert.equal(stop.status, 0, stop.stderr);
assert.equal(JSON.parse(stop.stdout).stopped, true);
assert.equal((await busStatus(ctx, 'alpha')).healthy, false);
assert.equal((await busStatus(ctx, 'beta')).healthy, true);
// Recovery removes dead host and broker locks, and a fresh start can take them.
const staleHost = { pid: 2147483647, startTime: '1', business: 'alpha', startedAt: '2026-01-01T00:00:00Z' };
writeFileSync(join(alpha.root, 'bus-host/host.json'), JSON.stringify(staleHost), { mode: 0o600 });
writeFileSync(join(alpha.root, 'bus-host/host.lock'), JSON.stringify(staleHost), { mode: 0o600 });
writeFileSync(join(alpha.root, 'bus/writer.lock'), JSON.stringify({ pid: 2147483647, at: '2026-01-01T00:00:00Z' }), { mode: 0o600 });
const recovery = spawnSync(join(REPO, 'scripts/mosaic'), ['bus', 'recover', 'alpha'], { env, encoding: 'utf8' });
assert.equal(recovery.status, 0, recovery.stderr);
const restarted = await busStart(ctx, 'alpha', { detach: true, noLaunch: true });
assert.ok(restarted.pid > 0);
assert.equal((await busStatus(ctx, 'alpha')).healthy, true);
await busStop(ctx, 'alpha');
// A forged alpha state cannot name the running beta host as alpha.
const alphaHost = join(alpha.root, 'bus-host');
const betaState = JSON.parse(readFileSync(join(beta.root, 'bus-host/host.json'), 'utf8'));
const forged = { ...betaState, business: 'alpha' };
writeFileSync(join(alphaHost, 'host.json'), JSON.stringify(forged), { mode: 0o600 });
writeFileSync(join(alphaHost, 'host.lock'), JSON.stringify(forged), { mode: 0o600 });
try { await assert.rejects(busStatus(ctx, 'alpha'), /mismatch|identity/i); }
finally { rmSync(join(alphaHost, 'host.json')); rmSync(join(alphaHost, 'host.lock')); }
assert.equal((await busStatus(ctx, 'beta')).healthy, true);
mkdirSync(join(alpha.root, 'bus'), { recursive: true, mode: 0o700 });
writeFileSync(join(alpha.root, 'bus/writer.lock'), JSON.stringify({ pid: 99999999 }), { mode: 0o600 });
await assert.rejects(busStart(ctx, 'alpha', { detach: true }), /lock/i);
await assert.rejects(busStatus(ctx, 'alpha'), /lock/i);
writeFileSync(join(alpha.root, '.mosaic-org.json'), JSON.stringify({ instance: 's1test', id: 'beta' }), { mode: 0o600 });
await assert.rejects(busStatus(ctx, 'alpha'), e => e.code === 'org-mismatch');
assert.equal((await busStatus(ctx, 'beta')).healthy, true);
});
+51
View File
@@ -0,0 +1,51 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdtempSync, mkdirSync, writeFileSync, readdirSync, rmSync } from 'node:fs';
import { get } from 'node:http';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { reserveOrg, activateOrg } from '../../business/src/org-registry.mjs';
import { humanEnv } from './helpers.mjs';
import { consoleStart, consoleStop } from '../src/org-console.mjs';
const REPO = new URL('../../../', import.meta.url).pathname;
test('detached console reports its live loopback URL and stops', { timeout: 60000 }, async t => {
const home = mkdtempSync(join(tmpdir(), 's1-'));
const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-s1test/config.json') });
const setup = spawnSync(join(REPO, 'scripts/bootstrap.sh'), ['--instance', 's1test'], { env, encoding: 'utf8' });
assert.equal(setup.status, 0, setup.stderr);
const ctx = { configDir: join(home, '.config/mosaic-s1test'), dataRoot: join(home, '.mosaic-s1test'), instance: 's1test', env };
await reserveOrg(ctx, 'alpha');
await activateOrg(ctx, 'alpha');
t.after(async () => { try { await consoleStop(ctx); } catch {} rmSync(home, { recursive: true, force: true }); });
mkdirSync(join(ctx.dataRoot, 'console'), { mode: 0o700 });
writeFileSync(join(ctx.dataRoot, 'console/console.json'), JSON.stringify({ pid: 2147483647, startTime: '1', url: 'http://127.0.0.1:1/' }), { mode: 0o600 });
const warnings = [];
const result = await consoleStart(ctx, { devLocalOwner: true, detach: true, port: 'auto', warn: s => warnings.push(s) });
assert.equal(warnings.length, 1);
assert.equal(readdirSync(join(ctx.dataRoot, 'console')).filter(x => x.startsWith('console.json.stale-')).length, 1);
await assert.rejects(consoleStart(ctx, { devLocalOwner: true, detach: true }), /state exists/);
const url = new URL(result.url);
assert.equal(url.hostname, '127.0.0.1');
assert.ok(Number.isSafeInteger(result.pid));
assert.ok(![7332, 7340, 7341, 33223, 17450, 17443, 44307, 7350].includes(Number(url.port)));
const status = await new Promise((resolve, reject) => get(url, res => { res.resume(); resolve(res.statusCode); }).on('error', reject));
assert.equal(status, 200);
assert.equal((await consoleStop(ctx)).stopped, true);
});
test('mosaic console start and stop dispatch through the CLI', { timeout: 60000 }, async t => {
const home = mkdtempSync(join(tmpdir(), 's1-'));
const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-s1test/config.json') });
assert.equal(spawnSync(join(REPO, 'scripts/bootstrap.sh'), ['--instance', 's1test'], { env }).status, 0);
const ctx = { configDir: join(home, '.config/mosaic-s1test'), dataRoot: join(home, '.mosaic-s1test'), instance: 's1test', env };
await reserveOrg(ctx, 'alpha'); await activateOrg(ctx, 'alpha');
t.after(async () => { try { await consoleStop(ctx); } catch {} rmSync(home, { recursive: true, force: true }); });
const start = spawnSync(join(REPO, 'scripts/mosaic'), ['console', 'start', '--dev-local-owner', '--detach', '--port', 'auto'], { env, encoding: 'utf8', timeout: 30000 });
assert.equal(start.status, 0, start.stderr);
assert.match(JSON.parse(start.stdout).url, /^http:\/\/127\.0\.0\.1:\d+\/$/);
const stop = spawnSync(join(REPO, 'scripts/mosaic'), ['console', 'stop'], { env, encoding: 'utf8', timeout: 30000 });
assert.equal(stop.status, 0, stop.stderr);
assert.equal(JSON.parse(stop.stdout).stopped, true);
});
+21
View File
@@ -0,0 +1,21 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { chmodSync, mkdirSync, renameSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { fixture, tmp } from './helpers.mjs';
import { busStart, busStop, busStopForDelete } from '../src/org-bus.mjs';
import { createOrg, deleteOrg, templateRender } from '../../business/src/org-service.mjs';
test('deletion stops the recorded running bus before moving its runtime', { timeout: 60000 }, async t => {
const root = tmp(t, 'd-'), f = fixture(root), dir = join(root, '.config', 'mosaic-test');
mkdirSync(join(root, '.config'), { mode: 0o700 }); renameSync(join(root, 'config'), dir); chmodSync(dir, 0o700); chmodSync(join(dir, 'businesses'), 0o700);
writeFileSync(join(f.dataRoot, 'instance.json'), JSON.stringify({ instanceVersion: 1, name: 'test' }), { mode: 0o600 });
const env = { ...f.env, HOME: root, MOSAIC_CONFIG: join(dir, 'config.json') };
const ctx = { configDir: dir, dataRoot: f.dataRoot, instance: 'test', env, principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
await createOrg(ctx, 'a', await templateRender('a'));
const started = await busStart(ctx, 'a', { detach: true, devLocalOwner: true, noLaunch: true });
t.after(async () => { try { await busStop(ctx, 'a'); } catch {} });
const receipt = await deleteOrg(ctx, 'a', { busStop: busStopForDelete, confirm: 'a', ifRevision: 1 });
assert.equal(receipt.bus.stopped, true); assert.equal(receipt.bus.pid, started.pid);
assert.ok(receipt.moves.some(move => move.from === started.root));
});
+49
View File
@@ -0,0 +1,49 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { dirname, join } from 'node:path';
import { orgPaths } from '../src/config.mjs';
test('organization paths are rooted together and reject overlong sockets', () => {
const root = '/var/mosaic/org-runtime/alpha';
assert.deepEqual(orgPaths(root), {
socket: join(root, 'bus/broker.sock'), busDir: join(root, 'bus'),
runs: join(root, 'runs'), launches: join(root, 'launches'),
sessions: join(root, 'sessions'), workspaces: join(root, 'workspaces'),
threads: join(root, 'threads'), hostDir: join(root, 'bus-host'),
});
assert.throws(() => orgPaths('/' + 'a'.repeat(110)), /socket.*long/i);
assert.throws(() => orgPaths('relative'), /absolute/i);
});
import { bootConfig, loadSystem } from '../src/config.mjs';
import { fixture, tmp } from './helpers.mjs';
test('broker boot keeps instance and organization roots distinct', (t) => {
const f = fixture(tmp(t));
const root = join(f.dataRoot, 'org-runtime', 'acme');
const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: 'acme', orgRoot: root, env: f.env });
assert.equal(boot.dataRoot, root);
assert.equal(boot.instanceRoot, f.dataRoot);
assert.deepEqual(boot.org, { id: 'acme' });
assert.equal(boot.instanceDataRoot, f.dataRoot);
assert.equal(boot.instanceConfigDir, dirname(f.config));
});
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { claimHostLock, hostLock, startTimeOf } from '../src/host.mjs';
test('organization host claim refuses a stale lock without removing it', t => {
const root = tmp(t);
mkdirSync(join(root, 'bus-host'), { mode: 0o700 });
const stale = { pid: 99999999, startTime: '0', business: 'alpha', startedAt: 'old' };
writeFileSync(hostLock(root), JSON.stringify(stale), { mode: 0o600 });
const mine = { pid: process.pid, startTime: startTimeOf(process.pid), business: 'alpha', startedAt: 'now' };
assert.throws(() => claimHostLock(root, mine, { refuseStale: true }), /stale.*lock/i);
assert.deepEqual(JSON.parse(readFileSync(hostLock(root), 'utf8')), stale);
});
import { busStatus } from '../src/org-bus.mjs';
test('an inaccessible registry refuses as a configuration error', async t => {
const root = tmp(t);
await assert.rejects(busStatus({ configDir: join(root, 'missing'), dataRoot: root, instance: 'test' }, 'alpha'),
error => error.exitCode === 3 && /registry/i.test(error.message));
});
+162
View File
@@ -0,0 +1,162 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, readdirSync, existsSync, symlinkSync, unlinkSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { reserveOrg, activateOrg } from '../../business/src/org-registry.mjs';
import { startTimeOf } from '../src/host.mjs';
import { humanEnv } from './helpers.mjs';
import { execFile, spawn } from 'node:child_process';
import { once } from 'node:events';
import { consoleStart, consoleStop } from '../src/org-console.mjs';
import { promisify } from 'node:util';
const exec = promisify(execFile);
async function fixture() {
const root = mkdtempSync(join(tmpdir(), 'recover-'));
const configDir = join(root, '.config', 'mosaic-test'), dataRoot = join(root, 'data');
mkdirSync(configDir, { mode: 0o700, recursive: true }); mkdirSync(dataRoot, { mode: 0o700 });
const ctx = { configDir, dataRoot, instance: 'test' };
const org = await reserveOrg(ctx, 'alpha'); await activateOrg(ctx, 'alpha');
const env = humanEnv({ HOME: root, MOSAIC_CONFIG: join(configDir, 'config.json') });
const moduleUrl = new URL('../src/cli.mjs', import.meta.url).href;
const code = `import { run } from ${JSON.stringify(moduleUrl)}; process.exitCode = await run(JSON.parse(process.argv[1]), undefined, {system: {dataRoot: process.argv[2]}});`;
const call = async (...args) => { const x = await exec(process.execPath, ['--input-type=module', '-e', code, JSON.stringify(args), dataRoot], { env }); return { out: { text: x.stdout }, err: { text: x.stderr } }; };
return { root, ctx, org, call };
}
const dead = 2147483647;
const json = (path, value) => writeFileSync(path, JSON.stringify(value), { mode: 0o600 });
test('console stop quarantines dead and wrong-process states without signaling', async () => {
const f = await fixture(), dir = join(f.ctx.dataRoot, 'console'); mkdirSync(dir, { mode: 0o700 });
for (const pid of [dead, process.pid]) {
json(join(dir, 'console.json'), { pid, startTime: '1', url: 'http://127.0.0.1:1/' });
const x = await f.call('console', 'stop');
assert.equal(JSON.parse(x.out.text).stopped, false);
assert.match(x.err.text, /stale/i);
assert.equal(existsSync(join(dir, 'console.json')), false);
}
assert.equal(readdirSync(dir).filter(x => x.startsWith('console.json.stale-')).length, 2);
});
test('live console from another entry path remains recorded and is not signalled', { timeout: 30000 }, async t => {
const f = await fixture(), dir = join(f.ctx.dataRoot, 'console'), alias = join(f.root, 'console.mjs');
mkdirSync(dir, { mode: 0o700 });
symlinkSync(new URL('../../webui/src/cli.mjs', import.meta.url).pathname, alias);
const child = spawn(process.execPath, [alias, 'serve', '--port', '0'], { env: humanEnv({ HOME: f.root, MOSAIC_CONFIG: join(f.ctx.configDir, 'config.json') }), stdio: ['ignore', 'pipe', 'pipe'] });
t.after(async () => { if (child.exitCode === null && child.signalCode === null) { const ended = once(child, 'exit'); child.kill('SIGTERM'); await ended; } });
const url = await new Promise((resolve, reject) => {
let output = ''; const timer = setTimeout(() => reject(Error('startup timeout')), 10000);
child.once('exit', () => { clearTimeout(timer); reject(Error('console exited')); });
child.stdout.on('data', b => { output += b; const m = /Mosaic Console: (http:\/\/127\.0\.0\.1:\d+\/)/.exec(output); if (m) { clearTimeout(timer); resolve(m[1]); } });
});
const record = { pid: child.pid, startTime: startTimeOf(child.pid), url };
json(join(dir, 'console.json'), record);
await assert.rejects(consoleStart(f.ctx), /console state exists/);
await assert.rejects(consoleStop(f.ctx), e => /another path/.test(e.message) && e.message.includes(alias));
assert.deepEqual(JSON.parse(readFileSync(join(dir, 'console.json'), 'utf8')), record);
assert.equal(startTimeOf(child.pid), record.startTime);
assert.equal(readdirSync(dir).filter(x => x.startsWith('console.json.stale-')).length, 0);
});
test('interrupted quarantine leaves no hard link that blocks retry', async () => {
const f = await fixture(), dir = join(f.ctx.dataRoot, 'console'), file = join(dir, 'console.json');
mkdirSync(dir, { mode: 0o700 }); json(file, { pid: dead, startTime: '1', url: 'http://127.0.0.1:1/' });
const moduleUrl = new URL('../src/org-console.mjs', import.meta.url).href;
const code = `import fs from 'node:fs'; import { syncBuiltinESMExports } from 'node:module';
for (const name of ['linkSync', 'renameSync']) { const real = fs[name]; fs[name] = (...args) => { const value = real(...args); if (String(args[1]).includes('console.json.stale-')) process.kill(process.pid, 'SIGKILL'); return value; }; } syncBuiltinESMExports();
const { consoleStop } = await import(${JSON.stringify(moduleUrl)}); await consoleStop(${JSON.stringify(f.ctx)});`;
const child = spawn(process.execPath, ['--input-type=module', '-e', code], { stdio: 'ignore' });
assert.equal((await once(child, 'exit'))[1], 'SIGKILL');
unlinkSync(join(dir, 'start.lock'));
assert.deepEqual(await consoleStop(f.ctx), { stopped: false });
assert.equal(existsSync(file), false);
assert.equal(readdirSync(dir).filter(x => x.startsWith('console.json.stale-')).length, 1);
});
test('lock recovery reports fences and recovers a dead clearer while refusing live or unreadable fences', async () => {
const f = await fixture(), lock = join(f.ctx.configDir, 'org-lifecycle-alpha.lock'), fence = `${lock}.clear`;
json(lock, { pid: dead, at: '2026-01-01T00:00:00Z' });
json(fence, { pid: process.pid, startTime: startTimeOf(process.pid) });
let shown = JSON.parse((await f.call('org', 'lock', 'show', 'alpha')).out.text);
assert.equal(shown.fence.pid, process.pid);
await assert.rejects(f.call('org', 'lock', 'clear', 'alpha'), new RegExp(`recovery is already in progress.*${process.pid}`));
writeFileSync(fence, '');
await assert.rejects(f.call('org', 'lock', 'clear', 'alpha'), e => e.message.includes(fence) && /remove.*hand/i.test(e.message));
unlinkSync(fence);
const registry = new URL('../../business/src/org-registry.mjs', import.meta.url).href;
const code = `import fs from 'node:fs'; import { syncBuiltinESMExports } from 'node:module';
const real = fs.writeFileSync; fs.writeFileSync = (...args) => { const value = real(...args); if (typeof args[0] === 'number') process.kill(process.pid, 'SIGKILL'); return value; }; syncBuiltinESMExports();
const { orgLock } = await import(${JSON.stringify(registry)}); await orgLock(${JSON.stringify(f.ctx)}, 'alpha', { clear: true });`;
const child = spawn(process.execPath, ['--input-type=module', '-e', code], { stdio: 'ignore' });
assert.equal((await once(child, 'exit'))[1], 'SIGKILL');
shown = JSON.parse((await f.call('org', 'lock', 'show', 'alpha')).out.text);
assert.equal(shown.fence.pid, child.pid);
assert.equal(shown.fence.alive, false);
await f.call('org', 'lock', 'clear', 'alpha');
assert.equal(existsSync(lock), false); assert.equal(existsSync(fence), false);
assert.equal(readdirSync(f.ctx.configDir).filter(x => x.startsWith('org-lifecycle-alpha.lock.clear.stale-')).length, 1);
});
test('org lock show and clear require a provably dead holder', async () => {
const f = await fixture(), path = join(f.ctx.configDir, 'org-lifecycle-alpha.lock');
let x = await f.call('org', 'lock', 'show', 'alpha');
assert.deepEqual(JSON.parse(x.out.text), { held: false });
x = await f.call('org', 'lock', 'clear', 'alpha');
assert.deepEqual(JSON.parse(x.out.text), { held: false, cleared: false });
json(path, { pid: process.pid, at: '2026-01-01T00:00:00Z' });
x = await f.call('org', 'lock', 'show', 'alpha');
assert.equal(JSON.parse(x.out.text).alive, true);
await assert.rejects(f.call('org', 'lock', 'clear', 'alpha'), /held|alive/i);
assert.equal(existsSync(path), true);
json(path, { pid: dead, at: '2026-01-01T00:00:00Z' });
x = await f.call('org', 'lock', 'show', 'alpha');
assert.equal(JSON.parse(x.out.text).alive, false);
await f.call('org', 'lock', 'clear', 'alpha');
assert.equal(existsSync(path), false);
});
test('bus recover preserves evidence for dead and wrong-process hosts without signals', async () => {
const f = await fixture(), dir = join(f.org.root, 'bus-host'); mkdirSync(dir, { mode: 0o700 });
for (const pid of [dead, process.pid]) {
const state = { pid, startTime: startTimeOf(pid) ?? '1', business: 'alpha', startedAt: '2026-01-01T00:00:00Z' };
json(join(dir, 'host.json'), state); json(join(dir, 'host.lock'), state);
const x = await f.call('bus', 'recover', 'alpha');
const result = JSON.parse(x.out.text);
assert.equal(result.recovered, true);
const receipt = JSON.parse(readFileSync(result.receipt, 'utf8'));
assert.equal(receipt.action, 'bus.recover');
assert.equal(receipt.stopped, true);
assert.equal(receipt.host.pid, pid);
assert.equal(existsSync(join(dir, 'host.json')), false);
assert.equal(existsSync(join(dir, 'host.lock')), false);
assert.ok(readdirSync(dir).some(x => x.startsWith('recovery-')));
}
assert.equal(process.pid > 0, true);
});
test('lock recovery refuses malformed or unreadable holders', async () => {
const f = await fixture(), path = join(f.ctx.configDir, 'org-lifecycle-alpha.lock');
for (const value of [{ pid: 0, at: '2026-01-01T00:00:00Z' }, { pid: dead }, null]) {
json(path, value);
await assert.rejects(f.call('org', 'lock', 'clear', 'alpha'));
assert.equal(existsSync(path), true);
}
});
test('bus recovery refuses a live writer or listening socket', async t => {
const { createServer } = await import('node:net');
const f = await fixture(), dir = join(f.org.root, 'bus-host'), bus = join(f.org.root, 'bus');
mkdirSync(dir, { mode: 0o700 }); mkdirSync(bus, { mode: 0o700 });
const state = { pid: dead, startTime: '1', business: 'alpha', startedAt: '2026-01-01T00:00:00Z' };
json(join(dir, 'host.json'), state); json(join(dir, 'host.lock'), state);
json(join(bus, 'writer.lock'), { pid: process.pid, at: '2026-01-01T00:00:00Z' });
await assert.rejects(f.call('bus', 'recover', 'alpha'), /writer.*alive/);
assert.equal(existsSync(join(dir, 'host.json')), true);
const { unlinkSync } = await import('node:fs'); unlinkSync(join(bus, 'writer.lock'));
const server = createServer(s => s.end());
await new Promise((resolve, reject) => server.once('error', reject).listen(join(bus, 'broker.sock'), resolve));
t.after(() => new Promise(resolve => server.close(resolve)));
await assert.rejects(f.call('bus', 'recover', 'alpha'), /socket/);
assert.equal(existsSync(join(dir, 'host.json')), true);
});
+29
View File
@@ -0,0 +1,29 @@
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');
info = JSON.parse(invoke('org', 'show', 'alpha', '--json')); const archived = JSON.parse(invoke('org', 'archive', 'alpha', '--if-revision', String(info.revision), '--json')); assert.equal(archived.revision, info.revision + 1);
assert.equal(JSON.parse(invoke('org', 'list', '--json'))[0].state, 'archived');
const restored = JSON.parse(invoke('org', 'restore', 'alpha', '--if-revision', String(archived.revision), '--json')); assert.equal(restored.revision, archived.revision + 1);
info = JSON.parse(invoke('org', 'show', 'alpha', '--json')); const receipt = JSON.parse(invoke('org', 'delete', 'alpha', '--confirm', 'alpha', '--if-revision', String(info.revision), '--json')); assert.equal(receipt.org, 'alpha'); assert.deepEqual(JSON.parse(invoke('org', 'list', '--json')), []);
});
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); });
+14 -4
View File
@@ -3,6 +3,9 @@ import assert from "node:assert/strict";
import { OTHER_TREE, run } from "../src/cli.mjs";
import { humanTransport } from "../src/transport.mjs";
import { humanEnv, io, tmp } from "./helpers.mjs";
import { mkdirSync } from 'node:fs';
import { join } from 'node:path';
import { reserveOrg, activateOrg } from '../../business/src/org-registry.mjs';
// Row 130 (#1625): the broker accepts the human and tester proofs only from
// the tree the bus runs from, so a refusal with human-required or
@@ -11,6 +14,13 @@ import { humanEnv, io, tmp } from "./helpers.mjs";
// the code on stderr, and the real transport parses it.
const refusing = (code) => () => ({ status: 1, stdout: "", stderr: `${code}\n` });
const HINT = `mosaic: ${OTHER_TREE}\n`;
async function orgFixture(t) {
const root = tmp(t), dataRoot = join(root, 'data'), configDir = join(root, 'mosaic-test');
mkdirSync(dataRoot, { mode: 0o700 }); mkdirSync(configDir, { mode: 0o700 });
const ctx = { dataRoot, configDir, instance: 'test' };
await reserveOrg(ctx, 'demo'); await activateOrg(ctx, 'demo');
return { dataRoot, env: humanEnv({ MOSAIC_CONFIG: join(configDir, 'config.json') }) };
}
test("a human command refused with human-required prints the other-tree line and exits 3", async (t) => {
const x = io();
@@ -22,15 +32,15 @@ test("a human command refused with human-required prints the other-tree line and
});
test("a --tester command refused with tester-required prints the other-tree line and exits 3", async (t) => {
const x = io({ ...humanEnv(), CLAUDECODE: "1" });
const code = await run(["agents", "--tester", "--business", "demo"], x, { system: { dataRoot: tmp(t) }, testerSpawn: refusing("tester-required") });
const f = await orgFixture(t), x = io({ ...f.env, CLAUDECODE: "1" });
const code = await run(["agents", "--tester", "--business", "demo"], x, { system: { dataRoot: f.dataRoot }, testerSpawn: refusing("tester-required") });
assert.equal(code, 3);
assert.equal(x.err.text, `mosaic: tester-required\n${HINT}`);
});
test("other exit-3 refusals print no other-tree line", async (t) => {
const off = io({ ...humanEnv(), CLAUDECODE: "1" });
assert.equal(await run(["agents", "--tester", "--business", "demo"], off, { system: { dataRoot: tmp(t) }, testerSpawn: refusing("tester-off") }), 3);
const f = await orgFixture(t), off = io({ ...f.env, CLAUDECODE: "1" });
assert.equal(await run(["agents", "--tester", "--business", "demo"], off, { system: { dataRoot: f.dataRoot }, testerSpawn: refusing("tester-off") }), 3);
assert.equal(off.err.text, "mosaic: tester-off\n");
const agent = io({ ...humanEnv(), CLAUDECODE: "1" });
@@ -241,3 +241,45 @@ test('#774 R2 the stand-in key never reaches the launch log, the authorization r
}
f.valueFree();
});
test('a removed catalogue model is refused by final admission before newRun', { skip, timeout: 60000 }, async t => {
let reads=0;
const offered={key:'zai-fixture-id',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-id',label:'fixture-id',available:true,reason:null,harnesses:['pi']};
const modelCatalogue={invalidate(){},read:async()=>({status:'ready',observedAt:'2026-11-06T00:00:00.000Z',models:++reads===1?[offered]:[],notices:[]})};
const f=await managedLaunchFixture(t,{modelCatalogue});
editBusiness(f,doc=>{doc.plugins['model-provider']=['cliproxy'];doc.launch.models['zai-fixture-id']={provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'};doc.roles.coder.vars.model='zai-fixture-id';});
const result=await f.launcher.fromSession(chief(f),'coder');
assert.deepEqual(result,{ok:false,error:'model-provider-unavailable'});
assert.equal(reads,2);assert.equal(f.trace.preflightAttempts,1);assert.equal(f.runDirectories().length,0);assert.equal(f.trace.bindings.length,0);
});
test('fixture catalogue route persists, launches, and then refuses after model removal', { skip, timeout: 60000 }, async t => {
let present=true;
const offered={key:'zai-fixture-id',module:'cliproxy',upstream:'zai',provider:'zai',model:'fixture-id',label:'z.ai / fixture-id',available:true,reason:null,harnesses:['pi']};
const modelCatalogue={invalidate(){},read:async()=>({status:'ready',observedAt:'2026-11-06T00:00:00.000Z',models:present?[offered]:[],notices:[]})};
const f=await managedLaunchFixture(t,{modelCatalogue});
editBusiness(f,doc=>{doc.plugins['model-provider']=['cliproxy'];});
const human=f.bus.broker.bindHuman({business:'acme',human:'jason',via:'cli',outsideAgent:true});
const before=await f.bus.broker.request(human,{verb:'routes'});
assert.equal(before.models.includes(offered.key),false);assert.equal(before.catalogue.models[0].key,offered.key);
const route={harness:'pi',model:offered.key,thinking:'low',fallback:null};
await f.bus.broker.request(human,{verb:'routes.set',args:{revision:before.revision,routes:{coder:route,reviewer:route}}});
const doc=JSON.parse(fs.readFileSync(join(f.root,'config/businesses/acme.json'),'utf8'));
assert.deepEqual(doc.launch.models[offered.key],{provider:'zai',model:'fixture-id',module:'cliproxy',upstream:'zai'});
const cap=chief(f);const launched=await f.launcher.fromSession(cap,'coder');
assert.equal(launched.ok,true,JSON.stringify(launched));
const session=f.sessions().find(x=>x.run===launched.result.run);assert.equal(session.provider,'zai');assert.equal(session.model,'fixture-id');
present=false;const after=await f.bus.broker.request(human,{verb:'routes'});
assert.equal(after.instances.reviewer.model,offered.key);assert.equal(after.catalogue.models.length,0);
assert.deepEqual(await f.launcher.fromSession(cap,'reviewer'),{ok:false,error:'model-provider-unavailable'});
});
test('business revision changed during final limit check refuses before creating a run', { skip, timeout: 60000 }, async t => {
let currentModel='fixture-old';
const modelCatalogue={invalidate(){},read:async()=>({status:'ready',observedAt:'2026-11-06T00:00:00.000Z',models:[{key:`zai-${currentModel}`,module:'cliproxy',upstream:'zai',provider:'zai',model:currentModel,label:currentModel,available:true,reason:null,harnesses:['pi']}],notices:[]})};
const f=await managedLaunchFixture(t,{modelCatalogue});
editBusiness(f,doc=>{doc.plugins['model-provider']=['cliproxy'];doc.launch.models['legacy-fixture']={provider:'zai',model:'fixture-old'};doc.roles.coder.vars.model='legacy-fixture';});
const human=f.bus.broker.bindHuman({business:'acme',human:'jason',via:'cli',outsideAgent:true});
const original=f.bus.mayLaunch; let count=0;
f.bus.mayLaunch=async request=>{const answer=await original(request);if(++count===2){currentModel='fixture-new';const view=f.bus.broker.request(human,{verb:'limits'});const models=structuredClone(view.models);models['legacy-fixture'].model=currentModel;await f.bus.broker.request(human,{verb:'limits.set',args:{revision:view.revision,models}});}return answer;};
assert.deepEqual(await f.launcher.fromSession(chief(f),'coder'),{ok:false,error:'model-provider-unavailable'});
assert.equal(f.runDirectories().length,0);assert.equal(f.trace.bindings.length,0);
});
+146
View File
@@ -0,0 +1,146 @@
// `mosaic run evidence`: a fabricated run in a scratch HOME, verified, then
// tampered with. Nothing here starts a bus.
import test from 'node:test';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { appendFileSync, chmodSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { reserveOrg } from '../../business/src/org-registry.mjs';
import { launchLogFile, runsDir } from '../../seat/src/session.mjs';
import { DEV_OWNER } from '../src/dev-local-owner.mjs';
import { runCommand } from '../src/run-evidence.mjs';
import { io } from './helpers.mjs';
const HOME = mkdtempSync(join(tmpdir(), 'runev'));
process.env.HOME = HOME;
test.after(() => rmSync(HOME, { recursive: true, force: true }));
const sha = s => createHash('sha256').update(s).digest('hex');
const THREAD = '0b6f1c2e-3d4a-4b5c-8d6e-7f8091a2b3c4';
const OTHER = '11111111-2222-4333-8444-555555555555';
let n = 0;
// An instance with org alpha and one finished run for THREAD.
async function world({ run = 'r0123456789ab' } = {}) {
const name = `ev${++n}`;
const env = { PATH: process.env.PATH, HOME, MOSAIC_CONFIG: join(HOME, '.config', `mosaic-${name}`, 'config.json') };
mkdirSync(dirname(env.MOSAIC_CONFIG), { recursive: true, mode: 0o700 });
const dataRoot = join(HOME, `data-${name}`); mkdirSync(dataRoot, { mode: 0o700 });
const org = await reserveOrg({ configDir: dirname(env.MOSAIC_CONFIG), dataRoot, instance: name, principal: DEV_OWNER }, 'alpha');
const runDir = join(runsDir(org.root, 'alpha'), run);
mkdirSync(join(runDir, 'bundle'), { recursive: true, mode: 0o700 });
mkdirSync(join(runDir, 'home/.pi/agent'), { recursive: true, mode: 0o700 });
mkdirSync(join(runDir, 'turns'), { mode: 0o700 });
const put = (p, text) => { writeFileSync(join(runDir, p), text, { mode: 0o600 }); return sha(text); };
put('session.json', JSON.stringify({ business: 'alpha', instance: name, thread: THREAD, run }));
const files = { 'prompt.md': put('bundle/prompt.md', '# prompt\n'), '../home/.pi/agent/models.json': put('home/.pi/agent/models.json', '{}\n') };
put('bundle/manifest.json', JSON.stringify({ bundleVersion: 1, run, files }));
put('turns/0001.stderr', '');
const log = launchLogFile(org.root, 'alpha');
const lines = [
{ at: '2026-11-07T00:00:00.000Z', event: 'launch', instance: name, thread: OTHER, session: 's0', run: 'rffffffffffff' },
{ at: '2026-11-07T00:00:01.000Z', event: 'launch', instance: name, thread: THREAD, session: 's1', run, model: 'fixture-deterministic', harness: 'pi', manifest: join(runDir, 'bundle/manifest.json') },
{ at: '2026-11-07T00:00:02.000Z', event: 'end', instance: name, thread: THREAD, session: 's1', run, reason: 'idle', exitCode: 0 },
].map(l => JSON.stringify(l));
writeFileSync(log, `${lines[0]}\nnot json\n${lines[1]}\n${lines[2]}\n`, { mode: 0o600 });
return { env, dataRoot, runDir, log, launchLine: lines[1], name };
}
async function evidence(w, args, deps = {}) {
const x = io(w.env);
let error = null;
try { await runCommand(['evidence', 'alpha', ...args], x, { system: { dataRoot: w.dataRoot }, ...deps }); } catch (e) { error = e; }
return { error, out: x.out.text, json: args.includes('--json') && x.out.text ? JSON.parse(x.out.text) : null };
}
test('a complete run verifies and names its org, run and launch line', async () => {
const w = await world();
const { error, json } = await evidence(w, ['--thread', THREAD, '--verify', '--json']);
assert.equal(error, null, error?.message);
assert.equal(json.verified, true);
assert.deepEqual([json.org.id, json.org.state, json.instance, json.launchLog.malformed], ['alpha', 'reserved', w.name, 1]);
assert.equal(json.runs.length, 1);
const [r] = json.runs;
assert.deepEqual([r.run, r.launch.file, r.launch.line, r.launch.sha256], ['r0123456789ab', w.log, 3, sha(w.launchLine)]);
assert.deepEqual(r.end.map(e => [e.line, e.reason]), [[4, 'idle']]);
assert.equal(r.result.path, join(w.runDir, 'session.json'));
assert.deepEqual(r.snapshot.files.map(f => [f.name, f.ok]), [['prompt.md', true], ['../home/.pi/agent/models.json', true]]);
assert.deepEqual(r.stderr.map(s => [s.path, s.sha256]), [[join(w.runDir, 'turns/0001.stderr'), sha('')]]);
const text = await evidence(w, ['--thread', THREAD]);
assert.equal(text.error, null);
assert.match(text.out, /not verified \(pass --verify\)/);
});
test('a changed bundle file fails verification', async () => {
const w = await world();
appendFileSync(join(w.runDir, 'bundle/prompt.md'), 'edited\n');
const { error, json } = await evidence(w, ['--thread', THREAD, '--verify', '--json']);
assert.equal(error?.exitCode, 1);
assert.equal(json.verified, false);
assert.deepEqual(json.runs[0].problems, ['bundle file prompt.md: digest differs from the manifest']);
const plain = await evidence(w, ['--thread', THREAD, '--json']);
assert.equal(plain.error, null, 'without --verify a digest is not checked');
});
test('missing stderr, a loose session file and a linked session file fail', async () => {
let w = await world();
unlinkSync(join(w.runDir, 'turns/0001.stderr'));
let r = await evidence(w, ['--thread', THREAD, '--verify', '--json']);
assert.equal(r.error?.exitCode, 1);
assert.deepEqual(r.json.runs[0].problems, ['no turns/*.stderr']);
w = await world();
chmodSync(join(w.runDir, 'session.json'), 0o644);
r = await evidence(w, ['--thread', THREAD, '--verify', '--json']);
assert.deepEqual(r.json.runs[0].problems, ['session.json: mode 644, expected 600']);
w = await world();
writeFileSync(join(w.dataRoot, 'decoy.json'), JSON.stringify({ thread: THREAD, run: 'r0123456789ab' }), { mode: 0o600 });
unlinkSync(join(w.runDir, 'session.json')); symlinkSync(join(w.dataRoot, 'decoy.json'), join(w.runDir, 'session.json'));
r = await evidence(w, ['--thread', THREAD, '--verify', '--json']);
assert.equal(r.error?.exitCode, 1);
assert.match(r.json.runs[0].problems[0], /^session\.json: cannot open \(ELOOP\)$/);
w = await world();
rmSync(w.runDir, { recursive: true });
r = await evidence(w, ['--thread', THREAD, '--verify', '--json']);
assert.deepEqual(r.json.runs[0].problems, ['run directory missing']);
});
test('a manifest file outside the run directory or a mismatched run fails', async () => {
const w = await world();
const manifest = join(w.runDir, 'bundle/manifest.json');
writeFileSync(join(w.dataRoot, 'outside'), 'x', { mode: 0o600 });
rmSync(manifest);
writeFileSync(manifest, JSON.stringify({ run: 'r0123456789ab', files: { '../../../../../outside': sha('x') } }), { mode: 0o600 });
let r = await evidence(w, ['--thread', THREAD, '--verify', '--json']);
assert.deepEqual(r.json.runs[0].problems, ['bundle file ../../../../../outside: outside the run directory']);
rmSync(manifest);
writeFileSync(manifest, JSON.stringify({ run: 'rbbbbbbbbbbbb', files: {} }), { mode: 0o600 });
r = await evidence(w, ['--thread', THREAD, '--verify', '--json']);
assert.match(r.json.runs[0].problems[0], /names run rbbbbbbbbbbbb/);
});
test('no launch for the thread, an unknown org and a usage error', async () => {
const w = await world();
let r = await evidence(w, ['--thread', '99999999-2222-4333-8444-555555555555', '--json']);
assert.equal(r.error?.exitCode, 2);
assert.deepEqual(r.json.runs, []);
const x = io(w.env);
await assert.rejects(runCommand(['evidence', 'beta', '--thread', THREAD], x, { system: { dataRoot: w.dataRoot } }), e => e.exitCode === 2 && e.code === 'org-unknown');
await assert.rejects(runCommand(['evidence', 'alpha'], x, { system: { dataRoot: w.dataRoot } }), e => e.exitCode === 4);
await assert.rejects(runCommand(['evidence', 'alpha', '--thread', THREAD], io({ PATH: process.env.PATH, HOME }), { system: { dataRoot: w.dataRoot } }), e => e.exitCode === 3 && e.code === 'run-evidence-instance-refused');
});
test('a thread name resolves through the bus with the org as business', async () => {
const w = await world();
const seen = [];
const callFor = p => { seen.push([p.values['--business'], p.flags.has('--tester')]); return async verb => verb === 'thread.list' ? [{ id: THREAD, name: 'chat', project: 'p1' }, { id: OTHER, name: 'other', project: 'p1' }] : [{ id: 'p1', name: 'work' }]; };
let r = await evidence(w, ['--thread', 'work/chat', '--verify', '--json', '--tester'], { callFor });
assert.equal(r.error, null, r.error?.message);
assert.equal(r.json.thread.id, THREAD);
r = await evidence(w, ['--thread', 'chat', '--verify', '--json'], { callFor });
assert.equal(r.error, null, r.error?.message);
assert.equal(r.json.thread.label, 'chat');
assert.deepEqual(seen, [['alpha', true], ['alpha', false]]);
});
+13 -7
View File
@@ -1,7 +1,7 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {execFileSync,spawnSync,spawn} from 'node:child_process';
import {symlinkSync,unlinkSync,mkdtempSync,mkdirSync,writeFileSync,readFileSync,cpSync,rmSync,chmodSync,existsSync,statSync,readdirSync,lstatSync,readlinkSync} from 'node:fs';
import {symlinkSync,unlinkSync,mkdtempSync,mkdirSync,writeFileSync,readFileSync,cpSync,rmSync,renameSync,chmodSync,existsSync,statSync,readdirSync,lstatSync,readlinkSync} from 'node:fs';
import {createHash} from 'node:crypto';
import {tmpdir} from 'node:os';
import {join,dirname} from 'node:path';
@@ -11,6 +11,7 @@ import {createServer} from 'node:net';
import {createOwnedRunner} from './fixtures/owned-child.mjs';
import {pathToFileURL} from 'node:url';
import {fixture,notifyConfig,BUSINESSES} from './helpers.mjs';
import {reserveOrg,activateOrg} from '../../business/src/org-registry.mjs';
const ROOT=fileURLToPath(new URL('../../..',import.meta.url)).replace(/\/$/,'');
const git=(cwd,...args)=>execFileSync('git',['-c','user.name=fixture','-c','user.email=fixture@invalid','-c','commit.gpgsign=false','-c','core.hooksPath=/dev/null','-C',cwd,...args],{encoding:'utf8'}).trim();
export function setup(t, realPin=false){
@@ -142,13 +143,18 @@ test('real pinned install loads bus and Console; same-copy mosaic talk passes pr
const copied=(await s.run('stack-copy.sh',['copy',s.id,'--copies',s.copies]));assert.equal(copied.status,0,copied.stderr);
const root=join(s.copies,s.id),url=f=>pathToFileURL(join(root,f)).href;
const {checkEnginePin}=await import(url('packages/conversation/src/pi-pin.mjs'));assert.equal(checkEnginePin(root).version,'0.85.1');
const f=fixture(join(s.root,'home'));notifyConfig(f.dataRoot,'acme',null);
const home=join(s.root,'home'),f=fixture(home),configDir=join(home,'.config','mosaic-pin');
mkdirSync(join(home,'.config'),{recursive:true});renameSync(join(home,'config'),configDir);chmodSync(configDir,0o700);
writeFileSync(join(f.dataRoot,'instance.json'),JSON.stringify({instanceVersion:1,name:'pin'}));
const ctx={configDir,dataRoot:f.dataRoot,instance:'pin'};
const acme=await reserveOrg(ctx,'acme');await activateOrg(ctx,'acme');notifyConfig(acme.root,'acme',null);
const demo=await reserveOrg(ctx,'demo');await activateOrg(ctx,'demo');
// Only synthetic configuration, no inherited credentials/agent environment.
const env={...s.env,MOSAIC_CONFIG:f.config};
const env={...s.env,HOME:home,MOSAIC_CONFIG:join(configDir,'config.json'),MOSAIC_ROLES_DIR:f.roles};
const host=spawn(join(root,'scripts/mosaic'),['bus','start','acme'],{env,stdio:['ignore','pipe','pipe']});let hostOut='',hostErr='';
host.stdout.on('data',c=>hostOut+=c);host.stderr.on('data',c=>hostErr+=c);
try{await until(()=>hostOut.includes('bus host up')||host.exitCode!==null);assert.match(hostOut,/bus host up/,hostErr);}
finally{if(host.exitCode===null&&host.signalCode===null){const done=once(host,'close');host.kill('SIGTERM');await done;}}
try{await until(()=>hostOut.includes('\"org\":\"acme\"')||host.exitCode!==null);assert.match(hostOut,/\"org\":\"acme\"/,hostErr);}
finally{if(host.exitCode===null&&host.signalCode===null){const done=once(host,'close');const stop=spawnSync(join(root,'scripts/mosaic'),['bus','stop','acme'],{env,encoding:'utf8'});assert.equal(stop.status,0,stop.stderr);await done;}}
const web=spawn(process.execPath,[join(root,'packages/webui/src/cli.mjs'),'serve','--port','0','--business','acme','--board','http://127.0.0.1:9'],{env,stdio:['ignore','pipe','pipe']});let webOut='',webErr='';
web.stdout.on('data',c=>webOut+=c);web.stderr.on('data',c=>webErr+=c);
try{await until(()=>/Mosaic Console: http:/.test(webOut)||web.exitCode!==null);const port=webOut.match(/127\.0\.0\.1:(\d+)/)?.[1];assert.ok(port,webErr);assert.equal((await fetch(`http://127.0.0.1:${port}/healthz`)).status,200);}
@@ -163,7 +169,7 @@ test('real pinned install loads bus and Console; same-copy mosaic talk passes pr
const real=await import(humanURL);
mock.module(humanURL,{namedExports:{readProcess:real.readProcess,verifyHuman:(proof,options)=>real.verifyHuman(proof,{...options,readProcess:pid=>({...real.readProcess(pid),ppid:1})})}});
const {startBroker}=await import(${JSON.stringify(url('packages/bus/src/runtime.mjs'))});
const runtime=await startBroker({dataRoot:${JSON.stringify(f.dataRoot)},businesses:${JSON.stringify(BUSINESSES)}});
const runtime=await startBroker({dataRoot:${JSON.stringify(demo.root)},businesses:${JSON.stringify(BUSINESSES)}});
const broker=runtime.broker;
const pm=broker.bindLaunch({business:'demo',role:'pm',run:'pm-fixture',harness:'pi'});broker.request(pm,{verb:'role.claim'});
const request=broker.request.bind(broker);broker.request=(cap,r)=>{const result=request(cap,r);if(r.verb==='message.send'&&r.args.to==='pm')request(pm,{verb:'message.send',args:{to:'human',body:'same-copy reply',in_reply_to:result.id}});return result;};
@@ -172,7 +178,7 @@ test('real pinned install loads bus and Console; same-copy mosaic talk passes pr
`);
const proof=spawn(process.execPath,['--experimental-test-module-mocks',proofScript],{env,stdio:['ignore','pipe','pipe']});let proofOut='',proofErr='';
proof.stdout.on('data',c=>proofOut+=c);proof.stderr.on('data',c=>proofErr+=c);
const socket=join(f.dataRoot,'bus','broker.sock');
const socket=join(demo.root,'bus','broker.sock');
try{
await until(()=>proofOut.includes('proof runtime ready')||proof.exitCode!==null);assert.match(proofOut,/proof runtime ready/,proofErr);
const result=await childResult(join(root,'scripts/mosaic'),['talk','pm','hello','--business','demo','--wait','1'],env);
+28 -31
View File
@@ -12,6 +12,8 @@ import { createHash } from "node:crypto";
import { join, relative } from "node:path";
import { fileURLToPath } from "node:url";
import { LiveSessionGuard } from "../../conversation/src/guard.mjs";
import { reserveOrg, activateOrg } from "../../business/src/org-registry.mjs";
import { hostStatus } from "../src/host.mjs";
import { businessDoc, rolesCopy, writeJson } from "../../business/tests/helpers.mjs";
import { REPO_SKILLS } from "../../harness/tests/helpers.mjs";
import { humanEnv, notifyConfig, tmp } from "./helpers.mjs";
@@ -386,7 +388,7 @@ async function until(check, ms) {
// One named instance in `home`, ready for `bus start acme --no-launch`: bootstrap, a business file in
// its config dir, its own roles copy, the skills its roles name, and a notifier with no binding.
function hostInstance(home, root, name) {
async function hostInstance(home, root, name) {
assert.equal(bootstrap(home, "--instance", name).code, 0);
const cfg = cfgOf(home, name), dataRoot = join(home, `.mosaic-${name}`);
const files = join(root, `${name}-files`);
@@ -395,60 +397,55 @@ function hostInstance(home, root, name) {
for (const r of Object.values(doc.roles)) r.vars = { harness: "pi", model: "glm-5.3-flash", thinking: "high", ...r.vars };
writeJson(join(home, ".config", `mosaic-${name}`, "businesses", "acme.json"), { ...doc, plugins: { ...doc.plugins, harness: ["pi", "claude-code"] } });
cpSync(REPO_SKILLS, join(dataRoot, "skills-enabled"), { recursive: true });
notifyConfig(dataRoot, "acme", null);
return { name, cfg, dataRoot, env: homeEnv(home, { MOSAIC_CONFIG: cfg, MOSAIC_ROLES_DIR: roles }) };
const ctx = { configDir: join(home, ".config", `mosaic-${name}`), dataRoot, instance: name };
const org = await reserveOrg(ctx, "acme"); await activateOrg(ctx, "acme");
notifyConfig(org.root, "acme", null);
return { name, cfg, dataRoot, orgRoot: org.root, env: homeEnv(home, { MOSAIC_CONFIG: cfg, MOSAIC_ROLES_DIR: roles }) };
}
test("T-I5 two real bus hosts, one per instance, run at once and each answers from its own socket under its own root", async (t) => {
const root = tmp(t, "si-");
const home = join(root, "h");
mkdirSync(home);
const both = ["alpha", "beta"].map((name) => hostInstance(home, root, name));
const both = await Promise.all(["alpha", "beta"].map((name) => hostInstance(home, root, name)));
for (const inst of both) {
const sock = join(inst.dataRoot, "bus", "broker.sock");
// The kernel's limit for a Unix socket path is 107 bytes (the row 177 lesson).
const sock = join(inst.orgRoot, "bus", "broker.sock");
assert.ok(Buffer.byteLength(sock) <= 107, `socket path is ${Buffer.byteLength(sock)} bytes: ${sock}`);
inst.sock = sock;
inst.child = spawn(MOSAIC, ["bus", "start", "acme", "--no-launch"], { env: inst.env, stdio: ["ignore", "pipe", "pipe"] });
t.after(() => inst.child.exitCode === null && inst.child.kill("SIGKILL"));
t.after(() => { run(MOSAIC, ["bus", "stop", "acme"], inst.env); if (inst.child.exitCode === null) inst.child.kill("SIGTERM"); });
inst.out = "";
inst.child.stdout.on("data", (b) => (inst.out += b));
inst.child.stderr.on("data", (b) => (inst.out += b));
}
for (const inst of both) await until(() => /bus host up: business acme.*\n/.test(inst.out) || inst.child.exitCode !== null, 30000);
for (const inst of both) await until(() => inst.out.includes('"org":"acme"') || inst.child.exitCode !== null, 30000);
for (const inst of both) {
assert.match(inst.out, new RegExp(`bus host up: business acme, socket ${inst.sock}, notifier off, launches disabled\n`), inst.out);
const receipt = JSON.parse(inst.out);
assert.equal(receipt.org, "acme"); assert.equal(receipt.socket, inst.sock);
inst.hostPid = receipt.pid;
assert.ok(lstatSync(inst.sock).isSocket());
}
for (const inst of both) {
const r = run(MOSAIC, ["bus", "status", "--json"], inst.env);
const r = run(MOSAIC, ["bus", "status", "acme", "--json"], inst.env);
assert.equal(r.code, 0, r.err);
const s = JSON.parse(r.out);
assert.equal(s.host.business, "acme");
assert.equal(s.host.pid, inst.child.pid);
assert.equal(s.host.live, true);
assert.equal(s.socket, true);
// The broker process under the host holds the writer lock.
assert.equal(s.writerLock.live, true);
inst.lockPid = s.writerLock.pid;
const status = JSON.parse(r.out);
assert.equal(status.org, "acme"); assert.equal(status.instance, inst.name);
assert.equal(status.pid, inst.hostPid); assert.equal(status.healthy, true);
const state = hostStatus(inst.orgRoot);
assert.equal(state.writerLock.live, true); inst.lockPid = state.writerLock.pid;
}
assert.notEqual(both[0].child.pid, both[1].child.pid);
assert.notEqual(both[0].hostPid, both[1].hostPid);
assert.notEqual(both[0].lockPid, both[1].lockPid);
// Stopping alpha leaves beta up.
const [alpha, beta] = both;
const exitA = once(alpha.child, "exit");
const stopA = run(MOSAIC, ["bus", "stop"], alpha.env);
assert.equal(stopA.code, 0, stopA.err);
assert.match(stopA.out, new RegExp(`stopped bus host for acme \\(pid ${alpha.child.pid}\\)`));
const stopA = run(MOSAIC, ["bus", "stop", "acme"], alpha.env);
assert.equal(stopA.code, 0, stopA.err); assert.equal(JSON.parse(stopA.out).stopped, true);
assert.equal((await exitA)[0], 0, alpha.out);
assert.equal(beta.child.exitCode, null);
const still = JSON.parse(run(MOSAIC, ["bus", "status", "--json"], beta.env).out);
assert.equal(still.host.pid, beta.child.pid);
assert.equal(still.writerLock.pid, beta.lockPid);
assert.equal(still.writerLock.live, true);
assert.equal(existsSync(join(alpha.dataRoot, "bus", "writer.lock")), false);
const still = JSON.parse(run(MOSAIC, ["bus", "status", "acme", "--json"], beta.env).out);
assert.equal(still.pid, beta.hostPid); assert.equal(still.healthy, true);
assert.equal(hostStatus(beta.orgRoot).writerLock.pid, beta.lockPid);
assert.equal(existsSync(join(alpha.orgRoot, "bus", "writer.lock")), false);
const exitB = once(beta.child, "exit");
assert.equal(run(MOSAIC, ["bus", "stop"], beta.env).code, 0);
assert.equal(run(MOSAIC, ["bus", "stop", "acme"], beta.env).code, 0);
assert.equal((await exitB)[0], 0, beta.out);
});
+8 -5
View File
@@ -600,6 +600,9 @@ test('#385 R2-F1 controls: library args without --send and a factory args file p
// checkout's own case2-unit.sh is A4's RENDER, both into an owned unit directory with fake getent and
// systemctl.
const BASE_REV='09a847eda';
// The real renderer requires a root-owned node path; the test runner's
// process.execPath may be an unprivileged toolchain installation.
const RENDER_NODE='/usr/bin/node';
let baseRenderer;
const baseCase2=()=>{
if(baseRenderer)return baseRenderer;
@@ -620,7 +623,7 @@ function case2Host(e,dir){
writeFileSync(join(bin,'systemctl'),`#!/bin/sh\necho "$*" >> '${log}'\n[ "$1" != is-active ] || exit 3\n`,{mode:0o755});
const env={CASE2_UNIT_DIR:dir,CASE2_GETENT:join(bin,'getent'),CASE2_SYSTEMCTL:join(bin,'systemctl')};
const serve=['--port','443','--business','acme','--public-host','books.example','--auth-header','X-Auth-User','--allow-user','melanie','--proof-header','X-Proxy-Proof','--proof-file',proof];
const args=(copy=B,extra=[])=>['--user','fixture','--home',home,'--business','acme','--copy',copy,'--node',NODE,...extra,'--',...serve];
const args=(copy=B,extra=[])=>['--user','fixture','--home',home,'--business','acme','--copy',copy,'--node',RENDER_NODE,...extra,'--',...serve];
const render=(script,a)=>{const r=spawnSync('bash',[script,...a],{env:{PATH:e.env.PATH,...env},encoding:'utf8'});return {code:r.status,out:r.stdout,err:r.stderr}};
const legacy=()=>{const a=args();a.splice(a.indexOf('melanie')+1,0,'--send');const r=render(baseCase2(),a);assert.equal(r.code,0,r.err);return r};
const bytes=()=>Object.fromEntries(readdirSync(dir).sort().map((n)=>[n,readFileSync(join(dir,n),'utf8')]));
@@ -632,12 +635,12 @@ function case2Host(e,dir){
test('#772: the real Case 2 renderer re-pins and rolls back all three factory units with the saved --proxy args',async(t)=>{
const e=await useed(t),h=case2Host(e,join(e.root,'case2','units')),script=join(checkout(C1),'scripts','case2-unit.sh');
const args=(copy)=>['--user','fixture','--home',h.home,'--business','acme','--copy',copy,'--node',NODE,'--profile','factory','--proxy','--','--port','443','--business','acme'];
const args=(copy)=>['--user','fixture','--home',h.home,'--business','acme','--copy',copy,'--node',RENDER_NODE,'--profile','factory','--proxy','--','--port','443','--business','acme'];
const first=h.render(script,args(B));assert.equal(first.code,0,first.out+first.err);
const before=h.bytes();
const next=h.render(script,args(C1));assert.equal(next.code,0,next.out+next.err);
for(const [u,text] of Object.entries(h.bytes()))assert.ok(text.includes(`WorkingDirectory=${e.hc}/${C1}\n`),u);
assert.ok(h.bytes()[PROXY].includes(`ExecStart=${NODE} ${e.hc}/${C1}/packages/model-proxy/src/run.mjs\n`));
assert.ok(h.bytes()[PROXY].includes(`ExecStart=${RENDER_NODE} ${e.hc}/${C1}/packages/model-proxy/src/run.mjs\n`));
const changed=h.bytes(),n=h.syslog().length;
const missing=h.render(script,args(C1).filter((x)=>x!=='--proxy'));assert.equal(missing.code,1,missing.out+missing.err);
assert.deepEqual(h.bytes(),changed);assert.equal(h.syslog().length,n);
@@ -653,7 +656,7 @@ test('#385 R3-F1: an installed pre-#385 library Console unit with --send refuses
const e=await useed(t),h=case2Host(e,join(e.root,'case2','units'));
h.legacy();
const c=readFileSync(h.con,'utf8');
assert.ok(c.includes(`\nExecStart=${NODE} ${e.hc}/${B}/packages/webui/src/cli.mjs serve --port 443 --business acme --public-host books.example --auth-header X-Auth-User --allow-user melanie --send --proof-header X-Proxy-Proof --proof-file `),c);
assert.ok(c.includes(`\nExecStart=${RENDER_NODE} ${e.hc}/${B}/packages/webui/src/cli.mjs serve --port 443 --business acme --public-host books.example --auth-header X-Auth-User --allow-user melanie --send --proof-header X-Proxy-Proof --proof-file `),c);
// The operator removed the args file's --send line, as R3's section 7 said, so read_args passes.
h.use(h.args().join('\n')+'\n');
assert.ok(!e.argsText.includes('--send'));
@@ -661,7 +664,7 @@ test('#385 R3-F1: an installed pre-#385 library Console unit with --send refuses
// The premise: the current renderer, as A4 runs it, refuses the installed legacy unit and writes nothing.
const p=h.render(join(checkout(C1),'scripts','case2-unit.sh'),h.args(C1));
assert.equal(p.code,1,p.out+p.err);
assert.ok(p.err.startsWith(`case2-unit: installed ${h.con}: ExecStart= must be exactly once in [Service], as ExecStart=${NODE} ${e.hc}/${B}/packages/webui/src/cli.mjs serve --port 443 `),p.err);
assert.ok(p.err.startsWith(`case2-unit: installed ${h.con}: ExecStart= must be exactly once in [Service], as ExecStart=${RENDER_NODE} ${e.hc}/${B}/packages/webui/src/cli.mjs serve --port 443 `),p.err);
assert.deepEqual(h.bytes(),before);assert.equal(h.syslog().length,sys);
for(const [why,run] of [['upgrade',()=>upgrade(e,C1)],['rollback',()=>drive(e,B,'rollback','--instance','dev','--to',B,...TO)]]){
const n=existsSync(e.slog)?slog(e).length:0;
+3 -4
View File
@@ -12,7 +12,7 @@ import { main } from "../src/cli.mjs";
import { BusError } from "../../bus/src/broker.mjs";
import { startBroker } from "../../bus/src/runtime.mjs";
import { systemFor, writeJson } from "../../business/tests/helpers.mjs";
import { fixture, humanEnv, io, tmp } from "./helpers.mjs";
import { fixture, humanEnv, io, tmp, orgFixture } from "./helpers.mjs";
const none = { authority: { withinRole: ["message.send"], crossRole: [] } };
const inAgent = (env) => io({ ...env, CLAUDECODE: "1", CLAUDE_CODE_ENTRYPOINT: "sdk-ts" });
@@ -158,9 +158,8 @@ test("row 175: scripts/mosaic sends plugins, name, names, follow, threads and th
const listing = (dir) => readdirSync(dir, { recursive: true }).map(String).sort();
async function w7(t, vars, edit) {
const root = tmp(t, "mosaic-cli-tester-init-");
const f = fixture(root, "acme", edit);
const rt = await startBroker({ dataRoot: f.dataRoot, businesses: bus(vars) });
const { root, f, org } = await orgFixture(t, "acme", edit);
const rt = await startBroker({ dataRoot: org.root, businesses: bus(vars) });
t.after(() => rt.close());
return { root, f, agent: { ...f.env, CLAUDECODE: "1", CLAUDE_CODE_ENTRYPOINT: "sdk-ts" } };
}
+3 -4
View File
@@ -18,7 +18,7 @@ import { systemFor } from "../../business/tests/helpers.mjs";
import { run } from "../src/cli.mjs";
import { CliError } from "../src/errors.mjs";
import { busExit } from "../src/transport.mjs";
import { fixture, humanEnv, io, tmp } from "./helpers.mjs";
import { fixture, humanEnv, io, tmp, orgFixture } from "./helpers.mjs";
const shipped = (name) => JSON.parse(readFileSync(fileURLToPath(new URL(`../../../roles/${name}.json`, import.meta.url)), "utf8"));
const roles = {
@@ -279,12 +279,11 @@ function mosaic(args, env) {
}
test("row 184: scripts/mosaic runs thread create, threads, talk --thread and thread read as the tester through tester-cli.mjs", async (t) => {
const root = tmp(t, "mosaic-cli-tester-threads-");
const f = fixture(root, "acme");
const { root, f, org } = await orgFixture(t);
const none = { authority: { withinRole: ["message.send"], crossRole: [] } };
const businesses = { acme: { id: "acme", human: "jason", arbiters: { technical: "coder", delivery: "coder" }, vars: { "bus.tester": true },
roles: { coder: { definition: "coder", ...none }, assistant: { definition: "assistant", ...none } } } };
const rt = await startBroker({ dataRoot: f.dataRoot, businesses, organization: { dir: join(root, "config"), rolesDir: f.roles, system: systemFor(root) } });
const rt = await startBroker({ dataRoot: org.root, businesses, organization: { dir: join(root, ".config", "mosaic-test"), rolesDir: f.roles, system: systemFor(root) } });
t.after(() => rt.close());
const human = rt.broker.bindHuman({ business: "acme", human: "jason", via: "cli", outsideAgent: true });
const call = (verb, args = {}) => rt.broker.request(human, { verb, args });
+5 -4
View File
@@ -6,7 +6,7 @@ import { main } from "../src/cli.mjs";
import { HUMAN_CLI, TESTER_CLI, busExit, testerTransport } from "../src/transport.mjs";
import { BusError, Broker } from "../../bus/src/broker.mjs";
import { Store } from "../../bus/src/store.mjs";
import { BUSINESSES, humanEnv, io, tmp } from "./helpers.mjs";
import { BUSINESSES, humanEnv, io, tmp, orgFixture } from "./helpers.mjs";
// Row 110 (#1604): --tester binds as the business's tester instead of as the
// human. The in-process broker here has bus.tester on; the real tester-cli.mjs
@@ -121,11 +121,12 @@ test("the tester transport runs tester-cli.mjs, and the tester's refusals exit 3
});
test("with no stand-in transport, --tester runs tester-cli.mjs on the data root's socket", async (t) => {
const dataRoot = tmp(t);
const { f, org } = await orgFixture(t, "demo");
const dataRoot = f.dataRoot;
const seen = [];
const testerSpawn = (cmd, argv, options) => (seen.push({ argv, input: JSON.parse(options.input) }), { status: 0, stdout: "[]" });
await main(["agents", "--tester", "--business", "demo"], inAgent(), { system: { dataRoot }, testerSpawn });
assert.deepEqual(seen, [{ argv: [TESTER_CLI, join(dataRoot, "bus", "broker.sock")], input: { business: "demo", verb: "agents", args: {} } }]);
await main(["agents", "--tester", "--business", "demo"], io({ ...f.env, CLAUDECODE: "1" }), { system: { dataRoot }, testerSpawn });
assert.deepEqual(seen, [{ argv: [TESTER_CLI, join(org.root, "bus", "broker.sock")], input: { business: "demo", verb: "agents", args: {} } }]);
});
// Row 277 (#1782, C1/C2): the human's text adds thread.claims, which a tester can't read, so
Loaded 100 of 250 files, more files were not shown because too many files have changed in this diff. Show more