Compare commits
268
Commits
sp2
...
account-remove
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ccd85604c6 | ||
|
|
e035b6dea9 | ||
|
|
735e828aa2 | ||
|
|
e698b7d785 | ||
|
|
8ed3e251be | ||
|
|
784748347b | ||
|
|
9869200a03 | ||
|
|
be7066b725 | ||
|
|
4aa008e9fe | ||
|
|
ca7f46e5d4 | ||
|
|
256e80f26b | ||
|
|
3225536adc | ||
|
|
9287e2fac7 | ||
|
|
9d02ab4a69 | ||
|
|
5210522e13 | ||
|
|
8e9b05de8d | ||
|
|
0f07301607 | ||
|
|
a4d06b95c6 | ||
|
|
f2463ab36d | ||
|
|
7afe892e94 | ||
|
|
4a3f501e13 | ||
|
|
c065034a6e | ||
|
|
83250ef36b | ||
|
|
b432f9cd6f | ||
|
|
e0065b6a44 | ||
|
|
3f39ed3f11 | ||
|
|
28a5d38349 | ||
|
|
824b1213c1 | ||
|
|
ca7388743b | ||
|
|
59af86a5ee | ||
|
|
d6d3271808 | ||
|
|
4a7815d29e | ||
|
|
af8c2f9777 | ||
|
|
b156727c00 | ||
|
|
29daa4c4bf | ||
|
|
c8db3665cf | ||
|
|
f2c262cf40 | ||
|
|
ebfbdb13a0 | ||
|
|
4d378fc38b | ||
|
|
ab8633ede8 | ||
|
|
6ad11e6993 | ||
|
|
e52a6b714f | ||
|
|
2d065b936e | ||
|
|
098b3771c0 | ||
|
|
eac42663ba | ||
|
|
b41bc00b26 | ||
|
|
749d6d98f5 | ||
|
|
fa5ce3ae65 | ||
|
|
d009ccaec4 | ||
|
|
2613fb1c4c | ||
|
|
df26085303 | ||
|
|
7c1b87d154 | ||
|
|
8908df113e | ||
|
|
b8354e4304 | ||
|
|
f62b1b9518 | ||
|
|
7355b233a8 | ||
|
|
88d08c8437 | ||
|
|
4377941bd4 | ||
|
|
de39750ebb | ||
|
|
3b17326b74 | ||
|
|
d03ae494e8 | ||
|
|
e1b56d9ea7 | ||
|
|
e6f6f5445b | ||
|
|
55f9652f6a | ||
|
|
9e434c0e9c | ||
|
|
e732ae7450 | ||
|
|
66255c9bf2 | ||
|
|
cecc901854 | ||
|
|
81ca8399a2 | ||
|
|
37c3854849 | ||
|
|
db2dc96aa2 | ||
|
|
f666590d54 | ||
|
|
2f20346719 | ||
|
|
524132b42b | ||
|
|
ae53b1988a | ||
|
|
940ac53b65 | ||
|
|
799df9cb9f | ||
|
|
23964b1224 | ||
|
|
d692ca9b81 | ||
|
|
57e86d9867 | ||
|
|
d502a75e57 | ||
|
|
15801ccb07 | ||
|
|
50ca7f0341 | ||
|
|
b9b3e56821 | ||
|
|
32b56b8351 | ||
|
|
78d1ef7705 | ||
|
|
af1bbf7859 | ||
|
|
a7362a8fc5 | ||
|
|
5a07f9e1f9 | ||
|
|
07eca4a670 | ||
|
|
fc9e4dfca8 | ||
|
|
10a35ce65a | ||
|
|
3b9cac5452 | ||
|
|
bc6d3565b0 | ||
|
|
72ea7272ec | ||
|
|
27a359590e | ||
|
|
6c5ec23b47 | ||
|
|
dd0a8612c8 | ||
|
|
9276277970 | ||
|
|
517a3fe467 | ||
|
|
149712816b | ||
|
|
4f63176676 | ||
|
|
1e0aba8b37 | ||
|
|
680bb90af3 | ||
|
|
6a24cf743d | ||
|
|
06ee172a9d | ||
|
|
66676804eb | ||
|
|
a3ad6578c4 | ||
|
|
77a951cfe7 | ||
|
|
cd58ac23cd | ||
|
|
fbaae25d06 | ||
|
|
c9d64a341e | ||
|
|
4cbf3f07b0 | ||
|
|
48a2ed675c | ||
|
|
e70b6bd684 | ||
|
|
2db4f24fd8 | ||
|
|
9bfe101176 | ||
|
|
869d259036 | ||
|
|
8200ea238c | ||
|
|
196505e6ef | ||
|
|
f7c0e4f46c | ||
|
|
5c975faddd | ||
|
|
462215b4bd | ||
|
|
d0293d6602 | ||
|
|
912138ff61 | ||
|
|
4a8d310cd5 | ||
|
|
c28ca57af6 | ||
|
|
255c1575bb | ||
|
|
0ba50111f6 | ||
|
|
dab07f0ff5 | ||
|
|
561ff77e48 | ||
|
|
e1642be760 | ||
|
|
b23ccdfc12 | ||
|
|
57af33630d | ||
|
|
17b972b92d | ||
|
|
d531d7181d | ||
|
|
67e1ff193d | ||
|
|
9a3004cba4 | ||
|
|
4b833d23e9 | ||
|
|
05be6c62bd | ||
|
|
c9fad00d25 | ||
|
|
4c8527fcd3 | ||
|
|
2d44750b96 | ||
|
|
9343baffff | ||
|
|
38ea639d70 | ||
|
|
fc558df31a | ||
|
|
56cc7b59dd | ||
|
|
c641dbfbc9 | ||
|
|
eb1146ebad | ||
|
|
4b5d027361 | ||
|
|
80ce06851d | ||
|
|
b39d5e2ff3 | ||
|
|
c85c768a4a | ||
|
|
b7649ecd73 | ||
|
|
0354452971 | ||
|
|
bbbefb3f28 | ||
|
|
2f91ff5bea | ||
|
|
1ca3a38f86 | ||
|
|
cb80b2b8a8 | ||
|
|
fd300b4fe3 | ||
|
|
a88302d6eb | ||
|
|
820f45e474 | ||
|
|
2e9a2068bd | ||
|
|
fe6c22e0f2 | ||
|
|
9eed814d3c | ||
|
|
7ee606cda2 | ||
|
|
b2d52b57c1 | ||
|
|
cbf1836bd7 | ||
|
|
6fe3625fae | ||
|
|
24551c9eea | ||
|
|
cad9cd057f | ||
|
|
4f24501801 | ||
|
|
02e053866a | ||
|
|
ff096afa27 | ||
|
|
70af20e29d | ||
|
|
d92cdefc1d | ||
|
|
9994f89790 | ||
|
|
4a9c7a931c | ||
|
|
5c7194e05b | ||
|
|
f0bf85befe | ||
|
|
c26879637a | ||
|
|
2fc97a3edb | ||
|
|
68df55130e | ||
|
|
6ad1d33dd1 | ||
|
|
5fba4726dc | ||
|
|
69ce460f91 | ||
|
|
307f88be3e | ||
|
|
bfd3ed6811 | ||
|
|
5b6b39f080 | ||
|
|
2d7d7be5ca | ||
|
|
f73c761ad3 | ||
|
|
8b4a26dd2b | ||
|
|
11d2b43b3a | ||
|
|
8aefc0f7a9 | ||
|
|
9cff3255e6 | ||
|
|
d50d79cddd | ||
|
|
fb3bd16052 | ||
|
|
13615e12b9 | ||
|
|
bd29e756a5 | ||
|
|
6bd9be78ac | ||
|
|
3a84f46b6e | ||
|
|
208439814a | ||
|
|
7c6571768f | ||
|
|
3597e0daf6 | ||
|
|
3116f43e65 | ||
|
|
c66f1ec00c | ||
|
|
9400e0d94d | ||
|
|
a9912983e6 | ||
|
|
626321608f | ||
|
|
5f965cdb30 | ||
|
|
8296d33100 | ||
|
|
69ac78b97e | ||
|
|
a2a7df73df | ||
|
|
789e2bcdd4 | ||
|
|
02df2a3a25 | ||
|
|
b982404963 | ||
|
|
5dd940165d | ||
|
|
ed4f45ed23 | ||
|
|
79df01f1de | ||
|
|
d7b98b4698 | ||
|
|
0f29fe0d92 | ||
|
|
32be3a01bc | ||
|
|
f97e0d3aeb | ||
|
|
eebdb2f0ce | ||
|
|
5373b110ac | ||
|
|
3885f4628b | ||
|
|
ba9933309b | ||
|
|
0785af8129 | ||
|
|
d1b01329bc | ||
|
|
c7aecbffa9 | ||
|
|
bb2f79f3d9 | ||
|
|
e24e7e93a2 | ||
|
|
387f73a7a4 | ||
|
|
61371adb10 | ||
|
|
b913f0f4c8 | ||
|
|
bfd2cc5e79 | ||
|
|
3e53af3941 | ||
|
|
359b3299b4 | ||
|
|
65f7288cea | ||
|
|
211b32ef1a | ||
|
|
163972e08e | ||
|
|
23e504f331 | ||
|
|
23b5e24ebc | ||
|
|
266f3a45ff | ||
|
|
0c35eeed9d | ||
|
|
e6ab8bc1fd | ||
|
|
0251acdad9 | ||
|
|
13560660ae | ||
|
|
08e50e513f | ||
|
|
a9d72b3cd1 | ||
|
|
4b35e3c1d2 | ||
|
|
56aeefb370 | ||
|
|
fdbc34413b | ||
|
|
c5bf71ab08 | ||
|
|
dbb69c8a41 | ||
|
|
34dd331016 | ||
|
|
808b2b7cbf | ||
|
|
5ffe38e3b2 | ||
|
|
991eb8c332 | ||
|
|
504fcd9220 | ||
|
|
154c2216f3 | ||
|
|
9a649abf85 | ||
|
|
1ba3288bec | ||
|
|
079ced93f3 | ||
|
|
95c2ce1435 | ||
|
|
2196d37a47 | ||
|
|
ef1d3a4583 | ||
|
|
90245765f2 |
No files matched your search
@@ -222,8 +222,18 @@ Compacted, restarted, or new? Nothing that matters is lost: this file +
|
||||
`git log --oneline -10` + the suites reconstruct the full state. **Never
|
||||
guess** — verify with the suites; the run records and logs hold the receipts.
|
||||
|
||||
## Version pin
|
||||
## Harness versions
|
||||
|
||||
`@earendil-works/pi-coding-agent` is pinned exactly (see `package.json` /
|
||||
`RELEASE`); never install unversioned. Release identity: `RELEASE` file
|
||||
(0.0.X until declared stable); image tags derive from it.
|
||||
Harness binaries are not pinned (Jason, 2026-11-19). pi runs from
|
||||
`MOSAIC_PI_BIN`, else the first `pi` on PATH, else the repository's
|
||||
`node_modules`. `package.json` holds a floor range, and `package-lock.json`
|
||||
records the version `npm ci` installs and the worker image carries. Nothing
|
||||
refuses a pi for its version. Before a session or conversation starts, a
|
||||
fit check proves the flags and behaviours Mosaic needs and refuses with a
|
||||
plain line when one is missing. The bus host repeats the check on a timer
|
||||
and when a binary changes. The version and a digest of the files that
|
||||
ran are recorded in the claim and the run record. A change under a live
|
||||
claim still refuses. Never install unversioned: a lock change is a
|
||||
reviewed commit that passes the fit check and the suites. Release
|
||||
identity: `RELEASE` file (0.0.X until declared stable). Image tags derive
|
||||
from it and the lock's resolved pi version.
|
||||
@@ -6347,3 +6347,9 @@ R5 quote fallback room (#797 D3, 2026-11-06, MVP3 lead): r4 review P2 found that
|
||||
2026-11-09 UTC, PRD 1.20 and defect l (#1873, #1886; PRs #1887 03fe321b1 and #1888 7a97cfc4f). PRD 1.20: Jason approved draft 2 at 22:57Z ("The file looks fine other than that"; the Montage module name stays deferred). The PRD now has 118 requirements, with REQ-WS-1..8, REQ-AGENT-1..3, REQ-SYS-1..2, REQ-ACCT-1..2 and REQ-SETUP-1..2 added and REQ-ASSIST-1, REQ-USER-1, REQ-TASK-1 and REQ-WEB-3 amended. Builder Codex Terra xhigh declined to commit because stack-copy failed 20/1 (node-gyp missing from the test Node's npm); that failure is pre-existing on 483eccb3d, so the lead committed. Sol review APPROVE 0 (word-for-word diff against draft 2, catalog order, both requirements tests). Defect l: before, an agent that declared a store credential made the next bus start refuse, because Credentials refuses a missing store root and only issue() created it. After: start creates the root 0700 when any reference names the store; a root that exists with loose modes or as a symlink still refuses. Two new credential-verbs tests fail on the parent. Bus suite 171 files one at a time, 1713/0 (Node 26.12.1, env -i). Sol review APPROVE 0. uiorg redeployed at 7a97cfc4f, 23:15Z. Follow-up: the stack-copy node-gyp test environment. Reports: /mnt/storage/scratch/mosaic-lead/reports/{prd120-land,prd120-review,defectl-review}.md.
|
||||
|
||||
2026-11-10 UTC, unified workspace U1 and SYS1 (#1873; PRs #1889 d35447005 and #1890 2c6556ab1). U1, before: each org page read only its own bus and nothing read every org at once. After: /api/workspace returns projects, sessions, inbox, tasks and metrics from every org the owner holds, each row tagged with its org and each org with a state (ok, starting, down, timeout, invalid); `mosaic console restart` and `mosaic org reload <org>` send the F3 routes. Sol review CHANGES 0/2 on 2109458b3: malformed data from one org turned the whole read into a 503, and the per-org deadline left sockets and transport children running. Terra fix 0672d5fe8 validates each org's result inside its boundary and carries an absolute deadline and AbortSignal through the status check and transport; the new workspace-failures test is 0/10 on the parent and 10/0 on the fix. The lead re-pinned the webui bus.mjs hash in bus channel-proof (the deliberate signal/deadline change; nonces, argv and limits unchanged) and amended as d35447005. SYS1, before: no system-type kind. After: a system-type plug-in kind with five modules (business-management, coding, creative-writing, marketing, montage), an org enable list under plugins.system-type, owner-only project selection with an append-only project_system_types table (migration 3t-3u) and CLI verbs; the modules grant no tools yet (REQ-SYS-2 deferred). Sol review CHANGES 1/2 on a9629f332: the restore guard did not count project_system_types, the CLI channel did not admit the two project verbs, and the #811 migration-reader pin still expected 18 steps. Lead fix 2c6556ab1 (rebased on d35447005) adds the table to COUNTED and LISTED with refusal and in-run recount tests, admits both verbs with a run()-level channel test (red on the unfixed channel), and moves the pin to 19 steps ending 3t-3u; fix files 323/0 and rebase overlap files 63/0 (Node 26.12.1, one file at a time, env -i). Full gate on a9629f332 4040/5: the two stack-release pins above, and tester-reads 3/3, which also fails on 1616d1dd6 (project.mjs:95 refuses an org --tester init; follow-up). Correction to the uiorg deploy at 00:50Z: the deploy script switched the tree without the offline schema migration, so all five buses refused to start (normal startup refuses an older schema by design). The lead ran migrate-schema.mjs for the five org stores (3t to 3u, all ready), restarted the buses (201 each) and added the migration to the deploy script with stop-on-refusal. test3 refused writer-locked and stays down, as before. Reports: /mnt/storage/scratch/mosaic-lead/reports/{u1-review,u1-fix,org-sys1-build,sys1-review}.md.
|
||||
|
||||
2026-11-13 UTC, starter packs replace system types (#1873; PRs #1949 c0a60ed3a, #1950 1e65ff3f8, #1951 755ba3fa8, #1952 1ced0e392, #1953 dfef78834, #1955 9a649abf8, #1956 991eb8c33, #1957 34dd33101, #1959 0251acdad and #1960 e6ab8bc1f; final e6ab8bc1f). Backfilled 2026-11-16 by lane docs-1116 from uidev/LEAD-1110.md and git; the Lead wrote no entry at the time. Before: SYS1 (PR #1890, 2c6556ab1) made system types a plug-in kind with five modules, an org enable list under plugins.system-type and an append-only project_system_types table (migration 3t-3u); the modules granted no tools. After: the design docs/plans/2026-11-12_starter-packs-design.md landed as PR #1949 (doc only) after three fix rounds; Jason's FULL SEND at 21:06Z on 11-12 answered Q1 to Q3: system-type removed outright with no legacy, SP10 dropped, SP9 deletes the kind and drops project_system_types. Slices in landing order: SP3 the org keys packs, board and workflows and the applyPack writer op; SP1 the starter-pack plug-in kind and its checker; SP7 workflow guidance in the harness prompt; SP2 the five shipped packs; SP4 pack, board and workflow verbs, routes and CLI words; SP6 board labels, pack picker and workflow editor on the WebUI organization page; SP5 the chief applies a pack's non-definition half and raises hires from its proposals; SP8 the owner saves an organization's shape as a template; SP9 system types removed outright and bus schema 3v-3w dropping project_system_types. Test counts, Lead check then lane gate: SP3 98 files 1294/0, 1828/1; SP1 51 files 708/0, 2854/1; SP7 1281/0, 2537/1; SP2 150/0, 1556/1 at 18add86cb before its rebase onto SP7; SP4 4848/2, 6049/1; SP6 371 files 4233/1, 8262/23; SP5 229 files 2721/0, 8061/2; SP8 415 files 4650/1, 8125/2; SP9 497 files 5542/1, 8655/28. Each single Lead check red is CHAT-01 (host Python lacks jsonschema), red on base too; SP4's second red was the fixture federation-host.mjs wrongly in the run list, and Lead check lists are filtered to *.test.mjs since. The lane gate reds the log names are base or environment reds: CHAT-01, 19 agents/*/work probes, budget-supervisor 3v, ui-browser SSO, six v1 .woodpecker files and one claude-session timing test. Every slice was deployed to uiorg 7350 and verified; at e6ab8bc1f (03:46Z on 11-13) the offline 3v-3w migration ran on all five live stores, 5 stop 200, 5 bus schema 3w ready, 5 setup 201, the system-types route and its js returned 404, and no org file named system-type, so no wipe was needed. Corrections: the Lead's 21:38Z carry that SP2 pack steps must drop id was reversed at 21:46Z: pack steps keep id (SP1 checker) and SP4 drops id when it copies a step into the org file. The Lead's SP7 brief misstated the workflow shape; the lane built to the SP3 loader, which was correct. SP5's last commit before landing changed the broker canonical string builder after its re-check with no suite test; the Lead compared it with organization canonicalJson (7/7 same) and accepted it, and the integer-key pin test is deferred. SP9's first gate, before its rebase, ran packages/cli/live/ui-browser.test.mjs, which binds its own fixture on 17443, a lane rule breach; nothing was listening before or after, and the file was left out of later gates. The 'bus recover' deploy step refuses rc=3 in the agent shell on every deploy since D1; it is harmless while bus stop succeeds. Reports: /mnt/storage/scratch/mosaic-lead/reports/{starter-pack-design-build,starter-pack-design-recheck2,sp1-build-1112-starter-packs,sp2-build-1112-starter-packs,sp3-build-1112-starter-packs,sp4-build,sp5-build,sp6-build,sp7-build,sp8-build,sp9-build,sp9-rebase-recheck}.md.
|
||||
|
||||
2026-11-15 UTC, applying a starter pack is the hire decision, with the route fallback (#1873; PR #1963, b913f0f4c; commits 359b3299b and 3e53af394 lane pd, bfd2cc5e7 and b913f0f4c lane pr2). Backfilled 2026-11-16 by lane docs-1116 from uidev/LEAD-1110.md and git. Before: section 3 of the starter packs design said apply never hires; a person's apply left the pack's hires as proposals for the chief to raise or for a person with org.hire to start. On 7350 at 65f7288ce Jason applied packs and saw no agents. His ruling at 20:48Z on 11-14, verbatim: "Damnit. If I choose a starter pack and apply it, that is a decision!" After, lane pd: a person's apply (pack.apply.console from the WebUI, pack.apply from the CLI) raises one hire decision per proposal after the pack's commit and resolves it, with the evidence an inbox approval leaves. The owner's apply approves flagged and credential lines; a member with org.hire hires only proposals with neither, and the rest wait for the owner as awaiting-owner. An agent apply hires nothing. Hires are written with no credential material, so services show unbound until someone binds them. The design's section 3 carries a 2026-11-14 change note. Route fallback: at 22:03Z the Lead found every 7350 chief had model null, so pack hires would refuse pack-hire-route-missing on Jason's orgs. Ruling at 22:13Z: the hire route is the chief's, else the org assistant's harness and model, else refuse; lane pr2 stacked on pd. Tests: pd gate 3957/0 at its first commit, review changes 6 (2 P2) fixed or deferred, re-check APPROVE; Lead check of pd with pr2 333 files 3813/1, the red a control-board scan.test mtime-tie flake in a file neither lane touched, 70/0 three times alone. Lead ruling on the re-check's P2 1: a member pack hire surface is a follow-up row, because a new transport for a member hire cap is a security boundary and the owner path covers apply; Jason said "proceed". Deployed b913f0f4c at 01:03Z on 11-15: stop 5, schema 3w 5, setup 5. The Lead ran pack hire on faceless (montage ideas-1 and research-1, coding pm-1, coder-1 and reviewer-1, all on fixture-deterministic) and acme (business-management pm-1 and research-1 on opus), each with routeFrom assistant. Related fix the same night: Jason reported "adapter exited 127" in Messages. Lane r127 found the Lead's deploy-uiorg.sh (a scratch script, not in the repository) started the Console without MOSAIC_CLAUDE_BIN, so claude-code sessions ran the /usr/bin/claude wrapper whose target under /opt the sandbox does not bind. The Lead patched the script and landed PR #1964 387f73a7a, which makes exit 127 name the missing program; Lead check 16 files 285/0. Both assistant kinds replied on 7350 at 387f73a7a by 03:33Z. Reports: /mnt/storage/scratch/mosaic-lead/reports/{pd-build,pd-review,pd-recheck,pr2-build,pr2-review,r127-build,r127-review,r127-recheck}.md.
|
||||
|
||||
2026-11-15 UTC, single pane slices sp1 to sp14 (#1873; PRs #1965 to #1978; final c26879637). Backfilled 2026-11-16 by lane docs-1116 from uidev/LEAD-1110.md and git. Before: a Side by side page sat in the sidebar beside the organization pages, and Jason's walkthrough on 65f7288ce (20:48Z on 11-14) found agents from an applied pack not visible. His direction: #/dashboard system-wide, no organization URL base, side-by-side Assistant threads in place of Side by side, and the 3-click rule. The Lead ruled at 03:33Z on 11-15 that #/orgs/<id>/... stays the address of an org page but is a route inside one global shell, with no reload and no mode switch. After, slice by slice (PR, tip, Lead check, lane gate): sp1 shell, #/dashboard system page, org drill-in without reload, a down org says so (#1965, c7aecbffa, 128/0, 1031/0, two reviews, nine findings fixed); sp2 dashboard polish, bare org route, theme, one down banner (#1966, d1b01329b, 132/0); sp3 system Inbox (#1967, 3885f4628, 111/0, 892/0; a theme fix and a render-order fix were read by the Lead, not a reviewer); sp5 system Agents, landed before sp4 (#1968, f97e0d3ae, 89/0 plus unified-workspace 18/0, 745/0); sp4 system Tasks, which the Lead rebased onto f97e0d3ae with three one-line union conflicts (#1969, ed4f45ed2, 131/0, 937/0 before the rebase); sp6 system Assistant with up to three threads from any org side by side, the open set in the hash, Side by side removed from the sidebar and #/side-by-side landing on Assistant (#1970, 789e2bcdd, 95/0, 1472/0, four review rounds); sp7 seven small fixes (#1971, a9912983e, 141/0, 1483/0, three review rounds); sp8 KPI tiles from real reads only and the 3-clicks table (#1972, c66f1ec00, 119/0 plus org-live-edit-browser 4/0 alone, 1490/2 with two load flakes that each passed alone twice, three small fixes not re-reviewed); sp9 read-only workflow list and graph per org (#1973, 7c6571768, 143/0, 1504/0); sp10 workflow edit in the graph (#1974, bd29e756a, 146/0, 1515/0); sp11 eight fixes including agent counts that disagreed and Done this week (#1975, 5b6b39f08, 164/0, 1527/0, three review rounds); sp12 agent drawer from #/agents and org session rows (#1976, 69ce460f9, 196/0, 191 files 1538/0); sp13 organization page fits its width, #/orgs/<id>?query parses, drawer state pending until the instances read answers (#1977, 68df55130, 205/0, 192 files 1547/0); sp14 Queue card off the organization page, running not read where an answer carries no runs, kept Assistant pane messages keyed by an opaque viewer, drawer repaint without new page reads (#1978, commits 2fc97a3ed and c26879637, 218/0, 192 files 1549/0, review changes 1 with the P2 fixed, re-check approve). sp14 adds `viewer` to /api/config (32 hex of sha256 over provider, iss and sub; `local` without sign-in) and adds no route. The Lead ruled at landing of sp13 that the Queue card leaves the organization page; #/queue stays. State at c26879637: system pages #/dashboard, #/inbox, #/agents, #/tasks and #/assistant; org pages at #/orgs/<id>/... inside the same shell. Each slice was deployed to uiorg 7350 with five buses up (deploy log per tip under reports/deploy-<sha>.log); sp1 and sp8 to sp14 record 5 stop, 5 schema 3w and 5 setup. An acme assistant reply was checked live after sp1 and sp6 to sp14; after sp4, #/agents showed the acme assistant running mid-reply; the log records no reply check for sp2, sp3 and sp5. Corrections: sp15 was launched at 19:23Z on a wrong premise. The Lead had told Jason, from reading code only, that the broker's async instances path was the real cause of the sp12 drawer saying not running. The lane stopped at BASE with no diff: the shipped runtime answers instances synchronously (runtime.mjs:1263), and a probe on a real startBroker showed the running and alive arrays present. The sp12 live shot had a 9000 ms screenshot budget that caught the paint before the read; the sp13 shot at 30000 ms shows State running. sp15 closed at 19:26Z with no change and no bus row; sp14's running not read item now only guards a failed or odd read. The Lead's lesson: prove a fault live before naming it a cause. The sp14 live shots read 0 running because they were taken right after the bus restart, before any session started. Follow-ups after sp14: empty space under Active agent sessions at 1440, a drawer pending gap, viewer read once per tab with an unsalted hash, the open pane set kept per browser, and the Dashboard org table scrolling sideways at 390. Reports: /mnt/storage/scratch/mosaic-lead/reports/{sp1-build,sp2-build,sp3-build,sp4tasks-build,sp5agents-build,sp6threads-build,sp7polish-build,sp8kpis-build,sp9workflows-build,sp10workflowedit-build,sp11fixes-build,sp12agentdrawer-build,sp13orgpagefit-build,sp14orgpagetruth-build,sp15businstancesruns-build}.md.
|
||||
+5
-2
@@ -15,8 +15,11 @@ RUN apt-get update \
|
||||
# uid/gid 1000, which matches the host user that owns the runtime
|
||||
# state directory mounted at /var/lib/mosaic. It is reused as-is.
|
||||
|
||||
# Pinned Pi install: package.json pins the exact version and
|
||||
# package-lock.json is installed with npm ci. No unversioned installs.
|
||||
# Pi install: package.json gives pi a range, not a pin (#1873), and npm ci
|
||||
# installs the version package-lock.json resolves. No unversioned installs.
|
||||
# The build entry points run scripts/pi-fit-image.mjs on the host first, so a
|
||||
# lock that resolves an unfit pi fails the build (the fit check needs bwrap,
|
||||
# which docker build's seccomp profile refuses, so it is not a RUN step).
|
||||
WORKDIR /opt/app
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci --ignore-scripts
|
||||
|
||||
@@ -6,8 +6,8 @@ not implemented (see BRIEF.md, "Explicit exclusions").
|
||||
|
||||
## L0 — Implemented: container returns MOSAIC_HELLO_OK
|
||||
|
||||
One image (`mosaic-poc-agent:0.84.4`, built on digest-pinned `node:24.22.2-bookworm-slim`, non-root,
|
||||
pinned Pi) runs one Pi agent one-shot. Four immutable local contract files are
|
||||
One image (`mosaic-poc-agent:<pi>-r<release>`, the pi version `package-lock.json` resolves; built on digest-pinned `node:24.22.2-bookworm-slim`, non-root,
|
||||
pi from the lock, not pinned, and fit-checked before the build) runs one Pi agent one-shot. Four immutable local contract files are
|
||||
loaded in fixed order into the generated system prompt
|
||||
(`/var/lib/mosaic/private/system-prompt.md`, private to that container). One real model request is sent
|
||||
noninteractively; the response must equal `MOSAIC_HELLO_OK` exactly or the
|
||||
|
||||
@@ -193,7 +193,14 @@ explicit managed-name refusals with migration compatibility.
|
||||
## How it works
|
||||
|
||||
1. `scripts/build.sh` builds the release image (`mosaic-poc-agent:<pi>-r<release>`,
|
||||
tag derived from `RELEASE` + the pinned Pi version) with Docker Compose.
|
||||
tag derived from `RELEASE` + the pi version `package-lock.json` resolves, by
|
||||
`scripts/image-tag.mjs`) with `docker build -f Containerfile`. `package.json`
|
||||
gives pi a range, never a pin. Before the build, `scripts/pi-fit-image.mjs`
|
||||
runs the fit check on the pi the lock resolves, so a lock that resolves an
|
||||
unfit pi fails it. The check runs on the host's node, which can differ from
|
||||
the image's (the Containerfile's `FROM`). `compose.yaml` has no build section
|
||||
and `pull_policy: never`, so a compose run never builds or pulls the image;
|
||||
it refuses a missing one.
|
||||
2. On each run, `/opt/mosaic/src/load-contracts.sh` reads the four contract files
|
||||
in fixed order (CONSTITUTION, STANDARDS, SOUL, USER), joins them with clear
|
||||
separators, and writes `/var/lib/mosaic/private/system-prompt.md` in the
|
||||
|
||||
+19
-1
@@ -52,7 +52,7 @@ Optional, adapter-specific (documented per adapter):
|
||||
|
||||
## Included adapters
|
||||
|
||||
- `pi` — the pinned `@earendil-works/pi-coding-agent` CLI in noninteractive
|
||||
- `pi` — the `@earendil-works/pi-coding-agent` CLI (not pinned; #1873) in noninteractive
|
||||
print mode (`-p`), ambient discovery disabled, stdin detached.
|
||||
- `mock` — deterministic echo of `MOSAIC_MOCK_RESPONSE`. Test-only: never use
|
||||
it where a real model response is required.
|
||||
@@ -106,6 +106,24 @@ keeps its existing channel; the CLI does not inherit the completion pipe. No ext
|
||||
writable mount or completion file is needed. Pi retains print mode and signals
|
||||
activity through its existing extension. Mock remains a test-only echo.
|
||||
|
||||
### A worker's skills (M17, #1873)
|
||||
|
||||
`MOSAIC_SKILLS` is a comma list of skill directories. Today only the
|
||||
interactive container launch (`scripts/agent.sh`) sets it; task launches
|
||||
refuse skill directories and set it empty
|
||||
(`packages/processing-control/src/containers.mjs`). The pi adapter passes `--no-skills` and then
|
||||
`--skill <dir>` for each one. On pi 0.85.1 `--skill` adds to discovery and
|
||||
does not replace it. With `--skill` alone, a probe with a scratch HOME also
|
||||
loaded the user's skills, the skills a `settings.json` or a listed package
|
||||
names, and `~/.agents/skills` (and the project's, without `--no-approve`).
|
||||
With `--no-skills` first, only the given directories load. A directory that
|
||||
doesn't exist stops the adapter with exit 2. The fit check runs the
|
||||
headless form of this argv, with two skill directories, one `-e` file, no
|
||||
tools and no session, as its last `-p` run (`workerShape`,
|
||||
`packages/harness/README.md`); the TUI shape the launch uses is not run. The fit check runs
|
||||
before bus sessions, and on the pi `package-lock.json` resolves before an
|
||||
image build (`scripts/pi-fit-image.mjs`); worker containers run that pi.
|
||||
|
||||
### A role's skills (row 64)
|
||||
|
||||
A managed session's skills reach both adapters as text in the bundle's
|
||||
|
||||
+21
-8
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
# Pi adapter: implements the Mosaic adapter contract for the pinned
|
||||
# @earendil-works/pi-coding-agent CLI.
|
||||
# Pi adapter: implements the Mosaic adapter contract for the
|
||||
# @earendil-works/pi-coding-agent CLI. Its behaviour notes are read from 0.85.1.
|
||||
#
|
||||
# Contract: see /opt/mosaic/adapters/README.md.
|
||||
# Headless (default): stdout = response only; stderr = diagnostics; exit 0.
|
||||
@@ -57,14 +57,18 @@ fi
|
||||
TOOLS_FLAG="--no-tools"
|
||||
[ -n "${MOSAIC_TOOLS:-}" ] && TOOLS_FLAG="--tools $MOSAIC_TOOLS"
|
||||
|
||||
# Skills (M17): explicitly provided skill dirs replace discovery. When none
|
||||
# are provided the agent runs with --no-skills (nothing ambient to find).
|
||||
# Skills (M17, #1873 pi-skills): --no-skills always, then one --skill per
|
||||
# given dir. On pi 0.85.1 --skill adds to discovery and does not replace it:
|
||||
# with --skill alone pi also loads the user's skills, settings and package
|
||||
# skills and ~/.agents/skills (a planted-skill probe showed it, offline, with a
|
||||
# scratch HOME). With --no-skills first only the given dirs load. The fit check
|
||||
# (packages/harness/src/pi-fit.mjs) runs its -p form as its last run (workerShape).
|
||||
# Only the interactive launch (scripts/agent.sh) sets MOSAIC_SKILLS today.
|
||||
# Role sessions never get MOSAIC_SKILLS (ENV_ALLOW in packages/seat/src/
|
||||
# session.mjs leaves it out), so they run with --no-skills; their skills
|
||||
# session.mjs leaves it out), so they run with --no-skills alone; their skills
|
||||
# are text in the system prompt (row 64).
|
||||
SKILLS_FLAG="--no-skills"
|
||||
if [ -n "${MOSAIC_SKILLS:-}" ]; then
|
||||
SKILLS_FLAG=""
|
||||
OLDIFS=$IFS; IFS=','
|
||||
for s in $MOSAIC_SKILLS; do
|
||||
[ -d "$s" ] || { echo "pi adapter: skill dir missing: $s" >&2; exit 2; }
|
||||
@@ -92,6 +96,11 @@ fi
|
||||
THINKING_FLAG=""
|
||||
[ -n "${MOSAIC_THINKING:-}" ] && THINKING_FLAG="--thinking $MOSAIC_THINKING"
|
||||
|
||||
# MCP (#1873 S3): the harness module sets MOSAIC_PI_NO_MCP=1 when the binary's --help lists
|
||||
# --no-mcp, so MCP stays off by its own flag and not only as a side effect of --no-extensions.
|
||||
NO_MCP_FLAG=""
|
||||
[ "${MOSAIC_PI_NO_MCP:-}" = "1" ] && NO_MCP_FLAG="--no-mcp"
|
||||
|
||||
# Mode (M13): interactive TUI or one-shot print.
|
||||
PRINT_MODE="-p"
|
||||
REQUEST_ARG=""
|
||||
@@ -107,6 +116,8 @@ fi
|
||||
# --system-prompt replace the default prompt with the generated one, given
|
||||
# as the file's absolute path (checked above), never its text
|
||||
# --no-* no ambient context/skills/extensions/templates/themes
|
||||
# SKILLS_FLAG --no-skills, then --skill <dir> for each MOSAIC_SKILLS dir
|
||||
# NO_MCP_FLAG --no-mcp when the binary lists it, else nothing
|
||||
# EXT_FLAGS the explicitly provided extensions only (-e)
|
||||
# SESSION_FLAGS ephemeral | persistent | forked (per env)
|
||||
# TOOLS_FLAG per capabilities
|
||||
@@ -118,6 +129,7 @@ set -- \
|
||||
--offline \
|
||||
--no-approve \
|
||||
--no-extensions \
|
||||
$NO_MCP_FLAG \
|
||||
$EXT_FLAGS \
|
||||
$SKILLS_FLAG \
|
||||
--no-prompt-templates \
|
||||
@@ -134,6 +146,7 @@ set -- \
|
||||
[ "$INTERACTIVE" = "1" ] || set -- "$@" -p "$MOSAIC_REQUEST"
|
||||
if [ -n "${MOSAIC_MODEL_EVENT_MONITOR:-}" ] && [ "$INTERACTIVE" != "1" ]; then
|
||||
STREAM="$(dirname "$0")/../../packages/plugins/harness/pi/model-stream.mjs"
|
||||
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" pi "$@" --mode json
|
||||
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" "${MOSAIC_PI_BIN:-pi}" "$@" --mode json
|
||||
fi
|
||||
exec pi "$@"
|
||||
# #1873 S2: the binary the harness module found (MOSAIC_PI_BIN, its path inside the sandbox).
|
||||
exec "${MOSAIC_PI_BIN:-pi}" "$@"
|
||||
+5
-3
@@ -1,8 +1,10 @@
|
||||
services:
|
||||
mosaic-agent:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Containerfile
|
||||
# #1873 S5: no build section, and never pulled. The image is built only by
|
||||
# build_image (scripts/common.sh) or the gate's prepareImage, which run the
|
||||
# image fit step first, so a missing image refuses here instead of compose
|
||||
# building one that skipped the fit check.
|
||||
pull_policy: never
|
||||
image: ${MOSAIC_IMAGE_TAG:?MOSAIC_IMAGE_TAG must be set by scripts/load_release (run via scripts/*.sh)}
|
||||
user: "1000:1000"
|
||||
environment:
|
||||
|
||||
@@ -2124,3 +2124,9 @@ are never rewritten or removed; corrections are new entries.
|
||||
2026-11-09 UTC | Mos (lead, Claude Opus 5.5) | Organizations F3 fix round and landing (#1873) | PR #1884 merged a8f9f0957; review P3 fixed, P1 README corrected with follow-up #1883; 195/0 focused suite; uiorg redeployed 22:45Z.
|
||||
2026-11-09 UTC | Mos (lead, Claude Opus 5.5) | PRD 1.20 landing and defect l (#1873, #1886) | PR #1887 merged 03fe321b1, PR #1888 merged 7a97cfc4f; both Sol reviews APPROVE 0; bus suite 1713/0; uiorg redeployed 23:15Z; U1, U2 and U4a builds dispatched.
|
||||
2026-11-10 UTC | Mos (lead, Claude Opus 5.5) | U1 and SYS1 landings (#1873) | PR #1889 merged d35447005, PR #1890 merged 2c6556ab1; both Sol reviews CHANGES, fixed (U1 by Terra, SYS1 by the lead); uiorg at 2c6556ab1 after the missed 3t-3u store migration was run; deploy script now migrates.
|
||||
2026-11-10 UTC | Sage (Lead) | redact3 landing (#1873) | PR #1912 merged 96c900d21 at 23:56Z, gate 268 files 3412/0, Opus review changes 2 fixed; uiorg deploy started. Backfilled 2026-11-16 by lane docs-1116 from uidev/LEAD-1110.md and git.
|
||||
2026-11-11 UTC | Sage (Lead) | Organizations, redaction, accounts, dev readiness and credential binding landings (#1873) | PRs #1913 to #1944 merged, a4eed1e21 to 6d0e9f89b: A2, golden1, TI, redact4, A3, M3 with M3-recon, acct-anchor, org-names, polish2, run sheet doc, cred-bind design, PL1, base-reds, redact5, T1, timing2, cleanup-time, A4, supcost, defscan, redact6, AU, D4, D3, harness-scan, D2, D1, CB01, bus-stop, agent-guard, proxy-ui, CB2. Jason approved CB0 to CB5 at 17:55Z. Per-landing check counts are in uidev/LEAD-1110.md. Backfilled 2026-11-16 by lane docs-1116.
|
||||
2026-11-12 UTC | Sage (Lead) | E1, E2, CB3, E3, CB4 and starter packs design, SP3, SP1, SP7, SP2, SP4 landings (#1873) | PRs #1945 8827905ba (E1), #1946 666f4d686 (E2), #1947 0b8fe202b (CB3), #1948 2ad4b30eb (E3), #1949 c0a60ed3a (design), #1950 1e65ff3f8 (SP3), #1951 755ba3fa8 (SP1), #1952 1ced0e392 (SP7), #1953 dfef78834 (SP2), #1954 95c2ce143 (CB4), #1955 9a649abf8 (SP4). Jason's FULL SEND on starter packs at 21:06Z: system types removed outright, SP10 dropped. The log first dated the E1 landing 11-11; its own correction and git put it at 02:48Z on 11-12. Backfilled 2026-11-16 by lane docs-1116.
|
||||
2026-11-13 UTC | Sage (Lead) | Starter packs SP6, SP5, SP8, SP9 and CB5 landings; E2E round 6 started (#1873) | PRs #1956 991eb8c33 (SP6), #1957 34dd33101 (SP5), #1958 a9d72b3cd (CB5, credential binding CB0 to CB5 complete), #1959 0251acdad (SP8), #1960 e6ab8bc1f (SP9). uiorg 7350 at e6ab8bc1f: bus schema 3w on all five stores, system-types route 404. E2E R6 CLI and WebUI lanes started 23:59Z. Backfilled 2026-11-16 by lane docs-1116.
|
||||
2026-11-14 UTC | Sage (Lead) | E2E round 6 fixes and the pack apply ruling (#1873) | E2E R6 CLI 0/0/7, WebUI 0/3/15. PR #1961 23b5e24eb (f6w) and PR #1962 65f7288ce (f6c) merged; uiorg verified at 65f7288ce 02:40Z. Jason's walkthrough at 20:48Z: applying a starter pack is a decision, plus the single pane direction. Route fallback ruled 22:13Z. Backfilled 2026-11-16 by lane docs-1116.
|
||||
2026-11-15 UTC | Sage (Lead) | Pack decision, exit 127 fix and single pane slices sp1 to sp14 (#1873) | PR #1963 b913f0f4c (pd with pr2), PR #1964 387f73a7a (r127), PRs #1965 to #1978 (sp1 to sp14), final c26879637 at 20:19Z; each deployed to uiorg 7350. sp15 launched 19:23Z on a wrong premise (a bus instances fault that did not exist) and closed 19:26Z with no change. Backfilled 2026-11-16 by lane docs-1116.
|
||||
+6
-2
@@ -16,7 +16,7 @@ and changes nothing.
|
||||
| Command | Purpose | Notes |
|
||||
|---|---|---|
|
||||
| `scripts/bootstrap.sh [--instance NAME]` | Create `~/.config/mosaic-dev/config.json` if absent; with `--instance NAME`, `~/.config/mosaic-NAME/config.json` and the claimed root `~/.mosaic-NAME` | Idempotent; existing config validated, never rewritten. See [Stack instances](#stack-instances---instance-name-row-190) |
|
||||
| `scripts/build.sh` | Build the release image | Tag derived from `RELEASE` + pinned pi version |
|
||||
| `scripts/build.sh` | Build the release image | Tag `mosaic-poc-agent:<pi>-r<RELEASE>`, pi being the version `package-lock.json` resolves (`scripts/image-tag.mjs`); runs the image fit step (`scripts/pi-fit-image.mjs`) first. compose runs never build or pull the image, so run this before anything that needs a new tag |
|
||||
| `scripts/hello.sh` | One-shot startup request | Prints model response on stdout |
|
||||
| `scripts/verify.sh` | Full gated test | Exit 0 only on exact `MOSAIC_HELLO_OK`; `EXPECTED_MARKER` overrides for negative drills |
|
||||
|
||||
@@ -65,8 +65,8 @@ addition to its full repository gate; it was not run by this source repair:
|
||||
```bash
|
||||
node --test --test-concurrency=1 packages/processing-control/tests/containers.test.mjs packages/processing-control/tests/coordinator.test.mjs packages/processing-control/tests/effects.test.mjs packages/processing-control/tests/mount.test.mjs packages/processing-control/tests/runtimes.test.mjs packages/processing-control/tests/state.test.mjs packages/processing-control/tests/stop-run.test.mjs packages/processing-control/tests/task.test.mjs
|
||||
node --test scripts/test-darkwing-launch.mjs
|
||||
scripts/test-task.sh
|
||||
scripts/build.sh
|
||||
scripts/test-task.sh
|
||||
```
|
||||
|
||||
## Agent (interactive TUI)
|
||||
@@ -346,8 +346,12 @@ For local development on a named instance only, `mosaic bus start BUSINESS --dev
|
||||
|
||||
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`.
|
||||
|
||||
Starter packs (#1873): `mosaic org pack list`, `mosaic org pack preview ID PACK [--project P]`, `mosaic org pack apply ID PACK --digest D [--project P]` and `mosaic org pack hire ID PACK [--project P]` call the bus as the organization's human (`packs`, `pack.preview`, `pack.apply`, `pack.hire`). Ruling 2026-11-14 ("If I choose a starter pack and apply it, that is a decision!"): a person's apply is the hire decision. After the pack commits, the same act hires each proposed agent through the hire-decisions path and the receipt's `hires` lists `{name, state, decision | code}` per proposal (`hired`, `exists`, `open`, `awaiting-owner`, `refused`); a `hired` or `awaiting-owner` line adds `routeFrom: 'chief' | 'assistant'`. The owner's apply approves the flagged and credential lines; a credential line is approved but not bound, so the agent's launch refuses `credential-unbound` until its services are bound. The bus verb `pack.hire.console` also takes a member's console cap with `org.hire`: it hires only the proposals with no flagged or credential line, and the rest wait for the owner in the inbox. No member surface calls it yet: the WebUI packs routes and the CLI `pack` words are the owner's. An owner's pack hire also replaces and approves a hire this pack's own member hire left in the inbox; the old decision is withdrawn only after the new hire applies. Two pack hires on one organization at once: the second refuses `organization-busy`; an apply whose hire loop did not run still answers the apply's receipt with `hireError` and no hires. An agent's apply hires nothing. `pack hire` reruns the loop on a pack already applied and hires none twice; it refuses `pack-not-applied` when the pack has no packs entry, and a hire runs as the chief of staff's harness and model, else the organization assistant's (the instance whose definition is `assistant`), and a proposal refuses `pack-hire-route-missing` only when neither has both. All pack words refuse inside an agent run (exit 3). The WebUI pack card's "Hire the N proposed agents" is `POST /api/orgs/<id>/packs/hire`. Details: `packages/cli/README.md`, `packages/bus/README.md` and `packages/webui/README.md`, "Starter pack hires"; design: `docs/plans/2026-11-12_starter-packs-design.md` section 3.
|
||||
|
||||
The bus and Console lifecycle verbs refuse inside an agent run (agent-guard, #1873): `mosaic bus start`, `bus stop`, `bus restart` and `bus recover`, and `mosaic console start`, `console stop` and `console restart`, and `mosaic org archive` and `org delete`, which stop the organization's bus, and `mosaic org restore`, which starts it (`org restore --no-bus` starts nothing and is not refused), and `mosaic org role-templates`, `hire`, `fire`, `change` and `autonomy` (E2). Each exits 3 with `refused: this runs only from a human shell, outside any agent run (found MARKER in the environment)` before it reads anything. It checks the environment it is given and its own process environment, so a sanitized environment does not hide a marker. A usage error (a missing or extra argument, `--pm` with `--no-launch`, or a bad `--dev-insecure-bind`/`--dev-allow-host`) still comes first (exit 4). `mosaic bus status` and `mosaic console status` stay open to agents. The Console's Restart bus and Restart Console controls keep working: the Console starts its bus host with its own environment, which carries no marker, and Restart Console relaunches the Console under its supervisor without going through the CLI. The markers are the ones `packages/cli/src/transport.mjs` lists. The check is cooperative: a process that drops the markers from its environment is not stopped by it.
|
||||
|
||||
Refusal lines (E2E6, #1873). A refusal that has a next step prints it on a second line, `mosaic: next: ...`, after the unchanged message, so callers that match the message keep working. `mosaic bus status`, `start`, `stop` and `restart` on a stale bus host lock exit 3 with `mosaic: stale bus host lock ...` and `mosaic: next: if the bus host died, clear it with mosaic bus recover ORG; recover refuses while the host still runs`. A human-transport verb whose bus is not running (no socket, or the connect refused before anything was sent) exits 3 with `bus-not-running: the bus for ORG is not running, so the request was not sent` and a next line naming `mosaic bus start ORG` and `mosaic bus recover ORG`; `outcome-unknown` (exit 1) stays for a request that left and got no answer. `mosaic org definitions` with an unknown organization, an unknown or malformed name, or a bad id prints one refusal line (exit 3; `unknown-organization` only when the organization file itself is missing, `definitions-unreadable` when `templates` cannot read the roles directory) or, for a bad id, the usage (exit 4), with no stack trace and no host path. Pack, board and workflow refusals carry their next step as `template save` does. `mosaic console start` and `console stop` clear a `console/start.lock` whose named supervisor (pid and start time) is gone, once, with a `cleared a console start lock left by process PID, which is gone` warning; a lock whose holder still runs, or that names no holder, refuses and stays. A start or stop releasing its lock marks it released through its own descriptor and removes the path only while it is still that file, so a lock put back after its release is cleared the same way, with a `cleared a console start lock its holder had released` warning. An `autonomy-precondition` refusal from the bus carries the check that failed (owner notices off, no tested reverse, a resolver without the right, no `review.verdict` holder, no enforced budget, out of the loop only from near the loop, the trail review or near-loop decision missing); the human-transport child writes only the code, so a terminal `org autonomy set` shows the Console's message. `mosaic org template save` writes free text verbatim: it never rewrites prose, and the preview lists every field that mentions the organization id, a project id, an agent name or the owner's account, with the names it mentions, for the owner to edit before sharing. Saving under a name already saved for the account refuses `template-exists` (exit 2, nothing written) with a next line to save under another name or list the saved ones with `mosaic org template list ORG`.
|
||||
|
||||
The instance processing switch is separate from per-business launches off/on.
|
||||
`mosaic halt` durably stops managed processing across every business in the
|
||||
selected configuration's data root. The bus stays up for reads and human
|
||||
|
||||
@@ -374,6 +374,48 @@ exactly as if a person had made them by hand. The steps:
|
||||
`projects.<p>.agents`; those entries refuse instance credential material
|
||||
(`BOUND_KEYS`, `agent-levels.mjs:21`).
|
||||
|
||||
**2026-11-14 change (#1873, lane pd).** Jason's ruling: "Damnit. If I
|
||||
choose a starter pack and apply it, that is a decision!" This replaces
|
||||
"Apply never hires" above for a person's apply. The `packs` entry and the
|
||||
proposals stay as written; what changes is who acts on them:
|
||||
- A person's apply (`pack.apply.console` from the WebUI, `pack.apply` from
|
||||
the CLI) is the hire decision. In the same act, after the pack's commit,
|
||||
the broker raises one hire decision per proposal through the
|
||||
hire-decisions path and resolves it, leaving the evidence an inbox
|
||||
approval leaves: the decision row, its resolved event, `human.input`,
|
||||
`hire.consume`, the `hire.apply` event and `action.allowed pack.hire`.
|
||||
The hired entry carries `hireDecision`.
|
||||
- Invariant 7 still decides each line. The owner's apply approves the
|
||||
flagged and credential lines. A member with `org.hire` hires only the
|
||||
proposals with neither a flagged nor a credential line; each of the
|
||||
others is raised as an open decision that waits for the owner in the
|
||||
inbox (`awaiting-owner`). The owner's later pack hire withdraws such a
|
||||
decision and hires the agent itself. An
|
||||
agent apply hires nothing: the hire stays a proposal for the chief, and
|
||||
a hire verb from an agent is refused `human-required`.
|
||||
- Credentials take the smallest rule. The hire writes the entry with no
|
||||
credential material, so its services show `unbound` and a launch is
|
||||
refused `credential-unbound` until someone binds them. The owner's
|
||||
approval covers the credential lines; it binds nothing and reads no
|
||||
secret.
|
||||
- The route is the chief of staff's resolved harness and model. With
|
||||
none, that proposal is refused `pack-hire-route-missing` and stays open.
|
||||
Note 2026-11-14 (lead ruling, lane pr2): the route is the chief's, else
|
||||
the organization assistant's (the instance whose definition is
|
||||
`assistant`; the one named `assistant` when there are several, else the
|
||||
first in key order), and the refusal applies only when neither resolves
|
||||
a harness and a model. Hire lines carry `routeFrom`.
|
||||
- `pack.hire` / `pack.hire.console` (`mosaic org pack hire ID PACK
|
||||
[--project P]`, `POST /api/orgs/<id>/packs/hire`) runs the same loop on
|
||||
a pack already applied, refused `pack-not-applied` otherwise. A proposal
|
||||
whose instance exists is `exists`, and one with an open decision is
|
||||
`open` or `awaiting-owner`, so neither a re-apply nor `pack hire` ever
|
||||
hires twice. A project pack hires into `projects.<p>.agents`.
|
||||
- Each hire line in the receipt is `{name, state, decision | code}` with
|
||||
state `hired`, `exists`, `open`, `awaiting-owner` or `refused`. The
|
||||
packs read adds `hired` and `open` (with each proposal's reason) to each
|
||||
applied entry, and the instances read names the pack an agent came from.
|
||||
|
||||
### The surface
|
||||
|
||||
| Layer | Name |
|
||||
|
||||
@@ -586,3 +586,4 @@ git history + Gitea issues.
|
||||
|
||||
- 2026-09-26 (sage, lead): Jason's ruling: Sage leads the project, Darkwing is a collaborating seat, more seats to follow, development stays in T3 for now. The fleet Sage seat that took Invoice Ninja coordination outside Jason's instructions is being decommissioned; moving work onto the new stack is the fix. Suites all green. Rows 24 and 25 pushed (1685deb4..43d7574d). Open: about 11 days of uncommitted agent work in the tree (row 16 launch files, row 22, #1512, WUI evidence), 23 untracked aws-* skill directories in skills/ from 2026-09-21, and Jason's target for the next phase.
|
||||
- 2026-10-13 (sage, lead): Jason's PM re-test on 664199f7 is finished (Mos; PM run rcc0f72be8b17; Vikunja #228). Paging, the Board's held roles, the Messages page and the #229 loop (coder push 010d30cb, review, approve, PM close) all worked; #230 and #231 are done, #232 stays open for the secrets-path probe. Two faults checked and dropped: the reviewer did run the tests before its verdict, and the PM's clone did hold the row 95 Board code. Two filed: row 104 (#1598) seat git identity, row 105 (#1599) a review request says whether a reviewer is running. Rows 96, 101-105 briefed; row 85 released to build. Row 85's live steps, any bus restart and row 102's public/ change each need Mos's go. Row 100 waits on Jason (Vikunja #257). Formatting, Jason's direction: no formatting row now. Chat output will be formatted in the TUI and WebUI the way chat interfaces usually do it, made our own; T3 Code's chat is his example. Carry it into the TUI and WebUI chat work.
|
||||
- 2026-11-15 (sage, lead; written 2026-11-16 by lane docs-1116 from uidev/LEAD-1110.md and git): single pane state at c26879637 (#1873, PRs #1965 to #1978). One WebUI shell. System pages #/dashboard, #/inbox, #/agents, #/tasks and #/assistant cover every organization; #/queue stays its own page. Organization pages are #/orgs/<id>/... routes inside the same shell, with no reload and no mode switch (Lead ruling 03:33Z 11-15). Side by side is gone from the sidebar; the Assistant page holds up to three threads from any organization side by side, and #/side-by-side lands there. The organization page has KPI tiles from real reads, a workflow list and graph with edit, an agent drawer (?agent=<org>:<name>), and no Queue card. Where an instances answer carries no runs the page says running was not read, never 0. /api/config names an opaque `viewer` that keys kept Assistant pane messages. Applying a starter pack is the hire decision (PR #1963 b913f0f4c). Follow-ups: empty space under Active agent sessions at 1440, a drawer pending gap, viewer read once per tab with an unsalted hash, the open pane set kept per browser, the Dashboard org table scrolling sideways at 390. Detail: BUILD-LOG.md 2026-11-15 entries.
|
||||
@@ -1240,16 +1240,22 @@ Added in version 1.3 (Q155).
|
||||
changes it, not in one rewrite (Q155). Until it converts, Technical
|
||||
considerations lists where it is hard-coded.
|
||||
|
||||
### System types
|
||||
### Starter packs
|
||||
|
||||
Added in version 1.20.
|
||||
|
||||
- **REQ-SYS-1.** System types are a plug-in kind. Its modules include
|
||||
coding, creative writing, marketing, Mosaic Montage (the OpenMontage
|
||||
fork) and business management. An organization enables a list of them.
|
||||
- **REQ-SYS-2.** A project may use any system type its organization
|
||||
enables, and none is required. An agent working in a project gets the
|
||||
toolsets of the system types that project uses.
|
||||
- **REQ-SYS-1.** Starter packs are a plug-in kind. Mosaic Stack ships five:
|
||||
coding, creative writing, marketing, Mosaic Montage and business
|
||||
management. A pack is a starting configuration of role definitions,
|
||||
proposed hires, a board style and workflow templates. An organization or
|
||||
project copies a pack in once and then owns and changes what it copied. A
|
||||
user can save an organization or project as their own template and apply it
|
||||
the same way.
|
||||
- **REQ-SYS-2.** Applying a pack grants nothing by itself. Every agent it
|
||||
proposes is hired through the hire decision (REQ-AGENT-3), bounded by the
|
||||
chief's own role, with any line above it approved by the owner. A pack never
|
||||
sets credentials, rights, the autonomy setting or plug-in choices. Removing
|
||||
a pack removes nothing it copied.
|
||||
|
||||
### Meta-harness
|
||||
|
||||
|
||||
Generated
+1
-1
@@ -9,7 +9,7 @@
|
||||
"version": "0.0.3",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-coding-agent": "0.85.1",
|
||||
"@earendil-works/pi-coding-agent": ">=0.85.1",
|
||||
"oauth4webapi": "3.8.8",
|
||||
"yaml": "2.9.0"
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
"description": "Minimal Mosaic Stack container proof of concept: one Pi agent, four local contract files, one real model request returning MOSAIC_HELLO_OK.",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-coding-agent": "0.85.1",
|
||||
"@earendil-works/pi-coding-agent": ">=0.85.1",
|
||||
"oauth4webapi": "3.8.8",
|
||||
"yaml": "2.9.0"
|
||||
}
|
||||
|
||||
@@ -1135,3 +1135,40 @@ Each write records `human.input`. A task read returns a `digest` that can be sup
|
||||
`task-conflict`. Existing assignees may remain after a role is removed, but new assignees
|
||||
must be current roles. Automatic tail placement and an explicitly occupied position refuse
|
||||
`task-position-full` when there is no room; no task row is written.
|
||||
|
||||
## Starter pack hires (ruling 2026-11-14, #1873)
|
||||
|
||||
"If I choose a starter pack and apply it, that is a decision!" A person's
|
||||
`pack.apply` (the organization's human, outside any run) or `pack.apply.console`
|
||||
hires the proposals of the packs entry it wrote, once the apply has committed.
|
||||
An agent's `pack.apply` hires nothing. `pack.hire` (the owner's human session)
|
||||
and `pack.hire.console` (the owner's or a member's console cap, needing
|
||||
`org.hire`) run the same loop on a pack already applied; with no packs entry they
|
||||
refuse `pack-not-applied`.
|
||||
|
||||
Each proposal goes through `#raiseHire` and `hire.resolve.console`, the path an
|
||||
inbox approval takes: the decision row, its resolved event, `human.input`,
|
||||
`hire.consume`, the hire's commit and `hire.apply` event, plus
|
||||
`action.allowed {operation: 'pack.hire', pack, version, origin, project, hire,
|
||||
decision}`. The route is the chief of staff's resolved harness and model (the
|
||||
`launch.by` instance), else the organization assistant's: the instance whose
|
||||
definition is `assistant`, the one named `assistant` when there are several,
|
||||
else the first in key order. Thinking is carried when set. With neither the
|
||||
proposal refuses `pack-hire-route-missing`. A `hired` or `awaiting-owner` line
|
||||
carries `routeFrom: 'chief' | 'assistant'`. The entry is written with
|
||||
no credential material, so a credential line leaves its services `unbound`.
|
||||
A proposal whose instance exists is `exists`; one an open hire decision names is
|
||||
`open`, so nothing is hired twice. The owner approves every line, and replaces
|
||||
an open decision an earlier hire of the same pack left for the owner: the new
|
||||
hire is raised and applied first, and the old decision is withdrawn only once it
|
||||
has applied, so a replacement that refuses leaves the old one in the inbox. A member's flagged or credentialed hires are raised after
|
||||
the others, left open for the owner's inbox (`awaiting-owner`). A hire that
|
||||
refuses records `action.allowed {operation: 'pack.hire.refused', code}` and the
|
||||
loop goes on. The `packs` read adds `hired` and `open` to each applied entry,
|
||||
and the `instances` read adds `pack: {id, origin, project}` to each instance a
|
||||
pack hire created. An `awaiting-owner` line this pack's own hire raised carries
|
||||
`fromPack: true` (the owner's pack hire would replace it). One pack hire loop
|
||||
runs per business at a time; a second refuses `organization-busy`. An apply
|
||||
whose hire loop throws after the apply committed still answers the apply's
|
||||
receipt, with no hires and the loop's code as `hireError`. No member surface
|
||||
calls `pack.hire.console` yet; the Console's packs routes are the owner's.
|
||||
@@ -639,22 +639,6 @@ CREATE TRIGGER request_snapshot_events_no_replace BEFORE INSERT ON request_snaps
|
||||
CREATE TRIGGER request_project_events_no_update BEFORE UPDATE ON request_project_events BEGIN SELECT RAISE(ABORT, 'request_project_events is append-only'); END;
|
||||
CREATE TRIGGER request_project_events_no_delete BEFORE DELETE ON request_project_events BEGIN SELECT RAISE(ABORT, 'request_project_events is append-only'); END;
|
||||
CREATE TRIGGER request_project_events_no_replace BEFORE INSERT ON request_project_events WHEN EXISTS (SELECT 1 FROM request_project_events WHERE seq=NEW.seq OR (business=NEW.business AND project=NEW.project AND revision=NEW.revision)) BEGIN SELECT RAISE(ABORT, 'request_project_events is append-only'); END;
|
||||
-- Per-project selections are a human-authored append-only settings trail.
|
||||
CREATE TABLE project_system_types (
|
||||
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
at TEXT NOT NULL,
|
||||
business TEXT NOT NULL,
|
||||
project TEXT NOT NULL REFERENCES projects(id),
|
||||
types TEXT NOT NULL,
|
||||
by TEXT NOT NULL
|
||||
) STRICT;
|
||||
CREATE INDEX project_system_types_project ON project_system_types (business, project, seq);
|
||||
CREATE TRIGGER project_system_types_no_update BEFORE UPDATE ON project_system_types BEGIN SELECT RAISE(ABORT, 'project_system_types is append-only'); END;
|
||||
CREATE TRIGGER project_system_types_no_delete BEFORE DELETE ON project_system_types BEGIN SELECT RAISE(ABORT, 'project_system_types is append-only'); END;
|
||||
CREATE TRIGGER project_system_types_no_replace BEFORE INSERT ON project_system_types WHEN EXISTS (SELECT 1 FROM project_system_types WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'project_system_types is append-only'); END;
|
||||
CREATE TRIGGER project_system_types_business BEFORE INSERT ON project_system_types
|
||||
WHEN (SELECT business FROM projects WHERE id = NEW.project) IS NOT NEW.business
|
||||
BEGIN SELECT RAISE(ABORT, 'a system types row belongs to its project business'); END;
|
||||
-- U4a (#1873): the task store built into each organization, the default task-tracker module
|
||||
-- (REQ-TASK-1). Each row is the whole task after one write, so a task's state is its latest row
|
||||
-- and the rows are its history: who wrote it (a role and run, or the person), when, under which
|
||||
|
||||
+614
-82
File diff suppressed because it is too large.
Load diff
@@ -1,5 +1,11 @@
|
||||
import { connect } from 'node:net';
|
||||
import { BusError, PROJECT_SAVE_VERB, PROJECT_SAVE_REQUEST_MAX } from './broker.mjs';
|
||||
// E2E6 fix 6: a connect that fails with one of these never sent the request. The code stays
|
||||
// outcome-unknown for every caller that reads it (the harness, the Console), and the error is marked
|
||||
// notSent so the human and tester shims can exit with NOT_SENT_EXIT and `mosaic` can say the bus is
|
||||
// not running.
|
||||
const NOT_SENT = new Set(['ENOENT', 'ECONNREFUSED', 'ENOTDIR', 'ENOTSOCK']);
|
||||
export const NOT_SENT_EXIT = 5;
|
||||
export class Client {
|
||||
constructor({ path, cap, human, tester, user, console: owner, timeout = 5000 }) {
|
||||
this.path = path;
|
||||
@@ -22,7 +28,8 @@ export class Client {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = connect(this.path);
|
||||
let input = Buffer.alloc(0),
|
||||
settled = false;
|
||||
settled = false,
|
||||
connected = false;
|
||||
const end = (error, value) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
@@ -30,8 +37,12 @@ export class Client {
|
||||
error ? reject(error) : resolve(value);
|
||||
};
|
||||
socket.setTimeout(['processing.halt', 'processing.resume'].includes(verb) ? Math.max(this.timeout, 20000) : this.timeout, () => end(new BusError('outcome-unknown')));
|
||||
socket.on('connect', () => socket.write(request));
|
||||
socket.on('error', () => end(new BusError('outcome-unknown')));
|
||||
socket.on('connect', () => { connected = true; socket.write(request); });
|
||||
socket.on('error', (error) => {
|
||||
const unknown = new BusError('outcome-unknown');
|
||||
if (!connected && NOT_SENT.has(error?.code)) unknown.notSent = true;
|
||||
end(unknown);
|
||||
});
|
||||
socket.on('end', () => end(new BusError('outcome-unknown')));
|
||||
socket.on('data', (b) => {
|
||||
input = Buffer.concat([input, b]);
|
||||
|
||||
@@ -152,6 +152,8 @@ export function copyGrant({ credentials, writer, copies = null, organizations =
|
||||
boot: copies?.definitions ? { org: copies.org, definitions: copies.definitions } : null }, (v) => assertNotHeld(v, [value]));
|
||||
const record = doc.copyBindings?.[instance];
|
||||
if (!object(record) || record.state === 'unbound' || !object(record.services?.[service])) deny('credential-unbound');
|
||||
// CB4: a pending revoke ends first; an issue never overwrites the slot it is discarding.
|
||||
if (record.state === 'revoking') deny(record.revoke?.failed ? 'credential-revoke-failed' : 'credential-revoking');
|
||||
const copy = copies?.org === business && Object.hasOwn(copies.copies ?? {}, instance) ? copies.copies[instance] : null;
|
||||
if (!copy || copy.state === 'refused' || Object.hasOwn(doc.roles ?? {}, instance)) deny('copy-unavailable');
|
||||
if (copy.level !== record.source?.level || copy.name !== record.source?.name || (copy.project ?? null) !== (record.source?.project ?? null)
|
||||
|
||||
@@ -21,14 +21,23 @@
|
||||
// the next boot. The token is never held: each call reads the slot by the identity the boot proved
|
||||
// and checks it against a keyed MAC of the token the boot proved. Nothing here logs, and no result
|
||||
// or refusal holds the token, the slot's path or anything derived from the token.
|
||||
//
|
||||
// #1873 CB4 (design sections 3 and 6): close() shuts a copy before its revoke publishes anything:
|
||||
// it admits nothing and every call refuses, until open() after a definite finish. drain() aborts the
|
||||
// copy's requests in flight (prepare's proof and call()) and waits for each to settle. A check that
|
||||
// finds the binding's authority gone for good (the live copy shadowed, removed or colliding, its
|
||||
// role or pin changed, the slot gone or replaced, the rotation date passed, the service answering
|
||||
// 401) tells `onLoss` once per copy, so the revoke runs without waiting for a human; a transient
|
||||
// read failure never does. sweep() runs those checks for every copy holding a grant.
|
||||
import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
|
||||
import { BusError } from './broker.mjs';
|
||||
import { assertNotHeld } from './credentials.mjs';
|
||||
import { send } from './gitea-grant.mjs';
|
||||
import { boundRepositories, tokenLevels, verifyGiteaToken } from './copy-grant.mjs';
|
||||
import { readOrgEntrySync } from '../../organization/src/org-registry.mjs';
|
||||
import { allGranted, copyBotLogin, definitionPin, resolveCopyBinding } from '../../organization/src/copy-bindings.mjs';
|
||||
import { COPY_BINDABLE } from '../../organization/src/agent-copies.mjs';
|
||||
import { allGranted, CAUSE_CODES, copyBotLogin, definitionPin, resolveCopyBinding } from '../../organization/src/copy-bindings.mjs';
|
||||
import { COPY_BINDABLE, materializeCopies } from '../../organization/src/agent-copies.mjs';
|
||||
import { copyBindingStatus } from '../../organization/src/copy-binding-status.mjs';
|
||||
import { checkDefinitions, readTemplate } from '../../organization/src/definitions.mjs';
|
||||
import { loadRole } from '../../organization/src/role.mjs';
|
||||
import { resolveAgentLevels } from '../../organization/src/agent-levels.mjs';
|
||||
@@ -37,6 +46,13 @@ import { canonicalJson, sha256 } from '../../organization/src/util.mjs';
|
||||
const deny = (code) => {
|
||||
throw new BusError(code);
|
||||
};
|
||||
// A refusal that also means the binding's authority is gone for good: `cause` is the revoke cause
|
||||
// (copy-bindings.mjs REVOKE_CAUSES) the gate reports through onLoss.
|
||||
const lose = (code, cause) => {
|
||||
const e = new BusError(code);
|
||||
e.loss = cause;
|
||||
throw e;
|
||||
};
|
||||
const object = (x) => x !== null && typeof x === 'object' && !Array.isArray(x);
|
||||
// The repository API a copy session may call: /api/v1/repos/<owner>/<repo>[/<rest>], ASCII names,
|
||||
// no dot segment, no encoded separator, no query beyond plain pairs.
|
||||
@@ -54,27 +70,58 @@ export const PREPARE_BUDGET_MS = 8000;
|
||||
export const BOOT_BUDGET_MS = 20000;
|
||||
|
||||
export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snapshot, transport = send, ca = undefined, timeoutMs = undefined, now = () => new Date(),
|
||||
prepareMs = PREPARE_BUDGET_MS, bootMs = BOOT_BUDGET_MS }) {
|
||||
prepareMs = PREPARE_BUDGET_MS, bootMs = BOOT_BUDGET_MS, onLoss = null }) {
|
||||
const org = snapshot?.org ?? null;
|
||||
const key = randomBytes(32);
|
||||
const mac = (token) => createHmac('sha256', key).update(token).digest();
|
||||
const verified = new Map();
|
||||
const failed = new Map();
|
||||
const fences = new Map();
|
||||
// CB4: instance -> the code a closed copy answers; instance -> Set of { abort, settled } in flight;
|
||||
// the copies already reported lost.
|
||||
const closed = new Map();
|
||||
const inflight = new Map();
|
||||
const lost = new Set();
|
||||
// CB4: the boot proof of a copy close() shut, kept until its revoke settles (open(…, true) restores it).
|
||||
const parked = new Map();
|
||||
const track = (instance) => {
|
||||
const ac = new AbortController();
|
||||
const entry = { ac, settled: null };
|
||||
if (!inflight.has(instance)) inflight.set(instance, new Set());
|
||||
inflight.get(instance).add(entry);
|
||||
return {
|
||||
signal: ac.signal,
|
||||
run: (p) => {
|
||||
entry.settled = Promise.resolve(p).then(() => {}, () => {});
|
||||
return p;
|
||||
},
|
||||
done: () => {
|
||||
const set = inflight.get(instance);
|
||||
set?.delete(entry);
|
||||
if (set && !set.size) inflight.delete(instance);
|
||||
},
|
||||
};
|
||||
};
|
||||
const today = () => now().toISOString().slice(0, 10);
|
||||
// The organization's registry incarnation as the registry stands now, or null when the entry is
|
||||
// gone, carries none, or is not active (reserved, deleting, archived). Read on every check, never
|
||||
// cached: an organization archived while the bus runs admits and sends nothing. An unreadable or
|
||||
// unsafe registry refuses credential-unavailable.
|
||||
const liveIncarnation = () => {
|
||||
let entry;
|
||||
const registry = () => {
|
||||
try {
|
||||
entry = readOrgEntrySync(configDir, org);
|
||||
return readOrgEntrySync(configDir, org);
|
||||
} catch {
|
||||
return deny('credential-unavailable');
|
||||
}
|
||||
};
|
||||
const liveIncarnation = () => {
|
||||
const entry = registry();
|
||||
return entry?.state === 'active' ? entry.incarnation : null;
|
||||
};
|
||||
// CB4 (design section 6): a verified copy of an organization no longer active answers the
|
||||
// organization's own state: archived, or gone (deleting, deleted, recreated under a new
|
||||
// incarnation).
|
||||
const inactive = () => (registry()?.state === 'archived' ? 'org-archived' : 'org-unavailable');
|
||||
// The organization file is still the revision the launch resolved its bundle from (#1873 A3).
|
||||
const sameRevision = (business, revision) => {
|
||||
if (revision === undefined) return;
|
||||
@@ -97,7 +144,27 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
// bindingFacts, without the refusals that function makes for a proposal).
|
||||
const facts = (business, instance, doc) => {
|
||||
const copy = snapshot?.org === business && Object.hasOwn(snapshot.copies ?? {}, instance) ? snapshot.copies[instance] : null;
|
||||
if (!copy || copy.state === 'refused' || Object.hasOwn(doc.roles ?? {}, instance)) deny('copy-unavailable');
|
||||
// CB4: the copies the file makes now, from the boot's root agents. An org instance of the name
|
||||
// shadows the copy, a removed source makes none, and a second claimant refuses both: each is
|
||||
// the end of this copy's binding. A file the materializer cannot read refuses and loses nothing.
|
||||
if (Object.hasOwn(doc.roles ?? {}, instance)) lose('copy-unavailable', 'shadowed');
|
||||
// A copy the boot did not make at all, from a root agents file it read, was fired or removed
|
||||
// before this start (a crash may have come before its revoke began): the binding ends. A boot
|
||||
// that could not read the root file makes no copy and loses nothing.
|
||||
if (!copy) return snapshot?.org === business && snapshot.root === 'ok' ? lose('copy-unavailable', 'retire') : deny('copy-unavailable');
|
||||
if (copy.state === 'refused') return copy.claims ? lose('copy-refused', 'collision') : deny('copy-unavailable');
|
||||
let live;
|
||||
try {
|
||||
live = materializeCopies({ orgId: business, doc, rolesDir,
|
||||
root: snapshot.root === 'refused' ? null : { state: snapshot.root, agents: snapshot.rootAgents ?? {}, definitions: snapshot.definitions ?? {} } });
|
||||
} catch {
|
||||
deny('copy-unavailable');
|
||||
}
|
||||
const now = Object.hasOwn(live.copies, instance) ? live.copies[instance] : null;
|
||||
if (!now) lose('copy-unavailable', 'retire');
|
||||
if (now.state === 'refused') return now.claims ? lose('copy-refused', 'collision') : deny('copy-unavailable');
|
||||
if (now.level !== copy.level || now.name !== copy.name || now.project !== copy.project) lose('copy-unavailable', 'retire');
|
||||
if (now.definition !== copy.definition) lose('credential-role-changed', 'role-changed');
|
||||
const definitions = { ...(snapshot.definitions ?? {}), ...(doc.definitions ?? {}) };
|
||||
const pin = definitionPin(copy.definition, { definitions, rolesDir, readTemplate });
|
||||
let role = null;
|
||||
@@ -106,7 +173,8 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
} catch {
|
||||
role = null;
|
||||
}
|
||||
if (!pin || !role) deny('credential-role-changed');
|
||||
if (!pin) lose('credential-role-changed', 'role-changed');
|
||||
if (!role) deny('credential-role-changed');
|
||||
const code = Object.keys(doc.projects ?? {}).filter((p) => doc.projects[p]?.kind !== 'library');
|
||||
let projects = code;
|
||||
if (copy.level === 'root') {
|
||||
@@ -123,7 +191,10 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
const resolve = (business, instance, doc, record, inc) => {
|
||||
const f = facts(business, instance, doc);
|
||||
const res = resolveCopyBinding({ orgId: business, incarnation: inc, snapshot, instance, record, pin: f.pin, role: f.role, projects: f.projects });
|
||||
if (!res.ok) deny(res.code);
|
||||
if (!res.ok) {
|
||||
if (res.code === 'credential-role-changed' || res.code === 'credential-scope-exceeded') lose(res.code, 'role-changed');
|
||||
deny(res.code);
|
||||
}
|
||||
if (Object.keys(res.services).some((s) => !COPY_BINDABLE.includes(s))) deny('credential-scope-unverified');
|
||||
return { ...f, res };
|
||||
};
|
||||
@@ -133,24 +204,25 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
// Live proof of one slot token against the record: the service's own answer, the grant id the
|
||||
// verifier committed, and a rotation date not yet passed.
|
||||
// The transport held to a deadline: each call gets what is left, and none starts after it.
|
||||
const within = (deadline) => (req) => {
|
||||
const within = (deadline, signal = undefined) => (req) => {
|
||||
const left = deadline - Date.now();
|
||||
if (left <= 0) return Promise.resolve({ status: 0, json: null, total: null });
|
||||
return transport({ ...req, timeoutMs: Math.min(req.timeoutMs ?? left, left) });
|
||||
if (left <= 0 || signal?.aborted) return Promise.resolve({ status: 0, json: null, total: null });
|
||||
return transport({ ...req, timeoutMs: Math.min(req.timeoutMs ?? left, left), ...(signal ? { signal } : {}) });
|
||||
};
|
||||
const prove = async (doc, record, token, inc, deadline) => {
|
||||
const prove = async (doc, record, token, inc, deadline, signal = undefined) => {
|
||||
const approved = record.services.gitea;
|
||||
if (approved.rotateBy < today()) deny('credential-expired');
|
||||
const { origin, repos } = boundRepositories(doc, approved.resources.projects);
|
||||
const login = copyBotLogin(inc, record.copyId, 'gitea');
|
||||
if (!login) deny('credential-revoked');
|
||||
const grant = await verifyGiteaToken({ transport: within(deadline), url: origin, ca, timeoutMs, token, approved: approved.scopes, repos, login, rotateBy: approved.rotateBy });
|
||||
const grant = await verifyGiteaToken({ transport: within(deadline, signal), url: origin, ca, timeoutMs, token, approved: approved.scopes, repos, login, rotateBy: approved.rotateBy });
|
||||
if (`gitea:${grant.userId}:${grant.tokenId}` !== approved.grantId) deny('credential-revoked');
|
||||
};
|
||||
|
||||
async function boot(business) {
|
||||
if (business !== org) return;
|
||||
const deadline = Date.now() + bootMs;
|
||||
const losses = [];
|
||||
// An inactive or unreadable registry entry leaves null: the resolver refuses every copy with its
|
||||
// own code.
|
||||
let incarnation = null;
|
||||
@@ -172,8 +244,9 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
try {
|
||||
if ((copy.missing ?? []).some((s) => !COPY_BINDABLE.includes(s))) deny('credential-scope-unverified');
|
||||
const { pin, res } = resolve(business, instance, doc, record, incarnation);
|
||||
if (record.services.gitea.rotateBy < today()) lose('credential-expired', 'expired');
|
||||
const identity = credentials.copyIdentity(slot(instance), 'gitea');
|
||||
if (!identity) deny('credential-unavailable');
|
||||
if (!identity) lose('credential-unavailable', 'unavailable');
|
||||
const { token } = credentials.readCopy(slot(instance), 'gitea', identity);
|
||||
await prove(doc, record, token, incarnation, deadline);
|
||||
// The origin the token was proved at is the only one a call goes to.
|
||||
@@ -184,18 +257,51 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
}));
|
||||
} catch (e) {
|
||||
failed.set(instance, e instanceof BusError && KEPT.test(e.code) ? e.code : 'credential-unavailable');
|
||||
if (e?.loss) losses.push([instance, e.loss]);
|
||||
}
|
||||
}
|
||||
for (const [instance, cause] of losses) report(business, instance, cause);
|
||||
}
|
||||
// CB4: tell onLoss once that a copy's binding lost its authority for good, when its record still
|
||||
// holds a grant to revoke. onLoss is the revoker's start: it never throws into a check.
|
||||
function report(business, instance, cause) {
|
||||
if (lost.has(instance) || typeof onLoss !== 'function') return;
|
||||
let record;
|
||||
try {
|
||||
record = writer.read(business).document.copyBindings?.[instance];
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (!object(record) || record.state !== 'bound' || !Object.values(record.services ?? {}).some((x) => typeof x?.grantId === 'string')) return;
|
||||
lost.add(instance);
|
||||
try {
|
||||
onLoss(business, instance, cause);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// The current authority of a copy, or the refusal. `withToken` also returns the slot's token,
|
||||
// read now by the boot's identity and checked against the boot's MAC; only call() asks for it.
|
||||
function current(business, instance, { withToken = false } = {}) {
|
||||
function current(business, instance, opts = {}) {
|
||||
try {
|
||||
return check(business, instance, opts);
|
||||
} catch (e) {
|
||||
if (e?.loss && business === org && typeof instance === 'string') report(business, instance, e.loss);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
function check(business, instance, { withToken = false } = {}) {
|
||||
if (business !== org || typeof instance !== 'string') deny('copy-unavailable');
|
||||
if (fences.get(instance)) deny('credential-revoking');
|
||||
if (closed.has(instance)) deny(closed.get(instance));
|
||||
const v = verified.get(instance);
|
||||
// A verified copy of an organization archived or deleted since boot answers that first: a deleted
|
||||
// organization's file is gone, and its read would only say credential-unavailable.
|
||||
if (v) {
|
||||
const live = liveIncarnation();
|
||||
if (live !== v.incarnation) deny(live === null ? inactive() : 'org-unavailable');
|
||||
}
|
||||
const doc = read(business);
|
||||
const record = doc.copyBindings?.[instance];
|
||||
const v = verified.get(instance);
|
||||
if (!v) {
|
||||
if (object(record) && record.state === 'bound' && allGranted(record.services))
|
||||
deny(failed.get(instance) ?? 'restart-required');
|
||||
@@ -208,24 +314,27 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
}
|
||||
// A verified copy holds only while its organization is active under the incarnation boot proved.
|
||||
const incarnation = liveIncarnation();
|
||||
if (!object(record) || record.state !== 'bound' || record.copyId !== v.copyId || record.orgIncarnation !== v.incarnation
|
||||
|| incarnation !== v.incarnation) deny('credential-revoked');
|
||||
if (incarnation !== v.incarnation) deny(incarnation === null ? inactive() : 'org-unavailable');
|
||||
// CB4: a revoke in flight, an incomplete one, or a finished one's tombstone answers its own code.
|
||||
if (object(record) && record.state === 'revoking') deny(record.revoke?.failed ? 'credential-revoke-failed' : 'credential-revoking');
|
||||
if (object(record) && record.state === 'unbound' && record.revoke?.done === true && CAUSE_CODES[record.revoke.cause]) deny(CAUSE_CODES[record.revoke.cause]);
|
||||
if (!object(record) || record.state !== 'bound' || record.copyId !== v.copyId || record.orgIncarnation !== v.incarnation) deny('credential-revoked');
|
||||
if (record.generation !== v.generation || canonicalJson(grantsOf(record)) !== canonicalJson(v.grants)) deny('restart-required');
|
||||
const { res } = resolve(business, instance, doc, record, incarnation);
|
||||
if (canonicalJson(Object.keys(res.services)) !== canonicalJson(v.services)) deny('restart-required');
|
||||
// A project's remote moved to another server is a new binding: the token never goes there.
|
||||
const reach = boundRepositories(doc, res.services.gitea.projects);
|
||||
if (reach.origin !== v.origin) deny('restart-required');
|
||||
if (record.services.gitea.rotateBy < today()) deny('credential-expired');
|
||||
if (record.services.gitea.rotateBy < today()) lose('credential-expired', 'expired');
|
||||
const identity = credentials.copyIdentity(slot(instance), 'gitea');
|
||||
if (!identity || identity.dev !== v.identity.dev || identity.ino !== v.identity.ino) deny('credential-unavailable');
|
||||
if (!identity || identity.dev !== v.identity.dev || identity.ino !== v.identity.ino) lose('credential-unavailable', 'unavailable');
|
||||
let token;
|
||||
try {
|
||||
token = credentials.readCopy(slot(instance), 'gitea', v.identity).token;
|
||||
} catch {
|
||||
deny('credential-unavailable');
|
||||
}
|
||||
if (!timingSafeEqual(mac(token), v.mac)) deny('credential-unavailable');
|
||||
if (!timingSafeEqual(mac(token), v.mac)) lose('credential-unavailable', 'unavailable');
|
||||
const authority = sha256(canonicalJson({
|
||||
org, instance, pin: v.pin, copyId: v.copyId, incarnation: v.incarnation, generation: v.generation, grants: v.grants, services: res.services,
|
||||
origin: v.origin, repos: [...reach.repos].sort(),
|
||||
@@ -250,7 +359,12 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
async prepare(business, instance, revision = undefined) {
|
||||
sameRevision(business, revision);
|
||||
const before = current(business, instance, { withToken: true });
|
||||
await prove(before.doc, before.record, before.token, before.record.orgIncarnation, Date.now() + prepareMs);
|
||||
const use = track(instance);
|
||||
try {
|
||||
await use.run(prove(before.doc, before.record, before.token, before.record.orgIncarnation, Date.now() + prepareMs, use.signal));
|
||||
} finally {
|
||||
use.done();
|
||||
}
|
||||
const after = current(business, instance);
|
||||
if (after.authority !== before.authority) deny('copy-authority-changed');
|
||||
sameRevision(business, revision);
|
||||
@@ -263,7 +377,100 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
if (current(business, instance).authority !== digest) deny('copy-authority-changed');
|
||||
sameRevision(business, revision);
|
||||
},
|
||||
revoking: (business, instance) => business === org && Boolean(fences.get(instance)),
|
||||
// CB5: the copy's binding status for its owner's views, as copy-binding-status.mjs's fixed
|
||||
// { status, next }. The same checks a launch runs, without the loss report (the sweep makes it)
|
||||
// and without returning anything the check read. An approved binding not yet issued, or one a
|
||||
// refused issue left pending, needs the owner's issue before anything else. `entry` is the copy's
|
||||
// boot entry: only one bound at boot, or unbound for want of a service, has a status. One that
|
||||
// lacks a service a copy cannot bind is refused at boot whatever it binds (above), so it reads
|
||||
// copy-service-unbindable and never asks for a gitea binding, unless a revoke of a binding it
|
||||
// was issued anyway is running or failed: that state is the owner's next step.
|
||||
status(business, instance, entry) {
|
||||
const unbound = entry?.state === 'unbound' && Array.isArray(entry.missing) && entry.missing.length > 0;
|
||||
if (!Array.isArray(entry?.bound) && !unbound) return null;
|
||||
const revoking = business === org && (Boolean(fences.get(instance)) || closed.has(instance));
|
||||
if (unbound && !revoking && entry.missing.some((s) => !COPY_BINDABLE.includes(s))) return copyBindingStatus('copy-service-unbindable');
|
||||
let code = 'ready';
|
||||
try {
|
||||
check(business, instance);
|
||||
} catch (e) {
|
||||
code = typeof e?.code === 'string' ? e.code : 'credential-unavailable';
|
||||
}
|
||||
if (['credential-unbound', 'credential-revoked', 'restart-required'].includes(code)) {
|
||||
let record = null;
|
||||
try {
|
||||
record = read(business).copyBindings?.[instance];
|
||||
} catch {}
|
||||
if (object(record) && record.state === 'pending') code = 'issue-required';
|
||||
}
|
||||
return copyBindingStatus(code);
|
||||
},
|
||||
revoking: (business, instance) => business === org && (Boolean(fences.get(instance)) || closed.has(instance)),
|
||||
// CB4: shut the copy before its revoke publishes anything. It admits nothing and every check
|
||||
// answers `code` until open(); the boot's proof of it is forgotten, so nothing reopens it but a
|
||||
// new boot. Idempotent.
|
||||
close(business, instance, code = 'credential-revoking') {
|
||||
if (business !== org || typeof instance !== 'string') return false;
|
||||
closed.set(instance, code);
|
||||
if (verified.has(instance) && !parked.has(instance)) parked.set(instance, verified.get(instance));
|
||||
verified.delete(instance);
|
||||
lost.add(instance);
|
||||
return true;
|
||||
},
|
||||
// After a definite finish: the record's own code (its tombstone) answers from then on. `restore`
|
||||
// is for an owner's revoke the writer refused before any change: the copy is as it was.
|
||||
open(business, instance, restore = false) {
|
||||
if (business !== org) return;
|
||||
closed.delete(instance);
|
||||
if (restore && parked.has(instance)) {
|
||||
verified.set(instance, parked.get(instance));
|
||||
lost.delete(instance);
|
||||
}
|
||||
parked.delete(instance);
|
||||
},
|
||||
closed: (business, instance) => business === org && closed.has(instance),
|
||||
// Abort every request in flight for the copy and wait for each to settle. Returns how many.
|
||||
async drain(business, instance) {
|
||||
if (business !== org) return 0;
|
||||
const entries = [...(inflight.get(instance) ?? [])];
|
||||
for (const e of entries) e.ac.abort();
|
||||
await Promise.allSettled(entries.map((e) => e.settled));
|
||||
return entries.length;
|
||||
},
|
||||
inflight: (business, instance) => (business === org ? inflight.get(instance)?.size ?? 0 : 0),
|
||||
// Every copy whose record holds a grant, checked as a use would check it (no service call): a
|
||||
// verified copy through current(), an unverified one by its live copy, role and slot. A loss
|
||||
// reaches onLoss. Returns the instances found lost.
|
||||
sweep(business) {
|
||||
if (business !== org) return [];
|
||||
let doc;
|
||||
try {
|
||||
doc = writer.read(business).document;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
const found = [];
|
||||
for (const [instance, record] of Object.entries(doc.copyBindings ?? {})) {
|
||||
if (lost.has(instance) || !object(record) || record.state !== 'bound'
|
||||
|| !Object.values(record.services ?? {}).some((x) => typeof x?.grantId === 'string')) continue;
|
||||
try {
|
||||
if (verified.has(instance)) current(business, instance);
|
||||
else {
|
||||
const inc = liveIncarnation();
|
||||
if (inc === null || record.orgIncarnation !== inc) continue;
|
||||
resolve(business, instance, doc, record, inc);
|
||||
if (record.services.gitea?.rotateBy < today()) lose('credential-expired', 'expired');
|
||||
if (!credentials.copyIdentity(slot(instance), 'gitea')) lose('credential-unavailable', 'unavailable');
|
||||
}
|
||||
} catch (e) {
|
||||
if (e?.loss) {
|
||||
report(business, instance, e.loss);
|
||||
found.push(instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
},
|
||||
// A write that can take a copy's grant away runs inside the fence. A definite answer (success,
|
||||
// or a refusal the writer gave before any change) lifts it; an unknown outcome keeps it.
|
||||
async fence(business, instance, fn) {
|
||||
@@ -305,10 +512,21 @@ export function copyLaunchGate({ credentials, writer, configDir, rolesDir, snaps
|
||||
if (READ.includes(method) && body !== undefined) deny('copy-request-refused');
|
||||
const { origin, repos } = before.reach;
|
||||
if (!repos.has(`${m[1]}/${m[2]}`.toLowerCase())) deny('copy-request-refused');
|
||||
const r = await transport({ url: origin, method, path, auth: { token: before.token }, ...(body === undefined ? {} : { body }), ca, timeoutMs });
|
||||
const use = track(instance);
|
||||
let r;
|
||||
try {
|
||||
r = await use.run(transport({ url: origin, method, path, auth: { token: before.token }, ...(body === undefined ? {} : { body }), ca, timeoutMs, signal: use.signal }));
|
||||
} finally {
|
||||
use.done();
|
||||
}
|
||||
// The answer is the session's only while the authority still holds.
|
||||
const after = current(business, instance);
|
||||
if (after.authority !== copyAuthority) deny('credential-revoked');
|
||||
// CB4: the service refusing the token it accepted at boot is an upstream revoke.
|
||||
if (object(r) && r.status === 401) {
|
||||
report(business, instance, 'upstream');
|
||||
deny('credential-revoked');
|
||||
}
|
||||
if (!object(r) || !Number.isSafeInteger(r.status) || r.status === 0) deny('credential-unavailable');
|
||||
if (r.status >= 300 && r.status < 400) deny('copy-redirect-refused');
|
||||
const out = { status: r.status, json: r.json ?? null };
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
// #1873 CB4: the revoke of an agent copy's credential binding, for every lifecycle event that ends it
|
||||
// (design docs/plans/2026-11-11_copy-credential-binding-design.md sections 3 and 6).
|
||||
//
|
||||
// One job per copy, in this order, and no lock held across any step:
|
||||
// 1. close the launch gate shuts the copy: no admission, every call refuses (sync, before any
|
||||
// await, so a launch that has not been admitted yet never escapes the stop inventory);
|
||||
// 2. begin the writer records the pending op (`revoking`, a fresh op id) or joins the one pending;
|
||||
// 3. drain the gate aborts the copy's requests in flight and waits for each to settle;
|
||||
// 4. stop every ordinary and thread run of the copy is ended, and the job waits for each run's
|
||||
// end to be acknowledged;
|
||||
// 5. slot the copy's slot file goes;
|
||||
// 6. remote the grant is revoked at the service through the organization's managed adapter, or
|
||||
// recorded `not-managed` when the organization has none;
|
||||
// 7. finish the writer records the unbound tombstone when every step was definite, or keeps the
|
||||
// record revoking with the steps that were not. Only a definite finish reopens the gate
|
||||
// (to the tombstone's own code); the stop is confirmed before that, always.
|
||||
// A throw at any step leaves the record revoking: resume() at the next start runs the same op id
|
||||
// again. Nothing in between resolves, launches or reloads the copy's token. `hooks` are for tests:
|
||||
// hooks[name]() runs at each named point and may throw to simulate a crash there.
|
||||
//
|
||||
// `gate` is the launch gate, or business => gate|null when only some organizations have one (the
|
||||
// gate holds one organization's copies). sweep() also finds a copy a named instance now shadows: the
|
||||
// name is in the file's roles, and its former copy's record is not a finished tombstone.
|
||||
import { BusError } from './broker.mjs';
|
||||
import { COPY_BINDABLE } from '../../organization/src/agent-copies.mjs';
|
||||
import { copyBotLogin } from '../../organization/src/copy-bindings.mjs';
|
||||
import { boundRepositories } from './copy-grant.mjs';
|
||||
import { giteaGrant } from './gitea-grant.mjs';
|
||||
import { organizationWriter } from '../../organization/src/organization.mjs';
|
||||
import { readOrgEntrySync } from '../../organization/src/org-registry.mjs';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
export const REVOKE_POINTS = Object.freeze([
|
||||
'beforePublication', 'afterPublication', 'beforeStop', 'afterStop', 'beforeSecret', 'afterSecret',
|
||||
'beforeRemote', 'afterRemote', 'beforeReceipt', 'afterReceipt',
|
||||
]);
|
||||
const OWNER_CAUSES = ['revoke', 'unbind'];
|
||||
const definite = (e) => typeof e?.code === 'string' && /^[a-z][a-z0-9-]{1,80}$/.test(e.code) && !e.code.startsWith('outcome-unknown');
|
||||
|
||||
export function copyRevoker({ writer, gate = null, credentials, stopRuns, remote = null, definitions = () => ({}), hooks = {} }) {
|
||||
const jobs = new Map();
|
||||
const gateOf = (business) => (typeof gate === 'function' ? gate(business) : gate) ?? null;
|
||||
const tombstone = (r) => r?.state === 'unbound' && r.revoke?.done === true;
|
||||
const doc = (business) => {
|
||||
try {
|
||||
return writer.read(business).document;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
const hook = async (name) => {
|
||||
if (typeof hooks[name] === 'function') await hooks[name]();
|
||||
};
|
||||
const slot = (business, instance) => business + '/' + instance;
|
||||
|
||||
async function job(business, instance, { cause, actor = 'bus', owner = false, expect = undefined }) {
|
||||
const gate = gateOf(business);
|
||||
const wasClosed = gate?.closed?.(business, instance) ?? false;
|
||||
gate?.close(business, instance);
|
||||
let begun;
|
||||
try {
|
||||
await hook('beforePublication');
|
||||
begun = writer.revokeCopyBinding(business, { phase: 'begin', instance, cause, ...(expect ? { expect } : {}) },
|
||||
{ actor, owner, definitions: definitions(business) });
|
||||
} catch (e) {
|
||||
// An owner's verb refused before any change leaves the copy as it was; a loss the bus found
|
||||
// keeps it closed whatever the writer said.
|
||||
if (definite(e) && !wasClosed && OWNER_CAUSES.includes(cause)) gate?.open(business, instance, true);
|
||||
throw e;
|
||||
}
|
||||
await hook('afterPublication');
|
||||
if (begun.state !== 'revoking') {
|
||||
gate?.open(business, instance);
|
||||
return begun;
|
||||
}
|
||||
const op = begun.op;
|
||||
const record = writer.read(business).document.copyBindings?.[instance];
|
||||
if (record?.state !== 'revoking' || record.revoke?.id !== op) throw new BusError('credential-binding-changed');
|
||||
await gate?.drain(business, instance);
|
||||
await hook('beforeStop');
|
||||
let stop = 'failed';
|
||||
try {
|
||||
const r = await stopRuns(business, instance, { op, cause: record.revoke.cause });
|
||||
if (r?.confirmed === true) stop = 'confirmed';
|
||||
} catch {
|
||||
stop = 'failed';
|
||||
}
|
||||
await hook('afterStop');
|
||||
// The gate is closed and drained: nothing reads the slot again, whether or not every run has
|
||||
// acknowledged its end yet. The record stays revoking until it has.
|
||||
await hook('beforeSecret');
|
||||
let secret = 'absent';
|
||||
for (const service of [...new Set([...Object.keys(record.services ?? {}), ...COPY_BINDABLE])].sort()) {
|
||||
let r;
|
||||
try {
|
||||
r = credentials.discardCopy(slot(business, instance), service);
|
||||
} catch {
|
||||
r = 'failed';
|
||||
}
|
||||
if (r === 'failed') secret = 'failed';
|
||||
else if (r === 'removed' && secret !== 'failed') secret = 'removed';
|
||||
}
|
||||
await hook('afterSecret');
|
||||
await hook('beforeRemote');
|
||||
const outcomes = {};
|
||||
for (const service of Object.keys(record.services ?? {}).sort()) {
|
||||
if (typeof record.services[service]?.grantId !== 'string') {
|
||||
outcomes[service] = 'absent';
|
||||
continue;
|
||||
}
|
||||
// An earlier pass of this op already revoked it at the service.
|
||||
if (record.revoke?.remote?.[service] === 'revoked') {
|
||||
outcomes[service] = 'revoked';
|
||||
continue;
|
||||
}
|
||||
if (typeof remote !== 'function') {
|
||||
outcomes[service] = 'not-managed';
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
outcomes[service] = await remote(business, instance, record, service);
|
||||
} catch {
|
||||
outcomes[service] = 'failed';
|
||||
}
|
||||
if (!['revoked', 'absent', 'not-managed', 'failed'].includes(outcomes[service])) outcomes[service] = 'failed';
|
||||
}
|
||||
await hook('afterRemote');
|
||||
await hook('beforeReceipt');
|
||||
const done = writer.revokeCopyBinding(business, { phase: 'finish', instance, opId: op, steps: { stop, slot: secret, remote: outcomes } },
|
||||
{ actor: 'bus', owner: true, definitions: definitions(business) });
|
||||
await hook('afterReceipt');
|
||||
if (done.state === 'unbound') gate?.open(business, instance);
|
||||
else gate?.close(business, instance, 'credential-revoke-failed');
|
||||
return { ...done, steps: { stop, slot: secret, remote: outcomes } };
|
||||
}
|
||||
|
||||
const revoker = {
|
||||
// Run (or join) the copy's revoke and wait for it. Refusals propagate.
|
||||
run(business, instance, opts) {
|
||||
const key = business + '/' + instance;
|
||||
if (jobs.has(key)) return jobs.get(key);
|
||||
const p = job(business, instance, opts).finally(() => jobs.delete(key));
|
||||
jobs.set(key, p);
|
||||
return p;
|
||||
},
|
||||
// Fire and forget, for a loss the gate found: the job's outcome is in the record and its receipt.
|
||||
start(business, instance, opts) {
|
||||
revoker.run(business, instance, opts).catch(() => {});
|
||||
},
|
||||
has: (business, instance) => jobs.has(business + '/' + instance),
|
||||
// The copy's live binding record (pending, bound or revoking), or null. An unreadable file is null:
|
||||
// the verbs then take their ordinary path, which refuses on the same read.
|
||||
binding(business, instance) {
|
||||
const r = doc(business)?.copyBindings?.[instance];
|
||||
return r && ['pending', 'bound', 'revoking'].includes(r.state) ? r : null;
|
||||
},
|
||||
// True while the name's former copy still holds a record that is not a finished tombstone, or its
|
||||
// revoke runs: its slot is not the named instance's yet. An unreadable file answers true.
|
||||
pending(business, instance) {
|
||||
if (jobs.has(business + '/' + instance)) return true;
|
||||
const d = doc(business);
|
||||
if (d === null) return true;
|
||||
const r = d.copyBindings?.[instance];
|
||||
return r !== undefined && !tombstone(r);
|
||||
},
|
||||
// The gate's checks for every copy holding a grant, and each shadowed name's cleanup. Returns
|
||||
// nothing; each loss starts its revoke.
|
||||
sweep(business) {
|
||||
try {
|
||||
gateOf(business)?.sweep(business);
|
||||
} catch {}
|
||||
const d = doc(business);
|
||||
if (d === null) return;
|
||||
for (const [instance, r] of Object.entries(d.copyBindings ?? {}))
|
||||
if (Object.hasOwn(d.roles ?? {}, instance) && !tombstone(r) && r.state !== 'revoking')
|
||||
revoker.start(business, instance, { cause: 'shadowed', actor: 'bus' });
|
||||
},
|
||||
// Every revoke a previous run of the bus left pending, run again with its own op id. The owner
|
||||
// authorized an owner cause when it began; the host finishes it. Returns the settled outcomes.
|
||||
async resume(business) {
|
||||
const d = doc(business);
|
||||
if (d === null) return [];
|
||||
const pending = Object.entries(d.copyBindings ?? {}).filter(([, r]) => r?.state === 'revoking');
|
||||
// A pass that already failed a step keeps its failure code while it runs again.
|
||||
for (const [instance, r] of pending) gateOf(business)?.close(business, instance, r.revoke?.failed ? 'credential-revoke-failed' : undefined);
|
||||
return Promise.allSettled(pending.map(([instance, r]) => revoker.run(business, instance, { cause: r.revoke.cause, actor: 'bus', owner: true })));
|
||||
},
|
||||
};
|
||||
return Object.freeze(revoker);
|
||||
}
|
||||
|
||||
// The remote step for a Gitea grant, through the organization's managed admin slot (row 236's): the
|
||||
// copy's token is deleted by id from its bot user. 204 is `revoked`, 404 `absent`. With no admin slot,
|
||||
// or one on another server than the binding's projects, the organization does not manage the copy's
|
||||
// Gitea and the step is `not-managed`: the record says so and never claims the token is gone. Any
|
||||
// other answer, an admin slot that fails its checks, a server that cannot be reached, bound projects
|
||||
// whose server can no longer be read from the organization file, or a record whose organization
|
||||
// incarnation is not the live organization's (`incarnation`, business => string|null) is `failed`,
|
||||
// and the copy stays fenced until a later pass revokes it.
|
||||
export function giteaCopyRemote({ grant, read, incarnation = null, today = () => new Date().toISOString().slice(0, 10) }) {
|
||||
return async (business, instance, record, service) => {
|
||||
if (service !== 'gitea' || !grant) return 'not-managed';
|
||||
const m = /^gitea:(\d+):(\d+)$/.exec(record.services?.gitea?.grantId ?? '');
|
||||
if (!m) return 'failed';
|
||||
let origin;
|
||||
try {
|
||||
origin = boundRepositories(read(business), record.services.gitea.resources?.projects).origin;
|
||||
} catch {
|
||||
return 'failed';
|
||||
}
|
||||
if (typeof incarnation === 'function') {
|
||||
let live = null;
|
||||
try {
|
||||
live = incarnation(business);
|
||||
} catch {}
|
||||
if (typeof live !== 'string' || live !== record.orgIncarnation) return 'failed';
|
||||
}
|
||||
let admin;
|
||||
try {
|
||||
admin = grant.admin(business, (op, run) => run());
|
||||
} catch (e) {
|
||||
return e?.code === 'grant-admin-missing' ? 'not-managed' : 'failed';
|
||||
}
|
||||
let adminOrigin = null;
|
||||
try {
|
||||
adminOrigin = new URL(admin.meta.url).origin;
|
||||
} catch {}
|
||||
if (adminOrigin !== origin) return 'not-managed';
|
||||
if (admin.meta.rotateBy < today()) return 'failed';
|
||||
const login = copyBotLogin(record.orgIncarnation, record.copyId, 'gitea');
|
||||
if (!login) return 'failed';
|
||||
try {
|
||||
await grant.adminChecks(admin.api, admin.meta.account);
|
||||
const r = await admin.api.tokenDelete(login, m[2]);
|
||||
if (r?.status === 204 || r?.status === 200) return 'revoked';
|
||||
if (r?.status === 404) return 'absent';
|
||||
return 'failed';
|
||||
} catch {
|
||||
return 'failed';
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// The managed remote step outside a running bus, for archive and delete from the CLI or the Console:
|
||||
// the organization's Gitea admin slot, its file and its registry incarnation, each read when the
|
||||
// step runs. `transport` and `ca` are the scratch tests' (giteaGrant's own options).
|
||||
const ROLES_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '../../../roles');
|
||||
export function managedCopyRemote({ configDir, dataRoot, transport = undefined, ca = undefined }) {
|
||||
const writer = organizationWriter({ dir: configDir, rolesDir: ROLES_DIR, dataRoot });
|
||||
return giteaCopyRemote({
|
||||
grant: giteaGrant({ configDir, ...(transport ? { transport, ca } : {}) }),
|
||||
read: (id) => writer.read(id).document,
|
||||
incarnation: (id) => readOrgEntrySync(configDir, id)?.incarnation ?? null,
|
||||
});
|
||||
}
|
||||
@@ -618,6 +618,45 @@ export class Credentials {
|
||||
unlinkSync(file);
|
||||
return true;
|
||||
}
|
||||
// #1873 CB4 (design section 3, revoke step "slot"): the slot of a copy whose binding is being
|
||||
// revoked goes, and its temp file with it, whatever file is there now; a named instance that
|
||||
// shadows a former copy's name shares the path, and its entry is held until this runs (runtime
|
||||
// boot), so the slot is never a token the bus loaded. An entry with a token in memory refuses:
|
||||
// that slot is the named instance's own. Returns 'removed' or 'absent'; a directory or an
|
||||
// unsafe parent refuses credential-file and the slot stays for the retry.
|
||||
discardCopy(instance, service) {
|
||||
const k = instance + ':' + service;
|
||||
if (this.#tokens.has(k) || !SERVICES.includes(service)) deny('credential-reference');
|
||||
const file = this.#storePath(instance, service);
|
||||
let removed = false;
|
||||
try {
|
||||
privateDir(this.#storeRoot, { missing: true });
|
||||
privateDir(dirname(dirname(file)), { missing: true });
|
||||
privateDir(dirname(file), { missing: true });
|
||||
} catch (e) {
|
||||
if (e instanceof BusError) throw e;
|
||||
deny('credential-file');
|
||||
}
|
||||
for (const path of [file + '.tmp', file]) {
|
||||
let st;
|
||||
try {
|
||||
st = lstatSync(path);
|
||||
} catch (e) {
|
||||
if (e.code === 'ENOENT') continue;
|
||||
deny('credential-file');
|
||||
}
|
||||
if (st.isDirectory()) deny('credential-file');
|
||||
try {
|
||||
unlinkSync(path);
|
||||
} catch (e) {
|
||||
if (e.code !== 'ENOENT') deny('credential-file');
|
||||
}
|
||||
if (path === file) removed = true;
|
||||
}
|
||||
this.#inflight.get(k)?.abort();
|
||||
this.#held.delete(k);
|
||||
return removed ? 'removed' : 'absent';
|
||||
}
|
||||
// Row 236 (#436b section 5, settle forward): memory takes the store file as it is now, with the
|
||||
// constructor's checks, and `date` becomes the entry's date. A held entry refuses.
|
||||
reload(instance, service, date) {
|
||||
|
||||
@@ -92,21 +92,29 @@ export { definitionSha256 };
|
||||
// Section 3d: one HTTPS call. Never rejects: no answer (refused, reset, TLS failure, timeout, a body over
|
||||
// the cap) is status 0. A redirect is returned as its status, never followed. `auth` is {token} for the
|
||||
// `token` header or {basic: [account, token]} (F1). The result is {status, json, total}, total from
|
||||
// X-Total-Count; it holds no header and no request detail.
|
||||
export function send({ url, method, path, auth, body, ca, timeoutMs = CALL_MS }) {
|
||||
// X-Total-Count; it holds no header and no request detail. An aborted `signal` (#1873 CB4: a revoke
|
||||
// draining a copy's in-flight call) destroys the request and answers status 0.
|
||||
export function send({ url, method, path, auth, body, ca, timeoutMs = CALL_MS, signal = undefined }) {
|
||||
return new Promise((resolve) => {
|
||||
let done = false,
|
||||
req = null;
|
||||
const onAbort = () => {
|
||||
req?.destroy();
|
||||
end({ status: 0, json: null, total: null });
|
||||
};
|
||||
const end = (r) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
signal?.removeEventListener?.('abort', onAbort);
|
||||
resolve(r);
|
||||
};
|
||||
const timer = setTimeout(() => {
|
||||
req?.destroy();
|
||||
end({ status: 0, json: null, total: null });
|
||||
}, timeoutMs);
|
||||
if (signal?.aborted) return onAbort();
|
||||
signal?.addEventListener?.('abort', onAbort, { once: true });
|
||||
try {
|
||||
const headers = {
|
||||
accept: 'application/json',
|
||||
|
||||
@@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { Client } from './client.mjs';
|
||||
import { Client, NOT_SENT_EXIT } from './client.mjs';
|
||||
import { readProcess } from './human.mjs';
|
||||
const file = fileURLToPath(import.meta.url);
|
||||
if (!process.env.MOSAIC_BUS_CLI_NONCE) {
|
||||
@@ -39,5 +39,7 @@ try {
|
||||
process.stdout.write(JSON.stringify(result) + '\n');
|
||||
} catch (e) {
|
||||
process.stderr.write((/^[a-z-]{1,64}$/.test(e.code ?? '') ? e.code : 'invalid-request') + '\n');
|
||||
process.exitCode = 2;
|
||||
// E2E6 fix 6: a request that never left (no socket, or the connect refused) keeps its code and
|
||||
// exits NOT_SENT_EXIT, so `mosaic` can say the bus is not running. Every other failure exits 2.
|
||||
process.exitCode = e?.notSent === true ? NOT_SENT_EXIT : 2;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
-- #1873 SP9: the system-type kind is removed outright (Jason, 2026-11-12: "The only deployed
|
||||
-- system is the dev environment on dragon-lin. It can be wiped and reconfigured. No reason to hold
|
||||
-- on to legacy for this change."). This step drops what 3t-3u created: the four guards, the index
|
||||
-- and the table, with its rows. It is the one deletion of append-only rows in that go, and the
|
||||
-- ruling is its authority. SQLite removes the table's sqlite_sequence row with the table.
|
||||
DROP TRIGGER project_system_types_no_update;
|
||||
DROP TRIGGER project_system_types_no_delete;
|
||||
DROP TRIGGER project_system_types_no_replace;
|
||||
DROP TRIGGER project_system_types_business;
|
||||
DROP INDEX project_system_types_project;
|
||||
DROP TABLE project_system_types;
|
||||
@@ -6,6 +6,8 @@ import { isDeepStrictEqual } from 'node:util';
|
||||
import { BusError } from './broker.mjs';
|
||||
export const REVOKE_TIMEOUT_MS = 5000;
|
||||
export const REVOKE_POLL_MS = 25;
|
||||
export const ISSUE_PROBE_TIMEOUT_MS = 2000;
|
||||
export const ISSUE_REPROBE_MIN_MS = 250;
|
||||
export const MODEL_STOP_TIMEOUT_MS = 5000;
|
||||
const keyId = key => createHash('sha256').update(key).digest('hex').slice(0, 12);
|
||||
const fail = code => { throw new BusError(code); };
|
||||
@@ -223,9 +225,21 @@ export class ModelKeys {
|
||||
this.#sentinel(keys);
|
||||
return this.#issue(instance, false, keys);
|
||||
});
|
||||
let status;
|
||||
try { status = await this.#client.probeKey(proxy.key); }
|
||||
catch { fail('proxy-down'); }
|
||||
// Every v8 CLIProxyAPI applies a key-list PUT by an async reload after it answers, so a
|
||||
// just-issued key can answer 401 for a moment. On 8.0.79 that moment outlasts the confirming
|
||||
// GET (measured, #1873). Only 401 is probed again, until a short deadline; a re-probe waits
|
||||
// about the time left, at least ISSUE_REPROBE_MIN_MS. Any other non-2xx refuses on the first
|
||||
// answer, as before.
|
||||
const deadline = performance.now() + ISSUE_PROBE_TIMEOUT_MS;
|
||||
let status, first = true;
|
||||
for (;;) {
|
||||
const options = first ? undefined : { timeout: Math.max(ISSUE_REPROBE_MIN_MS, Math.ceil(deadline - performance.now())) };
|
||||
first = false;
|
||||
try { status = await this.#client.probeKey(proxy.key, options); }
|
||||
catch { fail('proxy-down'); }
|
||||
if (status !== 401 || performance.now() >= deadline) break;
|
||||
await new Promise(resolve => setTimeout(resolve, REVOKE_POLL_MS));
|
||||
}
|
||||
if (status < 200 || status >= 300) fail('proxy-key-refused');
|
||||
return proxy;
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ const LAUNCH_OPS = new Set([
|
||||
'landHeld',
|
||||
'threadsState',
|
||||
'threadIdle',
|
||||
'failureNotice',
|
||||
'mayLaunch',
|
||||
'landHolders',
|
||||
'landRelease',
|
||||
@@ -67,6 +68,8 @@ function launchOp(m) {
|
||||
// Row 121 (#1615): the launcher's view of the business's threads, and its idle release.
|
||||
if (m.op === 'threadsState') return runtime.threadsState(m.business);
|
||||
if (m.op === 'threadIdle') return runtime.threadIdle(m.record);
|
||||
// #1873 chat-fail F2: one plain line per message a refused thread start or a proxy-stall stop left unanswered.
|
||||
if (m.op === 'failureNotice') return runtime.failureNotice(m.record);
|
||||
// Row 125 (#1619, REQ-LIMIT-4): the limit checks' answer before a launch (async).
|
||||
if (m.op === 'mayLaunch') return runtime.mayLaunch(m);
|
||||
// Row 156 (#1651): the launcher's landing pass: who holds a merge decision, and the stop of a
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Pure broker/runner parsing vocabulary. No host management or state imports.
|
||||
export const PROXY_PAGE_VERBS = Object.freeze(['proxy.accounts', 'proxy.login.start', 'proxy.login.submit',
|
||||
'proxy.login.status', 'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys']);
|
||||
'proxy.login.status', 'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys',
|
||||
// #1873 account-remove (Jason 2026-11-20): one account out of the model service, confirmed.
|
||||
'proxy.account.remove']);
|
||||
export const PROXY_PAGE_CODES = Object.freeze(['not-configured', 'human-required', 'provider-not-allowed',
|
||||
'login-state-unknown', 'callback-url-invalid', 'apikey-invalid', 'priority-invalid', 'priority-unconfirmed',
|
||||
'priority-write-refused', 'models-invalid', 'models-ambiguous', 'models-write-refused', 'models-unconfirmed', 'proxy-uncertain', 'proxy-down', 'invalid-request']);
|
||||
'priority-write-refused', 'models-invalid', 'models-ambiguous', 'models-write-refused', 'models-unconfirmed', 'remove-write-refused', 'remove-unconfirmed', 'proxy-uncertain', 'proxy-down', 'invalid-request']);
|
||||
@@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto';
|
||||
import { isDeepStrictEqual } from 'node:util';
|
||||
import { sha256, refuse } from '../../model-proxy/src/common.mjs';
|
||||
import { PROXY_PAGE_CODES } from './proxy-page-vocabulary.mjs';
|
||||
import { ZAI_URL, MODEL_ID } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
import { MODEL_ID, PROVIDERS, publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
export { PROXY_PAGE_VERBS, PROXY_PAGE_CODES } from './proxy-page-vocabulary.mjs';
|
||||
const object = x => x !== null && typeof x === 'object' && !Array.isArray(x);
|
||||
function fields(args, names) {
|
||||
@@ -13,19 +13,61 @@ const priorityOk = n => Number.isInteger(n) && n >= -1000 && n <= 1000;
|
||||
const identifier = x => typeof x === 'string' && x.length > 0 && x.length <= 512;
|
||||
const stateOk = x => typeof x === 'string' && /^[a-zA-Z0-9_-]{1,512}$/.test(x);
|
||||
const keyId = value => sha256(value).slice(0, 12);
|
||||
const providerName = provider => ({ claude: 'Claude', codex: 'Codex', 'z.ai': 'z.ai', other: 'Other' })[provider];
|
||||
// #1873 settings-backend-a3: provider names, the login allow list and the key allow list all come
|
||||
// from the plugin's PROVIDERS. An account the list does not name reads Other.
|
||||
// An entry counts only with what its door needs: a sign-in host, or a key base URL.
|
||||
const usable = p => p.signIn === 'oauth' ? typeof p.signInHost === 'string' : p.signIn === 'apikey' && typeof p.baseUrl === 'string';
|
||||
const listed = (providers, id, signIn) => providers.find(p => p.id === id && p.signIn === signIn && usable(p)) ?? null;
|
||||
const providerName = (providers, provider) => providers.find(p => p.id === provider)?.label ?? 'Other';
|
||||
// Credential labels/emails and group names are arbitrary upstream strings. A stable
|
||||
// generated display name provides identification without reflecting a misplaced token.
|
||||
function view(id, provider, status, priority) {
|
||||
return { accountId: keyId(id), provider, displayName: `${providerName(provider)} account ${keyId(id)}`,
|
||||
function view(providers, id, provider, status, priority) {
|
||||
return { accountId: keyId(id), provider, displayName: `${providerName(providers, provider)} account ${keyId(id)}`,
|
||||
status, priority: priorityOk(priority) ? priority : 0 };
|
||||
}
|
||||
// #1873 chat-fail F3: one plain line under an account the proxy reports as not working, with
|
||||
// the next step. It is built only from the structured fields of the account read this page
|
||||
// already makes. status_message is matched against fixed patterns and never shown; a label,
|
||||
// email, recovery hint or refresh error message is never read into the line.
|
||||
const SIGN_IN = 'The account needs to sign in again. Sign in again, or add another account.';
|
||||
const NO_REASON = 'The model service gave no reason. Wait and try again later, sign in again, or add another account.';
|
||||
const USAGE_TEXT = /usage limit|quota|rate.?limit|limit reached|too many requests|\b429\b|insufficient.?balance|credit balance/i;
|
||||
// A bare "expired" is not enough: a usage window expires too.
|
||||
const SIGN_IN_TEXT = /\b401\b|unauthori[sz]ed|invalid.?grant|\b(?:\w*[_-])?(?:tokens?|sessions?|credentials?|logins?|auth\w*|grants?)\s+(?:(?:has|have|is|are|was|were)\s+)?(?:been\s+)?expired|\brevoked\b|\bsign.?in\b|\b(?:re-?)?log.?in\b|\bre-?auth/i;
|
||||
const RFC3339 = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})$/;
|
||||
// A reset time the read gives, as "YYYY-MM-DD HH:MM UTC" made from the parsed date, only when
|
||||
// it is a valid RFC 3339 time still ahead of now. Anything else gives no time.
|
||||
function resetTime(value, now) {
|
||||
const m = typeof value === 'string' ? RFC3339.exec(value) : null;
|
||||
if (!m || +m[2] < 1 || +m[2] > 12 || +m[3] < 1 || +m[3] > 31 || +m[4] > 23 || +m[5] > 59 || +m[6] > 59) return null;
|
||||
// A day the month doesn't have (02-31) would roll into the next month, so it is refused.
|
||||
const day = new Date(Date.UTC(2000, +m[2] - 1, +m[3]));
|
||||
day.setUTCFullYear(+m[1]);
|
||||
if (day.getUTCMonth() !== +m[2] - 1 || day.getUTCDate() !== +m[3]) return null;
|
||||
const at = Date.parse(`${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}${m[8]}`);
|
||||
if (!Number.isFinite(at) || at <= now) return null;
|
||||
return `${new Date(at).toISOString().slice(0, 16).replace('T', ' ')} UTC`;
|
||||
}
|
||||
function accountReason(row, now) {
|
||||
const message = typeof row.status_message === 'string' ? row.status_message.slice(0, 4096) : '';
|
||||
const time = resetTime(row.next_retry_after, now);
|
||||
const usage = time ? `The account is out of usage. It resets at ${time}. Wait until then, or add another account.`
|
||||
: 'The account is out of usage. Wait until it resets, or add another account.';
|
||||
// The structured flags come first, then the message patterns.
|
||||
if (row.requires_sign_in === true || (object(row.refresh_error) && Object.keys(row.refresh_error).length > 0)) return SIGN_IN;
|
||||
if (row.quota_blocked === true) return usage;
|
||||
if (SIGN_IN_TEXT.test(message)) return SIGN_IN;
|
||||
if (USAGE_TEXT.test(message)) return usage;
|
||||
if (time) return `The model service paused this account until ${time}. Wait until then, sign in again, or add another account.`;
|
||||
return NO_REASON;
|
||||
}
|
||||
export class ProxyPage {
|
||||
#client; #tag; #records; #lock; #org; #dirty = false; #now; #timeout; #onChange; #pending = new Map(); #writes = Promise.resolve();
|
||||
constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300, onChange = () => {}, lock = null, org = null }) {
|
||||
#client; #tag; #records; #lock; #org; #dirty = false; #now; #timeout; #onChange; #providers; #pending = new Map(); #writes = Promise.resolve();
|
||||
// providers: the plugin's PROVIDERS; a test passes a copy with one taken out.
|
||||
constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300, onChange = () => {}, lock = null, org = null, providers = PROVIDERS }) {
|
||||
if (!client || !/^[a-zA-Z0-9_-]{1,64}$/.test(instanceTag) || typeof keyRecords !== 'function' || typeof now !== 'function'
|
||||
|| typeof onChange !== 'function' || !Number.isSafeInteger(loginTimeoutSeconds) || loginTimeoutSeconds <= 0) throw new TypeError('invalid proxy page settings');
|
||||
this.#lock = lock; this.#org = org;
|
||||
|| typeof onChange !== 'function' || !Number.isSafeInteger(loginTimeoutSeconds) || loginTimeoutSeconds <= 0 || !Array.isArray(providers)) throw new TypeError('invalid proxy page settings');
|
||||
this.#lock = lock; this.#org = org; this.#providers = providers;
|
||||
this.#client = client; this.#tag = instanceTag; this.#records = keyRecords; this.#now = now; this.#timeout = loginTimeoutSeconds * 1000; this.#onChange = onChange;
|
||||
}
|
||||
// Serializes read-modify-write provider config within the bus. P2 owns the
|
||||
@@ -48,28 +90,30 @@ export class ProxyPage {
|
||||
const rows = [];
|
||||
for (const row of response.files) {
|
||||
if (!identifier(row?.id) || !identifier(row?.name)) continue;
|
||||
const provider = ['claude', 'codex'].includes(row.provider) ? row.provider : 'other';
|
||||
const provider = listed(this.#providers, row.provider, 'oauth') ? row.provider : 'other';
|
||||
const status = row.disabled === true ? 'disabled' : row.unavailable === true ? 'unavailable'
|
||||
: ['ready', 'active', 'error', 'disabled', 'unavailable'].includes(row.status) ? row.status : 'unknown';
|
||||
rows.push({ ...view(row.id, provider, status, row.priority), source: 'oauth', name: row.name });
|
||||
rows.push({ ...view(this.#providers, row.id, provider, status, row.priority), source: 'oauth', name: row.name,
|
||||
...(['unavailable', 'error'].includes(status) ? { reason: accountReason(row, this.#now()) } : {}) });
|
||||
}
|
||||
for (const [groupIndex, group] of groups.entries()) {
|
||||
if (!Array.isArray(group?.keys)) continue;
|
||||
for (const [keyIndex, key] of group.keys.entries()) {
|
||||
// auth_index is generated by the proxy. Never fall back to hashing api-key.
|
||||
if (!identifier(key?.auth_index)) continue;
|
||||
const provider = group['base-url'] === ZAI_URL ? 'z.ai' : 'other';
|
||||
const provider = this.#providers.find(p => p.signIn === 'apikey' && usable(p) && p.baseUrl === group['base-url'])?.id ?? 'other';
|
||||
const status = key.disabled === true || group.disabled === true ? 'disabled' : 'active';
|
||||
const configured = group.models ?? [];
|
||||
if (!Array.isArray(configured) || configured.length > 32 || configured.some(m => !m || typeof (m.alias || m.name) !== 'string' || !MODEL_ID.test(m.alias || m.name))) refuse('proxy-down');
|
||||
rows.push({ ...view(key.auth_index, provider, status, key.priority ?? group.priority), source: 'apikey', groupIndex, keyIndex, models: configured.map(m => m.alias || m.name) });
|
||||
rows.push({ ...view(this.#providers, key.auth_index, provider, status, key.priority ?? group.priority), source: 'apikey', groupIndex, keyIndex, models: configured.map(m => m.alias || m.name) });
|
||||
}
|
||||
}
|
||||
return { rows, groups };
|
||||
}
|
||||
#public(row) {
|
||||
const { accountId, provider, displayName, status, priority } = row;
|
||||
return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && provider === 'z.ai' ? { models: row.models } : {}) };
|
||||
return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && listed(this.#providers, provider, 'apikey') ? { models: row.models } : {}),
|
||||
...(typeof row.reason === 'string' ? { reason: row.reason } : {}) };
|
||||
}
|
||||
#session(state, scope, { reportTimeout = false } = {}) {
|
||||
const entry = stateOk(state) ? this.#pending.get(state) : null;
|
||||
@@ -93,17 +137,18 @@ export class ProxyPage {
|
||||
async #request(verb, args, scope) {
|
||||
if (verb === 'proxy.accounts') {
|
||||
fields(args, []);
|
||||
return { accounts: (await this.#accounts()).rows.map(row => this.#public(row)) };
|
||||
// The provider list rides along for the page's select: id, label and sign-in kind only.
|
||||
return { accounts: (await this.#accounts()).rows.map(row => this.#public(row)), providers: publicProviders(this.#providers) };
|
||||
}
|
||||
if (verb === 'proxy.login.start') {
|
||||
fields(args, ['provider']);
|
||||
if (!['claude', 'codex'].includes(args.provider)) refuse('provider-not-allowed');
|
||||
const oauth = listed(this.#providers, args.provider, 'oauth');
|
||||
if (!oauth) refuse('provider-not-allowed');
|
||||
for (const [state, value] of this.#pending) if (value.expires <= this.#now()) this.#pending.delete(state);
|
||||
if (this.#pending.size >= 128) refuse('proxy-down');
|
||||
const answer = await (this.#lock ? this.#write(() => this.#client.oauthStart(args.provider)) : this.#client.oauthStart(args.provider));
|
||||
let url; try { url = new URL(answer.url); } catch { refuse('proxy-down'); }
|
||||
const host = args.provider === 'claude' ? 'claude.ai' : 'auth.openai.com';
|
||||
if (url.protocol !== 'https:' || url.hostname !== host || url.username || url.password || url.port
|
||||
if (url.protocol !== 'https:' || typeof oauth.signInHost !== 'string' || url.hostname !== oauth.signInHost || url.username || url.password || url.port
|
||||
|| url.href.length > 8192 || !stateOk(answer.state) || url.searchParams.getAll('state').length !== 1
|
||||
|| url.searchParams.get('state') !== answer.state || this.#pending.has(answer.state)) refuse('proxy-down');
|
||||
this.#pending.set(answer.state, { business: scope.business, human: scope.human, expires: this.#now() + this.#timeout });
|
||||
@@ -148,12 +193,13 @@ export class ProxyPage {
|
||||
}
|
||||
if (verb === 'proxy.apikey.add') {
|
||||
fields(args, ['provider', 'key']);
|
||||
if (args.provider !== 'z.ai') refuse('provider-not-allowed');
|
||||
const keyed = listed(this.#providers, args.provider, 'apikey');
|
||||
if (!keyed) refuse('provider-not-allowed');
|
||||
if (typeof args.key !== 'string' || !/^[\x21-\x7e]{1,4096}$/.test(args.key)) refuse('apikey-invalid');
|
||||
return this.#write(async () => {
|
||||
const groups = await this.#client.getUpstreamGroups();
|
||||
if (!Array.isArray(groups)) refuse('proxy-down');
|
||||
const group = { name: `mosaic-zai-${randomBytes(6).toString('hex')}`, 'base-url': ZAI_URL,
|
||||
const group = { name: `mosaic-${keyed.id.replace(/[^a-z0-9]/g, '')}-${randomBytes(6).toString('hex')}`, 'base-url': keyed.baseUrl,
|
||||
priority: 0, keys: [{ 'api-key': args.key }] };
|
||||
if (this.#lock && !isDeepStrictEqual(await this.#client.getUpstreamGroups(), groups)) refuse('proxy-down');
|
||||
this.#beforeMutation('proxy.apikey.add');
|
||||
@@ -217,6 +263,39 @@ export class ProxyPage {
|
||||
return { accountId: row.accountId, priority: args.priority };
|
||||
});
|
||||
}
|
||||
// #1873 account-remove (Jason 2026-11-20): one account out of the model service. A signed-in
|
||||
// account's credential is deleted; an API key comes out of its key group, and a group left with
|
||||
// no key comes out whole. Then the accounts are read again: a write the proxy refused is
|
||||
// remove-write-refused, an account still listed after it is remove-unconfirmed.
|
||||
if (verb === 'proxy.account.remove') {
|
||||
fields(args, ['accountId', 'confirm']);
|
||||
if (typeof args.accountId !== 'string' || !/^[a-f0-9]{12}$/.test(args.accountId) || args.confirm !== true) refuse('invalid-request');
|
||||
return this.#write(async () => {
|
||||
const { rows, groups } = await this.#accounts();
|
||||
const original = this.#lock ? structuredClone(groups) : null;
|
||||
const matches = rows.filter(row => row.accountId === args.accountId);
|
||||
if (matches.length !== 1) refuse('invalid-request');
|
||||
const row = matches[0];
|
||||
// The DELETE names a credential file; another listed account on the same name would go too.
|
||||
if (row.source === 'oauth' && rows.some(r => r !== row && r.source === 'oauth' && r.name === row.name)) refuse('invalid-request');
|
||||
if (this.#lock && !isDeepStrictEqual(await this.#client.getUpstreamGroups(), original)) refuse('proxy-down');
|
||||
this.#beforeMutation('proxy.account.remove');
|
||||
try {
|
||||
if (row.source === 'oauth') await this.#client.deleteCredential(row.name);
|
||||
else {
|
||||
const group = groups[row.groupIndex];
|
||||
const keys = group.keys.filter((_, index) => index !== row.keyIndex);
|
||||
await this.#client.putUpstreamGroups(keys.length > 0 ? groups.map((g, index) => index === row.groupIndex ? { ...g, keys } : g)
|
||||
: groups.filter((_, index) => index !== row.groupIndex));
|
||||
}
|
||||
} catch (error) {
|
||||
refuse(error.code === 'proxy-client-refused' || error.code === 'proxy-client-redirect' ? 'remove-write-refused' : 'proxy-down');
|
||||
}
|
||||
if ((await this.#accounts()).rows.some(r => r.accountId === args.accountId)) refuse('remove-unconfirmed');
|
||||
this.#onChange('invalidate');
|
||||
return { accountId: row.accountId, provider: row.provider, removed: true };
|
||||
});
|
||||
}
|
||||
if (verb === 'proxy.keys') {
|
||||
fields(args, []);
|
||||
const values = await this.#client.getKeys();
|
||||
|
||||
@@ -37,6 +37,9 @@ group("copy-request-refused", "R1/R8: a copy calls only its bound repositories w
|
||||
group("organization-changed organization-unreadable", "R3/R8: a launch holds to the organization file revision it resolved", "The organization file changed, or could not be read, while the launch was in progress. Launch the agent again so it resolves the file as it stands now.");
|
||||
group("copy-redirect-refused", "R1/R8: a copy credential goes only to the approved Gitea origin", "The Gitea server answered with a redirect. Fix the bound project's remote to name the server's final address, then ask the owner to issue the credential again; redirects are never followed.");
|
||||
group("project-repo-bound", "R1/R8: a binding grants the repository its projects name when the owner approved it", "Revoke the copy binding that names the project first: the chief raises an unbind decision and the owner approves it. Then set the project repository, then ask the chief to raise a new binding decision for the owner.");
|
||||
// #1873 CB4: a copy of an organization that is archived, being deleted or recreated never launches
|
||||
// or calls; its bindings were made for the organization as it was.
|
||||
group("org-archived org-unavailable", "R1/R8: a copy works only for the active organization it was bound in", "Read the organization's state in the owner Console. Ask the owner to restore it if it is archived; a deleted or recreated organization needs a fresh binding decision for each copy.");
|
||||
group("copy-unavailable copy-refused", "R3/R8: one active, unambiguous copy identity", "Read the organization's copies. Ask the owner to resolve the definition collision or restore the copy's source before raising a binding decision.");
|
||||
group("copy-stale", "R3: root policy changed since this bus booted", "Ask the owner to restart the organization bus to load the approved root policy, then launch a fresh copy.");
|
||||
group("root-policy-unreadable", "R3: root policy evidence must be readable", "Ask the owner to inspect root agent receipt metadata and restart the bus after repairing the read failure.");
|
||||
@@ -68,7 +71,7 @@ group(
|
||||
"Read current session/status and ask the launcher or owner to finish the supported lifecycle transition. A stale or ended run must not reclaim another holder's identity."
|
||||
);
|
||||
group(
|
||||
"apikey-invalid callback-url-invalid harness-off harness-refused key-quota-no-model-key login-state-unknown model-catalogue-unavailable model-harness-incompatible model-key-collision model-provider-unavailable models-ambiguous models-invalid models-unconfirmed models-write-refused priority-invalid priority-unconfirmed priority-write-refused provider-not-allowed proxy-config-invalid proxy-down proxy-first-output-invalid proxy-init-failed proxy-key-invalid proxy-key-issue-failed proxy-key-refused proxy-key-revoking proxy-open proxy-revoke-revision-conflict proxy-rotate-grace-invalid proxy-uncertain thinking-unsupported",
|
||||
"apikey-invalid callback-url-invalid harness-off harness-refused key-quota-no-model-key login-state-unknown model-catalogue-unavailable model-harness-incompatible model-key-collision model-provider-unavailable models-ambiguous models-invalid models-unconfirmed models-write-refused priority-invalid priority-unconfirmed priority-write-refused provider-not-allowed proxy-config-invalid proxy-down proxy-first-output-invalid proxy-init-failed proxy-key-invalid proxy-key-issue-failed proxy-key-refused proxy-key-revoking proxy-open proxy-revoke-revision-conflict proxy-rotate-grace-invalid proxy-uncertain remove-unconfirmed remove-write-refused thinking-unsupported",
|
||||
"R1/R3: owner-managed model and provider configuration",
|
||||
"Ask the owner to inspect the provider configuration through its supported controls; never put keys or callback contents in messages."
|
||||
);
|
||||
@@ -84,6 +87,10 @@ group("proxy-key-mismatch", "R1: a pasted management key is confirmed before ini
|
||||
group("proxy-lock-busy proxy-lock-invalid", "R4: one proxy key write at a time", "Wait for the running proxy write to finish and retry. An invalid lock file needs the owner to inspect the proxy state directory.");
|
||||
group("proxy-not-configured proxy-unit-usage proxy-unit-exists proxy-unit-dropin proxy-unit-copy proxy-unit-path proxy-unit-missing proxy-unit-unhealthy proxy-unit-failed", "R1/R4: proxy user unit is explicit creation", "Initialize the proxy first, then install the unit from the Proxy page or with mosaic proxy unit install. An existing unit or drop-in is never replaced; remove it by hand. An unhealthy unit: read its journal by unit name and restart it.");
|
||||
group("proxy-client-invalid proxy-client-refused proxy-client-response", "R1: proxy management answers", "Check the proxy unit is running and its port answers on the Proxy page, then retry. Upstream bodies are never shown.");
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): the Triage key lives with a bus that rides the owner's
|
||||
// proxy, and a Triage session on a key past its retireAt ends before the key leaves the proxy.
|
||||
group("triage-no-riding-bus", "R1/R4: the Triage key is issued and rotated by a bus that rides the owner's proxy", "Triage needs one organization that uses the owner's models. Start an organization that rides the owner's model proxy, then repair Triage access from Settings, Triage.");
|
||||
group("triage-key-rotated", "R1/R6: a session ends when its Triage key's grace ends", "The Triage key was rotated and the old key's grace has ended. Send the next message; Triage starts again on the current key.");
|
||||
group(
|
||||
"block-recipient block-reply-required block-sender message-recipient-refused message-sender-refused tester-reply-required tester-thread-human user-reply-required user-thread-human",
|
||||
"Role messaging: permitted sender, recipient and reply context",
|
||||
@@ -180,7 +187,7 @@ group(
|
||||
"Read my_rails and use the applicable decision path; ask the owner to inspect grants if no supported path exists."
|
||||
);
|
||||
group(
|
||||
"not-configured project-already-archived project-archived project-exists project-not-archived project-not-configured project-not-found project-record-invalid project-remote-configured project-remote-invalid project-remote-library project-remote-protected project-remote-unknown project-spec-human-only system-type-not-enabled tracker-not-store usage-not-configured",
|
||||
"not-configured project-already-archived project-archived project-exists project-not-archived project-not-configured project-not-found project-record-invalid project-remote-configured project-remote-invalid project-remote-library project-remote-protected project-remote-unknown project-spec-human-only tracker-not-store usage-not-configured",
|
||||
"R3/role scope: configured project and service state",
|
||||
"Read the supported project/service status and ask its owner to repair missing configuration or state before retrying."
|
||||
);
|
||||
@@ -196,11 +203,58 @@ group(
|
||||
"R10: only the caller's own saved templates",
|
||||
"List your saved templates and use one of those names. A saved template is visible only to the owner who saved it."
|
||||
);
|
||||
// #1873 triage-fence: a template on the system-only list (organization vocabulary.mjs SYSTEM_ONLY)
|
||||
// joins no organization by any path.
|
||||
group(
|
||||
"template-system-only",
|
||||
"R8/R10: a system-only role template joins no organization",
|
||||
"This role template serves the system outside every organization. Pick another template or definition; no hire, change, pack, import or instance write gives an organization one."
|
||||
);
|
||||
group(
|
||||
"starter-pack-template-changed",
|
||||
"R8: a pack's hash pin must match the current template",
|
||||
"The template the pack pins has changed since the pack was written. Review the current template, then re-pin the pack to its new hash, or use a pack that already matches."
|
||||
);
|
||||
// #1873 SP4: apply writes exactly the plan the owner previewed. The digest binds the plan to the
|
||||
// organization file's revision; any change in between refuses.
|
||||
group(
|
||||
"pack-plan-stale",
|
||||
"R8: apply writes only the plan its digest names",
|
||||
"The organization file or the pack changed since the preview. Preview again and apply the new digest."
|
||||
);
|
||||
// #1873 SP5: a hire raised from a pack proposal is the role the proposal pinned. An inline pin is the
|
||||
// canonical hash of the entry stored beside it; a stored entry that no longer hashes to it refuses.
|
||||
group(
|
||||
"starter-pack-proposal-stale",
|
||||
"R8: a hire raised from a pack proposal matches its recorded pin",
|
||||
"The role entry stored with this proposal no longer matches the hash it was recorded with. Preview and apply the pack again for a fresh proposal, then raise the hire from that one."
|
||||
);
|
||||
// Ruling 2026-11-14: a person's pack apply is the hire decision. The hire action works from the packs
|
||||
// entry an apply recorded, and each hire runs as the chief's harness and model, else the org
|
||||
// assistant's (lane pr2).
|
||||
group(
|
||||
"pack-not-applied",
|
||||
"R8: a pack hire works from the packs entry an apply recorded",
|
||||
"Preview and apply the pack first; its proposed hires come from the packs entry that apply records."
|
||||
);
|
||||
group(
|
||||
"pack-hire-route-missing",
|
||||
"R3/R8: a hire runs as a harness and model the organization names",
|
||||
"Neither the chief of staff nor the organization's assistant resolves a harness and a model. Give the chief of staff or the assistant both in its vars, or set them at the organization or system level, then use the pack's hire action again."
|
||||
);
|
||||
// #1873 SP8: a saved template carries no credential the bus holds. Save checks every kept string and
|
||||
// key against the credential store; a hit names the field, never the text.
|
||||
group(
|
||||
"template-secret-suspected",
|
||||
"R1/R10: a saved template carries no credential value the bus holds",
|
||||
"A field the template would keep holds a credential value. Remove it from the named definition, board or workflow text, then preview and save again."
|
||||
);
|
||||
// E2E6 fix 8: save never replaces a template already saved under the name.
|
||||
group(
|
||||
"template-exists",
|
||||
"R10: a saved template is never replaced",
|
||||
"A template of this name is already saved for your account. Save under another name, or list your saved templates to pick a free one."
|
||||
);
|
||||
group(
|
||||
"not-resolver",
|
||||
"Role resolve right and decision route",
|
||||
|
||||
+558
-20
@@ -1,5 +1,5 @@
|
||||
import { autonomyHost, authorityHost } from './autonomy-host.mjs';
|
||||
import { hireDecisions } from '../../organization/src/hire-decisions.mjs';
|
||||
import { credentialLines, hireDecisions } from '../../organization/src/hire-decisions.mjs';
|
||||
import { organizationPlugins } from '../../plugins/src/organization.mjs';
|
||||
import { pluginService } from '../../plugins/src/tools.mjs';
|
||||
import { readPluginLock } from '../../plugins/src/pinned.mjs';
|
||||
@@ -27,30 +27,38 @@ import { readSessions, sessionsFile, stopSession } from '../../seat/src/session.
|
||||
import { compactionCount, storeDir } from '../../seat/src/compactions.mjs';
|
||||
import { publishRemote, workingRepo, WorkingError } from '../../seat/src/working.mjs';
|
||||
import { createLoader, hasModule } from '../../plugins/src/loader.mjs';
|
||||
import { installedSystemTypes } from '../../plugins/src/system-types.mjs';
|
||||
import { readWatchView } from '../../plugins/src/harness-watch.mjs';
|
||||
import { pluginSettings } from '../../organization/src/plugin-settings.mjs';
|
||||
import { loadManagementClient } from '../../model-proxy/src/client.mjs';
|
||||
import { ProxyPage } from './proxy-page.mjs';
|
||||
import { createModelCatalogue } from './model-catalogue.mjs';
|
||||
import { sha256 } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { initializeProxy, loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { createProxyLock } from './proxy-lock.mjs';
|
||||
import { ensureKey, readKey as readTriageKey, revokeKeys, sweepDue, sweepKeys } from './triage-key.mjs';
|
||||
import { verifyRestart } from '../../model-proxy/src/admin.mjs';
|
||||
import { ModelKeys, modelOrganization, stopModelRuns, MODEL_STOP_TIMEOUT_MS } from './model-keys.mjs';
|
||||
import { instanceSettings } from '../../organization/src/instance-settings.mjs';
|
||||
import { organizationWriter, OrganizationError, setReplyLimits } from '../../organization/src/organization.mjs';
|
||||
import { definitionVerbs, DEFINITION_BUS_VERBS } from '../../organization/src/definition-verbs.mjs';
|
||||
import { ORG_PREFIX, orgDefinitionName } from '../../organization/src/definitions.mjs';
|
||||
import { ORG_PREFIX, orgDefinitionName, compositionOf, readTemplate, entryFromTemplate, lineDiff, PRIMITIVES } from '../../organization/src/definitions.mjs';
|
||||
import { SYSTEM_ONLY_REFUSAL, systemOnly } from '../../organization/src/vocabulary.mjs';
|
||||
import { installedStarterPacks, readSavedTemplate } from '../../plugins/src/starter-pack.mjs';
|
||||
import { TemplateError, templateList, templatePreview, templateSave } from '../../organization/src/template-save.mjs';
|
||||
import { checkComposition } from '../../organization/src/composition.mjs';
|
||||
import { validateBusinessDocument } from '../../organization/src/organization-config.mjs';
|
||||
import { DEFINITION_HOLDS_CREDENTIAL, heldPath, scanDefinitions } from './definition-scan.mjs';
|
||||
import { ROLE_RIGHTS } from '../../organization/src/org-rights.mjs';
|
||||
import { getProjectRemote } from '../../organization/src/project-remotes.mjs';
|
||||
import { credentialVerbs } from './credential-verbs.mjs';
|
||||
import { loadWithCopies, promoteCopies } from '../../organization/src/agent-copies.mjs';
|
||||
import { COPY_BINDABLE, loadWithCopies, promoteCopies } from '../../organization/src/agent-copies.mjs';
|
||||
import { copyGrant, sweepCopySlots } from './copy-grant.mjs';
|
||||
import { copyLaunchGate } from './copy-launch.mjs';
|
||||
import { copyRevoker as makeCopyRevoker, giteaCopyRemote } from './copy-revoke.mjs';
|
||||
import { readOrgEntrySync } from '../../organization/src/org-registry.mjs';
|
||||
import { loadRole } from '../../organization/src/role.mjs';
|
||||
import { resolveInstance } from '../../organization/src/resolve.mjs';
|
||||
import { canonicalJson } from '../../organization/src/util.mjs';
|
||||
import { canonicalJson, sha256 as textSha256 } from '../../organization/src/util.mjs';
|
||||
import { selectModelProvider } from '../../plugins/src/model-provider-selection.mjs';
|
||||
import { busOrganization } from './organization-context.mjs';
|
||||
import { webService } from './web.mjs';
|
||||
@@ -282,6 +290,313 @@ function modelRunInventory(dataRoot, bound, credentials) {
|
||||
};
|
||||
}
|
||||
|
||||
// #1873 CB4: how often the bus re-checks the copies holding a grant, and retries a pending revoke.
|
||||
const COPY_SWEEP_MS = 60000;
|
||||
|
||||
// #1873 SP4: the human's pack, board and workflow ops (design section 3). preview reads the pack and
|
||||
// the organization file and returns the plan with each item's mark, the lines each proposed hire
|
||||
// would carry against the chief, the noHire marks and the skills this machine has not enabled, plus
|
||||
// a digest of the plan and the file revision. apply rebuilds the plan, refuses pack-plan-stale when
|
||||
// the digest differs and commits it in one applyPack. board.set and workflow.set are one-key plans.
|
||||
// Nothing here reads a saved template but the caller's own (readSavedTemplate, account = actor), and
|
||||
// a pack's origin comes from where it was found, never from the request.
|
||||
const PACK_AS = /^(definitions|workflows)\.([a-z0-9][a-z0-9._-]{0,63})=([a-z0-9][a-z0-9._-]{0,63})$/;
|
||||
const SAVED_PREFIX = 'saved:';
|
||||
// Each op, and whether it writes.
|
||||
const PACK_OPS = Object.freeze({ packs: false, 'pack.preview': false, 'pack.state': false, 'pack.apply': true, 'board.set': true, 'workflow.set': true });
|
||||
const EMPTY_CHIEF = Object.freeze({ tools: [], network: 'none', authority: { withinRole: [], crossRole: [] }, credentials: [] });
|
||||
function packOps({ writer, rolesDir, dataRoot, system, registryFor, assertClean, configDir }) {
|
||||
const bad = () => { throw new BusError('invalid-request'); };
|
||||
const isObject = v => v !== null && typeof v === 'object' && !Array.isArray(v);
|
||||
const scan = value => {
|
||||
if (typeof value === 'string') assertClean(value);
|
||||
else if (value !== null && typeof value === 'object') for (const [k, v] of Object.entries(value)) { assertClean(k); scan(v); }
|
||||
};
|
||||
const installed = async () => (await installedStarterPacks({ dataRoot, rolesDir })).packs;
|
||||
async function loadPack(name, actor) {
|
||||
if (typeof name !== 'string' || name.length > 128) bad();
|
||||
if (name.startsWith(SAVED_PREFIX)) return readSavedTemplate({ configDir, account: actor, name: name.slice(SAVED_PREFIX.length), rolesDir });
|
||||
const { packs, refused } = await installedStarterPacks({ dataRoot, rolesDir });
|
||||
const found = packs.find(p => p.id === name);
|
||||
// #1873 triage-fence: an installed pack refused for naming a system-only template says so.
|
||||
if (!found && refused.some(r => r.name === name && r.code === SYSTEM_ONLY_REFUSAL)) throw new BusError(SYSTEM_ONLY_REFUSAL);
|
||||
if (!found) throw new BusError('starter-pack-not-found');
|
||||
return found;
|
||||
}
|
||||
function renames(as) {
|
||||
const out = { definitions: new Map(), workflows: new Map() };
|
||||
if (as === undefined) return out;
|
||||
if (!Array.isArray(as) || as.length > 32) bad();
|
||||
for (const value of as) {
|
||||
const m = typeof value === 'string' ? PACK_AS.exec(value) : null;
|
||||
// `human` is the reserved step role: no rename may write a definition by that name.
|
||||
if (!m || out[m[1]].has(m[2]) || [...out[m[1]].values()].includes(m[3]) || (m[1] === 'definitions' && m[3] === 'human')) bad();
|
||||
out[m[1]].set(m[2], m[3]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
// The entry a pack definition writes: a template reference becomes the template's own entry, pinned
|
||||
// by its hash (a moved hash refuses), and a full entry is copied as it is. Neither carries rev.
|
||||
function entryOf(value) {
|
||||
// #1873 triage-fence: preview and apply both refuse a pack naming a system-only template, as a
|
||||
// reference or as the from of a full entry.
|
||||
if (systemOnly(value?.template) || systemOnly(value?.from?.template)) throw new BusError(SYSTEM_ONLY_REFUSAL);
|
||||
if (Object.hasOwn(value, 'template') && Object.hasOwn(value, 'sha256') && Object.keys(value).length === 2) {
|
||||
const template = readTemplate(rolesDir, value.template);
|
||||
if (template.sha256 !== value.sha256) throw new BusError('starter-pack-template-changed');
|
||||
const { rev: _rev, ...entry } = entryFromTemplate(template);
|
||||
return { entry, ref: { template: value.template, sha256: value.sha256 } };
|
||||
}
|
||||
return { entry: structuredClone(value), ref: null };
|
||||
}
|
||||
function chiefOf(doc, file) {
|
||||
const loaded = validateBusinessDocument(doc, file, { rolesDir, dataRoot });
|
||||
const chief = loaded.definitions[doc.roles?.[doc.launch?.by]?.definition];
|
||||
if (!chief) return null;
|
||||
return { ...chief, ...resolveInstance({ system, business: loaded, instance: doc.launch.by }).limits };
|
||||
}
|
||||
// Each earlier packs entry of this id and origin (and, for a project pack, this project) lists what
|
||||
// it added; an item absent now that one of them added was removed on purpose, so it stays removed.
|
||||
// A saved template may reuse a shipped id, so the origin is part of the key.
|
||||
const sameApply = (e, id, origin, project) => e.id === id && e.origin === origin && (e.project ?? null) === project;
|
||||
const tombstones = (doc, id, origin, project) => new Set((doc.packs ?? []).filter(e => sameApply(e, id, origin, project)).flatMap(e => e.added ?? []));
|
||||
// #1873 SP5 (design section 3, "The chief applying a pack"): context.agent is an
|
||||
// organization.change holder's call. It names no saved template and no definitions rename
|
||||
// (human-required), and its plan writes no definition: a definition a human apply would add is
|
||||
// marked not-written, and its hire proposal pins the template or the inline entry, which the chief
|
||||
// raises as a hire decision. A workflow step naming a pack definition the organization does not
|
||||
// hold and that is not a V2 template is left out and listed; a workflow left with no step is
|
||||
// marked left-out and not written. applyPack refuses an agent's definitions or saved origin
|
||||
// whatever this builds.
|
||||
async function build(id, args, actor, context) {
|
||||
const agent = context?.agent === true;
|
||||
if (agent && typeof args.pack === 'string' && args.pack.startsWith(SAVED_PREFIX)) throw new BusError('human-required');
|
||||
if (agent && Array.isArray(args.as) && args.as.some(v => typeof v === 'string' && v.startsWith('definitions.'))) throw new BusError('human-required');
|
||||
const { document: doc, revision } = writer.read(id);
|
||||
const found = await loadPack(args.pack, actor);
|
||||
const pack = found.pack;
|
||||
const origin = found.origin;
|
||||
let project = null;
|
||||
if (pack.scope === 'org') { if (args.project !== undefined) bad(); }
|
||||
else {
|
||||
project = args.project;
|
||||
const entry = typeof project === 'string' && Object.hasOwn(doc.projects ?? {}, project) ? doc.projects[project] : null;
|
||||
if (!isObject(entry) || entry.kind === 'library') bad();
|
||||
}
|
||||
const as = renames(args.as);
|
||||
for (const name of as.definitions.keys()) if (!Object.hasOwn(pack.definitions, name)) bad();
|
||||
for (const name of as.workflows.keys()) if (!Object.hasOwn(pack.workflows, name)) bad();
|
||||
// Two pack items of one kind never land on one name: a rename onto another item's own name
|
||||
// would show both as add while the later one replaced the earlier in the plan.
|
||||
for (const kind of ['definitions', 'workflows']) {
|
||||
const targets = Object.keys(pack[kind]).map(name => as[kind].get(name) ?? name);
|
||||
if (new Set(targets).size !== targets.length) bad();
|
||||
}
|
||||
scan(pack);
|
||||
const gone = tombstones(doc, pack.id, origin, project);
|
||||
const scope = project === null ? doc : doc.projects[project];
|
||||
const prefix = project === null ? '' : `projects.${project}.`;
|
||||
const chief = chiefOf(doc, `${id}.json`);
|
||||
const registry = registryFor(doc);
|
||||
const noHire = doc.launch?.noHire ?? [];
|
||||
const templates = new Map();
|
||||
const isTemplate = name => {
|
||||
if (!templates.has(name)) {
|
||||
try { readTemplate(rolesDir, name); templates.set(name, true); } catch (error) {
|
||||
if (error?.name !== 'BusinessError') throw error;
|
||||
templates.set(name, false);
|
||||
}
|
||||
}
|
||||
return templates.get(name);
|
||||
};
|
||||
const items = [];
|
||||
const plan = {};
|
||||
// Definitions are organization-wide in either scope: a project's agents name org definitions.
|
||||
const definitions = {};
|
||||
const checked = {};
|
||||
for (const [name, value] of Object.entries(pack.definitions)) {
|
||||
const target = as.definitions.get(name) ?? name;
|
||||
const { entry, ref } = entryOf(value);
|
||||
const { composition, flagged } = checkComposition(compositionOf(entry), registry, chief ?? EMPTY_CHIEF, { assertClean });
|
||||
checked[name] = { target, entry, ref, flagged: chief ? flagged : null, credentialLines: credentialLines(composition.credentials) };
|
||||
const path = `definitions.${target}`;
|
||||
const current = doc.definitions?.[target];
|
||||
let mark;
|
||||
if (current === undefined) mark = gone.has(path) ? 'removed' : 'add';
|
||||
else mark = canonicalJson(compositionOf(current)) === canonicalJson(compositionOf(entry)) ? 'same' : 'kept';
|
||||
const item = { item: 'definition', name, target, path, mark };
|
||||
if (mark === 'kept') {
|
||||
const before = compositionOf(current), after = compositionOf(entry);
|
||||
const primitives = PRIMITIVES.filter(k => canonicalJson(before[k] ?? null) !== canonicalJson(after[k] ?? null));
|
||||
item.diff = { primitives, instructions: primitives.includes('instructions') ? lineDiff(before.instructions, after.instructions) : [] };
|
||||
}
|
||||
if (agent && mark === 'add') item.mark = mark = 'not-written';
|
||||
if (mark === 'add') definitions[target] = entry;
|
||||
checked[name].added = mark === 'add';
|
||||
items.push(item);
|
||||
}
|
||||
if (Object.keys(definitions).length) plan.definitions = definitions;
|
||||
{
|
||||
const path = `${prefix}board`;
|
||||
let mark;
|
||||
if (scope.board === undefined) mark = gone.has(path) ? 'removed' : 'add';
|
||||
else mark = canonicalJson(scope.board) === canonicalJson(pack.board) ? 'same' : 'kept';
|
||||
items.push({ item: 'board', path, mark });
|
||||
if (mark === 'add') plan[path] = structuredClone(pack.board);
|
||||
}
|
||||
const workflows = {};
|
||||
for (const [name, value] of Object.entries(pack.workflows)) {
|
||||
const target = as.workflows.get(name) ?? name;
|
||||
// The step ids are the pack's own; the organization's workflow keeps title, state and roles.
|
||||
// A role naming one of this pack's definitions follows that definition's --as rename; the
|
||||
// reserved `human` role never moves. A step whose mapped roles collide refuses here, not at apply.
|
||||
const role = r => r !== 'human' && Object.hasOwn(pack.definitions, r) ? as.definitions.get(r) ?? r : r;
|
||||
const workflow = { description: value.description, steps: value.steps.map(s => ({ title: s.title, state: s.state, roles: s.roles.map(role) })) };
|
||||
if (workflow.steps.some(s => new Set(s.roles).size !== s.roles.length)) bad();
|
||||
const path = `${prefix}workflows.${target}`;
|
||||
const leftOut = [];
|
||||
if (agent) {
|
||||
// A step keeps a pack role the organization already defines, or one the pack itself pins to the
|
||||
// template of that name. An inline definition that only shares a template's name is left out.
|
||||
const unwritable = r => r !== 'human' && Object.hasOwn(pack.definitions, r) && !Object.hasOwn(doc.definitions ?? {}, role(r))
|
||||
&& !(checked[r].ref?.template === role(r) && isTemplate(role(r)));
|
||||
workflow.steps = workflow.steps.filter((s, i) => {
|
||||
const missing = value.steps[i].roles.filter(unwritable).map(role);
|
||||
if (missing.length) leftOut.push({ step: i + 1, title: s.title, roles: missing });
|
||||
return missing.length === 0;
|
||||
});
|
||||
}
|
||||
const current = scope.workflows?.[target];
|
||||
let mark;
|
||||
if (agent && workflow.steps.length === 0) mark = 'left-out';
|
||||
else if (current === undefined) mark = gone.has(path) ? 'removed' : 'add';
|
||||
else mark = canonicalJson(current) === canonicalJson(workflow) ? 'same' : 'kept';
|
||||
items.push({ item: 'workflow', name, target, path, mark, ...(leftOut.length ? { leftOut } : {}) });
|
||||
if (mark === 'add') workflows[target] = workflow;
|
||||
}
|
||||
if (Object.keys(workflows).length) plan[`${prefix}workflows`] = workflows;
|
||||
// A proposal is filled when an instance by that name exists, or an open hire decision names it.
|
||||
const instances = project === null ? doc.roles ?? {} : doc.projects[project].agents ?? {};
|
||||
const openHires = context?.openHires ?? [];
|
||||
const hires = [];
|
||||
const proposedHires = [];
|
||||
for (const hire of pack.hires) {
|
||||
const d = checked[hire.definition];
|
||||
const dropped = Object.hasOwn(instances, hire.name) || openHires.some(h => h.name === hire.name && (h.project ?? null) === project);
|
||||
const pin = d.added ? { definition: d.target, rev: 1 } : d.ref ? { ...d.ref } : { inline: d.target, sha256: textSha256(canonicalJson(d.entry)) };
|
||||
const marked = [d.target, hire.definition, d.ref?.template, d.entry.from?.template].some(n => n !== undefined && noHire.includes(n));
|
||||
hires.push({ name: hire.name, definition: d.target, mark: dropped ? 'dropped' : 'propose', pin, flagged: d.flagged, credentialLines: d.credentialLines, noHire: marked });
|
||||
if (!dropped) proposedHires.push({ name: hire.name, reason: hire.reason, pin, ...(Object.hasOwn(pin, 'inline') ? { entry: structuredClone(d.entry) } : {}) });
|
||||
}
|
||||
plan.packs = { id: pack.id, version: pack.version, sha256: textSha256(canonicalJson(pack)), origin, scope: pack.scope, project, proposedHires };
|
||||
scan(plan);
|
||||
const notEnabled = pack.skills.filter(name => { try { lstatSync(join(dataRoot, 'skills-enabled', name)); return false; } catch { return true; } });
|
||||
const digest = textSha256(canonicalJson({ plan, revision }));
|
||||
return {
|
||||
pack: { id: pack.id, version: pack.version, label: pack.label, origin, scope: pack.scope }, project, revision,
|
||||
items, hires, skills: { notEnabled }, chief: chief !== null, plan, digest,
|
||||
};
|
||||
}
|
||||
// context.beforeWrite (SP5) is the broker's recheck of an agent's organization.change grant,
|
||||
// run once more before the writer commits.
|
||||
const write = (id, plan, revision, actor, detail, context) => {
|
||||
context?.beforeWrite?.();
|
||||
const out = writer.applyPack(id, plan, { revision, actor, assertClean, detail });
|
||||
return { revision: out.revision, added: out.added };
|
||||
};
|
||||
const scoped = (args, key) => {
|
||||
if (args.project === undefined) return key;
|
||||
if (typeof args.project !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(args.project)) bad();
|
||||
return `projects.${args.project}.${key}`;
|
||||
};
|
||||
const revisionOf = args => { if (typeof args.revision !== 'string' || !/^[0-9a-f]{64}$/.test(args.revision)) bad(); return args.revision; };
|
||||
// Ruling 2026-11-14 ("If I choose a starter pack and apply it, that is a decision!"): the
|
||||
// organization facts the broker's pack hire loop and the packs read need, as the file stands.
|
||||
// `pack` names a packs entry the way apply names the pack: saved:NAME is the owner's saved
|
||||
// template, any other name the latest shipped or installed entry of that id. The route is a
|
||||
// resolved harness, model and thinking (system, organization and agent vars): the chief's (the
|
||||
// launch.by instance), else the org assistant's (lane pr2, lead ruling 2026-11-14), else null.
|
||||
// routeFrom names which one gave it.
|
||||
const latestEntries = doc => {
|
||||
const latest = new Map();
|
||||
for (const e of doc.packs ?? []) if (isObject(e) && typeof e.id === 'string') latest.set(canonicalJson([e.id, e.origin ?? null, e.project ?? null]), e);
|
||||
return [...latest.values()];
|
||||
};
|
||||
const agentsOf = doc => {
|
||||
const launched = doc.launch?.instances ?? [];
|
||||
const one = (entry, isLaunched) => ({ hireDecision: typeof entry?.hireDecision === 'string' ? entry.hireDecision : null, launched: isLaunched });
|
||||
return {
|
||||
org: Object.fromEntries(Object.entries(doc.roles ?? {}).map(([name, e]) => [name, one(e, launched.includes(name))])),
|
||||
projects: Object.fromEntries(Object.entries(doc.projects ?? {}).filter(([, p]) => isObject(p) && p.kind !== 'library')
|
||||
.map(([project, p]) => [project, Object.fromEntries(Object.entries(p.agents ?? {}).map(([name, e]) => [name, one(e, true)]))])),
|
||||
};
|
||||
};
|
||||
// The org assistant is the instance whose definition is `assistant` (broker.mjs continuationEffect);
|
||||
// with several, the one named `assistant`, else the first in key order.
|
||||
const packRoute = (doc, id) => {
|
||||
const roles = doc.roles ?? {};
|
||||
let loaded = null;
|
||||
const routeOf = instance => {
|
||||
loaded ??= validateBusinessDocument(doc, `${id}.json`, { rolesDir, dataRoot });
|
||||
const { vars } = resolveInstance({ system, business: loaded, instance });
|
||||
const pick = k => (typeof vars?.[k] === 'string' && vars[k] ? vars[k] : null);
|
||||
if (!pick('harness') || !pick('model')) return null;
|
||||
return { harness: pick('harness'), model: pick('model'), ...(pick('thinking') ? { thinking: pick('thinking') } : {}) };
|
||||
};
|
||||
const by = doc.launch?.by;
|
||||
const chief = typeof by === 'string' && Object.hasOwn(roles, by) ? routeOf(by) : null;
|
||||
if (chief) return { route: chief, routeFrom: 'chief' };
|
||||
const assistants = Object.keys(roles).filter(name => isObject(roles[name]) && roles[name].definition === 'assistant');
|
||||
const assistant = assistants.includes('assistant') ? 'assistant' : assistants[0];
|
||||
const route = assistant === undefined ? null : routeOf(assistant);
|
||||
return route ? { route, routeFrom: 'assistant' } : { route: null, routeFrom: null };
|
||||
};
|
||||
const applied = doc => latestEntries(doc).map(e => ({ id: e.id, origin: e.origin ?? null, project: e.project ?? null, version: e.version ?? null,
|
||||
proposedHires: (Array.isArray(e.proposedHires) ? e.proposedHires : []).filter(isObject).map(h => ({ name: h.name, reason: h.reason ?? null })) }));
|
||||
return {
|
||||
// Shipped and installed packs only: a saved template is its owner's, read by name. `applied` is
|
||||
// the latest packs entry of each id, origin and project, and `agents` each instance's hire record.
|
||||
packs: async (id) => {
|
||||
const doc = writer.read(id).document;
|
||||
return { packs: (await installed()).map(({ id: pid, origin, pack }) => ({ id: pid, origin, pack })), applied: applied(doc), agents: agentsOf(doc) };
|
||||
},
|
||||
'pack.state': (id, args) => {
|
||||
if (typeof args.pack !== 'string' || !args.pack) bad();
|
||||
const project = args.project ?? null;
|
||||
if (project !== null && (typeof project !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(project))) bad();
|
||||
const { document: doc, revision } = writer.read(id);
|
||||
const saved = args.pack.startsWith(SAVED_PREFIX);
|
||||
const packId = saved ? args.pack.slice(SAVED_PREFIX.length) : args.pack;
|
||||
const entry = (doc.packs ?? []).filter(e => isObject(e) && e.id === packId && (e.project ?? null) === project && (saved ? e.origin === 'saved' : e.origin !== 'saved')).at(-1) ?? null;
|
||||
const scope = project === null ? doc.roles ?? {} : doc.projects?.[project]?.agents ?? {};
|
||||
return { revision, entry: entry ? structuredClone(entry) : null, ...packRoute(doc, id), names: Object.keys(scope), noHire: [...(doc.launch?.noHire ?? [])] };
|
||||
},
|
||||
'pack.preview': (id, args, actor, context) => build(id, args, actor, context),
|
||||
'pack.apply': async (id, args, actor, context) => {
|
||||
if (typeof args.digest !== 'string') bad();
|
||||
const preview = await build(id, args, actor, context);
|
||||
if (preview.digest !== args.digest) throw new BusError('pack-plan-stale');
|
||||
// An apply of the same pack (same sha256) that adds nothing and proposes what the last apply of
|
||||
// it proposed writes nothing, so a retry loop does not fill the packs list to its cap. A changed
|
||||
// pack still records its entry. The digest pins the revision.
|
||||
const { plan } = preview;
|
||||
const last = (writer.read(id).document.packs ?? []).filter(e => sameApply(e, plan.packs.id, plan.packs.origin, plan.packs.project)).at(-1);
|
||||
if (Object.keys(plan).every(k => k === 'packs') && last && last.sha256 === plan.packs.sha256
|
||||
&& canonicalJson(last.proposedHires ?? []) === canonicalJson(plan.packs.proposedHires))
|
||||
return { revision: preview.revision, added: [], pack: preview.pack.id };
|
||||
return { ...write(id, plan, preview.revision, actor, {}, context), pack: preview.pack.id };
|
||||
},
|
||||
'board.set': (id, args, actor, context) => {
|
||||
if (args.board !== null && !isObject(args.board)) bad();
|
||||
return write(id, { [scoped(args, 'board')]: args.board }, revisionOf(args), actor, {}, context);
|
||||
},
|
||||
'workflow.set': (id, args, actor, context) => {
|
||||
if (typeof args.name !== 'string' || (args.workflow !== null && !isObject(args.workflow))) bad();
|
||||
return write(id, { [scoped(args, 'workflows')]: { [args.name]: args.workflow } }, revisionOf(args), actor, {}, context);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function startBroker({
|
||||
dataRoot,
|
||||
instanceRoot = null,
|
||||
@@ -309,6 +624,10 @@ export async function startBroker({
|
||||
// P1 validated sidecar from the trusted host; never a socket request setting.
|
||||
proxy = null,
|
||||
org = null, instanceDataRoot = null, instanceConfigDir = null,
|
||||
// proxy-scope slice 4: the Triage key sweep on a riding org bus. intervalMs (default 30 s) and
|
||||
// marginMs (default 30 s past a retiring key's retireAt, so the Console stops a session on it
|
||||
// first) are in-process seams, as are now and hook (triage-key.mjs's clock and stage hook).
|
||||
triageKeys = {},
|
||||
// Host-only preparation/admission capabilities. The host builds its own from the organization writer and `trackers`
|
||||
// (request-host.mjs); anything given here replaces the host's, and a capability nothing supplies leaves a setup wait.
|
||||
requestServices = {},
|
||||
@@ -318,6 +637,7 @@ export async function startBroker({
|
||||
builtinBinding = null,
|
||||
modelCatalogue = null, // in-process fixture seam; boot JSON cannot supply a service
|
||||
makeProviderLoader = createLoader, // in-process fixture seam; boot JSON cannot supply a function
|
||||
initProxy = initializeProxy, // in-process fixture seam (a test install pin); boot JSON cannot supply a function
|
||||
// #1873 A3: the boot's root and project agent copies (cli config.mjs copiesAtBoot), plain data. Every
|
||||
// reload of the organization file below re-applies this snapshot, never the root file.
|
||||
copies = null,
|
||||
@@ -325,6 +645,7 @@ export async function startBroker({
|
||||
// In-process only, like librarySync; production passes none.
|
||||
copyTransport = null,
|
||||
}) {
|
||||
if (typeof initProxy !== 'function') throw new BusError('invalid-host-request');
|
||||
if (copies !== null && (typeof copies !== 'object' || Array.isArray(copies) || typeof copies.org !== 'string'
|
||||
|| !Object.hasOwn(businesses ?? {}, copies.org) || copies.copies === null || typeof copies.copies !== 'object' || Array.isArray(copies.copies)))
|
||||
throw new BusError('invalid-host-request');
|
||||
@@ -342,8 +663,13 @@ export async function startBroker({
|
||||
throw new BusError('invalid-host-request');
|
||||
let modelProxyClient = null, proxyPage = null, proxyUsage = null, usageTimer = null, proxyLock = null, proxyPort = null;
|
||||
let modelKeys = null, modelTimer = null, modelSweep = null, modelCatalogues = null;
|
||||
// proxy-scope slice 4: the settings of the proxy this bus rides (boot, picked up or initialized).
|
||||
let proxySettings = null, triageTimer = null, triageSweepRun = null;
|
||||
const modelRuns = new Map();
|
||||
let store, credentials, server, adapter, processing, syncTimer = null, syncPass = null, accessTimer = null, requestTimer = null, copyLaunch = null;
|
||||
let store, credentials, server, adapter, processing, syncTimer = null, syncPass = null, accessTimer = null, requestTimer = null, copyLaunch = null, copyRevoker = null, copyTimer = null;
|
||||
// #1873 CB4: a revoke's stop waits for the broker; a start that fails before it exists stops nothing.
|
||||
let brokerUp = null;
|
||||
const brokerReady = new Promise((resolve) => { brokerUp = resolve; });
|
||||
try {
|
||||
// Lifecycle Task 2 (#797): a product whose mapping is published but not yet activated is not part of
|
||||
// this process. A managed product is part of it only with both a committed activate receipt in the
|
||||
@@ -389,7 +715,7 @@ export async function startBroker({
|
||||
proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${proxy.port}` });
|
||||
}
|
||||
modelProxyClient = await loadManagementClient({ configDir: org ? instanceConfigDir : organization.dir, proxy, receipt: proxyReceipt });
|
||||
proxyPort = proxy.port;
|
||||
proxyPort = proxy.port; proxySettings = proxy;
|
||||
}
|
||||
|
||||
// Row 234 (#1740, r6 X4 third bullet): each host grant record gives its agent a store reference to
|
||||
@@ -421,6 +747,13 @@ export async function startBroker({
|
||||
});
|
||||
for (const entry of sweepCopySlots(storeRoot, documents, references))
|
||||
process.stderr.write(`copies: ${JSON.stringify({ event: 'copy-slot-swept', ...entry })}\n`);
|
||||
// #1873 CB4: a named instance shares its slot path with a former copy of the same name. While
|
||||
// that copy's record is not a finished tombstone, the slot is held (never loaded as the named
|
||||
// instance's) until the shadowed revoke discards it.
|
||||
for (const [id, doc] of documents)
|
||||
for (const [name, r] of Object.entries(doc?.copyBindings ?? {}))
|
||||
if (Object.hasOwn(doc.roles ?? {}, name) && !(r?.state === 'unbound' && r.revoke?.done === true))
|
||||
for (const service of new Set([...COPY_BINDABLE, ...Object.keys(r?.services ?? {})])) held.push(id + '/' + name + ':' + service);
|
||||
}
|
||||
// #1886 (F3 defect l): a store reference with no file yet reads as no key (design D3a), but Credentials
|
||||
// refuses a missing store root, and only issue() made it. Start makes it the same way issue() does when
|
||||
@@ -446,9 +779,26 @@ export async function startBroker({
|
||||
// `businesses`. A binding approved later answers restart-required. copyGrant keeps the boot's own
|
||||
// snapshot (bootCopies), and so does the gate: what it promotes is decided at boot only.
|
||||
const bootCopies = copies;
|
||||
// #1873 CB4: the revoke of a copy's binding, for every organization this bus serves. The gate (one
|
||||
// organization's) closes and drains the copy; the broker stops its runs once it is up.
|
||||
if (organization && storeRoot) {
|
||||
const revokeWriter = organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot: instanceRoot ?? dataRoot });
|
||||
const grant = giteaGrant({ configDir: organization.dir, ...(copyTransport ? { transport: copyTransport.send, ca: copyTransport.ca } : {}) });
|
||||
copyRevoker = makeCopyRevoker({
|
||||
writer: revokeWriter, credentials,
|
||||
gate: (id) => (copyLaunch && bootCopies?.org === id ? copyLaunch : null),
|
||||
stopRuns: async (id, instance, opts) => {
|
||||
const b = await brokerReady;
|
||||
return b ? b.stopCopyRuns(id, instance, opts) : { confirmed: false };
|
||||
},
|
||||
remote: giteaCopyRemote({ grant, read: (id) => revokeWriter.read(id).document, incarnation: (id) => readOrgEntrySync(organization.dir, id)?.incarnation ?? null }),
|
||||
definitions: (id) => (bootCopies?.org === id ? bootCopies.definitions ?? {} : {}),
|
||||
});
|
||||
}
|
||||
if (organization && storeRoot && copies) {
|
||||
copyLaunch = copyLaunchGate({ credentials, writer: organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot: instanceRoot ?? dataRoot }),
|
||||
configDir: organization.dir, rolesDir: organization.rolesDir, snapshot: copies, ...(copyTransport ? { transport: copyTransport.send, ca: copyTransport.ca } : {}) });
|
||||
configDir: organization.dir, rolesDir: organization.rolesDir, snapshot: copies, ...(copyTransport ? { transport: copyTransport.send, ca: copyTransport.ca } : {}),
|
||||
onLoss: (id, instance, cause) => copyRevoker?.start(id, instance, { cause, actor: 'bus' }) });
|
||||
await copyLaunch.boot(copies.org);
|
||||
const verified = copyLaunch.bound();
|
||||
if (Object.keys(verified).length) {
|
||||
@@ -495,7 +845,126 @@ export async function startBroker({
|
||||
// records. A daily key quota moved within one agent's lineage keeps its since and its count.
|
||||
const keyLineage=(business,keyId)=>modelKeyLineage(credentials.status(),business,keyId);
|
||||
const settings=organization?launchSettings({...organization,sessions:()=>readSessions(dataRoot),levels,keyLineage}):null;
|
||||
modelCatalogues = modelCatalogue ?? (settings ? createModelCatalogue({ pluginDataRoot:instanceRoot ?? dataRoot, client: () => modelProxyClient, settings: id => settings.current(id).plugins ?? {}, proxyLock }) : null);
|
||||
// #1873 proxy-pickup: a bus started before the instance had a shared proxy picks it up at the
|
||||
// first request that needs one, through the start path's checks, loader and lock. A client got
|
||||
// that way (or by this bus's own proxy.init) is checked again at every such request and dropped
|
||||
// when the start path would now refuse it. A bus started with a proxy keeps its boot client:
|
||||
// its ModelKeys holds that client. `proxyCause` is the one plain line the owner sees while there
|
||||
// is no client: static text and a refusal code, never a path or a key.
|
||||
let lateProxy = false, proxyCause = null, proxySync = Promise.resolve();
|
||||
const dropLateProxy = cause => {
|
||||
if (lateProxy) { modelProxyClient = null; proxyPage = null; proxyPort = null; proxyLock = null; proxySettings = null; lateProxy = false; modelCatalogues?.invalidate?.(); }
|
||||
proxyCause = cause;
|
||||
};
|
||||
// The host refuses fixture-proxy-configured at boot when a dev fixture is recorded for the
|
||||
// instance and the organization routes a launch to the fixture (cli dev-fixture.mjs
|
||||
// fixtureLauncherOptions). Anything but a plain absence of the record counts as recorded.
|
||||
const fixtureRecorded = () => { try { lstatSync(join(instanceRoot ?? dataRoot, 'dev-fixture', 'fixture.json')); return true; } catch (error) { return error.code !== 'ENOENT'; } };
|
||||
const usesFixture = id => {
|
||||
try {
|
||||
const doc = settings.current(id), providers = doc.plugins?.['model-provider'];
|
||||
return (Array.isArray(providers) && providers.includes('fixture')) || Object.values(doc.launch?.models ?? {}).some(m => m?.module === 'fixture');
|
||||
} catch { return true; }
|
||||
};
|
||||
async function pickProxy() {
|
||||
if (proxy || !organization || !settings) return;
|
||||
if (org !== null && (!org || typeof org.id !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org.id) || typeof instanceDataRoot !== 'string' || typeof instanceConfigDir !== 'string'))
|
||||
return dropLateProxy('This bus has no valid organization binding for the shared proxy (invalid-host-request). Restart it from the host.');
|
||||
const configDir = org ? instanceConfigDir : organization.dir;
|
||||
let found;
|
||||
try { found = loadProxy(configDir, instanceRoot ?? dataRoot); }
|
||||
catch (error) { return dropLateProxy(`The model proxy settings do not load (${/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : 'proxy-config-invalid'}). This organization reads no accounts or catalogue until they do.`); }
|
||||
if (found === undefined) return dropLateProxy('The model proxy has not been set up for this instance yet. Set it up on the Model proxy page.');
|
||||
if (modelProxyClient && found.port !== proxyPort) dropLateProxy(null);
|
||||
if (fixtureRecorded() && Object.keys(businesses).some(usesFixture))
|
||||
return dropLateProxy('This organization routes launches to the dev fixture, which the host refuses next to a model proxy (fixture-proxy-configured). Remove the fixture or stop using it here.');
|
||||
let client;
|
||||
try { client = await loadManagementClient({ configDir, proxy: found, receipt: proxyReceipt }); }
|
||||
catch { return dropLateProxy('The model proxy management key does not load (proxy-management-key-invalid). This bus cannot reach the proxy until the key file is fixed.'); }
|
||||
let lock = null;
|
||||
if (org) {
|
||||
try { lock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${found.port}` }); }
|
||||
catch { return dropLateProxy('The shared proxy lock does not load (proxy-lock-invalid). Check the proxy state for this instance, then restart this bus.'); }
|
||||
}
|
||||
proxyCause = null;
|
||||
proxySettings = found;
|
||||
// Same port, key and lock still load: keep the client and its page, which holds login states.
|
||||
if (modelProxyClient) return;
|
||||
modelProxyClient = client; proxyPort = found.port; proxyLock = lock; lateProxy = true;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
}
|
||||
// proxy-scope slice 4: what triage-key.mjs needs from a riding org bus. A key error becomes a bus
|
||||
// refusal the table already holds: proxy codes it knows pass through, the rest name the key
|
||||
// store's code in the reason only. Neither carries a key, a path or upstream text.
|
||||
// The verb checks the proxy first, as proxy.fence.clear does; the sweep uses what this bus holds
|
||||
// and never picks a proxy up on its own timer.
|
||||
const triageOptions = async ({ sync = true } = {}) => {
|
||||
if (sync) await syncProxy();
|
||||
if (org === null || !organization || !proxyLock || !modelProxyClient || !proxySettings || typeof instanceConfigDir !== 'string' || typeof instanceDataRoot !== 'string')
|
||||
throw new BusError('triage-no-riding-bus');
|
||||
return { configDir: instanceConfigDir, system: { dataRoot: instanceDataRoot, proxy: proxySettings }, client: modelProxyClient, lock: proxyLock,
|
||||
...(triageKeys.now ? { now: triageKeys.now } : {}), ...(triageKeys.hook ? { hook: triageKeys.hook } : {}) };
|
||||
};
|
||||
const TRIAGE_PASS = new Set(['proxy-lock-busy', 'proxy-lock-invalid', 'proxy-lock-uncertain', 'proxy-client-invalid', 'proxy-client-refused', 'proxy-client-response', 'proxy-management-key-invalid']);
|
||||
const TRIAGE_MAP = { 'triage-key-no-proxy': 'triage-no-riding-bus', 'triage-key-raced': 'proxy-lock-busy', 'triage-key-unconfirmed': 'proxy-lock-uncertain',
|
||||
'proxy-uncertain': 'proxy-lock-uncertain', 'proxy-client-down': 'proxy-client-response', 'proxy-client-redirect': 'proxy-client-response', 'triage-key-proxy-open': 'proxy-not-configured' };
|
||||
const triageRefusal = error => {
|
||||
if (error instanceof BusError) return error;
|
||||
const code = typeof error?.code === 'string' ? error.code : '';
|
||||
if (TRIAGE_PASS.has(code)) return new BusError(code);
|
||||
if (Object.hasOwn(TRIAGE_MAP, code)) return new BusError(TRIAGE_MAP[code], code);
|
||||
return new BusError('storage-unavailable', /^triage-key-[a-z-]+$/.test(code) ? code : 'triage-key-failed');
|
||||
};
|
||||
// The sweep: a cheap read of the file first, so the proxy is asked only when something is due;
|
||||
// sweepKeys decides again under the lock. It never issues: no file, nothing.
|
||||
const triageMarginMs = Number.isSafeInteger(triageKeys.marginMs) && triageKeys.marginMs >= 0 ? triageKeys.marginMs : 30_000;
|
||||
// A refusal that repeats on every tick is logged when it first appears or changes, and then once
|
||||
// an hour while it stays the same; any other outcome clears it.
|
||||
let triageLastRefusal = null;
|
||||
const sweepRefused = (now, entry) => {
|
||||
const sig = `${entry.code}|${entry.reason ?? ''}`;
|
||||
if (triageLastRefusal?.sig === sig && now - triageLastRefusal.at < 3_600_000) return;
|
||||
triageLastRefusal = { sig, at: now };
|
||||
proxyReceipt({ action: 'triage.key.sweep-refused', ...entry });
|
||||
};
|
||||
const sweepTriage = async () => {
|
||||
let opts;
|
||||
try { opts = await triageOptions({ sync: false }); } catch { return { action: 'none' }; }
|
||||
const now = (opts.now ?? Date.now)();
|
||||
let record;
|
||||
try { record = readTriageKey(opts.configDir); } catch { sweepRefused(now, { code: 'triage-key-unreadable' }); return { action: 'none' }; }
|
||||
if (!sweepDue(record, now, triageMarginMs)) { triageLastRefusal = null; return { action: 'none' }; }
|
||||
try {
|
||||
const result = await sweepKeys(opts, { marginMs: triageMarginMs });
|
||||
triageLastRefusal = null;
|
||||
if (result.action !== 'none') proxyReceipt({ action: `triage.key.${result.action}`, keyId: result.keyId ?? null, addedKeyIds: result.addedKeyIds, removedKeyIds: result.removedKeyIds });
|
||||
return result;
|
||||
} catch (error) {
|
||||
const refusal = triageRefusal(error);
|
||||
sweepRefused(now, { code: refusal.code, ...(typeof refusal.reason === 'string' ? { reason: refusal.reason } : {}) });
|
||||
return { action: 'refused', code: refusal.code };
|
||||
}
|
||||
};
|
||||
const triageSweepOnce = () => (triageSweepRun ??= sweepTriage().finally(() => { triageSweepRun = null; }));
|
||||
// One check at a time; an unexpected failure drops a late client, never keeps it.
|
||||
const syncProxy = () => (proxySync = proxySync.then(pickProxy).catch(() => dropLateProxy('The model proxy could not be checked (proxy-down). Try again shortly.')));
|
||||
// Unavailable with no client: the catalogue names the cause instead of a bare unavailable.
|
||||
// pickup-launch: a late client serves accounts and the catalogue, but a launch on it needs what
|
||||
// only the start path builds (ModelKeys here, the proxy in the host launcher's boot system), so
|
||||
// while this bus holds one a ready catalogue says plainly that a restart is needed. Not on an
|
||||
// unavailable one: a fence, a collision or a down proxy still refuses after a restart.
|
||||
const RESTART_TO_LAUNCH = 'Restart this organization to start agents on these models.';
|
||||
const explainCatalogue = value => {
|
||||
if (value?.status === 'unavailable' && value.reason === undefined && !modelProxyClient && proxyCause)
|
||||
return { ...value, reason: 'proxy-not-configured', notices: [proxyCause] };
|
||||
if (lateProxy && value?.status === 'ready')
|
||||
return { ...value, notices: [...(value?.notices ?? []), RESTART_TO_LAUNCH] };
|
||||
return value;
|
||||
};
|
||||
const baseCatalogue = settings ? createModelCatalogue({ pluginDataRoot:instanceRoot ?? dataRoot, client: () => modelProxyClient, settings: id => settings.current(id).plugins ?? {},
|
||||
// A view, so a lock taken by a later pickup or init counts as it does for the start path's.
|
||||
proxyLock: { assertCertain: () => proxyLock?.assertCertain(), generation: () => proxyLock?.generation() } }) : null;
|
||||
modelCatalogues = modelCatalogue ?? (baseCatalogue ? { ...baseCatalogue, read: async (id, options) => { await syncProxy(); return explainCatalogue(await baseCatalogue.read(id, options)); } } : null);
|
||||
// Resolve legacy table entries with the enabled modules' declarations. A
|
||||
// missing or ambiguous mapping never becomes an available route.
|
||||
async function catalogueFor(id, options = {}) {
|
||||
@@ -845,6 +1314,7 @@ export async function startBroker({
|
||||
copies: bootCopies, organizations: () => Object.keys(businesses), ...(copyTransport ? { transport: copyTransport.send, ca: copyTransport.ca } : {}) })
|
||||
: null,
|
||||
copyLaunch,
|
||||
copyRevoker,
|
||||
usage,
|
||||
limits,
|
||||
store,
|
||||
@@ -900,10 +1370,33 @@ export async function startBroker({
|
||||
known.set(id, canonicalJson(next.projects));
|
||||
return getProjectRemote(writer, id, args.project);
|
||||
}
|
||||
if (verb === 'system-types.installed') return installedSystemTypes({dataRoot:instanceRoot ?? dataRoot});
|
||||
if (verb === 'system-types.current') return settings.current(id).plugins?.['system-type'] ?? [];
|
||||
// #1873 SP4: the human's pack, board and workflow ops. The broker passes only the business's human;
|
||||
// the reads return here, and each write reloads the organization and refreshes its limit checks.
|
||||
// SP5: or an organization.change holder's actor, with context.agent and context.beforeWrite.
|
||||
if (Object.hasOwn(PACK_OPS, verb)) {
|
||||
const ops = packOps({ writer: organizationWriter({dir:organization.dir,rolesDir:organization.rolesDir}), rolesDir: organization.rolesDir, dataRoot: instanceRoot ?? dataRoot,
|
||||
system: organization.system, registryFor: doc => organizationPlugins(instanceRoot ?? dataRoot, doc.plugins).registry, assertClean: value => credentials.assertClean(value), configDir: organization.dir });
|
||||
const out = ops[verb](id, args, actor, context);
|
||||
if (!PACK_OPS[verb]) return out;
|
||||
const committed = value => { reload(id); limits.refresh(id).catch(e=>limitLog({event:'limit-refresh-failed',business:id,message:e.message})); return value; };
|
||||
return out?.then ? out.then(committed) : committed(out);
|
||||
}
|
||||
// #1873 SP8: saving a template and listing the owner's own. The broker passes only the owner's
|
||||
// human; both read the organization file and neither writes it, so nothing reloads.
|
||||
if (verb === 'template.save' || verb === 'template.list') {
|
||||
const base = { rolesDir: organization.rolesDir, configDir: organization.dir, account: actor };
|
||||
if (verb === 'template.list') return templateList(base);
|
||||
const { name, project, preview, digest } = args;
|
||||
const document = organizationWriter({dir:organization.dir,rolesDir:organization.rolesDir}).read(id).document;
|
||||
const assertClean = value => credentials.assertClean(value);
|
||||
if (preview === true && digest === undefined) return templatePreview({ document, name, project, rolesDir: organization.rolesDir, assertClean });
|
||||
if (preview === undefined && typeof digest === 'string') return templateSave({ ...base, document, name, project, digest, assertClean });
|
||||
throw new TemplateError('invalid-request', 'template.save takes preview: true or the preview digest');
|
||||
}
|
||||
if(verb==='limits')return withTokens(id,settings.read(id));
|
||||
if(verb==='plugins')return {...plugins.read(id), available:{harness:levels.names ?? []}};
|
||||
// #1873 W3: harnessWatch is what the host's harness watch last wrote under the org's data root,
|
||||
// read only; a missing or bad file gives each harness "not checked yet" (readWatchView).
|
||||
if(verb==='plugins')return {...plugins.read(id), available:{harness:levels.names ?? []}, harnessWatch:readWatchView({dataRoot, names:levels.names ?? [], paths:[instanceRoot]})};
|
||||
// Row 256 (#1761): the actor is the reader, so endProtected is the delete check for that reader.
|
||||
if(verb==='instances')return instances.read(id,actor);
|
||||
// Row 65 (mos-0840): the Console's routing table.
|
||||
@@ -978,24 +1471,38 @@ export async function startBroker({
|
||||
// Row 199 (#1697, design section 6): issue, rotate, revoke and delete, live with no restart; each
|
||||
// rewrites or records through the organization writer, so changes.jsonl holds the receipt.
|
||||
proxyPage: async (verb, args, scope) => {
|
||||
if (!proxyPage) throw new BusError('not-configured');
|
||||
await syncProxy();
|
||||
if (!proxyPage) throw new BusError('not-configured', proxyCause ?? undefined);
|
||||
return proxyPage.request(verb, args, scope);
|
||||
},
|
||||
proxyInit: organization ? async args => {
|
||||
const settings = await initializeProxy({ configDir: organization.dir, dataRoot: instanceRoot ?? dataRoot, ...args });
|
||||
modelProxyClient = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt });
|
||||
proxyPort = settings.port;
|
||||
const settings = await initProxy({ configDir: organization.dir, dataRoot: instanceRoot ?? dataRoot, ...args });
|
||||
const client = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt });
|
||||
// #1873: an org bus that initializes the shared proxy also holds its write lock from now on.
|
||||
if (org && !proxyLock) proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` });
|
||||
configureProxyPage();
|
||||
// proxy-pickup: the lock comes first, so a lock that refuses leaves no client behind.
|
||||
const lock = org && !proxyLock ? createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` }) : proxyLock;
|
||||
// proxy-pickup: a client this bus did not boot with is checked again like a picked-up one.
|
||||
if (!proxy) { lateProxy = true; proxyCause = null; }
|
||||
modelProxyClient = client; proxyPort = settings.port; proxyLock = lock; proxySettings = settings;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
return settings;
|
||||
} : null,
|
||||
// #1873: clear the uncertain-write fence only on restart evidence this host re-proves: the
|
||||
// process on the proxy port is new, alive and started after the fence was set.
|
||||
proxyFenceClear: organization ? async ({ evidence }) => {
|
||||
if (!proxyLock || !modelProxyClient) throw new BusError('not-configured');
|
||||
await syncProxy();
|
||||
if (!proxyLock || !modelProxyClient) throw new BusError('not-configured', proxyCause ?? undefined);
|
||||
return proxyLock.clear({ restarted: true, read: () => modelProxyClient.getKeys(), verify: previous => verifyRestart(evidence, { fence: previous, port: proxyPort }) });
|
||||
} : null,
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): ensure or revoke the instance's Triage key through the
|
||||
// proxy this org bus rides, under the bus's own client and instance proxy lock. The answer is
|
||||
// ids only. A no-org bus, or an org bus with no proxy client and lock, refuses
|
||||
// triage-no-riding-bus.
|
||||
proxyTriageKey: async (op) => {
|
||||
const opts = await triageOptions();
|
||||
try { return op === 'ensure' ? await ensureKey(opts) : await revokeKeys(opts); }
|
||||
catch (error) { throw triageRefusal(error); }
|
||||
},
|
||||
credentialVerbs: organization
|
||||
? credentialVerbs({ credentials, writer: organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot:instanceRoot ?? dataRoot }),
|
||||
organizations: () => Object.keys(businesses), boot: copies?.definitions ? { org: copies.org, definitions: copies.definitions } : null })
|
||||
@@ -1303,6 +1810,12 @@ export async function startBroker({
|
||||
}, access.intervalMs ?? ACCESS_SWEEP_MS);
|
||||
accessTimer.unref();
|
||||
}
|
||||
// proxy-scope slice 4: only an org bus sweeps the Triage key; each tick checks it still rides.
|
||||
if (org !== null && organization) {
|
||||
triageTimer = setInterval(() => { triageSweepOnce(); },
|
||||
Number.isSafeInteger(triageKeys.intervalMs) && triageKeys.intervalMs > 0 ? triageKeys.intervalMs : 30_000);
|
||||
triageTimer.unref();
|
||||
}
|
||||
if (modelKeys) {
|
||||
modelTimer = setInterval(() => {
|
||||
if (modelSweep) return;
|
||||
@@ -1328,6 +1841,20 @@ export async function startBroker({
|
||||
requestLifecycle.recover({ limit: 64 }).catch(e => process.stderr.write(`request: ${JSON.stringify({ event: 'reconcile-refused', code: e.code ?? 'request-failed' })}\n`));
|
||||
}, 30000);
|
||||
requestTimer.unref();
|
||||
// #1873 CB4: a revoke a previous run left pending finishes (or reports) with its own op id before
|
||||
// anything else can use the copy, then each copy holding a grant is checked now and every minute.
|
||||
brokerUp(broker);
|
||||
if (copyRevoker) {
|
||||
const pass = () => {
|
||||
for (const id of Object.keys(businesses)) {
|
||||
copyRevoker.resume(id).catch(() => {});
|
||||
copyRevoker.sweep(id);
|
||||
}
|
||||
};
|
||||
pass();
|
||||
copyTimer = setInterval(pass, COPY_SWEEP_MS);
|
||||
copyTimer.unref();
|
||||
}
|
||||
return {
|
||||
broker,
|
||||
requestLifecycle,
|
||||
@@ -1416,6 +1943,8 @@ export async function startBroker({
|
||||
},
|
||||
threadsState: (business) => broker.threadsState(business),
|
||||
threadIdle: (r) => broker.threadIdle(r),
|
||||
// #1873 chat-fail F2: the plain line a refused thread start or a proxy-stall stop leaves.
|
||||
failureNotice: (r) => broker.failureNotice(r),
|
||||
// Row 125 (#1619, REQ-LIMIT-4): the launcher asks the limit checks before every launch.
|
||||
mayLaunch: async ({ business, instance, provider, model, account = null }) => {
|
||||
processingWork();
|
||||
@@ -1455,12 +1984,18 @@ export async function startBroker({
|
||||
definitionScan,
|
||||
launches: bound,
|
||||
readers: readCaps,
|
||||
// proxy-scope slice 4: one Triage key sweep now, as the timer runs it (tests).
|
||||
triageKeySweep: () => triageSweepOnce(),
|
||||
async close() {
|
||||
clearInterval(copyTimer);
|
||||
brokerUp(null);
|
||||
clearInterval(requestTimer);
|
||||
clearInterval(usageTimer);
|
||||
await proxyUsage.close();
|
||||
clearInterval(modelTimer);
|
||||
await modelSweep;
|
||||
clearInterval(triageTimer);
|
||||
await triageSweepRun;
|
||||
clearInterval(accessTimer);
|
||||
await stopSync();
|
||||
await server.close();
|
||||
@@ -1472,10 +2007,13 @@ export async function startBroker({
|
||||
},
|
||||
};
|
||||
} catch (e) {
|
||||
clearInterval(copyTimer);
|
||||
brokerUp(null);
|
||||
clearInterval(requestTimer);
|
||||
clearInterval(usageTimer);
|
||||
await proxyUsage?.close();
|
||||
clearInterval(modelTimer);
|
||||
clearInterval(triageTimer);
|
||||
clearInterval(accessTimer);
|
||||
await server?.close();
|
||||
await adapter?.close?.();
|
||||
|
||||
@@ -155,6 +155,8 @@ export async function serve({ broker, path, authenticateHuman = null, authentica
|
||||
// Row 236 (#1742): the grants verbs may call Gitea several times, 15 s each at most.
|
||||
GRANTS_VERBS.includes(r.verb) ||
|
||||
r.verb === 'proxy.init' ||
|
||||
// proxy-scope slice 4: the Triage key verb waits on the proxy lock and the proxy.
|
||||
r.verb === 'proxy.triage.key' ||
|
||||
(r.verb === 'decision.raise' && (Object.hasOwn(r.args ?? {}, 'spec_path') || Object.hasOwn(r.args ?? {}, 'spec_commit') ||
|
||||
Object.hasOwn(r.args ?? {}, 'grant')))) {
|
||||
const own = transient;
|
||||
|
||||
@@ -13,8 +13,8 @@ import { join, isAbsolute } from 'node:path';
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
const ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8');
|
||||
export const SCHEMA_SHA256 = 'e5868996851ee60a3c6d2798a17b9da446ba666c859d84d1b575ca5104ae61f8';
|
||||
export const SCHEMA_VERSION = '3v';
|
||||
export const SCHEMA_SHA256 = '1c305d842619360c14c8388cd43cb9de81afbe46ff0662fd5de9851be9ff3ea0';
|
||||
export const SCHEMA_VERSION = '3w';
|
||||
// Ordered, named offline steps. New schema changes append a step and keep prior DDL pinned.
|
||||
const legacyDDL = readFileSync(new URL('./migrations/3b.sql', import.meta.url), 'utf8');
|
||||
const LEGACY_SHA256 = '179ffe356d4ff19a49b5ebad39b6c6bfd7771deb8e1b7c55b5e746f039d69e65';
|
||||
@@ -120,6 +120,11 @@ const migrations = [{
|
||||
// Only a new append-only table, its index and its guards; no existing row changes.
|
||||
from: '3u', to: '3v',
|
||||
sql: readFileSync(new URL('./migrations/3u-3v.sql', import.meta.url), 'utf8'),
|
||||
}, {
|
||||
// SP9 (#1873, Jason's ruling 2026-11-12): drops the table 3t-3u created, its index and its four
|
||||
// guards, with its rows.
|
||||
from: '3v', to: '3w',
|
||||
sql: readFileSync(new URL('./migrations/3v-3w.sql', import.meta.url), 'utf8'),
|
||||
}];
|
||||
const timedTables = [
|
||||
'events',
|
||||
@@ -146,7 +151,6 @@ const timedTables = [
|
||||
'request_snapshot_events',
|
||||
'request_project_events',
|
||||
'project_archives',
|
||||
'project_system_types',
|
||||
'store_tasks',
|
||||
];
|
||||
const hex = (value) => createHash('sha256').update(value).digest('hex');
|
||||
|
||||
@@ -15,7 +15,7 @@ if (!process.env.MOSAIC_BUS_TESTER_NONCE) {
|
||||
// P19 #876: only the re-executed child, whose initial environment holds the nonce, talks to the
|
||||
// bus. Loading the client (and the broker module graph behind it) here instead of at the top
|
||||
// keeps the re-exec parent to a bare node start.
|
||||
const { Client } = await import('./client.mjs');
|
||||
const { Client, NOT_SENT_EXIT } = await import('./client.mjs');
|
||||
const { readProcess } = await import('./human.mjs');
|
||||
try {
|
||||
const request = JSON.parse(readFileSync(0, 'utf8'));
|
||||
@@ -39,5 +39,6 @@ try {
|
||||
process.stdout.write(JSON.stringify(result) + '\n');
|
||||
} catch (e) {
|
||||
process.stderr.write((/^[a-z-]{1,64}$/.test(e.code ?? '') ? e.code : 'invalid-request') + '\n');
|
||||
process.exitCode = 2;
|
||||
// E2E6 fix 6: as human-cli.mjs, a request that never left exits NOT_SENT_EXIT.
|
||||
process.exitCode = e?.notSent === true ? NOT_SENT_EXIT : 2;
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
// #1873 triage-wiring: the Triage key file's reader, moved out of triage-key.mjs so the Console can
|
||||
// read the key at each Triage spawn without loading the proxy management client or the proxy lock.
|
||||
// It imports node built-ins only; packages/webui/tests/triage-wiring.test.mjs fails if the Console's
|
||||
// Triage modules load model-proxy/src/client.mjs. triage-key.mjs (the owner's key verbs) imports
|
||||
// these and re-exports what it exported before, so its callers are unchanged.
|
||||
//
|
||||
// <configDir>/secrets/triage/model (directory 0700, file 0600, the running user's)
|
||||
// { version, key, keyId, issuedAt, rotateBy, retiring: [{ key, keyId, issuedAt, retireAt }] }
|
||||
//
|
||||
// Messages name files, never values.
|
||||
import { closeSync, constants, fchmodSync, fstatSync, lstatSync, mkdirSync, openSync, readFileSync } from "node:fs";
|
||||
import { createHash } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
|
||||
export const KEY_VERSION = 1;
|
||||
const FIELDS = Object.freeze(["version", "key", "keyId", "issuedAt", "rotateBy", "retiring"]);
|
||||
const RETIRING_FIELDS = Object.freeze(["key", "keyId", "issuedAt", "retireAt"]);
|
||||
const MAX_BYTES = 64 * 1024;
|
||||
export const MAX_RETIRING = 16;
|
||||
export const SECRET = /^[A-Za-z0-9_-]{43}$/;
|
||||
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
|
||||
const DAY = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
export class TriageKeyError extends Error {
|
||||
constructor(code, message, exitCode = 3) {
|
||||
super(message);
|
||||
this.name = "TriageKeyError";
|
||||
this.code = code;
|
||||
this.exitCode = exitCode;
|
||||
}
|
||||
}
|
||||
export const unreadable = (why) => new TriageKeyError("triage-key-unreadable", `the Triage key store is unreadable: ${why}`);
|
||||
|
||||
export const keyIdOf = (key) => createHash("sha256").update(key).digest("hex").slice(0, 12);
|
||||
export const instanceTag = (configDir) => createHash("sha256").update(configDir).digest("hex").slice(0, 12);
|
||||
export const keyPrefix = (configDir) => `mst-${instanceTag(configDir)}-triage-`;
|
||||
const ownKey = (configDir) => (k) => typeof k === "string" && k.startsWith(keyPrefix(configDir)) && SECRET.test(k.slice(keyPrefix(configDir).length));
|
||||
|
||||
export function keyPaths(configDir) {
|
||||
const secrets = join(configDir, "secrets");
|
||||
const dir = join(secrets, "triage");
|
||||
return { secrets, dir, file: join(dir, "model") };
|
||||
}
|
||||
|
||||
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
|
||||
const exactKeys = (v, keys) => plain(v) && Object.keys(v).length === keys.length && keys.every((k) => Object.hasOwn(v, k));
|
||||
const canonicalTime = (s) => typeof s === "string" && ISO.test(s) && !Number.isNaN(Date.parse(s)) && new Date(s).toISOString() === s;
|
||||
const canonicalDay = (s) => typeof s === "string" && DAY.test(s) && !Number.isNaN(Date.parse(`${s}T00:00:00Z`)) && new Date(`${s}T00:00:00Z`).toISOString().slice(0, 10) === s;
|
||||
|
||||
export function validRecord(doc, configDir) {
|
||||
const own = ownKey(configDir);
|
||||
return exactKeys(doc, FIELDS)
|
||||
&& doc.version === KEY_VERSION
|
||||
&& own(doc.key) && doc.keyId === keyIdOf(doc.key)
|
||||
&& canonicalTime(doc.issuedAt) && canonicalDay(doc.rotateBy)
|
||||
&& Array.isArray(doc.retiring) && doc.retiring.length <= MAX_RETIRING
|
||||
&& doc.retiring.every((r) => exactKeys(r, RETIRING_FIELDS) && own(r.key) && r.key !== doc.key && r.keyId === keyIdOf(r.key)
|
||||
&& canonicalTime(r.issuedAt) && canonicalTime(r.retireAt))
|
||||
&& new Set(doc.retiring.map((r) => r.key)).size === doc.retiring.length;
|
||||
}
|
||||
|
||||
const uidOf = (opts) => opts.uid ?? process.getuid();
|
||||
|
||||
// A real directory, the running user's, mode 0700. Missing: false, or made when `create` is on.
|
||||
export function checkDir(dir, uid, what, { create }) {
|
||||
let st;
|
||||
try { st = lstatSync(dir); }
|
||||
catch (e) {
|
||||
if (e.code !== "ENOENT") throw unreadable(`${what} cannot be read`);
|
||||
if (!create) return false;
|
||||
try { mkdirSync(dir, { mode: 0o700 }); } catch { throw unreadable(`${what} cannot be made`); }
|
||||
try {
|
||||
const fd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
||||
try { fchmodSync(fd, 0o700); } finally { closeSync(fd); }
|
||||
} catch { throw unreadable(`${what} cannot be made`); }
|
||||
st = lstatSync(dir);
|
||||
}
|
||||
if (st.isSymbolicLink()) throw unreadable(`${what} is a link`);
|
||||
if (!st.isDirectory()) throw unreadable(`${what} is not a directory`);
|
||||
if (st.uid !== uid) throw unreadable(`${what} is not owned by this user`);
|
||||
if ((st.mode & 0o777) !== 0o700) throw unreadable(`${what} mode is not 0700`);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function checkFile(fd, uid, what, fail = unreadable) {
|
||||
const st = fstatSync(fd);
|
||||
if (!st.isFile()) throw fail(`${what} is not a regular file`);
|
||||
if (st.nlink !== 1) throw fail(`${what} has more than one link`);
|
||||
if (st.uid !== uid) throw fail(`${what} is not owned by this user`);
|
||||
if ((st.mode & 0o777) !== 0o600) throw fail(`${what} mode is not 0600`);
|
||||
return st;
|
||||
}
|
||||
|
||||
// The stored record, or null when no key is issued. Throws triage-key-unreadable on anything unsafe.
|
||||
// opts.uid (both directories and the file) and opts.fileUid (the file only) are test seams for the
|
||||
// owner checks.
|
||||
export function readKey(configDir, opts = {}) {
|
||||
const { secrets, dir, file } = keyPaths(configDir);
|
||||
const uid = uidOf(opts);
|
||||
if (!checkDir(secrets, uid, "the secrets directory", { create: false })) return null;
|
||||
if (!checkDir(dir, uid, "the triage secrets directory", { create: false })) return null;
|
||||
let fd;
|
||||
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
|
||||
catch (e) {
|
||||
if (e.code === "ENOENT") return null;
|
||||
if (e.code === "ELOOP") throw unreadable("the key file is a link");
|
||||
throw unreadable("the key file cannot be opened");
|
||||
}
|
||||
try {
|
||||
const st = checkFile(fd, opts.fileUid ?? uid, "the key file");
|
||||
if (st.size > MAX_BYTES) throw unreadable("the key file is too large");
|
||||
let doc;
|
||||
try { doc = JSON.parse(readFileSync(fd, "utf8")); } catch { throw unreadable("the key file is not JSON"); }
|
||||
if (!validRecord(doc, configDir)) throw unreadable("the key file does not match the key shape");
|
||||
return doc;
|
||||
} finally { closeSync(fd); }
|
||||
}
|
||||
@@ -0,0 +1,397 @@
|
||||
// #1873 triage-key: Triage's own model proxy client key, issued from the owner's terminal
|
||||
// (design report reports/triage-route-design.md slice 3, Jason's ruling 2026-11-18), and since
|
||||
// proxy-scope slice 4 (Jason's ruling 2026-11-19) by the bus too: the verb proxy.triage.key
|
||||
// (ensure, revoke) and a riding org bus's sweep call this module with the bus's own management
|
||||
// client and instance proxy lock. It moved here from packages/cli/src so the bus can import it; the
|
||||
// terminal verbs (packages/cli/src/triage.mjs) call it unchanged.
|
||||
//
|
||||
// <configDir>/secrets/triage/model (directory 0700, file 0600, the running user's)
|
||||
// { version, key, keyId, issuedAt, rotateBy, retiring: [{ key, keyId, issuedAt, retireAt }] }
|
||||
//
|
||||
// The key is `mst-<instance tag>-triage-<43 base64url>`, where the tag is the first 12 hex of
|
||||
// sha256(configDir), the tag the instance's own `msk-` sentinel carries. An org bus reconciles only
|
||||
// its own `mso-<tag>-<org>-key-` names and a no-org bus only `msk-<tag>-`, so neither touches it.
|
||||
//
|
||||
// Every proxy write runs under the instance proxy lock (packages/bus/src/proxy-lock.mjs), the lock
|
||||
// org buses take: read the list, check this instance's sentinel, fence, PUT, read back, confirm.
|
||||
// The proxy changes first and the file second, as the bus does for an agent key. A crash between
|
||||
// leaves a key in the Triage namespace that the file does not hold; the next issue, rotate or retire
|
||||
// removes it, and status names it. A crash after the PUT and before the read-back leaves the lock's
|
||||
// uncertain fence, which every proxy writer on the instance refuses until a human clears it
|
||||
// (`mosaic proxy fence clear --proxy-restarted`).
|
||||
//
|
||||
// rotate records when the old key's grace ends; the key stays in the proxy until a later retire,
|
||||
// rotate, issue or sweep removes it, and status says when that is due. A riding org bus runs
|
||||
// sweepKeys on a timer: under the lock it reads the file again and rotates when rotateBy is due, or
|
||||
// retires keys whose grace (plus a margin, so the Console stops a session on the key first) has
|
||||
// passed. The sweep only replaces a key that exists; it never issues the first one.
|
||||
//
|
||||
// Nothing here prints, logs or records a key. Receipts and output carry key ids only (the first 12
|
||||
// hex of sha256(key), the id the bus uses). Messages name files and options, never values.
|
||||
// The Console reads the key at each Triage spawn through triage-key-file.mjs, never this module.
|
||||
import { closeSync, constants, fchmodSync, fsyncSync, lstatSync, openSync, readdirSync, renameSync, unlinkSync, writeSync } from "node:fs";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { createProxyLock } from "./proxy-lock.mjs";
|
||||
import { loadManagementClient } from "../../model-proxy/src/client.mjs";
|
||||
|
||||
// #1873 triage-wiring: the key file's reader lives in triage-key-file.mjs (node built-ins only), so
|
||||
// the Console reads the key without this module's proxy client. Re-exported here unchanged.
|
||||
import { KEY_VERSION, MAX_RETIRING, SECRET, TriageKeyError, checkDir, checkFile, instanceTag, keyIdOf, keyPaths, keyPrefix, readKey, unreadable, validRecord } from "./triage-key-file.mjs";
|
||||
export { KEY_VERSION, TriageKeyError, instanceTag, keyIdOf, keyPaths, keyPrefix, readKey, validRecord };
|
||||
|
||||
// The org key lifetime and the rotate grace default. The bus holds both as literals
|
||||
// (packages/bus/src/model-keys.mjs #issue: 30 days to rotateBy; packages/bus/src/runtime.mjs:
|
||||
// `proxy.rotateGraceMinutes ?? 120`) and exports neither, and this lane may not change the bus.
|
||||
// packages/cli/tests/triage-key.test.mjs drives the bus's own ModelKeys with the same clock and
|
||||
// proves these give the same rotateBy and retireAt, so a change on either side goes red there.
|
||||
// The grace itself is read from proxy.json first, where the bus reads it.
|
||||
export const KEY_LIFETIME_DAYS = 30;
|
||||
export const DEFAULT_ROTATE_GRACE_MINUTES = 120;
|
||||
const sentinelPrefix = (configDir) => `msk-${instanceTag(configDir)}-sentinel-`;
|
||||
// Any name under the prefix is Triage's to remove, well formed or not, as the bus treats its own
|
||||
// `msk-<tag>-` names. Only a well-formed key may be stored in the file.
|
||||
const inNamespace = (configDir) => (k) => typeof k === "string" && k.startsWith(keyPrefix(configDir));
|
||||
|
||||
// Both directories, checked (and the triage one made) before any proxy write, so a store that cannot
|
||||
// be written refuses while the proxy is still untouched.
|
||||
function prepareDirs(configDir, uid) {
|
||||
const { secrets, dir } = keyPaths(configDir);
|
||||
if (!checkDir(secrets, uid, "the secrets directory", { create: false })) throw unreadable("the secrets directory is missing");
|
||||
checkDir(dir, uid, "the triage secrets directory", { create: true });
|
||||
}
|
||||
|
||||
// A temp file writeKey left when it was killed before its rename. It may hold a key.
|
||||
const TEMP = /^\.model\.\d+\.[0-9a-f]{12}\.tmp$/;
|
||||
const tempsIn = (dir) => { try { return readdirSync(dir).filter((n) => TEMP.test(n)); } catch { return []; } };
|
||||
// Under the lock no other Triage writer runs, so every temp file left in the directory is dead.
|
||||
function removeTemps(configDir) {
|
||||
const { dir } = keyPaths(configDir);
|
||||
for (const name of tempsIn(dir)) {
|
||||
try { if (!lstatSync(join(dir, name)).isDirectory()) unlinkSync(join(dir, name)); }
|
||||
catch { throw unreadable("a leftover temp file in the triage secrets directory cannot be removed"); }
|
||||
}
|
||||
}
|
||||
|
||||
// A directory sync that failed after the rename: the file is in place but may not be durable.
|
||||
class NotSynced extends Error {}
|
||||
|
||||
// Atomic: a 0600 temp file in the same directory, fsync, rename, fsync of the directory.
|
||||
function writeKey(configDir, record, uid, hook = () => {}) {
|
||||
const { dir, file } = keyPaths(configDir);
|
||||
prepareDirs(configDir, uid);
|
||||
const temp = join(dir, `.model.${process.pid}.${randomBytes(6).toString("hex")}.tmp`);
|
||||
let fd;
|
||||
try { fd = openSync(temp, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); }
|
||||
catch { throw unreadable("the key file cannot be written"); }
|
||||
try {
|
||||
try { fchmodSync(fd, 0o600); writeSync(fd, `${JSON.stringify(record, null, 2)}\n`); fsyncSync(fd); }
|
||||
finally { closeSync(fd); }
|
||||
hook("rename");
|
||||
renameSync(temp, file);
|
||||
} catch { try { unlinkSync(temp); } catch {} throw unreadable("the key file cannot be written"); }
|
||||
try {
|
||||
hook("sync");
|
||||
const dfd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
try { fsyncSync(dfd); } finally { closeSync(dfd); }
|
||||
} catch { throw new NotSynced(); }
|
||||
}
|
||||
|
||||
// One line per write in the proxy lock's receipts file, ids only. `receiptsReady` runs under the
|
||||
// lock before any change, so an unsafe receipts file refuses while nothing has changed yet.
|
||||
const RECEIPTS_FLAGS = constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
||||
const receiptsUnsafe = (why) => new TriageKeyError("triage-key-receipts-unsafe", `${why}; nothing was changed`);
|
||||
function receiptsReady(stateDir, uid) {
|
||||
let fd;
|
||||
try { fd = openSync(join(stateDir, "mosaic-keys.receipts.jsonl"), RECEIPTS_FLAGS, 0o600); }
|
||||
catch { throw receiptsUnsafe("the proxy receipts file cannot be opened"); }
|
||||
try { checkFile(fd, uid, "the proxy receipts file", receiptsUnsafe); } finally { closeSync(fd); }
|
||||
}
|
||||
function receipt(stateDir, uid, entry) {
|
||||
const unrecorded = new TriageKeyError("triage-key-unrecorded", "the change was made but its receipt line was not written; run mosaic triage key status");
|
||||
let fd;
|
||||
try { fd = openSync(join(stateDir, "mosaic-keys.receipts.jsonl"), RECEIPTS_FLAGS, 0o600); } catch { throw unrecorded; }
|
||||
try {
|
||||
checkFile(fd, uid, "the proxy receipts file", () => unrecorded);
|
||||
writeSync(fd, `${JSON.stringify(entry)}\n`);
|
||||
fsyncSync(fd);
|
||||
} catch { throw unrecorded; }
|
||||
finally { closeSync(fd); }
|
||||
}
|
||||
|
||||
// The grace the bus uses: proxy.json's rotateGraceMinutes, or the bus default.
|
||||
export const graceMinutes = (proxy) => proxy?.rotateGraceMinutes ?? DEFAULT_ROTATE_GRACE_MINUTES;
|
||||
export const rotateByFor = (nowMs) => new Date(nowMs + KEY_LIFETIME_DAYS * 86400_000).toISOString().slice(0, 10);
|
||||
export const retireAtFor = (nowMs, proxy) => new Date(nowMs + graceMinutes(proxy) * 60_000).toISOString();
|
||||
|
||||
const proxyFailure = (code) => {
|
||||
const words = {
|
||||
"proxy-lock-busy": "another proxy writer holds the instance proxy lock; try again when it ends, and if no writer is running, see mosaic proxy fence status",
|
||||
"proxy-lock-uncertain": "the instance proxy lock is in a state this verb will not guess about; check <dataRoot>/proxy by hand",
|
||||
"proxy-lock-invalid": "the instance proxy lock directory is missing or unsafe, or names another proxy",
|
||||
"proxy-uncertain": "an earlier proxy write has an uncertain outcome; see mosaic proxy fence status",
|
||||
"proxy-management-key-invalid": "the proxy management key file is missing or unsafe",
|
||||
"proxy-client-invalid": "the proxy settings are not usable",
|
||||
"proxy-client-down": "the proxy did not answer",
|
||||
"proxy-client-refused": "the proxy refused the management request",
|
||||
"proxy-client-redirect": "the proxy answered with a redirect",
|
||||
"proxy-client-response": "the proxy gave an answer this verb cannot use",
|
||||
};
|
||||
return new TriageKeyError(code, words[code] ?? "the proxy write failed");
|
||||
};
|
||||
|
||||
// opts.uid, opts.now and opts.hook(stage) are test seams; a stage hook may throw or kill the process.
|
||||
// opts.client and opts.lock: the bus passes its own management client and instance proxy lock, so a
|
||||
// bus change runs under the lock object its model key sweep uses and reads no key file of its own.
|
||||
function context({ configDir, system, uid, fileUid, now, hook, client, lock }) {
|
||||
if (!system?.proxy || !Number.isInteger(system.proxy.port)) throw new TriageKeyError("triage-key-no-proxy", "the instance has no model proxy set up (proxy.json)");
|
||||
return {
|
||||
configDir, dataRoot: system.dataRoot, proxy: system.proxy, stateDir: join(system.dataRoot, "proxy"),
|
||||
uid: uid ?? process.getuid(), fileUid, now: now ?? Date.now, hook: hook ?? (() => {}), client, lock,
|
||||
};
|
||||
}
|
||||
|
||||
function lockFor(c) {
|
||||
if (c.lock) return c.lock;
|
||||
try { return createProxyLock({ stateDir: c.stateDir, identity: `${c.configDir}:${c.proxy.port}` }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-lock-invalid"); }
|
||||
}
|
||||
|
||||
async function clientFor(c) {
|
||||
if (c.client) return c.client;
|
||||
try { return await loadManagementClient({ configDir: c.configDir, proxy: c.proxy }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-management-key-invalid"); }
|
||||
}
|
||||
|
||||
function checkSentinel(c, keys) {
|
||||
const prefix = sentinelPrefix(c.configDir);
|
||||
if (!keys.some((k) => k.startsWith(prefix) && SECRET.test(k.slice(prefix.length)))) {
|
||||
throw new TriageKeyError("triage-key-proxy-open", "the proxy's key list has no sentinel for this instance, so it may be another proxy");
|
||||
}
|
||||
}
|
||||
|
||||
// Under the instance proxy lock: read the list, check the sentinel, run `plan`, write the result.
|
||||
// `plan(keys, record)` returns { after, record, remove, op, done }; `record` null leaves the file
|
||||
// alone, `remove` true unlinks it (revoke), `op` names the change when the plan picks it (sweep).
|
||||
// The proxy changes first, then the file, then one receipt line.
|
||||
async function change(c, opName, plan) {
|
||||
const lock = lockFor(c);
|
||||
const client = await clientFor(c);
|
||||
try {
|
||||
return await lock.run(async () => {
|
||||
const before = await client.getKeys();
|
||||
checkSentinel(c, before);
|
||||
const current = readKey(c.configDir, { uid: c.uid, fileUid: c.fileUid });
|
||||
const result = plan(before, current);
|
||||
const op = result.op ?? opName;
|
||||
const remove = result.remove === true && current !== null;
|
||||
if (result.record) prepareDirs(c.configDir, c.uid);
|
||||
removeTemps(c.configDir);
|
||||
receiptsReady(c.stateDir, c.uid);
|
||||
const after = result.after;
|
||||
const removed = before.filter((k) => !after.includes(k));
|
||||
const added = after.filter((k) => !before.includes(k));
|
||||
// A key in another namespace never leaves the list.
|
||||
if (removed.some((k) => !inNamespace(c.configDir)(k))) throw new TriageKeyError("triage-key-failed", "the change would remove a key outside the Triage namespace");
|
||||
if (added.length || removed.length) {
|
||||
// As the bus does: a list that moved since the first read is not overwritten.
|
||||
if (!isDeepStrictEqual(await client.getKeys(), before)) throw new TriageKeyError("triage-key-raced", "the proxy's key list changed while this verb ran; run it again");
|
||||
lock.fence({ org: null, op: `triage.${op}` });
|
||||
await client.putKeys(after);
|
||||
c.hook("put");
|
||||
const confirmed = await client.getKeys();
|
||||
if (!confirmed.some((k) => k.startsWith(sentinelPrefix(c.configDir))) || added.some((k) => !confirmed.includes(k)) || removed.some((k) => confirmed.includes(k))) {
|
||||
throw new TriageKeyError("triage-key-unconfirmed", "the proxy did not confirm the change; the instance proxy fence stays set, see mosaic proxy fence status");
|
||||
}
|
||||
lock.confirm();
|
||||
}
|
||||
c.hook("write");
|
||||
let unsynced = false;
|
||||
if (result.record || remove) {
|
||||
try { if (remove) unlinkKey(c.configDir, c.hook); else writeKey(c.configDir, result.record, c.uid, c.hook); }
|
||||
catch (e) {
|
||||
if (e instanceof NotSynced) unsynced = true;
|
||||
else throw new TriageKeyError("triage-key-unwritten", `the proxy list changed but ${keyPaths(c.configDir).file} was not written (${e.message}); fix the file's directory, then run mosaic triage key ${op} again, which clears what this run left`);
|
||||
}
|
||||
}
|
||||
c.hook("receipt");
|
||||
// A run that changed neither the proxy nor the file leaves no receipt.
|
||||
if (added.length || removed.length || result.record || remove) receipt(c.stateDir, c.uid, { op: `triage.key.${op}`, time: new Date(c.now()).toISOString(), addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) });
|
||||
// The file is in place and the receipt written; only its durability is in doubt.
|
||||
if (unsynced) throw new TriageKeyError("triage-key-unsynced", `${keyPaths(c.configDir).file} was written but its directory could not be synced, so it may not survive a power loss; run mosaic triage key status`);
|
||||
return { ...result.done, addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) };
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof TriageKeyError) throw e;
|
||||
throw proxyFailure(typeof e?.code === "string" && /^proxy-[a-z-]+$/.test(e.code) ? e.code : "triage-key-failed");
|
||||
}
|
||||
}
|
||||
|
||||
// Revoke's file step: unlink, then sync the directory, with writeKey's stages and failures.
|
||||
function unlinkKey(configDir, hook = () => {}) {
|
||||
const { dir, file } = keyPaths(configDir);
|
||||
try { hook("rename"); unlinkSync(file); }
|
||||
catch (e) { if (e?.code !== "ENOENT") throw unreadable("the key file cannot be removed"); }
|
||||
try {
|
||||
hook("sync");
|
||||
const dfd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
try { fsyncSync(dfd); } finally { closeSync(dfd); }
|
||||
} catch { throw new NotSynced(); }
|
||||
}
|
||||
|
||||
const newKey = (configDir) => keyPrefix(configDir) + randomBytes(32).toString("base64url");
|
||||
|
||||
// Keys in the Triage namespace that the file does not hold: left by a crash between the PUT and the
|
||||
// file write, or by a file that was removed by hand.
|
||||
const orphansOf = (configDir, keys, record) => {
|
||||
const held = record ? [record.key, ...record.retiring.map((r) => r.key)] : [];
|
||||
return keys.filter((k) => inNamespace(configDir)(k) && !held.includes(k));
|
||||
};
|
||||
|
||||
export async function issueKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "issue", (before, current) => {
|
||||
if (current) throw new TriageKeyError("triage-key-issued", "a Triage key is already issued; use mosaic triage key rotate");
|
||||
const orphans = orphansOf(c.configDir, before, null);
|
||||
const key = newKey(c.configDir), nowMs = c.now();
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring: [] };
|
||||
return { after: [...before.filter((k) => !orphans.includes(k)), key], record, done: { keyId: record.keyId, rotateBy: record.rotateBy } };
|
||||
});
|
||||
}
|
||||
|
||||
// Retiring keys whose grace (plus marginMs, 0 for the terminal verbs) has passed, or that the proxy
|
||||
// no longer holds.
|
||||
const dueOf = (record, keys, nowMs, marginMs = 0) => record.retiring.filter((r) => Date.parse(r.retireAt) + marginMs <= nowMs || !keys.includes(r.key));
|
||||
const rotateDueAt = (record) => Date.parse(`${record.rotateBy}T00:00:00Z`);
|
||||
|
||||
// The rotate plan, shared by the terminal verb and the sweep.
|
||||
function rotatePlan(c, before, current, nowMs, marginMs = 0) {
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = dueOf(current, before, nowMs, marginMs);
|
||||
const keep = before.includes(current.key);
|
||||
const retiring = [...current.retiring.filter((r) => !due.includes(r)),
|
||||
...(keep ? [{ key: current.key, keyId: current.keyId, issuedAt: current.issuedAt, retireAt: retireAtFor(nowMs, c.proxy) }] : [])];
|
||||
if (retiring.length > MAX_RETIRING) throw new TriageKeyError("triage-key-failed", "too many keys are retiring; run mosaic triage key retire once their grace has passed");
|
||||
const key = newKey(c.configDir);
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return {
|
||||
after: [...before.filter((k) => !drop.includes(k)), key], record,
|
||||
done: { keyId: record.keyId, rotateBy: record.rotateBy, old: keep ? { keyId: current.keyId, retireAt: retiring.at(-1).retireAt } : { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
}
|
||||
|
||||
export async function rotateKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "rotate", (before, current) => {
|
||||
if (!current) throw new TriageKeyError("triage-key-not-issued", "no Triage key is issued; use mosaic triage key issue");
|
||||
return rotatePlan(c, before, current, c.now());
|
||||
});
|
||||
}
|
||||
function retirePlan(c, before, current, nowMs, marginMs = 0) {
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = current ? dueOf(current, before, nowMs, marginMs) : [];
|
||||
const record = current && due.length ? { ...current, retiring: current.retiring.filter((r) => !due.includes(r)) } : null;
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return { after: before.filter((k) => !drop.includes(k)), record, done: { droppedKeyIds: due.map((r) => r.keyId) } };
|
||||
}
|
||||
|
||||
export async function retireKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "retire", (before, current) => retirePlan(c, before, current, c.now()));
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: the bus verb proxy.triage.key op ensure. Leaves a held key alone, issues one
|
||||
// when none exists, replaces one the proxy lost. Never prints the key; the answer is ids only.
|
||||
// issued no file: a new key (keys in the namespace the file does not hold are removed)
|
||||
// held the proxy holds the file's key: only orphans and retiring keys the proxy lost go
|
||||
// replaced the proxy does not hold the file's key: a new key, the old one marked lost
|
||||
export async function ensureKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "ensure", (before, current) => {
|
||||
const nowMs = c.now();
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const fresh = (retiring) => {
|
||||
const key = newKey(c.configDir);
|
||||
return { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
};
|
||||
if (!current) {
|
||||
const record = fresh([]);
|
||||
return { after: [...before.filter((k) => !orphans.includes(k)), record.key], record, done: { action: "issued", keyId: record.keyId, rotateBy: record.rotateBy } };
|
||||
}
|
||||
const lost = current.retiring.filter((r) => !before.includes(r.key));
|
||||
const retiring = current.retiring.filter((r) => !lost.includes(r));
|
||||
if (before.includes(current.key)) {
|
||||
return {
|
||||
after: before.filter((k) => !orphans.includes(k)), record: lost.length ? { ...current, retiring } : null,
|
||||
done: { action: "held", keyId: current.keyId, rotateBy: current.rotateBy },
|
||||
};
|
||||
}
|
||||
const record = fresh(retiring);
|
||||
return {
|
||||
after: [...before.filter((k) => !orphans.includes(k)), record.key], record,
|
||||
done: { action: "replaced", keyId: record.keyId, rotateBy: record.rotateBy, old: { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: op revoke, when the owner turns Triage off. Every name in this instance's
|
||||
// Triage namespace leaves the proxy list, then the key file goes. No file and no names: nothing.
|
||||
export async function revokeKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "revoke", (before, current) => {
|
||||
const mine = before.filter(inNamespace(c.configDir));
|
||||
return { after: before.filter((k) => !mine.includes(k)), record: null, remove: true, done: { action: mine.length || current ? "revoked" : "none" } };
|
||||
});
|
||||
}
|
||||
|
||||
// What a sweep would do from the file alone: "rotate" when rotateBy has come, "retire" when a
|
||||
// retiring key's grace plus marginMs has passed, else null. A cheap check without the lock or the
|
||||
// proxy; sweepKeys decides again under the lock.
|
||||
export function sweepDue(record, nowMs, marginMs = 0) {
|
||||
if (!record) return null;
|
||||
if (rotateDueAt(record) <= nowMs) return "rotate";
|
||||
return record.retiring.some((r) => Date.parse(r.retireAt) + marginMs <= nowMs) ? "retire" : null;
|
||||
}
|
||||
|
||||
// The bus sweep. Under the instance proxy lock it reads the file again and acts only if the action
|
||||
// is still due, so two buses that wake together change the proxy once. No file: nothing, ever.
|
||||
export async function sweepKeys(opts, { marginMs = 0 } = {}) {
|
||||
const c = context(opts);
|
||||
return change(c, "sweep", (before, current) => {
|
||||
const nowMs = c.now();
|
||||
const due = sweepDue(current, nowMs, marginMs);
|
||||
if (due === "rotate") {
|
||||
const plan = rotatePlan(c, before, current, nowMs, marginMs);
|
||||
return { ...plan, op: "rotate", done: { action: "rotated", ...plan.done } };
|
||||
}
|
||||
if (due === "retire") {
|
||||
const plan = retirePlan(c, before, current, nowMs, marginMs);
|
||||
return { ...plan, op: "retire", done: { action: "retired", ...plan.done } };
|
||||
}
|
||||
return { after: before, record: null, done: { action: "none" } };
|
||||
});
|
||||
}
|
||||
|
||||
// Read only: the file, and when the proxy is set up, what its list holds. No lock, no write.
|
||||
// The fence is checked by name only, so status writes nothing, not even the lock's identity file.
|
||||
export async function keyStatus(opts) {
|
||||
const { configDir, system, uid, fileUid } = opts;
|
||||
const record = readKey(configDir, { uid, fileUid });
|
||||
const proxy = { state: "not-set-up", fence: false, leftoverTemps: tempsIn(keyPaths(configDir).dir).length };
|
||||
if (system?.proxy && Number.isInteger(system.proxy.port)) {
|
||||
try { lstatSync(join(system.dataRoot, "proxy", "mosaic-keys.uncertain")); proxy.fence = true; }
|
||||
catch (e) { if (e.code !== "ENOENT") proxy.fence = true; }
|
||||
try {
|
||||
const client = await loadManagementClient({ configDir, proxy: system.proxy });
|
||||
const keys = await client.getKeys();
|
||||
const prefix = sentinelPrefix(configDir);
|
||||
proxy.state = keys.some((k) => k.startsWith(prefix) && SECRET.test(k.slice(prefix.length))) ? "read" : "no-sentinel";
|
||||
proxy.holdsKey = record ? keys.includes(record.key) : null;
|
||||
proxy.retiringHeld = record ? record.retiring.map((r) => keys.includes(r.key)) : [];
|
||||
proxy.orphanKeyIds = orphansOf(configDir, keys, record).map(keyIdOf);
|
||||
} catch (e) { proxy.state = "unreadable"; proxy.code = typeof e?.code === "string" && /^proxy-[a-z-]+$/.test(e.code) ? e.code : "proxy-client-response"; }
|
||||
}
|
||||
return { record, proxy };
|
||||
}
|
||||
@@ -19,7 +19,7 @@ const here = (p) => new URL(p, import.meta.url);
|
||||
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
|
||||
// Row 206 (#1704): and the 3l→3m step's thread_shares. thread-shares.test.mjs tests it.
|
||||
// Row 271 (#1776): and the 3m→3n step's session_ops and message_cancels. session-clear.test.mjs tests them.
|
||||
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'project_system_types', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
|
||||
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
|
||||
function source3h(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'account-migrate-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
@@ -96,6 +96,7 @@ async function engine(root, name, { store, schema, step = (s) => s }) {
|
||||
copyFileSync(here('../src/migrations/3s-3t.sql'), join(moduleRoot, 'src/migrations/3s-3t.sql'));
|
||||
copyFileSync(here('../src/migrations/3t-3u.sql'), join(moduleRoot, 'src/migrations/3t-3u.sql'));
|
||||
copyFileSync(here('../src/migrations/3u-3v.sql'), join(moduleRoot, 'src/migrations/3u-3v.sql'));
|
||||
copyFileSync(here('../src/migrations/3v-3w.sql'), join(moduleRoot, 'src/migrations/3v-3w.sql'));
|
||||
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
|
||||
}
|
||||
|
||||
@@ -103,7 +104,7 @@ test('REQ-LIMIT-4 3h copy migrates to 3i: every row keeps its seq, the sequence
|
||||
const { root, path } = source3h(t), before = snapshot(path), was = master(path);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
// Every table's rows as they were, usage_records with its seq values and sqlite_sequence with 50.
|
||||
assert.deepEqual(migrated3l(snapshot(path, before.columns).rows), before.rows);
|
||||
assert.deepEqual(before.rows.usage_records.map(r => r.seq), [1, 2, 3]);
|
||||
@@ -153,7 +154,7 @@ test('REQ-LIMIT-4 a 3h→3i step that fails partway leaves the store at 3h with
|
||||
assert.deepEqual(snapshot(path, before.columns).rows, before.rows);
|
||||
// Still a whole 3h store: the real step migrates it.
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(migrated3l(snapshot(path, before.columns).rows), before.rows);
|
||||
});
|
||||
|
||||
@@ -165,14 +166,14 @@ test('REQ-LIMIT-4 the 3h engine refuses a migrated store, migrating or not, and
|
||||
schema: ddl3h,
|
||||
store: (source) => source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3h).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3h';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3h';")
|
||||
.replace(/\}, \{\n {2}\/\/ Row 126 \(#1620\)[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables'),
|
||||
});
|
||||
const pinned = readFileSync(join(root, 'engine-3h/src/store.mjs'), 'utf8');
|
||||
assert.ok(pinned.includes("export const SCHEMA_VERSION = '3h';") && !pinned.includes('3h-3i'), 'the 3h engine has no 3h→3i step');
|
||||
assert.throws(() => new Store3h(root), /schema-mismatch/);
|
||||
assert.throws(() => new Store3h(root, { migrateSchema: true }), /schema-mismatch/);
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(master(path), was);
|
||||
assert.deepEqual(snapshot(path, after.columns).rows, after.rows);
|
||||
});
|
||||
@@ -64,7 +64,7 @@ test('AU authority reverse crossing deadline in prepared IO never publishes',asy
|
||||
});
|
||||
test('AU authority resolver rechecks budget removal after a proposal was raised',async t=>{
|
||||
const f=world(t),d=await f.raise(),b=structuredClone(f.loaded());b.plugins['limit-check']=[];f.broker.reloadBusiness('demo',b);
|
||||
const before=f.writer.read('demo').revision;await assert.rejects(f.call('judge','decision.resolve',{id:d.id,choice:'yes'}),{code:'autonomy-precondition'});
|
||||
const before=f.writer.read('demo').revision;await assert.rejects(f.call('judge','decision.resolve',{id:d.id,choice:'yes'}),{code:'autonomy-precondition',reason:'no enforced budget covers new agents; authority away from the loop needs one'});
|
||||
assert.equal(f.writer.read('demo').revision,before);assert.equal(f.store.get('SELECT count(*) n FROM decision_events WHERE decision=?',d.id).n,0);
|
||||
});
|
||||
|
||||
@@ -113,7 +113,7 @@ test('AU an account-quota authority raise routes to the resolver only when the h
|
||||
const covered=world(t,{account:true,countsAccount:name=>name==='pi'}),d=await covered.raise();assert.equal(d.route_to,'judge');
|
||||
for(const countsAccount of [undefined,()=>false,()=>null]){
|
||||
const f=world(t,{account:true,countsAccount});
|
||||
await assert.rejects(f.raise(),{code:'autonomy-precondition'});
|
||||
await assert.rejects(f.raise(),{code:'autonomy-precondition',reason:'no enforced budget covers new agents; authority away from the loop needs one'});
|
||||
assert.equal(f.store.get("SELECT count(*) n FROM decisions").n,0);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -137,11 +137,11 @@ test('AU authority near requires an enforced organization budget covering every
|
||||
const f=await settingsFixture(t);
|
||||
for(const tokens of [{},{instances:{coder:tokenCap}},{providers:{unrelated:tokenCap}}]){
|
||||
f.business.launch.limits.tokens=tokens;f.broker.reloadBusiness('demo',f.business);
|
||||
await assert.rejects(f.set(authorityNear),{code:'autonomy-precondition'});
|
||||
await assert.rejects(f.set(authorityNear),{code:'autonomy-precondition',reason:'no enforced budget covers new agents; authority away from the loop needs one'});
|
||||
}
|
||||
f.business.launch.limits.tokens={providers:{anthropic:{...tokenCap,limit:0}}};
|
||||
f.business.plugins={'limit-check':[]};f.broker.reloadBusiness('demo',f.business);
|
||||
await assert.rejects(f.set(authorityNear),{code:'autonomy-precondition'});
|
||||
await assert.rejects(f.set(authorityNear),{code:'autonomy-precondition',reason:'no enforced budget covers new agents; authority away from the loop needs one'});
|
||||
f.business.plugins={'limit-check':['token-limit']};f.broker.reloadBusiness('demo',f.business);
|
||||
await f.set(authorityNear);assert.equal(f.writes(),1);
|
||||
});
|
||||
@@ -153,12 +153,20 @@ test('AU authority out refuses until per-definition agent limits exist, even wit
|
||||
await assert.rejects(f.set({...authorityNear,authority:'out of the loop'}),e=>e.code==='authority-limits-unavailable' && /near the loop/.test(e.nextStep));
|
||||
assert.equal(f.writes(),1);
|
||||
});
|
||||
test('AU a class moves out of the loop only from near the loop, and the refusal says so (E2E6 fix 7)',async t=>{
|
||||
const f=await settingsFixture(t);await f.set({'cross-role':'in the loop'});
|
||||
await assert.rejects(f.set({'cross-role':'out of the loop',notices:true}),{code:'autonomy-precondition',reason:'cross-role moves out of the loop only from near the loop'});
|
||||
assert.equal(f.writes(),1);
|
||||
});
|
||||
test('AU moving out requires an actual near resolution and a later owner review',async t=>{
|
||||
const f=await settingsFixture(t);await f.set({merge:'in the loop'});await f.set({merge:'near the loop',notices:true});
|
||||
// E2E6 fix 7: each refusal names the check that failed.
|
||||
await assert.rejects(f.set({merge:'near the loop'}),{code:'autonomy-precondition',reason:'owner notices are off; a class near the loop needs them on'});
|
||||
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition',reason:'merge needs your review of its trail since the last autonomy change'});
|
||||
f.call(f.owner,'autonomy.review.console',{decisionClass:'merge'});
|
||||
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition'});
|
||||
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition',reason:'merge needs a near-loop decision since the last autonomy change, and your trail review after it'});
|
||||
const d=f.raise({action:'git.merge.protected'});f.call(f.chief,'decision.resolve',{id:d.id,choice:'yes'});
|
||||
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition'});
|
||||
await assert.rejects(f.set({merge:'out of the loop',notices:true}),{code:'autonomy-precondition',reason:'merge needs a near-loop decision since the last autonomy change, and your trail review after it'});
|
||||
f.call(f.owner,'autonomy.review.console',{decisionClass:'merge'});
|
||||
await f.set({merge:'out of the loop',notices:true});
|
||||
});
|
||||
@@ -11,6 +11,8 @@
|
||||
// which shares humanCall's spawn as transport(). The human proof and its spawn are unchanged.
|
||||
// Row 225 (C2-10b) re-pins webui bus.mjs for one STATUS entry, library-revert-not-agent: 409; no proof
|
||||
// or transport change.
|
||||
// E2E6 fix 6 re-pins human-cli.mjs, which exits 5 when its connect failed before the request was
|
||||
// written, and cli transport.mjs, which reads that exit as bus-not-running. The proof is unchanged.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
@@ -21,9 +23,10 @@ import { Client } from "../src/client.mjs";
|
||||
|
||||
const baselines = {
|
||||
"packages/bus/src/human.mjs": "bb13c146717cdc46d75d157177e400edac4d3e10f14fe511027a25fd44cec885",
|
||||
"packages/bus/src/human-cli.mjs": "271b9781d73cf31cc562a56180e3ebcd725b5fde148a83347a201fdef2fc28af",
|
||||
"packages/bus/src/human-cli.mjs": "410892787cc70440b0ba4c45d6f24861d12ee5e70cba678cfeb1ed86b796c149",
|
||||
// Row 239 (#1745): EXIT outcome-unknown-revoked 1; no proof or transport change.
|
||||
"packages/cli/src/transport.mjs": "662642a8ce739fcb6c188ad07125a94e008c8a305eced127c8041d472c56e55b",
|
||||
// E2E6 fix 6: shim exit 5 with outcome-unknown becomes bus-not-running (exit 3, start and recover next).
|
||||
"packages/cli/src/transport.mjs": "101870f7ffb47a9cbd54ebd8c1901734cc9f442d6770cb2a0d059afd4d14b160",
|
||||
// Row 165 (#1662): seven STATUS entries for the role instance routes (organization-instance-exists,
|
||||
// organization-unknown-instance, organization-credentials-out-of-scope, organization-launch-not-configured,
|
||||
// organization-no-change, organization-project-mismatch, organization-unresolvable-instance), no proof or
|
||||
@@ -39,7 +42,8 @@ const baselines = {
|
||||
// deadline; userCall shares transport(). Nonces, argv and limits are unchanged.
|
||||
// #1873 A3: STATUS maps credential-unbound, copy-refused, project-refused and project-agent-elsewhere
|
||||
// to 409; no proof or transport change.
|
||||
"packages/webui/src/bus.mjs": "d8f3e0eb5299fdb192e98a1238ca693aae7c36af9a21de72d5f5d39610589377",
|
||||
// #1873 triage-fence: STATUS maps template-system-only to 400; no proof or transport change.
|
||||
"packages/webui/src/bus.mjs": "cd08e1beb49e9e881bb1b308f2ab3343a480b47dd9b17ccbd16e120d341d7511",
|
||||
};
|
||||
const repoFile = path => new URL("../../../" + path, import.meta.url);
|
||||
|
||||
|
||||
@@ -615,7 +615,7 @@ async function database3c(t) {
|
||||
let source = readFileSync(here('../src/store.mjs'), 'utf8');
|
||||
const pinned = source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3c).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3c';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3c';")
|
||||
.replace(/\}, \{\n {2}from: '3c',[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
|
||||
assert.notEqual(pinned, source);
|
||||
assert.ok(!pinned.includes('3c-3d'), 'the 3c engine has no 3c→3d step');
|
||||
@@ -639,7 +639,7 @@ test('REQ-ROLE-2, REQ-COS-1: the 3c→3d migration lets the chief of staff end a
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
t.after(() => store.close());
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
assert.equal(store.get("SELECT body FROM events WHERE id='preserved'").body, '{"fixture":true}');
|
||||
assert.equal(store.get("SELECT holder_run FROM role_claims WHERE role='coder'").holder_run, 'coder-run');
|
||||
const b = new Broker({ store, businesses: demo(), organization: null });
|
||||
|
||||
@@ -33,7 +33,7 @@ test('explicit 3b copy migration preserves evidence and opens as 3g; normal star
|
||||
const original = readFileSync(source);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
assert.equal(store.get("SELECT body FROM events WHERE id='preserved'").body, '{"fixture":true}');
|
||||
assert.equal(store.get('SELECT count(*) n FROM task_comment_cursors').n, 0);
|
||||
const epoch = store.get("SELECT value FROM meta WHERE key='comment_cursor_epoch'").value;
|
||||
@@ -75,15 +75,15 @@ test('named migration registry chains 3b through 3g to a fixture-only future ver
|
||||
const sha = createHash('sha256').update(futureDDL).digest('hex');
|
||||
let source = readFileSync(new URL('../src/store.mjs', import.meta.url), 'utf8');
|
||||
source = source.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${sha}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3w';")
|
||||
.replace('}];\nconst timedTables', `}, { from: '3v', to: '3w', sql: ${JSON.stringify(futureSQL)} }];\nconst timedTables`);
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3x';")
|
||||
.replace('}];\nconst timedTables', `}, { from: '3w', to: '3x', sql: ${JSON.stringify(futureSQL)} }];\nconst timedTables`);
|
||||
writeFileSync(join(moduleRoot, 'src/store.mjs'), source);
|
||||
writeFileSync(join(moduleRoot, 'schema.sql'), futureDDL);
|
||||
for (const name of ['3b.sql', '3b-3c.sql', '3c-3d.sql', '3d-3e.sql', '3e-3f.sql', '3f-3g.sql', '3g-3h.sql', '3h-3i.sql', '3i-3j.sql', '3j-3k.sql', '3k-3l.sql', '3l-3m.sql', '3m-3n.sql', '3n-3o.sql', '3o-3p.sql', '3p-3q.sql', '3q-3r.sql', '3r-3s.sql', '3s-3t.sql', '3t-3u.sql', '3u-3v.sql'])
|
||||
for (const name of ['3b.sql', '3b-3c.sql', '3c-3d.sql', '3d-3e.sql', '3e-3f.sql', '3f-3g.sql', '3g-3h.sql', '3h-3i.sql', '3i-3j.sql', '3j-3k.sql', '3k-3l.sql', '3l-3m.sql', '3m-3n.sql', '3n-3o.sql', '3o-3p.sql', '3p-3q.sql', '3q-3r.sql', '3r-3s.sql', '3s-3t.sql', '3t-3u.sql', '3u-3v.sql', '3v-3w.sql'])
|
||||
copyFileSync(new URL('../src/migrations/' + name, import.meta.url), join(moduleRoot, 'src/migrations', name));
|
||||
const { Store: FutureStore } = await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')));
|
||||
const future = new FutureStore(root, { migrateSchema: true });
|
||||
assert.equal(future.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
assert.equal(future.get("SELECT value FROM meta WHERE key='schema_version'").value, '3x');
|
||||
assert.equal(future.get("SELECT name FROM sqlite_master WHERE name='future_fixture'").name, 'future_fixture');
|
||||
assert.equal(future.get("SELECT count(*) n FROM task_comment_cursors").n, 0);
|
||||
assert.equal(future.get("SELECT id FROM events").id, 'preserved');
|
||||
@@ -109,7 +109,7 @@ test('offline command migrates only the supplied fixture root and reports usage
|
||||
const command = new URL('../src/migrate-schema.mjs', import.meta.url).pathname;
|
||||
const migrated = spawnSync(process.execPath, [command, root], { encoding: 'utf8' });
|
||||
assert.equal(migrated.status, 0, migrated.stderr);
|
||||
assert.match(migrated.stdout, /bus schema 3v ready/);
|
||||
assert.match(migrated.stdout, /bus schema 3w ready/);
|
||||
new Store(root).close();
|
||||
const usage = spawnSync(process.execPath, [command], { encoding: 'utf8' });
|
||||
assert.equal(usage.status, 2);
|
||||
|
||||
@@ -89,8 +89,12 @@ function setup(t) {
|
||||
// #797 / #1839 adds the owner-only artifact read to the existing decision and request controls;
|
||||
// no unsuffixed verb is ever a console verb.
|
||||
test('N4: owner Console verbs have an explicit immutable whitelist and exclude decision.resolve', () => {
|
||||
assert.deepEqual([...CONSOLE_VERBS], ['system-types.console', 'project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
|
||||
'request.get.console', 'request.projects.console', 'request.answer.console', 'request.attach.console', 'request.cancel.console']);
|
||||
assert.deepEqual([...CONSOLE_VERBS], ['project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'pack.hire.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
|
||||
'request.get.console', 'request.projects.console', 'request.answer.console', 'request.attach.console', 'request.cancel.console',
|
||||
// Starter packs SP4 (#1873): the owner's pack list and preview, and the three pack writes.
|
||||
'packs.console', 'pack.preview.console', 'pack.apply.console', 'board.set.console', 'workflow.set.console',
|
||||
// Starter packs SP8 (#1873): the owner saves and lists templates.
|
||||
'template.save.console', 'template.list.console']);
|
||||
assert.ok(CONSOLE_VERBS.every(verb => verb.endsWith('.console')));
|
||||
assert.equal(CONSOLE_VERBS.includes('decision.resolve'), false);
|
||||
assert.ok(Object.isFrozen(CONSOLE_VERBS));
|
||||
|
||||
@@ -133,3 +133,52 @@ test('CB1 bus: every new refusal has a table entry with a next step', () => {
|
||||
assert.ok(!/(^|[\s("'`])(\/|~\/)/.test(REFUSALS[code].nextStep), code);
|
||||
}
|
||||
});
|
||||
|
||||
// #1873 CB5 (security review P2-1, P3-1): the binding record the raise holds (copy id, incarnation,
|
||||
// definition pin, services, grant ids) leaves the bus only through the owner's preview verb. The
|
||||
// raise answer, the inbox preview and the trail give every other session the copy and the change.
|
||||
test('CB5 bus: the raise answer, the inbox preview and the trail carry a binding as its copy and change only', async t => {
|
||||
const f = await fixture(t);
|
||||
const d = await f.raise();
|
||||
const raised = f.store.all('SELECT body FROM events').map((e) => JSON.parse(e.body)).find((b) => b.operation === 'decision.raise' && b.decision === d.id);
|
||||
const held = raised.hire.binding;
|
||||
assert.equal(typeof held.copyId, 'string');
|
||||
const secrets = [held.copyId, held.orgIncarnation, held.definition?.sha256].filter((v) => typeof v === 'string' && v.length >= 8);
|
||||
assert.equal(secrets.length, 3);
|
||||
const summary = { instance: 'rev', change: 'bind' };
|
||||
assert.deepEqual(d.hire.binding, summary);
|
||||
const sessions = { owner: f.b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true }), tester: f.b.bindTester({ business: 'demo' }), reader: f.b.bindReader({ business: 'demo' }), chief: f.caps.chief, coder: f.caps.coder };
|
||||
const seen = [JSON.stringify(d)];
|
||||
for (const name of ['owner', 'tester', 'reader']) {
|
||||
const rows = await f.call(sessions[name], 'inbox');
|
||||
const row = rows.find((r) => r.id === d.id);
|
||||
assert.deepEqual(row?.console?.binding, summary, `${name} inbox`);
|
||||
seen.push(JSON.stringify(rows));
|
||||
}
|
||||
for (const name of Object.keys(sessions)) {
|
||||
const trail = await f.call(sessions[name], 'trail', { subject: d.id });
|
||||
const ev = trail.find((r) => r.table === 'events' && r.body?.operation === 'decision.raise');
|
||||
assert.deepEqual(ev?.body?.hire?.binding, summary, `${name} trail`);
|
||||
seen.push(JSON.stringify(trail));
|
||||
}
|
||||
for (const text of seen) for (const v of secrets) assert.equal(text.includes(v), false);
|
||||
// The owner's preview still has the record, and the resolve (which reads the raise) still writes it.
|
||||
const preview = await f.call(f.owner, 'hire.preview.console', { id: d.id });
|
||||
assert.equal(preview.binding.copyId, held.copyId);
|
||||
await f.call(f.owner, 'hire.resolve.console', { id: d.id, choice: 'yes', digest: d.hire.digest });
|
||||
assert.equal(f.bindings().rev.state, 'pending');
|
||||
const after = JSON.stringify(await f.call(sessions.tester, 'trail', { subject: d.id }));
|
||||
for (const v of secrets) assert.equal(after.includes(v), false);
|
||||
// An unbind of that binding reads as an unbind in the raise answer, the inbox and the trail.
|
||||
const u = await f.raise({ binding: { services: null } });
|
||||
const unbind = { instance: 'rev', change: 'unbind' };
|
||||
assert.deepEqual(u.hire.binding, unbind);
|
||||
for (const name of ['owner', 'tester', 'reader']) {
|
||||
const row = (await f.call(sessions[name], 'inbox')).find((r) => r.id === u.id);
|
||||
assert.deepEqual(row?.console?.binding, unbind, `${name} inbox unbind`);
|
||||
}
|
||||
for (const name of Object.keys(sessions)) {
|
||||
const ev = (await f.call(sessions[name], 'trail', { subject: u.id })).find((r) => r.table === 'events' && r.body?.operation === 'decision.raise');
|
||||
assert.deepEqual(ev?.body?.hire?.binding, unbind, `${name} trail unbind`);
|
||||
}
|
||||
});
|
||||
@@ -367,17 +367,19 @@ const registryEdit = (f, fn) => {
|
||||
};
|
||||
|
||||
test('CB3 an organization no longer active admits and sends nothing, with the binding and incarnation unchanged', async (t) => {
|
||||
for (const state of ['archived', 'deleting', 'reserved']) {
|
||||
// #1873 CB4 (design section 6): an archived organization answers org-archived, any other state that
|
||||
// is not active org-unavailable (CB3 answered credential-revoked for both).
|
||||
for (const [state, code] of [['archived', 'org-archived'], ['deleting', 'org-unavailable'], ['reserved', 'org-unavailable']]) {
|
||||
await t.test(`${state} after launch`, async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.boot();
|
||||
const { cap, authority } = await f.launch();
|
||||
registryEdit(f, (e) => { e.state = state; });
|
||||
const before = f.sent.length;
|
||||
assert.throws(() => f.gate.authority('demo', 'rev'), refuses('credential-revoked'));
|
||||
assert.throws(() => f.gate.admit('demo', 'rev', authority), refuses('credential-revoked'));
|
||||
await assert.rejects(f.gate.prepare('demo', 'rev'), refuses('credential-revoked'));
|
||||
await assert.rejects(f.use(cap, f.GET), refuses('credential-revoked'));
|
||||
assert.throws(() => f.gate.authority('demo', 'rev'), refuses(code));
|
||||
assert.throws(() => f.gate.admit('demo', 'rev', authority), refuses(code));
|
||||
await assert.rejects(f.gate.prepare('demo', 'rev'), refuses(code));
|
||||
await assert.rejects(f.use(cap, f.GET), refuses(code));
|
||||
assert.equal(f.sent.length, before);
|
||||
assert.equal(f.repoCalls(), 0);
|
||||
});
|
||||
@@ -390,7 +392,7 @@ test('CB3 an organization no longer active admits and sends nothing, with the bi
|
||||
await h.arrived;
|
||||
registryEdit(f, (e) => { e.state = 'archived'; });
|
||||
h.release();
|
||||
await assert.rejects(preparing, refuses('credential-revoked'));
|
||||
await assert.rejects(preparing, refuses('org-archived'));
|
||||
assert.equal(f.repoCalls(), 0);
|
||||
});
|
||||
await t.test('archived before boot', async (t) => {
|
||||
@@ -416,9 +418,9 @@ test('CB3 an organization recreated under the same id (a new incarnation) admits
|
||||
const { cap, authority } = await f.launch();
|
||||
registryEdit(f, (e) => { e.incarnation = 'f'.repeat(32); });
|
||||
const before = f.sent.length;
|
||||
assert.throws(() => f.gate.admit('demo', 'rev', authority), refuses('credential-revoked'));
|
||||
await assert.rejects(f.gate.prepare('demo', 'rev'), refuses('credential-revoked'));
|
||||
await assert.rejects(f.use(cap, f.GET), refuses('credential-revoked'));
|
||||
assert.throws(() => f.gate.admit('demo', 'rev', authority), refuses('org-unavailable'));
|
||||
await assert.rejects(f.gate.prepare('demo', 'rev'), refuses('org-unavailable'));
|
||||
await assert.rejects(f.use(cap, f.GET), refuses('org-unavailable'));
|
||||
assert.equal(f.sent.length, before);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
// #1873 CB4 live acceptance: the owner's revoke of a bound copy against a scratch gitea/gitea:1.27.1, a
|
||||
// throwaway container on 127.0.0.1 over TLS with a scratch CA, removed after. A bound copy boots, launches
|
||||
// and reads its repository; the owner revokes; the run is stopped and acknowledged, the slot goes, the
|
||||
// copy's token is deleted at the container through the organization's admin slot, and the next call on
|
||||
// the launch's authority refuses before any request. The test never pulls an image; without Docker or
|
||||
// the image it skips. Every token here is a scratch token this container made; none is printed, and
|
||||
// nothing here reaches git.mosaicstack.dev or any real service.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { request } from 'node:https';
|
||||
import { createServer } from 'node:net';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { Broker, BusError } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { Credentials } from '../src/credentials.mjs';
|
||||
import { copyGrant } from '../src/copy-grant.mjs';
|
||||
import { copyLaunchGate } from '../src/copy-launch.mjs';
|
||||
import { copyRevoker, giteaCopyRemote } from '../src/copy-revoke.mjs';
|
||||
import { giteaGrant, send } from '../src/gitea-grant.mjs';
|
||||
import { hireDecisions } from '../../organization/src/hire-decisions.mjs';
|
||||
import { organizationWriter } from '../../organization/src/organization.mjs';
|
||||
import { checkRootAgents } from '../../organization/src/agent-levels.mjs';
|
||||
import { loadWithCopies, materializeCopies, promoteCopies } from '../../organization/src/agent-copies.mjs';
|
||||
import { activateOrg, reserveOrg } from '../../organization/src/org-registry.mjs';
|
||||
import { copyBotLogin } from '../../organization/src/copy-bindings.mjs';
|
||||
import { businessDoc, rolesCopy, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
import { scratchCa } from '../../seat/tests/fake-gitea.mjs';
|
||||
|
||||
const IMAGE = 'gitea/gitea:1.27.1';
|
||||
const SUBJECT = { provider: 'test', iss: 'https://issuer.test', sub: 'owner' };
|
||||
const READ = { gitea: { scopes: ['read:repository', 'read:user'], projects: ['stack'] } };
|
||||
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||||
const ahead = (days) => new Date(Date.now() + days * 86400000).toISOString().slice(0, 10);
|
||||
const sha = (s) => createHash('sha256').update(s).digest('hex');
|
||||
const docker = (...args) => spawnSync('docker', args, { encoding: 'utf8', timeout: 120000 });
|
||||
const skip = docker('image', 'inspect', IMAGE).status === 0 ? false : `no Docker or no local ${IMAGE}; this test never pulls`;
|
||||
|
||||
const freePort = () =>
|
||||
new Promise((resolve, reject) => {
|
||||
const s = createServer();
|
||||
s.once('error', reject);
|
||||
s.listen(0, '127.0.0.1', () => {
|
||||
const { port } = s.address();
|
||||
s.close(() => resolve(port));
|
||||
});
|
||||
});
|
||||
|
||||
// One HTTPS call to the scratch container, trusting only the scratch CA.
|
||||
function call(url, ca, method, path, { auth = null, body = null } = {}) {
|
||||
return new Promise((resolve) => {
|
||||
const data = body ? JSON.stringify(body) : null;
|
||||
const headers = { accept: 'application/json', ...(data ? { 'content-type': 'application/json' } : {}), ...(auth ? { authorization: auth } : {}) };
|
||||
const req = request(url + path, { method, ca, headers, timeout: 10000 }, (res) => {
|
||||
let text = '';
|
||||
res.on('data', (c) => (text += c));
|
||||
res.on('end', () => {
|
||||
let json = null;
|
||||
try {
|
||||
json = text ? JSON.parse(text) : null;
|
||||
} catch {}
|
||||
resolve({ status: res.statusCode, json });
|
||||
});
|
||||
});
|
||||
req.on('timeout', () => req.destroy());
|
||||
req.on('error', () => resolve({ status: 0, json: null }));
|
||||
req.end(data ?? undefined);
|
||||
});
|
||||
}
|
||||
|
||||
test('CB4-D: on scratch 1.27.1 the owner revokes a bound copy: run stopped, slot gone, token deleted at the service, and the next call refuses unsent', { skip, timeout: 300000 }, async (t) => {
|
||||
// The child's complete TAP output is captured before any of it reaches this runner, so the parent
|
||||
// can search it for the minted values without putting one in an assertion.
|
||||
if (!process.env.CB4_REVOKE_CAPTURE) {
|
||||
const capture = mkdtempSync(join(tmpdir(), 'cb4cap-'));
|
||||
t.after(() => rmSync(capture, { recursive: true, force: true }));
|
||||
const file = join(capture, 'minted');
|
||||
const childEnv = { ...process.env, CB4_REVOKE_CAPTURE: file };
|
||||
delete childEnv.NODE_TEST_CONTEXT;
|
||||
const child = spawnSync(process.execPath, ['--test', '--test-name-pattern=^CB4-D:', fileURLToPath(import.meta.url)], {
|
||||
encoding: 'utf8', timeout: 280000, env: childEnv,
|
||||
});
|
||||
const values = existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : [];
|
||||
const output = (child.stdout ?? '') + (child.stderr ?? '');
|
||||
assert.ok(values.every((value) => !output.includes(value) && !output.includes(sha(value))), 'complete TAP stdout/stderr contain no minted token');
|
||||
t.diagnostic(output);
|
||||
t.diagnostic(`minted ${values.length}, hits 0 (complete TAP stdout/stderr)`);
|
||||
assert.equal(child.status, 0, 'Docker child passed');
|
||||
assert.equal(values.length, 2, 'both scratch tokens were minted: the acceptance actually ran');
|
||||
return;
|
||||
}
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), 'cb4d-'));
|
||||
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
||||
const tlsDir = join(dir, 'tls'), tls = scratchCa(tlsDir);
|
||||
// The container's git user reads the scratch key; it is a throwaway key for this run only.
|
||||
chmodSync(tlsDir, 0o755);
|
||||
chmodSync(join(tlsDir, 'server.key'), 0o644);
|
||||
const ca = readFileSync(tls.ca, 'utf8'), port = await freePort();
|
||||
const url = `https://127.0.0.1:${port}`, name = 'cb4-' + randomBytes(6).toString('hex');
|
||||
t.after(() => docker('rm', '-f', name));
|
||||
const started = docker('run', '-d', '--rm', '--name', name, '-p', `127.0.0.1:${port}:3000`, '-v', `${tlsDir}:/certs:ro`,
|
||||
'-e', 'GITEA__security__INSTALL_LOCK=true', '-e', 'GITEA__database__DB_TYPE=sqlite3',
|
||||
'-e', 'GITEA__service__DISABLE_REGISTRATION=true', '-e', 'GITEA__server__PROTOCOL=https',
|
||||
'-e', 'GITEA__server__CERT_FILE=/certs/server.pem', '-e', 'GITEA__server__KEY_FILE=/certs/server.key',
|
||||
'-e', `GITEA__server__ROOT_URL=${url}/`, IMAGE);
|
||||
assert.equal(started.status, 0, 'scratch container starts');
|
||||
const end = Date.now() + 120000;
|
||||
while ((await call(url, ca, 'GET', '/api/v1/version')).status !== 200) {
|
||||
assert.ok(Date.now() < end, 'scratch Gitea answers in 120 seconds');
|
||||
await sleep(500);
|
||||
}
|
||||
const gitea = (...args) => docker('exec', '-u', 'git', name, 'gitea', ...args);
|
||||
const minted = [];
|
||||
const createUser = (user, admin = false) => {
|
||||
const r = gitea('admin', 'user', 'create', '--username', user, '--email', `${user}@example.test`,
|
||||
'--password', randomBytes(16).toString('base64url'), '--must-change-password=false', ...(admin ? ['--admin'] : []));
|
||||
assert.equal(r.status, 0, `create scratch ${user}`);
|
||||
};
|
||||
const mint = (user, scopes) => {
|
||||
const r = gitea('admin', 'user', 'generate-access-token', '--username', user, '--token-name', 'cb4-' + randomBytes(4).toString('hex'),
|
||||
'--scopes', scopes.join(','), '--raw');
|
||||
assert.equal(r.status, 0, `mint scratch ${user}`);
|
||||
const value = r.stdout.trim();
|
||||
assert.ok(/^[0-9a-f]{40}$/.test(value), 'minted token shape');
|
||||
minted.push(value);
|
||||
writeFileSync(process.env.CB4_REVOKE_CAPTURE, JSON.stringify(minted), { mode: 0o600 });
|
||||
return value;
|
||||
};
|
||||
// The organization's dedicated grant administrator (owns no repository) and the repository owner.
|
||||
createUser('mosaic-grant', true);
|
||||
createUser('owner');
|
||||
const adminToken = mint('mosaic-grant', ['write:admin', 'write:repository', 'write:user']);
|
||||
const admin = 'token ' + adminToken, basic = 'Basic ' + Buffer.from(`mosaic-grant:${adminToken}`).toString('base64');
|
||||
const made = await call(url, ca, 'POST', '/api/v1/admin/users/owner/repos', { auth: admin, body: { name: 'stack', auto_init: true, private: true } });
|
||||
assert.equal(made.status, 201, 'create owner/stack');
|
||||
|
||||
// The organization: one root agent "rev" (research, Gitea alone) bound read-only to project stack,
|
||||
// and the organization's Gitea admin slot on this container.
|
||||
const root = mkdtempSync(join(tmpdir(), 'cb4o-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const rolesDir = rolesCopy(root);
|
||||
const doc = { ...businessDoc(root, 'demo'), plugins: { harness: ['pi'] }, vars: { 'bus.tester': true } };
|
||||
doc.projects.stack.remote = `${url}/owner/stack`;
|
||||
writeJson(join(root, 'businesses/demo.json'), doc);
|
||||
const dataRoot = join(root, 'data');
|
||||
mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const ctx = { configDir: root, dataRoot, instance: 'test' };
|
||||
await reserveOrg(ctx, 'demo');
|
||||
await activateOrg(ctx, 'demo');
|
||||
const snapshot = materializeCopies({ orgId: 'demo', doc, root: { state: 'ok', ...checkRootAgents({ agentsVersion: 1, agents: { rev: { definition: 'research' } } }, { rolesDir }) }, rolesDir });
|
||||
const writer = organizationWriter({ dir: root, rolesDir }), store = new Store(dataRoot);
|
||||
t.after(() => store.close());
|
||||
const service = hireDecisions({ writer, rolesDir, dataRoot, configDir: root, assertClean: () => {}, copies: (id) => (snapshot.org === id ? snapshot : null) });
|
||||
const storeRoot = join(root, 'secrets', 'agents');
|
||||
mkdirSync(storeRoot, { recursive: true, mode: 0o700 });
|
||||
chmodSync(join(root, 'secrets'), 0o700);
|
||||
const grant = giteaGrant({ configDir: root, ca, timeoutMs: 10000 });
|
||||
grant.writeAdmin('demo', adminToken, { url, account: 'mosaic-grant', rotateBy: ahead(60), setAt: new Date().toISOString() });
|
||||
const credentials = new Credentials({ references: {}, storeRoot, repoRoots: [], dataRoot: '/nonexistent-data' });
|
||||
t.after(() => credentials.close?.());
|
||||
// Every request the gate sends, by op only.
|
||||
const sent = [];
|
||||
const transport = (req) => { sent.push(`${req.method} ${req.path.split('?')[0]}`); return send(req); };
|
||||
let revoker = null, b = null;
|
||||
const gate = copyLaunchGate({ credentials, writer, configDir: root, rolesDir, snapshot, transport, ca, timeoutMs: 10000,
|
||||
onLoss: (business, instance, cause) => revoker?.start(business, instance, { cause, actor: 'bus' }) });
|
||||
// The launcher's part of a stop: every run the broker told it to kill exits and is acknowledged.
|
||||
const killed = () => store.all("SELECT body FROM events WHERE kind='action.allowed'").map((r) => JSON.parse(r.body))
|
||||
.filter((x) => x.action === 'lifecycle.end' && x.kill === true).flatMap((x) => x.runs);
|
||||
const stops = [];
|
||||
const stopRuns = async (business, instance, opts) => {
|
||||
stops.push(instance);
|
||||
const p = b.stopCopyRuns(business, instance, { ...opts, timeoutMs: 5000 });
|
||||
for (const run of killed()) try { b.endLaunch({ business, run, reason: 'killed' }); } catch {}
|
||||
return p;
|
||||
};
|
||||
revoker = copyRevoker({ writer, gate, credentials, stopRuns, remote: giteaCopyRemote({ grant, read: (x) => writer.read(x).document }),
|
||||
definitions: () => snapshot.definitions ?? {} });
|
||||
let promoted = snapshot;
|
||||
const business = () => {
|
||||
const d = loadWithCopies('demo', { dir: root, rolesDir, snapshot: promoted });
|
||||
return { ...d, roles: Object.fromEntries(Object.entries(d.roles).map(([n, e]) => [n, { ...d.definitions?.[e.definition], ...e }])) };
|
||||
};
|
||||
const issue = copyGrant({ credentials, writer, copies: snapshot, organizations: () => ['demo'], ca, timeoutMs: 10000 });
|
||||
b = new Broker({
|
||||
store, businesses: { demo: business() }, copyLaunch: gate, copyRevoker: revoker,
|
||||
hires: { ...service, apply: async (...args) => { const r = await service.apply(...args); b.reloadBusiness('demo', business()); return r; } },
|
||||
credentialVerbs: () => { throw new BusError('credential-unknown'); }, copyGrant: (...a) => issue(...a),
|
||||
});
|
||||
const consoleCap = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT });
|
||||
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const request_ = (cap, verb, args) => Promise.resolve().then(() => b.request(cap, { verb, args }));
|
||||
await request_(consoleCap, 'hire.start.console', { op: 'change', level: 'root', name: 'rev', reason: 'Bind the copy', binding: { services: READ } });
|
||||
const rec = () => JSON.parse(readFileSync(join(root, 'businesses/demo.json'), 'utf8')).copyBindings.rev;
|
||||
const login = copyBotLogin(rec().orgIncarnation, rec().copyId, 'gitea');
|
||||
createUser(login);
|
||||
assert.equal((await call(url, ca, 'PUT', `/api/v1/repos/owner/stack/collaborators/${login}`, { auth: admin, body: { permission: 'read' } })).status, 204,
|
||||
'bot is a read collaborator on owner/stack');
|
||||
const botToken = mint(login, ['read:repository', 'read:user']);
|
||||
assert.equal((await request_(human, 'credentials.issue', { agent: 'rev', service: 'gitea', date: ahead(30), token: botToken })).state, 'bound');
|
||||
const tokens = async () => (await call(url, ca, 'GET', `/api/v1/users/${login}/tokens`, { auth: basic })).json;
|
||||
assert.equal((await tokens()).length, 1, 'the bot holds one token at the service');
|
||||
assert.match(rec().services.gitea.grantId, /^gitea:\d+:\d+$/, 'the record names the grant by ids');
|
||||
|
||||
// Boot, launch, one checked call on the launch's authority.
|
||||
await gate.boot('demo');
|
||||
assert.deepEqual(gate.bound(), { rev: ['gitea'] });
|
||||
promoted = promoteCopies(snapshot, gate.bound());
|
||||
b.reloadBusiness('demo', business());
|
||||
const project = (await request_(human, 'project.create', { name: 'stack work', repo: 'stack' })).id;
|
||||
const thread = (await request_(human, 'thread.create', { project, name: 'one', instance: 'rev' })).id;
|
||||
const revision = writer.read('demo').revision;
|
||||
const { authority } = await gate.prepare('demo', 'rev', revision);
|
||||
const cap = b.bindLaunch({ business: 'demo', role: 'rev', run: 'rev-run', harness: 'pi', thread, copyAuthority: authority, revision });
|
||||
const session = { business: 'demo', role: 'rev', copyAuthority: authority };
|
||||
const own = await gate.call(b.copySession(cap), { method: 'GET', path: '/api/v1/repos/owner/stack' });
|
||||
assert.equal(own.status, 200);
|
||||
assert.equal(own.json.full_name, 'owner/stack');
|
||||
|
||||
// The owner's revoke.
|
||||
const out = await request_(human, 'credentials.revoke', { agent: 'rev', service: 'gitea', reason: 'Owner revoke' });
|
||||
assert.ok(out, 'the revoke answers');
|
||||
const r = rec();
|
||||
assert.equal(r.state, 'unbound');
|
||||
assert.equal(r.revoke.done, true);
|
||||
assert.equal(r.revoke.cause, 'revoke');
|
||||
assert.deepEqual(r.revoke.remote, { gitea: 'revoked' }, 'the record says the token was revoked at the service');
|
||||
assert.deepEqual(r.services, {});
|
||||
assert.ok(!existsSync(join(storeRoot, 'demo', 'rev', 'gitea')), 'slot removed');
|
||||
assert.deepEqual(stops, ['rev'], 'one stop, acknowledged');
|
||||
assert.ok(killed().includes('rev-run'), 'the run was told to stop');
|
||||
assert.throws(() => b.copySession(cap), (e) => e instanceof BusError, 'the launch session is gone');
|
||||
|
||||
// At the service: the bot has no token, and the old token itself is refused by Gitea.
|
||||
assert.deepEqual(await tokens(), [], 'token deleted at the service');
|
||||
const direct = await call(url, ca, 'GET', '/api/v1/user', { auth: 'token ' + botToken });
|
||||
assert.equal(direct.status, 401, 'Gitea refuses the revoked token');
|
||||
|
||||
// The next call on the launch's authority, and a new prepare: both refuse, nothing sent.
|
||||
const before = sent.length;
|
||||
await assert.rejects(gate.call(session, { method: 'GET', path: '/api/v1/repos/owner/stack' }), (e) => e instanceof BusError);
|
||||
await assert.rejects(gate.prepare('demo', 'rev', writer.read('demo').revision), { code: 'credential-revoked' });
|
||||
assert.equal(sent.length, before, 'no request after the revoke');
|
||||
t.diagnostic(`minted ${minted.length}`);
|
||||
});
|
||||
@@ -0,0 +1,842 @@
|
||||
// #1873 CB4 (design sections 3 and 6, section 8 row CB4): every lifecycle event that ends an agent
|
||||
// copy's credential binding closes its admissions, drains its calls in flight, stops each of its runs
|
||||
// and waits for the stop to be acknowledged, removes its slot, revokes the grant at the service
|
||||
// through the organization's managed admin slot (or records why not), and only then writes the
|
||||
// tombstone. A crash at any point leaves a pending op that the next start finishes under the same
|
||||
// id; nothing in between resolves, launches or reloads the copy's token. The fake Gitea is
|
||||
// fake-gitea-api.mjs on 127.0.0.1; the live check against a real Gitea is copy-revoke-docker.test.mjs.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, unlinkSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { Broker, BusError } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { Credentials } from '../src/credentials.mjs';
|
||||
import { REFUSALS } from '../src/refusals.mjs';
|
||||
import { copyGrant } from '../src/copy-grant.mjs';
|
||||
import { copyLaunchGate } from '../src/copy-launch.mjs';
|
||||
import { copyRevoker, giteaCopyRemote, managedCopyRemote, REVOKE_POINTS } from '../src/copy-revoke.mjs';
|
||||
import { giteaGrant, send } from '../src/gitea-grant.mjs';
|
||||
import { fakeGiteaApi } from './fake-gitea-api.mjs';
|
||||
import { hireDecisions } from '../../organization/src/hire-decisions.mjs';
|
||||
import { organizationWriter } from '../../organization/src/organization.mjs';
|
||||
import { checkRootAgents } from '../../organization/src/agent-levels.mjs';
|
||||
import { loadWithCopies, materializeCopies, promoteCopies } from '../../organization/src/agent-copies.mjs';
|
||||
import { activateOrg, listOrgs, readOrgEntrySync, reserveOrg } from '../../organization/src/org-registry.mjs';
|
||||
import * as orgService from '../../organization/src/org-service.mjs';
|
||||
import { copyBotLogin } from '../../organization/src/copy-bindings.mjs';
|
||||
import { COPY_BINDING_STATUS } from '../../organization/src/copy-binding-status.mjs';
|
||||
import { businessDoc, rolesCopy, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
const SUBJECT = { provider: 'test', iss: 'https://issuer.test', sub: 'owner' };
|
||||
const LOCAL = { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' };
|
||||
const READ = { gitea: { scopes: ['read:repository', 'read:user'], projects: ['stack'] } };
|
||||
const ahead = (days) => new Date(Date.now() + days * 86400000).toISOString().slice(0, 10);
|
||||
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||||
const CODES = ['credential-revoke-failed', 'credential-revoking', 'credential-revoked', 'credential-unbound', 'credential-unavailable',
|
||||
'credential-expired', 'credential-role-changed', 'copy-unavailable', 'copy-refused', 'org-archived', 'org-unavailable'];
|
||||
|
||||
const refuses = (code) => (e) => { assert.ok(e instanceof BusError || e?.code, String(e?.stack ?? e)); assert.equal(e.code, code); return true; };
|
||||
async function until(pred, what, ms = 8000) {
|
||||
const end = Date.now() + ms;
|
||||
while (Date.now() < end) {
|
||||
if (await pred()) return;
|
||||
await sleep(10);
|
||||
}
|
||||
assert.fail(`timed out waiting for ${what}`);
|
||||
}
|
||||
|
||||
// One scratch root with one fake Gitea, shared by every organization a test makes, as one stack
|
||||
// instance's configuration directory is.
|
||||
async function world(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'cb4-')), rolesDir = rolesCopy(root);
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const api = await fakeGiteaApi(t, root);
|
||||
const data = join(root, 'data'); mkdirSync(data, { mode: 0o700 });
|
||||
return { root, rolesDir, api, data };
|
||||
}
|
||||
|
||||
// Organization `id` with project stack on the fake Gitea and the root copy `name` (research: it
|
||||
// needs gitea alone). bind() has the owner bind the copy and issue one live token for it. start()
|
||||
// builds the bus for the organization (store, credentials, gate, revoker, broker) on the files as
|
||||
// they stand, as a restart of the bus process would; boot() runs the gate's boot proof and promotes.
|
||||
// stopRuns acknowledges each run the broker told the launcher to kill, as the launcher's exit does,
|
||||
// unless `ack` is false. The revoke's remote step goes to the organization's admin slot.
|
||||
async function org(t, w, { id = 'demo', name = 'rev', services = READ, perm = 'read', definition = 'research', admin = true, adminRotateBy = ahead(60), stopMs = 2000, project = null, gitOnly = false, definitions = null } = {}) {
|
||||
const { root, rolesDir, api } = w;
|
||||
const owner = id === 'demo' ? 'owner' : id;
|
||||
const doc = { ...businessDoc(root, id), plugins: { harness: ['pi'] }, vars: { 'bus.tester': true } };
|
||||
doc.projects.stack.remote = `${api.url}/${owner}/stack`;
|
||||
// A project set up in git remote mode names its repository there, with no top-level remote.
|
||||
if (gitOnly) { doc.projects.stack.git = { mode: 'remote', remote: doc.projects.stack.remote }; delete doc.projects.stack.remote; }
|
||||
// The project root the organization service derives (archive and delete check it).
|
||||
doc.projects.stack.root = join(w.data, 'org-runtime', id, 'projects', 'stack');
|
||||
if (project) doc.projects.stack.agents = project;
|
||||
if (definitions) doc.definitions = definitions;
|
||||
const file = writeJson(join(root, `businesses/${id}.json`), doc);
|
||||
chmodSync(join(root, 'businesses'), 0o700);
|
||||
const ctx = { configDir: root, dataRoot: w.data, instance: 'test', principal: LOCAL };
|
||||
const entry = await reserveOrg(ctx, id);
|
||||
// The organization service's owner proof, as createOrg writes it.
|
||||
writeFileSync(join(entry.root, 'owner.json'), JSON.stringify(LOCAL) + '\n', { mode: 0o600, flag: 'wx' });
|
||||
mkdirSync(doc.projects.stack.root, { recursive: true, mode: 0o700 });
|
||||
await activateOrg(ctx, id);
|
||||
const writer = organizationWriter({ dir: root, rolesDir });
|
||||
const storeRoot = join(root, 'secrets', 'agents');
|
||||
mkdirSync(storeRoot, { recursive: true, mode: 0o700 }); chmodSync(join(root, 'secrets'), 0o700);
|
||||
const grant = giteaGrant({ configDir: root, ca: api.ca, timeoutMs: 3000 });
|
||||
if (admin) grant.writeAdmin(id, api.adminToken, { url: api.url, account: 'mosaic-grant', rotateBy: adminRotateBy, setAt: new Date().toISOString() });
|
||||
const repo = api.addRepo(owner, 'stack');
|
||||
const storeDir = join(root, `store-${id}`); mkdirSync(storeDir, { mode: 0o700 });
|
||||
const f = {
|
||||
id, name, root, rolesDir, api, file, writer, ctx, storeRoot, grant, repo, stopMs, ack: true, sent: [], stops: [],
|
||||
clock: Date.now(), hooks: {}, rootAgents: { [name]: { definition } }, project: null, runs: [],
|
||||
GET: { method: 'GET', path: `/api/v1/repos/${owner}/stack` },
|
||||
};
|
||||
f.slot = join(storeRoot, id, name, 'gitea');
|
||||
f.rec = () => JSON.parse(readFileSync(file, 'utf8')).copyBindings?.[name];
|
||||
f.advance = (days) => { f.clock += days * 86400000; };
|
||||
// The owner's edit of the organization file, through the writer (a hand edit breaks the registry
|
||||
// digest fence, and the writer then refuses every write, a revoke's included).
|
||||
f.edit = (fn) => { const { document, revision } = writer.read(id); const d = structuredClone(document); fn(d); writer.write(id, d, { revision, actor: 'local-dev-owner' }); };
|
||||
f.call = (cap, verb, args = {}) => Promise.resolve().then(() => f.b.request(cap, { verb, args }));
|
||||
f.repoCalls = () => f.sent.filter((x) => x.includes('/api/v1/repos/')).length;
|
||||
f.killed = () => f.store.all("SELECT body FROM events WHERE kind='action.allowed'").map((r) => JSON.parse(r.body))
|
||||
.filter((x) => x.action === 'lifecycle.end' && x.kill === true).flatMap((x) => x.runs);
|
||||
f.stopRuns = async (business, instance, opts) => {
|
||||
f.stops.push({ instance, ...opts });
|
||||
const p = f.b.stopCopyRuns(business, instance, { ...opts, timeoutMs: f.stopMs });
|
||||
if (f.ack) for (const run of f.killed()) try { f.b.endLaunch({ business, run, reason: 'killed' }); } catch {}
|
||||
return p;
|
||||
};
|
||||
// A start of the bus process on the files as they stand: the boot snapshot from `rootAgents`
|
||||
// (the root agents file at this start), and a fresh store handle, credentials, gate and revoker.
|
||||
f.start = (rootAgents = f.rootAgents) => {
|
||||
try { f.store?.close(); } catch {}
|
||||
try { f.credentials?.close(); } catch {}
|
||||
const now = JSON.parse(readFileSync(file, 'utf8'));
|
||||
const snapshot = materializeCopies({ orgId: id, doc: now, root: { state: 'ok', ...checkRootAgents({ agentsVersion: 1, agents: rootAgents }, { rolesDir }) }, rolesDir });
|
||||
const store = new Store(storeDir);
|
||||
const credentials = new Credentials({ references: {}, storeRoot, repoRoots: [], dataRoot: '/nonexistent-data' });
|
||||
const service = hireDecisions({ writer, rolesDir, dataRoot: storeDir, configDir: root, assertClean: () => {}, copies: (x) => snapshot.org === x ? snapshot : null });
|
||||
const transport = async (req) => { f.sent.push(`${req.method} ${req.path.split('?')[0]}`); return send(req); };
|
||||
let revoker = null;
|
||||
const gate = copyLaunchGate({ credentials, writer, configDir: root, rolesDir, snapshot, transport, ca: api.ca, timeoutMs: 3000, now: () => new Date(f.clock),
|
||||
onLoss: (business, instance, cause) => revoker?.start(business, instance, { cause, actor: 'bus' }) });
|
||||
revoker = copyRevoker({ writer, gate, credentials, hooks: f.hooks, stopRuns: (...a) => f.stopRuns(...a),
|
||||
remote: giteaCopyRemote({ grant, read: (x) => writer.read(x).document, incarnation: (x) => readOrgEntrySync(root, x)?.incarnation ?? null }), definitions: () => snapshot.definitions ?? {} });
|
||||
let promoted = snapshot;
|
||||
const business = () => {
|
||||
const d = loadWithCopies(id, { dir: root, rolesDir, snapshot: promoted });
|
||||
return { ...d, roles: Object.fromEntries(Object.entries(d.roles).map(([n, e]) => [n, { ...d.definitions?.[e.definition], ...e }])) };
|
||||
};
|
||||
const issueGrant = (...a) => copyGrant({ credentials, writer, copies: snapshot, organizations: () => [id], ca: api.ca, timeoutMs: 3000 })(...a);
|
||||
const b = new Broker({ store, businesses: { [id]: business() }, copyLaunch: gate, copyRevoker: revoker,
|
||||
hires: { ...service, apply: async (...args) => { const r = await service.apply(...args); b.reloadBusiness(id, business()); return r; } },
|
||||
credentialVerbs: () => { throw new BusError('credential-unknown'); }, copyGrant: (...a) => issueGrant(...a) });
|
||||
Object.assign(f, { store, credentials, gate, revoker, b, snapshot, business, reload: () => b.reloadBusiness(id, business()) });
|
||||
f.owner = b.bindConsole({ business: id, human: 'jason', subject: SUBJECT });
|
||||
f.human = b.bindHuman({ business: id, human: 'jason', via: 'cli', outsideAgent: true });
|
||||
f.boot = async () => {
|
||||
await gate.boot(id);
|
||||
promoted = promoteCopies(snapshot, gate.bound());
|
||||
b.reloadBusiness(id, business());
|
||||
};
|
||||
return f;
|
||||
};
|
||||
t.after(() => { try { f.store?.close(); } catch {} try { f.credentials?.close(); } catch {} });
|
||||
f.bind = async (hire = { level: 'root', name }) => {
|
||||
await f.call(f.owner, 'hire.start.console', { op: 'change', ...hire, reason: 'Bind the copy', binding: { services } });
|
||||
const r = f.rec();
|
||||
f.login = copyBotLogin(r.orgIncarnation, r.copyId, 'gitea');
|
||||
f.bot = api.addUser(f.login).user;
|
||||
repo.collaborators.push(f.login); repo.permissions[f.login] = perm;
|
||||
f.token = api.addToken(f.login, `t${api.issued.length}`, services.gitea.scopes);
|
||||
await f.call(f.human, 'credentials.issue', { agent: name, service: 'gitea', date: ahead(30), token: f.token.sha1 });
|
||||
};
|
||||
f.launch = async (run) => {
|
||||
f.project ??= (await f.call(f.human, 'project.create', { name: 'stack work', repo: 'stack' })).id;
|
||||
const thread = (await f.call(f.human, 'thread.create', { project: f.project, name: run, instance: name })).id;
|
||||
const revision = writer.read(id).revision;
|
||||
const { authority } = await f.gate.prepare(id, name, revision);
|
||||
const cap = f.b.bindLaunch({ business: id, role: name, run, harness: 'pi', thread, copyAuthority: authority, revision });
|
||||
f.runs.push(run);
|
||||
return { run, thread, authority, cap };
|
||||
};
|
||||
f.use = (cap, request = f.GET) => Promise.resolve().then(() => f.gate.call(f.b.copySession(cap), request));
|
||||
// The launcher's prepare at the file's revision (none when the file is gone: a deleted organization).
|
||||
f.prepare = () => Promise.resolve().then(() => {
|
||||
let revision;
|
||||
try { revision = writer.read(id).revision; } catch { revision = undefined; }
|
||||
return f.gate.prepare(id, name, revision);
|
||||
});
|
||||
// The next request matching `match` waits at the fake server until release() lets it through.
|
||||
f.hold = (match) => {
|
||||
let release, arrived;
|
||||
const go = new Promise((r) => { release = r; });
|
||||
const seen = new Promise((r) => { arrived = r; });
|
||||
const unrule = api.rule((e, body) => match(e, body), async () => { unrule(); arrived(); await go; return { pass: true }; });
|
||||
return { release: () => release(), arrived: seen };
|
||||
};
|
||||
f.start();
|
||||
return f;
|
||||
}
|
||||
|
||||
// A bound, booted copy with one live run: what every lifecycle test starts from.
|
||||
async function live(t, w, opts) {
|
||||
const f = await org(t, w, opts);
|
||||
await f.bind();
|
||||
assert.equal(f.rec().state, 'bound');
|
||||
f.start();
|
||||
await f.boot();
|
||||
assert.deepEqual(Object.keys(f.gate.bound()), [f.name]);
|
||||
const r = await f.launch('run-1');
|
||||
assert.equal((await f.use(r.cap)).status, 200);
|
||||
return { f, r };
|
||||
}
|
||||
const tombstoned = (f) => f.rec()?.state === 'unbound' && f.rec()?.revoke?.done === true;
|
||||
// The end state of every completed revoke: the run is gone, the slot is gone, the token is gone at
|
||||
// the service (or the record says why it was not attempted there), and the copy refuses with
|
||||
// nothing sent.
|
||||
async function assertRevoked(f, r, { cause, code, remote = 'revoked' }) {
|
||||
await until(() => tombstoned(f), `${cause} tombstone`);
|
||||
const rec = f.rec();
|
||||
assert.equal(rec.revoke.cause, cause);
|
||||
assert.deepEqual(rec.revoke.remote, { gitea: remote });
|
||||
assert.deepEqual(rec.services, {});
|
||||
assert.ok(!existsSync(f.slot), 'slot removed');
|
||||
if (remote === 'revoked') assert.deepEqual(f.bot.tokens, [], 'token deleted at the service');
|
||||
if (r) {
|
||||
assert.ok(f.killed().includes(r.run), 'the run was told to stop');
|
||||
assert.throws(() => f.b.copySession(r.cap), (e) => e instanceof BusError);
|
||||
}
|
||||
const sent = f.sent.length;
|
||||
await assert.rejects(f.prepare(), refuses(code));
|
||||
assert.equal(f.sent.length, sent, 'a refused prepare sends nothing');
|
||||
assert.ok(!JSON.stringify(rec).includes(f.token.sha1));
|
||||
}
|
||||
|
||||
test('CB4 every revoke refusal code is in the refusal table with a rail and a next step naming no path', () => {
|
||||
for (const code of CODES) {
|
||||
const entry = REFUSALS[code];
|
||||
assert.ok(entry, code);
|
||||
assert.match(entry.rail, /^R\d+[/:]/, code);
|
||||
assert.ok(entry.nextStep.length > 20, code);
|
||||
assert.ok(!/(^|\s)(\/|~\/)\S/.test(entry.nextStep), `${code} names a path`);
|
||||
}
|
||||
assert.deepEqual(REVOKE_POINTS.length, 10);
|
||||
});
|
||||
|
||||
for (const verb of ['credentials.revoke', 'credentials.delete']) {
|
||||
test(`CB4 the owner's ${verb} of a bound copy: closed, drained, every run stopped and acknowledged, slot gone, token revoked at Gitea, then tombstone`, async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
const r2 = await f.launch('run-2');
|
||||
// A call in flight when the revoke starts is aborted and settles before the stop.
|
||||
const held = f.hold((e) => e.path === f.GET.path);
|
||||
let settledBeforeRelease = false, released = false;
|
||||
const inflight = f.use(r2.cap).then((x) => ({ ok: x }), (e) => ({ err: e })).finally(() => { settledBeforeRelease = !released; });
|
||||
await held.arrived;
|
||||
assert.equal(f.gate.inflight(f.id, f.name), 1);
|
||||
const args = verb === 'credentials.revoke' ? { agent: f.name, service: 'gitea', reason: 'Owner revoke' } : { agent: f.name, reason: 'Owner delete' };
|
||||
const out = await f.call(f.human, verb, args);
|
||||
// The drain aborted the call and waited for it: it settled before the server would ever answer.
|
||||
await new Promise((r) => setImmediate(r));
|
||||
assert.equal(f.gate.inflight(f.id, f.name), 0, 'drained before the revoke answered');
|
||||
assert.equal(settledBeforeRelease, true, 'the in-flight call settled before the server released it');
|
||||
released = true;
|
||||
held.release();
|
||||
assert.equal(out.state, 'unbound');
|
||||
assert.deepEqual(out.remote, { gitea: 'revoked' });
|
||||
const settled = await inflight;
|
||||
assert.ok(settled.err || settled.ok?.status === 0, 'the in-flight call was aborted');
|
||||
assert.equal(f.gate.inflight(f.id, f.name), 0);
|
||||
// Both runs got one kill end, and each acknowledged before the tombstone was written.
|
||||
assert.deepEqual(f.killed().sort(), ['run-1', 'run-2']);
|
||||
assert.equal(f.stops.length, 1);
|
||||
assert.equal(f.stops[0].op, f.rec().revoke.id);
|
||||
await assertRevoked(f, r, { cause: 'revoke', code: 'credential-revoked' });
|
||||
assert.throws(() => f.b.copySession(r2.cap), (e) => e instanceof BusError);
|
||||
// A second revoke is a no-op answer, never a second remote call.
|
||||
const deletes = w.api.calls().filter((c) => c.startsWith('DELETE')).length;
|
||||
await assert.rejects(f.call(f.human, verb, args), (e) => e instanceof BusError);
|
||||
assert.equal(w.api.calls().filter((c) => c.startsWith('DELETE')).length, deletes);
|
||||
});
|
||||
}
|
||||
|
||||
test('CB4 only the owner revokes: the tester, a reader, a member with org.hire and the copy itself refuse, and nothing changes', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
const member = f.b.bindHireConsole({ business: f.id, actor: 'pat', subject: { provider: 'test', iss: 'https://issuer.test', sub: 'pat' }, rights: ['org.hire', 'decide'] });
|
||||
const callers = { tester: f.b.bindTester({ business: f.id }), reader: f.b.bindReader({ business: f.id }), member, copy: r.cap };
|
||||
for (const [who, cap] of Object.entries(callers))
|
||||
for (const [verb, args] of [['credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Not mine' }], ['credentials.delete', { agent: f.name, reason: 'Not mine' }]])
|
||||
await assert.rejects(f.call(cap, verb, args), (e) => e instanceof BusError, `${who} ${verb}`);
|
||||
assert.equal(f.rec().state, 'bound');
|
||||
assert.equal(f.rec().revoke, undefined);
|
||||
assert.equal(f.gate.closed(f.id, f.name), false);
|
||||
assert.equal((await f.use(r.cap)).status, 200);
|
||||
assert.equal(f.bot.tokens.length, 1);
|
||||
assert.deepEqual(f.killed(), []);
|
||||
});
|
||||
|
||||
test('CB4 unbind through the owner\'s decision revokes the copy: credential-unbound, token gone', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
await f.call(f.owner, 'hire.start.console', { op: 'change', level: 'root', name: f.name, reason: 'Unbind the copy', binding: { services: null } });
|
||||
await assertRevoked(f, r, { cause: 'unbind', code: 'credential-unbound' });
|
||||
});
|
||||
|
||||
test('CB4 slot removal: the next call refuses credential-unavailable and the binding is revoked', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
unlinkSync(f.slot);
|
||||
await assert.rejects(f.use(r.cap), refuses('credential-unavailable'));
|
||||
await assertRevoked(f, r, { cause: 'unavailable', code: 'credential-unavailable' });
|
||||
});
|
||||
|
||||
test('CB4 expiry: past rotateBy the call refuses credential-expired and the binding is revoked', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
f.advance(31);
|
||||
const before = f.repoCalls();
|
||||
await assert.rejects(f.use(r.cap), refuses('credential-expired'));
|
||||
assert.equal(f.repoCalls(), before);
|
||||
await assertRevoked(f, r, { cause: 'expired', code: 'credential-expired' });
|
||||
});
|
||||
|
||||
test('CB4 expiry found by the sweep with no call at all revokes too', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
f.advance(31);
|
||||
f.revoker.sweep(f.id);
|
||||
await assertRevoked(f, r, { cause: 'expired', code: 'credential-expired' });
|
||||
});
|
||||
|
||||
test('CB4 role change: the definition the binding pinned changed, the call refuses and the binding is revoked', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
const role = join(f.rolesDir, 'research.json');
|
||||
const d = JSON.parse(readFileSync(role, 'utf8'));
|
||||
writeFileSync(role, JSON.stringify({ ...d, title: `${d.title} changed` }, null, 2) + '\n');
|
||||
await assert.rejects(f.use(r.cap), refuses('credential-role-changed'));
|
||||
await assertRevoked(f, r, { cause: 'role-changed', code: 'credential-role-changed' });
|
||||
});
|
||||
|
||||
test('CB4 fire: a copy whose root agent is gone at the next start (the fire landed, its revoke never began) is revoked at boot', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
// The root agent was fired and the bus died before the revoke published anything.
|
||||
f.start({});
|
||||
await f.boot();
|
||||
assert.deepEqual(f.gate.bound(), {});
|
||||
f.revoker.sweep(f.id);
|
||||
await until(() => tombstoned(f), 'retire tombstone');
|
||||
assert.equal(f.rec().revoke.cause, 'retire');
|
||||
assert.deepEqual(f.rec().revoke.remote, { gitea: 'revoked' });
|
||||
assert.ok(!existsSync(f.slot));
|
||||
assert.deepEqual(f.bot.tokens, []);
|
||||
await assert.rejects(f.prepare(), refuses('copy-unavailable'));
|
||||
});
|
||||
|
||||
test('CB4 fire through the owner\'s decision: the fire is written, the copy\'s runs are stopped and its binding is revoked after the write', async (t) => {
|
||||
const w = await world(t);
|
||||
const f = await org(t, w, { name: 'rev.stack', project: { rev: { definition: 'research' } } });
|
||||
f.rootAgents = {};
|
||||
f.start();
|
||||
await f.bind({ level: 'project', project: 'stack', name: 'rev' });
|
||||
f.start();
|
||||
await f.boot();
|
||||
const r = await f.launch('run-1');
|
||||
assert.equal((await f.use(r.cap)).status, 200);
|
||||
// The fire decision itself must not be refused decision-stale by a revoke written under it.
|
||||
const out = await f.call(f.owner, 'hire.start.console', { op: 'fire', level: 'project', project: 'stack', name: 'rev', reason: 'Fire the copy' });
|
||||
assert.ok(out, 'the fire applied');
|
||||
assert.equal(JSON.parse(readFileSync(f.file, 'utf8')).projects.stack.agents?.rev, undefined, 'the fire is written');
|
||||
assert.ok(f.killed().includes(r.run), 'the run was told to stop before the write');
|
||||
await assertRevoked(f, r, { cause: 'retire', code: 'copy-unavailable' });
|
||||
});
|
||||
|
||||
test('CB4 a project copy removed from its project while the bus runs (no reload): the next call refuses and the binding is revoked', async (t) => {
|
||||
const w = await world(t);
|
||||
const f = await org(t, w, { name: 'rev.stack', project: { rev: { definition: 'research' } } });
|
||||
f.rootAgents = {};
|
||||
f.start();
|
||||
await f.bind({ level: 'project', project: 'stack', name: 'rev' });
|
||||
assert.equal(f.rec().state, 'bound');
|
||||
f.start();
|
||||
await f.boot();
|
||||
assert.deepEqual(Object.keys(f.gate.bound()), ['rev.stack']);
|
||||
const r = await f.launch('run-1');
|
||||
assert.equal((await f.use(r.cap)).status, 200);
|
||||
// The agent leaves the project through the writer alone: the broker never reloads, so only the
|
||||
// gate's live check sees the copy gone.
|
||||
f.writer.setProjectAgent(f.id, { revision: f.writer.read(f.id).revision, project: 'stack', name: 'rev', entry: null }, 'jason');
|
||||
await assert.rejects(f.use(r.cap), refuses('copy-unavailable'));
|
||||
await assertRevoked(f, r, { cause: 'retire', code: 'copy-unavailable' });
|
||||
});
|
||||
|
||||
test('CB4 the finish keeps a remote revoke an earlier pass recorded: a later absent never replaces it', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
f.ack = false; f.stopMs = 200;
|
||||
await assert.rejects(f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' }), refuses('credential-revoke-failed'));
|
||||
const op = f.rec().revoke.id;
|
||||
assert.deepEqual(f.rec().revoke.remote, { gitea: 'revoked' });
|
||||
// A later pass that saw the token already gone reports absent; the writer keeps revoked.
|
||||
const opts = { actor: 'bus', owner: true, definitions: f.snapshot.definitions ?? {} };
|
||||
f.writer.revokeCopyBinding(f.id, { phase: 'finish', instance: f.name, opId: op, steps: { stop: 'failed', slot: 'absent', remote: { gitea: 'absent' } } }, opts);
|
||||
assert.deepEqual(f.rec().revoke.remote, { gitea: 'revoked' });
|
||||
f.writer.revokeCopyBinding(f.id, { phase: 'finish', instance: f.name, opId: op, steps: { stop: 'confirmed', slot: 'absent', remote: { gitea: 'absent' } } }, opts);
|
||||
assert.ok(tombstoned(f));
|
||||
assert.deepEqual(f.rec().revoke.remote, { gitea: 'revoked' });
|
||||
});
|
||||
|
||||
test('CB4 name collision: a project agent now claims the copy\'s instance name; the call refuses copy-refused and the binding is revoked', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w, { name: 'rev.stack' });
|
||||
f.writer.setProjectAgent(f.id, { revision: f.writer.read(f.id).revision, project: 'stack', name: 'rev', entry: { definition: 'research' } }, 'jason');
|
||||
await assert.rejects(f.use(r.cap), refuses('copy-refused'));
|
||||
await assertRevoked(f, r, { cause: 'collision', code: 'copy-refused' });
|
||||
});
|
||||
|
||||
test('CB4 shadowing: a named instance over a former copy\'s slot revokes the copy; nothing issues to the name until the tombstone', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
// Hold the stop so the revoke stays pending while the named instance asks for its slot.
|
||||
f.ack = false; f.stopMs = 60000;
|
||||
f.edit((d) => { d.roles[f.name] = { definition: 'research', credentials: { gitea: d.roles.coder.credentials.gitea } }; });
|
||||
f.revoker.sweep(f.id);
|
||||
await until(() => f.rec().state === 'revoking', 'revoking record');
|
||||
assert.equal(f.rec().revoke.cause, 'shadowed');
|
||||
assert.ok(f.revoker.pending(f.id, f.name));
|
||||
f.reload();
|
||||
await assert.rejects(f.call(f.human, 'credentials.issue', { agent: f.name, service: 'gitea', date: ahead(30), token: 'a'.repeat(40) }), refuses('credential-revoking'));
|
||||
await assert.rejects(f.use(r.cap), (e) => e instanceof BusError);
|
||||
// The launcher acknowledges the kill: the revoke finishes and the name is free.
|
||||
for (const run of f.killed()) f.b.endLaunch({ business: f.id, run, reason: 'killed' });
|
||||
await until(() => tombstoned(f), 'shadowed tombstone');
|
||||
assert.deepEqual(f.rec().revoke.remote, { gitea: 'revoked' });
|
||||
assert.ok(!existsSync(f.slot));
|
||||
assert.deepEqual(f.bot.tokens, []);
|
||||
assert.equal(f.revoker.pending(f.id, f.name), false);
|
||||
// The issue now takes the named instance's ordinary path (this fixture's verbs answer unknown).
|
||||
await assert.rejects(f.call(f.human, 'credentials.issue', { agent: f.name, service: 'gitea', date: ahead(30), token: 'a'.repeat(40) }), refuses('credential-unknown'));
|
||||
});
|
||||
|
||||
test('CB4 an unacknowledged stop keeps the copy fenced and the record revoking; resume after the ack finishes the same op', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
f.ack = false; f.stopMs = 200;
|
||||
await assert.rejects(f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' }), refuses('credential-revoke-failed'));
|
||||
const rec = f.rec();
|
||||
assert.equal(rec.state, 'revoking');
|
||||
assert.deepEqual(rec.revoke.failed, ['stop']);
|
||||
// The remote step ran (the gate was closed and drained) and its definite outcome is kept.
|
||||
assert.deepEqual(rec.revoke.remote, { gitea: 'revoked' });
|
||||
assert.deepEqual(f.bot.tokens, []);
|
||||
const op = rec.revoke.id;
|
||||
const deletes = w.api.calls().filter((c) => c.startsWith('DELETE')).length;
|
||||
// Fenced: no call, no prepare, no launch.
|
||||
await assert.rejects(f.prepare(), refuses('credential-revoke-failed'));
|
||||
await assert.rejects(f.use(r.cap), (e) => e instanceof BusError);
|
||||
// The launcher's exit arrives; the next pass finishes the op.
|
||||
f.b.endLaunch({ business: f.id, run: r.run, reason: 'killed' });
|
||||
f.ack = true;
|
||||
await f.revoker.resume(f.id);
|
||||
assert.ok(tombstoned(f));
|
||||
assert.equal(f.rec().revoke.id, op);
|
||||
assert.equal(w.api.calls().filter((c) => c.startsWith('DELETE')).length, deletes, 'a revoked grant is never called again');
|
||||
await assertRevoked(f, null, { cause: 'revoke', code: 'credential-revoked' });
|
||||
});
|
||||
|
||||
test('CB4 a remote revoke that fails keeps the copy fenced; the slot is gone; a later pass revokes it', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
const unrule = w.api.rule((e) => e.method === 'DELETE' && e.path.includes('/tokens/'), { status: 500, json: { message: 'down' } });
|
||||
await assert.rejects(f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' }), refuses('credential-revoke-failed'));
|
||||
assert.equal(f.rec().state, 'revoking');
|
||||
assert.deepEqual(f.rec().revoke.failed, ['remote']);
|
||||
assert.ok(!existsSync(f.slot));
|
||||
assert.equal(f.bot.tokens.length, 1, 'never claimed revoked');
|
||||
await assert.rejects(f.prepare(), refuses('credential-revoke-failed'));
|
||||
await assert.rejects(f.use(r.cap), (e) => e instanceof BusError);
|
||||
unrule();
|
||||
await f.revoker.resume(f.id);
|
||||
await assertRevoked(f, null, { cause: 'revoke', code: 'credential-revoked' });
|
||||
});
|
||||
|
||||
test('CB4 an organization without a managed admin slot records not-managed and never claims the token revoked', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w, { admin: false });
|
||||
const out = await f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' });
|
||||
assert.deepEqual(out.remote, { gitea: 'not-managed' });
|
||||
await assertRevoked(f, r, { cause: 'revoke', code: 'credential-revoked', remote: 'not-managed' });
|
||||
assert.equal(f.bot.tokens.length, 1);
|
||||
});
|
||||
|
||||
test('CB4 an expired admin slot is a failed remote step, not not-managed: the copy stays fenced', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w, { adminRotateBy: '2020-01-01' });
|
||||
await assert.rejects(f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' }), refuses('credential-revoke-failed'));
|
||||
assert.deepEqual(f.rec().revoke.failed, ['remote']);
|
||||
assert.equal(f.bot.tokens.length, 1);
|
||||
});
|
||||
|
||||
// A crash at each point of the revoke, then a restart of the bus on the same files: the copy never
|
||||
// gets its authority back, the pending op is never lost, and resume finishes it under the same id.
|
||||
for (const point of REVOKE_POINTS) {
|
||||
test(`CB4 crash at ${point}, then restart: no resurrected authority, no lost op`, async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
let fired = false;
|
||||
f.hooks[point] = () => { if (!fired) { fired = true; throw new Error(`crash at ${point}`); } };
|
||||
await assert.rejects(f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' }));
|
||||
assert.ok(fired);
|
||||
const crashed = f.rec();
|
||||
// Nothing the crashed process holds counts: a new process starts on the files.
|
||||
f.start();
|
||||
await f.boot();
|
||||
if (point === 'beforePublication') {
|
||||
// Nothing was published: the binding is as it was, and the owner's verb answered an error.
|
||||
assert.equal(crashed.state, 'bound');
|
||||
assert.deepEqual(Object.keys(f.gate.bound()), [f.name]);
|
||||
const again = await f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' });
|
||||
assert.equal(again.state, 'unbound');
|
||||
await assertRevoked(f, null, { cause: 'revoke', code: 'credential-revoked' });
|
||||
return;
|
||||
}
|
||||
assert.ok(['revoking', 'unbound'].includes(crashed.state), crashed.state);
|
||||
const op = crashed.revoke.id;
|
||||
assert.match(op, /\S/);
|
||||
// No resurrected authority: the boot proves nothing, prepare and launch refuse, nothing is sent.
|
||||
assert.deepEqual(f.gate.bound(), {});
|
||||
const sent = f.sent.length;
|
||||
await assert.rejects(f.prepare(), (e) => ['credential-revoking', 'credential-revoked', 'credential-revoke-failed'].includes(e.code));
|
||||
assert.equal(f.sent.length, sent);
|
||||
assert.throws(() => f.b.copySession(r.cap), (e) => e instanceof BusError);
|
||||
// No lost op: resume finishes it under the same id.
|
||||
await f.revoker.resume(f.id);
|
||||
assert.ok(tombstoned(f), JSON.stringify(f.rec()));
|
||||
assert.equal(f.rec().revoke.id, op);
|
||||
// The token went at the service before the crash or at resume; either way it is gone now.
|
||||
assert.ok(['revoked', 'absent'].includes(f.rec().revoke.remote.gitea), f.rec().revoke.remote.gitea);
|
||||
assert.deepEqual(f.bot.tokens, []);
|
||||
assert.ok(!existsSync(f.slot));
|
||||
await assert.rejects(f.prepare(), refuses('credential-revoked'));
|
||||
// A second restart finds nothing pending and changes nothing.
|
||||
const done = readFileSync(f.file, 'utf8');
|
||||
f.start();
|
||||
await f.boot();
|
||||
assert.deepEqual(await f.revoker.resume(f.id), []);
|
||||
assert.equal(readFileSync(f.file, 'utf8'), done);
|
||||
});
|
||||
}
|
||||
|
||||
test('CB4 archive with no remote adapter: slot gone, the copy stays revoking and fenced, the receipt claims no revoke, and the bus finishes it after restore', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
const revision = (await listOrgs(f.ctx)).find((o) => o.id === f.id).revision;
|
||||
const out = await orgService.archiveOrg(f.ctx, f.id, { ifRevision: revision, busStop: async () => ({ stopped: true, reason: 'stopped' }) });
|
||||
assert.deepEqual(out.copies, [{ agent: f.name, state: 'revoking', slot: 'removed', remote: { gitea: 'not-attempted' } }]);
|
||||
assert.equal(f.rec().state, 'revoking');
|
||||
assert.equal(f.rec().revoke.cause, 'archive');
|
||||
assert.deepEqual(f.rec().revoke.failed, ['remote']);
|
||||
assert.ok(!existsSync(f.slot));
|
||||
// Not attempted at the service: the token is still there and nothing says otherwise.
|
||||
assert.equal(f.bot.tokens.length, 1);
|
||||
const log = readFileSync(join(w.data, 'org-lifecycle.log'), 'utf8').trim().split('\n').map(JSON.parse);
|
||||
assert.deepEqual(log.at(-1).copies, out.copies);
|
||||
// A bus still holding the old gate answers the organization's state.
|
||||
await assert.rejects(f.use(r.cap), (e) => ['org-archived', 'run-ended'].includes(e.code));
|
||||
await assert.rejects(f.prepare(), refuses('org-archived'));
|
||||
await orgService.restoreOrg(f.ctx, f.id, { ifRevision: out.revision });
|
||||
f.start();
|
||||
await f.boot();
|
||||
assert.deepEqual(f.gate.bound(), {});
|
||||
await assert.rejects(f.prepare(), (e) => ['credential-revoking', 'credential-revoke-failed'].includes(e.code));
|
||||
// The bus's resume at its start runs the remote step through the managed adapter.
|
||||
await f.revoker.resume(f.id);
|
||||
await assertRevoked(f, null, { cause: 'archive', code: 'credential-unbound' });
|
||||
});
|
||||
|
||||
test('CB4 archive with the managed adapter wired revokes the token at Gitea', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
const revision = (await listOrgs(f.ctx)).find((o) => o.id === f.id).revision;
|
||||
// The adapter the CLI and Console wire (org-bus.mjs copyRemoteFor), on the fake Gitea.
|
||||
const copyRemote = managedCopyRemote({ configDir: f.root, dataRoot: w.data, transport: send, ca: w.api.ca });
|
||||
const out = await orgService.archiveOrg(f.ctx, f.id, { ifRevision: revision, busStop: async () => ({ stopped: true, reason: 'stopped' }), copyRemote });
|
||||
assert.deepEqual(out.copies, [{ agent: f.name, state: 'unbound', slot: 'removed', remote: { gitea: 'revoked' } }]);
|
||||
assert.ok(tombstoned(f));
|
||||
assert.deepEqual(f.bot.tokens, []);
|
||||
});
|
||||
|
||||
test('CB4 archive refuses when a slot parent is not the stack user\'s private directory, and the slot stays', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
chmodSync(dirname(f.slot), 0o755);
|
||||
const revision = (await listOrgs(f.ctx)).find((o) => o.id === f.id).revision;
|
||||
await assert.rejects(orgService.archiveOrg(f.ctx, f.id, { ifRevision: revision, busStop: async () => ({ stopped: true, reason: 'stopped' }) }), refuses('credential-revoke-failed'));
|
||||
assert.ok(existsSync(f.slot));
|
||||
assert.equal(f.rec().state, 'revoking');
|
||||
});
|
||||
|
||||
test('CB4 archive rerun after a crash joins the pending op', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
// The archive crashed after its first write.
|
||||
const begun = f.writer.revokeCopyBinding(f.id, { phase: 'begin', instance: f.name, cause: 'archive' }, { actor: 'bus', owner: true });
|
||||
assert.equal(begun.state, 'revoking');
|
||||
const revision = (await listOrgs(f.ctx)).find((o) => o.id === f.id).revision;
|
||||
const copyRemote = managedCopyRemote({ configDir: f.root, dataRoot: w.data, transport: send, ca: w.api.ca });
|
||||
const out = await orgService.archiveOrg(f.ctx, f.id, { ifRevision: revision, busStop: async () => ({ stopped: true, reason: 'stopped' }), copyRemote });
|
||||
assert.equal(out.copies[0].state, 'unbound');
|
||||
assert.equal(f.rec().revoke.id, begun.op);
|
||||
assert.ok(!existsSync(f.slot));
|
||||
});
|
||||
|
||||
test('CB4 delete quarantines the copy slot and its receipt never claims a remote revoke; a recreated organization never resolves the old copy', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w);
|
||||
const revision = (await listOrgs(f.ctx)).find((o) => o.id === f.id).revision;
|
||||
const out = await orgService.deleteOrg(f.ctx, f.id, { confirm: f.id, ifRevision: revision, busStop: async () => ({ stopped: true, reason: 'stopped' }) });
|
||||
assert.deepEqual(out.copies, [{ agent: f.name, service: 'gitea', slot: 'quarantined', remote: 'not-attempted' }]);
|
||||
assert.match(out.note, /only where copies lists a copy with remote revoked/);
|
||||
assert.ok(!existsSync(f.slot));
|
||||
assert.equal(f.bot.tokens.length, 1);
|
||||
await assert.rejects(f.use(r.cap), (e) => ['org-unavailable', 'run-ended'].includes(e.code));
|
||||
await assert.rejects(f.prepare(), (e) => ['org-unavailable', 'organization-unreadable', 'credential-unavailable'].includes(e.code));
|
||||
// Recreated under the same id: a new incarnation. The old gate still refuses.
|
||||
const doc = { ...businessDoc(w.root, f.id), plugins: { harness: ['pi'] } };
|
||||
writeJson(f.file, doc);
|
||||
await reserveOrg(f.ctx, f.id); await activateOrg(f.ctx, f.id);
|
||||
await assert.rejects(f.prepare(), (e) => ['org-unavailable', 'copy-unavailable', 'credential-unbound'].includes(e.code));
|
||||
});
|
||||
|
||||
test('CB4 delete with the managed adapter revokes each copy token at Gitea and says so in the receipt', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
const revision = (await listOrgs(f.ctx)).find((o) => o.id === f.id).revision;
|
||||
const copyRemote = managedCopyRemote({ configDir: f.root, dataRoot: w.data, transport: send, ca: w.api.ca });
|
||||
const out = await orgService.deleteOrg(f.ctx, f.id, { confirm: f.id, ifRevision: revision, busStop: async () => ({ stopped: true, reason: 'stopped' }), copyRemote });
|
||||
assert.deepEqual(out.copies, [{ agent: f.name, service: 'gitea', slot: 'quarantined', remote: 'revoked' }]);
|
||||
assert.equal(out.copiesUnreadable, undefined);
|
||||
assert.deepEqual(f.bot.tokens, []);
|
||||
});
|
||||
|
||||
test('CB4 delete with an unreadable organization file says so, never an empty copy list', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
const revision = (await listOrgs(f.ctx)).find((o) => o.id === f.id).revision;
|
||||
// The file fails the private read when the copies step reads it (busStop runs just before).
|
||||
const busStop = async () => { chmodSync(f.file, 0o644); return { stopped: true, reason: 'stopped' }; };
|
||||
const out = await orgService.deleteOrg(f.ctx, f.id, { confirm: f.id, ifRevision: revision, busStop });
|
||||
assert.equal(out.copiesUnreadable, true);
|
||||
assert.deepEqual(out.copies, []);
|
||||
assert.match(out.note, /copiesUnreadable/);
|
||||
});
|
||||
|
||||
test('CB4 an owner update cannot change the repository of a project a copy binding still grants', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
const { revision } = (await listOrgs(f.ctx)).find((o) => o.id === f.id);
|
||||
const doc = { ...JSON.parse(readFileSync(f.file, 'utf8')), human: f.ctx.principal.sub };
|
||||
const moved = structuredClone(doc); moved.projects.stack.remote = `${w.api.url}/elsewhere/stack`;
|
||||
await assert.rejects(orgService.updateOrg(f.ctx, f.id, moved, { ifRevision: revision }), refuses('project-repo-bound'));
|
||||
// Removed (with another project left, so the document itself is valid).
|
||||
const dropped = structuredClone(doc);
|
||||
dropped.projects.other = { ...structuredClone(doc.projects.stack), root: join(w.data, 'org-runtime', f.id, 'projects', 'other'), remote: `${w.api.url}/owner/other` };
|
||||
delete dropped.projects.other.agents; delete dropped.projects.stack;
|
||||
await assert.rejects(orgService.updateOrg(f.ctx, f.id, dropped, { ifRevision: revision }), refuses('project-repo-bound'));
|
||||
// An update that leaves the bound project's repository as it is still goes through.
|
||||
const other = structuredClone(doc); other.vars = { ...other.vars, 'bus.tester': false };
|
||||
await orgService.updateOrg(f.ctx, f.id, other, { ifRevision: revision });
|
||||
});
|
||||
|
||||
test('CB4 the owner\'s project.remote.set cannot change the repository of a project a copy binding still grants; the revoke then finishes at Gitea', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f, r } = await live(t, w, { gitOnly: true });
|
||||
const { revision } = f.writer.read(f.id);
|
||||
assert.throws(() => f.writer.setProjectRemote(f.id, { revision, project: 'stack', remote: `${w.api.url}/owner/elsewhere` }, 'jason'), refuses('project-repo-bound'));
|
||||
assert.equal(f.writer.read(f.id).revision, revision);
|
||||
const out = await f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' });
|
||||
assert.equal(out.state, 'unbound');
|
||||
await assertRevoked(f, r, { cause: 'revoke', code: 'credential-revoked' });
|
||||
});
|
||||
|
||||
test('CB4 a remote step whose bound server cannot be read, or whose record names another incarnation, fails and the copy stays fenced', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f } = await live(t, w);
|
||||
const rec = f.rec();
|
||||
const base = { grant: f.grant, read: (x) => f.writer.read(x).document };
|
||||
// The bound project is not in the file any more: no origin to revoke against, never not-managed.
|
||||
const gone = giteaCopyRemote({ ...base, read: (x) => { const d = structuredClone(f.writer.read(x).document); delete d.projects.stack; return d; } });
|
||||
assert.equal(await gone(f.id, f.name, rec, 'gitea'), 'failed');
|
||||
// Another incarnation, or none: the admin slot is never asked to delete.
|
||||
const sent = f.api.calls().length;
|
||||
for (const incarnation of [() => 'f'.repeat(32), () => null, () => { throw new Error('unreadable'); }])
|
||||
assert.equal(await giteaCopyRemote({ ...base, incarnation })(f.id, f.name, rec, 'gitea'), 'failed');
|
||||
assert.equal(f.bot.tokens.length, 1);
|
||||
assert.equal(f.api.calls().length, sent, 'no request reached the server');
|
||||
// Through the revoker: the record stays revoking with the remote step failed, and fenced.
|
||||
const revoker = copyRevoker({ writer: f.writer, gate: f.gate, credentials: f.credentials, stopRuns: (...a) => f.stopRuns(...a),
|
||||
remote: gone, definitions: () => f.snapshot.definitions ?? {} });
|
||||
const done = await revoker.run(f.id, f.name, { cause: 'revoke', actor: 'jason', owner: true });
|
||||
assert.equal(done.state, 'revoking');
|
||||
assert.deepEqual(f.rec().revoke.failed, ['remote']);
|
||||
assert.equal(f.bot.tokens.length, 1);
|
||||
await assert.rejects(f.prepare(), refuses('credential-revoke-failed'));
|
||||
});
|
||||
|
||||
// Build review P3-4: when the begin write refuses before any change, only an owner's verb on a copy
|
||||
// that was open reopens it (to its parked proof); a loss the bus found, or a copy already closed,
|
||||
// stays closed.
|
||||
test('CB4 a refused begin reopens the gate only for an owner verb on an open copy; a loss the bus found stays closed', async () => {
|
||||
const run = async ({ cause, closed }) => {
|
||||
const calls = [];
|
||||
const gate = { closed: () => closed, close: (b, i, c) => calls.push(['close', i, c ?? null]), open: (b, i, parked) => calls.push(['open', i, parked === true]), drain: async () => {} };
|
||||
const writer = { read: () => ({ document: { copyBindings: {} } }), revokeCopyBinding: () => { throw new BusError('org-mismatch'); } };
|
||||
const stopRuns = async () => { throw new Error('stop must not run'); };
|
||||
const revoker = copyRevoker({ writer, gate, credentials: { discardCopy: () => { throw new Error('slot must not go'); } }, stopRuns });
|
||||
await assert.rejects(revoker.run('acme', 'rev', { cause, owner: cause === 'revoke' }), { code: 'org-mismatch' });
|
||||
return calls;
|
||||
};
|
||||
for (const cause of ['expired', 'unavailable', 'retire', 'shadowed', 'upstream'])
|
||||
assert.deepEqual(await run({ cause, closed: false }), [['close', 'rev', null]], cause);
|
||||
assert.deepEqual(await run({ cause: 'revoke', closed: true }), [['close', 'rev', null]]);
|
||||
assert.deepEqual(await run({ cause: 'revoke', closed: false }), [['close', 'rev', null], ['open', 'rev', true]]);
|
||||
});
|
||||
|
||||
test('CB4 resume keeps a failed pass closed with its failure code while it runs again', async () => {
|
||||
const calls = [];
|
||||
const gate = { closed: () => true, close: (b, i, c) => calls.push([i, c ?? null]), open: () => calls.push(['open']), drain: async () => {} };
|
||||
const copyBindings = { rev: { state: 'revoking', revoke: { id: 'op-1', cause: 'expired', at: 'x', failed: ['remote'] } }, two: { state: 'revoking', revoke: { id: 'op-2', cause: 'expired', at: 'x' } } };
|
||||
const writer = { read: () => ({ document: { copyBindings } }), revokeCopyBinding: () => { throw new BusError('org-mismatch'); } };
|
||||
const revoker = copyRevoker({ writer, gate, credentials: { discardCopy: () => 'absent' }, stopRuns: async () => ({ confirmed: true }) });
|
||||
await revoker.resume('acme');
|
||||
assert.deepEqual(calls.slice(0, 2), [['rev', 'credential-revoke-failed'], ['two', null]]);
|
||||
assert.ok(!calls.some((c) => c[0] === 'open'));
|
||||
});
|
||||
|
||||
test('CB4 a second organization launches and calls throughout the first one\'s revoke, archive and delete', async (t) => {
|
||||
const w = await world(t);
|
||||
const { f: demo, r: dr } = await live(t, w);
|
||||
const { f: acme, r: ar } = await live(t, w, { id: 'acme' });
|
||||
const ok = async (label) => {
|
||||
assert.equal((await acme.use(ar.cap)).status, 200, label);
|
||||
const fresh = await acme.launch(`acme-${label}`);
|
||||
assert.equal((await acme.use(fresh.cap)).status, 200, label);
|
||||
};
|
||||
// During the revoke: hold demo's stop and call acme meanwhile.
|
||||
demo.ack = false; demo.stopMs = 60000;
|
||||
const revoke = demo.call(demo.human, 'credentials.revoke', { agent: demo.name, service: 'gitea', reason: 'Owner revoke' });
|
||||
await until(() => demo.rec().state === 'revoking', 'demo revoking');
|
||||
await ok('during-revoke');
|
||||
demo.b.endLaunch({ business: demo.id, run: dr.run, reason: 'killed' });
|
||||
assert.equal((await revoke).state, 'unbound');
|
||||
await ok('after-revoke');
|
||||
// Archive and delete demo.
|
||||
let revision = (await listOrgs(demo.ctx)).find((o) => o.id === 'demo').revision;
|
||||
const archived = await orgService.archiveOrg(demo.ctx, 'demo', { ifRevision: revision, busStop: async () => ({ stopped: true, reason: 'stopped' }) });
|
||||
await ok('after-archive');
|
||||
await orgService.deleteOrg(demo.ctx, 'demo', { confirm: 'demo', ifRevision: archived.revision, busStop: async () => ({ stopped: true, reason: 'stopped' }) });
|
||||
await ok('after-delete');
|
||||
assert.equal(acme.rec().state, 'bound');
|
||||
assert.equal(acme.bot.tokens.length, 1);
|
||||
assert.ok(existsSync(acme.slot));
|
||||
assert.deepEqual(demo.bot.tokens, []);
|
||||
});
|
||||
|
||||
// #1873 CB5 (CB4 review P3-1): the gate finds a shadowing name only in the organization it serves,
|
||||
// and the runtime gives a gate only to a boot that made a copy (cli config.mjs bootConfig passes no
|
||||
// snapshot otherwise). A named instance the owner wrote over the only copy while the bus was down
|
||||
// leaves the next start with no copy and no gate; only the revoker's own sweep then finds the former
|
||||
// copy's record and ends its binding.
|
||||
test('CB5 shadowing in an organization no gate serves: only the revoker\'s own sweep finds the former copy, and it revokes the binding', async (t) => {
|
||||
const w = await world(t);
|
||||
const f = await org(t, w);
|
||||
await f.bind();
|
||||
assert.equal(f.rec().state, 'bound');
|
||||
assert.ok(existsSync(f.slot));
|
||||
f.edit((d) => { d.roles[f.name] = { definition: 'research', credentials: { gitea: d.roles.coder.credentials.gitea } }; });
|
||||
f.start();
|
||||
// The start makes no copy of the name, so the runtime would build no gate for this organization.
|
||||
assert.deepEqual(Object.keys(f.snapshot.copies ?? {}), []);
|
||||
const revoker = copyRevoker({ writer: f.writer, gate: () => null, credentials: f.credentials, hooks: {}, stopRuns: (...a) => f.stopRuns(...a),
|
||||
remote: giteaCopyRemote({ grant: f.grant, read: (x) => f.writer.read(x).document, incarnation: (x) => readOrgEntrySync(f.root, x)?.incarnation ?? null }),
|
||||
definitions: () => ({}) });
|
||||
assert.equal(revoker.pending(f.id, f.name), true);
|
||||
revoker.sweep(f.id);
|
||||
await until(() => tombstoned(f), 'shadowed tombstone');
|
||||
assert.equal(f.rec().revoke.cause, 'shadowed');
|
||||
assert.deepEqual(f.rec().revoke.remote, { gitea: 'revoked' });
|
||||
assert.ok(!existsSync(f.slot));
|
||||
assert.deepEqual(f.bot.tokens, []);
|
||||
assert.equal(revoker.pending(f.id, f.name), false);
|
||||
// A second pass finds a finished tombstone and starts nothing.
|
||||
const deletes = w.api.calls().filter((c) => c.startsWith('DELETE')).length;
|
||||
revoker.sweep(f.id);
|
||||
await sleep(50);
|
||||
assert.equal(revoker.has(f.id, f.name), false);
|
||||
assert.equal(w.api.calls().filter((c) => c.startsWith('DELETE')).length, deletes);
|
||||
});
|
||||
|
||||
// #1873 CB5 (CB4 review P3-3): an organization's own definition that needs gitea alone is not a
|
||||
// thread definition, so its copy runs on the ordinary path: a role claim, no thread. A revoke ends
|
||||
// that claim, kills the run, drains its call in flight and leaves its session dead.
|
||||
const GITTER = { rev: 1, role: { tools: ['read'], network: 'api-only', authority: { withinRole: ['message.send'], crossRole: [] },
|
||||
credentials: [{ service: 'gitea', scopes: ['read:repository', 'read:user'] }] }, instructions: 'Read the stack repository.' };
|
||||
test('CB5 a revoke of a copy on the ordinary run path ends its role claim, kills the run, drains its call and kills its session', async (t) => {
|
||||
const w = await world(t);
|
||||
const f = await org(t, w, { name: 'rev.stack', project: { rev: { definition: 'org:gitter' } }, definitions: { gitter: GITTER } });
|
||||
f.rootAgents = {};
|
||||
f.start();
|
||||
await f.bind({ level: 'project', project: 'stack', name: 'rev' });
|
||||
assert.equal(f.rec().state, 'bound');
|
||||
f.start();
|
||||
await f.boot();
|
||||
assert.deepEqual(Object.keys(f.gate.bound()), ['rev.stack']);
|
||||
// An ordinary run: a thread is refused, none is required.
|
||||
const revision = f.writer.read(f.id).revision;
|
||||
const { authority } = await f.gate.prepare(f.id, f.name, revision);
|
||||
assert.throws(() => f.b.bindLaunch({ business: f.id, role: f.name, run: 'run-t', harness: 'pi', thread: 'x', copyAuthority: authority, revision }), refuses('thread-not-allowed'));
|
||||
const cap = f.b.bindLaunch({ business: f.id, role: f.name, run: 'run-1', harness: 'pi', copyAuthority: authority, revision });
|
||||
await f.call(cap, 'role.claim');
|
||||
const claim = () => f.store.get('SELECT op, holder_run, reason, by FROM role_claims WHERE business=? AND role=? ORDER BY seq DESC LIMIT 1', f.id, f.name);
|
||||
assert.deepEqual({ ...claim() }, { op: 'claim', holder_run: 'run-1', reason: null, by: 'run-1' });
|
||||
assert.equal((await f.use(cap)).status, 200);
|
||||
const held = f.hold((e) => e.path === f.GET.path);
|
||||
const inflight = f.use(cap).then((x) => ({ ok: x }), (e) => ({ err: e }));
|
||||
await held.arrived;
|
||||
assert.equal(f.gate.inflight(f.id, f.name), 1);
|
||||
const out = await f.call(f.human, 'credentials.revoke', { agent: f.name, service: 'gitea', reason: 'Owner revoke' });
|
||||
assert.equal(out.state, 'unbound');
|
||||
assert.equal(f.gate.inflight(f.id, f.name), 0, 'drained before the revoke answered');
|
||||
held.release();
|
||||
const settled = await inflight;
|
||||
assert.ok(settled.err || settled.ok?.status === 0, 'the in-flight call was aborted');
|
||||
// The claim ended for the revoke, by the bus, and the run got its kill end.
|
||||
assert.deepEqual({ ...claim() }, { op: 'revoke', holder_run: 'run-1', reason: 'copy-revoked', by: 'bus' });
|
||||
assert.deepEqual(f.killed(), ['run-1']);
|
||||
assert.throws(() => f.b.copySession(cap), (e) => e instanceof BusError);
|
||||
await assert.rejects(f.call(cap, 'role.claim'), (e) => e instanceof BusError);
|
||||
await assertRevoked(f, null, { cause: 'revoke', code: 'credential-revoked' });
|
||||
});
|
||||
|
||||
// #1873 CB5 re-check: a copy whose role needs a service a copy cannot bind reads
|
||||
// copy-service-unbindable, but a revoke of a binding it was issued anyway outranks that.
|
||||
test('CB5 status: copy-service-unbindable never hides a revoke that is running or failed', async (t) => {
|
||||
const w = await world(t);
|
||||
const f = await org(t, w);
|
||||
const unbindable = { state: 'unbound', missing: ['gitea', 'vikunja'] };
|
||||
assert.equal(f.gate.status('demo', f.name, unbindable).status, 'copy-service-unbindable');
|
||||
let release;
|
||||
const held = f.gate.fence('demo', f.name, () => new Promise((r) => { release = r; }));
|
||||
assert.deepEqual(f.gate.status('demo', f.name, unbindable), { status: 'credential-revoking', next: COPY_BINDING_STATUS['credential-revoking'] });
|
||||
release();
|
||||
await held;
|
||||
f.gate.close('demo', f.name, 'credential-revoke-failed');
|
||||
assert.deepEqual(f.gate.status('demo', f.name, unbindable), { status: 'credential-revoke-failed', next: COPY_BINDING_STATUS['credential-revoke-failed'] });
|
||||
});
|
||||
@@ -36,11 +36,11 @@ function source3j(t) {
|
||||
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
|
||||
// Row 206 (#1704): and the 3l→3m step's thread_shares. thread-shares.test.mjs tests it.
|
||||
// Row 271 (#1776): and the 3m→3n step's session_ops and message_cancels. session-clear.test.mjs tests them.
|
||||
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'project_system_types', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
|
||||
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
|
||||
function rows(path) {
|
||||
const db = new DatabaseSync(path, { readOnly: true });
|
||||
try {
|
||||
return Object.fromEntries(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('gitea_users','gitea_grants','sessions','session_defaults','thread_shares','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_archives','project_system_types','store_tasks')").all()
|
||||
return Object.fromEntries(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('gitea_users','gitea_grants','sessions','session_defaults','thread_shares','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_archives','store_tasks')").all()
|
||||
.map(({ name }) => [name, db.prepare(`SELECT * FROM ${name} ORDER BY rowid`).all()
|
||||
.filter(r => name !== 'meta' || !['schema_version', 'schema_digest'].includes(r.key)).map(r => ({ ...r }))]));
|
||||
} finally { db.close(); }
|
||||
@@ -86,6 +86,7 @@ async function engine(root, name, { store, schema, step = (s) => s }) {
|
||||
copyFileSync(here('../src/migrations/3s-3t.sql'), join(moduleRoot, 'src/migrations/3s-3t.sql'));
|
||||
copyFileSync(here('../src/migrations/3t-3u.sql'), join(moduleRoot, 'src/migrations/3t-3u.sql'));
|
||||
copyFileSync(here('../src/migrations/3u-3v.sql'), join(moduleRoot, 'src/migrations/3u-3v.sql'));
|
||||
copyFileSync(here('../src/migrations/3v-3w.sql'), join(moduleRoot, 'src/migrations/3v-3w.sql'));
|
||||
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
|
||||
}
|
||||
function fresh(t) {
|
||||
@@ -107,7 +108,7 @@ test('3j copy migrates to 3k: every row kept, and sqlite_master gains only the t
|
||||
const { root, path } = source3j(t), before = rows(path), was = master(path);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(migrated3l(rows(path)), before);
|
||||
const now = master(path);
|
||||
assert.deepEqual(now.filter(r => !NEW.includes(r.name) && !later3l(r)), was.filter(r => !later3l(r)));
|
||||
@@ -133,7 +134,7 @@ test('a 3j→3k step that fails partway leaves the store at 3j with every row an
|
||||
assert.deepEqual(master(path), was);
|
||||
assert.deepEqual(rows(path), before);
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
});
|
||||
|
||||
test('the 3j engine refuses a migrated store, migrating or not, and leaves it as it was', async t => {
|
||||
@@ -144,14 +145,14 @@ test('the 3j engine refuses a migrated store, migrating or not, and leaves it as
|
||||
schema: ddl3j,
|
||||
store: (source) => source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3j).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3j';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3j';")
|
||||
.replace(/\}, \{\n {2}\/\/ Row 236 \(#1742[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables'),
|
||||
});
|
||||
const pinned = readFileSync(join(root, 'engine-3j/src/store.mjs'), 'utf8');
|
||||
assert.ok(pinned.includes("export const SCHEMA_VERSION = '3j';") && !pinned.includes('3j-3k'), 'the 3j engine has no 3j→3k step');
|
||||
assert.throws(() => new Store3j(root), /schema-mismatch/);
|
||||
assert.throws(() => new Store3j(root, { migrateSchema: true }), /schema-mismatch/);
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(master(path), was);
|
||||
assert.deepEqual(rows(path), after);
|
||||
});
|
||||
|
||||
@@ -22,7 +22,7 @@ const LATER = ['gitea_users', 'gitea_grants', 'sqlite_autoindex_gitea_users_1',
|
||||
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
|
||||
// Row 206 (#1704): and the 3l→3m step's thread_shares. thread-shares.test.mjs tests it.
|
||||
// Row 271 (#1776): and the 3m→3n step's session_ops and message_cancels. session-clear.test.mjs tests them.
|
||||
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'project_system_types', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
|
||||
const later3l = (r) => [...SESSIONS_3L, 'thread_shares', 'session_ops', 'message_cancels', 'project_archives', 'store_tasks', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events'].includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session', 'threads_id_not_session'].includes(r.name);
|
||||
function source3i(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'grant-migrate-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
@@ -42,7 +42,7 @@ function source3i(t) {
|
||||
function rows(path) {
|
||||
const db = new DatabaseSync(path, { readOnly: true });
|
||||
try {
|
||||
return Object.fromEntries(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('hosts','host_grants','gitea_users','gitea_grants','sessions','session_defaults','thread_shares','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_archives','project_system_types','store_tasks')").all()
|
||||
return Object.fromEntries(db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('hosts','host_grants','gitea_users','gitea_grants','sessions','session_defaults','thread_shares','session_ops','message_cancels','request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events','project_archives','store_tasks')").all()
|
||||
.map(({ name }) => [name, db.prepare(`SELECT * FROM ${name} ORDER BY rowid`).all()
|
||||
.filter(r => name !== 'meta' || !['schema_version', 'schema_digest'].includes(r.key)).map(r => ({ ...r }))]));
|
||||
} finally { db.close(); }
|
||||
@@ -89,6 +89,7 @@ async function engine(root, name, { store, schema, step = (s) => s }) {
|
||||
copyFileSync(here('../src/migrations/3s-3t.sql'), join(moduleRoot, 'src/migrations/3s-3t.sql'));
|
||||
copyFileSync(here('../src/migrations/3t-3u.sql'), join(moduleRoot, 'src/migrations/3t-3u.sql'));
|
||||
copyFileSync(here('../src/migrations/3u-3v.sql'), join(moduleRoot, 'src/migrations/3u-3v.sql'));
|
||||
copyFileSync(here('../src/migrations/3v-3w.sql'), join(moduleRoot, 'src/migrations/3v-3w.sql'));
|
||||
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
|
||||
}
|
||||
function fresh(t) {
|
||||
@@ -112,7 +113,7 @@ test('3i copy migrates to 3j: every row kept, and sqlite_master gains only the t
|
||||
const { root, path } = source3i(t), before = rows(path), was = master(path);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(migrated3l(rows(path)), before);
|
||||
const now = master(path).filter(r => !LATER.includes(r.name));
|
||||
assert.deepEqual(now.filter(r => !NEW.includes(r.name) && !later3l(r)), was.filter(r => !later3l(r)));
|
||||
@@ -137,7 +138,7 @@ test('a 3i→3j step that fails partway leaves the store at 3i with every row an
|
||||
assert.deepEqual(master(path), was);
|
||||
assert.deepEqual(rows(path), before);
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
});
|
||||
|
||||
test('the 3i engine refuses a migrated store, migrating or not, and leaves it as it was', async t => {
|
||||
@@ -148,14 +149,14 @@ test('the 3i engine refuses a migrated store, migrating or not, and leaves it as
|
||||
schema: ddl3i,
|
||||
store: (source) => source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3i).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3i';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3i';")
|
||||
.replace(/\}, \{\n {2}\/\/ Row 234 \(#1740\)[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables'),
|
||||
});
|
||||
const pinned = readFileSync(join(root, 'engine-3i/src/store.mjs'), 'utf8');
|
||||
assert.ok(pinned.includes("export const SCHEMA_VERSION = '3i';") && !pinned.includes('3i-3j'), 'the 3i engine has no 3i→3j step');
|
||||
assert.throws(() => new Store3i(root), /schema-mismatch/);
|
||||
assert.throws(() => new Store3i(root, { migrateSchema: true }), /schema-mismatch/);
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(master(path), was);
|
||||
assert.deepEqual(rows(path), after);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
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 { startBroker } from '../src/runtime.mjs';
|
||||
import { checkedTransport } from '../../harness/src/bundle.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
// #1873 harness-matrix: an OAuth Claude model runs on claude-code only. A stored route that puts an
|
||||
// OAuth Claude model on pi is refused at launch admission with model-harness-incompatible, even when a stale
|
||||
// catalogue row still says pi. A z.ai and a codex model are admitted on pi.
|
||||
const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: { authority: { withinRole: [], crossRole: [] } } };
|
||||
|
||||
async function admission(t, entry, catalogueRow) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'harness-matrix-')); t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const rolesDir = rolesCopy(root), doc = businessDoc(root, 'demo');
|
||||
doc.plugins = { ...doc.plugins, harness: ['pi'], 'model-provider': ['cliproxy'] };
|
||||
doc.launch.models['fixture-key'] = entry;
|
||||
doc.roles.coder.vars = { ...doc.roles.coder.vars, harness: 'pi', model: 'fixture-key', thinking: 'low' };
|
||||
writeJson(join(root, 'businesses/demo.json'), doc); mkdirSync(join(root, 'data'), { mode: 0o700 });
|
||||
const modelCatalogue = { invalidate() {}, read: async () => ({ status: 'ready', notices: [], models: [{ module: 'cliproxy', available: true, ...catalogueRow }] }) };
|
||||
const bus = await startBroker({ dataRoot: join(root, 'data'), businesses: { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles } }, organization: { dir: root, rolesDir, system: systemFor(root) }, modelCatalogue });
|
||||
t.after(() => bus.close());
|
||||
const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const revision = bus.broker.request(human, { verb: 'limits' }).revision;
|
||||
// The launcher's own transport check refuses first; admission is the second gate, so the request
|
||||
// carries the transport the provider would give with no harness named.
|
||||
const transport = checkedTransport(entry);
|
||||
return bus.modelAdmission({ business: 'demo', instance: 'coder', modelKey: 'fixture-key', harness: 'pi', revision, entry, provider: entry.provider, model: entry.model, account: null, route: 'primary', transport });
|
||||
}
|
||||
|
||||
test('#1873 a stored Claude route on pi is refused model-harness-incompatible at admission, even with a stale catalogue row that says pi', async t => {
|
||||
const entry = { provider: 'anthropic', model: 'claude-opus-5-5', module: 'cliproxy', upstream: 'claude' };
|
||||
assert.deepEqual(await admission(t, entry, { upstream: 'claude', model: 'claude-opus-5-5', harnesses: ['claude-code', 'pi'] }), { allow: false, code: 'model-harness-incompatible' });
|
||||
});
|
||||
|
||||
test('#1873 a Claude model from an API-key group (upstream other) is admitted on pi; one whose name an OAuth account offers is refused', async t => {
|
||||
const entry = { provider: 'other', model: 'anthropic/claude-sonnet-5-5', module: 'cliproxy', upstream: 'other' };
|
||||
assert.deepEqual(await admission(t, entry, { upstream: 'other', model: 'anthropic/claude-sonnet-5-5', harnesses: ['claude-code', 'pi'] }), { allow: true });
|
||||
// The catalogue holds a group model to claude-code when an OAuth Claude account offers its name.
|
||||
assert.deepEqual(await admission(t, entry, { upstream: 'other', model: 'anthropic/claude-sonnet-5-5', harnesses: ['claude-code'] }), { allow: false, code: 'model-harness-incompatible' });
|
||||
});
|
||||
|
||||
test('#1873 a z.ai and a codex model are admitted on pi', async t => {
|
||||
for (const [provider, upstream, model] of [['zai', 'zai', 'glm-fixture'], ['codex', 'codex', 'gpt-fixture']]) {
|
||||
const entry = { provider, model, module: 'cliproxy', upstream };
|
||||
assert.deepEqual(await admission(t, entry, { upstream, model, harnesses: ['claude-code', 'pi'] }), { allow: true }, provider);
|
||||
}
|
||||
});
|
||||
@@ -27,7 +27,9 @@ export async function ownershipFixture(t,{launches=true,listed=true,keep=false,r
|
||||
// Available before any acquired resource/setup can fail; no later f/launcher TDZ.
|
||||
const launchLog=()=>{const file=launchLogFile(join(root,'data'),'acme');return fs.existsSync(file)?fs.readFileSync(file,'utf8').trim().split('\n').filter(Boolean).map(line=>JSON.parse(line)):[];};
|
||||
t.after(async()=>{const failures=[];for(const g of gates)g.release();try{await waitFor(()=>active.size===0,'owned operations did not settle',5000);}catch{failures.push('owned-operation-close');}for(const close of [...closers].reverse()){try{await close();}catch{failures.push('owned-close');}}try{server?.closeAllConnections();if(server?.listening)await new Promise((resolve,reject)=>server.close(e=>e?reject(e):resolve()));}catch{failures.push('owned-server-close');}try{checkHistory({logs,proxyReceipts,trace});}catch{failures.push('value-free-receipts');}try{checkHistory({launch:launchLog()});}catch{failures.push('owned-launch-log-guard');}if(stderr)process.stderr.write=stderr;try{fs.rmSync(root,{recursive:true,force:true});}catch{failures.push('owned-root-close');}if(failures.length)throw new AggregateError(failures.map(code=>Error(code)),'owned managed fixture cleanup failed');});
|
||||
const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:['pi']};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness:'pi'};doc.roles.chief.vars.model='claude-opus-5-5';doc.roles.aide={definition:'aide',vars:{harness:'pi',model:'claude-opus-5-5',thinking:'high'}};if(listed)doc.launch.instances.push('aide');if(keep)doc.launch.keep=['aide'];if(retired)doc.retired=['alpha'];if(noHire)doc.launch.noHire=['aide'];if(!listed)delete doc.roles.aide;return doc;});
|
||||
const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:['pi']};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness:'pi'};doc.roles.chief.vars.model='claude-opus-5-5';doc.roles.aide={definition:'aide',vars:{harness:'pi',model:'claude-opus-5-5',thinking:'high'}};if(listed)doc.launch.instances.push('aide');if(keep)doc.launch.keep=['aide'];if(retired)doc.retired=['alpha'];if(noHire)doc.launch.noHire=['aide'];if(!listed)delete doc.roles.aide;doc.launch.models['claude-opus-5-5']={provider:'zai',model:'fixture-aide-model'};return doc;});
|
||||
// #1873 harness-matrix: an OAuth Claude model runs on claude-code only, so on pi the model key
|
||||
// claude-opus-5-5 names a non-Claude upstream model.
|
||||
// The established scratch aide pattern only; repository roles/policy stay untouched.
|
||||
for(const name of ['aide','helper']){fs.writeFileSync(join(f.roles,name+'.md'),'# Scratch aide\n\n## Authority\n');writeJson(join(f.roles,name+'.json'),{roleVersion:2,name,title:'Aide',contract:name+'.md',tools:['read'],network:'api-only',authority:{withinRole:['message.send'],crossRole:[]},credentials:[]});}
|
||||
const chiefFile=join(f.roles,'chief-of-staff.json');
|
||||
@@ -74,6 +76,7 @@ export async function ownershipFixture(t,{launches=true,listed=true,keep=false,r
|
||||
case 'landRelease':return bus.landRelease(m.record);
|
||||
case 'threadsState':return bus.threadsState(m.business);
|
||||
case 'threadIdle':return bus.threadIdle(m.record);
|
||||
case 'failureNotice':return bus.failureNotice(m.record);
|
||||
case 'lifecycleRequests':return bus.lifecycleRequests(m.business);
|
||||
case 'lifecycleTake':return bus.lifecycleTake(m.record);
|
||||
case 'lifecycleDelete':return bus.lifecycleDelete(m.record);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { join } from 'node:path';
|
||||
import { sandboxProblem } from '../../seat/src/sandbox.mjs';
|
||||
import { startTimeOf } from '../../seat/src/proc.mjs';
|
||||
import { ownershipFixture, waitFor } from './model-hire-ownership-fixture.mjs';
|
||||
import * as pi from '../../plugins/harness/pi/index.mjs';
|
||||
const unavailable=sandboxProblem()??false; // Any skip means UNVERIFIED, not green acceptance.
|
||||
const done=async box=>{await box.promise;return box;};
|
||||
const refused=async(box,code)=>{await done(box);assert.equal(box.ok,false,'real operation refuses');assert.equal(box.code,code,'real operation preserves exact refusal');};
|
||||
@@ -17,7 +18,12 @@ test('655 listed duplicate with no record refuses before any model or management
|
||||
assert.deepEqual(f.snapshot(),before,'duplicate hire makes no credential or management effects');f.assertHistoryGuard();f.valueFree();
|
||||
});
|
||||
|
||||
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:30000},async t=>{
|
||||
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:120000},async t=>{
|
||||
// #1873 pi-unpin re-check 9 and 10: the launch's preflight shares pi's fit check, which is cold in
|
||||
// a fresh process. Wait for it before the fixture starts its launcher, so the launcher's warm-up
|
||||
// and the launch's preflight hit the cache, the gate's 5 s times the launch, and the launch starts
|
||||
// at the same point in the launcher's 2 s tick as it did before the fit check existed.
|
||||
await pi.ready({env:{PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR}});
|
||||
const f=await ownershipFixture(t),pGet=f.hold('GET'),p=f.launchAide();await pGet.entered();
|
||||
// P's own acquisition GET is held. Its PUT confirmation GET is ordinal+1;
|
||||
// only an OLD H reuse GET can be ordinal+2. The correct early refusal removes it.
|
||||
|
||||
@@ -11,7 +11,7 @@ import { tmpdir } from 'node:os';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { Credentials } from '../src/credentials.mjs';
|
||||
import { ManagementClient } from '../../model-proxy/src/client.mjs';
|
||||
import { ModelKeys, REVOKE_TIMEOUT_MS, REVOKE_POLL_MS } from '../src/model-keys.mjs';
|
||||
import { ModelKeys, REVOKE_TIMEOUT_MS, REVOKE_POLL_MS, ISSUE_PROBE_TIMEOUT_MS, ISSUE_REPROBE_MIN_MS } from '../src/model-keys.mjs';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { Registry } from '../../seat/src/session.mjs';
|
||||
@@ -42,6 +42,7 @@ async function fixture(t, { delayed = 0 } = {}) {
|
||||
const key = req.headers.authorization?.slice(7);
|
||||
const accepted = state.keys.includes(key) || (state.removed.has(key) && performance.now() - state.removed.get(key) < state.delay);
|
||||
state.onProbe?.(state.status ?? (accepted ? 200 : 401));
|
||||
if (state.stallProbe?.()) return;
|
||||
res.writeHead(state.status ?? (accepted ? 200 : 401)); res.end('{}');
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
@@ -123,10 +124,33 @@ test('proxy-revoke-revision-conflict keeps local revoking record and ids-only re
|
||||
assert.equal(JSON.stringify(f.receipts).includes(key), false);
|
||||
await f.create().reconcile(); assert.equal(f.credentials.modelRecord('demo/coder') === null, true);
|
||||
});
|
||||
for (const status of [401, 503, 302]) test(`proxy-key-refused: preflight rejects HTTP ${status} with exactly one probe`, async t => {
|
||||
for (const status of [503, 302]) test(`proxy-key-refused: preflight rejects HTTP ${status} with exactly one probe`, async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = status;
|
||||
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-key-refused')); assert.equal(f.state.probes, 1);
|
||||
});
|
||||
test('proxy-key-refused: a key that keeps answering 401 refuses once the short probe deadline passes', async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = 401;
|
||||
const started = performance.now();
|
||||
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-key-refused'));
|
||||
const took = performance.now() - started;
|
||||
assert.ok(f.state.probes > 1, 'a 401 is probed again'); assert.ok(took >= ISSUE_PROBE_TIMEOUT_MS && took < ISSUE_PROBE_TIMEOUT_MS + 2000, `bounded: ${took}`);
|
||||
});
|
||||
test('proxy-down: a re-probe that stalls ends near the probe deadline, not the client default', async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = 401;
|
||||
f.state.stallProbe = () => f.state.probes >= 2;
|
||||
const started = performance.now();
|
||||
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-down'));
|
||||
const took = performance.now() - started;
|
||||
assert.equal(f.state.probes, 2); assert.ok(took < ISSUE_PROBE_TIMEOUT_MS + ISSUE_REPROBE_MIN_MS + 1000, `bounded: ${took}`);
|
||||
});
|
||||
// Every v8 CLIProxyAPI applies the PUT by an async reload; on 8.0.79 a just-issued key answers 401
|
||||
// past the confirming GET (#1873).
|
||||
test('first launch passes when the proxy applies the new key a moment after the PUT answers', async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); f.state.status = 401;
|
||||
f.state.onProbe = () => { if (f.state.probes === 3) f.state.status = null; };
|
||||
const answer = await f.manager.preflight('demo/coder');
|
||||
assert.equal(f.state.probes, 3); assert.equal(f.state.keys.includes(answer.key), true);
|
||||
});
|
||||
test('rotate keeps old key while its run lives, new launches use new, exit removes old', async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); const first = await f.manager.ensure('demo/coder');
|
||||
f.runs.push({ instance: 'demo/coder', run: 'old', keyId: hash(first.key) });
|
||||
|
||||
@@ -65,7 +65,7 @@ test('3f copy migrates to 3g preserving every old column/row, with an empty inst
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
try {
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
assert.deepEqual({ ...store.get('SELECT to_name FROM messages') }, { to_name: null });
|
||||
assert.equal(store.get('SELECT count(*) AS n FROM instance_names').n, 0);
|
||||
assert.deepEqual({ ...store.get('SELECT spec_path,spec_commit FROM decisions') }, { spec_path: 'docs/spec.md', spec_commit: 'c'.repeat(40) });
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
// Ruling 2026-11-14 ("If I choose a starter pack and apply it, that is a decision!"): a person's
|
||||
// apply records one hire decision per proposed hire in the same act and applies it, so the agents
|
||||
// exist when the call returns. The owner approves every line; a member with org.hire gets the
|
||||
// unflagged hires and the rest wait in the owner's inbox; an agent's apply never hires.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { pluginHash } from '../../plugins/src/pinned.mjs';
|
||||
import { installPlugin } from '../../plugins/src/install.mjs';
|
||||
import { starterPack as codingPack } from '../../plugins/starter-pack/coding/index.mjs';
|
||||
import { templateHash } from '../../organization/src/definitions.mjs';
|
||||
|
||||
const SUBJECT = { provider: 'test', iss: 'test', sub: 'owner' };
|
||||
const ROUTE = { harness: 'pi', model: 'claude-opus-5-5' };
|
||||
const ASSISTANT_ROUTE = { harness: 'pi', model: 'glm-5.3-flash', thinking: 'low' };
|
||||
|
||||
// assistant: false, no assistant instance; {} an assistant with no vars; any other object the
|
||||
// assistant's vars. Lane pr2: a pack hire falls back to the org assistant's route.
|
||||
async function harness(t, { route = true, assistant = false, mutate = () => {} } = {}) {
|
||||
const { startBroker } = await import('../src/runtime.mjs');
|
||||
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'packhire-'));
|
||||
const rolesDir = rolesCopy(root);
|
||||
const doc = businessDoc(root, 'demo');
|
||||
doc.plugins = { harness: ['pi'] };
|
||||
if (route) doc.roles.chief.vars = { ...ROUTE };
|
||||
if (assistant) doc.roles.assistant = { definition: 'assistant', ...(Object.keys(assistant).length ? { vars: { ...assistant } } : {}) };
|
||||
mutate(doc);
|
||||
writeJson(join(root, 'businesses/demo.json'), doc);
|
||||
const data = join(root, 'data');
|
||||
mkdirSync(data, { mode: 0o700 });
|
||||
const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery', 'organization.change'], crossRole: [] } },
|
||||
coder: { definition: 'coder', authority: { withinRole: ['message.send'], crossRole: [] } } };
|
||||
const businesses = { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles } };
|
||||
const bus = await startBroker({ dataRoot: data, businesses, organization: { dir: root, rolesDir, system: systemFor(root) } });
|
||||
const thaw = (p) => { const st = lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { chmodSync(p, 0o700); for (const e of readdirSync(p)) thaw(join(p, e)); } };
|
||||
t.after(async () => { await bus.close(); thaw(root); rmSync(root, { recursive: true, force: true }); });
|
||||
const b = bus.broker;
|
||||
const consoleOwner = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT });
|
||||
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const member = b.bindHireConsole({ business: 'demo', actor: 'alice', subject: { provider: 'test', iss: 'test', sub: 'alice' }, rights: ['org.hire', 'board.read'] });
|
||||
const call = async (verb, args = {}, cap = consoleOwner) => b.request(cap, { verb, args });
|
||||
const path = join(root, 'businesses/demo.json');
|
||||
const file = () => JSON.parse(readFileSync(path, 'utf8'));
|
||||
const decisions = list => b.request(human, { verb: 'decisions', args: { list } });
|
||||
// The broker's own database, read-only.
|
||||
const db = (sql, ...a) => { const d = new DatabaseSync(join(data, 'bus/bus.sqlite'), { readOnly: true }); try { return d.prepare(sql).all(...a); } finally { d.close(); } };
|
||||
const store = { all: db, get: (sql, ...a) => db(sql, ...a)[0] };
|
||||
const events = () => store.all("SELECT kind, body FROM events WHERE business='demo' ORDER BY seq").map(r => ({ kind: r.kind, body: JSON.parse(r.body) }));
|
||||
let n = 0;
|
||||
const install = (pack) => {
|
||||
const src = join(root, `src-${n++}`);
|
||||
mkdirSync(join(src, 'starter-pack'), { recursive: true });
|
||||
writeFileSync(join(src, 'starter-pack', 'index.mjs'), `export const interfaceVersion = 1;\nexport function starterPack() { return ${JSON.stringify(pack)}; }\n`);
|
||||
const manifest = { name: pack.id, version: '1.0.0', kinds: ['starter-pack'], interfaceVersions: { 'starter-pack': 1 }, sha256: '0'.repeat(64),
|
||||
source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
|
||||
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
manifest.sha256 = pluginHash(src);
|
||||
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
installPlugin({ dataRoot: data, source: src, expectedHash: manifest.sha256 });
|
||||
};
|
||||
const op = (name) => events().filter(e => e.kind === 'action.allowed' && e.body.operation === name).map(e => e.body);
|
||||
return { bus, b, root, rolesDir, path, consoleOwner, human, member, call, file, decisions, events, op, store, install };
|
||||
}
|
||||
|
||||
const apply = async (call, args = { pack: 'coding' }, cap) => {
|
||||
const preview = await call('pack.preview.console', args, cap);
|
||||
return { preview, out: await call('pack.apply.console', { ...args, digest: preview.digest }, cap) };
|
||||
};
|
||||
|
||||
// The evidence an inbox-approved hire leaves: a decision.raise naming the hire, a console
|
||||
// resolution yes, a hire.consume and a hire.apply, and the instance's hireDecision.
|
||||
function assertHireRecord(h, name, decision) {
|
||||
const raise = h.op('decision.raise').find(r => r.decision === decision);
|
||||
assert.ok(raise, `decision.raise for ${name}`);
|
||||
assert.equal(raise.hire.name, name);
|
||||
assert.equal(raise.action, 'role.hire');
|
||||
assert.ok(h.op('hire.consume').some(r => r.decision === decision), `hire.consume for ${name}`);
|
||||
assert.ok(h.op('hire.apply').some(r => r.decision === decision), `hire.apply for ${name}`);
|
||||
const resolved = h.store.all('SELECT op, choice, via, by FROM decision_events WHERE decision=?', decision);
|
||||
assert.deepEqual(resolved.map(r => [r.op, r.choice]), [['resolved', 'yes']]);
|
||||
assert.equal(h.file().roles[name].hireDecision, decision);
|
||||
}
|
||||
|
||||
test('PD the owner applies a pack with flagged hires on a fresh org; the agents exist on return, each with its decision record', async t => {
|
||||
const h = await harness(t);
|
||||
const { preview, out } = await apply(h.call);
|
||||
// The preview lists the flagged and credential lines the owner's apply approves.
|
||||
assert.ok(preview.hires.some(x => x.flagged.length || x.credentialLines.length), JSON.stringify(preview.hires));
|
||||
assert.deepEqual(out.hires.map(x => [x.name, x.state]), [['pm-1', 'hired'], ['coder-1', 'hired'], ['reviewer-1', 'hired']]);
|
||||
const doc = h.file();
|
||||
for (const x of out.hires) {
|
||||
assert.ok(Object.hasOwn(doc.roles, x.name), `${x.name} exists`);
|
||||
assert.ok(doc.launch.instances.includes(x.name), `${x.name} is launched`);
|
||||
assertHireRecord(h, x.name, x.decision);
|
||||
}
|
||||
// The coder's credential lines are approved, not bound: the instance is written with no credential.
|
||||
assert.equal(doc.roles['coder-1'].credentials, undefined);
|
||||
// Each hire names its pack.
|
||||
assert.deepEqual(h.op('pack.hire').map(e => [e.hire, e.pack, e.origin, e.project]), out.hires.map(x => [x.name, 'coding', 'shipped', null]));
|
||||
// The instances read carries the pack each agent came from.
|
||||
const inst = await h.call('instances', {}, h.human);
|
||||
assert.deepEqual(inst.instances['coder-1'].pack, { id: 'coding', origin: 'shipped', project: null });
|
||||
assert.equal(inst.instances.pm.pack, undefined);
|
||||
// The packs read counts the hires and lists nothing open.
|
||||
const packs = await h.call('packs.console');
|
||||
const applied = packs.applied.find(a => a.id === 'coding');
|
||||
assert.deepEqual(applied.hired.sort(), ['coder-1', 'pm-1', 'reviewer-1']);
|
||||
assert.deepEqual(applied.open, []);
|
||||
});
|
||||
|
||||
test('PD a re-apply hires nothing twice', async t => {
|
||||
const h = await harness(t);
|
||||
await apply(h.call);
|
||||
const raised = h.op('decision.raise').length;
|
||||
const again = await apply(h.call);
|
||||
// The second apply proposes nothing: each proposal's instance exists.
|
||||
assert.deepEqual(again.out.hires, []);
|
||||
assert.deepEqual((await h.call('packs.console')).applied.find(a => a.id === 'coding').hired.sort(), ['coder-1', 'pm-1', 'reviewer-1']);
|
||||
// The pack action on the same entry skips each instance that exists.
|
||||
const hired = await h.call('pack.hire.console', { pack: 'coding' });
|
||||
assert.equal(h.op('decision.raise').length, raised);
|
||||
assert.ok(hired.hires.every(x => x.state === 'exists'), JSON.stringify(hired));
|
||||
});
|
||||
|
||||
test('PD the owner CLI apply hires through the same path, resolved via cli', async t => {
|
||||
const h = await harness(t);
|
||||
const preview = await h.call('pack.preview', { pack: 'coding' }, h.human);
|
||||
const out = await h.call('pack.apply', { pack: 'coding', digest: preview.digest }, h.human);
|
||||
assert.deepEqual(out.hires.map(x => x.state), ['hired', 'hired', 'hired']);
|
||||
for (const x of out.hires) {
|
||||
assertHireRecord(h, x.name, x.decision);
|
||||
assert.equal(h.store.get('SELECT via FROM decision_events WHERE decision=?', x.decision).via, 'cli');
|
||||
assert.equal(h.store.get('SELECT raised_by_run FROM decisions WHERE id=?', x.decision).raised_by_run, 'human-cli');
|
||||
}
|
||||
});
|
||||
|
||||
test('PD with no chief route the apply records the pack, hires nothing and says why; the hire action on the existing record hires once a route exists', async t => {
|
||||
const h = await harness(t, { route: false });
|
||||
const { out } = await apply(h.call);
|
||||
assert.deepEqual(out.hires.map(x => [x.name, x.state, x.code]), [['pm-1', 'refused', 'pack-hire-route-missing'], ['coder-1', 'refused', 'pack-hire-route-missing'], ['reviewer-1', 'refused', 'pack-hire-route-missing']]);
|
||||
assert.equal(h.op('decision.raise').length, 0);
|
||||
const packs = await h.call('packs.console');
|
||||
assert.deepEqual(packs.applied.find(a => a.id === 'coding').open.map(o => [o.name, o.state, o.code, typeof o.reason]),
|
||||
[['pm-1', 'refused', 'pack-hire-route-missing', 'string'], ['coder-1', 'refused', 'pack-hire-route-missing', 'string'], ['reviewer-1', 'refused', 'pack-hire-route-missing', 'string']]);
|
||||
// The owner gives the chief a route; the pack action hires from the packs entry already recorded.
|
||||
const doc = h.file();
|
||||
doc.roles.chief.vars = { ...ROUTE };
|
||||
writeFileSync(h.path, JSON.stringify(doc, null, 2) + '\n');
|
||||
const hired = await h.call('pack.hire.console', { pack: 'coding' });
|
||||
assert.deepEqual(hired.hires.map(x => [x.name, x.state]), [['pm-1', 'hired'], ['coder-1', 'hired'], ['reviewer-1', 'hired']]);
|
||||
for (const x of hired.hires) assertHireRecord(h, x.name, x.decision);
|
||||
assert.deepEqual((await h.call('packs.console')).applied.find(a => a.id === 'coding').open, []);
|
||||
// The CLI's pack hire is the same action; nothing is left to hire.
|
||||
const cli = await h.call('pack.hire', { pack: 'coding' }, h.human);
|
||||
assert.ok(cli.hires.every(x => x.state === 'exists'));
|
||||
await assert.rejects(h.call('pack.hire.console', { pack: 'nosuch' }), /pack-not-applied/);
|
||||
});
|
||||
|
||||
// A pack with one hire inside the chief's own grants and two above them.
|
||||
// An installed pack's own definition narrows a template; the scout keeps only read.
|
||||
const mixkit = (rolesDir) => {
|
||||
const SCOUT = { from: { template: 'coder', sha256: templateHash(rolesDir, 'coder') }, role: { tools: ['read'], network: 'none', authority: { withinRole: [], crossRole: [] }, credentials: [] }, instructions: 'Read and report.' };
|
||||
const p = structuredClone(codingPack());
|
||||
p.id = 'mixkit';
|
||||
p.definitions = { scout: SCOUT, coder: p.definitions.coder, reviewer: p.definitions.reviewer };
|
||||
p.hires = [{ name: 'scout-1', definition: 'scout', reason: 'reads and reports' }, { name: 'coder-1', definition: 'coder', reason: 'implements each change' }, { name: 'reviewer-1', definition: 'reviewer', reason: 'reviews each change' }];
|
||||
p.workflows = { change: { description: 'One change.', steps: [{ id: 'build', title: 'Build it', roles: ['coder'], state: 'in-progress' }, { id: 'review', title: 'Review it', roles: ['reviewer'], state: 'in-review' }] } };
|
||||
return p;
|
||||
};
|
||||
|
||||
test('PD a member with org.hire gets the unflagged hires; the flagged ones become open decisions in the owner inbox', async t => {
|
||||
const h = await harness(t, { route: false });
|
||||
h.install(mixkit(h.rolesDir));
|
||||
await apply(h.call, { pack: 'mixkit' });
|
||||
const doc = h.file();
|
||||
doc.roles.chief.vars = { ...ROUTE };
|
||||
writeFileSync(h.path, JSON.stringify(doc, null, 2) + '\n');
|
||||
const out = await h.call('pack.hire.console', { pack: 'mixkit' }, h.member);
|
||||
assert.deepEqual(out.hires.map(x => [x.name, x.state]), [['scout-1', 'hired'], ['coder-1', 'awaiting-owner'], ['reviewer-1', 'awaiting-owner']]);
|
||||
const after = h.file();
|
||||
assert.ok(after.launch.instances.includes('scout-1'));
|
||||
assert.equal(Object.hasOwn(after.roles, 'coder-1'), false);
|
||||
assert.equal(Object.hasOwn(after.roles, 'reviewer-1'), false);
|
||||
assertHireRecord(h, 'scout-1', out.hires[0].decision);
|
||||
assert.equal(h.store.get('SELECT via FROM decision_events WHERE decision=?', out.hires[0].decision).via, 'console');
|
||||
// Each flagged hire is an open decision routed to the human, raised by the member's act.
|
||||
const waiting = out.hires.filter(x => x.state === 'awaiting-owner');
|
||||
for (const x of waiting) {
|
||||
const d = h.store.get('SELECT route_to, raised_by_role, action FROM decisions WHERE id=?', x.decision);
|
||||
assert.deepEqual([d.route_to, d.raised_by_role, d.action], ['human', 'human', 'role.hire']);
|
||||
assert.equal(h.store.all('SELECT 1 FROM decision_events WHERE decision=?', x.decision).length, 0);
|
||||
}
|
||||
// The packs read lists both as open, awaiting the owner, each with the pack's reason.
|
||||
const applied = (await h.call('packs.console')).applied.find(a => a.id === 'mixkit');
|
||||
assert.deepEqual(applied.hired, ['scout-1']);
|
||||
assert.deepEqual(applied.open.map(o => [o.name, o.state, o.reason, o.decision]), [['coder-1', 'awaiting-owner', 'implements each change', waiting[0].decision], ['reviewer-1', 'awaiting-owner', 'reviews each change', waiting[1].decision]]);
|
||||
// The member's pack action never re-raises a hire an open decision names.
|
||||
const again = await h.call('pack.hire.console', { pack: 'mixkit' }, h.member);
|
||||
assert.deepEqual(again.hires.map(x => [x.name, x.state, x.decision ?? null]), [['scout-1', 'exists', null], ['coder-1', 'open', waiting[0].decision], ['reviewer-1', 'open', waiting[1].decision]]);
|
||||
// The member cannot approve a flagged hire; the owner can, from the inbox, and the agent exists.
|
||||
const view = await h.call('hire.preview.console', { id: waiting[0].decision });
|
||||
await assert.rejects(h.call('hire.resolve.console', { id: waiting[0].decision, choice: 'yes', digest: view.digest }, h.member), /hire-owner-required|human-required/);
|
||||
await h.call('hire.resolve.console', { id: waiting[0].decision, choice: 'yes', digest: view.digest });
|
||||
assert.ok(h.file().launch.instances.includes('coder-1'));
|
||||
// The owner's pack action approves the rest: the open pack decision is withdrawn by the owner and
|
||||
// replaced by one approved in the same act.
|
||||
const owner = await h.call('pack.hire.console', { pack: 'mixkit' });
|
||||
assert.deepEqual(owner.hires.map(x => [x.name, x.state, x.supersedes ?? null]), [['scout-1', 'exists', null], ['coder-1', 'exists', null], ['reviewer-1', 'hired', waiting[1].decision]]);
|
||||
assert.deepEqual(h.store.all('SELECT op, by FROM decision_events WHERE decision=?', waiting[1].decision).map(r => [r.op, r.by]), [['withdrawn', 'jason']]);
|
||||
assertHireRecord(h, 'reviewer-1', owner.hires[2].decision);
|
||||
const done = (await h.call('packs.console')).applied.find(a => a.id === 'mixkit');
|
||||
assert.deepEqual(done.hired.sort(), ['coder-1', 'reviewer-1', 'scout-1']);
|
||||
assert.deepEqual(done.open, []);
|
||||
});
|
||||
|
||||
test('PD an agent apply still refuses human-required and hires nothing; the tester and a cli cap on the console name refuse', async t => {
|
||||
const h = await harness(t);
|
||||
const agent = h.b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi', address: 'coder-run' });
|
||||
h.b.request(agent, { verb: 'role.claim' });
|
||||
assert.throws(() => h.b.request(agent, { verb: 'pack.apply.console', args: { pack: 'coding', digest: 'a'.repeat(64) } }), /human-required/);
|
||||
await assert.rejects(Promise.resolve().then(() => h.b.request(agent, { verb: 'pack.hire', args: { pack: 'coding' } })), /human-required/);
|
||||
await assert.rejects(Promise.resolve().then(() => h.b.request(agent, { verb: 'pack.hire.console', args: { pack: 'coding' } })), /human-required/);
|
||||
await assert.rejects(Promise.resolve().then(() => h.b.request(h.human, { verb: 'pack.hire.console', args: { pack: 'coding' } })), /human-required/);
|
||||
await assert.rejects(h.call('pack.hire', { pack: 'coding' }), /human-required/);
|
||||
for (const args of [{}, { pack: 7 }, { pack: 'coding', extra: 1 }, { pack: 'coding', project: 'Bad Name' }])
|
||||
await assert.rejects(h.call('pack.hire.console', args), /invalid-request/);
|
||||
assert.equal(h.op('decision.raise').length, 0);
|
||||
});
|
||||
|
||||
test('PD a project pack hires into projects.<p>.agents', async t => {
|
||||
const h = await harness(t);
|
||||
const pack = structuredClone(codingPack());
|
||||
pack.id = 'projkit'; pack.scope = 'project';
|
||||
h.install(pack);
|
||||
const { out } = await apply(h.call, { pack: 'projkit', project: 'stack' });
|
||||
assert.deepEqual(out.hires.map(x => x.state), ['hired', 'hired', 'hired']);
|
||||
const agents = h.file().projects.stack.agents;
|
||||
for (const x of out.hires) { assert.equal(agents[x.name].hireDecision, x.decision); assert.equal(agents[x.name].credentials, undefined); }
|
||||
assert.equal(Object.hasOwn(h.file().roles, 'coder-1'), false);
|
||||
assert.deepEqual(h.op('pack.hire').map(e => e.project), ['stack', 'stack', 'stack']);
|
||||
const applied = (await h.call('packs.console')).applied.find(a => a.id === 'projkit');
|
||||
assert.deepEqual(applied.hired.sort(), ['coder-1', 'pm-1', 'reviewer-1']);
|
||||
const again = await h.call('pack.hire.console', { pack: 'projkit', project: 'stack' });
|
||||
assert.ok(again.hires.every(x => x.state === 'exists'));
|
||||
});
|
||||
|
||||
test('PD the new codes are in the refusal table with a rail and a next step that names no path', async () => {
|
||||
const { REFUSALS } = await import('../src/refusals.mjs');
|
||||
for (const c of ['pack-not-applied', 'pack-hire-route-missing', 'hire-owner-required', 'no-chief-of-staff']) {
|
||||
assert.ok(Object.hasOwn(REFUSALS, c), c);
|
||||
assert.ok(REFUSALS[c].rail?.length > 5);
|
||||
assert.ok(REFUSALS[c].nextStep?.length > 20);
|
||||
assert.ok(!/(^|[\s("'`])(\/|~\/)/.test(REFUSALS[c].nextStep));
|
||||
}
|
||||
});
|
||||
|
||||
test('PD the owner supersede withdraws an inbox decision only after its replacement hires; a refused replacement leaves it open', async t => {
|
||||
const h = await harness(t, { route: false });
|
||||
h.install(mixkit(h.rolesDir));
|
||||
await apply(h.call, { pack: 'mixkit' });
|
||||
const setRoute = (vars) => { const doc = h.file(); if (vars) doc.roles.chief.vars = { ...vars }; else delete doc.roles.chief.vars; writeFileSync(h.path, JSON.stringify(doc, null, 2) + '\n'); };
|
||||
setRoute(ROUTE);
|
||||
const out = await h.call('pack.hire.console', { pack: 'mixkit' }, h.member);
|
||||
const waiting = out.hires.filter(x => x.state === 'awaiting-owner');
|
||||
assert.equal(waiting.length, 2);
|
||||
// The packs read marks each member-raised line as raised by a pack hire.
|
||||
const open = (await h.call('packs.console')).applied.find(a => a.id === 'mixkit').open;
|
||||
assert.deepEqual(open.map(o => [o.name, o.state, o.fromPack]), [['coder-1', 'awaiting-owner', true], ['reviewer-1', 'awaiting-owner', true]]);
|
||||
// The chief loses its route; the owner's pack hire cannot build a replacement.
|
||||
setRoute(null);
|
||||
const owner = await h.call('pack.hire.console', { pack: 'mixkit' });
|
||||
assert.deepEqual(owner.hires.map(x => [x.name, x.state, x.code ?? null]), [['scout-1', 'exists', null], ['coder-1', 'refused', 'pack-hire-route-missing'], ['reviewer-1', 'refused', 'pack-hire-route-missing']]);
|
||||
// Both inbox decisions are still open: nothing withdrew them.
|
||||
for (const x of waiting) assert.deepEqual(h.store.all('SELECT op FROM decision_events WHERE decision=?', x.decision), []);
|
||||
// With the route back, the owner's pack hire replaces them, withdrawing each only once its replacement applied.
|
||||
setRoute(ROUTE);
|
||||
const done = await h.call('pack.hire.console', { pack: 'mixkit' });
|
||||
assert.deepEqual(done.hires.map(x => [x.name, x.state, x.supersedes ?? null]), [['scout-1', 'exists', null], ['coder-1', 'hired', waiting[0].decision], ['reviewer-1', 'hired', waiting[1].decision]]);
|
||||
for (const x of waiting) assert.deepEqual(h.store.all('SELECT op, by FROM decision_events WHERE decision=?', x.decision).map(r => [r.op, r.by]), [['withdrawn', 'jason']]);
|
||||
});
|
||||
|
||||
test('PD a second pack hire on the same organization while one runs refuses organization-busy and raises nothing', async t => {
|
||||
const h = await harness(t, { route: false });
|
||||
await apply(h.call);
|
||||
const doc = h.file();
|
||||
doc.roles.chief.vars = { ...ROUTE };
|
||||
writeFileSync(h.path, JSON.stringify(doc, null, 2) + '\n');
|
||||
const first = h.call('pack.hire.console', { pack: 'coding' });
|
||||
const second = h.call('pack.hire.console', { pack: 'coding' });
|
||||
await assert.rejects(second, /organization-busy/);
|
||||
const hired = await first;
|
||||
assert.deepEqual(hired.hires.map(x => [x.name, x.state]), [['pm-1', 'hired'], ['coder-1', 'hired'], ['reviewer-1', 'hired']]);
|
||||
// One decision per hire: the refused call raised nothing.
|
||||
assert.equal(h.op('decision.raise').length, 3);
|
||||
// Once the loop ends the latch is free again: the next pack hire runs and finds nothing to hire.
|
||||
const later = await h.call('pack.hire.console', { pack: 'coding' });
|
||||
assert.ok(later.hires.every(x => x.state === 'exists'), JSON.stringify(later));
|
||||
});
|
||||
|
||||
// Lane pr2 (lead ruling 2026-11-14): a pack hire takes the chief of staff's harness and model, else
|
||||
// the org assistant's, else refuses pack-hire-route-missing.
|
||||
test('PR2 with no chief route the apply hires every proposal with the org assistant harness and model', async t => {
|
||||
const h = await harness(t, { route: false, assistant: ASSISTANT_ROUTE });
|
||||
const { out } = await apply(h.call);
|
||||
assert.deepEqual(out.hires.map(x => [x.name, x.state, x.routeFrom]), [['pm-1', 'hired', 'assistant'], ['coder-1', 'hired', 'assistant'], ['reviewer-1', 'hired', 'assistant']]);
|
||||
const doc = h.file();
|
||||
for (const x of out.hires) {
|
||||
assertHireRecord(h, x.name, x.decision);
|
||||
assert.deepEqual(doc.roles[x.name].vars, ASSISTANT_ROUTE);
|
||||
assert.deepEqual(h.op('decision.raise').find(r => r.decision === x.decision).hire.route, ASSISTANT_ROUTE);
|
||||
}
|
||||
});
|
||||
|
||||
test('PR2 a chief route wins over a different assistant route', async t => {
|
||||
const h = await harness(t, { assistant: ASSISTANT_ROUTE });
|
||||
const { out } = await apply(h.call);
|
||||
assert.deepEqual(out.hires.map(x => [x.name, x.state, x.routeFrom]), [['pm-1', 'hired', 'chief'], ['coder-1', 'hired', 'chief'], ['reviewer-1', 'hired', 'chief']]);
|
||||
for (const x of out.hires) assert.deepEqual(h.file().roles[x.name].vars, ROUTE);
|
||||
});
|
||||
|
||||
test('PR2 an assistant with no harness and model gives no route: the apply refuses as before', async t => {
|
||||
for (const assistant of [{}, { harness: 'pi' }]) {
|
||||
const h = await harness(t, { route: false, assistant });
|
||||
const { out } = await apply(h.call);
|
||||
assert.deepEqual(out.hires.map(x => [x.name, x.state, x.code]), [['pm-1', 'refused', 'pack-hire-route-missing'], ['coder-1', 'refused', 'pack-hire-route-missing'], ['reviewer-1', 'refused', 'pack-hire-route-missing']]);
|
||||
assert.equal(h.op('decision.raise').length, 0);
|
||||
}
|
||||
});
|
||||
|
||||
test('PR2 with several assistants the one named assistant gives the route, else the first in key order', async t => {
|
||||
const OTHER = { harness: 'pi', model: 'claude-sonnet-5-5' };
|
||||
const named = await harness(t, { route: false, assistant: ASSISTANT_ROUTE, mutate: doc => { doc.roles = { aide: { definition: 'assistant', vars: { ...OTHER } }, ...doc.roles }; } });
|
||||
const a = await apply(named.call);
|
||||
for (const x of a.out.hires) assert.deepEqual(named.file().roles[x.name].vars, ASSISTANT_ROUTE);
|
||||
const first = await harness(t, { route: false, mutate: doc => { doc.roles.aide = { definition: 'assistant', vars: { ...OTHER } }; doc.roles.zed = { definition: 'assistant', vars: { ...ASSISTANT_ROUTE } }; } });
|
||||
const b = await apply(first.call);
|
||||
assert.deepEqual(b.out.hires.map(x => [x.state, x.routeFrom]), [['hired', 'assistant'], ['hired', 'assistant'], ['hired', 'assistant']]);
|
||||
for (const x of b.out.hires) assert.deepEqual(first.file().roles[x.name].vars, OTHER);
|
||||
});
|
||||
|
||||
test('PR2 a member pack hire on the assistant route marks hired and awaiting-owner lines routeFrom assistant', async t => {
|
||||
const h = await harness(t, { route: false, assistant: ASSISTANT_ROUTE });
|
||||
h.install(mixkit(h.rolesDir));
|
||||
// The member's apply would not run; the owner's apply would hire everything. Record the pack first
|
||||
// with the assistant route absent, then give it back and let the member hire.
|
||||
const doc0 = h.file(); delete doc0.roles.assistant.vars; writeFileSync(h.path, JSON.stringify(doc0, null, 2) + '\n');
|
||||
await apply(h.call, { pack: 'mixkit' });
|
||||
const doc = h.file(); doc.roles.assistant.vars = { ...ASSISTANT_ROUTE }; writeFileSync(h.path, JSON.stringify(doc, null, 2) + '\n');
|
||||
const out = await h.call('pack.hire.console', { pack: 'mixkit' }, h.member);
|
||||
assert.deepEqual(out.hires.map(x => [x.name, x.state, x.routeFrom]), [['scout-1', 'hired', 'assistant'], ['coder-1', 'awaiting-owner', 'assistant'], ['reviewer-1', 'awaiting-owner', 'assistant']]);
|
||||
assert.deepEqual(h.file().roles['scout-1'].vars, ASSISTANT_ROUTE);
|
||||
});
|
||||
|
||||
test('PR2 a project pack with no chief route hires into the project on the assistant route', async t => {
|
||||
const h = await harness(t, { route: false, assistant: ASSISTANT_ROUTE });
|
||||
const pack = structuredClone(codingPack());
|
||||
pack.id = 'projkit'; pack.scope = 'project';
|
||||
h.install(pack);
|
||||
const { out } = await apply(h.call, { pack: 'projkit', project: 'stack' });
|
||||
assert.deepEqual(out.hires.map(x => [x.state, x.routeFrom]), [['hired', 'assistant'], ['hired', 'assistant'], ['hired', 'assistant']]);
|
||||
const agents = h.file().projects.stack.agents;
|
||||
for (const x of out.hires) { assert.equal(agents[x.name].hireDecision, x.decision); assert.deepEqual(h.op('decision.raise').find(r => r.decision === x.decision).hire.route, ASSISTANT_ROUTE); }
|
||||
});
|
||||
@@ -0,0 +1,384 @@
|
||||
// #1873 SP5: the pack, board and workflow verbs for an agent whose role file lists
|
||||
// organization.change, and hire.raise from a pack's recorded proposal (design section 3, "The
|
||||
// chief applying a pack", and section 8, SP5). The chief's authority is read from the shipped
|
||||
// roles/chief-of-staff.json, so the test pins the grant the stack runs.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { Broker } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { REFUSALS } from '../src/refusals.mjs';
|
||||
import { pluginHash } from '../../plugins/src/pinned.mjs';
|
||||
import { installPlugin } from '../../plugins/src/install.mjs';
|
||||
import { starterPack as codingPack } from '../../plugins/starter-pack/coding/index.mjs';
|
||||
import { hireDecisions } from '../../organization/src/hire-decisions.mjs';
|
||||
import { organizationWriter } from '../../organization/src/organization.mjs';
|
||||
import { loadBusiness } from '../../organization/src/organization-config.mjs';
|
||||
import { compositionOf, entryFromTemplate, readTemplate, templateHash } from '../../organization/src/definitions.mjs';
|
||||
import { canonicalJson } from '../../organization/src/util.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
const shipped = (name) => JSON.parse(readFileSync(fileURLToPath(new URL(`../../../roles/${name}.json`, import.meta.url)), 'utf8'));
|
||||
const chiefAuthority = shipped('chief-of-staff').authority;
|
||||
const sha = (text) => createHash('sha256').update(text).digest('hex');
|
||||
const ROUTE = { harness: 'pi', model: 'claude-opus-5-5' };
|
||||
|
||||
// The SP4 harness (packs.test.mjs) with the shipped chief grant and a coder without it. With
|
||||
// fromFile, the broker boots from the loaded organization file, as autonomy-authority.test.mjs does,
|
||||
// so the reload after a write reaches the broker and a later verb reads what was written.
|
||||
async function harness(t, { mutate = () => {}, fromFile = false } = {}) {
|
||||
const { startBroker } = await import('../src/runtime.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'packs5-'));
|
||||
const rolesDir = rolesCopy(root);
|
||||
const doc = businessDoc(root, 'demo');
|
||||
doc.plugins = { harness: ['pi'] };
|
||||
mutate(doc);
|
||||
writeJson(join(root, 'businesses/demo.json'), doc);
|
||||
const data = join(root, 'data');
|
||||
mkdirSync(data, { mode: 0o700 });
|
||||
const roles = {
|
||||
chief: { definition: 'chief-of-staff', authority: chiefAuthority },
|
||||
coder: { definition: 'coder', authority: { withinRole: ['message.send'], crossRole: [] } },
|
||||
};
|
||||
const loaded = () => { const l = loadBusiness('demo', { dir: root, rolesDir }); return { ...l, roles: Object.fromEntries(Object.entries(l.roles).map(([n, e]) => [n, { ...l.definitions[e.definition], ...e }])) }; };
|
||||
const businesses = { demo: fromFile ? loaded() : { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles } };
|
||||
const bus = await startBroker({ dataRoot: data, businesses, organization: { dir: root, rolesDir, system: systemFor(root) } });
|
||||
const thaw = (p) => { const st = lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { chmodSync(p, 0o700); for (const e of readdirSync(p)) thaw(join(p, e)); } };
|
||||
t.after(async () => { await bus.close(); thaw(root); rmSync(root, { recursive: true, force: true }); });
|
||||
const b = bus.broker;
|
||||
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const chief = b.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi', address: 'chief-run' });
|
||||
const coder = b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi', address: 'coder-run' });
|
||||
b.request(chief, { verb: 'role.claim' });
|
||||
b.request(coder, { verb: 'role.claim' });
|
||||
const call = async (cap, verb, args = {}) => b.request(cap, { verb, args });
|
||||
const path = join(root, 'businesses/demo.json');
|
||||
const file = () => JSON.parse(readFileSync(path, 'utf8'));
|
||||
const revision = () => b.request(human, { verb: 'limits' }).revision;
|
||||
let n = 0;
|
||||
const install = (pack) => {
|
||||
const src = join(root, `src-${n++}`);
|
||||
mkdirSync(join(src, 'starter-pack'), { recursive: true });
|
||||
writeFileSync(join(src, 'starter-pack', 'index.mjs'), `export const interfaceVersion = 1;\nexport function starterPack() { return ${JSON.stringify(pack)}; }\n`);
|
||||
const manifest = { name: pack.id, version: '1.0.0', kinds: ['starter-pack'], interfaceVersions: { 'starter-pack': 1 }, sha256: '0'.repeat(64),
|
||||
source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
|
||||
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
manifest.sha256 = pluginHash(src);
|
||||
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
installPlugin({ dataRoot: data, source: src, expectedHash: manifest.sha256 });
|
||||
};
|
||||
// The broker's own database, read-only: grants spent and decisions raised.
|
||||
const query = (sql) => { const db = new DatabaseSync(join(data, 'bus/bus.sqlite'), { readOnly: true }); try { return db.prepare(sql).all(); } finally { db.close(); } };
|
||||
const spent = () => query("SELECT body FROM events WHERE kind='action.allowed' ORDER BY seq").map(e => JSON.parse(e.body)).filter(e => e.action === 'organization.change').length;
|
||||
const decisions = () => query('SELECT count(*) n FROM decisions')[0].n;
|
||||
return { b, root, rolesDir, path, human, chief, coder, call, file, revision, install, spent, decisions };
|
||||
}
|
||||
const variant = (id, patch = () => {}) => { const p = structuredClone(codingPack()); p.id = id; patch(p); return p; };
|
||||
const marks = (preview) => Object.fromEntries(preview.items.map(i => [i.path, i.mark]));
|
||||
|
||||
test('SP5 the chief applies the coding pack: board and workflows written, no definitions, template pins, one grant spent', async t => {
|
||||
const { call, chief, file, path, rolesDir, spent, decisions } = await harness(t);
|
||||
const before = file();
|
||||
const preview = await call(chief, 'pack.preview', { pack: 'coding' });
|
||||
assert.equal(spent(), 0);
|
||||
assert.deepEqual(readFileSync(path, 'utf8'), JSON.stringify(before, null, 2) + '\n');
|
||||
// A definition a human apply would add is not written for an agent; its hire pins the template.
|
||||
for (const name of ['coder', 'cto', 'merge-gate', 'pm', 'reviewer', 'security-reviewer']) assert.equal(marks(preview)[`definitions.${name}`], 'not-written');
|
||||
assert.equal(marks(preview).board, 'add');
|
||||
assert.equal(marks(preview)['workflows.change'], 'add');
|
||||
assert.equal(preview.plan.definitions, undefined);
|
||||
for (const h of preview.hires) {
|
||||
assert.equal(h.mark, 'propose');
|
||||
assert.deepEqual(h.pin, { template: h.definition, sha256: templateHash(rolesDir, h.definition) });
|
||||
}
|
||||
await call(chief, 'pack.apply', { pack: 'coding', digest: preview.digest });
|
||||
const after = file();
|
||||
assert.equal(after.board.layout, 'kanban');
|
||||
assert.deepEqual(after.board, codingPack().board);
|
||||
assert.deepEqual(after.workflows.change.steps.map(s => s.roles), [['pm'], ['coder'], ['reviewer'], ['human', 'merge-gate']]);
|
||||
assert.deepEqual(after.definitions ?? null, before.definitions ?? null);
|
||||
assert.equal(after.packs.length, 1);
|
||||
assert.equal(after.packs[0].by, 'chief-of-staff.chief');
|
||||
assert.deepEqual(after.packs[0].added.sort(), ['board', 'workflows.change']);
|
||||
assert.deepEqual(after.packs[0].proposedHires.map(h => [h.name, h.pin.template]), [['pm-1', 'pm'], ['coder-1', 'coder'], ['reviewer-1', 'reviewer']]);
|
||||
assert.equal(spent(), 1);
|
||||
assert.equal(decisions(), 0);
|
||||
});
|
||||
|
||||
test('SP5 a repeat apply that writes nothing spends no grant', async t => {
|
||||
const { call, chief, file, spent } = await harness(t);
|
||||
const first = await call(chief, 'pack.preview', { pack: 'coding' });
|
||||
await call(chief, 'pack.apply', { pack: 'coding', digest: first.digest });
|
||||
assert.equal(spent(), 1);
|
||||
const before = file();
|
||||
// The same pack again: the board and workflow are the same and the proposals unchanged, so the
|
||||
// runtime skips the write and the broker spends nothing.
|
||||
const again = await call(chief, 'pack.preview', { pack: 'coding' });
|
||||
const out = await call(chief, 'pack.apply', { pack: 'coding', digest: again.digest });
|
||||
assert.deepEqual(out.added, []);
|
||||
assert.deepEqual(file(), before);
|
||||
assert.equal(spent(), 1);
|
||||
});
|
||||
|
||||
test('SP5 end to end: the chief applies a pack, the organization reloads, and the chief raises a hire from the recorded proposal', async t => {
|
||||
const { call, chief, file, rolesDir, decisions } = await harness(t, { fromFile: true });
|
||||
const preview = await call(chief, 'pack.preview', { pack: 'coding' });
|
||||
await call(chief, 'pack.apply', { pack: 'coding', digest: preview.digest });
|
||||
assert.equal(decisions(), 0);
|
||||
// The raise reads the packs list the broker reloaded after the write, not a fixture's.
|
||||
const d = await call(chief, 'hire.raise', { proposal: { pack: 'coding', hire: 'coder-1' }, route: ROUTE });
|
||||
assert.equal(d.hire.name, 'coder-1');
|
||||
assert.equal(d.hire.level, 'org');
|
||||
assert.equal(decisions(), 1);
|
||||
const direct = await call(chief, 'hire.raise', { op: 'hire', level: 'org', name: 'coder-b', role: { template: 'coder', sha256: templateHash(rolesDir, 'coder') }, route: ROUTE, reason: 'Code.' });
|
||||
assert.deepEqual(d.hire.role, direct.hire.role);
|
||||
assert.deepEqual(d.hire.flagged, direct.hire.flagged);
|
||||
// A second preview leaves out the hire the open decision names; the file is unchanged by the raise.
|
||||
assert.equal((await call(chief, 'pack.preview', { pack: 'coding' })).hires.find(h => h.name === 'coder-1').mark, 'dropped');
|
||||
assert.equal(file().packs.length, 1);
|
||||
});
|
||||
|
||||
test('SP5 a project pack: the chief applies it to a code project and raises the project hire from it', async t => {
|
||||
const { call, chief, file, install, decisions } = await harness(t, { fromFile: true });
|
||||
install(variant('projkit', p => { p.scope = 'project'; }));
|
||||
const preview = await call(chief, 'pack.preview', { pack: 'projkit', project: 'stack' });
|
||||
assert.equal(marks(preview)['projects.stack.board'], 'add');
|
||||
assert.equal(marks(preview)['definitions.coder'], 'not-written');
|
||||
await call(chief, 'pack.apply', { pack: 'projkit', project: 'stack', digest: preview.digest });
|
||||
assert.deepEqual(file().projects.stack.board, codingPack().board);
|
||||
assert.equal(file().packs.at(-1).project, 'stack');
|
||||
// The proposal names its project; without it, no org-level entry matches.
|
||||
await assert.rejects(call(chief, 'hire.raise', { proposal: { pack: 'projkit', hire: 'coder-1' }, route: ROUTE }), { code: 'invalid-request' });
|
||||
const d = await call(chief, 'hire.raise', { proposal: { pack: 'projkit', project: 'stack', hire: 'coder-1' }, route: ROUTE });
|
||||
assert.equal(d.hire.level, 'project');
|
||||
assert.equal(d.hire.project, 'stack');
|
||||
assert.equal(decisions(), 1);
|
||||
});
|
||||
|
||||
test('SP5 an inline pack definition that shares a template name does not keep its workflow step', async t => {
|
||||
const { call, chief, install, rolesDir } = await harness(t);
|
||||
const { rev: _rev, ...entry } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
|
||||
install(variant('shadowkit', p => {
|
||||
p.definitions = { reviewer: { ...entry, instructions: entry.instructions + '\nShadow.' } };
|
||||
p.hires = [];
|
||||
p.workflows = { check: { description: 'Check.', steps: [{ id: 'look', title: 'Look', state: 'todo', roles: ['human'] }, { id: 'review', title: 'Review it', state: 'in-review', roles: ['reviewer'] }] } };
|
||||
}));
|
||||
const preview = await call(chief, 'pack.preview', { pack: 'shadowkit' });
|
||||
const item = preview.items.find(i => i.path === 'workflows.check');
|
||||
assert.equal(item.mark, 'add');
|
||||
assert.deepEqual(item.leftOut, [{ step: 2, title: 'Review it', roles: ['reviewer'] }]);
|
||||
assert.deepEqual(preview.plan.workflows.check.steps.map(s => s.roles), [['human']]);
|
||||
});
|
||||
|
||||
test('SP5 a role without organization.change gets decision-required for the writes and human-required for the packs read', async t => {
|
||||
const { call, coder, chief, path, revision, install, spent } = await harness(t);
|
||||
const before = readFileSync(path);
|
||||
for (const [verb, args] of [['pack.apply', { pack: 'coding', digest: 'a'.repeat(64) }], ['board.set', { revision: revision(), board: null }],
|
||||
['workflow.set', { revision: revision(), name: 'x', workflow: null }], ['pack.preview', { pack: 'coding' }]])
|
||||
await assert.rejects(call(coder, verb, args), /decision-required/, verb);
|
||||
await assert.rejects(call(coder, 'packs'), /human-required/);
|
||||
// The console names stay the owner's.
|
||||
await assert.rejects(call(chief, 'pack.preview.console', { pack: 'coding' }), /human-required/);
|
||||
await assert.rejects(call(chief, 'packs.console'), /human-required/);
|
||||
assert.deepEqual(readFileSync(path), before);
|
||||
// The chief's packs read lists shipped and installed packs and spends nothing.
|
||||
install(variant('teamkit'));
|
||||
const listed = (await call(chief, 'packs')).packs;
|
||||
assert.ok(listed.some(p => p.id === 'coding' && p.origin === 'shipped'));
|
||||
assert.ok(listed.some(p => p.id === 'teamkit' && p.origin === 'installed'));
|
||||
assert.equal(spent(), 0);
|
||||
// The agent request carries no origin, actor or unknown key.
|
||||
await assert.rejects(call(chief, 'pack.preview', { pack: 'coding', actor: 'jason' }), /invalid-request/);
|
||||
await assert.rejects(call(chief, 'packs', { all: true }), /invalid-request/);
|
||||
});
|
||||
|
||||
test('SP5 the chief writes a board and a workflow, each spending the grant once, with its own actor', async t => {
|
||||
const { call, chief, file, revision, spent } = await harness(t);
|
||||
await call(chief, 'board.set', { revision: revision(), board: codingPack().board });
|
||||
assert.deepEqual(file().board, codingPack().board);
|
||||
assert.equal(spent(), 1);
|
||||
const workflow = { description: 'Intake.', steps: [{ title: 'Take it', state: 'todo', roles: ['human'] }, { title: 'Build it', state: 'in-progress', roles: ['coder'] }] };
|
||||
await call(chief, 'workflow.set', { revision: revision(), name: 'intake', workflow });
|
||||
assert.deepEqual(file().workflows.intake, workflow);
|
||||
assert.equal(spent(), 2);
|
||||
await call(chief, 'workflow.set', { revision: revision(), name: 'intake', workflow: null });
|
||||
assert.equal(file().workflows, undefined);
|
||||
assert.equal(spent(), 3);
|
||||
// A refused write spends nothing.
|
||||
await assert.rejects(call(chief, 'board.set', { revision: 'b'.repeat(64), board: null }), { code: 'revision-conflict' });
|
||||
await assert.rejects(call(chief, 'workflow.set', { revision: revision(), name: 'x', workflow: { description: 'x', steps: [{ title: 'Do', state: 'todo', roles: ['nobody-at-all'] }] } }), /invalid-request/);
|
||||
assert.equal(spent(), 3);
|
||||
});
|
||||
|
||||
test('SP5 an agent apply never writes a definition or a saved template: human-required', async t => {
|
||||
const { call, chief, path, root, rolesDir } = await harness(t);
|
||||
const before = readFileSync(path);
|
||||
// A definitions rename names a definition to write.
|
||||
await assert.rejects(call(chief, 'pack.preview', { pack: 'coding', as: ['definitions.coder=dev'] }), /human-required/);
|
||||
await assert.rejects(call(chief, 'pack.apply', { pack: 'coding', as: ['definitions.coder=dev'], digest: 'a'.repeat(64) }), /human-required/);
|
||||
// A saved template refuses before anything is read; the name need not exist.
|
||||
await assert.rejects(call(chief, 'pack.preview', { pack: 'saved:x' }), /human-required/);
|
||||
await assert.rejects(call(chief, 'pack.apply', { pack: 'saved:x', digest: 'a'.repeat(64) }), /human-required/);
|
||||
// A workflows rename is allowed.
|
||||
const renamed = await call(chief, 'pack.preview', { pack: 'coding', as: ['workflows.change=ship'] });
|
||||
assert.equal(marks(renamed)['workflows.ship'], 'add');
|
||||
assert.deepEqual(readFileSync(path), before);
|
||||
// The writer's own fence: the chief's actor with a definition in the plan, or a saved origin.
|
||||
const writer = organizationWriter({ dir: root, rolesDir });
|
||||
const { rev: _rev, ...entry } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
|
||||
const pack = { id: 'kit', version: '1.0.0', sha256: 'a'.repeat(64), origin: 'installed', scope: 'org', project: null, proposedHires: [] };
|
||||
const opts = () => ({ revision: writer.read('demo').revision, actor: 'chief-of-staff.chief', assertClean: () => {}, detail: {} });
|
||||
assert.throws(() => writer.applyPack('demo', { definitions: { dev: entry }, packs: pack }, opts()), /human-required/);
|
||||
assert.throws(() => writer.applyPack('demo', { board: codingPack().board, packs: { ...pack, origin: 'saved' } }, opts()), /human-required/);
|
||||
assert.deepEqual(readFileSync(path), before);
|
||||
});
|
||||
|
||||
test('SP5 a workflow step naming a definition the agent apply cannot write is left out and marked', async t => {
|
||||
const { call, chief, file, install, rolesDir } = await harness(t);
|
||||
const { rev: _rev, ...scribe } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
|
||||
install(variant('scribekit', p => {
|
||||
p.definitions = { scribe, pm: p.definitions.pm };
|
||||
p.hires = [{ name: 'scribe-1', definition: 'scribe', reason: 'Take notes.' }];
|
||||
p.workflows = {
|
||||
notes: { description: 'Notes.', steps: [{ id: 'plan', title: 'Plan it', state: 'todo', roles: ['pm'] }, { id: 'write', title: 'Write it', state: 'in-progress', roles: ['scribe', 'human'] }] },
|
||||
solo: { description: 'Solo.', steps: [{ id: 'write', title: 'Write it', state: 'todo', roles: ['scribe'] }] },
|
||||
};
|
||||
}));
|
||||
const preview = await call(chief, 'pack.preview', { pack: 'scribekit' });
|
||||
assert.equal(marks(preview)['definitions.scribe'], 'not-written');
|
||||
const items = Object.fromEntries(preview.items.map(i => [i.path, i]));
|
||||
assert.equal(items['workflows.notes'].mark, 'add');
|
||||
assert.deepEqual(items['workflows.notes'].leftOut, [{ step: 2, title: 'Write it', roles: ['scribe'] }]);
|
||||
assert.equal(items['workflows.solo'].mark, 'left-out');
|
||||
assert.deepEqual(items['workflows.solo'].leftOut, [{ step: 1, title: 'Write it', roles: ['scribe'] }]);
|
||||
assert.deepEqual(Object.keys(preview.plan.workflows), ['notes']);
|
||||
// The full-entry definition's hire pins the inline entry.
|
||||
const hire = preview.hires.find(h => h.name === 'scribe-1');
|
||||
assert.deepEqual(hire.pin, { inline: 'scribe', sha256: sha(canonicalJson(scribe)) });
|
||||
await call(chief, 'pack.apply', { pack: 'scribekit', digest: preview.digest });
|
||||
assert.deepEqual(file().workflows, { notes: { description: 'Notes.', steps: [{ title: 'Plan it', state: 'todo', roles: ['pm'] }] } });
|
||||
assert.equal(file().definitions?.scribe, undefined);
|
||||
assert.deepEqual(file().packs.at(-1).proposedHires[0].entry, scribe);
|
||||
});
|
||||
|
||||
// The hire-decisions.test.mjs fixture: a Broker on the loaded organization and the hire service.
|
||||
function fixture(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'sp5-bus-')), rolesDir = rolesCopy(root);
|
||||
const file = writeJson(join(root, 'businesses/demo.json'), { ...businessDoc(root, 'demo'), plugins: { harness: ['pi'] } });
|
||||
const storeRoot = join(root, 'data'); mkdirSync(storeRoot, { mode: 0o700 });
|
||||
const writer = organizationWriter({ dir: root, rolesDir }), store = new Store(storeRoot);
|
||||
const service = hireDecisions({ writer, rolesDir, dataRoot: storeRoot, configDir: root, assertClean: () => {} });
|
||||
const loaded = () => { const doc = loadBusiness('demo', { dir: root, rolesDir }); return { ...doc, roles: Object.fromEntries(Object.entries(doc.roles).map(([n, e]) => [n, { ...doc.definitions[e.definition], ...e }])) }; };
|
||||
const b = new Broker({ store, businesses: { demo: loaded() }, hires: service });
|
||||
const caps = { chief: b.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi' }), coder: b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi' }) };
|
||||
b.request(caps.chief, { verb: 'role.claim' }); b.request(caps.coder, { verb: 'role.claim' });
|
||||
t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
|
||||
const call = (cap, verb, args = {}) => b.request(cap, { verb, args });
|
||||
// A packs entry as an owner's apply records it, then the broker's organization reloads.
|
||||
const record = (plan) => {
|
||||
writer.applyPack('demo', plan, { revision: writer.read('demo').revision, actor: 'jason', assertClean: () => {}, detail: {} });
|
||||
b.reloadBusiness('demo', loaded());
|
||||
};
|
||||
const pack = (proposedHires, extra = {}) => ({ id: 'kit', version: '1.0.0', sha256: 'a'.repeat(64), origin: 'installed', scope: 'org', project: null, proposedHires, ...extra });
|
||||
// Edit the organization file directly, as a later change would, then reload.
|
||||
const edit = (change) => { const doc = JSON.parse(readFileSync(file, 'utf8')); change(doc); writeJson(file, doc); b.reloadBusiness('demo', loaded()); };
|
||||
const raise = (proposal, extra = {}) => call(caps.chief, 'hire.raise', { proposal, route: ROUTE, ...extra });
|
||||
const count = () => store.get('SELECT count(*) n FROM decisions').n;
|
||||
return { root, rolesDir, b, store, file, writer, caps, call, record, pack, edit, raise, count, loaded };
|
||||
}
|
||||
|
||||
test('SP5 a hire raised from a template proposal carries the broker-computed role and lines, as a direct raise does', async t => {
|
||||
const { rolesDir, record, pack, raise, call, caps, count } = fixture(t);
|
||||
const pin = { template: 'reviewer', sha256: templateHash(rolesDir, 'reviewer') };
|
||||
record({ packs: pack([{ name: 'scout', reason: 'Review the pack work.', pin }]) });
|
||||
const d = await raise({ pack: 'kit', hire: 'scout' });
|
||||
assert.equal(d.hire.name, 'scout');
|
||||
assert.equal(d.hire.level, 'org');
|
||||
// The lines are the broker's, the same as a direct raise of the same role computes.
|
||||
const direct = await call(caps.chief, 'hire.raise', { op: 'hire', level: 'org', name: 'scout-b', role: pin, route: ROUTE, reason: 'Review.' });
|
||||
assert.deepEqual(d.hire.role, direct.hire.role);
|
||||
assert.deepEqual(d.hire.flagged, direct.hire.flagged);
|
||||
assert.deepEqual(d.hire.credentialLines, direct.hire.credentialLines);
|
||||
assert.ok(Array.isArray(d.hire.credentialLines));
|
||||
assert.equal(count(), 2);
|
||||
// hire.raise requires a reason and stores none. The raise above sent none, so the recorded one
|
||||
// filled it; a caller's own reason replaces it, and an empty one refuses as a direct raise does.
|
||||
await assert.rejects(raise({ pack: 'kit', hire: 'scout' }, { reason: '' }), { code: 'invalid-request' });
|
||||
assert.equal((await raise({ pack: 'kit', hire: 'scout' }, { reason: 'Mine.' })).hire.name, 'scout');
|
||||
assert.equal(count(), 3);
|
||||
});
|
||||
|
||||
test('SP5 a proposal raise: role-not-chief, decision-stale, definition-rev-conflict, starter-pack-proposal-stale, definition-not-hireable', async t => {
|
||||
const { rolesDir, record, pack, raise, call, caps, edit, count } = fixture(t);
|
||||
const { rev: _rev, ...scribe } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
|
||||
const inline = { name: 'inl', reason: 'Inline.', pin: { inline: 'scribe', sha256: sha(canonicalJson(scribe)) }, entry: scribe };
|
||||
record({
|
||||
definitions: { helper: scribe },
|
||||
packs: pack([
|
||||
{ name: 'moved', reason: 'Moved.', pin: { template: 'reviewer', sha256: 'b'.repeat(64) } },
|
||||
{ name: 'def', reason: 'Def.', pin: { definition: 'helper', rev: 1 } },
|
||||
inline,
|
||||
]),
|
||||
});
|
||||
// Only the chief raises, with or without a proposal.
|
||||
await assert.rejects(call(caps.coder, 'hire.raise', { proposal: { pack: 'kit', hire: 'def' }, route: ROUTE }), { code: 'role-not-chief' });
|
||||
await assert.rejects(raise({ pack: 'kit', hire: 'moved' }), { code: 'decision-stale' });
|
||||
edit(doc => { doc.definitions.helper = { ...doc.definitions.helper, rev: 2, instructions: doc.definitions.helper.instructions + '\nAlso this.' }; });
|
||||
await assert.rejects(raise({ pack: 'kit', hire: 'def' }), { code: 'definition-rev-conflict' });
|
||||
// The inline entry stored with the proposal still hashes to its pin, so it raises.
|
||||
const ok = await raise({ pack: 'kit', hire: 'inl' });
|
||||
assert.equal(ok.hire.name, 'inl');
|
||||
const direct = await call(caps.chief, 'hire.raise', { op: 'hire', level: 'org', name: 'inl-b', role: { inline: compositionOf(scribe) }, route: ROUTE, reason: 'Inline.' });
|
||||
assert.deepEqual(ok.hire.role, direct.hire.role);
|
||||
assert.equal(count(), 2);
|
||||
// A later edit to the stored entry is caught at the raise.
|
||||
edit(doc => { doc.packs[0].proposedHires[2].entry.instructions += '\nAnd more.'; });
|
||||
await assert.rejects(raise({ pack: 'kit', hire: 'inl' }), { code: 'starter-pack-proposal-stale' });
|
||||
// An inline entry from a template launch.noHire names refuses, though prepare reads no ref for it.
|
||||
edit(doc => { doc.packs[0].proposedHires[2].entry = scribe; doc.launch.noHire = ['coder']; });
|
||||
await assert.rejects(raise({ pack: 'kit', hire: 'inl' }), { code: 'definition-not-hireable' });
|
||||
assert.equal(count(), 2);
|
||||
});
|
||||
|
||||
test('SP5 a chief without role.hire is refused a proposal raise before the packs list is read', async t => {
|
||||
const { record, pack, raise, b, loaded, rolesDir, count } = fixture(t);
|
||||
record({ packs: pack([{ name: 'scout', reason: 'Review.', pin: { template: 'reviewer', sha256: templateHash(rolesDir, 'reviewer') } }]) });
|
||||
const doc = loaded();
|
||||
const authority = doc.roles.chief.authority;
|
||||
b.reloadBusiness('demo', { ...doc, roles: { ...doc.roles, chief: { ...doc.roles.chief, authority: { ...authority, withinRole: authority.withinRole.filter(a => a !== 'role.hire') } } } });
|
||||
await assert.rejects(raise({ pack: 'kit', hire: 'scout' }), { code: 'not-authorized' });
|
||||
// A proposal that does not exist gets the same refusal, so the refusal leaks nothing.
|
||||
await assert.rejects(raise({ pack: 'nosuch', hire: 'nobody' }), { code: 'not-authorized' });
|
||||
assert.equal(count(), 0);
|
||||
});
|
||||
|
||||
test('SP5 a proposal raise names no role, name, level or other hire key; unknown proposals refuse; a plain raise is unchanged', async t => {
|
||||
const { rolesDir, record, pack, raise, call, caps, count } = fixture(t);
|
||||
const pin = { template: 'reviewer', sha256: templateHash(rolesDir, 'reviewer') };
|
||||
record({ packs: pack([{ name: 'scout', reason: 'Review.', pin }]) });
|
||||
for (const extra of [{ role: pin }, { name: 'other' }, { level: 'org' }, { op: 'hire' }, { credentials: {} }, { personality: 'x' }, { binding: {} }])
|
||||
await assert.rejects(raise({ pack: 'kit', hire: 'scout' }, extra), { code: 'invalid-request' }, JSON.stringify(extra));
|
||||
for (const proposal of [{ pack: 'kit' }, { pack: 'kit', hire: 'nobody' }, { pack: 'other', hire: 'scout' }, { pack: 'kit', hire: 'scout', project: 'stack' },
|
||||
{ pack: 'kit', hire: 'scout', role: 'x' }, { pack: 'kit', hire: 7 }, 'kit'])
|
||||
await assert.rejects(raise(proposal), { code: 'invalid-request' }, JSON.stringify(proposal));
|
||||
await assert.rejects(call(caps.chief, 'hire.raise', { proposal: { pack: 'kit', hire: 'scout' } }), { code: 'invalid-request' });
|
||||
assert.equal(count(), 0);
|
||||
// Without proposal, hire.raise is what it was.
|
||||
const INLINE = { tools: ['read'], network: 'none', authority: { withinRole: [], crossRole: [] }, credentials: [], instructions: 'Read and report.' };
|
||||
const d = await call(caps.chief, 'hire.raise', { op: 'hire', level: 'org', name: 'plain', role: { inline: INLINE }, route: ROUTE, reason: 'Need help' });
|
||||
assert.equal(d.hire.name, 'plain');
|
||||
assert.equal(count(), 1);
|
||||
});
|
||||
|
||||
test('SP5 starter-pack-proposal-stale is in the refusal table with a rail and a next step that names no path', () => {
|
||||
const c = 'starter-pack-proposal-stale';
|
||||
assert.ok(Object.hasOwn(REFUSALS, c));
|
||||
assert.ok(REFUSALS[c].rail?.length > 5);
|
||||
assert.ok(REFUSALS[c].nextStep?.length > 20);
|
||||
assert.ok(!/(^|[\s("'`])(\/|~\/)/.test(REFUSALS[c].nextStep));
|
||||
});
|
||||
@@ -0,0 +1,402 @@
|
||||
// #1873 SP4: starter packs at the bus. The owner previews and applies a pack; the plan is the write.
|
||||
// Preview writes nothing; apply writes only what preview marked `add` plus one packs entry, and
|
||||
// raises no decision. board.set and workflow.set are one-key plans through the same writer.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { PACK_BUS_KEYS, TESTER_ORGANIZATION_VERBS } from '../src/broker.mjs';
|
||||
import { pluginHash } from '../../plugins/src/pinned.mjs';
|
||||
import { installPlugin } from '../../plugins/src/install.mjs';
|
||||
import { starterPack as codingPack } from '../../plugins/starter-pack/coding/index.mjs';
|
||||
|
||||
const TOKEN = 'fixture-held-token-sp4-7d1f2b';
|
||||
const SUBJECT = { provider: 'test', iss: 'test', sub: 'owner' };
|
||||
|
||||
async function harness(t, { mutate = () => {} } = {}) {
|
||||
const { startBroker } = await import('../src/runtime.mjs');
|
||||
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'packs-'));
|
||||
const rolesDir = rolesCopy(root);
|
||||
const doc = businessDoc(root, 'demo');
|
||||
doc.plugins = { harness: ['pi'] };
|
||||
mutate(doc);
|
||||
writeJson(join(root, 'businesses/demo.json'), doc);
|
||||
const other = businessDoc(join(root, 'b2'), 'other');
|
||||
other.human = 'alex';
|
||||
writeJson(join(root, 'businesses/other.json'), other);
|
||||
const data = join(root, 'data');
|
||||
mkdirSync(data, { mode: 0o700 });
|
||||
const token = join(root, 'held.token');
|
||||
writeFileSync(token, TOKEN, { mode: 0o600 });
|
||||
chmodSync(token, 0o600);
|
||||
const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } },
|
||||
coder: { authority: { withinRole: [], crossRole: [] }, credentials: { gitea: { file: token, rotateBy: '2099-01-01' } } } };
|
||||
const businesses = {
|
||||
demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, vars: { 'bus.tester': true }, roles },
|
||||
other: { id: 'other', human: 'alex', arbiters: { technical: 'chief', delivery: 'chief' }, roles: { chief: roles.chief } },
|
||||
};
|
||||
const bus = await startBroker({ dataRoot: data, businesses, organization: { dir: root, rolesDir, system: systemFor(root) } });
|
||||
// An installed plugin's directories are read-only; open them so the scratch tree can go.
|
||||
const thaw = (p) => { const st = lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { chmodSync(p, 0o700); for (const e of readdirSync(p)) thaw(join(p, e)); } };
|
||||
t.after(async () => { await bus.close(); thaw(root); rmSync(root, { recursive: true, force: true }); });
|
||||
const b = bus.broker;
|
||||
const consoleOwner = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT });
|
||||
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const call = async (verb, args = {}, cap = consoleOwner) => b.request(cap, { verb, args });
|
||||
const path = join(root, 'businesses/demo.json');
|
||||
const file = () => JSON.parse(readFileSync(path, 'utf8'));
|
||||
const revision = () => b.request(human, { verb: 'limits' }).revision;
|
||||
// A pack installed on this machine (origin installed), as the plugin installer writes it.
|
||||
let n = 0;
|
||||
const install = (pack) => {
|
||||
const src = join(root, `src-${n++}`);
|
||||
mkdirSync(join(src, 'starter-pack'), { recursive: true });
|
||||
writeFileSync(join(src, 'starter-pack', 'index.mjs'), `export const interfaceVersion = 1;\nexport function starterPack() { return ${JSON.stringify(pack)}; }\n`);
|
||||
const manifest = { name: pack.id, version: '1.0.0', kinds: ['starter-pack'], interfaceVersions: { 'starter-pack': 1 }, sha256: '0'.repeat(64),
|
||||
source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
|
||||
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
manifest.sha256 = pluginHash(src);
|
||||
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
installPlugin({ dataRoot: data, source: src, expectedHash: manifest.sha256 });
|
||||
};
|
||||
// A pack an owner saved, under <configDir>/templates/<account>/<name>.json (origin saved).
|
||||
const save = (account, name, pack) => {
|
||||
const dir = join(root, 'templates', account);
|
||||
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
chmodSync(join(root, 'templates'), 0o700);
|
||||
chmodSync(dir, 0o700);
|
||||
writeFileSync(join(dir, `${name}.json`), JSON.stringify(pack), { mode: 0o600 });
|
||||
};
|
||||
const decisions = () => ['roles', 'recent'].map(list => bus.broker.request(human, { verb: 'decisions', args: { list } }));
|
||||
return { bus, b, root, rolesDir, path, consoleOwner, human, call, file, revision, install, save, decisions };
|
||||
}
|
||||
|
||||
// A pack built from the shipped coding pack, renamed and patched.
|
||||
const variant = (id, patch = () => {}) => { const p = structuredClone(codingPack()); p.id = id; patch(p); return p; };
|
||||
const marks = (preview) => Object.fromEntries(preview.items.map(i => [i.path, i.mark]));
|
||||
|
||||
test('SP4 preview writes nothing; apply writes the add items and one packs entry, raises no decision, and a second apply is all same', async t => {
|
||||
const { call, file, path, decisions, root, rolesDir } = await harness(t);
|
||||
const before = readFileSync(path);
|
||||
const decided = JSON.stringify(decisions());
|
||||
const preview = await call('pack.preview.console', { pack: 'coding' });
|
||||
assert.deepEqual(readFileSync(path), before);
|
||||
assert.equal(preview.pack.origin, 'shipped');
|
||||
assert.ok(preview.items.every(i => i.mark === 'add'), JSON.stringify(preview.items));
|
||||
assert.deepEqual(preview.hires.map(h => [h.name, h.mark, h.noHire]), [['pm-1', 'propose', false], ['coder-1', 'propose', false], ['reviewer-1', 'propose', false]]);
|
||||
// The chief is known, so every proposed hire lists its flagged and credential lines.
|
||||
assert.equal(preview.chief, true);
|
||||
for (const h of preview.hires) { assert.ok(Array.isArray(h.flagged)); assert.ok(Array.isArray(h.credentialLines)); }
|
||||
// coder-1's credential lines name the services the coder template reaches.
|
||||
const coderServices = JSON.parse(readFileSync(join(rolesDir, 'coder.json'), 'utf8')).credentials.map(c => c.service).sort();
|
||||
assert.ok(coderServices.length > 0);
|
||||
const lines = preview.hires.find(h => h.name === 'coder-1').credentialLines;
|
||||
assert.ok(lines.length >= coderServices.length);
|
||||
assert.deepEqual([...new Set(lines.map(l => l.service))].sort(), [...new Set(coderServices)]);
|
||||
// On an empty data root every pack skill is not enabled; an enabled one drops out.
|
||||
assert.deepEqual(preview.skills.notEnabled, codingPack().skills);
|
||||
mkdirSync(join(root, 'data', 'skills-enabled', 'ms-sdlc'), { recursive: true });
|
||||
assert.deepEqual((await call('pack.preview.console', { pack: 'coding' })).skills.notEnabled, codingPack().skills.filter(n => n !== 'ms-sdlc'));
|
||||
assert.match(preview.digest, /^[0-9a-f]{64}$/);
|
||||
|
||||
const out = await call('pack.apply.console', { pack: 'coding', digest: preview.digest });
|
||||
assert.equal(out.pack, 'coding');
|
||||
const after = file();
|
||||
assert.deepEqual(Object.keys(after.definitions).sort(), ['coder', 'cto', 'merge-gate', 'pm', 'reviewer', 'security-reviewer']);
|
||||
assert.equal(after.board.layout, 'kanban');
|
||||
// The organization's workflow drops the pack's step ids.
|
||||
assert.deepEqual(after.workflows.change.steps[0], { title: 'Write the task', state: 'todo', roles: ['pm'] });
|
||||
assert.ok(after.workflows.change.steps.every(s => !Object.hasOwn(s, 'id')));
|
||||
assert.equal(after.packs.length, 1);
|
||||
const entry = after.packs[0];
|
||||
assert.equal(entry.id, 'coding');
|
||||
assert.equal(entry.origin, 'shipped');
|
||||
assert.ok(entry.added.includes('definitions.coder') && entry.added.includes('board') && entry.added.includes('workflows.change'));
|
||||
assert.deepEqual(entry.proposedHires.find(h => h.name === 'coder-1').pin, { definition: 'coder', rev: 1 });
|
||||
// No decision is raised: the owner hires from the proposals later.
|
||||
assert.equal(JSON.stringify(decisions()), decided);
|
||||
|
||||
const again = await call('pack.preview.console', { pack: 'coding' });
|
||||
assert.ok(again.items.every(i => i.mark === 'same'), JSON.stringify(again.items));
|
||||
await call('pack.apply.console', { pack: 'coding', digest: again.digest });
|
||||
assert.deepEqual(file().definitions, after.definitions);
|
||||
assert.deepEqual(file().workflows, after.workflows);
|
||||
// An apply that adds nothing and proposes what the last apply proposed writes nothing at all.
|
||||
const settled = readFileSync(path);
|
||||
const third = await call('pack.preview.console', { pack: 'coding' });
|
||||
const out3 = await call('pack.apply.console', { pack: 'coding', digest: third.digest });
|
||||
assert.deepEqual(out3.added, []);
|
||||
assert.equal(out3.revision, third.revision);
|
||||
assert.deepEqual(readFileSync(path), settled);
|
||||
assert.equal(file().packs.length, 2);
|
||||
});
|
||||
|
||||
test('SP4 a stale digest refuses pack-plan-stale and writes nothing', async t => {
|
||||
const { call, path, revision } = await harness(t);
|
||||
const preview = await call('pack.preview.console', { pack: 'coding' });
|
||||
await call('workflow.set.console', { revision: revision(), name: 'intake', workflow: { description: 'Intake.', steps: [{ title: 'Take it', state: 'todo', roles: ['human'] }] } });
|
||||
const before = readFileSync(path);
|
||||
await assert.rejects(call('pack.apply.console', { pack: 'coding', digest: preview.digest }), /pack-plan-stale/);
|
||||
await assert.rejects(call('pack.apply.console', { pack: 'coding', digest: 'f'.repeat(64) }), /pack-plan-stale/);
|
||||
assert.deepEqual(readFileSync(path), before);
|
||||
});
|
||||
|
||||
test('SP4 an edited definition is kept with its diff; a removed item stays removed; another pack naming it adds it', async t => {
|
||||
const { call, human, file, revision, install, save } = await harness(t);
|
||||
const first = await call('pack.preview.console', { pack: 'coding' });
|
||||
await call('pack.apply.console', { pack: 'coding', digest: first.digest });
|
||||
|
||||
const coder = file().definitions.coder;
|
||||
const { rev } = coder;
|
||||
const definition = { role: coder.role, instructions: `${coder.instructions}Keep each change small.\n`, ...(coder.personality === undefined ? {} : { personality: coder.personality }) };
|
||||
await call('definition.edit', { revision: revision(), rev, name: 'coder', definition }, human);
|
||||
await call('definition.remove', { revision: revision(), rev: file().definitions.reviewer.rev, name: 'reviewer' }, human);
|
||||
await call('board.set.console', { revision: revision(), board: null });
|
||||
await call('workflow.set.console', { revision: revision(), name: 'change', workflow: null });
|
||||
assert.equal(file().board, undefined);
|
||||
assert.equal(file().workflows?.change, undefined);
|
||||
|
||||
const next = await call('pack.preview.console', { pack: 'coding' });
|
||||
const m = marks(next);
|
||||
assert.equal(m['definitions.coder'], 'kept');
|
||||
assert.deepEqual(next.items.find(i => i.path === 'definitions.coder').diff.primitives, ['instructions']);
|
||||
assert.equal(m['definitions.reviewer'], 'removed');
|
||||
assert.equal(m.board, 'removed');
|
||||
assert.equal(m['workflows.change'], 'removed');
|
||||
assert.equal(m['definitions.pm'], 'same');
|
||||
// The reviewer instance, its definition gone, pins the template the pack names.
|
||||
assert.deepEqual(next.hires.find(h => h.name === 'reviewer-1').pin, { template: 'reviewer', sha256: codingPack().definitions.reviewer.sha256 });
|
||||
const kept = file();
|
||||
await call('pack.apply.console', { pack: 'coding', digest: next.digest });
|
||||
assert.deepEqual(file().definitions, kept.definitions);
|
||||
assert.equal(file().board, undefined);
|
||||
assert.deepEqual(file().packs.at(-1).added, []);
|
||||
|
||||
// A different pack naming the same items adds them: a tombstone is per pack.
|
||||
install(variant('teamkit'));
|
||||
const other = await call('pack.preview.console', { pack: 'teamkit' });
|
||||
assert.equal(other.pack.origin, 'installed');
|
||||
const o = marks(other);
|
||||
assert.equal(o['definitions.reviewer'], 'add');
|
||||
assert.equal(o.board, 'add');
|
||||
assert.equal(o['workflows.change'], 'add');
|
||||
assert.equal(o['definitions.coder'], 'kept');
|
||||
// A saved template reusing the shipped id is another pack: the shipped apply's removals don't bind it.
|
||||
save('jason', 'mine', variant('coding'));
|
||||
const mine = await call('pack.preview.console', { pack: 'saved:mine' });
|
||||
assert.equal(mine.pack.origin, 'saved');
|
||||
assert.equal(marks(mine)['definitions.reviewer'], 'add');
|
||||
assert.equal(marks(mine).board, 'add');
|
||||
});
|
||||
|
||||
test('SP4 --as renames a definition or workflow and pins the hire to the new name; a bad rename refuses', async t => {
|
||||
const { call, file } = await harness(t);
|
||||
const preview = await call('pack.preview.console', { pack: 'coding', as: ['definitions.coder=builder', 'workflows.change=ship'] });
|
||||
const m = marks(preview);
|
||||
assert.equal(m['definitions.builder'], 'add');
|
||||
assert.equal(m['workflows.ship'], 'add');
|
||||
assert.deepEqual(preview.hires.find(h => h.name === 'coder-1').pin, { definition: 'builder', rev: 1 });
|
||||
await call('pack.apply.console', { pack: 'coding', as: ['definitions.coder=builder', 'workflows.change=ship'], digest: preview.digest });
|
||||
assert.ok(file().definitions.builder);
|
||||
assert.equal(file().definitions.coder, undefined);
|
||||
assert.ok(file().workflows.ship);
|
||||
// A rename onto another pack item's own name would show both as add while one replaced the other.
|
||||
for (const as of [['definitions.nobody=x'], ['definitions.coder=a', 'definitions.coder=b'], ['definitions.coder=x', 'definitions.pm=x'], ['roles.coder=x'], 'definitions.coder=x', ['definitions.coder=reviewer'], ['definitions.coder=human'], ['definitions.merge-gate=human']])
|
||||
await assert.rejects(call('pack.preview.console', { pack: 'coding', as }), /invalid-request/);
|
||||
});
|
||||
|
||||
test('SP4 --as onto another workflow of the pack refuses; a renamed inline definition renames the workflow roles naming it', async t => {
|
||||
const { call, file, install, rolesDir } = await harness(t);
|
||||
install(variant('twoflow', p => { p.workflows.other = structuredClone(p.workflows.change); }));
|
||||
await assert.rejects(call('pack.preview.console', { pack: 'twoflow', as: ['workflows.change=other'] }), /invalid-request/);
|
||||
const { entryFromTemplate, readTemplate } = await import('../../organization/src/definitions.mjs');
|
||||
const { rev: _rev, ...scribe } = entryFromTemplate(readTemplate(rolesDir, 'coder'));
|
||||
install(variant('scribekit', p => {
|
||||
p.definitions = { scribe };
|
||||
p.hires = [];
|
||||
p.workflows = { notes: { description: 'Notes.', steps: [{ id: 'write', title: 'Write it', state: 'todo', roles: ['scribe', 'human'] }] } };
|
||||
}));
|
||||
const as = ['definitions.scribe=writer'];
|
||||
const preview = await call('pack.preview.console', { pack: 'scribekit', as });
|
||||
assert.equal(marks(preview)['definitions.writer'], 'add');
|
||||
assert.deepEqual(preview.plan.workflows.notes.steps[0].roles, ['writer', 'human']);
|
||||
await call('pack.apply.console', { pack: 'scribekit', as, digest: preview.digest });
|
||||
assert.ok(file().definitions.writer);
|
||||
assert.equal(file().definitions.scribe, undefined);
|
||||
assert.deepEqual(file().workflows.notes.steps[0].roles, ['writer', 'human']);
|
||||
// A rename onto a role the same step already names refuses at preview, not only at apply.
|
||||
install(variant('pairkit', p => {
|
||||
p.definitions = { scribe };
|
||||
p.hires = [];
|
||||
p.workflows = { notes: { description: 'Notes.', steps: [{ id: 'write', title: 'Write it', state: 'todo', roles: ['scribe', 'pm'] }] } };
|
||||
}));
|
||||
await call('pack.preview.console', { pack: 'pairkit' });
|
||||
await assert.rejects(call('pack.preview.console', { pack: 'pairkit', as: ['definitions.scribe=pm'] }), /invalid-request/);
|
||||
// The reserved human step role never follows a rename, even when the pack defines a definition by that name.
|
||||
install(variant('humankit', p => {
|
||||
p.definitions = { human: scribe };
|
||||
p.hires = [];
|
||||
p.workflows = { signoff: { description: 'Sign-off.', steps: [{ id: 'sign', title: 'Sign it', state: 'todo', roles: ['human'] }] } };
|
||||
}));
|
||||
const renamed = await call('pack.preview.console', { pack: 'humankit', as: ['definitions.human=bot'] });
|
||||
assert.deepEqual(renamed.plan.workflows.signoff.steps[0].roles, ['human']);
|
||||
});
|
||||
|
||||
test('SP4 a changed pack with nothing to add still records its entry; an identical repeat writes nothing', async t => {
|
||||
const { call, file, save } = await harness(t);
|
||||
save('jason', 'mine', variant('mine'));
|
||||
// The first apply pins the definitions it adds; the second pins the pack's template refs, so it
|
||||
// records an entry too. From the second on, the proposals are settled.
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const p = await call('pack.preview.console', { pack: 'saved:mine' });
|
||||
await call('pack.apply.console', { pack: 'saved:mine', digest: p.digest });
|
||||
}
|
||||
assert.equal(file().packs.length, 2);
|
||||
save('jason', 'mine', variant('mine', p => { p.version = '1.0.1'; p.description = 'A changed description.'; }));
|
||||
const changed = await call('pack.preview.console', { pack: 'saved:mine' });
|
||||
assert.ok(changed.items.every(i => i.mark === 'same'), JSON.stringify(changed.items));
|
||||
await call('pack.apply.console', { pack: 'saved:mine', digest: changed.digest });
|
||||
assert.equal(file().packs.length, 3);
|
||||
assert.equal(file().packs.at(-1).version, '1.0.1');
|
||||
assert.notEqual(file().packs.at(-1).sha256, file().packs[1].sha256);
|
||||
const repeat = await call('pack.preview.console', { pack: 'saved:mine' });
|
||||
const out = await call('pack.apply.console', { pack: 'saved:mine', digest: repeat.digest });
|
||||
assert.deepEqual(out.added, []);
|
||||
assert.equal(file().packs.length, 3);
|
||||
});
|
||||
|
||||
test('SP4 an instance by the hire name drops the proposal; launch.noHire marks it', async t => {
|
||||
const { call } = await harness(t, { mutate: doc => {
|
||||
doc.roles['coder-1'] = { ...structuredClone(doc.roles.reviewer), definition: 'coder', tracker: { bot: 'bot-demo-coder-1', botId: 7 } };
|
||||
doc.launch.noHire = ['pm'];
|
||||
} });
|
||||
const preview = await call('pack.preview.console', { pack: 'coding' });
|
||||
const hires = Object.fromEntries(preview.hires.map(h => [h.name, h]));
|
||||
assert.equal(hires['coder-1'].mark, 'dropped');
|
||||
assert.equal(hires['pm-1'].noHire, true);
|
||||
assert.equal(hires['reviewer-1'].noHire, false);
|
||||
assert.deepEqual(preview.plan.packs.proposedHires.map(h => h.name), ['pm-1', 'reviewer-1']);
|
||||
});
|
||||
|
||||
test('SP4 a held credential in a pack, a board or a workflow refuses credential-leak and is never echoed', async t => {
|
||||
const { call, path, revision, save } = await harness(t);
|
||||
save('jason', 'leaky', variant('leaky', p => { p.description = `Team notes ${TOKEN}`; }));
|
||||
const before = readFileSync(path);
|
||||
const leaks = [
|
||||
() => call('pack.preview.console', { pack: 'saved:leaky' }),
|
||||
() => call('pack.apply.console', { pack: 'saved:leaky', digest: 'a'.repeat(64) }),
|
||||
() => call('board.set.console', { revision: revision(), board: { columns: codingPack().board.columns.map((c, i) => i ? c : { ...c, label: TOKEN }), layout: 'kanban' } }),
|
||||
() => call('workflow.set.console', { revision: revision(), name: 'leak', workflow: { description: 'x', steps: [{ title: TOKEN, state: 'todo', roles: ['human'] }] } }),
|
||||
];
|
||||
for (const leak of leaks) {
|
||||
await assert.rejects(leak(), e => { assert.match(e.message, /credential-leak/); assert.ok(!e.message.includes(TOKEN)); assert.ok(!JSON.stringify(e).includes(TOKEN)); return true; });
|
||||
}
|
||||
assert.deepEqual(readFileSync(path), before);
|
||||
});
|
||||
|
||||
test('SP4 a template reference to a definition a hired agent may not hold refuses at preview', async t => {
|
||||
const { call, install, rolesDir } = await harness(t);
|
||||
const { templateHash } = await import('../../organization/src/definitions.mjs');
|
||||
const cos = templateHash(rolesDir, 'chief-of-staff');
|
||||
install(variant('chiefkit', p => { p.definitions.cos = { template: 'chief-of-staff', sha256: cos }; }));
|
||||
await assert.rejects(call('pack.preview.console', { pack: 'chiefkit' }), /invalid-request/);
|
||||
});
|
||||
|
||||
test('SP4 who may call: the owner only; the tester, a member, an agent and a console cap on the cli name refuse', async t => {
|
||||
const { b, call, human, revision, path } = await harness(t);
|
||||
// TESTER_ORGANIZATION_VERBS is unchanged: no pack, board or workflow verb.
|
||||
assert.deepEqual(TESTER_ORGANIZATION_VERBS, ['limits', 'plugins', 'instances', 'plugins.set', 'instances.set', 'routes.set', 'routes']);
|
||||
const tester = b.bindTester({ business: 'demo' });
|
||||
const before = readFileSync(path);
|
||||
assert.throws(() => b.request(tester, { verb: 'board.set', args: { revision: revision(), board: null } }), /tester-refused/);
|
||||
assert.throws(() => b.request(tester, { verb: 'workflow.set', args: { revision: revision(), name: 'x', workflow: null } }), /tester-refused/);
|
||||
assert.throws(() => b.request(tester, { verb: 'pack.preview', args: { pack: 'coding' } }), /tester-refused/);
|
||||
const member = b.bindHireConsole({ business: 'demo', actor: 'alice', subject: { provider: 'test', iss: 'test', sub: 'alice' }, rights: ['org.hire', 'board.read'] });
|
||||
for (const [verb, args] of [['pack.apply.console', { pack: 'coding', digest: 'a'.repeat(64) }], ['board.set.console', { revision: revision(), board: null }], ['workflow.set.console', { revision: revision(), name: 'x', workflow: null }], ['packs.console', {}],
|
||||
['pack.apply', { pack: 'coding', digest: 'a'.repeat(64) }], ['board.set', { revision: revision(), board: null }]])
|
||||
assert.throws(() => b.request(member, { verb, args }), /human-required/);
|
||||
const agent = b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi', address: 'coder-run' });
|
||||
// SP5: an agent reaches the unsuffixed names through its role's organization.change; the coder
|
||||
// lists none, so its write needs a decision (packs-agent.test.mjs covers the chief).
|
||||
b.request(agent, { verb: 'role.claim' });
|
||||
assert.throws(() => b.request(agent, { verb: 'pack.apply', args: { pack: 'coding', digest: 'a'.repeat(64) } }), /decision-required/);
|
||||
assert.throws(() => b.request(agent, { verb: 'pack.preview.console', args: { pack: 'coding' } }), /human-required/);
|
||||
// The owner's console cap calls the .console names; the cli cap the unsuffixed ones.
|
||||
await assert.rejects(call('pack.preview', { pack: 'coding' }), /human-required/);
|
||||
await assert.rejects(call('pack.preview.console', { pack: 'coding' }, human), /human-required/);
|
||||
assert.deepEqual(readFileSync(path), before);
|
||||
});
|
||||
|
||||
test('SP4 the request carries no origin, actor or unknown key; scope and argument checks refuse invalid-request', async t => {
|
||||
const { call, revision, install } = await harness(t);
|
||||
for (const args of [{ pack: 'coding', origin: 'shipped' }, { pack: 'coding', actor: 'mallory' }, {}, { pack: 7 }, { pack: 'coding', project: 'stack' }])
|
||||
await assert.rejects(call('pack.preview.console', args), /invalid-request/);
|
||||
await assert.rejects(call('pack.apply.console', { pack: 'coding' }), /invalid-request/);
|
||||
await assert.rejects(call('pack.preview.console', { pack: 'nosuch' }), /starter-pack-not-found/);
|
||||
await assert.rejects(call('board.set.console', { revision: 'nope', board: null }), /invalid-request/);
|
||||
await assert.rejects(call('board.set.console', { revision: revision(), board: [] }), /invalid-request/);
|
||||
await assert.rejects(call('workflow.set.console', { revision: revision(), name: 'x', workflow: 'y' }), /invalid-request/);
|
||||
// A workflow step naming a role no definition, template or `human` resolves refuses.
|
||||
await assert.rejects(call('workflow.set.console', { revision: revision(), name: 'x', workflow: { description: 'x', steps: [{ title: 'Do', state: 'todo', roles: ['nobody-at-all'] }] } }), /invalid-request/);
|
||||
// A project pack needs a code project; an org pack takes none.
|
||||
install(variant('projkit', p => { p.scope = 'project'; }));
|
||||
await assert.rejects(call('pack.preview.console', { pack: 'projkit' }), /invalid-request/);
|
||||
await assert.rejects(call('pack.preview.console', { pack: 'projkit', project: 'nosuch' }), /invalid-request/);
|
||||
const preview = await call('pack.preview.console', { pack: 'projkit', project: 'stack' });
|
||||
assert.equal(marks(preview)['projects.stack.board'], 'add');
|
||||
assert.equal(marks(preview)['projects.stack.workflows.change'], 'add');
|
||||
assert.equal(marks(preview)['definitions.coder'], 'add');
|
||||
});
|
||||
|
||||
test('SP4 a saved template is read only from its own owner, and packs lists none', async t => {
|
||||
const { b, call, save } = await harness(t);
|
||||
save('jason', 'mine', variant('mine'));
|
||||
const preview = await call('pack.preview.console', { pack: 'saved:mine' });
|
||||
assert.equal(preview.pack.origin, 'saved');
|
||||
const alex = b.bindHuman({ business: 'other', human: 'alex', via: 'cli', outsideAgent: true });
|
||||
await assert.rejects(b.request(alex, { verb: 'pack.preview', args: { pack: 'saved:mine' } }), /starter-pack-not-found/);
|
||||
const listed = await call('packs.console');
|
||||
assert.ok(listed.packs.length >= 5);
|
||||
assert.ok(listed.packs.every(p => p.origin !== 'saved' && p.id !== 'mine'));
|
||||
for (const name of ['saved:../jason/mine', 'saved:', 'saved:a/b']) await assert.rejects(call('pack.preview.console', { pack: name }), /starter-pack-invalid|invalid-request/);
|
||||
});
|
||||
|
||||
test('SP4 board.set changes only the board; workflow.set and remove change only that workflow', async t => {
|
||||
const { call, file, revision } = await harness(t);
|
||||
const before = file();
|
||||
const board = structuredClone(codingPack().board);
|
||||
await call('board.set.console', { revision: revision(), board });
|
||||
const after = file();
|
||||
assert.deepEqual(after.board, board);
|
||||
delete after.board;
|
||||
assert.deepEqual(after, before);
|
||||
const flow = { description: 'One.', steps: [{ title: 'Do it', state: 'todo', roles: ['coder'] }] };
|
||||
await call('workflow.set.console', { revision: revision(), name: 'one', workflow: flow });
|
||||
await call('workflow.set.console', { revision: revision(), name: 'two', workflow: flow });
|
||||
await call('workflow.set.console', { revision: revision(), name: 'one', workflow: null });
|
||||
assert.deepEqual(file().workflows, { two: flow });
|
||||
assert.deepEqual(file().board, board);
|
||||
});
|
||||
|
||||
test('SP4 caps: at most 32 workflows and 256 packs entries', async t => {
|
||||
const { call, file, revision, path } = await harness(t);
|
||||
const flow = { description: 'x', steps: [{ title: 'Do it', state: 'todo', roles: ['human'] }] };
|
||||
const doc = file();
|
||||
doc.workflows = Object.fromEntries(Array.from({ length: 32 }, (_, i) => [`w${i}`, flow]));
|
||||
writeFileSync(path, JSON.stringify(doc, null, 2) + '\n');
|
||||
await assert.rejects(call('workflow.set.console', { revision: revision(), name: 'w32', workflow: flow }), /invalid-request/);
|
||||
await call('workflow.set.console', { revision: revision(), name: 'w0', workflow: null });
|
||||
const preview = await call('pack.preview.console', { pack: 'coding' });
|
||||
await call('pack.apply.console', { pack: 'coding', digest: preview.digest });
|
||||
const full = file();
|
||||
full.packs = Array.from({ length: 256 }, () => structuredClone(full.packs[0]));
|
||||
writeFileSync(path, JSON.stringify(full, null, 2) + '\n');
|
||||
const again = await call('pack.preview.console', { pack: 'coding' });
|
||||
await assert.rejects(call('pack.apply.console', { pack: 'coding', digest: again.digest }), /invalid-request/);
|
||||
});
|
||||
|
||||
test('SP4 the broker knows each verb under both names', () => {
|
||||
assert.deepEqual(Object.keys(PACK_BUS_KEYS).sort(), ['board.set', 'board.set.console', 'pack.apply', 'pack.apply.console', 'pack.preview', 'pack.preview.console', 'packs', 'packs.console', 'workflow.set', 'workflow.set.console']);
|
||||
});
|
||||
@@ -0,0 +1,306 @@
|
||||
// #1873 pickup-launch: a bus that picked up the shared proxy late serves accounts and a ready
|
||||
// catalogue, but a launch on a proxy model still needs a restart (no ModelKeys on the bus, no proxy
|
||||
// in the host launcher's boot system). While it holds such a client, its catalogue answer says so
|
||||
// in one plain notice, and a launch refuses exactly as before. A refused or dropped client shows
|
||||
// its cause instead. Real buses, a real launcher whose model ops go to the bus, a fake loopback
|
||||
// management server; no live config.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { createProxyLock } from '../src/proxy-lock.mjs';
|
||||
import { Registry, launchLogFile } from '../../seat/src/session.mjs';
|
||||
import { loadBusiness } from '../../organization/src/organization-config.mjs';
|
||||
import { resolveInstance } from '../../organization/src/resolve.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
import { PIN } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy, installPaths, loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { createLauncher } from '../../cli/src/launcher.mjs';
|
||||
import { initializeControl } from '../../processing-control/src/state.mjs';
|
||||
import { ZAI_URL } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
import { fakeHarnesses, humanEnv, withoutHostCredentials } from '../../cli/tests/helpers.mjs';
|
||||
import { REPO_SKILLS } from '../../harness/tests/helpers.mjs';
|
||||
|
||||
const RESTART = 'Restart this organization to start agents on these models.';
|
||||
const MODEL = 'claude-claude-opus-5-5';
|
||||
const sha = value => createHash('sha256').update(value).digest('hex');
|
||||
|
||||
// A loopback stand-in for the proxy's management API: one ready Claude account with one model,
|
||||
// and an access key list that keeps what a PUT sends, so a boot ModelKeys reconciles against it.
|
||||
async function fakeProxy(t, key, port = 0, { groups = [] } = {}) {
|
||||
const seen = { calls: 0, wrongKey: 0 };
|
||||
let accessKeys = [];
|
||||
const server = http.createServer((req, res) => {
|
||||
seen.calls++;
|
||||
if (req.headers.authorization !== `Bearer ${key}`) { seen.wrongKey++; res.writeHead(401); res.end('{}'); return; }
|
||||
const url = new URL(req.url, 'http://127.0.0.1');
|
||||
if (url.pathname === '/v8/management/config/access/api-keys' && req.method === 'PUT') {
|
||||
const chunks = [];
|
||||
req.on('data', c => chunks.push(c));
|
||||
req.on('end', () => { accessKeys = JSON.parse(Buffer.concat(chunks)); res.end(JSON.stringify({ status: 'ok' })); });
|
||||
return;
|
||||
}
|
||||
const answer = url.pathname === '/v8/management/credentials' ? { files: [{ id: 'acct-1', name: 'acct-1', provider: 'claude', status: 'ready', priority: 1 }] }
|
||||
: url.pathname === '/v8/management/credentials/models' ? { models: [{ id: 'claude-opus-5-5' }] }
|
||||
: url.pathname === '/v8/management/config/api-keys/claude' ? groups
|
||||
: url.pathname === '/v8/management/config/access/api-keys' ? accessKeys
|
||||
: null;
|
||||
if (answer === null) { res.writeHead(404); res.end('{}'); return; }
|
||||
res.end(JSON.stringify(answer));
|
||||
});
|
||||
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(port, '127.0.0.1', resolve); });
|
||||
t.after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); });
|
||||
return { port: server.address().port, seen, server };
|
||||
}
|
||||
|
||||
// One instance (config dir + data root) and a fake proxy install whose bytes a test pin names.
|
||||
function instance(t) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pl-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'instance');
|
||||
fs.mkdirSync(join(configDir, 'secrets', 'agents'), { recursive: true, mode: 0o700 });
|
||||
fs.chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const paths = installPaths(dataRoot);
|
||||
fs.mkdirSync(join(paths.dir, 'static'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(paths.binary, 'fixture proxy binary\n', { mode: 0o700 });
|
||||
fs.writeFileSync(paths.panel, '<!doctype html>\n', { mode: 0o600 });
|
||||
const pin = { ...PIN, binary: { ...PIN.binary, sha256: sha('fixture proxy binary\n') }, panel: { ...PIN.panel, sha256: sha('<!doctype html>\n') } };
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root);
|
||||
const key = `pl1873-${randomBytes(24).toString('hex')}`;
|
||||
return { root, configDir, dataRoot, paths, pin, rolesDir, system, key };
|
||||
}
|
||||
|
||||
// A real organization bus on the instance, started with no proxy unless `binding.proxy` gives one.
|
||||
async function orgBus(t, f, id, { providers = ['cliproxy'], binding = {} } = {}) {
|
||||
const doc = businessDoc(f.root, id);
|
||||
// The coder's model is a Claude model on a legacy entry, which counts as OAuth and runs on
|
||||
// claude-code only, so the organization enables claude-code.
|
||||
doc.plugins = { harness: ['claude-code'], 'model-provider': providers };
|
||||
writeJson(join(f.configDir, 'businesses', `${id}.json`), doc);
|
||||
const business = loadBusiness(id, { dir: f.configDir, rolesDir: f.rolesDir }), resolved = {};
|
||||
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system: f.system, business, instance: name });
|
||||
const dataRoot = join(f.root, `${id}-bus`); fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
|
||||
const bus = await startBroker({ dataRoot, businesses: { [id]: busOrganization(business, resolved) },
|
||||
organization: { dir: f.configDir, rolesDir: f.rolesDir, system: f.system },
|
||||
instanceRoot: f.dataRoot, org: { id }, instanceDataRoot: f.dataRoot, instanceConfigDir: f.configDir,
|
||||
initProxy: args => initializeProxy({ ...args, pin: f.pin }), ...binding });
|
||||
t.after(() => bus.close().catch(() => {}));
|
||||
const human = bus.broker.bindHuman({ business: id, human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const ask = (verb, args) => Promise.resolve().then(() => bus.broker.request(human, { verb, args }));
|
||||
return { id, bus, ask, dataRoot };
|
||||
}
|
||||
|
||||
// Every stderr line written while `run` is in flight.
|
||||
async function capture(run) {
|
||||
const write = process.stderr.write, lines = [];
|
||||
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
|
||||
try { return { value: await run(), lines }; } finally { process.stderr.write = write; }
|
||||
}
|
||||
|
||||
async function initThroughA(t, f, a, options) {
|
||||
const settings = await a.ask('proxy.init', { key: f.key });
|
||||
const proxy = await fakeProxy(t, f.key, settings.port, options);
|
||||
return { settings, proxy };
|
||||
}
|
||||
|
||||
// The owner picks the catalogue's proxy model for the coder on the Routing page, through the bus.
|
||||
async function pickModel(o) {
|
||||
const view = await o.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'ready');
|
||||
assert.ok(view.catalogue.models.some(m => m.key === MODEL));
|
||||
await o.ask('routes.set', { revision: view.revision, routes: { coder: { harness: 'claude-code', model: MODEL, thinking: 'low', fallback: null } } });
|
||||
}
|
||||
|
||||
// The host launcher as cli.mjs builds it for a bus that booted with no proxy.json: its system has
|
||||
// no proxy. The model ops go to the bus itself; the rest are the launch-routes stub, and
|
||||
// authorizeLaunch stops a launch that passed every check before it.
|
||||
async function hostLauncher(t, f, o, { system = {} } = {}) {
|
||||
initializeControl(o.dataRoot);
|
||||
// A launch whose role names a skill that is not enabled refuses before the model gate.
|
||||
if (!fs.existsSync(join(f.dataRoot, 'skills-enabled'))) fs.cpSync(REPO_SKILLS, join(f.dataRoot, 'skills-enabled'), { recursive: true });
|
||||
// claude-code's preflight needs a session login or a proxy key, and this launcher boots with no
|
||||
// proxy, so the env carries a stand-in login (as launcher.test.mjs does). No adapter ever runs.
|
||||
const env = withoutHostCredentials(humanEnv({ MOSAIC_CONFIG: join(f.configDir, 'config.json'), MOSAIC_ROLES_DIR: f.rolesDir }));
|
||||
env.CLAUDE_CODE_OAUTH_TOKEN = 'stand-in';
|
||||
const answers = [], ops = [];
|
||||
const host = { business: o.id, beforeClose: () => {}, op: async m => {
|
||||
ops.push(m.op);
|
||||
if (m.op === 'identity') return { business: o.id, role: 'pm' };
|
||||
if (m.op === 'mayLaunch') return { allow: true };
|
||||
if (m.op === 'modelAdmission' || m.op === 'modelPreflight') { const answer = await o.bus[m.op](m); answers.push({ op: m.op, answer }); return answer; }
|
||||
if (m.op === 'authorizeLaunch') throw Object.assign(Error('fixture authorization stop'), { code: 'fixture-policy' });
|
||||
return {};
|
||||
} };
|
||||
const cliproxy = JSON.stringify(pathToFileURL(new URL('../../plugins/model-provider/cliproxy/index.mjs', import.meta.url).pathname).href);
|
||||
// The stand-in adapter path skips only the binary check; the launch stops at the model gate or
|
||||
// authorizeLaunch, before any adapter runs.
|
||||
const makeLoader = await fakeHarnesses(t, { 'claude-code': '/fixture-unused' }, write => write('model-provider', 'cliproxy', `export * from ${cliproxy};\n`));
|
||||
// The validated system config shape (mosaic-config.mjs), as cli.mjs passes it with the org root.
|
||||
const config = { configVersion: 1, environment: 'development', dataRoot: o.dataRoot, instanceRoot: f.dataRoot,
|
||||
execution: { backend: 'docker', provider: 'zai', model: 'glm-5.3-flash', adapter: 'mock' }, ...system };
|
||||
const launcher = createLauncher({ host, system: config, env, makeLoader, bwrap: '/usr/bin/true', log: () => {} });
|
||||
t.after(() => launcher.close?.().catch?.(() => {}));
|
||||
const events = () => { try { return fs.readFileSync(launchLogFile(o.dataRoot, o.id), 'utf8').trim().split('\n').filter(Boolean).map(l => JSON.parse(l)); } catch { return []; } };
|
||||
return { launch: () => launcher.fromSession('fixture-cap', 'coder'), answers, ops, events };
|
||||
}
|
||||
|
||||
const restartNotices = view => view.catalogue.notices.filter(n => n === RESTART).length;
|
||||
|
||||
test('a bus that picked up the proxy says on its ready catalogue that a restart starts agents on these models, and the launch refuses as before', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
const before = await b.ask('routes');
|
||||
assert.equal(before.catalogue.status, 'unavailable');
|
||||
assert.equal(restartNotices(before), 0, 'no client yet: the not-set-up cause, not the restart notice');
|
||||
await initThroughA(t, f, a);
|
||||
await pickModel(b);
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'ready');
|
||||
assert.equal(restartNotices(view), 1);
|
||||
assert.equal(restartNotices(await b.ask('routes')), 1, 'one notice at every read');
|
||||
// The bus that ran init holds a client it did not boot with too.
|
||||
const own = await a.ask('routes');
|
||||
assert.equal(own.catalogue.status, 'ready');
|
||||
assert.equal(restartNotices(own), 1);
|
||||
// The launch refuses as before, with the existing code and line, before any key or run: the
|
||||
// launcher booted with no proxy sends no proxy transport, and the bus's admission refuses that.
|
||||
const host = await hostLauncher(t, f, b);
|
||||
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
|
||||
assert.deepEqual(host.answers, [{ op: 'modelAdmission', answer: { allow: false, code: 'model-provider-unavailable' } }]);
|
||||
assert.equal(host.ops.includes('authorizeLaunch'), false);
|
||||
assert.equal(host.ops.includes('modelPreflight'), false);
|
||||
const refused = host.events().filter(e => e.event === 'refused');
|
||||
assert.equal(refused.length, 1);
|
||||
assert.equal(refused[0].code, 'model-provider-unavailable');
|
||||
assert.equal(refused[0].why, 'model cliproxy/claude/claude-opus-5-5 unavailable (model-provider-unavailable)');
|
||||
// The bus side has no ModelKeys: a preflight issues no key.
|
||||
assert.equal(await b.bus.modelPreflight({ business: 'beta', role: 'coder' }), null);
|
||||
});
|
||||
|
||||
test('the restart notice is added after the notices the catalogue already carries', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
// A z.ai upstream group with no models: the cliproxy catalogue's own notice.
|
||||
await initThroughA(t, f, a, { groups: [{ 'base-url': ZAI_URL, keys: [{ name: 'group-1' }] }] });
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'ready');
|
||||
assert.deepEqual(view.catalogue.notices, ['z.ai account has no models configured. Add them on Model proxy.', RESTART]);
|
||||
});
|
||||
|
||||
test('a bus started with the proxy shows no restart notice', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
await initThroughA(t, f, a);
|
||||
const c = await orgBus(t, f, 'gamma', { binding: { proxy: loadProxy(f.configDir, f.dataRoot) } });
|
||||
const view = await c.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'ready', JSON.stringify(view.catalogue));
|
||||
assert.deepEqual(view.catalogue.notices, []);
|
||||
});
|
||||
|
||||
test('an organization with no model provider shows a disabled catalogue with no restart notice (the routes verb answers disabled itself)', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta', { providers: [] });
|
||||
await initThroughA(t, f, a);
|
||||
await b.ask('proxy.accounts', {});
|
||||
assert.ok(b.bus.modelProxy, 'the client is held');
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'disabled');
|
||||
assert.deepEqual(view.catalogue.notices, []);
|
||||
});
|
||||
|
||||
test('a fenced catalogue on a picked-up client reads unavailable with no restart notice, since a restart would not help', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
const { proxy } = await initThroughA(t, f, a);
|
||||
assert.equal(restartNotices(await b.ask('routes')), 1);
|
||||
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }).fence({ org: 'alpha' });
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.ok(b.bus.modelProxy);
|
||||
assert.equal(restartNotices(view), 0);
|
||||
// A bus that booted with the proxy on the same fenced instance, which is what a restart gives,
|
||||
// reads the same unavailable catalogue: the restart notice would have sent the owner nowhere.
|
||||
const c = await orgBus(t, f, 'gamma', { binding: { proxy: loadProxy(f.configDir, f.dataRoot) } });
|
||||
assert.equal((await c.ask('routes')).catalogue.status, 'unavailable');
|
||||
// Once the fence is gone, the ready catalogue says again that a restart is needed.
|
||||
fs.rmSync(join(f.dataRoot, 'proxy', 'mosaic-keys.uncertain'));
|
||||
const after = await b.ask('routes');
|
||||
assert.equal(after.catalogue.status, 'ready', JSON.stringify(after.catalogue));
|
||||
assert.equal(restartNotices(after), 1);
|
||||
});
|
||||
|
||||
test('refused settings still refuse the launch and name their cause, not the restart notice', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
await pickModel(b);
|
||||
// The management key file stops being private: the start path refuses it.
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
assert.equal(view.catalogue.notices.length, 1);
|
||||
assert.match(view.catalogue.notices[0], /management key does not load/);
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
const host = await hostLauncher(t, f, b);
|
||||
// The bus itself refuses now: the catalogue keeps the model it saw, marked unavailable.
|
||||
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
|
||||
assert.deepEqual(host.answers, [{ op: 'modelAdmission', answer: { allow: false, code: 'model-provider-unavailable' } }]);
|
||||
assert.match(host.events().at(-1).why, /^model cliproxy\/claude\/claude-opus-5-5 unavailable \(model-provider-unavailable\)$/);
|
||||
});
|
||||
|
||||
test('a dropped client refuses the next launch, and the catalogue names the cause instead of the restart notice', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
await pickModel(b);
|
||||
const host = await hostLauncher(t, f, b);
|
||||
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
|
||||
assert.equal(restartNotices(await b.ask('routes')), 1);
|
||||
fs.rmSync(join(f.configDir, 'proxy.json'));
|
||||
// The next launch refuses with the same code and line.
|
||||
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
|
||||
assert.deepEqual(host.answers.map(a => a.answer), [{ allow: false, code: 'model-provider-unavailable' }, { allow: false, code: 'model-provider-unavailable' }]);
|
||||
assert.equal(host.events().filter(e => e.event === 'refused').length, 2);
|
||||
// The page's next read drops the client and names the cause.
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
assert.deepEqual(view.catalogue.notices.length, 1);
|
||||
assert.match(view.catalogue.notices[0], /not been set up/);
|
||||
});
|
||||
|
||||
test('the management key is in no stderr line, response, refusal, launch log or file the launch path writes', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
await pickModel(b);
|
||||
const host = await hostLauncher(t, f, b);
|
||||
const outputs = [];
|
||||
const { lines } = await capture(async () => {
|
||||
outputs.push(await b.ask('routes'));
|
||||
outputs.push(await host.launch());
|
||||
outputs.push(await b.bus.modelPreflight({ business: 'beta', role: 'coder' }));
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
|
||||
outputs.push(await b.ask('routes'));
|
||||
outputs.push(await host.launch());
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o600);
|
||||
outputs.push(await b.ask('routes'));
|
||||
outputs.push(await host.launch());
|
||||
});
|
||||
outputs.push(host.answers, host.events());
|
||||
assert.equal(lines.join('').includes(f.key), false, 'stderr');
|
||||
assert.equal(JSON.stringify(outputs).includes(f.key), false, 'responses, answers and launch log');
|
||||
const walk = dir => fs.readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? walk(join(dir, e.name)) : e.isFile() ? [join(dir, e.name)] : []);
|
||||
const files = [...walk(b.dataRoot), ...walk(join(f.dataRoot, 'proxy')).filter(p => !p.endsWith('/config.yaml'))];
|
||||
assert.ok(files.some(p => p === launchLogFile(b.dataRoot, 'beta')), 'the launch log is scanned');
|
||||
for (const file of files) assert.equal(fs.readFileSync(file).includes(f.key), false, file);
|
||||
});
|
||||
@@ -102,3 +102,67 @@ test('plugins read advertises loadable harnesses even when switched off', async
|
||||
const view=host.broker.request(human,{verb:'plugins'});
|
||||
assert.deepEqual(view.plugins.harness,['claude-code']);assert.ok(view.available.harness.includes('pi'));assert.ok(view.available.harness.includes('claude-code'));
|
||||
});
|
||||
|
||||
// #1873 harness watch W3: the plugins answer carries what the host's watch last wrote under the
|
||||
// organization's data root (state/harness-watch.json), read only, redacted again, and "not checked
|
||||
// yet" for every harness when the file is missing, cut short or not version 1's shape.
|
||||
test('W3 runtime: the plugins answer carries the harness watch record per harness, and a missing, truncated or wrong-shape file gives not checked yet', async (t) => {
|
||||
const { startBroker } = await import('../src/runtime.mjs');
|
||||
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs');
|
||||
const { writeFileSync } = await import('node:fs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'plugins-watch-'));
|
||||
const rolesDir = rolesCopy(root);
|
||||
writeJson(join(root, 'businesses/demo.json'), businessDoc(root, 'demo'));
|
||||
const dataRoot = join(root, 'data');
|
||||
mkdirSync(join(dataRoot, 'state'), { recursive: true, mode: 0o700 });
|
||||
const planted = 'w3-planted-env-value';
|
||||
process.env.W3_PLANTED = planted;
|
||||
const bus = await startBroker({ dataRoot, businesses: { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: { authority: { withinRole: [], crossRole: [] } } } } }, organization: { dir: root, rolesDir, system: systemFor(root) } });
|
||||
t.after(async () => { delete process.env.W3_PLANTED; await bus.close(); rmSync(root, { recursive: true, force: true }); });
|
||||
const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const file = join(dataRoot, 'state', 'harness-watch.json');
|
||||
const read = () => bus.broker.request(human, { verb: 'plugins' });
|
||||
const names = read().available.harness;
|
||||
assert.ok(names.length > 0, 'the tree gives harnesses');
|
||||
const unchecked = (name) => ({ name, state: 'unchecked', version: null, checkedAt: null, changedAt: null, failed: [], line: 'not checked yet' });
|
||||
|
||||
// No file: every harness the loader gives reads not checked yet, in the loader's order.
|
||||
assert.deepEqual(read().harnessWatch, names.map(unchecked));
|
||||
|
||||
const at = '2026-11-20T10:00:00.000Z', moved = '2026-11-19T08:00:00.000Z';
|
||||
const rec = (harness, over) => ({ harness, state: 'fit', via: 'inspect', found: true, source: 'PATH', version: '1.0.0', digest: 'a'.repeat(64), lastDigest: 'a'.repeat(64), fit: 'pass', definite: true, failed: [], line: 'ok', checkedAt: at, changedAt: moved, ...over });
|
||||
const doc = { version: 1, checkedAt: at, refused: [], gone: [], harnesses: [
|
||||
rec(names[0], { version: '2.1.323', line: 'every flag the adapter passes is in --help' }),
|
||||
rec('w3-red', { state: 'red', fit: 'fail', failed: ['--restricted'], line: '--help lacks --restricted' }),
|
||||
rec('w3-missing', { state: 'missing', found: false, version: null, digest: null, fit: 'unknown', definite: false, line: 'no binary from MOSAIC_PI_BIN, PATH or the repo copy' }),
|
||||
rec('w3-unknown', { state: 'unknown', fit: 'unknown', definite: false, line: 'inspect did not answer in time' }),
|
||||
// A path and an env value planted in the file: the broker redacts them again before answering.
|
||||
rec('w3-leak', { state: 'red', fit: 'fail', version: `9.9 ${planted}`, failed: [planted, '--ok'], line: `failed under ${dataRoot}/x/y and ${join(root, 'businesses')}/demo.json with ${planted}` }),
|
||||
] };
|
||||
writeFileSync(file, JSON.stringify(doc), { mode: 0o600 });
|
||||
const view = read().harnessWatch;
|
||||
const by = Object.fromEntries(view.map((e) => [e.name, e]));
|
||||
assert.deepEqual(view.map((e) => e.name), [...names, 'w3-red', 'w3-missing', 'w3-unknown', 'w3-leak'], 'the loader\'s harnesses first, then the file\'s others');
|
||||
assert.deepEqual(by[names[0]], { name: names[0], state: 'fit', version: '2.1.323', checkedAt: at, changedAt: moved, failed: [], line: 'every flag the adapter passes is in --help' });
|
||||
assert.deepEqual(by['w3-red'], { name: 'w3-red', state: 'red', version: '1.0.0', checkedAt: at, changedAt: moved, failed: ['--restricted'], line: '--help lacks --restricted' });
|
||||
assert.deepEqual(by['w3-missing'], { name: 'w3-missing', state: 'missing', version: null, checkedAt: at, changedAt: moved, failed: [], line: 'no binary from MOSAIC_PI_BIN, PATH or the repo copy' });
|
||||
assert.deepEqual(by['w3-unknown'], { name: 'w3-unknown', state: 'unknown', version: '1.0.0', checkedAt: at, changedAt: moved, failed: [], line: 'inspect did not answer in time' });
|
||||
for (const name of names.slice(1)) assert.deepEqual(by[name], unchecked(name), 'a harness with no record is not checked yet');
|
||||
const leak = JSON.stringify(by['w3-leak']);
|
||||
for (const needle of [planted, dataRoot, root]) assert.ok(!leak.includes(needle), `the answer holds no ${needle === planted ? 'env value' : 'host path'}: ${leak}`);
|
||||
assert.deepEqual(by['w3-leak'].failed, ['--ok'], 'a rule id that is an env value is dropped');
|
||||
assert.match(by['w3-leak'].line, /^failed under <path>.* with <W3_PLANTED>$/);
|
||||
assert.deepEqual(JSON.parse(readFileSync(file, 'utf8')), doc, 'the broker does not write the file');
|
||||
|
||||
// A truncated file, a wrong shape and a wrong version each read as not checked yet, never a throw.
|
||||
for (const bad of [JSON.stringify(doc).slice(0, 80), JSON.stringify({ ...doc, harnesses: { [names[0]]: doc.harnesses[0] } }), JSON.stringify({ ...doc, version: 2 }), JSON.stringify([doc]), 'null', '']) {
|
||||
writeFileSync(file, bad, { mode: 0o600 });
|
||||
assert.deepEqual(read().harnessWatch, names.map(unchecked), bad.slice(0, 40));
|
||||
}
|
||||
// A record of the wrong shape reads as not checked yet for that harness only.
|
||||
writeFileSync(file, JSON.stringify({ ...doc, harnesses: [rec(names[0], { state: 'green' }), rec('w3-red', { state: 'red', fit: 'fail' }), 'x', null, rec('../x', {})] }), { mode: 0o600 });
|
||||
const odd = Object.fromEntries(read().harnessWatch.map((e) => [e.name, e]));
|
||||
assert.deepEqual(odd[names[0]], unchecked(names[0]));
|
||||
assert.equal(odd['w3-red'].state, 'red');
|
||||
assert.equal(odd['../x'], undefined, 'a name that is not a harness name is dropped');
|
||||
});
|
||||
@@ -198,8 +198,7 @@ test('3p through 3s migration preserves existing request rows and the append-onl
|
||||
const path = join(root, 'bus/bus.sqlite'), ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8')
|
||||
// 3s-3t (#797) adds project_archives; the 3p fixture predates it and the replayed chain recreates it.
|
||||
.replace(readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8'), '')
|
||||
.replace(readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8'), '')
|
||||
// 3u-3v (#1873, U4a) adds store_tasks after project_system_types; the same holds for it.
|
||||
// 3u-3v (#1873, U4a) adds store_tasks; the same holds for it.
|
||||
.replace(readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8'), '')
|
||||
.replace(",'prd.submitted','prd.activated','prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','advanced'", '');
|
||||
const db = new DatabaseSync(path); db.exec(ddl);
|
||||
@@ -216,7 +215,7 @@ test('3p through 3s migration preserves existing request rows and the append-onl
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const store = new Store(root, { migrateSchema: true }); t.after(() => store.close());
|
||||
assert.deepEqual(store.all('SELECT * FROM request_events'), before);
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
store.run("INSERT INTO request_events(business,request,revision,kind,at,body) VALUES('demo','r',2,'prd.submitted',?,'{}')", at);
|
||||
assert.throws(() => store.run('DELETE FROM request_events'), /append-only/);
|
||||
assert.throws(() => store.run("UPDATE request_events SET body='{}'"), /append-only/);
|
||||
|
||||
@@ -143,8 +143,8 @@ const here = (p) => new URL(p, import.meta.url);
|
||||
const migrationSQL = readFileSync(here('../src/migrations/3s-3t.sql'), 'utf8');
|
||||
const currentDDL = readFileSync(here('../schema.sql'), 'utf8');
|
||||
assert.equal(currentDDL.split(migrationSQL).length, 2, 'schema.sql holds the 3s→3t step exactly once');
|
||||
// 3u-3v (#1873, U4a) adds store_tasks after them; a 3s store has neither.
|
||||
const ddl3s = currentDDL.replace(migrationSQL, '').replace(readFileSync(here('../src/migrations/3t-3u.sql'), 'utf8'), '').replace(readFileSync(here('../src/migrations/3u-3v.sql'), 'utf8'), '');
|
||||
// 3u-3v (#1873, U4a) adds store_tasks after it; a 3s store has neither table.
|
||||
const ddl3s = currentDDL.replace(migrationSQL, '').replace(readFileSync(here('../src/migrations/3u-3v.sql'), 'utf8'), '');
|
||||
|
||||
function source3s(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'project-archive-3s-'));
|
||||
@@ -179,7 +179,7 @@ test('a 3s store migrates to 3t: project_archives is new and empty, every row st
|
||||
assert.deepEqual(master(path), was, 'the refused open changed nothing');
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
t.after(() => store.close());
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.equal(store.get("SELECT id FROM events").id, 'preserved');
|
||||
assert.equal(store.get("SELECT name FROM projects").name, 'kept');
|
||||
assert.deepEqual(store.all('SELECT seq,at,business,project,op,by FROM project_archives'), []);
|
||||
@@ -212,7 +212,7 @@ test('the 3s engine refuses a 3t store, migrating or not, and leaves it as it wa
|
||||
const source = readFileSync(here('../src/store.mjs'), 'utf8');
|
||||
const edited = source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3s).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3s';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3s';")
|
||||
.replace(/\}, \{\n {2}\/\/ #797 MVP3[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
|
||||
assert.ok(edited.includes("export const SCHEMA_VERSION = '3s';") && edited.includes('3r-3s') && !edited.includes('3s-3t'), 'the 3s engine has no 3s→3t step');
|
||||
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
|
||||
@@ -222,7 +222,7 @@ test('the 3s engine refuses a 3t store, migrating or not, and leaves it as it wa
|
||||
const Store3s = (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
|
||||
assert.throws(() => new Store3s(root), /schema-mismatch/);
|
||||
assert.throws(() => new Store3s(root, { migrateSchema: true }), /schema-mismatch/);
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(master(path), was);
|
||||
// The same engine opens a fresh 3s store, so the refusal is the version's and not a broken copy.
|
||||
const { root: old } = source3s(t);
|
||||
|
||||
@@ -43,7 +43,8 @@ test('Broker keeps the sandbox client import closure: only the listed imports, n
|
||||
// Row 236 (#1742, #436b) reads giteaName and parseGiteaUrl for the Gitea grant path.
|
||||
// FED R3 r5 (row 274, #414) reads covers, a user's grant check, from it.
|
||||
// Row 219 reads THREAD_DEFINITIONS from the same vocabulary for creative thread admission.
|
||||
"import { pluginName, ID_PATTERN, THREAD_DEFINITIONS, END_PROTECTED, forkProblem, forkUrl, credentialKind, validArgv, commandAllowed, HOST_ID, CREATIVE_DEFINITIONS, giteaName, parseGiteaUrl, covers, grantScopeOf, grantInScope } from '../../organization/src/vocabulary.mjs';",
|
||||
// #1873 triage-fence reads SYSTEM_ONLY_REFUSAL and systemOnly from the same vocabulary.
|
||||
"import { pluginName, ID_PATTERN, THREAD_DEFINITIONS, END_PROTECTED, forkProblem, forkUrl, credentialKind, validArgv, commandAllowed, HOST_ID, CREATIVE_DEFINITIONS, giteaName, parseGiteaUrl, covers, grantScopeOf, grantInScope, SYSTEM_ONLY_REFUSAL, systemOnly } from '../../organization/src/vocabulary.mjs';",
|
||||
]);
|
||||
assert.doesNotMatch(source, /\bimport\s*\(/);
|
||||
assert.doesNotMatch(readFileSync(new URL('../src/proxy-page-vocabulary.mjs', import.meta.url), 'utf8'), /\bimport\b/);
|
||||
|
||||
@@ -4,6 +4,7 @@ import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { fakeManagement } from './proxy-page-fixture.mjs';
|
||||
import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
const id = value => createHash('sha256').update(value).digest('hex').slice(0, 12);
|
||||
const oauth = extra => ({ id: 'oauth-one.json', name: 'oauth-one.json', provider: 'claude', label: 'p4-private-label',
|
||||
email: 'p4-private-email', token: 'p4-private-token', status: 'ready', priority: 3, ...extra });
|
||||
@@ -11,7 +12,9 @@ test('accounts have exactly the allowlisted public fields, id from credential id
|
||||
const s = await fakeManagement(t, { files: [oauth()] });
|
||||
const result = await s.call('proxy.accounts');
|
||||
assert.deepEqual(result, { accounts: [{ accountId: id('oauth-one.json'), provider: 'claude',
|
||||
displayName: `Claude account ${id('oauth-one.json')}`, status: 'ready', priority: 3 }] });
|
||||
displayName: `Claude account ${id('oauth-one.json')}`, status: 'ready', priority: 3 }],
|
||||
// settings-backend-a3: the provider list rides along, id, label and sign-in kind only.
|
||||
providers: PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn })) });
|
||||
for (const value of ['p4-private-label', 'p4-private-email', 'p4-private-token']) {
|
||||
assert.equal(JSON.stringify(result).includes(value), false);
|
||||
assert.notEqual(result.accounts[0].accountId, id(value));
|
||||
@@ -140,3 +143,96 @@ test('OAuth callback changes the shared generation for other organization catalo
|
||||
await s.call('proxy.login.submit', { redirect_url: `http://localhost/?state=${login.state}&code=fixture` });
|
||||
assert.equal(lock.generation(), 1);
|
||||
});
|
||||
|
||||
// #1873 account-remove (Jason 2026-11-20): proxy.account.remove { accountId, confirm: true }.
|
||||
const ZAI = 'https://api.z.ai/api/anthropic';
|
||||
const zaiGroup = (keys, extra = {}) => ({ name: 'fixture-zai', 'base-url': ZAI, models: [{ name: 'glm-fixture' }], ...extra,
|
||||
keys: keys.map(k => ({ 'api-key': `${k}-secret`, auth_index: k })) });
|
||||
test('remove: an OAuth account is deleted by its credential name, read back gone, and the catalogue told', async t => {
|
||||
const changes = [];
|
||||
const s = await fakeManagement(t, { files: [oauth(), oauth({ id: 'oauth-two.json', name: 'oauth-two.json' })], onChange: c => changes.push(c) });
|
||||
const result = await s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true });
|
||||
assert.deepEqual(result, { accountId: id('oauth-one.json'), provider: 'claude', removed: true });
|
||||
assert.deepEqual(s.state.deleted, ['oauth-one.json']);
|
||||
assert.deepEqual((await s.call('proxy.accounts')).accounts.map(a => a.accountId), [id('oauth-two.json')]);
|
||||
assert.ok(changes.includes('invalidate'));
|
||||
for (const value of ['p4-private-label', 'p4-private-email', 'p4-private-token', 'oauth-one.json']) {
|
||||
assert.equal(JSON.stringify(result).includes(value), false);
|
||||
assert.equal(JSON.stringify(s.state.receipts).includes(value), false);
|
||||
}
|
||||
});
|
||||
test('remove: a z.ai group with one key goes; with two keys only that key goes and the rest of the group stays', async t => {
|
||||
const s = await fakeManagement(t, { groups: [zaiGroup(['zai-one']), zaiGroup(['zai-two', 'zai-three'], { name: 'fixture-zai-2' })] });
|
||||
assert.deepEqual(await s.call('proxy.account.remove', { accountId: id('zai-one'), confirm: true }), { accountId: id('zai-one'), provider: 'z.ai', removed: true });
|
||||
assert.deepEqual(s.state.groups.map(g => g.name), ['fixture-zai-2']);
|
||||
await s.call('proxy.account.remove', { accountId: id('zai-two'), confirm: true });
|
||||
assert.deepEqual(s.state.groups.map(g => [g.name, g.keys.map(k => k['api-key']), g.models]), [['fixture-zai-2', ['zai-three-secret'], [{ name: 'glm-fixture' }]]]);
|
||||
assert.equal(s.state.calls.some(c => c.startsWith('DELETE ')), false);
|
||||
for (const value of ['zai-one-secret', 'zai-two-secret']) assert.equal(JSON.stringify(s.state.receipts).includes(value), false);
|
||||
});
|
||||
test('remove-unconfirmed: the proxy answers ok but the account is still listed', async t => {
|
||||
const s = await fakeManagement(t, { files: [oauth()], deleteNoop: true });
|
||||
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'remove-unconfirmed' });
|
||||
const g = await fakeManagement(t, { groups: [zaiGroup(['zai-one'])], groupsNoop: true });
|
||||
await assert.rejects(g.call('proxy.account.remove', { accountId: id('zai-one'), confirm: true }), { code: 'remove-unconfirmed' });
|
||||
assert.equal(g.state.receives, 1);
|
||||
});
|
||||
test('remove-write-refused: a 500 from the proxy, a partial delete, or a refused group write', async t => {
|
||||
const a = await fakeManagement(t, { files: [oauth()], deleteStatus: 500 });
|
||||
await assert.rejects(a.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'remove-write-refused' });
|
||||
const b = await fakeManagement(t, { files: [oauth()], deleteBody: { status: 'partial', deleted: 0, failed: [{ name: 'oauth-one.json' }] }, deleteNoop: true });
|
||||
await assert.rejects(b.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'remove-write-refused' });
|
||||
const c = await fakeManagement(t, { groups: [zaiGroup(['zai-one'])], groupsPutStatus: 500 });
|
||||
await assert.rejects(c.call('proxy.account.remove', { accountId: id('zai-one'), confirm: true }), { code: 'remove-write-refused' });
|
||||
assert.equal(c.state.groups.length, 1);
|
||||
});
|
||||
test('remove: a missing or false confirm, an unknown or malformed id, or an extra field refuses before any write', async t => {
|
||||
const s = await fakeManagement(t, { files: [oauth()] });
|
||||
for (const args of [{ accountId: id('oauth-one.json') }, { accountId: id('oauth-one.json'), confirm: false }, { accountId: id('oauth-one.json'), confirm: 'true' },
|
||||
{ accountId: 'a'.repeat(12), confirm: true }, { accountId: 'oauth-one.json', confirm: true }, { accountId: id('oauth-one.json').toUpperCase(), confirm: true },
|
||||
{ accountId: id('oauth-one.json'), confirm: true, name: 'oauth-one.json' }])
|
||||
await assert.rejects(s.call('proxy.account.remove', args), { code: 'invalid-request' });
|
||||
assert.equal(s.state.calls.some(c => !c.startsWith('GET ')), false);
|
||||
});
|
||||
test('remove: a fenced shared proxy refuses with proxy-uncertain and writes nothing', async t => {
|
||||
const fs = await import('node:fs'); const { join } = await import('node:path'); const { tmpdir } = await import('node:os');
|
||||
const { createProxyLock } = await import('../src/proxy-lock.mjs');
|
||||
const dir = fs.mkdtempSync(join(tmpdir(), 'p4-remove-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const lock = createProxyLock({ stateDir: dir, identity: 'fixture' });
|
||||
const s = await fakeManagement(t, { lock, org: 'alpha', files: [oauth()], groups: [zaiGroup(['zai-one'])] });
|
||||
await lock.run(async () => lock.fence({ org: 'beta', op: 'keys.put' }));
|
||||
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'proxy-uncertain' });
|
||||
await assert.rejects(s.call('proxy.account.remove', { accountId: id('zai-one'), confirm: true }), { code: 'proxy-uncertain' });
|
||||
assert.equal(s.state.calls.some(c => !c.startsWith('GET ')), false);
|
||||
assert.equal(s.state.files.length, 1);
|
||||
});
|
||||
test('remove: under the lock, a delete that fails after the fence leaves the proxy uncertain', async t => {
|
||||
const fs = await import('node:fs'); const { join } = await import('node:path'); const { tmpdir } = await import('node:os');
|
||||
const { createProxyLock } = await import('../src/proxy-lock.mjs');
|
||||
const dir = fs.mkdtempSync(join(tmpdir(), 'p4-remove-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const lock = createProxyLock({ stateDir: dir, identity: 'fixture' });
|
||||
const s = await fakeManagement(t, { lock, org: 'alpha', files: [oauth()], deleteStatus: 500 });
|
||||
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'proxy-uncertain' });
|
||||
});
|
||||
test('remove: two listed accounts on one credential name, or one account id listed twice, refuse before any write', async t => {
|
||||
// The DELETE names the file, so it would take the second account too while reporting one removed.
|
||||
const s = await fakeManagement(t, { files: [oauth(), oauth({ id: 'oauth-other-id', name: 'oauth-one.json' })] });
|
||||
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'invalid-request' });
|
||||
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-other-id'), confirm: true }), { code: 'invalid-request' });
|
||||
const d = await fakeManagement(t, { files: [oauth(), oauth({ name: 'oauth-one-copy.json' })] });
|
||||
await assert.rejects(d.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'invalid-request' });
|
||||
for (const x of [s, d]) { assert.equal(x.state.calls.some(c => !c.startsWith('GET ')), false); assert.equal(x.state.files.length, 2); }
|
||||
});
|
||||
test('remove: under the lock, key groups that change between the read and the write refuse with proxy-down before the fence', async t => {
|
||||
const fs = await import('node:fs'); const { join } = await import('node:path'); const { tmpdir } = await import('node:os');
|
||||
const { createProxyLock } = await import('../src/proxy-lock.mjs');
|
||||
const dir = fs.mkdtempSync(join(tmpdir(), 'p4-remove-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const lock = createProxyLock({ stateDir: dir, identity: 'fixture' });
|
||||
const s = await fakeManagement(t, { lock, org: 'alpha', files: [oauth()], groups: [zaiGroup(['zai-one'])] });
|
||||
// Another writer adds a key group after the page's first read.
|
||||
const read = s.client.getUpstreamGroups.bind(s.client); let reads = 0;
|
||||
s.client.getUpstreamGroups = async () => { const groups = await read(); return ++reads === 2 ? [...groups, zaiGroup(['zai-late'], { name: 'fixture-late' })] : groups; };
|
||||
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'proxy-down' });
|
||||
assert.equal(s.state.calls.some(c => !c.startsWith('GET ')), false);
|
||||
assert.equal(lock.generation(), 0, 'no fence was set');
|
||||
});
|
||||
@@ -14,6 +14,14 @@ export async function fakeManagement(t, options = {}) {
|
||||
state.calls.push(req.method + ' ' + u.pathname);
|
||||
const reply = (status, body) => { res.writeHead(status, { 'content-type': 'application/json' }); res.end(JSON.stringify(body)); };
|
||||
if (req.headers.authorization !== 'Bearer p4-management') return reply(401, { error: 'refused' });
|
||||
// #1873 account-remove: DELETE takes {names:[...]} (DeleteAuthFileV8). deleteNoop answers ok and
|
||||
// keeps the file; deleteStatus and deleteBody shape a refusal.
|
||||
if (u.pathname === '/v8/management/credentials' && req.method === 'DELETE') {
|
||||
state.deleted = [...(state.deleted ?? []), ...(value?.names ?? [])];
|
||||
if ((state.deleteStatus ?? 200) !== 200) return reply(state.deleteStatus, { error: 'p4-private-detail' });
|
||||
if (!state.deleteNoop) state.files = state.files.filter(f => !(value?.names ?? []).includes(f.name));
|
||||
return reply(200, state.deleteBody ?? { status: 'ok', deleted: (value?.names ?? []).length, failed: [] });
|
||||
}
|
||||
if (u.pathname === '/v8/management/credentials') return reply(200, { files: state.files, observed_at: '2026-10-24T00:00:00Z' });
|
||||
if (u.pathname === '/v8/management/config/api-keys/claude') {
|
||||
if (req.method === 'GET') {
|
||||
@@ -24,7 +32,8 @@ export async function fakeManagement(t, options = {}) {
|
||||
// Stand-in treats auth_index as derived/read-only metadata.
|
||||
if (value.some(g => (g.keys ?? []).some(k => Object.hasOwn(k, 'auth_index')))) return reply(422, { error: 'derived field' });
|
||||
state.receives++;
|
||||
if (!state.priorityNoop) state.groups = value.map(g => ({ ...g, keys: g.keys.map(k => ({ ...k, auth_index: state.groups.flatMap(old => old.keys ?? []).find(old => old['api-key'] === k['api-key'])?.auth_index ?? `upstream-${nextId++}` })) }));
|
||||
if (state.groupsPutStatus) return reply(state.groupsPutStatus, { error: 'p4-private-detail' });
|
||||
if (!state.priorityNoop && !state.groupsNoop) state.groups = value.map(g => ({ ...g, keys: g.keys.map(k => ({ ...k, auth_index: state.groups.flatMap(old => old.keys ?? []).find(old => old['api-key'] === k['api-key'])?.auth_index ?? `upstream-${nextId++}` })) }));
|
||||
return reply(200, { status: 'ok' });
|
||||
}
|
||||
if (u.pathname === '/v8/management/credentials/fields') {
|
||||
@@ -43,7 +52,7 @@ export async function fakeManagement(t, options = {}) {
|
||||
await new Promise(r => server.listen(0, '127.0.0.1', r));
|
||||
t.after(() => new Promise(r => server.close(r)));
|
||||
const client = new ManagementClient({ baseUrl: `http://127.0.0.1:${server.address().port}`, key: 'p4-management', receipt: entry => state.receipts.push(entry) });
|
||||
const page = new ProxyPage({ client, instanceTag: 'fixture', lock: options.lock ?? null, org: options.org ?? null, keyRecords: () => options.records ?? [] });
|
||||
const page = new ProxyPage({ client, instanceTag: 'fixture', lock: options.lock ?? null, org: options.org ?? null, keyRecords: () => options.records ?? [], onChange: options.onChange ?? (() => {}) });
|
||||
const call = (verb, args = {}) => page.request(verb, args, { business: 'demo', human: 'owner' });
|
||||
return { state, page, call, client, server };
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
// #1873 settings-backend-a3: the proxy page's provider names, login allow list, sign-in host and key
|
||||
// allow list all come from the plugin's PROVIDERS. A provider a test copy of the list leaves out is
|
||||
// refused at each door with the same code as before; one a copy adds is let through. The accounts
|
||||
// answer carries the list as id, label and sign-in kind only.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { ProxyPage } from '../src/proxy-page.mjs';
|
||||
import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
const owner = { business: 'demo', human: 'owner' };
|
||||
function setup(options = {}) {
|
||||
const calls = [], state = { files: [], groups: [] };
|
||||
const client = Object.fromEntries(['getCredentials', 'oauthStart', 'oauthCallback', 'oauthStatus', 'oauthCancel', 'getUpstreamGroups', 'putUpstreamGroups'].map(name => [name, async (...args) => {
|
||||
calls.push([name, ...args]);
|
||||
if (name === 'oauthStart') return { status: 'ok', state: `s-${calls.length}`, url: `https://${options.hosts?.[args[0]] ?? 'nowhere.invalid'}/oauth/authorize?state=s-${calls.length}` };
|
||||
if (name === 'getCredentials') return { files: state.files };
|
||||
if (name === 'getUpstreamGroups') return structuredClone(state.groups);
|
||||
if (name === 'putUpstreamGroups') { state.groups = args[0].map((g, i) => ({ ...g, keys: g.keys.map((k, j) => ({ ...k, auth_index: `idx-${i}-${j}` })) })); return { status: 'ok' }; }
|
||||
return { status: 'ok' };
|
||||
}]));
|
||||
const p = new ProxyPage({ client, instanceTag: 'fixture', ...(options.providers ? { providers: options.providers } : {}) });
|
||||
return { calls, state, call: (verb, args = {}) => p.request(verb, args, owner) };
|
||||
}
|
||||
const hosts = Object.fromEntries(PROVIDERS.filter(p => p.signIn === 'oauth').map(p => [p.id, p.signInHost]));
|
||||
const door = p => p.signIn === 'oauth' ? ['proxy.login.start', { provider: p.id }] : ['proxy.apikey.add', { provider: p.id, key: 'fixture-key' }];
|
||||
const otherDoor = p => p.signIn === 'oauth' ? ['proxy.apikey.add', { provider: p.id, key: 'fixture-key' }] : ['proxy.login.start', { provider: p.id }];
|
||||
|
||||
test('a3: each provider in PROVIDERS passes its own door and is refused at the other with provider-not-allowed', async () => {
|
||||
assert.ok(PROVIDERS.some(p => p.signIn === 'oauth') && PROVIDERS.some(p => p.signIn === 'apikey'));
|
||||
for (const p of PROVIDERS) {
|
||||
const s = setup({ hosts });
|
||||
const answer = await s.call(...door(p));
|
||||
if (p.signIn === 'oauth') assert.equal(new URL(answer.url).hostname, p.signInHost, p.id);
|
||||
else { assert.deepEqual(answer, { added: true }); assert.equal(s.state.groups.at(-1)['base-url'], p.baseUrl); }
|
||||
const before = s.calls.length;
|
||||
await assert.rejects(s.call(...otherDoor(p)), { code: 'provider-not-allowed' }, p.id);
|
||||
assert.equal(s.calls.length, before, `${p.id}: a refusal reaches no proxy call`);
|
||||
}
|
||||
for (const [verb, args] of [['proxy.login.start', { provider: 'other' }], ['proxy.apikey.add', { provider: 'other', key: 'fixture-key' }]]) {
|
||||
const s = setup({ hosts });
|
||||
await assert.rejects(s.call(verb, args), { code: 'provider-not-allowed' }); assert.equal(s.calls.length, 0);
|
||||
}
|
||||
});
|
||||
|
||||
test('a3: a provider taken out of a test copy of the list is refused at its door, reads Other and leaves the providers answer', async () => {
|
||||
for (const gone of PROVIDERS) {
|
||||
const s = setup({ hosts, providers: PROVIDERS.filter(p => p !== gone) });
|
||||
await assert.rejects(s.call(...door(gone)), { code: 'provider-not-allowed' }, gone.id);
|
||||
assert.equal(s.calls.length, 0, `${gone.id}: refused before any proxy call`);
|
||||
if (gone.signIn === 'oauth') s.state.files = [{ id: 'one.json', name: 'one.json', provider: gone.id, status: 'ready' }];
|
||||
else s.state.groups = [{ name: 'g', 'base-url': gone.baseUrl, keys: [{ auth_index: 'idx-0-0' }], models: [{ name: 'm1' }] }];
|
||||
const answer = await s.call('proxy.accounts');
|
||||
assert.equal(answer.accounts.length, 1);
|
||||
assert.equal(answer.accounts[0].provider, 'other', gone.id);
|
||||
assert.match(answer.accounts[0].displayName, /^Other account /);
|
||||
assert.equal(Object.hasOwn(answer.accounts[0], 'models'), false, `${gone.id}: an unlisted group shows no models`);
|
||||
assert.deepEqual(answer.providers.map(p => p.id), PROVIDERS.filter(p => p !== gone).map(p => p.id));
|
||||
}
|
||||
});
|
||||
|
||||
test('a3: a provider a test copy adds is let through: key group from its base URL, sign-in on its host, name from its label', async () => {
|
||||
const extra = [{ id: 'fixture.ai', label: 'Fixture AI', signIn: 'apikey', baseUrl: 'https://api.fixture.invalid/v1' },
|
||||
{ id: 'fixture-sso', label: 'Fixture SSO', signIn: 'oauth', signInHost: 'sso.fixture.invalid' }];
|
||||
const s = setup({ providers: [...PROVIDERS, ...extra], hosts: { ...hosts, 'fixture-sso': 'sso.fixture.invalid' } });
|
||||
assert.deepEqual(await s.call('proxy.apikey.add', { provider: 'fixture.ai', key: 'fixture-key' }), { added: true });
|
||||
assert.equal(s.state.groups[0]['base-url'], 'https://api.fixture.invalid/v1');
|
||||
assert.match(s.state.groups[0].name, /^mosaic-fixtureai-[0-9a-f]{12}$/);
|
||||
assert.equal(new URL((await s.call('proxy.login.start', { provider: 'fixture-sso' })).url).hostname, 'sso.fixture.invalid');
|
||||
s.state.files = [{ id: 'sso.json', name: 'sso.json', provider: 'fixture-sso', status: 'ready' }];
|
||||
const { accounts } = await s.call('proxy.accounts');
|
||||
assert.deepEqual(accounts.map(a => [a.provider, a.displayName.replace(/ [0-9a-f]{12}$/, '')]), [['fixture-sso', 'Fixture SSO account'], ['fixture.ai', 'Fixture AI account']]);
|
||||
assert.deepEqual(accounts[1].models, []);
|
||||
});
|
||||
|
||||
test('a3: the sign-in address must be on the host the list names for that provider', async () => {
|
||||
const codex = PROVIDERS.find(p => p.id === 'codex');
|
||||
const moved = PROVIDERS.map(p => p === codex ? { ...p, signInHost: 'elsewhere.invalid' } : p);
|
||||
const s = setup({ hosts, providers: moved });
|
||||
await assert.rejects(s.call('proxy.login.start', { provider: 'codex' }), { code: 'proxy-down' });
|
||||
const t = setup({ hosts: { ...hosts, codex: 'elsewhere.invalid' }, providers: moved });
|
||||
assert.equal(new URL((await t.call('proxy.login.start', { provider: 'codex' })).url).hostname, 'elsewhere.invalid');
|
||||
});
|
||||
|
||||
test('a3: the accounts answer carries the providers as id, label and sign-in kind only, no URL, host or key', async () => {
|
||||
const s = setup({ hosts });
|
||||
await s.call('proxy.apikey.add', { provider: 'z.ai', key: 'a3-canary-key-value' });
|
||||
const answer = await s.call('proxy.accounts');
|
||||
assert.deepEqual(answer.providers, PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn })));
|
||||
for (const p of answer.providers) assert.deepEqual(Object.keys(p).sort(), ['id', 'label', 'signIn']);
|
||||
const text = JSON.stringify(answer.providers);
|
||||
for (const word of ['a3-canary-key-value', 'https://', 'api.z.ai', 'claude.ai', 'auth.openai.com', 'baseUrl', 'signInHost']) assert.equal(text.includes(word), false, word);
|
||||
});
|
||||
|
||||
// Review P3-1: an entry missing what its door needs is refused before any proxy call, so no sign-in
|
||||
// is opened at the proxy that the page could not cancel; a key group with no base URL is no provider's.
|
||||
test('a3: an entry with no sign-in host or no key base URL is refused before any proxy call and names no group', async () => {
|
||||
const bare = [{ id: 'nohost', label: 'No Host', signIn: 'oauth' }, { id: 'nourl', label: 'No URL', signIn: 'apikey' }];
|
||||
const s = setup({ providers: [...PROVIDERS, ...bare], hosts: { ...hosts, nohost: 'nohost.invalid' } });
|
||||
await assert.rejects(s.call('proxy.login.start', { provider: 'nohost' }), { code: 'provider-not-allowed' });
|
||||
await assert.rejects(s.call('proxy.apikey.add', { provider: 'nourl', key: 'fixture-key' }), { code: 'provider-not-allowed' });
|
||||
assert.equal(s.calls.length, 0);
|
||||
s.state.groups = [{ name: 'g', keys: [{ auth_index: 'idx-0-0' }], models: [{ name: 'm1' }] }];
|
||||
s.state.files = [{ id: 'one.json', name: 'one.json', provider: 'nohost', status: 'ready' }];
|
||||
const { accounts } = await s.call('proxy.accounts');
|
||||
assert.deepEqual(accounts.map(a => a.provider), ['other', 'other']);
|
||||
assert.equal(accounts.some(a => Object.hasOwn(a, 'models')), false);
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
// #1873 chat-fail F3: an account the proxy reports as not working carries one plain line,
|
||||
// built only from the structured fields of the proxy's own account read. No upstream text,
|
||||
// label or email is ever reflected, and no new proxy call is made.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { fakeManagement } from './proxy-page-fixture.mjs';
|
||||
const PRIVATE = ['p4-private-label', 'p4-private-email', 'p4-private-token', 'p4-private-message'];
|
||||
const oauth = extra => ({ id: 'oauth-one.json', name: 'oauth-one.json', provider: 'claude', label: PRIVATE[0],
|
||||
email: PRIVATE[1], token: PRIVATE[2], status: 'ready', priority: 3, ...extra });
|
||||
const SIGN_IN = 'The account needs to sign in again. Sign in again, or add another account.';
|
||||
const USAGE = 'The account is out of usage. Wait until it resets, or add another account.';
|
||||
const usageAt = time => `The account is out of usage. It resets at ${time}. Wait until then, or add another account.`;
|
||||
const PAUSED = time => `The model service paused this account until ${time}. Wait until then, sign in again, or add another account.`;
|
||||
const NO_REASON = 'The model service gave no reason. Wait and try again later, sign in again, or add another account.';
|
||||
async function reasonOf(t, extra) {
|
||||
const s = await fakeManagement(t, { files: [oauth(extra)] });
|
||||
const result = await s.call('proxy.accounts');
|
||||
const text = JSON.stringify(result);
|
||||
for (const value of PRIVATE) assert.equal(text.includes(value), false, `${value} reached the page`);
|
||||
assert.deepEqual(s.state.calls, ['GET /v8/management/credentials', 'GET /v8/management/config/api-keys/claude'], 'no new proxy call');
|
||||
return result.accounts[0];
|
||||
}
|
||||
test('an account that must sign in again says so, from requires_sign_in or a refresh error', async t => {
|
||||
for (const extra of [{ status: 'error', requires_sign_in: true, next_action: 'sign_in', recovery_hint: PRIVATE[3] },
|
||||
{ status: 'error', refresh_error: { code: 'invalid_grant', message: PRIVATE[3], http_status: 400 } },
|
||||
{ unavailable: true, status_message: `401 Unauthorized ${PRIVATE[3]}` },
|
||||
{ status: 'error', status_message: `token expired ${PRIVATE[3]}` }])
|
||||
assert.equal((await reasonOf(t, extra)).reason, SIGN_IN, JSON.stringify(extra));
|
||||
});
|
||||
test('an account out of usage names the reset time when the read carries one', async t => {
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, next_retry_after: '2099-03-04T05:06:07Z' })).reason,
|
||||
usageAt('2099-03-04 05:06 UTC'));
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message: `usage limit reached ${PRIVATE[3]}`, next_retry_after: '2099-03-04T05:06:07+02:00' })).reason,
|
||||
usageAt('2099-03-04 03:06 UTC'));
|
||||
// No time, a past time or a value that is not a time gives no time, never the value itself.
|
||||
for (const next_retry_after of [undefined, '2000-01-01T00:00:00Z', PRIVATE[3], '2099-13-40T99:99:99Z', 12345, { at: 1 }])
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, next_retry_after })).reason, USAGE, String(next_retry_after));
|
||||
});
|
||||
test('a paused account with only a retry time says until when', async t => {
|
||||
assert.equal((await reasonOf(t, { unavailable: true, status_message: PRIVATE[3], next_retry_after: '2099-03-04T05:06:07.123456789Z' })).reason,
|
||||
PAUSED('2099-03-04 05:06 UTC'));
|
||||
});
|
||||
test('a failing account with nothing more in the read says the model service gave no reason', async t => {
|
||||
for (const extra of [{ status: 'error' }, { unavailable: true }, { status: 'unavailable', status_message: PRIVATE[3] },
|
||||
{ status: 'error', status_message: 42, refresh_error: null, requires_sign_in: 'yes', quota_blocked: 1 }])
|
||||
assert.equal((await reasonOf(t, extra)).reason, NO_REASON, JSON.stringify(extra));
|
||||
});
|
||||
test('a working, turned-off or unknown account keeps exactly its fields and gets no line', async t => {
|
||||
for (const extra of [{}, { status: 'active', status_message: 'usage limit reached' }, { disabled: true, requires_sign_in: true }, { status: 'mystery' }])
|
||||
assert.deepEqual(Object.keys(await reasonOf(t, extra)).sort(), ['accountId', 'displayName', 'priority', 'provider', 'status'], JSON.stringify(extra));
|
||||
});
|
||||
test('every line is plain: no em dash, no control character, at most one sentence pair', () => {
|
||||
for (const line of [SIGN_IN, USAGE, usageAt('2099-03-04 05:06 UTC'), PAUSED('2099-03-04 05:06 UTC'), NO_REASON]) {
|
||||
assert.doesNotMatch(line, /[—\u0000-\u001f\u007f]/);
|
||||
assert.ok(line.length <= 160, line);
|
||||
}
|
||||
});
|
||||
// Review round 1: a day the month doesn't have is refused, not rolled into the next month.
|
||||
test('an impossible date gives no reset time; a real leap day does', async t => {
|
||||
for (const next_retry_after of ['2099-02-31T00:00:00Z', '2099-04-31T10:00:00Z', '2099-02-29T00:00:00Z'])
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, next_retry_after })).reason, USAGE, next_retry_after);
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, next_retry_after: '2096-02-29T01:02:03Z' })).reason, usageAt('2096-02-29 01:02 UTC'));
|
||||
});
|
||||
// Review round 1: quota_blocked wins over a message word, and a bare "expired" is not a sign-in.
|
||||
test('an account blocked on usage whose message says expired gets the usage line', async t => {
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, status_message: `usage window expired ${PRIVATE[3]}` })).reason, USAGE);
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message: `trial window expired ${PRIVATE[3]}` })).reason, NO_REASON);
|
||||
for (const status_message of ['session expired', 'credentials expired', 'OAuth token has expired'])
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message })).reason, SIGN_IN, status_message);
|
||||
});
|
||||
// Review round 2: the sign-in words match whole words only, and a prefixed or "is expired" token counts.
|
||||
test('a message word inside another word is not a sign-in; a prefixed or "is expired" token is', async t => {
|
||||
for (const status_message of ['assigning a slot', 'logging paused', 'design input'])
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message })).reason, NO_REASON, status_message);
|
||||
for (const status_message of ['access_token expired', 'token is expired', 'refresh-token has been expired', 'please sign in'])
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message })).reason, SIGN_IN, status_message);
|
||||
});
|
||||
// Review round 3: "relogin", plurals and past forms of "expired" read as a sign-in too.
|
||||
test('relogin, plural and past forms of an expired sign-in say sign in again', async t => {
|
||||
for (const status_message of ['relogin required', 'tokens have expired', 'session was expired', 'credentials were expired'])
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message })).reason, SIGN_IN, status_message);
|
||||
});
|
||||
@@ -8,7 +8,9 @@ import { Broker } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
|
||||
const verbs = ['proxy.accounts', 'proxy.login.start', 'proxy.login.submit', 'proxy.login.status',
|
||||
'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys'];
|
||||
'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys',
|
||||
// #1873 account-remove: a tester, agent or user is refused like every other proxy verb.
|
||||
'proxy.account.remove'];
|
||||
function setup(t, proxyPage = async () => ({ ok: true })) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'p4-bus-'));
|
||||
const store = new Store(root);
|
||||
|
||||
@@ -0,0 +1,371 @@
|
||||
// #1873 proxy-pickup: a running organization bus picks up the shared model proxy that another
|
||||
// organization's bus initialized, with no restart, through the start path's loader and lock. A
|
||||
// bus whose proxy settings the start path would refuse still refuses, and names the cause in one
|
||||
// plain line. Real buses and a fake loopback management server; no live config.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { createProxyLock } from '../src/proxy-lock.mjs';
|
||||
import { Registry } from '../../seat/src/session.mjs';
|
||||
import { loadBusiness } from '../../organization/src/organization-config.mjs';
|
||||
import { resolveInstance } from '../../organization/src/resolve.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
import { PIN } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy, installPaths } from '../../model-proxy/src/settings.mjs';
|
||||
import { FIXTURE_ENTRY, FIXTURE_KEY_NAME } from '../../plugins/model-provider/fixture/index.mjs';
|
||||
|
||||
const sha = value => createHash('sha256').update(value).digest('hex');
|
||||
|
||||
// A loopback stand-in for the proxy's management API: one ready Claude account with one model.
|
||||
async function fakeProxy(t, key, port = 0) {
|
||||
const seen = { calls: 0, wrongKey: 0 };
|
||||
const server = http.createServer((req, res) => {
|
||||
seen.calls++;
|
||||
if (req.headers.authorization !== `Bearer ${key}`) { seen.wrongKey++; res.writeHead(401); res.end('{}'); return; }
|
||||
const url = new URL(req.url, 'http://127.0.0.1');
|
||||
const answer = url.pathname === '/v8/management/credentials' ? { files: [{ id: 'acct-1', name: 'acct-1', provider: 'claude', status: 'ready', priority: 1 }] }
|
||||
: url.pathname === '/v8/management/credentials/models' ? { models: [{ id: 'claude-opus-5-5' }] }
|
||||
: url.pathname === '/v8/management/config/api-keys/claude' ? []
|
||||
: url.pathname === '/v8/management/config/access/api-keys' ? []
|
||||
: null;
|
||||
if (answer === null) { res.writeHead(404); res.end('{}'); return; }
|
||||
res.end(JSON.stringify(answer));
|
||||
});
|
||||
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(port, '127.0.0.1', resolve); });
|
||||
t.after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); });
|
||||
return { port: server.address().port, seen, server };
|
||||
}
|
||||
|
||||
// One instance (config dir + data root) and a fake proxy install whose bytes a test pin names.
|
||||
// The pin keeps the shipped version, so the pinned binary path is the one loadProxy accepts.
|
||||
function instance(t) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pk-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'instance');
|
||||
fs.mkdirSync(join(configDir, 'secrets', 'agents'), { recursive: true, mode: 0o700 });
|
||||
fs.chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const paths = installPaths(dataRoot);
|
||||
fs.mkdirSync(join(paths.dir, 'static'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(paths.binary, 'fixture proxy binary\n', { mode: 0o700 });
|
||||
fs.writeFileSync(paths.panel, '<!doctype html>\n', { mode: 0o600 });
|
||||
const pin = { ...PIN, binary: { ...PIN.binary, sha256: sha('fixture proxy binary\n') }, panel: { ...PIN.panel, sha256: sha('<!doctype html>\n') } };
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root);
|
||||
const key = `pk1873-${randomBytes(24).toString('hex')}`;
|
||||
return { root, configDir, dataRoot, paths, pin, rolesDir, system, key, closers: [] };
|
||||
}
|
||||
|
||||
// A real organization bus on the instance, started with no proxy (as cli bootConfig does when the
|
||||
// instance has no proxy.json at boot).
|
||||
async function orgBus(t, f, id, { providers = ['cliproxy'], edit = () => {}, org = { id }, binding = {} } = {}) {
|
||||
const doc = businessDoc(f.root, id);
|
||||
doc.plugins = { harness: ['pi'], 'model-provider': providers };
|
||||
edit(doc);
|
||||
writeJson(join(f.configDir, 'businesses', `${id}.json`), doc);
|
||||
const business = loadBusiness(id, { dir: f.configDir, rolesDir: f.rolesDir }), resolved = {};
|
||||
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system: f.system, business, instance: name });
|
||||
const dataRoot = join(f.root, `${id}-bus`); fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
|
||||
const bus = await startBroker({ dataRoot, businesses: { [id]: busOrganization(business, resolved) },
|
||||
organization: { dir: f.configDir, rolesDir: f.rolesDir, system: f.system },
|
||||
instanceRoot: f.dataRoot, org, instanceDataRoot: f.dataRoot, instanceConfigDir: f.configDir,
|
||||
initProxy: args => initializeProxy({ ...args, pin: f.pin }), ...binding });
|
||||
t.after(() => bus.close().catch(() => {}));
|
||||
const human = bus.broker.bindHuman({ business: id, human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const ask = (verb, args) => Promise.resolve().then(() => bus.broker.request(human, { verb, args }));
|
||||
return { bus, ask, dataRoot };
|
||||
}
|
||||
|
||||
// Every stderr line written while `run` is in flight.
|
||||
async function capture(run) {
|
||||
const write = process.stderr.write, lines = [];
|
||||
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
|
||||
try { return { value: await run(), lines }; } finally { process.stderr.write = write; }
|
||||
}
|
||||
|
||||
// The proxy init through bus A writes proxy.json and the key, then the proxy starts on that port.
|
||||
async function initThroughA(t, f, a) {
|
||||
const settings = await a.ask('proxy.init', { key: f.key });
|
||||
const proxy = await fakeProxy(t, f.key, settings.port);
|
||||
return { settings, proxy };
|
||||
}
|
||||
|
||||
const notConfigured = (pattern) => e => e.code === 'not-configured' && typeof e.reason === 'string' && pattern.test(e.reason) && e.reason.length <= 300;
|
||||
|
||||
test('a running organization bus answers accounts and a cliproxy catalogue once another bus initialized the shared proxy, with no restart', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
// Before init: bus B has nothing to pick up, and says so.
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/not been set up/));
|
||||
const before = await b.ask('routes');
|
||||
assert.equal(before.catalogue.status, 'unavailable');
|
||||
assert.equal(before.catalogue.reason, 'proxy-not-configured');
|
||||
assert.equal(before.catalogue.notices.length, 1);
|
||||
const { proxy } = await initThroughA(t, f, a);
|
||||
// Bus A holds the client from its own init; bus B picks it up at its next read.
|
||||
assert.deepEqual((await a.ask('proxy.accounts', {})).accounts.map(r => r.provider), ['claude']);
|
||||
const accounts = await b.ask('proxy.accounts', {});
|
||||
assert.deepEqual(accounts.accounts.map(r => [r.provider, r.status]), [['claude', 'ready']]);
|
||||
const after = await b.ask('routes');
|
||||
assert.equal(after.catalogue.status, 'ready');
|
||||
assert.deepEqual(after.catalogue.models.map(m => m.key), ['claude-claude-opus-5-5']);
|
||||
assert.equal(after.catalogue.reason, undefined);
|
||||
assert.equal(proxy.seen.wrongKey, 0);
|
||||
// Bus B reached the proxy through the same lock identity the start path uses.
|
||||
assert.ok(createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }));
|
||||
});
|
||||
|
||||
test('a bus started before the proxy existed and asked only for a catalogue picks the proxy up', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
assert.equal((await b.ask('routes')).catalogue.status, 'unavailable');
|
||||
await initThroughA(t, f, a);
|
||||
assert.equal((await b.ask('routes')).catalogue.status, 'ready');
|
||||
});
|
||||
|
||||
// Each case writes the shared proxy's files by hand into a state the start path refuses.
|
||||
const refusedStates = [
|
||||
['proxy.json names an unpinned binary', /settings do not load/, f => writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: '/elsewhere/cli-proxy-api' })],
|
||||
['proxy.json is not JSON', /settings do not load/, f => fs.writeFileSync(join(f.configDir, 'proxy.json'), '{', { mode: 0o600 })],
|
||||
['the management key file is readable by others', /management key does not load/, f => {
|
||||
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
|
||||
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o644 });
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
|
||||
}],
|
||||
['the management key file is missing', /management key does not load/, f => writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary })],
|
||||
['the proxy state names another port', /proxy lock/, f => {
|
||||
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
|
||||
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o600 });
|
||||
fs.mkdirSync(join(f.dataRoot, 'proxy'), { mode: 0o700 });
|
||||
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:40998` });
|
||||
}],
|
||||
['the proxy state directory is open to others', /proxy lock/, f => {
|
||||
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
|
||||
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o600 });
|
||||
fs.mkdirSync(join(f.dataRoot, 'proxy'), { mode: 0o755 }); fs.chmodSync(join(f.dataRoot, 'proxy'), 0o755);
|
||||
}],
|
||||
];
|
||||
for (const [name, cause, arrange] of refusedStates) test(`no pickup when ${name}: accounts and catalogue refuse with the cause`, async t => {
|
||||
const f = instance(t);
|
||||
const b = await orgBus(t, f, 'beta');
|
||||
arrange(f);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(cause));
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
assert.match(view.catalogue.notices[0], cause);
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
// The host refuses fixture-proxy-configured only when a dev fixture is recorded for the instance.
|
||||
const recordFixture = f => {
|
||||
fs.mkdirSync(join(f.dataRoot, 'dev-fixture'), { mode: 0o700 });
|
||||
fs.writeFileSync(join(f.dataRoot, 'dev-fixture', 'fixture.json'), '{}\n', { mode: 0o600 });
|
||||
};
|
||||
|
||||
test('no pickup for an organization on the fixture model provider, which the host refuses with a proxy', async t => {
|
||||
const f = instance(t);
|
||||
recordFixture(f);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'], edit: doc => { doc.launch.models[FIXTURE_KEY_NAME] = { ...FIXTURE_ENTRY }; } });
|
||||
await initThroughA(t, f, a);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
assert.match(view.catalogue.notices[0], /fixture/);
|
||||
});
|
||||
|
||||
test('no pickup for an organization whose launch table names the fixture module only', async t => {
|
||||
const f = instance(t);
|
||||
recordFixture(f);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
const b = await orgBus(t, f, 'beta', { edit: doc => { doc.launch.models[FIXTURE_KEY_NAME] = { ...FIXTURE_ENTRY }; } });
|
||||
await initThroughA(t, f, a);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('an organization that lists the fixture still picks the proxy up when no fixture is recorded, as the host would start it', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'] });
|
||||
await initThroughA(t, f, a);
|
||||
await b.ask('proxy.accounts', {});
|
||||
assert.ok(b.bus.modelProxy);
|
||||
// A fixture recorded later drops the client at the next request.
|
||||
recordFixture(f);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('a picked-up client is dropped when proxy.json goes away, its port changes, or the key stops loading', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
const { settings } = await initThroughA(t, f, a);
|
||||
await b.ask('proxy.accounts', {});
|
||||
assert.ok(b.bus.modelProxy);
|
||||
const file = join(f.configDir, 'proxy.json'), keyFile = join(f.configDir, 'secrets', 'proxy-management.key');
|
||||
const saved = fs.readFileSync(file);
|
||||
// Port change: the lock identity is bound to the port, so the bus refuses until a restart.
|
||||
writeJson(file, { ...settings, port: settings.port + 1 === 8317 ? settings.port + 2 : settings.port + 1 });
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/proxy lock/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
assert.equal((await b.ask('routes')).catalogue.status, 'unavailable');
|
||||
// Back to the original port, the bus picks it up again.
|
||||
fs.writeFileSync(file, saved);
|
||||
await b.ask('proxy.accounts', {});
|
||||
assert.ok(b.bus.modelProxy);
|
||||
// Key file no longer 0600.
|
||||
fs.chmodSync(keyFile, 0o640);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/management key does not load/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
fs.chmodSync(keyFile, 0o600);
|
||||
await b.ask('proxy.accounts', {});
|
||||
// proxy.json removed.
|
||||
fs.rmSync(file);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/not been set up/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
});
|
||||
|
||||
test('a default-instance bus (no organization binding, no lock) follows a port change to the new client, never the old one', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), d = await orgBus(t, f, 'delta', { org: null });
|
||||
const { settings, proxy } = await initThroughA(t, f, a);
|
||||
assert.deepEqual((await d.ask('proxy.accounts', {})).accounts.map(r => r.provider), ['claude']);
|
||||
const old = d.bus.modelProxy;
|
||||
// A listener on the new port that answers nothing to the right key: reaching it proves the move.
|
||||
const moved = await fakeProxy(t, `other-${f.key.slice(0, 6)}`);
|
||||
writeJson(join(f.configDir, 'proxy.json'), { ...settings, port: moved.port });
|
||||
const before = proxy.seen.calls;
|
||||
await assert.rejects(d.ask('proxy.accounts', {}));
|
||||
assert.ok(d.bus.modelProxy && d.bus.modelProxy !== old);
|
||||
assert.ok(moved.seen.calls > 0);
|
||||
assert.equal(proxy.seen.calls, before);
|
||||
});
|
||||
|
||||
test('a picked-up bus reads its catalogue through the shared lock: a fence makes it unavailable', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
const { proxy } = await initThroughA(t, f, a);
|
||||
assert.equal((await b.ask('routes')).catalogue.status, 'ready');
|
||||
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }).fence({ org: 'alpha' });
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.ok(b.bus.modelProxy, 'the client stays; the fence alone holds the catalogue');
|
||||
});
|
||||
|
||||
test('a fixture record path in any state but absent counts as recorded', async t => {
|
||||
const f = instance(t);
|
||||
fs.writeFileSync(join(f.dataRoot, 'dev-fixture'), 'not a directory\n', { mode: 0o600 });
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'] });
|
||||
await initThroughA(t, f, a);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('with a fixture recorded, an organization document that no longer validates counts as using it', async t => {
|
||||
const f = instance(t);
|
||||
recordFixture(f);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
fs.writeFileSync(join(f.configDir, 'businesses', 'beta.json'), '{', { mode: 0o600 });
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('the bus that ran init also drops its client when the key stops loading', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
await initThroughA(t, f, a);
|
||||
await a.ask('proxy.accounts', {});
|
||||
fs.rmSync(join(f.configDir, 'secrets', 'proxy-management.key'));
|
||||
await assert.rejects(a.ask('proxy.accounts', {}), notConfigured(/management key does not load/));
|
||||
assert.equal(a.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('an init whose lock refuses leaves the init bus holding no client: no ready catalogue without the lock', async t => {
|
||||
const f = instance(t);
|
||||
// The real init, then the proxy state dir is left readable by the group, which the lock refuses.
|
||||
const initProxy = async args => { const settings = await initializeProxy({ ...args, pin: f.pin });
|
||||
fs.mkdirSync(join(f.dataRoot, 'proxy'), { recursive: true }); fs.chmodSync(join(f.dataRoot, 'proxy'), 0o750); return settings; };
|
||||
const a = await orgBus(t, f, 'alpha', { binding: { initProxy } });
|
||||
await assert.rejects(a.ask('proxy.init', { key: f.key }));
|
||||
await fakeProxy(t, f.key, JSON.parse(fs.readFileSync(join(f.configDir, 'proxy.json'), 'utf8')).port);
|
||||
assert.equal(a.bus.modelProxy, null);
|
||||
const view = await a.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.match(view.catalogue.notices[0], /proxy-lock-invalid/);
|
||||
assert.equal(a.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('a default-instance bus (no lock generation) refreshes its catalogue at once on pickup and on drop', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), d = await orgBus(t, f, 'delta', { org: null });
|
||||
// An unavailable catalogue read before init must not be served after the pickup.
|
||||
assert.equal((await d.ask('routes')).catalogue.status, 'unavailable');
|
||||
await initThroughA(t, f, a);
|
||||
assert.equal((await d.ask('routes')).catalogue.status, 'ready');
|
||||
// A ready catalogue read through the picked-up client must not be served after the drop.
|
||||
fs.rmSync(join(f.configDir, 'proxy.json'));
|
||||
const gone = await d.ask('routes');
|
||||
assert.equal(gone.catalogue.status, 'unavailable');
|
||||
assert.equal(gone.catalogue.reason, 'proxy-not-configured');
|
||||
assert.equal(d.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('a picked-up bus clears a proxy fence through the same client and lock', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
// proxy.fence.clear on a bus that has not read anything yet still picks the proxy up first; the
|
||||
// restart evidence check is the existing one and refuses this empty evidence.
|
||||
await assert.rejects(b.ask('proxy.fence.clear', { evidence: {} }));
|
||||
assert.ok(b.bus.modelProxy);
|
||||
});
|
||||
|
||||
test('a bus serving an invalid organization id never picks up a proxy', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
await initThroughA(t, f, a);
|
||||
let b;
|
||||
try { b = await orgBus(t, f, 'beta', { binding: { instanceConfigDir: 42 } }); }
|
||||
catch (error) { assert.equal(error.code, 'invalid-host-request'); return; }
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/organization binding/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('the management key is in no stderr line, response, refusal, event or file the pickup path writes', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
const outputs = [];
|
||||
const { lines } = await capture(async () => {
|
||||
outputs.push(await b.ask('proxy.accounts', {}));
|
||||
outputs.push(await b.ask('routes'));
|
||||
// A refusal after the key stops loading, and the pickup after it loads again.
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
|
||||
await b.ask('proxy.accounts', {}).catch(error => outputs.push({ code: error.code, reason: error.reason, message: error.message, next: error.next }));
|
||||
outputs.push(await b.ask('routes'));
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o600);
|
||||
outputs.push(await b.ask('proxy.accounts', {}));
|
||||
});
|
||||
assert.ok(lines.some(line => line.startsWith('model-proxy: ')), 'the pickup client writes its usual receipts');
|
||||
assert.equal(lines.join('').includes(f.key), false, 'stderr');
|
||||
assert.equal(JSON.stringify(outputs).includes(f.key), false, 'responses');
|
||||
// Every file under bus B's data root (store, WAL, sessions) and the proxy state directory,
|
||||
// except the proxy's own config.yaml that init writes for the proxy process.
|
||||
const walk = dir => fs.readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? walk(join(dir, e.name)) : e.isFile() ? [join(dir, e.name)] : []);
|
||||
const files = [...walk(b.dataRoot), ...walk(join(f.dataRoot, 'proxy')).filter(p => !p.endsWith('/config.yaml'))];
|
||||
assert.ok(files.length > 0);
|
||||
for (const file of files) assert.equal(fs.readFileSync(file).includes(f.key), false, file);
|
||||
});
|
||||
@@ -90,8 +90,12 @@ const ask = (x, state, extra = {}) => x.call('pm', 'request.question', { request
|
||||
options: [{ key: 'web', text: 'Web' }, { key: 'cli', text: 'CLI' }], ...extra });
|
||||
|
||||
test('the console verb list includes the artifact read and existing owner controls', () => {
|
||||
assert.deepEqual([...CONSOLE_VERBS], ['system-types.console', 'project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
|
||||
'request.get.console', 'request.projects.console', 'request.answer.console', 'request.attach.console', 'request.cancel.console']);
|
||||
assert.deepEqual([...CONSOLE_VERBS], ['project.remote.get.console', 'project.remote.set.console', 'autonomy.get.console', 'autonomy.set.console', 'autonomy.review.console', 'decision.confirm.console', 'decision.reverse.console', 'request.workspace.console', 'request.artifact.console', 'decision.preview.console', 'decision.resolve.console', 'hire.preview.console', 'hire.resolve.console', 'hire.start.console', 'hire.templates.console', 'pack.hire.console', 'prd.revoke.console', 'request.mode.console', 'request.draft.console',
|
||||
'request.get.console', 'request.projects.console', 'request.answer.console', 'request.attach.console', 'request.cancel.console',
|
||||
// Starter packs SP4 (#1873): the owner's pack list and preview, and the three pack writes.
|
||||
'packs.console', 'pack.preview.console', 'pack.apply.console', 'board.set.console', 'workflow.set.console',
|
||||
// Starter packs SP8 (#1873): the owner saves and lists templates.
|
||||
'template.save.console', 'template.list.console']);
|
||||
});
|
||||
|
||||
test('only the signed-in owner capability reaches the console verbs and it reaches nothing else', async t => {
|
||||
@@ -302,7 +306,7 @@ test('a cancel is refused when the owner-to-PM edge closed, with no withdrawn de
|
||||
});
|
||||
|
||||
test('3q through 3s widens the kind list to the union, keeps every 3q kind, row, seq and guard, and the chain matches the fresh digest', t => {
|
||||
assert.equal(SCHEMA_VERSION, '3v');
|
||||
assert.equal(SCHEMA_VERSION, '3w');
|
||||
const root = mkdtempSync(join(tmpdir(), 'request-migration-3r-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true })); mkdirSync(join(root, 'bus'), { mode: 0o700 });
|
||||
const path = join(root, 'bus/bus.sqlite'), AT = '2026-11-03T00:00:00.000Z';
|
||||
@@ -310,11 +314,10 @@ test('3q through 3s widens the kind list to the union, keeps every 3q kind, row,
|
||||
// 3s-3t (#797) adds project_archives, outside request_events; the 3q fixture predates it and
|
||||
// the replayed chain recreates it, so the step's text is removed here like the kinds are.
|
||||
const step3t = readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8');
|
||||
// 3t-3u (#1873, SYS1) adds project_system_types the same way, and 3u-3v (U4a) adds store_tasks.
|
||||
const step3u = readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8');
|
||||
// 3u-3v (#1873, U4a) adds store_tasks the same way.
|
||||
const step3v = readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8');
|
||||
// 3p-3q shipped the PRD kinds and mode.changed; 3q-3r adds the three Task 3 kinds after them.
|
||||
const ddl3q = current.replace(step3t, '').replace(step3u, '').replace(step3v, '').replace(",'advanced'", '').replace("'prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','cancelled'", "'prd.invalidated','mode.changed','cancelled'");
|
||||
const ddl3q = current.replace(step3t, '').replace(step3v, '').replace(",'advanced'", '').replace("'prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','cancelled'", "'prd.invalidated','mode.changed','cancelled'");
|
||||
assert.notEqual(ddl3q, current, 'the fixture is the narrower schema');
|
||||
const migration = readFileSync(new URL('../src/migrations/3q-3r.sql', import.meta.url), 'utf8');
|
||||
assert.ok(migration.includes("'prd.submitted','prd.activated','prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','cancelled'"), 'the step carries every 3q kind plus its own');
|
||||
@@ -331,7 +334,7 @@ test('3q through 3s widens the kind list to the union, keeps every 3q kind, row,
|
||||
db.close(); chmodSync(path, 0o600);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const cli = spawnSync(process.execPath, [new URL('../src/migrate-schema.mjs', import.meta.url).pathname, root], { encoding: 'utf8' });
|
||||
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3v ready\n');
|
||||
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3w ready\n');
|
||||
const store = new Store(root);
|
||||
assert.deepEqual(store.all('SELECT * FROM request_events ORDER BY seq'), before, 'every row keeps its seq and bytes, including the 3q kinds');
|
||||
const fresh = new DatabaseSync(':memory:'); fresh.exec(current);
|
||||
|
||||
@@ -230,8 +230,7 @@ test('3r to 3s migration preserves journal and sequence and admits only the new
|
||||
// 3s-3t (#797) adds project_archives; the 3r fixture predates it and the replayed chain recreates it.
|
||||
db.exec(readFileSync(new URL('../schema.sql', import.meta.url), 'utf8')
|
||||
.replace(readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8'), '')
|
||||
.replace(readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8'), '')
|
||||
// 3u-3v (#1873, U4a) adds store_tasks after project_system_types; the same holds for it.
|
||||
// 3u-3v (#1873, U4a) adds store_tasks; the same holds for it.
|
||||
.replace(readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8'), '')
|
||||
.replace(",'advanced'", ''));
|
||||
db.prepare('INSERT INTO meta VALUES(?,?)').run('schema_version', '3r');
|
||||
@@ -253,7 +252,7 @@ test('3r to 3s migration preserves journal and sequence and admits only the new
|
||||
assert.deepEqual(store.all('SELECT * FROM request_events ORDER BY seq'), before);
|
||||
assert.deepEqual(store.all("SELECT name,sql FROM sqlite_master WHERE type='trigger' AND tbl_name IN ('request_events','request_effects') ORDER BY name"), guards);
|
||||
assert.equal(store.get("SELECT name FROM sqlite_master WHERE type='index' AND name='request_events_request'").name, 'request_events_request');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
store.run("INSERT INTO request_events(business,request,revision,kind,at,body) VALUES('demo','r',16,'advanced',?,'{}')", at);
|
||||
assert.equal(store.get("SELECT max(seq) n FROM request_events").n, 42);
|
||||
assert.throws(() => store.run("UPDATE request_events SET kind='opened'"), /append-only/);
|
||||
|
||||
@@ -278,8 +278,7 @@ test('3n_to_3o_is_explicit_offline_preserves_rows_and_old_engine_refuses_new_sch
|
||||
// 3s-3t (#797) adds project_archives after the request tables; the pre-3t fixtures below
|
||||
// predate it and the replayed chain recreates it, so the step's text comes out here too.
|
||||
.replace(readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8'), '')
|
||||
.replace(readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8'), '')
|
||||
// 3u-3v (#1873, U4a) adds store_tasks after project_system_types; the same holds for it.
|
||||
// 3u-3v (#1873, U4a) adds store_tasks; the same holds for it.
|
||||
.replace(readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8'), '')
|
||||
.replace("'setup.wait','bootstrap.stale','bootstrap.completed','prd.submitted','prd.activated','prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','advanced','cancelled'", "'setup.wait','cancelled'");
|
||||
const newDDL = readFileSync(new URL('../src/migrations/3n-3o.sql', import.meta.url), 'utf8');
|
||||
@@ -293,7 +292,7 @@ test('3n_to_3o_is_explicit_offline_preserves_rows_and_old_engine_refuses_new_sch
|
||||
db.close(); chmodSync(path, 0o600); const before = readFileSync(path);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/); assert.deepEqual(readFileSync(path), before);
|
||||
const cli = spawnSync(process.execPath, [new URL('../src/migrate-schema.mjs', import.meta.url).pathname, root], { encoding: 'utf8' });
|
||||
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3v ready\n'); assert.equal(SCHEMA_VERSION, '3v');
|
||||
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3w ready\n'); assert.equal(SCHEMA_VERSION, '3w');
|
||||
const store = new Store(root); assert.equal(store.get("SELECT id FROM events").id, 'preserved');
|
||||
for (const table of ['request_events','request_effects','request_snapshots','request_snapshot_events','request_project_events']) assert.equal(store.get(`SELECT count(*) n FROM ${table}`).n, 0);
|
||||
store.close(); assert.equal(spawnSync(process.execPath, [new URL('../src/migrate-schema.mjs', import.meta.url).pathname, root]).status, 0);
|
||||
@@ -316,8 +315,7 @@ test('3o_to_3p_widens_only_the_event_kinds_and_keeps_rows_seq_and_guards', t =>
|
||||
// The 3o schema is the current one with the narrower kind list.
|
||||
const ddl3o = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8')
|
||||
.replace(readFileSync(new URL('../src/migrations/3s-3t.sql', import.meta.url), 'utf8'), '')
|
||||
.replace(readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8'), '')
|
||||
// 3u-3v (#1873, U4a) adds store_tasks after project_system_types; the same holds for it.
|
||||
// 3u-3v (#1873, U4a) adds store_tasks; the same holds for it.
|
||||
.replace(readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8'), '')
|
||||
.replace("'setup.wait','bootstrap.stale','bootstrap.completed','prd.submitted','prd.activated','prd.invalidated','question.asked','question.answered','attach.selected','mode.changed','advanced','cancelled'", "'setup.wait','cancelled'");
|
||||
const { schemaDigest } = awaitImportStore, AT = '2026-11-03T00:00:00.000Z';
|
||||
@@ -335,7 +333,7 @@ test('3o_to_3p_widens_only_the_event_kinds_and_keeps_rows_seq_and_guards', t =>
|
||||
db.close(); chmodSync(path, 0o600);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const cli = spawnSync(process.execPath, [new URL('../src/migrate-schema.mjs', import.meta.url).pathname, root], { encoding: 'utf8' });
|
||||
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3v ready\n');
|
||||
assert.equal(cli.status, 0, cli.stderr); assert.equal(cli.stdout, 'bus schema 3w ready\n');
|
||||
const store = new Store(root);
|
||||
assert.deepEqual(store.all('SELECT * FROM request_events ORDER BY seq'), before, 'every row keeps its seq and bytes');
|
||||
assert.deepEqual(store.all('SELECT * FROM request_effects ORDER BY seq'), effects);
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { SCHEMA_VERSION, Store, schemaDigest } from '../src/store.mjs';
|
||||
|
||||
// #1873 SP9: bus schema step 3v-3w drops project_system_types with its index, its four triggers and
|
||||
// its rows (Jason's 2026-11-12 ruling removes the system-type kind outright).
|
||||
const TABLE = 'project_system_types';
|
||||
const ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8');
|
||||
const createdBy3u = readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8');
|
||||
const named = (path) => {
|
||||
const db = new DatabaseSync(path, { readOnly: true });
|
||||
try {
|
||||
return {
|
||||
master: db.prepare('SELECT type,name FROM sqlite_master WHERE name LIKE ? OR tbl_name=? ORDER BY type,name').all(`${TABLE}%`, TABLE).map((r) => ({ ...r })),
|
||||
sequence: db.prepare('SELECT name FROM sqlite_sequence WHERE name=?').all(TABLE).map((r) => r.name),
|
||||
digest: schemaDigest(db),
|
||||
version: db.prepare("SELECT value FROM meta WHERE key='schema_version'").get().value,
|
||||
};
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
};
|
||||
|
||||
test('SP9 a store opened at schema 3w has no project_system_types table', (t) => {
|
||||
assert.equal(SCHEMA_VERSION, '3w');
|
||||
assert.equal(ddl.includes(TABLE), false, 'schema.sql no longer creates the table');
|
||||
const root = mkdtempSync(join(tmpdir(), 'sp9-fresh-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
new Store(root).close();
|
||||
const fresh = named(join(root, 'bus/bus.sqlite'));
|
||||
assert.equal(fresh.version, '3w');
|
||||
assert.deepEqual(fresh.master, []);
|
||||
});
|
||||
|
||||
test('SP9 a 3v store holding project_system_types rows migrates to 3w with nothing of the table left', (t) => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'sp9-3v-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
mkdirSync(join(root, 'bus'), { mode: 0o700 });
|
||||
const path = join(root, 'bus/bus.sqlite');
|
||||
const db = new DatabaseSync(path);
|
||||
// The 3v schema is today's schema.sql plus what 3t-3u created; the store's step check refuses any
|
||||
// other shape, so a migration that succeeds proves the fixture is a real 3v store.
|
||||
db.exec(ddl + createdBy3u);
|
||||
db.prepare('INSERT INTO meta VALUES (?,?)').run('schema_version', '3v');
|
||||
db.prepare('INSERT INTO meta VALUES (?,?)').run('schema_digest', schemaDigest(db));
|
||||
db.prepare('INSERT INTO meta VALUES (?,?)').run('comment_cursor_epoch', '2026-11-12T00:00:00.000Z');
|
||||
db.prepare('INSERT INTO projects(id,at,business,name,repo,created_by) VALUES(?,?,?,?,?,?)').run('p1', '2026-11-12T00:00:00.000Z', 'acme', 'example', null, 'jason');
|
||||
const select = db.prepare(`INSERT INTO ${TABLE}(at,business,project,types,by) VALUES(?,?,?,?,?)`);
|
||||
select.run('2026-11-12T00:00:01.000Z', 'acme', 'p1', '["coding"]', 'jason');
|
||||
select.run('2026-11-12T00:00:02.000Z', 'acme', 'p1', '["coding","montage"]', 'jason');
|
||||
db.close();
|
||||
chmodSync(path, 0o600);
|
||||
const before = named(path);
|
||||
assert.deepEqual(before.master.map((r) => r.name).sort(), [TABLE, `${TABLE}_business`, `${TABLE}_no_delete`, `${TABLE}_no_replace`, `${TABLE}_no_update`, `${TABLE}_project`]);
|
||||
assert.deepEqual(before.sequence, [TABLE]);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
assert.deepEqual(named(path), before, 'the refused open changed nothing');
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
t.after(() => store.close());
|
||||
assert.equal(store.get('SELECT name FROM projects WHERE id=?', 'p1').name, 'example', 'other rows stay');
|
||||
const after = named(path);
|
||||
assert.equal(after.version, '3w');
|
||||
assert.deepEqual(after.master, [], 'no table, index or trigger of that name is left');
|
||||
assert.deepEqual(after.sequence, [], 'no sqlite_sequence row of that name is left');
|
||||
const freshRoot = mkdtempSync(join(tmpdir(), 'sp9-ref-'));
|
||||
t.after(() => rmSync(freshRoot, { recursive: true, force: true }));
|
||||
new Store(freshRoot).close();
|
||||
assert.equal(after.digest, named(join(freshRoot, 'bus/bus.sqlite')).digest, 'the migrated schema is the fresh one');
|
||||
});
|
||||
@@ -528,7 +528,8 @@ test('v3b prototype refusals, views and append-only mutations', (t) => {
|
||||
// Task 4 (3p to 3q), Task 3 (3q to 3r) and Task 6b (3r to 3s) widen request_events.kind in place.
|
||||
// #797 MVP3 (3s to 3t) adds project_archives and its five guards; project-archive.test.mjs tests them.
|
||||
// 3t to 3u adds project_system_types and four guards; U4a #1873 (3u to 3v) adds store_tasks and its seven guards.
|
||||
assert.equal(count('table') - 1, 33);
|
||||
assert.equal(count('trigger'), 130);
|
||||
// SP9 #1873 (3v to 3w) drops project_system_types and its four guards.
|
||||
assert.equal(count('table') - 1, 32);
|
||||
assert.equal(count('trigger'), 126);
|
||||
assert.equal(count('view'), 5);
|
||||
});
|
||||
@@ -530,9 +530,9 @@ test('T4 schema: a 3m store migrates to 3n, session_ops and message_cancels are
|
||||
const before = tables(path);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
const after = tables(path);
|
||||
for (const name of ['session_ops', 'message_cancels', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events', 'project_archives', 'project_system_types', 'store_tasks']) {
|
||||
for (const name of ['session_ops', 'message_cancels', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events', 'project_archives', 'store_tasks']) {
|
||||
assert.deepEqual(after[name], [], name);
|
||||
delete after[name];
|
||||
}
|
||||
@@ -571,7 +571,7 @@ async function engine3m(root) {
|
||||
const source = readFileSync(here('../src/store.mjs'), 'utf8');
|
||||
const edited = source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3m).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3m';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3m';")
|
||||
.replace(/\}, \{\n {2}\/\/ Row 271 \(#1776\)[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
|
||||
assert.ok(edited.includes("export const SCHEMA_VERSION = '3m';") && edited.includes('3l-3m') && !edited.includes('3m-3n'), 'the 3m engine has no 3m→3n step');
|
||||
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
|
||||
@@ -588,7 +588,7 @@ test('T4 schema: the 3m engine refuses a 3n store, migrating or not, and leaves
|
||||
const Store3m = await engine3m(root);
|
||||
assert.throws(() => new Store3m(root), /schema-mismatch/);
|
||||
assert.throws(() => new Store3m(root, { migrateSchema: true }), /schema-mismatch/);
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(master(path), was);
|
||||
assert.deepEqual(tables(path), before);
|
||||
// The same engine opens a fresh 3m store, so the refusal is the version's and not a broken copy.
|
||||
|
||||
@@ -374,7 +374,7 @@ test('T7: a 3k store migrates to 3l, each thread\'s default session has the thre
|
||||
const { root, path } = source3k(t);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
// The migrated schema is the fresh one, object for object.
|
||||
const freshRoot = mkdtempSync(join(tmpdir(), 'sessions-fresh-'));
|
||||
t.after(() => rmSync(freshRoot, { recursive: true, force: true }));
|
||||
@@ -430,7 +430,7 @@ async function engine3k(root) {
|
||||
const source = readFileSync(here('../src/store.mjs'), 'utf8');
|
||||
const edited = source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3k).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3k';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3k';")
|
||||
.replace(/\}, \{\n {2}\/\/ Row 264a \(#1769\)[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
|
||||
assert.ok(edited.includes("export const SCHEMA_VERSION = '3k';") && edited.includes('3j-3k') && !edited.includes('3k-3l'), 'the 3k engine has no 3k→3l step');
|
||||
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
|
||||
@@ -447,7 +447,7 @@ test('T7 D9: the 3k engine refuses a 3l store, migrating or not, and leaves it a
|
||||
const Store3k = await engine3k(root);
|
||||
assert.throws(() => new Store3k(root), /schema-mismatch/);
|
||||
assert.throws(() => new Store3k(root, { migrateSchema: true }), /schema-mismatch/);
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(master(path), was);
|
||||
assert.deepEqual(rows(path), before);
|
||||
// The same engine opens a fresh 3k store, so the refusal is the version's and not a broken copy.
|
||||
|
||||
@@ -63,7 +63,7 @@ test('3e copy migrates through 3f to 3g preserving every old column/row and addi
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
try {
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
assert.deepEqual({ ...store.get('SELECT spec_path,spec_commit FROM decisions') }, { spec_path: null, spec_commit: null });
|
||||
assert.equal(store.get('PRAGMA quick_check').quick_check, 'ok');
|
||||
assert.deepEqual(store.all('PRAGMA foreign_key_check'), []);
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
// #1873 lane triage-fence: through the broker, no organization gains the system-only Triage
|
||||
// template. Pack preview and apply, the Console hire start, the chief's hire.raise and
|
||||
// decision.raise, and instances.set add as the human and the tester each refuse with
|
||||
// template-system-only; the Console's hire template list omits triage. The coding pack still
|
||||
// previews as before. Fixtures live in a temp directory.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { Broker } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { pluginHash } from '../../plugins/src/pinned.mjs';
|
||||
import { installPlugin } from '../../plugins/src/install.mjs';
|
||||
import { starterPack as codingPack } from '../../plugins/starter-pack/coding/index.mjs';
|
||||
import { templateHash } from '../../organization/src/definitions.mjs';
|
||||
import { hireDecisions } from '../../organization/src/hire-decisions.mjs';
|
||||
import { organizationWriter } from '../../organization/src/organization.mjs';
|
||||
import { loadBusiness } from '../../organization/src/organization-config.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
const CODE = 'template-system-only';
|
||||
const SUBJECT = { provider: 'test', iss: 'https://issuer.test', sub: 'owner' };
|
||||
const ROUTE = { harness: 'pi', model: 'claude-opus-5-5' };
|
||||
|
||||
// The broker started as the runtime starts it, with the organization writer and installed packs.
|
||||
async function runtime(t) {
|
||||
const { startBroker } = await import('../src/runtime.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'sobus-'));
|
||||
const rolesDir = rolesCopy(root);
|
||||
const doc = businessDoc(root, 'demo');
|
||||
doc.plugins = { harness: ['pi'] };
|
||||
doc.roles.chief.vars = { ...ROUTE };
|
||||
writeJson(join(root, 'businesses/demo.json'), doc);
|
||||
const data = join(root, 'data');
|
||||
mkdirSync(data, { mode: 0o700 });
|
||||
const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery', 'organization.change'], crossRole: [] } } };
|
||||
const businesses = { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles, vars: { 'bus.tester': true } } };
|
||||
const bus = await startBroker({ dataRoot: data, businesses, organization: { dir: root, rolesDir, system: systemFor(root) } });
|
||||
const thaw = (p) => { const st = lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { chmodSync(p, 0o700); for (const e of readdirSync(p)) thaw(join(p, e)); } };
|
||||
t.after(async () => { await bus.close(); thaw(root); rmSync(root, { recursive: true, force: true }); });
|
||||
const b = bus.broker;
|
||||
const owner = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT });
|
||||
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const tester = b.bindTester({ business: 'demo' });
|
||||
const call = async (verb, args = {}, cap = owner) => b.request(cap, { verb, args });
|
||||
const file = () => readFileSync(join(root, 'businesses/demo.json'), 'utf8');
|
||||
let n = 0;
|
||||
const install = (pack) => {
|
||||
const src = join(root, `src-${n++}`);
|
||||
mkdirSync(join(src, 'starter-pack'), { recursive: true });
|
||||
writeFileSync(join(src, 'starter-pack', 'index.mjs'), `export const interfaceVersion = 1;\nexport function starterPack() { return ${JSON.stringify(pack)}; }\n`);
|
||||
const manifest = { name: pack.id, version: '1.0.0', kinds: ['starter-pack'], interfaceVersions: { 'starter-pack': 1 }, sha256: '0'.repeat(64),
|
||||
source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
|
||||
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
manifest.sha256 = pluginHash(src);
|
||||
writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
installPlugin({ dataRoot: data, source: src, expectedHash: manifest.sha256 });
|
||||
};
|
||||
return { b, root, rolesDir, owner, human, tester, call, file, install };
|
||||
}
|
||||
|
||||
// The A4 broker with the real hire service, so the chief's raise and the Console start reach it.
|
||||
function hires(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'sohire-')), rolesDir = rolesCopy(root);
|
||||
writeJson(join(root, 'businesses/demo.json'), { ...businessDoc(root, 'demo'), plugins: { harness: ['pi'] } });
|
||||
const storeRoot = join(root, 'data'); mkdirSync(storeRoot, { mode: 0o700 });
|
||||
const writer = organizationWriter({ dir: root, rolesDir }), store = new Store(storeRoot);
|
||||
const service = hireDecisions({ writer, rolesDir, dataRoot: storeRoot, configDir: root, assertClean: () => {} });
|
||||
const loaded = () => { const doc = loadBusiness('demo', { dir: root, rolesDir }); return { ...doc, roles: Object.fromEntries(Object.entries(doc.roles).map(([k, e]) => [k, { ...doc.definitions[e.definition], ...e }])) }; };
|
||||
const b = new Broker({ store, businesses: { demo: loaded() }, hires: service });
|
||||
t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
|
||||
const owner = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT });
|
||||
const chief = b.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi' });
|
||||
b.request(chief, { verb: 'role.claim' });
|
||||
const call = (cap, verb, args = {}) => b.request(cap, { verb, args });
|
||||
const triage = { template: 'triage', sha256: templateHash(rolesDir, 'triage') };
|
||||
const hire = (extra = {}) => ({ op: 'hire', level: 'org', name: 'sorter', role: triage, route: ROUTE, reason: 'Sort the inbox', ...extra });
|
||||
const decisions = () => store.get('SELECT count(*) n FROM decisions').n;
|
||||
return { owner, chief, call, hire, decisions };
|
||||
}
|
||||
|
||||
const refuses = async (promise) => assert.rejects(Promise.resolve().then(() => promise), (e) => e?.code === CODE);
|
||||
|
||||
test('pack preview refuses an installed pack that defines triage', async (t) => {
|
||||
const h = await runtime(t);
|
||||
const pack = { ...codingPack(), id: 'sorting', label: 'Sorting' };
|
||||
pack.definitions = { ...pack.definitions, triage: { template: 'triage', sha256: templateHash(h.rolesDir, 'triage') } };
|
||||
h.install(pack);
|
||||
const before = h.file();
|
||||
await refuses(h.call('pack.preview.console', { pack: 'sorting' }));
|
||||
assert.equal(h.file(), before);
|
||||
});
|
||||
|
||||
test('pack apply refuses an installed pack that defines triage', async (t) => {
|
||||
const h = await runtime(t);
|
||||
const pack = { ...codingPack(), id: 'sorting', label: 'Sorting' };
|
||||
pack.definitions = { ...pack.definitions, triage: { template: 'triage', sha256: templateHash(h.rolesDir, 'triage') } };
|
||||
h.install(pack);
|
||||
const before = h.file();
|
||||
await refuses(h.call('pack.apply.console', { pack: 'sorting', digest: 'a'.repeat(64) }));
|
||||
assert.equal(h.file(), before);
|
||||
});
|
||||
|
||||
test('instances.set add refuses triage for the human', async (t) => {
|
||||
const h = await runtime(t);
|
||||
const { revision } = await h.call('instances', {}, h.human);
|
||||
const before = h.file();
|
||||
await refuses(h.call('instances.set', { revision, instance: 'sorter', op: 'add', definition: 'triage' }, h.human));
|
||||
assert.equal(h.file(), before);
|
||||
});
|
||||
|
||||
test('instances.set add refuses triage for the tester', async (t) => {
|
||||
const h = await runtime(t);
|
||||
const { revision } = await h.call('instances', {}, h.human);
|
||||
const before = h.file();
|
||||
await refuses(h.call('instances.set', { revision, instance: 'sorter', op: 'add', definition: 'triage' }, h.tester));
|
||||
assert.equal(h.file(), before);
|
||||
});
|
||||
|
||||
test('the instances read offers no triage add', async (t) => {
|
||||
const h = await runtime(t);
|
||||
const view = await h.call('instances', {}, h.human);
|
||||
assert.equal(Object.hasOwn(view.definitions, 'triage'), false);
|
||||
});
|
||||
|
||||
test('the Console hire start refuses a triage template', async (t) => {
|
||||
const f = hires(t);
|
||||
await refuses(f.call(f.owner, 'hire.start.console', f.hire()));
|
||||
assert.equal(f.decisions(), 0);
|
||||
});
|
||||
|
||||
test('the chief hire.raise refuses a triage template at org, project and root level', async (t) => {
|
||||
const f = hires(t);
|
||||
await refuses(f.call(f.chief, 'hire.raise', f.hire()));
|
||||
await refuses(f.call(f.chief, 'hire.raise', f.hire({ level: 'project', project: 'stack' })));
|
||||
await refuses(f.call(f.chief, 'hire.raise', f.hire({ level: 'root' })));
|
||||
assert.equal(f.decisions(), 0);
|
||||
});
|
||||
|
||||
test('the chief decision.raise naming role.hire or role.change refuses a triage template', async (t) => {
|
||||
const f = hires(t);
|
||||
await refuses(f.call(f.chief, 'decision.raise', { action: 'role.hire', hire: f.hire() }));
|
||||
await refuses(f.call(f.chief, 'decision.raise', { action: 'role.change', hire: f.hire({ op: 'change', name: 'coder' }) }));
|
||||
assert.equal(f.decisions(), 0);
|
||||
});
|
||||
|
||||
test('the Console hire template list omits triage and keeps the rest', async (t) => {
|
||||
const f = hires(t);
|
||||
const names = (await f.call(f.owner, 'hire.templates.console')).templates.map((x) => x.name);
|
||||
assert.ok(!names.includes('triage'));
|
||||
assert.ok(names.includes('coder') && names.includes('reviewer'));
|
||||
});
|
||||
|
||||
// The guard: a shipped pack previews and applies as before, and an installed pack with no triage loads.
|
||||
test('the shipped coding pack and an installed pack without triage preview as before', async (t) => {
|
||||
const h = await runtime(t);
|
||||
const preview = await h.call('pack.preview.console', { pack: 'coding' });
|
||||
assert.match(preview.digest, /^[a-f0-9]{64}$/);
|
||||
h.install({ ...codingPack(), id: 'plain', label: 'Plain' });
|
||||
const plain = await h.call('pack.preview.console', { pack: 'plain' });
|
||||
assert.match(plain.digest, /^[a-f0-9]{64}$/);
|
||||
const out = await h.call('pack.apply.console', { pack: 'coding', digest: preview.digest });
|
||||
assert.deepEqual(out.hires.map((x) => [x.name, x.state]), [['pm-1', 'hired'], ['coder-1', 'hired'], ['reviewer-1', 'hired']]);
|
||||
});
|
||||
@@ -1,109 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { Broker } from '../src/broker.mjs';
|
||||
import { installedSystemTypes } from '../../plugins/src/system-types.mjs';
|
||||
|
||||
test('project selections require enabled types, retain inactive selections, and only the owner writes', t => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'sys-'));
|
||||
const store = new Store(root); t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
|
||||
const business = { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: [], crossRole: [] } } }, projects: {}, plugins: { 'system-type': ['coding'] }, vars: { 'bus.tester': true } };
|
||||
const broker = new Broker({ store, businesses: { demo: business } });
|
||||
const owner = broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const tester = broker.bindTester({ business: 'demo' });
|
||||
const agent = broker.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi' });
|
||||
const call = (verb, args = {}, cap = owner) => broker.request(cap, { verb, args });
|
||||
const project = call('project.create', { name: 'example' }).id;
|
||||
assert.deepEqual(call('project.system-types', { project }), { project, selected: [], active: [], inactive: [] });
|
||||
assert.throws(() => call('project.system-types.set', { project, systemTypes: ['montage'] }), /system-type-not-enabled/);
|
||||
assert.throws(() => call('project.system-types.set', { project, systemTypes: ['coding'] }, tester), /tester-refused/);
|
||||
assert.throws(() => call('project.system-types.set', { project, systemTypes: ['coding'] }, agent), /human-required/);
|
||||
assert.throws(() => call('project.system-types.set', { project, systemTypes: ['coding', 'coding'] }), /invalid-request/);
|
||||
call('project.system-types.set', { project, systemTypes: ['coding'] });
|
||||
assert.deepEqual(call('project.system-types', { project }).active, ['coding']);
|
||||
broker.reloadBusiness('demo', { ...business, plugins: { 'system-type': [] } });
|
||||
assert.deepEqual(call('project.system-types', { project }), { project, selected: ['coding'], active: [], inactive: ['coding'] });
|
||||
assert.deepEqual(store.all('SELECT by FROM project_system_types').map(r => r.by), ['jason']);
|
||||
assert.throws(() => store.run("UPDATE project_system_types SET types='[]'"), /append-only/);
|
||||
assert.throws(() => store.run('DELETE FROM project_system_types'), /append-only/);
|
||||
assert.throws(() => store.run("INSERT INTO project_system_types(at,business,project,types,by) VALUES(?,?,?,?,?)", new Date().toISOString(), 'other', project, '[]', 'jason'), /belongs to its project/);
|
||||
});
|
||||
|
||||
test('console owner can read system types but cannot write project selections', async t => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'sys-'));
|
||||
const store = new Store(root); t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
|
||||
const business = { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: [], crossRole: [] } } }, projects: {}, plugins: { 'system-type': ['coding'] }, vars: { 'bus.tester': true } };
|
||||
let discoveries = 0, enabled = ['coding'];
|
||||
const broker = new Broker({ store, businesses: { demo: business }, organization: (_id, verb) => { if (verb === 'system-types.current') return enabled; if (verb === 'system-types.installed') { discoveries++; return installedSystemTypes(); } } });
|
||||
const owner = broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const consoleOwner = broker.bindConsole({ business: 'demo', human: 'jason', subject: { provider: 'test', iss: 'test', sub: 'owner' } });
|
||||
const tester = broker.bindTester({ business: 'demo' });
|
||||
assert.throws(() => broker.request(tester, { verb: 'plugins.set', args: { revision: 'a'.repeat(64), plugins: { 'system-type': ['coding'] } } }), /human-required/);
|
||||
const project = broker.request(owner, { verb: 'project.create', args: { name: 'example' } }).id;
|
||||
const result = await broker.request(consoleOwner, { verb: 'system-types.console', args: { project } });
|
||||
assert.deepEqual(result.enabled, ['coding']);
|
||||
assert.equal(result.installed.length, 5);
|
||||
assert.equal(discoveries, 1);
|
||||
broker.request(owner, { verb: 'project.system-types.set', args: { project, systemTypes: ['coding'] } });
|
||||
enabled = [];
|
||||
assert.deepEqual(broker.request(owner, { verb: 'project.system-types', args: { project } }).inactive, ['coding']);
|
||||
assert.throws(() => broker.request(owner, { verb: 'project.system-types.set', args: { project, systemTypes: ['coding'] } }), /system-type-not-enabled/);
|
||||
assert.deepEqual(result.project, { project, selected: [], active: [], inactive: [] });
|
||||
assert.throws(() => broker.request(consoleOwner, { verb: 'project.system-types.set', args: { project, systemTypes: ['coding'] } }), /human-required/);
|
||||
});
|
||||
|
||||
test('3t store migrates offline without altering existing project rows', async t => {
|
||||
const { DatabaseSync } = await import('node:sqlite');
|
||||
const { readFileSync, mkdirSync, chmodSync } = await import('node:fs');
|
||||
const { schemaDigest } = await import('../src/store.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'sys-migration-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
mkdirSync(join(root, 'bus'), { mode: 0o700 });
|
||||
const dbpath = join(root, 'bus/bus.sqlite');
|
||||
const db = new DatabaseSync(dbpath);
|
||||
const ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8');
|
||||
const step = readFileSync(new URL('../src/migrations/3t-3u.sql', import.meta.url), 'utf8');
|
||||
assert.equal(ddl.split(step).length, 2);
|
||||
// 3u-3v (U4a) adds store_tasks after this step; a 3t store has neither.
|
||||
const later = readFileSync(new URL('../src/migrations/3u-3v.sql', import.meta.url), 'utf8');
|
||||
db.exec(ddl.replace(step, '').replace(later, ''));
|
||||
db.prepare('INSERT INTO meta VALUES(?,?)').run('schema_version', '3t');
|
||||
db.prepare('INSERT INTO meta VALUES(?,?)').run('schema_digest', schemaDigest(db));
|
||||
db.prepare('INSERT INTO meta VALUES(?,?)').run('comment_cursor_epoch', '2026-01-01T00:00:00.000Z');
|
||||
db.prepare('INSERT INTO projects(id,at,business,name,repo,created_by) VALUES(?,?,?,?,?,?)').run('old', '2026-01-01T00:00:00.000Z', 'demo', 'old', null, 'jason');
|
||||
db.close(); chmodSync(dbpath, 0o600);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const migrated = new Store(root, { migrateSchema: true }); t.after(() => migrated.close());
|
||||
assert.equal(migrated.get('SELECT name FROM projects WHERE id=?', 'old').name, 'old');
|
||||
assert.equal(migrated.get('SELECT count(*) AS n FROM project_system_types').n, 0);
|
||||
assert.equal(migrated.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
});
|
||||
|
||||
test('runtime reads the current organization file for project selections and console discovery', async t => {
|
||||
const { mkdirSync } = await import('node:fs');
|
||||
const { startBroker } = await import('../src/runtime.mjs');
|
||||
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'sys-runtime-'));
|
||||
const rolesDir = rolesCopy(root), doc = businessDoc(root, 'demo');
|
||||
doc.plugins = { 'system-type': ['coding'] };
|
||||
writeJson(join(root, 'businesses/demo.json'), doc);
|
||||
mkdirSync(join(root, 'data'), { mode: 0o700 });
|
||||
const business = { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: {chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: { authority: { withinRole: [], crossRole: [] } } } };
|
||||
const bus = await startBroker({ dataRoot: join(root, 'data'), businesses: { demo: business }, organization: { dir: root, rolesDir, system: systemFor(root) } });
|
||||
t.after(async () => { await bus.close(); rmSync(root, { recursive: true, force: true }); });
|
||||
const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const consoleOwner = bus.broker.bindConsole({ business: 'demo', human: 'jason', subject: { provider: 'test', iss: 'test', sub: 'owner' } });
|
||||
const call = (verb, args = {}, cap = human) => bus.broker.request(cap, { verb, args });
|
||||
const project = call('project.create', { name: 'example' }).id;
|
||||
call('project.system-types.set', { project, systemTypes: ['coding'] });
|
||||
const view = await call('system-types.console', { project }, consoleOwner);
|
||||
assert.deepEqual(view.enabled, ['coding']);
|
||||
assert.deepEqual(view.project.active, ['coding']);
|
||||
assert.equal(view.installed.length, 5);
|
||||
const plugins = call('plugins');
|
||||
call('plugins.set', { revision: plugins.revision, plugins: { 'system-type': [] } });
|
||||
assert.deepEqual(call('project.system-types', { project }).inactive, ['coding']);
|
||||
});
|
||||
@@ -402,7 +402,8 @@ test('REQ-TASK-1 a 3u store migrates to 3v: store_tasks is new and empty, every
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
mkdirSync(join(root, 'bus'), { mode: 0o700 });
|
||||
const path = join(root, 'bus/bus.sqlite'), db = new DatabaseSync(path);
|
||||
db.exec(currentDDL.replace(stepSQL, ''));
|
||||
// SP9 (#1873): a 3u store still has what 3t-3u created; 3v-3w drops it on the way to the fresh schema.
|
||||
db.exec(currentDDL.replace(stepSQL, '') + readFileSync(here('../src/migrations/3t-3u.sql'), 'utf8'));
|
||||
db.prepare('INSERT INTO meta VALUES (?,?)').run('schema_version', '3u');
|
||||
db.prepare('INSERT INTO meta VALUES (?,?)').run('schema_digest', schemaDigest(db));
|
||||
db.prepare('INSERT INTO meta VALUES (?,?)').run('comment_cursor_epoch', '2026-11-09T00:00:00.000Z');
|
||||
@@ -414,7 +415,7 @@ test('REQ-TASK-1 a 3u store migrates to 3v: store_tasks is new and empty, every
|
||||
assert.deepEqual(master(path), was, 'the refused open changed nothing');
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
t.after(() => store.close());
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.equal(store.get('SELECT id FROM events').id, 'preserved');
|
||||
assert.deepEqual(store.all('SELECT * FROM store_tasks'), []);
|
||||
const freshRoot = mkdtempSync(join(tmpdir(), 'ts-fresh-'));
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
// #1873 SP8: saving a template at the bus. Only the organization's owner saves or lists: the CLI's
|
||||
// human cap on the unsuffixed names, the owner's console cap on the .console ones. The saved file
|
||||
// applies through SP4 as saved:<name> for its saver.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { CONSOLE_VERBS, TEMPLATE_BUS_KEYS, TESTER_ORGANIZATION_VERBS } from '../src/broker.mjs';
|
||||
import { REFUSALS } from '../src/refusals.mjs';
|
||||
import { VERB_RIGHTS } from '../../organization/src/org-rights.mjs';
|
||||
|
||||
const TOKEN = 'fixture-held-token-sp8-bus-3a9d';
|
||||
const SUBJECT = { provider: 'test', iss: 'test', sub: 'owner' };
|
||||
|
||||
async function harness(t, { mutate = () => {} } = {}) {
|
||||
const { startBroker } = await import('../src/runtime.mjs');
|
||||
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs');
|
||||
const { entryFromTemplate, readTemplate } = await import('../../organization/src/definitions.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'tmpl-'));
|
||||
const rolesDir = rolesCopy(root);
|
||||
const doc = businessDoc(root, 'demo');
|
||||
doc.plugins = { harness: ['pi'] };
|
||||
const writer = entryFromTemplate(readTemplate(rolesDir, 'coder'));
|
||||
writer.instructions = 'Write code for demo. Ask jason.';
|
||||
doc.definitions = { writer };
|
||||
// An instance of the org definition, with its own token files and tracker bot.
|
||||
doc.roles.ana = { ...structuredClone(doc.roles.coder), definition: 'org:writer', tracker: { bot: 'bot-demo-ana', botId: 11 } };
|
||||
mutate(doc, root);
|
||||
writeJson(join(root, 'businesses/demo.json'), doc);
|
||||
const data = join(root, 'data');
|
||||
mkdirSync(data, { mode: 0o700 });
|
||||
const token = join(root, 'held.token');
|
||||
writeFileSync(token, TOKEN, { mode: 0o600 });
|
||||
chmodSync(token, 0o600);
|
||||
const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } },
|
||||
coder: { authority: { withinRole: [], crossRole: [] }, credentials: { gitea: { file: token, rotateBy: '2099-01-01' } } } };
|
||||
const businesses = { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, vars: { 'bus.tester': true }, roles } };
|
||||
const bus = await startBroker({ dataRoot: data, businesses, organization: { dir: root, rolesDir, system: systemFor(root) } });
|
||||
t.after(async () => { await bus.close(); rmSync(root, { recursive: true, force: true }); });
|
||||
const b = bus.broker;
|
||||
const consoleOwner = b.bindConsole({ business: 'demo', human: 'jason', subject: SUBJECT });
|
||||
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const call = async (verb, args = {}, cap = consoleOwner) => b.request(cap, { verb, args });
|
||||
const path = join(root, 'businesses/demo.json');
|
||||
const saved = (name, account = 'jason') => join(root, 'templates', account, `${name}.json`);
|
||||
return { bus, b, root, rolesDir, path, consoleOwner, human, call, saved };
|
||||
}
|
||||
|
||||
// Preview, then save with the preview's digest: the console names by default, the cli names for cap.
|
||||
const save = async (call, name, extra = {}, cap) => {
|
||||
const verb = cap ? 'template.save' : 'template.save.console';
|
||||
const preview = await call(verb, { name, preview: true, ...extra }, cap);
|
||||
return { preview, out: await call(verb, { name, digest: preview.digest, ...extra }, cap) };
|
||||
};
|
||||
|
||||
test('SP8 red before: the owner saves an organization whose instance holds credentials; neither credentials nor copy material reach the file', async t => {
|
||||
const { call, saved, root } = await harness(t);
|
||||
const { preview, out } = await save(call, 'demo-shape');
|
||||
assert.deepEqual(out, { name: 'demo-shape', scope: 'org', digest: preview.digest });
|
||||
const text = readFileSync(saved('demo-shape'), 'utf8');
|
||||
assert.equal(text, preview.json);
|
||||
for (const k of ['copyBindings', 'copyId', 'orgIncarnation', 'generation', 'tracker', 'holder']) assert.ok(!text.includes(`"${k}"`), k);
|
||||
assert.ok(!text.includes(join(root, 'secrets')), 'a token file path reached the template');
|
||||
assert.ok(!text.includes('"file"'));
|
||||
// The role's credential request lines are kept.
|
||||
assert.ok(JSON.parse(text).definitions.writer.role.credentials.length > 0);
|
||||
});
|
||||
|
||||
test('SP8 a saved template applies through SP4 as saved:<name> for its saver', async t => {
|
||||
const { call, path } = await harness(t);
|
||||
await save(call, 'demo-shape');
|
||||
const listed = await call('template.list.console');
|
||||
assert.deepEqual(listed.templates.map(x => [x.name, x.scope]), [['demo-shape', 'org']]);
|
||||
const preview = await call('pack.preview.console', { pack: 'saved:demo-shape' });
|
||||
assert.equal(preview.pack.origin, 'saved');
|
||||
assert.ok(preview.hires.some(h => h.name === 'coder-1'), JSON.stringify(preview.hires.map(h => h.name)));
|
||||
const before = JSON.parse(readFileSync(path, 'utf8'));
|
||||
const out = await call('pack.apply.console', { pack: 'saved:demo-shape', digest: preview.digest });
|
||||
const after = JSON.parse(readFileSync(path, 'utf8'));
|
||||
assert.equal(after.packs.length, (before.packs ?? []).length + 1);
|
||||
assert.equal(after.packs.at(-1).origin, 'saved');
|
||||
assert.equal(after.packs.at(-1).id, 'demo-shape');
|
||||
// writer is already the organization's own (same); the template references the coder template.
|
||||
assert.ok(out.added.includes('definitions.coder'), JSON.stringify(out.added));
|
||||
assert.equal(preview.hires.find(h => h.name === 'coder-1').definition, 'writer');
|
||||
});
|
||||
|
||||
test('SP8 the owner\'s CLI cap saves on the unsuffixed name and writes the file', async t => {
|
||||
const { call, human, saved } = await harness(t);
|
||||
const { out } = await save(call, 'cli-shape', {}, human);
|
||||
assert.equal(out.name, 'cli-shape');
|
||||
assert.ok(existsSync(saved('cli-shape')));
|
||||
assert.deepEqual((await call('template.list', {}, human)).templates.map(x => x.name), ['cli-shape']);
|
||||
});
|
||||
|
||||
test('SP8 who may call: a member\'s session, an agent and the tester refuse; the owner\'s console cap cannot call the cli name', async t => {
|
||||
const { b, call, human, root } = await harness(t);
|
||||
assert.deepEqual(TESTER_ORGANIZATION_VERBS, ['limits', 'plugins', 'instances', 'plugins.set', 'instances.set', 'routes.set', 'routes']);
|
||||
const member = b.bindHireConsole({ business: 'demo', actor: 'alice', subject: { provider: 'test', iss: 'test', sub: 'alice' }, rights: ['org.hire', 'board.read', 'org.setup'] });
|
||||
for (const verb of ['template.save', 'template.save.console']) assert.throws(() => b.request(member, { verb, args: { name: 'x', preview: true } }), /human-required/);
|
||||
for (const verb of ['template.list', 'template.list.console']) assert.throws(() => b.request(member, { verb, args: {} }), /human-required/);
|
||||
const tester = b.bindTester({ business: 'demo' });
|
||||
assert.throws(() => b.request(tester, { verb: 'template.save', args: { name: 'x', preview: true } }), /tester-refused/);
|
||||
const agent = b.bindLaunch({ business: 'demo', role: 'coder', run: 'coder-run', harness: 'pi', address: 'coder-run' });
|
||||
b.request(agent, { verb: 'role.claim' });
|
||||
for (const verb of ['template.save', 'template.save.console']) assert.throws(() => b.request(agent, { verb, args: { name: 'x', preview: true } }), /human-required/);
|
||||
await assert.rejects(call('template.save', { name: 'x', preview: true }), /human-required/);
|
||||
await assert.rejects(call('template.save.console', { name: 'x', preview: true }, human), /human-required/);
|
||||
assert.ok(!existsSync(join(root, 'templates')));
|
||||
});
|
||||
|
||||
test('SP8 the real credential store: a held value in kept text refuses template-secret-suspected with the field path as its reason', async t => {
|
||||
// A definition holding one never loads (definition-holds-credential); workflow text is what save
|
||||
// must catch itself.
|
||||
const workflow = { description: `Ship it, key ${TOKEN}`, steps: [{ title: 'Write', state: 'todo', roles: ['writer'] }] };
|
||||
const { call, root } = await harness(t, { mutate: doc => { doc.workflows = { change: workflow }; } });
|
||||
await assert.rejects(call('template.save.console', { name: 'leaky', preview: true }), (e) => {
|
||||
assert.equal(e.code, 'template-secret-suspected');
|
||||
assert.equal(e.reason, 'workflows.change.description');
|
||||
assert.ok(!String(e.message).includes(TOKEN) && !JSON.stringify(e).includes(TOKEN));
|
||||
return true;
|
||||
});
|
||||
assert.ok(!existsSync(join(root, 'templates')));
|
||||
});
|
||||
|
||||
test('SP8 a key that holds a held value: the refusal reason masks it as [key], never the value', async t => {
|
||||
// A definition keyed by one never loads (definition-holds-credential); a workflow keyed by one is
|
||||
// what save sees. The broker also drops any reason its credential check refuses.
|
||||
const workflow = { description: 'Ship it.', steps: [{ title: 'Write', state: 'todo', roles: ['writer'] }] };
|
||||
const { call, root } = await harness(t, { mutate: doc => { doc.workflows = { [TOKEN]: workflow }; } });
|
||||
await assert.rejects(call('template.save.console', { name: 'leaky', preview: true }), (e) => {
|
||||
assert.equal(e.code, 'template-secret-suspected');
|
||||
assert.equal(e.reason, 'workflows[key]');
|
||||
assert.ok(!String(e.message).includes(TOKEN) && !JSON.stringify(e).includes(TOKEN));
|
||||
return true;
|
||||
});
|
||||
assert.ok(!existsSync(join(root, 'templates')));
|
||||
});
|
||||
|
||||
test('SP8 a save after a real pack.apply.console keeps each packs entry as {id, version}', async t => {
|
||||
const { call, saved } = await harness(t);
|
||||
const packs = await call('packs.console');
|
||||
const shipped = packs.packs.find(p => p.origin === 'shipped');
|
||||
assert.ok(shipped, JSON.stringify(packs.packs.map(p => [p.id, p.origin])));
|
||||
const pack = await call('pack.preview.console', { pack: shipped.id });
|
||||
await call('pack.apply.console', { pack: shipped.id, digest: pack.digest });
|
||||
await save(call, 'after-apply');
|
||||
const kept = JSON.parse(readFileSync(saved('after-apply'), 'utf8')).packs;
|
||||
assert.deepEqual(kept, [{ id: shipped.id, version: pack.pack.version }]);
|
||||
});
|
||||
|
||||
test('SP8 argument checks: a second save, a .. name, an unknown key, preview with a digest, and a stale digest', async t => {
|
||||
const { call, saved } = await harness(t);
|
||||
const { preview } = await save(call, 'demo-shape');
|
||||
const first = readFileSync(saved('demo-shape'));
|
||||
await assert.rejects(call('template.save.console', { name: 'demo-shape', digest: preview.digest }), (e) => e.code === 'template-exists' && e.reason === 'a template of this name is already saved; save under another name');
|
||||
assert.deepEqual(readFileSync(saved('demo-shape')), first);
|
||||
for (const name of ['..', '../x', 'a/b']) await assert.rejects(call('template.save.console', { name, preview: true }), { code: 'invalid-request' });
|
||||
for (const args of [{ name: 'x', preview: true, account: 'alex' }, { name: 'x', preview: true, digest: preview.digest }, { name: 'x' }, { preview: true }, { name: 'x', preview: false }])
|
||||
await assert.rejects(call('template.save.console', args), { code: 'invalid-request' });
|
||||
await assert.rejects(call('template.list.console', { account: 'alex' }), { code: 'invalid-request' });
|
||||
await assert.rejects(call('template.save.console', { name: 'other', digest: preview.digest }), { code: 'pack-plan-stale' });
|
||||
});
|
||||
|
||||
test('SP8 the schema pins: both .console verbs are console verbs, owner-only in VERB_RIGHTS, and template-secret-suspected has a rail', () => {
|
||||
for (const verb of ['template.save.console', 'template.list.console']) {
|
||||
assert.ok(CONSOLE_VERBS.includes(verb), verb);
|
||||
assert.ok(Object.hasOwn(VERB_RIGHTS, verb) && VERB_RIGHTS[verb] === null, verb);
|
||||
assert.ok(Object.hasOwn(TEMPLATE_BUS_KEYS, verb));
|
||||
}
|
||||
assert.ok(!CONSOLE_VERBS.includes('template.save') && !CONSOLE_VERBS.includes('template.list'));
|
||||
const c = 'template-secret-suspected';
|
||||
assert.ok(Object.hasOwn(REFUSALS, c));
|
||||
assert.ok(REFUSALS[c].rail?.length > 5);
|
||||
assert.ok(REFUSALS[c].nextStep?.length > 20);
|
||||
assert.ok(!/(^|[\s("'`])(\/|~\/)/.test(REFUSALS[c].nextStep));
|
||||
});
|
||||
@@ -49,7 +49,7 @@ async function database3d(t) {
|
||||
const source = readFileSync(here('../src/store.mjs'), 'utf8');
|
||||
const pinned = source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3d).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3d';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3d';")
|
||||
.replace(/\}, \{\n {2}from: '3d',[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
|
||||
assert.ok(!pinned.includes('3d-3e'), 'the 3d engine has no 3d→3e step');
|
||||
writeFileSync(join(moduleRoot, 'src/store.mjs'), pinned);
|
||||
@@ -101,7 +101,7 @@ test('REQ-THREAD-1 REQ-ORG-2: the 3d→3e migration adds the thread tables and l
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
t.after(() => store.close());
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
for (const table of ['projects', 'threads', 'thread_claims', 'message_threads', 'thread_opens'])
|
||||
assert.equal(store.get(`SELECT count(*) n FROM ${table}`).n, 0, table);
|
||||
// Parked: the undelivered ones are counted per business and role, and none is changed or given a thread.
|
||||
@@ -161,5 +161,5 @@ test('REQ-THREAD-1: the offline command prints the kept-message counts before th
|
||||
const command = new URL('../src/migrate-schema.mjs', import.meta.url).pathname;
|
||||
const migrated = spawnSync(process.execPath, [command, root], { encoding: 'utf8' });
|
||||
assert.equal(migrated.status, 0, migrated.stderr);
|
||||
assert.equal(migrated.stdout, [...REPORT, 'bus schema 3v ready', ''].join('\n'));
|
||||
assert.equal(migrated.stdout, [...REPORT, 'bus schema 3w ready', ''].join('\n'));
|
||||
});
|
||||
@@ -403,12 +403,12 @@ test('a 3l store migrates to 3m: thread_shares is new and empty, and every other
|
||||
const before = tables(path);
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
new Store(root, { migrateSchema: true }).close();
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
const after = tables(path);
|
||||
assert.deepEqual(after.thread_shares, []);
|
||||
delete after.thread_shares;
|
||||
// Row 271 (#1776): the 3m→3n step's session_ops and message_cancels, new and empty too.
|
||||
for (const name of ['session_ops', 'message_cancels', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events', 'project_archives', 'project_system_types', 'store_tasks']) {
|
||||
for (const name of ['session_ops', 'message_cancels', 'request_events', 'request_effects', 'request_snapshots', 'request_snapshot_events', 'request_project_events', 'project_archives', 'store_tasks']) {
|
||||
assert.deepEqual(after[name], [], name);
|
||||
delete after[name];
|
||||
}
|
||||
@@ -439,7 +439,7 @@ async function engine3l(root) {
|
||||
const source = readFileSync(here('../src/store.mjs'), 'utf8');
|
||||
const edited = source
|
||||
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3l).digest('hex')}';`)
|
||||
.replace("export const SCHEMA_VERSION = '3v';", "export const SCHEMA_VERSION = '3l';")
|
||||
.replace("export const SCHEMA_VERSION = '3w';", "export const SCHEMA_VERSION = '3l';")
|
||||
.replace(/\}, \{\n {2}\/\/ Row 206 \(193b2[\s\S]*?\}\];\nconst timedTables/, '}];\nconst timedTables');
|
||||
assert.ok(edited.includes("export const SCHEMA_VERSION = '3l';") && edited.includes('3k-3l') && !edited.includes('3l-3m'), 'the 3l engine has no 3l→3m step');
|
||||
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
|
||||
@@ -456,7 +456,7 @@ test('the 3l engine refuses a 3m store, migrating or not, and leaves it as it wa
|
||||
const Store3l = await engine3l(root);
|
||||
assert.throws(() => new Store3l(root), /schema-mismatch/);
|
||||
assert.throws(() => new Store3l(root, { migrateSchema: true }), /schema-mismatch/);
|
||||
assert.equal(version(path), '3v');
|
||||
assert.equal(version(path), '3w');
|
||||
assert.deepEqual(master(path), was);
|
||||
assert.deepEqual(tables(path), before);
|
||||
// The same engine opens a fresh 3l store, so the refusal is the version's and not a broken copy.
|
||||
|
||||
@@ -656,3 +656,80 @@ test('C2-09a: creative bindLaunch without a thread refuses thread-required', t =
|
||||
assert.equal(ctx.count('SELECT count(*) n FROM threads'), 0);
|
||||
assert.equal(ctx.count('SELECT count(*) n FROM role_claims'), 0);
|
||||
});
|
||||
|
||||
// #1873 chat-fail F2: the launcher's trusted failureNotice puts one plain line in the conversation
|
||||
// for each message a refused start or a stalled run left unanswered.
|
||||
const LIMIT_LINE = "The turn failed: the agent reached a usage limit. Wait for it to reset, or check the limits in Settings.\nDetails: start refused: limit-reached";
|
||||
const STALL_LINE = "The turn failed: the model service gave no answer in time, so the turn was stopped. Try again, or check the account in Settings > Model setup.\nDetails: stopped: proxy-stall";
|
||||
const lines = (store, id) => store.all("SELECT * FROM messages WHERE in_reply_to=? AND from_run='bus'", id);
|
||||
|
||||
test('#1873 F2: a refused start puts one line per pending message in its session, once, and none for a wait', (t) => {
|
||||
const { b, store, call, A, B } = twoThreads(t);
|
||||
b.endLaunch({ business: 'demo', run: 'a-run', reason: 'idle' });
|
||||
const session = store.get('SELECT session FROM session_defaults WHERE thread=?', A).session;
|
||||
const notice = (code, extra = {}) => b.failureNotice({ business: 'demo', thread: A, session, code, ...extra });
|
||||
const one = call('human', 'thread.send', { thread: A, body: 'one' });
|
||||
const two = call('human', 'thread.send', { thread: A, body: 'two' });
|
||||
const other = call('human', 'thread.send', { thread: B, body: 'for B' });
|
||||
// A start that waits for a free place leaves no line.
|
||||
for (const code of ['capacity-full', 'instance-running', 'legacy-run-live', 'session-workspace-busy', 'processing-generation-changed', 'proxy-lock-busy', 'project-pending', 'processing-halted']) assert.deepEqual(notice(code), { posted: 0 });
|
||||
assert.deepEqual(notice('limit-reached'), { posted: 2 });
|
||||
for (const sent of [one, two]) {
|
||||
const [line] = lines(store, sent.id);
|
||||
assert.equal(line.body, LIMIT_LINE);
|
||||
assert.deepEqual([line.class, line.from_role, line.to_role, line.from_run], ['RESULT', 'assistant', 'human', 'bus']);
|
||||
assert.equal(store.get('SELECT session FROM message_threads WHERE message=?', line.id).session, session, 'the line is in the session');
|
||||
}
|
||||
assert.deepEqual(lines(store, other.id), [], "another thread's message gets none");
|
||||
// Every retry meets the same refusal: still one line each, under any code.
|
||||
assert.deepEqual(notice('limit-reached'), { posted: 0 });
|
||||
assert.deepEqual(notice('model-provider-unavailable'), { posted: 0 });
|
||||
assert.equal(lines(store, one.id).length, 1);
|
||||
// The message stays pending, so the next start still takes it.
|
||||
assert.equal(b.threadsState('demo').find((s) => s.thread === A).pending, 2);
|
||||
const three = call('human', 'thread.send', { thread: A, body: 'three' });
|
||||
assert.deepEqual(notice('some-new-code'), { posted: 1 });
|
||||
assert.match(lines(store, three.id)[0].body, /^The turn failed: the assistant couldn't start\. .*\nDetails: start refused: some-new-code$/);
|
||||
});
|
||||
|
||||
test('#1873 F2: a stalled run puts one line on each message delivered to it and unread, thread or role', (t) => {
|
||||
const { b, store, call, A } = twoThreads(t);
|
||||
const taken = call('human', 'thread.send', { thread: A, body: 'taken' });
|
||||
assert.deepEqual(call('a', 'message.receive').map((m) => m.id), [taken.id]);
|
||||
const waiting = call('human', 'thread.send', { thread: A, body: 'still waiting' });
|
||||
assert.deepEqual(b.failureNotice({ business: 'demo', run: 'a-run', code: 'proxy-stall' }), { posted: 1 });
|
||||
assert.equal(lines(store, taken.id)[0].body, STALL_LINE);
|
||||
assert.deepEqual(lines(store, waiting.id), [], 'a message the run never took waits for the next run');
|
||||
assert.deepEqual(b.failureNotice({ business: 'demo', run: 'a-run', code: 'proxy-stall' }), { posted: 0 });
|
||||
// A read message was answered by the turn: no line.
|
||||
const read = call('human', 'thread.send', { thread: A, body: 'read' });
|
||||
call('a', 'message.receive');
|
||||
call('a', 'message.read', { id: read.id });
|
||||
assert.deepEqual(b.failureNotice({ business: 'demo', run: 'a-run', code: 'proxy-stall' }), { posted: 1 }, 'only waiting, delivered now');
|
||||
assert.deepEqual(lines(store, read.id), []);
|
||||
// A role run: the human's message to the chief, outside any thread.
|
||||
const asked = call('human', 'message.send', { to: 'chief', class: 'REQUEST', body: 'chief, please' });
|
||||
assert.ok(call('chief', 'message.receive').some((m) => m.id === asked.id));
|
||||
assert.deepEqual(b.failureNotice({ business: 'demo', run: 'chief-run', code: 'proxy-stall' }), { posted: 1 });
|
||||
const [line] = lines(store, asked.id);
|
||||
assert.deepEqual([line.body, line.from_role, line.to_role], [STALL_LINE, 'chief', 'human']);
|
||||
assert.equal(store.get('SELECT count(*) n FROM message_threads WHERE message=?', line.id).n, 0);
|
||||
});
|
||||
|
||||
test('#1873 F2: failureNotice refuses a malformed record before any write', (t) => {
|
||||
const { b, store, A } = twoThreads(t);
|
||||
const session = store.get('SELECT session FROM session_defaults WHERE thread=?', A).session;
|
||||
const before = store.get('SELECT count(*) n FROM messages').n;
|
||||
const bad = [
|
||||
[{ business: 'demo', code: 'limit-reached' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'limit-reached', run: 'a-run', thread: A, session }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'Limit Reached', run: 'a-run' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'x'.repeat(65), run: 'a-run' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 7, run: 'a-run' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'limit-reached', run: 'a-run', why: 'secret text' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'limit-reached', run: 'nobody-run' }, 'unknown-run'],
|
||||
[{ business: 'demo', code: 'limit-reached', thread: A, session: 'no-such-session' }, 'session-not-found'],
|
||||
];
|
||||
for (const [record, code] of bad) assert.throws(() => b.failureNotice(record), { code }, JSON.stringify(record));
|
||||
assert.equal(store.get('SELECT count(*) n FROM messages').n, before);
|
||||
});
|
||||
@@ -121,3 +121,24 @@ test('committed mutation followed by dropped reply reports unknown and is never
|
||||
assert.equal(calls, 1);
|
||||
assert.equal(store.get('SELECT count(*) n FROM messages').n, 1);
|
||||
});
|
||||
|
||||
// E2E6 fix 6: a connect that fails before the request is written is marked notSent; the code stays
|
||||
// outcome-unknown for every caller. A request that left and got no answer is not marked.
|
||||
test('E2E6 a failed connect is outcome-unknown marked notSent; a dropped answer is not marked', async (t) => {
|
||||
const { createServer } = await import('node:net');
|
||||
const { writeFileSync } = await import('node:fs');
|
||||
const { NOT_SENT_EXIT } = await import('../src/client.mjs');
|
||||
assert.equal(NOT_SENT_EXIT, 5);
|
||||
const dir = mkdtempSync(join(tmpdir(), 'e2e6-'));
|
||||
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
||||
const plain = join(dir, 'plain');
|
||||
writeFileSync(plain, '');
|
||||
for (const path of [join(dir, 'missing.sock'), plain, join(plain, 'below')]) {
|
||||
await assert.rejects(new Client({ path, cap: 'x' }).call('inbox', {}), (e) => e.code === 'outcome-unknown' && e.notSent === true, path);
|
||||
}
|
||||
const sock = join(dir, 'drop.sock');
|
||||
const server = createServer((c) => c.once('data', () => c.destroy()));
|
||||
await new Promise((r) => server.listen(sock, r));
|
||||
t.after(() => server.close());
|
||||
await assert.rejects(new Client({ path: sock, cap: 'x' }).call('inbox', {}), (e) => e.code === 'outcome-unknown' && e.notSent === undefined);
|
||||
});
|
||||
@@ -0,0 +1,305 @@
|
||||
// proxy-scope slice 4 (#1873): the Triage key in the bus. Real organization buses on one instance,
|
||||
// a stand-in proxy management server on loopback and a temporary config directory; nothing here
|
||||
// reads a real config, proxy or secret. Each test scans every answer, event, receipt line and
|
||||
// stderr line for every Triage key the stand-in ever held.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { keyPaths, readKey } from '../src/triage-key.mjs';
|
||||
import { Registry } from '../../seat/src/session.mjs';
|
||||
import { loadBusiness } from '../../organization/src/organization-config.mjs';
|
||||
import { resolveInstance } from '../../organization/src/resolve.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
const NOW = Date.parse('2026-11-18T12:00:00.000Z');
|
||||
const MIN = 60_000, HOUR = 60 * MIN, DAY = 24 * HOUR;
|
||||
const MANAGE = 'fx1873-s4-manage';
|
||||
const KEY_PATH = '/v8/management/config/access/api-keys';
|
||||
const sha = s => createHash('sha256').update(s).digest('hex');
|
||||
const tag = dir => sha(dir).slice(0, 12);
|
||||
const id = key => sha(key).slice(0, 12);
|
||||
const b64 = () => randomBytes(32).toString('base64url');
|
||||
const code = name => e => e.code === name;
|
||||
|
||||
// Every Triage key the stand-in ever held, and every text a bus produced, across the file.
|
||||
const SEEN = new Set();
|
||||
const TEXTS = [];
|
||||
function leaks(text) {
|
||||
for (const key of SEEN) {
|
||||
const secret = key.slice(-43);
|
||||
if (text.includes(key) || text.includes(secret)) return key.slice(0, 8);
|
||||
for (let i = 0; i + 8 <= secret.length; i++) if (text.includes(secret.slice(i, i + 8))) return key.slice(0, 8);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Every stderr line while the test runs: the bus's model-proxy receipt lines go there.
|
||||
function captureStderr(t) {
|
||||
const write = process.stderr.write, lines = [];
|
||||
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
|
||||
t.after(() => { process.stderr.write = write; TEXTS.push(lines.join('')); });
|
||||
return lines;
|
||||
}
|
||||
|
||||
async function instance(t) {
|
||||
const root = fs.realpathSync(fs.mkdtempSync(join(tmpdir(), 'tkb-')));
|
||||
const configDir = join(root, 'config');
|
||||
fs.mkdirSync(join(configDir, 'secrets'), { recursive: true, mode: 0o700 });
|
||||
fs.chmodSync(configDir, 0o700); fs.chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
fs.writeFileSync(join(configDir, 'secrets', 'proxy-management.key'), MANAGE, { mode: 0o600 });
|
||||
fs.mkdirSync(join(root, 'proxy'), { mode: 0o700 });
|
||||
const sentinel = `msk-${tag(configDir)}-sentinel-${b64()}`, orgKey = `mso-${tag(configDir)}-other-key-${b64()}`;
|
||||
// triagePuts counts the writes that changed a Triage name; the organization buses' own key
|
||||
// reconcile writes the list too, and those do not count.
|
||||
const state = { keys: [sentinel, 'foreign-client-key', orgKey], puts: [], triagePuts: 0 };
|
||||
const triageNames = list => JSON.stringify(list.filter(k => k.startsWith('mst-')).sort());
|
||||
const server = http.createServer(async (req, res) => {
|
||||
if (req.url !== KEY_PATH) {
|
||||
const key = req.headers.authorization?.slice(7);
|
||||
res.writeHead(state.keys.includes(key) ? 200 : 401); res.end('{}'); return;
|
||||
}
|
||||
if (req.headers.authorization !== `Bearer ${MANAGE}`) { res.writeHead(401); res.end('{}'); return; }
|
||||
if (req.method === 'GET') { res.end(JSON.stringify(state.keys)); return; }
|
||||
const chunks = []; for await (const c of req) chunks.push(c);
|
||||
const next = JSON.parse(Buffer.concat(chunks));
|
||||
for (const k of next) if (/^mst-.*-[A-Za-z0-9_-]{43}$/.test(k)) SEEN.add(k);
|
||||
state.puts.push(next);
|
||||
if (triageNames(next) !== triageNames(state.keys)) state.triagePuts++;
|
||||
state.keys = next;
|
||||
res.end('{"status":"ok"}');
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
const port = server.address().port;
|
||||
const closers = [];
|
||||
t.after(async () => {
|
||||
for (const close of closers.reverse()) await close().catch(() => {});
|
||||
server.closeAllConnections(); await new Promise(resolve => server.close(resolve));
|
||||
const receipts = join(root, 'proxy', 'mosaic-keys.receipts.jsonl');
|
||||
if (fs.existsSync(receipts)) TEXTS.push(fs.readFileSync(receipts, 'utf8'));
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root);
|
||||
const f = {
|
||||
root, configDir, port, state, closers, rolesDir, system, sentinel, orgKey, clock: { now: NOW },
|
||||
prefix: `mst-${tag(configDir)}-triage-`,
|
||||
file: keyPaths(configDir).file,
|
||||
record: () => readKey(configDir),
|
||||
triageKeys: () => f.state.keys.filter(k => k.startsWith(f.prefix)),
|
||||
receipts: () => {
|
||||
const p = join(root, 'proxy', 'mosaic-keys.receipts.jsonl');
|
||||
return fs.existsSync(p) ? fs.readFileSync(p, 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse) : [];
|
||||
},
|
||||
// A real organization bus on this instance. `proxy: false` starts it with no proxy settings, as
|
||||
// a bus on an instance with no proxy.json does.
|
||||
async bus(orgId, { proxy = true, triageKeys = {} } = {}) {
|
||||
const doc = businessDoc(root, orgId);
|
||||
doc.plugins = { ...doc.plugins, harness: ['pi'] };
|
||||
writeJson(join(configDir, 'businesses', `${orgId}.json`), doc);
|
||||
const business = loadBusiness(orgId, { dir: configDir, rolesDir }), resolved = {};
|
||||
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system, business, instance: name });
|
||||
const dataRoot = join(root, `${orgId}-bus`);
|
||||
fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
|
||||
const bus = await startBroker({ dataRoot, businesses: { [orgId]: busOrganization(business, resolved) },
|
||||
organization: { dir: configDir, rolesDir, system }, ...(proxy ? { proxy: { port, binary: '/fixture-unused' } } : {}),
|
||||
org: { id: orgId }, instanceDataRoot: root, instanceConfigDir: configDir,
|
||||
triageKeys: { intervalMs: 3_600_000, now: () => f.clock.now, ...triageKeys } });
|
||||
closers.push(() => bus.close());
|
||||
const cap = bus.broker.bindHuman({ business: orgId, human: business.human ?? doc.human, via: 'cli', outsideAgent: true });
|
||||
const ask = async (verb, args) => {
|
||||
try { const answer = await bus.broker.request(cap, { verb, args }); TEXTS.push(JSON.stringify(answer)); return answer; }
|
||||
catch (error) { TEXTS.push(JSON.stringify({ code: error.code, reason: error.reason, message: error.message, nextStep: error.nextStep })); throw error; }
|
||||
};
|
||||
const sweep = async () => { const r = await bus.triageKeySweep(); TEXTS.push(JSON.stringify(r)); return r; };
|
||||
return { bus, ask, sweep, dataRoot };
|
||||
},
|
||||
};
|
||||
return f;
|
||||
}
|
||||
|
||||
// Every byte under a bus's data root (its store, with events and refusals, and its logs), as text.
|
||||
function eventsText(b, called = true) {
|
||||
const out = [];
|
||||
const walk = dir => {
|
||||
for (const name of fs.readdirSync(dir)) {
|
||||
const p = join(dir, name), st = fs.lstatSync(p);
|
||||
if (st.isDirectory()) walk(p);
|
||||
else if (st.isFile()) out.push(fs.readFileSync(p).toString('latin1'));
|
||||
}
|
||||
};
|
||||
walk(b.dataRoot);
|
||||
if (called) assert.ok(out.join('').includes('proxy.triage.key'), 'the store holds the verb\'s events');
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
test('ensure issues one key, a second ensure keeps it, and both answers carry ids only', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
const first = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(first.action, 'issued');
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
const record = f.record();
|
||||
assert.equal(first.keyId, record.keyId);
|
||||
assert.equal(first.keyId, id(f.triageKeys()[0]));
|
||||
assert.deepEqual(first.addedKeyIds, [record.keyId]);
|
||||
assert.equal(fs.statSync(f.file).mode & 0o777, 0o600);
|
||||
const second = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.deepEqual(second, { op: 'ensure', action: 'held', addedKeyIds: [], removedKeyIds: [], keyId: record.keyId, rotateBy: record.rotateBy });
|
||||
assert.equal(f.triageKeys().length, 1, 'ensure twice gives one key');
|
||||
assert.equal(f.record().key, record.key);
|
||||
// Through a second riding bus on the same instance: still the one key.
|
||||
const b = await f.bus('beta');
|
||||
assert.equal((await b.ask('proxy.triage.key', { op: 'ensure' })).action, 'held');
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
assert.equal(f.state.triagePuts, 1, 'only the first ensure wrote the proxy list');
|
||||
// The receipt names the change by op and ids.
|
||||
const receipts = f.receipts().filter(r => r.op?.startsWith('triage.'));
|
||||
assert.deepEqual(receipts.map(r => [r.op, r.addedKeyIds, r.removedKeyIds]), [['triage.key.ensure', [record.keyId], []]]);
|
||||
for (const text of [eventsText(a), eventsText(b)]) assert.equal(leaks(text), null);
|
||||
});
|
||||
|
||||
test('ensure replaces a key the proxy lost, and revoke removes every Triage name and the file', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
const issued = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
// The proxy loses the key (removed by hand): ensure issues a new one and names the old as lost.
|
||||
f.state.keys = f.state.keys.filter(k => !k.startsWith(f.prefix));
|
||||
const replaced = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(replaced.action, 'replaced');
|
||||
assert.notEqual(replaced.keyId, issued.keyId);
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
// An orphan in the namespace (a crash left it) goes with revoke too.
|
||||
const orphan = `${f.prefix}${b64()}`; SEEN.add(orphan);
|
||||
f.state.keys.push(orphan);
|
||||
const revoked = await a.ask('proxy.triage.key', { op: 'revoke' });
|
||||
assert.equal(revoked.action, 'revoked');
|
||||
assert.deepEqual(revoked.removedKeyIds.sort(), [replaced.keyId, id(orphan)].sort());
|
||||
assert.equal(f.triageKeys().length, 0);
|
||||
assert.equal(fs.existsSync(f.file), false);
|
||||
assert.equal(f.record(), null);
|
||||
// Revoke again: nothing to do, nothing written.
|
||||
const puts = f.state.triagePuts;
|
||||
assert.deepEqual(await a.ask('proxy.triage.key', { op: 'revoke' }), { op: 'revoke', action: 'none', addedKeyIds: [], removedKeyIds: [] });
|
||||
assert.equal(f.state.triagePuts, puts);
|
||||
// Namespace guard: the sentinel, the organization key and the foreign key are still there.
|
||||
for (const k of [f.sentinel, f.orgKey, 'foreign-client-key']) assert.ok(f.state.keys.includes(k), k.slice(0, 4));
|
||||
assert.equal(leaks(eventsText(a)), null);
|
||||
});
|
||||
|
||||
test('a bus that does not ride the owner\'s proxy refuses triage-no-riding-bus, and its sweep does nothing', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const loose = await f.bus('loose', { proxy: false });
|
||||
await assert.rejects(loose.ask('proxy.triage.key', { op: 'ensure' }), e => e.code === 'triage-no-riding-bus'
|
||||
&& /^Triage needs one organization that uses the owner's models\./.test(e.nextStep));
|
||||
await assert.rejects(loose.ask('proxy.triage.key', { op: 'revoke' }), code('triage-no-riding-bus'));
|
||||
assert.equal(f.state.triagePuts, 0);
|
||||
// With a key issued through a riding bus and its rotation due, the loose bus still does nothing.
|
||||
const a = await f.bus('alpha');
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
f.clock.now += 31 * DAY;
|
||||
assert.deepEqual(await loose.sweep(), { action: 'none' });
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
});
|
||||
|
||||
test('the sweep rotates when rotateBy comes, once across two buses that wake together, and again a rotation later', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha'), b = await f.bus('beta');
|
||||
const issued = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
// Not due yet: neither bus touches the proxy.
|
||||
assert.deepEqual([await a.sweep(), await b.sweep()], [{ action: 'none' }, { action: 'none' }]);
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
f.clock.now = Date.parse(`${issued.rotateBy}T00:00:00Z`);
|
||||
const both = await Promise.all([a.sweep(), b.sweep()]);
|
||||
assert.deepEqual(both.map(r => r.action).sort(), ['none', 'rotated']);
|
||||
assert.equal(f.state.triagePuts, 2, 'two buses rotated once');
|
||||
const rotated = both.find(r => r.action === 'rotated');
|
||||
const record = f.record();
|
||||
assert.equal(rotated.keyId, record.keyId);
|
||||
assert.deepEqual(record.retiring.map(r => r.keyId), [issued.keyId]);
|
||||
assert.equal(f.triageKeys().length, 2, 'the old key stays live through its grace');
|
||||
// Inside the grace both buses leave it; at retireAt plus the margin one of them retires it.
|
||||
const retireAt = Date.parse(record.retiring[0].retireAt);
|
||||
f.clock.now = retireAt + 10_000;
|
||||
assert.deepEqual((await Promise.all([a.sweep(), b.sweep()])).map(r => r.action), ['none', 'none']);
|
||||
f.clock.now = retireAt + 30_000;
|
||||
const retired = await Promise.all([a.sweep(), b.sweep()]);
|
||||
assert.deepEqual(retired.map(r => r.action).sort(), ['none', 'retired']);
|
||||
assert.equal(f.state.triagePuts, 3);
|
||||
assert.deepEqual(f.triageKeys().map(id), [record.keyId]);
|
||||
assert.deepEqual(f.record().retiring, []);
|
||||
// A second rotation 30 days on: again once.
|
||||
f.clock.now = Date.parse(`${record.rotateBy}T00:00:00Z`) + HOUR;
|
||||
const again = await Promise.all([b.sweep(), a.sweep()]);
|
||||
assert.deepEqual(again.map(r => r.action).sort(), ['none', 'rotated']);
|
||||
assert.equal(f.state.triagePuts, 4);
|
||||
assert.equal(f.triageKeys().length, 2);
|
||||
// Namespace guard across every sweep.
|
||||
for (const k of [f.sentinel, f.orgKey, 'foreign-client-key']) assert.ok(f.state.keys.includes(k), k.slice(0, 4));
|
||||
const ops = f.receipts().filter(r => r.op?.startsWith('triage.')).map(r => r.op);
|
||||
assert.deepEqual(ops, ['triage.key.ensure', 'triage.key.rotate', 'triage.key.retire', 'triage.key.rotate']);
|
||||
for (const bus of [a, b]) assert.equal(leaks(eventsText(bus, bus === a)), null);
|
||||
});
|
||||
|
||||
test('the sweep never issues the first key, and a sweep refusal is a receipt line with a code only', async t => {
|
||||
const lines = captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
f.clock.now += 400 * DAY;
|
||||
assert.deepEqual(await a.sweep(), { action: 'none' });
|
||||
assert.equal(f.state.triagePuts, 0);
|
||||
assert.equal(f.record(), null);
|
||||
// A due rotation on a proxy with no sentinel (it may be another proxy): refused, nothing written.
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
f.state.keys = f.state.keys.filter(k => k !== f.sentinel);
|
||||
f.clock.now += 31 * DAY;
|
||||
const r = await a.sweep();
|
||||
assert.deepEqual(r, { action: 'refused', code: 'proxy-not-configured' });
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
const refusal = lines.join('').split('\n').filter(l => l.includes('triage.key.sweep-refused'));
|
||||
assert.equal(refusal.length, 1);
|
||||
assert.deepEqual(JSON.parse(refusal[0].replace(/^model-proxy: /, '')), { action: 'triage.key.sweep-refused', code: 'proxy-not-configured', reason: 'triage-key-proxy-open' });
|
||||
// The same refusal on the next ticks is not logged again within the hour; after it, once more.
|
||||
const refusals = () => lines.join('').split('\n').filter(l => l.includes('triage.key.sweep-refused')).length;
|
||||
for (let i = 0; i < 3; i++) { f.clock.now += 30_000; assert.equal((await a.sweep()).code, 'proxy-not-configured'); }
|
||||
assert.equal(refusals(), 1);
|
||||
f.clock.now += HOUR;
|
||||
await a.sweep();
|
||||
assert.equal(refusals(), 2);
|
||||
});
|
||||
|
||||
test('a revoke stopped between the proxy write and the file is finished by the next revoke; ensure after a stopped revoke replaces the key', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
let stage = null;
|
||||
const a = await f.bus('alpha', { triageKeys: { hook: s => { if (s === stage) throw new Error(`stop at ${s}`); } } });
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
stage = 'write';
|
||||
await assert.rejects(a.ask('proxy.triage.key', { op: 'revoke' }), e => e.code === 'storage-unavailable' && !/stop at/.test(e.message));
|
||||
assert.equal(f.triageKeys().length, 0, 'the proxy write happened');
|
||||
assert.ok(f.record(), 'the file stayed');
|
||||
stage = null;
|
||||
// ensure now sees a file whose key the proxy lacks: a new key, the old named lost.
|
||||
const replaced = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(replaced.action, 'replaced');
|
||||
stage = 'rename';
|
||||
await assert.rejects(a.ask('proxy.triage.key', { op: 'revoke' }), code('storage-unavailable'));
|
||||
stage = null;
|
||||
assert.equal((await a.ask('proxy.triage.key', { op: 'revoke' })).action, 'revoked');
|
||||
assert.equal(f.record(), null);
|
||||
assert.equal(f.triageKeys().length, 0);
|
||||
});
|
||||
|
||||
test('no answer, event, receipt line or stderr line in this file holds any Triage key the stand-in saw', () => {
|
||||
assert.ok(SEEN.size >= 8, `only ${SEEN.size} keys were seen`);
|
||||
assert.ok(TEXTS.length >= 30, `only ${TEXTS.length} texts`);
|
||||
for (const text of TEXTS) assert.equal(leaks(text), null);
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
// proxy-scope slice 4 (#1873): the bus verb proxy.triage.key at the broker. Only the instance
|
||||
// owner's path-bound proof (a human cap from the cli transport, outside any agent) reaches the
|
||||
// runtime's hook; an agent, a tester, a Console proof, a grant holder and an organization owner get
|
||||
// human-required, a reader read-only. The answer and the event carry key ids only, and a hook
|
||||
// failure that is not a bus refusal becomes storage-unavailable with no text of its own.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { Broker, BusError } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { REFUSALS } from '../src/refusals.mjs';
|
||||
|
||||
const role = { authority: { withinRole: ['message.send'], crossRole: [] } };
|
||||
const code = name => error => error.code === name;
|
||||
const SUBJECT = { provider: 'fixture', iss: 'https://sso.invalid', sub: 'owner-1' };
|
||||
// A Triage key as triage-key.mjs makes them, so a leak check has a real-looking value to look for.
|
||||
const KEY = `mst-0123456789ab-triage-${'Q'.repeat(20)}xY_-${'z'.repeat(19)}`;
|
||||
|
||||
function fixture(t, { proxyTriageKey = null } = {}) {
|
||||
const store = new Store(fs.mkdtempSync(join(tmpdir(), 'tkv-bus-')));
|
||||
t.after(() => store.close());
|
||||
const b = new Broker({ store, proxyTriageKey, businesses: { demo: { id: 'demo', human: 'owner', arbiters: { technical: 'chief', delivery: 'chief' }, vars: { 'bus.tester': true }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: role } } } });
|
||||
return {
|
||||
store, b,
|
||||
human: b.bindHuman({ business: 'demo', human: 'owner', via: 'cli', outsideAgent: true }),
|
||||
tester: b.bindTester({ business: 'demo' }), reader: b.bindReader({ business: 'demo' }),
|
||||
agent: b.bindLaunch({ business: 'demo', role: 'coder', run: 'tkv-run', harness: 'pi' }),
|
||||
// The owner's Console proof, a granted account that holds org.hire, and an organization owner
|
||||
// who is not the instance owner: none is the path-bound cli proof.
|
||||
console: b.bindConsole({ business: 'demo', human: 'owner', subject: SUBJECT }),
|
||||
grant: b.bindHireConsole({ business: 'demo', actor: 'grantee', subject: { ...SUBJECT, sub: 'grantee-1' }, rights: ['org.hire', 'decide'] }),
|
||||
orgOwner: b.bindHireConsole({ business: 'demo', actor: 'orgowner', subject: { ...SUBJECT, sub: 'orgowner-1' }, rights: ['org.hire'], owner: true }),
|
||||
};
|
||||
}
|
||||
const events = store => JSON.stringify(store.all('SELECT body FROM events'));
|
||||
const bodies = store => store.all('SELECT body FROM events').map(r => JSON.parse(r.body));
|
||||
|
||||
test('proxy.triage.key refuses an agent, a tester, a Console proof, a grant holder and an organization owner with human-required, and a reader with read-only', async t => {
|
||||
let calls = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => { calls++; return { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-19', addedKeyIds: ['0123456789ab'], removedKeyIds: [] }; } });
|
||||
for (const op of ['ensure', 'revoke']) {
|
||||
for (const [name, cap] of [['agent', f.agent], ['tester', f.tester], ['console', f.console], ['grant', f.grant], ['orgOwner', f.orgOwner]])
|
||||
assert.throws(() => f.b.request(cap, { verb: 'proxy.triage.key', args: { op } }), code('human-required'), `${name} ${op}`);
|
||||
assert.throws(() => f.b.request(f.reader, { verb: 'proxy.triage.key', args: { op } }), code('read-only'));
|
||||
}
|
||||
assert.equal(calls, 0);
|
||||
// The same verb runs for the owner's cli proof.
|
||||
const answer = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } });
|
||||
assert.equal(answer.action, 'issued');
|
||||
assert.equal(calls, 1);
|
||||
});
|
||||
|
||||
test('proxy.triage.key takes op ensure or revoke and nothing else, and a bus with no hook is not-configured', async t => {
|
||||
let calls = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => { calls++; return { action: 'none', addedKeyIds: [], removedKeyIds: [] }; } });
|
||||
for (const args of [{}, { op: 'rotate' }, { op: 'issue' }, { op: 'ensure', key: KEY }, { op: 'revoke', force: true }, { op: ['ensure'] }])
|
||||
assert.throws(() => f.b.request(f.human, { verb: 'proxy.triage.key', args }), code('invalid-request'), JSON.stringify(args));
|
||||
assert.throws(() => f.b.request(f.human, { verb: 'proxy.triage.key' }), code('invalid-request'));
|
||||
assert.equal(calls, 0);
|
||||
assert.equal(events(f.store).includes(KEY), false);
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
const g = fixture(t);
|
||||
assert.throws(() => g.b.request(g.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), code('not-configured'));
|
||||
});
|
||||
|
||||
test('the answer and the event of each op carry ids only, even when the hook returns more', async t => {
|
||||
const seen = [];
|
||||
const f = fixture(t, { proxyTriageKey: async op => {
|
||||
seen.push(op);
|
||||
// A hook that returns the key beside the ids: the broker copies the named fields only.
|
||||
return op === 'ensure'
|
||||
? { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-19', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], key: KEY, old: { key: KEY } }
|
||||
: { action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'], key: KEY };
|
||||
} });
|
||||
const ensure = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } });
|
||||
assert.deepEqual(ensure, { op: 'ensure', action: 'issued', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], keyId: '0123456789ab', rotateBy: '2026-12-19' });
|
||||
const revoke = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } });
|
||||
assert.deepEqual(revoke, { op: 'revoke', action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'] });
|
||||
assert.deepEqual(seen, ['ensure', 'revoke']);
|
||||
assert.deepEqual(bodies(f.store).filter(b => b.call === 'proxy.triage.key'), [{ call: 'proxy.triage.key', ...ensure }, { call: 'proxy.triage.key', ...revoke }]);
|
||||
for (const text of [JSON.stringify(ensure), JSON.stringify(revoke), events(f.store)]) {
|
||||
assert.equal(text.includes(KEY), false);
|
||||
assert.equal(text.includes(KEY.slice(-43)), false);
|
||||
}
|
||||
});
|
||||
|
||||
test('an answer the broker cannot vouch for refuses proxy-client-response and records no allowed event', async t => {
|
||||
const answers = [
|
||||
null, { action: 'issued' },
|
||||
{ action: 'deleted', keyId: '0123456789ab', addedKeyIds: [], removedKeyIds: [] },
|
||||
{ action: 'issued', keyId: KEY, addedKeyIds: [], removedKeyIds: [] },
|
||||
{ action: 'issued', keyId: '0123456789ab', addedKeyIds: [KEY], removedKeyIds: [] },
|
||||
{ action: 'held', keyId: '0123456789ab', addedKeyIds: [], removedKeyIds: 'none' },
|
||||
];
|
||||
let next = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => answers[next++] });
|
||||
for (const answer of answers)
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), code('proxy-client-response'), JSON.stringify(answer));
|
||||
await assert.rejects(Promise.resolve().then(() => fixture(t, { proxyTriageKey: async () => ({ action: 'issued', addedKeyIds: [], removedKeyIds: [] }) }))
|
||||
.then(g => g.b.request(g.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } })), code('proxy-client-response'));
|
||||
assert.equal(bodies(f.store).some(b => b.call === 'proxy.triage.key'), false);
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
});
|
||||
|
||||
test('a bus refusal from the hook passes through with its guidance; any other failure is storage-unavailable and names nothing from the error', async t => {
|
||||
const f = fixture(t, { proxyTriageKey: async op => { throw op === 'ensure' ? new BusError('triage-no-riding-bus') : Object.assign(new Error(`boom ${KEY} /home/x`), { code: 'EACCES' }); } });
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), e => e.code === 'triage-no-riding-bus'
|
||||
&& /Triage needs one organization that uses the owner's models/.test(e.nextStep ?? REFUSALS['triage-no-riding-bus'].nextStep));
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } }), e => e.code === 'storage-unavailable'
|
||||
&& !JSON.stringify({ ...e, message: e.message }).includes(KEY.slice(-43)) && !String(e.message).includes('/home/x'));
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
});
|
||||
|
||||
test('the two slice 4 codes have refusal-table entries with a next step and no absolute path', () => {
|
||||
for (const c of ['triage-no-riding-bus', 'triage-key-rotated']) {
|
||||
const entry = REFUSALS[c];
|
||||
assert.ok(entry, c);
|
||||
assert.equal(typeof entry.nextStep, 'string', c);
|
||||
assert.ok(entry.nextStep.length > 20, c);
|
||||
assert.equal(/\/(home|mnt|srv|tmp)\//.test(entry.nextStep), false, c);
|
||||
}
|
||||
assert.match(REFUSALS['triage-no-riding-bus'].nextStep, /^Triage needs one organization that uses the owner's models\./);
|
||||
});
|
||||
@@ -68,7 +68,7 @@ test('REQ-LIMIT-4 3g copy migrates through 3h, 3i, 3j and 3k to 3l preserving ev
|
||||
assert.throws(() => new Store(root), /schema-mismatch/);
|
||||
const store = new Store(root, { migrateSchema: true });
|
||||
try {
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3v');
|
||||
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3w');
|
||||
assert.deepEqual({ ...store.get('SELECT to_name FROM messages') }, { to_name: null });
|
||||
assert.equal(store.get('SELECT count(*) AS n FROM usage_records').n, 0);
|
||||
assert.equal(store.get('SELECT name FROM instance_names').name, 'Builder');
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
// Old path of packages/organization/src/copy-binding-status.mjs (rename design r2, stage B).
|
||||
export * from "../../organization/src/copy-binding-status.mjs";
|
||||
@@ -0,0 +1,2 @@
|
||||
// Old path of packages/organization/src/template-save.mjs (rename design r2, stage B).
|
||||
export * from "../../organization/src/template-save.mjs";
|
||||
+133
-13
@@ -1295,9 +1295,16 @@ organization bus, moves its local organization document, history, runtime and
|
||||
organization-specific secret directories into the instance's private
|
||||
`deleted/` directory, and removes it from the registry. The ID may then be
|
||||
reused. The deleted folder contains a receipt; `deleted/deleted.log` is an
|
||||
append-only index. Remote repositories and remote credentials are untouched.
|
||||
Purging deleted data and revoking remote credentials are separate operations and
|
||||
are not provided by this command.
|
||||
append-only index. Remote repositories are untouched. Purging deleted data is a
|
||||
separate operation and is not provided by this command. The receipt's `copies`
|
||||
lists each agent copy that held a service grant (#1873 CB4), with its slot
|
||||
`quarantined` and its remote outcome: `revoked` or `absent` when the
|
||||
organization's managed Gitea admin slot deleted the copy's token, `not-managed`
|
||||
when the organization has no admin slot on that server, and `failed` when the
|
||||
service could not be reached or refused. For any outcome but `revoked` and
|
||||
`absent`, the copy's token at the service still has to be revoked there. When
|
||||
the organization file could not be read, the receipt says `copiesUnreadable:
|
||||
true` instead of listing copies; check the service for copy tokens by hand.
|
||||
|
||||
If deletion stops, the organization remains in `deleting` state and cannot
|
||||
start a bus. Inspect the reported receipt and correct the refusal, then resume
|
||||
@@ -1309,7 +1316,15 @@ 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.
|
||||
their usage checks and before any config is read. After the bus stops,
|
||||
`mosaic org archive` discards every agent copy's credential (#1873 CB4): each
|
||||
copy's slot file is removed and its token is revoked at the service through
|
||||
the organization's managed Gitea admin slot. The receipt's `copies` lists each
|
||||
one with its state and remote outcome. A copy whose token was revoked, was
|
||||
already gone, or is on a server the organization does not manage (`not-managed`)
|
||||
becomes an unbound tombstone. A copy whose remote revoke `failed` stays
|
||||
`revoking`, fenced, and the bus finishes the revoke when it starts after a
|
||||
restore. A restore brings no copy credential back.
|
||||
|
||||
`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
|
||||
@@ -1348,6 +1363,23 @@ 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.
|
||||
|
||||
Starter pack hires (ruling 2026-11-14, #1873): `mosaic org pack apply ID PACK
|
||||
--digest D [--project P]` is the owner's hire decision. After the pack commits,
|
||||
the same command hires each proposed agent through the bus's hire-decisions
|
||||
path (`pack.apply` as the organization's human) and the receipt adds `hires`,
|
||||
one `{name, state, decision | code}` per proposal: `hired`, `exists` (an
|
||||
instance by that name is there), `open` (a hire decision already names it),
|
||||
`awaiting-owner` or `refused` with the hire's code. The owner's apply approves
|
||||
the flagged and credential lines; a credential line is approved, not bound, so
|
||||
the agent's launch is refused `credential-unbound` until its services are
|
||||
bound. `mosaic org pack hire ID PACK [--project P]` (`pack.hire`) runs the same
|
||||
loop on a pack already applied, for the proposals an earlier apply left open,
|
||||
and hires none twice; a pack with no packs entry is refused `pack-not-applied`
|
||||
with the hint to preview and apply it first. A proposal is refused
|
||||
`pack-hire-route-missing` when neither the chief of staff nor the
|
||||
organization's assistant has a harness and model to give it; the chief's route
|
||||
wins when both have one. Like the other pack words, both refuse inside an agent run (exit 3).
|
||||
|
||||
`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,
|
||||
@@ -1355,15 +1387,6 @@ field, digest and archive checks. Edit and archive need the task's digest from
|
||||
`mosaic tasks --json` as `--expect`; a stale one is refused `task-conflict`
|
||||
(exit 2) and nothing is written. They refuse inside an agent run.
|
||||
|
||||
System types are installed plug-ins. `mosaic org system-types installed` prints
|
||||
module metadata; `mosaic org system-types enabled ID` prints an organization's enabled
|
||||
ids. The owner changes the enabled list through the existing organization file
|
||||
update or `mosaic plugins set`. `mosaic project system-types PROJECT --org ID`
|
||||
shows selected, active and inactive ids. The owner replaces the selection with
|
||||
`mosaic project system-types-set PROJECT --types coding,montage --org ID`; an
|
||||
empty `--types ''` clears it. Disabled selections stay recorded but become
|
||||
inactive. Project settings changes are append-only and require human proof.
|
||||
|
||||
## Accounts
|
||||
|
||||
`mosaic accounts` administers the instance-level accounts store locally, as the
|
||||
@@ -1402,3 +1425,100 @@ installer writes `<dataRoot>/.deployed` as mode 0444 naming the instance.
|
||||
`webui-service.sh` refuses accounts and deployed instances because its user
|
||||
manager runs under a human or development seat uid. The receipt uid identifies
|
||||
the invoking process; it does not attribute a human or resist tampering by that uid.
|
||||
|
||||
## Triage route (#1873, held for Jason's ruling)
|
||||
|
||||
`mosaic triage route show` prints the Triage route the instance owner set, or
|
||||
`triage route: not set` (exit 0). `mosaic triage route set --model KEY
|
||||
--harness NAME --cap TOKENS --time-zone ZONE [--revision N]` writes it. Both
|
||||
refuse inside an agent run before anything is read (exit 3).
|
||||
|
||||
The file is `<dataRoot>/triage/route.json`
|
||||
(packages/webui/src/triage-route-store.mjs): a catalogue model key, a harness
|
||||
name and a daily cap leaf `{limit, since, reset, timeZone}`. It holds no env
|
||||
value and no key. The day resets at 00:00 in the zone given, which has no
|
||||
default; an unknown zone refuses. The first cap counts from the time it is
|
||||
set; a later change keeps that `since`, as an organization's token limits do,
|
||||
so lowering the cap never drops tokens already spent. The directory is 0700 and the file 0600,
|
||||
owned by the running user; a link, a non-file, a wrong mode or owner, or an
|
||||
unknown field refuses (exit 3). A write is compare-and-set on `revision`
|
||||
(`--revision 0`: only when nothing is set), atomic, and appends one line with
|
||||
the old and new values to `route-receipts.jsonl`. A write that died leaves
|
||||
`route.lock`; the next write refuses and names it, and you remove it by hand.
|
||||
If the route changed but its receipt line could not be written, the write
|
||||
refuses with `triage-route-unrecorded` (exit 3) and names the new revision.
|
||||
`set` checks the harness against packages/plugins/harness/; it does not check
|
||||
the model key against a catalogue yet.
|
||||
|
||||
The Console does not read this file: the Triage host keeps `route: null` until
|
||||
the wiring slice.
|
||||
|
||||
## Triage key (#1873)
|
||||
|
||||
`mosaic triage key issue|rotate|retire|status` manage Triage's own model
|
||||
proxy client key. Every verb refuses inside an agent run before anything is
|
||||
read (exit 3), and takes no options.
|
||||
|
||||
The key is `mst-<tag>-triage-<43 base64url>`, where `<tag>` is the first 12
|
||||
hex of sha256 of the config directory, the tag on the instance's own `msk-`
|
||||
sentinel. An org bus reconciles only its `mso-<tag>-<org>-key-` names and a
|
||||
no-org bus only its `msk-<tag>-` names, so neither removes it. The file is
|
||||
`<configDir>/secrets/triage/model`: `{version, key, keyId, issuedAt,
|
||||
rotateBy, retiring}`. The directory is 0700 and the file 0600, owned by the
|
||||
running user. A link, a non-file, a second hard link, a wrong mode or owner, a
|
||||
file over 64 KiB, or a record that does not match the shape refuses every verb
|
||||
with `triage-key-unreadable` (exit 3).
|
||||
|
||||
- `issue` adds one key and writes the file. It refuses `triage-key-issued`
|
||||
when a key is already issued; that is `rotate`.
|
||||
- `rotate` adds a new key and moves the old one to `retiring`, with a retire
|
||||
time of now plus `rotateGraceMinutes` from proxy.json, where the bus reads
|
||||
it (default 120). `rotateBy` is 30 days on, as for an org key. An old key
|
||||
the proxy no longer holds is dropped at once.
|
||||
- `retire` removes retiring keys whose grace has passed or that the proxy no
|
||||
longer holds. It changes nothing before then.
|
||||
|
||||
Nothing runs on a timer. The retire time is the earliest a retiring key may
|
||||
go; the key stays live in the proxy until someone runs `retire` (or a later
|
||||
`rotate` or `issue`) after it. `rotateBy` works the same way: `status` marks
|
||||
it due, and only `rotate` replaces the key.
|
||||
- `status` prints whether a key is issued, its key id, its age, its rotate-by
|
||||
day, each retiring key, what the proxy list holds, a set fence, any key in
|
||||
the Triage namespace the file does not hold, and the next command. It takes
|
||||
no lock and writes nothing.
|
||||
|
||||
`issue`, `rotate` and `retire` hold the instance proxy lock that org buses
|
||||
take (`<dataRoot>/proxy`). Each reads the list, refuses without this
|
||||
instance's sentinel (`triage-key-proxy-open`), reads it again and refuses if
|
||||
it moved (`triage-key-raced`), sets the fence, writes, reads back and clears
|
||||
the fence. The proxy changes first and the file second. Each run that
|
||||
changes something appends one line of key ids, never keys, to
|
||||
`<dataRoot>/proxy/mosaic-keys.receipts.jsonl`. That file is checked under the
|
||||
lock before anything changes; an unsafe one refuses
|
||||
`triage-key-receipts-unsafe`. A receipt line that still fails after the change
|
||||
refuses `triage-key-unrecorded`. No output, error or receipt holds a key.
|
||||
|
||||
The file is written to a 0600 temp file in the same directory, synced, renamed
|
||||
into place, and the directory is synced. A failed directory sync after the
|
||||
rename refuses `triage-key-unsynced`: the file is in place but may not survive
|
||||
a power loss. `proxy-lock-busy` means another proxy writer holds the lock; if
|
||||
none is running, `mosaic proxy fence status` shows the lock state.
|
||||
|
||||
After a crash:
|
||||
|
||||
- Killed after the proxy write and before the read-back: the fence stays set
|
||||
and every proxy writer on the instance refuses `proxy-uncertain`. `status`
|
||||
shows the fence. Restart the proxy, run `mosaic proxy fence clear
|
||||
--proxy-restarted`, then run the verb again.
|
||||
- Killed after the read-back and before the file write: the proxy holds a key
|
||||
the file does not. `status` names it; the next `issue`, `rotate` or `retire`
|
||||
removes it. A file that could not be written refuses
|
||||
`triage-key-unwritten` and says to run the verb again.
|
||||
- Killed after the temp file is written and before its rename: a
|
||||
`.model.<pid>.<hex>.tmp` file that may hold a key stays in the triage
|
||||
directory. `status` counts it; the next `issue`, `rotate` or `retire`
|
||||
removes it under the lock.
|
||||
- A `retire` killed after the proxy write leaves a retiring key the proxy no
|
||||
longer holds. `status` marks it; the next `retire` drops it from the file.
|
||||
|
||||
Nothing gives the key to the Triage host yet; that is the wiring slice.
|
||||
@@ -75,7 +75,8 @@ export function readAccounting({ dataRoot,business,start,end }) {
|
||||
try {
|
||||
db.exec('BEGIN');
|
||||
// U4a (#1873) 3u-3v adds only store_tasks, which accounting never reads, so 3v joins the closed list.
|
||||
if (!['3s','3t','3u','3v'].includes(db.prepare("SELECT value FROM meta WHERE key='schema_version'").get()?.value)) fail('test-store-schema-refused');
|
||||
// SP9 (#1873) 3v-3w drops only the table 3t-3u added, which accounting never reads, so 3w joins too.
|
||||
if (!['3s','3t','3u','3v','3w'].includes(db.prepare("SELECT value FROM meta WHERE key='schema_version'").get()?.value)) fail('test-store-schema-refused');
|
||||
const store = { all:(sql,...args) => db.prepare(sql).all(...args) };
|
||||
const records = turnRecords(store,business);
|
||||
const raw = store.all("SELECT * FROM usage_records WHERE business=? AND source<>'@proxy/cliproxy-queue' ORDER BY seq",business);
|
||||
|
||||
@@ -79,7 +79,7 @@ test('accounting reads a real store at the current bus schema version',t=>{
|
||||
const records=readAccounting({dataRoot:f.dataRoot,business:f.c.business,start:f.c.start,end:f.c.end});
|
||||
assert.equal(records.length,1);assert.equal(records[0].input,1);
|
||||
});
|
||||
for(const version of ['3s','3r','3v',null]) test(`accounting ${version==='3s'?'accepts':'refuses'} schema ${version??'missing'}`,t=>{
|
||||
for(const version of ['3s','3r','3x',null]) test(`accounting ${version==='3s'?'accepts':'refuses'} schema ${version??'missing'}`,t=>{
|
||||
const f=setup(t,100);
|
||||
// Only this disposable fixture is altered; the production meta table is append-only.
|
||||
f.store.run('DROP TRIGGER meta_no_update');f.store.run('DROP TRIGGER meta_no_delete');
|
||||
|
||||
@@ -56,6 +56,7 @@ import { ORG_CONFIG, runConfigCommand } from "../../organization/src/config-comm
|
||||
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";
|
||||
import { triageCommand, TRIAGE_USAGE } from "./triage.mjs";
|
||||
|
||||
export const USAGE = `usage:
|
||||
${ACCOUNTS_USAGE}
|
||||
@@ -103,6 +104,7 @@ export const USAGE = `usage:
|
||||
mosaic console start [--dev-local-owner [--dev-insecure-bind ADDR --dev-allow-host NAME[,NAME...]]] [--detach] [--port auto|N]
|
||||
mosaic console stop|status
|
||||
mosaic console restart
|
||||
${TRIAGE_USAGE}
|
||||
${DEV_USAGE}
|
||||
${RUN_USAGE}`;
|
||||
|
||||
@@ -675,15 +677,18 @@ async function bus(rest, io, deps) {
|
||||
const priorBoot = readBootStatus(org.root);
|
||||
try { recordBootOk(org.root, { business: businessId, file }); } catch { /* status is advisory */ }
|
||||
const binding = readNotifyConfig(org.root, businessId);
|
||||
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches } }).catch((e) => {
|
||||
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
|
||||
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
||||
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
|
||||
// #1873 W2: the harness watch runs in the host with the env sessions launch from, unless the
|
||||
// host launches nothing (--no-launch). deps.harnessWatch replaces its options in tests.
|
||||
const harnessWatch = launches ? (deps.harnessWatch === undefined ? {} : deps.harnessWatch) : null;
|
||||
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches }, harnessWatch: harnessWatch ? { env: launchEnv, ...harnessWatch } : null }).catch((e) => {
|
||||
// org-defscan: the broker refused a definition that holds a credential it holds; the load was not good after all.
|
||||
if (e.code === "definition-holds-credential") try { recordBootRefused(org.root, { business: businessId, file, reason: e.message, prior: priorBoot }); } catch { /* the refusal still throws */ }
|
||||
throw e;
|
||||
});
|
||||
try {
|
||||
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
|
||||
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
||||
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
|
||||
// U4a (#1873): agents get the task tools when the bus serves task verbs for the organization: a
|
||||
// Vikunja tracker in the boot config, or the built-in store, which an organization with no
|
||||
// tracker gets (the bus runtime makes the same choice, runtime.mjs trackerOf).
|
||||
@@ -778,7 +783,7 @@ export async function main(argv, io = { env: process.env, stdin: process.stdin,
|
||||
if (["inbox", "decide", "tasks", "agents", "trail"].includes(verb)) return human(verb, rest, io, deps);
|
||||
if (verb === "project" && rest[0] === "north-star") return northStarCommand(rest.slice(1), io, (p) => callFor(p, io, deps));
|
||||
if (verb === "project" && rest[0] === "interview") return interviewCommand(rest.slice(1), io);
|
||||
if (verb === "project") return ["create", "list", "archive", "unarchive", "system-types", "system-types-set"].includes(rest[0]) ? projectBusCommand(rest, io, (p) => channelCallFor(p, io, deps)) : projectCommand(rest, io, deps, (p) => callFor(p, io, deps));
|
||||
if (verb === "project") return ["create", "list", "archive", "unarchive"].includes(rest[0]) ? projectBusCommand(rest, io, (p) => channelCallFor(p, io, deps)) : projectCommand(rest, io, deps, (p) => callFor(p, io, deps));
|
||||
if (verb === "threads") return threadsCommand(rest, io, (p) => channelCallFor(p, io, deps));
|
||||
if (verb === "session") return sessionCommand(rest, io, (p) => channelCallFor(p, io, deps));
|
||||
if (verb === "thread") return threadCommand(rest, io, (p) => channelCallFor(p, io, deps), formatMessage);
|
||||
@@ -818,6 +823,8 @@ export async function main(argv, io = { env: process.env, stdin: process.stdin,
|
||||
if (verb === "launches") return launches(rest, io, deps);
|
||||
if (verb === "bus") return bus(rest, io, deps);
|
||||
if (verb === "console") return consoleCommand(rest, io, deps);
|
||||
// triage-route-store (#1873): the owner's Triage route file, read and written as the person.
|
||||
if (verb === "triage") return triageCommand(rest, io, deps);
|
||||
if (verb === "dev") return devCommand(rest, io, { system: deps.system });
|
||||
if (verb === "run") return runCommand(rest, io, { system: deps.system, callFor: (p) => channelCallFor(p, io, deps) });
|
||||
if (verb === "-h" || verb === "--help") return io.stdout.write(`${USAGE}\n`);
|
||||
@@ -845,6 +852,7 @@ export async function run(argv, io = { env: process.env, stdin: process.stdin, s
|
||||
if (!(error instanceof CliError)) throw error;
|
||||
io.stderr.write(`mosaic: ${error.message}\n`);
|
||||
if (OTHER_TREE_CODES.has(error.code)) io.stderr.write(`mosaic: ${OTHER_TREE}\n`);
|
||||
if (typeof error.next === "string" && error.next !== "") io.stderr.write(`mosaic: next: ${error.next}\n`);
|
||||
return error.exitCode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,15 @@
|
||||
// input, a decision or option that doesn't exist); 3 refused (system or
|
||||
// business config problem, the broker or notifier refused, the human proof
|
||||
// refused, a host already running); 4 usage.
|
||||
//
|
||||
// E2E6: `next` is an optional next-step line. run() prints it after the message as
|
||||
// `mosaic: next: ...`; the message itself stays as callers (the Console among them) match it.
|
||||
export class CliError extends Error {
|
||||
constructor(message, exitCode = 2) {
|
||||
constructor(message, exitCode = 2, { next } = {}) {
|
||||
super(message);
|
||||
this.name = "CliError";
|
||||
this.exitCode = exitCode;
|
||||
if (typeof next === "string" && next !== "") this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
// #1873 harness watch, slice W2 (reports/harness-watch-design.md section 1, "How often" and "Where
|
||||
// results are kept"): the bus host's timer over the W1 watch (packages/plugins/src/harness-watch.mjs).
|
||||
// It runs in the host process with the env sessions launch from.
|
||||
//
|
||||
// at start one full pass
|
||||
// every statMs (5 minutes) a stat pass: the stat key of every harness module the loader gives
|
||||
// (its optional watchKey({ env }), else its install roots), and a full pass when
|
||||
// the key is not the last full pass's, or when dayMs (a day) has gone by since it
|
||||
// halted/invalid while processing is halted or invalid (processingStatus), no pass at all; the
|
||||
// first tick after it clears runs a full pass
|
||||
//
|
||||
// A harness module may ask for a stat pass now (packages/plugins/src/watch-request.mjs; claude-code's
|
||||
// preflight asks when it has no verdict for its binary): it runs at once, or right after the pass
|
||||
// running, and runs a full pass only when the key changed, as a tick does.
|
||||
//
|
||||
// One pass at a time: a tick that comes while a pass runs does nothing. A pass that throws is
|
||||
// logged, with no path in the line, and the timer goes on; its key is not kept, so the next tick
|
||||
// runs a full pass again. stop() clears the timer, aborts the pass running (the watch aborts the
|
||||
// inspect it is in, which stops what it started) and waits for it; nothing is written after.
|
||||
//
|
||||
// The host is the one writer of two files under <org dataRoot>/state/:
|
||||
// harness-watch.json { version: 1, checkedAt, harnesses: [record], refused, gone }: the current
|
||||
// record per harness, as the watch returns it but without surfaces (their
|
||||
// store is slice W6). Written whole to a temp file in the same directory, then
|
||||
// renamed over the last one, 0600.
|
||||
// harness-watch.jsonl one line per transition the watch reports, appended, never rewritten.
|
||||
// The watch redacts its records (no host path, no env value). Both files are checked again before
|
||||
// they are written: a line that would hold the scratch directory, the home directory, the data
|
||||
// root or an env value of six characters or more is not written, and the pass is logged as failed.
|
||||
// The timer adds no model call and no network call: the stat pass reads file metadata only, and a
|
||||
// full pass is the watch's (its modules run their binaries with no network where the host allows).
|
||||
import { appendFileSync, chmodSync, closeSync, fsyncSync, mkdirSync, mkdtempSync, openSync, readFileSync, renameSync, rmSync, writeSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { loadInstalled } from "../../plugins/src/loader.mjs";
|
||||
import { watchHarnesses, watchStateFile } from "../../plugins/src/harness-watch.mjs";
|
||||
import { statKey } from "../../plugins/src/stat-key.mjs";
|
||||
import { onWatchRequest } from "../../plugins/src/watch-request.mjs";
|
||||
import { processingStatus } from "../../processing-control/src/state.mjs";
|
||||
|
||||
export const STAT_MS = 5 * 60 * 1000;
|
||||
export const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
export const WATCH_VERSION = 1;
|
||||
// The bus's plugins verb reads this file (readWatchView in the same module as watchStateFile).
|
||||
export const watchFile = watchStateFile;
|
||||
export const watchLog = (dataRoot) => join(dataRoot, "state", "harness-watch.jsonl");
|
||||
const VALUE_MIN = 6;
|
||||
|
||||
const realClock = Object.freeze({
|
||||
now: () => Date.now(),
|
||||
setInterval: (fn, ms) => setInterval(fn, ms),
|
||||
clearInterval: (t) => clearInterval(t),
|
||||
});
|
||||
|
||||
// The stat key of every harness the loader gives: per module, [name, its key]. A module's key is
|
||||
// its watchKey({ env }) when it has one, else the stat key of its installRoots({ env }), else null.
|
||||
// A module that comes or goes changes the key too. The key holds paths; it is compared in memory
|
||||
// only.
|
||||
export async function harnessKeys({ dataRoot, env }) {
|
||||
const loaded = await loadInstalled("harness", { dataRoot });
|
||||
const out = [];
|
||||
for (const { name, module } of loaded.modules) {
|
||||
let key = null;
|
||||
try {
|
||||
if (typeof module.watchKey === "function") key = module.watchKey({ env });
|
||||
else if (typeof module.installRoots === "function") key = statKey(module.installRoots({ env }));
|
||||
} catch (e) {
|
||||
key = ["error", typeof e?.code === "string" ? e.code : "threw"];
|
||||
}
|
||||
out.push([name, typeof key === "string" || Array.isArray(key) ? key : null]);
|
||||
}
|
||||
out.push(["refused", loaded.refused.map((r) => r?.name ?? null)]);
|
||||
return JSON.stringify(out);
|
||||
}
|
||||
|
||||
// Starts the timer and its first pass. dataRoot is the organization's: the state files go under it.
|
||||
// pluginRoot (W3) is where the harness plugins load from: the instance root when there is one, as
|
||||
// the launcher loads them (launcher.mjs, system.instanceRoot ?? dataRoot); it defaults to dataRoot. Returns { stop, settled }: stop() ends it (a promise), and
|
||||
// settled() is the pass running, or a resolved promise, for tests and for stop.
|
||||
export function startHarnessWatch({
|
||||
dataRoot,
|
||||
pluginRoot = dataRoot,
|
||||
env = process.env,
|
||||
log = () => {},
|
||||
clock = realClock,
|
||||
processing = () => processingStatus(dataRoot),
|
||||
watch = watchHarnesses,
|
||||
keys = () => harnessKeys({ dataRoot: pluginRoot, env }),
|
||||
statMs = STAT_MS,
|
||||
dayMs = DAY_MS,
|
||||
timeoutMs = undefined,
|
||||
scratchRoot = () => tmpdir(),
|
||||
requests = onWatchRequest,
|
||||
} = {}) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot) throw new TypeError("startHarnessWatch needs the organization's dataRoot");
|
||||
if (typeof pluginRoot !== "string" || !pluginRoot) throw new TypeError("startHarnessWatch needs a plugin root");
|
||||
const control = new AbortController();
|
||||
let stopped = false;
|
||||
let running = null;
|
||||
let held = false;
|
||||
let again = false;
|
||||
let needFull = true;
|
||||
let lastKey = null;
|
||||
let lastFull = null;
|
||||
let last = readRecords(dataRoot);
|
||||
|
||||
async function pass() {
|
||||
let status;
|
||||
try { status = processing(); } catch { status = { invalid: true }; }
|
||||
if (status?.halted || status?.invalid) {
|
||||
if (!held) log("harness watch: processing is halted or invalid, so passes wait for it");
|
||||
held = true;
|
||||
return;
|
||||
}
|
||||
if (held) { held = false; needFull = true; }
|
||||
const key = await keys();
|
||||
if (stopped) return;
|
||||
const now = clock.now();
|
||||
if (!needFull && key === lastKey && lastFull !== null && now - lastFull < dayMs) return;
|
||||
const scratch = mkdtempSync(join(scratchRoot(), "mosaic-watch-"));
|
||||
try {
|
||||
const result = await watch({ last, env, scratch, dataRoot: pluginRoot, now: () => new Date(clock.now()), signal: control.signal, ...(timeoutMs ? { timeoutMs } : {}) });
|
||||
if (stopped || result?.stopped) return;
|
||||
store(dataRoot, result, { env, scratch, pluginRoot, at: new Date(now).toISOString() });
|
||||
last = result.records;
|
||||
lastKey = key;
|
||||
lastFull = now;
|
||||
needFull = false;
|
||||
} finally {
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function tick() {
|
||||
if (stopped || running) return;
|
||||
running = pass()
|
||||
.catch((e) => { if (!stopped) log(`harness watch: a pass failed (${failure(e)}); the timer goes on`); })
|
||||
.finally(() => {
|
||||
running = null;
|
||||
if (again && !stopped) { again = false; tick(); }
|
||||
});
|
||||
}
|
||||
|
||||
// A request runs a tick now, or once right after the pass running.
|
||||
function request() {
|
||||
if (stopped) return;
|
||||
if (running) again = true;
|
||||
else tick();
|
||||
}
|
||||
|
||||
const timer = clock.setInterval(tick, statMs);
|
||||
timer?.unref?.();
|
||||
const unlisten = typeof requests === "function" ? requests(request) : null;
|
||||
tick();
|
||||
|
||||
return Object.freeze({
|
||||
settled: () => running ?? Promise.resolve(),
|
||||
async stop() {
|
||||
if (!stopped) {
|
||||
stopped = true;
|
||||
clock.clearInterval(timer);
|
||||
if (typeof unlisten === "function") unlisten();
|
||||
control.abort();
|
||||
}
|
||||
await (running ?? Promise.resolve());
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// What failed, without the error's text, which can name a host path.
|
||||
const failure = (e) => (typeof e?.code === "string" && /^[A-Za-z0-9_-]{1,40}$/.test(e.code) ? e.code : e?.name === "LeakError" ? "a state line held a host path or an env value" : "an error");
|
||||
|
||||
// The records of the last harness-watch.json, or none when it is missing or not this shape.
|
||||
function readRecords(dataRoot) {
|
||||
try {
|
||||
const doc = JSON.parse(readFileSync(watchFile(dataRoot), "utf8"));
|
||||
return doc?.version === WATCH_VERSION && Array.isArray(doc.harnesses) ? doc.harnesses.filter((r) => r && typeof r === "object" && typeof r.harness === "string") : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function store(dataRoot, result, { env, scratch, pluginRoot = dataRoot, at }) {
|
||||
const records = (Array.isArray(result?.records) ? result.records : []).map(({ surfaces, ...rest }) => rest);
|
||||
const doc = { version: WATCH_VERSION, checkedAt: at, harnesses: records, refused: Array.isArray(result?.refused) ? result.refused : [], gone: Array.isArray(result?.gone) ? result.gone : [] };
|
||||
const transitions = Array.isArray(result?.transitions) ? result.transitions : [];
|
||||
const leaks = leakChecker({ env, scratch, dataRoot, pluginRoot });
|
||||
if (leaks(doc) || transitions.some(leaks)) throw Object.assign(new Error("leak"), { name: "LeakError" });
|
||||
const text = `${JSON.stringify(doc, null, 2)}\n`;
|
||||
const lines = transitions.map((t) => `${JSON.stringify(t)}\n`).join("");
|
||||
const dir = join(dataRoot, "state");
|
||||
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
const file = watchFile(dataRoot);
|
||||
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
||||
try {
|
||||
const fd = openSync(tmp, "wx", 0o600);
|
||||
try { writeSync(fd, text); fsyncSync(fd); } finally { closeSync(fd); }
|
||||
renameSync(tmp, file);
|
||||
} catch (e) {
|
||||
rmSync(tmp, { force: true });
|
||||
throw e;
|
||||
}
|
||||
chmodSync(file, 0o600);
|
||||
if (lines) appendFileSync(watchLog(dataRoot), lines, { mode: 0o600 });
|
||||
}
|
||||
|
||||
// Whether a value holds a path the host knows (the scratch directory, the home directory, the data
|
||||
// root) or an env value of VALUE_MIN characters or more, the host's or the launch env's, in any of
|
||||
// its strings. Names the watch sets itself are not text and are skipped: a harness or module name
|
||||
// (an env value may name the harness), a lookup step, a state, a fit, a via, a time or a digest.
|
||||
const NOT_TEXT = new Set(["harness", "gone", "name", "kept", "source", "code", "state", "fit", "via", "at", "checkedAt", "changedAt", "digest", "lastDigest"]);
|
||||
export function leakChecker({ env, scratch, dataRoot, pluginRoot = null }) {
|
||||
const needles = new Set();
|
||||
const add = (v) => { if (typeof v === "string" && v.length >= VALUE_MIN) needles.add(v); };
|
||||
for (const p of [scratch, dataRoot, pluginRoot, homedir()]) add(p);
|
||||
for (const source of [env ?? {}, process.env]) for (const v of Object.values(source)) add(v);
|
||||
const holds = (text) => { for (const n of needles) if (text.includes(n)) return true; return false; };
|
||||
const walk = (v, key) => {
|
||||
if (typeof v === "string") return !NOT_TEXT.has(key) && holds(v);
|
||||
if (Array.isArray(v)) return v.some((x) => walk(x, key));
|
||||
if (v && typeof v === "object") return Object.entries(v).some(([k, x]) => walk(x, k));
|
||||
return false;
|
||||
};
|
||||
return (value) => walk(value, null);
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { CliError } from "./errors.mjs";
|
||||
import { cmdlineOf, startTimeOf } from "../../seat/src/proc.mjs";
|
||||
import { startHarnessWatch } from "./harness-watch-host.mjs";
|
||||
|
||||
const BROKER = fileURLToPath(new URL("../../bus/src/process.mjs", import.meta.url));
|
||||
const NOTIFIER = fileURLToPath(new URL("./notifier-process.mjs", import.meta.url));
|
||||
@@ -76,6 +77,11 @@ const same = (a, b) => a?.pid === b?.pid && a?.startTime === b?.startTime && a?.
|
||||
// tried again, like one that goes before the check. `found` runs once the
|
||||
// lock is found and before it is read, `between` after the stale lock is
|
||||
// read; both are for the tests. Exported for the tests.
|
||||
// E2E6 fix 1: every stale-lock refusal names the verb that clears a host that died.
|
||||
export const recoverNext = (business) =>
|
||||
typeof business === "string" && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(business)
|
||||
? `if the bus host died, clear it with mosaic bus recover ${business}; recover refuses while the host still runs`
|
||||
: "if the bus host died, clear it with mosaic bus recover <org>; recover refuses while the host still runs";
|
||||
export function claimHostLock(dataRoot, state, { found = () => {}, between = () => {}, refuseStale = false } = {}) {
|
||||
mkdirSync(hostDir(dataRoot), { recursive: true, mode: 0o700 });
|
||||
const file = hostLock(dataRoot);
|
||||
@@ -94,7 +100,8 @@ export function claimHostLock(dataRoot, state, { found = () => {}, between = ()
|
||||
const prior = readState(file, found);
|
||||
if (!prior) continue;
|
||||
if (prior.live) throw new CliError(`a bus host already runs for ${prior.business} (pid ${prior.pid})`, 3);
|
||||
if (refuseStale) throw new CliError("stale bus host lock", 3);
|
||||
// E2E6 fix 1: the message stays (the Console matches it); the next line names the clearing verb.
|
||||
if (refuseStale) throw new CliError("stale bus host lock", 3, { next: recoverNext(prior.business) });
|
||||
between();
|
||||
try {
|
||||
renameSync(file, aside);
|
||||
@@ -169,7 +176,10 @@ export function watchChildren(children, onDeath) {
|
||||
// {binding, base?, pollMs?}; base and pollMs exist for the tests, and the
|
||||
// command line never sets them, nor requestTimeoutMs. Resolves once both
|
||||
// children are up.
|
||||
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`) }) {
|
||||
// harnessWatch (#1873 W2): { env, ...options } starts the harness watch's timer in this process
|
||||
// once the host is up (harness-watch-host.mjs), with env the environment sessions launch from; the
|
||||
// host's close stops it first. null (the default) starts none.
|
||||
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`), harnessWatch = null }) {
|
||||
const dataRoot = boot.dataRoot;
|
||||
// A host started before host.lock existed holds only host.json.
|
||||
const prior = readHostState(dataRoot);
|
||||
@@ -366,6 +376,13 @@ export async function startHost({ boot, business, notifier = null, options = nul
|
||||
log(`${name} exited (${signal ?? code}); stopping the host`);
|
||||
close(1);
|
||||
});
|
||||
// #1873 W2: the harness watch's timer, stopped before anything else when the host closes.
|
||||
if (harnessWatch) {
|
||||
// W3: plugins load from the instance root when there is one, as the launcher loads them; the
|
||||
// state files stay under the organization's dataRoot.
|
||||
const watch = startHarnessWatch({ log, pluginRoot: boot.instanceRoot ?? dataRoot, ...harnessWatch, dataRoot });
|
||||
hooks.unshift(() => watch.stop());
|
||||
}
|
||||
|
||||
// #1873 CB3: the copies the broker's boot proved live, names only; anything malformed counts as none.
|
||||
const copyBound = {};
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
// instance.request until the request closes or --wait runs out (D1.1 item 5).
|
||||
import { readFileSync } from "node:fs";
|
||||
import { CliError } from "./errors.mjs";
|
||||
import { copyBindingStatus } from "../../organization/src/copy-binding-status.mjs";
|
||||
import { SERVICES } from "../../organization/src/vocabulary.mjs";
|
||||
|
||||
export const INSTANCES_USAGE = "mosaic business instances BUSINESS [--json] [--tester] | " +
|
||||
"business instance add BUSINESS INSTANCE --definition DEF [--vars FILE] [--launch] [--bot BOT --bot-id N] --revision REV [--tester] | " +
|
||||
@@ -219,6 +221,16 @@ export function formatInstances(view) {
|
||||
const vars = Object.keys(entry.vars).length ? ` vars ${JSON.stringify(entry.vars)}` : "";
|
||||
lines.push(` ${name}: ${entry.definition}${marks.length ? ` (${marks.join(", ")})` : ""}${vars}`);
|
||||
}
|
||||
// #1873 CB5: each copy that needs a credential binding, with the bus's status code and its fixed
|
||||
// next step (organization copy-binding-status.mjs). A code the table doesn't know prints its own
|
||||
// fixed fallback; nothing else from the answer is printed for it.
|
||||
for (const [name, copy] of Object.entries(view.copies ?? {})) {
|
||||
if (!copy?.binding) continue;
|
||||
const { status, next } = copyBindingStatus(copy.binding.status);
|
||||
// The services the copy lacks, as registered service labels only; nothing else the bus sent.
|
||||
const missing = copy.state === "unbound" && Array.isArray(copy.missing) ? SERVICES.filter((s) => copy.missing.includes(s)) : [];
|
||||
lines.push(` ${name} (copy): ${status}${missing.length ? ` (missing ${missing.join(", ")})` : ""}. ${next}`);
|
||||
}
|
||||
const definitions = Object.entries(view.definitions);
|
||||
const accepted = definitions.filter(([, d]) => d.accepted).map(([name]) => name);
|
||||
const bound = definitions.filter(([, d]) => d.services.length).map(([name, d]) => `${name} (${d.services.join(", ")})`);
|
||||
|
||||
@@ -215,12 +215,19 @@ const relayed = (failed) => failed && (typeof failed.message === "string" ? { ..
|
||||
// and names the module rather than escaping the host. What it threw is named by its system code
|
||||
// or its class, never its message, which the host didn't write (#1873).
|
||||
function call(name, what, fn) {
|
||||
try {
|
||||
return fn();
|
||||
} catch (e) {
|
||||
const failed = (e) => {
|
||||
const thrown = quietMessage(e, "a host file") ?? `threw ${/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(String(e?.name)) ? e.name : "a value"} without a system code`;
|
||||
throw new Refusal("harness-failed", `harness ${name} ${what}: ${thrown}`);
|
||||
};
|
||||
let out;
|
||||
try {
|
||||
out = fn();
|
||||
} catch (e) {
|
||||
failed(e);
|
||||
}
|
||||
// #1873 pi-unpin re-check 7: a module may answer with a promise (pi's preflight runs its fit
|
||||
// check as child processes). The caller awaits it; a rejection is refused as a throw is.
|
||||
return typeof out?.then === "function" ? Promise.resolve(out).then(undefined, failed) : out;
|
||||
}
|
||||
|
||||
// host: startHost()'s handle. tracker: true when the broker has task verbs for
|
||||
@@ -302,6 +309,11 @@ export function createLauncher({
|
||||
let ticking = false;
|
||||
const stopping = new Set();
|
||||
const refusedThreads = new Map();
|
||||
// #1873 chat-fail F2: the refusal and message each thread session's plain line was asked for.
|
||||
const noticedThreads = new Map();
|
||||
// #1873 F2: the last refusal seen per thread start, and the last failed post of its line.
|
||||
const refusalSeen = new Map();
|
||||
const noticeFailed = new Map();
|
||||
const startedFor = new Map();
|
||||
// Row 271 (264d): each clear's stop time (by op), and each clear refusal logged once (by op and code).
|
||||
const clearStops = new Map();
|
||||
@@ -449,14 +461,11 @@ export function createLauncher({
|
||||
for (const m of [primary, fallback].filter(Boolean)) {
|
||||
if (!on.includes(m.harness)) throw new Refusal("harness-off", `harness ${m.harness} isn't switched on in plugins.harness`);
|
||||
}
|
||||
// One harness through a fresh loader, so a refusal never leaves an earlier load's module in use.
|
||||
const harnessOf = async (m) => {
|
||||
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log: (entry) => record({ ...entry, instance }) });
|
||||
const { refused } = await loader.load({ harness: [m.harness] });
|
||||
const { refused, module } = await freshHarness(m, (entry) => record({ ...entry, instance }));
|
||||
// #1873: a loader message can quote the module's path or its load error; only a
|
||||
// missing-export one, which names the export, is relayed. Any other refusal names its code.
|
||||
if (refused.length) throw new Refusal("harness-refused", `harness ${m.harness}: ${refused.map((r) => r.code === "missing-export" ? `${r.code}: ${relay(r.message)}` : String(r.code)).join("; ")}`);
|
||||
const module = loader.modules("harness")[0];
|
||||
const levels = call(m.harness, "levels", () => module.levels());
|
||||
if (!levels.includes(m.thinking)) throw new Refusal("thinking-unsupported", `harness ${m.harness} takes thinking ${levels.join(", ")}, not ${m.thinking}`);
|
||||
return module;
|
||||
@@ -551,7 +560,7 @@ export function createLauncher({
|
||||
const pickRoute = async (proxyOf) => {
|
||||
let sessionProxy = await proxyOf(primary);
|
||||
// #1873: a preflight's message is the module's own text, relayed with host paths replaced.
|
||||
const failed = relayed(call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
const failed = relayed(await call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
if (failed) {
|
||||
if (!fallback) throw new Refusal(failed.code, failed.message);
|
||||
// The fallback only on a failed primary preflight, and only at launch (row 65). Any refusal of
|
||||
@@ -561,7 +570,7 @@ export function createLauncher({
|
||||
throw e;
|
||||
});
|
||||
sessionProxy = await proxyOf(fallback);
|
||||
const also = relayed(call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
const also = relayed(await call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
if (also) throw new Refusal(also.code, `fallback ${fallback.harness}: ${also.message} (primary ${primary.harness}: ${failed.message})`);
|
||||
[model, harness, route, routeReason] = [fallback, second, "fallback", failed.code];
|
||||
}
|
||||
@@ -735,7 +744,7 @@ export function createLauncher({
|
||||
if (store) compactions(store, model.harness, harness, { instance, thread, session, run });
|
||||
const toolSocket = join(runDir, "tools.sock");
|
||||
const turnMarker = join(runDir, "turn.done");
|
||||
const version = harnessVersion !== undefined ? harnessVersion : call(model.harness, "version", () => harness.version({ env }));
|
||||
const version = harnessVersion !== undefined ? harnessVersion : await call(model.harness, "version", () => harness.version({ env }));
|
||||
// Row 137 (#1631): a role and thread holding an approved, unspent merge decision get land_work.
|
||||
// The broker checks the decision again on every land; this only decides what the bundle shows.
|
||||
// Row 156 (#1651): a non-thread instance's land and landing come from one landHolders read, so a
|
||||
@@ -896,7 +905,13 @@ export function createLauncher({
|
||||
}
|
||||
children.set(run, exited);
|
||||
const outputWatch = admittedProxy ? watchModelOutput({ runDir, armAtFirstTurn: true, keyId: modelKeyId, seconds: system.proxy.firstOutputSeconds ?? 120,
|
||||
stop: () => host.op({ op: 'modelStall', business: b.id, role: instance, run, keyId: modelKeyId }), receipt: record }) : null;
|
||||
stop: async () => {
|
||||
const out = await host.op({ op: 'modelStall', business: b.id, role: instance, run, keyId: modelKeyId });
|
||||
// #1873 chat-fail F2: the message the stopped turn was on gets one plain line, as a failed turn's.
|
||||
try { await host.op({ op: 'failureNotice', record: { business: b.id, run, code: 'proxy-stall' } }); }
|
||||
catch (err) { log(`run ${run}: failure line refused: ${err.code ?? 'failed'}`); }
|
||||
return out;
|
||||
}, receipt: record }) : null;
|
||||
exited.then(async ({ code, signal }) => {
|
||||
outputWatch?.cancel();
|
||||
let receipt;
|
||||
@@ -1066,8 +1081,30 @@ export function createLauncher({
|
||||
await start({ instance: t.instance, launchedBy: b.human, ...where, legacy: t.legacy, legacyThread: legacyThreads.has(t.thread), ...(await check(t.instance, where)) });
|
||||
startedFor.set(key, signature);
|
||||
refusedThreads.delete(key);
|
||||
noticedThreads.delete(key);
|
||||
refusalSeen.delete(key);
|
||||
noticeFailed.delete(key);
|
||||
} catch (e) {
|
||||
const code = e instanceof Refusal ? e.code : "launch-failed";
|
||||
// #1873 chat-fail F2: the person who wrote gets one plain line per message the refusal
|
||||
// leaves unanswered. Asked once per code and message; the bus keeps it to one per message.
|
||||
// launch-failed can be a lost race that the next try wins, so it gets a line only when
|
||||
// the same message is refused with it twice in a row. A post that fails is tried again
|
||||
// on the next tick, and its failure is logged once.
|
||||
const noticed = `${code}|${signature}`;
|
||||
const repeated = refusalSeen.get(key) === noticed;
|
||||
refusalSeen.set(key, noticed);
|
||||
if (noticedThreads.get(key) !== noticed && (code !== "launch-failed" || repeated)) {
|
||||
try {
|
||||
await host.op({ op: "failureNotice", record: { business: b.id, thread: t.thread, session: t.session, code } });
|
||||
noticedThreads.set(key, noticed);
|
||||
noticeFailed.delete(key);
|
||||
} catch (err) {
|
||||
const failed = `${noticed}|${err.code ?? "failed"}`;
|
||||
if (noticeFailed.get(key) !== failed) log(`thread ${t.thread} session ${t.session}: failure line refused: ${err.code ?? "failed"}`);
|
||||
noticeFailed.set(key, failed);
|
||||
}
|
||||
}
|
||||
// Logged once per code, not once per tick.
|
||||
if (refusedThreads.get(key) === code) return;
|
||||
refusedThreads.set(key, code);
|
||||
@@ -1637,6 +1674,38 @@ export function createLauncher({
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
// One harness ({ harness: name }) through a fresh loader, so a refusal never leaves an earlier
|
||||
// load's module in use: { refused, module }, module null when refused. A launch (harnessOf) and
|
||||
// the start's fit warm-up below (#1873 re-check 8) both load through here.
|
||||
async function freshHarness(m, log) {
|
||||
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log });
|
||||
const { refused } = await loader.load({ harness: [m.harness] });
|
||||
return { refused, module: refused.length ? null : loader.modules("harness")[0] };
|
||||
}
|
||||
|
||||
// #1873 pi-unpin re-check 8: each switched-on harness module with ready() (pi's fit check) starts
|
||||
// it here, in the background, so the first launch joins the check in flight instead of starting
|
||||
// it. Nothing waits on it: a launch still awaits its own preflight, which shares this check or
|
||||
// reads its pass. A warm-up that fails or throws is dropped; it caches nothing, and the launch's
|
||||
// preflight runs the check again. Re-check 9: a harness plugin the warm-up can't load leaves one
|
||||
// launch log entry, by its code only (a launch that loads it records the message).
|
||||
function warmHarnesses() {
|
||||
const on = Array.isArray(b.plugins?.harness) ? b.plugins.harness : [];
|
||||
for (const name of on) {
|
||||
(async () => {
|
||||
let logged = false;
|
||||
const refusal = (entry) => {
|
||||
if (logged || entry?.event !== "plugin-refused") return;
|
||||
logged = true;
|
||||
record({ event: "plugin-refused", kind: entry.kind, name: entry.name, code: entry.code, during: "warm-up" });
|
||||
};
|
||||
const { module } = await freshHarness({ harness: name }, refusal);
|
||||
if (typeof module?.ready === "function") await module.ready({ env });
|
||||
})().catch(() => {});
|
||||
}
|
||||
}
|
||||
if (launches) warmHarnesses();
|
||||
|
||||
host.beforeClose(close);
|
||||
return { business: b, registry, listen, launchPm, fromSession, tick, close };
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { resolveOrg, withOrgLifecycle } from '../../organization/src/org-registry.mjs';
|
||||
import { orgPaths } from './config.mjs';
|
||||
import { cmdlineOf, hostFile, hostLock, hostStatus, readHostState, startTimeOf, stopHost } from './host.mjs';
|
||||
import { cmdlineOf, hostFile, hostLock, hostStatus, readHostState, recoverNext, startTimeOf, stopHost } from './host.mjs';
|
||||
import { CliError } from './errors.mjs';
|
||||
import { readBootStatus } from './boot-status.mjs';
|
||||
|
||||
@@ -27,20 +27,22 @@ async function target(ctx, id, allowDeleting = false) {
|
||||
return { ...org, paths: orgPaths(org.root) };
|
||||
}
|
||||
function checkedState(org) {
|
||||
// E2E6 fix 1: each refusal keeps its message and adds the recover line as its next step.
|
||||
const stale = message => { throw new CliError(message, 3, { next: recoverNext(org.id) }); };
|
||||
const s = hostStatus(org.root);
|
||||
let lock = null;
|
||||
try { lock = JSON.parse(readFileSync(hostLock(org.root), 'utf8')); }
|
||||
catch (e) { if (e.code !== 'ENOENT') fail('bus host lock is unreadable'); }
|
||||
if (lock && (!Number.isSafeInteger(lock.pid) || typeof lock.startTime !== 'string' || startTimeOf(lock.pid) !== lock.startTime)) fail('stale bus host lock');
|
||||
if (s.host && (!s.host.live || s.host.business !== org.id)) fail('stale or mismatched bus host state');
|
||||
catch (e) { if (e.code !== 'ENOENT') stale('bus host lock is unreadable'); }
|
||||
if (lock && (!Number.isSafeInteger(lock.pid) || typeof lock.startTime !== 'string' || startTimeOf(lock.pid) !== lock.startTime)) stale('stale bus host lock');
|
||||
if (s.host && (!s.host.live || s.host.business !== org.id)) stale('stale or mismatched bus host state');
|
||||
if (s.host) {
|
||||
const args = cmdlineOf(s.host.pid) ?? [], i = args.indexOf(CLI);
|
||||
if (i < 0 || args[i + 1] !== 'bus' || args[i + 2] !== 'start' || args[i + 3] !== org.id) fail('bus host identity mismatch');
|
||||
if (i < 0 || args[i + 1] !== 'bus' || args[i + 2] !== 'start' || args[i + 3] !== org.id) stale('bus host identity mismatch');
|
||||
}
|
||||
if (lock && (!s.host || lock.pid !== s.host.pid || lock.business !== org.id)) fail('bus host lock does not match host state');
|
||||
if (s.writerLock && !s.writerLock.live) fail('stale bus writer lock');
|
||||
if (!s.host && s.writerLock) fail('bus writer lock has no matching host');
|
||||
if (s.host && !lock) fail('bus host lock is missing');
|
||||
if (lock && (!s.host || lock.pid !== s.host.pid || lock.business !== org.id)) stale('bus host lock does not match host state');
|
||||
if (s.writerLock && !s.writerLock.live) stale('stale bus writer lock');
|
||||
if (!s.host && s.writerLock) stale('bus writer lock has no matching host');
|
||||
if (s.host && !lock) stale('bus host lock is missing');
|
||||
return s;
|
||||
}
|
||||
export function probeBusSocket(path, { signal, deadline } = {}) {
|
||||
@@ -77,6 +79,12 @@ export async function busStop(ctx, id) {
|
||||
checkedState(org);
|
||||
return stopHost(org.root, { expectedBusiness: id });
|
||||
}
|
||||
// #1873 CB4: the remote step archive and delete give each copy's grant: the organization's managed
|
||||
// Gitea adapter (its admin slot), the one the bus's own revoke uses. Loaded only when needed.
|
||||
export async function copyRemoteFor(ctx) {
|
||||
const { managedCopyRemote } = await import('../../bus/src/copy-revoke.mjs');
|
||||
return managedCopyRemote({ configDir: ctx.configDir, dataRoot: ctx.dataRoot });
|
||||
}
|
||||
// Deletion refuses stale or ambiguous host state and any listening socket.
|
||||
export async function busStopForDelete(ctx, id) {
|
||||
const org = await target(ctx, id, true);
|
||||
|
||||
Loaded 100 of 415 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user