Compare commits

...
5 Commits
Author SHA1 Message Date
jason.woltje 666f4d6864 fix(cli): build review fixes for the parity verbs (#1873, P1-3)
- autonomy set: an empty --authority-resolver clears the resolver as the
  panel does; --reverse-window-hours takes 1 to 168, as the page and bus do.
- The default instance gets a one-line refusal (exit 3) naming the named
  instance and --dev-local-owner, not a stack trace.
- tasks create|edit|archive: a value starting with -- is a usage error;
  --business is checked before the agent guard, and the guard also reads
  process.env.
- Drop the unused consoleRefusal export.
- Docs: --business is required, resolver clearing, window range, and the
  named-instance requirement.
2026-11-11 20:47:35 -06:00
jason.woltje 226e28d1e6 fix(cli): autonomy set takes the bus's 64-hex revision (#1873, P1-3)
The panel's revision is the bus's sha256 of the organization, so a
whole-number --revision could never match a real Console. The fake in
cli-parity now answers hex revisions, and a whole number is a usage error.
2026-11-11 20:47:35 -06:00
jason.woltje 550bd084d0 feat(cli): hire, autonomy and task-board verbs for WebUI parity (#1873, P1-3)
mosaic org role-templates|hire|change|fire and mosaic org autonomy
[set|review] send the hire form's and the autonomy panel's own requests
to the running Console's org route, so the Console runs the same owner,
send, origin, body and revision checks and the same bus verbs. They
refuse inside an agent run after their usage checks.

mosaic tasks create|edit|archive pass the board's parseTaskWrite and go
to the bus as the person (tasks.create|edit|archive).

Docs: cli README, docs/TOOLS.md, webui README Console actions table.
2026-11-11 20:47:34 -06:00
jason.woltje 8827905ba0 fix(org): restore names bus recover for a stale lock; receipt taken at mount; restore-then-dashboard test (#1873)
Build review P3-1, P3-3 and P3-4.
2026-11-11 20:14:46 -06:00
jason.woltje a559957085 feat(org): restore starts the bus; pages follow restart and quick setup (#1873)
P1-4: org restore starts the bus as mosaic bus start ID --dev-local-owner
--detach would, and its receipt names the bus. Where that start is refused
(deployed or stamped instance, agent run) restore refuses before changing
anything; --no-bus keeps the old restore. The Console Restore action
restores and starts the bus with one receipt, and offers Restart bus when
the start fails after a good restore.

P1-5: Quick Setup refreshes the sidebar organization list, and the
organization dashboard redraws on the config event Restart bus already
dispatches, so the bus line and System types follow without a reload.
2026-11-11 20:03:35 -06:00
18 changed files with 1255 additions and 31 deletions

No files matched your search

+12 -1
View File
@@ -313,6 +313,15 @@ scripts/mosaic bus start <business> [--pm | --no-launch] [--dev-local-owner] [--
scripts/mosaic console start [--dev-local-owner [--dev-insecure-bind ADDR --dev-allow-host NAME[,NAME...]]] [--detach] [--port auto|N]
scripts/mosaic console stop | status | restart
scripts/mosaic org reload <org>
scripts/mosaic org role-templates <org> [--json]
scripts/mosaic org hire|change <org> --name <name> --template <template> --harness <harness> --model <model> [--thinking <level>] [--level org|project|root] [--project <project>] --reason <text> [--json]
scripts/mosaic org fire <org> --name <name> [--level org|project|root] [--project <project>] --reason <text> [--json]
scripts/mosaic org autonomy <org> [--json]
scripts/mosaic org autonomy <org> set --revision <rev> [--cross-role|--merge|--policy|--authority in|near|out] [--notices on|off] [--reverse-window-hours 1-168] [--authority-resolver <name>|""] [--json]
scripts/mosaic org autonomy <org> review cross-role|merge|policy|authority [--json]
scripts/mosaic tasks create --title <text> --requirement <REQ-X-N> [--project <name>] [--state <state>] [--assignees <role,...>] [--priority 0-5] [--due <iso>|none] --business <id> [--json]
scripts/mosaic tasks edit task:<n> --expect <digest> [--title ...] [--state ...] [--assignees ...] [--priority ...] [--due ...] --business <id> [--json]
scripts/mosaic tasks archive task:<n> --expect <digest> --business <id> [--json]
scripts/mosaic talk <role> <text> [--wait SECONDS] [--business <id>]
scripts/mosaic project create <name> [--repo <repo>] | list [--json]
scripts/mosaic threads [--project <project>] [--json] [--tester]
@@ -335,7 +344,9 @@ For local development on a named instance only, `mosaic bus start BUSINESS --dev
`mosaic console restart` and `mosaic org reload ORG` are the Console's Settings controls (F3) from a terminal (U1, #1873). Each sends one request to the running Console named in the instance's console state, as the page sends it (the Console's own Host and Origin, `Sec-Fetch-Site: same-origin`, a JSON `{}` body), so the Console applies the same owner, `--send` and origin checks and writes the same action trail, `<dataRoot>/console/actions.jsonl`. Both refuse inside an agent run (exit 3) before reading anything, and refuse with exit 3 when no Console runs. `console restart` works only for a Console started in the foreground by `mosaic console start`, or by `mosaic console start --dev-local-owner --detach`, which leaves a detached supervisor (`packages/cli/src/console-supervisor.mjs`, its own session) running the Console as the foreground start does; its receipt adds the supervisor's `supervisor` pid, and its later warnings go to `console/supervisor-*.log` under the data root. `mosaic console stop` stops that Console, and its supervisor then exits. `mosaic console start` refuses inside an agent run (exit 3), with or without `--dev-local-owner` or `--detach`, checking the starting process's own environment as well as the one given for the Console (agent-guard, #1873). A detached start whose supervisor does not reply within 60 seconds signals the supervisor's process group, then kills it, and clears `console/start.lock` only when the lock names that supervisor (pid and start time) and it is gone. Any other Console answers `not-supervised`. `console restart` waits up to 30 seconds for the relaunched Console to answer and prints `{"outcome":"restarted","from":OLD,"boot":NEW}`. `org reload ORG` restarts a running bus or starts a stopped one and prints the Console's done receipt. A request that left but got no answer is reported as outcome unknown (exit 1) and is never retried; check `mosaic console status`, `mosaic bus status ORG` and the trail. `mosaic bus restart ORG` remains the trail-less host verb.
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. 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.
The hire form and the autonomy panel from a terminal (E2, #1873, P1-3). `mosaic org role-templates|hire|fire|change ORG` and `mosaic org autonomy ORG [set|review]` send the page's own request to the running Console's org route (`/api/orgs/ORG/decisions/hire/templates|start`, `/api/orgs/ORG/autonomy[/review]`), as `org reload` does, so the Console applies the same owner, `--send`, origin, body and live-session checks and the same bus verbs run (`hire.templates.console`, `hire.start.console`, `autonomy.get.console`, `autonomy.set.console`, `autonomy.review.console`). `hire` and `change` read the templates first and send the chosen template's name and sha256, as the form does. `autonomy set` needs the 64-hex `--revision` from `mosaic org autonomy ORG` and sends every setting the panel sends, with only the named ones changed; a stale revision is refused `revision-conflict`; an empty `--authority-resolver ""` clears the resolver, as emptying the panel's field does. These verbs need a named instance (`MOSAIC_CONFIG` set to `~/.config/mosaic-NAME/config.json`) whose Console runs with `--dev-local-owner`; the default instance is refused with exit 3. A Console refusal exits 3 with the Console's code and message; `outcome-unknown` exits 1; an unknown template exits 2. Every one of these verbs, reads included, refuses inside an agent run. `mosaic tasks create|edit|archive` are the task board's three writes: the arguments pass the board's own check (`workspace-tasks.mjs parseTaskWrite`) and go to the bus as the person (`tasks.create|edit|archive`, human transport), so the bus runs its human, field, digest and archive checks. A stale `--expect` is refused `task-conflict`; read the digest with `mosaic tasks --json`.
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.
The instance processing switch is separate from per-business launches off/on.
`mosaic halt` durably stops managed processing across every business in the
+48 -1
View File
@@ -28,6 +28,9 @@ mosaic inbox [--business <id>] [--json]
mosaic inbox --read <id>... | --read-all [--business <id>]
mosaic decide <decision> <option> [--note <text>] [--yes] [--business <id>]
mosaic tasks [--business <id>] [--json]
mosaic tasks create --title <text> --requirement <REQ-X-N> [--project <name>] [--state <state>] [--assignees <role,...>] [--priority 0-5] [--due <iso>|none] --business <id> [--json]
mosaic tasks edit task:<n> --expect <digest> [--title <text>] [--state <state>] [--assignees <role,...>] [--priority 0-5] [--due <iso>|none] --business <id> [--json]
mosaic tasks archive task:<n> --expect <digest> --business <id> [--json]
mosaic agents [--business <id>] [--json] [--tester]
mosaic trail <task|decision> [--business <id>] [--json]
mosaic talk <role> <text> [--wait <seconds> | --follow] [--business <id>]
@@ -1299,7 +1302,51 @@ require the source and deleted folder to be on the same filesystem.
`mosaic org archive` and `mosaic org delete` stop the organization bus, so like
`bus stop` they refuse inside an agent run (exit 3, agent-guard #1873), after
their usage checks and before any config is read. `org restore` is unchanged.
their usage checks and before any config is read.
`mosaic org restore ID --if-revision N` makes the organization active and starts
its bus, as `mosaic bus start ID --dev-local-owner --detach` does (E1, P1-4). It
makes that verb's checks before the restore, so a stamped build or an instance
that refuses `--dev-local-owner` refuses the restore before anything changes
(exit 3). Starting a bus is a human action, so inside an agent run `org restore`
refuses like `bus start` (exit 3) before any config is read. The receipt's `bus`
names the outcome: `started` with the host pid, or `failed` with the message and
the verb that starts it (after `mosaic bus recover ID` when a host that died left a
stale lock). A bus that does not start leaves the restore in place;
the command prints the receipt and exits 3. `org restore ID --if-revision N
--no-bus` restores without starting the bus, as before, and stays open to agents.
The hire form and the autonomy panel from a terminal (E2, #1873, P1-3):
```
mosaic org role-templates ORG [--json]
mosaic org hire|change ORG --name NAME --template TEMPLATE --harness H --model M [--thinking T] [--level org|project|root] [--project P] --reason TEXT [--json]
mosaic org fire ORG --name NAME [--level org|project|root] [--project P] --reason TEXT [--json]
mosaic org autonomy ORG [--json]
mosaic org autonomy ORG set --revision REV [--cross-role|--merge|--policy|--authority in|near|out] [--notices on|off] [--reverse-window-hours 1-168] [--authority-resolver NAME|""] [--json]
mosaic org autonomy ORG review cross-role|merge|policy|authority [--json]
```
Each sends the page's own request to the running Console's org route, as
`org reload` does, so the Console runs the same owner, `--send`, origin, body
and session checks and the same bus verbs. `hire` and `change` read the role
templates first and send the chosen template's name and sha256. `autonomy set`
needs the 64-hex `--revision` that `mosaic org autonomy ORG` prints and sends every
setting the panel sends, changing only the named ones; a stale revision is
refused `revision-conflict`. `--authority-resolver ""` clears the resolver, as
emptying the panel's field does, and `--reverse-window-hours` takes 1 to 168.
These verbs need a named instance whose Console runs with `--dev-local-owner`;
the default instance is refused with exit 3. A Console refusal exits 3 with its code and
message, an answer that never came exits 1, an unknown template exits 2. All of
these verbs, reads included, refuse inside an agent run (exit 3) after their
usage checks (exit 4) and before any config is read.
`mosaic tasks create|edit|archive` are the task board's three writes. The
arguments pass the board's own check, then go to the bus as the person
(`tasks.create`, `tasks.edit`, `tasks.archive`), so the bus runs its human,
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
+4
View File
@@ -55,6 +55,7 @@ import { agentsRootCommand, AGENTS_ROOT_USAGE, projectAgentsCommand, PROJECT_AGE
import { ORG_CONFIG, runConfigCommand } from "../../organization/src/config-command.mjs";
import { devCommand, DEV_USAGE, fixtureLaunchEnv, fixtureLauncherOptions } from "./dev-fixture.mjs";
import { runCommand, RUN_USAGE } from "./run-evidence.mjs";
import { tasksWriteCommand, TASKS_WRITE_OPS, TASKS_WRITE_USAGE } from "./tasks-write.mjs";
export const USAGE = `usage:
${ACCOUNTS_USAGE}
@@ -82,6 +83,7 @@ export const USAGE = `usage:
mosaic inbox --read <id>... | --read-all [--business <id>]
mosaic decide <decision> <option> [--note <text>] [--yes] [--business <id>]
mosaic tasks [--business <id>] [--json] [--tester]
${TASKS_WRITE_USAGE}
mosaic agents [--business <id>] [--json] [--tester]
mosaic trail <task|decision> [--business <id>] [--json] [--tester]
mosaic talk <role> <text> [--wait <seconds> | --follow] [--business <id>] [--tester]
@@ -771,6 +773,8 @@ export async function main(argv, io = { env: process.env, stdin: process.stdin,
// A2 (#1873): the owner's root and project agent levels. `mosaic agents` without `root` stays the bus read.
if (verb === "agents" && rest[0] === "root") return agentsRootCommand(rest.slice(1), io);
if (verb === "project" && rest[0] === "agents") return projectAgentsCommand(rest.slice(1), io);
// E2 (#1873, P1-3): the task board's create, edit and archive, as the person (tasks-write.mjs).
if (verb === "tasks" && TASKS_WRITE_OPS.includes(rest[0])) return tasksWriteCommand(rest, io, (p) => callFor(p, io, deps));
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);
+22 -5
View File
@@ -318,9 +318,10 @@ function consoleTarget(ctx) {
const address = s.bind && s.bind !== '0.0.0.0' ? s.bind : '127.0.0.1';
return { address, port, host: `${address}:${port}` };
}
function consoleCall(target, { method = 'GET', path, timeoutMs }) {
function consoleCall(target, { method = 'GET', path, timeoutMs, body = {} }) {
return new Promise((resolve, reject) => {
const headers = { host: target.host, ...(method === 'POST' ? { origin: `http://${target.host}`, 'sec-fetch-site': 'same-origin', 'content-type': 'application/json', 'content-length': 2 } : {}) };
const payload = method === 'POST' ? JSON.stringify(body) : undefined;
const headers = { host: target.host, ...(method === 'POST' ? { origin: `http://${target.host}`, 'sec-fetch-site': 'same-origin', 'content-type': 'application/json', 'content-length': Buffer.byteLength(payload) } : {}) };
let sent = false;
const req = httpRequest({ host: target.address, port: target.port, method, path, headers, timeout: timeoutMs }, res => {
let text = '';
@@ -332,14 +333,14 @@ function consoleCall(target, { method = 'GET', path, timeoutMs }) {
req.on('timeout', () => req.destroy(Object.assign(new Error('timed out'), { code: 'timeout' })));
req.on('finish', () => { sent = true; });
req.on('error', e => reject(Object.assign(e, { sent })));
req.end(method === 'POST' ? '{}' : undefined);
req.end(payload);
});
}
// A refused connection never reached the Console. Anything else after the request left may have
// changed something, so it is outcome-unknown and nothing is retried.
async function consolePost(target, path, { timeoutMs, unknown }) {
async function consolePost(target, path, { timeoutMs, unknown, body }) {
let answer;
try { answer = await consoleCall(target, { method: 'POST', path, timeoutMs }); }
try { answer = await consoleCall(target, { method: 'POST', path, timeoutMs, body }); }
catch (e) {
if (e.code === 'ECONNREFUSED' && !e.sent) fail('the Console refused the connection; see mosaic console status');
throw new CliError(`outcome unknown: ${unknown}`, 1);
@@ -374,3 +375,19 @@ export async function consoleBusRestart(ctx, id, { timeoutMs = 120000 } = {}) {
if (answer.status !== 200 || answer.body?.outcome !== 'done' || answer.body.step !== 'bus-restart') throw refusal(answer, 'org reload');
return answer.body;
}
// E2 (#1873, P1-3): the owner's org page actions from a terminal (org-owner.mjs): the hire form and
// the autonomy page, each through the running Console's own org route, sent as the page sends it.
// The route applies its owner, --send, origin, body and live-session checks and asks the org's own
// bus; nothing here decides or writes anything. A GET that fails changed nothing, so it is exit 1
// with no outcome-unknown wording; a POST that fails after it left is outcome-unknown.
export async function consoleOrgRequest(ctx, id, { method = 'GET', path, body, timeoutMs = 30000, unknown }) {
if (typeof id !== 'string' || !ORG_ID.test(id)) throw new CliError('invalid organization id', 4);
const target = consoleTarget(ctx);
const route = `/api/orgs/${id}/${path}`;
if (method === 'POST') return consolePost(target, route, { timeoutMs, unknown, body });
try { return await consoleCall(target, { path: route, timeoutMs }); }
catch (e) {
if (e.code === 'ECONNREFUSED' && !e.sent) fail('the Console refused the connection; see mosaic console status');
throw new CliError('the Console did not answer; see mosaic console status', 1);
}
}
+213
View File
@@ -0,0 +1,213 @@
// E2 (#1873, P1-3, org ruling 6): the owner's org page actions from a terminal. Each verb sends the
// request the WebUI page sends, to the running Console's own org route (org-console.mjs
// consoleOrgRequest), so the same route checks the owner, --send, the origin, the body and the live
// session, and the same bus verb runs on the organization's own bus:
// - role-templates, hire, fire, change: the inbox hire form (public/bus.js hireSubmit), the routes
// /api/orgs/ID/decisions/hire/templates and /start (decision-http.mjs), the bus verbs
// hire.templates.console and hire.start.console.
// - autonomy, autonomy set, autonomy review: the org Autonomy panel (public/autonomy.js), the
// route /api/orgs/ID/autonomy[/review] (autonomy-http.mjs), the bus verbs autonomy.get.console,
// autonomy.set.console (with its revision check) and autonomy.review.console.
// No new authority: a Console that is not the owner's, not started with --send, or not signed in
// refuses exactly as it refuses the page. Usage errors (exit 4) come first; then every verb refuses
// inside an agent run, before any config is read, as `org archive|delete` do (agent guard #1942).
import { configDir } from '../../organization/src/organization-config.mjs';
import { DEV_OWNER, devOwnerInstance } from './dev-local-owner.mjs';
import { loadSystem } from './config.mjs';
import { CliError } from './errors.mjs';
import { consoleOrgRequest } from './org-console.mjs';
import { refuseInsideAgent } from './transport.mjs';
export const ORG_OWNER_VERBS = Object.freeze(['role-templates', 'hire', 'fire', 'change', 'autonomy']);
export const ORG_OWNER_USAGE = 'mosaic org role-templates ID [--json] | hire|change ID --name NAME --template TEMPLATE --harness HARNESS --model MODEL [--thinking LEVEL] [--level org|project|root] [--project PROJECT] --reason TEXT [--json] | fire ID --name NAME [--level org|project|root] [--project PROJECT] --reason TEXT [--json] | autonomy ID [--json] | autonomy ID set --revision REV [--cross-role|--merge|--policy|--authority in|near|out] [--notices on|off] [--reverse-window-hours N] [--authority-resolver NAME|""] [--json] | autonomy ID review cross-role|merge|policy|authority [--json]';
const usage = why => new CliError(`${why}\nusage: ${ORG_OWNER_USAGE}`, 4);
const ORG_ID = /^[a-z0-9][a-z0-9._-]{0,63}$/;
const CLASSES = Object.freeze(['cross-role', 'merge', 'policy', 'authority']);
const LEVELS = Object.freeze({ in: 'in the loop', near: 'near the loop', out: 'out of the loop' });
const HIRE_VALUES = ['--name', '--template', '--harness', '--model', '--thinking', '--level', '--project', '--reason'];
const AUTONOMY_VALUES = ['--revision', ...CLASSES.map(c => `--${c}`), '--notices', '--reverse-window-hours', '--authority-resolver'];
function parse(args, values) {
const pos = [], got = {}, flags = new Set();
for (let i = 0; i < args.length; i++) {
const a = args[i];
if (a === '--json') { if (flags.has(a)) throw usage('duplicate --json'); flags.add(a); }
else if (values.includes(a)) {
if (Object.hasOwn(got, a)) throw usage(`duplicate ${a}`);
if (i + 1 >= args.length || args[i + 1].startsWith('--')) throw usage(`${a} needs one value`);
got[a] = args[++i];
} else if (a.startsWith('--')) throw usage(`unknown option ${a}`);
else pos.push(a);
}
return { pos, values: got, json: flags.has('--json') };
}
// The hire form's body (bus.js hireSubmit), from flags. The bus's proposal checks (hire-decisions.mjs
// prepare) run on it unchanged; these checks only refuse what the form itself would not send.
function hireBody(op, v) {
const need = key => { if (!v[key]?.trim()) throw usage(`${op} needs ${key}`); return v[key]; };
const level = v['--level'] ?? 'org';
if (!['org', 'project', 'root'].includes(level)) throw usage('--level takes org, project or root');
if (level === 'project' ? !v['--project']?.trim() : v['--project'] !== undefined) throw usage(level === 'project' ? '--level project needs --project' : '--project goes with --level project');
const reason = need('--reason');
if (reason.length > 500) throw usage('--reason takes at most 500 characters');
if (op === 'fire') for (const k of ['--template', '--harness', '--model', '--thinking']) if (v[k] !== undefined) throw usage(`fire takes no ${k}`);
const hire = { op, level, name: need('--name'), reason, ...(level === 'project' ? { project: v['--project'] } : {}) };
if (op === 'fire') return { hire, template: null };
const template = need('--template');
const thinking = v['--thinking']?.trim();
return { hire: { ...hire, route: { harness: need('--harness'), model: need('--model'), ...(thinking ? { thinking } : {}) } }, template };
}
function autonomyChanges(v) {
const revision = v['--revision'];
if (revision === undefined) throw usage('autonomy set needs --revision, the revision mosaic org autonomy ID shows');
// The panel's revision is the bus's 64-hex digest of the organization (autonomy.set.console).
if (!/^[a-f0-9]{64}$/.test(revision)) throw usage('--revision takes the 64-hex revision mosaic org autonomy ID shows');
const changes = {};
for (const c of CLASSES) {
const value = v[`--${c}`];
if (value === undefined) continue;
if (!Object.hasOwn(LEVELS, value)) throw usage(`--${c} takes in, near or out`);
changes[c] = LEVELS[value];
}
if (v['--notices'] !== undefined) {
if (!['on', 'off'].includes(v['--notices'])) throw usage('--notices takes on or off');
changes.notices = v['--notices'] === 'on';
}
if (v['--reverse-window-hours'] !== undefined) {
if (!/^[1-9][0-9]{0,2}$/.test(v['--reverse-window-hours']) || Number(v['--reverse-window-hours']) > 168) throw usage('--reverse-window-hours takes a whole number of hours from 1 to 168');
changes.reverseWindowHours = Number(v['--reverse-window-hours']);
}
if (v['--authority-resolver'] !== undefined) changes.authorityResolver = v['--authority-resolver'];
if (!Object.keys(changes).length) throw usage('autonomy set needs a setting to change');
return { revision, changes };
}
// The panel's save sends every class with the level it shows, notices, the reverse window (24 when
// none is set) and the resolver when one is set (autonomy.js). The terminal sends the same document
// with the named settings changed, so an unchanged field keeps the value the page would send.
export function autonomyDocument(current, changes) {
const shown = current?.autonomy ?? {};
const doc = Object.fromEntries(CLASSES.map(c => [c, current?.levels?.[c]]));
doc.notices = shown.notices === true;
doc.reverseWindowHours = shown.reverseWindowHours ?? 24;
if (shown.authorityResolver) doc.authorityResolver = shown.authorityResolver;
const out = { ...doc, ...changes };
// An empty --authority-resolver clears it: the panel leaves an emptied field out of its save.
if (out.authorityResolver === '') delete out.authorityResolver;
return out;
}
// 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',
'autonomy-precondition': 'this move needs a separate resolver, a tested reverse, owner notices and an enforced budget covering new agents for authority; out of the loop needs a near-loop decision followed by your review of its trail',
'policy-reverse-unavailable': 'policy has no near-loop setting until a reverse exists',
'authority-limits-unavailable': 'authority cannot move out of the loop',
'owner-only': 'only the owner changes autonomy',
'read-only': 'start the Console with --send to make changes',
'sign-in-required': 'sign in to the Console',
};
// A refusal is exit 3 and changed nothing. outcome-unknown (and a 5xx the route did not name) may
// have changed something, so it is exit 1 and nothing is retried.
function refused(answer, what, unknown, guidance = {}) {
const raw = answer.body?.error;
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];
return new CliError(`${what} refused (${code})${said ? `: ${said}` : ''}`, 3);
}
function ok(answer) { return answer.status === 200 && answer.body && typeof answer.body === 'object'; }
export async function orgOwnerCommand(args, io, deps = {}) {
const verb = args[0];
const autonomy = verb === 'autonomy';
const p = parse(args.slice(1), autonomy ? AUTONOMY_VALUES : verb === 'role-templates' ? [] : HIRE_VALUES);
const id = p.pos[0];
if (id === undefined) throw usage(`org ${verb} takes an organization id`);
if (!ORG_ID.test(id)) throw usage('invalid organization id');
let run;
if (verb === 'role-templates') {
if (p.pos.length !== 1) throw usage('role-templates takes one organization id');
run = async ctx => {
const answer = await consoleOrgRequest(ctx, id, { method: 'POST', path: 'decisions/hire/templates', body: { business: id }, unknown: 'the templates read did not finish; nothing was changed' });
if (!ok(answer) || !Array.isArray(answer.body.templates)) throw answer.status === 200 ? new CliError('the Console answered without templates', 1) : refused(answer, 'role-templates', 'the templates read did not finish; nothing was changed');
return { value: answer.body, text: b => b.templates.length ? b.templates.map(t => `${t.name} ${String(t.sha256 ?? '').slice(0, 12)}\n`).join('') : 'role templates: none\n' };
};
} else if (['hire', 'fire', 'change'].includes(verb)) {
if (p.pos.length !== 1) throw usage(`${verb} takes one organization id`);
const { hire, template } = hireBody(verb, p.values);
run = async ctx => {
let body = hire;
// The form chooses a template from the ones it read and sends its name and sha256.
if (template !== null) {
const read = await consoleOrgRequest(ctx, id, { method: 'POST', path: 'decisions/hire/templates', body: { business: id }, unknown: 'the templates read did not finish; nothing was changed' });
if (!ok(read) || !Array.isArray(read.body.templates)) throw refused(read, verb, 'the templates read did not finish; nothing was changed');
const chosen = read.body.templates.find(t => t?.name === template);
if (!chosen) throw new CliError(`${verb}: no role template ${JSON.stringify(template)}; see mosaic org role-templates ${id}`, 2);
const { route, ...rest } = hire;
body = { ...rest, role: { template: chosen.name, sha256: chosen.sha256 }, route };
}
const unknown = `the ${verb} may have been recorded; check mosaic inbox --business ${id} and mosaic trail before trying again`;
const answer = await consoleOrgRequest(ctx, id, { method: 'POST', path: 'decisions/hire/start', body: { business: id, hire: body }, timeoutMs: 120000, unknown });
if (!ok(answer)) throw refused(answer, verb, unknown);
if (typeof answer.body.id !== 'string' || answer.body.op !== 'resolved') throw new CliError(`outcome unknown: ${unknown}`, 1);
return { value: answer.body, text: b => `recorded decision ${b.id}: ${verb} ${hire.name} (${b.op} ${b.choice})\n` };
};
} else if (autonomy) {
const step = p.pos[1] ?? null;
if (step === null) {
if (p.pos.length !== 1 || Object.keys(p.values).length) throw usage('autonomy ID reads; use autonomy ID set to change it');
run = async ctx => {
const answer = await consoleOrgRequest(ctx, id, { path: 'autonomy' });
if (!ok(answer)) throw refused(answer, 'autonomy', 'the autonomy read did not finish; nothing was changed', AUTONOMY_GUIDANCE);
return { value: answer.body, text: formatAutonomy };
};
} else if (step === 'set') {
if (p.pos.length !== 2) throw usage('autonomy ID set takes options only');
const { revision, changes } = autonomyChanges(p.values);
run = async ctx => {
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);
return { value: answer.body, text: b => `saved autonomy for ${id}: revision ${b.revision ?? 'unknown'}${b.transaction ? `, receipt ${b.transaction}` : ''}\n` };
};
} else if (step === 'review') {
if (p.pos.length !== 3 || !CLASSES.includes(p.pos[2])) throw usage(`autonomy ID review takes one class: ${CLASSES.join(', ')}`);
if (Object.keys(p.values).length) throw usage('autonomy ID review takes no options but --json');
run = async ctx => {
const unknown = `the review may have been recorded; check mosaic trail before trying again`;
const answer = await consoleOrgRequest(ctx, id, { method: 'POST', path: 'autonomy/review', body: { decisionClass: p.pos[2] }, unknown });
if (!ok(answer)) throw refused(answer, 'autonomy review', unknown, AUTONOMY_GUIDANCE);
return { value: answer.body, text: () => `recorded your trail review for ${p.pos[2]} on ${id}\n` };
};
} else throw usage(`autonomy takes set or review, not ${JSON.stringify(step)}`);
} else throw usage(`unknown org verb ${verb}`);
// agent-guard (#1873): after the usage checks, before any config is read; this process's own
// environment as well as io.env, as org archive|delete check.
refuseInsideAgent(process.env);
refuseInsideAgent(io.env);
let instance;
try { instance = devOwnerInstance(io.env); }
catch (e) { throw new CliError(`${e.message}; these verbs need a named instance whose Console runs with --dev-local-owner`, 3); }
const system = deps.system ?? loadSystem({ env: io.env, instance });
const ctx = { configDir: configDir(io.env), dataRoot: system.dataRoot, instance, principal: DEV_OWNER };
const { value, text } = await run(ctx);
io.stdout.write(p.json ? `${JSON.stringify(value, null, 2)}\n` : text(value));
}
function formatAutonomy(b) {
const a = b.autonomy ?? {};
return [
`revision ${b.revision}${b.writable ? '' : ' (read-only: this Console takes no changes)'}`,
...CLASSES.map(c => `${c}: ${b.levels?.[c] ?? 'unknown'}`),
`notices: ${a.notices === true ? 'on' : 'off'}`,
`reverse window: ${a.reverseWindowHours === undefined ? 'not set (the page offers 24 hours)' : `${a.reverseWindowHours} hours`}`,
`authority resolver: ${a.authorityResolver ?? 'none'}`,
].join('\n') + '\n';
}
+35 -6
View File
@@ -6,23 +6,35 @@ import { installedSystemTypes } from '../../plugins/src/system-types.mjs';
import { DEV_OWNER, devOwnerInstance } from './dev-local-owner.mjs';
import { loadSystem } from './config.mjs';
import { CliError } from './errors.mjs';
import { busStopForDelete } from './org-bus.mjs';
import { busStart, busStopForDelete } from './org-bus.mjs';
import { assertBuildStart, assertDevInstance } from './deployment.mjs';
import { consoleBusRestart } from './org-console.mjs';
import { refuseInsideAgent } from './transport.mjs';
import { definitionsCommand, DEFINITIONS_USAGE } from './org-definitions.mjs';
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 [--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] | ' + DEFINITIONS_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'].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 }; }
import { orgOwnerCommand, ORG_OWNER_USAGE, ORG_OWNER_VERBS } from './org-owner.mjs';
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] | ' + DEFINITIONS_USAGE.slice('mosaic org '.length) + ' | ' + ORG_OWNER_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';
async function document(file) { try { return JSON.parse(await readFile(file, 'utf8')); } catch (e) { throw new CliError(`cannot read organization document: ${e instanceof SyntaxError ? 'not valid JSON' : errorCode(e)}`, 2); } }
// agent-guard (#1873): archive and delete stop the organization's bus, so an agent run is refused
// as it is for `mosaic bus stop`: after the usage checks, before any config is read, and checking
// this process's own environment as well as io.env. Restore starts nothing and stays as it was.
// this process's own environment as well as io.env. Restore starts the organization's bus (E1, P1-4),
// so it is refused the same way unless --no-bus is given; `restore --no-bus` starts nothing.
function refuseAgentRun(io) { refuseInsideAgent(process.env); refuseInsideAgent(io.env); }
// E1 (P1-4): the bus a restore starts is the one `mosaic bus start ID --dev-local-owner --detach`
// starts, after that verb's checks. The checks run before the restore, so a refusal changes nothing.
function restoreBusCheck(system, env) {
try { assertBuildStart({ devOwner: true }); assertDevInstance(system, env); }
catch (e) { throw new CliError(`restore starts the bus as the local dev owner, and ${e.message}; restore with --no-bus, then start the bus with mosaic bus start`, 3); }
}
const busStartVerb = id => `mosaic bus start ${id} --dev-local-owner --detach`;
export async function orgCommand(args, io, deps = {}) {
try {
// #1873 T1: the definitions verbs take their own options.
if (args[0] === 'definitions') return await definitionsCommand(args.slice(1), 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);
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]; };
@@ -32,7 +44,7 @@ export async function orgCommand(args, io, deps = {}) {
if (words[0] === 'reload') refuseInsideAgent(io.env);
const revision = (r, required) => { if (r === undefined && !required) return undefined; if (!/^[1-9][0-9]*$/.test(r ?? '')) throw new CliError(r === undefined ? '--if-revision is required' : 'invalid revision', 4); return Number(r); };
let lifecycle = null;
if (['archive', 'restore'].includes(words[0]) && words.length === 2) { allowed(['--json'], ['--if-revision']); lifecycle = { ifRevision: revision(p.values['--if-revision'], true) }; if (words[0] === 'archive') refuseAgentRun(io); }
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 };
@@ -40,7 +52,24 @@ export async function orgCommand(args, io, deps = {}) {
else if (words[0] === 'lock' && ['show', 'clear'].includes(words[1]) && words.length === 3) { allowed([], []); result = await orgLock(ctx, words[2], { clear: words[1] === 'clear' }); }
else if (words[0] === 'create' && words.length === 2) { allowed([], ['--from']); result = await createOrg(ctx, words[1], await document(need('--from'))); }
else if (words[0] === 'update' && words.length === 2) { allowed([], ['--from', '--if-revision']); const r = need('--if-revision'); if (!/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid revision', 4); result = await updateOrg(ctx, words[1], await document(need('--from')), { ifRevision: Number(r) }); }
else if (['archive', 'restore'].includes(words[0]) && words.length === 2) result = words[0] === 'archive' ? await archiveOrg(ctx, words[1], { ...lifecycle, busStop: busStopForDelete }) : await restoreOrg(ctx, words[1], lifecycle);
else if (words[0] === 'archive' && words.length === 2) result = await archiveOrg(ctx, words[1], { ...lifecycle, busStop: busStopForDelete });
else if (words[0] === 'restore' && words.length === 2) {
const startBus = !p.flags.has('--no-bus');
if (startBus) restoreBusCheck(system, io.env);
result = await restoreOrg(ctx, words[1], lifecycle);
// The restore stands whatever the bus does. A bus that does not start is said so, with the verb
// that starts it, and the command exits 3 after printing the restore receipt.
if (startBus) {
try { const started = await (deps.busStart ?? busStart)({ ...ctx, env: io.env }, words[1], { detach: true, devLocalOwner: true, pm: false }); result = { ...result, bus: { outcome: 'started', pid: started.pid } }; }
catch (e) {
// A host that died left its lock behind; `bus start` refuses it until `bus recover` clears it.
const message = e instanceof CliError ? e.message : 'the bus did not start', dead = /^stale /.test(message);
result = { ...result, bus: { outcome: 'failed', message, next: dead ? `mosaic bus recover ${words[1]}; ${busStartVerb(words[1])}` : busStartVerb(words[1]) } };
io.stdout.write(JSON.stringify(result, null, 2) + '\n');
throw new CliError(`${words[1]} was restored, but its bus did not start: ${message}; ${dead ? `clear the dead host with mosaic bus recover ${words[1]}, then start it with` : 'start it with'} ${busStartVerb(words[1])}`, 3);
}
}
}
else if (words[0] === 'delete' && words.length === 2) result = await deleteOrg(ctx, words[1], { busStop: busStopForDelete, ...lifecycle });
// U1 (#1873): the F3 bus control from a terminal, through the running Console's own route, which
// checks the owner and writes the action trail (org-console.mjs consoleBusRestart).
+104
View File
@@ -0,0 +1,104 @@
// E2 (#1873, P1-3, org ruling 6): the task board's writes from a terminal. `mosaic tasks create`,
// `edit` and `archive` send the board's three ops (workspace-tasks.mjs) to the organization's bus as
// the person, through the human transport (U4a: the bus's tasks.create, tasks.edit and tasks.archive
// are the person's writes "from the terminal (human-cli)"). The arguments pass the board's own
// check, parseTaskWrite, before anything is sent, so the terminal takes exactly what the board
// takes; the bus then runs its own human, field, digest and archive checks, as it does for the
// board. Writes take no --tester and refuse inside an agent run (callFor), and the bus refuses any
// caller that is not the person (human-required).
import { parseTaskWrite } from '../../webui/src/workspace-tasks.mjs';
import { CliError } from './errors.mjs';
import { orgSelector } from './org-selector.mjs';
import { refuseInsideAgent } from './transport.mjs';
export const TASKS_WRITE_OPS = Object.freeze(['create', 'edit', 'archive']);
export const TASKS_WRITE_USAGE = 'mosaic tasks create --title TEXT --requirement REQ-X-N [--project NAME] [--state STATE] [--assignees ROLE,...] [--priority 0-5] [--due ISO|none] --business <id> [--json]\n' +
' mosaic tasks edit task:N --expect DIGEST [--title TEXT] [--state STATE] [--assignees ROLE,...] [--priority 0-5] [--due ISO|none] --business <id> [--json]\n' +
' mosaic tasks archive task:N --expect DIGEST --business <id> [--json]';
const usage = why => new CliError(`${why}\nusage: ${TASKS_WRITE_USAGE}`, 4);
// Each op's options, as the board's op takes its keys (workspace-tasks.mjs OPS).
const OPTIONS = {
create: ['--title', '--requirement', '--project', '--state', '--assignees', '--priority', '--due'],
edit: ['--expect', '--title', '--state', '--assignees', '--priority', '--due'],
archive: ['--expect'],
};
const KEY = { '--title': 'title', '--requirement': 'requirement', '--project': 'group', '--state': 'state', '--assignees': 'assignees', '--priority': 'priority', '--due': 'due_date', '--expect': 'expect' };
// The board's answer when the bus refuses, by code (workspace-tasks.mjs REFUSALS), so the terminal
// reads the same next step.
const GUIDANCE = {
'human-required': 'the bus takes task changes only from a person; nothing was written',
'tracker-not-store': 'this organization keeps its tasks in Vikunja; nothing was written',
'task-conflict': 'the task changed after you read it; nothing was written. Read it again with mosaic tasks --json',
'task-archived': 'the task is archived; nothing was written',
'task-already-archived': 'the task was already archived; nothing was written',
'task-not-found': 'the task is not on this organization\'s bus; nothing was written',
'task-position-full': 'this project has no room for another task position; nothing was written',
'invalid-state': 'the bus does not have that state; nothing was written',
'unknown-role': 'an assignee is not a role in this organization; nothing was written',
'invalid-request': 'the bus refused the change as invalid; nothing was written',
'credential-leak': 'the change holds part of a stored credential, so the bus refused it; nothing was written',
};
export function parseTasksWrite(args) {
const op = args[0];
if (!TASKS_WRITE_OPS.includes(op)) throw usage(`tasks takes create, edit or archive, not ${JSON.stringify(op)}`);
const selector = orgSelector(usage);
const values = {}, positional = [];
let json = false;
for (let i = 1; i < args.length; i++) {
const a = args[i];
if (selector.owns(a)) { selector.take(a, args[i + 1]); i++; }
else if (a === '--json') { if (json) throw usage('duplicate --json'); json = true; }
else if (OPTIONS[op].includes(a)) {
if (Object.hasOwn(values, a)) throw usage(`duplicate ${a}`);
if (i + 1 >= args.length || args[i + 1].startsWith('--')) throw usage(`${a} needs one value`);
values[a] = args[++i];
} else if (a.startsWith('--')) throw usage(`tasks ${op} does not take ${a}`);
else positional.push(a);
}
// The organization is named before anything else is checked, so a missing --business is a usage
// error (exit 4) inside an agent run too, as for the org owner verbs.
if (!selector.given) throw usage('name the organization with --business');
if (positional.length !== (op === 'create' ? 0 : 1)) throw usage(op === 'create' ? 'tasks create takes no task' : `tasks ${op} takes one task, task:N`);
const body = op === 'create' ? {} : { task_ref: positional[0] };
for (const [flag, raw] of Object.entries(values)) {
const key = KEY[flag];
if (key === 'assignees') body[key] = raw === '' ? [] : raw.split(',').map(r => r.trim());
else if (key === 'priority') body[key] = /^[0-9]$/.test(raw) ? Number(raw) : raw;
else if (key === 'due_date') body[key] = raw === 'none' ? null : raw;
else body[key] = raw;
}
// The board's own check. Its org is the page's; here the organization is checked by the selector
// and the bus, so a fixed valid id stands in for it.
let write;
try { write = parseTaskWrite({ op, org: 'terminal', ...body }); }
catch (e) { if (typeof e?.status === 'number') throw usage(`tasks ${op}: ${e.message}`); throw e; }
return { op, verb: write.verb, args: write.args, json, business: selector.given ? selector.value : undefined };
}
export async function tasksWriteCommand(args, io, callFor) {
const write = parseTasksWrite(args);
// agent-guard (#1873): this process's own environment as well as io.env (callFor checks io.env),
// as the org owner verbs check.
refuseInsideAgent(process.env);
const parsed = { positional: [], flags: new Set(), values: write.business === undefined ? {} : { '--business': write.business } };
const call = callFor(parsed);
let result;
try { result = await call(write.verb, write.args); }
catch (e) {
if (e?.code === 'outcome-unknown' || e?.exitCode === 1) throw new CliError(`outcome unknown: the task change may or may not have been written; read it with mosaic tasks --json before trying again`, 1);
if (typeof e?.code === 'string' && Object.hasOwn(GUIDANCE, e.code)) {
const refused = new CliError(`tasks ${write.op} refused (${e.code}): ${GUIDANCE[e.code]}`, e.exitCode ?? 2);
refused.code = e.code;
throw refused;
}
throw e;
}
const taskRef = typeof result?.task_ref === 'string' ? result.task_ref : write.args.task_ref ?? null;
const digest = typeof result?.digest === 'string' ? result.digest : null;
// The board's answer shape (workspace-tasks.mjs), with the organization the call went to.
const answer = { ok: true, org: parsed.business ?? write.business ?? null, op: write.op, task_ref: taskRef, digest };
const done = { create: 'created', edit: 'changed', archive: 'archived' }[write.op];
io.stdout.write(write.json ? `${JSON.stringify(answer, null, 2)}\n` : `${done} ${taskRef}${digest ? `, digest ${digest}` : ''}\n`);
}
+33 -2
View File
@@ -40,6 +40,16 @@ const GUARDED = [
// archive and delete stop the organization's bus (review finding 2).
['org', 'archive', 'alpha', '--if-revision', '1'],
['org', 'delete', 'alpha', '--confirm', 'alpha'],
// E1 (P1-4): restore starts the organization's bus unless --no-bus is given.
['org', 'restore', 'alpha', '--if-revision', '1'],
// E2 (#1873, P1-3): the hire form and the autonomy panel from a terminal, reads included.
['org', 'role-templates', 'alpha'],
['org', 'hire', 'alpha', '--name', 'ada', '--template', 'worker', '--harness', 'pi', '--model', 'glm', '--reason', 'r'],
['org', 'change', 'alpha', '--name', 'ada', '--template', 'worker', '--harness', 'pi', '--model', 'glm', '--reason', 'r'],
['org', 'fire', 'alpha', '--name', 'ada', '--reason', 'r'],
['org', 'autonomy', 'alpha'],
['org', 'autonomy', 'alpha', 'set', '--revision', 'a'.repeat(64), '--merge', 'near'],
['org', 'autonomy', 'alpha', 'review', 'merge'],
];
// The CLI in this process, with the given io.env; stdout and stderr are collected.
@@ -129,7 +139,7 @@ test('consoleStart refuses a marker in its own process whatever environment it i
test('usage errors still come first, so an agent sees the usage line, not the refusal', async () => {
const env = { ...NOWHERE(), MOSAIC_AGENT_RUN: '1' };
for (const args of [['bus', 'stop'], ['bus', 'start'], ['bus', 'start', 'alpha', '--pm', '--no-launch'], ['console', 'stop', 'extra'], ['console', 'restart', 'extra'], ['console', 'start', '--dev-insecure-bind', '0.0.0.0'], ['console', 'start', '--dev-allow-host', 'x'], ['console', 'start', 'extra'], ['org', 'archive', 'alpha'], ['org', 'delete', 'alpha'], ['org', 'delete', 'alpha', '--confirm', 'alpha', '--if-revision', 'x']]) {
for (const args of [['bus', 'stop'], ['bus', 'start'], ['bus', 'start', 'alpha', '--pm', '--no-launch'], ['console', 'stop', 'extra'], ['console', 'restart', 'extra'], ['console', 'start', '--dev-insecure-bind', '0.0.0.0'], ['console', 'start', '--dev-allow-host', 'x'], ['console', 'start', 'extra'], ['org', 'archive', 'alpha'], ['org', 'restore', 'alpha'], ['org', 'restore', 'alpha', '--if-revision', '1', '--no-bus', '--no-bus'], ['org', 'delete', 'alpha'], ['org', 'delete', 'alpha', '--confirm', 'alpha', '--if-revision', 'x'], ['org', 'hire', 'alpha', '--name', 'ada'], ['org', 'fire', 'alpha'], ['org', 'autonomy', 'alpha', 'set', '--merge', 'near'], ['org', 'autonomy', 'alpha', 'review'], ['org', 'role-templates']]) {
const r = await mosaic(env, args, 30000);
assert.equal(r.status, 4, `${args.join(' ')}: ${r.stderr}`);
}
@@ -247,8 +257,29 @@ test("the Console's Restart bus works: the new host runs with the Console's envi
// An archived organization has no bus status, so the old host's /proc entry shows it stopped.
for (let i = 0; i < 100 && existsSync(`/proc/${after.pid}`); i++) await new Promise(r => setTimeout(r, 100));
assert.ok(!existsSync(`/proc/${after.pid}`), 'archive from a human shell stops the bus host');
const restored = await mosaic(env, ['org', 'restore', 'alpha', '--if-revision', String(JSON.parse(archived.stdout).revision), '--json']);
// E1 (P1-4): restore starts the bus, so an agent run is refused before anything changes, and
// `--no-bus` (which starts nothing) is the only restore an agent run may make.
const archivedRevision = JSON.parse(archived.stdout).revision;
for (const marker of ['CLAUDECODE', 'MOSAIC_AGENT_RUN']) {
const r = await mosaic({ ...env, [marker]: '1' }, ['org', 'restore', 'alpha', '--if-revision', String(archivedRevision), '--json']);
assert.equal(r.status, 3, r.stderr); assert.match(r.stderr, REFUSED(marker)); assert.equal(r.stdout, '');
}
const listed = await mosaic(env, ['org', 'list', '--json']); assert.equal(listed.status, 0, listed.stderr);
assert.equal(JSON.parse(listed.stdout).find(o => o.id === 'alpha').state, 'archived', 'the refused restore changed nothing');
const quiet = await mosaic(agent, ['org', 'restore', 'alpha', '--if-revision', String(archivedRevision), '--no-bus', '--json']);
assert.equal(quiet.status, 0, quiet.stderr); assert.equal(JSON.parse(quiet.stdout).bus, null);
assert.equal((await busStatus(ctx, 'alpha')).healthy, false, 'restore --no-bus starts nothing');
// A person's restore starts the bus as the dev owner, with no agent marker in its host.
const again = await mosaic(env, ['org', 'archive', 'alpha', '--if-revision', String((await show()).revision), '--json']);
assert.equal(again.status, 0, again.stderr);
const restored = await mosaic(env, ['org', 'restore', 'alpha', '--if-revision', String(JSON.parse(again.stdout).revision), '--json']);
assert.equal(restored.status, 0, restored.stderr);
const restoredBus = JSON.parse(restored.stdout).bus;
assert.equal(restoredBus.outcome, 'started');
const up = await busStatus(ctx, 'alpha'); assert.equal(up.healthy, true); assert.equal(up.pid, restoredBus.pid);
const hostNames = readFileSync(`/proc/${up.pid}/environ`, 'utf8').split('\0').map(l => l.split('=')[0]);
for (const marker of AGENT_MARKERS) assert.ok(!hostNames.includes(marker), marker);
await busStop({ ...ctx, env }, 'alpha');
// A marker in the environment the host is started with stops it at the guarded entry.
await assert.rejects(busStart({ ...ctx, env: { ...env, MOSAIC_AGENT_RUN: '1' } }, 'alpha', { detach: true, noLaunch: true }), e => e.exitCode === 3 && /bus failed to start/.test(e.message));
assert.match(readFileSync(join(after.root ?? join(ctx.dataRoot, 'org-runtime/alpha'), 'bus-host/host.log'), 'utf8'), /outside any agent run \(found MOSAIC_AGENT_RUN/);
+366
View File
@@ -0,0 +1,366 @@
// E2 (#1873, P1-3, org ruling 6): the WebUI's task board, hire form and autonomy panel from a
// terminal. Each test sends the CLI verb and the page's own request for the same input, and checks
// that both reach the same bus verb with the same arguments and get the same answer, a refusal
// included. Hire and autonomy run the real scripts/mosaic against an in-process dev-owner Console
// whose organization transports are fakes, so no bus host starts; tasks run against a real broker
// bound to the person, as the human transport binds it.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { spawn, spawnSync } from 'node:child_process';
import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { request } from 'node:http';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { reserveOrg, activateOrg } from '../../organization/src/org-registry.mjs';
import { processState } from '../../seat/src/process-state.mjs';
import { startServer } from '../../webui/src/serve.mjs';
import { createDevOwner } from '../../webui/src/dev-owner.mjs';
import { BusReadError } from '../../webui/src/bus.mjs';
import { parseTaskWrite } from '../../webui/src/workspace-tasks.mjs';
import { close } from '../../webui/tests/fixture.mjs';
import { main } from '../src/cli.mjs';
import { CliError } from '../src/errors.mjs';
import { orgCommand } from '../src/org.mjs';
import { broker, humanEnv, io } from './helpers.mjs';
const REPO = new URL('../../../', import.meta.url).pathname;
const MOSAIC = join(REPO, 'scripts/mosaic');
const FORBIDDEN = [7332, 7340, 7341, 33223, 17450, 17443, 44307, 7350];
const SUBJECT = { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' };
const SHA = 'a'.repeat(64);
async function instance(t) {
const home = mkdtempSync(join(tmpdir(), 'e2-'));
const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-e2test/config.json') });
const setup = spawnSync(join(REPO, 'scripts/bootstrap.sh'), ['--instance', 'e2test'], { env, encoding: 'utf8' });
assert.equal(setup.status, 0, setup.stderr);
const ctx = { configDir: join(home, '.config/mosaic-e2test'), dataRoot: join(home, '.mosaic-e2test'), instance: 'e2test', env };
await reserveOrg(ctx, 'alpha'); await activateOrg(ctx, 'alpha');
t.after(() => rmSync(home, { recursive: true, force: true }));
return { ctx, env };
}
function mosaic(env, args, timeout = 60000) {
return new Promise((resolve, reject) => {
const child = spawn(MOSAIC, args, { env, stdio: ['ignore', 'pipe', 'pipe'] });
let stdout = '', stderr = '';
child.stdout.setEncoding('utf8').on('data', c => { stdout += c; });
child.stderr.setEncoding('utf8').on('data', c => { stderr += c; });
const timer = setTimeout(() => child.kill('SIGKILL'), timeout);
child.on('error', reject);
child.on('close', status => { clearTimeout(timer); resolve({ status, stdout, stderr }); });
});
}
function recordConsole(ctx, url) {
const dir = join(ctx.dataRoot, 'console');
mkdirSync(dir, { recursive: true, mode: 0o700 }); chmodSync(dir, 0o700);
const file = join(dir, 'console.json');
writeFileSync(file, JSON.stringify({ pid: process.pid, startTime: processState(process.pid).startTime, url }), { mode: 0o600 }); chmodSync(file, 0o600);
}
// The page's own request: a same-origin fetch from the Console's page.
function page(port, method, path, body) {
return new Promise((resolve, reject) => {
const payload = body === undefined ? undefined : JSON.stringify(body);
const headers = { host: `127.0.0.1:${port}`, ...(payload === undefined ? {} : { origin: `http://127.0.0.1:${port}`, 'sec-fetch-site': 'same-origin', 'content-type': 'application/json', 'content-length': Buffer.byteLength(payload) }) };
request({ host: '127.0.0.1', port, method, path, headers }, res => {
let text = ''; res.setEncoding('utf8'); res.on('data', c => { text += c; });
res.on('end', () => resolve({ status: res.statusCode, body: JSON.parse(text) }));
}).on('error', reject).end(payload);
});
}
// The bus's revision is a 64-hex digest; REV(n) stands in for the nth.
const REV = n => n.toString(16).padStart(64, '0');
const LOOP = { 'cross-role': 'in the loop', merge: 'in the loop', policy: 'in the loop', authority: 'in the loop' };
// One organization, alpha, whose bus transports record each call. `send` false is a Console
// started without --send.
function organizations({ send = true } = {}) {
const calls = [];
let state = { revision: REV(3), levels: { ...LOOP }, autonomy: { notices: false } };
const org = {
busHealthy: true,
send: send ? async () => ({}) : null,
decisionWrite: async (verb, args, opts) => {
calls.push({ verb, args, opts });
if (verb === 'hire.templates.console') return { templates: [{ name: 'worker', sha256: SHA }] };
if (args.name === 'blocked') throw new BusReadError('definition-not-hireable');
return { id: `hire-${calls.length}`, op: 'resolved', choice: 'yes' };
},
requestRead: async (verb, args, opts) => { calls.push({ verb, args, opts }); return structuredClone(state); },
autonomyWrite: async (verb, args, opts) => {
calls.push({ verb, args, opts });
if (verb === 'autonomy.review.console') return { reviewed: args.decisionClass };
if (args.revision !== state.revision) throw new BusReadError('revision-conflict');
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)}` };
},
};
return { calls, organizations: { list: async () => [{ id: 'alpha' }], resolve: async id => (id === 'alpha' ? org : null) } };
}
async function consoleFor(t, ctx, options) {
const f = organizations(options);
const server = await startServer({ port: 0, board: 'http://127.0.0.1:9', sso: createDevOwner(), send: async () => ({}), organizations: f.organizations, log: () => {} });
t.after(() => close(server));
const port = server.address().port;
assert.ok(!FORBIDDEN.includes(port));
recordConsole(ctx, `http://127.0.0.1:${port}/`);
return { ...f, port, server };
}
const HIRE = ['--name', 'ada', '--template', 'worker', '--harness', 'pi', '--model', 'glm', '--reason', 'more hands'];
const GUARDED = [
['org', 'role-templates', 'alpha'],
['org', 'hire', 'alpha', ...HIRE],
['org', 'change', 'alpha', ...HIRE],
['org', 'fire', 'alpha', '--name', 'ada', '--reason', 'done'],
['org', 'autonomy', 'alpha'],
['org', 'autonomy', 'alpha', 'set', '--revision', REV(3), '--merge', 'near'],
['org', 'autonomy', 'alpha', 'review', 'merge'],
];
test('hire and autonomy verbs: usage errors first (exit 4), then refused inside an agent run before any config is read', async () => {
// The config path is not an instance config, so a read would refuse with another message.
const env = { ...humanEnv({ MOSAIC_CONFIG: '/nonexistent/e2/config.json' }), CLAUDECODE: '1' };
for (const args of GUARDED) {
const r = await mosaic(env, args);
assert.equal(r.status, 3, `${args.join(' ')}: ${r.stderr}`);
assert.match(r.stderr, /outside any agent run \(found CLAUDECODE/, args.join(' '));
assert.equal(r.stdout, '');
}
for (const args of [
['org', 'hire', 'alpha', '--name', 'ada'],
['org', 'fire', 'alpha', '--name', 'ada', '--reason', 'done', '--model', 'glm'],
['org', 'hire', 'alpha', ...HIRE, '--level', 'project'],
['org', 'autonomy', 'alpha', 'set', '--merge', 'near'],
['org', 'autonomy', 'alpha', 'set', '--revision', REV(3), '--merge', 'sideways'],
['org', 'autonomy', 'alpha', 'set', '--revision', '3', '--merge', 'near'],
['org', 'autonomy', 'alpha', 'set', '--revision', REV(3), '--reverse-window-hours', '200'],
['org', 'autonomy', 'alpha', 'review', 'everything'],
['org', 'role-templates', 'Not An Id'],
]) {
const r = await mosaic(env, args);
assert.equal(r.status, 4, `${args.join(' ')}: ${r.stderr}`);
assert.doesNotMatch(r.stderr, /agent run/);
}
// This process's own environment counts too, as for org archive|delete: the marker is only in
// process.env here, never in io.env.
const saved = process.env.CLAUDECODE;
process.env.CLAUDECODE = '1';
try {
for (const args of GUARDED) {
const x = io(humanEnv({ MOSAIC_CONFIG: '/nonexistent/e2/config.json' }));
delete x.env.CLAUDECODE;
await assert.rejects(orgCommand(args.slice(1), x, {}), e => e instanceof CliError && e.exitCode === 3 && /outside any agent run/.test(e.message), args.join(' '));
}
} finally { if (saved === undefined) delete process.env.CLAUDECODE; else process.env.CLAUDECODE = saved; }
// The default instance is a one-line refusal (exit 3), not an uncaught error.
const d = io(humanEnv());
delete d.env.MOSAIC_CONFIG;
await assert.rejects(orgCommand(['autonomy', 'alpha'], d, {}), e => e instanceof CliError && e.exitCode === 3 && /requires a named instance.*--dev-local-owner/.test(e.message));
});
test('role-templates, hire, change and fire send the hire form\'s request and get its answer, refusals included', { timeout: 120000 }, async t => {
const { ctx, env } = await instance(t);
const c = await consoleFor(t, ctx);
const opts = { subject: SUBJECT, business: 'alpha' };
let r = await mosaic(env, ['org', 'role-templates', 'alpha', '--json']);
assert.equal(r.status, 0, r.stderr);
const pageTemplates = await page(c.port, 'POST', '/api/orgs/alpha/decisions/hire/templates', { business: 'alpha' });
assert.deepEqual(JSON.parse(r.stdout), pageTemplates.body);
assert.deepEqual(c.calls.splice(0), [{ verb: 'hire.templates.console', args: {}, opts }, { verb: 'hire.templates.console', args: {}, opts }]);
r = await mosaic(env, ['org', 'role-templates', 'alpha']);
assert.equal(r.stdout, `worker ${SHA.slice(0, 12)}\n`);
c.calls.splice(0);
// The form's body for this input (bus.js hireSubmit): the template it read, by name and sha256.
const form = (op, name, extra = {}) => ({ op, level: 'org', name, reason: 'more hands', ...extra, ...(op === 'fire' ? {} : { role: { template: 'worker', sha256: SHA }, route: { harness: 'pi', model: 'glm' } }) });
for (const op of ['hire', 'change']) {
r = await mosaic(env, ['org', op, 'alpha', ...HIRE, '--json']);
assert.equal(r.status, 0, r.stderr);
const cli = c.calls.splice(0);
assert.deepEqual(cli.map(x => x.verb), ['hire.templates.console', 'hire.start.console']);
const pageAnswer = await page(c.port, 'POST', '/api/orgs/alpha/decisions/hire/start', { business: 'alpha', hire: form(op, 'ada') });
const pageCall = c.calls.splice(0);
assert.deepEqual(cli[1], pageCall[0]);
assert.deepEqual(cli[1].args, form(op, 'ada'));
assert.equal(pageAnswer.status, 200);
// Each decision gets its own id; the rest of the answer is the page's.
assert.deepEqual({ ...JSON.parse(r.stdout), id: 'x' }, { ...pageAnswer.body, id: 'x' });
}
r = await mosaic(env, ['org', 'fire', 'alpha', '--name', 'ada', '--reason', 'more hands', '--level', 'project', '--project', 'site']);
assert.equal(r.status, 0, r.stderr);
assert.match(r.stdout, /^recorded decision hire-1: fire ada \(resolved yes\)\n$/);
assert.deepEqual(c.calls.splice(0), [{ verb: 'hire.start.console', args: { op: 'fire', level: 'project', name: 'ada', reason: 'more hands', project: 'site' }, opts }]);
// A bus refusal reaches the terminal with the Console's code and message, as the form shows it.
r = await mosaic(env, ['org', 'hire', 'alpha', ...HIRE.map(a => (a === 'ada' ? 'blocked' : a))]);
const pageRefusal = await page(c.port, 'POST', '/api/orgs/alpha/decisions/hire/start', { business: 'alpha', hire: form('hire', 'blocked') });
assert.equal(pageRefusal.status, 409);
assert.equal(r.status, 3, r.stderr);
assert.ok(r.stderr.includes(`hire refused (definition-not-hireable): ${pageRefusal.body.message}`), r.stderr);
c.calls.splice(0);
// A template the Console did not offer is refused before anything is raised.
r = await mosaic(env, ['org', 'hire', 'alpha', ...HIRE.map(a => (a === 'worker' ? 'ghost' : a))]);
assert.equal(r.status, 2, r.stderr);
assert.match(r.stderr, /no role template "ghost"/);
assert.deepEqual(c.calls.splice(0).map(x => x.verb), ['hire.templates.console']);
// A Console without --send: the page's refusal, and nothing reaches the bus.
await close(c.server);
const ro = await consoleFor(t, ctx, { send: false });
r = await mosaic(env, ['org', 'hire', 'alpha', ...HIRE]);
const pageRo = await page(ro.port, 'POST', '/api/orgs/alpha/decisions/hire/templates', { business: 'alpha' });
assert.equal(r.status, 3, r.stderr);
assert.match(r.stderr, new RegExp(`hire refused \\(${pageRo.body.error}\\)`));
assert.equal(pageRo.body.error, 'read-only');
assert.deepEqual(ro.calls, []);
// An organization the Console does not know is refused as the page is.
r = await mosaic(env, ['org', 'role-templates', 'beta']);
assert.equal(r.status, 3, r.stderr);
assert.match(r.stderr, /role-templates refused \(org-unavailable\)/);
});
test('autonomy reads, saves with the panel\'s revision check and records a review through the panel\'s route', { timeout: 120000 }, async t => {
const { ctx, env } = await instance(t);
const c = await consoleFor(t, ctx);
const opts = { subject: SUBJECT };
let r = await mosaic(env, ['org', 'autonomy', 'alpha', '--json']);
assert.equal(r.status, 0, r.stderr);
assert.deepEqual(JSON.parse(r.stdout), (await page(c.port, 'GET', '/api/orgs/alpha/autonomy')).body);
r = await mosaic(env, ['org', 'autonomy', 'alpha']);
assert.equal(r.stdout, `revision ${REV(3)}\ncross-role: in the loop\nmerge: in the loop\npolicy: in the loop\nauthority: in the loop\nnotices: off\nreverse window: not set (the page offers 24 hours)\nauthority resolver: none\n`);
c.calls.splice(0);
// The panel's save for "merge near the loop" (autonomy.js): every class as shown, notices, the
// window it offers (24) and no resolver.
const panel = { ...LOOP, merge: 'near the loop', notices: false, reverseWindowHours: 24 };
// Imported here so that the other tests still run, and fail on their own, on a base without it.
const { autonomyDocument } = await import('../src/org-owner.mjs');
assert.deepEqual(autonomyDocument({ revision: REV(3), levels: LOOP, autonomy: { notices: false } }, { merge: 'near the loop' }), panel);
// An emptied resolver field: the panel leaves it out of the save, and so does the terminal.
assert.deepEqual(autonomyDocument({ revision: REV(3), levels: LOOP, autonomy: { notices: false, authorityResolver: 'ops' } }, { authorityResolver: '' }), { ...LOOP, notices: false, reverseWindowHours: 24 });
assert.equal(autonomyDocument({ revision: REV(3), levels: LOOP, autonomy: { notices: false, authorityResolver: 'ops' } }, {}).authorityResolver, 'ops');
r = await mosaic(env, ['org', 'autonomy', 'alpha', 'set', '--revision', REV(3), '--merge', 'near', '--json']);
assert.equal(r.status, 0, r.stderr);
assert.deepEqual(JSON.parse(r.stdout), { revision: REV(4), transaction: 'tx-4', writable: true });
assert.deepEqual(c.calls.splice(0), [{ verb: 'autonomy.get.console', args: {}, opts }, { verb: 'autonomy.set.console', args: { revision: REV(3), autonomy: panel }, opts }]);
// The page's save of the same document at revision 4 lands the same way.
const pageSave = await page(c.port, 'POST', '/api/orgs/alpha/autonomy', { revision: REV(4), autonomy: panel });
assert.deepEqual(pageSave.body, { revision: REV(5), transaction: 'tx-5', writable: true });
assert.deepEqual(c.calls.splice(0), [{ verb: 'autonomy.set.console', args: { revision: REV(4), autonomy: panel }, opts }]);
// A stale revision is the panel's revision-conflict, and nothing changes.
r = await mosaic(env, ['org', 'autonomy', 'alpha', 'set', '--revision', REV(3), '--policy', 'near']);
const pageStale = await page(c.port, 'POST', '/api/orgs/alpha/autonomy', { revision: REV(3), autonomy: panel });
assert.deepEqual(pageStale, { status: 409, body: { error: 'revision-conflict' } });
assert.equal(r.status, 3, r.stderr);
assert.match(r.stderr, /autonomy set refused \(revision-conflict\): the organization changed; read it again with mosaic org autonomy ID/);
assert.equal((await page(c.port, 'GET', '/api/orgs/alpha/autonomy')).body.revision, REV(5));
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');
await page(c.port, 'POST', '/api/orgs/alpha/autonomy/review', { decisionClass: 'merge' });
const [cli, pageReview] = c.calls.splice(0);
assert.deepEqual(cli, pageReview);
assert.deepEqual(cli, { verb: 'autonomy.review.console', args: { decisionClass: 'merge' }, opts });
// Without --send the read still answers and both writes are the page's read-only refusal.
await close(c.server);
const ro = await consoleFor(t, ctx, { send: false });
r = await mosaic(env, ['org', 'autonomy', 'alpha', '--json']);
assert.equal(r.status, 0, r.stderr);
assert.equal(JSON.parse(r.stdout).writable, false);
for (const args of [['set', '--revision', REV(3), '--merge', 'near'], ['review', 'merge']]) {
r = await mosaic(env, ['org', 'autonomy', 'alpha', ...args]);
assert.equal(r.status, 3, r.stderr);
assert.match(r.stderr, /refused \(read-only\): start the Console with --send/);
}
assert.deepEqual((await page(ro.port, 'POST', '/api/orgs/alpha/autonomy/review', { decisionClass: 'merge' })).body, { error: 'read-only' });
assert.deepEqual(ro.calls.map(x => x.verb), ['autonomy.get.console', 'autonomy.get.console']);
});
// mosaic tasks create|edit|archive with the person's transport over a real broker.
async function run(argv, s) {
const x = io();
try { await main(argv, x, { system: { dataRoot: s.dataRoot }, transport: s.transport }); return { code: 0, out: x.out.text, err: '' }; }
catch (e) { if (!(e instanceof CliError)) throw e; return { code: e.exitCode, out: x.out.text, err: e.message }; }
}
test('tasks create, edit and archive send the board\'s writes to the bus as the person', async t => {
const b = broker(t);
const s = { ...b, dataRoot: mkdtempSync(join(tmpdir(), 'e2t-')) };
t.after(() => rmSync(s.dataRoot, { recursive: true, force: true }));
// The board's request for the same input, through the board's own check.
const board = body => { const w = parseTaskWrite({ org: 'demo', ...body }); return { verb: w.verb, args: w.args }; };
let r = await run(['tasks', 'create', '--title', 'Plan the store', '--requirement', 'REQ-TASK-1', '--assignees', 'coder', '--priority', '2', '--business', 'demo', '--json'], s);
assert.equal(r.code, 0, r.err);
const created = JSON.parse(r.out);
assert.deepEqual(s.calls.splice(0).map(({ verb, args }) => ({ verb, args })), [board({ op: 'create', title: 'Plan the store', requirement: 'REQ-TASK-1', assignees: ['coder'], priority: 2 })]);
assert.deepEqual({ ...created, digest: 'd' }, { ok: true, org: 'demo', op: 'create', task_ref: 'task:1', digest: 'd' });
const row = () => s.read('tasks').find(x => x.task_ref === 'task:1');
assert.equal(created.digest, row().digest);
assert.equal(row().fields.title, 'Plan the store');
assert.deepEqual(row().fields.assignees, ['coder']);
r = await run(['tasks', 'edit', 'task:1', '--expect', created.digest, '--state', 'in-progress', '--due', 'none', '--business', 'demo'], s);
assert.equal(r.code, 0, r.err);
assert.equal(r.out, `changed task:1, digest ${row().digest}\n`);
assert.deepEqual(s.calls.splice(0).map(({ verb, args }) => ({ verb, args })), [board({ op: 'edit', task_ref: 'task:1', expect: created.digest, state: 'in-progress', due_date: null })]);
assert.equal(row().fields.state, 'in-progress');
// A stale digest is the board's task-conflict, and nothing is written.
const before = row();
r = await run(['tasks', 'archive', 'task:1', '--expect', created.digest, '--business', 'demo'], s);
assert.equal(r.code, 2, r.err);
assert.match(r.err, /^tasks archive refused \(task-conflict\): the task changed after you read it/);
assert.deepEqual(row(), before);
s.calls.splice(0);
r = await run(['tasks', 'archive', 'task:1', '--expect', before.digest, '--business', 'demo'], s);
assert.equal(r.code, 0, r.err);
assert.deepEqual(s.calls.splice(0).map(({ verb, args }) => ({ verb, args })), [board({ op: 'archive', task_ref: 'task:1', expect: before.digest })]);
assert.match(r.out, /^archived task:1, digest [0-9a-f]{64}\n$/);
// What the board refuses with 400 the terminal refuses as usage (exit 4), with the board's words,
// before anything is sent.
for (const [argv, body] of [
[['tasks', 'create', '--title', 'x', '--business', 'demo'], { op: 'create', title: 'x' }],
[['tasks', 'edit', 'task:1', '--expect', 'nope', '--title', 'y', '--business', 'demo'], { op: 'edit', task_ref: 'task:1', expect: 'nope', title: 'y' }],
[['tasks', 'archive', 'task:1', '--business', 'demo'], { op: 'archive', task_ref: 'task:1' }],
]) {
let boardError;
try { parseTaskWrite({ org: 'demo', ...body }); } catch (e) { boardError = e; }
assert.equal(boardError?.status, 400, JSON.stringify(body));
r = await run(argv, s);
assert.equal(r.code, 4, argv.join(' '));
assert.ok(r.err.includes(boardError.message), `${r.err} / ${boardError.message}`);
}
assert.deepEqual(s.calls, []);
// Terminal-only slips the board cannot make are usage too: an option-looking word is not a value,
// and the organization is named (exit 4 even inside an agent run).
for (const [argv, env] of [
[['tasks', 'create', '--title', '--json', '--requirement', 'REQ-TASK-1', '--business', 'demo'], humanEnv()],
[['tasks', 'create', '--title', 'x', '--requirement', 'REQ-TASK-1'], { ...humanEnv(), CLAUDECODE: '1' }],
]) {
const y = io(env);
await assert.rejects(main(argv, y, { system: { dataRoot: s.dataRoot }, transport: s.transport }), e => e instanceof CliError && e.exitCode === 4, argv.join(' '));
}
assert.deepEqual(s.calls, []);
// A marker only in this process's own environment counts too.
const savedMarker = process.env.CLAUDECODE;
process.env.CLAUDECODE = '1';
try {
const y = io(humanEnv());
delete y.env.CLAUDECODE;
await assert.rejects(main(['tasks', 'create', '--title', 'x', '--requirement', 'REQ-TASK-1', '--business', 'demo'], y, { system: { dataRoot: s.dataRoot }, transport: s.transport }), e => e instanceof CliError && e.exitCode === 3 && /outside any agent run/.test(e.message));
} finally { if (savedMarker === undefined) delete process.env.CLAUDECODE; else process.env.CLAUDECODE = savedMarker; }
assert.deepEqual(s.calls, []);
// Inside an agent run the write is refused before anything is sent.
const x = io({ ...humanEnv(), CLAUDECODE: '1' });
await assert.rejects(main(['tasks', 'create', '--title', 'x', '--requirement', 'REQ-TASK-1', '--business', 'demo'], x, { system: { dataRoot: s.dataRoot }, transport: s.transport }), e => e instanceof CliError && e.exitCode === 3);
assert.deepEqual(s.calls, []);
});
@@ -0,0 +1,94 @@
// E1 (P1-4): `mosaic org restore` brings the organization back running. It starts the bus as
// `mosaic bus start ID --dev-local-owner --detach` would; a bus that does not start is said so with
// that verb, and the restore stands. On an instance where that start is refused, the restore is
// refused first and changes nothing. `--no-bus` keeps the old restore. The bus start itself is a
// stand-in here; agent-guard.test.mjs and org.test.mjs start a real one.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import * as service from '../../organization/src/org-service.mjs';
import { listOrgs } from '../../organization/src/org-registry.mjs';
import { DEV_OWNER } from '../src/dev-local-owner.mjs';
import { orgCommand } from '../src/org.mjs';
import { io, humanEnv } from './helpers.mjs';
// A named instance under a temporary HOME (the dev-owner checks read the instance from HOME), with
// one archived organization.
async function archived(t) {
const home = mkdtempSync(join(tmpdir(), 'e1-restore-')), saved = process.env.HOME;
process.env.HOME = home;
t.after(() => { process.env.HOME = saved; rmSync(home, { recursive: true, force: true }); });
const configDir = join(home, '.config', 'mosaic-e1test'), dataRoot = join(home, 'data');
mkdirSync(configDir, { recursive: true, mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
const ctx = { configDir, dataRoot, instance: 'e1test', principal: DEV_OWNER };
const created = await service.createOrg(ctx, 'alpha', await service.templateRender('alpha'));
const gone = await service.archiveOrg(ctx, 'alpha', { ifRevision: created.revision, busStop: async () => ({ stopped: false }) });
const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(configDir, 'config.json') });
const state = async () => (await listOrgs(ctx)).find(e => e.id === 'alpha').state;
return { ctx, env, revision: gone.revision, state, system: { dataRoot } };
}
const restore = (f, deps, ...extra) => { const x = io(f.env); return orgCommand(['restore', 'alpha', '--if-revision', String(f.revision), '--json', ...extra], x, { system: f.system, ...deps }).then(() => x, e => { e.io = x; throw e; }); };
test('E1: restore starts the bus as the dev owner and its receipt names the bus', async t => {
const f = await archived(t), calls = [];
const x = await restore(f, { busStart: async (ctx, id, opts) => { calls.push([ctx.dataRoot, ctx.env === f.env, id, opts]); return { org: id, pid: 4242, socket: '/s', root: '/r' }; } });
assert.deepEqual(calls, [[f.ctx.dataRoot, true, 'alpha', { detach: true, devLocalOwner: true, pm: false }]]);
const receipt = JSON.parse(x.out.text);
assert.equal(receipt.revision, f.revision + 1);
assert.deepEqual(receipt.bus, { outcome: 'started', pid: 4242 });
assert.equal(await f.state(), 'active');
});
test('E1: a bus that does not start after a good restore is said so, with the verb, and the restore stands', async t => {
const f = await archived(t);
const { CliError } = await import('../src/errors.mjs');
const busStart = async () => { throw new CliError('bus failed to start; see the bus log', 3); };
await assert.rejects(restore(f, { busStart }), e => {
assert.equal(e.exitCode, 3);
assert.equal(e.message, 'alpha was restored, but its bus did not start: bus failed to start; see the bus log; start it with mosaic bus start alpha --dev-local-owner --detach');
// The restore receipt is printed before the refusal.
const receipt = JSON.parse(e.io.out.text);
assert.equal(receipt.revision, f.revision + 1);
assert.deepEqual(receipt.bus, { outcome: 'failed', message: 'bus failed to start; see the bus log', next: 'mosaic bus start alpha --dev-local-owner --detach' });
return true;
});
assert.equal(await f.state(), 'active');
});
test('E1: a bus host that died leaves a stale lock; the receipt names bus recover before the start', async t => {
const f = await archived(t);
const { CliError } = await import('../src/errors.mjs');
const busStart = async () => { throw new CliError('stale bus host lock', 3); };
await assert.rejects(restore(f, { busStart }), e => {
assert.equal(e.exitCode, 3);
assert.equal(e.message, 'alpha was restored, but its bus did not start: stale bus host lock; clear the dead host with mosaic bus recover alpha, then start it with mosaic bus start alpha --dev-local-owner --detach');
assert.deepEqual(JSON.parse(e.io.out.text).bus, { outcome: 'failed', message: 'stale bus host lock', next: 'mosaic bus recover alpha; mosaic bus start alpha --dev-local-owner --detach' });
return true;
});
assert.equal(await f.state(), 'active');
});
test('E1: where a dev-owner bus start is refused, restore is refused before it changes anything, and --no-bus restores', async t => {
const f = await archived(t); let starts = 0;
const busStart = async () => { starts++; return { pid: 1 }; };
writeFileSync(join(f.ctx.dataRoot, '.deployed'), '{}\n');
await assert.rejects(restore(f, { busStart }), e => e.exitCode === 3 && /^restore starts the bus as the local dev owner, and --dev-local-owner refuses \.deployed; restore with --no-bus, then start the bus with mosaic bus start$/.test(e.message) && e.io.out.text === '');
assert.equal(await f.state(), 'archived');
const x = await restore(f, { busStart }, '--no-bus');
assert.equal(JSON.parse(x.out.text).bus, null);
assert.equal(starts, 0);
assert.equal(await f.state(), 'active');
});
test('E1: an agent run may restore only with --no-bus', async t => {
const f = await archived(t); let starts = 0;
const busStart = async () => { starts++; return { pid: 1 }; };
f.env = { ...f.env, MOSAIC_AGENT_RUN: '1' };
await assert.rejects(restore(f, { busStart }), e => e.exitCode === 3 && /MOSAIC_AGENT_RUN/.test(e.message));
assert.equal(await f.state(), 'archived');
await restore(f, { busStart }, '--no-bus');
assert.equal(starts, 0);
assert.equal(await f.state(), 'active');
});
+2
View File
@@ -21,6 +21,8 @@ test('organization CLI renders, validates, creates, updates and initializes git'
info = JSON.parse(invoke('org', 'show', 'alpha', '--json')); const archived = JSON.parse(invoke('org', 'archive', 'alpha', '--if-revision', String(info.revision), '--json')); assert.equal(archived.revision, info.revision + 1);
assert.equal(JSON.parse(invoke('org', 'list', '--json'))[0].state, 'archived');
const restored = JSON.parse(invoke('org', 'restore', 'alpha', '--if-revision', String(archived.revision), '--json')); assert.equal(restored.revision, archived.revision + 1);
// E1 (P1-4): restore brings the organization back running; the delete below stops the bus again.
assert.equal(restored.bus.outcome, 'started'); assert.ok(Number.isInteger(restored.bus.pid)); assert.equal(JSON.parse(invoke('bus', 'status', 'alpha', '--json')).healthy, true);
info = JSON.parse(invoke('org', 'show', 'alpha', '--json')); const receipt = JSON.parse(invoke('org', 'delete', 'alpha', '--confirm', 'alpha', '--if-revision', String(info.revision), '--json')); assert.equal(receipt.org, 'alpha'); assert.deepEqual(JSON.parse(invoke('org', 'list', '--json')), []);
});
test('business and org aliases conflict in either order', () => { for (const args of [['--business','alpha','--org','beta'], ['--org','beta','--business','alpha']]) { const r = run(process.env, 'tasks', ...args); assert.notEqual(r.status, 0); assert.match(r.stderr, /conflict/i); } });
+3 -1
View File
@@ -22,7 +22,9 @@ baseline CLI.
| Start, stop or restart a bus | `mosaic bus start <id>` / `mosaic bus stop <id>` / `mosaic bus restart <id>` | S6 integration hook |
| Restart (or first start) an organization's bus | `mosaic bus restart <id>`, or `mosaic bus start <id>` for a bus never started | Enabled (F3): the owner's **Restart bus** control, offered where a page says the bus is down or a change needs a restart; trailed |
| Restart the Console | Stop and run `mosaic console start` again | Enabled (F3) when `mosaic console start` runs it in the foreground, or with `--dev-local-owner --detach` under a detached supervisor (D1): Settings > Console; the owner only; trailed. Other Consoles answer `not-supervised` |
| Save, review, confirm or reverse an organization's autonomy | Save only: edit the autonomy block and run `mosaic org update ORG --from FILE --if-revision N`; no autonomy verb yet (readiness P1-3) | Enabled (D1) on a `--dev-local-owner` Console: the owner only, on the selected organization's bus, behind a fence that passes only the four console verbs `autonomy.set.console`, `autonomy.review.console`, `decision.confirm.console` and `decision.reverse.console` |
| Save, review, confirm or reverse an organization's autonomy | Read, save and review: `mosaic org autonomy ORG`, `mosaic org autonomy ORG set --revision REV [--cross-role|--merge|--policy|--authority in|near|out] [--notices on|off] [--reverse-window-hours 1-168] [--authority-resolver NAME|""]`, `mosaic org autonomy ORG review CLASS` (E2, through this route and its revision check); confirm and reverse act on inbox decisions and have no verb yet | Enabled (D1) on a `--dev-local-owner` Console: the owner only, on the selected organization's bus, behind a fence that passes only the four console verbs `autonomy.set.console`, `autonomy.review.console`, `decision.confirm.console` and `decision.reverse.console` |
| Hire, fire or change an agent; read role templates | `mosaic org role-templates ORG`; `mosaic org hire|change ORG --name NAME --template TEMPLATE --harness H --model M [--thinking T] [--level org|project|root] [--project P] --reason TEXT`; `mosaic org fire ORG --name NAME [--level ...] --reason TEXT` (E2, through this route) | Enabled (D4) for the owner on a selected organization's bus: one decision raised and approved through `hire.start.console`, with the bus's hire checks |
| Create, edit or archive a board task | `mosaic tasks create --title T --requirement REQ [--project P] [--state S] [--assignees R,...] [--priority 0-5] [--due ISO|none]`; `mosaic tasks edit task:N --expect DIGEST ...`; `mosaic tasks archive task:N --expect DIGEST` (E2: the board's own check, then the person's `tasks.create|edit|archive` on the bus) | Enabled for the owner on an organization with the built-in task store |
| Create chat project linked to a configured repository | `mosaic project create <name> --repo <project> --org <id>` | Enabled |
| Create a thread | `mosaic thread create <project> <name> --org <id>` | Planned enabled |
| Open a thread | `mosaic thread open <thread> --org <id>` | Planned enabled |
+44 -1
View File
@@ -16,6 +16,7 @@
// POST /api/orgs/ID/setup/queue C2 queueInit mosaic org queue init
// POST /api/orgs/ID/setup/project/P C2 projectRepoInit mosaic org project repo init
// POST /api/orgs/ID/setup/bus C3 busStart mosaic bus start --detach
// POST /api/orgs/ID/restore C2 restoreOrg, then C3 busStart (E1: the answer's `bus`)
// POST /api/orgs/ID/restart/bus C3 busRestart mosaic bus restart (F3; busStart when stopped;
// busRevive for a host that died, dev-local owner only, D2)
// POST /api/orgs/ID/stop/bus C3 busStop mosaic bus stop (a stopped bus answers state stopped)
@@ -465,7 +466,9 @@ async function route(req, res, { path, search, org, admin, principal, write, log
const fn = kind === 'archive' ? service.archiveOrg : service.restoreOrg;
if (typeof fn !== 'function') return json(res, 503, { error: 'not-configured', step: kind });
const receipt = await fn(ctx, id, { ifRevision: body.revision, ...(busStop ? { busStop } : {}) });
return json(res, 200, { outcome: 'done', step: kind, receipt: forReceipt(ctx, receipt), verb: verbFor(kind, id, { revision: body.revision }) });
const done = { outcome: 'done', step: kind, receipt: forReceipt(ctx, receipt), verb: verbFor(kind, id, { revision: body.revision }) };
if (kind === 'archive') return json(res, 200, done);
return json(res, 200, { ...done, bus: await restoreBus({ id, ctx, admin, log, trail, by }) });
} catch (e) { return refusal(res, e, { step: kind, id, unknownOk: true }); }
}
if (kind === 'delete' && !sub && write) {
@@ -571,6 +574,46 @@ async function route(req, res, { path, search, org, admin, principal, write, log
return json(res, 404, { error: 'not found' });
}
// E1 (P1-4): Restore brings the organization back running. After a good restore the bus starts as
// Restart bus starts a stopped one: the owner, origin and --send checks were made for the restore,
// the trail line is written first, and a trail that cannot be written starts nothing. The restore
// stands whatever happens here; the answer's `bus` says what did, and the page offers Restart bus
// when the bus did not start. Never throws.
async function restoreBus({ id, ctx, admin, log, trail, by }) {
const verb = verbFor('bus', id);
const notStarted = (error, message) => ({ outcome: 'not-started', error, message, verb });
let bus = null;
try { bus = await admin.bus(); } catch { /* reported as not configured */ }
if (!bus || typeof bus.busStart !== 'function' || typeof bus.busStatus !== 'function') return notStarted('not-configured', 'bus control is not available on this Console, so the bus was not started');
if (!trail) return notStarted('trail-unavailable', 'this Console has no action trail, so it does not start buses');
let root = null;
try {
const status = await bus.busStatus(ctx, id);
if (status?.healthy) return { outcome: 'done', existing: true, healthy: true, receipt: forReceipt(ctx, status), verb };
root = status?.root ?? null;
} catch (e) {
if (!isCliError(e)) { log(`org-admin: ${id} restore: bus status: ${e?.code ?? e?.name ?? 'error'}`); return notStarted('bus-refused', 'the bus state could not be read, so the bus was not started'); }
return notStarted('bus-refused', `${plainMessage(ctx, e.message)}. Inspect it with mosaic bus status ${id}; mosaic bus recover ${id} clears a host that died.`);
}
try { trail({ by, action: 'bus.start', org: id, outcome: 'requested', after: 'restore' }); }
catch { return notStarted('trail-unavailable', 'the action trail could not be written, so the bus was not started'); }
const note = outcome => { try { trail({ by, action: 'bus.start', org: id, outcome, after: 'restore' }); } catch { log(`org-admin: ${id} restore bus start: trail write failed after ${outcome}`); } };
let receipt;
try { receipt = await bus.busStart(ctx, id, { detach: true, devLocalOwner: by?.provider === 'local-dev', pm: false }); }
catch (e) {
const failed = isCliError(e) && /^bus failed to start\b/.test(e.message);
note(failed ? 'failed' : 'refused');
if (!isCliError(e)) { log(`org-admin: ${id} restore bus start: ${e?.code ?? e?.name ?? 'error'}`); return { outcome: 'failed', error: 'bus-failed', message: 'the bus did not start', verb }; }
const said = failed ? await hostRefusal(root) : null;
return { outcome: 'failed', error: failed ? 'bus-failed' : 'bus-refused', message: plainMessage(ctx, said ? `${e.message}. The bus host said: ${said}` : e.message), verb };
}
note('done');
log(`org-admin: ${id} bus started after restore`);
let healthy = null;
try { healthy = (await bus.busStatus(ctx, id)).healthy; } catch { /* the receipt still stands */ }
return { outcome: 'done', healthy, receipt: forReceipt(ctx, receipt), verb };
}
// F3 (REQ-WEB-3): one control restarts an organization's bus and answers with a done receipt. A
// running host restarts on the options it was started with (C3 busRestart keeps --dev-local-owner);
// a stopped one starts as setup starts it. The trail line is written before anything changes, and
+2 -1
View File
@@ -1923,7 +1923,8 @@
if (path.startsWith('/chat')||path.startsWith('/projects')||path.startsWith('/inbox')||path.startsWith('/settings')||path.startsWith('/docs/')||wsPage(path)) render(false); else if (window.MosaicOrg ? path === '/board' : path === '/' || path === '') paintHeld(heldErr);
};
configRead = fetch('/api/config', { cache: 'no-store', redirect: 'manual' }).then(r => { if (r.type === 'opaqueredirect') { signOut(); return null; } return r.json(); }).then(applyConfig).catch(() => {});
document.addEventListener('mosaic-config', e => { if (e.detail && typeof e.detail === 'object') applyConfig(e.detail); });
// E1 (P1-5): after Restart bus the organization dashboard draws its bus line and system types now, not at the next poll.
document.addEventListener('mosaic-config', e => { if (e.detail && typeof e.detail === 'object') { applyConfig(e.detail); if (window.MosaicOrg && routeOf().split('?')[0] === '/organization') render(false); } });
// The cross-organization pages ask for a repaint after a filter, sort or selection change,
// and report the inbox count across organizations for the sidebar.
document.addEventListener('mosaic-workspace-render', () => { if (wsPage(routeOf().split('?')[0])) render(false); });
+45 -8
View File
@@ -68,13 +68,13 @@
<button type="button" id="org-archive-open" data-write>Archive organization</button>
<div id="org-archive-confirm" tabindex="-1" hidden aria-labelledby="org-archive-confirm-title">
<h4 id="org-archive-confirm-title">Review archive</h4>
<ul><li>The organization's bus stops.</li><li>All files stay where they are.</li><li>The organization leaves the switcher.</li><li>Restore brings it back without starting the bus.</li></ul>
<ul><li>The organization's bus stops.</li><li>All files stay where they are.</li><li>The organization leaves the switcher.</li><li>Restore brings it back and starts its bus.</li></ul>
<div class="settings-actions"><button type="button" id="org-archive-cancel" class="org-secondary">Cancel</button><button type="button" id="org-archive-final" data-write>Archive</button></div>
</div>
</section>
<section id="org-archived" class="panel" tabindex="-1" hidden aria-labelledby="org-archived-title">
<h3 id="org-archived-title">Organization archived</h3>
<p>Editing and setup are unavailable while archived. Files remain in place; restoring does not start the bus.</p>
<p>Editing and setup are unavailable while archived. Files remain in place; restoring starts the bus again.</p>
<p id="org-restore-verb" class="org-verb"></p>
<button type="button" id="org-restore" data-write>Restore organization</button>
</section>
@@ -342,7 +342,8 @@
if (result.body.verb) card.querySelector('code').textContent = result.body.verb;
}
editorDirty = false; $('org-new-id').value = ''; $('org-quick-open-org').hidden = false; $('org-quick-interview').hidden = !window.MosaicOnboarding; status('Organization setup complete.');
await refreshList();
// E1 (P1-5): the sidebar and header list the new organization without a reload.
await refreshOrganizations();
} catch (e) {
const card = list.children[Math.min(current, steps.length - 1)];
card.querySelector('.org-pill').textContent = 'Stopped'; card.querySelector('.org-field-err').textContent = current === 4
@@ -350,7 +351,7 @@
: `${e.message} Open the organization to inspect and continue; do not repeat a step whose outcome is unknown.`;
if (current >= 4) { editorDirty = false; $('org-new-id').value = ''; $('org-quick-open-org').hidden = false; }
card.classList.add('org-step-warn'); card.focus(); status('Setup stopped.');
await refreshList();
await refreshOrganizations();
}
}
async function openExisting(id, { toSetup = false } = {}) {
@@ -549,6 +550,37 @@
const hash = next ? `#/orgs/${encodeURIComponent(next.id)}/settings/organizations` : '#/settings/organizations';
history.replaceState(null, '', hash); location.reload(); return true;
}
// E1 (P1-4): a restore also starts the bus (org-admin.mjs restoreBus). One receipt names both
// results; a bus that did not start says why and offers Restart bus for this organization.
const RESTORED = 'mosaic-org-restored:';
function keepRestored(id, bus) { try { sessionStorage.setItem(RESTORED + id, JSON.stringify(bus ?? null)); } catch { /* the reloaded page shows the bus state only */ } }
function takeRestored(id) {
try { const v = sessionStorage.getItem(RESTORED + id); sessionStorage.removeItem(RESTORED + id); return v === null ? undefined : JSON.parse(v); } catch { return undefined; }
}
function restoredReceipt(id, bus) {
const check = `mosaic bus status ${id}`, known = bus && typeof bus === 'object';
const line = !known ? `The bus outcome is unknown. Check it with ${check}.`
: bus.outcome === 'done' ? (bus.existing ? 'Its bus was already running.' : `Bus started. ${bus.healthy ? 'It answers.' : `It has not answered yet. Check it with ${check}.`}`)
: `The bus did not start: ${bus.message || 'no reason was given.'}`;
const offer = (!known || bus.outcome !== 'done') && canWrite;
const box = $('org-archive-success');
box.innerHTML = `<p>Organization restored. ${txt(line)}</p>${known && bus.verb ? `<p class="org-verb">Same as <code>${txt(bus.verb)}</code></p>` : ''}${offer ? `<p><button type="button" data-restore-bus="${txt(id)}">Restart bus</button> <span role="status" data-restore-bus-receipt></span></p>` : ''}`;
box.hidden = false; box.focus();
}
// Restart bus for the restored organization, the same route as the dashboard's (console-restart.js).
async function restartRestored(e) {
const button = e.target.closest('[data-restore-bus]'); if (!button || button.disabled) return;
const id = button.dataset.restoreBus, out = button.parentElement.querySelector('[data-restore-bus-receipt]');
button.disabled = true; button.textContent = 'Restarting…'; out.textContent = '';
let r = null; try { r = await api('POST', `/api/orgs/${encodeURIComponent(id)}/restart/bus`, {}); } catch { r = null; }
if (!alive) return;
const done = r?.status === 200 && r.body.outcome === 'done' && r.body.step === 'bus-restart';
out.textContent = !r ? `Outcome unknown. Check the bus with mosaic bus status ${id} before retrying.`
: done ? `${r.body.recovered ? 'Dead bus host cleared; bus started' : r.body.restarted ? 'Bus restarted' : 'Bus started'}. ${r.body.healthy ? 'It answers.' : `It has not answered yet. Check it with mosaic bus status ${id}.`}`
: (r.body.message || 'The restart was refused. Nothing was retried.');
button.textContent = 'Restart bus'; button.disabled = done && r.body.healthy === true;
if (done) await refreshOrganizations();
}
async function runLifecycle(step) {
const id = st.id, revision = st.revision, button = $(step === 'archive' ? 'org-archive-final' : 'org-restore');
button.disabled = true; if (step === 'archive') button.textContent = 'Archiving…'; $('org-archive-error').textContent = '';
@@ -558,11 +590,11 @@
if (!verifiedReply(reply, step)) throw Error('unknown lifecycle outcome');
const rows = await refreshOrganizations();
if (step === 'archive' && switchIfCurrentRemoved(id, rows)) return;
if (step === 'restore' && !window.MosaicOrg) { history.replaceState(null, '', `#/orgs/${encodeURIComponent(id)}/settings/organizations`); location.reload(); return; }
// The page reloads into the restored organization; its receipt is kept for that page to show.
if (step === 'restore' && !window.MosaicOrg) { keepRestored(id, reply.body.bus); history.replaceState(null, '', `#/orgs/${encodeURIComponent(id)}/settings/organizations?org=${encodeURIComponent(id)}`); location.reload(); return; }
await openExisting(id);
if (step === 'restore') {
const box = $('org-archive-success'); box.textContent = 'Organization restored. The bus remains stopped.'; box.hidden = false; box.focus();
} else $('org-archived').focus();
if (step === 'restore') restoredReceipt(id, reply.body.bus);
else $('org-archived').focus();
status(`Organization ${step === 'archive' ? 'archived' : 'restored'}.`);
} catch {
if (step === 'archive') button.textContent = 'Archive';
@@ -615,6 +647,7 @@
[$('org-archive-cancel'), 'click', () => { resetArchive(); $('org-archive-open').focus(); }],
[$('org-archive-final'), 'click', () => runLifecycle('archive')],
[$('org-restore'), 'click', () => runLifecycle('restore')],
[$('org-archive-success'), 'click', restartRestored],
[$('org-delete-open'), 'click', () => { $('org-delete-open').hidden = true; $('org-delete-first').hidden = false; $('org-delete-first').focus(); }],
[$('org-delete-cancel'), 'click', cancelDelete],
[$('org-delete-cancel-second'), 'click', cancelDelete],
@@ -643,6 +676,9 @@
[$('org-steps'), 'input', e => { const card = e.target.closest('.org-step'); if (card && e.target.matches('[data-remote]')) { keepChoice(card); showVerb(card); } }],
];
for (const [el, ev, fn] of handlers) el.addEventListener(ev, fn);
// A restore receipt carried across the reload is taken now, before anything below can fail, so a
// later visit never shows it again.
const carried = (() => { const o = query().get('org'); return o && ID.test(o) ? takeRestored(o) : undefined; })();
fetch(new URL('/api/config', location.href), { cache: 'no-store' }).then(r => r.json()).then(async config => {
if (!alive) return;
canWrite = (config.orgSetup === true || config.send === true) && config.tester !== true && config.user !== true;
@@ -655,6 +691,7 @@
// Resume: ?org=ID reopens the organization and its setup, read back from the server.
if (org && ID.test(org)) {
await openExisting(org, { toSetup: setup });
if (carried !== undefined) restoredReceipt(org, carried);
if (created && st.setup?.notifications?.receipt) showReceipt('Organization created', st.setup.notifications);
if (setup) focusFirstOpen();
}
+54
View File
@@ -579,6 +579,60 @@ test('a bus that refuses to restart is a visible failure and the trail says refu
assert.equal(c.calls.slice(before).filter(y => ['busStart', 'busRestart'].includes(y[0])).length, 0);
});
// E1 (P1-4): a restore brings the organization back running. The bus starts as Restart bus starts a
// stopped one, the trail says it came after a restore, and the answer names both results.
const restorable = c => { c.service.restoreOrg = async (ctx, id, opts) => { c.calls.push(['restoreOrg', id, opts.ifRevision]); return { op: 'org.restore', org: id, revision: 3 }; }; };
test('E1: a restore starts the organization\'s bus and the answer names both results', async t => {
const tr = trailIn(t);
const c = await console_(t, { trail: tr.trail });
await c.call('POST', '/api/orgs', { id: 'acme', doc: browserDoc('acme') }); restorable(c);
const r = await c.call('POST', '/api/orgs/acme/restore', { revision: 2 });
assert.equal(r.status, 200, r.raw);
assert.equal(r.body.outcome, 'done'); assert.equal(r.body.step, 'restore'); assert.equal(r.body.verb, 'mosaic org restore acme --if-revision 2');
assert.deepEqual(r.body.bus, { outcome: 'done', healthy: true, receipt: { org: 'acme', pid: 4242, socket: '<host path>', root: '<data root>/org-runtime/acme' }, verb: 'mosaic bus start acme --dev-local-owner --detach' });
assert.deepEqual(c.calls.filter(x => ['restoreOrg', 'busStart'].includes(x[0])).map(x => x.slice(0, 3)), [['restoreOrg', 'acme', 2], ['busStart', 'acme', { detach: true, devLocalOwner: true, pm: false }]]);
assert.deepEqual(tr.lines(), [
{ by: DEV, action: 'bus.start', org: 'acme', outcome: 'requested', after: 'restore' },
{ by: DEV, action: 'bus.start', org: 'acme', outcome: 'done', after: 'restore' },
]);
// A bus that already answers is left as it is.
const again = await c.call('POST', '/api/orgs/acme/restore', { revision: 3 });
assert.deepEqual({ outcome: again.body.bus.outcome, existing: again.body.bus.existing, healthy: again.body.bus.healthy }, { outcome: 'done', existing: true, healthy: true });
assert.equal(c.calls.filter(x => x[0] === 'busStart').length, 1);
});
test('E1: a bus that does not start after a good restore is said so, with the verb, and the restore stands', async t => {
const tr = trailIn(t);
const c = await console_(t, { trail: tr.trail });
await c.call('POST', '/api/orgs', { id: 'acme', doc: browserDoc('acme') }); restorable(c);
const bus = await c.admin.bus();
bus.busStart = async () => { throw cliError('bus failed to start; see /data/org-runtime/acme/bus-host/host.log'); };
const r = await c.call('POST', '/api/orgs/acme/restore', { revision: 2 });
assert.equal(r.status, 200, r.raw); assert.equal(r.body.outcome, 'done'); assert.equal(r.body.step, 'restore');
assert.deepEqual(r.body.bus, { outcome: 'failed', error: 'bus-failed', message: 'bus failed to start; see <data root>/org-runtime/acme/bus-host/host.log', verb: 'mosaic bus start acme --dev-local-owner --detach' });
assert.doesNotMatch(r.raw, /\/data\//);
assert.deepEqual(tr.lines().map(e => e.outcome), ['requested', 'failed']);
// A bus whose state cannot be read is not started.
bus.busStatus = async () => { throw cliError('stale bus host lock'); };
const s = await c.call('POST', '/api/orgs/acme/restore', { revision: 3 });
assert.equal(s.status, 200); assert.equal(s.body.bus.outcome, 'not-started'); assert.equal(s.body.bus.error, 'bus-refused');
assert.equal(s.body.bus.message, 'stale bus host lock. Inspect it with mosaic bus status acme; mosaic bus recover acme clears a host that died.');
assert.equal(tr.lines().length, 2);
});
test('E1: a Console with no trail, or a trail that cannot be written, restores without starting the bus', async t => {
for (const trail of [undefined, () => { throw new Error('disk full'); }]) {
const c = await console_(t, trail ? { trail } : {});
await c.call('POST', '/api/orgs', { id: 'acme', doc: browserDoc('acme') }); restorable(c);
const r = await c.call('POST', '/api/orgs/acme/restore', { revision: 2 });
assert.equal(r.status, 200, r.raw); assert.equal(r.body.outcome, 'done');
assert.equal(r.body.bus.outcome, 'not-started'); assert.equal(r.body.bus.error, 'trail-unavailable');
assert.equal(r.body.bus.verb, 'mosaic bus start acme --dev-local-owner --detach');
assert.equal(c.calls.filter(x => x[0] === 'busStart').length, 0);
}
});
// D2 (P0-3): on the dev Console, Restart bus on a host that died (its state left behind by a
// Console unit stop) clears that state through busRevive and starts the bus. busRevive's
// busRecover is the proof; a live or unprovable host refuses and nothing starts.
@@ -11,18 +11,23 @@ import * as service from '../../organization/src/org-service.mjs';
import * as registry from '../../organization/src/org-registry.mjs';
import { orgPaths } from '../../cli/src/config.mjs';
import { busStopForDelete } from '../../cli/src/org-bus.mjs';
import { CliError } from '../../cli/src/errors.mjs';
import { actionTrail } from '../src/action-trail.mjs';
import { browser } from './browser.mjs';
import { systemTypesFake } from './helpers/system-types-fake.mjs';
import { close } from './fixture.mjs';
async function fixture(t, ids = ['alpha', 'beta'], { health = null } = {}) {
// E1: `bus` adds verbs to the admin's bus (busStart for a restore); `trail` gives the Console its action trail;
// `extra` adds to each organization's transports (the dashboard's bus reads and System types).
async function fixture(t, ids = ['alpha', 'beta'], { health = null, bus = {}, trail = false, extra = () => ({}) } = {}) {
const dir = await mkdtemp(join(tmpdir(), 'archive-browser-'));
const configDir = join(dir, 'config'), dataRoot = join(dir, 'data'); await mkdir(configDir, { mode: 0o700 }); await mkdir(dataRoot, { mode: 0o700 });
const ctx = { configDir, dataRoot, instance: 'test', principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
for (const id of ids) await service.createOrg(ctx, id, await service.templateRender(id));
const busStatus = async () => ({ healthy: health ? health.up : false });
const organizations = organizationConsole({ ctx, listOrgs: registry.listOrgs, resolveOrg: registry.resolveOrg, showOrg: service.showOrg, orgPaths, busStatus, options: ({ id }) => ({ reads: { business: async () => ({ business: { id, projects: ['work'] } }), settings: async () => ({}) }, bus: {}, send: async () => ({}) }) });
const admin = createOrgAdmin({ service, registry, bus: { busStatus, busStopForDelete }, context: async () => ({ configDir, dataRoot, instance: 'test' }) });
const server = await startServer({ port: 0, sso: createDevOwner(), orgAdmin: admin, organizations, send: async () => ({}), board: 'http://127.0.0.1:9', log: () => {} });
const organizations = organizationConsole({ ctx, listOrgs: registry.listOrgs, resolveOrg: registry.resolveOrg, showOrg: service.showOrg, orgPaths, busStatus, options: ({ id }) => ({ reads: { business: async () => ({ business: { id, projects: ['work'] } }), settings: async () => ({}) }, bus: {}, send: async () => ({}), ...extra(id) }) });
const admin = createOrgAdmin({ service, registry, bus: { busStatus, busStopForDelete, ...bus }, context: async () => ({ configDir, dataRoot, instance: 'test' }) });
const server = await startServer({ port: 0, sso: createDevOwner(), orgAdmin: admin, organizations, send: async () => ({}), board: 'http://127.0.0.1:9', log: () => {}, ...(trail ? { trail: actionTrail(join(dir, 'console')) } : {}) });
const b = await browser({ machine: false }); t.after(async () => { await b.close(); await close(server); await rm(dir, { recursive: true, force: true }); });
const wait = async expression => { for (let i = 0; i < 400; i++) { try { if (await b.evaluate(expression)) return; } catch {} await new Promise(r => setTimeout(r, 30)); } throw Error(expression); };
await b.navigate(`http://127.0.0.1:${server.address().port}/#/orgs/alpha/settings/organizations`);
@@ -36,7 +41,7 @@ async function fixture(t, ids = ['alpha', 'beta'], { health = null } = {}) {
await b.screenshot(join(process.env.ORG_ARCHIVE_SHOTS, `${state}-${width}-${mode}.png`));
}
}
return { b, wait, open, shots, ctx, port: server.address().port };
return { b, wait, open, shots, ctx, port: server.address().port, dir };
}
test('archive and restore a non-current organization', { timeout: 45000 }, async t => {
@@ -88,6 +93,76 @@ test('opening another editor resets the archive confirmation and error', { timeo
assert.deepEqual(await f.b.evaluate(`({trigger:document.querySelector('#org-archive-open').hidden,panel:document.querySelector('#org-archive-confirm').hidden,error:document.querySelector('#org-archive-error').textContent,disabled:document.querySelector('#org-archive-final').disabled})`), {trigger:false,panel:true,error:'',disabled:false});
});
// E1 (P1-4): one Restore restores the organization and starts its bus; the receipt names both.
const startingBus = (health, fails = 0) => {
const starts = [];
return { starts, busStart: async (ctx, id, opts) => { starts.push([id, opts]); if (starts.length <= fails) throw new CliError('bus failed to start; see the bus log', 3); health.up = true; return { org: id, pid: 4242, socket: join(ctx.dataRoot, 'bus.sock'), root: join(ctx.dataRoot, 'org-runtime', id) }; } };
};
const archiveBeta = async f => { await f.open('beta'); await f.b.evaluate(`document.querySelector('#org-archive-open').click();document.querySelector('#org-archive-final').click()`); await f.wait(`!document.querySelector('#org-archived').hidden`); };
test('E1 (P1-4): Restore restores the organization and starts its bus, with one receipt naming both', { timeout: 45000 }, async t => {
const health = { up: false }, bus = startingBus(health);
const f = await fixture(t, ['alpha', 'beta'], { health, bus, trail: true }); await archiveBeta(f);
await f.b.evaluate(`document.querySelector('#org-restore').click()`);
await f.wait(`!document.querySelector('#org-archive-success').hidden`);
assert.equal((await registry.listOrgs(f.ctx)).find(e => e.id === 'beta').state, 'active');
assert.deepEqual(bus.starts, [['beta', { detach: true, devLocalOwner: true, pm: false }]]);
const text = await f.b.evaluate(`document.querySelector('#org-archive-success').innerText`);
assert.match(text, /^Organization restored\. Bus started\. It answers\./);
assert.match(text, /Same as mosaic bus start beta --dev-local-owner --detach/);
assert.doesNotMatch(text, /remains stopped/);
assert.equal(await f.b.evaluate(`document.querySelector('[data-restore-bus]')`), null, 'a running bus needs no Restart bus');
assert.deepEqual(await f.b.evaluate(`window.MosaicOrg.list.then(rows=>rows.map(row=>[row.id,row.busHealthy]))`), [['alpha', true], ['beta', true]]);
await f.shots('e1-restored-started', '#org-archive-success');
});
test('E1 (P1-4): a bus that does not start after a good restore is said so, and Restart bus starts it', { timeout: 45000 }, async t => {
const health = { up: false }, bus = startingBus(health, 1);
const f = await fixture(t, ['alpha', 'beta'], { health, bus, trail: true }); await archiveBeta(f);
await f.b.evaluate(`document.querySelector('#org-restore').click()`);
await f.wait(`!document.querySelector('#org-archive-success').hidden`);
assert.equal((await registry.listOrgs(f.ctx)).find(e => e.id === 'beta').state, 'active', 'the restore stands');
assert.match(await f.b.evaluate(`document.querySelector('#org-archive-success').innerText`), /^Organization restored\. The bus did not start: bus failed to start; see the bus log/);
assert.equal(await f.b.evaluate(`document.querySelector('[data-restore-bus]').textContent`), 'Restart bus');
await f.shots('e1-restored-bus-failed', '#org-archive-success');
await f.b.evaluate(`document.querySelector('[data-restore-bus]').click()`);
await f.wait(`/It answers/.test(document.querySelector('[data-restore-bus-receipt]')?.textContent || '')`);
assert.equal(await f.b.evaluate(`document.querySelector('[data-restore-bus-receipt]').textContent`), 'Bus started. It answers.');
assert.equal(await f.b.evaluate(`document.querySelector('[data-restore-bus]').disabled`), true);
assert.deepEqual(bus.starts.map(([id]) => id), ['beta', 'beta']);
await f.shots('e1-restored-bus-restarted', '#org-archive-success');
});
test('E1 (P1-4): restoring the only organization reloads into it and still shows the restore and bus receipt', { timeout: 45000 }, async t => {
const health = { up: false }, bus = startingBus(health);
const f = await fixture(t, ['alpha'], { health, bus, trail: true }); await f.open('alpha');
await f.b.evaluate(`document.querySelector('#org-archive-open').click();document.querySelector('#org-archive-final').click()`);
await f.wait(`!window.MosaicOrg && document.querySelector('#org-open-id')?.value === 'alpha'`);
await f.open('alpha'); await f.b.evaluate(`document.querySelector('#org-restore').click()`);
await f.wait(`window.MosaicOrg?.id === 'alpha' && document.querySelector('#org-archive-success')?.hidden === false`);
assert.match(await f.b.evaluate(`document.querySelector('#org-archive-success').innerText`), /^Organization restored\. Bus started\. It answers\./);
assert.deepEqual(bus.starts.map(([id]) => id), ['alpha']);
// The kept receipt is shown once: a later visit does not repeat it.
assert.equal(await f.b.evaluate(`Object.keys(sessionStorage).filter(k=>k.startsWith('mosaic-org-restored:')).length`), 0);
});
test('E1 (P1-5): after restoring another organization, its dashboard shows the bus running and its System types', { timeout: 45000 }, async t => {
const health = { up: false }, bus = startingBus(health), st = await systemTypesFake({ enabled: ['coding'] });
const extra = id => ({
requestRead: st.requestRead,
reads: { business: async () => ({ business: { id, human: 'local-dev-owner', arbiters: [], projects: [], vars: {}, instances: [] } }), settings: async () => ({}), queue: async () => ({ rows: [] }), busStatus: async () => ({}) },
bus: { instances: async () => ({ business: id, revision: 'a'.repeat(64), launch: true, noHire: [], retired: [], definitions: {}, instances: {} }), messages: async () => ({ messages: [], unread: 0 }), agents: async () => [], 'thread.claims': async () => [], inbox: async () => [], tasks: async () => [] },
});
const f = await fixture(t, ['alpha', 'beta'], { health, bus, trail: true, extra }); await archiveBeta(f);
await f.b.evaluate(`document.querySelector('#org-restore').click()`);
await f.wait(`/Bus started\. It answers\./.test(document.querySelector('#org-archive-success')?.innerText || '')`);
await f.wait(`!!document.querySelector('#workspace-nav [data-workspace-org=beta]')`);
await f.b.evaluate(`location.hash = '#/orgs/beta/organization'`);
await f.wait(`/bus running, read/.test(document.querySelector('[data-org-dashboard]')?.innerText || '') && !!document.querySelector('[data-org-dashboard] [data-system-type-chips] .ws-chip')`);
assert.doesNotMatch(await f.b.evaluate(`document.querySelector('[data-org-dashboard] [data-system-type-chips]').textContent`), /unavailable/);
assert.deepEqual(bus.starts.map(([id]) => id), ['beta']);
});
test('restore does not announce success for an unexpected response', { timeout: 45000 }, async t => {
const f = await fixture(t); await f.open('beta');
await f.b.evaluate(`document.querySelector('#org-archive-open').click();document.querySelector('#org-archive-final').click()`);
@@ -0,0 +1,94 @@
// E1 (P1-5): pages follow a state change without a reload. Restart bus redraws the organization
// dashboard's bus line and System types at once, and Quick Setup puts the new organization in the
// sidebar. Neither waits for the 10 s repaint: each check runs well inside that window.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { startServer } from '../src/serve.mjs';
import { createDevOwner } from '../src/dev-owner.mjs';
import { createOrgAdmin } from '../src/org-admin.mjs';
import { actionTrail } from '../src/action-trail.mjs';
import * as service from '../../organization/src/org-service.mjs';
import { browser } from './browser.mjs';
import { close } from './fixture.mjs';
import { systemTypesFake } from './helpers/system-types-fake.mjs';
const waitFor = (b, tries = 150) => expression => b.evaluate(`(async()=>{for(let i=0;i<${tries};i++){if(${expression})return;await new Promise(r=>setTimeout(r,30))}throw Error(${JSON.stringify(expression)})})()`);
// One organization whose bus state the fake bus below owns. A stopped bus has no reads, as
// organizations.mjs resolves it; a running one answers the dashboard's reads and System types.
async function westOrgs(state) {
const st = await systemTypesFake({ enabled: ['coding'] });
const running = () => ({
busHealthy: true, requestRead: st.requestRead, systemTypesWrite: null,
reads: { business: async () => ({ business: { id: 'west', human: 'jason', arbiters: [], projects: [], vars: {}, instances: [] } }), settings: async () => ({}), queue: async () => ({ rows: [] }), busStatus: async () => ({}) },
bus: { instances: async () => ({ business: 'west', revision: 'a'.repeat(64), launch: true, noHire: [], retired: [], definitions: {}, instances: {} }), messages: async () => ({ messages: [], unread: 0 }), agents: async () => [], 'thread.claims': async () => [], inbox: async () => [], tasks: async () => [] },
chat: { projects: async () => [], threads: async () => [] },
});
return {
list: async () => [{ id: 'west', label: 'West', busHealthy: state.running }],
resolve: async id => id !== 'west' ? null : state.running ? running() : { busHealthy: false, reads: null, bus: {}, chat: { projects: async () => [], threads: async () => [] } },
};
}
test('E1 (P1-5b): Restart bus redraws the dashboard bus line and System types without a reload or the 10 s repaint', { timeout: 45000 }, async t => {
const state = { running: false }; const starts = [];
const bus = {
busStatus: async (ctx, id) => ({ org: id, healthy: state.running, pid: state.running ? 4242 : null }),
busStart: async (ctx, id, opts) => { starts.push([id, opts]); state.running = true; return { org: id, pid: 4242, socket: '/data/s', root: '/data/org-runtime/west' }; },
busRestart: async () => { throw Error('not reached: the bus is stopped'); },
};
const orgAdmin = createOrgAdmin({ service: { showOrg: async (ctx, id) => ({ id, revision: 1, doc: {} }) }, registry: { listOrgs: async () => [] }, bus, context: async () => ({ configDir: '/cfg', dataRoot: '/data', instance: 'test' }) });
const root = mkdtempSync(join(tmpdir(), 'e1-refresh-')); t.after(() => rmSync(root, { recursive: true, force: true }));
const server = await startServer({ port: 0, sso: createDevOwner(), send: async () => ({}), organizations: await westOrgs(state), orgAdmin, trail: actionTrail(join(root, 'console')), log: () => {} }); t.after(() => close(server));
const b = await browser({ machine: false }); t.after(() => b.close());
const wait = waitFor(b);
await b.navigate(`http://127.0.0.1:${server.address().port}/#/orgs/west/organization`);
await wait('document.querySelector("[data-org-dashboard] [data-bus-restart]") && /bus unavailable, read/.test(document.querySelector("[data-org-dashboard]").innerText)');
assert.match(await b.evaluate('document.querySelector("[data-org-dashboard] [data-system-type-chips]").textContent'), /System types unavailable/);
// The page's own repaint is 10 s after its last draw; mark the draw this test starts from.
await b.evaluate('window.__drawn = Date.now(); window.__first = document.querySelector("[data-org-dashboard]")');
await b.evaluate('document.querySelector("[data-org-dashboard] [data-bus-restart]").click()');
await wait('/It answers/.test(document.querySelector("[data-org-dashboard] [data-bus-restart-receipt]")?.textContent || "")');
// No reload: the page keeps its globals, and the dashboard shows the running bus and the enabled type.
await wait('/bus running, read/.test(document.querySelector("[data-org-dashboard]")?.innerText || "") && !!document.querySelector("[data-org-dashboard] [data-system-type-chips] .ws-chip")');
assert.ok(await b.evaluate('Date.now() - window.__drawn') < 8000, 'the dashboard redrew before the 10 s repaint');
assert.equal(await b.evaluate('typeof window.__first'), 'object', 'the page was not reloaded');
assert.doesNotMatch(await b.evaluate('document.querySelector("[data-org-dashboard] [data-system-type-chips]").textContent'), /unavailable/);
assert.match(await b.evaluate('document.querySelector("[data-org-dashboard] [data-bus-restart-receipt]").textContent'), /^Bus started at .*It answers\./);
assert.deepEqual(starts.map(([id]) => id), ['west']);
});
test('E1 (P1-5a): Quick Setup from inside another organization lists the new one in the sidebar without a reload', { timeout: 60000 }, async t => {
const registry = await import('../../organization/src/org-registry.mjs');
const dir = mkdtempSync(join(tmpdir(), 'e1-quick-'));
const configDir = join(dir, 'config'), dataRoot = join(dir, 'data'); mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
let started = false;
const bus = { busStatus: async () => ({ healthy: false }), busStart: async () => { started = true; return { healthy: true }; } };
const admin = createOrgAdmin({ service, registry, bus, context: async () => ({ configDir, dataRoot, instance: 'test' }) });
const projects = [];
const shell = { send: async () => ({}), busHealthy: true, chat: { projects: async () => projects, threads: async () => [], createProject: async args => { const row = { id: '20000000-0000-4000-8000-000000000002', ...args }; projects.push(row); return row; } } };
const organizations = {
list: async () => [{ id: 'east', label: 'East', busHealthy: true }, ...(started ? [{ id: 'alpha', label: 'alpha', busHealthy: true }] : [])],
resolve: async id => (id === 'east' || (id === 'alpha' && started)) ? shell : null,
};
const server = await startServer({ port: 0, sso: createDevOwner(), organizations, orgAdmin: admin, send: async () => ({}), board: 'http://127.0.0.1:9', log: () => {} }); t.after(() => close(server));
const b = await browser({ machine: false }); t.after(() => b.close());
t.after(() => rmSync(dir, { recursive: true, force: true }));
const wait = waitFor(b, 300);
await b.navigate(`http://127.0.0.1:${server.address().port}/#/orgs/east/settings/organizations`);
await wait('document.querySelector("[data-workspace-org=east]") && document.querySelector("#org-new-form [data-write]") && !document.querySelector("#org-new-form [data-write]").disabled');
assert.equal(await b.evaluate('document.querySelector("[data-workspace-org=alpha]")'), null);
await b.evaluate('window.__first = true');
await b.evaluate(`document.querySelector('#org-new-id').value='alpha';document.querySelector('#org-new-form [data-write]').click()`);
await wait('document.querySelector("#org-quick-open") && !document.querySelector("#org-paths").hidden');
await b.evaluate(`document.querySelector('#org-quick-open').click();document.querySelector('#org-risk-accept').click();document.querySelector('#org-quick-run').click()`);
await wait(`document.querySelector('[data-quick-step="5"] .org-pill')?.textContent === 'Done'`);
await b.evaluate('window.__done = Date.now()');
await wait('!!document.querySelector("[data-workspace-org=alpha]")');
assert.ok(await b.evaluate('Date.now() - window.__done') < 8000, 'the sidebar followed the setup, not a later poll');
assert.equal(await b.evaluate('window.__first'), true, 'the page was not reloaded');
assert.equal(started, true);
});