Compare commits

..
23 Commits
Author SHA1 Message Date
jason.woltje 65f7288cea fix(webui): template save maps the new template-exists refusal to 409, not outcome-unknown (#1873)
f6c added template-exists for a save under a taken name. The template
route had no entry for it, so a duplicate save answered 503
outcome-unknown. Map it to 409 and move the SP8 pin.
2026-11-13 20:14:49 -06:00
jason.woltje 211b32ef1a fix(cli): f6c re-check, a failed released-marker write still removes the start lock; docs name the new warning and code (#1873) 2026-11-13 20:14:21 -06:00
jason.woltje 163972e08e fix(cli): f6c review round, released start locks, ENOENT and path cut (#1873)
P3-1: releasing a console start lock marks it released through its own
descriptor and removes the path only while it is still that file; a
released lock put back by a racing clear is cleared by the next start or
stop.
P3-2: org definitions reports unknown-organization only when the
organization file itself is missing; another missing file names the
operation.
P3-3: the path cut also covers a path after ':' or inside backticks.
2026-11-13 20:14:21 -06:00
jason.woltje 23e504f331 fix(cli,bus,organization): E2E6 lane f6c, refusal lines name the next step (#1873)
1. Stale bus host lock refusals from bus status, start, stop and restart
   name mosaic bus recover ORG.
2. org definitions: unknown org, unknown or malformed name and bad id give
   one refusal or usage line, no stack trace, no host path.
3. Pack, board and workflow refusals print their hint as template save does;
   tested with the real CliError transport shape.
4. console start and stop clear a console/start.lock whose named supervisor
   (pid and start time) is gone; a live or unnamed holder refuses.
5. Template save keeps free text verbatim and writes no placeholders; the
   preview lists each field that mentions an org-specific name.
6. A request the shim never sent (no socket, connect refused) exits 5 and
   the CLI reports bus-not-running with the start and recover line.
7. autonomy-precondition carries the check that failed, including on the
   hire paths.
8. Saving under an existing template name refuses template-exists with a
   next step; refusal table entry added, channel-proof pins moved.
2026-11-13 20:14:21 -06:00
jason.woltje 23b5e24ebc fix(webui): E2E6 re-check round, consumed-decision wording, autonomy focus and read errors (#1873)
Re-check finding 1 (P2): the wording for refusals that can come after the
decision is consumed told the user to look for the proposal "as answered"
in the inbox and otherwise read "nothing was applied". The bus inbox lists
no closed decision, so a consumed one drops out of it and the user would
read the false claim. Those codes now say only that part of it may already
be applied and to check the Agents page. decision-http tests assert no
"nothing was applied" in any case and no inbox pointer for those codes.

Re-check P3-b: the autonomy controls (Save, Discard, notices, review
summary and buttons) carry data-focus-key, so a focused button in a clean
form gets its focus back after the poll repaints the section.

Re-check P3-c: a failed autonomy read keeps the last read only while the
page holds the form (dirty, or focus inside it). A clean form shows the
read error. org-live-edit-browser test 4 covers both.

Re-check P3-a (harness-off after the consume through a corrupt prior
settings file) is deferred: wording it exactly needs the bus to mark the
phase on the refusal.
2026-11-13 20:05:10 -06:00
jason.woltje 266f3a45ff fix(webui): E2E6 review round, apply-phase refusals, held sections, tab fade, header pins (#1873)
Review finding 1: a hire start refusal the apply step can also raise after the decision is
consumed (decision-stale, organization-instance-*, organization-refused, definition-missing,
invalid-request and the rest) no longer says "Nothing was applied". It says that part may be
applied when the inbox shows the proposal as answered, and to check Agents first. Only codes
that prepare() or the route checks alone raise keep "Nothing was applied".
Review P3: a failed workflows or autonomy read keeps the last good read, so an open editor or
a dirty form stays through it. Focus holds a section only in a field being typed in; a focused
button is rebuilt and gets focus back by data-focus-key. Autonomy has Discard changes. The
project tab fade is 0px when every tab fits.
s8 and s9 header pins at 1440 now measure from the content edge: 1418 was 100vw-based, ran
under the page scrollbar and is the P2-11 clipping itself.

Correction to the previous commit's message: P1-2 is the unknown project, agent or harness
wording; P2-4 names the refused field; P2-6 is the Queue refusal reason; P2-8 is "bus stopped".
2026-11-13 19:52:26 -06:00
jason.woltje 0c35eeed9d fix(webui): E2E6 round, live edits survive refresh, org page layout, decision refusals (#1873)
P1-1 an open workflow editor, a dirty autonomy form and a pack's picked project keep their
node, draft and focus across the 10-second poll and an org-changed repaint.
P1-2 decision-http names the refused field and keeps the bus code and detail.
P1-3 long editors span the full width below the org page split.
P2-1 workflow roles resolve as definitions, templates and human; P2-2 a pack apply re-reads
workflows, autonomy and board and keeps its receipt; P2-3 pack preview fits its column;
P2-4 unknown project, agent or harness say so instead of outcome-unknown; P2-6 a stopped bus
reads "bus stopped"; P2-8 Queue says why it is unavailable; P2-9 pane settings values are not
clipped; P2-10 spend wording; P2-11 project header stays in its column; P2-12 project tabs
fade at the edge until scrolled to the end; P2-13 Restart bus and Stop bus line up in panes;
P2-15 backticks in chat text render as code.
Minor: Same-as commands wrap between words; harness checkboxes share a row.
Tests: org-live-edit-browser, e2e6-layout-browser, chat-replies and decision-http additions.
2026-11-13 19:24:48 -06:00
jason.woltje e6ab8bc1fd feat(bus,plugins,organization,cli,webui): SP9 remove system types outright, bus schema 3v-3w drops project_system_types (#1873)
Jason's 2026-11-12 ruling: the only deployed system is the dev environment on
dragon-lin, so the system-type plug-in kind goes with no retired kind, fence,
conversion or new refusal code.

- plugins: system-type leaves KINDS; system-types.mjs and the five modules are
  deleted. An org file naming the kind refuses unknown-kind.
- organization: the plugins.system-type check, systemTypesFence and the
  system-types.console right are removed.
- bus: system-types.console, project.system-types[.set], the system-type half
  of the tester check, the runtime reads and system-type-not-enabled are
  removed. Step 3v-3w drops project_system_types, its index, its four guards
  and its rows; SCHEMA_VERSION is 3w (32 tables, 126 triggers, 5 views).
- cli: mosaic org system-types and mosaic project system-types[-set] are
  removed; stack-release COUNTED and the budget supervisor's closed list move
  to 3w.
- channel: the project.system-types verbs are removed.
- webui: the System types page, route, dashboard row, chips and project
  selection are removed.
- docs: REQ-SYS-1/2 now describe starter packs; README system-type text and
  the requirements titles follow.
2026-11-12 21:02:46 -06:00
jason.woltje 0251acdad9 fix(packs): SP8 re-check, close before unlink cannot skip it; check the whole reason before the cut (#1873) 2026-11-12 20:05:32 -06:00
jason.woltje 13560660ae fix(packs): SP8 review round, single-pass placeholders, CLI hints, pins (#1873)
- template-save: one alternation pass replaces every literal, so an
  instance named root, org or agent never rewrites a placeholder already
  written in. checkPack's refusal message is passed on as the reason. A
  write or fsync failure removes the partial file it created.
- broker #templateFail drops a reason the credential check refuses.
- cli template words: humanTransport throws a CliError named by the bus
  code; a known code now gains its hint (the test transport throws the
  same CliError).
- template-http: the header notes serve.mjs strips /api/orgs/<id>.
- pins: the two .console verbs in console-decide and request-console;
  the packages/business/src re-export for template-save.mjs.
- tests: placeholder collision, a held-value key masked as [key], and a
  save after a real pack.apply.console keeping {id, version}.
2026-11-12 20:05:32 -06:00
jason.woltje 08e50e513f feat(packs): the owner saves an organization's shape as a template (#1873 SP8)
template-save.mjs captures the org (or one project) as a starter-pack JSON under
<config>/templates/<owner>/<name>.json: copy material and instance credentials
stripped, role credential request lines kept, owner/org/project/agent/root names
replaced by placeholders in free text, at most 8 hires, packs entries {id, version}.
A held credential value in kept text refuses template-secret-suspected naming the
field path. Preview then save with the preview's digest; O_EXCL 0600 write, a
second save refuses.

Bus: template.save/template.list for the owner's CLI cap, .console names for the
owner's Console cap, null in VERB_RIGHTS. CLI: mosaic org template save|list.
Console: GET /api/templates, POST /api/templates/preview, POST /api/templates.
2026-11-12 20:05:32 -06:00
jason.woltje a9d72b3cd1 fix(business): CB5 old-path re-export for copy-binding-status.mjs, the rename r2 B guard (#1873)
Every packages/organization/src module has a one-line re-export at its old packages/business/src
path; the CB5 status table had none, so rename-b-identity refused it.
2026-11-12 19:19:23 -06:00
jason.woltje 4b35e3c1d2 fix(bus): CB5 re-check, a running or failed revoke outranks copy-service-unbindable; unbind summary tested (#1873)
- copy-launch status(): a fenced or closed copy falls through to the gate check, so the owner
  sees credential-revoking or credential-revoke-failed for a binding issued to a copy whose role
  also needs a service a copy cannot bind.
- copy-bindings: an unbind raise reads change 'unbind' in the raise answer, every inbox and trail.
2026-11-12 19:19:22 -06:00
jason.woltje 56aeefb370 fix(bus,webui,cli): CB5 review fixes, binding summary on the inbox, raise and trail reads, copy-service-unbindable status, Console allowlist (#1873)
- broker: the hire.raise answer, the inbox console preview and the trail read carry a
  binding as {instance, change} only; the raise event keeps the full proposal the resolve reads.
- copy-launch status(): a copy whose role needs a service a copy cannot bind reads
  copy-service-unbindable (new fixed status text) instead of a gitea binding step.
- cli instances: an unbound copy's line names its missing services.
- webui serve: the instances verb rebuilds each copy binding from the fixed status table.
- bus.js reads the binding change field only.
- browser tests: system Chromium starts with --disable-extensions; the A3 booted-bus test passes it.
2026-11-12 19:19:22 -06:00
jason.woltje fdbc34413b feat(webui,bus): CB5 Console operator flow, binding confirmation and next steps, copy binding status on the Agents grid (#1873)
The bus instances read adds a copy's binding status only when the copy was
bound at boot or is unbound for want of a bindable service, never to a
tester. decision-http returns an allowlisted binding (instance and change)
on a binding confirmation and fixed next-step text on an approved resolve
and on a binding hire start. The decision page names the copy and the
change; the receipt shows the next step; the Agents grid shows can't launch
with the binding's next step. Every new string is fixed text.
2026-11-12 19:19:22 -06:00
jason.woltje c5bf71ab08 feat(bus,cli): CB5 operator status, each copy's binding status and next step in the owner's instances read and mosaic org instances (#1873)
The launch gate's status() runs the same checks a launch runs, without the loss report, and
answers a fixed { status, next } from organization copy-binding-status.mjs (design section 6
texts plus issue-required and ready). A tester's read never carries it.
2026-11-12 19:19:22 -06:00
jason.woltje dbb69c8a41 test(bus,cli): CB5 two-organization acceptance and the CB4 deferred P3 tests (#1873)
The two-org test boots real buses for two organizations with a shipped
Gitea-requiring thread role, binds each copy through its own owner
decision and verified issue, and checks identity separation, record
isolation, token isolation, B through A's revoke, archive and delete,
and refusal of a replayed raw A binding or A token in B.

P3-1 shadowed cleanup, P3-2 runtime revoke wiring, P3-3 ordinary-run
stop. Each passes on BASE; guard-removal mutants recorded.
2026-11-12 19:19:22 -06:00
jason.woltje 34dd331016 fix(packs): SP5 re-check, the inline pin digest is canonicalJson's for integer-like keys (#1873)
digestPrd rebuilds an object, so integer-like keys come first and the JSON
differs from canonicalJson, the digest the pack apply records. An inline
entry with such a key (a credential scope map group) refused as stale on
every raise. The broker now builds the canonical string itself, a local copy
of util.mjs canonicalJson, so the sandbox import closure is unchanged.
2026-11-12 19:05:33 -06:00
jason.woltje 808b2b7cbf fix(packs): SP5 review round, broker import closure, no-op spend, role.hire before the read, template-named steps (#1873)
The broker no longer imports organization util or definitions; the proposal
pin digest is digestPrd and the inline composition is built in place, so the
sandbox client import closure project-runtime.test.mjs pins is unchanged.
An agent apply the runtime skips (a repeat that adds nothing) never reaches
beforeWrite and spends no grant. A proposal raise checks role.hire before it
reads the packs list. An agent apply keeps a workflow step only for a pack
role the organization defines or one the pack pins to the template of that
name; an inline definition sharing a template's name is left out. Tests: a
repeat apply spends nothing, apply then raise end to end with the broker
booted from the file, a project pack, the template-name shadow, and a chief
without role.hire; the revision refusal is matched exactly.
2026-11-12 19:05:33 -06:00
jason.woltje 5ffe38e3b2 feat(packs): the chief applies a pack's non-definition half and raises hires from its proposals (#1873 SP5)
pack.preview, pack.apply, board.set and workflow.set accept an agent session whose role
lists organization.change (Jason 11-12 Q2): the check runs before the read, and the grant
is spent once after a write commits. A role without it gets decision-required. The packs
read lists packs for a within-role holder and spends nothing.

An agent apply never writes a definition. A definition a human apply would add is marked
not-written and its hire is pinned to the template or the inline entry. A workflow step
naming such a definition is left out and listed, and a workflow left with no steps is not
written. A definitions rename or a saved template refuses human-required, with the writer's
own guard behind both.

hire.raise takes {proposal: {pack, project?, hire}, route, reason?} from the chief. The broker
reads the recorded proposal, checks an inline pin's hash (starter-pack-proposal-stale) and
launch.noHire, and hands the pin to the hire service, which refuses a moved template or
definition as decision-stale or definition-rev-conflict.
2026-11-12 19:05:33 -06:00
jason.woltje 991eb8c332 fix(webui): SP6 review round, the writer's workflow name rule, no silent replace, preview redraw (#1873)
The workflow editor checks the writer's PLAN_NAME (lowercase letters, digits
and dashes, starting with a letter), so a name the bus would refuse no longer
reaches it with step guidance. A new workflow under a name its scope already
holds is refused on the page instead of replacing that workflow. A pack
preview the page cannot draw says so rather than leaving Preview disabled.
The browser test launches Chromium with --disable-extensions.
2026-11-12 18:40:34 -06:00
jason.woltje 504fcd9220 fix(webui): pack preview reads credential lines as {service, scope} objects (#1873 SP6)
The bus sends each hire's credential lines as {service, scope} (scope absent
when a definition names a service without scopes). The picker destructured
them as tuples, so a real preview threw and never rendered. The browser test
fixture now uses the producer's shape.
2026-11-12 18:31:21 -06:00
jason.woltje 154c2216f3 feat(webui): board labels, pack picker and workflow editor on the organization page (#1873 SP6)
The Tasks board takes the selected organization's board: its labels and column order over the five
fixed states, and its default layout unless the viewer saved one. The all-organizations view keeps
the fixed labels. The organization page gains the owner's starter pack picker (list, preview with
marks, flagged lines, credential lines and noHire marks, apply by digest) and a workflow list and
editor that marks a step role naming no current role. Every organization- or pack-supplied string
goes through esc or textContent.

The workspace read carries each organization's and project's board. GET /api/orgs/<id>/workflows
is the owner's read of the workflows, the role names a step may resolve to and the file revision.
packsServes pins HEAD and GET /api/board to the Board read, never the pack route.
2026-11-12 18:17:26 -06:00
145 changed files with 5189 additions and 1689 deletions

No files matched your search

+2
View File
@@ -348,6 +348,8 @@ The hire form and the autonomy panel from a terminal (E2, #1873, P1-3). `mosaic
The bus and Console lifecycle verbs refuse inside an agent run (agent-guard, #1873): `mosaic bus start`, `bus stop`, `bus restart` and `bus recover`, and `mosaic console start`, `console stop` and `console restart`, and `mosaic org archive` and `org delete`, which stop the organization's bus, and `mosaic org restore`, which starts it (`org restore --no-bus` starts nothing and is not refused), and `mosaic org role-templates`, `hire`, `fire`, `change` and `autonomy` (E2). Each exits 3 with `refused: this runs only from a human shell, outside any agent run (found MARKER in the environment)` before it reads anything. It checks the environment it is given and its own process environment, so a sanitized environment does not hide a marker. A usage error (a missing or extra argument, `--pm` with `--no-launch`, or a bad `--dev-insecure-bind`/`--dev-allow-host`) still comes first (exit 4). `mosaic bus status` and `mosaic console status` stay open to agents. The Console's Restart bus and Restart Console controls keep working: the Console starts its bus host with its own environment, which carries no marker, and Restart Console relaunches the Console under its supervisor without going through the CLI. The markers are the ones `packages/cli/src/transport.mjs` lists. The check is cooperative: a process that drops the markers from its environment is not stopped by it.
Refusal lines (E2E6, #1873). A refusal that has a next step prints it on a second line, `mosaic: next: ...`, after the unchanged message, so callers that match the message keep working. `mosaic bus status`, `start`, `stop` and `restart` on a stale bus host lock exit 3 with `mosaic: stale bus host lock ...` and `mosaic: next: if the bus host died, clear it with mosaic bus recover ORG; recover refuses while the host still runs`. A human-transport verb whose bus is not running (no socket, or the connect refused before anything was sent) exits 3 with `bus-not-running: the bus for ORG is not running, so the request was not sent` and a next line naming `mosaic bus start ORG` and `mosaic bus recover ORG`; `outcome-unknown` (exit 1) stays for a request that left and got no answer. `mosaic org definitions` with an unknown organization, an unknown or malformed name, or a bad id prints one refusal line (exit 3; `unknown-organization` only when the organization file itself is missing, `definitions-unreadable` when `templates` cannot read the roles directory) or, for a bad id, the usage (exit 4), with no stack trace and no host path. Pack, board and workflow refusals carry their next step as `template save` does. `mosaic console start` and `console stop` clear a `console/start.lock` whose named supervisor (pid and start time) is gone, once, with a `cleared a console start lock left by process PID, which is gone` warning; a lock whose holder still runs, or that names no holder, refuses and stays. A start or stop releasing its lock marks it released through its own descriptor and removes the path only while it is still that file, so a lock put back after its release is cleared the same way, with a `cleared a console start lock its holder had released` warning. An `autonomy-precondition` refusal from the bus carries the check that failed (owner notices off, no tested reverse, a resolver without the right, no `review.verdict` holder, no enforced budget, out of the loop only from near the loop, the trail review or near-loop decision missing); the human-transport child writes only the code, so a terminal `org autonomy set` shows the Console's message. `mosaic org template save` writes free text verbatim: it never rewrites prose, and the preview lists every field that mentions the organization id, a project id, an agent name or the owner's account, with the names it mentions, for the owner to edit before sharing. Saving under a name already saved for the account refuses `template-exists` (exit 2, nothing written) with a next line to save under another name or list the saved ones with `mosaic org template list ORG`.
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
+13 -7
View File
@@ -1240,16 +1240,22 @@ Added in version 1.3 (Q155).
changes it, not in one rewrite (Q155). Until it converts, Technical
considerations lists where it is hard-coded.
### System types
### Starter packs
Added in version 1.20.
- **REQ-SYS-1.** System types are a plug-in kind. Its modules include
coding, creative writing, marketing, Mosaic Montage (the OpenMontage
fork) and business management. An organization enables a list of them.
- **REQ-SYS-2.** A project may use any system type its organization
enables, and none is required. An agent working in a project gets the
toolsets of the system types that project uses.
- **REQ-SYS-1.** Starter packs are a plug-in kind. Mosaic Stack ships five:
coding, creative writing, marketing, Mosaic Montage and business
management. A pack is a starting configuration of role definitions,
proposed hires, a board style and workflow templates. An organization or
project copies a pack in once and then owns and changes what it copied. A
user can save an organization or project as their own template and apply it
the same way.
- **REQ-SYS-2.** Applying a pack grants nothing by itself. Every agent it
proposes is hired through the hire decision (REQ-AGENT-3), bounded by the
chief's own role, with any line above it approved by the owner. A pack never
sets credentials, rights, the autonomy setting or plug-in choices. Removing
a pack removes nothing it copied.
### Meta-harness
-16
View File
@@ -639,22 +639,6 @@ CREATE TRIGGER request_snapshot_events_no_replace BEFORE INSERT ON request_snaps
CREATE TRIGGER request_project_events_no_update BEFORE UPDATE ON request_project_events BEGIN SELECT RAISE(ABORT, 'request_project_events is append-only'); END;
CREATE TRIGGER request_project_events_no_delete BEFORE DELETE ON request_project_events BEGIN SELECT RAISE(ABORT, 'request_project_events is append-only'); END;
CREATE TRIGGER request_project_events_no_replace BEFORE INSERT ON request_project_events WHEN EXISTS (SELECT 1 FROM request_project_events WHERE seq=NEW.seq OR (business=NEW.business AND project=NEW.project AND revision=NEW.revision)) BEGIN SELECT RAISE(ABORT, 'request_project_events is append-only'); END;
-- Per-project selections are a human-authored append-only settings trail.
CREATE TABLE project_system_types (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
project TEXT NOT NULL REFERENCES projects(id),
types TEXT NOT NULL,
by TEXT NOT NULL
) STRICT;
CREATE INDEX project_system_types_project ON project_system_types (business, project, seq);
CREATE TRIGGER project_system_types_no_update BEFORE UPDATE ON project_system_types BEGIN SELECT RAISE(ABORT, 'project_system_types is append-only'); END;
CREATE TRIGGER project_system_types_no_delete BEFORE DELETE ON project_system_types BEGIN SELECT RAISE(ABORT, 'project_system_types is append-only'); END;
CREATE TRIGGER project_system_types_no_replace BEFORE INSERT ON project_system_types WHEN EXISTS (SELECT 1 FROM project_system_types WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'project_system_types is append-only'); END;
CREATE TRIGGER project_system_types_business BEFORE INSERT ON project_system_types
WHEN (SELECT business FROM projects WHERE id = NEW.project) IS NOT NEW.business
BEGIN SELECT RAISE(ABORT, 'a system types row belongs to its project business'); END;
-- U4a (#1873): the task store built into each organization, the default task-tracker module
-- (REQ-TASK-1). Each row is the whole task after one write, so a task's state is its latest row
-- and the rows are its history: who wrote it (a role and run, or the person), when, under which
+162 -71
View File
@@ -178,13 +178,15 @@ export const USER_VERBS = Object.freeze(['project.list', 'thread.create', 'threa
'thread.share', 'thread.unshare', 'thread.shares']);
// Row 252 (#1753, design sections 2-3): the only verbs a Console owner cap reaches. Every other verb
// refuses it human-required before its own checks, and these refuse every other cap.
export const CONSOLE_VERBS = Object.freeze(['system-types.console', 'project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
export const CONSOLE_VERBS = Object.freeze(['project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
// Task 3 (#1839): and the owner's intake controls. The reads and writes are the same lifecycle methods the PM's
// unsuffixed verbs reach, with the owner's verified (provider, iss, sub) as the only caller; the PM-only
// request.question and the PM's own reads keep their unsuffixed names and refuse a console cap.
'request.get.console', 'request.projects.console', 'request.answer.console', 'request.attach.console', 'request.cancel.console',
// #1873 SP4: the owner's starter packs, board and workflows.
'packs.console', 'pack.preview.console', 'pack.apply.console', 'board.set.console', 'workflow.set.console']);
'packs.console', 'pack.preview.console', 'pack.apply.console', 'board.set.console', 'workflow.set.console',
// #1873 SP8: and saving a template, with the list of the owner's own.
'template.save.console', 'template.list.console']);
// What an owner's console cap may ask of the request lifecycle (a read, the intake controls, and Task 4's mode and draft).
const INTAKE_CONSOLE = Object.freeze(['artifactConsole', 'get', 'answer', 'attach', 'cancel', 'mode', 'draft']);
const REQUEST_VERBS = Object.freeze({ 'request.open': 'open', 'request.get': 'get', 'request.projects': 'projects', 'request.propose': 'propose',
@@ -221,6 +223,17 @@ const PACK_KEYS = Object.freeze({
'workflow.set': [['revision', 'name', 'workflow', 'project'], ['revision', 'name', 'workflow']],
});
export const PACK_BUS_KEYS = Object.freeze(Object.fromEntries(Object.entries(PACK_KEYS).flatMap(([verb, k]) => [[verb, k], [`${verb}.console`, k]])));
// #1873 SP8: saving a template and listing the caller's own, the owner's alone. The CLI calls the
// unsuffixed name as the owner's human cap, the Console the .console one; no agent, member or tester
// reaches either. template.save previews with preview: true and writes with the preview's digest.
const TEMPLATE_KEYS = Object.freeze({
'template.save': [['name', 'project', 'preview', 'digest'], ['name']],
'template.list': [[], []],
});
export const TEMPLATE_BUS_KEYS = Object.freeze(Object.fromEntries(Object.entries(TEMPLATE_KEYS).flatMap(([verb, k]) => [[verb, k], [`${verb}.console`, k]])));
// The codes a template refusal keeps, each with its reason: a field path or a checked name, never a
// value from the organization file and never a host path.
const TEMPLATE_CODES = Object.freeze(['template-secret-suspected', 'template-exists', 'invalid-request', 'human-required', 'not-configured', 'pack-plan-stale', 'starter-pack-invalid', 'starter-pack-template-changed']);
// Row 175: the organization verbs a tester may call. Its writes run every check the human's do
// and log actor `tester`; row 176 adds routes reads; limits.set, organization.set and organization.revision refuse it.
export const TESTER_ORGANIZATION_VERBS = Object.freeze(['limits', 'plugins', 'instances', 'plugins.set', 'instances.set', 'routes.set', 'routes']);
@@ -276,10 +289,18 @@ export class BusError extends Error {
if (reason !== undefined) this.reason = reason;
}
}
const fail = (code) => {
throw new BusError(code);
// E2E6 fix 7: a refusal may carry a reason, static text that names which check failed; it never
// quotes an argument or file bytes.
const fail = (code, reason) => {
throw new BusError(code, reason);
};
const BUDGET_UNCOVERED = 'no enforced budget covers new agents; authority away from the loop needs one';
const object = (x) => x !== null && typeof x === 'object' && !Array.isArray(x);
// #1873 CB5: a copy binding proposal leaves the bus only as its copy and its change. The copy id,
// incarnation, definition pin, services, grant ids and slots stay in the raise event, which the
// resolve reads; the inbox preview and the trail read carry this summary (design lines 162-163).
const bindingSummary = (v) => (object(v?.binding)
? { ...v, binding: { instance: v.binding.instance, change: v.binding.after === null ? 'unbind' : 'bind' } } : v);
// Lifecycle Task 5a: keep these pure helpers here because broker.mjs is also loaded by the
// sandbox client. A new host-only import would widen its mounted source closure.
const createJson = value => {
@@ -2539,7 +2560,7 @@ export class Broker {
options.some(o => !object(o) || !id(o.key) || !text(o.text, 1024)) ||
new Set(options.map(o => o.key)).size !== options.length) return null;
const digest = sha256(JSON.stringify([{ ...d, options }, raise]));
if (['role.hire','role.fire','role.change'].includes(d.action)) return raise.hire ? {kind:'hire',options,...raise.hire} : null;
if (['role.hire','role.fire','role.change'].includes(d.action)) return raise.hire ? {kind:'hire',options,...bindingSummary(raise.hire)} : null;
if (d.action !== 'credential.mint') return 'grant' in raise ? null : { kind: 'decision', options, digest };
if (!object(raise.grant) || typeof raise.grant.method !== 'string' || options.length !== 2) return null;
const approve = options.find((o) => o.key === raise.approvalChoice);
@@ -6159,28 +6180,6 @@ export class Broker {
return result;
}).catch(e => { throw this.#refused(s, e instanceof BusError ? e : new BusError(e.code ?? 'request-failed'), request); });
}
if (request.verb === 'system-types.console') {
if (!s.console || s.human !== this.#business(s.business).human) fail('human-required');
keys(args, ['project']);
if (!this.#organization) fail('not-configured');
const enabled = this.#organization(s.business, 'system-types.current', {}, s.human);
if (!Array.isArray(enabled)) fail('not-configured');
let project = null;
if (args.project !== undefined) {
const id = identifier(args.project);
if (!this.#store.get('SELECT 1 FROM projects WHERE id=? AND business=?', id, s.business)) fail('project-not-found');
const row = this.#store.get('SELECT types FROM project_system_types WHERE business=? AND project=? ORDER BY seq DESC LIMIT 1', s.business, id);
const selected = row ? JSON.parse(row.types) : [];
project = { project: id, selected, active: selected.filter(x => enabled.includes(x)), inactive: selected.filter(x => !enabled.includes(x)) };
}
return Promise.resolve(this.#organization(s.business, 'system-types.installed', {}, s.human))
.then(installed => {
if (!Array.isArray(installed)) fail('not-configured');
const result = { installed, enabled, project };
this.#secretCheck(result);
return result;
});
}
if (request.verb === 'project.remote.get.console' || request.verb === 'project.remote.set.console') {
if (!s.console || s.human !== this.#business(s.business).human) fail('human-required');
if (!this.#organization) fail('not-configured');
@@ -6266,7 +6265,7 @@ export class Broker {
if (CREDENTIAL_VERBS.includes(request.verb)) return this.#requestCredential(s, request.verb, structuredClone(args));
if(['autonomy.get.console','autonomy.set.console','autonomy.review.console','decision.reverse.console'].includes(request.verb)) {
this.#autonomyOwner(s);
return this.#autonomyConsole(s,request.verb,structuredClone(args)).catch(e=>{throw this.#refused(s,e instanceof BusError?e:new BusError(e.code??'outcome-unknown'),request);});
return this.#autonomyConsole(s,request.verb,structuredClone(args)).catch(e=>{throw this.#refused(s,e instanceof BusError?e:new BusError(e.code??'outcome-unknown',e instanceof BusError?e.reason:undefined),request);});
}
if (s.hireMembership && !['hire.preview.console','hire.resolve.console','hire.start.console','hire.templates.console'].includes(request.verb)) fail('human-required');
if(request.verb==='decision.resolve' && !s.human) {
@@ -6275,7 +6274,7 @@ export class Broker {
}
if(request.verb==='decision.resolve' && !s.human && ['role.hire','role.fire','role.change'].includes(this.#decision(s,args.id).action)) {
const d=this.#decision(s,args.id);
if(d.route_to!=='human')return this.#resolveAuthority(s,args).catch(e=>{throw this.#refused(s,e instanceof BusError?e:new BusError(e.code??'outcome-unknown'),request);});
if(d.route_to!=='human')return this.#resolveAuthority(s,args).catch(e=>{throw this.#refused(s,e instanceof BusError?e:new BusError(e.code??'outcome-unknown',e instanceof BusError?e.reason:undefined),request);});
}
if(request.verb==='hire.templates.console') {
keys(args,[]);const caller=this.#hireCaller(s);
@@ -6287,11 +6286,14 @@ export class Broker {
if (request.verb === 'hire.raise' || request.verb === 'hire.start.console' ||
(request.verb === 'decision.raise' && ['role.hire','role.fire','role.change'].includes(args.action))) {
const input=request.verb==='decision.raise' ? (()=>{ keys(args,['action','hire'],['action','hire']); if(args.action!==`role.${args.hire?.op}`)fail('invalid-request'); return args.hire; })() : args;
return this.#raiseHire(s,input,request.verb==='hire.start.console').catch(e=>{throw this.#refused(s,new BusError(e.code??'invalid-request'),request);});
// #1873 SP5: a raise from a pack's recorded proposal; the pin, not the caller, names the role.
if(request.verb==='hire.raise' && object(args) && Object.hasOwn(args,'proposal'))
return Promise.resolve().then(()=>this.#raiseHire(s,this.#proposalHire(s,args),false)).catch(e=>{throw this.#refused(s,new BusError(e.code??'invalid-request',e instanceof BusError?e.reason:undefined),request);});
return this.#raiseHire(s,input,request.verb==='hire.start.console').catch(e=>{throw this.#refused(s,new BusError(e.code??'invalid-request',e instanceof BusError?e.reason:undefined),request);});
}
if(request.verb==='decision.preview.console' && ['role.hire','role.fire','role.change'].includes(this.#decision(s,args.id).action))return this.#hireConsole(s,'hire.preview.console',args).catch(e=>{throw this.#refused(s,new BusError(e.code??'outcome-unknown'),request);});
if(request.verb==='decision.preview.console' && ['role.hire','role.fire','role.change'].includes(this.#decision(s,args.id).action))return this.#hireConsole(s,'hire.preview.console',args).catch(e=>{throw this.#refused(s,new BusError(e.code??'outcome-unknown',e instanceof BusError?e.reason:undefined),request);});
if (['hire.preview.console','hire.resolve.console'].includes(request.verb))
return this.#hireConsole(s,request.verb,args).catch(e=>{throw this.#refused(s,new BusError(e.code??'outcome-unknown'),request);});
return this.#hireConsole(s,request.verb,args).catch(e=>{throw this.#refused(s,new BusError(e.code??'outcome-unknown',e instanceof BusError?e.reason:undefined),request);});
if (request.verb === 'grants.scope' || request.verb === 'grants.scope.set')
return this.#grantScopeVerb(s, request.verb, structuredClone(args));
// #1873 SP4: the owner's starter packs, board and workflows. Only the organization's owner calls
@@ -6301,20 +6303,61 @@ export class Broker {
if (Object.hasOwn(PACK_BUS_KEYS, request.verb)) {
if (s.tester) fail('tester-refused');
const owner = this.#business(s.business).human;
if (request.verb.endsWith('.console')) { if (!s.console || s.human !== owner) fail('human-required'); }
else { this.#human(s); if (s.human !== owner) fail('human-required'); }
keys(args, ...PACK_BUS_KEYS[request.verb]);
// #1873 SP5: the unsuffixed verbs also serve an agent whose role file lists
// organization.change, the test organization.set uses; any other role gets decision-required.
// The packs read checks the grant and never spends it, as organization.revision; an agent
// without it gets human-required. A write checks before the writer, again inside it, and
// spends the grant once the writer has committed. A write the runtime skips (a repeat apply that
// adds nothing) never reaches beforeWrite and spends nothing. The actor is the definition and instance.
const agent = !request.verb.endsWith('.console') && !s.human && !s.console;
let actor = s.human, authorityError = null, wrote = false;
if (agent) {
keys(args, ...PACK_BUS_KEYS[request.verb]);
if (request.verb === 'packs') {
this.#processingWork(); this.#agent(s);
if (this.#classification(s, 'organization.change') !== 'within-role') fail('human-required');
} else this.#checkAuthority(s, 'organization.change');
actor = this.#business(s.business).roles[s.role].definition + '.' + s.role;
}
else {
if (request.verb.endsWith('.console')) { if (!s.console || s.human !== owner) fail('human-required'); }
else { this.#human(s); if (s.human !== owner) fail('human-required'); }
keys(args, ...PACK_BUS_KEYS[request.verb]);
}
if (!this.#organization) fail('not-configured');
const verb = request.verb.replace(/\.console$/, '');
const spends = agent && verb !== 'packs' && verb !== 'pack.preview';
const agentContext = agent ? { agent: true, beforeWrite: () => { try { this.#checkAuthority(s, 'organization.change'); } catch (e) { authorityError = e; throw e; } wrote = true; } } : {};
const packFail = e => { if (authorityError && e === authorityError) throw e; this.#packFail(e); };
const openHires = verb === 'pack.preview' || verb === 'pack.apply'
? this.#store.all(`SELECT d.id FROM decisions d WHERE d.business=? AND d.action='role.hire' AND NOT EXISTS(SELECT 1 FROM decision_events x WHERE x.decision=d.id AND x.op IN ${CLOSED}) ORDER BY d.at, d.id`, s.business)
.map(({ id }) => this.#raised(s.business, id)?.hire).filter(object).map(h => ({ name: h.name, project: h.project ?? null }))
: [];
let result;
try { result = this.#organization(s.business, verb, structuredClone(args), s.human, { openHires }); }
catch (e) { this.#packFail(e); }
try { result = this.#organization(s.business, verb, structuredClone(args), actor, { openHires, ...agentContext }); }
catch (e) { packFail(e); }
const done = (value) => {
if (spends && wrote) this.#store.transaction(() => this.#consumeAuthority(s, 'organization.change'));
this.#secretCheck(value); return value;
};
return result?.then ? result.then(done, packFail) : done(result);
}
// #1873 SP8: saving a template. Only the organization's owner: the human cap from the CLI or the
// owner's console cap. An agent, a member's cap and the tester are refused before any argument
// check. The account the template is saved under is the caller's own s.human.
if (Object.hasOwn(TEMPLATE_BUS_KEYS, request.verb)) {
if (s.tester) fail('tester-refused');
const owner = this.#business(s.business).human;
if (request.verb.endsWith('.console')) { if (!s.console || s.human !== owner) fail('human-required'); }
else { this.#human(s); if (s.human !== owner) fail('human-required'); }
keys(args, ...TEMPLATE_BUS_KEYS[request.verb]);
if (!this.#organization) fail('not-configured');
const verb = request.verb.replace(/\.console$/, '');
let result;
try { result = this.#organization(s.business, verb, structuredClone(args), s.human, {}); }
catch (e) { this.#templateFail(e); }
const done = (value) => { this.#secretCheck(value); return value; };
return result?.then ? result.then(done, e => this.#packFail(e)) : done(result);
return result?.then ? result.then(done, (e) => this.#templateFail(e)) : done(result);
}
if (["limits", "limits.set", "organization.set", "organization.revision", "plugins", "plugins.set", "instances", "instances.set", "routes", "routes.set", "veto.set", "library.limits.set"].includes(request.verb) || Object.hasOwn(DEFINITION_BUS_KEYS, request.verb)) {
// Row 123 (#1617, REQ-COS-1, REQ-LIMIT-1): organization.set is limits.set for a role whose
@@ -6332,7 +6375,7 @@ export class Broker {
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"]);
if (s.tester && object(args.plugins) && (Object.hasOwn(args.plugins, 'model-provider') || Object.hasOwn(args.plugins, 'system-type'))) fail('human-required');
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).
@@ -6423,6 +6466,10 @@ export class Broker {
if (request.verb === "instances" && result?.instances && typeof result.instances === "object" && this.#business(s.business).copies)
result.copies = Object.fromEntries(Object.entries(this.#business(s.business).copies).map(([name, c]) => [name, {
...structuredClone(c), running: c.state === 'instance' ? this.#running(s.business, name) : [],
// #1873 CB5: a copy that needs a binding (bound at boot, or missing a bindable service)
// carries its binding status and next step, fixed text from the launch gate's own checks.
// Never to a tester, and never a binding record.
...(!s.tester && this.#copyLaunch?.status ? ((binding) => (binding ? { binding } : {}))(this.#copyLaunch.status(s.business, name, c)) : {}),
}]));
return result;
}
@@ -6584,7 +6631,7 @@ export class Broker {
if(!this.#autonomyHost?.get || !this.#autonomyHost?.set)fail('not-configured');
const old=await this.#autonomyHost.get(s.business),b=this.#business(s.business);
if(a.revision!==old.revision)fail('revision-conflict');
if(AUTONOMY_CLASSES.some(cls=>decisionAutonomy(cls,a.autonomy)==='near the loop') && a.autonomy.notices!==true)fail('autonomy-precondition');
if(AUTONOMY_CLASSES.some(cls=>decisionAutonomy(cls,a.autonomy)==='near the loop') && a.autonomy.notices!==true)fail('autonomy-precondition','owner notices are off; a class near the loop needs them on');
for(const cls of AUTONOMY_CLASSES) {
const before=decisionAutonomy(cls,old.autonomy),after=decisionAutonomy(cls,a.autonomy);
// Pulling back is unconditional. Only a move away from the loop has prerequisites.
@@ -6593,22 +6640,24 @@ export class Broker {
if(cls==='policy')continue;
if(cls==='authority' && after==='out of the loop' && before!==after)fail('authority-limits-unavailable');
if(['in the loop','near the loop','out of the loop'].indexOf(after)<=['in the loop','near the loop','out of the loop'].indexOf(before))continue;
if(a.autonomy.notices!==true || !this.#autonomyHost.reversible?.(cls,b))fail('autonomy-precondition');
if(a.autonomy.notices!==true)fail('autonomy-precondition',`owner notices are off; moving ${cls} away from the loop needs them on`);
if(!this.#autonomyHost.reversible?.(cls,b))fail('autonomy-precondition',`${cls} has no tested reverse in this organization, so it stays in the loop`);
const resolvers=cls==='authority'?[a.autonomy.authorityResolver]:cls==='merge'?[b.arbiters.technical]:cls==='policy'?[b.arbiters.delivery]:Object.values(b.arbiters);
for(const name of resolvers) {
const role=b.roles[name],right=cls==='authority'?'decision.resolve.authority':`decision.resolve.${cls==='merge'?'technical':cls==='policy'?'delivery':name===b.arbiters.technical?'technical':'delivery'}`;
if(!role?.authority?.withinRole.includes(right) || (cls==='authority' && name===b.launch?.by))fail('autonomy-precondition');
if(!role?.authority?.withinRole.includes(right))fail('autonomy-precondition',cls==='authority'?'the authority resolver is not a role with decision.resolve.authority':`the ${cls} resolver is not a role with ${right}`);
if(cls==='authority' && name===b.launch?.by)fail('autonomy-precondition','the authority resolver is the role that launches agents; name a separate resolver');
}
if(['merge','policy'].includes(cls) && !Object.values(b.roles).some(r=>r.authority?.withinRole.includes('review.verdict')))fail('autonomy-precondition');
if(cls==='authority' && !this.#budgetCovered(b))fail('autonomy-precondition');
if(['merge','policy'].includes(cls) && !Object.values(b.roles).some(r=>r.authority?.withinRole.includes('review.verdict')))fail('autonomy-precondition',`no role holds review.verdict, which ${cls} away from the loop needs`);
if(cls==='authority' && !this.#budgetCovered(b))fail('autonomy-precondition',BUDGET_UNCOVERED);
if(after==='out of the loop') {
if(before!=='near the loop')fail('autonomy-precondition');
if(before!=='near the loop')fail('autonomy-precondition',`${cls} moves out of the loop only from near the loop`);
const review=this.#store.get("SELECT seq,body FROM events WHERE business=? AND kind='action.allowed' AND json_extract(body,'$.operation')='autonomy.review' AND json_extract(body,'$.decisionClass')=? ORDER BY seq DESC LIMIT 1",s.business,cls);
const latest=this.#store.get("SELECT seq FROM events WHERE business=? AND kind='action.allowed' AND json_extract(body,'$.operation')='autonomy.set' ORDER BY seq DESC LIMIT 1",s.business);
if(!review || JSON.parse(review.body).settingSeq!==latest?.seq)fail('autonomy-precondition');
if(!review || JSON.parse(review.body).settingSeq!==latest?.seq)fail('autonomy-precondition',`${cls} needs your review of its trail since the last autonomy change`);
const notice=this.#store.get("SELECT seq FROM events WHERE business=? AND kind='action.allowed' AND json_extract(body,'$.operation')='autonomy.notice' AND json_extract(body,'$.decisionClass')=? AND seq>? ORDER BY seq DESC LIMIT 1",s.business,cls,latest?.seq??0);
const reversal=this.#store.get("SELECT max(e.seq) seq FROM events e WHERE e.business=? AND e.kind='action.allowed' AND json_extract(e.body,'$.operation') LIKE 'autonomy.reverse%' AND EXISTS(SELECT 1 FROM events n WHERE n.business=e.business AND n.kind='action.allowed' AND json_extract(n.body,'$.operation')='autonomy.notice' AND json_extract(n.body,'$.decisionClass')=? AND json_extract(n.body,'$.decision')=json_extract(e.body,'$.decision'))",s.business,cls);
if(!notice || review.seq<=Math.max(notice.seq,reversal?.seq??0))fail('autonomy-precondition');
if(!notice || review.seq<=Math.max(notice.seq,reversal?.seq??0))fail('autonomy-precondition',`${cls} needs a near-loop decision since the last autonomy change, and your trail review after it`);
}
}
this.#store.transaction(()=>this.#event(s,'action.allowed',{operation:'autonomy.set.requested',autonomy:a.autonomy,subject:s.console},null));
@@ -6664,6 +6713,54 @@ export class Broker {
if(s.human!==this.#business(s.business).human)fail('human-required');
return {actor:s.human,agent:false,rights:['org.hire'],owner:true,instanceOwner:true,subject:s.console};
}
// #1873 SP5 (design section 3, "The chief applying a pack"): hire.raise {proposal: {pack, project?,
// hire}, route, reason?}. Only the chief gets this far. The proposal is the latest packs entry of
// that pack id and project, in the organization as loaded, whose proposedHires names the hire. Its
// pin fills op, name, level and role; a caller's own role, name, level, op, credentials,
// personality or binding refuses. A template pin and a definition pin go to prepare as recorded,
// which refuses decision-stale and definition-rev-conflict. An inline pin is checked here: the
// stored entry must still hash to the pin (starter-pack-proposal-stale), and its from template
// must not be in launch.noHire (definition-not-hireable), since prepare's own noHire check
// reads only a template or definition ref. The returned input carries no proposal key, so
// prepare's request keys are unchanged. reason defaults to the one the pack recorded. The chief
// check and the role.hire check run before the packs list is read, so neither a non-chief nor a
// chief without role.hire learns whether a proposal exists.
#proposalHire(s, args) {
this.#agent(s);
const b = this.#business(s.business);
if (s.role !== b.launch?.by) fail('role-not-chief');
if (this.#classification(s, 'role.hire') !== 'within-role') fail('not-authorized');
keys(args, ['proposal', 'route', 'reason'], ['proposal', 'route']);
const proposal = args.proposal;
keys(proposal, ['pack', 'project', 'hire'], ['pack', 'hire']);
for (const value of [proposal.pack, proposal.hire, ...(Object.hasOwn(proposal, 'project') ? [proposal.project] : [])])
if (typeof value !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(value)) fail('invalid-request');
const project = proposal.project ?? null;
const entry = (Array.isArray(b.packs) ? b.packs : [])
.filter(e => e?.id === proposal.pack && (e.project ?? null) === project && Array.isArray(e.proposedHires) && e.proposedHires.some(h => h?.name === proposal.hire))
.at(-1);
if (!entry) fail('invalid-request');
const hire = entry.proposedHires.find(h => h.name === proposal.hire);
const pin = hire.pin;
if (!object(pin)) fail('invalid-request');
let role;
if (Object.hasOwn(pin, 'inline')) {
if (!object(hire.entry) || typeof pin.sha256 !== 'string') fail('invalid-request');
// The broker's sandbox import closure is fixed, so the digest and the composition are local
// copies: canonical is organization util.mjs canonicalJson, built as a string so integer-like
// keys keep the sorted order (an object would put them first), and the composition is
// definitions.mjs compositionOf.
const canonical = v => Array.isArray(v) ? `[${v.map(canonical).join(',')}]` : object(v)
? `{${Object.keys(v).sort().map(k => `${JSON.stringify(k)}:${canonical(v[k])}`).join(',')}}` : JSON.stringify(v);
if (sha256(canonical(hire.entry)) !== pin.sha256) fail('starter-pack-proposal-stale');
const from = hire.entry.from?.template;
if (from !== undefined && (b.launch?.noHire ?? []).includes(from)) fail('definition-not-hireable');
if (!object(hire.entry.role) || typeof hire.entry.instructions !== 'string') fail('invalid-request');
role = { inline: { ...hire.entry.role, instructions: hire.entry.instructions, ...(hire.entry.personality !== undefined ? { personality: hire.entry.personality } : {}) } };
} else role = structuredClone(pin);
const reason = Object.hasOwn(args, 'reason') ? args.reason : hire.reason;
return { op: 'hire', level: project === null ? 'org' : 'project', ...(project === null ? {} : { project }), name: hire.name, role, route: structuredClone(args.route), reason };
}
async #raiseHire(s, input, immediate) {
const b=this.#business(s.business);
// Change is a replacement composition and uses the existing role.hire grant.
@@ -6693,9 +6790,9 @@ export class Broker {
// A binding always goes to the owner; no autonomy level moves it (design section 4).
if(!immediate && !p.binding && level!=='in the loop' && p.level!=='root' && !p.flagged.length && !p.credentialLines.length) {
if(level==='out of the loop')fail('authority-limits-unavailable');
if(!this.#budgetCovered(this.#business(s.business)))fail('autonomy-precondition');
if(!this.#budgetCovered(this.#business(s.business)))fail('autonomy-precondition',BUDGET_UNCOVERED);
route=b.autonomy?.authorityResolver;
if(!b.roles[route]?.authority?.withinRole.includes('decision.resolve.authority'))fail('autonomy-precondition');
if(!b.roles[route]?.authority?.withinRole.includes('decision.resolve.authority'))fail('autonomy-precondition','the authority resolver is not a role with decision.resolve.authority');
if(route===s.role || resolverSubject(b,route,p.name))route='human';
}
this.#store.transaction(()=>{
@@ -6705,7 +6802,7 @@ export class Broker {
this.#event(s,'action.allowed',{operation:'decision.raise',decision:did,action,approvalChoice:'yes',target:p.name,hire:p,autonomyLevel:level},did);
});
if(immediate) return this.#hireConsole(s,'hire.resolve.console',{id:did,choice:'yes',digest:p.digest});
return {...this.#decisionView(this.#decision(s,did)),hire:p};
return {...this.#decisionView(this.#decision(s,did)),hire:bindingSummary(p)};
}
// The harness fact comes from the host's loaded modules; no host, no account coverage.
#budgetCovered(b) { return authorityBudgetCovered(b,{countsAccount:this.#autonomyHost?.countsAccount}); }
@@ -6726,7 +6823,7 @@ export class Broker {
if(this.#expireHire(s,d,p))fail('decision-expired');
if(this.#autonomyLevel(d)==='in the loop')fail('hire-resolution-required');
if(this.#autonomyLevel(d)==='out of the loop')fail('authority-limits-unavailable');
if(!this.#budgetCovered(this.#business(s.business)))fail('autonomy-precondition');
if(!this.#budgetCovered(this.#business(s.business)))fail('autonomy-precondition',BUDGET_UNCOVERED);
if(!this.#business(s.business).roles[s.role]?.authority?.withinRole.includes('decision.resolve.authority'))fail('not-resolver');
};
validate();
@@ -6746,7 +6843,7 @@ export class Broker {
const recheck=()=>{
this.#agent(s);
if(this.#autonomyLevel(d)==='out of the loop')fail('authority-limits-unavailable');
if(!this.#budgetCovered(this.#business(s.business)))fail('autonomy-precondition');
if(!this.#budgetCovered(this.#business(s.business)))fail('autonomy-precondition',BUDGET_UNCOVERED);
if(this.#autonomyLevel(d)==='in the loop' || !this.#business(s.business).roles[s.role]?.authority?.withinRole.includes('decision.resolve.authority') || resolverSubject(this.#business(s.business),s.role,p.name))fail('not-resolver');
};
recheck();
@@ -6929,6 +7026,18 @@ export class Broker {
if (e?.name === 'BusinessError') fail('invalid-request');
throw new BusError('outcome-unknown');
}
// #1873 SP8: a template refusal keeps its code and its reason (a path, never the text); anything
// else is read as a pack refusal.
#templateFail(e) {
if (e?.name === 'TemplateError' && TEMPLATE_CODES.includes(e.code)) {
// A reason names a field path or a definition name; one that would quote a held value is dropped.
// The check reads the whole reason before the cut, so a held value cut short is still found.
let reason = typeof e.reason === 'string' ? e.reason : undefined;
if (reason !== undefined) try { this.#secretCheck(reason); reason = reason.slice(0, 300); } catch { reason = undefined; }
throw new BusError(e.code, reason);
}
this.#packFail(e);
}
// The bus code for an organization writer's refusal. Anything the writer didn't refuse by name is
// outcome-unknown: the write may have committed.
#organizationFail(e, verb) {
@@ -8589,25 +8698,6 @@ export class Broker {
.filter((p) => !s.user || this.#granted(s, 'project.list', p.id, null))
.map((p) => (p.state === 'archive' ? { id: p.id, at: p.at, name: p.name, repo: p.repo, archived: true } : { id: p.id, at: p.at, name: p.name, repo: p.repo }));
}
case 'project.system-types':
case 'project.system-types.set': {
this.#human(s);
const write = verb === 'project.system-types.set';
keys(a, write ? ['project', 'systemTypes'] : ['project'], write ? ['project', 'systemTypes'] : ['project']);
const project = identifier(a.project);
if (!this.#store.get('SELECT 1 FROM projects WHERE id=? AND business=?', project, s.business)) fail('project-not-found');
// When configured, read the trusted file each time; a stale bus snapshot must not reactivate a disabled type.
const enabled = this.#organization ? this.#organization(s.business, 'system-types.current', {}, s.human) : (b.plugins?.['system-type'] ?? []);
if (!Array.isArray(enabled)) fail('not-configured');
if (write) {
if (!Array.isArray(a.systemTypes) || a.systemTypes.some(x => typeof x !== 'string') || new Set(a.systemTypes).size !== a.systemTypes.length) fail('invalid-request');
if (a.systemTypes.some(x => !enabled.includes(x))) fail('system-type-not-enabled');
this.#store.run('INSERT INTO project_system_types(at,business,project,types,by) VALUES(?,?,?,?,?)', this.#now(), s.business, project, JSON.stringify(a.systemTypes), this.#owner(s));
}
const row = this.#store.get('SELECT types FROM project_system_types WHERE business=? AND project=? ORDER BY seq DESC LIMIT 1', s.business, project);
const selected = row ? JSON.parse(row.types) : [];
return { project, selected, active: selected.filter(x => enabled.includes(x)), inactive: selected.filter(x => !enabled.includes(x)) };
}
// #797 MVP3 (owner report 2026-11-06): the owner's reversible retirement of a project. The
// human gate is project.create's minus the tester carve-out: tester, user and agent sessions
// are refused here. Each verb appends one project_archives row (the audit trail) and deletes
@@ -9404,6 +9494,7 @@ export class Broker {
try {
r[k] = JSON.parse(r[k]);
} catch {}
if (table === 'events' && object(r.body?.hire)) r.body = { ...r.body, hire: bindingSummary(r.body.hire) };
rows.set(table + ':' + row.seq, r);
}
};
+14 -3
View File
@@ -1,5 +1,11 @@
import { connect } from 'node:net';
import { BusError, PROJECT_SAVE_VERB, PROJECT_SAVE_REQUEST_MAX } from './broker.mjs';
// E2E6 fix 6: a connect that fails with one of these never sent the request. The code stays
// outcome-unknown for every caller that reads it (the harness, the Console), and the error is marked
// notSent so the human and tester shims can exit with NOT_SENT_EXIT and `mosaic` can say the bus is
// not running.
const NOT_SENT = new Set(['ENOENT', 'ECONNREFUSED', 'ENOTDIR', 'ENOTSOCK']);
export const NOT_SENT_EXIT = 5;
export class Client {
constructor({ path, cap, human, tester, user, console: owner, timeout = 5000 }) {
this.path = path;
@@ -22,7 +28,8 @@ export class Client {
return new Promise((resolve, reject) => {
const socket = connect(this.path);
let input = Buffer.alloc(0),
settled = false;
settled = false,
connected = false;
const end = (error, value) => {
if (settled) return;
settled = true;
@@ -30,8 +37,12 @@ export class Client {
error ? reject(error) : resolve(value);
};
socket.setTimeout(['processing.halt', 'processing.resume'].includes(verb) ? Math.max(this.timeout, 20000) : this.timeout, () => end(new BusError('outcome-unknown')));
socket.on('connect', () => socket.write(request));
socket.on('error', () => end(new BusError('outcome-unknown')));
socket.on('connect', () => { connected = true; socket.write(request); });
socket.on('error', (error) => {
const unknown = new BusError('outcome-unknown');
if (!connected && NOT_SENT.has(error?.code)) unknown.notSent = true;
end(unknown);
});
socket.on('end', () => end(new BusError('outcome-unknown')));
socket.on('data', (b) => {
input = Buffer.concat([input, b]);
+29
View File
@@ -37,6 +37,7 @@ import { boundRepositories, tokenLevels, verifyGiteaToken } from './copy-grant.m
import { readOrgEntrySync } from '../../organization/src/org-registry.mjs';
import { allGranted, CAUSE_CODES, copyBotLogin, definitionPin, resolveCopyBinding } from '../../organization/src/copy-bindings.mjs';
import { COPY_BINDABLE, materializeCopies } from '../../organization/src/agent-copies.mjs';
import { copyBindingStatus } from '../../organization/src/copy-binding-status.mjs';
import { checkDefinitions, readTemplate } from '../../organization/src/definitions.mjs';
import { loadRole } from '../../organization/src/role.mjs';
import { resolveAgentLevels } from '../../organization/src/agent-levels.mjs';
@@ -376,6 +377,34 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
if (current(business, instance).authority !== digest) deny('copy-authority-changed');
sameRevision(business, revision);
},
// CB5: the copy's binding status for its owner's views, as copy-binding-status.mjs's fixed
// { status, next }. The same checks a launch runs, without the loss report (the sweep makes it)
// and without returning anything the check read. An approved binding not yet issued, or one a
// refused issue left pending, needs the owner's issue before anything else. `entry` is the copy's
// boot entry: only one bound at boot, or unbound for want of a service, has a status. One that
// lacks a service a copy cannot bind is refused at boot whatever it binds (above), so it reads
// copy-service-unbindable and never asks for a gitea binding, unless a revoke of a binding it
// was issued anyway is running or failed: that state is the owner's next step.
status(business, instance, entry) {
const unbound = entry?.state === 'unbound' && Array.isArray(entry.missing) && entry.missing.length > 0;
if (!Array.isArray(entry?.bound) && !unbound) return null;
const revoking = business === org && (Boolean(fences.get(instance)) || closed.has(instance));
if (unbound && !revoking && entry.missing.some((s) => !COPY_BINDABLE.includes(s))) return copyBindingStatus('copy-service-unbindable');
let code = 'ready';
try {
check(business, instance);
} catch (e) {
code = typeof e?.code === 'string' ? e.code : 'credential-unavailable';
}
if (['credential-unbound', 'credential-revoked', 'restart-required'].includes(code)) {
let record = null;
try {
record = read(business).copyBindings?.[instance];
} catch {}
if (object(record) && record.state === 'pending') code = 'issue-required';
}
return copyBindingStatus(code);
},
revoking: (business, instance) => business === org && (Boolean(fences.get(instance)) || closed.has(instance)),
// CB4: shut the copy before its revoke publishes anything. It admits nothing and every check
// answers `code` until open(); the boot's proof of it is forgotten, so nothing reopens it but a
+4 -2
View File
@@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import { Client } from './client.mjs';
import { Client, NOT_SENT_EXIT } from './client.mjs';
import { readProcess } from './human.mjs';
const file = fileURLToPath(import.meta.url);
if (!process.env.MOSAIC_BUS_CLI_NONCE) {
@@ -39,5 +39,7 @@ try {
process.stdout.write(JSON.stringify(result) + '\n');
} catch (e) {
process.stderr.write((/^[a-z-]{1,64}$/.test(e.code ?? '') ? e.code : 'invalid-request') + '\n');
process.exitCode = 2;
// E2E6 fix 6: a request that never left (no socket, or the connect refused) keeps its code and
// exits NOT_SENT_EXIT, so `mosaic` can say the bus is not running. Every other failure exits 2.
process.exitCode = e?.notSent === true ? NOT_SENT_EXIT : 2;
}
+11
View File
@@ -0,0 +1,11 @@
-- #1873 SP9: the system-type kind is removed outright (Jason, 2026-11-12: "The only deployed
-- system is the dev environment on dragon-lin. It can be wiped and reconfigured. No reason to hold
-- on to legacy for this change."). This step drops what 3t-3u created: the four guards, the index
-- and the table, with its rows. It is the one deletion of append-only rows in that go, and the
-- ruling is its authority. SQLite removes the table's sqlite_sequence row with the table.
DROP TRIGGER project_system_types_no_update;
DROP TRIGGER project_system_types_no_delete;
DROP TRIGGER project_system_types_no_replace;
DROP TRIGGER project_system_types_business;
DROP INDEX project_system_types_project;
DROP TABLE project_system_types;
+21 -1
View File
@@ -183,7 +183,7 @@ group(
"Read my_rails and use the applicable decision path; ask the owner to inspect grants if no supported path exists."
);
group(
"not-configured project-already-archived project-archived project-exists project-not-archived project-not-configured project-not-found project-record-invalid project-remote-configured project-remote-invalid project-remote-library project-remote-protected project-remote-unknown project-spec-human-only system-type-not-enabled tracker-not-store usage-not-configured",
"not-configured project-already-archived project-archived project-exists project-not-archived project-not-configured project-not-found project-record-invalid project-remote-configured project-remote-invalid project-remote-library project-remote-protected project-remote-unknown project-spec-human-only tracker-not-store usage-not-configured",
"R3/role scope: configured project and service state",
"Read the supported project/service status and ask its owner to repair missing configuration or state before retrying."
);
@@ -211,6 +211,26 @@ group(
"R8: apply writes only the plan its digest names",
"The organization file or the pack changed since the preview. Preview again and apply the new digest."
);
// #1873 SP5: a hire raised from a pack proposal is the role the proposal pinned. An inline pin is the
// canonical hash of the entry stored beside it; a stored entry that no longer hashes to it refuses.
group(
"starter-pack-proposal-stale",
"R8: a hire raised from a pack proposal matches its recorded pin",
"The role entry stored with this proposal no longer matches the hash it was recorded with. Preview and apply the pack again for a fresh proposal, then raise the hire from that one."
);
// #1873 SP8: a saved template carries no credential the bus holds. Save checks every kept string and
// key against the credential store; a hit names the field, never the text.
group(
"template-secret-suspected",
"R1/R10: a saved template carries no credential value the bus holds",
"A field the template would keep holds a credential value. Remove it from the named definition, board or workflow text, then preview and save again."
);
// E2E6 fix 8: save never replaces a template already saved under the name.
group(
"template-exists",
"R10: a saved template is never replaced",
"A template of this name is already saved for your account. Save under another name, or list your saved templates to pick a free one."
);
group(
"not-resolver",
"Role resolve right and decision route",
+60 -11
View File
@@ -27,7 +27,6 @@ import { readSessions, sessionsFile, stopSession } from '../../seat/src/session.
import { compactionCount, storeDir } from '../../seat/src/compactions.mjs';
import { publishRemote, workingRepo, WorkingError } from '../../seat/src/working.mjs';
import { createLoader, hasModule } from '../../plugins/src/loader.mjs';
import { installedSystemTypes } from '../../plugins/src/system-types.mjs';
import { pluginSettings } from '../../organization/src/plugin-settings.mjs';
import { loadManagementClient } from '../../model-proxy/src/client.mjs';
import { ProxyPage } from './proxy-page.mjs';
@@ -42,6 +41,7 @@ import { organizationWriter, OrganizationError, setReplyLimits } from '../../org
import { definitionVerbs, DEFINITION_BUS_VERBS } from '../../organization/src/definition-verbs.mjs';
import { ORG_PREFIX, orgDefinitionName, compositionOf, readTemplate, entryFromTemplate, lineDiff, PRIMITIVES } from '../../organization/src/definitions.mjs';
import { installedStarterPacks, readSavedTemplate } from '../../plugins/src/starter-pack.mjs';
import { TemplateError, templateList, templatePreview, templateSave } from '../../organization/src/template-save.mjs';
import { checkComposition } from '../../organization/src/composition.mjs';
import { validateBusinessDocument } from '../../organization/src/organization-config.mjs';
import { DEFINITION_HOLDS_CREDENTIAL, heldPath, scanDefinitions } from './definition-scan.mjs';
@@ -351,7 +351,18 @@ function packOps({ writer, rolesDir, dataRoot, system, registryFor, assertClean,
// A saved template may reuse a shipped id, so the origin is part of the key.
const sameApply = (e, id, origin, project) => e.id === id && e.origin === origin && (e.project ?? null) === project;
const tombstones = (doc, id, origin, project) => new Set((doc.packs ?? []).filter(e => sameApply(e, id, origin, project)).flatMap(e => e.added ?? []));
// #1873 SP5 (design section 3, "The chief applying a pack"): context.agent is an
// organization.change holder's call. It names no saved template and no definitions rename
// (human-required), and its plan writes no definition: a definition a human apply would add is
// marked not-written, and its hire proposal pins the template or the inline entry, which the chief
// raises as a hire decision. A workflow step naming a pack definition the organization does not
// hold and that is not a V2 template is left out and listed; a workflow left with no step is
// marked left-out and not written. applyPack refuses an agent's definitions or saved origin
// whatever this builds.
async function build(id, args, actor, context) {
const agent = context?.agent === true;
if (agent && typeof args.pack === 'string' && args.pack.startsWith(SAVED_PREFIX)) throw new BusError('human-required');
if (agent && Array.isArray(args.as) && args.as.some(v => typeof v === 'string' && v.startsWith('definitions.'))) throw new BusError('human-required');
const { document: doc, revision } = writer.read(id);
const found = await loadPack(args.pack, actor);
const pack = found.pack;
@@ -379,6 +390,16 @@ function packOps({ writer, rolesDir, dataRoot, system, registryFor, assertClean,
const chief = chiefOf(doc, `${id}.json`);
const registry = registryFor(doc);
const noHire = doc.launch?.noHire ?? [];
const templates = new Map();
const isTemplate = name => {
if (!templates.has(name)) {
try { readTemplate(rolesDir, name); templates.set(name, true); } catch (error) {
if (error?.name !== 'BusinessError') throw error;
templates.set(name, false);
}
}
return templates.get(name);
};
const items = [];
const plan = {};
// Definitions are organization-wide in either scope: a project's agents name org definitions.
@@ -400,6 +421,7 @@ function packOps({ writer, rolesDir, dataRoot, system, registryFor, assertClean,
const primitives = PRIMITIVES.filter(k => canonicalJson(before[k] ?? null) !== canonicalJson(after[k] ?? null));
item.diff = { primitives, instructions: primitives.includes('instructions') ? lineDiff(before.instructions, after.instructions) : [] };
}
if (agent && mark === 'add') item.mark = mark = 'not-written';
if (mark === 'add') definitions[target] = entry;
checked[name].added = mark === 'add';
items.push(item);
@@ -423,11 +445,24 @@ function packOps({ writer, rolesDir, dataRoot, system, registryFor, assertClean,
const workflow = { description: value.description, steps: value.steps.map(s => ({ title: s.title, state: s.state, roles: s.roles.map(role) })) };
if (workflow.steps.some(s => new Set(s.roles).size !== s.roles.length)) bad();
const path = `${prefix}workflows.${target}`;
const leftOut = [];
if (agent) {
// A step keeps a pack role the organization already defines, or one the pack itself pins to the
// template of that name. An inline definition that only shares a template's name is left out.
const unwritable = r => r !== 'human' && Object.hasOwn(pack.definitions, r) && !Object.hasOwn(doc.definitions ?? {}, role(r))
&& !(checked[r].ref?.template === role(r) && isTemplate(role(r)));
workflow.steps = workflow.steps.filter((s, i) => {
const missing = value.steps[i].roles.filter(unwritable).map(role);
if (missing.length) leftOut.push({ step: i + 1, title: s.title, roles: missing });
return missing.length === 0;
});
}
const current = scope.workflows?.[target];
let mark;
if (current === undefined) mark = gone.has(path) ? 'removed' : 'add';
if (agent && workflow.steps.length === 0) mark = 'left-out';
else if (current === undefined) mark = gone.has(path) ? 'removed' : 'add';
else mark = canonicalJson(current) === canonicalJson(workflow) ? 'same' : 'kept';
items.push({ item: 'workflow', name, target, path, mark });
items.push({ item: 'workflow', name, target, path, mark, ...(leftOut.length ? { leftOut } : {}) });
if (mark === 'add') workflows[target] = workflow;
}
if (Object.keys(workflows).length) plan[`${prefix}workflows`] = workflows;
@@ -453,7 +488,10 @@ function packOps({ writer, rolesDir, dataRoot, system, registryFor, assertClean,
items, hires, skills: { notEnabled }, chief: chief !== null, plan, digest,
};
}
const write = (id, plan, revision, actor, detail) => {
// context.beforeWrite (SP5) is the broker's recheck of an agent's organization.change grant,
// run once more before the writer commits.
const write = (id, plan, revision, actor, detail, context) => {
context?.beforeWrite?.();
const out = writer.applyPack(id, plan, { revision, actor, assertClean, detail });
return { revision: out.revision, added: out.added };
};
@@ -479,15 +517,15 @@ function packOps({ writer, rolesDir, dataRoot, system, registryFor, assertClean,
if (Object.keys(plan).every(k => k === 'packs') && last && last.sha256 === plan.packs.sha256
&& canonicalJson(last.proposedHires ?? []) === canonicalJson(plan.packs.proposedHires))
return { revision: preview.revision, added: [], pack: preview.pack.id };
return { ...write(id, plan, preview.revision, actor, {}), pack: preview.pack.id };
return { ...write(id, plan, preview.revision, actor, {}, context), pack: preview.pack.id };
},
'board.set': (id, args, actor) => {
'board.set': (id, args, actor, context) => {
if (args.board !== null && !isObject(args.board)) bad();
return write(id, { [scoped(args, 'board')]: args.board }, revisionOf(args), actor, {});
return write(id, { [scoped(args, 'board')]: args.board }, revisionOf(args), actor, {}, context);
},
'workflow.set': (id, args, actor) => {
'workflow.set': (id, args, actor, context) => {
if (typeof args.name !== 'string' || (args.workflow !== null && !isObject(args.workflow))) bad();
return write(id, { [scoped(args, 'workflows')]: { [args.name]: args.workflow } }, revisionOf(args), actor, {});
return write(id, { [scoped(args, 'workflows')]: { [args.name]: args.workflow } }, revisionOf(args), actor, {}, context);
},
};
}
@@ -1140,6 +1178,7 @@ export async function startBroker({
}
// #1873 SP4: the human's pack, board and workflow ops. The broker passes only the business's human;
// the reads return here, and each write reloads the organization and refreshes its limit checks.
// SP5: or an organization.change holder's actor, with context.agent and context.beforeWrite.
if (Object.hasOwn(PACK_OPS, verb)) {
const ops = packOps({ writer: organizationWriter({dir:organization.dir,rolesDir:organization.rolesDir}), rolesDir: organization.rolesDir, dataRoot: instanceRoot ?? dataRoot,
system: organization.system, registryFor: doc => organizationPlugins(instanceRoot ?? dataRoot, doc.plugins).registry, assertClean: value => credentials.assertClean(value), configDir: organization.dir });
@@ -1148,8 +1187,18 @@ export async function startBroker({
const committed = value => { reload(id); limits.refresh(id).catch(e=>limitLog({event:'limit-refresh-failed',business:id,message:e.message})); return value; };
return out?.then ? out.then(committed) : committed(out);
}
if (verb === 'system-types.installed') return installedSystemTypes({dataRoot:instanceRoot ?? dataRoot});
if (verb === 'system-types.current') return settings.current(id).plugins?.['system-type'] ?? [];
// #1873 SP8: saving a template and listing the owner's own. The broker passes only the owner's
// human; both read the organization file and neither writes it, so nothing reloads.
if (verb === 'template.save' || verb === 'template.list') {
const base = { rolesDir: organization.rolesDir, configDir: organization.dir, account: actor };
if (verb === 'template.list') return templateList(base);
const { name, project, preview, digest } = args;
const document = organizationWriter({dir:organization.dir,rolesDir:organization.rolesDir}).read(id).document;
const assertClean = value => credentials.assertClean(value);
if (preview === true && digest === undefined) return templatePreview({ document, name, project, rolesDir: organization.rolesDir, assertClean });
if (preview === undefined && typeof digest === 'string') return templateSave({ ...base, document, name, project, digest, assertClean });
throw new TemplateError('invalid-request', 'template.save takes preview: true or the preview digest');
}
if(verb==='limits')return withTokens(id,settings.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.
+7 -3
View File
@@ -13,8 +13,8 @@ import { join, isAbsolute } from 'node:path';
import { createHash } from 'node:crypto';
const ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8');
export const SCHEMA_SHA256 = 'e5868996851ee60a3c6d2798a17b9da446ba666c859d84d1b575ca5104ae61f8';
export const SCHEMA_VERSION = '3v';
export const SCHEMA_SHA256 = '1c305d842619360c14c8388cd43cb9de81afbe46ff0662fd5de9851be9ff3ea0';
export const SCHEMA_VERSION = '3w';
// Ordered, named offline steps. New schema changes append a step and keep prior DDL pinned.
const legacyDDL = readFileSync(new URL('./migrations/3b.sql', import.meta.url), 'utf8');
const LEGACY_SHA256 = '179ffe356d4ff19a49b5ebad39b6c6bfd7771deb8e1b7c55b5e746f039d69e65';
@@ -120,6 +120,11 @@ const migrations = [{
// Only a new append-only table, its index and its guards; no existing row changes.
from: '3u', to: '3v',
sql: readFileSync(new URL('./migrations/3u-3v.sql', import.meta.url), 'utf8'),
}, {
// SP9 (#1873, Jason's ruling 2026-11-12): drops the table 3t-3u created, its index and its four
// guards, with its rows.
from: '3v', to: '3w',
sql: readFileSync(new URL('./migrations/3v-3w.sql', import.meta.url), 'utf8'),
}];
const timedTables = [
'events',
@@ -146,7 +151,6 @@ const timedTables = [
'request_snapshot_events',
'request_project_events',
'project_archives',
'project_system_types',
'store_tasks',
];
const hex = (value) => createHash('sha256').update(value).digest('hex');
+3 -2
View File
@@ -15,7 +15,7 @@ if (!process.env.MOSAIC_BUS_TESTER_NONCE) {
// P19 #876: only the re-executed child, whose initial environment holds the nonce, talks to the
// bus. Loading the client (and the broker module graph behind it) here instead of at the top
// keeps the re-exec parent to a bare node start.
const { Client } = await import('./client.mjs');
const { Client, NOT_SENT_EXIT } = await import('./client.mjs');
const { readProcess } = await import('./human.mjs');
try {
const request = JSON.parse(readFileSync(0, 'utf8'));
@@ -39,5 +39,6 @@ try {
process.stdout.write(JSON.stringify(result) + '\n');
} catch (e) {
process.stderr.write((/^[a-z-]{1,64}$/.test(e.code ?? '') ? e.code : 'invalid-request') + '\n');
process.exitCode = 2;
// E2E6 fix 6: as human-cli.mjs, a request that never left exits NOT_SENT_EXIT.
process.exitCode = e?.notSent === true ? NOT_SENT_EXIT : 2;
}
@@ -19,7 +19,7 @@ const here = (p) => new URL(p, import.meta.url);
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
// Row 206 (#1704): and the 3l→3m step's thread_shares. thread-shares.test.mjs tests it.
// Row 271 (#1776): and the 3m→3n step's session_ops and message_cancels. session-clear.test.mjs tests them.
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'project_system_types', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
function source3h(t) {
const root = mkdtempSync(join(tmpdir(), 'account-migrate-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
@@ -96,6 +96,7 @@ async function engine(root, name, { store, schema, step = (s) => s }) {
copyFileSync(here('../src/migrations/3s-3t.sql'), join(moduleRoot, 'src/migrations/3s-3t.sql'));
copyFileSync(here('../src/migrations/3t-3u.sql'), join(moduleRoot, 'src/migrations/3t-3u.sql'));
copyFileSync(here('../src/migrations/3u-3v.sql'), join(moduleRoot, 'src/migrations/3u-3v.sql'));
copyFileSync(here('../src/migrations/3v-3w.sql'), join(moduleRoot, 'src/migrations/3v-3w.sql'));
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
}
@@ -103,7 +104,7 @@ test('REQ-LIMIT-4 3h copy migrates to 3i: every row keeps its seq, the sequence
const { root, path } = source3h(t), before = snapshot(path), was = master(path);
assert.throws(() => new Store(root), /schema-mismatch/);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
// Every table's rows as they were, usage_records with its seq values and sqlite_sequence with 50.
assert.deepEqual(migrated3l(snapshot(path, before.columns).rows), before.rows);
assert.deepEqual(before.rows.usage_records.map(r => r.seq), [1, 2, 3]);
@@ -153,7 +154,7 @@ test('REQ-LIMIT-4 a 3h→3i step that fails partway leaves the store at 3h with
assert.deepEqual(snapshot(path, before.columns).rows, before.rows);
// Still a whole 3h store: the real step migrates it.
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(migrated3l(snapshot(path, before.columns).rows), before.rows);
});
@@ -165,14 +166,14 @@ test('REQ-LIMIT-4 the 3h engine refuses a migrated store, migrating or not, and
schema: ddl3h,
store: (source) => source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3h).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3h';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3h';")
.replace(/\}, \{\n {2}\/\/ Row 126 \(#1620\)[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables'),
});
const pinned = readFileSync(join(root, 'engine-3h/src/store.mjs'), 'utf8');
assert.ok(pinned.includes("export const SCHEMA_VERSION = '3h';") && !pinned.includes('3h-3i'), 'the 3h engine has no 3h→3i step');
assert.throws(() => new Store3h(root), /schema-mismatch/);
assert.throws(() => new Store3h(root, { migrateSchema: true }), /schema-mismatch/);
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(master(path), was);
assert.deepEqual(snapshot(path, after.columns).rows, after.rows);
});
@@ -64,7 +64,7 @@ test('AU authority reverse crossing deadline in prepared IO never publishes',asy
});
test('AU authority resolver rechecks budget removal after a proposal was raised',async t=>{
const f=world(t),d=await f.raise(),b=structuredClone(f.loaded());b.plugins['limit-check']=[];f.broker.reloadBusiness('demo',b);
const before=f.writer.read('demo').revision;await assert.rejects(f.call('judge','decision.resolve',{id:d.id,choice:'yes'}),{code:'autonomy-precondition'});
const before=f.writer.read('demo').revision;await assert.rejects(f.call('judge','decision.resolve',{id:d.id,choice:'yes'}),{code:'autonomy-precondition',reason:'no enforced budget covers new agents; authority away from the loop needs one'});
assert.equal(f.writer.read('demo').revision,before);assert.equal(f.store.get('SELECT count(*) n FROM decision_events WHERE decision=?',d.id).n,0);
});
@@ -113,7 +113,7 @@ test('AU an account-quota authority raise routes to the resolver only when the h
const covered=world(t,{account:true,countsAccount:name=>name==='pi'}),d=await covered.raise();assert.equal(d.route_to,'judge');
for(const countsAccount of [undefined,()=>false,()=>null]){
const f=world(t,{account:true,countsAccount});
await assert.rejects(f.raise(),{code:'autonomy-precondition'});
await assert.rejects(f.raise(),{code:'autonomy-precondition',reason:'no enforced budget covers new agents; authority away from the loop needs one'});
assert.equal(f.store.get("SELECT count(*) n FROM decisions").n,0);
}
});
+12 -4
View File
@@ -137,11 +137,11 @@ test('AU authority near requires an enforced organization budget covering every
const f=await settingsFixture(t);
for(const tokens of [{},{instances:{coder:tokenCap}},{providers:{unrelated:tokenCap}}]){
f.business.launch.limits.tokens=tokens;f.broker.reloadBusiness('demo',f.business);
await assert.rejects(f.set(authorityNear),{code:'autonomy-precondition'});
await assert.rejects(f.set(authorityNear),{code:'autonomy-precondition',reason:'no enforced budget covers new agents; authority away from the loop needs one'});
}
f.business.launch.limits.tokens={providers:{anthropic:{...tokenCap,limit:0}}};
f.business.plugins={'limit-check':[]};f.broker.reloadBusiness('demo',f.business);
await assert.rejects(f.set(authorityNear),{code:'autonomy-precondition'});
await assert.rejects(f.set(authorityNear),{code:'autonomy-precondition',reason:'no enforced budget covers new agents; authority away from the loop needs one'});
f.business.plugins={'limit-check':['token-limit']};f.broker.reloadBusiness('demo',f.business);
await f.set(authorityNear);assert.equal(f.writes(),1);
});
@@ -153,12 +153,20 @@ test('AU authority out refuses until per-definition agent limits exist, even wit
await assert.rejects(f.set({...authorityNear,authority:'out of the loop'}),e=>e.code==='authority-limits-unavailable' && /near the loop/.test(e.nextStep));
assert.equal(f.writes(),1);
});
test('AU a class moves out of the loop only from near the loop, and the refusal says so (E2E6 fix 7)',async t=>{
const f=await settingsFixture(t);await f.set({'cross-role':'in the loop'});
await assert.rejects(f.set({'cross-role':'out of the loop',notices:true}),{code:'autonomy-precondition',reason:'cross-role moves out of the loop only from near the loop'});
assert.equal(f.writes(),1);
});
test('AU moving out requires an actual near resolution and a later owner review',async t=>{
const f=await settingsFixture(t);await f.set({merge:'in the loop'});await f.set({merge:'near the loop',notices:true});
// E2E6 fix 7: each refusal names the check that failed.
await assert.rejects(f.set({merge:'near the loop'}),{code:'autonomy-precondition',reason:'owner notices are off; a class near the loop needs them on'});
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition',reason:'merge needs your review of its trail since the last autonomy change'});
f.call(f.owner,'autonomy.review.console',{decisionClass:'merge'});
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition'});
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition',reason:'merge needs a near-loop decision since the last autonomy change, and your trail review after it'});
const d=f.raise({action:'git.merge.protected'});f.call(f.chief,'decision.resolve',{id:d.id,choice:'yes'});
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition'});
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition',reason:'merge needs a near-loop decision since the last autonomy change, and your trail review after it'});
f.call(f.owner,'autonomy.review.console',{decisionClass:'merge'});
await f.set({merge:'out of the loop',notices:true});
});
+5 -2
View File
@@ -11,6 +11,8 @@
// which shares humanCall's spawn as transport(). The human proof and its spawn are unchanged.
// Row 225 (C2-10b) re-pins webui bus.mjs for one STATUS entry, library-revert-not-agent: 409; no proof
// or transport change.
// E2E6 fix 6 re-pins human-cli.mjs, which exits 5 when its connect failed before the request was
// written, and cli transport.mjs, which reads that exit as bus-not-running. The proof is unchanged.
import test from "node:test";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
@@ -21,9 +23,10 @@ import { Client } from "../src/client.mjs";
const baselines = {
"packages/bus/src/human.mjs": "bb13c146717cdc46d75d157177e400edac4d3e10f14fe511027a25fd44cec885",
"packages/bus/src/human-cli.mjs": "271b9781d73cf31cc562a56180e3ebcd725b5fde148a83347a201fdef2fc28af",
"packages/bus/src/human-cli.mjs": "410892787cc70440b0ba4c45d6f24861d12ee5e70cba678cfeb1ed86b796c149",
// Row 239 (#1745): EXIT outcome-unknown-revoked 1; no proof or transport change.
"packages/cli/src/transport.mjs": "662642a8ce739fcb6c188ad07125a94e008c8a305eced127c8041d472c56e55b",
// E2E6 fix 6: shim exit 5 with outcome-unknown becomes bus-not-running (exit 3, start and recover next).
"packages/cli/src/transport.mjs": "101870f7ffb47a9cbd54ebd8c1901734cc9f442d6770cb2a0d059afd4d14b160",
// Row 165 (#1662): seven STATUS entries for the role instance routes (organization-instance-exists,
// organization-unknown-instance, organization-credentials-out-of-scope, organization-launch-not-configured,
// organization-no-change, organization-project-mismatch, organization-unresolvable-instance), no proof or
+2 -2
View File
@@ -615,7 +615,7 @@ async function database3c(t) {
let source = readFileSync(here('../src/store.mjs'), 'utf8');
const pinned = source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3c).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3c';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3c';")
.replace(/\}, \{\n {2}from: '3c',[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
assert.notEqual(pinned, source);
assert.ok(!pinned.includes('3c-3d'), 'the 3c engine has no 3c→3d step');
@@ -639,7 +639,7 @@ test('REQ-ROLE-2, REQ-COS-1: the 3c→3d migration lets the chief of staff end a
assert.throws(() => new Store(root), /schema-mismatch/);
const store = new Store(root, { migrateSchema: true });
t.after(() => store.close());
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
assert.equal(store.get("SELECT body FROM events WHERE id='preserved'").body, '{"fixture":true}');
assert.equal(store.get("SELECT holder_run FROM role_claims WHERE role='coder'").holder_run, 'coder-run');
const b = new Broker({ store, businesses: demo(), organization: null });
@@ -33,7 +33,7 @@ test('explicit 3b copy migration preserves evidence and opens as 3g; normal star
const original = readFileSync(source);
assert.throws(() => new Store(root), /schema-mismatch/);
const store = new Store(root, { migrateSchema: true });
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
assert.equal(store.get("SELECT body FROM events WHERE id='preserved'").body, '{"fixture":true}');
assert.equal(store.get('SELECT count(*) n FROM task_comment_cursors').n, 0);
const epoch = store.get("SELECT value FROM meta WHERE key='comment_cursor_epoch'").value;
@@ -75,15 +75,15 @@ test('named migration registry chains 3b through 3g to a fixture-only future ver
const sha = createHash('sha256').update(futureDDL).digest('hex');
let source = readFileSync(new URL('../src/store.mjs', import.meta.url), 'utf8');
source = source.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${sha}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3w';")
.replace('}];\nconst timedTables', `}, { from: '3v', to: '3w', sql: ${JSON.stringify(futureSQL)} }];\nconst timedTables`);
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3x';")
.replace('}];\nconst timedTables', `}, { from: '3w', to: '3x', sql: ${JSON.stringify(futureSQL)} }];\nconst timedTables`);
writeFileSync(join(moduleRoot, 'src/store.mjs'), source);
writeFileSync(join(moduleRoot, 'schema.sql'), futureDDL);
for (const name of ['3b.sql', '3b-3c.sql', '3c-3d.sql', '3d-3e.sql', '3e-3f.sql', '3f-3g.sql', '3g-3h.sql', '3h-3i.sql', '3i-3j.sql', '3j-3k.sql', '3k-3l.sql', '3l-3m.sql', '3m-3n.sql', '3n-3o.sql', '3o-3p.sql', '3p-3q.sql', '3q-3r.sql', '3r-3s.sql', '3s-3t.sql', '3t-3u.sql', '3u-3v.sql'])
for (const name of ['3b.sql', '3b-3c.sql', '3c-3d.sql', '3d-3e.sql', '3e-3f.sql', '3f-3g.sql', '3g-3h.sql', '3h-3i.sql', '3i-3j.sql', '3j-3k.sql', '3k-3l.sql', '3l-3m.sql', '3m-3n.sql', '3n-3o.sql', '3o-3p.sql', '3p-3q.sql', '3q-3r.sql', '3r-3s.sql', '3s-3t.sql', '3t-3u.sql', '3u-3v.sql', '3v-3w.sql'])
copyFileSync(new URL('../src/migrations/' + name, import.meta.url), join(moduleRoot, 'src/migrations', name));
const { Store: FutureStore } = await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')));
const future = new FutureStore(root, { migrateSchema: true });
assert.equal(future.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
assert.equal(future.get("SELECT value FROM meta WHERE key='schema_version'").value, '3x');
assert.equal(future.get("SELECT name FROM sqlite_master WHERE name='future_fixture'").name, 'future_fixture');
assert.equal(future.get("SELECT count(*) n FROM task_comment_cursors").n, 0);
assert.equal(future.get("SELECT id FROM events").id, 'preserved');
@@ -109,7 +109,7 @@ test('offline command migrates only the supplied fixture root and reports usage
const command = new URL('../src/migrate-schema.mjs', import.meta.url).pathname;
const migrated = spawnSync(process.execPath, [command, root], { encoding: 'utf8' });
assert.equal(migrated.status, 0, migrated.stderr);
assert.match(migrated.stdout, /bus schema 3v ready/);
assert.match(migrated.stdout, /bus schema 3w ready/);
new Store(root).close();
const usage = spawnSync(process.execPath, [command], { encoding: 'utf8' });
assert.equal(usage.status, 2);
+4 -2
View File
@@ -89,10 +89,12 @@ function setup(t) {
// #797 / #1839 adds the owner-only artifact read to the existing decision and request controls;
// no unsuffixed verb is ever a console verb.
test('N4: owner Console verbs have an explicit immutable whitelist and exclude decision.resolve', () => {
assert.deepEqual([...CONSOLE_VERBS], ['system-types.console', 'project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
assert.deepEqual([...CONSOLE_VERBS], ['project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
'request.get.console', 'request.projects.console', 'request.answer.console', 'request.attach.console', 'request.cancel.console',
// Starter packs SP4 (#1873): the owner's pack list and preview, and the three pack writes.
'packs.console', 'pack.preview.console', 'pack.apply.console', 'board.set.console', 'workflow.set.console']);
'packs.console', 'pack.preview.console', 'pack.apply.console', 'board.set.console', 'workflow.set.console',
// Starter packs SP8 (#1873): the owner saves and lists templates.
'template.save.console', 'template.list.console']);
assert.ok(CONSOLE_VERBS.every(verb => verb.endsWith('.console')));
assert.equal(CONSOLE_VERBS.includes('decision.resolve'), false);
assert.ok(Object.isFrozen(CONSOLE_VERBS));
+49
View File
@@ -133,3 +133,52 @@ test('CB1 bus: every new refusal has a table entry with a next step', () => {
assert.ok(!/(^|[\s("'`])(\/|~\/)/.test(REFUSALS[code].nextStep), code);
}
});
// #1873 CB5 (security review P2-1, P3-1): the binding record the raise holds (copy id, incarnation,
// definition pin, services, grant ids) leaves the bus only through the owner's preview verb. The
// raise answer, the inbox preview and the trail give every other session the copy and the change.
test('CB5 bus: the raise answer, the inbox preview and the trail carry a binding as its copy and change only', async t => {
const f = await fixture(t);
const d = await f.raise();
const raised = f.store.all('SELECT body FROM events').map((e) => JSON.parse(e.body)).find((b) => b.operation === 'decision.raise' && b.decision === d.id);
const held = raised.hire.binding;
assert.equal(typeof held.copyId, 'string');
const secrets = [held.copyId, held.orgIncarnation, held.definition?.sha256].filter((v) => typeof v === 'string' && v.length >= 8);
assert.equal(secrets.length, 3);
const summary = { instance: 'rev', change: 'bind' };
assert.deepEqual(d.hire.binding, summary);
const sessions = { owner: f.b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true }), tester: f.b.bindTester({ business: 'demo' }), reader: f.b.bindReader({ business: 'demo' }), chief: f.caps.chief, coder: f.caps.coder };
const seen = [JSON.stringify(d)];
for (const name of ['owner', 'tester', 'reader']) {
const rows = await f.call(sessions[name], 'inbox');
const row = rows.find((r) => r.id === d.id);
assert.deepEqual(row?.console?.binding, summary, `${name} inbox`);
seen.push(JSON.stringify(rows));
}
for (const name of Object.keys(sessions)) {
const trail = await f.call(sessions[name], 'trail', { subject: d.id });
const ev = trail.find((r) => r.table === 'events' && r.body?.operation === 'decision.raise');
assert.deepEqual(ev?.body?.hire?.binding, summary, `${name} trail`);
seen.push(JSON.stringify(trail));
}
for (const text of seen) for (const v of secrets) assert.equal(text.includes(v), false);
// The owner's preview still has the record, and the resolve (which reads the raise) still writes it.
const preview = await f.call(f.owner, 'hire.preview.console', { id: d.id });
assert.equal(preview.binding.copyId, held.copyId);
await f.call(f.owner, 'hire.resolve.console', { id: d.id, choice: 'yes', digest: d.hire.digest });
assert.equal(f.bindings().rev.state, 'pending');
const after = JSON.stringify(await f.call(sessions.tester, 'trail', { subject: d.id }));
for (const v of secrets) assert.equal(after.includes(v), false);
// An unbind of that binding reads as an unbind in the raise answer, the inbox and the trail.
const u = await f.raise({ binding: { services: null } });
const unbind = { instance: 'rev', change: 'unbind' };
assert.deepEqual(u.hire.binding, unbind);
for (const name of ['owner', 'tester', 'reader']) {
const row = (await f.call(sessions[name], 'inbox')).find((r) => r.id === u.id);
assert.deepEqual(row?.console?.binding, unbind, `${name} inbox unbind`);
}
for (const name of Object.keys(sessions)) {
const ev = (await f.call(sessions[name], 'trail', { subject: u.id })).find((r) => r.table === 'events' && r.body?.operation === 'decision.raise');
assert.deepEqual(ev?.body?.hire?.binding, unbind, `${name} trail unbind`);
}
});
+95 -1
View File
@@ -26,6 +26,7 @@ import { loadWithCopies, materializeCopies, promoteCopies } from '../../organiza
import { activateOrg, listOrgs, readOrgEntrySync, reserveOrg } from '../../organization/src/org-registry.mjs';
import * as orgService from '../../organization/src/org-service.mjs';
import { copyBotLogin } from '../../organization/src/copy-bindings.mjs';
import { COPY_BINDING_STATUS } from '../../organization/src/copy-binding-status.mjs';
import { businessDoc, rolesCopy, writeJson } from '../../organization/tests/helpers.mjs';
const SUBJECT = { provider: 'test', iss: 'https://issuer.test', sub: 'owner' };
@@ -62,7 +63,7 @@ async function world(t) {
// they stand, as a restart of the bus process would; boot() runs the gate's boot proof and promotes.
// stopRuns acknowledges each run the broker told the launcher to kill, as the launcher's exit does,
// unless `ack` is false. The revoke's remote step goes to the organization's admin slot.
async function org(t, w, { id = 'demo', name = 'rev', services = READ, perm = 'read', definition = 'research', admin = true, adminRotateBy = ahead(60), stopMs = 2000, project = null, gitOnly = false } = {}) {
async function org(t, w, { id = 'demo', name = 'rev', services = READ, perm = 'read', definition = 'research', admin = true, adminRotateBy = ahead(60), stopMs = 2000, project = null, gitOnly = false, definitions = null } = {}) {
const { root, rolesDir, api } = w;
const owner = id === 'demo' ? 'owner' : id;
const doc = { ...businessDoc(root, id), plugins: { harness: ['pi'] }, vars: { 'bus.tester': true } };
@@ -72,6 +73,7 @@ async function org(t, w, { id = 'demo', name = 'rev', services = READ, perm = 'r
// The project root the organization service derives (archive and delete check it).
doc.projects.stack.root = join(w.data, 'org-runtime', id, 'projects', 'stack');
if (project) doc.projects.stack.agents = project;
if (definitions) doc.definitions = definitions;
const file = writeJson(join(root, `businesses/${id}.json`), doc);
chmodSync(join(root, 'businesses'), 0o700);
const ctx = { configDir: root, dataRoot: w.data, instance: 'test', principal: LOCAL };
@@ -746,3 +748,95 @@ test('CB4 a second organization launches and calls throughout the first one\'s r
assert.ok(existsSync(acme.slot));
assert.deepEqual(demo.bot.tokens, []);
});
// #1873 CB5 (CB4 review P3-1): the gate finds a shadowing name only in the organization it serves,
// and the runtime gives a gate only to a boot that made a copy (cli config.mjs bootConfig passes no
// snapshot otherwise). A named instance the owner wrote over the only copy while the bus was down
// leaves the next start with no copy and no gate; only the revoker's own sweep then finds the former
// copy's record and ends its binding.
test('CB5 shadowing in an organization no gate serves: only the revoker\'s own sweep finds the former copy, and it revokes the binding', async (t) => {
const w = await world(t);
const f = await org(t, w);
await f.bind();
assert.equal(f.rec().state, 'bound');
assert.ok(existsSync(f.slot));
f.edit((d) => { d.roles[f.name] = { definition: 'research', credentials: { gitea: d.roles.coder.credentials.gitea } }; });
f.start();
// The start makes no copy of the name, so the runtime would build no gate for this organization.
assert.deepEqual(Object.keys(f.snapshot.copies ?? {}), []);
const revoker = copyRevoker({ writer: f.writer, gate: () => null, credentials: f.credentials, hooks: {}, stopRuns: (...a) => f.stopRuns(...a),
remote: giteaCopyRemote({ grant: f.grant, read: (x) => f.writer.read(x).document, incarnation: (x) => readOrgEntrySync(f.root, x)?.incarnation ?? null }),
definitions: () => ({}) });
assert.equal(revoker.pending(f.id, f.name), true);
revoker.sweep(f.id);
await until(() => tombstoned(f), 'shadowed tombstone');
assert.equal(f.rec().revoke.cause, 'shadowed');
assert.deepEqual(f.rec().revoke.remote, { gitea: 'revoked' });
assert.ok(!existsSync(f.slot));
assert.deepEqual(f.bot.tokens, []);
assert.equal(revoker.pending(f.id, f.name), false);
// A second pass finds a finished tombstone and starts nothing.
const deletes = w.api.calls().filter((c) => c.startsWith('DELETE')).length;
revoker.sweep(f.id);
await sleep(50);
assert.equal(revoker.has(f.id, f.name), false);
assert.equal(w.api.calls().filter((c) => c.startsWith('DELETE')).length, deletes);
});
// #1873 CB5 (CB4 review P3-3): an organization's own definition that needs gitea alone is not a
// thread definition, so its copy runs on the ordinary path: a role claim, no thread. A revoke ends
// that claim, kills the run, drains its call in flight and leaves its session dead.
const GITTER = { rev: 1, role: { tools: ['read'], network: 'api-only', authority: { withinRole: ['message.send'], crossRole: [] },
credentials: [{ service: 'gitea', scopes: ['read:repository', 'read:user'] }] }, instructions: 'Read the stack repository.' };
test('CB5 a revoke of a copy on the ordinary run path ends its role claim, kills the run, drains its call and kills its session', async (t) => {
const w = await world(t);
const f = await org(t, w, { name: 'rev.stack', project: { rev: { definition: 'org:gitter' } }, definitions: { gitter: GITTER } });
f.rootAgents = {};
f.start();
await f.bind({ level: 'project', project: 'stack', name: 'rev' });
assert.equal(f.rec().state, 'bound');
f.start();
await f.boot();
assert.deepEqual(Object.keys(f.gate.bound()), ['rev.stack']);
// An ordinary run: a thread is refused, none is required.
const revision = f.writer.read(f.id).revision;
const { authority } = await f.gate.prepare(f.id, f.name, revision);
assert.throws(() => f.b.bindLaunch({ business: f.id, role: f.name, run: 'run-t', harness: 'pi', thread: 'x', copyAuthority: authority, revision }), refuses('thread-not-allowed'));
const cap = f.b.bindLaunch({ business: f.id, role: f.name, run: 'run-1', harness: 'pi', copyAuthority: authority, revision });
await f.call(cap, 'role.claim');
const claim = () => f.store.get('SELECT op, holder_run, reason, by FROM role_claims WHERE business=? AND role=? ORDER BY seq DESC LIMIT 1', f.id, f.name);
assert.deepEqual({ ...claim() }, { op: 'claim', holder_run: 'run-1', reason: null, by: 'run-1' });
assert.equal((await f.use(cap)).status, 200);
const held = f.hold((e) => e.path === f.GET.path);
const inflight = f.use(cap).then((x) => ({ ok: x }), (e) => ({ err: e }));
await held.arrived;
assert.equal(f.gate.inflight(f.id, f.name), 1);
const out = await f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' });
assert.equal(out.state, 'unbound');
assert.equal(f.gate.inflight(f.id, f.name), 0, 'drained before the revoke answered');
held.release();
const settled = await inflight;
assert.ok(settled.err || settled.ok?.status === 0, 'the in-flight call was aborted');
// The claim ended for the revoke, by the bus, and the run got its kill end.
assert.deepEqual({ ...claim() }, { op: 'revoke', holder_run: 'run-1', reason: 'copy-revoked', by: 'bus' });
assert.deepEqual(f.killed(), ['run-1']);
assert.throws(() => f.b.copySession(cap), (e) => e instanceof BusError);
await assert.rejects(f.call(cap, 'role.claim'), (e) => e instanceof BusError);
await assertRevoked(f, null, { cause: 'revoke', code: 'credential-revoked' });
});
// #1873 CB5 re-check: a copy whose role needs a service a copy cannot bind reads
// copy-service-unbindable, but a revoke of a binding it was issued anyway outranks that.
test('CB5 status: copy-service-unbindable never hides a revoke that is running or failed', async (t) => {
const w = await world(t);
const f = await org(t, w);
const unbindable = { state: 'unbound', missing: ['gitea', 'vikunja'] };
assert.equal(f.gate.status('demo', f.name, unbindable).status, 'copy-service-unbindable');
let release;
const held = f.gate.fence('demo', f.name, () => new Promise((r) => { release = r; }));
assert.deepEqual(f.gate.status('demo', f.name, unbindable), { status: 'credential-revoking', next: COPY_BINDING_STATUS['credential-revoking'] });
release();
await held;
f.gate.close('demo', f.name, 'credential-revoke-failed');
assert.deepEqual(f.gate.status('demo', f.name, unbindable), { status: 'credential-revoke-failed', next: COPY_BINDING_STATUS['credential-revoke-failed'] });
});
+7 -6
View File
@@ -36,11 +36,11 @@ function source3j(t) {
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
// Row 206 (#1704): and the 3l→3m step's thread_shares. thread-shares.test.mjs tests it.
// Row 271 (#1776): and the 3m→3n step's session_ops and message_cancels. session-clear.test.mjs tests them.
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'project_system_types', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
function rows(path) {
const db = new DatabaseSync(path, { readOnly: true });
try {
return Object.fromEntries(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('gitea_users','gitea_grants','sessions','session_defaults','thread_shares','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_archives','project_system_types','store_tasks')").all()
return Object.fromEntries(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('gitea_users','gitea_grants','sessions','session_defaults','thread_shares','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_archives','store_tasks')").all()
.map(({ name }) => [name, db.prepare(`SELECT * FROM ${name} ORDER BY rowid`).all()
.filter(r => name !== 'meta' || !['schema_version', 'schema_digest'].includes(r.key)).map(r => ({ ...r }))]));
} finally { db.close(); }
@@ -86,6 +86,7 @@ async function engine(root, name, { store, schema, step = (s) => s }) {
copyFileSync(here('../src/migrations/3s-3t.sql'), join(moduleRoot, 'src/migrations/3s-3t.sql'));
copyFileSync(here('../src/migrations/3t-3u.sql'), join(moduleRoot, 'src/migrations/3t-3u.sql'));
copyFileSync(here('../src/migrations/3u-3v.sql'), join(moduleRoot, 'src/migrations/3u-3v.sql'));
copyFileSync(here('../src/migrations/3v-3w.sql'), join(moduleRoot, 'src/migrations/3v-3w.sql'));
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
}
function fresh(t) {
@@ -107,7 +108,7 @@ test('3j copy migrates to 3k: every row kept, and sqlite_master gains only the t
const { root, path } = source3j(t), before = rows(path), was = master(path);
assert.throws(() => new Store(root), /schema-mismatch/);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(migrated3l(rows(path)), before);
const now = master(path);
assert.deepEqual(now.filter(r => !NEW.includes(r.name) && !later3l(r)), was.filter(r => !later3l(r)));
@@ -133,7 +134,7 @@ test('a 3j→3k step that fails partway leaves the store at 3j with every row an
assert.deepEqual(master(path), was);
assert.deepEqual(rows(path), before);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
});
test('the 3j engine refuses a migrated store, migrating or not, and leaves it as it was', async t => {
@@ -144,14 +145,14 @@ test('the 3j engine refuses a migrated store, migrating or not, and leaves it as
schema: ddl3j,
store: (source) => source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3j).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3j';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3j';")
.replace(/\}, \{\n {2}\/\/ Row 236 \(#1742[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables'),
});
const pinned = readFileSync(join(root, 'engine-3j/src/store.mjs'), 'utf8');
assert.ok(pinned.includes("export const SCHEMA_VERSION = '3j';") && !pinned.includes('3j-3k'), 'the 3j engine has no 3j→3k step');
assert.throws(() => new Store3j(root), /schema-mismatch/);
assert.throws(() => new Store3j(root, { migrateSchema: true }), /schema-mismatch/);
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(master(path), was);
assert.deepEqual(rows(path), after);
});
+7 -6
View File
@@ -22,7 +22,7 @@ const LATER = ['gitea_users', 'gitea_grants', 'sqlite_autoindex_gitea_users_1',
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
// Row 206 (#1704): and the 3l→3m step's thread_shares. thread-shares.test.mjs tests it.
// Row 271 (#1776): and the 3m→3n step's session_ops and message_cancels. session-clear.test.mjs tests them.
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'project_system_types', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
function source3i(t) {
const root = mkdtempSync(join(tmpdir(), 'grant-migrate-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
@@ -42,7 +42,7 @@ function source3i(t) {
function rows(path) {
const db = new DatabaseSync(path, { readOnly: true });
try {
return Object.fromEntries(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('hosts','host_grants','gitea_users','gitea_grants','sessions','session_defaults','thread_shares','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_archives','project_system_types','store_tasks')").all()
return Object.fromEntries(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('hosts','host_grants','gitea_users','gitea_grants','sessions','session_defaults','thread_shares','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_archives','store_tasks')").all()
.map(({ name }) => [name, db.prepare(`SELECT * FROM ${name} ORDER BY rowid`).all()
.filter(r => name !== 'meta' || !['schema_version', 'schema_digest'].includes(r.key)).map(r => ({ ...r }))]));
} finally { db.close(); }
@@ -89,6 +89,7 @@ async function engine(root, name, { store, schema, step = (s) => s }) {
copyFileSync(here('../src/migrations/3s-3t.sql'), join(moduleRoot, 'src/migrations/3s-3t.sql'));
copyFileSync(here('../src/migrations/3t-3u.sql'), join(moduleRoot, 'src/migrations/3t-3u.sql'));
copyFileSync(here('../src/migrations/3u-3v.sql'), join(moduleRoot, 'src/migrations/3u-3v.sql'));
copyFileSync(here('../src/migrations/3v-3w.sql'), join(moduleRoot, 'src/migrations/3v-3w.sql'));
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
}
function fresh(t) {
@@ -112,7 +113,7 @@ test('3i copy migrates to 3j: every row kept, and sqlite_master gains only the t
const { root, path } = source3i(t), before = rows(path), was = master(path);
assert.throws(() => new Store(root), /schema-mismatch/);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(migrated3l(rows(path)), before);
const now = master(path).filter(r => !LATER.includes(r.name));
assert.deepEqual(now.filter(r => !NEW.includes(r.name) && !later3l(r)), was.filter(r => !later3l(r)));
@@ -137,7 +138,7 @@ test('a 3i→3j step that fails partway leaves the store at 3i with every row an
assert.deepEqual(master(path), was);
assert.deepEqual(rows(path), before);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
});
test('the 3i engine refuses a migrated store, migrating or not, and leaves it as it was', async t => {
@@ -148,14 +149,14 @@ test('the 3i engine refuses a migrated store, migrating or not, and leaves it as
schema: ddl3i,
store: (source) => source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3i).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3i';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3i';")
.replace(/\}, \{\n {2}\/\/ Row 234 \(#1740\)[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables'),
});
const pinned = readFileSync(join(root, 'engine-3i/src/store.mjs'), 'utf8');
assert.ok(pinned.includes("export const SCHEMA_VERSION = '3i';") && !pinned.includes('3i-3j'), 'the 3i engine has no 3i→3j step');
assert.throws(() => new Store3i(root), /schema-mismatch/);
assert.throws(() => new Store3i(root, { migrateSchema: true }), /schema-mismatch/);
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(master(path), was);
assert.deepEqual(rows(path), after);
});
+1 -1
View File
@@ -65,7 +65,7 @@ test('3f copy migrates to 3g preserving every old column/row, with an empty inst
assert.throws(() => new Store(root), /schema-mismatch/);
const store = new Store(root, { migrateSchema: true });
try {
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
assert.deepEqual({ ...store.get('SELECT to_name FROM messages') }, { to_name: null });
assert.equal(store.get('SELECT count(*) AS n FROM instance_names').n, 0);
assert.deepEqual({ ...store.get('SELECT spec_path,spec_commit FROM decisions') }, { spec_path: 'docs/spec.md', spec_commit: 'c'.repeat(40) });
+384
View File
@@ -0,0 +1,384 @@
// #1873 SP5: the pack, board and workflow verbs for an agent whose role file lists
// organization.change, and hire.raise from a pack's recorded proposal (design section 3, "The
// chief applying a pack", and section 8, SP5). The chief's authority is read from the shipped
// roles/chief-of-staff.json, so the test pins the grant the stack runs.
import test from 'node:test';
import assert from 'node:assert/strict';
import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createHash } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import { DatabaseSync } from 'node:sqlite';
import { Broker } from '../src/broker.mjs';
import { Store } from '../src/store.mjs';
import { REFUSALS } from '../src/refusals.mjs';
import { pluginHash } from '../../plugins/src/pinned.mjs';
import { installPlugin } from '../../plugins/src/install.mjs';
import { starterPack as codingPack } from '../../plugins/starter-pack/coding/index.mjs';
import { hireDecisions } from '../../organization/src/hire-decisions.mjs';
import { organizationWriter } from '../../organization/src/organization.mjs';
import { loadBusiness } from '../../organization/src/organization-config.mjs';
import { compositionOf, entryFromTemplate, readTemplate, templateHash } from '../../organization/src/definitions.mjs';
import { canonicalJson } from '../../organization/src/util.mjs';
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
const shipped = (name) => JSON.parse(readFileSync(fileURLToPath(new URL(`../../../roles/${name}.json`, import.meta.url)), 'utf8'));
const chiefAuthority = shipped('chief-of-staff').authority;
const sha = (text) => createHash('sha256').update(text).digest('hex');
const ROUTE = { harness: 'pi', model: 'claude-opus-5-5' };
// The SP4 harness (packs.test.mjs) with the shipped chief grant and a coder without it. With
// fromFile, the broker boots from the loaded organization file, as autonomy-authority.test.mjs does,
// so the reload after a write reaches the broker and a later verb reads what was written.
async function harness(t, { mutate = () => {}, fromFile = false } = {}) {
const { startBroker } = await import('../src/runtime.mjs');
const root = mkdtempSync(join(tmpdir(), 'packs5-'));
const rolesDir = rolesCopy(root);
const doc = businessDoc(root, 'demo');
doc.plugins = { harness: ['pi'] };
mutate(doc);
writeJson(join(root, 'businesses/demo.json'), doc);
const data = join(root, 'data');
mkdirSync(data, { mode: 0o700 });
const roles = {
chief: { definition: 'chief-of-staff', authority: chiefAuthority },
coder: { definition: 'coder', authority: { withinRole: ['message.send'], crossRole: [] } },
};
const loaded = () => { const l = loadBusiness('demo', { dir: root, rolesDir }); return { ...l, roles: Object.fromEntries(Object.entries(l.roles).map(([n, e]) => [n, { ...l.definitions[e.definition], ...e }])) }; };
const businesses = { demo: fromFile ? loaded() : { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles } };
const bus = await startBroker({ dataRoot: data, businesses, organization: { dir: root, rolesDir, system: systemFor(root) } });
const thaw = (p) => { const st = lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { chmodSync(p, 0o700); for (const e of readdirSync(p)) thaw(join(p, e)); } };
t.after(async () => { await bus.close(); thaw(root); rmSync(root, { recursive: true, force: true }); });
const b = bus.broker;
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
const chief = b.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi', address: 'chief-run' });
const coder = b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi', address: 'coder-run' });
b.request(chief, { verb: 'role.claim' });
b.request(coder, { verb: 'role.claim' });
const call = async (cap, verb, args = {}) => b.request(cap, { verb, args });
const path = join(root, 'businesses/demo.json');
const file = () => JSON.parse(readFileSync(path, 'utf8'));
const revision = () => b.request(human, { verb: 'limits' }).revision;
let n = 0;
const install = (pack) => {
const src = join(root, `src-${n++}`);
mkdirSync(join(src, 'starter-pack'), { recursive: true });
writeFileSync(join(src, 'starter-pack', 'index.mjs'), `export const interfaceVersion = 1;\nexport function starterPack() { return ${JSON.stringify(pack)}; }\n`);
const manifest = { name: pack.id, version: '1.0.0', kinds: ['starter-pack'], interfaceVersions: { 'starter-pack': 1 }, sha256: '0'.repeat(64),
source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
manifest.sha256 = pluginHash(src);
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
installPlugin({ dataRoot: data, source: src, expectedHash: manifest.sha256 });
};
// The broker's own database, read-only: grants spent and decisions raised.
const query = (sql) => { const db = new DatabaseSync(join(data, 'bus/bus.sqlite'), { readOnly: true }); try { return db.prepare(sql).all(); } finally { db.close(); } };
const spent = () => query("SELECT body FROM events WHERE kind='action.allowed' ORDER BY seq").map(e => JSON.parse(e.body)).filter(e => e.action === 'organization.change').length;
const decisions = () => query('SELECT count(*) n FROM decisions')[0].n;
return { b, root, rolesDir, path, human, chief, coder, call, file, revision, install, spent, decisions };
}
const variant = (id, patch = () => {}) => { const p = structuredClone(codingPack()); p.id = id; patch(p); return p; };
const marks = (preview) => Object.fromEntries(preview.items.map(i => [i.path, i.mark]));
test('SP5 the chief applies the coding pack: board and workflows written, no definitions, template pins, one grant spent', async t => {
const { call, chief, file, path, rolesDir, spent, decisions } = await harness(t);
const before = file();
const preview = await call(chief, 'pack.preview', { pack: 'coding' });
assert.equal(spent(), 0);
assert.deepEqual(readFileSync(path, 'utf8'), JSON.stringify(before, null, 2) + '\n');
// A definition a human apply would add is not written for an agent; its hire pins the template.
for (const name of ['coder', 'cto', 'merge-gate', 'pm', 'reviewer', 'security-reviewer']) assert.equal(marks(preview)[`definitions.${name}`], 'not-written');
assert.equal(marks(preview).board, 'add');
assert.equal(marks(preview)['workflows.change'], 'add');
assert.equal(preview.plan.definitions, undefined);
for (const h of preview.hires) {
assert.equal(h.mark, 'propose');
assert.deepEqual(h.pin, { template: h.definition, sha256: templateHash(rolesDir, h.definition) });
}
await call(chief, 'pack.apply', { pack: 'coding', digest: preview.digest });
const after = file();
assert.equal(after.board.layout, 'kanban');
assert.deepEqual(after.board, codingPack().board);
assert.deepEqual(after.workflows.change.steps.map(s => s.roles), [['pm'], ['coder'], ['reviewer'], ['human', 'merge-gate']]);
assert.deepEqual(after.definitions ?? null, before.definitions ?? null);
assert.equal(after.packs.length, 1);
assert.equal(after.packs[0].by, 'chief-of-staff.chief');
assert.deepEqual(after.packs[0].added.sort(), ['board', 'workflows.change']);
assert.deepEqual(after.packs[0].proposedHires.map(h => [h.name, h.pin.template]), [['pm-1', 'pm'], ['coder-1', 'coder'], ['reviewer-1', 'reviewer']]);
assert.equal(spent(), 1);
assert.equal(decisions(), 0);
});
test('SP5 a repeat apply that writes nothing spends no grant', async t => {
const { call, chief, file, spent } = await harness(t);
const first = await call(chief, 'pack.preview', { pack: 'coding' });
await call(chief, 'pack.apply', { pack: 'coding', digest: first.digest });
assert.equal(spent(), 1);
const before = file();
// The same pack again: the board and workflow are the same and the proposals unchanged, so the
// runtime skips the write and the broker spends nothing.
const again = await call(chief, 'pack.preview', { pack: 'coding' });
const out = await call(chief, 'pack.apply', { pack: 'coding', digest: again.digest });
assert.deepEqual(out.added, []);
assert.deepEqual(file(), before);
assert.equal(spent(), 1);
});
test('SP5 end to end: the chief applies a pack, the organization reloads, and the chief raises a hire from the recorded proposal', async t => {
const { call, chief, file, rolesDir, decisions } = await harness(t, { fromFile: true });
const preview = await call(chief, 'pack.preview', { pack: 'coding' });
await call(chief, 'pack.apply', { pack: 'coding', digest: preview.digest });
assert.equal(decisions(), 0);
// The raise reads the packs list the broker reloaded after the write, not a fixture's.
const d = await call(chief, 'hire.raise', { proposal: { pack: 'coding', hire: 'coder-1' }, route: ROUTE });
assert.equal(d.hire.name, 'coder-1');
assert.equal(d.hire.level, 'org');
assert.equal(decisions(), 1);
const direct = await call(chief, 'hire.raise', { op: 'hire', level: 'org', name: 'coder-b', role: { template: 'coder', sha256: templateHash(rolesDir, 'coder') }, route: ROUTE, reason: 'Code.' });
assert.deepEqual(d.hire.role, direct.hire.role);
assert.deepEqual(d.hire.flagged, direct.hire.flagged);
// A second preview leaves out the hire the open decision names; the file is unchanged by the raise.
assert.equal((await call(chief, 'pack.preview', { pack: 'coding' })).hires.find(h => h.name === 'coder-1').mark, 'dropped');
assert.equal(file().packs.length, 1);
});
test('SP5 a project pack: the chief applies it to a code project and raises the project hire from it', async t => {
const { call, chief, file, install, decisions } = await harness(t, { fromFile: true });
install(variant('projkit', p => { p.scope = 'project'; }));
const preview = await call(chief, 'pack.preview', { pack: 'projkit', project: 'stack' });
assert.equal(marks(preview)['projects.stack.board'], 'add');
assert.equal(marks(preview)['definitions.coder'], 'not-written');
await call(chief, 'pack.apply', { pack: 'projkit', project: 'stack', digest: preview.digest });
assert.deepEqual(file().projects.stack.board, codingPack().board);
assert.equal(file().packs.at(-1).project, 'stack');
// The proposal names its project; without it, no org-level entry matches.
await assert.rejects(call(chief, 'hire.raise', { proposal: { pack: 'projkit', hire: 'coder-1' }, route: ROUTE }), { code: 'invalid-request' });
const d = await call(chief, 'hire.raise', { proposal: { pack: 'projkit', project: 'stack', hire: 'coder-1' }, route: ROUTE });
assert.equal(d.hire.level, 'project');
assert.equal(d.hire.project, 'stack');
assert.equal(decisions(), 1);
});
test('SP5 an inline pack definition that shares a template name does not keep its workflow step', async t => {
const { call, chief, install, rolesDir } = await harness(t);
const { rev: _rev, ...entry } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
install(variant('shadowkit', p => {
p.definitions = { reviewer: { ...entry, instructions: entry.instructions + '\nShadow.' } };
p.hires = [];
p.workflows = { check: { description: 'Check.', steps: [{ id: 'look', title: 'Look', state: 'todo', roles: ['human'] }, { id: 'review', title: 'Review it', state: 'in-review', roles: ['reviewer'] }] } };
}));
const preview = await call(chief, 'pack.preview', { pack: 'shadowkit' });
const item = preview.items.find(i => i.path === 'workflows.check');
assert.equal(item.mark, 'add');
assert.deepEqual(item.leftOut, [{ step: 2, title: 'Review it', roles: ['reviewer'] }]);
assert.deepEqual(preview.plan.workflows.check.steps.map(s => s.roles), [['human']]);
});
test('SP5 a role without organization.change gets decision-required for the writes and human-required for the packs read', async t => {
const { call, coder, chief, path, revision, install, spent } = await harness(t);
const before = readFileSync(path);
for (const [verb, args] of [['pack.apply', { pack: 'coding', digest: 'a'.repeat(64) }], ['board.set', { revision: revision(), board: null }],
['workflow.set', { revision: revision(), name: 'x', workflow: null }], ['pack.preview', { pack: 'coding' }]])
await assert.rejects(call(coder, verb, args), /decision-required/, verb);
await assert.rejects(call(coder, 'packs'), /human-required/);
// The console names stay the owner's.
await assert.rejects(call(chief, 'pack.preview.console', { pack: 'coding' }), /human-required/);
await assert.rejects(call(chief, 'packs.console'), /human-required/);
assert.deepEqual(readFileSync(path), before);
// The chief's packs read lists shipped and installed packs and spends nothing.
install(variant('teamkit'));
const listed = (await call(chief, 'packs')).packs;
assert.ok(listed.some(p => p.id === 'coding' && p.origin === 'shipped'));
assert.ok(listed.some(p => p.id === 'teamkit' && p.origin === 'installed'));
assert.equal(spent(), 0);
// The agent request carries no origin, actor or unknown key.
await assert.rejects(call(chief, 'pack.preview', { pack: 'coding', actor: 'jason' }), /invalid-request/);
await assert.rejects(call(chief, 'packs', { all: true }), /invalid-request/);
});
test('SP5 the chief writes a board and a workflow, each spending the grant once, with its own actor', async t => {
const { call, chief, file, revision, spent } = await harness(t);
await call(chief, 'board.set', { revision: revision(), board: codingPack().board });
assert.deepEqual(file().board, codingPack().board);
assert.equal(spent(), 1);
const workflow = { description: 'Intake.', steps: [{ title: 'Take it', state: 'todo', roles: ['human'] }, { title: 'Build it', state: 'in-progress', roles: ['coder'] }] };
await call(chief, 'workflow.set', { revision: revision(), name: 'intake', workflow });
assert.deepEqual(file().workflows.intake, workflow);
assert.equal(spent(), 2);
await call(chief, 'workflow.set', { revision: revision(), name: 'intake', workflow: null });
assert.equal(file().workflows, undefined);
assert.equal(spent(), 3);
// A refused write spends nothing.
await assert.rejects(call(chief, 'board.set', { revision: 'b'.repeat(64), board: null }), { code: 'revision-conflict' });
await assert.rejects(call(chief, 'workflow.set', { revision: revision(), name: 'x', workflow: { description: 'x', steps: [{ title: 'Do', state: 'todo', roles: ['nobody-at-all'] }] } }), /invalid-request/);
assert.equal(spent(), 3);
});
test('SP5 an agent apply never writes a definition or a saved template: human-required', async t => {
const { call, chief, path, root, rolesDir } = await harness(t);
const before = readFileSync(path);
// A definitions rename names a definition to write.
await assert.rejects(call(chief, 'pack.preview', { pack: 'coding', as: ['definitions.coder=dev'] }), /human-required/);
await assert.rejects(call(chief, 'pack.apply', { pack: 'coding', as: ['definitions.coder=dev'], digest: 'a'.repeat(64) }), /human-required/);
// A saved template refuses before anything is read; the name need not exist.
await assert.rejects(call(chief, 'pack.preview', { pack: 'saved:x' }), /human-required/);
await assert.rejects(call(chief, 'pack.apply', { pack: 'saved:x', digest: 'a'.repeat(64) }), /human-required/);
// A workflows rename is allowed.
const renamed = await call(chief, 'pack.preview', { pack: 'coding', as: ['workflows.change=ship'] });
assert.equal(marks(renamed)['workflows.ship'], 'add');
assert.deepEqual(readFileSync(path), before);
// The writer's own fence: the chief's actor with a definition in the plan, or a saved origin.
const writer = organizationWriter({ dir: root, rolesDir });
const { rev: _rev, ...entry } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
const pack = { id: 'kit', version: '1.0.0', sha256: 'a'.repeat(64), origin: 'installed', scope: 'org', project: null, proposedHires: [] };
const opts = () => ({ revision: writer.read('demo').revision, actor: 'chief-of-staff.chief', assertClean: () => {}, detail: {} });
assert.throws(() => writer.applyPack('demo', { definitions: { dev: entry }, packs: pack }, opts()), /human-required/);
assert.throws(() => writer.applyPack('demo', { board: codingPack().board, packs: { ...pack, origin: 'saved' } }, opts()), /human-required/);
assert.deepEqual(readFileSync(path), before);
});
test('SP5 a workflow step naming a definition the agent apply cannot write is left out and marked', async t => {
const { call, chief, file, install, rolesDir } = await harness(t);
const { rev: _rev, ...scribe } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
install(variant('scribekit', p => {
p.definitions = { scribe, pm: p.definitions.pm };
p.hires = [{ name: 'scribe-1', definition: 'scribe', reason: 'Take notes.' }];
p.workflows = {
notes: { description: 'Notes.', steps: [{ id: 'plan', title: 'Plan it', state: 'todo', roles: ['pm'] }, { id: 'write', title: 'Write it', state: 'in-progress', roles: ['scribe', 'human'] }] },
solo: { description: 'Solo.', steps: [{ id: 'write', title: 'Write it', state: 'todo', roles: ['scribe'] }] },
};
}));
const preview = await call(chief, 'pack.preview', { pack: 'scribekit' });
assert.equal(marks(preview)['definitions.scribe'], 'not-written');
const items = Object.fromEntries(preview.items.map(i => [i.path, i]));
assert.equal(items['workflows.notes'].mark, 'add');
assert.deepEqual(items['workflows.notes'].leftOut, [{ step: 2, title: 'Write it', roles: ['scribe'] }]);
assert.equal(items['workflows.solo'].mark, 'left-out');
assert.deepEqual(items['workflows.solo'].leftOut, [{ step: 1, title: 'Write it', roles: ['scribe'] }]);
assert.deepEqual(Object.keys(preview.plan.workflows), ['notes']);
// The full-entry definition's hire pins the inline entry.
const hire = preview.hires.find(h => h.name === 'scribe-1');
assert.deepEqual(hire.pin, { inline: 'scribe', sha256: sha(canonicalJson(scribe)) });
await call(chief, 'pack.apply', { pack: 'scribekit', digest: preview.digest });
assert.deepEqual(file().workflows, { notes: { description: 'Notes.', steps: [{ title: 'Plan it', state: 'todo', roles: ['pm'] }] } });
assert.equal(file().definitions?.scribe, undefined);
assert.deepEqual(file().packs.at(-1).proposedHires[0].entry, scribe);
});
// The hire-decisions.test.mjs fixture: a Broker on the loaded organization and the hire service.
function fixture(t) {
const root = mkdtempSync(join(tmpdir(), 'sp5-bus-')), rolesDir = rolesCopy(root);
const file = writeJson(join(root, 'businesses/demo.json'), { ...businessDoc(root, 'demo'), plugins: { harness: ['pi'] } });
const storeRoot = join(root, 'data'); mkdirSync(storeRoot, { mode: 0o700 });
const writer = organizationWriter({ dir: root, rolesDir }), store = new Store(storeRoot);
const service = hireDecisions({ writer, rolesDir, dataRoot: storeRoot, configDir: root, assertClean: () => {} });
const loaded = () => { const doc = loadBusiness('demo', { dir: root, rolesDir }); return { ...doc, roles: Object.fromEntries(Object.entries(doc.roles).map(([n, e]) => [n, { ...doc.definitions[e.definition], ...e }])) }; };
const b = new Broker({ store, businesses: { demo: loaded() }, hires: service });
const caps = { chief: b.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi' }), coder: b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi' }) };
b.request(caps.chief, { verb: 'role.claim' }); b.request(caps.coder, { verb: 'role.claim' });
t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
const call = (cap, verb, args = {}) => b.request(cap, { verb, args });
// A packs entry as an owner's apply records it, then the broker's organization reloads.
const record = (plan) => {
writer.applyPack('demo', plan, { revision: writer.read('demo').revision, actor: 'jason', assertClean: () => {}, detail: {} });
b.reloadBusiness('demo', loaded());
};
const pack = (proposedHires, extra = {}) => ({ id: 'kit', version: '1.0.0', sha256: 'a'.repeat(64), origin: 'installed', scope: 'org', project: null, proposedHires, ...extra });
// Edit the organization file directly, as a later change would, then reload.
const edit = (change) => { const doc = JSON.parse(readFileSync(file, 'utf8')); change(doc); writeJson(file, doc); b.reloadBusiness('demo', loaded()); };
const raise = (proposal, extra = {}) => call(caps.chief, 'hire.raise', { proposal, route: ROUTE, ...extra });
const count = () => store.get('SELECT count(*) n FROM decisions').n;
return { root, rolesDir, b, store, file, writer, caps, call, record, pack, edit, raise, count, loaded };
}
test('SP5 a hire raised from a template proposal carries the broker-computed role and lines, as a direct raise does', async t => {
const { rolesDir, record, pack, raise, call, caps, count } = fixture(t);
const pin = { template: 'reviewer', sha256: templateHash(rolesDir, 'reviewer') };
record({ packs: pack([{ name: 'scout', reason: 'Review the pack work.', pin }]) });
const d = await raise({ pack: 'kit', hire: 'scout' });
assert.equal(d.hire.name, 'scout');
assert.equal(d.hire.level, 'org');
// The lines are the broker's, the same as a direct raise of the same role computes.
const direct = await call(caps.chief, 'hire.raise', { op: 'hire', level: 'org', name: 'scout-b', role: pin, route: ROUTE, reason: 'Review.' });
assert.deepEqual(d.hire.role, direct.hire.role);
assert.deepEqual(d.hire.flagged, direct.hire.flagged);
assert.deepEqual(d.hire.credentialLines, direct.hire.credentialLines);
assert.ok(Array.isArray(d.hire.credentialLines));
assert.equal(count(), 2);
// hire.raise requires a reason and stores none. The raise above sent none, so the recorded one
// filled it; a caller's own reason replaces it, and an empty one refuses as a direct raise does.
await assert.rejects(raise({ pack: 'kit', hire: 'scout' }, { reason: '' }), { code: 'invalid-request' });
assert.equal((await raise({ pack: 'kit', hire: 'scout' }, { reason: 'Mine.' })).hire.name, 'scout');
assert.equal(count(), 3);
});
test('SP5 a proposal raise: role-not-chief, decision-stale, definition-rev-conflict, starter-pack-proposal-stale, definition-not-hireable', async t => {
const { rolesDir, record, pack, raise, call, caps, edit, count } = fixture(t);
const { rev: _rev, ...scribe } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
const inline = { name: 'inl', reason: 'Inline.', pin: { inline: 'scribe', sha256: sha(canonicalJson(scribe)) }, entry: scribe };
record({
definitions: { helper: scribe },
packs: pack([
{ name: 'moved', reason: 'Moved.', pin: { template: 'reviewer', sha256: 'b'.repeat(64) } },
{ name: 'def', reason: 'Def.', pin: { definition: 'helper', rev: 1 } },
inline,
]),
});
// Only the chief raises, with or without a proposal.
await assert.rejects(call(caps.coder, 'hire.raise', { proposal: { pack: 'kit', hire: 'def' }, route: ROUTE }), { code: 'role-not-chief' });
await assert.rejects(raise({ pack: 'kit', hire: 'moved' }), { code: 'decision-stale' });
edit(doc => { doc.definitions.helper = { ...doc.definitions.helper, rev: 2, instructions: doc.definitions.helper.instructions + '\nAlso this.' }; });
await assert.rejects(raise({ pack: 'kit', hire: 'def' }), { code: 'definition-rev-conflict' });
// The inline entry stored with the proposal still hashes to its pin, so it raises.
const ok = await raise({ pack: 'kit', hire: 'inl' });
assert.equal(ok.hire.name, 'inl');
const direct = await call(caps.chief, 'hire.raise', { op: 'hire', level: 'org', name: 'inl-b', role: { inline: compositionOf(scribe) }, route: ROUTE, reason: 'Inline.' });
assert.deepEqual(ok.hire.role, direct.hire.role);
assert.equal(count(), 2);
// A later edit to the stored entry is caught at the raise.
edit(doc => { doc.packs[0].proposedHires[2].entry.instructions += '\nAnd more.'; });
await assert.rejects(raise({ pack: 'kit', hire: 'inl' }), { code: 'starter-pack-proposal-stale' });
// An inline entry from a template launch.noHire names refuses, though prepare reads no ref for it.
edit(doc => { doc.packs[0].proposedHires[2].entry = scribe; doc.launch.noHire = ['coder']; });
await assert.rejects(raise({ pack: 'kit', hire: 'inl' }), { code: 'definition-not-hireable' });
assert.equal(count(), 2);
});
test('SP5 a chief without role.hire is refused a proposal raise before the packs list is read', async t => {
const { record, pack, raise, b, loaded, rolesDir, count } = fixture(t);
record({ packs: pack([{ name: 'scout', reason: 'Review.', pin: { template: 'reviewer', sha256: templateHash(rolesDir, 'reviewer') } }]) });
const doc = loaded();
const authority = doc.roles.chief.authority;
b.reloadBusiness('demo', { ...doc, roles: { ...doc.roles, chief: { ...doc.roles.chief, authority: { ...authority, withinRole: authority.withinRole.filter(a => a !== 'role.hire') } } } });
await assert.rejects(raise({ pack: 'kit', hire: 'scout' }), { code: 'not-authorized' });
// A proposal that does not exist gets the same refusal, so the refusal leaks nothing.
await assert.rejects(raise({ pack: 'nosuch', hire: 'nobody' }), { code: 'not-authorized' });
assert.equal(count(), 0);
});
test('SP5 a proposal raise names no role, name, level or other hire key; unknown proposals refuse; a plain raise is unchanged', async t => {
const { rolesDir, record, pack, raise, call, caps, count } = fixture(t);
const pin = { template: 'reviewer', sha256: templateHash(rolesDir, 'reviewer') };
record({ packs: pack([{ name: 'scout', reason: 'Review.', pin }]) });
for (const extra of [{ role: pin }, { name: 'other' }, { level: 'org' }, { op: 'hire' }, { credentials: {} }, { personality: 'x' }, { binding: {} }])
await assert.rejects(raise({ pack: 'kit', hire: 'scout' }, extra), { code: 'invalid-request' }, JSON.stringify(extra));
for (const proposal of [{ pack: 'kit' }, { pack: 'kit', hire: 'nobody' }, { pack: 'other', hire: 'scout' }, { pack: 'kit', hire: 'scout', project: 'stack' },
{ pack: 'kit', hire: 'scout', role: 'x' }, { pack: 'kit', hire: 7 }, 'kit'])
await assert.rejects(raise(proposal), { code: 'invalid-request' }, JSON.stringify(proposal));
await assert.rejects(call(caps.chief, 'hire.raise', { proposal: { pack: 'kit', hire: 'scout' } }), { code: 'invalid-request' });
assert.equal(count(), 0);
// Without proposal, hire.raise is what it was.
const INLINE = { tools: ['read'], network: 'none', authority: { withinRole: [], crossRole: [] }, credentials: [], instructions: 'Read and report.' };
const d = await call(caps.chief, 'hire.raise', { op: 'hire', level: 'org', name: 'plain', role: { inline: INLINE }, route: ROUTE, reason: 'Need help' });
assert.equal(d.hire.name, 'plain');
assert.equal(count(), 1);
});
test('SP5 starter-pack-proposal-stale is in the refusal table with a rail and a next step that names no path', () => {
const c = 'starter-pack-proposal-stale';
assert.ok(Object.hasOwn(REFUSALS, c));
assert.ok(REFUSALS[c].rail?.length > 5);
assert.ok(REFUSALS[c].nextStep?.length > 20);
assert.ok(!/(^|[\s("'`])(\/|~\/)/.test(REFUSALS[c].nextStep));
});
+4 -1
View File
@@ -318,7 +318,10 @@ test('SP4 who may call: the owner only; the tester, a member, an agent and a con
['pack.apply', { pack: 'coding', digest: 'a'.repeat(64) }], ['board.set', { revision: revision(), board: null }]])
assert.throws(() => b.request(member, { verb, args }), /human-required/);
const agent = b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi', address: 'coder-run' });
assert.throws(() => b.request(agent, { verb: 'pack.apply', args: { pack: 'coding', digest: 'a'.repeat(64) } }), /human-required/);
// SP5: an agent reaches the unsuffixed names through its role's organization.change; the coder
// lists none, so its write needs a decision (packs-agent.test.mjs covers the chief).
b.request(agent, { verb: 'role.claim' });
assert.throws(() => b.request(agent, { verb: 'pack.apply', args: { pack: 'coding', digest: 'a'.repeat(64) } }), /decision-required/);
assert.throws(() => b.request(agent, { verb: 'pack.preview.console', args: { pack: 'coding' } }), /human-required/);
// The owner's console cap calls the .console names; the cli cap the unsuffixed ones.
await assert.rejects(call('pack.preview', { pack: 'coding' }), /human-required/);
+2 -3
View File
@@ -198,8 +198,7 @@ test('3p through 3s migration preserves existing request rows and the append-onl
const path = join(root, 'bus/bus.sqlite'), ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8')
// 3s-3t (#797) adds project_archives; the 3p fixture predates it and the replayed chain recreates it.
.replace(readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8'), '')
.replace(readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8'), '')
// 3u-3v (#1873, U4a) adds store_tasks after project_system_types; the same holds for it.
// 3u-3v (#1873, U4a) adds store_tasks; the same holds for it.
.replace(readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8'), '')
.replace(",'prd.submitted','prd.activated','prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','advanced'", '');
const db = new DatabaseSync(path); db.exec(ddl);
@@ -216,7 +215,7 @@ test('3p through 3s migration preserves existing request rows and the append-onl
assert.throws(() => new Store(root), /schema-mismatch/);
const store = new Store(root, { migrateSchema: true }); t.after(() => store.close());
assert.deepEqual(store.all('SELECT * FROM request_events'), before);
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
store.run("INSERT INTO request_events(business,request,revision,kind,at,body) VALUES('demo','r',2,'prd.submitted',?,'{}')", at);
assert.throws(() => store.run('DELETE FROM request_events'), /append-only/);
assert.throws(() => store.run("UPDATE request_events SET body='{}'"), /append-only/);
+5 -5
View File
@@ -143,8 +143,8 @@ const here = (p) => new URL(p, import.meta.url);
const migrationSQL = readFileSync(here('../src/migrations/3s-3t.sql'), 'utf8');
const currentDDL = readFileSync(here('../schema.sql'), 'utf8');
assert.equal(currentDDL.split(migrationSQL).length, 2, 'schema.sql holds the 3s→3t step exactly once');
// 3u-3v (#1873, U4a) adds store_tasks after them; a 3s store has neither.
const ddl3s = currentDDL.replace(migrationSQL, '').replace(readFileSync(here('../src/migrations/3t-3u.sql'), 'utf8'), '').replace(readFileSync(here('../src/migrations/3u-3v.sql'), 'utf8'), '');
// 3u-3v (#1873, U4a) adds store_tasks after it; a 3s store has neither table.
const ddl3s = currentDDL.replace(migrationSQL, '').replace(readFileSync(here('../src/migrations/3u-3v.sql'), 'utf8'), '');
function source3s(t) {
const root = mkdtempSync(join(tmpdir(), 'project-archive-3s-'));
@@ -179,7 +179,7 @@ test('a 3s store migrates to 3t: project_archives is new and empty, every row st
assert.deepEqual(master(path), was, 'the refused open changed nothing');
const store = new Store(root, { migrateSchema: true });
t.after(() => store.close());
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.equal(store.get("SELECT id FROM events").id, 'preserved');
assert.equal(store.get("SELECT name FROM projects").name, 'kept');
assert.deepEqual(store.all('SELECT seq,at,business,project,op,by FROM project_archives'), []);
@@ -212,7 +212,7 @@ test('the 3s engine refuses a 3t store, migrating or not, and leaves it as it wa
const source = readFileSync(here('../src/store.mjs'), 'utf8');
const edited = source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3s).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3s';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3s';")
.replace(/\}, \{\n {2}\/\/ #797 MVP3[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
assert.ok(edited.includes("export const SCHEMA_VERSION = '3s';") && edited.includes('3r-3s') && !edited.includes('3s-3t'), 'the 3s engine has no 3s→3t step');
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
@@ -222,7 +222,7 @@ test('the 3s engine refuses a 3t store, migrating or not, and leaves it as it wa
const Store3s = (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
assert.throws(() => new Store3s(root), /schema-mismatch/);
assert.throws(() => new Store3s(root, { migrateSchema: true }), /schema-mismatch/);
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(master(path), was);
// The same engine opens a fresh 3s store, so the refusal is the version's and not a broken copy.
const { root: old } = source3s(t);
+8 -7
View File
@@ -90,10 +90,12 @@ const ask = (x, state, extra = {}) => x.call('pm', 'request.question', { request
options: [{ key: 'web', text: 'Web' }, { key: 'cli', text: 'CLI' }], ...extra });
test('the console verb list includes the artifact read and existing owner controls', () => {
assert.deepEqual([...CONSOLE_VERBS], ['system-types.console', 'project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
assert.deepEqual([...CONSOLE_VERBS], ['project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
'request.get.console', 'request.projects.console', 'request.answer.console', 'request.attach.console', 'request.cancel.console',
// Starter packs SP4 (#1873): the owner's pack list and preview, and the three pack writes.
'packs.console', 'pack.preview.console', 'pack.apply.console', 'board.set.console', 'workflow.set.console']);
'packs.console', 'pack.preview.console', 'pack.apply.console', 'board.set.console', 'workflow.set.console',
// Starter packs SP8 (#1873): the owner saves and lists templates.
'template.save.console', 'template.list.console']);
});
test('only the signed-in owner capability reaches the console verbs and it reaches nothing else', async t => {
@@ -304,7 +306,7 @@ test('a cancel is refused when the owner-to-PM edge closed, with no withdrawn de
});
test('3q through 3s widens the kind list to the union, keeps every 3q kind, row, seq and guard, and the chain matches the fresh digest', t => {
assert.equal(SCHEMA_VERSION, '3v');
assert.equal(SCHEMA_VERSION, '3w');
const root = mkdtempSync(join(tmpdir(), 'request-migration-3r-'));
t.after(() => rmSync(root, { recursive: true, force: true })); mkdirSync(join(root, 'bus'), { mode: 0o700 });
const path = join(root, 'bus/bus.sqlite'), AT = '2026-11-03T00:00:00.000Z';
@@ -312,11 +314,10 @@ test('3q through 3s widens the kind list to the union, keeps every 3q kind, row,
// 3s-3t (#797) adds project_archives, outside request_events; the 3q fixture predates it and
// the replayed chain recreates it, so the step's text is removed here like the kinds are.
const step3t = readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8');
// 3t-3u (#1873, SYS1) adds project_system_types the same way, and 3u-3v (U4a) adds store_tasks.
const step3u = readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8');
// 3u-3v (#1873, U4a) adds store_tasks the same way.
const step3v = readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8');
// 3p-3q shipped the PRD kinds and mode.changed; 3q-3r adds the three Task 3 kinds after them.
const ddl3q = current.replace(step3t, '').replace(step3u, '').replace(step3v, '').replace(",'advanced'", '').replace("'prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','cancelled'", "'prd.invalidated','mode.changed','cancelled'");
const ddl3q = current.replace(step3t, '').replace(step3v, '').replace(",'advanced'", '').replace("'prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','cancelled'", "'prd.invalidated','mode.changed','cancelled'");
assert.notEqual(ddl3q, current, 'the fixture is the narrower schema');
const migration = readFileSync(new URL('../src/migrations/3q-3r.sql', import.meta.url), 'utf8');
assert.ok(migration.includes("'prd.submitted','prd.activated','prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','cancelled'"), 'the step carries every 3q kind plus its own');
@@ -333,7 +334,7 @@ test('3q through 3s widens the kind list to the union, keeps every 3q kind, row,
db.close(); chmodSync(path, 0o600);
assert.throws(() => new Store(root), /schema-mismatch/);
const cli = spawnSync(process.execPath, [new URL('../src/migrate-schema.mjs', import.meta.url).pathname, root], { encoding: 'utf8' });
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3v ready\n');
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3w ready\n');
const store = new Store(root);
assert.deepEqual(store.all('SELECT * FROM request_events ORDER BY seq'), before, 'every row keeps its seq and bytes, including the 3q kinds');
const fresh = new DatabaseSync(':memory:'); fresh.exec(current);
@@ -230,8 +230,7 @@ test('3r to 3s migration preserves journal and sequence and admits only the new
// 3s-3t (#797) adds project_archives; the 3r fixture predates it and the replayed chain recreates it.
db.exec(readFileSync(new URL('../schema.sql', import.meta.url), 'utf8')
.replace(readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8'), '')
.replace(readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8'), '')
// 3u-3v (#1873, U4a) adds store_tasks after project_system_types; the same holds for it.
// 3u-3v (#1873, U4a) adds store_tasks; the same holds for it.
.replace(readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8'), '')
.replace(",'advanced'", ''));
db.prepare('INSERT INTO meta VALUES(?,?)').run('schema_version', '3r');
@@ -253,7 +252,7 @@ test('3r to 3s migration preserves journal and sequence and admits only the new
assert.deepEqual(store.all('SELECT * FROM request_events ORDER BY seq'), before);
assert.deepEqual(store.all("SELECT name,sql FROM sqlite_master WHERE type='trigger' AND tbl_name IN ('request_events','request_effects') ORDER BY name"), guards);
assert.equal(store.get("SELECT name FROM sqlite_master WHERE type='index' AND name='request_events_request'").name, 'request_events_request');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
store.run("INSERT INTO request_events(business,request,revision,kind,at,body) VALUES('demo','r',16,'advanced',?,'{}')", at);
assert.equal(store.get("SELECT max(seq) n FROM request_events").n, 42);
assert.throws(() => store.run("UPDATE request_events SET kind='opened'"), /append-only/);
+4 -6
View File
@@ -278,8 +278,7 @@ test('3n_to_3o_is_explicit_offline_preserves_rows_and_old_engine_refuses_new_sch
// 3s-3t (#797) adds project_archives after the request tables; the pre-3t fixtures below
// predate it and the replayed chain recreates it, so the step's text comes out here too.
.replace(readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8'), '')
.replace(readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8'), '')
// 3u-3v (#1873, U4a) adds store_tasks after project_system_types; the same holds for it.
// 3u-3v (#1873, U4a) adds store_tasks; the same holds for it.
.replace(readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8'), '')
.replace("'setup.wait','bootstrap.stale','bootstrap.completed','prd.submitted','prd.activated','prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','advanced','cancelled'", "'setup.wait','cancelled'");
const newDDL = readFileSync(new URL('../src/migrations/3n-3o.sql', import.meta.url), 'utf8');
@@ -293,7 +292,7 @@ test('3n_to_3o_is_explicit_offline_preserves_rows_and_old_engine_refuses_new_sch
db.close(); chmodSync(path, 0o600); const before = readFileSync(path);
assert.throws(() => new Store(root), /schema-mismatch/); assert.deepEqual(readFileSync(path), before);
const cli = spawnSync(process.execPath, [new URL('../src/migrate-schema.mjs', import.meta.url).pathname, root], { encoding: 'utf8' });
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3v ready\n'); assert.equal(SCHEMA_VERSION, '3v');
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3w ready\n'); assert.equal(SCHEMA_VERSION, '3w');
const store = new Store(root); assert.equal(store.get("SELECT id FROM events").id, 'preserved');
for (const table of ['request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events']) assert.equal(store.get(`SELECT count(*) n FROM ${table}`).n, 0);
store.close(); assert.equal(spawnSync(process.execPath, [new URL('../src/migrate-schema.mjs', import.meta.url).pathname, root]).status, 0);
@@ -316,8 +315,7 @@ test('3o_to_3p_widens_only_the_event_kinds_and_keeps_rows_seq_and_guards', t =>
// The 3o schema is the current one with the narrower kind list.
const ddl3o = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8')
.replace(readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8'), '')
.replace(readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8'), '')
// 3u-3v (#1873, U4a) adds store_tasks after project_system_types; the same holds for it.
// 3u-3v (#1873, U4a) adds store_tasks; the same holds for it.
.replace(readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8'), '')
.replace("'setup.wait','bootstrap.stale','bootstrap.completed','prd.submitted','prd.activated','prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','advanced','cancelled'", "'setup.wait','cancelled'");
const { schemaDigest } = awaitImportStore, AT = '2026-11-03T00:00:00.000Z';
@@ -335,7 +333,7 @@ test('3o_to_3p_widens_only_the_event_kinds_and_keeps_rows_seq_and_guards', t =>
db.close(); chmodSync(path, 0o600);
assert.throws(() => new Store(root), /schema-mismatch/);
const cli = spawnSync(process.execPath, [new URL('../src/migrate-schema.mjs', import.meta.url).pathname, root], { encoding: 'utf8' });
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3v ready\n');
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3w ready\n');
const store = new Store(root);
assert.deepEqual(store.all('SELECT * FROM request_events ORDER BY seq'), before, 'every row keeps its seq and bytes');
assert.deepEqual(store.all('SELECT * FROM request_effects ORDER BY seq'), effects);
+73
View File
@@ -0,0 +1,73 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { DatabaseSync } from 'node:sqlite';
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { SCHEMA_VERSION, Store, schemaDigest } from '../src/store.mjs';
// #1873 SP9: bus schema step 3v-3w drops project_system_types with its index, its four triggers and
// its rows (Jason's 2026-11-12 ruling removes the system-type kind outright).
const TABLE = 'project_system_types';
const ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8');
const createdBy3u = readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8');
const named = (path) => {
const db = new DatabaseSync(path, { readOnly: true });
try {
return {
master: db.prepare('SELECT type,name FROM sqlite_master WHERE name LIKE ? OR tbl_name=? ORDER BY type,name').all(`${TABLE}%`, TABLE).map((r) => ({ ...r })),
sequence: db.prepare('SELECT name FROM sqlite_sequence WHERE name=?').all(TABLE).map((r) => r.name),
digest: schemaDigest(db),
version: db.prepare("SELECT value FROM meta WHERE key='schema_version'").get().value,
};
} finally {
db.close();
}
};
test('SP9 a store opened at schema 3w has no project_system_types table', (t) => {
assert.equal(SCHEMA_VERSION, '3w');
assert.equal(ddl.includes(TABLE), false, 'schema.sql no longer creates the table');
const root = mkdtempSync(join(tmpdir(), 'sp9-fresh-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
new Store(root).close();
const fresh = named(join(root, 'bus/bus.sqlite'));
assert.equal(fresh.version, '3w');
assert.deepEqual(fresh.master, []);
});
test('SP9 a 3v store holding project_system_types rows migrates to 3w with nothing of the table left', (t) => {
const root = mkdtempSync(join(tmpdir(), 'sp9-3v-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
mkdirSync(join(root, 'bus'), { mode: 0o700 });
const path = join(root, 'bus/bus.sqlite');
const db = new DatabaseSync(path);
// The 3v schema is today's schema.sql plus what 3t-3u created; the store's step check refuses any
// other shape, so a migration that succeeds proves the fixture is a real 3v store.
db.exec(ddl + createdBy3u);
db.prepare('INSERT INTO meta VALUES (?,?)').run('schema_version', '3v');
db.prepare('INSERT INTO meta VALUES (?,?)').run('schema_digest', schemaDigest(db));
db.prepare('INSERT INTO meta VALUES (?,?)').run('comment_cursor_epoch', '2026-11-12T00:00:00.000Z');
db.prepare('INSERT INTO projects(id,at,business,name,repo,created_by) VALUES(?,?,?,?,?,?)').run('p1', '2026-11-12T00:00:00.000Z', 'acme', 'example', null, 'jason');
const select = db.prepare(`INSERT INTO ${TABLE}(at,business,project,types,by) VALUES(?,?,?,?,?)`);
select.run('2026-11-12T00:00:01.000Z', 'acme', 'p1', '["coding"]', 'jason');
select.run('2026-11-12T00:00:02.000Z', 'acme', 'p1', '["coding","montage"]', 'jason');
db.close();
chmodSync(path, 0o600);
const before = named(path);
assert.deepEqual(before.master.map((r) => r.name).sort(), [TABLE, `${TABLE}_business`, `${TABLE}_no_delete`, `${TABLE}_no_replace`, `${TABLE}_no_update`, `${TABLE}_project`]);
assert.deepEqual(before.sequence, [TABLE]);
assert.throws(() => new Store(root), /schema-mismatch/);
assert.deepEqual(named(path), before, 'the refused open changed nothing');
const store = new Store(root, { migrateSchema: true });
t.after(() => store.close());
assert.equal(store.get('SELECT name FROM projects WHERE id=?', 'p1').name, 'example', 'other rows stay');
const after = named(path);
assert.equal(after.version, '3w');
assert.deepEqual(after.master, [], 'no table, index or trigger of that name is left');
assert.deepEqual(after.sequence, [], 'no sqlite_sequence row of that name is left');
const freshRoot = mkdtempSync(join(tmpdir(), 'sp9-ref-'));
t.after(() => rmSync(freshRoot, { recursive: true, force: true }));
new Store(freshRoot).close();
assert.equal(after.digest, named(join(freshRoot, 'bus/bus.sqlite')).digest, 'the migrated schema is the fresh one');
});
+3 -2
View File
@@ -528,7 +528,8 @@ test('v3b prototype refusals, views and append-only mutations', (t) => {
// Task 4 (3p to 3q), Task 3 (3q to 3r) and Task 6b (3r to 3s) widen request_events.kind in place.
// #797 MVP3 (3s to 3t) adds project_archives and its five guards; project-archive.test.mjs tests them.
// 3t to 3u adds project_system_types and four guards; U4a #1873 (3u to 3v) adds store_tasks and its seven guards.
assert.equal(count('table') - 1, 33);
assert.equal(count('trigger'), 130);
// SP9 #1873 (3v to 3w) drops project_system_types and its four guards.
assert.equal(count('table') - 1, 32);
assert.equal(count('trigger'), 126);
assert.equal(count('view'), 5);
});
+4 -4
View File
@@ -530,9 +530,9 @@ test('T4 schema: a 3m store migrates to 3n, session_ops and message_cancels are
const before = tables(path);
assert.throws(() => new Store(root), /schema-mismatch/);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
const after = tables(path);
for (const name of ['session_ops', 'message_cancels', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events', 'project_archives', 'project_system_types', 'store_tasks']) {
for (const name of ['session_ops', 'message_cancels', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events', 'project_archives', 'store_tasks']) {
assert.deepEqual(after[name], [], name);
delete after[name];
}
@@ -571,7 +571,7 @@ async function engine3m(root) {
const source = readFileSync(here('../src/store.mjs'), 'utf8');
const edited = source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3m).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3m';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3m';")
.replace(/\}, \{\n {2}\/\/ Row 271 \(#1776\)[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
assert.ok(edited.includes("export const SCHEMA_VERSION = '3m';") && edited.includes('3l-3m') && !edited.includes('3m-3n'), 'the 3m engine has no 3m→3n step');
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
@@ -588,7 +588,7 @@ test('T4 schema: the 3m engine refuses a 3n store, migrating or not, and leaves
const Store3m = await engine3m(root);
assert.throws(() => new Store3m(root), /schema-mismatch/);
assert.throws(() => new Store3m(root, { migrateSchema: true }), /schema-mismatch/);
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(master(path), was);
assert.deepEqual(tables(path), before);
// The same engine opens a fresh 3m store, so the refusal is the version's and not a broken copy.
+3 -3
View File
@@ -374,7 +374,7 @@ test('T7: a 3k store migrates to 3l, each thread\'s default session has the thre
const { root, path } = source3k(t);
assert.throws(() => new Store(root), /schema-mismatch/);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
// The migrated schema is the fresh one, object for object.
const freshRoot = mkdtempSync(join(tmpdir(), 'sessions-fresh-'));
t.after(() => rmSync(freshRoot, { recursive: true, force: true }));
@@ -430,7 +430,7 @@ async function engine3k(root) {
const source = readFileSync(here('../src/store.mjs'), 'utf8');
const edited = source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3k).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3k';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3k';")
.replace(/\}, \{\n {2}\/\/ Row 264a \(#1769\)[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
assert.ok(edited.includes("export const SCHEMA_VERSION = '3k';") && edited.includes('3j-3k') && !edited.includes('3k-3l'), 'the 3k engine has no 3k→3l step');
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
@@ -447,7 +447,7 @@ test('T7 D9: the 3k engine refuses a 3l store, migrating or not, and leaves it a
const Store3k = await engine3k(root);
assert.throws(() => new Store3k(root), /schema-mismatch/);
assert.throws(() => new Store3k(root, { migrateSchema: true }), /schema-mismatch/);
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(master(path), was);
assert.deepEqual(rows(path), before);
// The same engine opens a fresh 3k store, so the refusal is the version's and not a broken copy.
+1 -1
View File
@@ -63,7 +63,7 @@ test('3e copy migrates through 3f to 3g preserving every old column/row and addi
assert.throws(() => new Store(root), /schema-mismatch/);
const store = new Store(root, { migrateSchema: true });
try {
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
assert.deepEqual({ ...store.get('SELECT spec_path,spec_commit FROM decisions') }, { spec_path: null, spec_commit: null });
assert.equal(store.get('PRAGMA quick_check').quick_check, 'ok');
assert.deepEqual(store.all('PRAGMA foreign_key_check'), []);
-109
View File
@@ -1,109 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Store } from '../src/store.mjs';
import { Broker } from '../src/broker.mjs';
import { installedSystemTypes } from '../../plugins/src/system-types.mjs';
test('project selections require enabled types, retain inactive selections, and only the owner writes', t => {
const root = mkdtempSync(join(tmpdir(), 'sys-'));
const store = new Store(root); t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
const business = { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: [], crossRole: [] } } }, projects: {}, plugins: { 'system-type': ['coding'] }, vars: { 'bus.tester': true } };
const broker = new Broker({ store, businesses: { demo: business } });
const owner = broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
const tester = broker.bindTester({ business: 'demo' });
const agent = broker.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi' });
const call = (verb, args = {}, cap = owner) => broker.request(cap, { verb, args });
const project = call('project.create', { name: 'example' }).id;
assert.deepEqual(call('project.system-types', { project }), { project, selected: [], active: [], inactive: [] });
assert.throws(() => call('project.system-types.set', { project, systemTypes: ['montage'] }), /system-type-not-enabled/);
assert.throws(() => call('project.system-types.set', { project, systemTypes: ['coding'] }, tester), /tester-refused/);
assert.throws(() => call('project.system-types.set', { project, systemTypes: ['coding'] }, agent), /human-required/);
assert.throws(() => call('project.system-types.set', { project, systemTypes: ['coding', 'coding'] }), /invalid-request/);
call('project.system-types.set', { project, systemTypes: ['coding'] });
assert.deepEqual(call('project.system-types', { project }).active, ['coding']);
broker.reloadBusiness('demo', { ...business, plugins: { 'system-type': [] } });
assert.deepEqual(call('project.system-types', { project }), { project, selected: ['coding'], active: [], inactive: ['coding'] });
assert.deepEqual(store.all('SELECT by FROM project_system_types').map(r => r.by), ['jason']);
assert.throws(() => store.run("UPDATE project_system_types SET types='[]'"), /append-only/);
assert.throws(() => store.run('DELETE FROM project_system_types'), /append-only/);
assert.throws(() => store.run("INSERT INTO project_system_types(at,business,project,types,by) VALUES(?,?,?,?,?)", new Date().toISOString(), 'other', project, '[]', 'jason'), /belongs to its project/);
});
test('console owner can read system types but cannot write project selections', async t => {
const root = mkdtempSync(join(tmpdir(), 'sys-'));
const store = new Store(root); t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
const business = { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: [], crossRole: [] } } }, projects: {}, plugins: { 'system-type': ['coding'] }, vars: { 'bus.tester': true } };
let discoveries = 0, enabled = ['coding'];
const broker = new Broker({ store, businesses: { demo: business }, organization: (_id, verb) => { if (verb === 'system-types.current') return enabled; if (verb === 'system-types.installed') { discoveries++; return installedSystemTypes(); } } });
const owner = broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
const consoleOwner = broker.bindConsole({ business: 'demo', human: 'jason', subject: { provider: 'test', iss: 'test', sub: 'owner' } });
const tester = broker.bindTester({ business: 'demo' });
assert.throws(() => broker.request(tester, { verb: 'plugins.set', args: { revision: 'a'.repeat(64), plugins: { 'system-type': ['coding'] } } }), /human-required/);
const project = broker.request(owner, { verb: 'project.create', args: { name: 'example' } }).id;
const result = await broker.request(consoleOwner, { verb: 'system-types.console', args: { project } });
assert.deepEqual(result.enabled, ['coding']);
assert.equal(result.installed.length, 5);
assert.equal(discoveries, 1);
broker.request(owner, { verb: 'project.system-types.set', args: { project, systemTypes: ['coding'] } });
enabled = [];
assert.deepEqual(broker.request(owner, { verb: 'project.system-types', args: { project } }).inactive, ['coding']);
assert.throws(() => broker.request(owner, { verb: 'project.system-types.set', args: { project, systemTypes: ['coding'] } }), /system-type-not-enabled/);
assert.deepEqual(result.project, { project, selected: [], active: [], inactive: [] });
assert.throws(() => broker.request(consoleOwner, { verb: 'project.system-types.set', args: { project, systemTypes: ['coding'] } }), /human-required/);
});
test('3t store migrates offline without altering existing project rows', async t => {
const { DatabaseSync } = await import('node:sqlite');
const { readFileSync, mkdirSync, chmodSync } = await import('node:fs');
const { schemaDigest } = await import('../src/store.mjs');
const root = mkdtempSync(join(tmpdir(), 'sys-migration-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
mkdirSync(join(root, 'bus'), { mode: 0o700 });
const dbpath = join(root, 'bus/bus.sqlite');
const db = new DatabaseSync(dbpath);
const ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8');
const step = readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8');
assert.equal(ddl.split(step).length, 2);
// 3u-3v (U4a) adds store_tasks after this step; a 3t store has neither.
const later = readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8');
db.exec(ddl.replace(step, '').replace(later, ''));
db.prepare('INSERT INTO meta VALUES(?,?)').run('schema_version', '3t');
db.prepare('INSERT INTO meta VALUES(?,?)').run('schema_digest', schemaDigest(db));
db.prepare('INSERT INTO meta VALUES(?,?)').run('comment_cursor_epoch', '2026-01-01T00:00:00.000Z');
db.prepare('INSERT INTO projects(id,at,business,name,repo,created_by) VALUES(?,?,?,?,?,?)').run('old', '2026-01-01T00:00:00.000Z', 'demo', 'old', null, 'jason');
db.close(); chmodSync(dbpath, 0o600);
assert.throws(() => new Store(root), /schema-mismatch/);
const migrated = new Store(root, { migrateSchema: true }); t.after(() => migrated.close());
assert.equal(migrated.get('SELECT name FROM projects WHERE id=?', 'old').name, 'old');
assert.equal(migrated.get('SELECT count(*) AS n FROM project_system_types').n, 0);
assert.equal(migrated.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
});
test('runtime reads the current organization file for project selections and console discovery', async t => {
const { mkdirSync } = await import('node:fs');
const { startBroker } = await import('../src/runtime.mjs');
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs');
const root = mkdtempSync(join(tmpdir(), 'sys-runtime-'));
const rolesDir = rolesCopy(root), doc = businessDoc(root, 'demo');
doc.plugins = { 'system-type': ['coding'] };
writeJson(join(root, 'businesses/demo.json'), doc);
mkdirSync(join(root, 'data'), { mode: 0o700 });
const business = { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: {chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: { authority: { withinRole: [], crossRole: [] } } } };
const bus = await startBroker({ dataRoot: join(root, 'data'), businesses: { demo: business }, organization: { dir: root, rolesDir, system: systemFor(root) } });
t.after(async () => { await bus.close(); rmSync(root, { recursive: true, force: true }); });
const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
const consoleOwner = bus.broker.bindConsole({ business: 'demo', human: 'jason', subject: { provider: 'test', iss: 'test', sub: 'owner' } });
const call = (verb, args = {}, cap = human) => bus.broker.request(cap, { verb, args });
const project = call('project.create', { name: 'example' }).id;
call('project.system-types.set', { project, systemTypes: ['coding'] });
const view = await call('system-types.console', { project }, consoleOwner);
assert.deepEqual(view.enabled, ['coding']);
assert.deepEqual(view.project.active, ['coding']);
assert.equal(view.installed.length, 5);
const plugins = call('plugins');
call('plugins.set', { revision: plugins.revision, plugins: { 'system-type': [] } });
assert.deepEqual(call('project.system-types', { project }).inactive, ['coding']);
});
+3 -2
View File
@@ -402,7 +402,8 @@ test('REQ-TASK-1 a 3u store migrates to 3v: store_tasks is new and empty, every
t.after(() => rmSync(root, { recursive: true, force: true }));
mkdirSync(join(root, 'bus'), { mode: 0o700 });
const path = join(root, 'bus/bus.sqlite'), db = new DatabaseSync(path);
db.exec(currentDDL.replace(stepSQL, ''));
// SP9 (#1873): a 3u store still has what 3t-3u created; 3v-3w drops it on the way to the fresh schema.
db.exec(currentDDL.replace(stepSQL, '') + readFileSync(here('../src/migrations/3t-3u.sql'), 'utf8'));
db.prepare('INSERT INTO meta VALUES (?,?)').run('schema_version', '3u');
db.prepare('INSERT INTO meta VALUES (?,?)').run('schema_digest', schemaDigest(db));
db.prepare('INSERT INTO meta VALUES (?,?)').run('comment_cursor_epoch', '2026-11-09T00:00:00.000Z');
@@ -414,7 +415,7 @@ test('REQ-TASK-1 a 3u store migrates to 3v: store_tasks is new and empty, every
assert.deepEqual(master(path), was, 'the refused open changed nothing');
const store = new Store(root, { migrateSchema: true });
t.after(() => store.close());
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.equal(store.get('SELECT id FROM events').id, 'preserved');
assert.deepEqual(store.all('SELECT * FROM store_tasks'), []);
const freshRoot = mkdtempSync(join(tmpdir(), 'ts-fresh-'));
+178
View File
@@ -0,0 +1,178 @@
// #1873 SP8: saving a template at the bus. Only the organization's owner saves or lists: the CLI's
// human cap on the unsuffixed names, the owner's console cap on the .console ones. The saved file
// applies through SP4 as saved:<name> for its saver.
import test from 'node:test';
import assert from 'node:assert/strict';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { CONSOLE_VERBS, TEMPLATE_BUS_KEYS, TESTER_ORGANIZATION_VERBS } from '../src/broker.mjs';
import { REFUSALS } from '../src/refusals.mjs';
import { VERB_RIGHTS } from '../../organization/src/org-rights.mjs';
const TOKEN = 'fixture-held-token-sp8-bus-3a9d';
const SUBJECT = { provider: 'test', iss: 'test', sub: 'owner' };
async function harness(t, { mutate = () => {} } = {}) {
const { startBroker } = await import('../src/runtime.mjs');
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs');
const { entryFromTemplate, readTemplate } = await import('../../organization/src/definitions.mjs');
const root = mkdtempSync(join(tmpdir(), 'tmpl-'));
const rolesDir = rolesCopy(root);
const doc = businessDoc(root, 'demo');
doc.plugins = { harness: ['pi'] };
const writer = entryFromTemplate(readTemplate(rolesDir, 'coder'));
writer.instructions = 'Write code for demo. Ask jason.';
doc.definitions = { writer };
// An instance of the org definition, with its own token files and tracker bot.
doc.roles.ana = { ...structuredClone(doc.roles.coder), definition: 'org:writer', tracker: { bot: 'bot-demo-ana', botId: 11 } };
mutate(doc, root);
writeJson(join(root, 'businesses/demo.json'), doc);
const data = join(root, 'data');
mkdirSync(data, { mode: 0o700 });
const token = join(root, 'held.token');
writeFileSync(token, TOKEN, { mode: 0o600 });
chmodSync(token, 0o600);
const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } },
coder: { authority: { withinRole: [], crossRole: [] }, credentials: { gitea: { file: token, rotateBy: '2099-01-01' } } } };
const businesses = { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, vars: { 'bus.tester': true }, roles } };
const bus = await startBroker({ dataRoot: data, businesses, organization: { dir: root, rolesDir, system: systemFor(root) } });
t.after(async () => { await bus.close(); rmSync(root, { recursive: true, force: true }); });
const b = bus.broker;
const consoleOwner = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT });
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
const call = async (verb, args = {}, cap = consoleOwner) => b.request(cap, { verb, args });
const path = join(root, 'businesses/demo.json');
const saved = (name, account = 'jason') => join(root, 'templates', account, `${name}.json`);
return { bus, b, root, rolesDir, path, consoleOwner, human, call, saved };
}
// Preview, then save with the preview's digest: the console names by default, the cli names for cap.
const save = async (call, name, extra = {}, cap) => {
const verb = cap ? 'template.save' : 'template.save.console';
const preview = await call(verb, { name, preview: true, ...extra }, cap);
return { preview, out: await call(verb, { name, digest: preview.digest, ...extra }, cap) };
};
test('SP8 red before: the owner saves an organization whose instance holds credentials; neither credentials nor copy material reach the file', async t => {
const { call, saved, root } = await harness(t);
const { preview, out } = await save(call, 'demo-shape');
assert.deepEqual(out, { name: 'demo-shape', scope: 'org', digest: preview.digest });
const text = readFileSync(saved('demo-shape'), 'utf8');
assert.equal(text, preview.json);
for (const k of ['copyBindings', 'copyId', 'orgIncarnation', 'generation', 'tracker', 'holder']) assert.ok(!text.includes(`"${k}"`), k);
assert.ok(!text.includes(join(root, 'secrets')), 'a token file path reached the template');
assert.ok(!text.includes('"file"'));
// The role's credential request lines are kept.
assert.ok(JSON.parse(text).definitions.writer.role.credentials.length > 0);
});
test('SP8 a saved template applies through SP4 as saved:<name> for its saver', async t => {
const { call, path } = await harness(t);
await save(call, 'demo-shape');
const listed = await call('template.list.console');
assert.deepEqual(listed.templates.map(x => [x.name, x.scope]), [['demo-shape', 'org']]);
const preview = await call('pack.preview.console', { pack: 'saved:demo-shape' });
assert.equal(preview.pack.origin, 'saved');
assert.ok(preview.hires.some(h => h.name === 'coder-1'), JSON.stringify(preview.hires.map(h => h.name)));
const before = JSON.parse(readFileSync(path, 'utf8'));
const out = await call('pack.apply.console', { pack: 'saved:demo-shape', digest: preview.digest });
const after = JSON.parse(readFileSync(path, 'utf8'));
assert.equal(after.packs.length, (before.packs ?? []).length + 1);
assert.equal(after.packs.at(-1).origin, 'saved');
assert.equal(after.packs.at(-1).id, 'demo-shape');
// writer is already the organization's own (same); the template references the coder template.
assert.ok(out.added.includes('definitions.coder'), JSON.stringify(out.added));
assert.equal(preview.hires.find(h => h.name === 'coder-1').definition, 'writer');
});
test('SP8 the owner\'s CLI cap saves on the unsuffixed name and writes the file', async t => {
const { call, human, saved } = await harness(t);
const { out } = await save(call, 'cli-shape', {}, human);
assert.equal(out.name, 'cli-shape');
assert.ok(existsSync(saved('cli-shape')));
assert.deepEqual((await call('template.list', {}, human)).templates.map(x => x.name), ['cli-shape']);
});
test('SP8 who may call: a member\'s session, an agent and the tester refuse; the owner\'s console cap cannot call the cli name', async t => {
const { b, call, human, root } = await harness(t);
assert.deepEqual(TESTER_ORGANIZATION_VERBS, ['limits', 'plugins', 'instances', 'plugins.set', 'instances.set', 'routes.set', 'routes']);
const member = b.bindHireConsole({ business: 'demo', actor: 'alice', subject: { provider: 'test', iss: 'test', sub: 'alice' }, rights: ['org.hire', 'board.read', 'org.setup'] });
for (const verb of ['template.save', 'template.save.console']) assert.throws(() => b.request(member, { verb, args: { name: 'x', preview: true } }), /human-required/);
for (const verb of ['template.list', 'template.list.console']) assert.throws(() => b.request(member, { verb, args: {} }), /human-required/);
const tester = b.bindTester({ business: 'demo' });
assert.throws(() => b.request(tester, { verb: 'template.save', args: { name: 'x', preview: true } }), /tester-refused/);
const agent = b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi', address: 'coder-run' });
b.request(agent, { verb: 'role.claim' });
for (const verb of ['template.save', 'template.save.console']) assert.throws(() => b.request(agent, { verb, args: { name: 'x', preview: true } }), /human-required/);
await assert.rejects(call('template.save', { name: 'x', preview: true }), /human-required/);
await assert.rejects(call('template.save.console', { name: 'x', preview: true }, human), /human-required/);
assert.ok(!existsSync(join(root, 'templates')));
});
test('SP8 the real credential store: a held value in kept text refuses template-secret-suspected with the field path as its reason', async t => {
// A definition holding one never loads (definition-holds-credential); workflow text is what save
// must catch itself.
const workflow = { description: `Ship it, key ${TOKEN}`, steps: [{ title: 'Write', state: 'todo', roles: ['writer'] }] };
const { call, root } = await harness(t, { mutate: doc => { doc.workflows = { change: workflow }; } });
await assert.rejects(call('template.save.console', { name: 'leaky', preview: true }), (e) => {
assert.equal(e.code, 'template-secret-suspected');
assert.equal(e.reason, 'workflows.change.description');
assert.ok(!String(e.message).includes(TOKEN) && !JSON.stringify(e).includes(TOKEN));
return true;
});
assert.ok(!existsSync(join(root, 'templates')));
});
test('SP8 a key that holds a held value: the refusal reason masks it as [key], never the value', async t => {
// A definition keyed by one never loads (definition-holds-credential); a workflow keyed by one is
// what save sees. The broker also drops any reason its credential check refuses.
const workflow = { description: 'Ship it.', steps: [{ title: 'Write', state: 'todo', roles: ['writer'] }] };
const { call, root } = await harness(t, { mutate: doc => { doc.workflows = { [TOKEN]: workflow }; } });
await assert.rejects(call('template.save.console', { name: 'leaky', preview: true }), (e) => {
assert.equal(e.code, 'template-secret-suspected');
assert.equal(e.reason, 'workflows[key]');
assert.ok(!String(e.message).includes(TOKEN) && !JSON.stringify(e).includes(TOKEN));
return true;
});
assert.ok(!existsSync(join(root, 'templates')));
});
test('SP8 a save after a real pack.apply.console keeps each packs entry as {id, version}', async t => {
const { call, saved } = await harness(t);
const packs = await call('packs.console');
const shipped = packs.packs.find(p => p.origin === 'shipped');
assert.ok(shipped, JSON.stringify(packs.packs.map(p => [p.id, p.origin])));
const pack = await call('pack.preview.console', { pack: shipped.id });
await call('pack.apply.console', { pack: shipped.id, digest: pack.digest });
await save(call, 'after-apply');
const kept = JSON.parse(readFileSync(saved('after-apply'), 'utf8')).packs;
assert.deepEqual(kept, [{ id: shipped.id, version: pack.pack.version }]);
});
test('SP8 argument checks: a second save, a .. name, an unknown key, preview with a digest, and a stale digest', async t => {
const { call, saved } = await harness(t);
const { preview } = await save(call, 'demo-shape');
const first = readFileSync(saved('demo-shape'));
await assert.rejects(call('template.save.console', { name: 'demo-shape', digest: preview.digest }), (e) => e.code === 'template-exists' && e.reason === 'a template of this name is already saved; save under another name');
assert.deepEqual(readFileSync(saved('demo-shape')), first);
for (const name of ['..', '../x', 'a/b']) await assert.rejects(call('template.save.console', { name, preview: true }), { code: 'invalid-request' });
for (const args of [{ name: 'x', preview: true, account: 'alex' }, { name: 'x', preview: true, digest: preview.digest }, { name: 'x' }, { preview: true }, { name: 'x', preview: false }])
await assert.rejects(call('template.save.console', args), { code: 'invalid-request' });
await assert.rejects(call('template.list.console', { account: 'alex' }), { code: 'invalid-request' });
await assert.rejects(call('template.save.console', { name: 'other', digest: preview.digest }), { code: 'pack-plan-stale' });
});
test('SP8 the schema pins: both .console verbs are console verbs, owner-only in VERB_RIGHTS, and template-secret-suspected has a rail', () => {
for (const verb of ['template.save.console', 'template.list.console']) {
assert.ok(CONSOLE_VERBS.includes(verb), verb);
assert.ok(Object.hasOwn(VERB_RIGHTS, verb) && VERB_RIGHTS[verb] === null, verb);
assert.ok(Object.hasOwn(TEMPLATE_BUS_KEYS, verb));
}
assert.ok(!CONSOLE_VERBS.includes('template.save') && !CONSOLE_VERBS.includes('template.list'));
const c = 'template-secret-suspected';
assert.ok(Object.hasOwn(REFUSALS, c));
assert.ok(REFUSALS[c].rail?.length > 5);
assert.ok(REFUSALS[c].nextStep?.length > 20);
assert.ok(!/(^|[\s("'`])(\/|~\/)/.test(REFUSALS[c].nextStep));
});
+3 -3
View File
@@ -49,7 +49,7 @@ async function database3d(t) {
const source = readFileSync(here('../src/store.mjs'), 'utf8');
const pinned = source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3d).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3d';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3d';")
.replace(/\}, \{\n {2}from: '3d',[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
assert.ok(!pinned.includes('3d-3e'), 'the 3d engine has no 3d→3e step');
writeFileSync(join(moduleRoot, 'src/store.mjs'), pinned);
@@ -101,7 +101,7 @@ test('REQ-THREAD-1 REQ-ORG-2: the 3d→3e migration adds the thread tables and l
assert.throws(() => new Store(root), /schema-mismatch/);
const store = new Store(root, { migrateSchema: true });
t.after(() => store.close());
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
for (const table of ['projects', 'threads', 'thread_claims', 'message_threads', 'thread_opens'])
assert.equal(store.get(`SELECT count(*) n FROM ${table}`).n, 0, table);
// Parked: the undelivered ones are counted per business and role, and none is changed or given a thread.
@@ -161,5 +161,5 @@ test('REQ-THREAD-1: the offline command prints the kept-message counts before th
const command = new URL('../src/migrate-schema.mjs', import.meta.url).pathname;
const migrated = spawnSync(process.execPath, [command, root], { encoding: 'utf8' });
assert.equal(migrated.status, 0, migrated.stderr);
assert.equal(migrated.stdout, [...REPORT, 'bus schema 3v ready', ''].join('\n'));
assert.equal(migrated.stdout, [...REPORT, 'bus schema 3w ready', ''].join('\n'));
});
+4 -4
View File
@@ -403,12 +403,12 @@ test('a 3l store migrates to 3m: thread_shares is new and empty, and every other
const before = tables(path);
assert.throws(() => new Store(root), /schema-mismatch/);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
const after = tables(path);
assert.deepEqual(after.thread_shares, []);
delete after.thread_shares;
// Row 271 (#1776): the 3m→3n step's session_ops and message_cancels, new and empty too.
for (const name of ['session_ops', 'message_cancels', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events', 'project_archives', 'project_system_types', 'store_tasks']) {
for (const name of ['session_ops', 'message_cancels', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events', 'project_archives', 'store_tasks']) {
assert.deepEqual(after[name], [], name);
delete after[name];
}
@@ -439,7 +439,7 @@ async function engine3l(root) {
const source = readFileSync(here('../src/store.mjs'), 'utf8');
const edited = source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3l).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3l';")
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3l';")
.replace(/\}, \{\n {2}\/\/ Row 206 \(193b2[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
assert.ok(edited.includes("export const SCHEMA_VERSION = '3l';") && edited.includes('3k-3l') && !edited.includes('3l-3m'), 'the 3l engine has no 3l→3m step');
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
@@ -456,7 +456,7 @@ test('the 3l engine refuses a 3m store, migrating or not, and leaves it as it wa
const Store3l = await engine3l(root);
assert.throws(() => new Store3l(root), /schema-mismatch/);
assert.throws(() => new Store3l(root, { migrateSchema: true }), /schema-mismatch/);
assert.equal(version(path), '3v');
assert.equal(version(path), '3w');
assert.deepEqual(master(path), was);
assert.deepEqual(tables(path), before);
// The same engine opens a fresh 3l store, so the refusal is the version's and not a broken copy.
+21
View File
@@ -121,3 +121,24 @@ test('committed mutation followed by dropped reply reports unknown and is never
assert.equal(calls, 1);
assert.equal(store.get('SELECT count(*) n FROM messages').n, 1);
});
// E2E6 fix 6: a connect that fails before the request is written is marked notSent; the code stays
// outcome-unknown for every caller. A request that left and got no answer is not marked.
test('E2E6 a failed connect is outcome-unknown marked notSent; a dropped answer is not marked', async (t) => {
const { createServer } = await import('node:net');
const { writeFileSync } = await import('node:fs');
const { NOT_SENT_EXIT } = await import('../src/client.mjs');
assert.equal(NOT_SENT_EXIT, 5);
const dir = mkdtempSync(join(tmpdir(), 'e2e6-'));
t.after(() => rmSync(dir, { recursive: true, force: true }));
const plain = join(dir, 'plain');
writeFileSync(plain, '');
for (const path of [join(dir, 'missing.sock'), plain, join(plain, 'below')]) {
await assert.rejects(new Client({ path, cap: 'x' }).call('inbox', {}), (e) => e.code === 'outcome-unknown' && e.notSent === true, path);
}
const sock = join(dir, 'drop.sock');
const server = createServer((c) => c.once('data', () => c.destroy()));
await new Promise((r) => server.listen(sock, r));
t.after(() => server.close());
await assert.rejects(new Client({ path: sock, cap: 'x' }).call('inbox', {}), (e) => e.code === 'outcome-unknown' && e.notSent === undefined);
});
+1 -1
View File
@@ -68,7 +68,7 @@ test('REQ-LIMIT-4 3g copy migrates through 3h, 3i, 3j and 3k to 3l preserving ev
assert.throws(() => new Store(root), /schema-mismatch/);
const store = new Store(root, { migrateSchema: true });
try {
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
assert.deepEqual({ ...store.get('SELECT to_name FROM messages') }, { to_name: null });
assert.equal(store.get('SELECT count(*) AS n FROM usage_records').n, 0);
assert.equal(store.get('SELECT name FROM instance_names').name, 'Builder');
@@ -0,0 +1,2 @@
// Old path of packages/organization/src/copy-binding-status.mjs (rename design r2, stage B).
export * from "../../organization/src/copy-binding-status.mjs";
+2
View File
@@ -0,0 +1,2 @@
// Old path of packages/organization/src/template-save.mjs (rename design r2, stage B).
export * from "../../organization/src/template-save.mjs";
-9
View File
@@ -1370,15 +1370,6 @@ field, digest and archive checks. Edit and archive need the task's digest from
`mosaic tasks --json` as `--expect`; a stale one is refused `task-conflict`
(exit 2) and nothing is written. They refuse inside an agent run.
System types are installed plug-ins. `mosaic org system-types installed` prints
module metadata; `mosaic org system-types enabled ID` prints an organization's enabled
ids. The owner changes the enabled list through the existing organization file
update or `mosaic plugins set`. `mosaic project system-types PROJECT --org ID`
shows selected, active and inactive ids. The owner replaces the selection with
`mosaic project system-types-set PROJECT --types coding,montage --org ID`; an
empty `--types ''` clears it. Disabled selections stay recorded but become
inactive. Project settings changes are append-only and require human proof.
## Accounts
`mosaic accounts` administers the instance-level accounts store locally, as the
+2 -1
View File
@@ -75,7 +75,8 @@ export function readAccounting({ dataRoot,business,start,end }) {
try {
db.exec('BEGIN');
// U4a (#1873) 3u-3v adds only store_tasks, which accounting never reads, so 3v joins the closed list.
if (!['3s','3t','3u','3v'].includes(db.prepare("SELECT value FROM meta WHERE key='schema_version'").get()?.value)) fail('test-store-schema-refused');
// SP9 (#1873) 3v-3w drops only the table 3t-3u added, which accounting never reads, so 3w joins too.
if (!['3s','3t','3u','3v','3w'].includes(db.prepare("SELECT value FROM meta WHERE key='schema_version'").get()?.value)) fail('test-store-schema-refused');
const store = { all:(sql,...args) => db.prepare(sql).all(...args) };
const records = turnRecords(store,business);
const raw = store.all("SELECT * FROM usage_records WHERE business=? AND source<>'@proxy/cliproxy-queue' ORDER BY seq",business);
+1 -1
View File
@@ -79,7 +79,7 @@ test('accounting reads a real store at the current bus schema version',t=>{
const records=readAccounting({dataRoot:f.dataRoot,business:f.c.business,start:f.c.start,end:f.c.end});
assert.equal(records.length,1);assert.equal(records[0].input,1);
});
for(const version of ['3s','3r','3v',null]) test(`accounting ${version==='3s'?'accepts':'refuses'} schema ${version??'missing'}`,t=>{
for(const version of ['3s','3r','3x',null]) test(`accounting ${version==='3s'?'accepts':'refuses'} schema ${version??'missing'}`,t=>{
const f=setup(t,100);
// Only this disposable fixture is altered; the production meta table is append-only.
f.store.run('DROP TRIGGER meta_no_update');f.store.run('DROP TRIGGER meta_no_delete');
+2 -1
View File
@@ -778,7 +778,7 @@ export async function main(argv, io = { env: process.env, stdin: process.stdin,
if (["inbox", "decide", "tasks", "agents", "trail"].includes(verb)) return human(verb, rest, io, deps);
if (verb === "project" && rest[0] === "north-star") return northStarCommand(rest.slice(1), io, (p) => callFor(p, io, deps));
if (verb === "project" && rest[0] === "interview") return interviewCommand(rest.slice(1), io);
if (verb === "project") return ["create", "list", "archive", "unarchive", "system-types", "system-types-set"].includes(rest[0]) ? projectBusCommand(rest, io, (p) => channelCallFor(p, io, deps)) : projectCommand(rest, io, deps, (p) => callFor(p, 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));
if (verb === "session") return sessionCommand(rest, io, (p) => channelCallFor(p, io, deps));
if (verb === "thread") return threadCommand(rest, io, (p) => channelCallFor(p, io, deps), formatMessage);
@@ -845,6 +845,7 @@ export async function run(argv, io = { env: process.env, stdin: process.stdin, s
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`);
if (typeof error.next === "string" && error.next !== "") io.stderr.write(`mosaic: next: ${error.next}\n`);
return error.exitCode;
}
}
+5 -1
View File
@@ -3,11 +3,15 @@
// input, a decision or option that doesn't exist); 3 refused (system or
// business config problem, the broker or notifier refused, the human proof
// refused, a host already running); 4 usage.
//
// E2E6: `next` is an optional next-step line. run() prints it after the message as
// `mosaic: next: ...`; the message itself stays as callers (the Console among them) match it.
export class CliError extends Error {
constructor(message, exitCode = 2) {
constructor(message, exitCode = 2, { next } = {}) {
super(message);
this.name = "CliError";
this.exitCode = exitCode;
if (typeof next === "string" && next !== "") this.next = next;
}
}
+7 -1
View File
@@ -76,6 +76,11 @@ 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.
// E2E6 fix 1: every stale-lock refusal names the verb that clears a host that died.
export const recoverNext = (business) =>
typeof business === "string" && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(business)
? `if the bus host died, clear it with mosaic bus recover ${business}; recover refuses while the host still runs`
: "if the bus host died, clear it with mosaic bus recover <org>; recover refuses while the host still runs";
export function claimHostLock(dataRoot, state, { found = () => {}, between = () => {}, refuseStale = false } = {}) {
mkdirSync(hostDir(dataRoot), { recursive: true, mode: 0o700 });
const file = hostLock(dataRoot);
@@ -94,7 +99,8 @@ 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", 3);
// E2E6 fix 1: the message stays (the Console matches it); the next line names the clearing verb.
if (refuseStale) throw new CliError("stale bus host lock", 3, { next: recoverNext(prior.business) });
between();
try {
renameSync(file, aside);
+12
View File
@@ -11,6 +11,8 @@
// instance.request until the request closes or --wait runs out (D1.1 item 5).
import { readFileSync } from "node:fs";
import { CliError } from "./errors.mjs";
import { copyBindingStatus } from "../../organization/src/copy-binding-status.mjs";
import { SERVICES } from "../../organization/src/vocabulary.mjs";
export const INSTANCES_USAGE = "mosaic business instances BUSINESS [--json] [--tester] | " +
"business instance add BUSINESS INSTANCE --definition DEF [--vars FILE] [--launch] [--bot BOT --bot-id N] --revision REV [--tester] | " +
@@ -219,6 +221,16 @@ export function formatInstances(view) {
const vars = Object.keys(entry.vars).length ? ` vars ${JSON.stringify(entry.vars)}` : "";
lines.push(` ${name}: ${entry.definition}${marks.length ? ` (${marks.join(", ")})` : ""}${vars}`);
}
// #1873 CB5: each copy that needs a credential binding, with the bus's status code and its fixed
// next step (organization copy-binding-status.mjs). A code the table doesn't know prints its own
// fixed fallback; nothing else from the answer is printed for it.
for (const [name, copy] of Object.entries(view.copies ?? {})) {
if (!copy?.binding) continue;
const { status, next } = copyBindingStatus(copy.binding.status);
// The services the copy lacks, as registered service labels only; nothing else the bus sent.
const missing = copy.state === "unbound" && Array.isArray(copy.missing) ? SERVICES.filter((s) => copy.missing.includes(s)) : [];
lines.push(` ${name} (copy): ${status}${missing.length ? ` (missing ${missing.join(", ")})` : ""}. ${next}`);
}
const definitions = Object.entries(view.definitions);
const accepted = definitions.filter(([, d]) => d.accepted).map(([name]) => name);
const bound = definitions.filter(([, d]) => d.services.length).map(([name, d]) => `${name} (${d.services.join(", ")})`);
+11 -9
View File
@@ -8,7 +8,7 @@ import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolveOrg, withOrgLifecycle } from '../../organization/src/org-registry.mjs';
import { orgPaths } from './config.mjs';
import { cmdlineOf, hostFile, hostLock, hostStatus, readHostState, startTimeOf, stopHost } from './host.mjs';
import { cmdlineOf, hostFile, hostLock, hostStatus, readHostState, recoverNext, startTimeOf, stopHost } from './host.mjs';
import { CliError } from './errors.mjs';
import { readBootStatus } from './boot-status.mjs';
@@ -27,20 +27,22 @@ async function target(ctx, id, allowDeleting = false) {
return { ...org, paths: orgPaths(org.root) };
}
function checkedState(org) {
// E2E6 fix 1: each refusal keeps its message and adds the recover line as its next step.
const stale = message => { throw new CliError(message, 3, { next: recoverNext(org.id) }); };
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');
catch (e) { if (e.code !== 'ENOENT') stale('bus host lock is unreadable'); }
if (lock && (!Number.isSafeInteger(lock.pid) || typeof lock.startTime !== 'string' || startTimeOf(lock.pid) !== lock.startTime)) stale('stale bus host lock');
if (s.host && (!s.host.live || s.host.business !== org.id)) stale('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 (i < 0 || args[i + 1] !== 'bus' || args[i + 2] !== 'start' || args[i + 3] !== org.id) stale('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');
if (lock && (!s.host || lock.pid !== s.host.pid || lock.business !== org.id)) stale('bus host lock does not match host state');
if (s.writerLock && !s.writerLock.live) stale('stale bus writer lock');
if (!s.host && s.writerLock) stale('bus writer lock has no matching host');
if (s.host && !lock) stale('bus host lock is missing');
return s;
}
export function probeBusSocket(path, { signal, deadline } = {}) {
+72 -7
View File
@@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto';
import { processState } from '../../seat/src/process-state.mjs';
import { spawn } from 'node:child_process';
import { request as httpRequest } from 'node:http';
import { constants, closeSync, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, renameSync, writeFileSync, writeSync } from 'node:fs';
import { constants, closeSync, fstatSync, ftruncateSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, rmSync, renameSync, writeFileSync, writeSync } from 'node:fs';
import { basename, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { listOrgs, resolveOrg } from '../../organization/src/org-registry.mjs';
@@ -60,14 +60,79 @@ function removeStale(ctx, s, warn) {
warn(`mosaic: stale console state moved to ${aside}\n`);
return true;
}
function takeLock(ctx) {
// E2E6 fix 4: what a start.lock that already exists names. Null when it names no holder: unreadable,
// empty (a holder between its open and its write), not a regular file of this user, or a bad pid or
// start time. Such a lock is never cleared. A lock its holder marked released ({"released":true},
// written through the holder's own descriptor) names no live holder and is cleared (f6c review P3-1).
function lockHolder(path) {
let fd;
try { fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
catch { return null; }
try {
const st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || st.nlink !== 1 || st.size > 4096) return null;
const data = JSON.parse(readFileSync(fd, 'utf8'));
if (data?.released === true && Object.keys(data).length === 1) return { released: true, dev: st.dev, ino: st.ino };
if (!Number.isSafeInteger(data?.pid) || data.pid <= 0 || typeof data.startTime !== 'string' || !/^\d+$/.test(data.startTime)) return null;
return { pid: data.pid, startTime: data.startTime, dev: st.dev, ino: st.ino };
} catch { return null; } finally { closeSync(fd); }
}
// True when the process the lock names (pid and start time) is gone. A /proc read that cannot settle
// it counts as live, so an uncertain holder is never cleared.
function holderGone(holder) {
if (holder.released) return true;
let p;
try { p = processState(holder.pid); } catch { return false; }
return !p.alive || p.startTime !== holder.startTime;
}
// Moves aside and removes the lock the holder check read, and only that file: a lock another start
// took in between is put back and the clear refuses.
function clearLock(path, holder) {
const aside = `${path}.dead-${randomBytes(4).toString('hex')}`;
try { renameSync(path, aside); } catch { fail('console start lock changed while it was being cleared; run the command again'); }
const st = lstatSync(aside);
if (st.dev !== holder.dev || st.ino !== holder.ino) {
// Put back without replacing: if a third start took the path meanwhile, the aside stays for a human.
let restored = false;
try { linkSync(aside, path); restored = true; } catch { /* left aside */ }
if (restored) rmSync(aside, { force: true });
fail('console start lock changed while it was being cleared; run the command again');
}
rmSync(aside, { force: true });
}
function takeLock(ctx, warn = () => {}) {
checkDir(ctx);
const path = join(directory(ctx), 'start.lock');
let fd; try { fd = openSync(path, 'wx', 0o600); } catch { fail('console start lock exists'); }
let fd;
for (let cleared = false; fd === undefined;) {
try { fd = openSync(path, 'wx', 0o600); }
catch (e) {
if (e?.code !== 'EEXIST' || cleared) fail('console start lock exists');
// E2E6 fix 4: a lock whose named holder (pid and start time) is gone is cleared once, and the
// take tried again. A live holder, or a lock that names none, refuses and stays.
const holder = lockHolder(path);
if (!holder) fail('console start lock exists and names no holder; remove console/start.lock under the data root once no console start or stop runs');
if (!holderGone(holder)) fail(`console start lock is held by process ${holder.pid}, which still runs; wait for its start or stop to finish`);
clearLock(path, holder);
warn(holder.released ? 'mosaic: cleared a console start lock its holder had released\n' : `mosaic: cleared a console start lock left by process ${holder.pid}, which is gone\n`);
cleared = true;
}
}
// D1: the lock names its holder, so a detached start that timed out can clear the lock of the
// supervisor it killed (clearDeadLock) and no other.
// supervisor it killed (clearDeadLock), and a later start or stop can clear it once that holder is gone.
try { writeSync(fd, JSON.stringify({ pid: process.pid, startTime: startTimeOf(process.pid) })); } catch { /* an unnamed lock is only never cleared */ }
return () => { closeSync(fd); rmSync(path, { force: true }); };
// f6c review P3-1: the release marks its own file released, through its own descriptor, and removes
// the path only while the path is still that file. A clear that moved the lock aside and puts it back
// after this release puts back a released lock, which the next start or stop clears; and the release
// never removes a lock another start took meanwhile.
// A failed marker write (disk full) still removes the path: an empty lock would refuse forever.
return () => {
try {
try { ftruncateSync(fd, 0); writeSync(fd, JSON.stringify({ released: true }), 0); } catch { /* the matched unlink below still runs */ }
const own = fstatSync(fd), now = lstatSync(path);
if (own.dev === now.dev && own.ino === now.ino) rmSync(path, { force: true });
} catch { /* gone already, or moved aside by a clear */ } finally { closeSync(fd); }
};
}
// Removes start.lock only when it names this exact process (pid and start time) and that process
// is gone. A lock that is unreadable, unnamed or another holder's stays.
@@ -242,7 +307,7 @@ export async function consoleStart(ctx, { devLocalOwner = false, detach = false,
// re-check P1). A Restart Console relaunch goes through relaunch(), not here.
refuseInsideAgent(process.env); if (ctx.env) refuseInsideAgent(ctx.env);
if (detach && devLocalOwner) return detachedSupervisor(ctx, { port, bind, allowHosts, warn });
const release = takeLock(ctx);
const release = takeLock(ctx, warn);
let run, args;
try {
const prior = state(ctx);
@@ -266,7 +331,7 @@ export async function consoleStart(ctx, { devLocalOwner = false, detach = false,
return result;
}
export async function consoleStop(ctx, { warn = s => process.stderr.write(s) } = {}) {
const release = takeLock(ctx);
const release = takeLock(ctx, warn);
try {
const s = state(ctx);
if (!s || removeStale(ctx, s, warn)) return { stopped: false };
+34
View File
@@ -4,10 +4,12 @@
// credential store, whose held-secret check every definition write needs (R1), and no other process
// holds the tokens it looks for. Each write prints its receipt.
import { readFile } from 'node:fs/promises';
import { basename } from 'node:path';
import { configDir } from '../../organization/src/organization-config.mjs';
import { organizationWriter } from '../../organization/src/organization.mjs';
import { definitionVerbs } from '../../organization/src/definition-verbs.mjs';
import { checkDefinition } from '../../organization/src/definitions.mjs';
import { ID_PATTERN } from '../../organization/src/vocabulary.mjs';
import { rolesDir } from './config.mjs';
import { CliError } from './errors.mjs';
import { busExit, refuseInsideAgent } from './transport.mjs';
@@ -59,6 +61,33 @@ function refusal(code, verb, name, rev, read) {
return error;
}
// E2E6 fix 2: a read or a write's file check that fails says so in one line: an unknown
// organization, a missing definition or a bad file, by code. No stack trace, and no host path: a raw
// filesystem error is named by its operation and code, and any absolute path left in a message is cut.
const NAME_SHOWN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
function missing(id, name) { return `${NAME_SHOWN.test(name ?? '') ? `definitions declares no ${name}` : 'definitions declares no definition by that name'}; list them with mosaic org definitions ${id} list`; }
// f6c review P3-3: a path after ':' or a backtick is cut too.
function pathFree(message) { return String(message).replace(/(^|[\s'"(=:`])\/[^\s'"),`]*/g, '$1<path>'); }
export function definitionsFailure(e, id, name) {
if (e instanceof CliError) return e;
const coded = (code, text, exit = 3) => Object.assign(new CliError(`${code}: ${text}`, exit), { code });
if (typeof e?.syscall === 'string' && typeof e.code === 'string' && /^E[A-Z0-9]+$/.test(e.code)) {
// f6c review P3-2: only a missing organization file means no organization; another missing file
// (the roles directory for `templates`, a role template) says which operation failed.
if (e.code === 'ENOENT' && (typeof e.path !== 'string' || basename(e.path) === `${id}.json`)) return coded('unknown-organization', `this instance has no organization ${id}; list them with mosaic org list`);
if (id === 'templates' || id === undefined) return coded('definitions-unreadable', `cannot ${e.syscall} a file the definitions command reads (${e.code})`);
return coded('organization-unreadable', `cannot ${e.syscall} the file of organization ${id} (${e.code})`);
}
const message = typeof e?.message === 'string' ? e.message : '';
if (/^definitions declares no /.test(message)) return coded('definition-missing', missing(id, name));
if (typeof e?.code === 'string' && /^[a-z][a-z-]{0,63}$/.test(e.code)) {
const text = e.code === 'definition-missing' ? missing(id, name) : pathFree(message);
return coded(e.code, text);
}
if (e?.name === 'OrganizationConfigError' || e?.name === 'BusinessError') return coded('organization-invalid', pathFree(message));
return coded('definitions-failed', 'the definitions command failed', 1);
}
// deps.callFor(parsed) returns the human's call(verb, args) for the organization parsed names (cli.mjs
// passes its callFor). Without one, a write refuses: it never runs without the bus's secret check.
export async function definitionsCommand(args, io, deps = {}) {
@@ -68,6 +97,10 @@ export async function definitionsCommand(args, io, deps = {}) {
const ifRev = () => { const r = need('--if-rev'); if (!/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid --if-rev', 4); return Number(r); };
const [id, verb, name] = pos;
if (WRITES.includes(verb)) refuseInsideAgent(io.env);
if (id !== 'templates' && id !== undefined && !ID_PATTERN.test(id)) throw new CliError(`invalid organization id: an id is 1 to 64 of a-z, 0-9, '.', '_' and '-', starting with a letter or digit\n${DEFINITIONS_USAGE}`, 4);
try { await run(); }
catch (e) { throw definitionsFailure(e, id, name); }
async function run() {
const writer = deps.writer ?? organizationWriter({ dir: configDir(io.env), rolesDir: rolesDir(io.env) });
const verbs = definitionVerbs({ writer, rolesDir: rolesDir(io.env) });
let result;
@@ -93,4 +126,5 @@ export async function definitionsCommand(args, io, deps = {}) {
}
} else throw new CliError(DEFINITIONS_USAGE, 4);
io.stdout.write(JSON.stringify(result, null, 2) + '\n');
}
}
+21 -2
View File
@@ -100,6 +100,15 @@ export function autonomyDocument(current, changes) {
return out;
}
// E2E6 fix 7: the autonomy-precondition check the CLI can see from the read and the document it sent:
// owner notices off while a class sits near the loop, or moves away from the loop.
const ORDER = ['in the loop', 'near the loop', 'out of the loop'];
export function preconditionSeen(current, doc) {
if (doc?.notices === true) return undefined;
const moved = CLASSES.filter(c => ORDER.indexOf(doc?.[c]) > ORDER.indexOf(current?.levels?.[c] ?? 'in the loop'));
if (CLASSES.some(c => doc?.[c] === 'near the loop') || moved.length) return 'owner notices are off; a class near the loop, or one moving away from it, needs them on (--notices on)';
return undefined;
}
// Autonomy codes carry no message from the route; these are the panel's own words (autonomy.js).
const AUTONOMY_GUIDANCE = {
'revision-conflict': 'the organization changed; read it again with mosaic org autonomy ID',
@@ -117,6 +126,10 @@ function refused(answer, what, unknown, guidance = {}) {
const code = typeof raw === 'string' && /^[a-z-]{1,64}$/.test(raw) ? raw : `http ${answer.status}`;
if (code === 'outcome-unknown' || (answer.status >= 500 && !raw)) return new CliError(`outcome unknown: ${unknown}`, 1);
const said = typeof answer.body?.message === 'string' ? answer.body.message.slice(0, 500) : guidance[code];
// E2E6 fix 7: a route that forwards the bus's reason (which check failed) has it said first; the
// general guidance becomes the next line. A reason is one printable line, never a control byte.
const reason = typeof answer.body?.reason === 'string' && /^[\x20-\x7e]{1,300}$/.test(answer.body.reason) ? answer.body.reason : undefined;
if (reason) return new CliError(`${what} refused (${code}): ${reason}`, 3, { next: said });
return new CliError(`${what} refused (${code})${said ? `: ${said}` : ''}`, 3);
}
@@ -173,8 +186,14 @@ export async function orgOwnerCommand(args, io, deps = {}) {
const read = await consoleOrgRequest(ctx, id, { path: 'autonomy' });
if (!ok(read)) throw refused(read, 'autonomy set', 'the autonomy read did not finish; nothing was changed', AUTONOMY_GUIDANCE);
const unknown = `the setting may have been saved; check mosaic org autonomy ${id} before trying again`;
const answer = await consoleOrgRequest(ctx, id, { method: 'POST', path: 'autonomy', body: { revision, autonomy: autonomyDocument(read.body, changes) }, unknown });
if (!ok(answer)) throw refused(answer, 'autonomy set', unknown, AUTONOMY_GUIDANCE);
const doc = autonomyDocument(read.body, changes);
const answer = await consoleOrgRequest(ctx, id, { method: 'POST', path: 'autonomy', body: { revision, autonomy: doc }, unknown });
if (!ok(answer)) {
// E2E6 fix 7: a route that forwards no reason still gets the one check this side can see.
const local = answer.body?.error === 'autonomy-precondition' && answer.body?.reason === undefined ? preconditionSeen(read.body, doc) : undefined;
if (local) throw new CliError(`autonomy set refused (autonomy-precondition): ${local}`, 3, { next: AUTONOMY_GUIDANCE['autonomy-precondition'] });
throw refused(answer, 'autonomy set', unknown, AUTONOMY_GUIDANCE);
}
return { value: answer.body, text: b => `saved autonomy for ${id}: revision ${b.revision ?? 'unknown'}${b.transaction ? `, receipt ${b.transaction}` : ''}\n` };
};
} else if (step === 'review') {
+60 -11
View File
@@ -4,7 +4,6 @@ import { basename } from 'node:path';
import { listOrgs, orgLock, resolveOrg } from '../../organization/src/org-registry.mjs';
import { organizationWriter } from '../../organization/src/organization.mjs';
import { templateRender, validateOrg, createOrg, showOrg, updateOrg, queueInit, queueShow, projectRepoInit, projectRepoShow, projectRepoSet, deleteOrg, archiveOrg, restoreOrg } from '../../organization/src/org-service.mjs';
import { installedSystemTypes } from '../../plugins/src/system-types.mjs';
import { installedStarterPacks } from '../../plugins/src/starter-pack.mjs';
import { DEV_OWNER, devOwnerInstance } from './dev-local-owner.mjs';
import { loadSystem, orgPaths, rolesDir } from './config.mjs';
@@ -17,7 +16,9 @@ import { definitionsCommand, DEFINITIONS_USAGE } from './org-definitions.mjs';
import { orgOwnerCommand, ORG_OWNER_USAGE, ORG_OWNER_VERBS } from './org-owner.mjs';
// Starter packs SP4 (design section 8): the owner's pack, board and workflow words.
export const PACKS_USAGE = 'mosaic org pack list | pack preview ID PACK [--project P] [--as KIND.NAME=NEW]... | pack apply ID PACK --digest D [--project P] [--as KIND.NAME=NEW]... | board set ID FILE [--project P] | workflow set ID NAME FILE [--project P] | workflow remove ID NAME [--project P]';
export const ORG_USAGE = 'mosaic org system-types installed | system-types enabled ID | lock show|clear ID | reload ID | archive ID --if-revision N [--json] | restore ID --if-revision N [--no-bus] [--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] | project repo set ID PROJECT (--local-only --accept-local-loss-risk | --remote URL) --if-revision N [--json] | ' + DEFINITIONS_USAGE.slice('mosaic org '.length) + ' | ' + ORG_OWNER_USAGE.slice('mosaic org '.length) + ' | ' + PACKS_USAGE.slice('mosaic org '.length) + ' | config validate ID | config resolve ID INSTANCE [--project ID]';
// #1873 SP8: saving the organization, or one project, as a template; the owner's alone.
export const TEMPLATE_USAGE = 'mosaic org template save ID NAME [--project P] (--preview | --digest D) | template list ID';
export const ORG_USAGE = 'mosaic org lock show|clear ID | reload ID | archive ID --if-revision N [--json] | restore ID --if-revision N [--no-bus] [--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] | project repo set ID PROJECT (--local-only --accept-local-loss-risk | --remote URL) --if-revision N [--json] | ' + DEFINITIONS_USAGE.slice('mosaic org '.length) + ' | ' + ORG_OWNER_USAGE.slice('mosaic org '.length) + ' | ' + PACKS_USAGE.slice('mosaic org '.length) + ' | ' + TEMPLATE_USAGE.slice('mosaic org '.length) + ' | config validate ID | config resolve ID INSTANCE [--project ID]';
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', '--no-bus'].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 }; }
// A refusal names the document by label and the error by its code, never by host path or bytes (PR #1916).
const errorCode = e => typeof e?.code === 'string' && /^E[A-Z0-9]+$/.test(e.code) ? e.code : 'unreadable';
@@ -62,7 +63,22 @@ const PACK_REFUSALS = {
'invalid-request': 'the bus refused the request: a bad argument, a project that is not a code project, or a pack that fails the composition check',
'human-required': 'only the organization\'s owner, from a human shell, may do this',
'starter-pack-not-found': 'no installed, shipped or saved pack has that name',
'starter-pack-invalid': 'the pack fails its own checks or the composition check; nothing was written',
'starter-pack-template-changed': 'a template the pack names changed since it was loaded; preview again',
'not-configured': 'this organization\'s bus has no organization writer configured',
};
// E2E6 fix 3: humanTransport throws a CliError whose message and .code are the bus code alone. A code
// a table knows gains its hint, as template save's do; a next line it carried is kept. Any other
// CliError (bus-not-running with its own next line, a usage error) passes unchanged, and a bare coded
// error (a test's call) becomes a CliError named by its code.
function hinted(e, ...tables) {
const code = typeof e?.code === 'string' ? e.code : '';
const table = tables.find(t => Object.hasOwn(t, code));
if (!table && (code === '' || e instanceof CliError)) return e;
const error = new CliError(table ? `${code}: ${table[code]}` : code, busExit(code), { next: e.next });
error.code = code;
return error;
}
async function packsCommand(args, io, deps) {
const pos = [], values = {}, as = [];
for (let i = 0; i < args.length; i++) {
@@ -90,8 +106,7 @@ async function packsCommand(args, io, deps) {
else if (word === 'workflow' && sub === 'remove' && pos.length === 4) { allowed('--project'); verb = 'workflow.set'; revised = true; request = { name, workflow: null, ...project }; }
else throw new CliError(PACKS_USAGE, 4);
// pack list names no organization, so it reads what the bus's `packs` verb reads (the shipped and
// installed packs, never a saved template) from this instance's data root, as `system-types
// installed` does.
// installed packs, never a saved template) from this instance's data root.
if (verb === 'packs') {
const system = deps.system ?? loadSystem({ env: io.env, instance: devOwnerInstance(io.env) });
const { packs } = await installedStarterPacks({ dataRoot: system.dataRoot, rolesDir: rolesDir(io.env) });
@@ -105,11 +120,46 @@ async function packsCommand(args, io, deps) {
}
let result;
try { result = await call(verb, request); }
catch (e) { throw hinted(e, PACK_REFUSALS); }
io.stdout.write(JSON.stringify(result, null, 2) + '\n');
}
// #1873 SP8: template save previews (--preview) or writes the previewed JSON (--digest); template
// list prints the owner's saved templates. Both call the bus as the owner's human; an agent run is
// refused before the bus is called.
const TEMPLATE_REFUSALS = {
'template-secret-suspected': 'a field the template would keep holds a credential the organization\'s bus holds; remove it and preview again (nothing was written)',
'pack-plan-stale': 'the organization or a template changed since the preview; preview again and save its digest',
'template-exists': 'a template of this name is already saved for your account; nothing was written',
'invalid-request': 'the bus refused the request: a bad name, an unknown project, or an organization the template cannot hold',
'human-required': 'only the organization\'s owner, from a human shell, may do this',
'not-configured': 'the templates directory is missing or not private to this instance\'s user',
'starter-pack-invalid': 'the template would not be a valid pack: more than 8 hires, or a definition wider than its template',
};
async function templateCommand(args, io, deps) {
const pos = [], values = {}, flags = new Set();
for (let i = 0; i < args.length; i++) {
const a = args[i];
if (['--project', '--digest'].includes(a)) {
if (!args[i + 1] || args[i + 1].startsWith('--') || values[a] !== undefined) throw new CliError(`${a} needs one value`, 4);
values[a] = args[++i];
} else if (a === '--preview') { if (flags.has(a)) throw new CliError('--preview is given twice', 4); flags.add(a); }
else if (a.startsWith('--')) throw new CliError(`unknown option ${a}`, 4);
else pos.push(a);
}
const [, sub, id, name] = pos;
refuseAgentRun(io);
let verb, request;
if (sub === 'save' && pos.length === 4 && flags.has('--preview') !== (values['--digest'] !== undefined)) {
verb = 'template.save';
request = { name, ...(values['--project'] === undefined ? {} : { project: values['--project'] }), ...(flags.has('--preview') ? { preview: true } : { digest: values['--digest'] }) };
} else if (sub === 'list' && pos.length === 3 && !flags.size && !Object.keys(values).length) { verb = 'template.list'; request = {}; }
else throw new CliError(TEMPLATE_USAGE, 4);
let result;
try { result = await packCall(id, io, deps)(verb, request); }
catch (e) {
if (typeof e?.code !== 'string' || e.code === '' || e instanceof CliError) throw e;
const why = PACK_REFUSALS[e.code];
const error = new CliError(why ? `${e.code}: ${why}` : e.code, busExit(e.code));
error.code = e.code;
const error = hinted(e, TEMPLATE_REFUSALS, PACK_REFUSALS);
// E2E6 fix 8: a taken name says what to do next.
if (error.code === 'template-exists' && error.next === undefined) error.next = `preview and save under another name, or see the saved names with mosaic org template list ${id}`;
throw error;
}
io.stdout.write(JSON.stringify(result, null, 2) + '\n');
@@ -122,6 +172,7 @@ export async function orgCommand(args, io, deps = {}) {
// E2 (#1873, P1-3): the hire form and the autonomy panel, through the running Console (org-owner.mjs).
if (ORG_OWNER_VERBS.includes(args[0])) return await orgOwnerCommand(args, io, deps);
if (['pack', 'board', 'workflow'].includes(args[0])) return await packsCommand(args, io, deps);
if (args[0] === 'template' && ['save', 'list'].includes(args[1])) return await templateCommand(args, io, deps);
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]; };
@@ -133,10 +184,8 @@ export async function orgCommand(args, io, deps = {}) {
let lifecycle = null;
if (['archive', 'restore'].includes(words[0]) && words.length === 2) { allowed(['--json', ...(words[0] === 'restore' ? ['--no-bus'] : [])], ['--if-revision']); lifecycle = { ifRevision: revision(p.values['--if-revision'], true) }; if (words[0] === 'archive' || !p.flags.has('--no-bus')) refuseAgentRun(io); }
else if (words[0] === 'delete' && words.length === 2) { allowed(['--json'], ['--confirm', '--if-revision']); const ifRevision = revision(p.values['--if-revision'], false); lifecycle = { confirm: need('--confirm'), ifRevision }; refuseAgentRun(io); }
if (words[0] === 'system-types' && words[1] === 'installed' && words.length === 2) { allowed(['--json'], []); io.stdout.write(JSON.stringify(await installedSystemTypes(), null, 2) + '\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] === 'system-types' && words[1] === 'enabled' && words.length === 3) { allowed(['--json'], []); const org = await showOrg(ctx, words[2]); result = { organization: words[2], enabled: org.doc.plugins?.['system-type'] ?? [] }; }
else if (words[0] === 'lock' && ['show', 'clear'].includes(words[1]) && words.length === 3) { allowed([], []); result = await orgLock(ctx, words[2], { clear: words[1] === 'clear' }); }
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 (words[0] === 'archive' && words.length === 2) result = await archiveOrg(ctx, words[1], { ...lifecycle, busStop: busStopForDelete, copyRemote: await (deps.copyRemoteFor ?? copyRemoteFor)(ctx) });
+2 -15
View File
@@ -14,8 +14,6 @@ export const PROJECT_USAGE = 'mosaic project init <business> <project> [--tester
// and check above work on the repository on disk and never contact the bus, except that
// init --tester (row 175, #1673) binds the tester once before it writes.
export const PROJECT_BUS_USAGE = [
'mosaic project system-types <name-or-id> [--business <id>] [--json]',
'mosaic project system-types-set <name-or-id> --types <comma-separated-ids> [--business <id>]',
'mosaic project create <name> [--repo <repo>] [--business <id>] [--tester]',
'mosaic project list [--archived] [--business <id>] [--json] [--tester]',
'mosaic project archive <name-or-id> [--business <id>]',
@@ -30,8 +28,8 @@ export async function projectBusCommand(args, io, callFor) {
? { flags: ['--tester'], values: ['--repo', '--business'] }
: sub === 'list'
? { flags: ['--json', '--tester', '--archived'], values: ['--business'] }
: { flags: sub === 'system-types' ? ['--json'] : [], values: sub === 'system-types-set' ? ['--business', '--types'] : ['--business'] });
const one = ['create', 'archive', 'unarchive', 'system-types', 'system-types-set'].includes(sub);
: { flags: [], values: ['--business'] });
const one = ['create', 'archive', 'unarchive'].includes(sub);
if (parsed.positional.length !== (one ? 1 : 0))
throw new CliError(`project ${sub} takes ${one ? (sub === 'create' ? 'one name' : 'one name or id') : 'no arguments'}\n${PROJECT_BUS_USAGE}`, 4);
const call = callFor(parsed);
@@ -47,17 +45,6 @@ export async function projectBusCommand(args, io, callFor) {
}
return io.stdout.write(`created project ${name} id ${made.id}\n`);
}
if (sub === 'system-types' || sub === 'system-types-set') {
const projects = await call('project.list', { includeArchived: true });
const ref = parsed.positional[0];
const hit = projects.find(p => p.id === ref) ?? projects.find(p => p.name === ref);
if (!hit) throw new CliError(`no project is named or numbered ${JSON.stringify(ref)}`, 2);
const write = sub === 'system-types-set';
if (write && parsed.values['--types'] === undefined) throw new CliError('--types is required (an empty string clears the list)', 4);
const systemTypes = parsed.values['--types'] ? parsed.values['--types'].split(',') : [];
const result = await call(write ? 'project.system-types.set' : 'project.system-types', { project: hit.id, ...(write ? { systemTypes } : {}) });
return io.stdout.write(JSON.stringify(result, null, 2) + '\n');
}
// #797 MVP3: the owner's reversible archive. Resolution reads the full list, archived included,
// so an archived project can be named for unarchive.
if (sub === 'archive' || sub === 'unarchive') {
+13
View File
@@ -41,6 +41,7 @@ const EXIT = {
"tester-required": 3,
"tester-off": 3,
"tester-refused": 3,
"bus-not-running": 3,
"outcome-unknown": 1,
"outcome-unknown-revoked": 1,
"response-too-large": 1,
@@ -51,6 +52,17 @@ export function busExit(code) {
return EXIT[code] ?? 2;
}
// E2E6 fix 6: the shim exits 5 (packages/bus/src/client.mjs NOT_SENT_EXIT) when its connect failed
// before the request was written: no socket, or the connect refused. That is a stopped bus, and
// nothing happened; outcome-unknown stays for a request that left and got no answer.
const NOT_SENT_EXIT = 5;
export function busNotRunning(business) {
const error = new CliError(`bus-not-running: the bus for ${business} is not running, so the request was not sent`, 3);
error.code = "bus-not-running";
error.next = `start it with mosaic bus start ${business}, or clear a host that died with mosaic bus recover ${business}`;
return error;
}
// Returns a call(verb, args) for one business. `cli` and `spawn` exist for
// the tests; the command line never sets them.
export function humanTransport({ socket, business, env = process.env, cli = HUMAN_CLI, spawn = spawnSync, timeoutMs = 30000 }) {
@@ -65,6 +77,7 @@ export function humanTransport({ socket, business, env = process.env, cli = HUMA
if (proc.error || proc.status === null) throw new CliError("outcome-unknown: the bus transport did not finish", 1);
if (proc.status !== 0) {
const code = String(proc.stderr ?? "").trim().split("\n").reverse().find((l) => /^[a-z-]{1,64}$/.test(l)) ?? "invalid-response";
if (proc.status === NOT_SENT_EXIT && code === "outcome-unknown") throw busNotRunning(business);
const error = new CliError(code, busExit(code));
error.code = code;
throw error;
+361 -8
View File
@@ -8,6 +8,7 @@ import { requestHostOps } from './request-host-ops.mjs';
import { test } from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import { DatabaseSync } from "node:sqlite";
import { chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, symlinkSync, writeFileSync } from "node:fs";
import { join } from "node:path";
@@ -24,10 +25,13 @@ import { startTimeOf } from "../src/host.mjs";
import { createLauncher } from "../src/launcher.mjs";
import { fakeHarnesses, fixture, tmp, withoutHostCredentials } from "./helpers.mjs";
import { fakeGiteaApi } from "../../bus/tests/fake-gitea-api.mjs";
import { send as giteaSend } from "../../bus/src/gitea-grant.mjs";
import { giteaGrant, send as giteaSend } from "../../bus/src/gitea-grant.mjs";
import { organizationWriter } from "../../organization/src/organization.mjs";
import { activateOrg, reserveOrg } from "../../organization/src/org-registry.mjs";
import { activateOrg, listOrgs, reserveOrg } from "../../organization/src/org-registry.mjs";
import * as orgService from "../../organization/src/org-service.mjs";
import { copyBotLogin } from "../../organization/src/copy-bindings.mjs";
import { COPY_BINDING_STATUS } from "../../organization/src/copy-binding-status.mjs";
import { formatInstances } from "../src/instances.mjs";
const FAKE = fileURLToPath(new URL("../../harness/tests/fixtures/fake-adapter.mjs", import.meta.url));
const skip = sandboxProblem() ?? false;
@@ -60,8 +64,8 @@ function gitRepo(dir) {
// holds gate (merge-gate, no credential: bound), researcher (research, needs gitea: unbound in every
// organization) and sec (security-reviewer, excluded from acme). The coder's gitea token file is
// named for, and holds, the test secret, so it is bound elsewhere in the organization.
function prepare(t, edit = (doc) => doc) {
const root = tmp(t, "mosaic-copy-");
function prepare(t, edit = (doc) => doc, prefix = "mosaic-copy-") {
const root = tmp(t, prefix);
const f = fixture(root, "acme", (doc) => {
doc.roles.chief.vars = { harness: "pi", model: "claude-opus-5-5", thinking: "high" };
doc.roles.coder.vars.model = "claude-sonnet-5-5";
@@ -86,7 +90,7 @@ function prepare(t, edit = (doc) => doc) {
return { root, f, adapter };
}
async function start(t, { f, adapter }, { businessId = "acme", orgRoot, copyTransport = null, beforeBind = null } = {}) {
async function start(t, { f, adapter }, { businessId = "acme", orgRoot, copyTransport = null, beforeBind = null, readOnly = [FAKE] } = {}) {
const system = loadSystem({ env: f.env });
const { trackers, ...boot } = bootConfig({ system, businessId, env: f.env, ...(orgRoot ? { orgRoot } : {}) });
const runtime = await startBroker({ ...boot, ...(copyTransport ? { copyTransport } : {}) });
@@ -143,7 +147,7 @@ async function start(t, { f, adapter }, { businessId = "acme", orgRoot, copyTran
makeLoader: await fakeHarnesses(t, { pi: adapter, "claude-code": adapter }),
sessionDefaults: { pollInterval: 100 },
harnessVersion: "0.85.1",
readOnly: [FAKE],
readOnly,
threadTickMs: 3600000,
stopTimeoutMs: 1000,
log: (l) => logs.push(l),
@@ -605,11 +609,11 @@ test("A3: a second organization boots its own copies from the same root file and
// #1873 CB3: the researcher copy (research, gitea alone) bound and granted on a fake Gitea, through
// the real bus, its boot check and the launcher. `sent` is every request the bus sends to the service.
async function boundCopy(t) {
async function boundCopy(t, edit = (doc) => doc) {
const api = await fakeGiteaApi(t, tmp(t, "cb3-api-"));
const p = prepare(t, (doc) => {
doc.projects.stack.remote = `${api.url}/owner/stack`;
return doc;
return edit(doc);
});
const ctx = { configDir: join(p.root, "config"), dataRoot: p.f.dataRoot, instance: "test" };
chmodSync(ctx.configDir, 0o700);
@@ -756,3 +760,352 @@ test("CB3 race: an organization file change between the launcher's admission and
assert.ok(events.some((e) => e.event === "refused" && e.why === "launch bind refused: organization-changed"), JSON.stringify(events));
assert.ok(!events.some((e) => e.event === "launch"), JSON.stringify(events));
});
// #1873 CB5 (CB4 review P3-2): startBroker's own wiring for a former copy a named instance now
// shadows. At start the named instance's slot is held (never loaded as its credential), and the first
// pass begins the shadowed revoke without waiting for the minute's sweep; the revoke deletes the copy's
// token at the service through the organization's admin slot.
test("CB5 runtime: a restart over a named instance that shadows a bound copy holds the slot, and the start's own pass revokes the copy at the service", { skip, timeout: 120000 }, async (t) => {
const p = await boundCopy(t);
const first = await start(t, p, { copyTransport: p.copyTransport });
const login = await p.bind(first);
await first.close();
const day = (n) => new Date(Date.now() + n * 86400000).toISOString().slice(0, 10);
giteaGrant({ configDir: join(p.root, "config") }).writeAdmin("acme", p.api.adminToken, { url: p.api.url, account: "mosaic-grant", rotateBy: day(60), setAt: new Date().toISOString() });
// The owner names an instance over the copy, through the writer; the binding record stays as it was.
const writer = organizationWriter({ dir: join(p.root, "config"), rolesDir: p.f.roles, dataRoot: p.f.dataRoot });
const { document, revision } = writer.read("acme");
const bindings = JSON.stringify(document.copyBindings);
const d = structuredClone(document);
d.roles.researcher = { definition: "research", vars: VARS, credentials: { gitea: { store: true, rotateBy: day(30) } } };
writer.write("acme", d, { revision, actor: "local-dev-owner" });
assert.equal(JSON.stringify(writer.read("acme").document.copyBindings), bindings);
// The token's delete waits until the test has looked at the held slot.
let go, arrived;
const deleting = new Promise((r) => { arrived = r; });
const release = new Promise((r) => { go = r; });
p.api.rule((e) => e.method === "DELETE" && e.path.includes("/tokens/"), async () => { arrived(); await release; return { pass: true }; });
const s = await start(t, p, { copyTransport: p.copyTransport });
// The start's own pass, not the minute's sweep, begins it.
await Promise.race([deleting, new Promise((_, no) => setTimeout(() => no(new Error("no revoke began at start")), 20000).unref())]);
assert.equal(p.rec().state, "revoking");
assert.equal(p.rec().revoke.cause, "shadowed");
// The slot went before the remote step. Had start loaded it as the named instance's credential
// (no hold), the discard would have refused and the record would never finish.
const slot = join(p.root, "config", "secrets", "agents", "acme", "researcher", "gitea");
assert.equal(existsSync(slot), false);
assert.deepEqual(s.runtime.credentialStatus("acme", "researcher").map((c) => [c.service, c.state]), [["gitea", "not-issued"]]);
go();
await until(() => p.rec().state === "unbound" && p.rec().revoke?.done === true);
assert.deepEqual(p.rec().revoke.remote, { gitea: "revoked" });
assert.deepEqual(p.rec().revoke.failed ?? null, null);
assert.deepEqual(p.api.tokens(login), []);
assert.ok(p.api.requests.some((e) => e.method === "DELETE" && e.as === "mosaic-grant"));
});
// #1873 CB5 (CB4 review P3-2): the gate's loss at boot reaches the revoker through startBroker's
// onLoss. A bound copy whose slot is gone at the next start is lost `credential-unavailable`, and its
// binding is revoked at the service with cause `unavailable`, without waiting for a sweep.
test("CB5 runtime: a bound copy whose slot is gone at restart is lost at boot, and onLoss revokes it at the service", { skip, timeout: 120000 }, async (t) => {
const p = await boundCopy(t);
const first = await start(t, p, { copyTransport: p.copyTransport });
const login = await p.bind(first);
await first.close();
const day = (n) => new Date(Date.now() + n * 86400000).toISOString().slice(0, 10);
giteaGrant({ configDir: join(p.root, "config") }).writeAdmin("acme", p.api.adminToken, { url: p.api.url, account: "mosaic-grant", rotateBy: day(60), setAt: new Date().toISOString() });
const slot = join(p.root, "config", "secrets", "agents", "acme", "researcher", "gitea");
assert.ok(existsSync(slot));
renameSync(slot, slot + ".gone");
const s = await start(t, p, { copyTransport: p.copyTransport });
assert.deepEqual(s.runtime.copyBound, {});
await until(() => p.rec().state === "unbound" && p.rec().revoke?.done === true, 20000);
assert.equal(p.rec().revoke.cause, "unavailable");
assert.deepEqual(p.rec().revoke.remote, { gitea: "revoked" });
assert.deepEqual(p.api.tokens(login), []);
assert.equal(refusedAt(s, "researcher").code, "credential-unavailable");
});
// #1873 CB5 two-organization acceptance (A3's deferred test). acme and beta each bind the shipped
// research copy (researcher, gitea alone) on one fake Gitea, each run by its own bus with its own
// transport. beta is registered as the organization service makes one (registry entry, owner proof,
// the derived project root), so archive and delete run on it. A transport keeps the sha256 of every
// credential its bus sends, never the value.
const LOCAL = { provider: "local-dev", iss: "local-dev", sub: "local-dev-owner" };
const THREAD_ADAPTER = fileURLToPath(new URL("./fixtures/thread-adapter.mjs", import.meta.url));
const sha = (x) => createHash("sha256").update(String(x)).digest("hex");
function hashingTransport(api) {
const auth = [];
return { auth, ca: api.ca, send: (req) => {
auth.push({ op: `${req.method} ${req.path.split("?")[0]}`, hash: sha(req.auth?.basic ? req.auth.basic.slice(1).join(":") : req.auth?.token) });
return giteaSend(req);
} };
}
async function twoOrgs(t) {
const api = await fakeGiteaApi(t, tmp(t, "cb5-api-"));
const p = prepare(t, (doc) => {
doc.projects.stack.remote = `${api.url}/owner/stack`;
return doc;
// A short root: beta's data root is the registry's <dataRoot>/org-runtime/beta, and a run's socket
// path under it must fit 107 bytes.
}, "c5-");
// The thread adapter keeps a history in the thread's store and answers with what it found there.
writeFileSync(p.adapter, `exec '${process.execPath}' '${THREAD_ADAPTER}'\n`);
const configDir = join(p.root, "config");
const dataRoot = p.f.dataRoot;
const ctx = { configDir, dataRoot, instance: "test", principal: LOCAL };
chmodSync(configDir, 0o700);
chmodSync(join(configDir, "businesses"), 0o700);
await reserveOrg(ctx, "acme");
await activateOrg(ctx, "acme");
const beta = businessDoc(p.root, "beta");
for (const r of Object.values(beta.roles)) r.vars = { ...VARS, ...r.vars, model: r.vars?.model ?? "claude-sonnet-5-5" };
beta.roles.chief.vars = { harness: "pi", model: "claude-opus-5-5", thinking: "high" };
beta.launch = launchDoc("chief", ["coder"], { opus: 1, sonnet: 4 });
beta.plugins = { ...beta.plugins, harness: ["pi", "claude-code"] };
beta.projects.stack.remote = `${api.url}/beta/stack`;
beta.projects.stack.root = join(dataRoot, "org-runtime", "beta", "projects", "stack");
writeJson(join(configDir, "businesses", "beta.json"), beta);
const entry = await reserveOrg(ctx, "beta");
writeFileSync(join(entry.root, "owner.json"), JSON.stringify(LOCAL) + "\n", { mode: 0o600, flag: "wx" });
gitRepo(beta.projects.stack.root);
await activateOrg(ctx, "beta");
const writer = organizationWriter({ dir: configDir, rolesDir: p.f.roles, dataRoot });
const day = (n) => new Date(Date.now() + n * 86400000).toISOString().slice(0, 10);
const orgs = {
acme: { id: "acme", repo: api.addRepo("owner", "stack"), transport: hashingTransport(api), orgRoot: undefined },
beta: { id: "beta", repo: api.addRepo("beta", "stack"), transport: hashingTransport(api), orgRoot: entry.root },
};
for (const o of Object.values(orgs)) {
o.rec = () => writer.read(o.id).document.copyBindings?.researcher;
o.slot = join(configDir, "secrets", "agents", o.id, "researcher", "gitea");
o.start = () => start(t, p, { businessId: o.id, orgRoot: o.orgRoot, copyTransport: o.transport, readOnly: [THREAD_ADAPTER] });
// Bind (the owner's console) and issue (the human), on a bus that is already up.
o.bind = async (s) => {
const owner = s.runtime.broker.bindConsole({ business: o.id, human: writer.read(o.id).document.human, subject: { provider: "test", iss: "https://issuer.test", sub: "owner" } });
await s.runtime.broker.request(owner, { verb: "hire.start.console", args: { op: "change", level: "root", name: "researcher", reason: "Bind the copy",
binding: { services: { gitea: { scopes: ["read:repository", "read:user"], projects: ["stack"] } } } } });
const r = o.rec();
o.login = copyBotLogin(r.orgIncarnation, r.copyId, "gitea");
api.addUser(o.login);
o.repo.collaborators.push(o.login); o.repo.permissions[o.login] = "read";
o.token = api.addToken(o.login, "t1", ["read:repository", "read:user"]).sha1;
await s.call("credentials.issue", { agent: "researcher", service: "gitea", date: day(30), token: o.token });
giteaGrant({ configDir }).writeAdmin(o.id, api.adminToken, { url: api.url, account: "mosaic-grant", rotateBy: day(60), setAt: new Date().toISOString() });
};
}
return { p, api, ctx, writer, orgs, day };
}
const orgLaunches = (s) => existsSync(launchLogFile(s.dataRoot, s.host.business))
? readFileSync(launchLogFile(s.dataRoot, s.host.business), "utf8").trim().split("\n").map((l) => JSON.parse(l)) : [];
// Send to a thread and wait for the copy's RESULT reply; the reply says what the thread's store held.
async function askThread(s, thread, body) {
const sent = s.call("thread.send", { thread, body });
await s.launcher.tick();
const reply = await until(() => s.call("thread.messages", { thread }).find((m) => m.in_reply_to === sent.id && m.class === "RESULT"), 30000)
.catch((e) => { throw new Error(`no reply: ${JSON.stringify(orgLaunches(s).slice(-3))}`, { cause: e }); });
return { sent, reply, saw: JSON.parse(reply.body.match(/THREAD (\{.*\})/)[1]) };
}
test("CB5 two organizations: each binds and runs its own research copy in a thread; identities, records and tokens stay apart; B works through A's revoke, archive and delete; A's raw binding or grant replayed in B is refused", { skip, timeout: 240000 }, async (t) => {
const { api, ctx, writer, orgs, day } = await twoOrgs(t);
const A = orgs.beta, B = orgs.acme;
// Bind each, then restart each so its boot proves the slot live.
for (const o of [A, B]) {
const first = await o.start();
await o.bind(first);
await first.close();
o.s = await o.start();
assert.deepEqual(o.s.runtime.copyBound, { researcher: ["gitea"] }, o.id);
}
assert.notEqual(A.login, B.login);
assert.notEqual(A.rec().copyId, B.rec().copyId);
assert.notEqual(A.rec().orgIncarnation, B.rec().orgIncarnation);
assert.notEqual(sha(readFileSync(A.slot, "utf8")), sha(readFileSync(B.slot, "utf8")));
// One thread per organization, launched, written and read.
for (const o of [A, B]) {
const project = o.s.call("project.create", { name: "stack work", repo: "stack" }).id;
o.thread = o.s.call("thread.create", { project, name: `work-${o.id}`, instance: "researcher" }).id;
const first = await askThread(o.s, o.thread, `hello ${o.id}`);
assert.deepEqual(first.saw.before, [], o.id);
const launch = orgLaunches(o.s).find((e) => e.event === "launch" && e.thread === o.thread);
assert.ok(launch, o.id);
o.run = launch.run;
[o.entry] = o.s.running("researcher");
assert.equal(o.entry.run, o.run, o.id);
o.session = o.s.runtime.threadsState(o.id).find((x) => x.thread === o.thread).session;
o.store = join(o.s.dataRoot, "threads", o.id, o.thread, "sessions", o.session);
assert.equal(readFileSync(join(o.store, "session", "history"), "utf8"), `hello ${o.id}\n`, o.id);
}
// Distinct session, run, trail, workspace and slot identities.
assert.notEqual(A.s.dataRoot, B.s.dataRoot);
for (const k of ["thread", "session", "run"]) assert.notEqual(A[k], B[k], k);
assert.notEqual(A.entry.workspace, B.entry.workspace);
assert.notEqual(A.slot, B.slot);
for (const [mine, other] of [[A, B], [B, A]]) {
assert.ok(mine.entry.workspace.startsWith(mine.s.dataRoot + "/"), mine.id);
assert.ok(mine.entry.runDir.startsWith(join(mine.s.dataRoot, "launches", mine.id) + "/"), mine.id);
// No record of the other's is visible here: not its thread, its session, its run or its request.
const seen = trail(mine.s.dataRoot);
for (const k of ["thread", "session", "run"]) assert.equal(seen.includes(other[k]), false, `${mine.id} trail holds ${other.id}'s ${k}`);
// The default organization's data root is the instance's, and every named organization's root
// sits under its org-runtime: that subtree is the other bus's, not this one's.
const own = readdirSync(mine.s.dataRoot, { recursive: true }).filter((x) => mine.orgRoot || !x.startsWith("org-runtime/"));
assert.equal(own.join("\n").includes(other.run), false, mine.id);
assert.ok(!mine.s.call("thread.list").some((x) => x.id === other.thread), mine.id);
assert.throws(() => mine.s.call("thread.messages", { thread: other.thread }), BusError, mine.id);
assert.ok(!mine.s.running("researcher").some((x) => x.run === other.run), mine.id);
}
// Reopen each: a restart of its bus, and the same thread finds its own history and session.
for (const o of [A, B]) {
await o.s.close();
o.s = await o.start();
const again = await askThread(o.s, o.thread, `again ${o.id}`);
assert.deepEqual(again.saw.before, [`hello ${o.id}`], o.id);
assert.equal(o.s.runtime.threadsState(o.id).find((x) => x.thread === o.thread).session, o.session, o.id);
}
// So far no request of B's bus carried A's token, and none of A's carried B's.
// The issue's identity check (copy-grant verifyGiteaToken): the server version, then the token's owner.
const CHECK = ["GET /api/v1/version", "GET /api/v1/token"];
const carried = (o, token) => o.transport.auth.filter((x) => x.hash === sha(token)).map((x) => x.op);
assert.deepEqual(carried(B, A.token), []);
assert.deepEqual(carried(A, B.token), []);
assert.ok(carried(B, B.token).length > 0);
// A's raw binding record written into B's file is refused, and B's file is as it was.
const { document, revision } = writer.read(B.id);
const replay = structuredClone(document);
replay.copyBindings = { ...replay.copyBindings, researcher: A.rec() };
assert.throws(() => writer.write(B.id, replay, { revision, actor: "local-dev-owner" }), (e) => e.code === "organization-bindings-protected");
assert.equal(writer.read(B.id).revision, revision);
// A's grant (its live token) issued to B's copy is refused at the service's identity check. The
// issue deactivates first (design section 5): the record is left pending with no grant and the
// staged file goes, so A's token is never B's credential. B's copy keeps its own identity.
const bound = B.rec();
await assert.rejects(Promise.resolve().then(() => B.s.call("credentials.issue", { agent: "researcher", service: "gitea", date: day(30), token: A.token })),
(e) => e instanceof BusError && e.code === "credential-identity-refused");
assert.equal(B.rec().state, "pending");
assert.equal(B.rec().services.gitea.grantId, undefined);
for (const k of ["copyId", "orgIncarnation", "decision"]) assert.equal(B.rec()[k], bound[k], k);
assert.equal(existsSync(B.slot), false);
// The only requests that carried A's token from B's bus are the identity check that refused it.
assert.deepEqual(carried(B, A.token), CHECK);
// The owner's recovery is a new verified issue of B's own token, then a restart.
B.token = api.addToken(B.login, "t2", ["read:repository", "read:user"]).sha1;
await B.s.call("credentials.issue", { agent: "researcher", service: "gitea", date: day(30), token: B.token });
assert.equal(B.rec().state, "bound");
await B.s.close();
B.s = await B.start();
assert.deepEqual(B.s.runtime.copyBound, { researcher: ["gitea"] });
const bTokens = api.tokens(B.login).length;
// B keeps working through each of A's lifecycle steps.
const bWorks = async (label) => {
const r = await askThread(B.s, B.thread, `after ${label}`);
assert.equal(r.saw.body, `after ${label}`, label);
assert.equal(B.rec().state, "bound", label);
assert.ok(existsSync(B.slot), label);
assert.equal(api.tokens(B.login).length, bTokens, label);
assert.deepEqual(B.s.runtime.copyBound, { researcher: ["gitea"] }, label);
};
const out = await A.s.call("credentials.revoke", { agent: "researcher", service: "gitea", reason: "Owner revoke" });
assert.equal(out.state, "unbound");
assert.deepEqual(out.remote, { gitea: "revoked" });
assert.deepEqual(api.tokens(A.login), []);
await until(() => A.s.running("researcher").length === 0);
await bWorks("revoke");
const rev = async () => (await listOrgs(ctx)).find((o) => o.id === A.id).revision;
const busStop = async () => { await A.s.close(); return { stopped: true, reason: "stopped" }; };
const archived = await orgService.archiveOrg(ctx, A.id, { ifRevision: await rev(), busStop });
assert.equal(archived.outcome, "done");
await bWorks("archive");
const deleted = await orgService.deleteOrg(ctx, A.id, { confirm: A.id, ifRevision: await rev(), busStop });
assert.equal(deleted.outcome ?? "done", "done");
assert.equal(existsSync(A.slot), false);
await bWorks("delete");
// Through all of it, B's bus sent A's token only in the refused issue's check, and A's never sent B's.
assert.deepEqual(carried(B, A.token), CHECK);
assert.deepEqual(carried(A, B.token), []);
assert.ok(carried(B, B.token).length > 0);
});
// #1873 CB5 operator flow: the copy's binding status in the owner's instances read and in
// `mosaic org instances`, at each step a binding takes, as fixed text. The copy's token is the
// sentinel: it, its hash and its leading fragment never appear in a read, the CLI text, the trail,
// the launcher's log or the bus's own stderr.
test("CB5 operator: the owner's instances read and mosaic org instances show each copy's binding status and next step in fixed text; the token never appears", { skip, timeout: 120000 }, async (t) => {
// bus.tester on, so the tester's read below is answered and its exclusion is the broker's.
const p = await boundCopy(t, (doc) => ({ ...doc, vars: { ...(doc.vars ?? {}), "bus.tester": true } }));
const said = [];
const write = process.stderr.write;
process.stderr.write = function (chunk, ...rest) { said.push(String(chunk)); return write.call(this, chunk, ...rest); };
t.after(() => { process.stderr.write = write; });
const first = await start(t, p, { copyTransport: p.copyTransport });
const seen = [];
const status = (s) => { const v = s.call("instances"); seen.push(JSON.stringify(v), formatInstances(v)); return v.copies.researcher.binding; };
const fixed = (code) => ({ status: code, next: COPY_BINDING_STATUS[code] });
// Nothing approved yet.
assert.deepEqual(status(first), fixed("credential-unbound"));
// Approved: the owner's next step is the issue.
const owner = first.runtime.broker.bindConsole({ business: "acme", human: p.f.doc.human, subject: { provider: "test", iss: "https://issuer.test", sub: "owner" } });
await first.runtime.broker.request(owner, { verb: "hire.start.console", args: { op: "change", level: "root", name: "researcher", reason: "Bind the copy",
binding: { services: { gitea: { scopes: ["read:repository", "read:user"], projects: ["stack"] } } } } });
assert.equal(p.rec().state, "pending");
assert.deepEqual(status(first), fixed("issue-required"));
// Issued: a restart verifies it.
const r = p.rec();
const login = copyBotLogin(r.orgIncarnation, r.copyId, "gitea");
p.api.addUser(login);
const repo = p.api.repos.find((x) => x.owner === "owner" && x.name === "stack");
repo.collaborators.push(login); repo.permissions[login] = "read";
const token = p.api.addToken(login, "t1", ["read:repository", "read:user"]).sha1;
await first.call("credentials.issue", { agent: "researcher", service: "gitea", date: new Date(Date.now() + 30 * 86400000).toISOString().slice(0, 10), token });
assert.equal(p.rec().state, "bound");
assert.deepEqual(status(first), fixed("restart-required"));
await first.close();
// Verified at the next start.
const s = await start(t, p, { copyTransport: p.copyTransport });
assert.deepEqual(s.runtime.copyBound, { researcher: ["gitea"] });
assert.deepEqual(status(s), fixed("ready"));
// A tester's read lists the copy and never its binding status (design section 4).
const tester = s.runtime.broker.bindTester({ business: "acme" });
const asTester = await s.runtime.broker.request(tester, { verb: "instances" });
assert.ok(asTester.copies?.researcher, "the tester sees the copy");
assert.equal(Object.hasOwn(asTester.copies.researcher, "binding"), false);
seen.push(JSON.stringify(asTester));
// The owner's revoke.
await s.call("credentials.revoke", { agent: "researcher", service: "gitea", reason: "Owner revoke" });
assert.deepEqual(status(s), fixed("credential-revoked"));
// The CLI prints the status code and its next step for the copy.
const text = formatInstances(s.call("instances"));
assert.ok(text.includes(` researcher (copy): credential-revoked. ${COPY_BINDING_STATUS["credential-revoked"]}\n`), text);
// The sentinel never appears.
const leaks = (x) => [token, sha(token), token.slice(0, 8)].filter((v) => x.includes(v));
assert.deepEqual(leaks(seen.join("\n")), []);
assert.deepEqual(leaks(trail(p.f.dataRoot)), []);
assert.deepEqual(leaks(said.join("")), []);
assert.deepEqual(leaks([...first.logs, ...s.logs].map((l) => (typeof l === "string" ? l : JSON.stringify(l))).join("\n")), []);
});
// #1873 CB5 review: a shipped role that needs gitea and vikunja. Boot denies such a copy whatever it
// binds, so its status must not send the owner to approve a gitea binding.
test("CB5 operator: a copy whose role needs a service a copy cannot bind reads copy-service-unbindable and names its missing services", { skip, timeout: 60000 }, async (t) => {
const p = prepare(t);
const file = join(p.root, "config", "agents.json");
const doc = JSON.parse(readFileSync(file, "utf8"));
doc.agents.rev = { definition: "reviewer", vars: VARS };
writeJson(file, doc, 0o600);
const s = await start(t, p);
const v = s.call("instances");
assert.equal(v.copies.rev.state, "unbound");
assert.deepEqual(v.copies.rev.binding, { status: "copy-service-unbindable", next: COPY_BINDING_STATUS["copy-service-unbindable"] });
// The gitea-only copy beside it still reads credential-unbound.
assert.deepEqual(v.copies.researcher.binding, { status: "credential-unbound", next: COPY_BINDING_STATUS["credential-unbound"] });
const text = formatInstances(v);
assert.ok(text.includes(` rev (copy): copy-service-unbindable (missing gitea, vikunja). ${COPY_BINDING_STATUS["copy-service-unbindable"]}\n`), text);
assert.ok(text.includes(` researcher (copy): credential-unbound (missing gitea). ${COPY_BINDING_STATUS["credential-unbound"]}\n`), text);
assert.equal(COPY_BINDING_STATUS["copy-service-unbindable"].includes("credentials.gitea"), false);
});
+17
View File
@@ -89,6 +89,8 @@ function organizations({ send = true } = {}) {
calls.push({ verb, args, opts });
if (verb === 'autonomy.review.console') return { reviewed: args.decisionClass };
if (args.revision !== state.revision) throw new BusReadError('revision-conflict');
// E2E6 fix 7: the bus refuses policy out of the loop here; the route forwards the code alone.
if (args.autonomy.policy === 'out of the loop') throw new BusReadError('autonomy-precondition');
const { notices, reverseWindowHours, authorityResolver, ...levels } = args.autonomy;
state = { revision: REV(parseInt(state.revision, 16) + 1), levels, autonomy: { notices, reverseWindowHours, ...(authorityResolver ? { authorityResolver } : {}) } };
return { revision: state.revision, transaction: `tx-${parseInt(state.revision, 16)}` };
@@ -259,6 +261,21 @@ test('autonomy reads, saves with the panel\'s revision check and records a revie
assert.equal((await page(c.port, 'GET', '/api/orgs/alpha/autonomy')).body.revision, REV(5));
c.calls.splice(0);
// E2E6 fix 7: an autonomy-precondition the route sends without a reason still names the one check the
// terminal can see from its read and the document it sent (notices off while a class moves away),
// and the general guidance becomes the next line.
r = await mosaic(env, ['org', 'autonomy', 'alpha', 'set', '--revision', REV(5), '--policy', 'out']);
assert.equal(r.status, 3, r.stderr);
assert.match(r.stderr, /^mosaic: autonomy set refused \(autonomy-precondition\): owner notices are off; a class near the loop, or one moving away from it, needs them on \(--notices on\)$/m);
assert.match(r.stderr, /^mosaic: next: this move needs a separate resolver/m);
const { preconditionSeen } = await import('../src/org-owner.mjs');
const shown = { levels: { ...LOOP, merge: 'near the loop' } };
assert.equal(preconditionSeen(shown, { ...LOOP, merge: 'near the loop', notices: true }), undefined);
assert.equal(preconditionSeen(shown, { ...LOOP, notices: false }), undefined, 'moving back toward the loop needs no notices');
assert.match(preconditionSeen(shown, { ...LOOP, merge: 'near the loop', notices: false }), /^owner notices are off/);
assert.match(preconditionSeen({ levels: LOOP }, { ...LOOP, authority: 'out of the loop', notices: false }), /^owner notices are off/);
c.calls.splice(0);
r = await mosaic(env, ['org', 'autonomy', 'alpha', 'review', 'merge']);
assert.equal(r.status, 0, r.stderr);
assert.equal(r.stdout, 'recorded your trail review for merge on alpha\n');
+9
View File
@@ -67,6 +67,15 @@ test('two organization buses are isolated and refusals never select a sibling',
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 });
// E2E6 fix 1: status, start, stop and restart refuse a dead host's lock with the message unchanged and a
// next line naming the verb that clears it.
for (const args of [['status', 'alpha', '--json'], ['start', 'alpha', '--no-launch', '--detach'], ['stop', 'alpha'], ['restart', 'alpha']]) {
const r = spawnSync(join(REPO, 'scripts/mosaic'), ['bus', ...args], { env, encoding: 'utf8', timeout: 30000 });
assert.equal(r.status, 3, `${args[0]}: ${r.stderr}`);
assert.match(r.stderr, /^mosaic: stale bus host lock$/m, args[0]);
assert.match(r.stderr, /^mosaic: next: if the bus host died, clear it with mosaic bus recover alpha; recover refuses while the host still runs$/m, args[0]);
}
await assert.rejects(busStatus(ctx, 'alpha'), e => e.message === 'stale bus host lock' && e.exitCode === 3 && /mosaic bus recover alpha/.test(e.next));
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 });
+57
View File
@@ -417,3 +417,60 @@ test('a detached console without the dev owner has no supervisor, so it refuses
assert.equal(start.stderr, '');
assert.equal((await consoleStop(ctx)).stopped, true);
});
// E2E6 fix 4: console start and stop clear a start.lock whose named supervisor (pid and start time) is
// gone, and never one whose holder still runs or that names no holder.
test('E2E6 a start.lock whose holder is gone is cleared by stop and by start; a live or unnamed one refuses and stays', { timeout: 60000 }, async t => {
const { startTimeOf } = await import('../src/host.mjs');
const { ctx } = await instance(t);
const dir = join(ctx.dataRoot, 'console'), lock = join(dir, 'start.lock');
mkdirSync(dir, { recursive: true, mode: 0o700 });
// A process this test starts and lets end: its pid and start time name a supervisor that is gone.
const child = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 30000)'], { stdio: 'ignore' });
await new Promise(r => child.once('spawn', r));
const deadHolder = { pid: child.pid, startTime: startTimeOf(child.pid) };
assert.match(deadHolder.startTime, /^\d+$/);
child.kill('SIGKILL');
await new Promise(r => child.once('exit', r));
const warnings = [];
writeFileSync(lock, JSON.stringify(deadHolder), { mode: 0o600 });
assert.deepEqual(await consoleStop(ctx, { warn: s => warnings.push(s) }), { stopped: false });
assert.deepEqual(warnings, [`mosaic: cleared a console start lock left by process ${child.pid}, which is gone\n`]);
assert.equal(existsSync(lock), false);
assert.deepEqual(readdirSync(dir).filter(x => x.startsWith('start.lock')), []);
// A live pid with another start time is a reused pid: its holder is gone too.
writeFileSync(lock, JSON.stringify({ pid: process.pid, startTime: '1' }), { mode: 0o600 });
warnings.length = 0;
const result = await consoleStart(ctx, { detach: true, port: 'auto', warn: s => warnings.push(s) });
assert.deepEqual(warnings, [`mosaic: cleared a console start lock left by process ${process.pid}, which is gone\n`]);
assert.equal(existsSync(lock), false, 'the start released its own lock');
assert.equal((await consoleStop(ctx)).stopped, true);
assert.ok(Number.isSafeInteger(result.pid));
// A live holder refuses on start and stop, and the lock stays as it was.
const live = JSON.stringify({ pid: process.pid, startTime: startTimeOf(process.pid) });
writeFileSync(lock, live, { mode: 0o600 });
await assert.rejects(consoleStop(ctx), e => e.message === `console start lock is held by process ${process.pid}, which still runs; wait for its start or stop to finish`);
await assert.rejects(consoleStart(ctx, { detach: true, port: 'auto' }), /which still runs/);
assert.equal(readFileSync(lock, 'utf8'), live);
// A lock that names no holder (empty: a holder between its open and its write; or bad JSON) stays too.
for (const body of ['', '{"pid":', JSON.stringify({ pid: 0, startTime: '1' }), JSON.stringify({ pid: child.pid, startTime: 7 })]) {
writeFileSync(lock, body, { mode: 0o600 });
await assert.rejects(consoleStop(ctx), /console start lock exists and names no holder/);
assert.equal(readFileSync(lock, 'utf8'), body);
}
// f6c review P3-1: a lock its holder marked released (a clear put it back after the release) is
// cleared; a released marker with anything else in it names no holder and stays.
writeFileSync(lock, JSON.stringify({ released: true }), { mode: 0o600 });
warnings.length = 0;
assert.deepEqual(await consoleStop(ctx, { warn: s => warnings.push(s) }), { stopped: false });
assert.deepEqual(warnings, ['mosaic: cleared a console start lock its holder had released\n']);
assert.equal(existsSync(lock), false);
const odd = JSON.stringify({ released: true, pid: 1 });
writeFileSync(lock, odd, { mode: 0o600 });
await assert.rejects(consoleStop(ctx), /console start lock exists and names no holder/);
assert.equal(readFileSync(lock, 'utf8'), odd);
rmSync(lock);
});
@@ -136,3 +136,55 @@ test('T1 mosaic org definitions: writes go through the bus and its secret check,
await assert.rejects(orgCommand(['definitions', 'acme', 'remove', 'copy', '--if-rev', '1'], x), (e) => e.exitCode === 3 && /organization bus/.test(e.message));
assert.deepEqual(readFileSync(file), before);
});
// E2E6 fix 2: an unknown definition, an unknown organization, a bad id and a bad name each end in
// one usage or refusal line: no stack trace and no host path.
test('E2E6 mosaic org definitions: unknown names, an unknown org and a bad id give one line, no stack and no path', async (t) => {
const root = tmp(t);
const f = fixture(root, 'acme');
const { run } = await import('../src/cli.mjs');
const cases = [
[['acme', 'show', 'ghost'], 3, 'mosaic: definition-missing: definitions declares no ghost; list them with mosaic org definitions acme list'],
[['acme', 'diff', 'ghost'], 3, 'mosaic: definition-missing: definitions declares no ghost; list them with mosaic org definitions acme list'],
[['acme', 'show', 'Bad Name'], 3, 'mosaic: definition-missing: definitions declares no definition by that name; list them with mosaic org definitions acme list'],
[['nosuch', 'list'], 3, 'mosaic: unknown-organization: this instance has no organization nosuch; list them with mosaic org list'],
[['nosuch', 'show', 'x'], 3, 'mosaic: unknown-organization: this instance has no organization nosuch; list them with mosaic org list'],
];
for (const [args, exit, line] of cases) {
const x = io(f.env);
assert.equal(await run(['org', 'definitions', ...args], x, {}), exit, args.join(' '));
assert.equal(x.err.text, `${line}\n`, args.join(' '));
assert.ok(!x.err.text.includes(root) && !/\n\s+at /.test(x.err.text), args.join(' '));
}
for (const args of [['Bad/Id', 'list'], ['../x', 'show', 'y']]) {
const x = io(f.env);
assert.equal(await run(['org', 'definitions', ...args], x, {}), 4, args.join(' '));
assert.match(x.err.text, /^mosaic: invalid organization id: an id is 1 to 64 of a-z/);
assert.match(x.err.text, /\nmosaic org definitions templates \| definitions ID list/);
assert.ok(!x.err.text.includes(root) && !/\n\s+at /.test(x.err.text));
}
});
test('E2E6 definitionsFailure: a raw filesystem error is named by its operation and code, a message path is cut, an unknown error says only that it failed', async () => {
const { definitionsFailure } = await import('../src/org-definitions.mjs');
const fsError = (code, syscall) => Object.assign(new Error(`${code}: no such file, open '/home/someone/.config/x/acme.json'`), { code, syscall, path: '/home/someone/.config/x/acme.json' });
let e = definitionsFailure(fsError('EACCES', 'open'), 'acme');
assert.deepEqual([e.code, e.exitCode, e.message], ['organization-unreadable', 3, 'organization-unreadable: cannot open the file of organization acme (EACCES)']);
e = definitionsFailure(Object.assign(new Error("organization file /srv/x/acme.json is not valid"), { code: 'organization-invalid' }), 'acme');
assert.equal(e.message, 'organization-invalid: organization file <path> is not valid');
e = definitionsFailure(new TypeError('Cannot read properties of undefined'), 'acme');
assert.deepEqual([e.code, e.exitCode, e.message], ['definitions-failed', 1, 'definitions-failed: the definitions command failed']);
// f6c review P3-2: only the organization file missing means no organization.
e = definitionsFailure(fsError('ENOENT', 'open'), 'acme');
assert.deepEqual([e.code, e.message], ['unknown-organization', 'unknown-organization: this instance has no organization acme; list them with mosaic org list']);
const rolesGone = Object.assign(new Error("ENOENT: no such file or directory, scandir '/home/someone/roles'"), { code: 'ENOENT', syscall: 'scandir', path: '/home/someone/roles' });
e = definitionsFailure(rolesGone, 'templates');
assert.deepEqual([e.code, e.exitCode, e.message], ['definitions-unreadable', 3, 'definitions-unreadable: cannot scandir a file the definitions command reads (ENOENT)']);
e = definitionsFailure(rolesGone, 'acme');
assert.equal(e.code, 'organization-unreadable');
// f6c review P3-3: a path after ':' or inside backticks is cut.
e = definitionsFailure(Object.assign(new Error('organization file:/srv/x/acme.json is not valid'), { code: 'organization-invalid' }), 'acme');
assert.equal(e.message, 'organization-invalid: organization file:<path> is not valid');
e = definitionsFailure(Object.assign(new Error('bad role at `/srv/roles/x.json`'), { code: 'organization-invalid' }), 'acme');
assert.equal(e.message, 'organization-invalid: bad role at `<path>`');
});
+33
View File
@@ -97,3 +97,36 @@ test('SP4 an agent shell refuses every pack, board and workflow word, pack list
assert.deepEqual(s.calls, []);
assert.deepEqual(readFileSync(s.file), before);
});
// E2E6 fix 3: the real transport shape. humanTransport throws a CliError whose message and .code are
// the bus code alone (packages/cli/src/transport.mjs); pack, board and workflow refusals must still
// gain their hint, as template save's do, and a CliError with its own next line passes unchanged.
test('E2E6 pack, board and workflow refusals from a CliError-shaped transport print the hint', async (t) => {
const { CliError } = await import('../src/errors.mjs');
const { busExit, busNotRunning } = await import('../src/transport.mjs');
const root = tmp(t);
const f = fixture(root, 'acme');
const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: 'acme', env: f.env });
const b = await startBroker({ dataRoot: f.dataRoot, businesses: boot.businesses, organization: boot.organization });
t.after(() => b.close());
const human = b.broker.bindHuman({ business: 'acme', human: f.doc.human, via: 'cli', outsideAgent: true });
let down = false;
const deps = { system: { dataRoot: f.dataRoot }, transport: () => async (verb, args = {}) => {
if (down) throw busNotRunning('acme');
try { return structuredClone(await b.broker.request(human, { verb, args })); }
catch (e) { if (!(e instanceof BusError)) throw e; throw Object.assign(new CliError(e.code, busExit(e.code)), { code: e.code }); }
} };
const run = async (args) => { const x = io(f.env); await main(['org', ...args], x, deps); return JSON.parse(x.out.text); };
const hinted = (e) => typeof e.code === 'string' && e instanceof CliError && e.message.startsWith(`${e.code}: `) && e.message.length > e.code.length + 10;
await assert.rejects(run(['pack', 'preview', 'acme', 'no-such-pack']), (e) => e.code === 'starter-pack-not-found' && e.message === 'starter-pack-not-found: no installed, shipped or saved pack has that name');
const spec = join(root, 'board.json');
writeFileSync(spec, JSON.stringify({ columns: [{ state: 'todo', label: 'Backlog' }], layout: 'kanban' }));
await assert.rejects(run(['board', 'set', 'acme', spec]), (e) => hinted(e) && e.exitCode === busExit(e.code));
await assert.rejects(run(['workflow', 'remove', 'acme', 'ghost']), (e) => hinted(e) && e.exitCode === busExit(e.code));
// A stopped bus keeps its own message and next line.
down = true;
await assert.rejects(run(['pack', 'preview', 'acme', 'no-such-pack']), (e) => e.code === 'bus-not-running' && e.exitCode === 3
&& e.message === 'bus-not-running: the bus for acme is not running, so the request was not sent' && /mosaic bus start acme/.test(e.next) && /mosaic bus recover acme/.test(e.next));
});
+6 -1
View File
@@ -37,8 +37,13 @@ test('organization host claim refuses a stale lock without removing it', t => {
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);
// E2E6 fix 1: the message stays; the next line names the verb that clears the dead host.
assert.throws(() => claimHostLock(root, mine, { refuseStale: true }), e => e.message === 'stale bus host lock' && e.exitCode === 3
&& e.next === 'if the bus host died, clear it with mosaic bus recover alpha; recover refuses while the host still runs');
assert.deepEqual(JSON.parse(readFileSync(hostLock(root), 'utf8')), stale);
// A lock naming no valid organization id still names the verb, with a placeholder.
writeFileSync(hostLock(root), JSON.stringify({ ...stale, business: '../x\nsecret' }), { mode: 0o600 });
assert.throws(() => claimHostLock(root, mine, { refuseStale: true }), e => e.next === 'if the bus host died, clear it with mosaic bus recover <org>; recover refuses while the host still runs');
});
import { busStatus } from '../src/org-bus.mjs';
+78
View File
@@ -0,0 +1,78 @@
// Starter packs SP8: `mosaic org template save|list`. Each word goes through the organization's bus as
// its human; save --preview writes nothing, save --digest writes the previewed JSON under the owner's
// account, and template render keeps its own words.
import test from 'node:test';
import assert from 'node:assert/strict';
import { existsSync, lstatSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { main } from '../src/cli.mjs';
import { ORG_USAGE, TEMPLATE_USAGE } from '../src/org.mjs';
import { bootConfig, loadSystem } from '../src/config.mjs';
import { CliError } from '../src/errors.mjs';
import { busExit } from '../src/transport.mjs';
import { BusError } from '../../bus/src/broker.mjs';
import { startBroker } from '../../bus/src/runtime.mjs';
import { fixture, io, tmp } from './helpers.mjs';
async function setup(t) {
const root = tmp(t);
const f = fixture(root, 'acme');
const file = join(root, 'config/businesses/acme.json');
const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: 'acme', env: f.env });
const b = await startBroker({ dataRoot: f.dataRoot, businesses: boot.businesses, organization: boot.organization });
t.after(() => b.close());
const human = b.broker.bindHuman({ business: 'acme', human: f.doc.human, via: 'cli', outsideAgent: true });
const calls = [];
const deps = { system: { dataRoot: f.dataRoot }, transport: () => async (verb, args = {}) => {
calls.push([verb, Object.keys(args).sort()]);
try { return structuredClone(await b.broker.request(human, { verb, args })); }
// What humanTransport throws for a bus refusal: a CliError named by the code alone.
catch (e) { if (!(e instanceof BusError)) throw e; throw Object.assign(new CliError(e.code, busExit(e.code)), { code: e.code }); }
} };
const run = async (args, env = f.env) => { const x = io(env); await main(['org', ...args], x, deps); return JSON.parse(x.out.text); };
const saved = (name) => join(boot.organization.dir, 'templates', f.doc.human, `${name}.json`);
return { root, f, file, calls, run, saved };
}
test('SP8 mosaic org template save: --preview writes nothing, --digest writes the previewed JSON, a second save refuses, list shows it', async (t) => {
const s = await setup(t);
assert.ok(ORG_USAGE.includes(TEMPLATE_USAGE.slice('mosaic org '.length)));
const before = readFileSync(s.file);
const preview = await s.run(['template', 'save', 'acme', 'acme-shape', '--preview']);
assert.match(preview.digest, /^[0-9a-f]{64}$/);
assert.ok(Array.isArray(preview.freeText) && Array.isArray(preview.mentions) && !('replacements' in preview));
assert.ok(!existsSync(s.saved('acme-shape')), 'preview writes no file');
const out = await s.run(['template', 'save', 'acme', 'acme-shape', '--digest', preview.digest]);
assert.deepEqual(out, { name: 'acme-shape', scope: 'org', digest: preview.digest });
assert.equal(readFileSync(s.saved('acme-shape'), 'utf8'), preview.json);
assert.equal(lstatSync(s.saved('acme-shape')).mode & 0o777, 0o600);
assert.deepEqual(readFileSync(s.file), before, 'save writes nothing to the organization file');
await assert.rejects(s.run(['template', 'save', 'acme', 'acme-shape', '--digest', preview.digest]), (e) => e.code === 'template-exists' && e.exitCode === 2
&& e.message === 'template-exists: a template of this name is already saved for your account; nothing was written'
&& /under another name/.test(e.next) && /mosaic org template list acme/.test(e.next));
await assert.rejects(s.run(['template', 'save', 'acme', 'other', '--digest', preview.digest]), (e) => e.code === 'pack-plan-stale' && /preview again/.test(e.message));
const listed = await s.run(['template', 'list', 'acme']);
assert.deepEqual(listed.templates.map((x) => x.name), ['acme-shape']);
assert.deepEqual(s.calls.map(([verb, keys]) => [verb, keys.join(',')]), [
['template.save', 'name,preview'], ['template.save', 'digest,name'], ['template.save', 'digest,name'], ['template.save', 'digest,name'], ['template.list', ''],
]);
});
test('SP8 template words: a bad shape prints the usage; an agent shell refuses before the bus is called; template render keeps its words', async (t) => {
const s = await setup(t);
for (const args of [['template', 'save', 'acme', 'x'], ['template', 'save', 'acme', 'x', '--preview', '--digest', 'a'.repeat(64)], ['template', 'save', 'acme'], ['template', 'list'], ['template', 'list', 'acme', '--preview']]) {
await assert.rejects(s.run(args), (e) => e.exitCode === 4 && e.message === TEMPLATE_USAGE, args.join(' '));
}
await assert.rejects(s.run(['template', 'save', 'acme', 'x', '--force']), (e) => e.exitCode === 4 && /unknown option --force/.test(e.message));
await assert.rejects(s.run(['template', 'save', 'acme', '..', '--preview']), (e) => e.code === 'invalid-request');
const agent = { ...s.f.env, MOSAIC_RUN_ID: 'run-1' };
const calls = s.calls.length;
for (const args of [['template', 'save', 'acme', 'x', '--preview'], ['template', 'save', 'acme', 'x', '--digest', 'a'.repeat(64)], ['template', 'list', 'acme']]) {
await assert.rejects(s.run(args, agent), (e) => e.exitCode === 3 && /human shell/.test(e.message), args.join(' '));
}
assert.equal(s.calls.length, calls);
// template render is still routed to its own words (it needs --output).
await assert.rejects(s.run(['template', 'render', 'acme']), (e) => e.exitCode === 4 && /--output/.test(e.message));
});
-1
View File
@@ -12,7 +12,6 @@ test('organization CLI renders, validates, creates, updates and initializes git'
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);
assert.deepEqual(JSON.parse(invoke('org', 'system-types', 'enabled', 'alpha')), { organization: 'alpha', enabled: [] });
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));
+4 -4
View File
@@ -306,7 +306,7 @@ test('#811 migration readers follow the engine order and refuse malformed pairs'
assert.notEqual(engineSource,source);
const engine=await import(`data:text/javascript;base64,${Buffer.from(engineSource).toString('base64')}`);
const pairs=engine.migrations.map(({from,to})=>[from,to]);
assert.equal(pairs.length,20);assert.deepEqual(pairs.at(-1),['3u','3v']);
assert.equal(pairs.length,21);assert.deepEqual(pairs.at(-1),['3v','3w']);
const release=readFileSync(join(ROOT,'scripts/stack-release.sh'),'utf8');
const fn=(name)=>{const m=release.match(new RegExp(`^${name}\\(\\) \{[\\s\\S]*?^\}`, 'm'));assert.ok(m,name);return m[0]};
const functions=['copy_schema','target_schema','plan_steps'].map(fn).join('\n');
@@ -885,7 +885,7 @@ const COUNTED=/^COUNTED="([^"]*)"$/m.exec(srText)[1].split(' ');
const STORE_JS=/^STORE_JS='([\s\S]*?)'$/m.exec(srText)[1];
// sage-1163's list 2, written out so that a table dropped from the script's COUNTED fails T45 as
// well as T46.
const LISTED=['role_claims','thread_claims','decision_events','messages','deliveries','task_snapshots','projects','project_archives','threads','sessions','session_defaults','thread_opens','thread_shares','instance_names','usage_records','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_system_types'];
const LISTED=['role_claims','thread_claims','decision_events','messages','deliveries','task_snapshots','projects','project_archives','threads','sessions','session_defaults','thread_opens','thread_shares','instance_names','usage_records','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events'];
const LATE='2999-01-01T00:00:00.000Z';
const addRow=(file,table,at=LATE)=>{const db=new DatabaseSync(file);db.exec(`CREATE TABLE IF NOT EXISTS ${table} (seq INTEGER PRIMARY KEY, at TEXT NOT NULL)`);db.prepare(`INSERT INTO ${table} (at) VALUES (?)`).run(at);db.close()};
@@ -903,7 +903,7 @@ test('T45 row 181 items 2 and 3: the restore guard counts events and every COUNT
both(store('3f.sqlite',['events'],['events','instance_names']),'1\t1 event(s)\n');
both(store('none.sqlite',[],['instance_names']),'0\t0 event(s)\n');
});
for(const x of ['instance_names','usage_records','project_system_types'])await t.test(`one ${x} row and no event refuses before any stop; --events 1 restores`,async(t)=>{
for(const x of ['instance_names','usage_records','request_project_events'])await t.test(`one ${x} row and no event refuses before any stop; --events 1 restores`,async(t)=>{
const e=await mk(t);await seed(e);
assert.equal((await run(e,'activate',C,'acme','--migrate')).code,0);const act=pointer(e).activation;
addRow(e.store,x);
@@ -925,7 +925,7 @@ test('T45 row 181 items 2 and 3: the restore guard counts events and every COUNT
assert.equal(r.code,3,r.out+r.err);assert.ok(r.err.endsWith(`stack-release: refusing restore-mismatch: cannot count the rows after ${stamp}\n`),r.err);
assert.deepEqual(fp(e),before);assert.deepEqual(startsStops(e,n),[]);assert.equal(logLines(e).at(-1).code,'restore-mismatch');
});
for(const x of ['thread_opens','project_system_types'])await t.test(`the in-run recount counts the listed tables too (${x})`,async(t)=>{
for(const x of ['thread_opens','project_archives'])await t.test(`the in-run recount counts the listed tables too (${x})`,async(t)=>{
const e=await mk(t);await seed(e);
assert.equal((await run(e,'activate',C,'acme','--migrate')).code,0);const act=pointer(e).activation;
fault(e,'stop-writes-row',x);
-53
View File
@@ -1,53 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { orgCommand } from '../src/org.mjs';
import { projectBusCommand } from '../src/project.mjs';
test('org installed system types and project selection reads print JSON', async () => {
let output = '';
const io = { stdout: { write: s => { output += s; } }, env: {} };
await orgCommand(['system-types', 'installed'], io);
assert.deepEqual(JSON.parse(output).map(x => x.id), ['business-management', 'coding', 'creative-writing', 'marketing', 'montage']);
output = '';
const calls = [];
await projectBusCommand(['system-types', 'work', '--business', 'demo', '--json'], io, () => async (verb, args) => {
calls.push([verb, args]);
return verb === 'project.list' ? [{ id: 'project-id', name: 'work' }] : { project: 'project-id', selected: ['coding'], active: ['coding'], inactive: [] };
});
assert.deepEqual(calls.map(x => x[0]), ['project.list', 'project.system-types']);
assert.deepEqual(JSON.parse(output).active, ['coding']);
});
// sys1-review P2: both commands go through run() and the real CLI channel module, so a verb the
// channel does not admit fails here. A disabled channel and an agent run still refuse.
test('project system-types and system-types-set pass the CLI channel', async t => {
const { run } = await import('../src/cli.mjs');
const { fixture, humanEnv, io: mkio, tmp } = await import('./helpers.mjs');
const root = tmp(t), f = fixture(root);
const P = '10000000-0000-4000-8000-000000000001';
const transportFor = channels => {
const calls = [];
const transport = () => async (verb, args) => {
if (verb === 'plugins') return { business: 'acme', revision: 'a'.repeat(64), plugins: { channel: channels } };
calls.push([verb, args]);
if (verb === 'project.list') return [{ id: P, at: '2026-11-09T00:00:00.000Z', name: 'work', repo: null }];
if (verb === 'project.system-types') return { project: P, selected: ['coding'], active: ['coding'], inactive: [] };
if (verb === 'project.system-types.set') return { project: P, selected: args.systemTypes, active: args.systemTypes, inactive: [] };
throw Object.assign(new Error(verb), { code: 'bus-refused' });
};
return { calls, transport };
};
const read = transportFor(['cli']), rx = mkio(humanEnv(f.env));
assert.equal(await run(['project', 'system-types', 'work', '--business', 'acme'], rx, { transport: read.transport }), 0, rx.err?.text);
assert.deepEqual(read.calls, [['project.list', { includeArchived: true }], ['project.system-types', { project: P }]]);
assert.deepEqual(JSON.parse(rx.out.text).active, ['coding']);
const write = transportFor(['cli']), wx = mkio(humanEnv(f.env));
assert.equal(await run(['project', 'system-types-set', 'work', '--types', 'coding,marketing', '--business', 'acme'], wx, { transport: write.transport }), 0, wx.err?.text);
assert.deepEqual(write.calls.at(-1), ['project.system-types.set', { project: P, systemTypes: ['coding', 'marketing'] }]);
const off = transportFor(['webui']), ox = mkio(humanEnv(f.env));
assert.equal(await run(['project', 'system-types', 'work', '--business', 'acme'], ox, { transport: off.transport }), 3);
assert.deepEqual(off.calls, []);
const agent = transportFor(['cli']);
assert.notEqual(await run(['project', 'system-types-set', 'work', '--types', 'coding', '--business', 'acme'], mkio({ ...humanEnv(f.env), CODEX_THREAD_ID: 'fixture-agent' }), { transport: agent.transport }), 0);
assert.deepEqual(agent.calls, []);
});
+29 -2
View File
@@ -1,8 +1,8 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { busExit, humanTransport, refuseInsideAgent } from "../src/transport.mjs";
import { busExit, humanTransport, refuseInsideAgent, HUMAN_CLI } from "../src/transport.mjs";
import { fakeCli, humanEnv, tmp } from "./helpers.mjs";
// The fake records argv and stdin, then answers per verb. The real
@@ -15,6 +15,8 @@ const { verb } = JSON.parse(input);
if (verb === "inbox") process.stdout.write(JSON.stringify([{ id: "d1" }]) + "\\n");
else if (verb === "garbage") process.stdout.write("not json");
else if (verb === "hang") setTimeout(() => {}, 60000);
else if (verb === "not-sent") { process.stderr.write("outcome-unknown\\n"); process.exit(5); }
else if (verb === "not-sent-other") { process.stderr.write("human-required\\n"); process.exit(5); }
else { process.stderr.write("some noise\\n" + verb + "\\n"); process.exit(2); }
`;
@@ -50,3 +52,28 @@ test("busExit and refuseInsideAgent", () => {
assert.doesNotThrow(() => refuseInsideAgent(humanEnv()));
assert.throws(() => refuseInsideAgent({ PI_AGENT_DIR: "/x" }), (e) => e.exitCode === 3 && /PI_AGENT_DIR/.test(e.message));
});
// E2E6 fix 6: the shim exits 5 when its connect failed before the request was written. That is a stopped
// bus: mosaic says so with the start and recover line. outcome-unknown stays for a request that left.
test("E2E6 exit 5 with outcome-unknown is bus-not-running; exit 2 with outcome-unknown is unchanged", async (t) => {
const s = setup(t);
await assert.rejects(s.call("not-sent"), (e) => e.code === "bus-not-running" && e.exitCode === 3
&& e.message === "bus-not-running: the bus for acme is not running, so the request was not sent"
&& e.next === "start it with mosaic bus start acme, or clear a host that died with mosaic bus recover acme");
await assert.rejects(s.call("outcome-unknown"), (e) => e.code === "outcome-unknown" && e.exitCode === 1 && e.message === "outcome-unknown" && e.next === undefined);
// Exit 5 says only that nothing was sent; any other code it carries keeps its own meaning.
await assert.rejects(s.call("not-sent-other"), (e) => e.code === "human-required" && e.exitCode === 3);
assert.equal(busExit("bus-not-running"), 3);
});
// The real shim, with no bus behind the path: nothing reaches a broker, so no proof runs. A missing socket
// and a path that is not a listening socket are both a bus that is not running.
test("E2E6 the real human shim against a missing socket or a plain file says the bus is not running", async (t) => {
const root = tmp(t);
const plain = join(root, "plain.sock");
writeFileSync(plain, "");
for (const socket of [join(root, "missing.sock"), plain]) {
const call = humanTransport({ socket, business: "acme", env: humanEnv(), cli: HUMAN_CLI, timeoutMs: 20000 });
await assert.rejects(call("inbox"), (e) => e.code === "bus-not-running" && e.exitCode === 3, socket);
}
});
-5
View File
@@ -615,11 +615,6 @@ 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.
The optional `plugins.system-type` list enables installed system-type modules for
an organization. Missing means none. Unknown ids refuse validation. Changes
are owner-only; project selections are separate bus settings and a selection
whose type is disabled remains recorded as inactive.
## Instance accounts (Phase M2)
`src/accounts.mjs` validates `<configDir>/accounts.json` without a dependency or
@@ -0,0 +1,32 @@
// #1873 CB5: an agent copy's credential binding status in its owner's views (the bus instances read,
// the Console agents table and `mosaic org instances`), with the one next step each status names.
// The texts are design docs/plans/2026-11-11_copy-credential-binding-design.md section 6's, and the
// three steps a binding takes before it is usable (approved, issued, verified at a bus start). Every
// string is fixed: none interpolates a name, a path, a URL, a service answer or anything from a token.
// A code this table does not know is credential-unavailable, never echoed.
export const COPY_BINDING_STATUS = Object.freeze({
ready: "credentials.gitea is bound and was verified at the last bus start.",
"issue-required": "The binding is approved; the owner must issue the copy's token with mosaic credentials issue, then restart the organization bus.",
"restart-required": "Restart the organization bus so it verifies the binding before the copy launches.",
"credential-unbound": "credentials.gitea needs an owner-approved binding.",
// Boot denies a copy whose role needs a service a copy cannot bind yet, whatever it binds.
"copy-service-unbindable": "this copy's role needs a credential service a copy cannot bind yet, so it cannot launch in this organization; binding gitea alone does not change that.",
"credential-revoking": "credentials.gitea revocation is in progress; read the copy's status again when it finishes.",
"credential-revoke-failed": "credentials.gitea revocation is incomplete; the owner must retry cleanup.",
"credential-revoked": "credentials.gitea was revoked; the owner must approve a new binding.",
"credential-unavailable": "credentials.gitea is unavailable; the owner must verify the slot.",
"credential-expired": "credentials.gitea has expired; the owner must approve a replacement.",
"credential-role-changed": "credentials.gitea no longer matches the approved definition; the owner must review the binding.",
"credential-scope-exceeded": "credentials.gitea.scopes exceeds the approved role.",
"credential-scope-unverified": "credentials.gitea permissions could not be verified.",
"copy-unavailable": "copy is no longer active in this organization.",
"copy-refused": "copy identity is ambiguous; the owner must resolve the definition collision.",
"org-archived": "organization is archived.",
"org-unavailable": "organization is not active.",
"organization-unreadable": "the organization file could not be read; the owner must check it before the copy launches.",
});
export function copyBindingStatus(code) {
const status = typeof code === "string" && Object.hasOwn(COPY_BINDING_STATUS, code) ? code : "credential-unavailable";
return Object.freeze({ status, next: COPY_BINDING_STATUS[status] });
}
+4 -1
View File
@@ -87,7 +87,6 @@ export const VERB_RIGHTS = record({
'request.draft.console': 'request.write',
'prd.revoke.console': 'request.write',
'plugins.set': 'org.setup',
'system-types.console': 'org.setup',
'project.remote.get.console': 'org.setup',
'project.remote.set.console': 'org.setup',
// #1873 SP4: starter packs, the board and workflows. The list is a board read; every write is
@@ -97,6 +96,10 @@ export const VERB_RIGHTS = record({
'pack.apply.console': null,
'board.set.console': null,
'workflow.set.console': null,
// #1873 SP8: saving a template, and listing the saved ones, are the owner's alone: a template is a
// copy of the organization's roles, and the files live under the owner's account.
'template.save.console': null,
'template.list.console': null,
});
function refuse(code) {
@@ -14,8 +14,6 @@ import { loadRole } from "./role.mjs";
import { checkDefinition, checkDefinitions, checkDefinitionName, orgDefinitionName, ORG_PREFIX } from "./definitions.mjs";
import { END_PROTECTED, parseRepoRemote } from "./vocabulary.mjs";
import { checkPlugins, checkUsageSettings, NAME as KIND_NAME } from "../../plugins/src/kinds.mjs";
import { hasModule } from "../../plugins/src/loader.mjs";
import { readPluginLock } from "../../plugins/src/pinned.mjs";
import { checkMapping } from "../../library/src/mapping.mjs";
import { forkProblem, giteaName, parseGiteaUrl, HOST_SERVICE, CREATIVE_DEFINITIONS } from "./vocabulary.mjs";
import { checkAppearance } from "./appearance.mjs";
@@ -505,14 +503,6 @@ export function validateOrganizationDocument(document, file, { rolesDir, dataRoo
// here; whether each module loads is the loader's check, at load time.
const plugins = document.plugins === undefined ? {}
: structuredClone(checkPlugins(document.plugins, `${file} plugins`, { fail: (_code, message) => refuse(message) }));
let installedNames = new Set();
if (dataRoot !== null && plugins["system-type"]?.some(id=>!hasModule("system-type",id))) {
try { installedNames = new Set(Object.keys(readPluginLock(dataRoot).plugins)); }
catch { installedNames = null; } // the loader reports an unreadable lock; core still loads
}
for (const id of plugins["system-type"] ?? []) {
if (!hasModule("system-type", id) && installedNames !== null && !installedNames.has(id)) refuse(`${file} plugins.system-type names unknown module ${id}`);
}
const usage = document.usage === undefined ? undefined : checkUsageSettings(document.usage);
const autonomy = checkAutonomy(document.autonomy);
if(autonomy?.authorityResolver) {
@@ -126,13 +126,6 @@ function autonomyFence(old, document, actor) {
if(actor!==old.human || actor==='tester' || agents.includes(actor))fail('human-required');
}
function systemTypesFence(old, document, actor) {
if (canonicalJson(old.plugins?.['system-type'] ?? []) === canonicalJson(document?.plugins?.['system-type'] ?? [])) return;
const agents = Object.entries(old.roles ?? {}).flatMap(([name, role]) => [name, `${role?.definition}.${name}`]);
if (actor === old.human && actor !== 'tester' && !agents.includes(actor)) return;
fail('organization-system-types-human-only');
}
// C2-06 M11: replyLimits is the human's alone. Any setter that changes it refuses unless the actor is
// the business's human as the file stood before the write, and no agent or the tester under that
// name. Compared as M9 compares projects, on the document and again on what lands on disk.
@@ -320,7 +313,6 @@ export function organizationWriter({ dir, rolesDir, dataRoot = null, barrier = (
replyLimitsFence(old, document, actor);
modelQuotaFence(old, document, actor);
usageFence(old, document, actor);
systemTypesFence(old, document, actor);
autonomyFence(old, document, actor);
validateBusinessDocument(document, file, { rolesDir, dataRoot });
const bytes = JSON.stringify(document, null, 2) + "\n";
@@ -332,7 +324,6 @@ export function organizationWriter({ dir, rolesDir, dataRoot = null, barrier = (
replyLimitsFence(old, written, actor);
modelQuotaFence(old, written, actor);
usageFence(old, written, actor);
systemTypesFence(old, written, actor);
autonomyFence(old, written, actor);
validateBusinessDocument(written, file, { rolesDir, dataRoot });
if (beforeWrite) {
+500
View File
@@ -0,0 +1,500 @@
// Starter packs SP8 (#1873): saving an organization, or one of its projects, as a template. Design:
// starter-packs-design section 5 and SP8 in section 8.
//
// A saved template is a pack document (packVersion 1) written to
// <configDir>/templates/<account>/<name>.json and read later with origin `saved`
// (plugins/src/starter-pack.mjs readSavedTemplate). Save copies what section 5 captures and nothing
// else: definitions (role, instructions, personality, from), one hire proposal per agent, the board,
// the workflows, the union of the captured roles' skills and {id, version} for each pack applied
// before. Everything else stays in the organization: members, identities, credential material
// (instance credentials, copyBindings, copyId, orgIncarnation, generation), launch, plugins, vars,
// project roots and git state, definition revs and hire decisions, and everything on the bus.
//
// Free text is kept verbatim: save never rewrites prose (E2E6 fix 5, Lead ruling). A placeholder
// could stand only where a field's whole value is an organization identifier, and a pack carries no
// such field (hire names are <role>-<n>, step roles name definitions or templates), so save writes
// none and apply has none to resolve. The preview lists every kept free-text field by path, each
// one that mentions the organization id, a project id, an agent name, the owner's account or a
// project root (so the owner can edit it before saving), and the exact JSON save writes; save takes
// that JSON's digest. Every string and key is passed to the credential store's
// assertClean; a hit refuses template-secret-suspected naming the path, never the text.
//
// A refusal is a TemplateError: a bus code and a reason that names a path or a checked name only,
// never a value from the document and never a host path.
import { closeSync, constants, fchmodSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, unlinkSync, writeSync } from "node:fs";
import { join } from "node:path";
import { ORG_PREFIX, checkDefinition, readTemplate } from "./definitions.mjs";
import { sha256 } from "./util.mjs";
import { ID_PATTERN } from "./vocabulary.mjs";
import { NAME } from "../../plugins/src/kinds.mjs";
import {
PACK_BOUNDS, PACK_VERSION, SAVED_MAX_BYTES, checkPack, narrowsTemplate, readSavedTemplate,
} from "../../plugins/src/starter-pack.mjs";
// A template name is a pack id (NAME), at most 64 characters, the length of every other pack name.
export const TEMPLATE_NAME_MAX = 64;
// list reads at most this many files; a longer directory refuses rather than truncating silently.
export const TEMPLATE_LIST_MAX = 256;
const CHIEF = "chief-of-staff";
const ASSISTANT = "assistant";
const HIRE_DEFINITION = /^hire-/;
// The board a template carries when the organization (or project) has none: the five task states in
// store order, with plain labels, as a kanban. A pack must carry a board (SP1 checkPack).
export const DEFAULT_BOARD = Object.freeze({
columns: Object.freeze([
Object.freeze({ state: "todo", label: "To do" }),
Object.freeze({ state: "in-progress", label: "In progress" }),
Object.freeze({ state: "in-review", label: "In review" }),
Object.freeze({ state: "blocked", label: "Blocked" }),
Object.freeze({ state: "done", label: "Done" }),
]),
layout: "kanban",
});
const HIRE_REASON = "The organization this template was saved from ran this agent.";
// A word boundary for a mention: an identifier character on either side means the literal is part of
// a longer name, which is not a mention.
const WORD = "A-Za-z0-9_-";
export class TemplateError extends Error {
constructor(code, reason) {
super(reason ? `${code}: ${reason}` : code);
this.name = "TemplateError";
this.code = code;
this.reason = reason;
}
}
const refuse = (code, reason) => {
throw new TemplateError(code, reason);
};
const plain = (value) => value !== null && typeof value === "object" && !Array.isArray(value)
&& [Object.prototype, null].includes(Object.getPrototypeOf(value));
const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
// The account (the caller's s.human) and the name are checked before any path is built from them.
export function checkAccount(account) {
if (typeof account !== "string" || !ID_PATTERN.test(account)) refuse("human-required", "the saving account is not a valid account name");
return account;
}
export function checkTemplateName(name) {
if (typeof name !== "string" || name.length > TEMPLATE_NAME_MAX || !NAME.test(name)) {
refuse("invalid-request", `a template name must match ${NAME}, at most ${TEMPLATE_NAME_MAX} characters`);
}
return name;
}
// One template, read now, or null when the name is no usable version 2 template.
function templateOrNull(rolesDir, name, cache) {
if (!cache.has(name)) {
let value = null;
if (typeof name === "string" && ID_PATTERN.test(name)) {
try {
value = readTemplate(rolesDir, name);
} catch (error) {
if (error?.name !== "BusinessError") throw error;
}
}
cache.set(name, value);
}
return cache.get(name);
}
// ---- Mentions ----
// The organization-specific names the preview looks for in kept text, longest first so a root path
// is found before the ids inside it. A name that is also a template or a definition name is a role's
// name, not a person's or a client's, and is not a mention: role text says "coder" on purpose. A
// mention is labelled by kind and name; a project root is labelled by kind alone, so the preview
// names no host path beyond the text it already shows.
function orgNames(document, { rolesDir, cache }) {
const out = [];
const add = (text, label) => {
if (typeof text === "string" && text.length > 0 && !out.some((l) => l.text === text)) out.push({ text, label });
};
for (const project of Object.values(document.projects ?? {})) {
add(project?.root, "project root");
add(project?.repo, "project root");
}
if (typeof document.id === "string") add(document.id, `organization ${document.id}`);
for (const id of Object.keys(document.projects ?? {})) add(id, `project ${id}`);
if (typeof document.human === "string") add(document.human, `owner ${document.human}`);
const role = (name) => Object.hasOwn(document.definitions ?? {}, name) || templateOrNull(rolesDir, name, cache) !== null;
const agents = new Set([...Object.keys(document.roles ?? {}), ...(Array.isArray(document.retired) ? document.retired : [])]);
for (const project of Object.values(document.projects ?? {})) for (const name of Object.keys(project?.agents ?? {})) agents.add(name);
for (const name of agents) if (!role(name)) add(name, `agent ${name}`);
out.sort((a, b) => b.text.length - a.text.length);
return out;
}
// The labels of every name the text mentions, in first-mention order, each once. One pass with one
// alternation, longest name first, so a root path is one mention and not the ids inside it.
const combined = new WeakMap();
function mentions(text, list) {
if (list.length === 0) return [];
let entry = combined.get(list);
if (!entry) {
const by = new Map(list.map((l) => [l.text, l.label]));
entry = { by, pattern: new RegExp(`(?<![${WORD}])(?:${list.map((l) => escape(l.text)).join("|")})(?![${WORD}])`, "g") };
combined.set(list, entry);
}
const found = [];
for (const match of text.matchAll(entry.pattern)) {
const label = entry.by.get(match[0]);
if (!found.includes(label)) found.push(label);
}
return found;
}
// ---- Capture ----
// Builds the template document. Free text is copied verbatim; each kept free-text field, and each
// one that mentions an organization-specific name, is recorded by path.
function capture(document, { name, project, rolesDir, list, record }) {
const cache = new Map();
const text = (value, path) => {
record.freeText.push({ path, text: value });
const names = mentions(value, list);
if (names.length > 0) record.mentions.push({ path, names });
return value;
};
const orgDefinitions = plain(document.definitions) ? document.definitions : {};
const scope = project === undefined ? "org" : "project";
let source;
if (scope === "project") {
if (!plain(document.projects) || !Object.hasOwn(document.projects, project)) refuse("invalid-request", "the project is not declared in this organization");
source = document.projects[project];
if (source?.kind === "library") refuse("invalid-request", "a library project has no agents, board or workflows to save");
} else {
source = document;
}
// The agents: one hire per org instance, or per project agent that defines one. Never the chief
// or the assistant, which every organization already has.
const agentEntries = scope === "org"
? Object.entries(plain(document.roles) ? document.roles : {})
: Object.entries(plain(source.agents) ? source.agents : {}).filter(([, e]) => typeof e?.definition === "string");
agentEntries.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
const agents = [];
for (const [, entry] of agentEntries) {
const definition = entry?.definition;
if (typeof definition !== "string") continue;
const org = definition.startsWith(ORG_PREFIX) ? definition.slice(ORG_PREFIX.length) : null;
const base = org === null ? definition : orgDefinitions[org]?.from?.template;
if (base === CHIEF || base === ASSISTANT) continue;
if (org !== null && !Object.hasOwn(orgDefinitions, org)) refuse("invalid-request", "an agent names an organization definition the file does not hold");
agents.push({ org, template: org === null ? definition : null });
}
// Which org definitions are copied: every one for an org template; for a project template the
// ones its agents and workflows name.
const sourceWorkflows = plain(source.workflows) ? source.workflows : {};
let copied;
if (scope === "org") {
copied = Object.keys(orgDefinitions);
} else {
const named = new Set(agents.filter((a) => a.org !== null).map((a) => a.org));
for (const workflow of Object.values(sourceWorkflows)) {
for (const step of Array.isArray(workflow?.steps) ? workflow.steps : []) {
for (const role of Array.isArray(step?.roles) ? step.roles : []) if (Object.hasOwn(orgDefinitions, role)) named.add(role);
}
}
copied = Object.keys(orgDefinitions).filter((n) => named.has(n));
}
// Names. A definition named hire-<decision> becomes <from>-<n>, so no decision id leaves the
// organization; every other copied definition keeps its name.
const taken = new Set(copied.filter((n) => !HIRE_DEFINITION.test(n)));
const free = (base) => {
if (!taken.has(base)) { taken.add(base); return base; }
for (let n = 1; ; n++) if (!taken.has(`${base}-${n}`)) { taken.add(`${base}-${n}`); return `${base}-${n}`; }
};
const numbered = (base) => {
for (let n = 1; ; n++) if (!taken.has(`${base}-${n}`)) { taken.add(`${base}-${n}`); return `${base}-${n}`; }
};
const renamed = new Map();
for (const n of copied) {
if (!HIRE_DEFINITION.test(n)) { renamed.set(n, n); continue; }
renamed.set(n, numbered(orgDefinitions[n]?.from?.template ?? "role"));
}
// Definitions: role, instructions, personality and from. No rev.
const definitions = {};
const skills = [];
const addSkills = (list) => { for (const s of Array.isArray(list) ? list : []) if (!skills.includes(s)) skills.push(s); };
for (const n of copied) {
const entry = orgDefinitions[n];
const key = renamed.get(n);
const at = `definitions.${key}`;
if (!plain(entry) || !plain(entry.role)) refuse("invalid-request", `${at} is not a definition entry`);
const out = {};
if (entry.from !== undefined) {
const template = templateOrNull(rolesDir, entry.from?.template, cache);
if (!template) refuse("starter-pack-invalid", `${at}.from names a template that is not a version 2 role`);
// A template that changed since the definition copied it: the saved from names its current
// hash, and the role must still narrow the current template. The preview lists the re-pin.
if (template.sha256 !== entry.from.sha256) record.repinned.push(`${at}.from`);
out.from = { template: template.name, sha256: template.sha256 };
let composition;
try {
({ composition } = checkDefinition(key, { ...entry, rev: 1, from: out.from }, { where: "definitions" }));
} catch (error) {
if (error?.name === "BusinessError") refuse("starter-pack-invalid", `${at} fails the composition check`);
throw error;
}
if (!narrowsTemplate(composition, template.role)) refuse("starter-pack-invalid", `${at}.role is wider than template ${template.name}`);
}
out.role = structuredClone(entry.role);
if (typeof entry.instructions === "string") out.instructions = text(entry.instructions, `${at}.instructions`);
if (entry.personality !== undefined) {
const p = structuredClone(entry.personality);
if (plain(p)) {
if (typeof p.summary === "string") p.summary = text(p.summary, `${at}.personality.summary`);
if (typeof p.voice === "string") p.voice = text(p.voice, `${at}.personality.voice`);
if (Array.isArray(p.traits)) p.traits = p.traits.map((t, i) => (typeof t === "string" ? text(t, `${at}.personality.traits[${i}]`) : t));
}
out.personality = p;
}
definitions[key] = out;
addSkills(entry.role.skills);
}
// Hires: <from>-<n> and the definition. A template agent hires a template reference; an org
// definition agent hires the copied definition.
const refs = new Map();
const hireNames = new Set();
const hires = [];
for (const agent of agents) {
let definition;
let base;
if (agent.template !== null) {
const template = templateOrNull(rolesDir, agent.template, cache);
if (!template) refuse("starter-pack-invalid", `an agent uses template ${agent.template}, which is not a version 2 role`);
if (!refs.has(agent.template)) {
const key = free(agent.template);
definitions[key] = { template: template.name, sha256: template.sha256 };
refs.set(agent.template, key);
addSkills(template.role.skills);
}
definition = refs.get(agent.template);
base = agent.template;
} else {
definition = renamed.get(agent.org);
base = orgDefinitions[agent.org]?.from?.template ?? definition;
}
let hireName;
for (let n = 1; ; n++) if (!hireNames.has(`${base}-${n}`)) { hireName = `${base}-${n}`; break; }
hireNames.add(hireName);
hires.push({ name: hireName, definition, reason: HIRE_REASON });
}
if (hires.length > PACK_BOUNDS.hires) refuse("starter-pack-invalid", `a template holds at most ${PACK_BOUNDS.hires} hires; this one would hold ${hires.length}`);
// The board, or the default when there is none.
const sourceBoard = plain(source.board) ? source.board : DEFAULT_BOARD;
const board = {
columns: sourceBoard.columns.map((c, i) => ({ state: c.state, label: text(c.label, `board.columns[${i}].label`) })),
layout: sourceBoard.layout,
};
// Workflows. A pack step carries an id; the organization's steps have none, so save numbers them.
// A pack workflow has no title: a workflow without a description takes its title as one.
const workflows = {};
for (const [wname, workflow] of Object.entries(sourceWorkflows)) {
const at = `workflows.${wname}`;
const description = typeof workflow?.description === "string" ? text(workflow.description, `${at}.description`)
: typeof workflow?.title === "string" ? text(workflow.title, `${at}.description`)
: `The ${wname} workflow.`;
const steps = (Array.isArray(workflow?.steps) ? workflow.steps : []).map((step, i) => ({
id: `step-${i + 1}`,
title: text(step.title, `${at}.steps[${i}].title`),
roles: (Array.isArray(step.roles) ? step.roles : []).map((r) => (renamed.has(r) ? renamed.get(r) : r)),
state: step.state,
}));
workflows[wname] = { description, steps };
}
// packs: {id, version} for each pack applied before at this scope, once each, the latest 32.
const seen = new Set();
const packs = [];
for (const entry of Array.isArray(document.packs) ? document.packs : []) {
if (scope === "org" ? entry?.scope !== "org" : entry?.project !== project) continue;
const k = `${entry.id}\n${entry.version}`;
if (seen.has(k)) continue;
seen.add(k);
packs.push({ id: entry.id, version: entry.version });
}
record.packsDropped = Math.max(0, packs.length - PACK_BOUNDS.packs);
const keptPacks = packs.slice(Math.max(0, packs.length - PACK_BOUNDS.packs));
const label = name.slice(0, PACK_BOUNDS.label);
// No placeholder: apply copies the description as it stands, so a "{org}" here would stay literal.
const descriptionText = scope === "org" ? "Saved from an organization." : "Saved from one project of an organization.";
return {
packVersion: PACK_VERSION, id: name, version: "1.0.0", label, description: descriptionText, scope,
definitions, hires, board, workflows, skills, packs: keptPacks,
};
}
// Every string and every key, by path, through the credential store's check. A hit refuses
// template-secret-suspected and names the path; the text never leaves.
function scan(value, path, assertClean) {
const check = (v, where) => {
try {
assertClean(v);
} catch (error) {
if (error?.code === "credential-leak") refuse("template-secret-suspected", where);
throw error;
}
};
if (typeof value === "string") return check(value, path);
if (Array.isArray(value)) return value.forEach((v, i) => scan(v, `${path}[${i}]`, assertClean));
if (value !== null && typeof value === "object") {
for (const [k, v] of Object.entries(value)) {
const where = path ? `${path}.${k}` : k;
check(k, `${path || "template"}[key]`);
scan(v, where, assertClean);
}
}
}
// The preview: the exact JSON save writes, its digest, each kept free-text field by path, each such
// field that mentions an organization-specific name, with the names it mentions, and each re-pin.
// Refuses before returning when any string or key holds a held credential value.
export function templatePreview({ document, name, project, rolesDir, assertClean }) {
checkTemplateName(name);
if (project !== undefined && (typeof project !== "string" || !ID_PATTERN.test(project))) refuse("invalid-request", "the project id is invalid");
if (typeof assertClean !== "function") throw new TypeError("templatePreview needs the credential store's assertClean");
if (!plain(document)) refuse("invalid-request", "the organization file is not readable");
const record = { freeText: [], mentions: [], repinned: [], packsDropped: 0 };
const template = capture(document, { name, project, rolesDir, list: orgNames(document, { rolesDir, cache: new Map() }), record });
scan(template, "", assertClean);
let checked;
try {
checked = checkPack(template, { origin: "saved", rolesDir });
} catch (error) {
if (error?.name === "PluginError") {
refuse(/^starter-pack-[a-z-]+$/.test(error.code ?? "") ? error.code : "starter-pack-invalid", typeof error.message === "string" ? error.message.slice(0, 300) : undefined);
}
throw error;
}
const json = `${JSON.stringify(checked, null, 2)}\n`;
if (Buffer.byteLength(json) > SAVED_MAX_BYTES) refuse("starter-pack-invalid", `a saved template is at most ${SAVED_MAX_BYTES} bytes`);
const byPath = (a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
return {
name, scope: checked.scope, ...(project === undefined ? {} : { project }),
template: checked, json, digest: sha256(json),
freeText: record.freeText.sort(byPath), mentions: record.mentions.sort(byPath),
repinned: record.repinned.sort(), packsDropped: record.packsDropped,
};
}
// ---- The file ----
const TEMPLATE_EXISTS = "a template of this name is already saved; save under another name";
// Whether a file (or anything, a symlink included) already holds the name. It looks only through a
// templates and an account directory that are real directories, so a link is never followed to say
// what lies elsewhere; any other case is left to the directory checks and the exclusive create.
function taken(templates, home, file) {
try {
if (!lstatSync(templates).isDirectory() || !lstatSync(home).isDirectory()) return false;
lstatSync(file);
return true;
} catch {
return false;
}
}
// A directory save writes under: real, the uid's own, writable by nobody else. The account's own
// directory is private (0700 or narrower), as readSavedTemplate requires.
function directory(path, uid, { private: closed }) {
try {
mkdirSync(path, { mode: 0o700 });
} catch (error) {
if (error?.code !== "EEXIST") refuse("not-configured", `the ${closed ? "account" : "templates"} directory cannot be created`);
}
let st;
try {
st = lstatSync(path);
} catch {
refuse("not-configured", `the ${closed ? "account" : "templates"} directory is unreadable`);
}
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== uid || (st.mode & 0o022) !== 0 || (closed && (st.mode & 0o077) !== 0)) {
refuse("not-configured", `the ${closed ? "account" : "templates"} directory is not private to this instance's user`);
}
}
// Save: the preview again, its digest compared, then the file created exclusively (wx, 0600). A
// name already saved refuses template-exists; nothing is replaced. The name is checked before the
// digest, so a save under a taken name says so even when the preview is stale.
export function templateSave({ document, name, project, digest, rolesDir, assertClean, configDir, account, uid = process.getuid() }) {
checkAccount(account);
checkTemplateName(name);
if (typeof digest !== "string" || !/^[0-9a-f]{64}$/.test(digest)) refuse("invalid-request", "save takes the preview's digest");
const templates = join(configDir, "templates");
const home = join(templates, account);
const file = join(home, `${name}.json`);
if (taken(templates, home, file)) refuse("template-exists", TEMPLATE_EXISTS);
const preview = templatePreview({ document, name, project, rolesDir, assertClean });
if (preview.digest !== digest) refuse("pack-plan-stale", "the organization or a template changed since the preview");
directory(templates, uid, { private: false });
directory(home, uid, { private: true });
let fd;
try {
fd = openSync(file, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
} catch (error) {
if (error?.code === "EEXIST") refuse("template-exists", TEMPLATE_EXISTS);
refuse("not-configured", "the template file cannot be created");
}
try {
fchmodSync(fd, 0o600);
const bytes = Buffer.from(preview.json);
let offset = 0;
while (offset < bytes.length) offset += writeSync(fd, bytes, offset);
fsyncSync(fd);
} catch (error) {
// A partial file would hold the name: remove the file this call created, then report the failure.
const open = fd;
fd = undefined;
try { closeSync(open); } catch { /* the write failure is the one reported */ }
try { unlinkSync(file); } catch { /* already gone */ }
throw error;
} finally {
if (fd !== undefined) closeSync(fd);
}
return { name, scope: preview.scope, ...(project === undefined ? {} : { project }), digest };
}
// List: the caller's own saved templates, each read through readSavedTemplate. A file that does not
// read is listed as unreadable, by name only. No account directory yet is an empty list.
export function templateList({ configDir, account, rolesDir, uid = process.getuid() }) {
checkAccount(account);
const home = join(configDir, "templates", account);
let names;
try {
const st = lstatSync(home);
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== uid || (st.mode & 0o077) !== 0) {
refuse("not-configured", "the account directory is not private to this instance's user");
}
names = readdirSync(home);
} catch (error) {
if (error instanceof TemplateError) throw error;
if (error?.code === "ENOENT") return { templates: [] };
refuse("not-configured", "the account directory is unreadable");
}
const files = names.filter((f) => f.endsWith(".json")).map((f) => f.slice(0, -".json".length))
.filter((n) => n.length <= TEMPLATE_NAME_MAX && NAME.test(n)).sort();
if (files.length > TEMPLATE_LIST_MAX) refuse("invalid-request", `the account holds more than ${TEMPLATE_LIST_MAX} saved templates`);
const templates = files.map((n) => {
try {
const { pack } = readSavedTemplate({ configDir, account, name: n, rolesDir, uid });
return { name: n, version: pack.version, label: pack.label, description: pack.description, scope: pack.scope, hires: pack.hires.length };
} catch (error) {
if (error?.name === "PluginError") return { name: n, readable: false, code: error.code };
throw error;
}
});
return { templates };
}
@@ -79,7 +79,6 @@ const VERBS = {
'request.draft.console': 'request.write',
'prd.revoke.console': 'request.write',
'plugins.set': 'org.setup',
'system-types.console': 'org.setup',
'project.remote.get.console': 'org.setup',
'project.remote.set.console': 'org.setup',
// #1873 SP4: starter packs, the board and workflows. The list is a board read; every write is
@@ -89,6 +88,9 @@ const VERBS = {
'pack.apply.console': null,
'board.set.console': null,
'workflow.set.console': null,
// Starter packs SP8 (#1873): saving a template and listing the saved ones are the owner's alone.
'template.save.console': null,
'template.list.console': null,
};
test('M1/R1 the eleven rights pin the grantable flags, including compulsory org.view', () => {
@@ -0,0 +1,19 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { join } from 'node:path';
import { KINDS, checkPlugins } from '../../plugins/src/kinds.mjs';
import { validateBusinessDocument } from '../src/organization-config.mjs';
import { businessDoc, REPO_ROLES, tmp } from './helpers.mjs';
// #1873 SP9: the system-type kind is removed outright (Jason, 2026-11-12). An org file that still
// names it refuses as an unknown kind, like any other name not in KINDS, even with an empty list.
test('SP9 an org file naming the removed system-type kind refuses unknown-kind', () => {
assert.equal(Object.hasOwn(KINDS, 'system-type'), false);
for (const list of [['coding'], []]) {
assert.throws(() => checkPlugins({ 'system-type': list }, 'acme.json plugins'),
(e) => e.code === 'unknown-kind' && /unknown kind "system-type"/.test(e.message));
const root = tmp(), doc = businessDoc(root), file = join(root, 'businesses/acme.json');
doc.plugins = { 'system-type': list };
assert.throws(() => validateBusinessDocument(doc, file, { rolesDir: REPO_ROLES }), /unknown kind "system-type"/);
}
});
@@ -1,29 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { join } from 'node:path';
import { validateBusinessDocument } from '../src/organization-config.mjs';
import { businessDoc, REPO_ROLES, tmp } from './helpers.mjs';
test('organization enables only installed system types; absent means none', () => {
const root = tmp(), doc = businessDoc(root), file = join(root, 'businesses/acme.json');
const validate = () => validateBusinessDocument(doc, file, { rolesDir: REPO_ROLES });
assert.deepEqual(validate().plugins['system-type'] ?? [], []);
doc.plugins = { 'system-type': ['coding', 'montage'] };
assert.deepEqual(validate().plugins['system-type'], ['coding', 'montage']);
doc.plugins['system-type'] = ['not-installed'];
assert.throws(validate, /system-type.*not-installed/);
});
test('system type changes through the organization writer are owner-only', async t => {
const { rolesCopy, writeJson } = await import('./helpers.mjs');
const { pluginSettings } = await import('../src/plugin-settings.mjs');
const { rmSync } = await import('node:fs');
const root = tmp(); t.after(() => rmSync(root, { recursive: true, force: true }));
const rolesDir = rolesCopy(root), doc = businessDoc(root), file = join(root, 'businesses/acme.json');
writeJson(file, doc);
const settings = pluginSettings({ dir: root, rolesDir });
const revision = settings.read('acme').revision;
assert.throws(() => settings.set('acme', { revision, plugins: { 'system-type': ['coding'] } }, 'tester'), /human-only/);
assert.deepEqual(settings.read('acme').plugins, {});
assert.deepEqual(settings.set('acme', { revision, plugins: { 'system-type': ['coding'] } }, doc.human).plugins['system-type'], ['coding']);
});
@@ -0,0 +1,322 @@
// Starter packs SP8 (#1873): saving an organization or one of its projects as a template. The saved
// file carries no credential material and no member, keeps each role's credential request lines,
// keeps free text verbatim (E2E6 fix 5) while the preview lists the fields that mention the
// organization's own names, and reads back as an origin-saved pack.
import test from "node:test";
import assert from "node:assert/strict";
import { chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { businessDoc, rolesCopy } from "./helpers.mjs";
import { entryFromTemplate, readTemplate } from "../src/definitions.mjs";
import {
DEFAULT_BOARD, TemplateError, templateList, templatePreview, templateSave,
} from "../src/template-save.mjs";
import { checkPack, readSavedTemplate } from "../../plugins/src/starter-pack.mjs";
const TOKEN = "fixture-held-token-sp8-91c4e7";
// The credential store's check, as the bus passes it: a held value anywhere in the text refuses.
const assertClean = (value) => {
if (JSON.stringify(value).includes(TOKEN)) throw Object.assign(new Error("credential-leak"), { name: "BusError", code: "credential-leak" });
};
const STRIPPED = ["copyBindings", "copyId", "orgIncarnation", "generation", "holder", "tracker", "vars", "launch", "members", "rev", "hireDecision"];
function fixture(t) {
const root = mkdtempSync(join(tmpdir(), "tsave-"));
t.after(() => rmSync(root, { recursive: true, force: true }));
const rolesDir = rolesCopy(root);
const doc = businessDoc(root, "acme");
const coder = readTemplate(rolesDir, "coder");
const writer = entryFromTemplate(coder);
writer.instructions = `Write code for acme in ${doc.projects.stack.root}/src. Ask jason; pair with ana on stack.`;
writer.personality = { summary: "Careful acme engineer.", traits: ["works with ana", "plain"], voice: "short" };
const hired = entryFromTemplate(readTemplate(rolesDir, "reviewer"));
hired.instructions = "Review what ana writes.";
doc.definitions = { writer, "hire-0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0": hired };
doc.roles.ana = { definition: "org:writer", credentials: { gitea: { file: join(root, "secrets", "ana.token"), rotateBy: "2099-01-01" } } };
doc.roles.bo = { definition: "org:hire-0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0" };
doc.retired = ["cy"];
// Credential material the save must never copy.
doc.copyBindings = { ana: { copyId: "copy-1", orgIncarnation: "inc-1", generation: 3, state: "bound", source: { level: "root", name: "ana", project: null } } };
doc.board = { columns: [
{ state: "todo", label: "acme backlog" }, { state: "in-progress", label: "Doing" }, { state: "in-review", label: "Review" },
{ state: "blocked", label: "Stuck" }, { state: "done", label: "Done" },
], layout: "list" };
doc.workflows = { change: { title: "Change for stack", steps: [
{ title: "ana writes it", state: "in-progress", roles: ["writer"] },
{ title: "Review it", state: "in-review", roles: ["hire-0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0", "human"] },
] } };
doc.packs = [
{ id: "coding", version: "1.0.0", origin: "shipped", scope: "org", appliedAt: "2026-11-01T00:00:00Z", revision: "r1", added: ["board"], proposedHires: [] },
{ id: "coding", version: "1.0.0", origin: "shipped", scope: "org", appliedAt: "2026-11-02T00:00:00Z", revision: "r2", added: [], proposedHires: [] },
{ id: "intake", version: "2.1.0", origin: "installed", project: "stack", appliedAt: "2026-11-03T00:00:00Z", revision: "r3", added: [], proposedHires: [] },
];
const configDir = join(root, "config");
mkdirSync(configDir, { mode: 0o700 });
return { root, rolesDir, doc, configDir };
}
const keysDeep = (value, out = []) => {
if (Array.isArray(value)) value.forEach((v) => keysDeep(v, out));
else if (value && typeof value === "object") for (const [k, v] of Object.entries(value)) { out.push(k); keysDeep(v, out); }
return out;
};
const refused = (fn, code, reason) => assert.throws(fn, (e) => {
assert.ok(e instanceof TemplateError, `${e?.name}: ${e?.message}`);
assert.equal(e.code, code);
if (reason !== undefined) assert.equal(e.reason, reason);
return true;
});
test("SP8 red before: an instance's credentials and copyBindings never reach the saved file", (t) => {
const { doc, rolesDir, configDir, root } = fixture(t);
const preview = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
templateSave({ document: doc, name: "acme-shape", digest: preview.digest, rolesDir, assertClean, configDir, account: "jason" });
const file = join(configDir, "templates", "jason", "acme-shape.json");
const text = readFileSync(file, "utf8");
const saved = JSON.parse(text);
const keys = keysDeep(saved);
for (const k of STRIPPED) assert.ok(!keys.includes(k), `the saved file holds ${k}`);
assert.ok(!text.includes(join(root, "secrets")), "the saved file names a token file");
assert.ok(!text.includes("copy-1") && !text.includes("inc-1"));
// credentials appears only as a definition's request lines: {service, scopes?}, never a file.
for (const [name, entry] of Object.entries(saved.definitions)) {
if (!entry.role) continue;
assert.ok(Array.isArray(entry.role.credentials), name);
for (const line of entry.role.credentials) assert.deepEqual(Object.keys(line).filter((k) => k !== "scopes"), ["service"]);
}
assert.equal(text, preview.json);
});
test("SP8 the saved file keeps each role's credential request lines", (t) => {
const { doc, rolesDir } = fixture(t);
const { template } = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
assert.deepEqual(template.definitions.writer.role.credentials, doc.definitions.writer.role.credentials);
assert.ok(template.definitions.writer.role.credentials.length > 0);
});
// E2E6 fix 5, red before: the old save rewrote prose word by word, so a project named "work" turned
// "hand finished work to review" into "hand finished {project} to review", and apply never resolves
// a placeholder. Save now keeps every free-text field as it stands.
test("E2E6 red before: save never rewrites prose; a project named work leaves \"hand finished work to review\" as it is", (t) => {
const { doc, rolesDir } = fixture(t);
doc.projects.work = { ...structuredClone(doc.projects.stack), root: join(doc.projects.stack.root, "..", "work-root") };
doc.workflows.change.steps[1].title = "hand finished work to review";
const preview = templatePreview({ document: doc, name: "team-shape", rolesDir, assertClean });
assert.equal(preview.template.workflows.change.steps[1].title, "hand finished work to review");
assert.ok(!preview.json.includes("{project}"));
// The owner sees the mention and may edit the text before saving.
assert.deepEqual(preview.mentions.find((m) => m.path === "workflows.change.steps[1].title"), { path: "workflows.change.steps[1].title", names: ["project work"] });
});
test("E2E6 free text is kept verbatim, no placeholder is written, and the preview lists each field that mentions an org-specific name", (t) => {
const { doc, rolesDir } = fixture(t);
const preview = templatePreview({ document: doc, name: "team-shape", rolesDir, assertClean });
const { template } = preview;
assert.equal(template.definitions.writer.instructions, doc.definitions.writer.instructions);
assert.equal(template.definitions.writer.personality.summary, "Careful acme engineer.");
assert.deepEqual(template.definitions.writer.personality.traits, ["works with ana", "plain"]);
assert.equal(template.board.columns[0].label, "acme backlog");
assert.equal(template.workflows.change.description, "Change for stack");
assert.equal(template.workflows.change.steps[0].title, "ana writes it");
for (const placeholder of ["{org}", "{project}", "{agent}", "{owner}", "{root}"]) assert.ok(!preview.json.includes(placeholder), placeholder);
assert.equal(template.description, "Saved from an organization.");
assert.ok(!Object.hasOwn(preview, "replacements"));
const at = (path) => preview.mentions.find((m) => m.path === path)?.names;
// A root path is one mention, labelled by kind alone, not the ids inside it.
assert.deepEqual(at("definitions.writer.instructions"), ["organization acme", "project root", "owner jason", "agent ana", "project stack"]);
assert.deepEqual(at("definitions.writer.personality.traits[1]"), undefined);
assert.deepEqual(at("board.columns[0].label"), ["organization acme"]);
// Role names (writer, coder) are not mentions; a longer word holding a name is not one either.
assert.equal(at("board.columns[1].label"), undefined);
doc.board.columns[1].label = "acmes and anas";
assert.equal(templatePreview({ document: doc, name: "team-shape", rolesDir, assertClean }).mentions.find((m) => m.path === "board.columns[1].label"), undefined);
// Every mention is a kept free-text field, in path order.
const paths = preview.mentions.map((m) => m.path);
assert.deepEqual(paths, [...paths].sort());
for (const p of paths) assert.ok(preview.freeText.some((f) => f.path === p), p);
});
test("E2E6 text that looks like a placeholder, or an instance named root, org or agent, stays verbatim", (t) => {
const { doc, rolesDir } = fixture(t);
doc.roles.root = { definition: "org:writer" };
doc.roles.org = { definition: "org:writer" };
doc.definitions.writer.instructions = "Work for acme with root on stack. Ask org, then jason. Keep {org} and {agent} literal.";
const preview = templatePreview({ document: doc, name: "team-shape", rolesDir, assertClean });
assert.equal(preview.template.definitions.writer.instructions, doc.definitions.writer.instructions);
assert.deepEqual(preview.mentions.find((m) => m.path === "definitions.writer.instructions").names, ["organization acme", "agent root", "project stack", "agent org", "owner jason"]);
});
test("SP8 the preview lists every kept free-text field with its saved text", (t) => {
const { doc, rolesDir } = fixture(t);
const preview = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
const { template } = preview;
const expected = [];
for (const [name, entry] of Object.entries(template.definitions)) {
if (typeof entry.instructions === "string") expected.push([`definitions.${name}.instructions`, entry.instructions]);
if (entry.personality) {
if (entry.personality.summary !== undefined) expected.push([`definitions.${name}.personality.summary`, entry.personality.summary]);
if (entry.personality.voice !== undefined) expected.push([`definitions.${name}.personality.voice`, entry.personality.voice]);
(entry.personality.traits ?? []).forEach((v, i) => expected.push([`definitions.${name}.personality.traits[${i}]`, v]));
}
}
template.board.columns.forEach((c, i) => expected.push([`board.columns[${i}].label`, c.label]));
for (const [w, wf] of Object.entries(template.workflows)) {
expected.push([`workflows.${w}.description`, wf.description]);
wf.steps.forEach((s, i) => expected.push([`workflows.${w}.steps[${i}].title`, s.title]));
}
expected.sort(([a], [b]) => (a < b ? -1 : 1));
assert.deepEqual(preview.freeText.map((f) => [f.path, f.text]), expected);
});
test("SP8 a held credential in kept text refuses template-secret-suspected naming the field path, never the text", (t) => {
const { doc, rolesDir } = fixture(t);
doc.definitions.writer.personality.voice = `short, key ${TOKEN}`;
refused(() => templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean }), "template-secret-suspected", "definitions.writer.personality.voice");
try { templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean }); } catch (e) { assert.ok(!e.message.includes(TOKEN)); }
// A held value inside other text still refuses: the scan reads the text save keeps.
const second = fixture(t);
second.doc.workflows.change.steps[1].title = `acme${TOKEN}`;
refused(() => templatePreview({ document: second.doc, name: "acme-shape", rolesDir: second.rolesDir, assertClean }), "template-secret-suspected", "workflows.change.steps[1].title");
// A workflow key that holds one is named by its parent and [key].
const third = fixture(t);
third.doc.workflows[TOKEN.replace(/-/g, "")] = third.doc.workflows.change;
const keyed = (v) => { if (JSON.stringify(v).includes(TOKEN.replace(/-/g, ""))) throw Object.assign(new Error("x"), { code: "credential-leak" }); };
refused(() => templatePreview({ document: third.doc, name: "acme-shape", rolesDir: third.rolesDir, assertClean: keyed }), "template-secret-suspected", "workflows[key]");
});
test("SP8 a second save to the same name refuses and leaves the first file", (t) => {
const { doc, rolesDir, configDir } = fixture(t);
const { digest } = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
templateSave({ document: doc, name: "acme-shape", digest, rolesDir, assertClean, configDir, account: "jason" });
const file = join(configDir, "templates", "jason", "acme-shape.json");
const first = readFileSync(file);
doc.definitions.writer.instructions = "Changed.";
const next = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
refused(() => templateSave({ document: doc, name: "acme-shape", digest: next.digest, rolesDir, assertClean, configDir, account: "jason" }), "template-exists", "a template of this name is already saved; save under another name");
// E2E6 fix 8: the name is checked first, so a stale or malformed-but-valid digest still says the name is taken.
refused(() => templateSave({ document: doc, name: "acme-shape", digest: "0".repeat(64), rolesDir, assertClean, configDir, account: "jason" }), "template-exists");
assert.deepEqual(readFileSync(file), first);
assert.equal(lstatSync(file).mode & 0o777, 0o600);
assert.equal(lstatSync(join(configDir, "templates", "jason")).mode & 0o777, 0o700);
});
test("SP8 a name that is no pack id refuses before any path is built", (t) => {
const { doc, rolesDir, configDir } = fixture(t);
for (const name of ["..", ".", "a/b", "../x", ".hidden", "", "A", "x".repeat(65), 7]) {
refused(() => templatePreview({ document: doc, name, rolesDir, assertClean }), "invalid-request");
refused(() => templateSave({ document: doc, name, digest: "0".repeat(64), rolesDir, assertClean, configDir, account: "jason" }), "invalid-request");
}
for (const account of ["..", "a/b", ""]) refused(() => templateList({ configDir, account, rolesDir }), "human-required");
assert.ok(!existsSync(join(configDir, "templates")));
});
test("SP8 a ninth hire refuses", (t) => {
const { doc, rolesDir } = fixture(t);
// pm, cto, coder, reviewer, ana and bo are six; three more make nine.
doc.roles.dee = { definition: "coder" };
doc.roles.eve = { definition: "coder" };
assert.equal(templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean }).template.hires.length, 8);
doc.roles.fay = { definition: "reviewer" };
refused(() => templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean }), "starter-pack-invalid", "a template holds at most 8 hires; this one would hold 9");
});
test("SP8 each packs entry holds only id and version, once, at the saved scope", (t) => {
const { doc, rolesDir } = fixture(t);
const org = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean }).template;
assert.deepEqual(org.packs, [{ id: "coding", version: "1.0.0" }]);
const project = templatePreview({ document: doc, name: "stack-shape", project: "stack", rolesDir, assertClean }).template;
assert.deepEqual(project.packs, [{ id: "intake", version: "2.1.0" }]);
for (const entry of [...org.packs, ...project.packs]) assert.deepEqual(Object.keys(entry).sort(), ["id", "version"]);
});
test("SP8 hires and names: no chief, no assistant, no decision id; hire-<decision> becomes <template>-<n>", (t) => {
const { doc, rolesDir } = fixture(t);
doc.roles.helper = { definition: "assistant" };
const preview = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
const { template } = preview;
assert.ok(!preview.json.includes("0f1e2d3c"), "a decision id left the organization");
assert.ok(Object.hasOwn(template.definitions, "reviewer-1"));
assert.deepEqual(template.workflows.change.steps[1].roles, ["reviewer-1", "human"]);
assert.deepEqual(template.workflows.change.steps.map((s) => s.id), ["step-1", "step-2"]);
const hires = Object.fromEntries(template.hires.map((h) => [h.name, h.definition]));
assert.deepEqual(hires, { "coder-1": "writer", "coder-2": "coder", "cto-1": "cto", "pm-1": "pm", "reviewer-1": "reviewer-1", "reviewer-2": "reviewer" });
for (const h of template.hires) assert.deepEqual(Object.keys(h).sort(), ["definition", "name", "reason"]);
assert.ok(!Object.values(hires).some((d) => /chief|assistant/.test(d)));
assert.deepEqual(template.definitions.coder, { template: "coder", sha256: readTemplate(rolesDir, "coder").sha256 });
assert.equal(template.board.layout, "list");
});
test("SP8 the saved file reads back as an origin-saved pack, and list shows it", (t) => {
const { doc, rolesDir, configDir } = fixture(t);
assert.deepEqual(templateList({ configDir, account: "jason", rolesDir }), { templates: [] });
const { digest } = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
templateSave({ document: doc, name: "acme-shape", digest, rolesDir, assertClean, configDir, account: "jason" });
const { pack } = readSavedTemplate({ configDir, account: "jason", name: "acme-shape", rolesDir, uid: process.getuid() });
assert.equal(pack.id, "acme-shape");
writeFileSync(join(configDir, "templates", "jason", "broken.json"), "{", { mode: 0o600 });
const listed = templateList({ configDir, account: "jason", rolesDir });
assert.deepEqual(listed.templates.map((x) => x.name), ["acme-shape", "broken"]);
assert.equal(listed.templates[0].hires, 6);
assert.equal(listed.templates[1].readable, false);
// Another account's list is its own.
assert.deepEqual(templateList({ configDir, account: "alex", rolesDir }), { templates: [] });
});
test("SP8 a project template carries the project's agents, the definitions it names and the default board", (t) => {
const { doc, rolesDir } = fixture(t);
doc.projects.stack.agents = { ana: { definition: "org:writer" }, rev: { definition: "reviewer" }, chief: { exclude: true } };
const { template, scope } = templatePreview({ document: doc, name: "stack-shape", project: "stack", rolesDir, assertClean });
assert.equal(scope, "project");
assert.deepEqual(Object.keys(template.definitions).sort(), ["reviewer", "writer"]);
assert.deepEqual(template.board, JSON.parse(JSON.stringify(DEFAULT_BOARD)));
assert.deepEqual(template.workflows, {});
assert.equal(template.description, "Saved from one project of an organization.");
refused(() => templatePreview({ document: doc, name: "x", project: "nope", rolesDir, assertClean }), "invalid-request");
});
test("SP8 a from the template moved under is re-pinned when the role still narrows it, and refused when it widens", (t) => {
const { doc, rolesDir } = fixture(t);
doc.definitions.writer.from.sha256 = "1".repeat(64);
const preview = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
assert.deepEqual(preview.repinned, ["definitions.writer.from"]);
assert.equal(preview.template.definitions.writer.from.sha256, readTemplate(rolesDir, "coder").sha256);
doc.definitions.writer.role.tools = [...doc.definitions.writer.role.tools, "not-a-coder-tool"];
assert.throws(() => templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean }), (e) => e instanceof TemplateError && /^starter-pack-invalid$/.test(e.code));
});
test("SP8 save takes the preview's digest; a changed organization refuses pack-plan-stale and writes nothing", (t) => {
const { doc, rolesDir, configDir } = fixture(t);
const { digest } = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
doc.board.columns[1].label = "Now";
refused(() => templateSave({ document: doc, name: "acme-shape", digest, rolesDir, assertClean, configDir, account: "jason" }), "pack-plan-stale");
refused(() => templateSave({ document: doc, name: "acme-shape", digest: "zz", rolesDir, assertClean, configDir, account: "jason" }), "invalid-request");
assert.ok(!existsSync(join(configDir, "templates", "jason", "acme-shape.json")));
});
test("SP8 save refuses a templates directory another user can write, or an account directory that is a link", (t) => {
const { doc, rolesDir, configDir, root } = fixture(t);
const { digest } = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
mkdirSync(join(configDir, "templates"), { mode: 0o700 });
chmodSync(join(configDir, "templates"), 0o777);
refused(() => templateSave({ document: doc, name: "acme-shape", digest, rolesDir, assertClean, configDir, account: "jason" }), "not-configured");
chmodSync(join(configDir, "templates"), 0o700);
const elsewhere = join(root, "elsewhere");
mkdirSync(elsewhere, { mode: 0o700 });
symlinkSync(elsewhere, join(configDir, "templates", "jason"));
refused(() => templateSave({ document: doc, name: "acme-shape", digest, rolesDir, assertClean, configDir, account: "jason" }), "not-configured");
refused(() => templateList({ configDir, account: "jason", rolesDir }), "not-configured");
assert.ok(!existsSync(join(elsewhere, "acme-shape.json")));
// A file planted as a link at the name is not followed.
rmSync(join(configDir, "templates", "jason"));
mkdirSync(join(configDir, "templates", "jason"), { mode: 0o700 });
symlinkSync(join(elsewhere, "target.json"), join(configDir, "templates", "jason", "acme-shape.json"));
assert.throws(() => templateSave({ document: doc, name: "acme-shape", digest, rolesDir, assertClean, configDir, account: "jason" }), TemplateError);
assert.ok(!existsSync(join(elsewhere, "target.json")));
});
test("SP8 the saved template is a pack checkPack accepts as saved", (t) => {
const { doc, rolesDir } = fixture(t);
const { template } = templatePreview({ document: doc, name: "acme-shape", rolesDir, assertClean });
assert.deepEqual(checkPack(structuredClone(template), { origin: "saved", rolesDir }), template);
});
-9
View File
@@ -499,15 +499,6 @@ record separation, bounded reads and per-key quota settings.
A harness declares `outputPolicy({ proxy })` as `adapter`, `non-newline-stdout`, or `stdout`. The launcher validates and serializes that declaration; old sessions retain their recorded activity behavior.
### System types
`system-type` modules export `interfaceVersion = 1` and `systemType()`, returning
`{ id, label, description, toolsets }`. The id is the module directory name;
`toolsets` is a distinct list of toolset ids. The installed list is discovered
from module directories, and malformed modules refuse discovery. An organization
selects modules with `plugins.system-type`; an omitted list enables none. The
initial five modules declare no toolsets yet.
### Instance-installed packages (PL1, #1873)
The instance owner may install trusted plugin code from the host shell:
-2
View File
@@ -6,8 +6,6 @@ const VERBS = new Set([
"project.create",
"project.archive",
"project.unarchive",
"project.system-types",
"project.system-types.set",
"thread.list",
"thread.create",
"thread.messages",
-4
View File
@@ -80,10 +80,6 @@ export const KINDS = Object.freeze({
"model-provider": Object.freeze({
kind: "model-provider", interfaceVersion: 1, exports: Object.freeze(["catalogue", "preflight", "route"]), switch: "list",
}),
// Project system types are installed modules, enabled per organization.
"system-type": Object.freeze({
kind: "system-type", interfaceVersion: 1, exports: Object.freeze(["systemType"]), switch: "list",
}),
// A starter pack (#1873 SP1): starterPack() takes nothing and returns a pack document, which
// starter-pack.mjs checks with the origin of where it was read. A pack is copied into an
// organization, never switched on, so its switch is "none": `plugins` can never name one.
-19
View File
@@ -1,19 +0,0 @@
import { loadInstalled } from './loader.mjs';
import { NAME } from './kinds.mjs';
// Discovery is fail-closed: malformed or unloadable installed modules cannot be advertised.
export async function installedSystemTypes({dataRoot = null} = {}) {
const { modules, refused } = await loadInstalled('system-type',{dataRoot});
if (refused.length) throw Object.assign(new Error('system type module refused'), { code: 'system-type-invalid' });
return modules.map(({ name, module }) => {
const value = module.systemType();
if (!value || Object.keys(value).sort().join(',') !== 'description,id,label,toolsets' || value.id !== name ||
typeof value.label !== 'string' || !value.label.trim() || /[\r\n]/.test(value.label) ||
typeof value.description !== 'string' || !value.description.trim() || /[\r\n]/.test(value.description) ||
!Array.isArray(value.toolsets) || value.toolsets.some(t => typeof t !== 'string' || !NAME.test(t)) ||
new Set(value.toolsets).size !== value.toolsets.length) {
throw Object.assign(new Error(`invalid system type ${name}`), { code: 'system-type-invalid' });
}
return structuredClone(value);
});
}
@@ -1,5 +0,0 @@
export const interfaceVersion = 1;
export function systemType() {
return { id: "business-management", label: "Business management", description: "Coordinate and manage business operations.", toolsets: [] };
}
@@ -1,5 +0,0 @@
export const interfaceVersion = 1;
export function systemType() {
return { id: "coding", label: "Coding", description: "Build and maintain software.", toolsets: [] };
}
@@ -1,5 +0,0 @@
export const interfaceVersion = 1;
export function systemType() {
return { id: "creative-writing", label: "Creative writing", description: "Develop and write creative works.", toolsets: [] };
}
@@ -1,5 +0,0 @@
export const interfaceVersion = 1;
export function systemType() {
return { id: "marketing", label: "Marketing", description: "Plan and produce marketing work.", toolsets: [] };
}
@@ -1,5 +0,0 @@
export const interfaceVersion = 1;
export function systemType() {
return { id: "montage", label: "Mosaic Montage", description: "Create and manage media productions.", toolsets: [] };
}
+1 -1
View File
@@ -6,7 +6,7 @@ import { KINDS, NAME, PluginError, checkPlugins } from "../src/kinds.mjs";
const code = expected => e => e instanceof PluginError && e.code === expected;
test("REQ-PLUG-1 REQ-LIMIT-4 the registry: usage-source at interface version 1, limit-check at 2 (row 125), each a list", () => {
assert.deepEqual(Object.keys(KINDS), ["usage-source", "limit-check", "search-source", "channel", "harness", "usage", "model-provider", "system-type", "starter-pack", "theme", "task-tracker"]);
assert.deepEqual(Object.keys(KINDS), ["usage-source", "limit-check", "search-source", "channel", "harness", "usage", "model-provider", "starter-pack", "theme", "task-tracker"]);
assert.deepEqual({ ...KINDS["task-tracker"], exports: [...KINDS["task-tracker"].exports] }, { kind: "task-tracker", interfaceVersion: 1, exports: ["adapter"], switch: "one" });
assert.ok(Object.isFrozen(KINDS["task-tracker"]) && Object.isFrozen(KINDS["task-tracker"].exports));
// Stream S4 (PLAN C5): a theme is data. interfaceVersion 1, one export, a list switch.
+5 -5
View File
@@ -12,7 +12,7 @@ import { validateBusinessDocument } from "../../organization/src/organization-co
const GOOD = { "usage-source": ["meter"], "limit-check": ["cap"] };
// What loaded() reports for GOOD: search-source is a kind too, switched off here.
const GOOD_LOADED = { ...GOOD, "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "system-type": [], "starter-pack": [], theme: [], "task-tracker": [] };
const GOOD_LOADED = { ...GOOD, "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "starter-pack": [], theme: [], "task-tracker": [] };
// A tree with one good module of each kind, already loaded.
async function loadedTree(t, options) {
@@ -56,7 +56,7 @@ test("REQ-PLUG-1 fixture modules of each kind switch on from a test organization
const loader = await s.loader();
const result = await loader.load(business.plugins);
assert.deepEqual(result.refused, []);
assert.deepEqual(result.loaded, { ...doc.plugins, "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "system-type": [], "starter-pack": [], theme: [], "task-tracker": [] });
assert.deepEqual(result.loaded, { ...doc.plugins, "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "starter-pack": [], theme: [], "task-tracker": [] });
assert.equal(loader.modules("usage-source")[0].readTurn(), "fixture-usage");
assert.equal(loader.modules("limit-check")[0].mayLaunch(), "fixture-limit");
assert.equal(loader.modules("limit-check")[0].mustStop(), false);
@@ -171,7 +171,7 @@ test("REQ-PLUG-1 a path that becomes unreadable after the lstat checks refuses a
assert.match(result.refused[0].message, /packages\/plugins can't be read \(EACCES\)/);
assert.deepEqual(result.refused[0].kept, ["meter"]);
assert.deepEqual(s.loader.logged.slice(before), result.refused);
assert.deepEqual(s.loader.loaded(), { "usage-source": ["meter"], "limit-check": [], "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "system-type": [], "starter-pack": [], theme: [], "task-tracker": [] });
assert.deepEqual(s.loader.loaded(), { "usage-source": ["meter"], "limit-check": [], "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "starter-pack": [], theme: [], "task-tracker": [] });
} finally {
if (lock) chmodSync(lock, 0o755);
}
@@ -233,7 +233,7 @@ test("REQ-PLUG-1 a kind refused on its first load has nothing switched on, and t
const loader = await s.loader();
// meter loads, absent refuses: the whole kind stays off, not half on.
const result = await loader.load({ "usage-source": ["meter", "absent"], "limit-check": ["cap"] });
assert.deepEqual(result.loaded, { "usage-source": [], "limit-check": ["cap"], "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "system-type": [], "starter-pack": [], theme: [], "task-tracker": [] });
assert.deepEqual(result.loaded, { "usage-source": [], "limit-check": ["cap"], "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "starter-pack": [], theme: [], "task-tracker": [] });
assert.deepEqual(loader.loaded(), result.loaded);
assert.deepEqual(loader.modules("usage-source"), []);
assert.equal(loader.logged.length, 1);
@@ -261,7 +261,7 @@ test("REQ-PLUG-1 the setting's shape: unknown kinds refuse alone, a bad whole se
await refusesKeepingOld(s, ["meter", "meter"], "bad-setting", "meter", /twice/);
// An absent key switches the kind off.
result = await s.loader.load({ "limit-check": ["cap"] });
assert.deepEqual(result.loaded, { "usage-source": [], "limit-check": ["cap"], "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "system-type": [], "starter-pack": [], theme: [], "task-tracker": [] });
assert.deepEqual(result.loaded, { "usage-source": [], "limit-check": ["cap"], "search-source": [], channel: [], harness: [], usage: [], "model-provider": [], "starter-pack": [], theme: [], "task-tracker": [] });
});
test("REQ-PLUG-1 loads run one at a time, so the last requested setting wins", async t => {
-16
View File
@@ -207,22 +207,6 @@ test('PL1 tool exceptions cannot impersonate trusted refusals or echo payload by
await assert.rejects(service.call('acme','writer','acmeplug.echo',{text:'hello'}),e=>e.code==='plugin-tool-failed' && e.plugin==='acmeplug' && !e.message.includes(SECRET));
});
test('PL1 A3 organization copy readers preserve the derived installed-plugin root',async t=>{
const {withCopies,loadWithCopies}=await import('../../organization/src/agent-copies.mjs');
const f=fixture(t);f.manifest.kinds=['system-type'];f.manifest.interfaceVersions={'system-type':1};
mkdirSync(join(f.src,'system-type'));writeFileSync(join(f.src,'system-type/index.mjs'),'export const interfaceVersion=1;export function systemType(){return {}};');
const hash=f.seal();f.install(f.src,{expectedHash:hash});
const doc=businessDoc(f.root),rolesDir=rolesCopy(f.root);doc.plugins={...doc.plugins,'system-type':['acmeplug']};writeJson(join(f.root,'businesses/acme.json'),doc);
const options={rolesDir,dataRoot:f.dataRoot,snapshot:null};
assert.deepEqual(withCopies(doc,join(f.root,'businesses/acme.json'),options).plugins['system-type'],['acmeplug']);
assert.deepEqual(loadWithCopies('acme',{...options,dir:f.root}).plugins['system-type'],['acmeplug']);
const installed=join(f.dataRoot,'plugins/acmeplug/system-type/index.mjs');chmodSync(installed,0o600);writeFileSync(installed,SECRET);
// Config checks names only; the loader owns bad-pin refusal and preserves core.
assert.deepEqual(withCopies(doc,join(f.root,'businesses/acme.json'),options).plugins['system-type'],['acmeplug']);
doc.plugins['system-type']=['defualt'];
assert.throws(()=>withCopies(doc,join(f.root,'businesses/acme.json'),options),/unknown module/);
});
test('PL1 replacement pins refuse across loader and host tools until a fresh process',async t=>{
for(const first of ['loader','tool']) {
const f=fixture(t);
@@ -1,19 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createLoader, loadInstalled } from '../src/loader.mjs';
test('installed system types describe the five modules and unknown names refuse', async () => {
const { modules, refused } = await loadInstalled('system-type');
assert.deepEqual(refused, []);
assert.deepEqual(modules.map(m => m.name), ['business-management', 'coding', 'creative-writing', 'marketing', 'montage']);
for (const { name, module } of modules) {
const info = module.systemType();
assert.equal(info.id, name);
assert.ok(info.label && info.description && Array.isArray(info.toolsets));
if (name === 'montage') assert.equal(info.label, 'Mosaic Montage');
}
const loader = createLoader({ log() {} });
const result = await loader.load({ 'system-type': ['unknown'] });
assert.equal(result.refused.length, 1);
assert.deepEqual(result.loaded['system-type'], []);
});
+2 -2
View File
@@ -70,8 +70,8 @@
{ "id": "REQ-LIMIT-4", "title": "Usage sources and limit checks are the first plug-in kinds; an unknown turn is logged and shown, never counted as zero" },
{ "id": "REQ-PLUG-1", "title": "Every part the core calls out to is a plug-in kind; modules load from packages/plugins, switched on in the org file" },
{ "id": "REQ-PLUG-2", "title": "New call-outs go through a plug-in kind from the start; a hard-coded part converts when a row changes it" },
{ "id": "REQ-SYS-1", "title": "System types are a plug-in kind; organizations enable a list of modules" },
{ "id": "REQ-SYS-2", "title": "Projects use enabled system types and their agents receive the corresponding toolsets" },
{ "id": "REQ-SYS-1", "title": "Starter packs are a plug-in kind; an organization or project copies one in once and owns the copy" },
{ "id": "REQ-SYS-2", "title": "Applying a pack grants nothing; every proposed agent is hired through the hire decision" },
{ "id": "REQ-HARN-1", "title": "The meta-harness generates each session's launch bundle from role and variables" },
{ "id": "REQ-HARN-2", "title": "Credential scope, verbs, the tool limit and a container enforce the rules" },
{ "id": "REQ-EVT-1", "title": "Every slice 1 step emits an event; measures come from events" },
+9 -4
View File
@@ -6,8 +6,8 @@ import { startServer, previewOptions, proofOptions, DEFAULT_BOARD } from './serv
import { busCaller, busBusiness, userCaller, consoleCall } from './bus.mjs';
import { organizationConsole } from './organizations.mjs';
import { consoleBus, userOptionsFor, agentCaller, TESTER_AGENT_VERBS } from './console-bus.mjs';
import { SYSTEM_TYPE_VERBS } from './system-types-http.mjs';
import { packsCaller } from './packs-http.mjs';
import { templatesCaller } from './template-http.mjs';
import { autonomyCaller } from './autonomy-http.mjs';
import { consoleAccess } from './access.mjs';
import { accessContextFor } from '../../cli/src/access.mjs';
@@ -21,6 +21,8 @@ import { listOrgs, resolveOrg } from '../../organization/src/org-registry.mjs';
import { showOrg } from '../../organization/src/org-service.mjs';
import { configDir } from '../../organization/src/organization-config.mjs';
import { loadRootAgents } from '../../organization/src/agent-levels.mjs';
import { organizationWriter } from '../../organization/src/organization.mjs';
import { templateHashes } from '../../organization/src/definitions.mjs';
import { consoleReads, redactor } from './reads.mjs';
import { createDevOwner } from './dev-owner.mjs';
import { devOwnerInstance, devLanOptions } from '../../cli/src/dev-local-owner.mjs';
@@ -189,9 +191,6 @@ try {
// The owner reads request status on the selected organization's bus, as on the root Console. Request
// writes stay off: the README "Console actions" inventory marks them unavailable for the organizations release.
requestRead: session.user === undefined ? consoleCall({ dataRoot: root, socket: paths.socket, business: id }) : null,
// Phase P2 (#1873): the owner's System types writes, the organization's enable switch (plugins.set
// with its revision) and a project's selection, over the owner's human transport under --send only.
systemTypesWrite: session.user === undefined && sendOn ? agentCaller(owner, SYSTEM_TYPE_VERBS) : null,
// D1 (#1873): the owner's autonomy writes (save, review, confirm, reverse) on this organization's bus.
// These options exist only on a --dev-local-owner Console (`organizations` above). The fence passes
// those four verbs and nothing else the console transport carries.
@@ -199,6 +198,12 @@ try {
// Starter packs SP4 (#1873): the owner's pack apply, board and workflow writes, the three write
// .console verbs only. Preview and the pack list are reads over requestRead.
packsWrite: session.user === undefined && sendOn ? packsCaller(consoleCall({ dataRoot: root, socket: paths.socket, business: id })) : null,
// Starter packs SP8 (#1873): the owner's template save, template.save.console with a digest only. The list
// and the preview are reads over requestRead.
templatesWrite: session.user === undefined && sendOn ? templatesCaller(consoleCall({ dataRoot: root, socket: paths.socket, business: id })) : null,
// Starter packs SP6 (#1873): the owner's workflow read, the organization file and its revision as the
// writer reads them (the read `mosaic org workflow set` takes its revision from), and the V2 template names.
workflowsRead: session.user === undefined ? () => ({ ...organizationWriter({ dir: configDir(), rolesDir: rolesDir() }).read(id), templates: templateHashes(rolesDir()).map(t => t.template) }) : null,
reads: consoleReads({ root: REPO, queueRoot: options.checkout ?? REPO, system, configDir: configDir(), rolesDir: rolesDir(), business: id, orgRoot: root }),
};
},
+151 -22
View File
@@ -6,6 +6,7 @@
// rechecks everything in the resolve transaction. Nothing here retries.
import { createHash, randomBytes } from 'node:crypto';
import { BusReadError, subjectOk } from './bus.mjs';
import { ID_PATTERN } from '../../organization/src/vocabulary.mjs';
export const DECISION_ROUTE = /^\/api\/decisions\/([^/]*)\/(confirmation|resolve|start|templates)$/;
// The endpoint contract, design section 4. Each refusal is one of these, never a 200.
@@ -42,21 +43,129 @@ const BUS_CODES = {
'decision-closed': 'decision-closed', 'decision-stale': 'decision-stale', 'invalid-choice': 'decision-stale',
'invalid-request': 'invalid-request', 'hire-owner-required':'hire-owner-required','right-required':'right-required','decision-expired':'decision-expired','hire-resolution-required':'hire-resolution-required',
};
// D4 (#1873, P1-2): a hire, fire or change start the bus definitely refused keeps its own code,
// but only for codes the broker raises before it records the decision: the proposal checks in
// hire-decisions.mjs prepare(), which #raiseHire runs before its INSERT. hire.start.console then
// resolves the decision and applies it in #hireConsole, and those steps can raise other codes (and
// some of the same generic ones) after the decision exists or is consumed, so every code not listed
// here stays outcome-unknown. Review P3: an allowlist, not a pass-through of any well-formed code.
// D4 (#1873, P1-2) and E2E6 P1-2: a hire, fire or change start the bus definitely refused keeps its
// own code and a next step. hire.start.console runs prepare() (hire-decisions.mjs) before it records
// the decision, then #hireConsole checks it, consumes it, and applies it. apply() runs check() again
// and the writers, authorityHost and #fireApproved run after the consume, so most codes can come from
// either side of it; for a fire the agent's runs may already be stopped. The code alone does not say
// which, and the inbox cannot tell either: it lists no closed decision (broker inbox verb), so a
// consumed one simply drops out of it. Those codes therefore say part of the change may already be
// applied and send the user to the Agents page (re-check finding 1). Only codes
// that prepare() alone raises say "Nothing was applied.": the route checks (checkRoutes runs in
// prepare(), and in adjusted() only for an adjust, which a start never carries), definition-shared,
// definition-not-hireable and no-chief-of-staff. organization-uncertain, a lost reply and the
// bus server's own request codes (request-timeout and invalid-envelope can fire while the request is
// processing) stay outcome-unknown.
const NONE = ' Nothing was applied.';
const EITHER = ' Part of it may already be applied: check the Agents page before trying again.';
const CHECK = ' Check the Agents page and the inbox before trying again.';
const HIRE_START_REFUSALS = {
'model-provider-unavailable': [409, 'The model proxy does not offer this model, or its catalogue is not ready. Pick a model it offers, or ask the owner to check Settings > Model proxy. Nothing was applied.'],
'model-catalogue-unavailable': [503, 'The model catalogue could not be read, so this model could not be checked. Check Settings > Model proxy, then try again. Nothing was applied.'],
'model-harness-incompatible': [409, 'This model does not run on the chosen harness. Pick another model or harness. Nothing was applied.'],
'thinking-unsupported': [409, 'The chosen harness does not apply this thinking level. Clear Thinking or pick a level it applies. Nothing was applied.'],
'definition-not-hireable': [409, 'This organization does not hire agents from this role template. Pick another template. Nothing was applied.'],
'definition-shared': [409, 'Another agent uses the same role definition, so this change cannot replace it. Nothing was applied.'],
'no-chief-of-staff': [409, 'This organization has no chief of staff, so a hire, fire or change cannot be checked against it. Nothing was applied.'],
'model-provider-unavailable': [409, 'The model proxy does not offer this model, or its catalogue is not ready. Pick a model it offers, or ask the owner to check Settings > Model proxy.' + NONE],
'model-catalogue-unavailable': [503, 'The model catalogue could not be read, so this model could not be checked. Check Settings > Model proxy, then try again.' + NONE],
'model-harness-incompatible': [409, 'This model does not run on the chosen harness. Pick another model or harness.' + NONE],
'thinking-unsupported': [409, 'The chosen harness does not apply this thinking level. Clear Thinking or pick a level it applies.' + NONE],
'unknown-model': [409, 'This model is not one of the organization\'s models. Pick a model listed under Settings > Model proxy.' + NONE],
'harness-off': [409, 'This harness is not switched on for the organization. Pick a harness listed under Settings > Routing, or ask the owner to switch it on.' + NONE],
'harness-refused': [409, 'The harness refused this route. Clear Thinking, or pick another model or harness under Settings > Routing.' + NONE],
'definition-not-hireable': [409, 'This organization does not hire agents from this role template. Pick another template.' + NONE],
'definition-shared': [409, 'Another agent uses the same role definition, so this change cannot replace it.' + NONE],
'definition-rev-conflict': [409, 'The role definition changed since it was read. Reload the page and propose again.' + EITHER],
'definition-missing': [409, 'The agent\'s role definition is missing from the organization file. Check the Agents page.' + EITHER],
'definition-secret-check-required': [503, 'The bus cannot check role text for secrets right now. Ask the owner to check the organization bus.' + EITHER],
'credential-leak': [409, 'The proposal carries a value the credential store holds. Remove it from the role text or the reason.' + EITHER],
'no-chief-of-staff': [409, 'This organization has no chief of staff, so a hire, fire or change cannot be checked against it.' + NONE],
'project-agents-invalid': [409, 'That project is not a code project of this organization. Pick a code project from Projects; a library project has no agents.' + EITHER],
'organization-unknown-instance': [409, 'No agent has that name at this level. Check the name on the Agents page.' + EITHER],
'organization-instance-exists': [409, 'An agent already has that name at this level. Pick another name, or propose a change to that agent.' + EITHER],
'organization-instance-retired': [409, 'That name belonged to a fired agent and cannot be reused. Pick another name.' + EITHER],
'organization-instance-referenced': [409, 'Another part of the organization still names this agent. Remove those references first.' + EITHER],
'instance-kept': [409, 'This agent is kept by the organization and cannot be fired or replaced this way.' + EITHER],
'root-fire-shared': [409, 'This root agent is shared with other organizations, so it cannot be fired from here.' + EITHER],
'decision-stale': [409, 'The role template or the organization changed since it was read. Reload the templates and propose again.' + EITHER],
'organization-refused': [409, 'The organization file refused this change. Check the role and route against the organization checks.' + EITHER],
'not-configured': [503, 'This Console\'s bus has no hire support configured. Ask the owner to check the organization bus.' + EITHER],
'organization-busy': [409, 'The organization is busy with another write.' + CHECK],
'fire-in-progress': [409, 'A fire of this agent is already in progress.' + CHECK],
'instance-fired': [409, 'This agent was fired.' + CHECK],
'copy-unavailable': [409, 'This copy is not available for credentials.' + CHECK],
'copy-refused': [409, 'The copy refused this credential change.' + CHECK],
'copy-stale': [409, 'The copy changed since it was read. Reload the Agents page.' + CHECK],
'root-policy-unreadable': [503, 'The root policy could not be read.' + CHECK],
'credential-definition-unpinned': [409, 'The copy\'s role definition is not pinned, so credentials cannot be bound to it.' + CHECK],
'credential-binding-changed': [409, 'The copy\'s credential binding changed since it was read.' + CHECK],
'credential-role-changed': [409, 'The copy\'s role changed since it was read.' + CHECK],
'credential-unbound': [409, 'The copy has no credential binding to remove.' + CHECK],
'credential-revoking': [409, 'The copy\'s credential is being revoked.' + CHECK],
'credential-scope-exceeded': [409, 'The requested credential scope is wider than the role allows.' + CHECK],
// The fields passed the shape check below, so a malformed answer here is a value the organization
// checks refused: most often a model that is not a launch.models key.
'invalid-request': [400, 'The bus refused a value in this proposal. Pick a model listed under Settings > Model proxy and a role the organization checks accept.' + EITHER],
};
// A reply the endpoint cannot read as a definite answer: no answer, or the bus server's request
// codes. Anything else that reads as a code is a definite refusal.
const NO_ANSWER = new Set(['outcome-unknown', 'invalid-response', 'response-too-large', 'organization-uncertain', 'request-timeout', 'invalid-envelope', 'incomplete-request', 'revision-conflict-revoked']);
const CODE = /^[a-z][a-z-]{0,63}$/;
const definiteStart = err => err instanceof BusReadError && typeof err.code === 'string' && CODE.test(err.code) &&
!NO_ANSWER.has(err.code) && !err.code.startsWith('outcome-unknown') ? err.code : null;
// E2E6 P2-4: the start's own shape check, the structural rules prepare() applies, so a malformed
// proposal names its field and rule before anything leaves for the bus. A binding request keeps the
// bus's own check (its shape lives in copy-bindings.mjs).
const HIRE_KEYS = ['op', 'level', 'project', 'name', 'role', 'credentials', 'personality', 'route', 'reason', 'binding'];
const plain = x => x !== null && typeof x === 'object' && !Array.isArray(x);
const idOk = x => typeof x === 'string' && ID_PATTERN.test(x) && !['human', 'tester', 'user'].includes(x);
const ID_RULE = 'lowercase letters, digits, dot, dash or underscore, starting with a letter or digit, at most 64 characters, and not human, tester or user';
export function hireProblem(hire) {
if (!plain(hire)) return { field: 'hire', rule: 'must be an object' };
const extra = Object.keys(hire).find(k => !HIRE_KEYS.includes(k));
if (extra) return { field: extra, rule: 'is not a hire field' };
if ('binding' in hire) return null;
for (const k of ['op', 'level', 'name', 'reason']) if (!Object.hasOwn(hire, k)) return { field: k, rule: 'is required' };
if (!['hire', 'fire', 'change'].includes(hire.op)) return { field: 'op', rule: 'must be hire, fire or change' };
if (!['org', 'project', 'root'].includes(hire.level)) return { field: 'level', rule: 'must be org, project or root' };
if (!idOk(hire.name)) return { field: 'name', rule: `must be ${ID_RULE}` };
if (typeof hire.reason !== 'string' || !hire.reason.trim()) return { field: 'reason', rule: 'must not be blank' };
if (hire.reason.length > 500) return { field: 'reason', rule: 'must be at most 500 characters' };
if (hire.level === 'project' && !idOk(hire.project)) return { field: 'project', rule: 'must name a code project at project level' };
if (hire.level !== 'project' && 'project' in hire) return { field: 'project', rule: 'is only for project level' };
if (hire.op === 'fire') {
const k = ['role', 'route', 'credentials', 'personality'].find(x => x in hire);
return k ? { field: k, rule: 'is not used by fire' } : null;
}
const route = hire.route;
if (!plain(route)) return { field: 'route', rule: 'must name a harness and a model' };
const rk = Object.keys(route).find(k => !['harness', 'model', 'thinking'].includes(k));
if (rk) return { field: `route.${rk}`, rule: 'is not a route field' };
for (const k of ['harness', 'model']) if (!Object.hasOwn(route, k)) return { field: `route.${k}`, rule: 'is required' };
for (const [k, v] of Object.entries(route)) if (typeof v !== 'string' || !v || v.length > 200) return { field: `route.${k}`, rule: 'must be text of 1 to 200 characters' };
const role = hire.role;
if (!plain(role)) return { field: 'role', rule: 'must name a role template' };
const kinds = ['template', 'definition', 'inline'].filter(k => k in role);
if (kinds.length !== 1) return { field: 'role', rule: 'must name exactly one of a template, a definition or an inline role' };
const ok = { template: ['template', 'sha256'], definition: ['definition', 'rev'], inline: ['inline'] }[kinds[0]];
const rx = Object.keys(role).find(k => !ok.includes(k));
if (rx) return { field: `role.${rx}`, rule: `is not used with role.${kinds[0]}` };
if (kinds[0] === 'template') {
if (!idOk(role.template)) return { field: 'role.template', rule: `must be ${ID_RULE}` };
if (!DIGEST.test(role.sha256 ?? '')) return { field: 'role.sha256', rule: 'must be the template\'s 64-character digest; read the templates again' };
}
if (kinds[0] === 'definition') {
if (!idOk(role.definition)) return { field: 'role.definition', rule: `must be ${ID_RULE}` };
if (!Number.isSafeInteger(role.rev)) return { field: 'role.rev', rule: 'must be a whole number' };
}
return null;
}
// #1873 CB5: the owner's next step once a copy credential binding decision is approved, fixed text
// (design section 6 and its three steps). Nothing in it comes from the decision, the bus or a token.
export const BINDING_NEXT = Object.freeze({
bind: "Binding approved. Issue the copy's token with mosaic credentials issue, then restart the organization bus. The copy's status on the Agents grid names each next step.",
unbind: "Unbind approved. The bus stops the copy's runs and revokes its credential. The copy's status on the Agents grid shows when the revoke finishes.",
});
// A binding decision's preview names its copy and whether it binds or unbinds. Only those two leave
// this server: the copy id, incarnation, definition pin and services stay with the bus. The owner's
// preview verb is the one bus answer that carries them; the inbox and the trail send the same two.
const bindingOf = (v) => (v?.binding && typeof v.binding === 'object' && subjectOk(v.binding.instance) &&
(v.binding.after === null || (typeof v.binding.after === 'object' && !Array.isArray(v.binding.after)))
? { instance: v.binding.instance, change: v.binding.after === null ? 'unbind' : 'bind' } : null);
const BODY_MAX = 4096;
const TOKEN = /^[a-f0-9]{64}$/;
const DIGEST = /^[a-f0-9]{64}$/;
@@ -144,19 +253,37 @@ export async function decisionHTTP(req, res, { path, decide, session, live, conf
if(step==='start' || step==='templates') {
const allowed=step==='start'?['business','hire']:['business'];
if(id!=='hire' || Object.keys(value).length!==allowed.length || Object.keys(value).some(k=>!allowed.includes(k)) || !subjectOk(value.business))return refuse('invalid-request');
if(step==='start') {
const problem=hireProblem(value.hire);
if(problem) {
const label=problem.field[0].toUpperCase()+problem.field.slice(1);
line(`error=invalid-request field=${problem.field}`);
return json(res,400,{error:'invalid-request',field:problem.field,rule:problem.rule,message:`${label} ${problem.rule}. Nothing was sent to the bus.`});
}
}
if(live()!==session)return refuse('sign-in-required');
try {
const result=await decide.call(step==='start'?'hire.start.console':'hire.templates.console',step==='start'?value.hire:{},{subject,business:value.business});
if(step==='start' && (typeof result?.id!=='string'||result.op!=='resolved'||result.choice!=='yes'))return refuse('outcome-unknown');
if(step==='templates' && !Array.isArray(result?.templates))return refuse('confirmation-unavailable');
line(step==='start'?'hire=recorded':'templates=read');
return json(res,200,step==='start'?{id:result.id,op:result.op,choice:result.choice}:result);
// CB5: an approved binding start says what the owner does next, fixed text by its shape.
const change=step==='start' && value.hire?.binding && typeof value.hire.binding==='object' && 'services' in value.hire.binding
? (value.hire.binding.services===null?'unbind':'bind') : null;
return json(res,200,step==='start'?{id:result.id,op:result.op,choice:result.choice,...(change?{next:BINDING_NEXT[change]}:{})}:result);
} catch(err) {
if(step!=='start' || !(err instanceof BusReadError) || !Object.hasOwn(HIRE_START_REFUSALS,err.code))
return refuse(busCode(err,step==='start'?'outcome-unknown':'confirmation-unavailable'));
const [status,message]=HIRE_START_REFUSALS[err.code];
line(`error=${err.code}`);
return json(res,status,{error:err.code,message});
if(step!=='start')return refuse(busCode(err,'confirmation-unavailable'));
const code=definiteStart(err);
if(!code)return refuse('outcome-unknown');
if(Object.hasOwn(HIRE_START_REFUSALS,code)) {
const [status,message]=HIRE_START_REFUSALS[code];
line(`error=${code}`);
return json(res,status,{error:code,message});
}
if(Object.hasOwn(BUS_CODES,code))return refuse(BUS_CODES[code]);
// A definite refusal this page has no wording for keeps its code, with a safe next step.
line(`error=${code}`);
return json(res,409,{error:code,message:'The bus refused this proposal.'+CHECK});
}
}
if (step === 'confirmation') {
@@ -171,11 +298,12 @@ export async function decisionHTTP(req, res, { path, decide, session, live, conf
const action = ordinary ? 'answer' : value.choice === v.approve.key ? 'approve' : 'deny';
if (live() !== session) return refuse('sign-in-required');
if(value.adjust!==undefined && v.kind!=='hire')return refuse('invalid-request');
const confirmation = confirmations.issue({ session, business: v.business, decision: id, choice: value.choice, digest: v.digest, ...(v.kind==='hire'?{kind:'hire',...(value.adjust===undefined?{}:{adjust:structuredClone(value.adjust)})}:{}) });
const binding = v.kind === 'hire' ? bindingOf(v) : null;
const confirmation = confirmations.issue({ session, business: v.business, decision: id, choice: value.choice, digest: v.digest, ...(v.kind==='hire'?{kind:'hire',...(value.adjust===undefined?{}:{adjust:structuredClone(value.adjust)}),...(binding?{change:binding.change}:{})}:{}) });
if (!confirmation) return refuse('confirmation-unavailable');
line(`action=${action}`);
return json(res, 200, { id, question: typeof v.question === 'string' ? v.question : '', action, choice: value.choice, text: chosen.text,
...(v.kind==='hire'?{kind:'hire',flagged:v.flagged,credentialLines:v.credentialLines,expiresAt:v.expiresAt}:ordinary ? { kind: 'decision' } : { grant: v.grant }), confirmation });
...(v.kind==='hire'?{kind:'hire',flagged:v.flagged,credentialLines:v.credentialLines,expiresAt:v.expiresAt,...(binding?{binding}:{})}:ordinary ? { kind: 'decision' } : { grant: v.grant }), confirmation });
}
const keys = Object.keys(value);
if (keys.length !== 2 || !keys.includes('choice') || !keys.includes('confirmation') || typeof value.choice !== 'string') return refuse('invalid-request');
@@ -189,7 +317,8 @@ export async function decisionHTTP(req, res, { path, decide, session, live, conf
} catch (err) { return refuse(busCode(err, 'outcome-unknown')); }
if (r?.id !== id || r.op !== 'resolved' || r.choice !== held.choice) return refuse('outcome-unknown');
line(`choice=${JSON.stringify(held.choice.slice(0, 64))}${r.grant ? ` grant=${r.grant}` : ''}`);
return json(res, 200, { id, op: 'resolved', choice: r.choice, ...(r.grant === 'started' ? { grant: 'started' } : {}) });
return json(res, 200, { id, op: 'resolved', choice: r.choice, ...(r.grant === 'started' ? { grant: 'started' } : {}),
...(held.change && r.choice === 'yes' ? { next: BINDING_NEXT[held.change] } : {}) });
}
function json(res, status, body) {
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' });
+5 -3
View File
@@ -29,9 +29,11 @@ export function organizationConsole({ ctx, listOrgs, showOrg, resolveOrg, orgPat
result.push({ id: entry.id, label: entry.id, invalid: true, reason: publicReason(ctx, error) });
continue;
}
let busHealthy = false;
try { busHealthy = (await busStatus(ctx, entry.id)).healthy === true; } catch { /* offline metadata remains visible */ }
result.push({ id: entry.id, label: entry.id, theme: verified.shown.doc?.appearance?.theme ?? null, busHealthy });
// E2E6 P2-8: a bus with no host recorded (stopped by its owner, or never started) is "stopped",
// not a failure. A host that is recorded but does not answer stays plain unavailable.
let busHealthy = false, busStopped = false;
try { const s = await busStatus(ctx, entry.id); busHealthy = s?.healthy === true; busStopped = !busHealthy && s?.pid == null; } catch { /* offline metadata remains visible */ }
result.push({ id: entry.id, label: entry.id, theme: verified.shown.doc?.appearance?.theme ?? null, busHealthy, ...(busStopped ? { busStopped: true } : {}) });
}
return result;
}
+41 -7
View File
@@ -9,12 +9,22 @@
// POST /api/orgs/<id>/board {revision, board, project?}: board.set.console (board null removes it)
// POST /api/orgs/<id>/workflows/<name> {revision, workflow, project?}: workflow.set.console
// DELETE /api/orgs/<id>/workflows/<name> {revision, project?}: workflow.set.console with workflow null
// GET /api/orgs/<id>/workflows {workflows, projects, roles, revision, writable} (SP6): the org's and
// each code project's workflows, the role names a step may resolve to
// now (org definitions and V2 templates; `human` always does), and the
// file revision a workflow write sends back
//
// `read` is the owner's Console call (consoleCall), used for the two verbs that write nothing. `write`
// is the owner's Console call fenced to the three write verbs (packsCaller), present only for the
// owner's own session under --send. The broker answers every .console verb to the business human's
// console cap alone, and runs the merge, the digest and the held-credential check itself.
export const PACKS_ROUTE = /^\/api\/(?:packs(?:\/(preview|apply))?|board|workflows\/([^/]+))$/;
// console cap alone, and runs the merge, the digest and the held-credential check itself. `workflows`
// (SP6) is the owner's read of the organization file as the writer reads it ({document, revision,
// templates}), the same read `mosaic org workflow set` takes its revision from; no bus verb answers it.
export const PACKS_ROUTE = /^\/api\/(?:packs(?:\/(preview|apply))?|board|workflows(?:\/([^/]+))?)$/;
// Which requests the pack route answers. GET and HEAD /api/board stay the Board read (the Console's own task
// board through its upstream); every other method on /api/board is this route's (POST writes, the rest 405).
// serve.mjs dispatches on this, so the HEAD pin in packs-http.test.mjs tests the predicate it uses.
export const packsServes = (path, method) => PACKS_ROUTE.test(path) && !(path === '/api/board' && (method === 'GET' || method === 'HEAD'));
export const PACK_WRITE_VERBS = Object.freeze(['pack.apply.console', 'board.set.console', 'workflow.set.console']);
export const packsCaller = call => async (verb, args, options) => {
if (!PACK_WRITE_VERBS.includes(verb)) throw new Error(`the pack writer does not call ${verb}`);
@@ -51,7 +61,7 @@ async function body(req, required, optional) {
return value;
}
export async function packsHTTP(req, res, { path, owner, read, write, session, live, origin }) {
export async function packsHTTP(req, res, { path, owner, read, write, workflows = null, session, live, origin }) {
const refuse = (status, error) => json(res, status, { error });
const fail = error => { const code = codes[error?.code] ? error.code : 'outcome-unknown'; return refuse(codes[code] ?? 503, code); };
const match = PACKS_ROUTE.exec(path);
@@ -61,20 +71,21 @@ export async function packsHTTP(req, res, { path, owner, read, write, session, l
try { name = decodeURIComponent(match[2]); } catch { return refuse(400, 'invalid-request'); }
if (!NAME.test(name)) return refuse(400, 'invalid-request');
}
const list = path === '/api/packs';
const allowed = list ? ['GET'] : name !== null ? ['POST', 'DELETE'] : ['POST'];
const list = path === '/api/packs', flows = path === '/api/workflows';
const allowed = list || flows ? ['GET'] : name !== null ? ['POST', 'DELETE'] : ['POST'];
if (!allowed.includes(req.method)) return refuse(405, 'method-not-allowed');
// A method the route never serves answers 405 first; every served method is the root owner's.
if (!owner) return refuse(403, 'owner-only');
if (!session) return refuse(401, 'sign-in-required');
if (!read) return refuse(503, 'not-configured');
const writes = !list && step !== 'preview';
if (flows ? !workflows : !read) return refuse(503, 'not-configured');
const writes = !list && !flows && step !== 'preview';
if (writes && !write) return refuse(403, 'read-only');
const site = one(req, 'sec-fetch-site'), from = one(req, 'origin');
if (req.method !== 'GET' ? from !== origin || site !== 'same-origin'
: (req.headersDistinct.origin && from !== origin) || (req.headersDistinct['sec-fetch-site'] && !['same-origin', 'none'].includes(site)))
return refuse(403, 'cross-origin');
if (flows) return workflowsRead(res, { workflows, live, session, writable: Boolean(write) });
let verb, args = {};
if (list) verb = 'packs.console';
else if (step !== null) {
@@ -124,3 +135,26 @@ export async function packsHTTP(req, res, { path, owner, read, write, session, l
if (!REVISION.test(result.revision ?? '')) return refuse(503, 'outcome-unknown');
return json(res, 200, { revision: result.revision, added: Array.isArray(result.added) ? result.added : [], ...(step === 'apply' ? { pack: result.pack } : {}) });
}
// SP6: the workflow list and editor's read. Only workflows, the names a step role may resolve to and the
// revision leave; nothing else of the organization file does. A workflow stays as the file holds it, so a
// step naming a role that no longer resolves is shown as written and the page marks it.
async function workflowsRead(res, { workflows, live, session, writable }) {
const refuse = (status, error) => json(res, status, { error });
let current;
try { current = await workflows(); } catch { return refuse(503, 'org-unavailable'); }
const doc = current?.document;
if (!plain(doc) || !REVISION.test(current.revision ?? '') || !Array.isArray(current.templates)) return refuse(503, 'outcome-unknown');
// A sign-out while the file was read shows nothing.
if (live() !== session) return refuse(401, 'sign-in-required');
const flowsOf = scope => (plain(scope?.workflows) ? scope.workflows : {});
const projects = {};
for (const [id, entry] of Object.entries(plain(doc.projects) ? doc.projects : {})) {
if (plain(entry) && entry.kind !== 'library') projects[id] = flowsOf(entry);
}
const roles = {
definitions: Object.keys(plain(doc.definitions) ? doc.definitions : {}).sort(),
templates: current.templates.filter(n => typeof n === 'string').sort(),
};
return json(res, 200, { workflows: flowsOf(doc), projects, roles, revision: current.revision, writable });
}
+22 -9
View File
@@ -22,19 +22,25 @@
const r=await fetch(path(org,step),body===undefined?{cache:'no-store'}:{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(body)});
const value=await r.json();if(!r.ok)throw Error(guidance[value.error]??`The bus refused: ${value.error??'outcome unknown'}. Refresh before retrying.`);return value;
}
async function load(org) {try{states.set(org,await call(org,''));}catch(error){states.set(org,{error:error.message});}}
// E2E6 P1-1: a poll reloads this state; the last save's receipt and the open review list stay.
// A failed poll keeps the last good read only while the page holds the form (dirty, or a field has
// focus), so the held form is not dropped (review P3). A clean form shows the read error (re-check P3).
const held=org=>{const form=[...document.querySelectorAll('[data-autonomy-org]')].find(n=>n.dataset.autonomyOrg===org)?.querySelector('[data-autonomy-form]');return !!form&&(form.hasAttribute('data-live-dirty')||form.contains(document.activeElement));};
async function load(org) {const prev=states.get(org),keep={status:prev?.status??'',reviewOpen:prev?.reviewOpen===true};try{states.set(org,{...await call(org,''),...keep});}catch(error){states.set(org,prev&&!prev.error&&held(org)?{...prev,...keep}:{error:error.message,...keep});}}
function section(org) {
const s=states.get(org);
if(!s || s.error)return `<section class="panel" data-autonomy><h3>Autonomy</h3><p>${esc(s?.error??'Autonomy could not be read.')}</p></section>`;
const v=s.autonomy??{};
return `<section class="panel" data-autonomy data-autonomy-org="${esc(org)}"><h3>Autonomy</h3><p>Choose how closely you follow each class of decision. Hard waits, flagged grants and credentials always need a person.</p><form data-autonomy-form>
const v=s.autonomy??{},fk=k=>` data-focus-key="${esc(`autonomy-${k}:${org}`)}"`;
// The form carries the revision it was drawn from: a form a poll kept (workspace holdLive) saves
// against that revision, so it cannot overwrite a change it never showed.
return `<section class="panel" data-autonomy data-autonomy-org="${esc(org)}" data-live-key="${esc(`autonomy:${org}`)}"><h3>Autonomy</h3><p>Choose how closely you follow each class of decision. Hard waits, flagged grants and credentials always need a person.</p><form data-autonomy-form data-revision="${esc(JSON.stringify(s.revision??null))}">
<div class="autonomy-fields">${classes.map(k=>`<label>${labels[k]}<select name="${k}"${s.writable?'':' disabled'}>${levels.map(l=>`<option${s.levels[k]===l?' selected':''}>${l}</option>`).join('')}</select></label>`).join('')}</div>
<div class="autonomy-fields"><label>Reverse window (hours)<input type="number" name="reverseWindowHours" min="1" max="168" value="${v.reverseWindowHours??24}"${s.writable?'':' disabled'}></label><label>Authority resolver<input name="authorityResolver" maxlength="64" value="${esc(v.authorityResolver??'')}" autocomplete="off"${s.writable?'':' disabled'}></label></div>
<label class="autonomy-checkbox"><input name="notices" type="checkbox"${v.notices===true?' checked':''}${s.writable?'':' disabled'}> Console notices for decisions near the loop</label>
<label class="autonomy-checkbox"><input name="notices" type="checkbox"${fk('notices')}${v.notices===true?' checked':''}${s.writable?'':' disabled'}> Console notices for decisions near the loop</label>
<p>In the loop, the arbiter resolves and you confirm before work takes effect. Near the loop, work takes effect and you can reverse it within the window. Out of the loop, the trail records it without a notice.</p>
${s.writable?'<button class="btn" type="submit">Save autonomy</button>':'<p>Only the owner can change autonomy.</p>'}
<p data-autonomy-status role="status"></p></form>
${s.writable?`<details><summary>Review reversals before moving out of the loop</summary><p>Read the decision trail and its reversals before recording your review.</p>${classes.map(k=>`<button class="btn" type="button" data-autonomy-review="${k}">Record review: ${labels[k]}</button>`).join('')}</details>`:''}</section>`;
${s.writable?`<p><button class="btn" type="submit"${fk('save')}>Save autonomy</button> <button class="btn" type="button" data-autonomy-discard${fk('discard')}>Discard changes</button></p>`:'<p>Only the owner can change autonomy.</p>'}
<p data-autonomy-status role="status">${esc(s.status)}</p></form>
${s.writable?`<details data-autonomy-review-list${s.reviewOpen?' open':''}><summary${fk('review-list')}>Review reversals before moving out of the loop</summary><p>Read the decision trail and its reversals before recording your review.</p>${classes.map(k=>`<button class="btn" type="button" data-autonomy-review="${k}"${fk(`review-${k}`)}>Record review: ${labels[k]}</button>`).join('')}</details>`:''}</section>`;
}
function notice(d) {
if(!d?.autonomy)return '';
@@ -49,9 +55,16 @@
const autonomy=Object.fromEntries(classes.map(k=>[k,fields.get(k)]));autonomy.notices=fields.has('notices');autonomy.reverseWindowHours=Number(fields.get('reverseWindowHours'));
if(fields.get('authorityResolver'))autonomy.authorityResolver=fields.get('authorityResolver');
const button=form.querySelector('button[type="submit"]'),status=form.querySelector('[data-autonomy-status]');button.disabled=true;
try{const r=await call(org,'',{revision:s.revision,autonomy});s.revision=r.revision;s.autonomy=autonomy;s.levels=Object.fromEntries(classes.map(k=>[k,autonomy[k]]));status.textContent=`Saved. Receipt ${r.transaction}.`;}
catch(error){status.textContent=error.message;}finally{button.disabled=false;}
let base=s.revision;try{if(form.dataset.revision)base=JSON.parse(form.dataset.revision);}catch{/* drawn without one */}
try{const r=await call(org,'',{revision:base,autonomy});s.revision=r.revision;s.autonomy=autonomy;s.levels=Object.fromEntries(classes.map(k=>[k,autonomy[k]]));s.status=`Saved. Receipt ${r.transaction}.`;form.dataset.revision=JSON.stringify(r.revision);delete form.dataset.liveDirty;status.textContent=s.status;}
catch(error){s.status=error.message;status.textContent=s.status;}finally{button.disabled=false;}
});
// An edited form is dirty until it saves: holdLive keeps a dirty form through every poll.
const dirty=e=>{const form=e.target.closest?.('[data-autonomy-form]');if(form)form.dataset.liveDirty='1';};
document.addEventListener('input',dirty);document.addEventListener('change',dirty);
// Review P3: Discard drops the held form and draws the last read in its place.
document.addEventListener('click',e=>{const b=e.target.closest?.('[data-autonomy-discard]');const el=b?.closest('[data-autonomy-org]');if(!el||!states.get(el.dataset.autonomyOrg))return;const org=el.dataset.autonomyOrg;el.outerHTML=section(org);[...document.querySelectorAll('[data-autonomy-org]')].find(n=>n.dataset.autonomyOrg===org)?.querySelector('[data-autonomy-discard]')?.focus();});
document.addEventListener('toggle',e=>{if(!e.target.matches?.('[data-autonomy-review-list]'))return;const s=states.get(e.target.closest('[data-autonomy-org]')?.dataset.autonomyOrg);if(s)s.reviewOpen=e.target.open;},true);
document.addEventListener('click',async e=>{
const button=e.target.closest('[data-autonomy-confirm],[data-autonomy-reverse],[data-autonomy-review]');if(!button)return;
const section=button.closest('[data-autonomy-org]'),org=section.dataset.autonomyOrg,status=section.querySelector('[data-autonomy-status]');
Loaded 100 of 145 files, more files were not shown because too many files have changed in this diff. Show more