diff --git a/packages/bus/README.md b/packages/bus/README.md index b0c469263..18f3039dd 100644 --- a/packages/bus/README.md +++ b/packages/bus/README.md @@ -324,6 +324,8 @@ refuses `message-not-found`; malformed arguments refuse `invalid-request`. Roles get `human-required`. A successful read records nothing, so Console history reads never prevent a later `message.receive` from delivering it. +A tester (row 110, #1604) is a PM test's sender, never the human. `src/tester-cli.mjs ` is human-cli.mjs with its own nonce variable, `MOSAIC_BUS_TESTER_NONCE`, so neither shim's nonce proves the other. `verifyTester` (`src/tester.mjs`) checks the same process proof but walks the ancestry for a managed run only: a launch record, `MOSAIC_RUN_ID`, `MOSAIC_BUS_CAP` or `MOSAIC_AGENT_RUN` refuses `tester-required`. Claude and Codex markers pass, since a tester runs from inside Mos or a T3 seat. A role session can't bind: its shim reports a pid from the sandbox's PID namespace, and the seat sandbox test runs that case under bwrap. `bindTester` then binds only when the business var `bus.tester` is exactly `true`, read at bus start, else `tester-off`. A request carries exactly one of `cap`, `human` or `tester`. The session may use `message.send`, `message.receive` and `message.read`, plus the reader's views of `inbox`, `agents`, `tasks`, `trail`, `decisions`, `messages` and `replies`; every other verb refuses `tester-refused`. It sends only to a role, with a body that starts `[mos test]` (`tester-prefix`) and no `corrects` or `decision`. Its message is stored `from_role 'tester'`, `from_run 'tester-cli'`, with no request and no event, so no `human.input` backs it and `task_create` can't cite it. A role answers at the `tester` address, and only with `in_reply_to` naming a tester's message (`tester-reply-required`); the human can't send there. Those replies never reach the human's `message.receive` or `messages`. + ## Shared reads and adapter boundary ```js diff --git a/packages/bus/src/broker.mjs b/packages/bus/src/broker.mjs index ddc18f21c..64a544d4b 100644 --- a/packages/bus/src/broker.mjs +++ b/packages/bus/src/broker.mjs @@ -50,6 +50,10 @@ const TASK_ARGS = ['done', 'assignee', 'label', 'requirement', 'limit', 'after', export const TASK_PAGE = 50; export const TASK_PAGE_MAX = 200; export const TASK_VERBS = Object.freeze(ACTIONS.filter((a) => a.startsWith('task.'))); +// Row 110 (#1604): what a tester session may do. Send to a role, receive and read the replies +// sent to `tester`, and the human's read views. Every other verb refuses tester-refused. +const TESTER_VERBS = ['message.send', 'message.receive', 'message.read', 'inbox', 'agents', 'tasks', 'trail', 'decisions', 'messages', 'replies']; +const TESTER_PREFIX = '[mos test]'; const SELF_KINDS = new Set(['task.created', 'task.assigned', 'task.state', 'task.closed', 'task.conflict']); const POLL_KINDS = new Set([ 'task.changed.external', @@ -133,7 +137,7 @@ export class Broker { if (!id(name) || b.id !== name || !id(b.human) || !object(b.roles) || !object(b.arbiters)) fail('invalid-business'); for (const role of Object.keys(b.roles)) { - if (!id(role) || role === 'human') fail('invalid-business'); + if (!id(role) || role === 'human' || role === 'tester') fail('invalid-business'); const a = b.roles[role].authority; if (!object(a) || !Array.isArray(a.withinRole) || !Array.isArray(a.crossRole)) fail('invalid-authority'); @@ -164,6 +168,7 @@ export class Broker { ); } #agent(s) { + if (s.tester) fail('tester-refused'); if (s.reader) fail('read-only'); if (s.human) fail('agent-required'); const h = this.#holder(s.business, s.role); @@ -254,6 +259,14 @@ export class Broker { fail('human-required'); return this.#cap({ ...record, role: null, run: null, human: b.human }); } + // Row 110 (#1604): the trusted tester transport invokes this only after checking its CLI process + // and that no managed run is among its ancestors. The business turns testing on with the business + // var `bus.tester`, exactly true; anything else refuses tester-off. + bindTester(record) { + keys(record, ['business'], ['business']); + if (this.#business(record.business).vars?.['bus.tester'] !== true) fail('tester-off'); + return this.#cap({ business: record.business, tester: true, role: 'tester', run: null, human: false }); + } bindReader({ business }) { this.#business(business); return this.#cap({ business, reader: true, role: null, run: null, human: false }); @@ -656,7 +669,10 @@ export class Broker { return this.#store.transaction(() => { if (s.reader && !['inbox', 'agents', 'tasks', 'trail', 'decisions', 'credentials', 'replies', 'messages', 'state'].includes(request.verb)) fail('read-only'); - const result = this.#dispatch(s, request.verb, args); + if (s.tester && !TESTER_VERBS.includes(request.verb)) fail('tester-refused'); + // A tester reads the human's views, as a reader does, and its message verbs are its own. + const view = s.tester && !request.verb.startsWith('message.') ? { ...s, reader: true } : s; + const result = this.#dispatch(view, request.verb, args); this.#secretCheck(result); return result; }); @@ -877,14 +893,29 @@ export class Broker { } case 'message.send': { keys(a, ['to', 'body', 'class', 'in_reply_to', 'corrects', 'decision'], ['to', 'body']); - if (!s.human) { + if (s.tester) { + // A tester writes to a role only, says so in its first words, and cites no decision. + // It consumes no authority and writes no human.input. + keys(a, ['to', 'body', 'class', 'in_reply_to'], ['to', 'body']); + if (!Object.hasOwn(b.roles, a.to)) fail('unknown-role'); + if (typeof a.body !== 'string' || !a.body.startsWith(TESTER_PREFIX)) fail('tester-prefix'); + } else if (!s.human) { const withinRole = this.#classification(s, 'message.send') === 'within-role'; this.#consumeAuthority(s, 'message.send', { decision: withinRole ? null : (a.decision ?? null), target: a.to, }); - } else this.#human(s); - if (a.to !== 'human' && !Object.hasOwn(b.roles, a.to)) fail('unknown-role'); + // A role answers a tester only in reply to a tester's message. + if ( + a.to === 'tester' && + !(id(a.in_reply_to) && this.#store.get("SELECT 1 FROM messages WHERE id=? AND business=? AND from_role='tester'", a.in_reply_to, s.business)) + ) + fail('tester-reply-required'); + } else { + this.#human(s); + if (a.to === 'tester') fail('unknown-role'); + } + if (a.to !== 'human' && a.to !== 'tester' && !Object.hasOwn(b.roles, a.to)) fail('unknown-role'); string(a.body, 32768); if ( a.class !== undefined && @@ -915,7 +946,7 @@ export class Broker { this.#now(), s.business, s.human ? 'human' : s.role, - s.human ? 'human-cli' : s.run, + s.human ? 'human-cli' : s.tester ? 'tester-cli' : s.run, a.to, a.class ?? 'INFO', a.in_reply_to ?? null, @@ -933,7 +964,7 @@ export class Broker { case 'message.receive': { keys(a, []); if (s.human) this.#human(s); - else this.#agent(s); + else if (!s.tester) this.#agent(s); const role = s.human ? 'human' : s.role; const rows = this.#store.all( "SELECT * FROM messages m WHERE business=? AND to_role=? AND NOT EXISTS(SELECT 1 FROM deliveries d WHERE d.message=m.id AND d.op IN ('delivered','read')) ORDER BY seq LIMIT 100", @@ -944,8 +975,8 @@ export class Broker { this.#delivery( m.id, 'delivered', - s.human ? null : this.#holder(s.business, s.role), - s.human ? 'cli' : 'broker', + s.human || s.tester ? null : this.#holder(s.business, s.role), + s.human || s.tester ? 'cli' : 'broker', ); return rows.map((m) => ({ ...m, @@ -960,7 +991,7 @@ export class Broker { case 'message.read': { keys(a, ['id'], ['id']); if (s.human) this.#human(s); - else this.#agent(s); + else if (!s.tester) this.#agent(s); const m = this.#store.get('SELECT * FROM messages WHERE business=? AND id=?', s.business, a.id); if (!m || m.to_role !== (s.human ? 'human' : s.role)) fail('message-not-found'); const delivery = this.#store.get( @@ -969,7 +1000,7 @@ export class Broker { ); if (!delivery || delivery.holder_run !== (s.run ?? null)) fail('not-recipient'); if (!this.#store.get("SELECT seq FROM deliveries WHERE message=? AND op='read'", m.id)) - this.#delivery(m.id, 'read', s.human ? null : this.#holder(s.business, s.role)); + this.#delivery(m.id, 'read', s.human || s.tester ? null : this.#holder(s.business, s.role)); return { read: true }; } // Row 93 (#1587): the coder asks a reviewer role to review the task's latest pushed commit. diff --git a/packages/bus/src/client.mjs b/packages/bus/src/client.mjs index 52d46be3d..11543b5b3 100644 --- a/packages/bus/src/client.mjs +++ b/packages/bus/src/client.mjs @@ -1,15 +1,20 @@ import { connect } from 'node:net'; import { BusError } from './broker.mjs'; export class Client { - constructor({ path, cap, human, timeout = 5000 }) { + constructor({ path, cap, human, tester, timeout = 5000 }) { this.path = path; this.cap = cap; this.human = human; + this.tester = tester; this.timeout = timeout; } call(verb, args = {}) { const request = - JSON.stringify({ ...(this.human ? { human: this.human } : { cap: this.cap }), verb, args }) + '\n'; + JSON.stringify({ + ...(this.human ? { human: this.human } : this.tester ? { tester: this.tester } : { cap: this.cap }), + verb, + args, + }) + '\n'; if (Buffer.byteLength(request) > 65536) return Promise.reject(new BusError('request-too-large')); return new Promise((resolve, reject) => { const socket = connect(this.path); diff --git a/packages/bus/src/runtime.mjs b/packages/bus/src/runtime.mjs index a4912853f..dbb09e9b6 100644 --- a/packages/bus/src/runtime.mjs +++ b/packages/bus/src/runtime.mjs @@ -5,6 +5,7 @@ import { Broker, BusError } from './broker.mjs'; import { Credentials } from './credentials.mjs'; import { serve } from './server.mjs'; import { verifyHuman } from './human.mjs'; +import { verifyTester } from './tester.mjs'; // Trusted host API. S1 supplies resolved definitions, S6 supplies launch records and ends them. // Neither a business-file writer nor a socket-accessible configuration endpoint. // `tasks` is S3's adapter factory: ({broker, credentials, businesses}) => {handle, timeout, close}. @@ -72,6 +73,15 @@ export async function startBroker({ if (!human) throw new BusError('unknown-business'); return broker.bindHuman({ business, human, via: 'cli', outsideAgent: true }); }, + // Row 110 (#1604): a tester proves its CLI process and that no managed run is above it. + // The broker then binds only if the business sets `bus.tester` to true. + authenticateTester: (proof) => { + const { business } = verifyTester(proof, { + cliPath: fileURLToPath(new URL('./tester-cli.mjs', import.meta.url)), + launches: launchRecords, + }); + return broker.bindTester({ business }); + }, }); // S6 launcher operations. The host decides capacity and the instance list; the broker // authorizes role.launch and keeps the evidence, refusals included. diff --git a/packages/bus/src/server.mjs b/packages/bus/src/server.mjs index 860d88bd0..b6517bfc3 100644 --- a/packages/bus/src/server.mjs +++ b/packages/bus/src/server.mjs @@ -4,7 +4,8 @@ import { BusError, TASK_VERBS } from './broker.mjs'; const LIMIT = 65536; // One request per connection. There is deliberately no reconnect/retry or SQL verb. // `tasks` ({handle, timeout}) is the trusted S3 adapter; only the eight task verbs reach it. -export async function serve({ broker, path, authenticateHuman = null, timeout = 5000, tasks = null }) { +// A request carries exactly one of cap, human (row S4) and tester (row 110) proof. +export async function serve({ broker, path, authenticateHuman = null, authenticateTester = null, timeout = 5000, tasks = null }) { try { lstatSync(path); throw new BusError('socket-exists'); @@ -44,9 +45,9 @@ export async function serve({ broker, path, authenticateHuman = null, timeout = !r || Array.isArray(r) || typeof r !== 'object' || - Object.keys(r).some((k) => !['cap', 'human', 'verb', 'args'].includes(k)) || + Object.keys(r).some((k) => !['cap', 'human', 'tester', 'verb', 'args'].includes(k)) || typeof r.verb !== 'string' || - Boolean(r.cap) === Boolean(r.human) + [r.cap, r.human, r.tester].filter(Boolean).length !== 1 ) throw new BusError('invalid-envelope'); let cap = r.cap; @@ -54,6 +55,10 @@ export async function serve({ broker, path, authenticateHuman = null, timeout = if (!authenticateHuman) throw new BusError('human-required'); transient = cap = authenticateHuman(r.human); } + if (r.tester) { + if (!authenticateTester) throw new BusError('tester-required'); + transient = cap = authenticateTester(r.tester); + } if (typeof cap !== 'string') throw new BusError('unauthenticated'); if (tasks && TASK_VERBS.includes(r.verb)) { const own = transient; diff --git a/packages/business/README.md b/packages/business/README.md index 8c7a43350..f36ded9eb 100644 --- a/packages/business/README.md +++ b/packages/business/README.md @@ -141,6 +141,10 @@ each layer that sets one narrows it, and the result also narrows the role definition. `limits.authority` is an allowlist, so a role action it leaves out becomes gated. +`bus.tester` is business-only and defaults to false. Set to `true`, the bus +binds a tester from `tester-cli.mjs` for PM tests; it reads the value at bus +start, so a change takes a bus restart. + `resolve` prints `provenance` beside `vars`. For a plain key it's the layer that set the value (`default`, `system`, `business:`, `project:`, `project::roles.`, diff --git a/packages/business/src/vars.mjs b/packages/business/src/vars.mjs index 69b6ea575..c7e8d0a67 100644 --- a/packages/business/src/vars.mjs +++ b/packages/business/src/vars.mjs @@ -96,6 +96,8 @@ export const REGISTRY = Object.freeze({ return v; }), "gitea.baseUrl": def(["business"], baseUrl), + // On: the bus binds a tester from tester-cli.mjs for PM tests (row 110). Read at bus start. + "bus.tester": def(["business"], boolean, { default: false }), // Project. "tracker.project": def(["project"], (v, key) => requirePositiveInt(v, `variable ${key}`)), diff --git a/packages/business/tests/vars.test.mjs b/packages/business/tests/vars.test.mjs index ab7d2b3cd..b5db7ef8c 100644 --- a/packages/business/tests/vars.test.mjs +++ b/packages/business/tests/vars.test.mjs @@ -33,7 +33,7 @@ test("unknown keys and wrong layers refuse", () => { test("types", () => { const ok = (vars, layer) => assert.deepEqual({ ...checkVars(vars, layer, "v") }, vars); - ok({ "tracker.baseUrl": "https://tasks.example:3456", "tracker.reconcileMinutes": 1, "tracker.pollSeconds": 10, "human.discordUserId": "123456789012345678" }, "business"); + ok({ "tracker.baseUrl": "https://tasks.example:3456", "tracker.reconcileMinutes": 1, "tracker.pollSeconds": 10, "human.discordUserId": "123456789012345678", "bus.tester": true }, "business"); ok({ "tracker.project": 3, "git.workingBranch": "refactor", "git.protectedBranches": ["main", "next"], suites: ["scripts/test-task.sh"], "issues.repo": "mosaicstack/stack", "tracker.descendants": true }, "project"); ok({ harness: "claude-code", model: "claude-opus-5-5", thinking: "high", "limits.tools": [], "limits.network": "none", "limits.authority": ["message.send"] }, "agent"); @@ -53,6 +53,11 @@ test("types", () => { [{ "tracker.descendants": "true" }, "project", /true or false/], [{ "tracker.descendants": 1 }, "project", /true or false/], [{ "tracker.descendants": true }, "business", /business layer \(allowed: project\)/], + [{ "bus.tester": "true" }, "business", /true or false/], + [{ "bus.tester": 1 }, "business", /true or false/], + [{ "bus.tester": true }, "project", /project layer \(allowed: business\)/], + [{ "bus.tester": true }, "agent", /agent layer \(allowed: business\)/], + [{ "bus.tester": true }, "system", /system layer \(allowed: business\)/], [{ "git.workingBranch": "-x" }, "project", /git branch name/], [{ "git.workingBranch": "a..b" }, "project", /git branch name/], [{ "git.workingBranch": "x.lock" }, "project", /git branch name/], @@ -83,6 +88,8 @@ test("merge: defaults, then the most specific layer wins", () => { assert.equal(vars["tracker.kind"], "vikunja"); assert.equal(vars["tracker.descendants"], false); assert.equal(provenance["tracker.descendants"], "default"); + assert.equal(vars["bus.tester"], false); + assert.equal(provenance["bus.tester"], "default"); assert.equal(vars.harness, undefined); assert.equal(provenance.harness, undefined); }); diff --git a/packages/cli/README.md b/packages/cli/README.md index 3111fe03d..e23d8fb0d 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -80,6 +80,16 @@ mosaic launches list [--json] the bus (see "Sessions" below). `stop` refuses inside an agent run; `launches list` does not, because the file is readable to the same user anyway. +- `--tester` (row 110, #1604) runs `inbox`, `tasks`, `agents`, `trail`, + `talk` and `follow` as the business's tester through + `packages/bus/src/tester-cli.mjs` instead of as the human. It is the one + exception to the agent check above: Mos or a T3 seat may run it for a PM + test, and the broker still refuses it under a managed run + (`tester-required`) or while the business var `bus.tester` isn't `true` + (`tester-off`), each exit 3. `talk --tester` text must start with + `[mos test]`, else exit 2 before any call. The role's reply comes back to + the tester, not to the human's inbox. `decide`, `stop` and `launches` + take no `--tester` (exit 4). ## The app diff --git a/packages/cli/src/cli.mjs b/packages/cli/src/cli.mjs index fcd7c0132..a547e36c7 100644 --- a/packages/cli/src/cli.mjs +++ b/packages/cli/src/cli.mjs @@ -15,7 +15,7 @@ import { fileURLToPath } from "node:url"; import { BINDING_NAME } from "../../discord/src/binding.mjs"; import { CliError } from "./errors.mjs"; import { bootConfig, loadSystem, socketPath } from "./config.mjs"; -import { humanTransport, refuseInsideAgent } from "./transport.mjs"; +import { humanTransport, refuseInsideAgent, testerTransport } from "./transport.mjs"; import { formatAgents, formatDecision, formatInbox, formatTasks, formatTrail, shortId } from "./format.mjs"; import { hostStatus, readHostState, startHost, stopHost } from "./host.mjs"; import { createLauncher } from "./launcher.mjs"; @@ -24,13 +24,13 @@ import { readSessions, stopSession } from "../../seat/src/session.mjs"; import { SeatError } from "../../seat/src/seat.mjs"; export const USAGE = `usage: - mosaic inbox [--business ] [--json] + mosaic inbox [--business ] [--json] [--tester] mosaic decide