diff --git a/packages/bus/README.md b/packages/bus/README.md index fff969101..5b57b7fc8 100644 --- a/packages/bus/README.md +++ b/packages/bus/README.md @@ -196,8 +196,17 @@ must not invoke the human CLI or work around its proof. Human resolutions and launch toggles append `human.input`. Agent capabilities and read-only WebUI capabilities cannot reach these operations. Reader -capabilities expose the human inbox plus tasks/agents/trail for one business; -they do not confer mutation or resolution authority. +capabilities expose the human inbox plus tasks/agents/trail/decisions for one +business; they do not confer mutation or resolution authority. + +`decisions` (row 57, #1546) is for human and reader sessions only; a role +session gets `human-required`, so it can't list another role's open +decisions. It takes `{list}`, nothing else. `roles` lists the business's open +decisions routed to a role, never to the human, oldest first. `recent` lists +its last `RECENT` (50) closed decisions of every route, newest close first, +each with `closed`, the closing `decision_events` row. Every row is the +decision as `trail` gives the same session for its id, so the lists add no +data a reader couldn't already read one id at a time; they add the listing. ## Shared reads and adapter boundary @@ -207,6 +216,7 @@ import {views} from './src/views.mjs'; const read = views(new Client({path,cap})); await read.inbox(); await read.tasks(); await read.agents(); await read.trail('vikunja:3/41'); +await read.decisions('roles'); await read.decisions('recent'); ``` S4 and S5 must import this client-only module. Neither opens `bus.sqlite`. diff --git a/packages/bus/src/broker.mjs b/packages/bus/src/broker.mjs index 0ebc8ff8d..84db87997 100644 --- a/packages/bus/src/broker.mjs +++ b/packages/bus/src/broker.mjs @@ -38,6 +38,8 @@ const GATED = new Set([ 'role.revoke', ]); const CLOSED = "('resolved','withdrawn','expired')"; +// How many closed decisions `decisions` with list 'recent' returns, newest close first. +export const RECENT = 50; export const TASK_VERBS = Object.freeze(ACTIONS.filter((a) => a.startsWith('task.'))); const SELF_KINDS = new Set(['task.created', 'task.assigned', 'task.state', 'task.closed', 'task.conflict']); const POLL_KINDS = new Set([ @@ -466,7 +468,8 @@ export class Broker { if (!object(args)) fail('invalid-request'); this.#secretCheck(args); return this.#store.transaction(() => { - if (s.reader && !['inbox', 'agents', 'tasks', 'trail'].includes(request.verb)) fail('read-only'); + if (s.reader && !['inbox', 'agents', 'tasks', 'trail', 'decisions'].includes(request.verb)) + fail('read-only'); const result = this.#dispatch(s, request.verb, args); this.#secretCheck(result); return result; @@ -833,6 +836,27 @@ export class Broker { .map((r) => ({ ...r, fields: JSON.parse(r.fields) })); return this.#trail(s, identifier(a.subject)); } + // Row 57 (#1546): the Inbox's "Routed to roles" and "Recent" lists, for human and + // reader sessions only. Each row is what trail gives the same session for that id. + case 'decisions': { + if (!s.human && !s.reader) fail('human-required'); + keys(a, ['list'], ['list']); + if (a.list === 'roles') + return this.#store + .all( + `SELECT d.* FROM decisions d WHERE business=? AND route_to<>'human' AND NOT EXISTS(SELECT 1 FROM decision_events x WHERE x.decision=d.id AND x.op IN ${CLOSED}) ORDER BY seq`, + s.business, + ) + .map((d) => this.#decisionView(d)); + if (a.list === 'recent') + return this.#store + .all( + `SELECT d.* FROM decisions d JOIN decision_events x ON x.decision=d.id AND x.op IN ${CLOSED} WHERE d.business=? ORDER BY x.seq DESC LIMIT ${RECENT}`, + s.business, + ) + .map((d) => ({ ...this.#decisionView(d), closed: this.#closed(d.id) })); + fail('invalid-request'); + } default: fail('unknown-verb'); } diff --git a/packages/bus/src/views.mjs b/packages/bus/src/views.mjs index d161c2cd4..2bffda6bc 100644 --- a/packages/bus/src/views.mjs +++ b/packages/bus/src/views.mjs @@ -5,5 +5,6 @@ export function views(client) { tasks: () => client.call('tasks'), agents: () => client.call('agents'), trail: (subject) => client.call('trail', { subject }), + decisions: (list) => client.call('decisions', { list }), }); } diff --git a/packages/webui/README.md b/packages/webui/README.md index 20a9a884b..e43118d50 100644 --- a/packages/webui/README.md +++ b/packages/webui/README.md @@ -193,17 +193,22 @@ configured board URL for the page's error message. No scanner, registration, session reader or transport is implemented here. The session reader is `packages/conversation`, served by the board. -GET `/api/bus/inbox`, `/api/bus/tasks`, `/api/bus/agents` and -`/api/bus/trail?subject=` are the bus reads (`src/bus.mjs`). Any other -method is 405. The subject must match the broker's identifier rule, else 400. +GET `/api/bus/inbox`, `/api/bus/tasks`, `/api/bus/agents`, +`/api/bus/trail?subject=` and `/api/bus/decisions?list=roles|recent` are +the bus reads (`src/bus.mjs`). Any other method is 405. The subject must match +the broker's identifier rule, and the list must be `roles` or `recent`, else +400. `decisions` (row 57, #1546) is for the Inbox tabs "Routed to roles" and +"Recent"; "Human and reader paths" in `packages/bus/README.md` has what each +list holds. Each read runs the S4 human transport (`packages/bus/src/human-cli.mjs`) as a child process, so a slow read doesn't stall the server. The bus checks that child's ancestry, which ends at the Console process; it never sees the browser or any other client of the port. So while the Console runs, anything that can connect to its port (7330 by default) reads what a reader capability reads ("Human and reader paths" in `packages/bus/README.md`): -the inbox, tasks, agents and trails. That includes a T3 seat using `curl` -and a managed S6 session, since S6 doesn't confine the network. No route +the inbox, tasks, agents, trails and the two decision lists. That includes +a T3 seat using `curl` and a managed S6 session, since S6 doesn't confine +the network. No route writes (Q4), so the exposure is reads only. Slice 1 doesn't change this (Filbert F1 on #1522). Answers are `{ rows, at }`; failures are `{ error, message }` with 403 for a refusal, 503 for no bus or no answer, and 502 for an answer diff --git a/packages/webui/src/bus.mjs b/packages/webui/src/bus.mjs index 290541efa..8fee21a6e 100644 --- a/packages/webui/src/bus.mjs +++ b/packages/webui/src/bus.mjs @@ -1,8 +1,9 @@ // Slice 1 S5 (#1522): the Console's read of the bus. It runs the S4 human // transport (packages/bus/src/human-cli.mjs, lead decision 70) as a child // with spawn, not spawnSync, so one slow read never stalls the HTTP server. -// Only the four verbs of the Q1 module (lead decision 56) are reachable: -// the WebUI writes nothing, not even seen (Q4). The bus does the proof; a +// Only the four verbs of the Q1 module (lead decision 56) and row 57's +// `decisions` read are reachable: the WebUI writes nothing, not even seen +// (Q4). The bus does the proof; a // server started inside an agent run gets `human-required` from the broker. import { spawn } from 'node:child_process'; diff --git a/packages/webui/src/serve.mjs b/packages/webui/src/serve.mjs index 8e89e3ed5..4d51942a5 100644 --- a/packages/webui/src/serve.mjs +++ b/packages/webui/src/serve.mjs @@ -46,16 +46,22 @@ async function body(req) { return bytes; } // Slice 1 S5 (#1522): GET only, the four Q1 verbs, nothing else (lead decision 56, Q4). -const BUS_VERBS = ['inbox', 'tasks', 'agents', 'trail']; +// Row 57 (#1546) adds `decisions`, also a read, for the Inbox tabs. +const BUS_VERBS = ['inbox', 'tasks', 'agents', 'trail', 'decisions']; +const DECISION_LISTS = ['roles', 'recent']; async function busRead(res, bus, verb, search) { - let subject; + let arg; if (verb === 'trail') { - subject = new URLSearchParams(search).get('subject'); - if (!subjectOk(subject)) return json(res, 400, { error: 'invalid-request', message: 'subject must be a decision id, message id or task ref' }); + arg = new URLSearchParams(search).get('subject'); + if (!subjectOk(arg)) return json(res, 400, { error: 'invalid-request', message: 'subject must be a decision id, message id or task ref' }); + } + if (verb === 'decisions') { + arg = new URLSearchParams(search).get('list'); + if (!DECISION_LISTS.includes(arg)) return json(res, 400, { error: 'invalid-request', message: 'list must be roles or recent' }); } if (!bus) return json(res, 503, { error: 'not-configured', message: 'the Console has no bus configured' }); try { - const rows = await bus[verb](subject); + const rows = await bus[verb](arg); if (!Array.isArray(rows)) throw new BusReadError('invalid-response'); return json(res, 200, { rows, at: new Date().toISOString() }); } catch (err) { diff --git a/packages/webui/tests/bus.test.mjs b/packages/webui/tests/bus.test.mjs index b141b5cc6..2d9ca6baa 100644 --- a/packages/webui/tests/bus.test.mjs +++ b/packages/webui/tests/bus.test.mjs @@ -87,13 +87,52 @@ test('bus routes serve the four reads from the reader view, unescaped JSON for t assert.deepEqual([...new Set(calls)].sort(), ['agents', 'inbox', 'tasks', 'trail']); }); -test('bus routes are GET-only, have no write verb, and keep the same-origin checks', async t => { +// Row 57 (#1546): the Inbox's "Routed to roles" and "Recent" lists. +test('decisions route serves the roles and recent lists from the reader view', async t => { + const store = new Store(tmp(t, 'webui-bus-decisions-')); + t.after(() => store.close()); + const b = new Broker({ store, businesses }); + const coder = b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi', address: 'coder-run' }); + b.request(coder, { verb: 'role.claim' }); + const raise = args => b.request(coder, { verb: 'decision.raise', args: { options: [{ key: 'yes', text: 'Allow' }, { key: 'no', text: 'Decline' }], recommendation: 'no', blocking: false, ...args } }); + const routed = raise({ action: 'task.scope.change', domain: 'technical', target: 'vikunja:32/8', question: `Split it? ${HOSTILE}` }); + const human = raise({ action: 'deploy', target: 'release-1', question: 'Deploy?' }); + const closed = raise({ action: 'message.send', question: 'Send?', choice: 'yes' }); + const reader = b.bindReader({ business: 'demo' }); const calls = []; - const bus = Object.fromEntries(['inbox', 'tasks', 'agents', 'trail'].map(v => [v, async () => { calls.push(v); return []; }])); + const bus = views({ call: async (verb, args = {}) => { calls.push([verb, args]); return structuredClone(b.request(reader, { verb, args })); } }); const web = await startServer({ port: 0, board: 'http://127.0.0.1:9', bus }); t.after(() => close(web)); const base = `http://127.0.0.1:${web.address().port}`; - for (const method of ['POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS']) assert.equal((await fetch(base + '/api/bus/inbox', { method })).status, 405, method); + const read = async path => { const r = await fetch(base + path); return { status: r.status, cache: r.headers.get('cache-control'), body: await r.json() }; }; + + const roles = await read('/api/bus/decisions?list=roles'); + assert.equal(roles.status, 200); assert.equal(roles.cache, 'no-store'); + assert.match(roles.body.at, /^\d{4}-\d\d-\d\dT/); + assert.deepEqual(roles.body.rows.map(r => [r.id, r.route_to]), [[routed.id, 'cto']]); + assert.ok(roles.body.rows[0].question.endsWith(HOSTILE)); + const recent = await read('/api/bus/decisions?list=recent'); + assert.deepEqual(recent.body.rows.map(r => [r.id, r.closed.op, r.closed.choice]), [[closed.id, 'resolved', 'yes']]); + assert.deepEqual((await read('/api/bus/inbox')).body.rows.map(r => r.id), [human.id]); + assert.deepEqual(calls.filter(([v]) => v === 'decisions'), [['decisions', { list: 'roles' }], ['decisions', { list: 'recent' }]]); + + // A missing or unknown list never reaches the bus. + const before = calls.length; + for (const q of ['', '?list=', '?list=all', '?list=Roles', '?list=roles%00', '?subject=roles']) + assert.deepEqual([q, (await read('/api/bus/decisions' + q)).body.error], [q, 'invalid-request']); + assert.equal(calls.length, before); +}); + +test('bus routes are GET-only, have no write verb, and keep the same-origin checks', async t => { + const calls = []; + const bus = Object.fromEntries(['inbox', 'tasks', 'agents', 'trail', 'decisions'].map(v => [v, async () => { calls.push(v); return []; }])); + const web = await startServer({ port: 0, board: 'http://127.0.0.1:9', bus }); + t.after(() => close(web)); + const base = `http://127.0.0.1:${web.address().port}`; + for (const method of ['POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS']) { + assert.equal((await fetch(base + '/api/bus/inbox', { method })).status, 405, method); + assert.equal((await fetch(base + '/api/bus/decisions?list=roles', { method })).status, 405, method); + } for (const path of ['/api/bus/decide', '/api/bus/seen', '/api/bus/', '/api/bus/inbox/x', '/api/bus/request', '/api/bus/raise']) { assert.equal((await fetch(base + path)).status, 404, path); assert.equal((await fetch(base + path, { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' })).status, 404, path); @@ -176,6 +215,7 @@ test('busReader takes --business, else the live bus host, else refuses', async t const dataRoot = join(root, 'data'); const pinned = busReader({ dataRoot, socket: 'sock', business: 'pinned', cli }); assert.equal((await pinned.inbox()).request.business, 'pinned'); + assert.deepEqual((await pinned.decisions('recent')).request, { business: 'pinned', verb: 'decisions', args: { list: 'recent' } }); const reader = busReader({ dataRoot, socket: 'sock', cli }); const code = () => reader.agents().then(() => 'resolved', e => e.code);