Compare commits

..
Author SHA1 Message Date
jason.woltje 4b369ddbaf row 264a r5 (#1769): build notes name the op rename to legacy-3l and migration-3l by the step's target version (sage-2415) 2026-10-21 22:59:14 -05:00
jason.woltje 21ca39d7bb row 264a r5 (#1769): build notes for the 3k-3l step on row 236 (8f51900b5, sage-2412): the renames, the merged pins and lists, gitea-migration reading both steps, and the in-memory dry run on 236's schema; README op value legacy-3l 2026-10-21 22:57:59 -05:00
jason.woltje b7078fb12b row 264a r3 (#1769): build notes say the generation fence is equivalent to the holder_run check it replaced, and M1 shows the refusal is tested (duckworth-0108, sage-2270) 2026-10-21 22:56:54 -05:00
jason.woltje 6b9a8a39cf row 264a r2 (#1769): generation fence for thread runs, a retired generation's approval, delivery and read refuse and write nothing (sage-2257); #route refuses an unknown session, README default-op note, Row 155 comment reworded, mutants M1-M3 in the build notes (duckworth-0107) 2026-10-21 22:56:46 -05:00
jason.woltje 8962572b0c row 264a (#1769, T1 item 7): bus schema 3k to 3l, sessions below a thread
sessions and session_defaults are new and append-only. thread_claims, message_threads and
thread_opens are rebuilt in place with a session column, and thread_claims gains a generation
counted from claim rows. Each existing thread gets a default session with the thread's id; new
threads get a random one. Holder, per-run, release and delivery triggers key on the session,
and two id triggers keep session and thread ids apart (Duckworth checks 1-7, duckworth-0106).

The broker binds a thread run to a session (the default when the record names none), and
thread.open, thread.send and thread.messages take an optional session. New verbs session.list
and session.create (idempotent on op). Tests: sessions.test.mjs for T1 bus half, T2 and T7 with
a pinned 3k fixture; count, version and verb pins; hand-made test threads write the legacy
default session.

Built on row 236 (8f51900b5), which takes 3j-3k, so this step is 3k-3l by landing order
(row264 Q2, sage-2412). The op values are legacy-3l and migration-3l. gitea-migration.test reads
its store after both steps. Not run yet: every run waits for Sage's CLEAR.
2026-10-21 22:56:35 -05:00
jason.woltje 46c8990958 gizmoduck: row 264a bus design in the build notes: sessions and session_defaults, thread_claims, message_threads and thread_opens rebuilt with a session (legacy default id = thread id), generation, broker lookups by session, verbs, tests and pinned lists 2026-10-21 22:53:01 -05:00
jason.woltje baef2575fd gizmoduck: row 264 build notes record Sage's Q1-Q7 rulings (sage-2235, 5f5b51dd6) 2026-10-21 22:53:01 -05:00
jason.woltje 71a15fda48 gizmoduck: row 264 phase A build notes, code at da9555ca2, consumer audit first pass, five proposed slices and Q1-Q7 to Sage (sage-2233, #1769, Vikunja 463) 2026-10-21 22:53:01 -05:00
jason.woltje 8f51900b5c bus: row 236 r3, one N5 test per settle branch; README notes for a left admin token and a running executor (sage-2388, #1742) 2026-10-21 22:42:26 -05:00
jason.woltje 352eb2d00f bus: row 236 r3, a revoke during a settle whoami ends revoked in the 401 and token-unknown branches (Dewey N5, #1742) 2026-10-21 22:42:26 -05:00
jason.woltje 95ed0b4091 bus: row 236 r2, a revoke during step 7 or a settle ends revoked; sweep fire retry checks the user id (Dewey B1, B2, #1742) 2026-10-21 22:42:26 -05:00
jason.woltje 4da781922c bus: row 236 gap test takes any HTTP status in a grant.result (debug run 2, #1742) 2026-10-21 22:42:26 -05:00
jason.woltje acb2f86312 bus: row 236 tests for the static mutant gaps M8, M9, M16, M28, M29, M37 (#1742) 2026-10-21 22:42:26 -05:00
jason.woltje 09fb7c14d2 bus, cli: #436b Gitea grant path, schema 3j-3k (row 236, #1742)
Gitea grant executor (steps 0-8, settle, restart, revoke and fire, sweep,
status), the grants.* verbs and mosaic grants, the 3j-3k migration with
gitea_users and gitea_grants, and the section 9 tests with a fake Gitea and
a scratch gitea/gitea:1.27.1 X-G3 test. Build notes in
agents/megavolt/work/queue-236/BUILD.md.
2026-10-21 22:42:26 -05:00
134 changed files with 6057 additions and 4388 deletions

No files matched your search

-1
View File
@@ -22,7 +22,6 @@ RUN npm ci --ignore-scripts
# runtime adapters.
COPY contracts /opt/mosaic/contracts
COPY src /opt/mosaic/src
COPY packages/processing-control/src/state.mjs packages/processing-control/src/entrypoint.mjs /opt/mosaic/processing-control/
COPY adapters /opt/mosaic/adapters
RUN chmod 0555 /opt/mosaic/contracts /opt/mosaic/contracts/* \
&& chmod 0555 /opt/mosaic/src /opt/mosaic/src/*.sh \
+257
View File
@@ -0,0 +1,257 @@
# Row 264 build notes (Gizmoduck)
T1 item 7: independent sessions, compaction counts and clear context. Gitea #1769, Vikunja 463
(design) and 466 (Claude compaction format, T3's dependency, linked and not built here). Brief:
docs/plans/2026-10-21_t1-sessions.md, "T1 item 7: independent sessions, compaction counts and clear
context" (blob ff0be0c79). Reviewer Duckworth, gate owner Sage. Branch gizmoduck/row264 from refactor
da9555ca2. MVP due 10-27.
The brief pins its code citations at cca206cdc. `git diff --stat cca206cdc da9555ca2` over every cited
source file is empty, so F1-F11 still hold line for line. Only docs/prd/mosaic-stack.md moved.
## Phase A: what the code has today
- Bus. `threads` binds one `instance` per thread (schema.sql:240-250). `thread_claims` keys on
`thread` alone (:251-261), and `message_threads` maps a message to a thread (:262-265). Schema is
3j (store.mjs:17). The thread tables first appear in migrations/3d-3e.sql.
- Launcher. `check()` refuses a second run for the same instance and thread
(launcher.mjs:233-240, `registry.running(b.id, instance, thread)`).
- Seat. `threadStore()` makes one `session/` and one `claude-projects/` per thread
(session.mjs:51-74). The registry's running key is business, instance and thread (:215-218).
- Runner. `stop()` sends TERM to the child group, then KILL after 5 s (runner.mjs:402-414). It has
no drain, no idle acknowledgement and no dequeue fence. D7 step 2 needs all three.
## Consumer audit (D9), first pass
`git grep -F` over packages/*/src/* and adapters/* at da9555ca2:
| Name | Files and hits |
| --- | --- |
| `thread_claims` | broker.mjs 12, store.mjs 1 |
| `message_threads` | broker.mjs 9 |
| `thread_opens` | broker.mjs 4, store.mjs 1 |
| `threadStore` | launcher.mjs 4, sandbox.mjs 7, session.mjs 3 |
| `running(` | launcher.mjs 5, session.mjs 1 |
Still to read: the broker holder, agents, end, usage, reply correlation and pending-approval paths
named in D9, the two adapters (F5), both harness plugins, the conversation reader (F8) and the
Console. Each gets a file:line row here before the first code commit.
## Proposed slices
One seat can't build D1-D9 in six days and keep every slice reviewable. I propose five candidates
under row 264, each with its own manifest and Duckworth round:
1. 264a bus. Session tables, claim key `(organization, thread, instance, session)` with a
generation, the D3 default session, the D9 bus mapping, `session.list` and `session.create`, and an
optional session selector on the thread verbs. Tests: T1 bus half, T2 claims, T7 legacy clients and
the rollback refusal.
2. 264b seat and launcher. Per-session native store and workspace, the single-writer fence, the
registry key with session, sandbox mounts of the selected store only, and pinned native ids in both
adapters. Tests: T1 run half, T2 workspace and path mutants.
3. 264c compaction count. Host collector, exact for Pi. Claude shows `unknown` with the 466 reason
until that format is pinned. Tests: T3 Pi half.
4. 264d clear. The D7 journal, the runner drain fence and idle acknowledgement, stop proof, then
retirement and successor in one transaction. Tests: T4, T6.
5. 264e surface. `mosaic session` CLI and the Console chooser, count and clear confirmation. Tests:
T5 for the legacy single human, T7 Console half.
## Phase A rulings (sage-2235)
Q1-Q7 went to Sage in gizmoduck-0154. Sage's answers are in agents/sage/work/queue-264/RULINGS.md at
5f5b51dd6. In short:
- Q1. Five candidates in the order a, b, c, d, e, each with its own manifest, Duckworth round and
landing. Row 264 carries 264a. Sage adds rows for 264b-264e. All five are MVP.
- Q2. 264a builds as 3j-3k.sql. If 236 or 206 lands first, I rebase onto a new branch name, take the
next label, and rerun the whole bus suite, the schema.test.mjs counts and the T46 lists.
- Q3. PRD 1.13 has landed, and REQ-ROLE-2 (mosaic-stack.md:323-329) already carries the session
text. Build to it.
- Q4. Row 194's lifecycle path owns the stop. honker/row194-b5 dfabbbe8a is read-only reference.
264d rebases onto 194's landing. The runner's dequeue fence and idle acknowledgement are new code
and a named review item. No 194 code gets copied.
- Q5. One shared admission hook for every session start, using today's limits and revocation. Of
191 and 264b, whichever lands second wires 191's checks in. No setting turns it off, and a check
that's configured but unavailable fails closed.
- Q6. 264c lands with Pi exact and Claude `unknown` with the 466 reason. The Claude half of T3 stays
open on row 264 and Vikunja 466.
- Q7. Legacy single human only. Collaborator and share paths refuse. A4 and the full matrix go to
whichever of 264 and 193 lands second.
## 264a design: the bus
Line numbers are at da9555ca2 unless they say otherwise.
### Schema 3j-3k
- `sessions`, new and immutable: `id`, `at`, `business`, `thread`, `instance`, `owner`, `name`, `op`
(the creation operation key) and `predecessor` (null until 264d's clear). UNIQUE `(thread, name)`
and `(thread, op)`, so a repeated create with the same key finds the first row. Guard triggers
check that the business, instance and owner match the thread's, and that a predecessor sits in
the same thread.
- `session_defaults`, new and append-only: `thread`, `session`, `at`, `by`. The latest row per
thread is that thread's default (D3). A trigger checks that the session is in the thread.
- `thread_claims`, `message_threads` and `thread_opens` are rebuilt, each with a `session` column,
and `thread_claims` also gets `generation`. Rows are copied with their seq values and every old
column as it was, and the sequence carries over, as 3h-3i.sql did for `usage_records`. That is a
named review item: the copy adds two derived columns and changes nothing else.
- Each existing thread gets one legacy default session with **id equal to the thread id**, the
name `default`, op `legacy-3k`, and the thread's own `at` and owner. Every copied row names that
session. This is D9's immutable mapping. 264b binds it to the existing native store at
`threads/<business>/<thread>/session/`. A new thread's default session gets a random id.
- `generation` is the claim's number within its session, counting from 1. A release or revoke
carries its claim's number. On copy, it's the count of claim rows in the thread up to that row.
- Triggers keyed on the thread now key on the session: one holder, release only while held, only the
holder releases, and a generation check. These two keep their thread meaning: one held session per
run per business, and the business check. New checks: the claim's, message's and open's session
is in the named thread. `delivery_same_thread` becomes `delivery_same_session`, which sends a
threaded message only to its own session's holder.
- A 3j binary refuses a 3k store with `schema-mismatch` (store.mjs:181-186), which covers D9's
rollback refusal. T7 tests it.
### Broker
- Run binding. `bindLaunch` takes an optional `session`. When it's missing, the run gets the
thread's default session, which is the D3 path for today's launcher. The binding refuses a
session that isn't in its thread. The run's `s.session` then drives `role.claim`, `#own`,
`message.receive` and the reply routing.
- Lookups. `#threadHolder(thread)` becomes `#sessionHolder(session)`, which `#recipient`,
`#routeThread`, `#threadHolders`, `#claimEnd`, `threadIdle` and run end all use.
`#threadFor` returns a thread and its session: a thread run's own, or the session of the nearest
threaded message up the reply chain. `#specThread` uses the opened thread's default session.
- Launcher view. `threadsState` and `threadIdle` keep their row shape, one row per thread computed
on its default session. Until 264b, the launcher starts only default sessions. Messages to another
session wait, and nothing reaches the default session's run.
- Verbs:
- The thread verbs take an optional `session`, and leaving it out uses the default (D3).
`thread.create` also makes the default session. `thread.messages` without a session reads the
default session.
- `session.list {thread}` and `session.create {thread, name, op}` (D8: metadata only). The build
dropped `session.get`, `session.open` and `session.send`. `thread.open`, `thread.send` and
`thread.messages` take an optional `session`, which does the same job with one owner check and
one code path.
- All of them are human or tester only, through `#human`/`#thread`, the same owner check as the
thread verbs (Q7: single human, no shares). They go into `TESTER_VERBS`.
- Other readers. `#grantNotice`, `#raisedThread` and the decision-close path (:736, :1500, :2941)
read the holder run's claim row. The rebuilt `thread_claims` keeps that query working, and the
session comes along from the same row.
### Tests and lists
- New file `packages/bus/tests/sessions.test.mjs`:
- T1 bus half: two sessions in one thread, distinct histories, receive and reply isolation, and a
non-thread role refused.
- T2: one holder per session, generation fencing, sibling delivery refused by the trigger.
- T7: legacy clients, the 3j-3k migration from a fixture (default session id = thread id, rows
copied with seq), and the rollback refusal.
- A new `fixtures/schema-3j.sql`.
- Update the schema.test.mjs pins (:519-525), the `'3j'` version pins in the migration tests,
`COUNTED` in scripts/stack-release.sh:404, and the T46 lists in
packages/cli/tests/stack-release.test.mjs:860-934. Also `timedTables` (store.mjs:65-79) and the
broker clock-seed list (:325-336).
### Duckworth's design checks (duckworth-0106)
Duckworth had no objection to the in-place rebuild and asked for seven checks. Where each one is:
1. The cross-table trigger. 3j-3k.sql drops `delivery_same_thread` before any rename and creates
`delivery_same_session` after the new tables.
2. Index names. The step drops `thread_claims_thread` and `message_threads_thread` before the
renames, so the new tables can reuse the names.
3. Per-table triggers. The step drops each guard and append-only trigger and creates it again with
text that matches schema.sql. The digest check at store.mjs:231 enforces that, and the T7
`sqlite_master` comparison shows it.
4. Generation counts claim rows only. T7's fixture is Duckworth's: claim, release, claim, revoke,
giving 1, 1, 2, 2, with t2's claim between them.
5. Sequences. Rows keep their seq values, `sqlite_sequence` carries over, and T7 checks that the
next insert is 41 after 40.
6. Id collisions. A legacy session id is its thread's id. `sessions_id_not_thread` refuses a session
id that names another thread, and `threads_id_not_session` refuses a thread id that names a
session. New ids come from randomUUID.
7. `thread_one_per_run`. The inner `max(seq)` is per session.
### Duckworth's early look (duckworth-0107) and Sage's ruling (sage-2257)
Duckworth read 312cf33a1 with git only and found nothing blocking. Round 2 (gizmoduck/row264-r2,
rebased on dcd8d4dda with no shared paths) answers his notes:
1. Retired-generation replay. Sage ruled it into 264a (sage-2257). `#agent` now states the thread
run's fence in generations: the session's latest claim must have the generation of the run's
own latest claim (`#runGeneration`). This refuses nothing new. The trigger
`thread_claim_generation` gives each claim in a session its own number, so with `h.op` 'claim'
the generations match exactly when `h.holder_run` is the run, which is the check it replaced
(duckworth-0108). 312cf33a1 would most likely refuse the same calls. A new sessions.test case has a released run and a revoked run each try
`decision.withdraw`, `message.receive` and `message.read` after another run claims the next
generation. All refuse `not-holder` and every table's row count stays the same. The workspace
and path halves of T2 stay in 264b.
2. `assistant.stop` keeps returning `{thread, run}`. Adding `session` would change the pins in
tester-threads.test and threads.test for a field no caller reads yet. The Console stop in 264e
is the first reader, so the field goes there.
3. `#route` refuses an unknown session with `session-not-found` instead of a TypeError.
4. The README's session section says the `default` retry answers on new threads only, because a
migrated default has op `legacy-3k` (`legacy-3l` from round 5).
5. The Row 155 comment at the role.revoke end now says "held thread session".
### Mutants
Each runs on a copy of the candidate tree with one edit, and the named test must fail.
- M1 (sage-2257). In `#agent`, replace `h.generation !== this.#runGeneration(s)` with `false`.
Killed by sessions.test "a released or revoked run's retired generation". M1 removes the thread
run's fence entirely, so the kill shows the refusal is tested, not that generations add a check.
A mutant that puts back `h.holder_run !== s.run` survives by equivalence. That isn't a coverage
gap.
- M2. In `message.receive`, drop the `mt.session=?` filter (the session clause and its argument).
Killed by sessions.test T1, where a1 would take the second session's message.
- M3. In 3k-3l.sql, count every claim row for `generation` instead of `op='claim'` rows only.
Killed by sessions.test T7, generations 1, 1, 1, 2, 2.
### Dry run
Before writing any node test, I ran 3j-3k.sql in an in-memory Python sqlite3 on a 3j fixture
(gizmo-264-out/dry-3k.py, no repo code). The claims were kept with generations 1, 1, 1, 2, 2 in seq
order for the mixed t1/t2 fixture. Seq 40 carried over and the next insert was 41. The migrated
`sqlite_master` matched a fresh schema.sql, 119 rows each. Every guard refused its bad write.
### Hand-made threads in tests
A thread that a test writes by hand has no default session, because only `thread.create` and the
migration write one. The broker refuses such a thread with `session-not-found`, and the launcher view
leaves it out. So every test that inserts a `threads` row now also writes the legacy-shape default
session (`DEFAULT_SESSION` in threads, end-launch, thread-cli, thread-launch and chat tests). That
mirrors the migration's rows rather than inventing a third shape.
### Round 5: on row 236, the step is 3k-3l
Row 236 (8f51900b5, which round 5 is built on) adds `migrations/3j-3k.sql` with the
`gitea_users` and `gitea_grants` tables, and lands first. By the landing-order label (Q2,
sage-2412) 264a's step moves one version. The design above still says 3j-3k; read it as 3k-3l.
- `migrations/3k-3l.sql` is round 4's `3j-3k.sql` with three renames and nothing else: the copy
tables are `*_3k`, and the op values are `legacy-3l` and `migration-3l`. The op values name the
step's target version, as round 4's `legacy-3k` did on 3j-3k (sage-2415). No earlier migration
set a precedent either way.
- store.mjs runs 236's 3j to 3k step, then 3k to 3l. `SCHEMA_VERSION` is `3l`, and
`SCHEMA_SHA256` is the merged schema.sql's, cd68512212ce8bf8f5631138b2e63a340fa7b940088430649bb7784af7df162a.
schema.sql merged with no conflict: 264a's edits are above 236's two tables at the end.
- The T7 fixture is `fixtures/schema-3k.sql`, 236's schema.sql at 8f51900b5 (SHA-256 aba92408...).
236's `fixtures/schema-3j.sql` is the same blob round 4 added, so that path no longer differs.
- Pins: schema.test 22 tables and 86 triggers; README 22 tables and 87 triggers. Every `'3k'`
version pin is `'3l'`, the comment-migration future step is 3l to 3m, and each copied engine
also copies `3k-3l.sql`.
- account-migration and grant-migration filter both 236's objects and the 3k-3l objects.
gitea-migration.test (236's) now reads its store after both steps: version `3l`, `rows` skips
`sessions` and `session_defaults`, `master` skips the 3k-3l objects, and its failing engine
copies `3k-3l.sql`. Its 3j engine already stripped every step after 3i-3j.
- sessions.test's engine is a 3k engine: 236's schema and steps through 3j-3k, no 3k-3l.
- The restore guard lists merged with no conflict: 236 adds `gitea_users` and `gitea_grants` to
T46's `untimed`, and 264a adds `sessions` and `session_defaults` to COUNTED and LISTED.
Dry run (gizmo-264-out/dry-3l.py, Python sqlite3 in memory, no repo node): 3k-3l.sql on 236's
schema kept every claim with generations 1, 1, 1, 2, 2, kept seq 40 with 41 next, and the migrated
`sqlite_master` matched a fresh schema.sql, 121 rows each. Against round 4's run from 86f95bb27
(dry-3k-r4.py), the output differs only in the op values and the 119 rows that 236's two tables make 121.
## Runs
None yet. Every run waits for Sage's CLEAR.
-80
View File
@@ -1,80 +0,0 @@
# Row 268 build notes (Gizmoduck)
Case 2 C2-01c, playground levels. Gitea #1773, Vikunja 464. Brief: docs/plans/2026-10-20_case2-rows.md,
"C2-01c playground levels: universe-level prefixes and draft universes" (blob a6ac627c8). The ruling is
agents/gosalyn/work/case2/DESIGN.md "Ruling after r11: playgrounds at three levels" (:49-88 at
db1e7732a), and it wins where the two differ. Reviewer Webby, gate owner Sage. Branch gizmoduck/row268
from refactor db1e7732a. Line numbers are on this branch unless a commit is named.
## mapping.mjs
- `kind` (:71). An entry with a `kind` key must say `"hybrid"` or `"draft"`. Before, a plain universe
with `kind` refused as an unknown key, and `kind: "draft"` did the same.
- A draft (:73-86) refuses `books` with its own message before the key check, so the cause reads
plainly. It takes `kind`, `name` and `playground`, needs one prefix or more, and comes back as
`{ kind: 'draft', name, books: [], playground }`, frozen with the rest.
- The prefix level check moved out of the per-entry loop to :127-140, after every entry is read,
because it needs every entry's books. It runs after the book-holds-book, prefix-holds-prefix and
listed-twice checks, so an overlap still reports as before. For each prefix and each book folder:
- equal to any book folder: `library-mapping-invalid` (brief, What ships, "or equal to a book folder");
- inside or holding another entry's book: `library-playground-other-universe`, and the message says
which, with the book and its entry;
- holding its own entry's book: `library-mapping-invalid`.
A prefix inside its own book is book level, and one that touches no book is universe level. A draft
has no books, so its prefixes are either outside every book or refused as another entry's.
- `sources` (:117-120) now refuses any entry with a `kind`, so a draft can't be a source either. The
message changed from "not a non-hybrid universe" to "not a plain universe". mapping.test.mjs:61 on
db1e7732a pinned the old words, and that line moves with it.
- `library-playground-outside-book` is gone from packages and docs/case2.
## scope.mjs
`classify` (:12-24) now runs two passes. The first finds a prefix in any entry, and the file is
`playground` with that entry's id and `book` set to the entry's book folder that holds the prefix, or
`null`. The second finds a book folder, and the file is `canon`. `place` (:26-32) works out `type` and
`folder` from the first folder under the prefix or the book, as before. The validator keeps prefixes
from overlapping, so the first pass finds at most one.
## Readers on this base
- reader.mjs:182 and :194 spread `classify`'s answer into each file record, so a universe-level or
draft file carries `book: null`. Nothing in the reader reads `book`.
- reader.mjs:206-209. R5 fires for a canon file linking into any playground. R6 fires when the target's
universe differs and isn't a source. A draft can't be a source, so a link into one gives R5 and R6.
A link into the file's own universe-level prefix gives R5 only. No code change was needed. The new
test covers both.
- survey.mjs:66 and mirror.mjs:246 read only `scope`. mirror.mjs:251 calls `readLibrary` and checks
only `path`.
- No other package calls this `classify` or reads `universes` (git grep over packages/*/src).
- C2-05a (row 216) and C2-06 (row 217) have no candidate branch on origin at the time of writing. I
check again before in-review.
## Tests
- mapping.test.mjs: the old outside-book test splits into the other-universe refusals (inside another
entry's book, holding one, a draft prefix inside a book), the mapping-invalid ones (equal to a book,
own or another's, and holding its own), universe level in a plain universe and in a hybrid with
`books: []`, and the draft shape (frozen with `books: []`, with `books`, with none or empty
prefixes, an extra key, and five bad `kind` values). The overlap test gains a cross-entry hold and a
cross-entry duplicate. The hybrid test gains a draft source. The caller-supplied `fail` test now
uses `Second World/Playground/` and sees the new code, since `Elsewhere/` is valid now.
- scope.test.mjs, new: one case per level (book, universe in a plain universe, universe in a hybrid,
draft), `.canvas` under each new prefix staying unmapped, prefixes first inside a book that holds a
prefix, and the reader case for R5 and R6, with `book: null` on the read records.
- business.test.mjs: `Elsewhere/Playground/` now loads, and `Second World/Playground/` refuses with
`library-playground-other-universe` through `validateBusinessDocument`.
## Docs
- packages/library/README.md: the universe keys, a line on the three levels, the refusal table and
the Scope section.
- docs/case2/SETUP-SHEET.md "Your playground": one line per level and the draft entry.
- docs/case2/SURVEY-RUN.md:114: the new code and message.
## Hash pins
`git grep -E '[0-9a-f]{64}'` over packages/*/tests finds no pin on any changed file.
## Runs
None yet. Every run waits for Sage's CLEAR.
+137
View File
@@ -0,0 +1,137 @@
# Row 236 build notes: #436b Gitea grant path (#1742, schema 3j-3k)
Builder Megavolt, reviewer Dewey, gate owner Sage. Draft; the results sections are filled in after the
runs on Sage's gate.
## Sources
- Brief: `docs/plans/2026-10-20_agent-credentials.md`, "Brief: #436b Gitea grant path, build (schema 3j-3k)",
blob 9259ba2a1.
- Design: `agents/megavolt/work/436b/DESIGN.md` r5 at 1be535aca (sha256 68df02cd…f4), unedited. Scope:
sections 2 to 7, and section 9's X-G1 to X-G15 and mutants. Section 10 (GitHub half) is out of scope.
- Build carry: `agents/megavolt/work/436b/BUILD-CARRY.md` at 3330c901b (N9, N10).
- Schema label 3j-3k per sage-2237. If 264a or 206 lands first, this rebases onto a new branch name and reruns
the whole bus suite, the schema.test counts and the T46 lists.
## What changed
- `packages/bus/src/gitea-grant.mjs` (new): the grant body check, the admin slot (set, rotate, remove), the
admin and seat API calls, the start-time scan (`giteaStart`, `openGiteaGrants`, `tokenUnknown`).
- `packages/bus/src/broker.mjs`: the gitea executor (steps 0 to 8), settle, restart reconcile, revoke and fire,
sweep, status, and the `grants.*` verbs.
- `packages/bus/src/credentials.mjs`: `held` pairs at start; the register and claim paths the executor uses.
- `packages/bus/src/migrations/3j-3k.sql`, `schema.sql`, `store.mjs`: `gitea_users` and `gitea_grants`, new
tables only.
- `packages/bus/src/runtime.mjs`, `server.mjs`, `human-cli.mjs`: wiring of the start scan and the verbs.
- `packages/cli/src/grants.mjs` (new), `cli.mjs`, `scripts/mosaic`: `mosaic grants`.
- Tests: `gitea-grant.test.mjs`, `gitea-migration.test.mjs`, `gitea-grant-docker.test.mjs` (X-G3),
`fake-gitea-api.mjs`, `fixtures/schema-3j.sql` (02da6a2ae's schema.sql), `cli/tests/grants.test.mjs`, and the
pin updates in the migration tests, `schema.test.mjs`, `channel-proof.test.mjs` and `stack-release.test.mjs`
(T46).
- READMEs of bus and cli.
## Choices and assumptions
Each is a reading of the design where it left room. Dewey may rule otherwise.
1. Admin rotation is due once today is past `rotateBy` (the date itself is still good).
2. A seat-token rotation moves the record at step 8, after the old token's delete (step 7).
3. A step 7 failure leaves the grant applied, with the old id in `serviceLeft` and the notice "The previous
token X is still live".
4. A failure after the commit step's rename returns `settle-pending`; the next start settles it.
5. tester-cli keeps its 5 s timeout; the grant verbs answer at once and the executor runs after.
6. A revoke of a token-unknown agent takes both ids from that decision's last commit step.
7. A user adopted at restart is upserted in the same transaction as its `grant.failed interrupted`.
8. `#giteaUnlink` (first-grant commit undo) uses a temporary reference dated `9999-12-31`, so it is never due
while it lives.
9. X-G12's org-team case: the fake models team membership as collaborator reach on the repository, which is
what `/user/repos` shows (F11, F12).
10. X-G15's look-alikes are stored under `action.refused` and under a non-grant kind, since the 3j trigger
refuses the human's NULL pair on `action.allowed`. The trigger cases of X-G13 (session, NULL pair,
`grant.host.enrolled`) are in `grant-migration.test.mjs`.
11. Step 6 faults: a pre-rename failure is injected by making `gitea.tmp` a directory; a step 8 store fault by
a TEMP trigger on `gitea_grants`.
12. The credentials-verb revoke close is tracked, so `giteaIdle()` waits for it.
13. `openGiteaGrants` gives `agent: null` for a spend with no step yet, so `status.running` can show a null
agent; the CLI prints it empty.
14. N9: a restart ignores a settle-pending commit with a `grant.revoked` after it and ends it `revoked` with
no message (notify false). A 404 on a delete by id counts as done.
15. N10: `serviceLeft` is a list in every body and answer.
16. A restart claims the agent's key for each open grant it undoes. One executor runs per agent
(grant-busy), so a killed child leaves at most one open grant per agent. If a store ever held two, the
second's claim fails and the undo runs with no admin: its token is left and named in `serviceLeft`,
never deleted unclaimed.
17. X-G7 changes the binding with a fixture decision whose hash differs (row 234's G9 pattern), since the
broker keeps its own copy of the business. The admin url check takes row 199's `parseGiteaUrl`, which
accepts the origin with or without a trailing slash.
18. r2, Dewey B1 (dewey-0781): step 8 and both settle branches (forward, back) check for a `grant.revoked`
of the agent after the decision's raise, in the same transaction as their write. A revoke that came
while step 7's or the settle's delete awaited took the record and both tokens, so the grant ends
`revoked` with the human's notice, as a revoke of a running executor ends it (X-G11), and no record is
written back. N9's restart case stays silent: a restart never settles a revoked rotation.
19. r2, Dewey B2: a sweep retry of `tokens` or `login` checks the user's id first, as the revoke close does,
against the `gitea_users` row (it survives a fire). A 404 skips with `gitea-user-gone`, a 200 with
another id with `gitea-user-foreign`, any other status leaves the entry. Skips go in `grant.swept` and
the sweep answer as `skipped: [{agent, user, id, reason}]`; a skipped entry is not left, so the next
sweep does not retry it.
20. r3, Dewey N5 (dewey-0783): the settle's 401 branch and its grant-token-unknown branch take the same
revoke check. A revoke during the whoami ends the grant `revoked`, not `grant-lost`, and the
token-unknown branch neither holds the agent again nor writes `grant-token-unknown`, so no start
names a file the revoke unlinked.
## Named for DEFERRED (r2, Dewey N1-N4)
None is a small local change, so none is in r2. sage-2388: all four DEFERRED; N3 and N4 get a bus README
sentence each in r3 (a left admin token is deleted by hand at Gitea; a running executor keeps the admin
api it started with).
- N1: an admin rotate cut between the mint and the slot write leaves a token at Gitea that no event names.
A `grant.admin rotate-begin {oldName}` before the mint would close it; that is a new event shape.
- N2: `adminTokenLeft` is `unknown` when `/api/v1/token` gives no name. Which 1.27 F-fact shows the name
needs a check against the container, not a code change.
- N3: admin set or remove records `adminTokenLeft: null`, so a left token drops out of status. Carrying it
forward lets `rotate --finish` delete by name under a slot of another account or url; refusing remove
while one is left blocks the human's remove when Gitea is down. Either needs a ruling.
- N4: rotate or remove while a gitea claim is held. Refusing it blocks the human's remove during a run;
the running executor keeps the api it began with. Needs a ruling or a line in the README.
## Q188
No test creates a grant except through fixtures or the human's yes on a scratch store. No test reaches
git.mosaicstack.dev, tasks.woltje.com or any provider. Unit tests use the fake Gitea on 127.0.0.1; X-G3 uses a
throwaway gitea/gitea:1.27.1 container from the local image (never pulled), removed after. Every token is a
fake or a scratch-container token; values are compared with `assert.ok`, never printed.
## Runs
(filled in after Sage's CLEAR)
## Mutants
(section 9's list, each with the test that kills it; filled in after the runs)
A static pass over the 39 mutants (all apply, problems=0) found five with no test to kill them. Four tests
were added before any mutant run:
- M8, M9 (user or mint step written after its call) and M16 (grant.result written before its call): "X-G6
and X-G8: the user and mint steps are written before their calls, and each result after its call". It
reads the store from the fake while user-create and token-create are at Gitea, and checks every result's
status.
- M29 (fork parent not checked): "X-G4: a fork by the seat user of another repository than the project
refuses fork-foreign".
- M28 (memory and file revoked after the Gitea delete): "X-G11: a revoke unloads memory and removes the file
before its Gitea delete", which holds the DELETE open and reads the file and state.
- M37 (evidence without its kind filter): "X-G15: a look-alike spend under another kind with the grant writer
pair is not an open grant".
r2 tests, each red on 8bf442be9 by reading:
- B1: "X-G11: a revoke during a rotation step 7 ends it revoked, and step 8 writes no record back" and
"X-G11: a revoke while a sweep settles a rotation, forward or back, ends it revoked with no record".
On 8bf442be9 step 8 and the settle write the record and `grant.applied` (forward) or `grant.failed
interrupted settled:back` after the revoke deleted it.
- B2: "X-G14: a sweep retries a fire only for the user it fired: a remade or gone user is skipped and
named". On 8bf442be9 the retry fires the remade user (token list, delete, PATCH) and has no `skipped`.
- N5 (r3, sage-2388): "X-G11: a revoke during a settle whoami that finds a foreign token ends it revoked, not
held" and "X-G11: a revoke during a settle whoami that gets a 401 ends it revoked, not lost". On 77dd43c9e
they end `grant-token-unknown` (the agent back in tokenUnknown) and `grant-lost`.
@@ -1,474 +0,0 @@
# Row 191 stopRun inspection round
Design: ../DESIGN-r2.md, approved by Dewey in 29473; scope sage-2244;
per-run contract ../STOP-RUN-CONTRACT.md, honker-0079/0081. Not review-ready:
full package/shell/Docker gate and acceptance/guard mutants remain pending.
Current commit 7aa18b0f6e1a09188fb602853ebb02815b642ac9, new branch
rocko/row191-stoprun-dcd8-20261022, parent dcd8d4dda. Worktree
/mnt/storage/scratch/rocko-r191-r3. Earlier inspection f7bf7dfec is preserved.
All 79 manifest paths have identical blobs at f7bf7dfec and 7aa18b0f6;
rebase-product-diff.stat is empty, rebase-product-check.txt names the check.
The manifest SHA-256 stays
9971ac5fe56d0d36eb914ecaf2c282e14ab3cd29860fd27b268ec0baadaf0e52.
Rocko author and committer identity were selected explicitly. No attribution
lines, force-push or branch deletion. Sage publishes through jarvis.
## Changes Dewey must review: two focused-run fixes
1. packages/bus/src/broker.mjs, requestHost: the new processingWork guard was
initially inserted into the inner refusal logger's try block. That let the
original request reach validation, then prevented its refusal being logged
while halted, returning storage-unavailable. Move it to the main admission
try, before keys/credentials/authority/effects. The refusal logger retains
its existing unconditional audit behavior. The new host-admission test now
expects processing-halted and proves neither executor method was called.
Initial evidence: ../logs/stoprun-focused-packages_bus_tests_processing.test.mjs.log.
2. packages/bus/tests/processing.test.mjs, real socket await test: row 196's
asynchronous kernel-peer subprocess now precedes dispatch. The old 5 ms
server deadline could fire during that subprocess, before the processing
await branch extends it. This test already stubs authenticateHuman; give it
a deterministic peerReader too, with read and close, and assert exactly two
proof reads for halt/resume. Keep the 5 ms ordinary timeout and 30 ms halt
delay. Production peer discovery, proof/transport and timeout behavior are
unchanged. The test still fails if the async branch timeout is removed.
The first fixture retry omitted close(); its teardown failed and left its
own server running. Only that verified test group 2831504 was killed. Both
unsuccessful logs remain. Corrected evidence:
../logs/stoprun-focused-bus-processing-fixed-2.log (14/14).
These are diagnosed code/fixture mistakes, not declared flakes. No failed log
was removed and no existing security assertion was weakened.
## Focused evidence, sage-2250
Ran singly on 41b0cf755 because none of row 254's seven paths overlaps ours.
Sage-2265 carries this evidence to dcd8d4dda after the empty 79-path diff.
Scripted busy check exited 0 before the first file. Every test runner used its
own process group, 180 s timeout, scratch TMPDIR
/mnt/storage/scratch/rocko-r191-tmp and DOCKER_HOST=unix:///nonexistent.sock.
There was no Docker, browser, package suite, shell suite or mutant run.
| File | Final passed | Failed | Skipped |
|---|---:|---:|---:|
| processing-control/tests/stop-run.test.mjs | 12 | 0 | 0 |
| processing-control/tests/runtimes.test.mjs | 10 | 0 | 0 |
| processing-control/tests/coordinator.test.mjs | 3 | 0 | 0 |
| processing-control/tests/containers.test.mjs | 6 | 0 | 0 |
| processing-control/tests/effects.test.mjs | 5 | 0 | 0 |
| processing-control/tests/task.test.mjs | 7 | 0 | 0 |
| bus/tests/processing.test.mjs | 14 | 0 | 0 |
| bus/tests/processing-runtime.test.mjs | 2 | 0 | 0 |
| bus/tests/project-runtime.test.mjs | 3 | 0 | 0 |
| Script sum | 62 | 0 | 0 |
All final runners exited 0. The batch stopped at the initial bus failure,
then that file was diagnosed/rerun before the final two files. Counts are from
focused-run-table-final.json and include only each final successful file log.
Earlier bus failures are reported above, not silently included as green.
Approximate run span 23:59Z–00:00:41Z; start second was not recorded.
Cwd-scoped node/chromium sweep was empty at 00:00:41Z; slot returned to Sage.
## stopRun implementation
The export resolves host-owned run identity even without a bound bus claim.
It checks boot/start/group identity immediately before each signal, preserves
verified member anchors, waits 5 s before KILL and observes for 2 s. Signals
are acknowledgements only, never invented. Root/business/run cache joins the
same Promise. Atomic hard-link publication plus durable exclusive claim
coordinates other host processes. First owner/reason/deadline/result persists;
completed retries never re-escalate or overwrite that result. Halt links its
generation to the first fire receipt without relabelling the runtime's stop
cause. A later verified exit is separate immutable observation evidence.
The seam writes no bus events. Honker maps killed/alive to lifecycle events.
No global unit, live config, binding, token, slot file or live store touched.
No schema migration. First activation still requires stopping old processing
before trusted initialization and aligning the copy/image/read-only mount in
Mos's window. Existing result.json evidence remains write-once.
## Integration and outstanding work
The 12 latest-base conflict resolutions are listed in STATUS.md. New landed
host execution, publish and library sync paths are now wrapped in processing
scopes; host credential cancellation composes with its existing revocation
signal. This changes only files already owned by 191. Those wider integrations
still need the full owning-file/package gate and mutant evidence.
Fifteen proposed per-run mutants preflight exact once in stoprun-mutants.json;
none has been applied or run. The broader kill-switch acceptance/guard table
and executable mutant runner are still being completed. No kill count or
review-ready claim is made. Ask Sage for the later CLEAR when the packet is
complete, as sage-2259/2262 requires.
### Gate preparation after the focused slot
The published product implementation is unchanged. Three uncommitted test-file
strengthenings are saved in post-inspection-tests.patch: effects.test.mjs adds a
4-second bounded abort expectation for the watcher mutant; Discord's early
start fence is also checked before a malformed journal can be read; WebUI's
body limit now uses oversized valid JSON, and a direct-handler test isolates
its own origin/site guards from serve's outer fence. These edits are unrun and
are not included in the published 9971ac5f manifest. They must pass before a
new candidate is cut. Thus 62/62 is prior focused evidence, not a claim for
these changed test bytes.
The complete proposed table has 70 exact-once sites in all-mutants.json,
including expected killing test names. Static preflight is clean; no mutant
has been applied. run-mutants.py validates preflight, uses new process groups,
tracks only its own verified descendant identities, kills those groups on
timeout, checks named TAP failures, and restores all source bytes in finally.
A timeout or wrong-test failure is not counted as a valid kill. The table and
runner are prepared evidence, not a kill result. The requested nine-package,
ten-shell, seventy-mutant gate is in gate-plan.json; Sage CLEAR is pending.
## Published test-strengthening commit and full gate (sage-2269/2271)
81889b58d57889b12a584aa225e1ac26e90e5700 is one fast-forward commit on
7aa18b0f6, same branch rocko/row191-stoprun-dcd8-20261022. It commits the
three test changes saved in post-inspection-tests.patch; that patch remains
historical evidence, not an uncommitted candidate. Product implementation is
unchanged. Earlier manifest 9971ac5f belongs to 7aa18b0f6; candidate.txt now
names the manifest for 81889b58d. Sage published this tip before granting CLEAR.
The full gate checks HEAD equality and clean product bytes before its first
file. Changed test files run singly, then nine packages, all ten shell suites,
then seventy mutants. Each step gates on its exit status and retains its full
log. run-gate.sh uses pipefail and an EXIT trap for the label-verified Compose
network rocko-r191-gate_default, project rocko-r191-gate. TMPDIR is
/mnt/storage/scratch/tmp/rocko-r191-gate. Docker is available only for test-task
and test-release; other steps use unix:///nonexistent.sock. Live provider calls
are disabled. No daemon, socket, volume or live configuration changes.
Gate start: 2026-10-22T00:18:00Z. Results remain pending until gate-results.json
and mutant-results.json provide actual exit statuses and counts.
### First full-gate stop and fixture correction (sage-2274)
The 81889b58d run stopped at its first failure, 00:18:00Z–00:19:21Z.
27 files completed: 268 passed, 1 failed, 0 skipped. Packages, shells and
mutants did not start. Full logs/results are in gate-attempt-81889/.
The tester refusal test's fixture requested tester:true over TCP, which row
196 now refuses before route handling. This is a stale fixture, not a flake.
The correction uses the existing startTesterSocket helper, a private fixture
root, socketPath requests, TESTER_ORIGIN and no human identity header. Every
assertion remains; the request now reaches the intended processing refusal
rather than failing listener setup or the outer identity fence. A grep of all
changed test files found no other tester:true TCP setup. The cwd node/chromium
sweep returned []; no Docker test workload ran. Commit c78f48946 is a single
fast-forward test-only fix on 81889b58d, authorized by sage-2274. The complete
gate restarts from its first single file only after Sage publishes this tip.
Sage-2275 published c78f48946 and granted full restart. Second attempt begins
2026-10-22T00:21:03Z; any further failure stops before additional edits.
Candidate manifest b49fd493dc75ef2484ffc255213dc8eeaaaa348dee416c8d26d1c2bab8e20067.
### Second attempt stopped; no further changes (sage-2275)
Run 00:21:03Z–00:23:50Z on published c78f48946: all 27 changed singles
269/269, processing-control 61/61, bus 646/646. The CLI package emitted 26
failed subtests before its verified own runner group 3119310 was stopped.
Its result is interrupted, not a complete package count. Six gate-lane image
checks refuse because scripts/testing/lane-image.mjs pins the old four COPY
lines and 191 adds processing-control to Containerfile. Twenty launch-account,
launch-limit-checks, launch-limits and launch-routes cases hit
processing-uninitialized on fixture roots with no explicit control init.
No edits, retries, later packages, shells, Docker test workloads or mutants
followed this second failure. Parent runner exited; cwd node/chromium sweep
returned [] at 00:23:50Z. Slot returned to Sage at 00:24:05Z with the exact
failures and a request for scope on image-manifest and fixture initialization.
This is an integration failure, not a declared flake or a passing gate.
## Image-input manifest and launcher fixture scope (sage-2278, sage-2282)
Commit eb3abba12 is one fast-forward commit on c78f48946. This is an image-input
manifest change: scripts/testing/lane-image.mjs now pins the exact Containerfile
COPY of packages/processing-control/src/state.mjs and entrypoint.mjs and hashes
both named source paths, without a wildcard. Its only two changed lines add
those inputs. Without them, a lane could reuse a stale image after a reader or
entrypoint change. The image test fixtures copy both files and each has its
own receipt-invalidating test. Mutant KS56 drops state.mjs from the hash list;
its named killer is the state.mjs receipt-invalidating test. Static preflight
is 71 sites / 0 errors. No new run result is claimed before CLEAR.
The launch-account, launch-limit-checks, launch-limits and launch-routes fixtures
now explicitly call initializeControl(f.dataRoot) before constructing their
launchers. Existing assertion lines are byte-identical. A new launch-limit-checks
test leaves initialization off and asserts processing-uninitialized, no
mayLaunch/authorizeLaunch, and no registered session. A live root gets control
initialized by the bus broker. The re-pin order is bus start before any seat
launch; a launcher started before that refuses. This supplements the existing
window precondition to stop old processing before the first trusted init.
Sage-2282 supersedes the test-only image ruling: lane-image.mjs is a product
blob, so mos-1200 carries no earlier results. The complete plan restarts from
the first single file, all nine packages, all ten shells and all 71 mutants
only after publication and Sage CLEAR. Earlier attempts remain in their
separate gate-attempt directories; neither is a passing full gate.
Sage-2283 published eb3abba12. Current 85-path candidate manifest is
01a2b33651414c9b0cb094d2108f9e39c48a41b79974dcc16bdec374f8371091.
The gate prints HEAD and this digest, verifies every manifest blob before its
first test, and carries no old results. Awaiting CLEAR after Gander 253.
## Full restart on eb3abba12 stopped at Discord (sage-2286)
Verified HEAD and all 85 manifest blobs before tests. Run span:
2026-10-22T00:34:50Z–00:47:33Z. No old result carried.
| Step | Passed | Failed | Skipped | Exit |
|---|---:|---:|---:|---:|
| 32 changed test files singly | 304 | 0 | 0 | 0 |
| processing-control | 61 | 0 | 0 | 0 |
| bus | 646 | 0 | 0 | 0 |
| cli | 761 | 0 | 0 | 0 |
| discord | 190 | 2 | 0 | 1 |
Singles sum independently confirmed by awk over all 32 TAP logs. Discord's
binding.test.mjs:201 STOP check and recover.test.mjs:145 ready recovery check
both get processing-uninitialized before the expected behavior. Those older
fixture roots do not explicitly initialize control. The gate stopped at this
step; no subsequent package, shell suite, Docker test workload or mutant ran.
Docker availability was checked read-only (29.7.2); no daemon/volume changes.
The EXIT trap completed. Cwd node/chromium sweep returned [] at 00:47:49Z,
source tree remains clean, and the slot was handed back at 00:47:58Z.
No test or product edits or retries followed this failure; Sage has the table
and the fixture-initialization recommendation. This is not a green full gate.
## Discord fixture init and all-package static sweep (sage-2294)
Commit e6800380dc0765ce3f1477044e2ce4f773ebb43c is test-only and fast-forwards eb3abba12.
Only binding.test.mjs and recover.test.mjs change: one initializeControl import
and one initializeControl(dep.dataRoot) call in each affected CLI fixture.
Every original assertion line is retained. Product blobs are unchanged.
FIXTURE-SWEEP.md and fixture-sweep.json enumerate 338 source call-site hits,
with the two fixes and the already-initialized chains, plus the screens for
rootless optional contexts and read-only/usage CLI calls. The generator reads
repository source only and invokes no fixture. Deliberate missing/deleted/
corrupt-state refusal tests remain negative; no shared helper masks them.
Current manifest be36af8f0855f57336e6a33283aa0a8e94297f338b1f8180936cbccf1119c60a, 87 paths. Sage-2294 carries only unchanged
processing-control 61/61, bus 646/646, CLI 761/761 and unchanged singles from
gate-attempt-eb3/. The resume must run both touched files wholly, whole Discord,
then the never-run WebUI/tasks/seat/project/library packages, every shell suite
and all 71 mutants. No result is claimed for the new test bytes before CLEAR.
The live initialization order remains bus start before any seat or Discord
start; no fixture change initializes or repairs a live root.
## Resume on e6800380d stopped at WebUI (sage-2311)
Start 2026-10-22T01:27:40Z; failed-step exit 01:28:45Z. Startup verified HEAD,
manifest be36af8f and all 87 blobs, and read-only Docker availability (29.7.2).
| New step | Passed | Failed | Skipped | Exit |
|---|---:|---:|---:|---:|
| binding.test.mjs whole | 15 | 0 | 0 | 0 |
| recover.test.mjs whole | 5 | 0 | 0 | 0 |
| Discord whole | 192 | 0 | 0 | 0 |
| WebUI whole | 314 | 4 | 1 | 1 |
Carried unchanged under sage-2294: 32 singles 304/304, processing-control 61/61,
bus 646/646 and CLI 761/761, from gate-attempt-eb3/. These are explicitly carried
results, not fresh runs on e6800380d.
Four WebUI failures are retained in gate-logs/package-webui.log:
- board-held.test.mjs:91: processing's config/state GETs omit redirect:manual.
- board-held.test.mjs:194 and send-browser.test.mjs:58: processing's independent
hash/focus/interval refresh continues to read after the shell signs out.
- shell.test.mjs:104: the added control bar makes the short desktop page 1095px
high at a 1000px viewport. Its new height is not accounted for by the existing
frame/sidebar/footer layout.
Static diagnosis: processing.js ignores the existing body.signed-out state and
mosaic-signed-out event that app.js and bus.js already share. Its GETs also lack
manual redirect handling. processing.css adds an external bar while shell.css's
frame minimum and sidebar heights still subtract only bar-h and foot-h. These
are product integration defects; no assertions were weakened and no fix was
made after the failed step. Sage received the table and recommendations.
One TAP skip: the editor rebuild requires exactly esbuild 0.28.2; installed
shared node_modules has 0.28.1. Full skip line is in tap-skips.json. No shell
suite ran, so there are no ^skip shell lines (skip-lines.txt remains empty).
No tasks/seat/project/library suite, shell Docker workload or mutant ran after
WebUI failed. Parent runner exited, EXIT cleanup completed, cwd node/chromium
sweep returned [] at 01:29:00Z, source tree is clean, and slot returned to Sage.
No retries or source edits were made. This is not a passing gate.
## Console shared sign-out and bounded desktop layout (sage-2317)
Commit 8e1f55c1db6a096e227b5114cd13a7142493b112, fast-forward on e6800380d, changes only the
processing assets and their owning two tests. Current 87-path manifest:
d3b014e6a0e9e547a29ae780eb27b1c0e57cc7b0e73dc49954abbff623929f41.
All three processing fetch sites use redirect:manual. responseBody dispatches
the existing mosaic-signed-out event for an opaque redirect, or its signIn
variant for exactly 401/sign-in required. No independent sign-out state is
added: refresh/action guards read body.signed-out. The existing event cancels
the interval, invalidates pending reads, clears stale state and disables
writes. In-flight answers cannot restore state after sign-out. Initial work
waits for DOMContentLoaded because this deferred asset precedes app.js in the
HTML and needs its shared sign-out listener registered first. app.js/bus.js
and their proof baselines remain untouched.
The CSS keeps the control bar in normal document flow. On desktop, the frame
flexes into the remaining viewport rather than imposing another full-height
minimum below the bar. Fixed navigation retains the shell's command/footer
bounds and existing responsive column widths. Mobile retains natural flow.
No short-page, sidebar or footer assertion is changed; rendering remains to
be verified on the next authorized gate, not claimed from static checks.
The VM fixture models the existing body class, document events and DOM ready;
its original assertion lines are unchanged. Six new browser cases cover config,
state and POST opaque redirects and sign-in-required replies, manual redirect
requests, interval cancellation and no subsequent fetch from forced refreshes
or clicks. KS57 removes manual redirect from the config GET; KS58 removes the
refresh signed-out guard. Both name the config-redirect browser case as their
killer. Static preflight: 73 exact-once sites / 0 errors. Syntax checks pass;
no owning test or mutant has run on this commit.
A product blob changes, so sage-2317/mos-1200 carries nothing. The next CLEAR
runs all changed files singly, all nine package suites, all ten shell suites
with Docker available and all 73 mutants. The editor rebuild's esbuild0.28.1
vs pinned0.28.2 skip remains host drift and is reported separately. Earlier
failed attempts remain immutable in gate-attempt-* directories.
## Rebase onto row 202 (sage-2329)
New branch rocko/row191-stoprun-ddfb-20261022, tip eafcbd3e7d6ef6e259fb59d87a7b0857b42337aa,
base ddfb29af7. The prior published branch is preserved; no force-push, delete
or old-name reuse. Six Rocko commits rebased, with one manual conflict in
scripts/mosaic's human CLI case: prepend halt|resume|processing and retain
row 202's appended access. packages/cli/src/cli.mjs auto-merges retain the
access import/branch and all three processing commands. CLI README retains
both the Access and Instance processing switch sections.
The full old/new tip diff has 15 paths (base plans/queue/logs, row 202's new
access files and its three shared CLI paths), 2327 insertions/32 deletions.
Within row 191's owned paths only CLI README, cli.mjs and scripts/mosaic differ,
with the row 202 additions. rebase-ddfb-log.txt, rebase-ddfb-tip-diff.stat,
rebase-ddfb-owned-diff.stat and rebase-ddfb-range-diff.txt retain exact evidence.
Manifest fd0f2740b1cccadaaad8c613640fdddb0497cdab33ec2c0fd11debf3cae6c4ee, 87 candidate paths. The package command lists were
regenerated from this base; CLI includes access.test.mjs. All 73 mutant sites
must preflight again. No tests ran, no result carries, and the complete gate
waits for publication and Sage CLEAR after Megavolt's debug run.
Sage-2333 published eafcbd3e7. Their per-file patch-id comparison matches
the prior six-commit range except the exactly ruled scripts/mosaic dispatch
line; binary/secrets/attribution checks pass. Gate remains unrun pending CLEAR.
## eafcbd3e7 full gate stopped on timer fixture attribution (sage-2339)
Run 2026-10-22T02:18:04Z–02:19:25Z. HEAD, 87 blobs and fd0f2740 manifest
verified. Short mktemp TMPDIR /mnt/storage/scratch/tmp/gxbC; named Compose
project rocko-r191-gate. Nothing carried. 32 changed files completed:
313 passed, 6 failed, 0 skipped. All six new processing-browser sign-out
cases stop at their common precondition, processingTimers length2 vs1.
The original two browser cases pass. Their recorder attributes every 5000ms
interval to processing, but app.js:307 already arms its own freshness timer.
The intended processing interval is at processing.js:87. These new cases have
not yet exercised sign-out or killed KS57/KS58, so no behavior proof is claimed.
The gate stopped at this first red step. No package, shell, Docker workload
or mutant ran; the esbuild rebuild was not reached. Runner exited, EXIT cleanup
completed and cwd node/chromium sweep returned [] at 02:19:34Z. No source edit
or retry followed the failure. Sage received the exact failure and a proposed
test-only caller-attribution filter with the assertions unchanged. This is a
fixture error, not a passing gate or a declared flake.
## Timer recorder attribution correction (sage-2345)
Commit 4db8b57785895bc62f5f015468a69515da7fc77f, fast-forward on eafcbd3e7, is a one-line
test-only correction in processing-browser.test.mjs. Cause: app.js:307's
freshness interval is also 5000ms, and the recorder incorrectly counted it as
a processing interval. The recorder now requires the processing.js caller
stack. Every assertion remains byte-identical, including exactly one timer
before sign-out and zero after it. A second 5000ms interval from processing.js
would still be counted and fail the exactly-one assertion. No product byte
changes, and no test or mutant ran for this correction.
Manifest ffb46fbdb875f97ff2eb7a7bed8bf6d5c4df53b8a63c738ffcde5fc5f6b7f6ad, 87 paths. Full restart after publication and CLEAR,
with nothing carried. Sage-2345 counts the timer-fixture red as the first in
this 191 window; any second red stops and is escalated to Mos. Previous logs
are retained in gate-attempt-eaf/.
### mos-1248 / sage-2363: desktop viewport allocation (unrun static candidate)
The second red on 0f8235277 was shell.test.mjs:380 (1062 versus 1000).
The shell gives .frame a viewport-based minimum at packages/webui/src/public/shell.css:135;
processing.css:12-14 replaced that minimum but used a column flex body with only
min-height:100vh, so the new bar was not allocated a bounded share of the viewport.
The desktop body now uses explicit grid rows for the command bar, controls, optional
banner, frame and footer. The frame row is minmax(min-content,1fr): it takes the
remaining short-page space while long content may grow the document. No clipping,
overflow suppression, fixed body height, test viewport change or assertion removal.
The existing shell.test.mjs:380 equality stays byte-identical. Added assertions
check actual processing-bar visibility, positive height, viewport placement and
no document scroll at 1440x1000; the existing 1440x420 long-page checks remain.
KS59 reverts processing.css to 0f8235277's bytes and must fail that same shell
browser test. Total: 74 planned mutants. No test or mutant has run on this fix.
Webby must statically review this delta before Sage opens the full no-carry gate.
### Correction after Webby B1 (webby-0104, 2026-10-22 03:00Z)
The cause stated in the mos-1248/sage-2363 note above is withdrawn.
processing.css loads after shell.css and already overrides the frame minimum.
With the tree fixed and inspector hidden, the remaining floor is the Board's
main content. The grid min-content row preserves that floor, so 5cd32f4c1
is not an accepted layout fix. Its static review is CHANGES at Webby's
REVIEW-layout.md, commit e2e7c60e5. No run established a new layout measurement.
Next step requires a granted fixture probe for main/control heights and an
owner layout ruling; no further product change or gate is authorized yet.
Existing shell assertions remain unchanged.
### Static nav placement: mos-1249/mos-1250, sage-2383/2385/2386, webby-0106/0108
Fast-forward on 5cd32f4c1; the rejected grid is removed. The unchanged labeled
region is now the first child of nav.tree, before its Console h2. The banner
stays at its original body location with every id, role, label and shipped
string unchanged. No processing.js change and no runtime reparenting.
At >=760 the tree is the same fixed box, with the same width breakpoints and
ordinary overflow:auto. The controls are non-shrinking and non-sticky; they
scroll with overflowing nav links (sage-2386 withdraws persistent visibility).
Below760 the region takes a full flex line before Console/Projects. Its mobile
location changes from before .frame to first in the static nav; the banner
therefore precedes it in a halted mobile view. Banner behavior itself is unchanged.
Specificity (0,3,0) resets .btn margin/order and .small margin/basis, leaving
the shell rules unchanged. No content is shortened and no overflow is hidden.
Correct height arithmetic (Webby B1): page=max(innerHeight,86+C), where C is
main content height. Controls consume space within the existing nav box, not
another body row. The old failing layout was max(innerHeight,86+barHeight+C).
The halted banner remains a body row; Sage tracks that separately.
Candidate-test design change at processing-ui.test.mjs:67 under mos-1250.
Old assertion:
```js
assert.ok(html.indexOf('id="processing-controls"') < html.indexOf('<div class="frame">'));
```
New assertion text:
```js
const controlsAt = html.indexOf('id="processing-controls"');
const mainAt = html.indexOf('<main id="main"'), inspectorAt = html.indexOf('<aside class="inspector"');
assert.ok(controlsAt >= 0 && controlsAt < mainAt, 'controls are outside and before #main');
assert.ok(!(inspectorAt < controlsAt && controlsAt < html.indexOf('</aside>', inspectorAt)),
'controls are outside aside.inspector');
```
Reason: the bar moves into the frame's first column under mos-1249 while
remaining outside route-specific #main and aside.inspector. This is a change
to our unlanded candidate assertion, not a landed assertion.
All existing shell assertions stay unchanged. Added bar-inside-tree assertion
and diagnostic height output. New processing browser coverage checks 400px
Stop order=0, visibility, focus and position above Projects; also on-load
visibility/focus at1440x1000,1440x420,800x420. Debug logs report controls/tree
heights at those desktop sizes and main height; Sage judges controls over half
the 334px nav at1440x420 before the window. No run yet.
Mutant delta: KS59 now reverts the complete layout (HTML and CSS together);
KS60 hides the controls; KS61 moves them into #main; KS62 drops the (0,3,0)
button reset. Total77. Every new site has its named killing test.
-2
View File
@@ -38,8 +38,6 @@ services:
volumes:
# Configured runtime state root (from config.json dataRoot).
- ${MOSAIC_DATA_ROOT:?MOSAIC_DATA_ROOT must be set by scripts/load_config (run via scripts/*.sh)}:/var/lib/mosaic
# The whole control directory is protected against unlink and rename-over replacement.
- ${MOSAIC_DATA_ROOT:?MOSAIC_DATA_ROOT must be set by scripts/load_config (run via scripts/*.sh)}/control:/var/lib/mosaic/control:ro
# Runtime credential only: pi auth file mounted READ-ONLY.
# Never copied into the image.
- ${PI_AUTH_FILE:-/home/jwoltje/.pi/agent/auth.json}:/home/node/.pi/agent/auth.json:ro
-14
View File
@@ -276,25 +276,11 @@ scripts/mosaic follow --thread <thread> [--tester]
scripts/mosaic stop <run>
scripts/mosaic launches off | on [--business <id>]
scripts/mosaic launches list [--json]
scripts/mosaic halt | resume [--business <id>]
scripts/mosaic processing status [--business <id>] [--json]
scripts/bus-service.sh [--instance NAME] render [--copy ID [--copies DIR]] [--pm] [--claude PATH]
scripts/bus-service.sh [--instance NAME] install [--copy ID [--copies DIR]] [--pm] [--dir DIR] [--no-reload] [--claude PATH]
scripts/bus-service.sh [--instance NAME] uninstall | status <business>
```
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
recovery. `mosaic processing status --json` reports settled, pending or unknown
stops; `mosaic resume` checks the current generation and refuses pending stops.
Only the existing human proof can write it, independently of channel settings.
A missing/invalid state is never off: use halt, then resume; an unsafe control
parent needs operator repair. Unknown task/remote outcomes are not retried.
Native diagnostic seats remain available. First activation must stop old
processing before trusted initialization and align the image/read-only control
mount. This is a separate approved window, never a build step.
The human commands read and resolve through the broker's human transport
(`packages/bus/src/human-cli.mjs`), never the SQLite file. They refuse inside
an agent run, except the forms that take `--tester` (rows 110, 175 and 184), which
+3 -9
View File
@@ -22,15 +22,9 @@ edits it by hand, and the agents can't change it. He will ask you to confirm it
## Your playground
A folder named in the `playground` list of that entry is your playground. A playground folder can sit
at one of three levels:
- Book level: inside one book folder, for ideas about that book, for example `<book>/playground/`.
- Universe level: outside every book folder, for ideas about a universe that aren't tied to one book.
- A new universe: a draft universe entry with a name and its own playground folders and no books, for a
world you are only starting. When it is real, Jason turns it into a plain universe with books and you
move the notes in Obsidian.
You choose the names and tell Jason. Until you have one, every note in a book folder is canon.
A folder named in the `playground` list of that entry is your playground. Each playground folder sits
inside one book folder, so one per book works well, for example `<book>/playground/`. You choose the
names and tell Jason. Until you have one, every note in a book folder is canon.
What the playground means for the agents:
- They search canon unless they ask for the playground on purpose.
+1 -1
View File
@@ -111,7 +111,7 @@ repo, so a bad mapping gives exit 3 even when the repo path is wrong too.
| 4 | `library survey: usage: mosaic library survey <repo> [--mapping <file>]` | no repo, two repos, an unknown flag, `--mapping` without a file or twice | Fix the command |
| 3 | `library survey: library-mapping-invalid: <file> is not a readable JSON file` | missing, unreadable by `mosaic`, or not JSON | Fix the file or its mode |
| 3 | `library survey: library-mapping-invalid: <file> <reason>` | the entry breaks the schema. The reason names the key | Give it to Jason for the draft |
| 3 | `library survey: library-playground-other-universe: <file> universes.<u>.playground "<prefix>" lies inside book folder "<book>" of universes.<v>`, or `holds book folder` | a playground prefix inside, or holding, another universe's book folder | Give it to Jason for the draft |
| 3 | `library survey: library-playground-outside-book: <file> universes.<u>.playground "<prefix>" is not inside one of its books` | a playground prefix outside its universe's books | Give it to Jason for the draft |
| 2 | `library survey: library-git: <repo> is not a directory` | wrong path | Check `<repo>` |
| 2 | `library survey: library-git: repository has no readable HEAD commit (project-git)` | not a repo, a folder inside one, another owner, no commits, a `HEAD` with no commit, or no `git` on `PATH` | Check each. If `<repo>` is the bus mirror, stop and report to Sage |
| 2 | `library survey: library-git: <other reason>`, such as `tree listing refused (project-git)` | git failed or ran past 100 seconds | Report to Sage |
+56 -29
View File
@@ -225,6 +225,11 @@ an operator inspect content and delivery evidence after an uncertain result.
| `hosts.remove` | `{host}`; the human or the tester. Deletes the record, writes `grant.host.removed` and removes the installer key. A `host_grants` record for the host is `host-in-use`, a guarded event naming a host line left on the host `host-line-left`. |
| `hosts.revoke` | `{agent,host,reason}`; the human, the business's assistant, or the tester when `bus.tester` is exactly `true` (row 235, #1741, r6 section 6). Anyone else gets `revoke-refused`. The tester check reads `bus.tester` again at the call, `tester-dev-only`. One transaction deletes the `host_grants` record and writes `grant.revoked` with `hostLine:"pending"`. With no await between, the key leaves memory and its file goes, and a `host.run` or executor in flight aborts. Then `remove <user> <tag>` runs on the host and `grant.revoke.closed` records `hostLine:"removed"` or `hostLineLeft`. A revoke with no record still runs, with `granted:false`. Returns `{agent,host,tag,granted,keyFile,hostLine or hostLineLeft}`. A restart with a pending marker runs the remove once the bus is up; `hosts.status` lists its tag under `revoking`. |
| `hosts.sweep` | `{host}`; the human or the tester (r6 X3). Runs `list <user>` and removes each listed tag this business recorded for the host in a `grant.step` or `grant.revoked` that no live grant holds. Another business's line and an unrecorded `mosaic-` line stay. Writes `grant.host.swept {host,removed,left}`; `hostLineLeft` then counts from it. Returns `{host,removed,hostLineLeft}`. |
| `grants.status` | `{service:"gitea"}`; the human, the tester or the chief (row 236, #1742, #436b r5). The admin slot's url, account and rotate-by date with `rotationDue`, `adminTokenLeft`, the `gitea_users` and `gitea_grants` records, approved decisions not yet run (`spentAtNextStart`), grants `running` or `settlePending`, agents held as `tokenUnknown`, the `serviceLeft` entries a sweep retries, and revokes still `revoking`. No token. |
| `grants.run` | `{decision}`; the human only. Starts the Gitea executor of a `credential.mint` decision with a gitea grant the human said yes to, as the yes itself does (X1). Step 0's refusals come back with no record: `not-authorized`, `decision-consumed`, `grant-token-unknown`, `grant-busy`, `gitea-not-configured`. Returns `{decision,agent,started:true}`; the outcome is a `grant.applied` or `grant.failed` and a notice. |
| `grants.revoke` | `{service:"gitea",agent,reason}`; the human, the business's assistant, or the tester when `bus.tester` is exactly `true`. The token leaves memory and its file goes, an executor in flight aborts, then one transaction deletes the `gitea_grants` record and writes `grant.revoked` with the token ids to delete. The deletes run with the admin token after C1 to C4, and `grant.revoke.closed` records `serviceLeft`, a list. `credentials.revoke AGENT gitea` and `credentials.delete` do the same for a stack-made user; delete also deletes every token of the user and sets `prohibit_login` (fire). |
| `grants.sweep` | `{service:"gitea"}`; the human or the tester. Closes open revokes, settles open rotations again, and retries each `serviceLeft` entry. A fire retry (`tokens`, `login`) first checks the user's id as the revoke close does, and a gone or remade user is skipped with `gitea-user-gone` or `gitea-user-foreign`. Writes `grant.sweep` and `grant.swept {left,skipped,settled,closed}`. |
| `grants.admin.set`, `grants.admin.rotate`, `grants.admin.remove` | The human only (#436b r5 section 3). set takes `{service,url,account,rotateBy,token}`, checks C1 to C4 with the token, then writes the slot under `<configDir>/secrets/grants/<business>/gitea`; a slot already there is `grant-admin-exists`. rotate `{service,rotateBy}` mints a successor admin token and names the old one in `adminTokenLeft`; `{service,finish:true}` deletes it. Rotation voids the approved decisions not yet run, as their `adminSha256` no longer matches. remove drops the slot. Each writes `grant.admin` with no secret. A left admin token that set or remove drops from `adminTokenLeft`, or one shown as `unknown`, has to be deleted by hand at Gitea. A running executor, revoke close or sweep keeps the admin api it started with: rotate voids only decisions not yet run, and remove takes effect from the next run. |
Each of the three leaves one receipt in the organization's `changes.jsonl`: the actor and a
`detail` of `{verb,agent,kind,date or reason}`, never a value. A change to the store alone writes
@@ -236,9 +241,11 @@ a receipt with empty `changes`. A refusal is `action.refused` with its code alon
| `project.list` | No args; human sessions only. The business's projects, oldest first. |
| `thread.create` | `{project,name}`; human sessions only. The thread belongs to the caller and to the business's one thread instance (Projects and threads, below). Returns `{id,instance}`. Refuses `project-not-found`, `thread-exists`, `thread-instance-missing` or `thread-instance-ambiguous`. |
| `thread.list` | `{project?}`; human sessions only. The caller's own threads, oldest first. |
| `thread.open` | `{thread}`; human sessions only. Records a `thread_opens` row, which asks the launcher to start the thread's session. Returns `{opened}`. |
| `thread.send` | `{thread,body,class?}`; human sessions only. A message to the thread's instance, in that thread, with its `human.input` event as `request`. Returns `{id,request}`. |
| `thread.messages` | `{thread,after?,limit?}`; human sessions only. The thread's messages oldest first, after the message `after` names (`message-not-found` when it isn't in the thread), at most `limit` (default 50, 1 to 200). Writes nothing. |
| `thread.open` | `{thread,session?}`; human sessions only. Records a `thread_opens` row, which asks the launcher to start the thread's session. Returns `{opened}`. |
| `thread.send` | `{thread,session?,body,class?}`; human sessions only. A message to the thread's instance, in that thread, with its `human.input` event as `request`. Returns `{id,request}`. |
| `thread.messages` | `{thread,session?,after?,limit?}`; human sessions only. The session's messages oldest first, after the message `after` names (`message-not-found` when it isn't in the session), at most `limit` (default 50, 1 to 200). Writes nothing. |
| `session.list` | `{thread}`; human and tester sessions (row 264a, #1769). The thread's sessions oldest first, `[{id,at,name,predecessor,default}]`. Writes nothing. |
| `session.create` | `{thread,name,op}`; human and tester sessions. A new session of the thread, metadata only. A retry with the same `op` and name returns the first row; another name under that `op` refuses `session-op-conflict`, and a taken name `session-exists`. Returns `{id,thread,name}`. |
### Projects and threads (row 121, #1615)
@@ -306,6 +313,42 @@ the threads they made. A revoked run can't claim its thread again (`run-revoked`
thread run can still be stopped another way: the launcher stops it once the thread is idle, the
human stops it with `mosaic stop <run>`, and the run's end releases its claim. `thread_revoke_needs_decision` guards the table against a direct write.
### Sessions (row 264a, #1769)
Schema `3l` puts sessions below a thread. `sessions` and `session_defaults` are new and
append-only. `thread_claims`, `message_threads` and `thread_opens` each gain a `session`
column, and `thread_claims` gains `generation`, the claim's number in its session counting
from 1. A release or revoke carries its claim's number.
Every thread has a default session, the latest `session_defaults` row (D3). `thread.create`
writes one with a random id. The 3k→3l step gives each existing thread a default session whose
id is the thread's own, named `default` with op `legacy-3l`, and files every old claim, message
and open under it. A thread verb, or a `bindLaunch` record, that names no `session` works the
default, so a client from before 3l keeps working. A `session` from another thread refuses
`session-not-found`, and a non-thread role that names one refuses `thread-not-allowed`.
The thread rules above hold per session. One run holds a session at a time, and a run holds at
most one session in its business, while sibling sessions of a thread may each have a holder.
A thread run receives and replies in its own session, and the chief of staff's reply returns
to the session that sent the work. `threadsState` and `threadIdle` keep one row per thread,
computed on its default session; 264b's launcher starts the others.
A thread run's claim fences by generation (T2). A run acts only while its session's latest claim
row has the generation of the run's own latest claim. After a release or revoke, once another run
claims the session, the old run's `decision.withdraw`, `message.receive` and `message.read`
refuse `not-holder` and write nothing.
`thread.create` writes the default session with op `default`, so a `session.create` retry naming
`default` with op `default` answers with it. A migrated thread's default has op `legacy-3l`, and
the same retry there refuses `session-exists`.
The triggers check that a session has its thread's business, instance and owner, that its
predecessor and any default, claim, message or open name a session of the same thread, and that
a claim's generation is the one its session expects. `delivery_same_session` replaces
`delivery_same_thread`. `sessions_id_not_thread` and `threads_id_not_session` keep a session id
from naming another thread and a thread id from naming a session. A 3k engine refuses a 3l
store with `schema-mismatch`.
### Agent state (row 59, #1548)
For a human or reader session, `agents` derives each held role's state from
@@ -711,10 +754,10 @@ scope probes and rotation/expiry notifications; S2 never probes real services.
## Schema and verification
`schema.sql` is v3j, SHA-256
`0a7b633ce7eec9d0bb37c3255f4244d97bd0c8eac38db91a11b4ef6cc982ec8c`.
It has 18 tables, 72 triggers and five views. Source digest and a trusted full
sqlite_master digest are checked at open, alongside schema version `3j`, WAL,
`schema.sql` is v3l, SHA-256
`cd68512212ce8bf8f5631138b2e63a340fa7b940088430649bb7784af7df162a`.
It has 22 tables, 87 triggers and five views. Source digest and a trusted full
sqlite_master digest are checked at open, alongside schema version `3l`, WAL,
quick_check and foreign-key integrity. Missing metadata refuses; it never
reinitializes an existing file. Full synchronous WAL transactions either commit
or roll back. SQL UPDATE, DELETE and INSERT OR REPLACE refuse on evidence tables.
@@ -882,6 +925,12 @@ step runs in one transaction, and a failure leaves the store at 3i. The 3i fixtu
`tests/fixtures/schema-3i.sql`, is pinned from 8fede5fc6's tree, and `tests/grant-migration.test.mjs`
covers the step and the trigger.
Schema 3k (row 236, #1742, #436b r5 section 7) adds only two tables: `gitea_users`, the Gitea
users the stack made (user, id and the nonce in their full name), and `gitea_grants`, the live Gitea
grants (user, token id and name, projects, rotate-by date and decision), whose key references its
`gitea_users` row. Each write goes in one transaction with a `grant.*` event. The step runs in one
transaction, and a failure leaves the store at 3j. `tests/gitea-migration.test.mjs` covers it.
The managed runner calls usage.begin before an actual harness turn and usage.end
after it, including a failed/stopped turn. Each takes only {turn: deliveredMessageId}.
The broker requires the bound role holder and its delivery, binds run identity,
@@ -935,25 +984,3 @@ narrow cross-instance reach, not reach within one instance.
The Console provenance option trusts verified code and process metadata, not an independent
bus-side OIDC exchange. That trade must be approved, not hidden behind the phrase zero trust.
## Durable instance processing control
`processing.status` is an authorized read. `processing.halt` ({}) and
`processing.resume` ({generation}) require the existing human proof before
validation or effects. They are management verbs independent of channels and
never tester or role grants. The strict state under the resolved dataRoot's
control/ directory is authoritative; reserved processing.halt/resume/stop
actions mirror its durable transitions and write-once stop receipts with
verified human and generation. Generic event writers cannot forge them. No
schema change is needed. Invalid state refuses work and leaves control reads
and human recovery reachable. Halt then resume repairs an invalid leaf; an
unsafe parent is not automatically repaired.
The bus remains a read/control service while new agent work, tracker polling,
notifications and launches pause. Narrow exit cleanup remains possible. Every
managed starter carries a generation and publishes identity before releasing
work. Stops inspect root/boot/start identity or generated container id/labels,
TERM concurrently, then KILL after five seconds and observe for two more.
Unknown targets remain visible and prevent resume. Existing run evidence is
not synthesized or rewritten. First activation stops old processing before
trusted initialization and aligns the copy and image in the human's window.
+95 -11
View File
@@ -248,11 +248,34 @@ CREATE TABLE threads (
name TEXT NOT NULL,
UNIQUE (project, name)
) STRICT;
CREATE TABLE sessions (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
instance TEXT NOT NULL,
owner TEXT NOT NULL,
name TEXT NOT NULL,
op TEXT NOT NULL,
predecessor TEXT REFERENCES sessions(id),
UNIQUE (thread, name),
UNIQUE (thread, op)
) STRICT;
CREATE TABLE session_defaults (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
session TEXT NOT NULL REFERENCES sessions(id),
by TEXT NOT NULL
) STRICT;
CREATE TABLE thread_claims (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
session TEXT NOT NULL REFERENCES sessions(id),
generation INTEGER NOT NULL CHECK (generation >= 1),
op TEXT NOT NULL CHECK (op IN ('claim','release','revoke')),
holder_run TEXT NOT NULL,
harness TEXT NOT NULL, address TEXT,
@@ -261,55 +284,95 @@ CREATE TABLE thread_claims (
) STRICT;
CREATE TABLE message_threads (
message TEXT PRIMARY KEY REFERENCES messages(id),
thread TEXT NOT NULL REFERENCES threads(id)
thread TEXT NOT NULL REFERENCES threads(id),
session TEXT NOT NULL REFERENCES sessions(id)
) STRICT;
CREATE TABLE thread_opens (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
session TEXT NOT NULL REFERENCES sessions(id),
by TEXT NOT NULL
) STRICT;
CREATE INDEX thread_claims_thread ON thread_claims (thread, seq);
CREATE INDEX thread_claims_session ON thread_claims (session, seq);
CREATE INDEX message_threads_thread ON message_threads (thread);
CREATE INDEX message_threads_session ON message_threads (session);
CREATE INDEX session_defaults_thread ON session_defaults (thread, seq);
CREATE TRIGGER threads_project_business BEFORE INSERT ON threads
WHEN (SELECT business FROM projects WHERE id = NEW.project) IS NOT NEW.business
BEGIN SELECT RAISE(ABORT, 'a thread belongs to its project''s business'); END;
CREATE TRIGGER sessions_thread_match BEFORE INSERT ON sessions
WHEN NOT EXISTS (SELECT 1 FROM threads t WHERE t.id = NEW.thread AND t.business = NEW.business AND t.instance = NEW.instance AND t.owner = NEW.owner)
BEGIN SELECT RAISE(ABORT, 'a session has its thread''s business, instance and owner'); END;
CREATE TRIGGER sessions_predecessor BEFORE INSERT ON sessions
WHEN NEW.predecessor IS NOT NULL AND (SELECT thread FROM sessions WHERE id = NEW.predecessor) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'a session''s predecessor is in its thread'); END;
CREATE TRIGGER sessions_id_not_thread BEFORE INSERT ON sessions
WHEN NEW.id IS NOT NEW.thread AND EXISTS (SELECT 1 FROM threads WHERE id = NEW.id)
BEGIN SELECT RAISE(ABORT, 'a session id names another thread'); END;
CREATE TRIGGER threads_id_not_session BEFORE INSERT ON threads
WHEN EXISTS (SELECT 1 FROM sessions WHERE id = NEW.id)
BEGIN SELECT RAISE(ABORT, 'a thread id names a session'); END;
CREATE TRIGGER session_defaults_match BEFORE INSERT ON session_defaults
WHEN (SELECT thread FROM sessions WHERE id = NEW.session) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'a default session is in its thread'); END;
CREATE TRIGGER thread_one_holder BEFORE INSERT ON thread_claims
WHEN NEW.op = 'claim' AND (SELECT op FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1) = 'claim'
BEGIN SELECT RAISE(ABORT, 'thread already held'); END;
WHEN NEW.op = 'claim' AND (SELECT op FROM thread_claims WHERE session = NEW.session ORDER BY seq DESC LIMIT 1) = 'claim'
BEGIN SELECT RAISE(ABORT, 'session already held'); END;
CREATE TRIGGER thread_one_per_run BEFORE INSERT ON thread_claims
WHEN NEW.op = 'claim' AND EXISTS (SELECT 1 FROM thread_claims c WHERE c.holder_run = NEW.holder_run AND c.business = NEW.business
AND c.seq = (SELECT max(seq) FROM thread_claims WHERE thread = c.thread) AND c.op = 'claim')
BEGIN SELECT RAISE(ABORT, 'run already holds a thread'); END;
AND c.seq = (SELECT max(seq) FROM thread_claims WHERE session = c.session) AND c.op = 'claim')
BEGIN SELECT RAISE(ABORT, 'run already holds a session'); END;
CREATE TRIGGER thread_release_by_holder BEFORE INSERT ON thread_claims
WHEN NEW.op IN ('release','revoke') AND COALESCE((SELECT op FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1),'') <> 'claim'
BEGIN SELECT RAISE(ABORT, 'thread is not held'); END;
WHEN NEW.op IN ('release','revoke') AND COALESCE((SELECT op FROM thread_claims WHERE session = NEW.session ORDER BY seq DESC LIMIT 1),'') <> 'claim'
BEGIN SELECT RAISE(ABORT, 'session is not held'); END;
CREATE TRIGGER thread_release_same_run BEFORE INSERT ON thread_claims
WHEN NEW.op = 'release' AND (SELECT holder_run FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1) <> NEW.holder_run
WHEN NEW.op = 'release' AND (SELECT holder_run FROM thread_claims WHERE session = NEW.session ORDER BY seq DESC LIMIT 1) <> NEW.holder_run
BEGIN SELECT RAISE(ABORT, 'only the holder releases; others revoke'); END;
CREATE TRIGGER thread_claim_generation BEFORE INSERT ON thread_claims
WHEN NEW.generation IS NOT (CASE WHEN NEW.op = 'claim'
THEN 1 + (SELECT count(*) FROM thread_claims WHERE session = NEW.session AND op = 'claim')
ELSE (SELECT generation FROM thread_claims WHERE session = NEW.session ORDER BY seq DESC LIMIT 1) END)
BEGIN SELECT RAISE(ABORT, 'a claim row names its session''s generation'); END;
CREATE TRIGGER thread_revoke_needs_decision BEFORE INSERT ON thread_claims
WHEN NEW.op = 'revoke' AND NEW.decision IS NULL AND NEW.reason IS NULL
BEGIN SELECT RAISE(ABORT, 'revoke needs a resolved decision or a reason'); END;
CREATE TRIGGER thread_claims_business BEFORE INSERT ON thread_claims
WHEN (SELECT business FROM threads WHERE id = NEW.thread) IS NOT NEW.business
BEGIN SELECT RAISE(ABORT, 'a thread claim names the thread''s business'); END;
CREATE TRIGGER thread_claims_session BEFORE INSERT ON thread_claims
WHEN (SELECT thread FROM sessions WHERE id = NEW.session) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'a claim''s session is in its thread'); END;
CREATE TRIGGER message_threads_match BEFORE INSERT ON message_threads
WHEN NOT EXISTS (SELECT 1 FROM messages m JOIN threads t ON t.id = NEW.thread
WHERE m.id = NEW.message AND m.business = t.business AND t.instance IN (m.to_role, m.from_role))
BEGIN SELECT RAISE(ABORT, 'a threaded message is to or from the thread''s instance'); END;
CREATE TRIGGER delivery_same_thread BEFORE INSERT ON deliveries
CREATE TRIGGER message_threads_session BEFORE INSERT ON message_threads
WHEN (SELECT thread FROM sessions WHERE id = NEW.session) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'a threaded message''s session is in its thread'); END;
CREATE TRIGGER thread_opens_session BEFORE INSERT ON thread_opens
WHEN (SELECT thread FROM sessions WHERE id = NEW.session) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'an open''s session is in its thread'); END;
CREATE TRIGGER delivery_same_session BEFORE INSERT ON deliveries
WHEN NEW.op IN ('routed','delivered') AND (NEW.op = 'delivered' OR NEW.holder_run IS NOT NULL)
AND EXISTS (SELECT 1 FROM message_threads mt JOIN messages m ON m.id = mt.message JOIN threads t ON t.id = mt.thread
WHERE mt.message = NEW.message AND m.to_role = t.instance
AND NOT EXISTS (SELECT 1 FROM thread_claims c WHERE c.seq = (SELECT max(seq) FROM thread_claims WHERE thread = t.id)
AND NOT EXISTS (SELECT 1 FROM thread_claims c WHERE c.seq = (SELECT max(seq) FROM thread_claims WHERE session = mt.session)
AND c.op = 'claim' AND c.holder_run IS NEW.holder_run))
BEGIN SELECT RAISE(ABORT, 'a threaded message goes only to its thread''s holder'); END;
BEGIN SELECT RAISE(ABORT, 'a threaded message goes only to its session''s holder'); END;
CREATE TRIGGER projects_no_update BEFORE UPDATE ON projects BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER projects_no_delete BEFORE DELETE ON projects BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER projects_no_replace BEFORE INSERT ON projects WHEN EXISTS (SELECT 1 FROM projects WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER threads_no_update BEFORE UPDATE ON threads BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER threads_no_delete BEFORE DELETE ON threads BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER threads_no_replace BEFORE INSERT ON threads WHEN EXISTS (SELECT 1 FROM threads WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER sessions_no_update BEFORE UPDATE ON sessions BEGIN SELECT RAISE(ABORT, 'sessions is append-only'); END;
CREATE TRIGGER sessions_no_delete BEFORE DELETE ON sessions BEGIN SELECT RAISE(ABORT, 'sessions is append-only'); END;
CREATE TRIGGER sessions_no_replace BEFORE INSERT ON sessions WHEN EXISTS (SELECT 1 FROM sessions WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'sessions is append-only'); END;
CREATE TRIGGER session_defaults_no_update BEFORE UPDATE ON session_defaults BEGIN SELECT RAISE(ABORT, 'session_defaults is append-only'); END;
CREATE TRIGGER session_defaults_no_delete BEFORE DELETE ON session_defaults BEGIN SELECT RAISE(ABORT, 'session_defaults is append-only'); END;
CREATE TRIGGER session_defaults_no_replace BEFORE INSERT ON session_defaults WHEN EXISTS (SELECT 1 FROM session_defaults WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'session_defaults is append-only'); END;
CREATE TRIGGER thread_claims_no_update BEFORE UPDATE ON thread_claims BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER thread_claims_no_delete BEFORE DELETE ON thread_claims BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER thread_claims_no_replace BEFORE INSERT ON thread_claims WHEN EXISTS (SELECT 1 FROM thread_claims WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
@@ -394,3 +457,24 @@ CREATE TRIGGER events_grant_actor BEFORE INSERT ON events
AND (json_extract(NEW.body, '$.action') GLOB 'grant.*' OR json_extract(NEW.body, '$.operation') GLOB 'grant.*')
AND NOT (NEW.actor_role IS NULL AND NEW.actor_run IS 'grant')
BEGIN SELECT RAISE(ABORT, 'only the grant writer records grant events'); END;
CREATE TABLE gitea_users (
business TEXT NOT NULL,
agent TEXT NOT NULL,
user TEXT NOT NULL,
id INTEGER NOT NULL CHECK (id > 0),
nonce TEXT NOT NULL CHECK (length(nonce) = 16 AND NOT nonce GLOB '*[^0-9a-f]*'),
decision TEXT NOT NULL REFERENCES decisions(id),
PRIMARY KEY (business, agent)
) STRICT;
CREATE TABLE gitea_grants (
business TEXT NOT NULL,
agent TEXT NOT NULL,
user TEXT NOT NULL,
token_id INTEGER NOT NULL CHECK (token_id > 0),
token_name TEXT NOT NULL,
projects TEXT NOT NULL CHECK (json_valid(projects) AND json_type(projects) = 'array' AND json_array_length(projects) > 0),
rotate_by TEXT NOT NULL,
decision TEXT NOT NULL REFERENCES decisions(id),
PRIMARY KEY (business, agent),
FOREIGN KEY (business, agent) REFERENCES gitea_users(business, agent)
) STRICT;
+1175 -179
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -27,7 +27,7 @@ export class Client {
socket.destroy();
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.setTimeout(this.timeout, () => end(new BusError('outcome-unknown')));
socket.on('connect', () => socket.write(request));
socket.on('error', () => end(new BusError('outcome-unknown')));
socket.on('end', () => end(new BusError('outcome-unknown')));
+65 -5
View File
@@ -116,20 +116,26 @@ export class Credentials {
#revoked = new Set();
// Row 210 (design D5, D9): instance:host.<id> -> the AbortController of the call in flight.
#inflight = new Map();
// Row 236 (#436b section 5): instance:service entries whose file memory never loads, the
// grant-token-unknown pairs. A revoke or a new issue clears one.
#held = new Set();
#storeRoot;
#repoRoots;
#dataRoot;
// storeRoot is <configDir>/secrets/agents (design D1); a reference with store: true must name the
// path under it for its own business, agent and service.
constructor({ references = {}, repoRoots = [], dataRoot, env = process.env, storeRoot = null }) {
// path under it for its own business, agent and service. `held` lists "instance:service" keys whose
// reference loads and whose file doesn't (#436b section 5).
constructor({ references = {}, repoRoots = [], dataRoot, env = process.env, storeRoot = null, held = [] }) {
this.#storeRoot = storeRoot;
this.#repoRoots = repoRoots;
this.#dataRoot = dataRoot;
for (const k of held) this.#held.add(k);
try {
for (const [instance, services] of Object.entries(references))
for (const [service, ref] of Object.entries(services)) {
const date = this.#checkRef(instance, service, ref);
this.#refs.set(instance + ':' + service, { ref, date });
if (this.#held.has(instance + ':' + service)) continue;
const value = this.#read(ref, env, service);
if (value !== null) this.#tokens.set(instance + ':' + service, { ...value, date });
}
@@ -319,14 +325,66 @@ export class Credentials {
this.#refs.set(instance + ':' + service, { ref: { service, store: true, file, [service === 'vikunja' ? 'expires' : 'rotateBy']: date }, date });
this.#tokens.set(instance + ':' + service, { ...value, date });
this.#revoked.delete(instance + ':' + service);
this.#held.delete(instance + ':' + service);
return this.status().find((e) => e.instance === instance && e.service === service);
}
// 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) {
const k = instance + ':' + service;
const { ref } = this.#known(instance, service);
if (this.#held.has(k) || !ref.store) deny('credential-reference');
if (!day(date)) deny('credential-date');
const value = this.#read(ref, {}, service);
if (value === null) deny('credential-file');
this.#refs.set(k, { ref: { ...ref, rotateBy: date }, date });
this.#tokens.set(k, { ...value, date });
this.#revoked.delete(k);
}
// Row 236 (#436b section 5, grant-token-unknown): memory drops the token, as revoke's memory step,
// and a call in flight aborts. The file stays for the human to look at, and nothing loads it again
// until a revoke or an issue.
hold(instance, service) {
const k = instance + ':' + service;
this.#known(instance, service);
this.#tokens.delete(k);
this.#held.add(k);
this.#inflight.get(k)?.abort();
}
held(instance, service) {
return this.#held.has(instance + ':' + service);
}
// Row 236 (#436b section 5, settle): `operation` gets the token the store file holds now, read with
// the constructor's checks, or null when there is no file. Memory is untouched. The result is checked
// as use() checks it.
async useFile(instance, service, operation) {
const { ref } = this.#known(instance, service);
if (!ref.store) deny('credential-reference');
const value = this.#read(ref, {}, service);
const result = await operation(value?.token ?? null);
this.assertClean(result);
if (value && JSON.stringify(result)?.includes(value.token)) deny('credential-leak');
return result;
}
// Row 236 (#436b section 5, a first grant's open commit at restart): the store file goes, and with it
// a token no record backs. An entry with a token in memory refuses: only a held entry is undone so.
unlinkStore(instance, service) {
const k = instance + ':' + service;
const { ref } = this.#known(instance, service);
if (this.#tokens.has(k) || !ref.store) deny('credential-reference');
try {
unlinkSync(ref.file);
} catch (e) {
if (e.code !== 'ENOENT') deny('credential-file');
}
}
// Memory first, then the store file (design section 6, D12). A file or env reference outside the
// store is never unlinked; the caller turns its reference into store, so a restart finds nothing.
revoke(instance, service) {
const { ref } = this.#known(instance, service);
this.#tokens.delete(instance + ':' + service);
this.#revoked.add(instance + ':' + service);
this.#held.delete(instance + ':' + service);
// Row 210 (design D9): a host.run in flight for this agent and host is killed first.
this.#inflight.get(instance + ':' + service)?.abort();
if (ref.store) {
@@ -425,9 +483,11 @@ export class Credentials {
date,
...(hostKey ? { fingerprint: hostKey.fingerprint, publicKey: hostKey.publicLine } : {}),
state: !this.#tokens.has(key)
? this.#revoked.has(key)
? 'revoked'
: 'not-issued'
? this.#held.has(key)
? 'held'
: this.#revoked.has(key)
? 'revoked'
: 'not-issued'
: service !== 'vikunja'
? left <= 0
? 'rotation-due'
+461
View File
@@ -0,0 +1,461 @@
// Row 236 (#1742, #436b design r5 at 1be535aca): the trusted half of the Gitea grant path, run in the bus
// child. The broker gets it as `giteaGrant` and holds every store write and every step's order. This module
// holds what touches the network or a secret file: the HTTPS calls to Gitea (each made through the
// broker's `log`, which writes grant.call before and grant.result after), the admin slot (section 3c), the
// binding's shape check (section 2), the admin checks C1 to C4 (section 3a), the start pre-load of held
// pairs (section 5) and `giteaKeys`, the broker's narrow handle on Credentials.
//
// The admin token lives at <configDir>/secrets/grants/<business>/gitea/admin, 0600 in 0700 dirs, with
// admin.json next to it ({url, account, rotateBy, setAt}, no secret). Both are written tmp, fsync, rename,
// as row 199's issue writes. Neither value reaches an event, a message or a log.
import { request as httpsRequest } from 'node:https';
import { createHash, randomBytes } from 'node:crypto';
import {
closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, rmdirSync, unlinkSync, writeSync,
} from 'node:fs';
import { join } from 'node:path';
import { BusError, GRANT_EVIDENCE } from './broker.mjs';
import { day, privateDir } from './credentials.mjs';
import { definitionSha256, grantTag } from './host-grant.mjs';
import { canonicalJson } from '../../business/src/util.mjs';
import { forkUrl, giteaName, ID_PATTERN, parseGiteaUrl } from '../../business/src/vocabulary.mjs';
const deny = (code) => {
throw new BusError(code);
};
const sha256 = (value) => createHash('sha256').update(canonicalJson(value)).digest('hex');
// Section 2: the seat token's scopes, fixed by code (row 199 A2). Section 3b: the admin token's, exactly.
// Both sorted, as Gitea reports scopes.
export const SEAT_SCOPES = Object.freeze(['read:user', 'write:repository']);
export const ADMIN_SCOPES = Object.freeze(['write:admin', 'write:repository', 'write:user']);
// F6: the child-scope cap first ships in v1.27.0.
export const MIN_VERSION = Object.freeze([1, 27, 0]);
// Step 5: pages of 50, at most 20 of them.
export const REPO_PAGE = 50;
export const REPO_PAGES = 20;
const TOKEN = /^[-A-Za-z0-9._~]+\n?$/;
const MAX_BODY = 4 * 1024 * 1024;
const CALL_MS = 15000;
const META_KEYS = ['url', 'account', 'rotateBy', 'setAt'];
const AT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/;
// C1: numbers, never strings, so 1.9.0 is below 1.27.0. A prerelease is below its release, so
// 1.27.0-rc0 refuses (F6). Build metadata after "+" is ignored. Anything that doesn't parse refuses.
const VERSION = /^v?(\d{1,6})\.(\d{1,6})\.(\d{1,6})(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$/;
export function versionAtLeast(value, floor = MIN_VERSION) {
const m = typeof value === 'string' ? VERSION.exec(value) : null;
if (!m) return false;
const v = [m[1], m[2], m[3]].map(Number);
for (let i = 0; i < 3; i++) if (v[i] !== floor[i]) return v[i] > floor[i];
return !m[4];
}
// Section 2's binding. Anything off refuses invalid-request; the broker checks the agent, its definition,
// identity, publish list and the admin slot inside the raise transaction. rotateBy is a real calendar day
// after `today`, the bus clock's UTC day: the rule row 199's host grants use (hostGrantCheck), since 199
// names no other rotation window. Returns a fresh object with only the bound keys.
export function giteaGrantCheck(grant, today) {
const allowed = ['method', 'agent', 'rotateBy', 'target'];
if (!grant || typeof grant !== 'object' || Array.isArray(grant)) deny('invalid-request');
const own = Object.keys(grant);
if (own.some((k) => !allowed.includes(k)) || allowed.some((k) => !own.includes(k))) deny('invalid-request');
if (grant.method !== 'gitea') deny('invalid-request');
if (typeof grant.agent !== 'string' || !ID_PATTERN.test(grant.agent)) deny('invalid-request');
const target = grant.target;
if (!target || typeof target !== 'object' || Array.isArray(target) || Object.keys(target).join() !== 'projects') deny('invalid-request');
const projects = target.projects;
if (!Array.isArray(projects) || projects.length < 1 || projects.length > 50) deny('invalid-request');
if (projects.some((p) => typeof p !== 'string' || !ID_PATTERN.test(p)) || new Set(projects).size !== projects.length) deny('invalid-request');
if (!day(grant.rotateBy) || grant.rotateBy <= today) deny('invalid-request');
return { method: 'gitea', agent: grant.agent, target: { projects: [...projects] }, rotateBy: grant.rotateBy };
}
// Section 2: over the agent's identity, its publish list and the remote of every project in it, since
// step 5 checks against all of them. `projects` is the business's project map.
export const identitySha256 = (role, projects) =>
sha256({
identity: role.identity ?? null,
publish: role.publish ?? [],
remotes: Object.fromEntries((role.publish ?? []).map((p) => [p, projects?.[p]?.remote ?? null])),
});
// Section 2: over the admin slot's metadata, never the token.
export const adminSha256 = (meta) => sha256({ url: meta.url, account: meta.account, setAt: meta.setAt });
// Step 3: `mosaic-<tag>-<decision>`. grantTag already starts with "mosaic-".
export const tokenName = (business, agent, decision) => `${grantTag(business, agent, 'gitea')}-${decision}`;
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 }) {
return new Promise((resolve) => {
let done = false,
req = null;
const end = (r) => {
if (done) return;
done = true;
clearTimeout(timer);
resolve(r);
};
const timer = setTimeout(() => {
req?.destroy();
end({ status: 0, json: null, total: null });
}, timeoutMs);
try {
const headers = {
accept: 'application/json',
authorization: auth.basic ? 'Basic ' + Buffer.from(auth.basic.join(':')).toString('base64') : 'token ' + auth.token,
};
const data = body === undefined ? null : Buffer.from(JSON.stringify(body));
if (data) {
headers['content-type'] = 'application/json';
headers['content-length'] = String(data.length);
}
req = httpsRequest(new URL(path, url), { method, headers, ca, agent: false }, (res) => {
const chunks = [];
let size = 0;
res.on('data', (c) => {
size += c.length;
if (size > MAX_BODY) {
req.destroy();
end({ status: 0, json: null, total: null });
} else chunks.push(c);
});
res.on('end', () => {
let json = null;
try {
json = size ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : null;
} catch {}
const t = res.headers['x-total-count'];
end({ status: res.statusCode, json, total: t === undefined || !/^\d+$/.test(t) ? null : Number(t) });
});
res.on('error', () => end({ status: 0, json: null, total: null }));
});
req.on('error', () => end({ status: 0, json: null, total: null }));
req.end(data ?? undefined);
} catch {
end({ status: 0, json: null, total: null });
}
});
}
const segment = (value) => encodeURIComponent(String(value));
// C1 to C4 (section 3a), each call through the api's log. Throws the refusal code. Returns the admin
// token's own name from C3 (F7), which rotate deletes by.
export async function adminChecks(api, account) {
const v = await api.version();
if (v.status !== 200) deny('gitea-failed');
if (!versionAtLeast(v.json?.version)) deny('gitea-too-old');
const self = await api.self();
if (self.status === 401 || self.status === 403) deny('grant-admin-account');
if (self.status !== 200) deny('gitea-failed');
if (self.json?.login !== account || self.json?.is_admin !== true) deny('grant-admin-account');
const t = await api.tokenScopes();
if (t.status !== 200) deny(t.status === 401 || t.status === 403 ? 'grant-admin-account' : 'gitea-failed');
const scopes = t.json?.scopes;
if (!Array.isArray(scopes) || [...scopes].sort().join() !== ADMIN_SCOPES.join()) deny('grant-admin-scope');
const r = await api.reposCheck();
if (r.status !== 200 || !Array.isArray(r.json)) deny('gitea-failed');
if (r.json.length !== 0 || (r.total !== null && r.total !== 0)) deny('grant-admin-not-dedicated');
return typeof t.json?.name === 'string' ? t.json.name : null;
}
// One token file's text, read with Credentials' checks: no symlink, a regular file the stack's user owns,
// 0600, at most 16 KiB, then the token format. null if `missing` and there is no file.
function readSecretFile(file, { missing = false } = {}) {
let fd;
try {
fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW);
} catch (e) {
if (missing && e.code === 'ENOENT') return null;
deny('credential-file');
}
try {
const s = fstatSync(fd);
if (!s.isFile() || s.uid !== process.getuid() || (s.mode & 0o777) !== 0o600 || s.size > 16384) deny('credential-file');
return readFileSync(fd, 'utf8');
} catch (e) {
if (e instanceof BusError) throw e;
deny('credential-file');
} finally {
closeSync(fd);
}
}
export function checkToken(token) {
if (typeof token !== 'string' || token.length > 16384 || !TOKEN.test(token)) deny('credential-format');
const t = token.replace(/\n$/, '');
if (t.length < 16) deny('credential-format');
return t;
}
function writeFileAtomic(dir, name, text) {
const file = join(dir, name);
const tmp = file + '.tmp';
try {
unlinkSync(tmp);
} catch (e) {
if (e.code !== 'ENOENT') deny('credential-file');
}
let fd;
try {
fd = openSync(tmp, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
writeSync(fd, text);
fsyncSync(fd);
} finally {
if (fd !== undefined) closeSync(fd);
}
renameSync(tmp, file);
const dirFd = openSync(dir, constants.O_RDONLY);
try {
fsyncSync(dirFd);
} finally {
closeSync(dirFd);
}
}
// The trusted Gitea half. `ca` is a PEM the HTTPS calls trust instead of the system's, for the tests'
// fake Gitea; production passes none. `send` is replaceable only for the tests' failure rows.
export function giteaGrant({ configDir, ca = undefined, timeoutMs = CALL_MS, transport = send }) {
const grantsRoot = join(configDir, 'secrets', 'grants');
const slotDir = (business) => join(grantsRoot, business, 'gitea');
const slotDirs = (business) => [grantsRoot, join(grantsRoot, business), slotDir(business)];
// Section 3c: the slot's metadata, or null when there is no slot. A slot dir or file that isn't the
// stack user's own and private refuses credential-file, as does metadata of any other shape.
const adminMeta = (business) => {
for (const dir of slotDirs(business)) if (!privateDir(dir, { missing: true })) return null;
const text = readSecretFile(join(slotDir(business), 'admin.json'), { missing: true });
if (text === null) return null;
let meta;
try {
meta = JSON.parse(text);
} catch {
deny('credential-file');
}
if (
!meta || typeof meta !== 'object' || Array.isArray(meta) ||
Object.keys(meta).sort().join() !== [...META_KEYS].sort().join() ||
!parseGiteaUrl(meta.url).url || !giteaName(meta.account) || !day(meta.rotateBy) ||
typeof meta.setAt !== 'string' || !AT.test(meta.setAt)
)
deny('credential-file');
return { url: meta.url, account: meta.account, rotateBy: meta.rotateBy, setAt: meta.setAt };
};
const adminToken = (business) => {
for (const dir of slotDirs(business)) privateDir(dir);
return checkToken(readSecretFile(join(slotDir(business), 'admin')));
};
// The admin api over one token. Basic on the token routes (F1), the `token` header elsewhere. Every
// call goes through log(op, run, after).
const adminApi = (url, account, token, log) => {
const header = { token };
const basic = { basic: [account, token] };
const go = (op, method, path, auth, body, after) => log(op, () => transport({ url, method, path, auth, body, ca, timeoutMs }), after);
return {
version: () => go('version', 'GET', '/api/v1/version', header),
self: () => go('self', 'GET', '/api/v1/user', header),
tokenScopes: () => go('token-scopes', 'GET', '/api/v1/token', header),
reposCheck: () => go('repos-check', 'GET', '/api/v1/user/repos?limit=1&page=1', header),
userGet: (user) => go('user-get', 'GET', `/api/v1/users/${segment(user)}`, header),
userCreate: (body, after) => go('user-create', 'POST', '/api/v1/admin/users', header, body, after),
userPatch: (user, body) => go('user-patch', 'PATCH', `/api/v1/admin/users/${segment(user)}`, header, body),
tokenList: (user, page) => go('token-list', 'GET', `/api/v1/users/${segment(user)}/tokens?limit=50&page=${page}`, basic),
tokenCreate: (user, name, scopes = SEAT_SCOPES, op = 'token-create') =>
go(op, 'POST', `/api/v1/users/${segment(user)}/tokens`, basic, { name, scopes: [...scopes] }),
tokenDelete: (user, idOrName) => go('token-delete', 'DELETE', `/api/v1/users/${segment(user)}/tokens/${segment(idOrName)}`, basic),
};
};
return {
giteaGrantCheck,
identitySha256,
adminSha256,
definitionSha256,
tokenName,
adminMeta,
openGrants: openGiteaGrants,
tokenUnknown,
checkToken,
forkUrl,
repoPage: REPO_PAGE,
repoPages: REPO_PAGES,
// The admin api over the slot's token, for a grant, an undo, a revoke or a sweep. Refuses
// grant-admin-missing with no slot.
admin(business, log) {
const meta = adminMeta(business);
if (!meta) deny('grant-admin-missing');
return { meta, api: adminApi(meta.url, meta.account, adminToken(business), log) };
},
adminChecks,
// The seat token's calls, made with the minted token (steps 4 and 5) or the store file's (settle).
seat(url, token, log) {
const header = { token };
const go = (op, method, path, body) => log(op, () => transport({ url, method, path, auth: header, body, ca, timeoutMs }));
return {
self: () => go('seat-self', 'GET', '/api/v1/user'),
repos: (page) => go('seat-repos', 'GET', `/api/v1/user/repos?limit=${REPO_PAGE}&page=${page}`),
forkGet: (owner, repo) => go('fork-get', 'GET', `/api/v1/repos/${segment(owner)}/${segment(repo)}`),
fork: (owner, repo) => go('fork', 'POST', `/api/v1/repos/${segment(owner)}/${segment(repo)}/forks`, {}),
whoami: () => go('token-whoami', 'GET', '/api/v1/token'),
};
},
// Section 3e: `set` checks C1 to C4 with the given token before it writes anything. A slot already
// there refuses grant-admin-exists, whatever its url (remove first). Returns the metadata written.
async setAdmin(business, { url, account, rotateBy, token, setAt }, log) {
const t = checkToken(token);
if (adminMeta(business)) deny('grant-admin-exists');
await adminChecks(adminApi(url, account, t, log), account);
if (adminMeta(business)) deny('grant-admin-exists');
const meta = { url, account, rotateBy, setAt };
this.writeAdmin(business, t, meta);
return meta;
},
// The slot's write: the token, then admin.json, each tmp, fsync, rename. Dirs are made 0700.
writeAdmin(business, token, meta) {
mkdirSync(join(configDir, 'secrets'), { recursive: true, mode: 0o700 });
for (const dir of slotDirs(business)) {
if (!privateDir(dir, { missing: true })) mkdirSync(dir, { mode: 0o700 });
privateDir(dir);
}
writeFileAtomic(slotDir(business), 'admin', checkToken(token) + '\n');
writeFileAtomic(slotDir(business), 'admin.json', JSON.stringify(meta) + '\n');
},
// Section 3d rotate: C1 to C4, the successor minted for the admin account itself and named by the
// UTC second, C2 and C3 with it, then the write. The old token is deleted by name with the new one;
// `left` names it if that fails. A name already taken (two rotations in one second) refuses before
// anything is written. `stamp` is yyyymmddThhmmssZ.
async rotateAdmin(business, { rotateBy, setAt, stamp }, log) {
const { meta, api } = this.admin(business, log);
const oldName = await adminChecks(api, meta.account);
const name = `mosaic-grant-${stamp}`;
if (name === oldName) deny('grant-admin-name-taken');
const r = await api.tokenCreate(meta.account, name, ADMIN_SCOPES, 'admin-rotate');
if (r.status === 400 || r.status === 422) deny('grant-admin-name-taken');
if (r.status !== 201 || typeof r.json?.sha1 !== 'string') deny('gitea-failed');
const successor = checkToken(r.json.sha1);
const next = adminApi(meta.url, meta.account, successor, log);
const self = await next.self();
if (self.status !== 200 || self.json?.login !== meta.account || self.json?.is_admin !== true) deny('grant-admin-account');
const t = await next.tokenScopes();
if (t.status !== 200 || !Array.isArray(t.json?.scopes) || [...t.json.scopes].sort().join() !== ADMIN_SCOPES.join()) deny('grant-admin-scope');
const written = { url: meta.url, account: meta.account, rotateBy, setAt };
this.writeAdmin(business, successor, written);
let left = null;
if (oldName === null) left = 'unknown';
else {
const d = await next.tokenDelete(meta.account, oldName);
if (d.status !== 204 && d.status !== 404) left = oldName;
}
return { meta: written, tokenName: name, adminTokenLeft: left };
},
// rotate --finish: the old token by name, with the slot's token.
async finishRotate(business, oldName, log) {
const { meta, api } = this.admin(business, log);
await adminChecks(api, meta.account);
const d = await api.tokenDelete(meta.account, oldName);
if (d.status !== 204 && d.status !== 404) deny('gitea-failed');
},
// Section 3d remove: the slot's files and dir go. No network call.
removeAdmin(business) {
if (!adminMeta(business)) deny('grant-admin-missing');
for (const name of ['admin', 'admin.json', 'admin.tmp', 'admin.json.tmp'])
try {
unlinkSync(join(slotDir(business), name));
} catch (e) {
if (e.code !== 'ENOENT') deny('credential-file');
}
try {
rmdirSync(slotDir(business));
} catch {
deny('credential-file');
}
},
// Step 2: the never-stored password of a new user and the nonce in its full_name.
password: () => randomBytes(24).toString('base64url'),
nonce: () => randomBytes(8).toString('hex'),
};
}
// The store reference for an agent's gitea token, as Credentials checks it.
export const giteaReference = (storeRoot, business, agent, rotateBy) => ({
store: true,
file: storeRoot ? join(storeRoot, business, agent, 'gitea') : null,
rotateBy,
});
// The grant executor's calls on Credentials, keyed by (business, agent). The broker gets this and never
// Credentials itself, so it still has no use(); useFile hands the file's token only to a settle call.
export function giteaKeys({ credentials, storeRoot }) {
const at = (business, agent) => business + '/' + agent;
return {
claim: (business, agent) => credentials.claim(at(business, agent), 'gitea'),
release: (business, agent, signal) => credentials.release(at(business, agent), 'gitea', signal),
abortClaim: (business, agent) => credentials.abortClaim(at(business, agent), 'gitea'),
known: (business, agent) => credentials.status().some((e) => e.instance === at(business, agent) && e.service === 'gitea'),
register: (business, agent, rotateBy) => credentials.register(at(business, agent), 'gitea', giteaReference(storeRoot, business, agent, rotateBy)),
unregister: (business, agent) => credentials.unregister(at(business, agent), 'gitea'),
issue: (business, agent, token, rotateBy, commit) => credentials.issue(at(business, agent), 'gitea', token, rotateBy, commit),
revoke: (business, agent) => credentials.revoke(at(business, agent), 'gitea'),
reload: (business, agent, rotateBy) => credentials.reload(at(business, agent), 'gitea', rotateBy),
hold: (business, agent) => credentials.hold(at(business, agent), 'gitea'),
held: (business, agent) => credentials.held(at(business, agent), 'gitea'),
useFile: (business, agent, fn) => credentials.useFile(at(business, agent), 'gitea', fn),
unlinkStore: (business, agent) => credentials.unlinkStore(at(business, agent), 'gitea'),
};
}
// Section 5, before Credentials loads, read-only on the store: each gitea_grants record gives its agent
// a store reference to gitea, and `held` lists the pairs whose file memory must not load. Those are an
// agent whose last gitea grant.failed is grant-token-unknown with no grant.revoked after it, and an
// agent with an open first grant whose last step is commit: its file may hold a token no record backs,
// and the broker's reconcile unlinks it. Only grant evidence counts (duckworth-0067 N1). `references`
// is changed in place: a record's reference replaces a business-file gitea reference, which names the
// same store path or a file the record supersedes.
export function giteaStart(store, storeRoot, businesses, references) {
const held = [];
for (const g of store.all('SELECT business, agent, rotate_by FROM gitea_grants ORDER BY business, agent')) {
if (!Object.hasOwn(businesses, g.business)) continue;
const refs = (references[g.business + '/' + g.agent] = { ...(references[g.business + '/' + g.agent] ?? {}) });
refs.gitea = giteaReference(storeRoot, g.business, g.agent, g.rotate_by);
}
for (const business of Object.keys(businesses)) {
for (const agent of tokenUnknown(store, business).keys()) held.push(business + '/' + agent + ':gitea');
for (const o of openGiteaGrants(store, business))
if (o.step?.step === 'commit' && o.step.previousTokenId === null) held.push(business + '/' + o.agent + ':gitea');
}
return { held: [...new Set(held)] };
}
const evidence = (store, business, where, ...args) =>
store.all(`SELECT seq, body FROM events WHERE business=? AND ${GRANT_EVIDENCE} AND ${where} ORDER BY seq`, business, ...args)
.map((e) => ({ seq: e.seq, body: JSON.parse(e.body) }));
// agent -> {decision, seq}: the agents whose last gitea grant.failed is grant-token-unknown with no gitea
// grant.revoked after it (section 5's held row).
export function tokenUnknown(store, business) {
const last = new Map();
for (const e of evidence(store, business, "json_extract(body,'$.method')='gitea' AND json_extract(body,'$.action') IN ('grant.failed','grant.revoked')"))
if (e.body.action === 'grant.revoked') last.delete(e.body.agent);
else if (e.body.code === 'grant-token-unknown') last.set(e.body.agent, { decision: e.body.grantDecision, seq: e.seq });
return last;
}
// Every gitea grant spent with no grant.applied or grant.failed after its spend: {decision, agent, step,
// seq, revokedAfter}. step is the last grant.step body or null, seq that step's or the spend's, and
// revokedAfter says a gitea grant.revoked of the agent came after it (N9). A step 0 grant.failed comes
// before the spend, so only ends after it count.
export function openGiteaGrants(store, business) {
const out = [];
for (const spend of evidence(store, business, "json_extract(body,'$.operation')='grant.gitea'")) {
const decision = spend.body.decision;
const mine = evidence(store, business, "json_extract(body,'$.grantDecision')=? AND seq > ?", decision, spend.seq);
if (mine.some((e) => e.body.action === 'grant.applied' || e.body.action === 'grant.failed')) continue;
const last = mine.filter((e) => e.body.action === 'grant.step').at(-1) ?? null;
out.push({ decision, agent: last?.body.agent ?? null, step: last?.body ?? null, seq: last?.seq ?? spend.seq });
}
for (const o of out)
o.revokedAfter =
o.agent !== null &&
evidence(store, business, "json_extract(body,'$.method')='gitea' AND json_extract(body,'$.action')='grant.revoked' AND json_extract(body,'$.agent')=? AND seq > ?", o.agent, o.seq)
.length > 0;
return out;
}
+3 -2
View File
@@ -25,8 +25,9 @@ try {
const client = new Client({
path: process.argv[2],
// Row 254: library.sync fetches for up to 100 s and the server holds its socket for 120 s,
// so wait past that for the server's own answer. Every other verb keeps the 5 s default.
timeout: request.verb === 'library.sync' ? 130_000 : undefined,
// so wait past that for the server's own answer. Row 236: the grants verbs call Gitea and get the
// same wait. Every other verb keeps the 5 s default.
timeout: request.verb === 'library.sync' || request.verb.startsWith('grants.') ? 130_000 : undefined,
human: {
business: request.business,
pid: me.pid,
+24
View File
@@ -0,0 +1,24 @@
-- Row 236 (#436b, #1742, design r5 section 7): the Gitea users this stack made and the Gitea grants
-- live in the store. The grant executor, revoke and sweep are the only writers, each in the
-- transaction of its event. Only new tables; the grant event trigger from 3j covers their events.
CREATE TABLE gitea_users (
business TEXT NOT NULL,
agent TEXT NOT NULL,
user TEXT NOT NULL,
id INTEGER NOT NULL CHECK (id > 0),
nonce TEXT NOT NULL CHECK (length(nonce) = 16 AND NOT nonce GLOB '*[^0-9a-f]*'),
decision TEXT NOT NULL REFERENCES decisions(id),
PRIMARY KEY (business, agent)
) STRICT;
CREATE TABLE gitea_grants (
business TEXT NOT NULL,
agent TEXT NOT NULL,
user TEXT NOT NULL,
token_id INTEGER NOT NULL CHECK (token_id > 0),
token_name TEXT NOT NULL,
projects TEXT NOT NULL CHECK (json_valid(projects) AND json_type(projects) = 'array' AND json_array_length(projects) > 0),
rotate_by TEXT NOT NULL,
decision TEXT NOT NULL REFERENCES decisions(id),
PRIMARY KEY (business, agent),
FOREIGN KEY (business, agent) REFERENCES gitea_users(business, agent)
) STRICT;
+168
View File
@@ -0,0 +1,168 @@
-- Row 264a (#1769, T1 item 7): sessions below a thread. Each existing thread gets one legacy default
-- session whose id is the thread id, and its claims, messages and opens name that session (D9's
-- mapping). thread_claims, message_threads and thread_opens are rebuilt with a session column, and
-- thread_claims gains generation, the claim's number in its session. Rows are copied with their seq
-- values and old columns as they were, and the sequences carry over, as 3h-3i did. The guards and the
-- delivery trigger that read the old tables are dropped first, so the renames rewrite nothing.
DROP TRIGGER thread_one_holder;
DROP TRIGGER thread_one_per_run;
DROP TRIGGER thread_release_by_holder;
DROP TRIGGER thread_release_same_run;
DROP TRIGGER thread_revoke_needs_decision;
DROP TRIGGER thread_claims_business;
DROP TRIGGER message_threads_match;
DROP TRIGGER delivery_same_thread;
DROP TRIGGER thread_claims_no_update;
DROP TRIGGER thread_claims_no_delete;
DROP TRIGGER thread_claims_no_replace;
DROP TRIGGER message_threads_no_update;
DROP TRIGGER message_threads_no_delete;
DROP TRIGGER message_threads_no_replace;
DROP TRIGGER thread_opens_no_update;
DROP TRIGGER thread_opens_no_delete;
DROP TRIGGER thread_opens_no_replace;
DROP INDEX thread_claims_thread;
DROP INDEX message_threads_thread;
ALTER TABLE thread_claims RENAME TO thread_claims_3k;
ALTER TABLE message_threads RENAME TO message_threads_3k;
ALTER TABLE thread_opens RENAME TO thread_opens_3k;
CREATE TABLE sessions (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
instance TEXT NOT NULL,
owner TEXT NOT NULL,
name TEXT NOT NULL,
op TEXT NOT NULL,
predecessor TEXT REFERENCES sessions(id),
UNIQUE (thread, name),
UNIQUE (thread, op)
) STRICT;
CREATE TABLE session_defaults (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
session TEXT NOT NULL REFERENCES sessions(id),
by TEXT NOT NULL
) STRICT;
INSERT INTO sessions(at,business,thread,id,instance,owner,name,op,predecessor)
SELECT at,business,id,id,instance,owner,'default','legacy-3l',NULL FROM threads ORDER BY seq;
INSERT INTO session_defaults(at,thread,session,by) SELECT at,id,id,'migration-3l' FROM threads ORDER BY seq;
CREATE TABLE thread_claims (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
session TEXT NOT NULL REFERENCES sessions(id),
generation INTEGER NOT NULL CHECK (generation >= 1),
op TEXT NOT NULL CHECK (op IN ('claim','release','revoke')),
holder_run TEXT NOT NULL,
harness TEXT NOT NULL, address TEXT,
by TEXT NOT NULL, reason TEXT,
decision TEXT
) STRICT;
CREATE TABLE message_threads (
message TEXT PRIMARY KEY REFERENCES messages(id),
thread TEXT NOT NULL REFERENCES threads(id),
session TEXT NOT NULL REFERENCES sessions(id)
) STRICT;
CREATE TABLE thread_opens (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
session TEXT NOT NULL REFERENCES sessions(id),
by TEXT NOT NULL
) STRICT;
INSERT INTO thread_claims(seq,at,business,thread,session,generation,op,holder_run,harness,address,by,reason,decision)
SELECT c.seq,c.at,c.business,c.thread,c.thread,
(SELECT count(*) FROM thread_claims_3k p WHERE p.thread = c.thread AND p.op = 'claim' AND p.seq <= c.seq),
c.op,c.holder_run,c.harness,c.address,c.by,c.reason,c.decision FROM thread_claims_3k c ORDER BY c.seq;
INSERT INTO message_threads(message,thread,session) SELECT message,thread,thread FROM message_threads_3k ORDER BY rowid;
INSERT INTO thread_opens(seq,at,thread,session,by) SELECT seq,at,thread,thread,by FROM thread_opens_3k ORDER BY seq;
DELETE FROM sqlite_sequence WHERE name IN ('thread_claims','thread_opens');
INSERT INTO sqlite_sequence(name,seq) SELECT 'thread_claims', seq FROM sqlite_sequence WHERE name='thread_claims_3k';
INSERT INTO sqlite_sequence(name,seq) SELECT 'thread_opens', seq FROM sqlite_sequence WHERE name='thread_opens_3k';
DROP TABLE thread_claims_3k;
DROP TABLE message_threads_3k;
DROP TABLE thread_opens_3k;
CREATE INDEX thread_claims_thread ON thread_claims (thread, seq);
CREATE INDEX thread_claims_session ON thread_claims (session, seq);
CREATE INDEX message_threads_thread ON message_threads (thread);
CREATE INDEX message_threads_session ON message_threads (session);
CREATE INDEX session_defaults_thread ON session_defaults (thread, seq);
CREATE TRIGGER sessions_thread_match BEFORE INSERT ON sessions
WHEN NOT EXISTS (SELECT 1 FROM threads t WHERE t.id = NEW.thread AND t.business = NEW.business AND t.instance = NEW.instance AND t.owner = NEW.owner)
BEGIN SELECT RAISE(ABORT, 'a session has its thread''s business, instance and owner'); END;
CREATE TRIGGER sessions_predecessor BEFORE INSERT ON sessions
WHEN NEW.predecessor IS NOT NULL AND (SELECT thread FROM sessions WHERE id = NEW.predecessor) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'a session''s predecessor is in its thread'); END;
CREATE TRIGGER sessions_id_not_thread BEFORE INSERT ON sessions
WHEN NEW.id IS NOT NEW.thread AND EXISTS (SELECT 1 FROM threads WHERE id = NEW.id)
BEGIN SELECT RAISE(ABORT, 'a session id names another thread'); END;
CREATE TRIGGER threads_id_not_session BEFORE INSERT ON threads
WHEN EXISTS (SELECT 1 FROM sessions WHERE id = NEW.id)
BEGIN SELECT RAISE(ABORT, 'a thread id names a session'); END;
CREATE TRIGGER session_defaults_match BEFORE INSERT ON session_defaults
WHEN (SELECT thread FROM sessions WHERE id = NEW.session) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'a default session is in its thread'); END;
CREATE TRIGGER thread_one_holder BEFORE INSERT ON thread_claims
WHEN NEW.op = 'claim' AND (SELECT op FROM thread_claims WHERE session = NEW.session ORDER BY seq DESC LIMIT 1) = 'claim'
BEGIN SELECT RAISE(ABORT, 'session already held'); END;
CREATE TRIGGER thread_one_per_run BEFORE INSERT ON thread_claims
WHEN NEW.op = 'claim' AND EXISTS (SELECT 1 FROM thread_claims c WHERE c.holder_run = NEW.holder_run AND c.business = NEW.business
AND c.seq = (SELECT max(seq) FROM thread_claims WHERE session = c.session) AND c.op = 'claim')
BEGIN SELECT RAISE(ABORT, 'run already holds a session'); END;
CREATE TRIGGER thread_release_by_holder BEFORE INSERT ON thread_claims
WHEN NEW.op IN ('release','revoke') AND COALESCE((SELECT op FROM thread_claims WHERE session = NEW.session ORDER BY seq DESC LIMIT 1),'') <> 'claim'
BEGIN SELECT RAISE(ABORT, 'session is not held'); END;
CREATE TRIGGER thread_release_same_run BEFORE INSERT ON thread_claims
WHEN NEW.op = 'release' AND (SELECT holder_run FROM thread_claims WHERE session = NEW.session ORDER BY seq DESC LIMIT 1) <> NEW.holder_run
BEGIN SELECT RAISE(ABORT, 'only the holder releases; others revoke'); END;
CREATE TRIGGER thread_claim_generation BEFORE INSERT ON thread_claims
WHEN NEW.generation IS NOT (CASE WHEN NEW.op = 'claim'
THEN 1 + (SELECT count(*) FROM thread_claims WHERE session = NEW.session AND op = 'claim')
ELSE (SELECT generation FROM thread_claims WHERE session = NEW.session ORDER BY seq DESC LIMIT 1) END)
BEGIN SELECT RAISE(ABORT, 'a claim row names its session''s generation'); END;
CREATE TRIGGER thread_revoke_needs_decision BEFORE INSERT ON thread_claims
WHEN NEW.op = 'revoke' AND NEW.decision IS NULL AND NEW.reason IS NULL
BEGIN SELECT RAISE(ABORT, 'revoke needs a resolved decision or a reason'); END;
CREATE TRIGGER thread_claims_business BEFORE INSERT ON thread_claims
WHEN (SELECT business FROM threads WHERE id = NEW.thread) IS NOT NEW.business
BEGIN SELECT RAISE(ABORT, 'a thread claim names the thread''s business'); END;
CREATE TRIGGER thread_claims_session BEFORE INSERT ON thread_claims
WHEN (SELECT thread FROM sessions WHERE id = NEW.session) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'a claim''s session is in its thread'); END;
CREATE TRIGGER message_threads_match BEFORE INSERT ON message_threads
WHEN NOT EXISTS (SELECT 1 FROM messages m JOIN threads t ON t.id = NEW.thread
WHERE m.id = NEW.message AND m.business = t.business AND t.instance IN (m.to_role, m.from_role))
BEGIN SELECT RAISE(ABORT, 'a threaded message is to or from the thread''s instance'); END;
CREATE TRIGGER message_threads_session BEFORE INSERT ON message_threads
WHEN (SELECT thread FROM sessions WHERE id = NEW.session) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'a threaded message''s session is in its thread'); END;
CREATE TRIGGER thread_opens_session BEFORE INSERT ON thread_opens
WHEN (SELECT thread FROM sessions WHERE id = NEW.session) IS NOT NEW.thread
BEGIN SELECT RAISE(ABORT, 'an open''s session is in its thread'); END;
CREATE TRIGGER delivery_same_session BEFORE INSERT ON deliveries
WHEN NEW.op IN ('routed','delivered') AND (NEW.op = 'delivered' OR NEW.holder_run IS NOT NULL)
AND EXISTS (SELECT 1 FROM message_threads mt JOIN messages m ON m.id = mt.message JOIN threads t ON t.id = mt.thread
WHERE mt.message = NEW.message AND m.to_role = t.instance
AND NOT EXISTS (SELECT 1 FROM thread_claims c WHERE c.seq = (SELECT max(seq) FROM thread_claims WHERE session = mt.session)
AND c.op = 'claim' AND c.holder_run IS NEW.holder_run))
BEGIN SELECT RAISE(ABORT, 'a threaded message goes only to its session''s holder'); END;
CREATE TRIGGER sessions_no_update BEFORE UPDATE ON sessions BEGIN SELECT RAISE(ABORT, 'sessions is append-only'); END;
CREATE TRIGGER sessions_no_delete BEFORE DELETE ON sessions BEGIN SELECT RAISE(ABORT, 'sessions is append-only'); END;
CREATE TRIGGER sessions_no_replace BEFORE INSERT ON sessions WHEN EXISTS (SELECT 1 FROM sessions WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'sessions is append-only'); END;
CREATE TRIGGER session_defaults_no_update BEFORE UPDATE ON session_defaults BEGIN SELECT RAISE(ABORT, 'session_defaults is append-only'); END;
CREATE TRIGGER session_defaults_no_delete BEFORE DELETE ON session_defaults BEGIN SELECT RAISE(ABORT, 'session_defaults is append-only'); END;
CREATE TRIGGER session_defaults_no_replace BEFORE INSERT ON session_defaults WHEN EXISTS (SELECT 1 FROM session_defaults WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'session_defaults is append-only'); END;
CREATE TRIGGER thread_claims_no_update BEFORE UPDATE ON thread_claims BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER thread_claims_no_delete BEFORE DELETE ON thread_claims BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER thread_claims_no_replace BEFORE INSERT ON thread_claims WHEN EXISTS (SELECT 1 FROM thread_claims WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER message_threads_no_update BEFORE UPDATE ON message_threads BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER message_threads_no_delete BEFORE DELETE ON message_threads BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER message_threads_no_replace BEFORE INSERT ON message_threads WHEN EXISTS (SELECT 1 FROM message_threads WHERE message = NEW.message) BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER thread_opens_no_update BEFORE UPDATE ON thread_opens BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
CREATE TRIGGER thread_opens_no_delete BEFORE DELETE ON thread_opens BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
CREATE TRIGGER thread_opens_no_replace BEFORE INSERT ON thread_opens WHEN EXISTS (SELECT 1 FROM thread_opens WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
+21 -52
View File
@@ -18,14 +18,12 @@ import { credentialVerbs } from './credential-verbs.mjs';
import { webService } from './web.mjs';
import { hostExecutor, sweepRunDir } from './host-run.mjs';
import { grantKeys, hostGrant, hostReference, sweepHostKeys } from './host-grant.mjs';
import { giteaGrant, giteaKeys, giteaStart } from './gitea-grant.mjs';
import { verifyPrincipal } from './principal.mjs';
import { inspectInterpreter, recordInterpreterStart } from './interpreter.mjs';
import { createPeerReader } from './peercred.mjs';
import { createUsage } from './usage.mjs';
import { createLimits } from './limits.mjs';
import { createProcessing } from '../../processing-control/src/coordinator.mjs';
import { discordProcessingHooks } from '../../discord/src/processing.mjs';
import { createContainerStopper } from '../../processing-control/src/containers.mjs';
// Trusted host API. S1 supplies resolved definitions, S6 supplies launch records and ends them.
// Human limits writes use the shared organization writer behind the verified transport.
// `tasks` is S3's adapter factory: ({broker, credentials, businesses}) => {handle, timeout, close}.
@@ -94,7 +92,7 @@ export async function startBroker({
|| (Object.hasOwn(librarySync, 'intervalMs') && !(Number.isSafeInteger(librarySync.intervalMs) && librarySync.intervalMs > 0))
|| (Object.hasOwn(librarySync, 'configDir') && typeof librarySync.configDir !== 'string')))
throw new BusError('invalid-host-request');
let store, credentials, server, adapter, processing, syncTimer = null, syncPass = null;
let store, credentials, server, adapter, syncTimer = null, syncPass = null;
try {
const references = {};
for (const [business, b] of Object.entries(businesses)) {
@@ -123,7 +121,10 @@ export async function startBroker({
// Row 235 (r6 section 6, N1): a host.<id> key file with no record goes before Credentials loads.
for (const entry of sweepHostKeys(storeRoot, Object.keys(businesses), granted))
process.stderr.write(`hosts: ${JSON.stringify({ event: 'host-key-swept', ...entry })}\n`);
credentials = new Credentials({ references, repoRoots: protectedRoots, dataRoot, storeRoot });
// Row 236 (#1742, #436b r5 section 5): each gitea grant record gives its agent a store reference to
// gitea, and `held` names the files memory must not load until reconcile or a revoke.
const { held } = storeRoot ? giteaStart(store, storeRoot, businesses, references) : { held: [] };
credentials = new Credentials({ references, repoRoots: protectedRoots, dataRoot, storeRoot, held });
// vikunja:19/305: the plug-in switch through the same human-only path as the limits: the verbs
// plugins and plugins.set, logged in changes.jsonl with the actor, a missing module refused.
const plugins=organization?pluginSettings({dir:organization.dir,rolesDir:organization.rolesDir,exists:hasModule}):null;
@@ -141,20 +142,11 @@ export async function startBroker({
const withTokens = (id, result) => ({ ...result, tokens: limits.report(id) });
// Row 210 (#1707, design D7 step 6) and row 234 (#1740, r6 E2): the run dir and the host enrollment half.
const runRoot = organization ? join(organization.dir, 'secrets', 'run') : null;
let broker;
processing = createProcessing({ dataRoot, ...discordProcessingHooks(dataRoot), containerStop: createContainerStopper({ dataRoot }),
onTransition: () => broker.reconcileProcessing(),
onStop: stop => broker.recordProcessingStop(stop),
log: line => process.stderr.write(`${line}\n`),
});
// C2-04: mirrors are host state, never session checkouts. An unsafe
// existing mirror refuses startup before a socket can become ready.
for (const business of Object.values(businesses))
for (const [project, entry] of Object.entries(business.projects ?? {}))
if (entry.kind === 'library') {
try { await processing.work(() => libraryModule.ensureMirror({ business, project, dataRoot })); }
catch (e) { if (!e.code?.startsWith('processing-')) throw e; }
}
if (entry.kind === 'library') await libraryModule.ensureMirror({ business, project, dataRoot });
// C2-03a: the slot is <configDir>/secrets/<business>/library-gitea (design 10 step 4). Sync only
// checks that it is there; the askpass helper reads it.
const slotDir = librarySync?.configDir ?? organization?.dir ?? null;
@@ -162,11 +154,13 @@ export async function startBroker({
slot: slotDir ? join(slotDir, 'secrets', business.id, libraryModule.SLOT) : null,
...(librarySync ? { command: librarySync.command } : {}),
});
broker = new Broker({
const broker = new Broker({
hostGrant: runRoot ? hostGrant({ configDir: organization.dir, runRoot }) : null,
// Row 235 (#1741, r6 X4, X5): the grant executor's claim, register and issue, never use().
hostKeys: storeRoot ? grantKeys({ credentials, storeRoot }) : null,
processing,
// Row 236 (#1742): the Gitea grant path. The admin slot is <configDir>/secrets/grants/<business>/gitea.
giteaGrant: organization ? giteaGrant({ configDir: organization.dir }) : null,
giteaKeys: storeRoot ? giteaKeys({ credentials, storeRoot }) : null,
usage,
limits,
store,
@@ -174,12 +168,12 @@ export async function startBroker({
library: {
readFile: context => libraryModule.readLibraryFile({ ...context, dataRoot }),
status: context => libraryModule.libraryStatus({ ...context, dataRoot }),
sync: (context, { accept }) => processing.work(() => libraryModule.sync({ ...context, dataRoot }, { ...syncOptions(context.business), accept })),
sync: (context, { accept }) => libraryModule.sync({ ...context, dataRoot }, { ...syncOptions(context.business), accept }),
LibraryError: libraryModule.LibraryError,
},
project: {
readSpec: context => projectModule.readSpec({ ...context, dataRoot }),
writeRecord: context => processing.work(() => projectModule.writeRecord({ ...context, dataRoot })),
writeRecord: context => projectModule.writeRecord({ ...context, dataRoot }),
listFiles: context => projectModule.listProjectFiles({ ...context, dataRoot }),
readFile: context => projectModule.readProjectFile({ ...context, dataRoot }),
writeFile: context => projectModule.writeHumanFile({ ...context, dataRoot }),
@@ -218,13 +212,6 @@ export async function startBroker({
}
},
});
broker.reconcileProcessing();
processing.start();
const processingWork = () => {
const state = processing.read();
if (state.invalid) throw new BusError(state.code);
if (state.halted) throw new BusError('processing-halted');
};
// Process identities must be supplied for every managed run when using the human transport.
const launchRecords = [];
function bindLaunch(r) {
@@ -242,7 +229,7 @@ export async function startBroker({
const bound = launches.map(bindLaunch);
const readCaps = readers.map((business) => ({ business, cap: broker.bindReader({ business }) }));
if (tasks) {
adapter = await tasks({ broker, credentials, businesses, processing });
adapter = await tasks({ broker, credentials, businesses });
if (typeof adapter?.handle !== 'function' || !Number.isSafeInteger(adapter.timeout) || adapter.timeout < 1)
throw new BusError('invalid-adapter');
}
@@ -261,18 +248,9 @@ export async function startBroker({
console: fileURLToPath(new URL('../../webui/src/cli.mjs', import.meta.url)),
};
const path = join(store.directory, 'broker.sock');
// The existing host executor owns SSH and its key lifecycle. Combine its revocation
// signal with processing cancellation without changing its proof or credential entry points.
const host = runRoot ? hostExecutor({ root: runRoot, credentials: {
busy: (...args) => credentials.busy(...args),
useFor: (session, name, fn, options) => credentials.useFor(session, name, (token, meta) =>
fn(token, { ...meta, signal: meta.signal
? AbortSignal.any([meta.signal, processing.signal()]) : processing.signal() }), options),
} }) : null;
server = await serve({
web: (business) => web.get(business) ?? null,
host: host ? { busy: (...args) => host.busy(...args),
run: (...args) => processing.work(() => host.run(...args)) } : null,
host: runRoot ? hostExecutor({ credentials, root: runRoot }) : null,
tasks: adapter ? { handle: adapter.handle, timeout: adapter.timeout } : null,
broker,
path,
@@ -289,6 +267,8 @@ export async function startBroker({
// Row 235 (r6 X3): the remote undo of interrupted host grants, once the bus is up, so a start doesn't
// wait on the network.
broker.reconcileHosts();
// Row 236 (#436b r5 section 5): the Gitea grants' remote undo, settle and open revokes, likewise.
broker.reconcileGitea();
// S6 launcher operations. The host decides capacity and the instance list; the broker
// authorizes role.launch and keeps the evidence, refusals included.
// A refusal is recorded against the caller, as request() records one.
@@ -326,11 +306,11 @@ export async function startBroker({
const { business, project, fork, user, source, dst } = broker.publishTarget(ticket);
const entry = { state: 'running' };
remotes.set(ticket, entry);
processing.work(() => withGitea(ticket, (token) =>
withGitea(ticket, (token) =>
publishRemote({ repo: workingRepo(dataRoot, business, project), fork, user, source, dst, token, tls: publishTls }).catch((e) => {
throw e instanceof WorkingError ? new BusError(e.code) : e;
}),
)).then(
).then(
(r) => Object.assign(entry, { state: 'pushed', ...r }),
(e) => {
// Not a refusal: the error's name and code only, never a message that could carry git's output.
@@ -403,13 +383,7 @@ export async function startBroker({
}
}
};
const tick = () => {
const state = processing.read();
if (!syncPass && !state.halted && !state.invalid)
syncPass = processing.work(syncAll).catch(e => {
if (!e.code?.startsWith('processing-')) syncLog({ event: 'library-sync-refused', code: 'library-sync-failed' });
}).finally(() => { syncPass = null; });
};
const tick = () => { if (!syncPass) syncPass = syncAll().finally(() => { syncPass = null; }); };
if (timed.length) {
tick();
syncTimer = setInterval(tick, librarySync?.intervalMs ?? libraryModule.SYNC_INTERVAL_MS);
@@ -425,10 +399,7 @@ export async function startBroker({
threadsState: (business) => broker.threadsState(business),
threadIdle: (r) => broker.threadIdle(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();
return limits.mayLaunch(business, { instance, provider, model, account });
},
mayLaunch: ({ business, instance, provider, model, account = null }) => limits.mayLaunch(business, { instance, provider, model, account }),
identity: (cap) => broker.identity(cap),
authorizeLaunch,
checkPush,
@@ -455,7 +426,6 @@ export async function startBroker({
await stopSync();
await server.close();
await adapter?.close?.();
await processing?.close();
store.close();
credentials.close();
},
@@ -463,7 +433,6 @@ export async function startBroker({
} catch (e) {
await server?.close();
await adapter?.close?.();
await processing?.close();
store?.close();
credentials?.close();
throw e;
+5 -4
View File
@@ -1,6 +1,6 @@
import { createServer } from 'node:net';
import { lstatSync, chmodSync, unlinkSync } from 'node:fs';
import { BusError, TASK_VERBS, WEB_VERBS, HOST_VERBS, HOSTS_VERBS, PROJECT_FILE_VERBS, LIBRARY_VERBS, PROJECT_SAVE_VERB, PROJECT_SAVE_REQUEST_MAX } from './broker.mjs';
import { BusError, TASK_VERBS, WEB_VERBS, HOST_VERBS, HOSTS_VERBS, GRANTS_VERBS, PROJECT_FILE_VERBS, LIBRARY_VERBS, PROJECT_SAVE_VERB, PROJECT_SAVE_REQUEST_MAX } from './broker.mjs';
import { inspectInterpreter } from './interpreter.mjs';
import { createPeerReader } from './peercred.mjs';
const LIMIT = 65536;
@@ -134,12 +134,13 @@ export async function serve({ broker, path, authenticateHuman = null, authentica
});
return;
}
// Async effects retain the synchronous path for every other verb.
if (r.verb === 'processing.halt' || r.verb === 'processing.resume' ||
r.verb === 'usage.begin' || r.verb === 'usage.end' ||
// Row138: only project Git effects need an await; preserve every other sync path below.
if (r.verb === 'usage.begin' || r.verb === 'usage.end' ||
r.verb === 'spec.read' || r.verb === 'record.write' || PROJECT_FILE_VERBS.includes(r.verb) || LIBRARY_VERBS.includes(r.verb) ||
// Row 234 (#1740): a grant raise awaits its keyscan, and hosts.check its ssh.
HOSTS_VERBS.includes(r.verb) ||
// Row 236 (#1742): the grants verbs may call Gitea several times, 15 s each at most.
GRANTS_VERBS.includes(r.verb) ||
(r.verb === 'decision.raise' && (Object.hasOwn(r.args ?? {}, 'spec_path') || Object.hasOwn(r.args ?? {}, 'spec_commit') ||
Object.hasOwn(r.args ?? {}, 'grant')))) {
const own = transient;
+15 -2
View File
@@ -13,8 +13,8 @@ import { join, isAbsolute } from 'node:path';
import { createHash } from 'node:crypto';
const ddl = readFileSync(new URL('../schema.sql', import.meta.url), 'utf8');
export const SCHEMA_SHA256 = '0a7b633ce7eec9d0bb37c3255f4244d97bd0c8eac38db91a11b4ef6cc982ec8c';
export const SCHEMA_VERSION = '3j';
export const SCHEMA_SHA256 = 'cd68512212ce8bf8f5631138b2e63a340fa7b940088430649bb7784af7df162a';
export const SCHEMA_VERSION = '3l';
// 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';
@@ -61,6 +61,17 @@ const migrations = [{
from: '3i',
to: '3j',
sql: readFileSync(new URL('./migrations/3i-3j.sql', import.meta.url), 'utf8'),
}, {
// Row 236 (#1742, #436b section 7): the Gitea users the stack made and the Gitea grants. Only new tables.
from: '3j',
to: '3k',
sql: readFileSync(new URL('./migrations/3j-3k.sql', import.meta.url), 'utf8'),
}, {
// Row 264a (#1769): sessions below a thread. Each thread gets a default session whose id is the
// thread id, and thread_claims, message_threads and thread_opens are rebuilt to name it.
from: '3k',
to: '3l',
sql: readFileSync(new URL('./migrations/3k-3l.sql', import.meta.url), 'utf8'),
}];
const timedTables = [
'events',
@@ -72,6 +83,8 @@ const timedTables = [
'task_snapshots',
'projects',
'threads',
'sessions',
'session_defaults',
'thread_claims',
'thread_opens',
'instance_names',
+1 -4
View File
@@ -11,7 +11,6 @@
// every call and labels every reply untrusted (broker.mjs, requestWeb).
import { SEARCH_MAX_QUERY_CHARS, WEB_DEFAULTS, WEB_REFUSAL, WebRefusal, webFetch } from "../../discord/src/web.mjs";
import { currentProcessing } from '../../processing-control/src/effects.mjs';
export const WEB_SEARCH_NOT_CONFIGURED = "search is not configured for this business";
// Under the runner's own 30 s wait for a tool call, so a slow source is a refusal, not a lost reply.
@@ -29,7 +28,6 @@ export function webService({ search = null, maxFetchBytes = WEB_DEFAULTS.maxFetc
// so a stalled backend is cut off, not just ignored: web.mjs destroys its request on abort, and a
// search-source module gets the signal as the `signal` of its last argument.
const within = (start) => {
const processing = currentProcessing(); processing?.guard();
const controller = new AbortController();
let timer;
const late = new Promise((_, reject) => {
@@ -38,8 +36,7 @@ export function webService({ search = null, maxFetchBytes = WEB_DEFAULTS.maxFetc
reject(new WebRefusal(WEB_REFUSAL.TIMEOUT));
}, deadlineMs);
});
const signal = processing ? AbortSignal.any([processing.signal, controller.signal]) : controller.signal;
return Promise.race([start(signal), late]).finally(() => clearTimeout(timer));
return Promise.race([start(controller.signal), late]).finally(() => clearTimeout(timer));
};
async function run(verb, args) {
if (verb === "web.fetch") {
+13 -6
View File
@@ -15,6 +15,9 @@ const AT = '2026-10-18T00:00:00.000Z';
const SEQUENCE = 50;
const ddl3h = readFileSync(new URL('./fixtures/schema-3h.sql', import.meta.url), 'utf8');
const here = (p) => new URL(p, import.meta.url);
// Row 264a (#1769): what the 3k→3l step rebuilds or adds. sessions.test.mjs tests those objects.
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
const later3l = (r) => SESSIONS_3L.includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session'].includes(r.name);
function source3h(t) {
const root = mkdtempSync(join(tmpdir(), 'account-migrate-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
@@ -70,6 +73,8 @@ async function engine(root, name, { store, schema, step = (s) => s }) {
copyFileSync(here('../src/migrations/' + file), join(moduleRoot, 'src/migrations', file));
writeFileSync(join(moduleRoot, 'src/migrations/3h-3i.sql'), step(readFileSync(here('../src/migrations/3h-3i.sql'), 'utf8')));
copyFileSync(here('../src/migrations/3i-3j.sql'), join(moduleRoot, 'src/migrations/3i-3j.sql'));
copyFileSync(here('../src/migrations/3j-3k.sql'), join(moduleRoot, 'src/migrations/3j-3k.sql'));
copyFileSync(here('../src/migrations/3k-3l.sql'), join(moduleRoot, 'src/migrations/3k-3l.sql'));
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
}
@@ -77,7 +82,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), '3j');
assert.equal(version(path), '3l');
// Every table's rows as they were, usage_records with its seq values and sqlite_sequence with 50.
assert.deepEqual(snapshot(path, before.columns).rows, before.rows);
assert.deepEqual(before.rows.usage_records.map(r => r.seq), [1, 2, 3]);
@@ -86,8 +91,10 @@ test('REQ-LIMIT-4 3h copy migrates to 3i: every row keeps its seq, the sequence
const old = ' account TEXT CHECK (account IS NULL),\n';
assert.equal(was.filter(r => r.sql?.includes(old)).map(r => r.name).join(), 'usage_records');
// Row 234 (#1740) adds 3i→3j's own objects after this step. Its test is grant-migration.test.mjs.
const later = ['hosts', 'host_grants', 'events_grant_actor', 'sqlite_autoindex_hosts_1', 'sqlite_autoindex_host_grants_1'];
assert.deepEqual(master(path).filter(r => !later.includes(r.name)), was.map(r => r.name === 'usage_records' ? { ...r, sql: r.sql.replace(old, ' account TEXT,\n') } : r));
// Row 236 (#1742) adds 3j→3k's; its test is gitea-migration.test.mjs.
const later = ['hosts', 'host_grants', 'events_grant_actor', 'sqlite_autoindex_hosts_1', 'sqlite_autoindex_host_grants_1',
'gitea_users', 'gitea_grants', 'sqlite_autoindex_gitea_users_1', 'sqlite_autoindex_gitea_grants_1'];
assert.deepEqual(master(path).filter(r => !later.includes(r.name) && !later3l(r)), was.filter(r => !later3l(r)).map(r => r.name === 'usage_records' ? { ...r, sql: r.sql.replace(old, ' account TEXT,\n') } : r));
assert.equal(was.filter(r => r.type === 'trigger' && r.tbl_name === 'usage_records').length, 3);
const store = new Store(root);
try {
@@ -125,7 +132,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), '3j');
assert.equal(version(path), '3l');
assert.deepEqual(snapshot(path, before.columns).rows, before.rows);
});
@@ -137,14 +144,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 = '3j';", "export const SCHEMA_VERSION = '3h';")
.replace("export const SCHEMA_VERSION = '3l';", "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), '3j');
assert.equal(version(path), '3l');
assert.deepEqual(master(path), was);
assert.deepEqual(snapshot(path, after.columns).rows, after.rows);
});
+3 -1
View File
@@ -4,6 +4,8 @@
// and the ban on channel plug-ins importing a proof caller are retained.
// Row 254 (#1759) re-pins human-cli.mjs for the 130 s library.sync client timeout and webui
// bus.mjs for six library-* STATUS entries; no proof or transport change.
// Row 236 (#1742) re-pins human-cli.mjs for the same 130 s wait on the grants verbs; no proof or
// transport change.
import test from "node:test";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
@@ -14,7 +16,7 @@ import { Client } from "../src/client.mjs";
const baselines = {
"packages/bus/src/human.mjs": "0c6d96abe4607cba0b02b0638a2fc217ff8135bac9a47761a0ef015fbe9d3079",
"packages/bus/src/human-cli.mjs": "9c9af66b98ea0a490c9a880fc81c3c62414a8ee0b5ba6478608771b8c4f87932",
"packages/bus/src/human-cli.mjs": "271b9781d73cf31cc562a56180e3ebcd725b5fde148a83347a201fdef2fc28af",
"packages/cli/src/transport.mjs": "5129c44b4d9f642e825344df65c9bfb4bac4fc8d6b25343b42d39853dbd40774",
// 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,
+2 -2
View File
@@ -606,7 +606,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 = '3j';", "export const SCHEMA_VERSION = '3c';")
.replace("export const SCHEMA_VERSION = '3l';", "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');
@@ -630,7 +630,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, '3j');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3l');
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, '3j');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3l');
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 = '3j';", "export const SCHEMA_VERSION = '3k';")
.replace('}];\nconst timedTables', `}, { from: '3j', to: '3k', sql: ${JSON.stringify(futureSQL)} }];\nconst timedTables`);
.replace("export const SCHEMA_VERSION = '3l';", "export const SCHEMA_VERSION = '3m';")
.replace('}];\nconst timedTables', `}, { from: '3l', to: '3m', 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'])
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'])
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, '3k');
assert.equal(future.get("SELECT value FROM meta WHERE key='schema_version'").value, '3m');
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 3j ready/);
assert.match(migrated.stdout, /bus schema 3l ready/);
new Store(root).close();
const usage = spawnSync(process.execPath, [command], { encoding: 'utf8' });
assert.equal(usage.status, 2);
+8 -1
View File
@@ -8,6 +8,12 @@ import { join } from 'node:path';
import { Store } from '../src/store.mjs';
import { Broker } from '../src/broker.mjs';
import { Client } from '../src/client.mjs';
// Row 264a (#1769): a thread made by hand gets the default session the 3k-3l migration gives a
// legacy thread, with the thread's id.
const DEFAULT_SESSION = [
'INSERT INTO sessions(at,business,thread,id,instance,owner,name,op) SELECT at,business,id,id,instance,owner,'default','legacy-3l' FROM threads WHERE id=?',
'INSERT INTO session_defaults(at,thread,session,by) SELECT at,id,id,'migration-3l' FROM threads WHERE id=?',
];
const businesses = {
demo: {
@@ -149,7 +155,8 @@ test('broker process: launch ops authorize role.launch, record refusals and end
const side = new Store(data);
side.run('INSERT INTO projects(id,at,business,name,repo,created_by) VALUES(?,?,?,?,?,?)', 'p-1', '2026-10-15T00:00:00.000Z', 'demo', 'work', null, 'jason');
side.run('INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES(?,?,?,?,?,?,?)', 'th-1', '2026-10-15T00:00:00.000Z', 'demo', 'p-1', 'coder', 'jason', 'one');
side.run('INSERT INTO thread_opens(at,thread,by) VALUES(?,?,?)', '2026-10-15T00:00:01.000Z', 'th-1', 'jason');
for (const sql of DEFAULT_SESSION) side.run(sql, 'th-1');
side.run('INSERT INTO thread_opens(at,thread,session,by) VALUES(?,?,?,?)', '2026-10-15T00:00:01.000Z', 'th-1', 'th-1', 'jason');
side.close();
const ready = await ask(child, {
op: 'boot',
+191
View File
@@ -0,0 +1,191 @@
// Row 236 (#1742, #436b r5 section 9): a fake Gitea API for the grant executor, on 127.0.0.1 over TLS
// with row 199's scratch CA. It keeps users, their tokens and repositories in memory and answers the
// routes gitea-grant.mjs calls, with Gitea's auth rules as section 1 found them: the token routes take
// Basic auth as the account itself or a site admin, the rest the `token` header. Every token is a fake
// made here. The request log holds the method, the path, the auth kind and the login, never a token.
// `rule(match, answer)` makes one route answer otherwise: a status and body, `hang` (no answer, so the
// caller's timeout runs) or `drop` (the socket closes). Nothing here leaves 127.0.0.1.
import { randomBytes } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { createServer } from 'node:https';
import { scratchCa } from '../../seat/tests/fake-gitea.mjs';
const fakeToken = () => randomBytes(20).toString('hex');
export async function fakeGiteaApi(t, dir, { version = '1.27.1', admin = 'mosaic-grant', adminScopes = ['write:admin', 'write:repository', 'write:user'] } = {}) {
const tls = scratchCa(dir);
let nextId = 1;
const users = new Map();
const repos = [];
const rules = [];
const requests = [];
const state = { version };
const lower = (x) => String(x).toLowerCase();
const userJson = (u) => ({ id: u.id, login: u.login, email: u.email, full_name: u.full_name, is_admin: u.is_admin, prohibit_login: u.prohibit_login });
const repoJson = (r) => ({
id: r.id, name: r.name, full_name: `${r.owner}/${r.name}`, owner: { login: r.owner }, fork: Boolean(r.parent),
parent: r.parent ? { full_name: r.parent } : null,
});
const api = {
url: null,
ca: readFileSync(tls.ca, 'utf8'),
state,
requests,
users,
repos,
// Every token value this fake made, for the tests' leak checks. Never printed.
issued: [],
// A user and, with `scopes`, one token named `tokenName`. Returns the user and that token or null.
addUser(login, { is_admin = false, prohibit_login = false, full_name = login, email = `${login}@example.test`, scopes = null, tokenName = 'test' } = {}) {
const u = { id: nextId++, login, email, full_name, is_admin, prohibit_login, tokens: [] };
users.set(lower(login), u);
return { user: u, token: scopes ? api.addToken(login, tokenName, scopes) : null };
},
addToken(login, name, scopes) {
const u = users.get(lower(login));
const tok = { id: nextId++, name, sha1: fakeToken(), scopes: [...scopes].sort() };
u.tokens.push(tok);
api.issued.push(tok.sha1);
return tok;
},
removeUser(login) {
users.delete(lower(login));
},
addRepo(owner, name, { parent = null, collaborators = [] } = {}) {
const r = { id: nextId++, owner, name, parent, collaborators: collaborators.map(lower) };
repos.push(r);
return r;
},
tokens: (login) => users.get(lower(login))?.tokens.map((x) => ({ id: x.id, name: x.name, scopes: x.scopes })) ?? null,
rule(match, answer) {
const r = { match, answer };
rules.push(r);
return () => rules.splice(rules.indexOf(r), 1);
},
// The ops of the log, as "METHOD path" lines without the query.
calls: () => requests.map((r) => `${r.method} ${r.path.split('?')[0]}`),
};
const adminUser = api.addUser(admin, { is_admin: true, scopes: adminScopes, tokenName: 'mosaic-grant-bootstrap' });
api.admin = adminUser.user;
api.adminToken = adminUser.token.sha1;
const byToken = (sha1) => {
for (const u of users.values()) {
const tok = u.tokens.find((x) => x.sha1 === sha1);
if (tok) return { user: u, token: tok };
}
return null;
};
const authOf = (req) => {
const h = req.headers.authorization ?? '';
if (h.startsWith('token ')) return { kind: 'token', who: byToken(h.slice(6)) };
if (h.startsWith('Basic ')) {
const [login, ...rest] = Buffer.from(h.slice(6), 'base64').toString('utf8').split(':');
const who = byToken(rest.join(':'));
return { kind: 'basic', who: who && lower(who.user.login) === lower(login) ? who : null };
}
return { kind: 'none', who: null };
};
const reachable = (u) => repos.filter((r) => lower(r.owner) === lower(u.login) || r.collaborators.includes(lower(u.login)));
function route(method, path, query, auth, body) {
const who = auth.who;
const p = path.split('/').slice(3).map(decodeURIComponent);
if (method === 'GET' && path === '/api/v1/version') return [200, { version: state.version }];
if (!who) return [401, { message: 'unauthorized' }];
const me = who.user;
if (method === 'GET' && path === '/api/v1/user') return [200, userJson(me)];
if (method === 'GET' && path === '/api/v1/token') return [200, { id: who.token.id, name: who.token.name, scopes: who.token.scopes }];
if (method === 'GET' && path === '/api/v1/user/repos') {
const all = reachable(me);
const limit = Number(query.get('limit') ?? 30);
const page = Number(query.get('page') ?? 1);
return [200, all.slice((page - 1) * limit, page * limit).map(repoJson), all.length];
}
if (p[0] === 'users' && p.length === 2 && method === 'GET') {
const u = users.get(lower(p[1]));
return u ? [200, userJson(u)] : [404, { message: 'user does not exist' }];
}
if (p[0] === 'admin' && p[1] === 'users') {
if (!me.is_admin || !who.token.scopes.includes('write:admin')) return [403, { message: 'forbidden' }];
if (method === 'POST' && p.length === 2) {
if (users.has(lower(body.username))) return [422, { message: 'user already exists' }];
const u = { id: nextId++, login: body.username, email: body.email, full_name: body.full_name, is_admin: false, prohibit_login: false, tokens: [] };
users.set(lower(u.login), u);
return [201, userJson(u)];
}
if (method === 'PATCH' && p.length === 3) {
const u = users.get(lower(p[2]));
if (!u) return [404, { message: 'user does not exist' }];
if (typeof body.prohibit_login === 'boolean') u.prohibit_login = body.prohibit_login;
return [200, userJson(u)];
}
}
if (p[0] === 'users' && p[2] === 'tokens') {
if (auth.kind !== 'basic') return [401, { message: 'basic auth required' }];
const u = users.get(lower(p[1]));
if (lower(me.login) !== lower(p[1]) && !me.is_admin) return [403, { message: 'forbidden' }];
if (!u) return [404, { message: 'user does not exist' }];
if (method === 'GET' && p.length === 3) {
const limit = Number(query.get('limit') ?? 30);
const page = Number(query.get('page') ?? 1);
const list = u.tokens.map((x) => ({ id: x.id, name: x.name, scopes: x.scopes }));
return [200, list.slice((page - 1) * limit, page * limit), list.length];
}
if (method === 'POST' && p.length === 3) {
if (u.tokens.some((x) => x.name === body.name)) return [400, { message: 'access token name has been used already' }];
const tok = api.addToken(u.login, body.name, body.scopes);
return [201, { id: tok.id, name: tok.name, sha1: tok.sha1, scopes: tok.scopes }];
}
if (method === 'DELETE' && p.length === 4) {
const i = u.tokens.findIndex((x) => String(x.id) === p[3] || x.name === p[3]);
if (i < 0) return [404, { message: 'token not found' }];
u.tokens.splice(i, 1);
return [204, null];
}
}
if (p[0] === 'repos' && p.length === 3 && method === 'GET') {
const r = repos.find((x) => lower(x.owner) === lower(p[1]) && lower(x.name) === lower(p[2]));
return r ? [200, repoJson(r)] : [404, { message: 'not found' }];
}
if (p[0] === 'repos' && p.length === 4 && p[3] === 'forks' && method === 'POST') {
const up = repos.find((x) => lower(x.owner) === lower(p[1]) && lower(x.name) === lower(p[2]));
if (!up) return [404, { message: 'not found' }];
if (repos.some((x) => lower(x.owner) === lower(me.login) && lower(x.name) === lower(up.name))) return [409, { message: 'exists' }];
return [202, repoJson(api.addRepo(me.login, up.name, { parent: `${up.owner}/${up.name}` }))];
}
return [404, { message: 'no route' }];
}
const server = createServer({ key: tls.key, cert: tls.cert }, (req, res) => {
const chunks = [];
req.on('data', (c) => chunks.push(c));
req.on('end', async () => {
const u = new URL(req.url, 'https://127.0.0.1');
const auth = authOf(req);
let body = null;
try {
body = chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : null;
} catch {}
const entry = { method: req.method, path: u.pathname + u.search, auth: auth.kind, as: auth.who?.user.login ?? null };
requests.push(entry);
const rule = rules.find((r) => r.match(entry, body));
// An answer function may be async, to hold a call open until the test lets it go.
const answer = rule ? (typeof rule.answer === 'function' ? await rule.answer(entry, body) : rule.answer) : null;
if (answer?.hang) return;
if (answer?.drop) return req.socket.destroy();
const [status, json, total] = answer && !answer.pass ? [answer.status, answer.json ?? null, answer.total] : route(req.method, u.pathname, u.searchParams, auth, body ?? {});
const headers = { 'content-type': 'application/json' };
if (total !== undefined && total !== null) headers['x-total-count'] = String(total);
res.writeHead(status, headers);
res.end(json === null || status === 204 ? undefined : JSON.stringify(json));
});
});
await new Promise((r) => server.listen(0, '127.0.0.1', r));
t.after(() => new Promise((r) => {
server.closeAllConnections?.();
server.close(() => r());
}));
api.url = `https://127.0.0.1:${server.address().port}`;
return api;
}
+396
View File
@@ -0,0 +1,396 @@
PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = ON;
CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT NOT NULL) STRICT;
CREATE TABLE events (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
kind TEXT NOT NULL CHECK (kind IN (
'session.launched',
'session.ended',
'action.allowed',
'action.refused',
'task.created',
'task.assigned',
'task.state',
'task.closed',
'task.changed.external',
'task.conflict',
'task.missing',
'review.requested',
'review.verdict',
'human.input',
'config.refused',
'credential.expiring',
'credential.expired',
'credential.changed',
'launch.revoked',
'launch.restored',
'digest.sent')),
actor_role TEXT, actor_run TEXT,
subject TEXT,
corrects TEXT REFERENCES events(id),
body TEXT NOT NULL CHECK (json_valid(body))
) STRICT;
CREATE TABLE role_claims (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL, role TEXT NOT NULL,
op TEXT NOT NULL CHECK (op IN ('claim','release','revoke')),
holder_run TEXT NOT NULL,
harness TEXT NOT NULL, address TEXT,
by TEXT NOT NULL, reason TEXT,
decision TEXT
) STRICT;
CREATE TABLE decisions (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL, project TEXT,
raised_by_role TEXT NOT NULL, raised_by_run TEXT NOT NULL,
class TEXT NOT NULL CHECK (class IN ('routine','within-role','cross-role','gated')),
action TEXT NOT NULL,
route_to TEXT NOT NULL,
question TEXT NOT NULL,
options TEXT NOT NULL CHECK (json_valid(options) AND json_array_length(options) BETWEEN 2 AND 9),
recommendation TEXT NOT NULL,
task_ref TEXT CHECK (task_ref IS NULL OR (task_ref GLOB 'vikunja:[1-9]*/[1-9]*' AND NOT substr(task_ref, 9) GLOB '*[^0-9/]*' AND NOT substr(task_ref, 9) GLOB '*/*/*')), requirement_ref TEXT,
blocking INTEGER NOT NULL CHECK (blocking IN (0,1)),
supersedes TEXT REFERENCES decisions(id)
, spec_path TEXT, spec_commit TEXT) STRICT;
CREATE TABLE decision_events (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
decision TEXT NOT NULL REFERENCES decisions(id),
at TEXT NOT NULL,
op TEXT NOT NULL CHECK (op IN ('seen','resolved','withdrawn','expired')),
by TEXT NOT NULL,
choice TEXT, note TEXT, via TEXT
) STRICT;
CREATE TABLE messages (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
from_role TEXT NOT NULL, from_run TEXT NOT NULL,
to_role TEXT NOT NULL,
class TEXT NOT NULL,
in_reply_to TEXT REFERENCES messages(id),
decision TEXT REFERENCES decisions(id),
corrects TEXT REFERENCES messages(id),
body TEXT NOT NULL
, to_name TEXT) STRICT;
CREATE TABLE deliveries (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
message TEXT NOT NULL REFERENCES messages(id),
at TEXT NOT NULL,
op TEXT NOT NULL CHECK (op IN ('routed','delivered','failed','read')),
holder_run TEXT, transport TEXT, address TEXT, detail TEXT
) STRICT;
CREATE TABLE task_snapshots (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
task_ref TEXT NOT NULL CHECK (task_ref GLOB 'vikunja:[1-9]*/[1-9]*' AND NOT substr(task_ref, 9) GLOB '*[^0-9/]*' AND NOT substr(task_ref, 9) GLOB '*/*/*'),
updated TEXT NOT NULL,
etag TEXT,
digest TEXT NOT NULL CHECK (length(digest) = 64 AND NOT digest GLOB '*[^0-9a-f]*'),
fields TEXT NOT NULL CHECK (json_valid(fields)),
source TEXT NOT NULL CHECK (source IN ('self','poll')),
via TEXT CHECK (via IN ('board','cursor','task','reconcile')),
read_at TEXT,
role TEXT, run TEXT,
CHECK ((source = 'self') = (role IS NOT NULL AND run IS NOT NULL)),
CHECK ((source = 'poll') = (via IS NOT NULL AND read_at IS NOT NULL)),
CHECK (json_type(fields, '$.bucket') IS 'integer'
OR (source IS 'poll' AND via IS 'task' AND json_type(fields, '$.gone') IS 'text'))
) STRICT;
CREATE INDEX task_snapshots_ref ON task_snapshots (business, task_ref, seq);
CREATE TRIGGER decisions_resolve_once BEFORE INSERT ON decision_events
WHEN NEW.op IN ('resolved','withdrawn','expired') AND EXISTS (
SELECT 1 FROM decision_events WHERE decision = NEW.decision AND op IN ('resolved','withdrawn','expired'))
BEGIN SELECT RAISE(ABORT, 'decision already closed'); END;
CREATE TRIGGER decisions_resolved_choice BEFORE INSERT ON decision_events
WHEN NEW.op = 'resolved' AND (NEW.choice IS NULL OR NOT EXISTS (
SELECT 1 FROM decisions d, json_each(d.options) o WHERE d.id = NEW.decision AND json_extract(o.value,'$.key') = NEW.choice))
BEGIN SELECT RAISE(ABORT, 'resolution must name one of the options'); END;
CREATE TRIGGER role_one_holder BEFORE INSERT ON role_claims
WHEN NEW.op = 'claim' AND (SELECT op FROM role_claims WHERE business = NEW.business AND role = NEW.role ORDER BY seq DESC LIMIT 1) = 'claim'
BEGIN SELECT RAISE(ABORT, 'role already held'); END;
CREATE TRIGGER role_release_by_holder BEFORE INSERT ON role_claims
WHEN NEW.op IN ('release','revoke') AND COALESCE((SELECT op FROM role_claims WHERE business = NEW.business AND role = NEW.role ORDER BY seq DESC LIMIT 1),'') <> 'claim'
BEGIN SELECT RAISE(ABORT, 'role is not held'); END;
CREATE TRIGGER role_release_same_run BEFORE INSERT ON role_claims
WHEN NEW.op = 'release' AND (SELECT holder_run FROM role_claims WHERE business = NEW.business AND role = NEW.role ORDER BY seq DESC LIMIT 1) <> NEW.holder_run
BEGIN SELECT RAISE(ABORT, 'only the holder releases; others revoke'); END;
CREATE TRIGGER role_revoke_needs_decision BEFORE INSERT ON role_claims
WHEN NEW.op = 'revoke' AND NEW.decision IS NULL AND NEW.reason IS NULL
BEGIN SELECT RAISE(ABORT, 'revoke needs a resolved decision or a reason'); END;
CREATE TRIGGER meta_no_update BEFORE UPDATE ON meta BEGIN SELECT RAISE(ABORT, 'meta is append-only'); END;
CREATE TRIGGER meta_no_delete BEFORE DELETE ON meta BEGIN SELECT RAISE(ABORT, 'meta is append-only'); END;
CREATE TRIGGER events_no_update BEFORE UPDATE ON events BEGIN SELECT RAISE(ABORT, 'events is append-only'); END;
CREATE TRIGGER events_no_delete BEFORE DELETE ON events BEGIN SELECT RAISE(ABORT, 'events is append-only'); END;
CREATE TRIGGER role_claims_no_update BEFORE UPDATE ON role_claims BEGIN SELECT RAISE(ABORT, 'role_claims is append-only'); END;
CREATE TRIGGER role_claims_no_delete BEFORE DELETE ON role_claims BEGIN SELECT RAISE(ABORT, 'role_claims is append-only'); END;
CREATE TRIGGER decisions_no_update BEFORE UPDATE ON decisions BEGIN SELECT RAISE(ABORT, 'decisions is append-only'); END;
CREATE TRIGGER decisions_no_delete BEFORE DELETE ON decisions BEGIN SELECT RAISE(ABORT, 'decisions is append-only'); END;
CREATE TRIGGER decision_events_no_update BEFORE UPDATE ON decision_events BEGIN SELECT RAISE(ABORT, 'decision_events is append-only'); END;
CREATE TRIGGER decision_events_no_delete BEFORE DELETE ON decision_events BEGIN SELECT RAISE(ABORT, 'decision_events is append-only'); END;
CREATE TRIGGER messages_no_update BEFORE UPDATE ON messages BEGIN SELECT RAISE(ABORT, 'messages is append-only'); END;
CREATE TRIGGER messages_no_delete BEFORE DELETE ON messages BEGIN SELECT RAISE(ABORT, 'messages is append-only'); END;
CREATE TRIGGER deliveries_no_update BEFORE UPDATE ON deliveries BEGIN SELECT RAISE(ABORT, 'deliveries is append-only'); END;
CREATE TRIGGER deliveries_no_delete BEFORE DELETE ON deliveries BEGIN SELECT RAISE(ABORT, 'deliveries is append-only'); END;
CREATE TRIGGER meta_no_replace BEFORE INSERT ON meta WHEN EXISTS (SELECT 1 FROM meta WHERE key = NEW.key) BEGIN SELECT RAISE(ABORT, 'meta is append-only'); END;
CREATE TRIGGER events_no_replace BEFORE INSERT ON events WHEN EXISTS (SELECT 1 FROM events WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'events is append-only'); END;
CREATE TRIGGER role_claims_no_replace BEFORE INSERT ON role_claims WHEN EXISTS (SELECT 1 FROM role_claims WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'role_claims is append-only'); END;
CREATE TRIGGER decisions_no_replace BEFORE INSERT ON decisions WHEN EXISTS (SELECT 1 FROM decisions WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'decisions is append-only'); END;
CREATE TRIGGER decision_events_no_replace BEFORE INSERT ON decision_events WHEN EXISTS (SELECT 1 FROM decision_events WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'decision_events is append-only'); END;
CREATE TRIGGER messages_no_replace BEFORE INSERT ON messages WHEN EXISTS (SELECT 1 FROM messages WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'messages is append-only'); END;
CREATE TRIGGER deliveries_no_replace BEFORE INSERT ON deliveries WHEN EXISTS (SELECT 1 FROM deliveries WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'deliveries is append-only'); END;
CREATE TRIGGER task_snapshots_no_update BEFORE UPDATE ON task_snapshots BEGIN SELECT RAISE(ABORT, 'task_snapshots is append-only'); END;
CREATE TRIGGER task_snapshots_no_delete BEFORE DELETE ON task_snapshots BEGIN SELECT RAISE(ABORT, 'task_snapshots is append-only'); END;
CREATE TRIGGER task_snapshots_no_replace BEFORE INSERT ON task_snapshots WHEN EXISTS (SELECT 1 FROM task_snapshots WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'task_snapshots is append-only'); END;
CREATE TRIGGER decisions_blocking_needs_task BEFORE INSERT ON decisions
WHEN NEW.blocking = 1 AND NEW.task_ref IS NULL
BEGIN SELECT RAISE(ABORT, 'a blocking decision cites the task it blocks'); END;
CREATE TRIGGER events_launch_by_human BEFORE INSERT ON events
WHEN NEW.kind IN ('launch.revoked','launch.restored') AND (NEW.actor_role IS NOT NULL OR NEW.actor_run IS NOT NULL)
BEGIN SELECT RAISE(ABORT, 'only the human revokes or restores launching'); END;
CREATE TRIGGER events_credential_body BEFORE INSERT ON events
WHEN NEW.kind GLOB 'credential.*' AND (
json_extract(NEW.body, '$.service') IS NULL OR json_extract(NEW.body, '$.service') NOT IN ('gitea','vikunja')
OR json_extract(NEW.body, '$.instance') IS NULL)
BEGIN SELECT RAISE(ABORT, 'credential events name a service and a role instance'); END;
CREATE TRIGGER events_task_missing_body BEFORE INSERT ON events
WHEN NEW.kind = 'task.missing' AND (
json_extract(NEW.body, '$.reason') IS NULL OR json_extract(NEW.body, '$.reason') NOT IN ('moved','not-found','no-access')
OR (json_extract(NEW.body, '$.reason') = 'moved' AND json_type(NEW.body, '$.project') IS NOT 'integer'))
BEGIN SELECT RAISE(ABORT, 'task.missing carries a reason, and the new project when moved'); END;
CREATE TRIGGER events_task_subject BEFORE INSERT ON events
WHEN NEW.kind GLOB 'task.*' AND (NEW.subject IS NULL OR NOT (NEW.subject GLOB 'vikunja:[1-9]*/[1-9]*' AND NOT substr(NEW.subject, 9) GLOB '*[^0-9/]*' AND NOT substr(NEW.subject, 9) GLOB '*/*/*'))
BEGIN SELECT RAISE(ABORT, 'a task event names its task in subject'); END;
CREATE TRIGGER events_task_created_body BEFORE INSERT ON events
WHEN NEW.kind = 'task.created' AND (
json_type(NEW.body, '$.request') IS NOT 'text'
OR NOT EXISTS (SELECT 1 FROM events WHERE id = json_extract(NEW.body, '$.request')
AND kind = 'human.input' AND business = NEW.business)
OR json_type(NEW.body, '$.requirement') IS NOT 'text'
OR NOT json_extract(NEW.body, '$.requirement') GLOB 'REQ-[A-Z]*-[1-9]*'
OR json_extract(NEW.body, '$.requirement') GLOB 'REQ-*[^A-Z0-9-]*')
BEGIN SELECT RAISE(ABORT, 'task.created cites the human.input that asked for it and a requirement id'); END;
CREATE VIEW launch_state AS
SELECT business, CASE kind WHEN 'launch.revoked' THEN 'revoked' ELSE 'allowed' END AS state, at
FROM events e WHERE kind IN ('launch.revoked','launch.restored')
AND seq = (SELECT max(seq) FROM events WHERE business = e.business AND kind IN ('launch.revoked','launch.restored'));
CREATE VIEW task_external_changes AS
SELECT p.business, p.task_ref, p.seq, p.via, p.updated, p.digest, ls.digest AS self_digest
FROM task_snapshots p
LEFT JOIN task_snapshots ls ON ls.seq = (SELECT max(seq) FROM task_snapshots
WHERE business = p.business AND task_ref = p.task_ref AND source = 'self')
WHERE p.source = 'poll'
AND p.seq = (SELECT max(seq) FROM task_snapshots WHERE business = p.business AND task_ref = p.task_ref)
AND (ls.seq IS NULL OR (p.updated >= ls.updated AND p.read_at > ls.at AND p.digest <> ls.digest));
CREATE VIEW task_current AS
SELECT s.seq, s.at, s.business, s.task_ref, s.updated, s.etag, s.digest, s.fields, s.source, s.via, s.read_at, s.role, s.run
FROM task_snapshots s
WHERE s.seq = (SELECT max(c.seq) FROM task_snapshots c
WHERE c.business = s.business AND c.task_ref = s.task_ref
AND (c.source = 'self'
OR c.read_at > coalesce((SELECT ls.at FROM task_snapshots ls
WHERE ls.business = c.business AND ls.task_ref = c.task_ref AND ls.source = 'self'
ORDER BY ls.seq DESC LIMIT 1), '')));
CREATE VIEW tasks_open AS
SELECT s.business, s.task_ref, json_extract(s.fields, '$.bucket') AS bucket, s.seq
FROM task_current s
WHERE json_type(s.fields, '$.gone') IS NULL
AND json_extract(s.fields, '$.done') = 0;
CREATE VIEW urgent_inbox AS
SELECT d.id, d.business, d.task_ref, d.question, d.at
FROM decisions d
WHERE d.class = 'gated' AND d.blocking = 1
AND NOT EXISTS (SELECT 1 FROM decision_events x WHERE x.decision = d.id AND x.op IN ('resolved','withdrawn','expired'));
CREATE TABLE task_comment_cursors (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
business TEXT NOT NULL,
task_id INTEGER NOT NULL CHECK (task_id > 0),
last_comment INTEGER NOT NULL CHECK (last_comment > 0)
) STRICT;
CREATE INDEX task_comment_cursors_latest ON task_comment_cursors (business, task_id, seq);
CREATE TRIGGER task_comment_cursors_no_update BEFORE UPDATE ON task_comment_cursors
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
CREATE TRIGGER task_comment_cursors_no_delete BEFORE DELETE ON task_comment_cursors
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
CREATE TRIGGER task_comment_cursors_no_replace BEFORE INSERT ON task_comment_cursors
WHEN EXISTS (SELECT 1 FROM task_comment_cursors WHERE seq = NEW.seq)
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
CREATE TRIGGER task_comment_cursors_forward BEFORE INSERT ON task_comment_cursors
WHEN NEW.last_comment <= COALESCE((SELECT last_comment FROM task_comment_cursors
WHERE business = NEW.business AND task_id = NEW.task_id ORDER BY seq DESC LIMIT 1), 0)
BEGIN SELECT RAISE(ABORT, 'comment cursor must advance'); END;
CREATE TABLE projects (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
name TEXT NOT NULL,
repo TEXT,
created_by TEXT NOT NULL,
UNIQUE (business, name)
) STRICT;
CREATE TABLE threads (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
project TEXT NOT NULL REFERENCES projects(id),
instance TEXT NOT NULL,
owner TEXT NOT NULL,
name TEXT NOT NULL,
UNIQUE (project, name)
) STRICT;
CREATE TABLE thread_claims (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
op TEXT NOT NULL CHECK (op IN ('claim','release','revoke')),
holder_run TEXT NOT NULL,
harness TEXT NOT NULL, address TEXT,
by TEXT NOT NULL, reason TEXT,
decision TEXT
) STRICT;
CREATE TABLE message_threads (
message TEXT PRIMARY KEY REFERENCES messages(id),
thread TEXT NOT NULL REFERENCES threads(id)
) STRICT;
CREATE TABLE thread_opens (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
by TEXT NOT NULL
) STRICT;
CREATE INDEX thread_claims_thread ON thread_claims (thread, seq);
CREATE INDEX message_threads_thread ON message_threads (thread);
CREATE TRIGGER threads_project_business BEFORE INSERT ON threads
WHEN (SELECT business FROM projects WHERE id = NEW.project) IS NOT NEW.business
BEGIN SELECT RAISE(ABORT, 'a thread belongs to its project''s business'); END;
CREATE TRIGGER thread_one_holder BEFORE INSERT ON thread_claims
WHEN NEW.op = 'claim' AND (SELECT op FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1) = 'claim'
BEGIN SELECT RAISE(ABORT, 'thread already held'); END;
CREATE TRIGGER thread_one_per_run BEFORE INSERT ON thread_claims
WHEN NEW.op = 'claim' AND EXISTS (SELECT 1 FROM thread_claims c WHERE c.holder_run = NEW.holder_run AND c.business = NEW.business
AND c.seq = (SELECT max(seq) FROM thread_claims WHERE thread = c.thread) AND c.op = 'claim')
BEGIN SELECT RAISE(ABORT, 'run already holds a thread'); END;
CREATE TRIGGER thread_release_by_holder BEFORE INSERT ON thread_claims
WHEN NEW.op IN ('release','revoke') AND COALESCE((SELECT op FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1),'') <> 'claim'
BEGIN SELECT RAISE(ABORT, 'thread is not held'); END;
CREATE TRIGGER thread_release_same_run BEFORE INSERT ON thread_claims
WHEN NEW.op = 'release' AND (SELECT holder_run FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1) <> NEW.holder_run
BEGIN SELECT RAISE(ABORT, 'only the holder releases; others revoke'); END;
CREATE TRIGGER thread_revoke_needs_decision BEFORE INSERT ON thread_claims
WHEN NEW.op = 'revoke' AND NEW.decision IS NULL AND NEW.reason IS NULL
BEGIN SELECT RAISE(ABORT, 'revoke needs a resolved decision or a reason'); END;
CREATE TRIGGER thread_claims_business BEFORE INSERT ON thread_claims
WHEN (SELECT business FROM threads WHERE id = NEW.thread) IS NOT NEW.business
BEGIN SELECT RAISE(ABORT, 'a thread claim names the thread''s business'); END;
CREATE TRIGGER message_threads_match BEFORE INSERT ON message_threads
WHEN NOT EXISTS (SELECT 1 FROM messages m JOIN threads t ON t.id = NEW.thread
WHERE m.id = NEW.message AND m.business = t.business AND t.instance IN (m.to_role, m.from_role))
BEGIN SELECT RAISE(ABORT, 'a threaded message is to or from the thread''s instance'); END;
CREATE TRIGGER delivery_same_thread BEFORE INSERT ON deliveries
WHEN NEW.op IN ('routed','delivered') AND (NEW.op = 'delivered' OR NEW.holder_run IS NOT NULL)
AND EXISTS (SELECT 1 FROM message_threads mt JOIN messages m ON m.id = mt.message JOIN threads t ON t.id = mt.thread
WHERE mt.message = NEW.message AND m.to_role = t.instance
AND NOT EXISTS (SELECT 1 FROM thread_claims c WHERE c.seq = (SELECT max(seq) FROM thread_claims WHERE thread = t.id)
AND c.op = 'claim' AND c.holder_run IS NEW.holder_run))
BEGIN SELECT RAISE(ABORT, 'a threaded message goes only to its thread''s holder'); END;
CREATE TRIGGER projects_no_update BEFORE UPDATE ON projects BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER projects_no_delete BEFORE DELETE ON projects BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER projects_no_replace BEFORE INSERT ON projects WHEN EXISTS (SELECT 1 FROM projects WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER threads_no_update BEFORE UPDATE ON threads BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER threads_no_delete BEFORE DELETE ON threads BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER threads_no_replace BEFORE INSERT ON threads WHEN EXISTS (SELECT 1 FROM threads WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER thread_claims_no_update BEFORE UPDATE ON thread_claims BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER thread_claims_no_delete BEFORE DELETE ON thread_claims BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER thread_claims_no_replace BEFORE INSERT ON thread_claims WHEN EXISTS (SELECT 1 FROM thread_claims WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER message_threads_no_update BEFORE UPDATE ON message_threads BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER message_threads_no_delete BEFORE DELETE ON message_threads BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER message_threads_no_replace BEFORE INSERT ON message_threads WHEN EXISTS (SELECT 1 FROM message_threads WHERE message = NEW.message) BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER thread_opens_no_update BEFORE UPDATE ON thread_opens BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
CREATE TRIGGER thread_opens_no_delete BEFORE DELETE ON thread_opens BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
CREATE TRIGGER thread_opens_no_replace BEFORE INSERT ON thread_opens WHEN EXISTS (SELECT 1 FROM thread_opens WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
CREATE TABLE instance_names (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
instance TEXT NOT NULL,
name TEXT NOT NULL,
by TEXT NOT NULL
) STRICT;
CREATE TRIGGER instance_names_unique BEFORE INSERT ON instance_names
WHEN EXISTS (SELECT 1 FROM instance_names n WHERE n.business = NEW.business AND n.name = NEW.name
AND n.instance <> NEW.instance
AND n.seq = (SELECT max(seq) FROM instance_names WHERE business = n.business AND instance = n.instance))
BEGIN SELECT RAISE(ABORT, 'an instance name is unique in its business'); END;
CREATE TRIGGER instance_names_no_update BEFORE UPDATE ON instance_names BEGIN SELECT RAISE(ABORT, 'instance_names is append-only'); END;
CREATE TRIGGER instance_names_no_delete BEFORE DELETE ON instance_names BEGIN SELECT RAISE(ABORT, 'instance_names is append-only'); END;
CREATE TRIGGER instance_names_no_replace BEFORE INSERT ON instance_names WHEN EXISTS (SELECT 1 FROM instance_names WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'instance_names is append-only'); END;
-- Row 124 (#1618): immutable source snapshots and turn outcomes, never transcript text.
CREATE TABLE usage_records (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
run TEXT NOT NULL,
instance TEXT NOT NULL,
provider TEXT,
model TEXT,
account TEXT,
turn TEXT NOT NULL,
source TEXT NOT NULL,
phase TEXT NOT NULL CHECK (phase IN ('begin','end')),
status TEXT NOT NULL CHECK (status IN ('known','unknown')),
code TEXT,
cursor TEXT CHECK (cursor IS NULL OR json_valid(cursor)),
input INTEGER,
output INTEGER,
cache_write INTEGER,
cache_read INTEGER,
UNIQUE (business,run,turn,source,phase),
CHECK ((status='unknown' AND input IS NULL AND output IS NULL AND cache_write IS NULL AND cache_read IS NULL)
OR (status='known' AND input >= 0 AND output >= 0 AND cache_write >= 0 AND cache_read >= 0
AND input IS NOT NULL AND output IS NOT NULL AND cache_write IS NOT NULL AND cache_read IS NOT NULL))
) STRICT;
CREATE TRIGGER usage_records_no_update BEFORE UPDATE ON usage_records BEGIN SELECT RAISE(ABORT, 'usage_records is append-only'); END;
CREATE TRIGGER usage_records_no_delete BEFORE DELETE ON usage_records BEGIN SELECT RAISE(ABORT, 'usage_records is append-only'); END;
CREATE TRIGGER usage_records_no_replace BEFORE INSERT ON usage_records WHEN EXISTS (SELECT 1 FROM usage_records WHERE seq=NEW.seq OR (business=NEW.business AND run=NEW.run AND turn=NEW.turn AND source=NEW.source AND phase=NEW.phase)) BEGIN SELECT RAISE(ABORT, 'usage_records is append-only'); END;
CREATE TABLE hosts (
business TEXT NOT NULL,
host TEXT NOT NULL,
address TEXT NOT NULL,
port INTEGER NOT NULL CHECK (port BETWEEN 1 AND 65535),
user TEXT NOT NULL,
sudo INTEGER NOT NULL CHECK (sudo IN (0,1)),
host_keys TEXT NOT NULL CHECK (json_valid(host_keys) AND json_type(host_keys) = 'array' AND json_array_length(host_keys) > 0),
installer_fingerprint TEXT,
state TEXT NOT NULL CHECK (state IN ('pending-install','ready')),
decision TEXT NOT NULL REFERENCES decisions(id),
checked_at TEXT,
PRIMARY KEY (business, host),
CHECK (state = 'pending-install' OR (installer_fingerprint IS NOT NULL AND checked_at IS NOT NULL))
) STRICT;
CREATE TABLE host_grants (
business TEXT NOT NULL,
agent TEXT NOT NULL,
host TEXT NOT NULL,
commands TEXT NOT NULL CHECK (json_valid(commands) AND json_type(commands) = 'array' AND json_array_length(commands) > 0),
rotate_by TEXT NOT NULL,
tag TEXT NOT NULL CHECK (length(tag) = 39 AND tag GLOB 'mosaic-*' AND NOT substr(tag, 8) GLOB '*[^0-9a-f]*'),
decision TEXT NOT NULL REFERENCES decisions(id),
PRIMARY KEY (business, agent, host),
FOREIGN KEY (business, host) REFERENCES hosts(business, host)
) STRICT;
CREATE TRIGGER events_grant_actor BEFORE INSERT ON events
WHEN NEW.kind = 'action.allowed'
AND (json_extract(NEW.body, '$.action') GLOB 'grant.*' OR json_extract(NEW.body, '$.operation') GLOB 'grant.*')
AND NOT (NEW.actor_role IS NULL AND NEW.actor_run IS 'grant')
BEGIN SELECT RAISE(ABORT, 'only the grant writer records grant events'); END;
+417
View File
@@ -0,0 +1,417 @@
PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = ON;
CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT NOT NULL) STRICT;
CREATE TABLE events (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
kind TEXT NOT NULL CHECK (kind IN (
'session.launched',
'session.ended',
'action.allowed',
'action.refused',
'task.created',
'task.assigned',
'task.state',
'task.closed',
'task.changed.external',
'task.conflict',
'task.missing',
'review.requested',
'review.verdict',
'human.input',
'config.refused',
'credential.expiring',
'credential.expired',
'credential.changed',
'launch.revoked',
'launch.restored',
'digest.sent')),
actor_role TEXT, actor_run TEXT,
subject TEXT,
corrects TEXT REFERENCES events(id),
body TEXT NOT NULL CHECK (json_valid(body))
) STRICT;
CREATE TABLE role_claims (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL, role TEXT NOT NULL,
op TEXT NOT NULL CHECK (op IN ('claim','release','revoke')),
holder_run TEXT NOT NULL,
harness TEXT NOT NULL, address TEXT,
by TEXT NOT NULL, reason TEXT,
decision TEXT
) STRICT;
CREATE TABLE decisions (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL, project TEXT,
raised_by_role TEXT NOT NULL, raised_by_run TEXT NOT NULL,
class TEXT NOT NULL CHECK (class IN ('routine','within-role','cross-role','gated')),
action TEXT NOT NULL,
route_to TEXT NOT NULL,
question TEXT NOT NULL,
options TEXT NOT NULL CHECK (json_valid(options) AND json_array_length(options) BETWEEN 2 AND 9),
recommendation TEXT NOT NULL,
task_ref TEXT CHECK (task_ref IS NULL OR (task_ref GLOB 'vikunja:[1-9]*/[1-9]*' AND NOT substr(task_ref, 9) GLOB '*[^0-9/]*' AND NOT substr(task_ref, 9) GLOB '*/*/*')), requirement_ref TEXT,
blocking INTEGER NOT NULL CHECK (blocking IN (0,1)),
supersedes TEXT REFERENCES decisions(id)
, spec_path TEXT, spec_commit TEXT) STRICT;
CREATE TABLE decision_events (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
decision TEXT NOT NULL REFERENCES decisions(id),
at TEXT NOT NULL,
op TEXT NOT NULL CHECK (op IN ('seen','resolved','withdrawn','expired')),
by TEXT NOT NULL,
choice TEXT, note TEXT, via TEXT
) STRICT;
CREATE TABLE messages (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
from_role TEXT NOT NULL, from_run TEXT NOT NULL,
to_role TEXT NOT NULL,
class TEXT NOT NULL,
in_reply_to TEXT REFERENCES messages(id),
decision TEXT REFERENCES decisions(id),
corrects TEXT REFERENCES messages(id),
body TEXT NOT NULL
, to_name TEXT) STRICT;
CREATE TABLE deliveries (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
message TEXT NOT NULL REFERENCES messages(id),
at TEXT NOT NULL,
op TEXT NOT NULL CHECK (op IN ('routed','delivered','failed','read')),
holder_run TEXT, transport TEXT, address TEXT, detail TEXT
) STRICT;
CREATE TABLE task_snapshots (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
task_ref TEXT NOT NULL CHECK (task_ref GLOB 'vikunja:[1-9]*/[1-9]*' AND NOT substr(task_ref, 9) GLOB '*[^0-9/]*' AND NOT substr(task_ref, 9) GLOB '*/*/*'),
updated TEXT NOT NULL,
etag TEXT,
digest TEXT NOT NULL CHECK (length(digest) = 64 AND NOT digest GLOB '*[^0-9a-f]*'),
fields TEXT NOT NULL CHECK (json_valid(fields)),
source TEXT NOT NULL CHECK (source IN ('self','poll')),
via TEXT CHECK (via IN ('board','cursor','task','reconcile')),
read_at TEXT,
role TEXT, run TEXT,
CHECK ((source = 'self') = (role IS NOT NULL AND run IS NOT NULL)),
CHECK ((source = 'poll') = (via IS NOT NULL AND read_at IS NOT NULL)),
CHECK (json_type(fields, '$.bucket') IS 'integer'
OR (source IS 'poll' AND via IS 'task' AND json_type(fields, '$.gone') IS 'text'))
) STRICT;
CREATE INDEX task_snapshots_ref ON task_snapshots (business, task_ref, seq);
CREATE TRIGGER decisions_resolve_once BEFORE INSERT ON decision_events
WHEN NEW.op IN ('resolved','withdrawn','expired') AND EXISTS (
SELECT 1 FROM decision_events WHERE decision = NEW.decision AND op IN ('resolved','withdrawn','expired'))
BEGIN SELECT RAISE(ABORT, 'decision already closed'); END;
CREATE TRIGGER decisions_resolved_choice BEFORE INSERT ON decision_events
WHEN NEW.op = 'resolved' AND (NEW.choice IS NULL OR NOT EXISTS (
SELECT 1 FROM decisions d, json_each(d.options) o WHERE d.id = NEW.decision AND json_extract(o.value,'$.key') = NEW.choice))
BEGIN SELECT RAISE(ABORT, 'resolution must name one of the options'); END;
CREATE TRIGGER role_one_holder BEFORE INSERT ON role_claims
WHEN NEW.op = 'claim' AND (SELECT op FROM role_claims WHERE business = NEW.business AND role = NEW.role ORDER BY seq DESC LIMIT 1) = 'claim'
BEGIN SELECT RAISE(ABORT, 'role already held'); END;
CREATE TRIGGER role_release_by_holder BEFORE INSERT ON role_claims
WHEN NEW.op IN ('release','revoke') AND COALESCE((SELECT op FROM role_claims WHERE business = NEW.business AND role = NEW.role ORDER BY seq DESC LIMIT 1),'') <> 'claim'
BEGIN SELECT RAISE(ABORT, 'role is not held'); END;
CREATE TRIGGER role_release_same_run BEFORE INSERT ON role_claims
WHEN NEW.op = 'release' AND (SELECT holder_run FROM role_claims WHERE business = NEW.business AND role = NEW.role ORDER BY seq DESC LIMIT 1) <> NEW.holder_run
BEGIN SELECT RAISE(ABORT, 'only the holder releases; others revoke'); END;
CREATE TRIGGER role_revoke_needs_decision BEFORE INSERT ON role_claims
WHEN NEW.op = 'revoke' AND NEW.decision IS NULL AND NEW.reason IS NULL
BEGIN SELECT RAISE(ABORT, 'revoke needs a resolved decision or a reason'); END;
CREATE TRIGGER meta_no_update BEFORE UPDATE ON meta BEGIN SELECT RAISE(ABORT, 'meta is append-only'); END;
CREATE TRIGGER meta_no_delete BEFORE DELETE ON meta BEGIN SELECT RAISE(ABORT, 'meta is append-only'); END;
CREATE TRIGGER events_no_update BEFORE UPDATE ON events BEGIN SELECT RAISE(ABORT, 'events is append-only'); END;
CREATE TRIGGER events_no_delete BEFORE DELETE ON events BEGIN SELECT RAISE(ABORT, 'events is append-only'); END;
CREATE TRIGGER role_claims_no_update BEFORE UPDATE ON role_claims BEGIN SELECT RAISE(ABORT, 'role_claims is append-only'); END;
CREATE TRIGGER role_claims_no_delete BEFORE DELETE ON role_claims BEGIN SELECT RAISE(ABORT, 'role_claims is append-only'); END;
CREATE TRIGGER decisions_no_update BEFORE UPDATE ON decisions BEGIN SELECT RAISE(ABORT, 'decisions is append-only'); END;
CREATE TRIGGER decisions_no_delete BEFORE DELETE ON decisions BEGIN SELECT RAISE(ABORT, 'decisions is append-only'); END;
CREATE TRIGGER decision_events_no_update BEFORE UPDATE ON decision_events BEGIN SELECT RAISE(ABORT, 'decision_events is append-only'); END;
CREATE TRIGGER decision_events_no_delete BEFORE DELETE ON decision_events BEGIN SELECT RAISE(ABORT, 'decision_events is append-only'); END;
CREATE TRIGGER messages_no_update BEFORE UPDATE ON messages BEGIN SELECT RAISE(ABORT, 'messages is append-only'); END;
CREATE TRIGGER messages_no_delete BEFORE DELETE ON messages BEGIN SELECT RAISE(ABORT, 'messages is append-only'); END;
CREATE TRIGGER deliveries_no_update BEFORE UPDATE ON deliveries BEGIN SELECT RAISE(ABORT, 'deliveries is append-only'); END;
CREATE TRIGGER deliveries_no_delete BEFORE DELETE ON deliveries BEGIN SELECT RAISE(ABORT, 'deliveries is append-only'); END;
CREATE TRIGGER meta_no_replace BEFORE INSERT ON meta WHEN EXISTS (SELECT 1 FROM meta WHERE key = NEW.key) BEGIN SELECT RAISE(ABORT, 'meta is append-only'); END;
CREATE TRIGGER events_no_replace BEFORE INSERT ON events WHEN EXISTS (SELECT 1 FROM events WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'events is append-only'); END;
CREATE TRIGGER role_claims_no_replace BEFORE INSERT ON role_claims WHEN EXISTS (SELECT 1 FROM role_claims WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'role_claims is append-only'); END;
CREATE TRIGGER decisions_no_replace BEFORE INSERT ON decisions WHEN EXISTS (SELECT 1 FROM decisions WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'decisions is append-only'); END;
CREATE TRIGGER decision_events_no_replace BEFORE INSERT ON decision_events WHEN EXISTS (SELECT 1 FROM decision_events WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'decision_events is append-only'); END;
CREATE TRIGGER messages_no_replace BEFORE INSERT ON messages WHEN EXISTS (SELECT 1 FROM messages WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'messages is append-only'); END;
CREATE TRIGGER deliveries_no_replace BEFORE INSERT ON deliveries WHEN EXISTS (SELECT 1 FROM deliveries WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'deliveries is append-only'); END;
CREATE TRIGGER task_snapshots_no_update BEFORE UPDATE ON task_snapshots BEGIN SELECT RAISE(ABORT, 'task_snapshots is append-only'); END;
CREATE TRIGGER task_snapshots_no_delete BEFORE DELETE ON task_snapshots BEGIN SELECT RAISE(ABORT, 'task_snapshots is append-only'); END;
CREATE TRIGGER task_snapshots_no_replace BEFORE INSERT ON task_snapshots WHEN EXISTS (SELECT 1 FROM task_snapshots WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'task_snapshots is append-only'); END;
CREATE TRIGGER decisions_blocking_needs_task BEFORE INSERT ON decisions
WHEN NEW.blocking = 1 AND NEW.task_ref IS NULL
BEGIN SELECT RAISE(ABORT, 'a blocking decision cites the task it blocks'); END;
CREATE TRIGGER events_launch_by_human BEFORE INSERT ON events
WHEN NEW.kind IN ('launch.revoked','launch.restored') AND (NEW.actor_role IS NOT NULL OR NEW.actor_run IS NOT NULL)
BEGIN SELECT RAISE(ABORT, 'only the human revokes or restores launching'); END;
CREATE TRIGGER events_credential_body BEFORE INSERT ON events
WHEN NEW.kind GLOB 'credential.*' AND (
json_extract(NEW.body, '$.service') IS NULL OR json_extract(NEW.body, '$.service') NOT IN ('gitea','vikunja')
OR json_extract(NEW.body, '$.instance') IS NULL)
BEGIN SELECT RAISE(ABORT, 'credential events name a service and a role instance'); END;
CREATE TRIGGER events_task_missing_body BEFORE INSERT ON events
WHEN NEW.kind = 'task.missing' AND (
json_extract(NEW.body, '$.reason') IS NULL OR json_extract(NEW.body, '$.reason') NOT IN ('moved','not-found','no-access')
OR (json_extract(NEW.body, '$.reason') = 'moved' AND json_type(NEW.body, '$.project') IS NOT 'integer'))
BEGIN SELECT RAISE(ABORT, 'task.missing carries a reason, and the new project when moved'); END;
CREATE TRIGGER events_task_subject BEFORE INSERT ON events
WHEN NEW.kind GLOB 'task.*' AND (NEW.subject IS NULL OR NOT (NEW.subject GLOB 'vikunja:[1-9]*/[1-9]*' AND NOT substr(NEW.subject, 9) GLOB '*[^0-9/]*' AND NOT substr(NEW.subject, 9) GLOB '*/*/*'))
BEGIN SELECT RAISE(ABORT, 'a task event names its task in subject'); END;
CREATE TRIGGER events_task_created_body BEFORE INSERT ON events
WHEN NEW.kind = 'task.created' AND (
json_type(NEW.body, '$.request') IS NOT 'text'
OR NOT EXISTS (SELECT 1 FROM events WHERE id = json_extract(NEW.body, '$.request')
AND kind = 'human.input' AND business = NEW.business)
OR json_type(NEW.body, '$.requirement') IS NOT 'text'
OR NOT json_extract(NEW.body, '$.requirement') GLOB 'REQ-[A-Z]*-[1-9]*'
OR json_extract(NEW.body, '$.requirement') GLOB 'REQ-*[^A-Z0-9-]*')
BEGIN SELECT RAISE(ABORT, 'task.created cites the human.input that asked for it and a requirement id'); END;
CREATE VIEW launch_state AS
SELECT business, CASE kind WHEN 'launch.revoked' THEN 'revoked' ELSE 'allowed' END AS state, at
FROM events e WHERE kind IN ('launch.revoked','launch.restored')
AND seq = (SELECT max(seq) FROM events WHERE business = e.business AND kind IN ('launch.revoked','launch.restored'));
CREATE VIEW task_external_changes AS
SELECT p.business, p.task_ref, p.seq, p.via, p.updated, p.digest, ls.digest AS self_digest
FROM task_snapshots p
LEFT JOIN task_snapshots ls ON ls.seq = (SELECT max(seq) FROM task_snapshots
WHERE business = p.business AND task_ref = p.task_ref AND source = 'self')
WHERE p.source = 'poll'
AND p.seq = (SELECT max(seq) FROM task_snapshots WHERE business = p.business AND task_ref = p.task_ref)
AND (ls.seq IS NULL OR (p.updated >= ls.updated AND p.read_at > ls.at AND p.digest <> ls.digest));
CREATE VIEW task_current AS
SELECT s.seq, s.at, s.business, s.task_ref, s.updated, s.etag, s.digest, s.fields, s.source, s.via, s.read_at, s.role, s.run
FROM task_snapshots s
WHERE s.seq = (SELECT max(c.seq) FROM task_snapshots c
WHERE c.business = s.business AND c.task_ref = s.task_ref
AND (c.source = 'self'
OR c.read_at > coalesce((SELECT ls.at FROM task_snapshots ls
WHERE ls.business = c.business AND ls.task_ref = c.task_ref AND ls.source = 'self'
ORDER BY ls.seq DESC LIMIT 1), '')));
CREATE VIEW tasks_open AS
SELECT s.business, s.task_ref, json_extract(s.fields, '$.bucket') AS bucket, s.seq
FROM task_current s
WHERE json_type(s.fields, '$.gone') IS NULL
AND json_extract(s.fields, '$.done') = 0;
CREATE VIEW urgent_inbox AS
SELECT d.id, d.business, d.task_ref, d.question, d.at
FROM decisions d
WHERE d.class = 'gated' AND d.blocking = 1
AND NOT EXISTS (SELECT 1 FROM decision_events x WHERE x.decision = d.id AND x.op IN ('resolved','withdrawn','expired'));
CREATE TABLE task_comment_cursors (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
business TEXT NOT NULL,
task_id INTEGER NOT NULL CHECK (task_id > 0),
last_comment INTEGER NOT NULL CHECK (last_comment > 0)
) STRICT;
CREATE INDEX task_comment_cursors_latest ON task_comment_cursors (business, task_id, seq);
CREATE TRIGGER task_comment_cursors_no_update BEFORE UPDATE ON task_comment_cursors
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
CREATE TRIGGER task_comment_cursors_no_delete BEFORE DELETE ON task_comment_cursors
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
CREATE TRIGGER task_comment_cursors_no_replace BEFORE INSERT ON task_comment_cursors
WHEN EXISTS (SELECT 1 FROM task_comment_cursors WHERE seq = NEW.seq)
BEGIN SELECT RAISE(ABORT, 'task_comment_cursors is append-only'); END;
CREATE TRIGGER task_comment_cursors_forward BEFORE INSERT ON task_comment_cursors
WHEN NEW.last_comment <= COALESCE((SELECT last_comment FROM task_comment_cursors
WHERE business = NEW.business AND task_id = NEW.task_id ORDER BY seq DESC LIMIT 1), 0)
BEGIN SELECT RAISE(ABORT, 'comment cursor must advance'); END;
CREATE TABLE projects (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
name TEXT NOT NULL,
repo TEXT,
created_by TEXT NOT NULL,
UNIQUE (business, name)
) STRICT;
CREATE TABLE threads (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at TEXT NOT NULL,
business TEXT NOT NULL,
project TEXT NOT NULL REFERENCES projects(id),
instance TEXT NOT NULL,
owner TEXT NOT NULL,
name TEXT NOT NULL,
UNIQUE (project, name)
) STRICT;
CREATE TABLE thread_claims (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
op TEXT NOT NULL CHECK (op IN ('claim','release','revoke')),
holder_run TEXT NOT NULL,
harness TEXT NOT NULL, address TEXT,
by TEXT NOT NULL, reason TEXT,
decision TEXT
) STRICT;
CREATE TABLE message_threads (
message TEXT PRIMARY KEY REFERENCES messages(id),
thread TEXT NOT NULL REFERENCES threads(id)
) STRICT;
CREATE TABLE thread_opens (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
thread TEXT NOT NULL REFERENCES threads(id),
by TEXT NOT NULL
) STRICT;
CREATE INDEX thread_claims_thread ON thread_claims (thread, seq);
CREATE INDEX message_threads_thread ON message_threads (thread);
CREATE TRIGGER threads_project_business BEFORE INSERT ON threads
WHEN (SELECT business FROM projects WHERE id = NEW.project) IS NOT NEW.business
BEGIN SELECT RAISE(ABORT, 'a thread belongs to its project''s business'); END;
CREATE TRIGGER thread_one_holder BEFORE INSERT ON thread_claims
WHEN NEW.op = 'claim' AND (SELECT op FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1) = 'claim'
BEGIN SELECT RAISE(ABORT, 'thread already held'); END;
CREATE TRIGGER thread_one_per_run BEFORE INSERT ON thread_claims
WHEN NEW.op = 'claim' AND EXISTS (SELECT 1 FROM thread_claims c WHERE c.holder_run = NEW.holder_run AND c.business = NEW.business
AND c.seq = (SELECT max(seq) FROM thread_claims WHERE thread = c.thread) AND c.op = 'claim')
BEGIN SELECT RAISE(ABORT, 'run already holds a thread'); END;
CREATE TRIGGER thread_release_by_holder BEFORE INSERT ON thread_claims
WHEN NEW.op IN ('release','revoke') AND COALESCE((SELECT op FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1),'') <> 'claim'
BEGIN SELECT RAISE(ABORT, 'thread is not held'); END;
CREATE TRIGGER thread_release_same_run BEFORE INSERT ON thread_claims
WHEN NEW.op = 'release' AND (SELECT holder_run FROM thread_claims WHERE thread = NEW.thread ORDER BY seq DESC LIMIT 1) <> NEW.holder_run
BEGIN SELECT RAISE(ABORT, 'only the holder releases; others revoke'); END;
CREATE TRIGGER thread_revoke_needs_decision BEFORE INSERT ON thread_claims
WHEN NEW.op = 'revoke' AND NEW.decision IS NULL AND NEW.reason IS NULL
BEGIN SELECT RAISE(ABORT, 'revoke needs a resolved decision or a reason'); END;
CREATE TRIGGER thread_claims_business BEFORE INSERT ON thread_claims
WHEN (SELECT business FROM threads WHERE id = NEW.thread) IS NOT NEW.business
BEGIN SELECT RAISE(ABORT, 'a thread claim names the thread''s business'); END;
CREATE TRIGGER message_threads_match BEFORE INSERT ON message_threads
WHEN NOT EXISTS (SELECT 1 FROM messages m JOIN threads t ON t.id = NEW.thread
WHERE m.id = NEW.message AND m.business = t.business AND t.instance IN (m.to_role, m.from_role))
BEGIN SELECT RAISE(ABORT, 'a threaded message is to or from the thread''s instance'); END;
CREATE TRIGGER delivery_same_thread BEFORE INSERT ON deliveries
WHEN NEW.op IN ('routed','delivered') AND (NEW.op = 'delivered' OR NEW.holder_run IS NOT NULL)
AND EXISTS (SELECT 1 FROM message_threads mt JOIN messages m ON m.id = mt.message JOIN threads t ON t.id = mt.thread
WHERE mt.message = NEW.message AND m.to_role = t.instance
AND NOT EXISTS (SELECT 1 FROM thread_claims c WHERE c.seq = (SELECT max(seq) FROM thread_claims WHERE thread = t.id)
AND c.op = 'claim' AND c.holder_run IS NEW.holder_run))
BEGIN SELECT RAISE(ABORT, 'a threaded message goes only to its thread''s holder'); END;
CREATE TRIGGER projects_no_update BEFORE UPDATE ON projects BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER projects_no_delete BEFORE DELETE ON projects BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER projects_no_replace BEFORE INSERT ON projects WHEN EXISTS (SELECT 1 FROM projects WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'projects is append-only'); END;
CREATE TRIGGER threads_no_update BEFORE UPDATE ON threads BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER threads_no_delete BEFORE DELETE ON threads BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER threads_no_replace BEFORE INSERT ON threads WHEN EXISTS (SELECT 1 FROM threads WHERE seq = NEW.seq OR id = NEW.id) BEGIN SELECT RAISE(ABORT, 'threads is append-only'); END;
CREATE TRIGGER thread_claims_no_update BEFORE UPDATE ON thread_claims BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER thread_claims_no_delete BEFORE DELETE ON thread_claims BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER thread_claims_no_replace BEFORE INSERT ON thread_claims WHEN EXISTS (SELECT 1 FROM thread_claims WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'thread_claims is append-only'); END;
CREATE TRIGGER message_threads_no_update BEFORE UPDATE ON message_threads BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER message_threads_no_delete BEFORE DELETE ON message_threads BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER message_threads_no_replace BEFORE INSERT ON message_threads WHEN EXISTS (SELECT 1 FROM message_threads WHERE message = NEW.message) BEGIN SELECT RAISE(ABORT, 'message_threads is append-only'); END;
CREATE TRIGGER thread_opens_no_update BEFORE UPDATE ON thread_opens BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
CREATE TRIGGER thread_opens_no_delete BEFORE DELETE ON thread_opens BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
CREATE TRIGGER thread_opens_no_replace BEFORE INSERT ON thread_opens WHEN EXISTS (SELECT 1 FROM thread_opens WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'thread_opens is append-only'); END;
CREATE TABLE instance_names (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
instance TEXT NOT NULL,
name TEXT NOT NULL,
by TEXT NOT NULL
) STRICT;
CREATE TRIGGER instance_names_unique BEFORE INSERT ON instance_names
WHEN EXISTS (SELECT 1 FROM instance_names n WHERE n.business = NEW.business AND n.name = NEW.name
AND n.instance <> NEW.instance
AND n.seq = (SELECT max(seq) FROM instance_names WHERE business = n.business AND instance = n.instance))
BEGIN SELECT RAISE(ABORT, 'an instance name is unique in its business'); END;
CREATE TRIGGER instance_names_no_update BEFORE UPDATE ON instance_names BEGIN SELECT RAISE(ABORT, 'instance_names is append-only'); END;
CREATE TRIGGER instance_names_no_delete BEFORE DELETE ON instance_names BEGIN SELECT RAISE(ABORT, 'instance_names is append-only'); END;
CREATE TRIGGER instance_names_no_replace BEFORE INSERT ON instance_names WHEN EXISTS (SELECT 1 FROM instance_names WHERE seq = NEW.seq) BEGIN SELECT RAISE(ABORT, 'instance_names is append-only'); END;
-- Row 124 (#1618): immutable source snapshots and turn outcomes, never transcript text.
CREATE TABLE usage_records (
seq INTEGER PRIMARY KEY AUTOINCREMENT,
at TEXT NOT NULL,
business TEXT NOT NULL,
run TEXT NOT NULL,
instance TEXT NOT NULL,
provider TEXT,
model TEXT,
account TEXT,
turn TEXT NOT NULL,
source TEXT NOT NULL,
phase TEXT NOT NULL CHECK (phase IN ('begin','end')),
status TEXT NOT NULL CHECK (status IN ('known','unknown')),
code TEXT,
cursor TEXT CHECK (cursor IS NULL OR json_valid(cursor)),
input INTEGER,
output INTEGER,
cache_write INTEGER,
cache_read INTEGER,
UNIQUE (business,run,turn,source,phase),
CHECK ((status='unknown' AND input IS NULL AND output IS NULL AND cache_write IS NULL AND cache_read IS NULL)
OR (status='known' AND input >= 0 AND output >= 0 AND cache_write >= 0 AND cache_read >= 0
AND input IS NOT NULL AND output IS NOT NULL AND cache_write IS NOT NULL AND cache_read IS NOT NULL))
) STRICT;
CREATE TRIGGER usage_records_no_update BEFORE UPDATE ON usage_records BEGIN SELECT RAISE(ABORT, 'usage_records is append-only'); END;
CREATE TRIGGER usage_records_no_delete BEFORE DELETE ON usage_records BEGIN SELECT RAISE(ABORT, 'usage_records is append-only'); END;
CREATE TRIGGER usage_records_no_replace BEFORE INSERT ON usage_records WHEN EXISTS (SELECT 1 FROM usage_records WHERE seq=NEW.seq OR (business=NEW.business AND run=NEW.run AND turn=NEW.turn AND source=NEW.source AND phase=NEW.phase)) BEGIN SELECT RAISE(ABORT, 'usage_records is append-only'); END;
CREATE TABLE hosts (
business TEXT NOT NULL,
host TEXT NOT NULL,
address TEXT NOT NULL,
port INTEGER NOT NULL CHECK (port BETWEEN 1 AND 65535),
user TEXT NOT NULL,
sudo INTEGER NOT NULL CHECK (sudo IN (0,1)),
host_keys TEXT NOT NULL CHECK (json_valid(host_keys) AND json_type(host_keys) = 'array' AND json_array_length(host_keys) > 0),
installer_fingerprint TEXT,
state TEXT NOT NULL CHECK (state IN ('pending-install','ready')),
decision TEXT NOT NULL REFERENCES decisions(id),
checked_at TEXT,
PRIMARY KEY (business, host),
CHECK (state = 'pending-install' OR (installer_fingerprint IS NOT NULL AND checked_at IS NOT NULL))
) STRICT;
CREATE TABLE host_grants (
business TEXT NOT NULL,
agent TEXT NOT NULL,
host TEXT NOT NULL,
commands TEXT NOT NULL CHECK (json_valid(commands) AND json_type(commands) = 'array' AND json_array_length(commands) > 0),
rotate_by TEXT NOT NULL,
tag TEXT NOT NULL CHECK (length(tag) = 39 AND tag GLOB 'mosaic-*' AND NOT substr(tag, 8) GLOB '*[^0-9a-f]*'),
decision TEXT NOT NULL REFERENCES decisions(id),
PRIMARY KEY (business, agent, host),
FOREIGN KEY (business, host) REFERENCES hosts(business, host)
) STRICT;
CREATE TRIGGER events_grant_actor BEFORE INSERT ON events
WHEN NEW.kind = 'action.allowed'
AND (json_extract(NEW.body, '$.action') GLOB 'grant.*' OR json_extract(NEW.body, '$.operation') GLOB 'grant.*')
AND NOT (NEW.actor_role IS NULL AND NEW.actor_run IS 'grant')
BEGIN SELECT RAISE(ABORT, 'only the grant writer records grant events'); END;
CREATE TABLE gitea_users (
business TEXT NOT NULL,
agent TEXT NOT NULL,
user TEXT NOT NULL,
id INTEGER NOT NULL CHECK (id > 0),
nonce TEXT NOT NULL CHECK (length(nonce) = 16 AND NOT nonce GLOB '*[^0-9a-f]*'),
decision TEXT NOT NULL REFERENCES decisions(id),
PRIMARY KEY (business, agent)
) STRICT;
CREATE TABLE gitea_grants (
business TEXT NOT NULL,
agent TEXT NOT NULL,
user TEXT NOT NULL,
token_id INTEGER NOT NULL CHECK (token_id > 0),
token_name TEXT NOT NULL,
projects TEXT NOT NULL CHECK (json_valid(projects) AND json_type(projects) = 'array' AND json_array_length(projects) > 0),
rotate_by TEXT NOT NULL,
decision TEXT NOT NULL REFERENCES decisions(id),
PRIMARY KEY (business, agent),
FOREIGN KEY (business, agent) REFERENCES gitea_users(business, agent)
) STRICT;
@@ -0,0 +1,200 @@
// Row 236 (#1742, #436b r5 section 9, X-G3): the grant path end to end on a scratch gitea/gitea:1.27.1, as
// section 1's facts were found: a throwaway container on 127.0.0.1 over TLS with row 199's scratch CA, its
// data inside the container, removed after. The test never pulls an image; without Docker or the image it
// skips. The admin and seat tokens are scratch tokens this container made; none is printed, and no test
// here reaches git.mosaicstack.dev or any provider.
import test from 'node:test';
import assert from 'node:assert/strict';
import { randomBytes } from 'node:crypto';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync } from 'node:fs';
import { request } from 'node:https';
import { createServer } from 'node:net';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { Broker, GRANT_EVIDENCE } from '../src/broker.mjs';
import { Credentials } from '../src/credentials.mjs';
import { SEAT_SCOPES, giteaGrant, giteaKeys } from '../src/gitea-grant.mjs';
import { Store } from '../src/store.mjs';
import { scratchCa } from '../../seat/tests/fake-gitea.mjs';
const IMAGE = 'gitea/gitea:1.27.1';
const USER = 'demo-coder';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const dayFrom = (n) => new Date(Date.now() + n * 86400000).toISOString().slice(0, 10);
const none = { authority: { withinRole: ['message.send'], crossRole: [] } };
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('X-G3: on scratch 1.27.1, a grant makes the user, mints, forks, checks reach, writes the file, and revoke deletes the token', { skip, timeout: 300000 }, async (t) => {
const dir = mkdtempSync(join(tmpdir(), 'g236-docker-'));
t.after(() => rmSync(dir, { recursive: true, force: true }));
const tlsDir = join(dir, 'tls');
const 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');
const port = await freePort();
const url = `https://127.0.0.1:${port}`;
const name = 'g236-' + 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, started.stderr);
const end = Date.now() + 120000;
while ((await call(url, ca, 'GET', '/api/v1/version')).status !== 200) {
if (Date.now() > end) assert.fail('the scratch Gitea did not answer in 120 s: ' + docker('logs', '--tail', '20', name).stdout);
await sleep(500);
}
const gitea = (...args) => docker('exec', '-u', 'git', name, 'gitea', ...args);
const password = () => randomBytes(18).toString('base64url');
for (const [user, admin] of [['mosaic-grant', true], ['acme', false]]) {
const r = gitea('admin', 'user', 'create', '--username', user, '--email', `${user}@example.test`, '--password', password(), '--must-change-password=false', ...(admin ? ['--admin'] : []));
assert.equal(r.status, 0, r.stderr);
}
const minted = gitea('admin', 'user', 'generate-access-token', '--username', 'mosaic-grant', '--token-name', 'mosaic-grant-bootstrap',
'--scopes', 'write:admin,write:repository,write:user', '--raw');
assert.equal(minted.status, 0, 'generate-access-token failed');
const adminToken = minted.stdout.trim();
assert.match(adminToken, /^[0-9a-f]{40}$/, 'the admin token has the shape of a Gitea token');
const basic = 'Basic ' + Buffer.from(`mosaic-grant:${adminToken}`).toString('base64');
for (const repo of ['app', 'lib']) {
const r = await call(url, ca, 'POST', '/api/v1/admin/users/acme/repos', { auth: 'token ' + adminToken, body: { name: repo, auto_init: true } });
assert.equal(r.status, 201);
}
const configDir = join(dir, 'cfg');
mkdirSync(configDir, { mode: 0o700 });
mkdirSync(join(dir, 'data'), { mode: 0o700 });
const storeRoot = join(configDir, 'secrets', 'agents');
const store = new Store(join(dir, 'data'));
const credentials = new Credentials({ references: {}, storeRoot, dataRoot: join(dir, 'data') });
t.after(() => {
credentials.close();
store.close();
});
const business = {
id: 'demo',
human: 'jason',
arbiters: { technical: 'pm', delivery: 'pm' },
roles: {
chief: { definition: 'chief-of-staff', ...none },
pm: { definition: 'pm', ...none },
coder: { definition: 'coder', ...none, identity: { name: 'Coder', email: '[email protected]', gitea: { url, user: USER } }, publish: ['app', 'lib'] },
},
definitions: {
'chief-of-staff': { roleVersion: 2, credentials: [] },
pm: { roleVersion: 2, credentials: [] },
coder: { roleVersion: 2, credentials: [{ service: 'gitea' }] },
},
projects: { app: { remote: `${url}/acme/app` }, lib: { remote: `${url}/acme/lib` } },
vars: {},
};
const broker = new Broker({
store,
businesses: { demo: business },
secretCheck: (value) => credentials.assertClean(value),
giteaGrant: giteaGrant({ configDir, ca, timeoutMs: 20000 }),
giteaKeys: giteaKeys({ credentials, storeRoot }),
});
const chief = broker.bindLaunch({ business: 'demo', role: 'chief', run: 'chief-run', harness: 'pi', address: 'chief' });
broker.request(chief, { verb: 'role.claim' });
const human = broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
// C1 to C4 on the real server, then the slot is written.
await broker.request(human, { verb: 'grants.admin.set', args: { service: 'gitea', url, account: 'mosaic-grant', rotateBy: dayFrom(60), token: adminToken + '\n' } });
const d = await broker.request(chief, {
verb: 'decision.raise',
args: {
action: 'credential.mint', question: 'Grant coder Gitea?', options: [{ key: 'yes', text: 'Yes' }, { key: 'no', text: 'No' }], recommendation: 'yes', blocking: false,
grant: { method: 'gitea', agent: 'coder', target: { projects: ['app'] }, rotateBy: dayFrom(30) },
},
});
broker.request(human, { verb: 'decision.resolve', args: { id: d.id, choice: 'yes' } });
const events = () => store.all(`SELECT body FROM events WHERE business='demo' AND ${GRANT_EVIDENCE} ORDER BY seq`).map((e) => JSON.parse(e.body));
const ended = () => events().find((e) => e.grantDecision === d.id && ['grant.applied', 'grant.failed'].includes(e.action));
const until = Date.now() + 120000;
while (!ended() && Date.now() < until) await sleep(100);
await broker.giteaIdle();
const applied = ended();
assert.equal(applied?.action, 'grant.applied', JSON.stringify(applied));
// The user, made with the nonce, and its id recorded.
const nonce = events().find((e) => e.grantDecision === d.id && e.step === 'user').nonce;
const u = await call(url, ca, 'GET', `/api/v1/users/${USER}`, { auth: 'token ' + adminToken });
assert.equal(u.status, 200);
assert.equal(u.json.full_name, `Coder [mosaic ${nonce}]`);
assert.equal(u.json.is_admin, false);
const row = store.get("SELECT id, nonce FROM gitea_users WHERE business='demo' AND agent='coder'");
assert.deepEqual({ ...row }, { id: u.json.id, nonce });
// One token, with the two scopes, at the id the record names.
const list = await call(url, ca, 'GET', `/api/v1/users/${USER}/tokens`, { auth: basic });
assert.equal(list.status, 200);
assert.equal(list.json.length, 1);
assert.deepEqual([...list.json[0].scopes].sort(), [...SEAT_SCOPES]);
const grant = store.get("SELECT token_id FROM gitea_grants WHERE business='demo' AND agent='coder'");
assert.equal(list.json[0].id, grant.token_id);
// The fork, with its parent.
const fork = await call(url, ca, 'GET', `/api/v1/repos/${USER}/app`, { auth: 'token ' + adminToken });
assert.equal(fork.status, 200);
assert.equal(fork.json.parent?.full_name, 'acme/app');
// The 0600 file, whose token Gitea knows as the recorded id; the value is compared, never printed.
const file = join(storeRoot, 'demo', 'coder', 'gitea');
assert.equal(statSync(file).mode & 0o777, 0o600);
const who = await call(url, ca, 'GET', '/api/v1/token', { auth: 'token ' + readFileSync(file, 'utf8').trim() });
assert.equal(who.status, 200);
assert.equal(who.json.id, grant.token_id);
// Revoke deletes the token at Gitea: gone from the list, the file and the record gone.
const out = await broker.request(human, { verb: 'grants.revoke', args: { service: 'gitea', agent: 'coder', reason: 'X-G3' } });
assert.deepEqual(out.serviceLeft, []);
await broker.giteaIdle();
const after = await call(url, ca, 'GET', `/api/v1/users/${USER}/tokens`, { auth: basic });
assert.equal(after.status, 200);
assert.deepEqual(after.json, []);
assert.equal(existsSync(file), false);
assert.equal(store.get("SELECT count(*) AS n FROM gitea_grants WHERE business='demo'").n, 0);
// No scratch token reached the store.
const text = store.all("SELECT body FROM events WHERE business='demo'").map((r) => r.body).join('\n');
assert.ok(!text.includes(adminToken), 'the admin token reached the store');
});
File diff suppressed because it is too large. Load diff
+163
View File
@@ -0,0 +1,163 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { createHash } from 'node:crypto';
import { DatabaseSync } from 'node:sqlite';
import { Store, schemaDigest } from '../src/store.mjs';
// Row 236 (#1742, #436b r5 section 7): the 3j→3k step adds the gitea_users and gitea_grants tables and
// nothing else. It runs on a 3j store built here from the pinned 3j schema (02da6a2ae's schema.sql) and
// fixture rows. No live store is read or copied.
const AT = '2026-10-21T00:00:00.000Z';
const ddl3j = readFileSync(new URL('./fixtures/schema-3j.sql', import.meta.url), 'utf8');
const here = (p) => new URL(p, import.meta.url);
const NEW = ['gitea_users', 'gitea_grants', 'sqlite_autoindex_gitea_users_1', 'sqlite_autoindex_gitea_grants_1'];
function source3j(t) {
const root = mkdtempSync(join(tmpdir(), 'gitea-migrate-'));
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);
assert.equal(createHash('sha256').update(ddl3j).digest('hex'), '0a7b633ce7eec9d0bb37c3255f4244d97bd0c8eac38db91a11b4ef6cc982ec8c');
db.exec(ddl3j);
const run = (sql, ...args) => db.prepare(sql).run(...args);
run('INSERT INTO meta VALUES (?,?)', 'schema_version', '3j');
run('INSERT INTO meta VALUES (?,?)', 'schema_digest', schemaDigest(db));
run('INSERT INTO meta VALUES (?,?)', 'comment_cursor_epoch', AT);
run(`INSERT INTO decisions(id,at,business,raised_by_role,raised_by_run,class,action,route_to,question,options,recommendation,blocking)
VALUES('d1',?,'demo','chief','run1','gated','credential.mint','human','q','["yes","no"]','yes',0)`, AT);
run('INSERT INTO events(id,at,business,kind,actor_role,actor_run,body) VALUES(?,?,?,?,?,?,?)', 'e1', AT, 'demo', 'action.allowed', null, 'grant', '{"operation":"grant.host-enroll","decision":"d1"}');
run('INSERT INTO events(id,at,business,kind,body) VALUES(?,?,?,?,?)', 'e2', AT, 'demo', 'action.allowed', '{}');
db.close(); chmodSync(path, 0o600);
return { root, path };
}
// Row 264a (#1769): what the 3k→3l step rebuilds or adds. sessions.test.mjs tests those objects.
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
const later3l = (r) => SESSIONS_3L.includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_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')").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(); }
}
function master(path) {
const db = new DatabaseSync(path, { readOnly: true });
try {
return db.prepare('SELECT type,name,tbl_name,sql FROM sqlite_master ORDER BY type,name').all().map(r => ({ ...r }));
} finally { db.close(); }
}
const version = (path) => {
const db = new DatabaseSync(path, { readOnly: true });
try { return db.prepare("SELECT value FROM meta WHERE key='schema_version'").get().value; } finally { db.close(); }
};
// A copy of the bus module whose store.mjs, schema.sql and 3j→3k step the caller edits.
async function engine(root, name, { store, schema, step = (s) => s }) {
const moduleRoot = join(root, name);
mkdirSync(join(moduleRoot, 'src/migrations'), { recursive: true });
const source = readFileSync(here('../src/store.mjs'), 'utf8'), edited = store ? store(source) : source;
if (store) assert.notEqual(edited, source);
writeFileSync(join(moduleRoot, 'src/store.mjs'), edited);
writeFileSync(join(moduleRoot, 'schema.sql'), schema ?? readFileSync(here('../schema.sql'), 'utf8'));
for (const file 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'])
copyFileSync(here('../src/migrations/' + file), join(moduleRoot, 'src/migrations', file));
writeFileSync(join(moduleRoot, 'src/migrations/3j-3k.sql'), step(readFileSync(here('../src/migrations/3j-3k.sql'), 'utf8')));
copyFileSync(here('../src/migrations/3k-3l.sql'), join(moduleRoot, 'src/migrations/3k-3l.sql'));
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
}
function fresh(t) {
const root = mkdtempSync(join(tmpdir(), 'gitea-store-'));
const store = new Store(root);
t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
store.run(`INSERT INTO decisions(id,at,business,raised_by_role,raised_by_run,class,action,route_to,question,options,recommendation,blocking)
VALUES('d1',?,'demo','chief','run1','gated','credential.mint','human','q','["yes","no"]','yes',0)`, AT);
return store;
}
const user = (store, values = {}) => store.run(
'INSERT INTO gitea_users(business,agent,user,id,nonce,decision) VALUES(:business,:agent,:user,:id,:nonce,:decision)',
{ business: 'demo', agent: 'coder', user: 'demo-coder', id: 7, nonce: '0123456789abcdef', decision: 'd1', ...values });
const grant = (store, values = {}) => store.run(
'INSERT INTO gitea_grants(business,agent,user,token_id,token_name,projects,rotate_by,decision) VALUES(:business,:agent,:user,:token,:name,:projects,:rotate,:decision)',
{ business: 'demo', agent: 'coder', user: 'demo-coder', token: 9, name: 'mosaic-demo-coder-gitea-d1', projects: '["app"]', rotate: '2026-12-01', decision: 'd1', ...values });
test('3j copy migrates to 3k: every row kept, and sqlite_master gains only the two tables and their keys', 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), '3l');
assert.deepEqual(rows(path), before);
const now = master(path);
assert.deepEqual(now.filter(r => !NEW.includes(r.name) && !later3l(r)), was.filter(r => !later3l(r)));
assert.deepEqual(now.filter(r => NEW.includes(r.name)).map(r => r.name).sort(), [...NEW].sort());
const store = new Store(root);
try {
assert.equal(store.get('PRAGMA quick_check').quick_check, 'ok');
assert.deepEqual(store.all('PRAGMA foreign_key_check'), []);
assert.equal(store.get('SELECT count(*) AS n FROM gitea_users').n, 0);
assert.equal(store.get('SELECT count(*) AS n FROM gitea_grants').n, 0);
user(store);
grant(store);
} finally { store.close(); }
});
test('a 3j→3k step that fails partway leaves the store at 3j with every row and the schema intact', async t => {
const { root, path } = source3j(t), before = rows(path), was = master(path);
// The real step plus a statement that fails only when events has a row, so the empty replay at
// import passes and the store's run fails after both tables exist.
const Failing = await engine(root, 'engine-failing', { step: (sql) => sql + 'INSERT INTO events SELECT * FROM events LIMIT 1;\n' });
assert.throws(() => new Failing(root, { migrateSchema: true }), /events is append-only/);
assert.equal(version(path), '3j');
assert.deepEqual(master(path), was);
assert.deepEqual(rows(path), before);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3l');
});
test('the 3j engine refuses a migrated store, migrating or not, and leaves it as it was', async t => {
const { root, path } = source3j(t);
new Store(root, { migrateSchema: true }).close();
const after = rows(path), was = master(path);
const Store3j = await engine(root, 'engine-3j', {
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 = '3l';", "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), '3l');
assert.deepEqual(master(path), was);
assert.deepEqual(rows(path), after);
});
test('gitea_users: one user per business and agent, an id above 0, a 16-hex nonce and a real decision', t => {
const store = fresh(t);
user(store);
assert.throws(() => user(store, { user: 'other' }), /UNIQUE constraint failed: gitea_users\.business, gitea_users\.agent/);
user(store, { business: 'other' });
for (const nonce of ['0123456789abcde', '0123456789abcdef0', '0123456789ABCDEF', '0123456789abcdeg'])
assert.throws(() => user(store, { agent: 'x', nonce }), /CHECK constraint failed/, nonce);
assert.throws(() => user(store, { agent: 'x', id: 0 }), /CHECK constraint failed/);
assert.throws(() => user(store, { agent: 'x', id: 'seven' }), /cannot store TEXT value in INTEGER column/);
assert.throws(() => user(store, { agent: 'x', decision: 'nope' }), /FOREIGN KEY constraint failed/);
});
test('gitea_grants: one grant per agent, a projects array, a token id above 0, and only for a recorded user', t => {
const store = fresh(t);
assert.throws(() => grant(store), /FOREIGN KEY constraint failed/);
user(store);
for (const projects of ['[]', '{}', '"app"', 'not json'])
assert.throws(() => grant(store, { projects }), /CHECK constraint failed/, projects);
assert.throws(() => grant(store, { token: 0 }), /CHECK constraint failed/);
assert.throws(() => grant(store, { decision: 'nope' }), /FOREIGN KEY constraint failed/);
grant(store);
assert.throws(() => grant(store, { token: 10 }), /UNIQUE constraint failed/);
assert.throws(() => store.run("DELETE FROM gitea_users WHERE business='demo' AND agent='coder'"), /FOREIGN KEY constraint failed/);
store.run("DELETE FROM gitea_grants WHERE business='demo' AND agent='coder'");
store.run("DELETE FROM gitea_users WHERE business='demo' AND agent='coder'");
});
+15 -8
View File
@@ -16,6 +16,11 @@ const TAG = 'mosaic-' + 'a'.repeat(32);
const ddl3i = readFileSync(new URL('./fixtures/schema-3i.sql', import.meta.url), 'utf8');
const here = (p) => new URL(p, import.meta.url);
const NEW = ['hosts', 'host_grants', 'events_grant_actor', 'sqlite_autoindex_hosts_1', 'sqlite_autoindex_host_grants_1'];
// Row 236 (#1742): 3j→3k's objects, after this step. Their test is gitea-migration.test.mjs.
const LATER = ['gitea_users', 'gitea_grants', 'sqlite_autoindex_gitea_users_1', 'sqlite_autoindex_gitea_grants_1'];
// Row 264a (#1769): what the 3k→3l step rebuilds or adds. sessions.test.mjs tests those objects.
const SESSIONS_3L = ['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens'];
const later3l = (r) => SESSIONS_3L.includes(r.tbl_name) || ['delivery_same_thread', 'delivery_same_session'].includes(r.name);
function source3i(t) {
const root = mkdtempSync(join(tmpdir(), 'grant-migrate-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
@@ -35,7 +40,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')").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')").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(); }
@@ -61,6 +66,8 @@ async function engine(root, name, { store, schema, step = (s) => s }) {
for (const file of ['3b.sql', '3b-3c.sql', '3c-3d.sql', '3d-3e.sql', '3e-3f.sql', '3f-3g.sql', '3g-3h.sql', '3h-3i.sql'])
copyFileSync(here('../src/migrations/' + file), join(moduleRoot, 'src/migrations', file));
writeFileSync(join(moduleRoot, 'src/migrations/3i-3j.sql'), step(readFileSync(here('../src/migrations/3i-3j.sql'), 'utf8')));
copyFileSync(here('../src/migrations/3j-3k.sql'), join(moduleRoot, 'src/migrations/3j-3k.sql'));
copyFileSync(here('../src/migrations/3k-3l.sql'), join(moduleRoot, 'src/migrations/3k-3l.sql'));
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
}
function fresh(t) {
@@ -84,10 +91,10 @@ 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), '3j');
assert.equal(version(path), '3l');
assert.deepEqual(rows(path), before);
const now = master(path);
assert.deepEqual(now.filter(r => !NEW.includes(r.name)), was);
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)));
assert.deepEqual(now.filter(r => NEW.includes(r.name)).map(r => r.name).sort(), [...NEW].sort());
const store = new Store(root);
try {
@@ -109,10 +116,10 @@ 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), '3j');
assert.equal(version(path), '3l');
});
test('the 3i engine refuses a 3j store, migrating or not, and leaves it at 3j', async t => {
test('the 3i engine refuses a migrated store, migrating or not, and leaves it as it was', async t => {
const { root, path } = source3i(t);
new Store(root, { migrateSchema: true }).close();
const after = rows(path), was = master(path);
@@ -120,14 +127,14 @@ test('the 3i engine refuses a 3j store, migrating or not, and leaves it at 3j',
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 = '3j';", "export const SCHEMA_VERSION = '3i';")
.replace("export const SCHEMA_VERSION = '3l';", "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), '3j');
assert.equal(version(path), '3l');
assert.deepEqual(master(path), was);
assert.deepEqual(rows(path), after);
});
+1 -1
View File
@@ -55,7 +55,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, '3j');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3l');
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) });
@@ -1,65 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { startBroker } from '../src/runtime.mjs';
import { Client } from '../src/client.mjs';
import { registerRuntime, updateRuntime, processIdentity } from '../../processing-control/src/runtimes.mjs';
import { readControl } from '../../processing-control/src/state.mjs';
const businesses = { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'coder', delivery: 'coder' },
roles: { coder: { authority: { withinRole: ['message.send'], crossRole: [] } } } } };
async function broker(root) {
const r = await startBroker({ dataRoot: root, businesses, readers: ['demo'] });
const cap = r.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
return { r, client: new Client({ path: r.path, cap, timeout: 15000 }) };
}
test('real runtime injects the coordinator, stops its workload and mirrors stable stop events across restart', async t => {
const root = mkdtempSync(join(tmpdir(), 'processing-runtime-bus-'));
let live;
const p = spawn(process.execPath, ['-e', "console.log('ready');setInterval(()=>{},1000)"], { detached: true, stdio: ['ignore', 'pipe', 'ignore'] });
const exit = once(p, 'exit');
t.after(async () => {
await live?.close();
if (p.exitCode === null && p.signalCode === null) { try { process.kill(-p.pid, 'SIGKILL'); } catch {} await exit; }
rmSync(root, { recursive: true, force: true });
});
await once(p.stdout, 'data');
let current = await broker(root); live = current.r;
const receipt = registerRuntime(root, { kind: 'session', business: 'demo', run: 'runtime-fixture' });
updateRuntime(root, receipt.id, { ...processIdentity(p.pid), phase: 'running', group: true });
const halted = await current.client.call('processing.halt');
assert.equal(halted.halted, true);
assert.equal(halted.quiescent, true);
await exit;
assert.equal(processIdentity(p.pid), null);
await assert.rejects(live.mayLaunch({ business: 'demo', instance: 'coder', provider: 'fixture', model: 'fixture' }), { code: 'processing-halted' });
await live.close(); live = null;
current = await broker(root); live = current.r;
assert.equal((await current.client.call('processing.status')).halted, true);
const events = [halted.generation, receipt.id].flatMap(subject =>
live.broker.request(live.readers[0].cap, { verb: 'trail', args: { subject } }));
assert.equal(events.filter(e => e.body?.action === 'processing.halt').length, 1);
assert.equal(events.filter(e => e.body?.action === 'processing.stop').length, 1);
assert.equal((await current.client.call('processing.resume', { generation: halted.generation })).halted, false);
});
test('invalid state still starts the control bus and human halt then resume repairs it', async t => {
const root = mkdtempSync(join(tmpdir(), 'processing-repair-bus-'));
let live;
t.after(async () => { await live?.close(); rmSync(root, { recursive: true, force: true }); });
let current = await broker(root); live = current.r;
await live.close(); live = null;
writeFileSync(join(root, 'control', 'processing.json'), '{');
current = await broker(root); live = current.r;
assert.equal((await current.client.call('processing.status')).code, 'processing-state-invalid');
await assert.rejects(current.client.call('processing.resume', { generation: 'bad' }), { code: 'processing-state-invalid' });
const halted = await current.client.call('processing.halt');
assert.equal(halted.recovered, true);
assert.equal((await current.client.call('processing.resume', { generation: halted.generation })).halted, false);
assert.equal(readControl(root).halted, false);
});
@@ -1,31 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createServer } from 'node:net';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Client } from '../src/client.mjs';
test('processing halt and resume extend only the socket timeout while ordinary status retains the explicit timeout', async t => {
const root = mkdtempSync(join(tmpdir(), 'processing-client-')), path = join(root, 'fixture.sock');
const seen = [], sockets = new Set();
const server = createServer(socket => {
sockets.add(socket); let buffer = '', timer;
socket.on('error', () => {});
socket.on('close', () => { sockets.delete(socket); clearTimeout(timer); });
socket.on('data', data => {
buffer += data;
if (!buffer.includes('\n')) return;
seen.push(JSON.parse(buffer));
timer = setTimeout(() => socket.end(JSON.stringify({ ok: true, result: { real: 'completion' } }) + '\n'), 80);
});
});
await new Promise(resolve => server.listen(path, resolve));
t.after(async () => { for (const socket of sockets) socket.destroy(); await new Promise(resolve => server.close(resolve)); rmSync(root, { recursive: true, force: true }); });
const client = new Client({ path, cap: 'a'.repeat(64), timeout: 15 });
for (const verb of ['processing.halt', 'processing.resume']) {
assert.deepEqual(await client.call(verb, {}), { real: 'completion' });
assert.equal(client.timeout, 15, 'the constructor setting stays unchanged');
}
await assert.rejects(client.call('processing.status'), { code: 'outcome-unknown' });
assert.deepEqual(seen, ['processing.halt', 'processing.resume', 'processing.status'].map(verb => ({ cap: 'a'.repeat(64), verb, args: {} })));
});
@@ -1,22 +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 { initializeControl, transition } from '../../processing-control/src/state.mjs';
import { withProcessing } from '../../processing-control/src/effects.mjs';
import { webService } from '../src/web.mjs';
test('a root halt aborts the active search-source signal before the normal web deadline', async t => {
const root = mkdtempSync(join(tmpdir(), 'processing-web-')); initializeControl(root);
t.after(() => rmSync(root, { recursive: true, force: true }));
let ready, observed;
const started = new Promise(resolve => { ready = resolve; });
const service = webService({ deadlineMs: 3000, search: { search: (_query, { signal }) => new Promise((_resolve, reject) => {
observed = signal; signal.addEventListener('abort', () => reject(signal.reason), { once: true }); ready();
}) } });
const call = withProcessing(root, () => service.handle('web.search', { query: 'fixture' }));
await started; const began = Date.now(); transition(root, { business: 'demo', human: 'jason', halted: true });
await call.catch(() => {});
assert.equal(observed.aborted, true); assert.equal(observed.reason.name, 'ProcessingError');
assert.ok(Date.now() - began < 1500);
});
-143
View File
@@ -1,143 +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 { Broker } from '../src/broker.mjs';
import { Store } from '../src/store.mjs';
import { serve } from '../src/server.mjs';
import { Client } from '../src/client.mjs';
const state = await import('../../processing-control/src/state.mjs').catch(e => {
if (e.code === 'ERR_MODULE_NOT_FOUND') return {};
throw e;
});
const businesses = { demo: { id: 'demo', human: 'jason', vars: { 'bus.tester': true },
arbiters: { technical: 'coder', delivery: 'coder' }, roles: {
coder: { definition: 'coder', authority: { withinRole: ['message.send', 'record.write'], crossRole: [] } },
} } };
function fixture(t, extra = {}) {
assert.equal(typeof state.initializeControl, 'function');
const root = mkdtempSync(join(tmpdir(), 'processing-bus-'));
state.initializeControl(root);
const store = new Store(root);
const processing = {
read: () => state.readControl(root),
async halt(actor) { return state.transition(root, { ...actor, halted: true }); },
async resume(actor) { return state.transition(root, { ...actor, halted: false }); },
};
const b = new Broker({ store, businesses, processing, ...extra });
const cap = b.bindLaunch({ business: 'demo', role: 'coder', run: 'run', harness: 'pi' });
b.request(cap, { verb: 'role.claim' });
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
const reader = b.bindReader({ business: 'demo' });
const tester = b.bindTester({ business: 'demo' });
t.after(() => { store.close(); rmSync(root, { recursive: true, force: true }); });
return { root, b, store, cap, human, reader, tester, processing,
call: (who, verb, args = {}) => b.request(who, { verb, args }) };
}
// Dropping #human before either effect would allow a role/tester to clear the switch.
for (const verb of ['processing.halt', 'processing.resume']) test(`${verb} refuses agent reader and tester before any transition`, async t => {
const f = fixture(t), before = state.readControl(f.root);
for (const cap of [f.cap, f.reader, f.tester]) {
await assert.rejects(async () => f.call(cap, verb, { generation: before.generation }), e => ['human-required', 'read-only', 'tester-refused'].includes(e.code));
}
assert.deepEqual(state.readControl(f.root), before);
});
test('human halt and clear each have one event with verified actor; launch.restore cannot clear', async t => {
const f = fixture(t);
const halted = await f.call(f.human, 'processing.halt');
assert.equal(halted.halted, true);
await f.call(f.human, 'processing.halt');
f.call(f.human, 'launch.restore');
assert.equal(f.call(f.reader, 'processing.status').halted, true);
const resumed = await f.call(f.human, 'processing.resume', { generation: halted.generation });
assert.equal(resumed.halted, false);
const events = f.store.all("SELECT body FROM events WHERE kind='action.allowed' AND json_extract(body,'$.action') IN ('processing.halt','processing.resume')").map(r => JSON.parse(r.body));
assert.deepEqual(events.map(e => [e.action, e.human]), [['processing.halt', 'jason'], ['processing.resume', 'jason']]);
assert.equal(new Set(events.map(e => e.operation)).size, 2);
});
test('all ordinary work verbs refuse before dispatch while pure reads remain available', async t => {
const f = fixture(t); await f.call(f.human, 'processing.halt');
for (const verb of ['role.claim', 'role.revoke', 'decision.raise', 'decision.resolve', 'message.send', 'message.receive', 'message.read', 'staff.request', 'review.request', 'review.verdict', 'event.emit', 'organization.set', 'record.write', 'usage.begin', 'unknown.future.work']) {
await assert.rejects(async () => f.call(f.cap, verb), { code: 'processing-halted' }, verb);
}
assert.equal(f.call(f.cap, 'state').launch.state, 'allowed');
assert.ok(Array.isArray(f.call(f.reader, 'agents')));
assert.equal(f.call(f.reader, 'processing.status').halted, true);
// A human can still queue intent and manage settings under their existing permissions.
assert.doesNotThrow(() => f.call(f.human, 'message.send', { to: 'coder', body: 'wait for resume' }));
assert.doesNotThrow(() => f.call(f.cap, 'role.release'));
});
test('task web authorization and bind paths refuse before external work', async t => {
const f = fixture(t); await f.call(f.human, 'processing.halt');
let effects = 0;
await assert.rejects(f.b.requestTask(f.cap, { verb: 'task.get', args: {} }, () => { effects++; }), { code: 'processing-halted' });
await assert.rejects(f.b.requestWeb(f.cap, { verb: 'web.fetch', args: { url: 'https://example.com' } }, { handle() { effects++; } }), { code: 'processing-halted' });
assert.throws(() => f.b.authorize(f.cap, 'role.launch', { target: 'coder' }), { code: 'processing-halted' });
assert.throws(() => f.b.bindLaunch({ business: 'demo', role: 'coder', run: 'later', harness: 'pi' }), { code: 'processing-halted' });
assert.equal(effects, 0);
});
for (const action of ['processing.halt', 'processing.resume', 'processing.stop']) test(`recordEvent cannot forge ${action}`, t => {
const f = fixture(t);
assert.throws(() => f.b.recordEvent(f.cap, { kind: 'action.allowed', body: { action, human: 'jason' } }), { code: 'reserved-event' });
assert.equal(f.store.get("SELECT count(*) n FROM events WHERE kind='action.allowed'").n, 0);
});
for (const own of [false, true]) test(`recordTask ${own ? 'self' : 'poll'} cannot forge a processing action through its kinds filter`, t => {
const f = fixture(t);
assert.throws(() => f.b.recordTask({ business: 'demo', ...(own ? { cap: f.cap } : {}), events: [
{ kind: 'action.allowed', body: { action: 'processing.halt', human: 'jason' } },
] }), { code: 'reserved-event' });
});
test('spec read remains a read but cannot release new write work after an await', async t => {
let finish;
const ready = new Promise(resolve => { finish = resolve; });
const f = fixture(t, { project: { async writeRecord() { await ready; return { path: 'docs/a.md', commit: 'a'.repeat(40) }; } } });
const writing = f.call(f.cap, 'record.write', { project: 'stack', path: 'docs/a.md', record: '# A' });
await f.call(f.human, 'processing.halt'); finish();
// An already committed external effect must remain outcome-unknown, not a fake rollback.
await assert.rejects(writing, { code: 'outcome-unknown' });
});
test('the real socket awaits halt and resume beyond the ordinary idle deadline', async t => {
const f = fixture(t), path = join(f.root, 'control.sock');
const halt = f.processing.halt;
f.processing.halt = async args => { await new Promise(r => setTimeout(r, 30)); return halt(args); };
let peers = 0;
const server = await serve({ broker: f.b, path, timeout: 5,
// Row 196 added asynchronous kernel-peer discovery before the dispatch timeout.
// This test isolates the post-auth await; peer proof is exercised in its owning files.
peerReader: { close: async () => {}, read: async () => { peers++; return { pid: process.pid, uid: process.getuid(), gid: process.getgid() }; } },
authenticateHuman: () => f.b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true }) });
try {
const client = new Client({ path, human: { fixture: true }, timeout: 1000 });
const halted = await client.call('processing.halt');
assert.equal(halted.halted, true);
assert.equal((await client.call('processing.resume', { generation: halted.generation })).halted, false);
assert.equal((await new Client({ path, cap: f.reader }).call('processing.status')).halted, false);
assert.equal(peers, 2);
} finally { await server.close(); }
});
test('a malformed processing reader never silently admits agent work', t => {
const f = fixture(t);
for (const answer of [null, {}, { halted: false }, { version: 1, generation: 'bad', halted: false }, { halted: 'false' }]) {
f.processing.read = () => answer;
assert.throws(() => f.call(f.cap, 'message.send', { to: 'human', body: 'no' }), { code: 'processing-state-invalid' });
}
});
test('halt refuses the newly landed host executor path before credentials or effects', async t => {
const f = fixture(t); await f.call(f.human, 'processing.halt');
let called = false;
await assert.rejects(f.b.requestHost(f.cap, { verb: 'host.run', args: {} }, {
busy: () => { called = true; return false; }, run: async () => { called = true; },
}), e => e.code === 'processing-halted');
assert.equal(called, false);
});
+1 -3
View File
@@ -15,9 +15,7 @@ test('Broker keeps the sandbox client import closure: only the listed imports, n
// Row 210 (#1707): createHash gives host.run events the argv, stdin and output sha256 values.
"import { randomUUID, randomBytes, createHash } from 'node:crypto';",
// Row 65 (#1558): bindLaunch checks a run's transcript path is absolute.
"import { isAbsolute, dirname } from 'node:path';",
// Row 191 / sage-1367: the one additional read-only CODE source, with no project/git imports.
"import { initializeControl, processingStatus } from '../../processing-control/src/state.mjs';",
"import { isAbsolute } from 'node:path';",
// Row 65: the plug-in name pattern a harness takes. kinds.mjs is in RUNNER_FILES and imports nothing.
"import { NAME as HARNESS_NAME } from '../../plugins/src/kinds.mjs';",
"import { loadRequirements } from '../../tasks/src/requirements.mjs';",
+4 -2
View File
@@ -520,7 +520,9 @@ test('v3b prototype refusals, views and append-only mutations', (t) => {
// Row 141 (#1635) adds instance_names and its 4 triggers; names.test.mjs tests them.
// Row 124 adds usage_records and its three append-only triggers.
// Row 234 (#1740) adds hosts, host_grants and the events_grant_actor guard; grant-migration.test.mjs tests them.
assert.equal(count('table') - 1, 18);
assert.equal(count('trigger'), 71);
// Row 236 (#1742) adds gitea_users and gitea_grants; gitea-migration.test.mjs tests them.
// Row 264a (#1769) adds sessions, session_defaults and 15 triggers; sessions.test.mjs tests them.
assert.equal(count('table') - 1, 22);
assert.equal(count('trigger'), 86);
assert.equal(count('view'), 5);
});
+441
View File
@@ -0,0 +1,441 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { DatabaseSync } from 'node:sqlite';
import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createHash } from 'node:crypto';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { Store, schemaDigest } from '../src/store.mjs';
import { Broker } from '../src/broker.mjs';
// Row 264a (#1769, T1 item 7): sessions below a thread, in the bus. T1's bus half, T2 and T7 of
// docs/plans/2026-10-21_t1-sessions.md. The launcher, seat and Console halves are 264b-264e's.
const shipped = (name) =>
JSON.parse(readFileSync(fileURLToPath(new URL(`../../../roles/${name}.json`, import.meta.url)), 'utf8'));
const roles = {
chief: { definition: 'chief-of-staff', authority: { withinRole: ['message.send'], crossRole: [] } },
assistant: { definition: 'assistant', authority: { withinRole: ['message.send'], crossRole: [] } },
pm: { definition: 'pm', authority: { withinRole: ['message.send'], crossRole: [] } },
};
const businesses = {
demo: {
id: 'demo',
human: 'jason',
arbiters: { technical: 'pm', delivery: 'chief' },
roles,
definitions: { 'chief-of-staff': {}, assistant: { messaging: shipped('assistant').messaging }, pm: {} },
projects: { stack: { root: '/nonexistent' } },
},
};
const AT = '2026-10-21T00:00:00.000Z';
const now = () => new Date().toISOString();
const here = (p) => new URL(p, import.meta.url);
const ddl3k = readFileSync(here('./fixtures/schema-3k.sql'), 'utf8');
function broker(t, store) {
const b = new Broker({ store, businesses });
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
const caps = { human };
const call = (who, verb, args = {}) => b.request(caps[who], { verb, args });
const launch = (name, role, extra = {}) => {
caps[name] = b.bindLaunch({ business: 'demo', role, run: `${name}-run`, harness: 'pi', ...extra });
return caps[name];
};
const claim = (name) => call(name, 'role.claim');
const sessionOf = (message) => store.get('SELECT session FROM message_threads WHERE message=?', message)?.session ?? null;
const claims = (session) => store.all('SELECT op, holder_run, generation FROM thread_claims WHERE session=? ORDER BY seq', session).map((r) => ({ ...r }));
return { b, store, caps, call, launch, claim, sessionOf, claims };
}
function setup(t) {
const root = mkdtempSync(join(tmpdir(), 'bus-sessions-'));
const store = new Store(root);
t.after(() => {
store.close();
rmSync(root, { recursive: true, force: true });
});
const ctx = broker(t, store);
const project = ctx.call('human', 'project.create', { name: 'stack work', repo: 'stack' }).id;
const A = ctx.call('human', 'thread.create', { project, name: 'alpha' }).id;
for (const r of ['chief', 'pm']) {
ctx.launch(r, r);
ctx.claim(r);
}
const [first] = ctx.call('human', 'session.list', { thread: A });
return { ...ctx, root, project, A, D: first.id };
}
test('T1 REQ-ROLE-2: two sessions of one thread keep their own history, delivery and replies', (t) => {
const { call, launch, claim, sessionOf, store, project, A, D } = setup(t);
// thread.create made one default session, with a random id that isn't the thread's.
assert.deepEqual(call('human', 'session.list', { thread: A }).map((r) => [r.name, r.predecessor, r.default]), [['default', null, true]]);
assert.notEqual(D, A);
assert.match(D, /^[a-f0-9-]{36}$/);
const S = call('human', 'session.create', { thread: A, name: 'second', op: 'op-second' }).id;
assert.deepEqual(call('human', 'session.list', { thread: A }).map((r) => [r.id, r.name, r.default]), [
[D, 'default', true],
[S, 'second', false],
]);
// A run with no session named works the default (D3). Each session has its own holder.
launch('a1', 'assistant', { thread: A });
launch('a2', 'assistant', { thread: A, session: S });
assert.deepEqual(claim('a1'), { role: 'assistant', run: 'a1-run', thread: A });
assert.deepEqual(claim('a2'), { role: 'assistant', run: 'a2-run', thread: A });
const toDefault = call('human', 'thread.send', { thread: A, body: 'to the default' });
const toSecond = call('human', 'thread.send', { thread: A, session: S, body: 'to the second' });
assert.equal(sessionOf(toDefault.id), D);
assert.equal(sessionOf(toSecond.id), S);
assert.deepEqual(call('a1', 'message.receive').map((m) => m.body), ['to the default']);
assert.deepEqual(call('a2', 'message.receive').map((m) => m.body), ['to the second']);
assert.deepEqual(call('a1', 'message.receive'), []);
// A reply stays in its run's session, and each history reads only its own.
const reply = call('a2', 'message.send', { to: 'human', body: 'second answers', in_reply_to: toSecond.id });
assert.equal(sessionOf(reply.id), S);
assert.deepEqual(call('human', 'thread.messages', { thread: A }).map((m) => m.body), ['to the default']);
assert.deepEqual(call('human', 'thread.messages', { thread: A, session: S }).map((m) => m.body), ['to the second', 'second answers']);
// The chief's reply to the second session's work comes back to that session.
const ask = call('a2', 'message.send', { to: 'chief', body: 'need help', class: 'REQUEST' });
const back = call('chief', 'message.send', { to: 'assistant', body: 'here', in_reply_to: ask.id });
assert.equal(sessionOf(back.id), S);
assert.deepEqual(call('a2', 'message.receive').map((m) => m.body), ['here']);
assert.deepEqual(call('a1', 'message.receive'), []);
// An open names its session too.
call('human', 'thread.open', { thread: A, session: S });
assert.equal(store.get('SELECT session FROM thread_opens ORDER BY seq DESC LIMIT 1').session, S);
// A non-thread role names no session, and a session of another thread is refused.
assert.throws(() => launch('p2', 'pm', { session: S }), { code: 'thread-not-allowed' });
const B = call('human', 'thread.create', { project, name: 'beta' }).id;
assert.throws(() => launch('a3', 'assistant', { thread: B, session: S }), { code: 'session-not-found' });
assert.throws(() => call('human', 'thread.send', { thread: B, session: S, body: 'x' }), { code: 'session-not-found' });
assert.throws(() => call('human', 'thread.messages', { thread: B, session: 'no-such-session' }), { code: 'session-not-found' });
});
test('T2 REQ-ROLE-2: one holder per session, generations count its claims, and a sibling session\'s message never reaches the other holder', (t) => {
const { b, call, launch, claim, claims, store, project, A, D } = setup(t);
const S = call('human', 'session.create', { thread: A, name: 'second', op: 'op-second' }).id;
launch('a1', 'assistant', { thread: A });
claim('a1');
launch('a1b', 'assistant', { thread: A });
assert.throws(() => claim('a1b'), { code: 'thread-already-held' });
// A sibling session is free to claim while the default is held.
launch('a2', 'assistant', { thread: A, session: S });
claim('a2');
// Release and claim again: the second claim is generation 2, and its release carries it.
call('a2', 'role.release');
launch('a3', 'assistant', { thread: A, session: S });
claim('a3');
b.endLaunch({ business: 'demo', run: 'a3-run', reason: 'idle' });
assert.deepEqual(claims(S), [
{ op: 'claim', holder_run: 'a2-run', generation: 1 },
{ op: 'release', holder_run: 'a2-run', generation: 1 },
{ op: 'claim', holder_run: 'a3-run', generation: 2 },
{ op: 'release', holder_run: 'a3-run', generation: 2 },
]);
// The human's stop revokes each held session, with that claim's generation.
launch('a4', 'assistant', { thread: A, session: S });
claim('a4');
assert.deepEqual(call('human', 'assistant.stop', { instance: 'assistant' }), {
stopped: [
{ thread: A, run: 'a1-run' },
{ thread: A, run: 'a4-run' },
],
});
assert.deepEqual(claims(D).at(-1), { op: 'revoke', holder_run: 'a1-run', generation: 1 });
assert.deepEqual(claims(S).at(-1), { op: 'revoke', holder_run: 'a4-run', generation: 3 });
// The triggers hold these for a write that skips the broker.
const raw = (thread, session, generation, run) =>
store.run(
"INSERT INTO thread_claims(at,business,thread,session,generation,op,holder_run,harness,by) VALUES(?,'demo',?,?,?,'claim',?,'pi',?)",
now(),
thread,
session,
generation,
run,
run,
);
assert.throws(() => raw(A, S, 5, 'x-run'), /a claim row names its session's generation/);
assert.throws(() => raw(A, S, 3, 'x-run'), /a claim row names its session's generation/);
const B = call('human', 'thread.create', { project, name: 'beta' }).id;
const [{ id: BD }] = call('human', 'session.list', { thread: B });
assert.throws(() => raw(A, BD, 1, 'x-run'), /a claim's session is in its thread/);
raw(A, S, 4, 'x-run');
assert.throws(() => raw(B, BD, 1, 'x-run'), /run already holds a session/);
// A threaded message goes only to its own session's holder.
launch('a5', 'assistant', { thread: A });
claim('a5');
const m = call('human', 'thread.send', { thread: A, session: S, body: 'for x' });
assert.throws(
() => store.run("INSERT INTO deliveries(message,at,op,holder_run) VALUES(?,?,'delivered',?)", m.id, now(), 'a5-run'),
/a threaded message goes only to its session's holder/,
);
store.run("INSERT INTO deliveries(message,at,op,holder_run) VALUES(?,?,'delivered',?)", m.id, now(), 'x-run');
});
test('T2 sage-2257: a released or revoked run\'s retired generation refuses its approval, delivery and read, and writes nothing', (t) => {
const { call, launch, claim, claims, store, A, D } = setup(t);
const tables = store.all("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'").map((r) => r.name);
const counts = () => Object.fromEntries(tables.map((n) => [n, store.get(`SELECT count(*) AS n FROM "${n}"`).n]));
// The run raises a decision, takes one message and leaves a second waiting.
const prepare = (name) => {
launch(name, 'assistant', { thread: A });
claim(name);
const d = call(name, 'decision.raise', {
action: 'spend',
target: 'demo',
question: 'spend?',
options: [
{ key: 'yes', text: 'Yes' },
{ key: 'no', text: 'No' },
],
recommendation: 'no',
blocking: false,
});
call('human', 'thread.send', { thread: A, body: `for ${name}` });
const [taken] = call(name, 'message.receive');
call('human', 'thread.send', { thread: A, body: `waiting after ${name}` });
return { d, taken };
};
const refused = (name, { d, taken }) => {
const before = counts();
assert.throws(() => call(name, 'decision.withdraw', { id: d.id }), { code: 'not-holder' }, 'approval');
assert.throws(() => call(name, 'message.receive'), { code: 'not-holder' }, 'delivery');
assert.throws(() => call(name, 'message.read', { id: taken.id }), { code: 'not-holder' }, 'read');
assert.deepEqual(counts(), before);
};
// Released: generation 1 is retired once a2 claims generation 2.
const one = prepare('a1');
call('a1', 'role.release');
const two = prepare('a2');
refused('a1', one);
// Revoked by the human's stop: generation 2 is retired once a3 claims generation 3.
call('human', 'assistant.stop', { instance: 'assistant' });
prepare('a3');
refused('a2', two);
assert.deepEqual(claims(D).map((r) => [r.op, r.holder_run, r.generation]), [
['claim', 'a1-run', 1],
['release', 'a1-run', 1],
['claim', 'a2-run', 2],
['revoke', 'a2-run', 2],
['claim', 'a3-run', 3],
]);
});
test('session.create is idempotent on its op, refuses a reused op or a taken name, and reaches only the owner\'s threads', (t) => {
const { call, store, project, A, D } = setup(t);
const first = call('human', 'session.create', { thread: A, name: 'second', op: 'op-1' });
assert.deepEqual(call('human', 'session.create', { thread: A, name: 'second', op: 'op-1' }), first);
assert.throws(() => call('human', 'session.create', { thread: A, name: 'third', op: 'op-1' }), { code: 'session-op-conflict' });
assert.throws(() => call('human', 'session.create', { thread: A, name: 'second', op: 'op-2' }), { code: 'session-exists' });
assert.throws(() => call('human', 'session.create', { thread: A, name: 'default', op: 'op-3' }), { code: 'session-exists' });
// thread.create's op answers a retry that names the default.
assert.deepEqual(call('human', 'session.create', { thread: A, name: 'default', op: 'default' }), { id: D, thread: A, name: 'default' });
for (const args of [{ thread: A, name: 'x\ny', op: 'op-4' }, { thread: A, name: 'x' }, { thread: A, name: 'x', op: 'op-5', extra: 1 }])
assert.throws(() => call('human', 'session.create', args), { code: 'invalid-request' });
assert.equal(store.get('SELECT count(*) n FROM sessions WHERE thread=?', A).n, 2);
// Another owner's thread, made by hand with the session the broker would write.
const other = 'thread-of-someone-else';
store.run('INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES(?,?,?,?,?,?,?)', other, now(), 'demo', project, 'assistant', 'someone-else', 'theirs');
store.run("INSERT INTO sessions(id,at,business,thread,instance,owner,name,op) VALUES('s-theirs',?,'demo',?,'assistant','someone-else','default','default')", now(), other);
store.run("INSERT INTO session_defaults(at,thread,session,by) VALUES(?,?,'s-theirs','someone-else')", now(), other);
for (const [verb, args] of [
['session.list', { thread: other }],
['session.create', { thread: other, name: 'mine', op: 'op-6' }],
['thread.send', { thread: other, session: 's-theirs', body: 'hi' }],
])
assert.throws(() => call('human', verb, args), { code: 'thread-not-owned' }, verb);
assert.throws(() => call('human', 'session.list', { thread: 'nope' }), { code: 'thread-not-found' });
// Agents don't call the session verbs.
assert.throws(() => call('pm', 'session.list', { thread: A }), { code: 'human-required' });
});
test('the 3l triggers keep sessions in their thread and session ids apart from thread ids', (t) => {
// A bare database from schema.sql, so only the triggers stand between a write and the tables.
const root = mkdtempSync(join(tmpdir(), 'bus-sessions-sql-'));
const db = new DatabaseSync(join(root, 'bus.sqlite'));
t.after(() => {
db.close();
rmSync(root, { recursive: true, force: true });
});
db.exec(readFileSync(here('../schema.sql'), 'utf8'));
const run = (sql, ...args) => db.prepare(sql).run(...args);
const session = (id, thread, extra = {}) =>
run(
'INSERT INTO sessions(id,at,business,thread,instance,owner,name,op,predecessor) VALUES(?,?,?,?,?,?,?,?,?)',
id,
now(),
extra.business ?? 'demo',
thread,
extra.instance ?? 'assistant',
extra.owner ?? 'jason',
extra.name ?? id,
extra.op ?? id,
extra.predecessor ?? null,
);
run("INSERT INTO projects(id,at,business,name,repo,created_by) VALUES('p-1',?,'demo','work',NULL,'jason')", now());
for (const id of ['th-1', 'th-2'])
run("INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES(?,?,'demo','p-1','assistant','jason',?)", id, now(), id);
session('s-1', 'th-1');
// The legacy shape: a session whose id is its own thread's.
session('th-1', 'th-1', { name: 'default', op: 'legacy-3l' });
// A session id that names another thread, and a thread id that names a session, are refused.
assert.throws(() => session('th-2', 'th-1'), /a session id names another thread/);
assert.throws(
() => run("INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES('s-1',?,'demo','p-1','assistant','jason','x')", now()),
/a thread id names a session/,
);
// A session has its thread's business, instance and owner, and its predecessor is in its thread.
for (const extra of [{ business: 'other' }, { instance: 'pm' }, { owner: 'someone-else' }])
assert.throws(() => session('s-x', 'th-1', extra), /a session has its thread's business, instance and owner/, JSON.stringify(extra));
assert.throws(() => session('s-x', 'th-2', { predecessor: 's-1' }), /a session's predecessor is in its thread/);
session('s-2', 'th-1', { predecessor: 's-1' });
// The names and op keys are unique in a thread.
assert.throws(() => session('s-3', 'th-1', { name: 's-1' }), /UNIQUE/);
assert.throws(() => session('s-3', 'th-1', { op: 's-1' }), /UNIQUE/);
// A default is a session of its thread.
assert.throws(() => run("INSERT INTO session_defaults(at,thread,session,by) VALUES(?,'th-2','s-1','jason')", now()), /a default session is in its thread/);
run("INSERT INTO session_defaults(at,thread,session,by) VALUES(?,'th-1','s-1','jason')", now());
// A message's and an open's session is in their thread.
run("INSERT INTO messages(id,at,business,from_role,from_run,to_role,class,body) VALUES('m-1',?,'demo','human','human-cli','assistant','INFO','hi')", now());
assert.throws(() => run("INSERT INTO message_threads(message,thread,session) VALUES('m-1','th-2','s-1')"), /a threaded message's session is in its thread/);
assert.throws(() => run("INSERT INTO thread_opens(at,thread,session,by) VALUES(?,'th-2','s-1','jason')", now()), /an open's session is in its thread/);
});
// T7: a 3k store from the pinned 3k schema (8f51900b5's, the parent of this change's schema.sql)
// and fixture rows. No live store is read or copied.
function source3k(t) {
const root = mkdtempSync(join(tmpdir(), 'sessions-migrate-'));
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);
assert.equal(createHash('sha256').update(ddl3k).digest('hex'), 'aba9240825a35fdcc741439d0708efa33afcf519d11cac9952c7207ad99250f0');
db.exec(ddl3k);
const run = (sql, ...args) => db.prepare(sql).run(...args);
run('INSERT INTO meta VALUES (?,?)', 'schema_version', '3k');
run('INSERT INTO meta VALUES (?,?)', 'schema_digest', schemaDigest(db));
run('INSERT INTO meta VALUES (?,?)', 'comment_cursor_epoch', AT);
run('INSERT INTO projects(id,at,business,name,repo,created_by) VALUES(?,?,?,?,?,?)', 'p-1', AT, 'demo', 'stack work', 'stack', 'jason');
for (const id of ['t1', 't2'])
run('INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES(?,?,?,?,?,?,?)', id, AT, 'demo', 'p-1', 'assistant', 'jason', id);
// Duckworth's fixture: claim, release, claim, revoke on t1, with t2's claim between, at seqs
// with gaps so a renumbered copy would show.
const claimRow = (seq, thread, op, holder, extra = {}) =>
run(
'INSERT INTO thread_claims(seq,at,business,thread,op,holder_run,harness,by,reason) VALUES(?,?,?,?,?,?,?,?,?)',
seq, AT, 'demo', thread, op, holder, 'pi', extra.by ?? holder, extra.reason ?? null,
);
claimRow(10, 't1', 'claim', 'a-run');
claimRow(11, 't2', 'claim', 'b-run');
claimRow(12, 't1', 'release', 'a-run');
claimRow(20, 't1', 'claim', 'c-run');
claimRow(40, 't1', 'revoke', 'c-run', { by: 'jason', reason: 'stuck' });
for (const [id, thread] of [['m-1', 't1'], ['m-2', 't2'], ['m-3', 't1']]) {
run('INSERT INTO messages(id,at,business,from_role,from_run,to_role,class,body) VALUES(?,?,?,?,?,?,?,?)', id, AT, 'demo', 'human', 'human-cli', 'assistant', 'INFO', id);
run('INSERT INTO message_threads VALUES(?,?)', id, thread);
}
run('INSERT INTO thread_opens(seq,at,thread,by) VALUES(?,?,?,?)', 7, AT, 't1', 'jason');
db.close(); chmodSync(path, 0o600);
return { root, path };
}
const master = (path) => {
const db = new DatabaseSync(path, { readOnly: true });
try {
return db.prepare('SELECT type,name,tbl_name,sql FROM sqlite_master ORDER BY type,name').all().map((r) => ({ ...r }));
} finally { db.close(); }
};
const rows = (path) => {
const db = new DatabaseSync(path, { readOnly: true });
try {
return Object.fromEntries(['sessions', 'session_defaults', 'thread_claims', 'message_threads', 'thread_opens', 'meta']
.map((name) => [name, db.prepare(`SELECT * FROM ${name} ORDER BY rowid`).all().map((r) => ({ ...r }))]));
} finally { db.close(); }
};
const version = (path) => {
const db = new DatabaseSync(path, { readOnly: true });
try { return db.prepare("SELECT value FROM meta WHERE key='schema_version'").get().value; } finally { db.close(); }
};
test('T7: a 3k store migrates to 3l, each thread\'s default session has the thread\'s id, and old rows keep their seqs', (t) => {
const { root, path } = source3k(t);
assert.throws(() => new Store(root), /schema-mismatch/);
new Store(root, { migrateSchema: true }).close();
assert.equal(version(path), '3l');
// 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 }));
new Store(freshRoot).close();
assert.deepEqual(master(path), master(join(freshRoot, 'bus/bus.sqlite')));
const store = new Store(root);
t.after(() => store.close());
assert.deepEqual(store.all('SELECT id,at,business,thread,instance,owner,name,op,predecessor FROM sessions ORDER BY seq').map((r) => ({ ...r })), ['t1', 't2'].map((id) => (
{ id, at: AT, business: 'demo', thread: id, instance: 'assistant', owner: 'jason', name: 'default', op: 'legacy-3l', predecessor: null })));
assert.deepEqual(store.all('SELECT at,thread,session,by FROM session_defaults ORDER BY seq').map((r) => ({ ...r })), ['t1', 't2'].map((id) => (
{ at: AT, thread: id, session: id, by: 'migration-3l' })));
// Generation counts claim rows only: claim, release, claim, revoke on t1 is 1, 1, 2, 2.
assert.deepEqual(store.all('SELECT seq,thread,session,op,generation FROM thread_claims ORDER BY seq').map((r) => ({ ...r })), [
{ seq: 10, thread: 't1', session: 't1', op: 'claim', generation: 1 },
{ seq: 11, thread: 't2', session: 't2', op: 'claim', generation: 1 },
{ seq: 12, thread: 't1', session: 't1', op: 'release', generation: 1 },
{ seq: 20, thread: 't1', session: 't1', op: 'claim', generation: 2 },
{ seq: 40, thread: 't1', session: 't1', op: 'revoke', generation: 2 },
]);
assert.deepEqual(store.all('SELECT message,thread,session FROM message_threads ORDER BY rowid').map((r) => ({ ...r })), [
{ message: 'm-1', thread: 't1', session: 't1' },
{ message: 'm-2', thread: 't2', session: 't2' },
{ message: 'm-3', thread: 't1', session: 't1' },
]);
assert.deepEqual({ ...store.get('SELECT seq,thread,session,by FROM thread_opens') }, { seq: 7, thread: 't1', session: 't1', by: 'jason' });
for (const [name, seq] of [['thread_claims', 40], ['thread_opens', 7]])
assert.equal(store.get('SELECT seq FROM sqlite_sequence WHERE name=?', name).seq, seq, name);
assert.equal(store.get('PRAGMA quick_check').quick_check, 'ok');
assert.deepEqual(store.all('PRAGMA foreign_key_check'), []);
// A legacy client names no session and gets the default: t2's holder releases at generation 1,
// and t1's next claim is its third.
const { call, launch, claim, claims, sessionOf } = broker(t, store);
for (const r of ['chief', 'pm']) {
launch(r, r);
claim(r);
}
assert.throws(() => call('human', 'session.create', { thread: 't1', name: 'default', op: 'op-new' }), { code: 'session-exists' });
store.run("INSERT INTO thread_claims(at,business,thread,session,generation,op,holder_run,harness,by) VALUES(?,'demo','t2','t2',1,'release','b-run','pi','b-run')", now());
assert.equal(store.get("SELECT seq FROM thread_claims WHERE thread='t2' ORDER BY seq DESC LIMIT 1").seq, 41);
launch('d', 'assistant', { thread: 't1' });
claim('d');
assert.deepEqual(claims('t1').at(-1), { op: 'claim', holder_run: 'd-run', generation: 3 });
const sent = call('human', 'thread.send', { thread: 't1', body: 'after the step' });
assert.equal(sessionOf(sent.id), 't1');
assert.deepEqual(call('d', 'message.receive').map((m) => m.body), ['m-1', 'm-3', 'after the step']);
assert.deepEqual(call('human', 'thread.messages', { thread: 't1' }).map((m) => m.body), ['m-1', 'm-3', 'after the step']);
});
// The 3k engine is store.mjs at this change with the 3k schema and version and no 3k→3l step.
async function engine3k(root) {
const moduleRoot = join(root, 'engine-3k');
mkdirSync(join(moduleRoot, 'src/migrations'), { recursive: true });
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 = '3l';", "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);
writeFileSync(join(moduleRoot, 'schema.sql'), ddl3k);
for (const file 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'])
copyFileSync(here('../src/migrations/' + file), join(moduleRoot, 'src/migrations', file));
return (await import(pathToFileURL(join(moduleRoot, 'src/store.mjs')))).Store;
}
test('T7 D9: the 3k engine refuses a 3l store, migrating or not, and leaves it as it was', async (t) => {
const { root, path } = source3k(t);
new Store(root, { migrateSchema: true }).close();
const was = master(path), before = rows(path);
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), '3l');
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.
const { root: old } = source3k(t);
new Store3k(old).close();
});
+1 -1
View File
@@ -53,7 +53,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, '3j');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3l');
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'), []);
+2 -5
View File
@@ -249,19 +249,16 @@ test('the runtime refuses an invalid adapter and closes a valid one', async (t)
// Each refusal closed what it had opened, the adapter included; the writer lock is free.
assert.equal(closed, 4);
assert.equal(existsSync(join(root, 'bus/writer.lock')), false);
let given = null, processing = null;
let given = null;
const runtime = await startBroker({
dataRoot: root,
businesses,
tasks: async (deps) => {
given = Object.keys(deps).sort();
processing = deps.processing;
return { handle: async () => ({ ok: 1 }), timeout: 1000, close: async () => closed++ };
},
});
// Row 191: runtime injects cancellation alongside the existing factory dependencies.
assert.deepEqual(given, ['broker', 'businesses', 'credentials', 'processing']);
assert.equal(typeof processing.work, 'function');
assert.deepEqual(given, ['broker', 'businesses', 'credentials']);
await runtime.close();
assert.equal(closed, 5);
await assert.rejects(
@@ -21,6 +21,8 @@ test('row 175: TESTER_VERBS and the organization branch tester set are pinned',
'usage', 'names', 'limits', 'plugins', 'instances', 'project.list', 'thread.list', 'name.set', 'project.create', 'credentials',
// Row 177 (#1675): the tester's own threads.
'thread.create', 'thread.open', 'thread.send', 'thread.messages', 'assistant.stop',
// Row 264a (#1769): the sessions of the tester's own threads.
'session.list', 'session.create',
]);
assert.deepEqual([...TESTER_ORGANIZATION_VERBS], ['limits', 'plugins', 'instances', 'plugins.set', 'instances.set', 'routes.set', 'routes']);
assert.ok(Object.isFrozen(TESTER_VERBS) && Object.isFrozen(TESTER_ORGANIZATION_VERBS));
+3 -3
View File
@@ -49,7 +49,7 @@ async function database3d(t) {
const source = readFileSync(here('../src/store.mjs'), 'utf8');
const pinned = source
.replace(/export const SCHEMA_SHA256 = '[a-f0-9]+';/, `export const SCHEMA_SHA256 = '${createHash('sha256').update(ddl3d).digest('hex')}';`)
.replace("export const SCHEMA_VERSION = '3j';", "export const SCHEMA_VERSION = '3d';")
.replace("export const SCHEMA_VERSION = '3l';", "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, '3j');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3l');
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 3j ready', ''].join('\n'));
assert.equal(migrated.stdout, [...REPORT, 'bus schema 3l ready', ''].join('\n'));
});
+43 -17
View File
@@ -7,6 +7,12 @@ import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Store } from '../src/store.mjs';
import { Broker, THREAD_PAGE } from '../src/broker.mjs';
// Row 264a (#1769): a thread made by hand gets the default session the 3k-3l migration gives a
// legacy thread, with the thread's id.
const DEFAULT_SESSION = [
'INSERT INTO sessions(at,business,thread,id,instance,owner,name,op) SELECT at,business,id,id,instance,owner,'default','legacy-3l' FROM threads WHERE id=?',
'INSERT INTO session_defaults(at,thread,session,by) SELECT at,id,id,'migration-3l' FROM threads WHERE id=?',
];
// Row 121 (#1615): projects and threads in the bus. The assistant's messaging lists are the
// shipped roles/assistant.json's, so the chain below runs under the lists the stack runs.
@@ -81,32 +87,36 @@ test('REQ-ROLE-2 REQ-THREAD-1: one holder per thread, through the broker and the
const { b, store, call, launch, claim, A, B } = twoThreads(t);
launch('a2', 'assistant', { thread: A });
assert.throws(() => claim('a2'), { code: 'thread-already-held' });
// The trigger refuses a second claim that skips the broker.
// The trigger refuses a second claim that skips the broker. Row 264a: the claim is of the
// thread's default session, with the generation the trigger expects.
const sessionOf = (thread) => store.get('SELECT session FROM session_defaults WHERE thread=?', thread).session;
const second = () =>
store.run(
"INSERT INTO thread_claims(at,business,thread,op,holder_run,harness,by) VALUES(?,?,?,'claim',?,?,?)",
"INSERT INTO thread_claims(at,business,thread,session,generation,op,holder_run,harness,by) VALUES(?,?,?,?,2,'claim',?,?,?)",
now(),
'demo',
A,
sessionOf(A),
'a2-run',
'pi',
'a2-run',
);
assert.throws(second, /thread already held/);
// Second case: one run holds one thread. a-run already holds A.
assert.throws(second, /session already held/);
// Second case: one run holds one session. a-run already holds A's.
call('b', 'role.release');
assert.throws(
() =>
store.run(
"INSERT INTO thread_claims(at,business,thread,op,holder_run,harness,by) VALUES(?,?,?,'claim',?,?,?)",
"INSERT INTO thread_claims(at,business,thread,session,generation,op,holder_run,harness,by) VALUES(?,?,?,?,2,'claim',?,?,?)",
now(),
'demo',
B,
sessionOf(B),
'a-run',
'pi',
'a-run',
),
/run already holds a thread/,
/run already holds a session/,
);
// A released thread can be claimed again by a new run; the claim is the thread's, not the role's.
launch('b2', 'assistant', { thread: B });
@@ -128,12 +138,20 @@ test('REQ-ROLE-2 REQ-THREAD-1: the 3e triggers refuse writes that skip the broke
});
db.exec(readFileSync(new URL('../schema.sql', import.meta.url), 'utf8'));
const run = (sql, ...args) => db.prepare(sql).run(...args);
// Row 264a (#1769): a claim row names its session and the generation thread_claim_generation
// expects, so each refusal below comes from the trigger it names.
const generation = (op) =>
op === 'claim'
? 1 + db.prepare("SELECT count(*) n FROM thread_claims WHERE session='s-1' AND op='claim'").get().n
: (db.prepare("SELECT generation FROM thread_claims WHERE session='s-1' ORDER BY seq DESC LIMIT 1").get()?.generation ?? null);
const claim = (op, run_, extra = {}) =>
run(
'INSERT INTO thread_claims(at,business,thread,op,holder_run,harness,by,reason,decision) VALUES(?,?,?,?,?,?,?,?,?)',
'INSERT INTO thread_claims(at,business,thread,session,generation,op,holder_run,harness,by,reason,decision) VALUES(?,?,?,?,?,?,?,?,?,?,?)',
now(),
extra.business ?? 'demo',
extra.thread ?? 'th-1',
's-1',
generation(op),
op,
run_,
'pi',
@@ -143,9 +161,11 @@ test('REQ-ROLE-2 REQ-THREAD-1: the 3e triggers refuse writes that skip the broke
);
run("INSERT INTO projects(id,at,business,name,repo,created_by) VALUES('p-1',?,'demo','work',NULL,'jason')", now());
run("INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES('th-1',?,'demo','p-1','assistant','jason','one')", now());
run("INSERT INTO thread_opens(at,thread,by) VALUES(?,'th-1','jason')", now());
run("INSERT INTO sessions(id,at,business,thread,instance,owner,name,op) VALUES('s-1',?,'demo','th-1','assistant','jason','default','default')", now());
run("INSERT INTO session_defaults(at,thread,session,by) VALUES(?,'th-1','s-1','jason')", now());
run("INSERT INTO thread_opens(at,thread,session,by) VALUES(?,'th-1','s-1','jason')", now());
run("INSERT INTO messages(id,at,business,from_role,from_run,to_role,class,body) VALUES('m-1',?,'demo','human','human-cli','assistant','INFO','hi')", now());
run("INSERT INTO message_threads(message,thread) VALUES('m-1','th-1')");
run("INSERT INTO message_threads(message,thread,session) VALUES('m-1','th-1','s-1')");
// A thread is in its project's business.
assert.throws(
() => run("INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES('th-x',?,'other','p-1','assistant','jason','x')", now()),
@@ -154,20 +174,22 @@ test('REQ-ROLE-2 REQ-THREAD-1: the 3e triggers refuse writes that skip the broke
// A claim names the thread's business.
assert.throws(() => claim('claim', 'a-run', { business: 'other' }), /a thread claim names the thread's business/);
// Nothing releases or revokes a thread nobody holds.
assert.throws(() => claim('release', 'a-run'), /thread is not held/);
assert.throws(() => claim('revoke', 'a-run', { reason: 'stuck' }), /thread is not held/);
assert.throws(() => claim('release', 'a-run'), /session is not held/);
assert.throws(() => claim('revoke', 'a-run', { reason: 'stuck' }), /session is not held/);
claim('claim', 'a-run');
// Only the holder releases; a revoke needs a decision or a reason.
assert.throws(() => claim('release', 'b-run'), /only the holder releases; others revoke/);
assert.throws(() => claim('revoke', 'a-run'), /revoke needs a resolved decision or a reason/);
claim('revoke', 'a-run', { reason: 'stuck' });
// Every 3e table is append-only: no UPDATE, no DELETE, no INSERT OR REPLACE.
// Every 3e table, and row 264a's two, is append-only: no UPDATE, no DELETE, no INSERT OR REPLACE.
const rows = {
projects: ["id='p-1'", 'name', "INSERT OR REPLACE INTO projects(seq,id,at,business,name,created_by) SELECT seq,id,at,business,name,created_by FROM projects WHERE id='p-1'"],
threads: ["id='th-1'", 'name', "INSERT OR REPLACE INTO threads(seq,id,at,business,project,instance,owner,name) SELECT seq,id,at,business,project,instance,owner,name FROM threads WHERE id='th-1'"],
thread_claims: ['seq=1', 'reason', 'INSERT OR REPLACE INTO thread_claims(seq,at,business,thread,op,holder_run,harness,by) SELECT seq,at,business,thread,op,holder_run,harness,by FROM thread_claims WHERE seq=1'],
message_threads: ["message='m-1'", 'thread', "INSERT OR REPLACE INTO message_threads(message,thread) VALUES('m-1','th-1')"],
thread_opens: ['seq=1', 'by', 'INSERT OR REPLACE INTO thread_opens(seq,at,thread,by) SELECT seq,at,thread,by FROM thread_opens WHERE seq=1'],
sessions: ["id='s-1'", 'name', "INSERT OR REPLACE INTO sessions(seq,id,at,business,thread,instance,owner,name,op) SELECT seq,id,at,business,thread,instance,owner,name,op FROM sessions WHERE id='s-1'"],
session_defaults: ['seq=1', 'by', 'INSERT OR REPLACE INTO session_defaults(seq,at,thread,session,by) SELECT seq,at,thread,session,by FROM session_defaults WHERE seq=1'],
thread_claims: ['seq=1', 'reason', 'INSERT OR REPLACE INTO thread_claims(seq,at,business,thread,session,generation,op,holder_run,harness,by) SELECT seq,at,business,thread,session,generation,op,holder_run,harness,by FROM thread_claims WHERE seq=1'],
message_threads: ["message='m-1'", 'thread', "INSERT OR REPLACE INTO message_threads(message,thread,session) VALUES('m-1','th-1','s-1')"],
thread_opens: ['seq=1', 'by', 'INSERT OR REPLACE INTO thread_opens(seq,at,thread,session,by) SELECT seq,at,thread,session,by FROM thread_opens WHERE seq=1'],
};
for (const [table, [where, col, replace]] of Object.entries(rows)) {
const only = new RegExp(`${table} is append-only`);
@@ -225,7 +247,7 @@ test('REQ-MSG-1: a message to or from the assistant names its thread, or is refu
// The store refuses a thread on a message the instance is not party to.
const pmOnly = call('pm', 'message.send', { to: 'coder', body: 'unrelated' });
assert.throws(
() => store.run('INSERT INTO message_threads(message,thread) VALUES(?,?)', pmOnly.id, A),
() => store.run('INSERT INTO message_threads(message,thread,session) VALUES(?,?,(SELECT session FROM session_defaults WHERE thread=?))', pmOnly.id, A, A),
/to or from the thread's instance/,
);
void b;
@@ -277,8 +299,10 @@ test('REQ-MSG-1: a reply can\'t carry one thread instance\'s thread to another t
const helper = { definition: 'assistant', authority: { withinRole: ['message.send'], crossRole: [] } };
const { store, call, launch, claim, count } = setup(t, demo({ roles: { ...roles, helper } }));
const project = call('human', 'project.create', { name: 'two instances' }).id;
for (const [id, instance] of [['th-a', 'assistant'], ['th-h', 'helper']])
for (const [id, instance] of [['th-a', 'assistant'], ['th-h', 'helper']]) {
store.run('INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES(?,?,?,?,?,?,?)', id, now(), 'demo', project, instance, 'jason', id);
for (const sql of DEFAULT_SESSION) store.run(sql, id);
}
launch('chief', 'chief');
claim('chief');
launch('a', 'assistant', { thread: 'th-a' });
@@ -437,6 +461,7 @@ test('Q169 REQ-USER-2: assistant.stop ends the human\'s own assistant thread run
'someone-else',
'theirs',
);
for (const sql of DEFAULT_SESSION) store.run(sql, other);
launch('o', 'assistant', { thread: other });
claim('o');
const last = (thread) => ({
@@ -486,6 +511,7 @@ test('REQ-USER-2: the human verbs refuse another owner\'s thread', (t) => {
'someone-else',
'theirs',
);
for (const sql of DEFAULT_SESSION) store.run(sql, other);
for (const [verb, args] of [
['thread.open', { thread: other }],
['thread.send', { thread: other, body: 'hi' }],
+2 -2
View File
@@ -51,12 +51,12 @@ function snapshot(path, columns) {
return { columns, rows };
} finally { db.close(); }
}
test('REQ-LIMIT-4 3g copy migrates through 3h and 3i to 3j preserving every old column, row and sequence; new usage table is empty', t => {
test('REQ-LIMIT-4 3g copy migrates through 3h, 3i, 3j and 3k to 3l preserving every old column, row and sequence; new usage table is empty', t => {
const { root, path, copy } = source3g(t), before = snapshot(path), untouched = readFileSync(copy);
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, '3j');
assert.equal(store.get("SELECT value FROM meta WHERE key='schema_version'").value, '3l');
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');
+1 -5
View File
@@ -21,12 +21,8 @@ test('C2-04: library mappings load without roots, remain frozen, and retain mapp
assert.ok(Object.isFrozen(loaded.projects.books.universes.harbor.books));
const only = structuredClone(doc); delete only.projects.stack;
assert.deepEqual(Object.keys(validateBusinessDocument(only, file, { rolesDir: REPO_ROLES }).projects), ['books']);
// C2-01c: a prefix outside every book is a universe-level playground; one inside another
// universe's book refuses.
doc.projects.books.universes.harbor.playground = ['Elsewhere/Playground/'];
assert.deepEqual(validateBusinessDocument(doc, file, { rolesDir: REPO_ROLES }).projects.books.universes.harbor.playground, ['Elsewhere/Playground/']);
doc.projects.books.universes.harbor.playground = ['Second World/Playground/'];
refuses(() => validateBusinessDocument(doc, file, { rolesDir: REPO_ROLES }), /library-playground-other-universe/);
refuses(() => validateBusinessDocument(doc, file, { rolesDir: REPO_ROLES }), /library-playground-outside-book/);
doc.projects.books = { ...mappingEntry(), root: '/forbidden' };
refuses(() => validateBusinessDocument(doc, file, { rolesDir: REPO_ROLES }), /library-mapping-invalid/);
});
+11 -11
View File
@@ -1061,6 +1061,17 @@ A status line shows `revoking:` with the tags of a revoke still in progress.
Enrollment itself is a `host-enroll` grant the chief raises and the human approves, not a CLI
command. A usage error exits 4 and a bus refusal 2.
## grants (row 236)
`mosaic grants status gitea [--json] [--tester]` prints the Gitea grant path's state: the admin
slot, each grant and open decision, settle-pending rotations, held agents, service left behind and
revokes in progress. `run DECISION` starts the executor of a Gitea grant the human approved.
`revoke AGENT gitea --reason R` takes the agent's Gitea token away and deletes it at Gitea. `sweep
gitea` retries what a failure left. `admin set gitea --url URL --account NAME --rotate-by DATE`
reads the admin token on stdin, never argv; `admin rotate gitea --rotate-by DATE`, `admin rotate
gitea --finish` and `admin remove gitea` keep the slot. run and admin are the human's alone. Nothing
prints a token. A usage error exits 4 and a bus refusal 2.
## Access (FED R5, #414)
```
@@ -1107,14 +1118,3 @@ The helper supplies the fixed logical origin, reports status/text/body as JSON,
and never retries an uncertain write. It cannot navigate a browser or upgrade the
tester to an owner. `mosaic ... --tester` continues using broker.sock directly.
See docs/TOOLS.md for explicit activation conversion and rollback behavior.
## Instance processing switch
`mosaic halt [--business ID]` and `mosaic resume [--business ID]` use the human
management path even when the CLI channel is disabled. The business chooses
proof context; the affected scope is every managed workload in the resolved
data root. Read `mosaic processing status --json` after an unknown or partial
result. Resume refuses unconfirmed stops and a stale generation. A restarted
`bus start --pm` keeps its control bus up while halted and skips the chief.
The switch does not clear other launch/Discord brakes or retry tasks. Native
diagnostic launch stays available. See packages/processing-control/README.md.
+6 -41
View File
@@ -19,6 +19,7 @@ import { limitsCommand, LIMITS_USAGE } from "./limits.mjs";
import { pluginsCommand, PLUGINS_USAGE } from "./plugins.mjs";
import { credentialsCommand, CREDENTIALS_USAGE } from "./credentials.mjs";
import { hostsCommand, HOSTS_USAGE } from "./hosts.mjs";
import { grantsCommand, GRANTS_USAGE } from "./grants.mjs";
import { instancesCommand, INSTANCES_USAGE } from "./instances.mjs";
import { accessCommand, accessContextFor } from "./access.mjs";
import { createChannelCaller } from "../../plugins/src/channel.mjs";
@@ -43,6 +44,7 @@ export const USAGE = `usage:
${PLUGINS_USAGE}
${CREDENTIALS_USAGE}
${HOSTS_USAGE}
${GRANTS_USAGE}
${INSTANCES_USAGE}
${PROJECT_USAGE}
${PROJECT_BUS_USAGE}
@@ -59,9 +61,6 @@ export const USAGE = `usage:
${THREAD_TALK_USAGE}
mosaic name <instance> <name> [--business <id>] [--tester]
mosaic names [--business <id>] [--json] [--tester]
mosaic halt [--business <id>]
mosaic resume [--business <id>]
mosaic processing status [--business <id>] [--json]
mosaic stop <run>
mosaic launches off|on [--business <id>]
mosaic launches list [--json]
@@ -461,36 +460,6 @@ async function stop(rest, io, deps) {
io.stdout.write(r.stopped ? `stopped ${r.business}/${r.instance} run ${r.run}\n` : `${r.reason}\n`);
}
async function processingCommand(verb, rest, io, deps) {
const parsed = parse(rest, { flags: ["--json"], values: ["--business"] });
if (verb === 'processing' ? parsed.positional.length !== 1 || parsed.positional[0] !== 'status' : parsed.positional.length !== 0)
throw usage('use mosaic halt, mosaic resume or mosaic processing status');
refuseInsideAgent(io.env);
const system = deps.system ?? loadSystem({ env: io.env });
const call = callFor(parsed, io, { ...deps, system });
try {
let result;
if (verb === 'resume') {
const current = await call('processing.status');
if (current?.invalid) throw new CliError(`processing state is invalid; use mosaic halt, then mosaic resume (${current.code})`, 3);
result = await call('processing.resume', { generation: current?.generation });
} else result = await call(verb === 'halt' ? 'processing.halt' : 'processing.status', {});
if (!result || typeof result !== 'object' || (typeof result.halted !== 'boolean' && !result.invalid))
throw new CliError('processing outcome unknown; use mosaic processing status before trying again', 1);
if (verb === 'halt' && result.quiescent !== true)
throw new CliError(`halt gate is on for instance ${system.dataRoot}; ${result.remaining ?? 'unknown'} stops unconfirmed. Use mosaic processing status`, 1);
if (parsed.flags.has('--json')) { io.stdout.write(JSON.stringify(result, null, 2) + '\n'); return; }
const status = verb === 'resume' ? 'resumed' : result.halted ? 'halted' : result.invalid ? 'uninitialized' : 'running';
io.stdout.write(`processing ${status} for instance ${system.dataRoot}\n`);
if (result.invalid) io.stdout.write('state is invalid; use mosaic halt, then mosaic resume\n');
} catch (e) {
if (e instanceof CliError) throw e;
const code = typeof e.code === 'string' && /^[a-z-]{1,64}$/.test(e.code) ? e.code : 'outcome-unknown';
if (code === 'outcome-unknown') throw new CliError('processing outcome unknown; use mosaic processing status before trying again', 1);
throw new CliError(`processing refused: ${code}`, 3);
}
}
async function launches(rest, io, deps) {
const [sub, ...args] = rest;
if (sub === "off" || sub === "on") {
@@ -545,13 +514,8 @@ async function bus(rest, io, deps) {
await launcher.listen();
io.stdout.write(`bus host up: business ${businessId}, socket ${host.path}, notifier ${binding ?? "off"}${launches ? "" : ", launches disabled"}\n`);
if (parsed.flags.has("--pm")) {
try {
const pm = await launcher.launchPm();
io.stdout.write(`launched ${pm.instance}: run ${pm.run}, ${pm.harness} ${pm.model}\n`);
} catch (e) {
if (e.code !== 'processing-halted') throw e;
io.stdout.write('processing halted: chief launch skipped; control bus remains available\n');
}
const pm = await launcher.launchPm();
io.stdout.write(`launched ${pm.instance}: run ${pm.run}, ${pm.harness} ${pm.model}\n`);
}
} catch (e) {
await host.close(1);
@@ -632,13 +596,14 @@ export async function main(argv, io = { env: process.env, stdin: process.stdin,
if (verb === "credentials") return credentialsCommand(rest, io, (p) => callFor(p, io, deps));
// Row 234 (#1740, r6 E4): the host records.
if (verb === "hosts") return hostsCommand(rest, io, (p) => callFor(p, io, deps));
// Row 236 (#1742, #436b r5): the Gitea grant path; admin set reads the admin token on stdin.
if (verb === "grants") return grantsCommand(rest, io, (p) => callFor(p, io, deps));
// Row 165 (#1662): scripts/mosaic sends only `business instances` and `business instance` here.
if (verb === "business") return instancesCommand(rest, io, (p) => callFor(p, io, deps));
if (verb === "talk") return talk(rest, io, deps);
if (verb === "follow") return talk(rest, io, deps, true);
if (verb === "name" || verb === "names") return names(verb, rest, io, deps);
if (verb === "stop") return stop(rest, io, deps);
if (["halt", "resume", "processing"].includes(verb)) return processingCommand(verb, rest, io, deps);
if (verb === "launches") return launches(rest, io, deps);
if (verb === "bus") return bus(rest, io, deps);
if (verb === "-h" || verb === "--help") return io.stdout.write(`${USAGE}\n`);
+11 -17
View File
@@ -85,7 +85,6 @@ import { gitSocketPath } from "../../seat/src/session.mjs";
import { gateEnv, isMosaicStack, rootHits, runGate } from "../../seat/src/landing.mjs";
import { BUNDLE_MAX, dropLand, finishLand, FSCK, HAVE_MAX, prepareLand, publish, push, review, validBranch, workRef, WorkingError } from "../../seat/src/working.mjs";
import { CliError } from "./errors.mjs";
import { withProcessing } from '../../processing-control/src/effects.mjs';
const HEADER_MAX = 4096;
const HEADER_TIMEOUT_MS = 10000;
@@ -373,7 +372,7 @@ export function createGitSocket({ host, business: b, dataRoot, env = process.env
const p = project(facts.project);
const where = { dataRoot, business: b.id, project: facts.project, env };
const refused = async (e, notify) => {
const code = e instanceof Refusal || e instanceof Recorded || e instanceof WorkingError || e.name === 'ProcessingError' ? e.code : "git-failed";
const code = e instanceof Refusal || e instanceof Recorded || e instanceof WorkingError ? e.code : "git-failed";
if (code === "git-failed" || code === "rollback-failed") log(`git socket: ${id}: ${code}: ${e.message}`);
// The host's own faults stay in its log; the role gets the code.
const detail = notify && code !== "git-failed" && e.message !== code ? e.message.slice(0, 1000) : null;
@@ -393,19 +392,20 @@ export function createGitSocket({ host, business: b, dataRoot, env = process.env
}
const { commit, base, reviewed, patch_id } = prepared;
socket.end(`${JSON.stringify({ ok: true, result: { landing: "started", commit, base, reviewed, patch_id } })}\n`);
slot.done = withProcessing(dataRoot, async scope => {
slot.done = (async () => {
try {
const r = await gate.run({ worktree: prepared.worktree, logs, env: gateEnv(env), signal: AbortSignal.any([slot.abort.signal, scope.signal]) });
scope.guard();
const r = await gate.run({ worktree: prepared.worktree, logs, env: gateEnv(env), signal: slot.abort.signal });
if (r.aborted || slot.abort.signal.aborted) throw new Refusal("host-closing", "the host closed during the gate; nothing moved");
if (!r.ok) throw new Refusal("gate-failed", `${r.step} failed, see ${r.log}`);
await finishLand({ ...where, land: prepared, record: (m) => op({ op: "recordLand", ticket, land: { ...m, reviewed, base, patch_id } }) });
} catch (e) {
await refused(e, true);
} finally {
await dropLand({ ...where, worktree: prepared.worktree }).catch((x) => log(`git socket: ${id}: ${x.message}`));
await gate.dropNetwork(`${id}_default`, gateEnv(env));
landing = null;
}
}).catch(e => refused(e, true));
})();
}
async function doPublish(socket, cap, req) {
@@ -479,17 +479,11 @@ export function createGitSocket({ host, business: b, dataRoot, env = process.env
if (typeof req.task_ref === "string") subject = req.task_ref;
if (!who.role || who.business !== b.id) throw new Refusal("not-a-role");
file = join(dir, `${randomBytes(8).toString("hex")}.bundle`);
await withProcessing(dataRoot, async scope => {
const abort = () => socket.destroy();
scope.signal.addEventListener('abort', abort, { once: true });
try {
if (req.op === "push") await doPush(socket, cap, req, rest, file);
else if (req.op === "fetch") await doFetch(socket, cap, req, file);
else if (req.op === "land") await doLand(socket, cap, req);
else if (req.op === "publish") await doPublish(socket, cap, req);
else throw new Refusal("invalid-request", "op is push, fetch, land or publish");
} finally { scope.signal.removeEventListener('abort', abort); }
});
if (req.op === "push") await doPush(socket, cap, req, rest, file);
else if (req.op === "fetch") await doFetch(socket, cap, req, file);
else if (req.op === "land") await doLand(socket, cap, req);
else if (req.op === "publish") await doPublish(socket, cap, req);
else throw new Refusal("invalid-request", "op is push, fetch, land or publish");
} catch (e) {
if (e instanceof Recorded) return fail(e.code);
const code = e instanceof Refusal || e instanceof WorkingError ? e.code : "git-failed";
+105
View File
@@ -0,0 +1,105 @@
// `mosaic grants` (row 236, #1742, #436b r5): the broker's grants verbs for the Gitea grant path. run
// starts the executor of a decision the human said yes to, and admin set, rotate and remove keep the
// admin slot; those are the human's alone. status is the human's, the tester's or the chief's; revoke
// and sweep the human's or, with --tester, the tester's. admin set reads the admin token on stdin, never
// argv, and nothing here prints a token: the answers carry ids and names only.
import { CliError } from "./errors.mjs";
export const GRANTS_USAGE =
"mosaic grants status gitea [--json] [--tester] [--business ID] | run DECISION [--business ID] | " +
"revoke AGENT gitea --reason R [--tester] [--business ID] | sweep gitea [--tester] [--business ID] | " +
"admin set gitea --url URL --account NAME --rotate-by DATE [--business ID] | " +
"admin rotate gitea --rotate-by DATE | admin rotate gitea --finish | admin remove gitea";
const MAX_TOKEN = 16384;
const VALUES = ["--business", "--reason", "--url", "--account", "--rotate-by"];
function parse(args) {
const out = { positional: [], flags: new Set(), values: {} };
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--json" || arg === "--tester" || arg === "--finish") {
if (out.flags.has(arg)) throw new CliError(GRANTS_USAGE, 4);
out.flags.add(arg);
} else if (VALUES.includes(arg)) {
if (!args[i + 1] || Object.hasOwn(out.values, arg)) throw new CliError(GRANTS_USAGE, 4);
out.values[arg] = args[++i];
} else if (arg.startsWith("--")) throw new CliError(GRANTS_USAGE, 4);
else out.positional.push(arg);
}
return out;
}
// The whole of stdin, one token, as `mosaic credentials issue` reads it.
async function readToken(stdin) {
let text = "";
for await (const chunk of stdin) {
text += chunk;
if (text.length > MAX_TOKEN + 2) throw new CliError("refused: the token on stdin is too long", 2);
}
if (!text.trim()) throw new CliError("refused: no token on stdin; pipe or paste it, then end input", 4);
return text;
}
// Each shape: the positional count after the verb, the flags and values it takes and needs.
const SHAPES = {
status: { n: 1, flags: ["--json", "--tester"], need: [], may: ["--business"] },
run: { n: 1, flags: [], need: [], may: ["--business"] },
revoke: { n: 2, flags: ["--tester"], need: ["--reason"], may: ["--business"] },
sweep: { n: 1, flags: ["--tester"], need: [], may: ["--business"] },
"admin set": { n: 1, flags: [], need: ["--url", "--account", "--rotate-by"], may: ["--business"] },
"admin rotate": { n: 1, flags: ["--finish"], need: [], may: ["--rotate-by", "--business"] },
"admin remove": { n: 1, flags: [], need: [], may: ["--business"] },
};
const lines = (s) => {
const out = [];
out.push(s.admin?.error ? `admin: ${s.admin.error}` : s.admin ? `admin: ${s.admin.account} on ${s.admin.url}, rotate by ${s.admin.rotateBy}${s.admin.rotationDue ? " (due)" : ""}` : "admin: none");
if (s.adminTokenLeft) out.push(`admin token left: ${s.adminTokenLeft}`);
for (const g of s.grants) out.push(["grant", g.agent, g.user, `token ${g.tokenId}`, g.projects.join(","), `rotate by ${g.rotateBy}`, g.decision].join("\t"));
for (const o of s.open) out.push(["open", o.decision, o.agent].join("\t"));
for (const o of s.running) out.push(["running", o.decision, o.agent, o.step ?? "spent"].join("\t"));
for (const o of s.settlePending) out.push(["settle-pending", o.decision, o.agent].join("\t"));
for (const o of s.tokenUnknown) out.push(["token-unknown", o.agent, o.decision].join("\t"));
for (const e of s.serviceLeft) out.push(["left", e.agent, e.user, String(e.id)].join("\t"));
for (const r of s.revoking) out.push(["revoking", r.agent, r.revoke].join("\t"));
return out.join("\n") + "\n";
};
export async function grantsCommand(args, io, callFor) {
const parsed = parse(args);
let [verb, ...rest] = parsed.positional;
if (verb === "admin") {
verb = `admin ${rest[0] ?? ""}`;
rest = rest.slice(1);
}
// Own keys only, so `constructor` is a usage error, not a verb.
const shape = Object.hasOwn(SHAPES, verb ?? "") ? SHAPES[verb] : null;
if (
!shape || rest.length !== shape.n ||
[...parsed.flags].some((f) => !shape.flags.includes(f)) ||
Object.keys(parsed.values).some((v) => !shape.need.includes(v) && !shape.may.includes(v)) ||
shape.need.some((v) => !Object.hasOwn(parsed.values, v)) ||
(verb !== "run" && rest.at(-1) !== "gitea") ||
(verb === "admin rotate" && parsed.flags.has("--finish") === Object.hasOwn(parsed.values, "--rotate-by"))
)
throw new CliError(GRANTS_USAGE, 4);
try {
const call = callFor(parsed);
const v = parsed.values;
let request;
if (verb === "status") request = ["grants.status", { service: "gitea" }];
else if (verb === "run") request = ["grants.run", { decision: rest[0] }];
else if (verb === "revoke") request = ["grants.revoke", { service: "gitea", agent: rest[0], reason: v["--reason"] }];
else if (verb === "sweep") request = ["grants.sweep", { service: "gitea" }];
else if (verb === "admin set")
request = ["grants.admin.set", { service: "gitea", url: v["--url"], account: v["--account"], rotateBy: v["--rotate-by"], token: await readToken(io.stdin) }];
else if (verb === "admin rotate")
request = ["grants.admin.rotate", parsed.flags.has("--finish") ? { service: "gitea", finish: true } : { service: "gitea", rotateBy: v["--rotate-by"] }];
else request = ["grants.admin.remove", { service: "gitea" }];
const result = await call(...request);
io.stdout.write(verb === "status" && !parsed.flags.has("--json") ? lines(result) : JSON.stringify(result, null, 2) + "\n");
} catch (error) {
if (error instanceof CliError) throw error;
throw new CliError(error.message, 2);
}
}
+7 -52
View File
@@ -127,9 +127,6 @@ import { createGitSocket } from "./git-socket.mjs";
import { cmdlineOf, startTimeOf } from "../../seat/src/proc.mjs";
import { rolesDir } from "./config.mjs";
import { CliError } from "./errors.mjs";
import { assertProcessing, processingStatus, ProcessingError } from "../../processing-control/src/state.mjs";
import { registerRuntime, updateRuntime, runtime, unregisterRuntime, processIdentity } from "../../processing-control/src/runtimes.mjs";
import { withProcessing } from '../../processing-control/src/effects.mjs';
const STOP_TIMEOUT_MS = 30000;
const LINE_MAX = 4096;
@@ -182,13 +179,6 @@ export function createLauncher({
log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`),
}) {
const dataRoot = system.dataRoot;
function admission(generation = null) {
try { return assertProcessing(dataRoot, generation); }
catch (e) {
if (e instanceof ProcessingError) throw new Refusal(e.code);
throw e;
}
}
// Row 64 (#1557): where a role's skills are read from; scripts/skill.sh activate fills it.
const skillsDir = join(dataRoot, "skills-enabled");
let b = loadBusiness(host.business, { dir: configDir(env), rolesDir: rolesDir(env) });
@@ -241,7 +231,6 @@ export function createLauncher({
// launch.by itself. A thread instance launches only for a thread, and
// nothing else launches for one.
async function check(instance, { pm = false, thread = null } = {}) {
const admissionGeneration = admission().generation;
try { b=loadBusiness(host.business,{dir:configDir(env),rolesDir:rolesDir(env)}); } catch(e){throw new Refusal("organization-refused",e.message);}
if (!b.launch) throw new Refusal("launch-not-configured", `business ${b.id} has no launch block`);
if (!(pm ? instance === b.launch.by : b.launch.instances.includes(instance))) throw new Refusal("instance-not-listed");
@@ -335,13 +324,11 @@ export function createLauncher({
if (may?.hit) { const error = new Refusal("limit-reached", JSON.stringify(may.hit)); error.limit = { check: may.check, ...may.hit }; throw error; }
throw new Refusal("limit-check-failed", `${may?.check ?? "bus"}: ${may?.code ?? "malformed-answer"}`);
}
admission(admissionGeneration);
return { resolved, model, account, harness, route, routeReason, admissionGeneration };
return { resolved, model, account, harness, route, routeReason };
}
// Bundle, spawn, bind, register, hand over the capability.
async function start({ instance, launchedBy, resolved, model, account = null, thread = null, admissionGeneration }) {
admission(admissionGeneration);
async function start({ instance, launchedBy, resolved, model, account = null, thread = null }) {
// Every launch path ends here, so a --no-launch host spawns nothing whichever path asked.
if (!launches) throw new Refusal("launches-disabled", "this host was started with --no-launch");
// A thread's store is checked first, so a bad id or an unsafe directory
@@ -355,7 +342,6 @@ export function createLauncher({
}
}
const credentials = await host.op({ op: "credentialStatus", business: b.id, role: instance });
admission(admissionGeneration);
// The clones come before the run, so one that can't be made leaves no
// run behind. The host's git treats each clone as hostile, and its
// checks hold only while no session writes the workspace: a workspace
@@ -365,10 +351,7 @@ export function createLauncher({
mkdirSync(workspace, { recursive: true, mode: 0o700 });
let checkouts;
try {
checkouts = await withProcessing(dataRoot, () => {
admission(admissionGeneration);
return ensureCheckouts({ workspace, projects: Object.fromEntries(Object.entries(b.projects).filter(([, entry]) => entry.kind !== 'library')), env });
});
checkouts = await ensureCheckouts({ workspace, projects: Object.fromEntries(Object.entries(b.projects).filter(([, entry]) => entry.kind !== 'library')), env });
} catch (e) {
if (e instanceof CheckoutError) throw new Refusal("checkout-failed", `${e.project}: ${e.message}`);
throw e;
@@ -379,7 +362,6 @@ export function createLauncher({
}
let harness, route, routeReason;
({resolved,model,account,harness,route,routeReason}=await check(instance,{pm:instance===b.launch.by,thread}));
admission(admissionGeneration);
const { run, runDir } = newRun(dataRoot, b.id);
const sessionDir = store ? store.session : join(runDir, "session");
if (!store) mkdirSync(sessionDir, { mode: 0o700 });
@@ -454,16 +436,7 @@ export function createLauncher({
threadStore: store,
readOnly,
};
admission(admissionGeneration);
const managed = registerRuntime(dataRoot, { kind: 'session', business: b.id, run });
let child;
try {
admission(admissionGeneration);
child = spawnSession({ runDir, env: sessionEnv(env, run), paths, business: b.id, role: instance, bwrap });
} catch (e) {
unregisterRuntime(dataRoot, managed.id);
throw e;
}
const child = spawnSession({ runDir, env: sessionEnv(env, run), paths, business: b.id, role: instance, bwrap });
let bound = false;
let entry = null;
const exited = new Promise((resolve) => child.once("close", (code, signal) => resolve(sessionExit(code, signal))));
@@ -472,16 +445,10 @@ export function createLauncher({
try {
if (child.pid === undefined) throw new Error("the session process did not start");
const outerStart = startTimeOf(child.pid);
const identity = processIdentity(child.pid);
if (!identity) throw new Error("the session process exited at once");
updateRuntime(dataRoot, managed.id, { ...identity, group: true, phase: 'running' });
const runner = await runnerOf(child);
updateRuntime(dataRoot, managed.id, { runnerPid: runner.pid, runnerStartTime: runner.startTime });
admission(admissionGeneration);
if (!outerStart) throw new Error("the session process exited at once");
const launch = await host.bindLaunch({ business: b.id, role: instance, run, harness: model.harness, provider: model.provider, model: model.model, account, transcript, pid: child.pid, startTime: outerStart, ...(thread ? { thread } : {}) });
bound = true;
admission(admissionGeneration);
entry = {
business: b.id, instance, ...(thread ? { thread } : {}), run, launchedBy, harness: model.harness, model: model.model, provider: model.provider, modelKey: model.modelKey,
pid: child.pid, startTime: outerStart, runnerPid: runner.pid, runnerStartTime: runner.startTime, runDir, startedAt: new Date().toISOString(),
@@ -495,29 +462,20 @@ export function createLauncher({
child.kill("SIGKILL");
await exited;
if (bound) await endRun(run, "failed", null);
unregisterRuntime(dataRoot, managed.id);
if (e instanceof Refusal) throw e;
throw new Refusal("launch-failed", e.message);
}
children.set(run, exited);
exited.then(async ({ code, signal }) => {
let receipt;
try { receipt = runtime(dataRoot, managed.id); }
catch { log(`run ${run}: processing receipt unavailable at exit`); }
const state = processingStatus(dataRoot);
const haltGeneration = receipt?.haltGeneration ?? (state.halted && !state.invalid ? state.generation : null);
const reason = haltGeneration ? 'kill-switch' : endReason(code, signal);
const reason = endReason(code, signal);
try {
const ended = await endRun(run, reason, signal ? null : code);
registry.remove(b.id, run);
// Row 125 (#1619, REQ-LIMIT-4): a run a limit check stopped names the limit, from the claim row.
record({ event: "end", instance, ...(thread ? { thread } : {}), run, reason, exitCode: code, signal, ...(haltGeneration ? { haltGeneration } : {}), ...(ended?.limit ? { limit: ended.limit } : {}) });
record({ event: "end", instance, ...(thread ? { thread } : {}), run, reason, exitCode: code, signal, ...(ended?.limit ? { limit: ended.limit } : {}) });
log(`run ${run} (${instance}) ended: ${reason}`);
} catch (e) {
log(`run ${run}: ${e.message}`);
} finally {
try { unregisterRuntime(dataRoot, managed.id); }
catch { log(`run ${run}: processing receipt cleanup refused`); }
children.delete(run);
stopping.delete(run);
landingOf.delete(run);
@@ -532,7 +490,6 @@ export function createLauncher({
// landing pass. A tick that is still running when the next is due skips it.
async function tick() {
if (ticking || closed) return;
try { admission(); } catch { return; }
ticking = true;
try {
await threadPass();
@@ -722,9 +679,7 @@ export function createLauncher({
} catch (e) {
if (!(e instanceof Refusal)) throw e;
record({ event: "refused", by: b.human, instance: b.launch.by, code: e.code, why: e.message, ...(e.limit?{limit:e.limit}:{}) });
const error = new CliError(`PM launch refused: ${e.code}${e.message !== e.code ? ` (${e.message})` : ""}`, 3);
error.code = e.code;
throw error;
throw new CliError(`PM launch refused: ${e.code}${e.message !== e.code ? ` (${e.message})` : ""}`, 3);
}
});
}
+2 -8
View File
@@ -7,7 +7,6 @@
import { Client } from "../../bus/src/client.mjs";
import { openDirect } from "../../discord/src/notify.mjs";
import { createNotifier, runLoop } from "./notifier.mjs";
import { withProcessing, currentProcessing } from '../../processing-control/src/effects.mjs';
const log = (line) => process.stderr.write(`mosaic-notify: ${line}\n`);
let loop = null;
@@ -38,13 +37,8 @@ if (!process.send) {
clearTimeout(timer);
try {
const client = new Client({ path: m.path, cap: m.cap });
const processing = { work: fn => withProcessing(m.dataRoot, fn), guard: () => currentProcessing()?.guard() };
const direct = openDirect({ dataRoot: m.dataRoot, name: m.binding, ...(m.base ? { base: m.base } : {}),
fetch: (url, init) => {
const scope = currentProcessing(); scope?.guard();
return fetch(url, { ...init, ...(scope ? { signal: scope.signal } : {}) });
}, log });
const notifier = createNotifier({ business: m.business, dataRoot: m.dataRoot, inbox: () => client.call("inbox"), direct, processing, log });
const direct = openDirect({ dataRoot: m.dataRoot, name: m.binding, ...(m.base ? { base: m.base } : {}), log });
const notifier = createNotifier({ business: m.business, dataRoot: m.dataRoot, inbox: () => client.call("inbox"), direct, log });
loop = runLoop(notifier, { pollMs: m.pollMs ?? undefined, log });
process.send({ ok: true });
} catch (e) {
+2 -7
View File
@@ -251,7 +251,7 @@ export function openJournal(file, { log = () => {}, now = () => new Date() } = {
// inbox(): the broker's inbox through the reader capability.
// direct.send({content, nonce}) resolves {messageId} or throws a RestOutcome.
export function createNotifier({ business, dataRoot, inbox, direct, now = () => new Date(), zone = ZONE, processing = null, log = () => {} }) {
export function createNotifier({ business, dataRoot, inbox, direct, now = () => new Date(), zone = ZONE, log = () => {} }) {
zoned(now(), zone);
const journal = openJournal(journalPath(dataRoot, business), { log, now });
const backoff = new Map();
@@ -269,7 +269,6 @@ export function createNotifier({ business, dataRoot, inbox, direct, now = () =>
}
async function attempt(key, record, message) {
processing?.guard();
const t = now().getTime();
try {
const { messageId } = await direct.send(message);
@@ -329,11 +328,7 @@ export function createNotifier({ business, dataRoot, inbox, direct, now = () =>
return done;
}
const poll = () => processing ? processing.work(tick).catch(e => {
if (e.name !== 'ProcessingError') throw e;
return { dms: 0, digest: false, failed: 0, halted: true };
}) : tick();
return Object.freeze({ tick: poll, journal: journalPath(dataRoot, business) });
return Object.freeze({ tick, journal: journalPath(dataRoot, business) });
}
// Runs tick() every pollMs until stop(). Ticks never overlap.
@@ -3,18 +3,10 @@ import { mkdtempSync,rmSync,writeFileSync,mkdirSync,cpSync,readFileSync,statSync
import { spawnSync } from 'node:child_process';
const image=await import('../../../scripts/testing/lane-image.mjs').catch(e=>{if(e.code!=='ERR_MODULE_NOT_FOUND')throw e;return {};});
const docker=await import('../../../scripts/testing/lane-docker.mjs').catch(e=>{if(e.code!=='ERR_MODULE_NOT_FOUND')throw e;return {};});
const processingInputs=['packages/processing-control/src/state.mjs','packages/processing-control/src/entrypoint.mjs'];
function copyProcessingInputs(root){
mkdirSync(join(root,'packages/processing-control/src'),{recursive:true});
for(const file of processingInputs)cpSync(new URL('../../../'+file,import.meta.url),join(root,file));
}
test('a changed COPY input or actual image ID refuses compatible image sharing',t=>{
assert.equal(typeof image.imageFingerprint,'function','image fingerprint required');
const root=mkdtempSync('/mnt/storage/scratch/gi-');t.after(()=>rmSync(root,{recursive:true,force:true}));
for(const f of ['Containerfile','package.json','package-lock.json'])cpSync(new URL('../../../'+f,import.meta.url),join(root,f));
copyProcessingInputs(root);
writeFileSync(join(root,'RELEASE'),'0.0.1\n');
for(const d of ['contracts','src','adapters']){mkdirSync(join(root,d));writeFileSync(join(root,d,'one'),'same');}
const digest=image.imageFingerprint(root),id='sha256:'+'a'.repeat(64);const receipt={inputDigest:digest,imageId:id,tag:'mosaic-poc-agent:0.85.1-r0.0.1',baseDigest:'node@sha256:'+'b'.repeat(64),platform:'linux/amd64'};
@@ -33,7 +25,6 @@ test('exclusive image preparation records actual image ID and build wall time an
assert.equal(typeof image.prepareImage,'function','prepareImage is required');
const root=mkdtempSync('/mnt/storage/scratch/gi-');t.after(()=>rmSync(root,{recursive:true,force:true}));
for(const f of ['Containerfile','package.json','package-lock.json'])cpSync(new URL('../../../'+f,import.meta.url),join(root,f));
copyProcessingInputs(root);
writeFileSync(join(root,'RELEASE'),'0.0.1\n');for(const d of ['contracts','src','adapters'])mkdirSync(join(root,d));
const calls=[];const invoke=(args)=>{calls.push(args);if(args[0]==='compose')return '';return JSON.stringify(args[2]==='node:24-bookworm-slim'?{RepoDigests:['node@sha256:'+'b'.repeat(64)]}:{Id:'sha256:'+'a'.repeat(64),Os:'linux',Architecture:'amd64'});};
assert.throws(()=>image.prepareImage(root,{grant:'',tempRoot:root},invoke),/grant/);
@@ -49,7 +40,6 @@ test('cleanup refuses a foreign Docker label and removes only an exact owned pro
function imageFixture(t){
const root=mkdtempSync('/mnt/storage/scratch/gi-');t.after(()=>rmSync(root,{recursive:true,force:true}));
for(const f of ['Containerfile','package.json','package-lock.json'])cpSync(new URL('../../../'+f,import.meta.url),join(root,f));
copyProcessingInputs(root);
writeFileSync(join(root,'RELEASE'),'0.0.1\n');for(const d of ['contracts','src','adapters'])mkdirSync(join(root,d));
const receipt={inputDigest:image.imageFingerprint(root),imageId:'sha256:'+'a'.repeat(64),tag:'mosaic-poc-agent:0.85.1-r0.0.1',baseDigest:'node@sha256:'+'b'.repeat(64),platform:'linux/amd64'};
return {root,receipt};
@@ -144,12 +134,3 @@ test('guard owns every Compose file argument and refuses caller configuration ov
for(const args of [['compose','-f','foreign.yaml','run','mosaic-agent'],['compose','--project-directory','/tmp','run','mosaic-agent']])
assert.throws(()=>docker.guardDocker(args,run),/unowned command/);
});
// Row 191 image-input manifest: either copied reader must invalidate the pinned image.
for(const file of processingInputs)test(`changing ${file} invalidates the image receipt`,t=>{
const {root,receipt}=imageFixture(t);
const inspect=()=>({Id:receipt.imageId,Os:'linux',Architecture:'amd64'});
assert.equal(image.verifyImage(receipt,root,inspect).imageId,receipt.imageId);
writeFileSync(join(root,file),readFileSync(join(root,file),'utf8')+'\n// changed fixture input\n');
assert.throws(()=>image.verifyImage(receipt,root,inspect),/image inputs differ/);
});
+138
View File
@@ -0,0 +1,138 @@
// Row 236 (#1742, #436b r5): `mosaic grants status|run|revoke|sweep|admin`. A fake transport records the
// calls; no bus runs here. The bus side of each verb is in packages/bus/tests/gitea-grant.test.mjs. The
// admin token is a fake made here, read once from stdin and never printed.
import test from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { randomBytes } from "node:crypto";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { main } from "../src/cli.mjs";
import { io, tmp } from "./helpers.mjs";
const MOSAIC = fileURLToPath(new URL("../../../scripts/mosaic", import.meta.url));
const STATUS = {
admin: { url: "https://git.example.test", account: "mosaic-grant", rotateBy: "2026-12-20", setAt: "2026-10-21T00:00:00.000Z", rotationDue: false },
adminTokenLeft: "mosaic-grant-bootstrap",
users: [{ agent: "coder", user: "demo-coder", id: 7 }],
grants: [{ agent: "coder", user: "demo-coder", tokenId: 9, projects: ["app", "lib"], rotateBy: "2026-11-20", decision: "d1" }],
open: [{ decision: "d2", agent: "coder", spentAtNextStart: true }],
running: [{ decision: "d3", agent: null, step: null }],
settlePending: [{ decision: "d4", agent: "coder" }],
tokenUnknown: [{ agent: "ops", decision: "d5" }],
serviceLeft: [{ agent: "coder", user: "demo-coder", id: 11 }],
revoking: [{ agent: "coder", revoke: "r1" }],
};
function deps(calls) {
const answer = (who) => (business) => async (verb, args) => {
calls.push({ who, business, verb, args });
if (verb === "grants.status") return STATUS;
if (verb === "grants.run") return { decision: args.decision, agent: "coder", started: true };
return { ok: true };
};
return { system: { dataRoot: "/nonexistent" }, transport: answer("human"), testerTransport: answer("tester") };
}
const withStdin = (x, text) => (x.stdin.end(text), x);
test("row 236: grants status prints one line per entry, and --json the answer", async () => {
const calls = [];
const x = io();
await main(["grants", "status", "gitea", "--business", "acme"], x, deps(calls));
assert.deepEqual(calls, [{ who: "human", business: "acme", verb: "grants.status", args: { service: "gitea" } }]);
assert.equal(x.out.text, [
"admin: mosaic-grant on https://git.example.test, rotate by 2026-12-20",
"admin token left: mosaic-grant-bootstrap",
"grant\tcoder\tdemo-coder\ttoken 9\tapp,lib\trotate by 2026-11-20\td1",
"open\td2\tcoder",
"running\td3\t\tspent",
"settle-pending\td4\tcoder",
"token-unknown\tops\td5",
"left\tcoder\tdemo-coder\t11",
"revoking\tcoder\tr1",
].join("\n") + "\n");
const y = io();
await main(["grants", "status", "gitea", "--json", "--tester", "--business", "acme"], y, deps(calls));
assert.deepEqual(JSON.parse(y.out.text), STATUS);
assert.equal(calls[1].who, "tester");
const z = io();
const due = { ...deps([]), transport: () => async () => ({ ...STATUS, admin: { ...STATUS.admin, rotationDue: true }, adminTokenLeft: null }) };
await main(["grants", "status", "gitea", "--business", "acme"], z, due);
assert.match(z.out.text, /^admin: mosaic-grant on https:\/\/git\.example\.test, rotate by 2026-12-20 \(due\)\ngrant\t/);
const none = { ...deps([]), transport: () => async () => ({ ...STATUS, admin: null, adminTokenLeft: null }) };
const w = io();
await main(["grants", "status", "gitea", "--business", "acme"], w, none);
assert.match(w.out.text, /^admin: none\n/);
});
test("row 236: run, revoke, sweep and admin rotate and remove send their args", async () => {
const calls = [];
const x = io();
await main(["grants", "run", "d1", "--business", "acme"], x, deps(calls));
await main(["grants", "revoke", "coder", "gitea", "--reason", "left the team", "--business", "acme"], x, deps(calls));
await main(["grants", "revoke", "coder", "gitea", "--reason", "r", "--tester", "--business", "acme"], x, deps(calls));
await main(["grants", "sweep", "gitea", "--tester", "--business", "acme"], x, deps(calls));
await main(["grants", "admin", "rotate", "gitea", "--rotate-by", "2027-01-20", "--business", "acme"], x, deps(calls));
await main(["grants", "admin", "rotate", "gitea", "--finish", "--business", "acme"], x, deps(calls));
await main(["grants", "admin", "remove", "gitea", "--business", "acme"], x, deps(calls));
assert.deepEqual(calls.map(({ who, verb, args }) => ({ who, verb, args })), [
{ who: "human", verb: "grants.run", args: { decision: "d1" } },
{ who: "human", verb: "grants.revoke", args: { service: "gitea", agent: "coder", reason: "left the team" } },
{ who: "tester", verb: "grants.revoke", args: { service: "gitea", agent: "coder", reason: "r" } },
{ who: "tester", verb: "grants.sweep", args: { service: "gitea" } },
{ who: "human", verb: "grants.admin.rotate", args: { service: "gitea", rotateBy: "2027-01-20" } },
{ who: "human", verb: "grants.admin.rotate", args: { service: "gitea", finish: true } },
{ who: "human", verb: "grants.admin.remove", args: { service: "gitea" } },
]);
assert.match(x.out.text, /^\{\n {2}"decision": "d1",\n {2}"agent": "coder",\n {2}"started": true\n\}\n/);
});
test("row 236: admin set sends the stdin token once to grants.admin.set and prints no value", async () => {
const calls = [];
const token = randomBytes(20).toString("hex");
const x = withStdin(io(), token + "\n");
await main(["grants", "admin", "set", "gitea", "--url", "https://git.example.test", "--account", "mosaic-grant", "--rotate-by", "2026-12-20", "--business", "acme"], x, deps(calls));
assert.equal(calls.length, 1);
assert.equal(calls[0].verb, "grants.admin.set");
const { token: sent, ...rest } = calls[0].args;
assert.ok(sent === token + "\n", "the stdin token went to the bus as read");
assert.deepEqual(rest, { service: "gitea", url: "https://git.example.test", account: "mosaic-grant", rotateBy: "2026-12-20" });
assert.ok(!x.out.text.includes(token) && !x.err.text.includes(token), "no token value printed");
// An empty stdin, and one too long, refuse before any call.
await assert.rejects(main(["grants", "admin", "set", "gitea", "--url", "u", "--account", "a", "--rotate-by", "2026-12-20", "--business", "acme"], withStdin(io(), "\n"), deps(calls)),
(e) => (assert.equal(e.exitCode, 4), assert.match(e.message, /no token on stdin/), true));
await assert.rejects(main(["grants", "admin", "set", "gitea", "--url", "u", "--account", "a", "--rotate-by", "2026-12-20", "--business", "acme"], withStdin(io(), "x".repeat(20000)), deps(calls)),
(e) => (assert.equal(e.exitCode, 2), assert.match(e.message, /too long/), true));
assert.equal(calls.length, 1);
});
test("row 236: a malformed grants command is a usage error and calls nothing", async () => {
const calls = [];
for (const argv of [
["grants"], ["grants", "status"], ["grants", "status", "host"], ["grants", "status", "gitea", "x"], ["grants", "run"], ["grants", "run", "d1", "d2"],
["grants", "run", "d1", "--tester"], ["grants", "revoke", "coder", "gitea"], ["grants", "revoke", "coder", "host", "--reason", "r"],
["grants", "revoke", "gitea", "--reason", "r"], ["grants", "sweep"], ["grants", "sweep", "host"], ["grants", "sweep", "gitea", "--reason", "r"],
["grants", "admin"], ["grants", "admin", "set", "gitea"], ["grants", "admin", "set", "gitea", "--url", "u", "--account", "a"],
["grants", "admin", "set", "gitea", "--url", "u", "--account", "a", "--rotate-by", "d", "--tester"],
["grants", "admin", "rotate", "gitea"], ["grants", "admin", "rotate", "gitea", "--finish", "--rotate-by", "2027-01-01"],
["grants", "admin", "rotate", "gitea", "--finish", "--finish"], ["grants", "admin", "remove", "gitea", "--tester"], ["grants", "admin", "purge", "gitea"],
["grants", "status", "gitea", "--business"], ["grants", "status", "gitea", "--force"], ["grants", "constructor", "gitea"],
["grants", "__proto__", "gitea"], ["grants", "admin", "constructor", "gitea"], ["grants", "status", "gitea", "--json", "--json"],
])
await assert.rejects(main([...argv], io(), deps(calls)), (e) => (assert.equal(e.exitCode, 4, argv.join(" ")), assert.match(e.message, /mosaic grants/), true));
assert.equal(calls.length, 0);
});
test("row 236: a bus refusal exits 2 with its code", async () => {
const refusing = { system: { dataRoot: "/nonexistent" }, transport: () => async () => { throw new Error("grant-admin-missing"); } };
await assert.rejects(main(["grants", "run", "d1", "--business", "acme"], io(), refusing), (e) => (assert.equal(e.exitCode, 2), assert.match(e.message, /grant-admin-missing/), true));
});
test("row 236: scripts/mosaic sends grants to packages/cli, not the seat CLI", (t) => {
const root = tmp(t, "mosaic-cli-grants-route-");
const env = { PATH: process.env.PATH, HOME: join(root, "home"), MOSAIC_CONFIG: join(root, "none.json") };
const r = spawnSync(MOSAIC, ["grants", "purge", "gitea"], { env, encoding: "utf8" });
assert.equal(r.status, 4);
assert.match(r.stderr, /^mosaic: mosaic grants status gitea/);
assert.doesNotMatch(r.stderr, /mosaic launch </);
});
-19
View File
@@ -1027,22 +1027,3 @@ test("bus-service.sh refuses a default claude without the execute bit, or in a P
assert.ok(r.stderr.includes(join(spaced, "claude")), r.stderr);
assert.equal(r.stdout, "");
});
// Row 191: a restarted halted instance must keep its recovery bus reachable.
test('bus start --pm while halted keeps the control host up without launching the chief', { timeout: 30000 }, async t => {
const { initializeControl, transition, readControl } = await import('../../processing-control/src/state.mjs');
const f = fixture(tmp(t)); notifyConfig(f.dataRoot, 'acme', null);
initializeControl(f.dataRoot);
transition(f.dataRoot, { business: 'acme', human: 'jason', halted: true });
const child = spawn(process.execPath, [CLI, 'bus', 'start', 'acme', '--pm'], { env: f.env, stdio: ['ignore', 'pipe', 'pipe'] });
const exited = once(child, 'exit'); let output = '';
child.stdout.on('data', b => { output += b; }); child.stderr.on('data', b => { output += b; });
t.after(async () => { if (child.exitCode === null && child.signalCode === null) { child.kill('SIGTERM'); await exited; } });
await until(() => /processing halted: chief launch skipped/.test(output) || child.exitCode !== null);
assert.match(output, /processing halted: chief launch skipped/);
assert.equal(child.exitCode, null, output);
const status = spawnSync(process.execPath, [CLI, 'bus', 'status', '--json'], { env: f.env, encoding: 'utf8', timeout: 5000 });
assert.equal(status.status, 0, status.stderr);
assert.equal(JSON.parse(status.stdout).host.live, true);
assert.equal(readControl(f.dataRoot).halted, true);
});
@@ -6,7 +6,6 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {createLauncher} from '../src/launcher.mjs';
import {initializeControl} from '../../processing-control/src/state.mjs';
import {fixture,tmp} from './helpers.mjs';
import {launchLogFile,SESSION_CREDENTIALS} from '../../seat/src/session.mjs';
@@ -20,7 +19,6 @@ function setup(t,{harness='claude-code',account=ACCOUNT,set=['CLAUDE_CODE_OAUTH_
if(account)doc.launch.account=account;
return doc;
}),ops=[];
initializeControl(f.dataRoot);
for(const k of SESSION_CREDENTIALS.flat())delete f.env[k];
for(const k of set)f.env[k]=VALUE;
const host={business:'acme',beforeClose:()=>{},op:async x=>{
@@ -6,16 +6,14 @@ import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {fileURLToPath} from 'node:url';
import {createLauncher} from '../src/launcher.mjs';
import {initializeControl} from '../../processing-control/src/state.mjs';
import {fixture,tmp} from './helpers.mjs';
import {launchLogFile} from '../../seat/src/session.mjs';
const HIT={path:['tokens','instances','coder'],limit:100,count:120,unknownTurns:1};
// answer is what the mayLaunch op returns, or an Error it throws.
function setup(t,answer,{initialize=true}={}){
function setup(t,answer){
const root=tmp(t),f=fixture(root,'acme',doc=>{doc.roles.coder.vars.model='claude-sonnet-5-5';return doc;}),ops=[];
if(initialize)initializeControl(f.dataRoot);
const host={business:'acme',beforeClose:()=>{},op:async x=>{
ops.push(x);
if(x.op==='identity')return {business:'acme',role:'pm'};
@@ -80,11 +78,3 @@ test('REQ-LIMIT-4 the launcher reaches token-limit only through the bus: launche
assert.doesNotMatch(src,/modules\(\s*["'](?!harness["'])/);
assert.doesNotMatch(src,/\bimport\s*\(/);
});
// Row 191: only trusted bus startup initializes control; an early seat launch refuses.
test('a launcher before trusted control initialization refuses processing-uninitialized',async t=>{
const {launcher,ops}=setup(t,{allow:true},{initialize:false});
assert.deepEqual(await launcher.fromSession('fixture-cap','coder'),{ok:false,error:'processing-uninitialized'});
assert.equal(ops.some(x=>x.op==='mayLaunch'||x.op==='authorizeLaunch'),false);
assert.deepEqual(launcher.registry.sessions,[]);
});
@@ -4,13 +4,11 @@ import assert from 'node:assert/strict';
import {join} from 'node:path';
import {readFileSync,writeFileSync} from 'node:fs';
import {createLauncher} from '../src/launcher.mjs';
import {initializeControl} from '../../processing-control/src/state.mjs';
import {fixture,tmp} from './helpers.mjs';
import {systemFor,writeJson} from '../../business/tests/helpers.mjs';
import {launchLogFile} from '../../seat/src/session.mjs';
test('each launch rereads limits and model selections; lowered limits keep sessions and log exact hit',async t=>{
const root=tmp(t),f=fixture(root,'acme',doc=>{doc.roles.coder.vars.model='claude-sonnet-5-5';return doc;}),file=join(root,'config/businesses/acme.json'),ops=[];
initializeControl(f.dataRoot);
const host={business:'acme',beforeClose:()=>{},op:async x=>{ops.push(x);if(x.op==='identity')return {business:'acme',role:'pm'};if(x.op==='mayLaunch')return {allow:true};if(x.op==='authorizeLaunch')throw Object.assign(Error('fixture authorization stop'),{code:'fixture-policy'});return {};}};
const launcher=createLauncher({host,system:JSON.parse(readFileSync(f.config,'utf8')),env:f.env,bwrap:'/usr/bin/true',log:()=>{}});
const session={business:'acme',instance:'reviewer',run:'fixture-existing',harness:'pi',provider:'zai',model:'claude-sonnet-5-5'};launcher.registry.add(session);
@@ -7,7 +7,6 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {createLauncher} from '../src/launcher.mjs';
import {initializeControl} from '../../processing-control/src/state.mjs';
import {fakeHarnesses,fixture,tmp} from './helpers.mjs';
import {launchLogFile,SESSION_CREDENTIALS} from '../../seat/src/session.mjs';
@@ -26,7 +25,6 @@ const ON=['pi','claude-code','down','sparse','metered','broken','throws','absent
// vars are the coder's whole agent vars; edit changes the rest of the file.
async function setup(t,vars,{on=ON,edit=doc=>doc,env:more={}}={}){
const root=tmp(t),f=fixture(root,'acme',doc=>{doc.roles.coder.vars=vars;doc.plugins={...doc.plugins,harness:on};return edit(doc);}),ops=[];
initializeControl(f.dataRoot);
Object.assign(f.env,more);
const host={business:'acme',beforeClose:()=>{},op:async x=>{
ops.push(x);
-46
View File
@@ -1075,49 +1075,3 @@ test("a runner that ignores SIGTERM is killed when the host closes", { skip, tim
assert.equal(ends.length, 1);
assert.equal(ends[0].signal, "SIGKILL");
});
// Row 191: root-scoped processing is stronger than per-business launch.restore.
test('kill switch refuses chief and hire before launch work and a clear permits a later request', { skip, timeout: 30000 }, async t => {
const { readControl, transition } = await import('../../processing-control/src/state.mjs');
const s = await setup(t);
const halted = transition(s.f.dataRoot, { business: 'acme', human: s.f.doc.human, halted: true });
await assert.rejects(s.launcher.launchPm(), /PM launch refused: processing-halted/);
assert.deepEqual(await s.launcher.fromSession(s.cto.cap, 'coder'), { ok: false, error: 'processing-halted' });
assert.deepEqual(readSessions(s.f.dataRoot), []);
transition(s.f.dataRoot, { business: 'acme', human: s.f.doc.human, halted: false, generation: halted.generation });
const run = await s.launcher.launchPm();
assert.equal(readControl(s.f.dataRoot).halted, false);
assert.equal(readSessions(s.f.dataRoot)[0].run, run.run);
});
test('kill switch stops an actual managed namespace and records the halt reason without rewriting its bundle', { skip, timeout: 30000 }, async t => {
const { transition } = await import('../../processing-control/src/state.mjs');
const s = await setup(t);
const run = await s.launcher.launchPm();
const entry = readSessions(s.f.dataRoot).find(r => r.run === run.run);
const session = readFileSync(join(entry.runDir, 'session.json'));
const halted = transition(s.f.dataRoot, { business: 'acme', human: s.f.doc.human, halted: true });
await until(() => !readSessions(s.f.dataRoot).some(r => r.run === run.run), 12000);
assert.deepEqual(readFileSync(join(entry.runDir, 'session.json')), session);
const end = log(s.f).find(r => r.event === 'end' && r.run === run.run);
assert.equal(end.reason, 'kill-switch');
assert.equal(end.haltGeneration, halted.generation);
assert.equal(startTimeOf(entry.runnerPid), null);
});
test('a launch waiting across halt and resume cannot use its stale admission', { skip, timeout: 30000 }, async t => {
const { transition } = await import('../../processing-control/src/state.mjs');
let root, first = true;
const prepared = prepare(t); root = prepared.f.dataRoot;
const s = await setup(t, doc => doc, prepared, { wrap: host => ({ ...host, async op(m) {
const answer = await host.op(m);
if (m.op === 'mayLaunch' && first) {
first = false;
const state = transition(root, { business: 'acme', human: 'jason', halted: true });
transition(root, { business: 'acme', human: 'jason', halted: false, generation: state.generation });
}
return answer;
} }) });
await assert.rejects(s.launcher.launchPm(), /processing-generation-changed/);
assert.deepEqual(readSessions(root), []);
});
-18
View File
@@ -451,21 +451,3 @@ test("runLoop never overlaps ticks and stops after the one in flight", async ()
assert.ok(after >= 2);
assert.equal(count, after);
});
// Row 191: the child remains alive for recovery but sends no processing while halted.
test('processing halt prevents notifier reads and sends and fences the wait before delivery', async t => {
const { initializeControl, transition } = await import('../../processing-control/src/state.mjs');
const { createProcessing } = await import('../../processing-control/src/coordinator.mjs');
const dataRoot = tmp(t); initializeControl(dataRoot);
const bus = broker(t);
bus.raise('git.push.protected', { target: 'refactor', blocking: true, task_ref: 'vikunja:1/7' });
const processing = createProcessing({ dataRoot }); t.after(() => processing.close());
const actor = { business: 'demo', human: 'jason' }; let reads = 0, sends = 0, haltInRead = false;
const notifier = createNotifier({ business: 'demo', dataRoot, processing, now: () => new Date(AT),
inbox: async () => { reads++; if (haltInRead) transition(dataRoot, { ...actor, halted: true }); return bus.read('inbox'); },
direct: { send: async () => { sends++; return { messageId: 'fixture' }; } } });
const halted = transition(dataRoot, { ...actor, halted: true });
await notifier.tick(); assert.equal(reads, 0); assert.equal(sends, 0);
transition(dataRoot, { ...actor, halted: false, generation: halted.generation }); haltInRead = true;
await notifier.tick(); assert.equal(reads, 1); assert.equal(sends, 0);
});
@@ -1,55 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { Store } from '../../bus/src/store.mjs';
import { Broker } from '../../bus/src/broker.mjs';
import { createProcessing } from '../../processing-control/src/coordinator.mjs';
import { main } from '../src/cli.mjs';
import { io, BUSINESSES } from './helpers.mjs';
function setup(t) {
const dataRoot = mkdtempSync(join(tmpdir(), 'processing-cli-')), calls = [];
const processing = createProcessing({ dataRoot });
const store = new Store(dataRoot);
const b = new Broker({ store, businesses: BUSINESSES, processing });
const human = b.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
const deps = { system: { dataRoot }, transport: business => async (verb, args = {}) => {
assert.equal(business, 'demo'); calls.push(verb); return b.request(human, { verb, args });
} };
t.after(async () => { await processing.close(); store.close(); rmSync(dataRoot, { recursive: true, force: true }); });
return { dataRoot, deps, calls };
}
test('human halt/status/resume uses management transport without loading any channel', async t => {
const f = setup(t), x = io();
await main(['halt', '--business', 'demo'], x, f.deps);
assert.match(x.out.text, /processing halted/);
assert.ok(x.out.text.includes(f.dataRoot), 'the affected instance root is named');
const status = io();
await main(['processing', 'status', '--business', 'demo', '--json'], status, f.deps);
assert.equal(JSON.parse(status.out.text).halted, true);
const resumed = io(); await main(['resume', '--business', 'demo'], resumed, f.deps);
assert.match(resumed.out.text, /processing resumed/);
assert.deepEqual(f.calls, ['processing.halt', 'processing.status', 'processing.status', 'processing.resume']);
});
for (const verb of ['halt', 'resume']) test(`${verb} refuses inside an agent before transport`, async t => {
const f = setup(t), x = io({ MOSAIC_RUN_ID: 'managed-run' });
await assert.rejects(main([verb, '--business', 'demo'], x, f.deps), e => e.exitCode === 3);
assert.deepEqual(f.calls, []);
});
test('unknown and partially stopped outcomes are never reported as a successful halt', async t => {
const f = setup(t);
const unknown = io();
await assert.rejects(main(['halt', '--business', 'demo'], unknown, {
...f.deps, transport: () => async () => { throw Object.assign(new Error('outcome-unknown'), { code: 'outcome-unknown' }); },
}), e => e.exitCode === 1 && /status/.test(e.message));
assert.doesNotMatch(unknown.out.text, /processing halted/);
const partial = io();
await assert.rejects(main(['halt', '--business', 'demo'], partial, {
...f.deps, transport: () => async () => ({ version: 1, generation: '11111111-1111-4111-8111-111111111111', operation: '11111111-1111-4111-8111-111111111111', business: 'demo', human: 'jason', at: '2026-10-20T00:00:00.000Z', recovered: false, halted: true, quiescent: false, remaining: 1, stopped: 0 }),
}), e => e.exitCode === 1 && /1.*unconfirmed/.test(e.message));
assert.doesNotMatch(partial.out.text, /all.*stopped/);
});
+1 -1
View File
@@ -49,7 +49,7 @@ test('a copy carries every file the harness modules read from it',async t=>{
test('shared copy: npm ci, pushed reachability, readonly/existing copy, webui delegation',t=>{
const s=setup(t),r=s.run('stack-copy.sh',['copy',s.id,'--copies',s.copies]);assert.equal(r.status,0,r.stderr);
const copy=join(s.copies,s.id);assert.equal(statSync(copy).mode&0o777,0o555);
for(const f of ['scripts/mosaic','adapters/pi/adapter.sh','packages/queue/src/cli.mjs','package.json','package-lock.json','packages/tasks/requirements.json','packages/project/src/index.mjs','packages/library/src/index.mjs','packages/project/schemas/project.schema.json','packages/project/templates/north-star.md','packages/plugins/src/kinds.mjs','packages/plugins/src/loader.mjs','packages/processing-control/src/state.mjs','packages/processing-control/src/containers.mjs','packages/processing-control/src/effects.mjs'])assert.ok(existsSync(join(copy,f)),f);
for(const f of ['scripts/mosaic','adapters/pi/adapter.sh','packages/queue/src/cli.mjs','package.json','package-lock.json','packages/tasks/requirements.json','packages/project/src/index.mjs','packages/library/src/index.mjs','packages/project/schemas/project.schema.json','packages/project/templates/north-star.md','packages/plugins/src/kinds.mjs','packages/plugins/src/loader.mjs'])assert.ok(existsSync(join(copy,f)),f);
const npm=join(s.bin,'npm');writeFileSync(npm,'#!/bin/sh\nexit 99\n');chmodSync(npm,0o755);
const existing=s.run('webui-service.sh',['copy',s.id,'--copies',s.copies]);assert.equal(existing.status,0);assert.equal(existing.stdout,`exists: ${copy}\n`);unlinkSync(npm);
git(s.repo,'update-ref','refs/remotes/origin/refactor',git(s.repo,'hash-object','-w','package.json'));
+3 -1
View File
@@ -858,7 +858,7 @@ const COUNTED=/^COUNTED="([^"]*)"$/m.exec(srText)[1].split(' ');
const STORE_JS=/^STORE_JS='([\s\S]*?)'$/m.exec(srText)[1];
// sage-1163's list 2, written out so that a table dropped from the script's COUNTED fails T45 as
// well as T46.
const LISTED=['role_claims','thread_claims','decision_events','messages','deliveries','task_snapshots','projects','threads','thread_opens','instance_names','usage_records'];
const LISTED=['role_claims','thread_claims','decision_events','messages','deliveries','task_snapshots','projects','threads','sessions','session_defaults','thread_opens','instance_names','usage_records'];
const LATE='2999-01-01T00:00:00.000Z';
const addRow=(file,table,at=LATE)=>{const db=new DatabaseSync(file);db.exec(`CREATE TABLE IF NOT EXISTS ${table} (seq INTEGER PRIMARY KEY, at TEXT NOT NULL)`);db.prepare(`INSERT INTO ${table} (at) VALUES (?)`).run(at);db.close()};
@@ -918,6 +918,8 @@ test('T46 row 181 item 4: every table in the bus schema is in exactly one of fou
const hasAt=(n)=>db.prepare(`SELECT count(*) AS n FROM pragma_table_info(?) WHERE name='at'`).get(n).n===1;
const always={decisions:'decision.raise writes action.allowed in the same transaction'};
const untimed={
gitea_grants:'no time column; row 236: each write goes in one transaction with a grant.* events row, which is counted',
gitea_users:'no time column; row 236: each write goes in one transaction with a grant.* events row, which is counted',
host_grants:'no time column; row 234: each write goes in one transaction with a grant.* events row, which is counted',
hosts:'no time column; row 234: each write goes in one transaction with a grant.* events row, which is counted',
meta:'key and value only; its one UPDATE is the migration step',
+8
View File
@@ -17,6 +17,12 @@ import { run } from "../src/cli.mjs";
import { CliError } from "../src/errors.mjs";
import { busExit } from "../src/transport.mjs";
import { humanEnv, io } from "./helpers.mjs";
// Row 264a (#1769): a thread made by hand gets the default session the 3k-3l migration gives a
// legacy thread, with the thread's id.
const DEFAULT_SESSION = [
"INSERT INTO sessions(at,business,thread,id,instance,owner,name,op) SELECT at,business,id,id,instance,owner,'default','legacy-3l' FROM threads WHERE id=?",
"INSERT INTO session_defaults(at,thread,session,by) SELECT at,id,id,'migration-3l' FROM threads WHERE id=?",
];
const shipped = (name) => JSON.parse(readFileSync(fileURLToPath(new URL(`../../../roles/${name}.json`, import.meta.url)), "utf8"));
const roles = {
@@ -185,6 +191,7 @@ test("REQ-CLI-1 a thread is its id, an id prefix of 8 characters or more, or <pr
const at = new Date().toISOString();
for (const n of ["1", "2"]) {
s.store.run("INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES(?,?,?,?,?,?,?)", `abcdef01-0000-4000-8000-00000000000${n}`, at, "demo", s.project, "assistant", "jason", `twin${n}`);
for (const sql of DEFAULT_SESSION) s.store.run(sql, `abcdef01-0000-4000-8000-00000000000${n}`);
}
const resolve = async (ref) => (await s.cli(["thread", "open", ref]));
const opened = (r) => r.out.trim();
@@ -322,6 +329,7 @@ test("REQ-USER-2 a thread of another user is refused for talk, open, read and fo
const theirs = randomUUID();
s.store.run("INSERT INTO projects(id,at,business,name,repo,created_by) VALUES(?,?,?,?,?,?)", "p-theirs", at, "demo", "theirs", "stack", "someone-else");
s.store.run("INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES(?,?,?,?,?,?,?)", theirs, at, "demo", "p-theirs", "assistant", "someone-else", "private");
for (const sql of DEFAULT_SESSION) s.store.run(sql, theirs);
// A message waiting for the user: message.receive would write a delivery row for it.
s.call("chief", "message.send", { to: "human", body: "waiting", class: "INFO" });
const before = s.writes();
+8 -1
View File
@@ -27,6 +27,12 @@ import { bootConfig, loadSystem } from "../src/config.mjs";
import { startTimeOf } from "../src/host.mjs";
import { createLauncher } from "../src/launcher.mjs";
import { fakeHarnesses, fixture, tmp } from "./helpers.mjs";
// Row 264a (#1769): a thread made by hand gets the default session the 3k-3l migration gives a
// legacy thread, with the thread's id.
const DEFAULT_SESSION = [
"INSERT INTO sessions(at,business,thread,id,instance,owner,name,op) SELECT at,business,id,id,instance,owner,'default','legacy-3l' FROM threads WHERE id=?",
"INSERT INTO session_defaults(at,thread,session,by) SELECT at,id,id,'migration-3l' FROM threads WHERE id=?",
];
const ADAPTER = fileURLToPath(new URL("./fixtures/thread-adapter.mjs", import.meta.url));
const skip = sandboxProblem() ?? false;
@@ -356,7 +362,8 @@ test("REQ-THREAD-2 REQ-LAUNCH-1 a thread id that isn't a safe segment, or a thre
store.run("INSERT INTO projects(id,at,business,name,repo,created_by) VALUES(?,?,?,?,?,?)", "p-hand", at(), "acme", "hand made", "stack", "jason");
for (const [id, instance] of [["../evil", "assistant"], ["coder-thread", "coder"]]) {
store.run("INSERT INTO threads(id,at,business,project,instance,owner,name) VALUES(?,?,?,?,?,?,?)", id, at(), "acme", "p-hand", instance, "jason", id);
store.run("INSERT INTO thread_opens(at,thread,by) VALUES(?,?,?)", at(), id, "jason");
for (const sql of DEFAULT_SESSION) store.run(sql, id);
store.run("INSERT INTO thread_opens(at,thread,session,by) VALUES(?,?,?,?)", at(), id, id, "jason");
}
store.close();
+8 -42
View File
@@ -50,8 +50,6 @@ import { existsSync, mkdirSync, mkdtempSync, writeFileSync, statSync, readdirSyn
import { join, resolve } from "node:path";
import { createHash } from "node:crypto";
import { DiscordError } from "./errors.mjs";
import { assertProcessing, processingStatus, ProcessingError } from "../../processing-control/src/state.mjs";
import { registerRuntime, updateRuntime, unregisterRuntime, processIdentity } from "../../processing-control/src/runtimes.mjs";
import { defaultConfigPath, loadDataRoot, bindingPath, bindingDataDir, loadBinding, readToken, resolveContextFiles, resolveToolRoots, reloadDiff } from "./binding.mjs";
import { createRest } from "./rest.mjs";
import { createGateway, CONNECTOR_INTENTS } from "./gateway.mjs";
@@ -60,7 +58,7 @@ import { TOOLS_ENV, enabledToolNames } from "./tools.mjs";
import { createSetsparkApi } from "./setspark.mjs";
import { assembleContext } from "./context.mjs";
import { createConnector } from "./connector.mjs";
import { ensureJournal, requestStop, requestProcessingStop, stopRequested, readPid, stopTarget, writePid, clearPid, unlock, recover, appendReload, BRAKE_EXIT } from "./journal.mjs";
import { ensureJournal, requestStop, stopRequested, readPid, stopTarget, writePid, clearPid, unlock, recover, appendReload, BRAKE_EXIT } from "./journal.mjs";
const USAGE = [
"usage: mosaic-discord check <binding> [--config PATH] [--repo PATH] [--offline]",
@@ -173,9 +171,6 @@ async function check(opts) {
}
async function run(opts) {
const root = loadDataRoot(opts.config);
const generation = assertProcessing(root).generation;
const processing = () => assertProcessing(root, generation);
const { bindingFile, binding, contextFiles, toolRoots, pi, journalDir, sessionDir } = prepare(opts);
const token = readToken(binding);
ensureJournal(journalDir);
@@ -185,18 +180,8 @@ async function run(opts) {
}
if (stopRequested(journalDir)) throw new DiscordError(`STOP is present in ${journalDir}; remove it to run`, BRAKE_EXIT);
writePid(journalDir, process.pid);
let managed = null;
const cleanupPid = () => {
clearPid(journalDir, process.pid);
if (!managed) return;
if (processingStatus(root).halted) updateRuntime(root, managed.id, { phase: 'ended' });
else unregisterRuntime(root, managed.id);
};
const cleanupPid = () => clearPid(journalDir, process.pid);
try {
processing();
managed = registerRuntime(root, { kind: 'discord', business: 'instance', run: binding.name });
updateRuntime(root, managed.id, { ...processIdentity(process.pid), phase: 'running', group: false });
processing();
await runClaimed();
} catch (err) {
cleanupPid();
@@ -216,7 +201,6 @@ async function run(opts) {
const rest = createRest({ token, log: warn });
const gw = await rest.getGatewayBot();
processing();
const gateway = createGateway({ url: gw.url, token, intents: CONNECTOR_INTENTS });
gateway.on("log", (m) => warn(`gateway: ${m}`));
gateway.on("ready", (r) => warn(`gateway: READY as ${r.user && r.user.id}, session ${r.sessionId}`));
@@ -224,7 +208,6 @@ async function run(opts) {
gateway.on("closed", (c) => warn(`gateway: closed ${c.code} ${c.reason} reconnect=${c.willReconnect}`));
const engine = createEngine({
processingRoot: root, binding: binding.name,
command: pi,
args: buildPiArgs({ ...binding.engine, sessionDir, appendSystemPromptFile: promptFile, continueSession, tools: toolRoots }),
cwd: opts.repo,
@@ -240,17 +223,15 @@ async function run(opts) {
// key as the model's verbs; without a setspark key it has none, and an
// approval request from the model is refused.
const api = binding.tools && binding.tools.setspark ? createSetsparkApi(binding.tools.setspark) : null;
const connector = createConnector({ binding, journalDir, rest, gateway, engine, api, processing, log: warn });
const connector = createConnector({ binding, journalDir, rest, gateway, engine, api, log: warn });
let shuttingDown = false;
let exitCode = 0;
let processingWatch = null;
const shutdown = (code, immediate = false) => {
const shutdown = (code) => {
if (shuttingDown) return;
shuttingDown = true;
clearInterval(processingWatch);
exitCode = code;
warn(immediate ? 'stopping: processing halted' : "stopping: finishing in-flight turns");
connector.stop({ immediate }).catch((err) => warn(`stop failed: ${err.message}`)).finally(() => {
warn("stopping: finishing in-flight turns");
connector.stop().catch((err) => warn(`stop failed: ${err.message}`)).finally(() => {
cleanupPid();
process.exit(exitCode);
});
@@ -259,20 +240,8 @@ async function run(opts) {
warn(`gateway fatal close ${f.code}: ${f.reason}`);
shutdown(2);
});
process.on("SIGTERM", () => shutdown(0, processingStatus(root).halted === true));
process.on("SIGTERM", () => shutdown(0));
process.on("SIGINT", () => shutdown(0));
processingWatch = setInterval(() => {
try { processing(); }
catch {
const state = processingStatus(root);
if (state.halted && !state.invalid) {
try { requestProcessingStop(journalDir, state.generation); }
catch { warn('processing STOP could not be recorded'); }
}
shutdown(BRAKE_EXIT, true);
}
}, 100);
processingWatch.unref();
// Reload on SIGHUP: re-read the file, refuse anything that is not a
// reloadable difference, verify new channels over REST, then swap.
@@ -281,13 +250,11 @@ async function run(opts) {
const reloadNow = async () => {
const at = new Date().toISOString();
try {
processing();
const next = loadBinding(bindingFile);
if (next.name !== binding.name) throw new DiscordError(`reload: name ${JSON.stringify(next.name)} does not match ${binding.name}`);
const diff = reloadDiff(connector.binding, next);
for (const id of diff.channels.added) {
const ch = await rest.getChannel(id);
processing();
if (ch.guild_id !== next.guildId) throw new DiscordError(`reload: channel ${id} is in guild ${ch.guild_id}, not ${next.guildId}`);
}
connector.reload(next);
@@ -363,7 +330,6 @@ function reloadCommand(opts) {
function recoverCommand(opts) {
const dataRoot = loadDataRoot(opts.config);
assertProcessing(dataRoot);
const binding = loadBinding(bindingPath(dataRoot, opts.binding));
const journalDir = bindingDataDir(dataRoot, binding.name);
ensureJournal(journalDir);
@@ -390,7 +356,7 @@ async function main() {
main().then((code) => {
if (code !== null) process.exit(code);
}).catch((err) => {
const code = err instanceof ProcessingError ? BRAKE_EXIT : err instanceof DiscordError ? err.exitCode : 1;
const code = err instanceof DiscordError ? err.exitCode : 1;
warn(err.message);
if (err.details && err.details.nonces) warn(`nonces: ${err.details.nonces.join(", ")}`);
process.exit(code);
+4 -34
View File
@@ -66,7 +66,6 @@ export function createConnector({
setTimeoutImpl = globalThis.setTimeout, clearTimeoutImpl = globalThis.clearTimeout,
typingIntervalMs = 8000,
readReceipt = READ_RECEIPT,
processing = () => {},
log = () => {},
} = {}) {
for (const [k, v] of Object.entries({ binding: initialBinding, journalDir, rest, gateway, engine })) {
@@ -74,22 +73,6 @@ export function createConnector({
}
ensureJournal(journalDir);
let binding = initialBinding;
const guarded = object => new Proxy(object, {
get(target, key) {
const value = Reflect.get(target, key);
if (typeof value !== 'function') return value;
return (...args) => { processing(); return value.apply(target, args); };
},
});
rest = guarded(rest);
if (api) api = guarded(api);
function processingRefusal() {
try { processing(); return null; }
catch (e) {
if (e.code?.startsWith('processing-')) return e.code;
throw e;
}
}
const state = {
inbox: new Set(), channels: new Map(), inFlight: 0, typingTimer: null, typingChannel: null,
@@ -157,7 +140,6 @@ export function createConnector({
function typingTick() {
state.typingTimer = null;
if (processingRefusal()) return;
if (state.inFlight === 0 || state.typingChannel === null) return;
rest.typing(state.typingChannel);
state.typingTimer = setTimeoutImpl(typingTick, typingIntervalMs);
@@ -305,8 +287,6 @@ export function createConnector({
// is answered at once with a private line; an approval is deferred, the
// service is asked, and the request message is then edited in place.
async function handleInteraction(interaction) {
const refusal = processingRefusal();
if (refusal) return { accepted: false, reason: refusal };
if (!interaction || typeof interaction !== "object" || typeof interaction.id !== "string") return { accepted: false, reason: "not-an-interaction" };
if (interaction.type !== INTERACTION_TYPE.MESSAGE_COMPONENT) return { accepted: false, reason: "not-a-component" };
if (state.interactions.has(interaction.id)) return { accepted: false, reason: "duplicate" };
@@ -403,7 +383,6 @@ ${status === "not-posted" ? APPROVAL_LINES.pressAgain : APPROVAL_LINES.failed}`;
record.engine.turns = result.turns ?? null;
if (binding.tools) record.tools = result.tools || [];
if (result.model) record.engine.model = result.model;
processing();
if (result.text.length === 0) throw new DiscordError("engine returned no text", 1, { code: "engine-empty" });
reply = await deliver({ channelId: targetChannel, replyTo: message.id, text: result.text, noncePrefix: message.id });
const opened = (result.tools || []).filter((t) => t && t.name === "open_approval_request" && t.ok && t.request);
@@ -413,12 +392,10 @@ ${status === "not-posted" ? APPROVAL_LINES.pressAgain : APPROVAL_LINES.failed}`;
}
} catch (err) {
status = "failed";
error = { code: err.code || (err.details && err.details.code) || "error", message: err.message };
error = { code: (err.details && err.details.code) || "error", message: err.message };
log(`turn ${message.id} failed: ${error.code}: ${error.message}`);
// Q19: one fixed line, never model output, on failure.
if (!processingRefusal()) {
reply = await deliver({ channelId: targetChannel, replyTo: message.id, text: FIXED_LINES.failed, noncePrefix: `${message.id}-f` });
}
reply = await deliver({ channelId: targetChannel, replyTo: message.id, text: FIXED_LINES.failed, noncePrefix: `${message.id}-f` });
} finally {
state.inFlight -= 1;
typingStop();
@@ -459,8 +436,6 @@ ${status === "not-posted" ? APPROVAL_LINES.pressAgain : APPROVAL_LINES.failed}`;
}
async function admit(message) {
const refusal = processingRefusal();
if (refusal) return drop(message, refusal);
// A thread the connector has not seen: one REST lookup, only when the
// cheap checks (guild, listed author) would let the message through.
const listed = binding.channels.some((c) => c.id === message.channel_id);
@@ -472,8 +447,6 @@ ${status === "not-posted" ? APPROVAL_LINES.pressAgain : APPROVAL_LINES.failed}`;
log(`channel lookup ${message.channel_id} failed: ${err.message}`);
}
}
const afterLookup = processingRefusal();
if (afterLookup) return drop(message, afterLookup);
const auth = authorize(binding, message, channelInfo);
if (!auth.ok) return drop(message, auth.reason);
if (typeof message.content !== "string") return drop(message, "no-content");
@@ -594,12 +567,10 @@ ${status === "not-posted" ? APPROVAL_LINES.pressAgain : APPROVAL_LINES.failed}`;
},
async start() {
processing();
if (state.started) throw new DiscordError("connector already started", 1);
if (stopRequested(journalDir)) throw new DiscordError(`STOP is present in ${journalDir}; remove it to run`, 1);
const reconciled = await reconcile();
const approvals = await reconcileApprovals();
processing();
if (approvals.length > 0) log(`approvals: ${approvals.map((a) => `${a.kind} ${a.requestId} ${a.status}`).join("; ")}`);
state.inbox = readInboxIds(journalDir);
state.started = true;
@@ -611,14 +582,13 @@ ${status === "not-posted" ? APPROVAL_LINES.pressAgain : APPROVAL_LINES.failed}`;
},
// Finish in-flight turns (each has its own timeout), then close.
async stop({ immediate = false } = {}) {
async stop() {
state.stopping = true;
gateway.close(1000, "stop");
if (immediate) await engine.stop();
await Promise.allSettled([...state.turnPromises]);
await state.deliveryChain;
typingStop();
if (!immediate) await engine.stop();
await engine.stop();
},
};
}
+2 -55
View File
@@ -35,8 +35,6 @@ import { spawn as nodeSpawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import { DiscordError } from "./errors.mjs";
import { enabledToolNames } from "./tools.mjs";
import { assertProcessing, processingStatus } from "../../processing-control/src/state.mjs";
import { registerRuntime, updateRuntime, unregisterRuntime, processIdentity, runtime, runtimeGroupState, createRuntimeSignaller } from "../../processing-control/src/runtimes.mjs";
export const PI_FIXED_ARGS = Object.freeze([
"--mode", "rpc", "--no-extensions", "--no-context-files", "--no-skills",
@@ -83,7 +81,6 @@ export function createEngine({
log = () => {},
onExit = () => {},
abortGraceMs = ABORT_GRACE_MS,
processingRoot = null, binding = null,
} = {}) {
if (typeof command !== "string" || command.length === 0) throw new DiscordError("engine: command required", 1);
if (!Array.isArray(args)) throw new DiscordError("engine: args required", 1);
@@ -93,18 +90,6 @@ export function createEngine({
// wedged: set when the engine gave up on pi and is stopping it. Nothing
// is sent to that child again.
const state = { child: null, buffer: "", pending: [], held: [], responses: new Map(), nextId: 1, busy: false, exited: null, wedged: false };
let managed = null;
const processing = () => {
if (processingRoot) return assertProcessing(processingRoot, managed?.generation ?? null);
};
const releaseRuntime = () => {
if (!managed) return;
try {
if (!processingStatus(processingRoot).halted && runtimeGroupState(runtime(processingRoot, managed.id)) === false)
unregisterRuntime(processingRoot, managed.id);
}
catch { log('engine: processing receipt cleanup refused'); }
};
// A turn that fails on the client side (timeout, protocol error) stays in
// the pending queue, marked done, until pi's own turn_end for it arrives.
@@ -297,7 +282,6 @@ export function createEngine({
}
function write(command) {
if (command.type === 'prompt') processing();
if (!state.child || state.exited !== null || state.wedged) throw new DiscordError("engine is not running", 1, { code: "engine-down" });
state.child.stdin.write(JSON.stringify(command) + "\n");
}
@@ -317,25 +301,7 @@ export function createEngine({
function stopChild({ graceMs = 5000 } = {}) {
const child = state.child;
if (!child || (state.exited !== null && !managed)) return Promise.resolve(state.exited);
if (managed) {
let record;
try { record = runtime(processingRoot, managed.id); } catch { return Promise.resolve(state.exited); }
const signal = createRuntimeSignaller(record, members => updateRuntime(processingRoot, managed.id, { members }));
signal('SIGTERM');
const began = Date.now();
return new Promise(resolve => {
let killed = false;
const poll = setInterval(() => {
let live;
try { live = runtimeGroupState(runtime(processingRoot, managed.id)); } catch { live = false; }
if (live === false || Date.now() - began > graceMs + 2000) {
clearInterval(poll); releaseRuntime(); resolve(state.exited); return;
}
if (!killed && Date.now() - began >= graceMs) { signal('SIGKILL'); killed = true; }
}, 20);
});
}
if (!child || state.exited !== null) return Promise.resolve(state.exited);
return new Promise((resolve) => {
const timer = setTimeoutImpl(() => {
try {
@@ -360,23 +326,7 @@ export function createEngine({
return {
start() {
if (state.child) throw new DiscordError("engine already started", 1);
processing();
if (processingRoot) managed = registerRuntime(processingRoot, { kind: 'discord', business: 'instance', run: binding });
let child;
try {
processing();
child = spawn(command, args, { cwd, env, stdio: ["pipe", "pipe", "pipe"], ...(processingRoot ? { detached: true } : {}) });
if (managed) {
const identity = processIdentity(child.pid);
if (!identity) throw new DiscordError('engine process identity unavailable', 1);
updateRuntime(processingRoot, managed.id, { ...identity, group: true, phase: 'running' });
processing();
}
} catch (e) {
if (child?.pid) child.kill('SIGKILL');
releaseRuntime();
throw e;
}
const child = spawn(command, args, { cwd, env, stdio: ["pipe", "pipe", "pipe"] });
state.child = child;
child.stdout.setEncoding("utf8");
child.stdout.on("data", (chunk) => {
@@ -392,14 +342,12 @@ export function createEngine({
child.stderr.setEncoding("utf8");
child.stderr.on("data", (chunk) => log(`pi: ${chunk.trimEnd()}`));
child.on("error", (err) => {
releaseRuntime();
log(`engine spawn error: ${err.message}`);
state.exited = { code: null, signal: null, error: err.message };
failAll("engine-down", `engine failed: ${err.message}`);
onExit(state.exited);
});
child.on("exit", (code, signal) => {
releaseRuntime();
state.exited = { code, signal };
failAll("engine-down", `engine exited (code ${code}, signal ${signal})`);
onExit(state.exited);
@@ -410,7 +358,6 @@ export function createEngine({
// Resolves {text, message, tools, turns, usage, model, provider}. Rejects
// with DiscordError carrying details.code for the turn record.
prompt(text, { timeoutMs = 180000 } = {}) {
try { processing(); } catch (e) { return Promise.reject(e); }
if (typeof text !== "string" || text.length === 0) throw new DiscordError("prompt text required", 1);
const turn = { resolve: null, reject: null, timer: null, grace: null, done: false, accepted: false, tools: new Map(), turns: 0, last: null };
const done = new Promise((resolve, reject) => {
+2 -50
View File
@@ -21,7 +21,7 @@
// Directories are 0700, files 0600. Lines are appended, never rewritten.
import {
appendFileSync, closeSync, constants, existsSync, fstatSync, fsyncSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, rmdirSync, rmSync,
appendFileSync, closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync,
unlinkSync, writeFileSync, writeSync,
} from "node:fs";
import { join } from "node:path";
@@ -176,64 +176,16 @@ export function stopRequested(dir) {
}
export function requestStop(dir, reason = "stop") {
return stopLock(dir, () => appendStop(dir, reason));
}
function stopLock(dir, action) {
const lock = join(dir, '.stop.lock');
try { mkdirSync(lock, { mode: 0o700 }); }
catch { throw new DiscordError('STOP update is locked; refusing', 1); }
try { return action(); } finally { rmdirSync(lock); }
}
function appendStop(dir, reason) {
const path = stopPath(dir);
const fd = openSync(path, constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW, 0o600);
const fd = openSync(path, "a", 0o600);
try {
writeSync(fd, JSON.stringify({ at: new Date().toISOString(), reason }) + "\n");
fsyncSync(fd);
} finally {
closeSync(fd);
}
return path;
}
const PROCESSING_GENERATION = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
function processingStops(dir) {
let fd;
try {
fd = openSync(stopPath(dir), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
const st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o7777) !== 0o600 || st.size > 65536) return null;
const buffer = Buffer.alloc(65537), size = readSync(fd, buffer, 0, buffer.length, 0);
if (size > 65536) return null;
const bytes = buffer.subarray(0, size);
return new TextDecoder('utf8', { fatal: true }).decode(bytes).split('\n').filter(Boolean).map(JSON.parse);
} catch (e) { return e.code === 'ENOENT' ? [] : null; }
finally { if (fd !== undefined) closeSync(fd); }
}
export function requestProcessingStop(dir, generation) {
if (!PROCESSING_GENERATION.test(generation)) throw new DiscordError('invalid processing generation', 1);
return stopLock(dir, () => {
const rows = processingStops(dir);
if (rows === null) throw new DiscordError('STOP cannot be verified', 1);
const reason = `processing:${generation}`;
if (!rows.some(r => r?.reason === reason)) appendStop(dir, reason);
return stopPath(dir);
});
}
export function clearProcessingStop(dir, generation) {
if (!PROCESSING_GENERATION.test(generation)) return false;
return stopLock(dir, () => {
const rows = processingStops(dir);
if (!rows?.length || rows.some(r => !r || Object.keys(r).sort().join(',') !== 'at,reason' ||
r.reason !== `processing:${generation}` || !Number.isFinite(Date.parse(r.at)) ||
new Date(r.at).toISOString() !== r.at)) return false;
unlinkSync(stopPath(dir));
return true;
});
}
export function clearStop(dir) {
const path = stopPath(dir);
if (existsSync(path)) unlinkSync(path);
-30
View File
@@ -1,30 +0,0 @@
// Host-only adapters for the root coordinator. Workers cannot select these paths.
import { lstatSync } from 'node:fs';
import { dirname } from 'node:path';
import { bindingDataDir } from './binding.mjs';
import { requestProcessingStop, clearProcessingStop } from './journal.mjs';
import { listRuntimes, unregisterRuntime } from '../../processing-control/src/runtimes.mjs';
function journal(root, record) {
const dir = bindingDataDir(root, record.run);
for (const path of [dirname(dir), dir]) {
const st = lstatSync(path);
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o077))
throw new Error('processing-discord-path-unsafe');
}
return dir;
}
export function discordProcessingHooks(dataRoot) {
return {
beforeStop(record, generation) {
if (record.kind === 'discord') requestProcessingStop(journal(dataRoot, record), generation);
},
onResume(generation) {
for (const r of listRuntimes(dataRoot)) {
if (r.kind === 'discord' && r.haltGeneration === generation) {
clearProcessingStop(journal(dataRoot, r), generation);
unregisterRuntime(dataRoot, r.id);
}
}
},
};
}
-2
View File
@@ -7,7 +7,6 @@ import { validateBinding, loadBinding, readToken, checkPrivateFile, resolveConte
import { homedir } from "node:os";
import { DiscordError } from "../src/errors.mjs";
import { makeRoot, makeRepo, makeDeployment, rawBinding } from "./helpers.mjs";
import { initializeControl } from '../../processing-control/src/state.mjs';
const cli = join(import.meta.dirname, "..", "src", "cli.mjs");
@@ -203,7 +202,6 @@ test("cli: run refuses when STOP is present, before any network use", () => {
const root = makeRoot();
const repo = makeRepo(root);
const dep = makeDeployment(root);
initializeControl(dep.dataRoot);
const r0 = runCli(["stop", "test-seat", "--config", dep.config]);
assert.equal(r0.status, 0, r0.stderr);
assert.match(r0.stdout, /STOP written/);
@@ -1,54 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, writeFileSync, symlinkSync, unlinkSync } from 'node:fs';
import { join } from 'node:path';
import { randomUUID } from 'node:crypto';
import { makeRoot } from './helpers.mjs';
import { ensureJournal, requestStop, stopRequested, stopPath } from '../src/journal.mjs';
const api = await import('../src/journal.mjs');
function fixture() { const dir = join(makeRoot(), 'journal'); ensureJournal(dir); return dir; }
test('resume clears only its own generation-tagged Discord STOP and keeps manual or newer brakes', () => {
assert.equal(typeof api.requestProcessingStop, 'function');
const dir = fixture(), generation = randomUUID();
api.requestProcessingStop(dir, generation); const first = readFileSync(stopPath(dir));
api.requestProcessingStop(dir, generation); assert.deepEqual(readFileSync(stopPath(dir)), first);
assert.equal(api.clearProcessingStop(dir, randomUUID()), false);
assert.equal(api.clearProcessingStop(dir, generation), true); assert.equal(stopRequested(dir), false);
api.requestProcessingStop(dir, generation); requestStop(dir, 'cli stop');
const mixed = readFileSync(stopPath(dir));
assert.equal(api.clearProcessingStop(dir, generation), false); assert.deepEqual(readFileSync(stopPath(dir)), mixed);
});
test('unreadable, malformed and symlink STOP never get removed or followed by processing recovery', () => {
assert.equal(typeof api.requestProcessingStop, 'function');
const dir = fixture(), generation = randomUUID();
writeFileSync(stopPath(dir), 'unreadable operator brake\n', { mode: 0o600 });
assert.equal(api.clearProcessingStop(dir, generation), false);
unlinkSync(stopPath(dir)); const outside = join(dir, 'outside');
writeFileSync(outside, 'untouched', { mode: 0o600 }); symlinkSync(outside, stopPath(dir));
assert.equal(api.clearProcessingStop(dir, generation), false);
assert.throws(() => api.requestProcessingStop(dir, generation));
assert.equal(readFileSync(outside, 'utf8'), 'untouched');
});
test('coordinator halt writes the Discord brake and resume clears only its own stopped runtime', async () => {
const { initializeControl } = await import('../../processing-control/src/state.mjs');
const { registerRuntime, updateRuntime } = await import('../../processing-control/src/runtimes.mjs');
const { createProcessing } = await import('../../processing-control/src/coordinator.mjs');
const { discordProcessingHooks } = await import('../src/processing.mjs');
const root = makeRoot(); initializeControl(root);
const dir = join(root, 'discord', 'test-seat'); ensureJournal(dir);
const processing = createProcessing({ dataRoot: root, ...discordProcessingHooks(root) });
const first = registerRuntime(root, { kind: 'discord', business: 'instance', run: 'test-seat' });
updateRuntime(root, first.id, { phase: 'ended' });
const halted = await processing.halt({ business: 'demo', human: 'jason' });
assert.equal(halted.quiescent, true); assert.equal(stopRequested(dir), true);
await processing.resume({ business: 'demo', human: 'jason', generation: halted.generation });
assert.equal(stopRequested(dir), false);
const second = registerRuntime(root, { kind: 'discord', business: 'instance', run: 'test-seat' });
updateRuntime(root, second.id, { phase: 'ended' });
const again = await processing.halt({ business: 'demo', human: 'jason' });
requestStop(dir, 'cli stop'); const bytes = readFileSync(stopPath(dir));
await processing.resume({ business: 'demo', human: 'jason', generation: again.generation });
assert.deepEqual(readFileSync(stopPath(dir)), bytes);
await processing.close();
});
@@ -1,82 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdirSync, unlinkSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { createConnector } from '../src/connector.mjs';
import { initializeControl, assertProcessing, transition } from '../../processing-control/src/state.mjs';
import { makeRoot, makeRepo, makeDeployment, binding, message, fakeRest, fakeGateway, fakeEngine } from './helpers.mjs';
import { appendOutbox, ensureJournal, readOutbox, listTurns } from '../src/journal.mjs';
const actor = { business: 'demo', human: 'jason', halted: true };
function fixture() {
const root = makeRoot(); initializeControl(root);
const journalDir = join(root, 'journal'), rest = fakeRest(), gateway = fakeGateway(), engine = fakeEngine({ replies: [{ text: 'reply' }] });
const processing = () => assertProcessing(root);
const options = { binding: binding(), journalDir, rest, gateway, engine, processing };
return { root, journalDir, rest, gateway, engine, options };
}
test('halted connector refuses before outbox reconciliation, engine or gateway start', async () => {
const f = fixture(); ensureJournal(f.journalDir);
appendOutbox(f.journalDir, { nonce: 'pending', channelId: '100000000000000200', status: 'intent', content: 'do not replay', at: new Date().toISOString() });
transition(f.root, actor);
await assert.rejects(createConnector(f.options).start(), e => e.code === 'processing-halted');
assert.equal(f.rest.calls.length, 0); assert.equal(f.engine.started, false);
const corrupt = fixture(); ensureJournal(corrupt.journalDir);
const connector = createConnector(corrupt.options);
transition(corrupt.root, actor);
writeFileSync(join(corrupt.journalDir, 'outbox.jsonl'), '{broken\n', { mode: 0o600 });
await assert.rejects(connector.start(), e => e.code === 'processing-halted');
assert.equal(corrupt.rest.calls.length, 0); assert.equal(corrupt.engine.started, false);
});
test('halt arriving during startup reconciliation prevents engine and gateway start', async () => {
const f = fixture(); ensureJournal(f.journalDir);
appendOutbox(f.journalDir, { nonce: 'pending', channelId: '100000000000000200', status: 'intent', content: 'first', at: new Date().toISOString() });
const create = f.rest.createMessage;
f.rest.createMessage = async (...args) => { const result = await create(...args); transition(f.root, actor); return result; };
await assert.rejects(createConnector(f.options).start(), e => e.code === 'processing-halted');
assert.equal(f.engine.started, false);
});
test('halted connector refuses a new inbound turn before claiming inbox or invoking the engine', async () => {
const f = fixture(), connector = createConnector(f.options); await connector.start();
transition(f.root, actor);
const answer = await connector.handleMessage(message());
assert.equal(answer.accepted, false); assert.equal(answer.reason, 'processing-halted');
assert.equal(connector.inboxSize, 0); assert.equal(f.engine.prompts.length, 0);
await connector.stop();
});
test('halt during a model turn never sends its answer or a fallback failure message', async () => {
const f = fixture();
f.engine.prompt = async () => { transition(f.root, actor); return { text: 'must not send', usage: { input: 1, output: 1 } }; };
const connector = createConnector(f.options); await connector.start();
const turn = await connector.handleMessage(message()); await turn.turn;
assert.equal(f.rest.calls.length, 0);
assert.equal(listTurns(f.journalDir)[0].error.code, 'processing-halted');
await connector.stop();
});
test('Discord run and recover refuse a halted root before reading a token or reaching network', () => {
const root = makeRoot(), repo = makeRepo(root), d = makeDeployment(root);
mkdirSync(d.dataRoot, { recursive: true, mode: 0o700 }); initializeControl(d.dataRoot);
transition(d.dataRoot, actor); unlinkSync(d.tokenFile);
for (const command of ['run', 'recover']) {
const r = spawnSync(process.execPath, [new URL('../src/cli.mjs', import.meta.url).pathname, command, d.raw.name, '--config', d.config, '--repo', repo], { encoding: 'utf8', timeout: 5000 });
assert.equal(r.status, 3, r.stderr); assert.match(r.stderr, /processing-halted/);
}
});
test('Discord engine publishes a verified group and a root halt kills it independently of the connector', async t => {
const { createEngine } = await import('../src/engine-pi.mjs');
const { listRuntimes, sweepRuntimes, processIdentity } = await import('../../processing-control/src/runtimes.mjs');
const f = fixture();
const engine = createEngine({ command: process.execPath,
args: [new URL('./fake-pi.mjs', import.meta.url).pathname], cwd: f.root,
env: { ...process.env }, processingRoot: f.root, binding: 'test-seat' });
t.after(() => engine.stop({ graceMs: 50 }));
const child = engine.start();
const tracked = listRuntimes(f.root);
assert.equal(tracked.length, 1); assert.equal(tracked[0].pid, child.pid);
assert.equal(tracked[0].kind, 'discord'); assert.equal(tracked[0].group, true);
const halted = transition(f.root, actor);
await assert.rejects(engine.prompt('not admitted'), e => e.code === 'processing-halted');
const result = await sweepRuntimes(f.root, halted.generation, { graceMs: 80, killWaitMs: 1000 });
assert.equal(result.quiescent, true); assert.equal(processIdentity(child.pid), null);
});
-2
View File
@@ -13,7 +13,6 @@ import {
} from "../src/journal.mjs";
import { DiscordError } from "../src/errors.mjs";
import { makeRoot, makeRepo, makeDeployment } from "./helpers.mjs";
import { initializeControl } from '../../processing-control/src/state.mjs';
const cli = fileURLToPath(new URL("../src/cli.mjs", import.meta.url));
const worker = fileURLToPath(new URL("../fixtures/legacy-owner-worker.mjs", import.meta.url));
@@ -147,7 +146,6 @@ test("cli: recover exits 0 when ready, 3 behind a brake or a held binding, and r
const root = makeRoot();
const repo = makeRepo(root);
const dep = makeDeployment(root);
initializeControl(dep.dataRoot);
const run = (args) => spawnSync(process.execPath, [cli, ...args, "--config", dep.config, "--repo", repo], { encoding: "utf8" });
let r = run(["recover", "test-seat"]);
assert.equal(r.status, 0, r.stderr);
+7 -15
View File
@@ -34,30 +34,22 @@ deep-frozen copy. `where` names the entry in messages. `fail(code, message)` def
- `types` maps a folder name to `character`, `place`, `research`, `tracker` or `summary`.
`manuscript` lists folder names and shares none with `types`.
- A universe has `name`, `books` and `playground`. A hybrid adds `kind: "hybrid"`, two or more
plain-universe `sources`, `constraints` (each one `{ "exclude": ... }` or
`{ "pin": { "entity", "field", "value" } }`) and `notes`, and may have `books: []`. A draft
universe has only `kind: "draft"`, `name` and one or more `playground` prefixes, and comes back as
`{ kind: "draft", name, books: [], playground }`. `kind` is absent, `"hybrid"` or `"draft"`.
- A playground prefix ends in `/` and has one of three levels (C2-01c). Book level lies inside one of
its own entry's book folders. Universe level, in a plain universe or a hybrid, lies outside every
book folder. A draft's prefixes all lie outside every book folder. No prefix holds another or is
listed twice, across every entry.
non-hybrid `sources`, `constraints` (each one `{ "exclude": ... }` or
`{ "pin": { "entity", "field", "value" } }`) and `notes`.
Refusals:
| Code | When |
|---|---|
| `library-playground-other-universe` | a playground prefix lies inside, or holds, a book folder of another entry |
| `library-mapping-invalid` | any other shape fault: unknown key, bad remote or branch, an unknown `kind`, a book folder in two universes, a book folder that holds another, a playground prefix that holds another or is listed twice, a prefix equal to a book folder or holding its own entry's book, a draft with `books` or with no prefix, a hybrid source that is unknown, a hybrid or a draft |
| `library-playground-outside-book` | a playground prefix is not strictly inside a book of its own universe |
| `library-mapping-invalid` | any other shape fault: unknown key, bad remote or branch, a book folder in two universes, a book folder that holds another, a playground prefix that holds another, an unknown hybrid source |
## Scope
`classify(mapping, path)` decides scope from the path alone. Only paths ending in lowercase `.md` are
mapped. It checks prefixes first. A path under any entry's playground prefix is `playground`, with that
entry as `universe` and the book folder holding the prefix as `book`, or `null` at universe level and
in a draft. Any other path under a book folder is `canon`. Everything else is `unmapped`. The type is
the first folder after the playground prefix, or after the book: a `types` name, `manuscript`, or
`null`.
mapped. A path under a playground prefix is `playground`. Any other path under a book folder is
`canon`. Everything else is `unmapped`. The type is the first folder after the book, or after the
playground prefix: a `types` name, `manuscript`, or `null`.
## Reader
+14 -30
View File
@@ -1,6 +1,5 @@
// The library project entry of a business file (design 1.1): her folders mapped onto universes,
// books, type folders, manuscript folders and playground prefixes at three levels (ruling after r11,
// C2-01c): inside one of the entry's books, outside every book, or in a draft universe. A pure validator. Business file
// books, type folders, manuscript folders and playground prefixes. A pure validator. Business file
// loading wires it in C2-04, which passes its own `fail` so the refusal reads as a business file
// problem. Agents can't write this entry (M9, C2-04).
import { raise } from './problems.mjs';
@@ -11,7 +10,6 @@ const BRANCH = /^[A-Za-z0-9][A-Za-z0-9._/-]{0,127}$/;
// C2-03a (sage-2021): the bot account sync answers git's Username prompt with. Row 199's NAME.
const USER = /^[A-Za-z0-9_.-]{1,100}$/;
const CONSTRAINT_KEYS = ['exclude', 'pin'];
const KINDS = ['hybrid', 'draft'];
// A folder segment from her vault: spaces and capitals are fine, separators, control characters
// and dot names are not.
@@ -63,29 +61,29 @@ export function checkMapping(entry, where = 'library', { fail = raise } = {}) {
object(entry.universes, 'universes');
if (!Object.keys(entry.universes).length) bad('universes must name at least one universe');
const universes = {}, bookOwner = new Map(), prefixes = [], prefixOwner = new Map();
const universes = {}, bookOwner = new Map(), prefixes = [];
for (const [id, universe] of Object.entries(entry.universes)) {
const name = `universes.${id}`;
if (!ID.test(id)) bad(`universe id ${JSON.stringify(id)} is not a plain id`);
object(universe, name);
if (Object.hasOwn(universe, 'kind') && !KINDS.includes(universe.kind)) bad(`${name}.kind must be "hybrid" or "draft"`);
const hybrid = universe.kind === 'hybrid', draft = universe.kind === 'draft';
if (draft && Object.hasOwn(universe, 'books')) bad(`${name} is a draft and takes no books`);
only(universe, hybrid ? ['kind', 'name', 'sources', 'books', 'playground', 'constraints', 'notes']
: draft ? ['kind', 'name', 'playground'] : ['name', 'books', 'playground'], name);
const hybrid = universe.kind === 'hybrid';
only(universe, hybrid ? ['kind', 'name', 'sources', 'books', 'playground', 'constraints', 'notes'] : ['name', 'books', 'playground'], name);
if (typeof universe.name !== 'string' || !universe.name.trim() || universe.name.length > 128 || /[\u0000-\u001f]/.test(universe.name))
bad(`${name}.name must be a short display name`);
const books = draft ? [] : list(universe.books, `${name}.books`, folder, 'a folder path');
if (!hybrid && !draft && !books.length) bad(`${name}.books must name at least one book folder`);
const books = list(universe.books, `${name}.books`, folder, 'a folder path');
if (!hybrid && !books.length) bad(`${name}.books must name at least one book folder`);
for (const book of books) {
if (bookOwner.has(book)) bad(`book folder ${JSON.stringify(book)} is in two universes`);
bookOwner.set(book, id);
}
const playground = list(universe.playground ?? [], `${name}.playground`,
value => typeof value === 'string' && value.endsWith('/') && folder(value.slice(0, -1)), 'a folder path ending in /');
if (draft && !playground.length) bad(`${name}.playground must name at least one prefix`);
for (const prefix of playground) { prefixes.push(prefix); prefixOwner.set(prefix, id); }
if (draft) { universes[id] = { kind: 'draft', name: universe.name, books, playground }; continue; }
for (const prefix of playground) {
// Every playground file must have a universe and a book (1.1, B1).
if (!books.some(book => holds(book, prefix) && prefix !== `${book}/`))
fail('library-playground-outside-book', `${where} ${name}.playground ${JSON.stringify(prefix)} is not inside one of its books`);
prefixes.push(prefix);
}
const out = { name: universe.name, books, playground };
if (hybrid) {
out.kind = 'hybrid';
@@ -116,28 +114,14 @@ export function checkMapping(entry, where = 'library', { fail = raise } = {}) {
}
for (const [id, universe] of Object.entries(universes)) {
for (const source of universe.sources ?? []) {
if (!Object.hasOwn(universes, source) || Object.hasOwn(universes[source], 'kind'))
bad(`universes.${id}.sources names ${JSON.stringify(source)}, which is not a plain universe`);
if (!Object.hasOwn(universes, source) || universes[source].kind === 'hybrid')
bad(`universes.${id}.sources names ${JSON.stringify(source)}, which is not a non-hybrid universe`);
}
}
const books = [...bookOwner.keys()];
for (const a of books) for (const b of books) if (a !== b && holds(a, b)) bad(`book folder ${JSON.stringify(a)} holds book folder ${JSON.stringify(b)}`);
for (const a of prefixes) for (const b of prefixes) if (a !== b && holds(a, b)) bad(`playground ${JSON.stringify(a)} holds playground ${JSON.stringify(b)}`);
if (new Set(prefixes).size !== prefixes.length) bad('a playground prefix is listed twice');
// A prefix's level comes from where it sits (ruling after r11). Inside one of its own entry's books
// is book level, outside every book is universe level, and a draft's prefixes are all outside. Scope
// checks prefixes first (scope.mjs), so a prefix equal to or holding a book would turn that book
// into playground.
for (const [prefix, id] of prefixOwner) {
for (const [book, owner] of bookOwner) {
const at = `universes.${id}.playground ${JSON.stringify(prefix)}`;
if (prefix === `${book}/`) bad(`${at} is the whole book folder ${JSON.stringify(book)}`);
const inside = holds(book, prefix), holding = holds(prefix, book);
if ((inside || holding) && owner !== id)
fail('library-playground-other-universe', `${where} ${at} ${inside ? 'lies inside' : 'holds'} book folder ${JSON.stringify(book)} of universes.${owner}`);
if (holding) bad(`${at} holds its own book folder ${JSON.stringify(book)}`);
}
}
return deepFreeze({
kind: 'library', remote: entry.remote, branch: entry.branch,
+10 -21
View File
@@ -3,30 +3,19 @@
const inside = (folder, path) => path.startsWith(folder.endsWith('/') ? folder : `${folder}/`);
// { scope: 'playground' | 'canon', universe, book, type, folder } or { scope: 'unmapped' }.
// Prefixes come first (ruling after r11, C2-01c): a `.md` file under any entry's playground prefix is
// playground, with that entry as `universe` and the book folder that holds the prefix as `book`, or
// null at universe level and in a draft. Otherwise a `.md` file in a book folder is canon. `type` is
// the mapped type of the first folder under the prefix (playground) or under the book (canon),
// 'manuscript' for a manuscript folder, else null. `folder` is that folder name. checkMapping keeps
// prefixes from overlapping, so at most one matches.
// `type` is the mapped type of the first folder under the book (canon) or under the playground
// prefix (playground), 'manuscript' for a manuscript folder, else null. `folder` is that folder name.
export function classify(mapping, path) {
if (typeof path !== 'string' || !path.endsWith('.md')) return { scope: 'unmapped' };
const entries = Object.entries(mapping.universes);
for (const [universe, entry] of entries) {
const prefix = entry.playground.find(p => inside(p, path));
if (prefix) return place(mapping, path, prefix, { scope: 'playground', universe, book: entry.books.find(b => inside(b, prefix)) ?? null });
}
for (const [universe, entry] of entries) {
for (const [universe, entry] of Object.entries(mapping.universes)) {
const book = entry.books.find(b => inside(b, path));
if (book) return place(mapping, path, `${book}/`, { scope: 'canon', universe, book });
if (!book) continue;
const prefix = entry.playground.find(p => inside(p, path));
const rest = path.slice((prefix ?? `${book}/`).length).split('/');
const folder = rest.length > 1 ? rest[0] : null;
const type = folder === null ? null : Object.hasOwn(mapping.types, folder) ? mapping.types[folder]
: mapping.manuscript.includes(folder) ? 'manuscript' : null;
return { scope: prefix ? 'playground' : 'canon', universe, book, type, folder };
}
return { scope: 'unmapped' };
}
function place(mapping, path, under, where) {
const rest = path.slice(under.length).split('/');
const folder = rest.length > 1 ? rest[0] : null;
const type = folder === null ? null : Object.hasOwn(mapping.types, folder) ? mapping.types[folder]
: mapping.manuscript.includes(folder) ? 'manuscript' : null;
return { ...where, type, folder };
}
+8 -52
View File
@@ -13,57 +13,16 @@ test('the fixture mapping checks and comes back frozen', () => {
assert.throws(() => { m.types.Extra = 'place'; });
});
const draft = (playground = ['Drafts/New World/']) => ({ kind: 'draft', name: 'New World', playground });
test('C2-01c: a prefix inside or holding another entry\'s book refuses library-playground-other-universe', () => {
refuses(e => { e.universes.harbor.playground = ['Second World/Playground/']; return e; },
'library-playground-other-universe', /universes.harbor.playground "Second World\/Playground\/" lies inside book folder "Second World" of universes.second/);
refuses(e => { e.universes.second.books = ['Worlds/Second']; e.universes.harbor.playground = ['Worlds/']; return e; },
'library-playground-other-universe', /"Worlds\/" holds book folder "Worlds\/Second" of universes.second/);
// A draft has no books, so a draft prefix inside a book is always inside another entry's.
refuses(e => { e.universes.idea = draft(['Book One/New World/']); return e; },
'library-playground-other-universe', /universes.idea.playground "Book One\/New World\/" lies inside book folder "Book One" of universes.harbor/);
});
test('C2-01c: a prefix equal to a book folder, or holding its own, refuses library-mapping-invalid', () => {
// The whole book as its playground would leave no canon.
refuses(e => { e.universes.harbor.playground = ['Book One/']; return e; }, 'library-mapping-invalid', /is the whole book folder "Book One"/);
refuses(e => { e.universes.harbor.playground = ['Second World/']; return e; }, 'library-mapping-invalid', /is the whole book folder "Second World"/);
refuses(e => { e.universes.harbor.books = ['Shelf/Book One']; e.universes.harbor.playground = ['Shelf/']; return e; },
'library-mapping-invalid', /"Shelf\/" holds its own book folder "Shelf\/Book One"/);
});
test('C2-01c: universe-level prefixes lie outside every book, in a plain universe and in a hybrid with no books', () => {
const m = mapping(e => {
e.universes.harbor.playground = ['Book One/Playground/', 'Harbor Ideas/'];
e.universes.cross = { kind: 'hybrid', name: 'Cross', sources: ['harbor', 'second'], books: [], playground: ['Crossover Ideas/'] };
return e;
});
assert.deepEqual(m.universes.harbor.playground, ['Book One/Playground/', 'Harbor Ideas/']);
assert.deepEqual([m.universes.cross.books, m.universes.cross.playground], [[], ['Crossover Ideas/']]);
});
test('C2-01c: a draft takes kind, name and playground only, and comes back frozen with books []', () => {
const m = mapping(e => { e.universes.idea = draft(); return e; });
assert.deepEqual(m.universes.idea, { kind: 'draft', name: 'New World', books: [], playground: ['Drafts/New World/'] });
assert.ok(Object.isFrozen(m.universes.idea) && Object.isFrozen(m.universes.idea.books) && Object.isFrozen(m.universes.idea.playground));
refuses(e => { e.universes.idea = { ...draft(), books: ['New World'] }; return e; }, 'library-mapping-invalid', /universes.idea is a draft and takes no books/);
refuses(e => { e.universes.idea = { ...draft(), books: [] }; return e; }, 'library-mapping-invalid', /takes no books/);
refuses(e => { e.universes.idea = draft([]); return e; }, 'library-mapping-invalid', /universes.idea.playground must name at least one prefix/);
refuses(e => { e.universes.idea = { kind: 'draft', name: 'New World' }; return e; }, 'library-mapping-invalid', /at least one prefix/);
refuses(e => { e.universes.idea = { ...draft(), notes: 'x' }; return e; }, 'library-mapping-invalid', /unknown key "notes"/);
for (const kind of ['library', 'Draft', 'plain', null, 1])
refuses(e => { e.universes.harbor.kind = kind; return e; }, 'library-mapping-invalid', /universes.harbor.kind must be "hybrid" or "draft"/);
test('a playground prefix outside its universe books refuses library-playground-outside-book', () => {
refuses(e => { e.universes.harbor.playground = ['Second World/Playground/']; return e; }, 'library-playground-outside-book');
refuses(e => { e.universes.harbor.playground = ['Elsewhere/']; return e; }, 'library-playground-outside-book');
// The whole book as its playground would leave no canon: not inside the book.
refuses(e => { e.universes.harbor.playground = ['Book One/']; return e; }, 'library-playground-outside-book');
});
test('no playground prefix holds another, and no book folder holds another', () => {
refuses(e => { e.universes.harbor.playground = ['Book One/Playground/', 'Book One/Playground/Deep/']; return e; },
'library-mapping-invalid', /holds playground/);
// The rule covers every level and every entry (C2-01c).
refuses(e => { e.universes.harbor.playground = ['Ideas/']; e.universes.idea = draft(['Ideas/New World/']); return e; },
'library-mapping-invalid', /playground "Ideas\/" holds playground "Ideas\/New World\/"/);
refuses(e => { e.universes.harbor.playground = ['Ideas/']; e.universes.idea = draft(['Ideas/']); return e; },
'library-mapping-invalid', /listed twice/);
refuses(e => { e.universes.second.books = ['Second World', 'Book One/Part Two']; return e; }, 'library-mapping-invalid', /holds book folder/);
refuses(e => { e.universes.second.books = ['Book One']; return e; }, 'library-mapping-invalid', /in two universes/);
});
@@ -99,10 +58,7 @@ test('a hybrid universe is a shape the query code can take (1.4), with sources t
notes: 'Set after book one.' }; return e; };
const m = checkMapping(hybrid(mappingEntry()));
assert.deepEqual(m.universes.cross.sources, ['harbor', 'second']);
refuses(e => { hybrid(e); e.universes.cross.sources = ['harbor', 'nowhere']; return e; }, 'library-mapping-invalid', /not a plain universe/);
// A draft has no canon to draw on (C2-01c).
refuses(e => { hybrid(e); e.universes.idea = draft(); e.universes.cross.sources = ['harbor', 'idea']; return e; },
'library-mapping-invalid', /sources names "idea", which is not a plain universe/);
refuses(e => { hybrid(e); e.universes.cross.sources = ['harbor', 'nowhere']; return e; }, 'library-mapping-invalid', /non-hybrid universe/);
refuses(e => { hybrid(e); e.universes.cross.sources = ['harbor']; return e; }, 'library-mapping-invalid', /two or more/);
refuses(e => { hybrid(e); e.universes.cross.constraints = [{ exclude: 'x', pin: {} }]; return e; }, 'library-mapping-invalid', /exactly one/);
});
@@ -111,9 +67,9 @@ test('a caller-supplied fail receives the code, as business file loading will pa
const seen = [];
const fail = (code, message) => { seen.push(code); throw new Error(message); };
const entry = mappingEntry();
entry.universes.harbor.playground = ['Second World/Playground/'];
entry.universes.harbor.playground = ['Elsewhere/'];
assert.throws(() => checkMapping(entry, 'businesses/acme.json projects.books', { fail }), /businesses\/acme.json projects.books/);
assert.deepEqual(seen, ['library-playground-other-universe']);
assert.deepEqual(seen, ['library-playground-outside-book']);
});
test('scope comes from the path alone (design 1.2)', () => {
-55
View File
@@ -1,55 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { classify, readLibrary } from '../src/index.mjs';
import { BASE, mapping, repo } from './helpers.mjs';
// Playgrounds at three levels (ruling after r11, C2-01c). The fixture's harbor keeps its book-level
// prefix and gains a universe-level one. A hybrid with no books has a universe-level prefix, and a
// draft has its own.
const levels = () => mapping(e => {
e.universes.harbor.playground = ['Book One/Playground/', 'Harbor Ideas/'];
e.universes.cross = { kind: 'hybrid', name: 'Cross', sources: ['harbor', 'second'], books: [], playground: ['Crossover Ideas/'] };
e.universes.idea = { kind: 'draft', name: 'New World', playground: ['Drafts/New World/'] };
return e;
});
test('C2-01c: each level gives its universe, and its book or null', () => {
const m = levels();
assert.deepEqual(classify(m, 'Book One/Playground/Characters/New.md'),
{ scope: 'playground', universe: 'harbor', book: 'Book One', type: 'character', folder: 'Characters' });
assert.deepEqual(classify(m, 'Harbor Ideas/Places/Island.md'),
{ scope: 'playground', universe: 'harbor', book: null, type: 'place', folder: 'Places' });
assert.deepEqual(classify(m, 'Harbor Ideas/Loose.md'), { scope: 'playground', universe: 'harbor', book: null, type: null, folder: null });
assert.deepEqual(classify(m, 'Crossover Ideas/Chapters/Meeting.md'),
{ scope: 'playground', universe: 'cross', book: null, type: 'manuscript', folder: 'Chapters' });
assert.deepEqual(classify(m, 'Drafts/New World/Characters/Ana.md'),
{ scope: 'playground', universe: 'idea', book: null, type: 'character', folder: 'Characters' });
for (const path of ['Harbor Ideas/Board.canvas', 'Crossover Ideas/Sketch.canvas', 'Drafts/New World/Map.canvas',
'Drafts/Other.md', 'Drafts/New World.md', 'Harbor Ideasx/a.md'])
assert.deepEqual(classify(m, path), { scope: 'unmapped' }, path);
});
test('C2-01c: prefixes come first: in a book that holds a prefix, a file outside it is canon and one under it playground', () => {
const m = levels();
assert.deepEqual(classify(m, 'Book One/Places/Dock.md'), { scope: 'canon', universe: 'harbor', book: 'Book One', type: 'place', folder: 'Places' });
assert.deepEqual(classify(m, 'Book One/Playground.md'), { scope: 'canon', universe: 'harbor', book: 'Book One', type: null, folder: null });
assert.deepEqual(classify(m, 'Book One/Playground/What If.md'),
{ scope: 'playground', universe: 'harbor', book: 'Book One', type: null, folder: null });
assert.deepEqual(classify(m, 'Second World/Chapters/01.md'), { scope: 'canon', universe: 'second', book: 'Second World', type: 'manuscript', folder: 'Chapters' });
});
test('C2-01c: a canon file linking into a draft gives R6, and into its own universe-level prefix gives R5', async t => {
const r = repo(t, { ...BASE, 'Harbor Ideas/Island.md': 'An island.\n', 'Drafts/New World/Ana.md': 'Ana.\n',
'Book One/Places/Dock.md': 'See [[Island]].\n', 'Book One/Places/Pier.md': 'Like [[Ana]].\n' });
const result = await readLibrary({ repo: r.repo, commit: r.commit, mapping: levels() });
const codes = path => result.problems.filter(p => p.path === path).map(p => p.code).sort();
const file = path => result.files.find(f => f.path === path);
assert.deepEqual(codes('Book One/Places/Dock.md'), ['R5']);
// Any playground gives R5. A draft is never a source, so it gives R6 as well.
assert.deepEqual(codes('Book One/Places/Pier.md'), ['R5', 'R6']);
assert.deepEqual(file('Book One/Places/Pier.md').links.map(l => l.path), ['Drafts/New World/Ana.md']);
for (const [path, universe] of [['Harbor Ideas/Island.md', 'harbor'], ['Drafts/New World/Ana.md', 'idea']]) {
assert.deepEqual([file(path).scope, file(path).universe, file(path).book], ['playground', universe, null], path);
assert.deepEqual(codes(path), [], path);
}
});
-42
View File
@@ -1,42 +0,0 @@
# Instance processing control
The durable switch is `<dataRoot>/control/processing.json`, using the root resolved
by the existing system configuration loader. All businesses in that root share
one switch; separate roots remain independent. No package selects an instance by
business name or systemd unit name.
A human runs `mosaic halt [--business ID]`, `mosaic processing status --json` and
`mosaic resume [--business ID]`. The business chooses the human proof context.
Halt publishes state before waiting for stops. Resume checks the displayed
generation and refuses while a stop is unresolved. Neither agents nor tester
sessions can change the switch. CLI channel settings cannot disable recovery.
The bus remains available for reads and human management. Managed processing,
new sessions, task containers, tracker activity and outbound notification stop.
Native diagnostic/development seats remain available. A remote request already
accepted cannot be undone: its completion or unknown outcome remains evidence.
No task is automatically retried on resume.
The trusted host explicitly initializes the first off document. Once control/
exists, a missing, malformed, unsafe or oversized state refuses work. Recovery
is halt, then resume; unsafe parent ownership/path boundaries require operator
repair. Never delete the file to clear a halt. State publication uses exclusive
temporary files, fsync and atomic rename. Run evidence remains write-once.
Runtime receipts bind generated ids to the canonical root, boot id and process
start ticks or inspected container id and labels. Stops begin concurrently,
TERM first, then KILL after five seconds, with two seconds for observation.
Unknown remains unknown and prevents resume. Stop receipts are write-once and
mirrored into reserved bus actions. An interrupted Docker CLI is not evidence
that its container stopped; a container that cannot be inspected stays unknown.
Task containers mount the whole control directory read-only, below the existing
root mount. The image entrypoint uses the shared strict reader and admission
generation. A row-191 upgrade must stop old processing before the first trusted
initialization and align the image, bus and copy in a separately approved window.
Nothing in a code build performs that activation.
The Console status and persistent halted banner work read-only. The controls
require the existing authenticated human write path and `--send`; during Q175's
interim the UI explains that the instance CLI is the working path. No write
exception or alternate authentication scheme is introduced.
-6
View File
@@ -1,6 +0,0 @@
{
"name": "@mosaic/processing-control",
"private": true,
"type": "module",
"scripts": { "test": "node --test tests/*.test.mjs" }
}
@@ -1,23 +0,0 @@
#!/usr/bin/env node
// The shell launchers already load/release/bootstrap. Resolve the selected config again here.
import { randomUUID } from 'node:crypto';
import { loadSystem } from '../../cli/src/config.mjs';
import { runContainer } from './containers.mjs';
import { unregisterRuntime } from './runtimes.mjs';
import { initializeControl, assertProcessing } from './state.mjs';
try {
const system = loadSystem();
initializeControl(system.dataRoot);
assertProcessing(system.dataRoot);
const args = process.argv.slice(2), tty = args[0] === '--tty';
if (args.length === 1 && args[0] === '--check') process.exit(0);
if (tty) args.shift();
const result = runContainer(system.dataRoot, { kind: 'container', business: 'instance', run: randomUUID() },
{ tty, args, env: process.env, stdio: 'inherit' });
if (!result.haltGeneration && !result.proc.error && result.proc.status === 0) unregisterRuntime(system.dataRoot, result.managed.id);
if (result.proc.error) throw result.proc.error;
process.exit(result.proc.status ?? 1);
} catch (e) {
process.stderr.write(`processing: ${e.code?.startsWith('processing-') ? e.code : 'container-launch-failed'}\n`);
process.exit(3);
}
@@ -1,86 +0,0 @@
import { randomUUID } from 'node:crypto';
import { spawnSync, execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { initializeControl, assertProcessing } from './state.mjs';
import { registerRuntime, updateRuntime, runtime, rootTag } from './runtimes.mjs';
const execute = promisify(execFile);
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export function prepareContainer(dataRoot, record) {
initializeControl(dataRoot);
const state = assertProcessing(dataRoot);
const containerName = `mosaic-${rootTag(dataRoot).slice(0, 12)}-${randomUUID()}`;
const managed = registerRuntime(dataRoot, { ...record, containerName });
assertProcessing(dataRoot, state.generation);
return managed;
}
export function containerArgs(record, { tty = false, args = [] } = {}) {
return ['compose', 'run', '--rm', ...(!tty ? ['-T'] : []), '--name', record.containerName,
'--label', `mosaic.root=${record.root}`, '--label', `mosaic.runtime=${record.id}`,
'-e', `MOSAIC_PROCESSING_GENERATION=${record.generation}`, 'mosaic-agent', ...args];
}
export function runContainer(dataRoot, record, { tty = false, args = [], ...options } = {}) {
const managed = prepareContainer(dataRoot, record);
assertProcessing(dataRoot, managed.generation);
// Keep this synchronous. The independent broker sweeps the prepublished name while this waits.
const proc = spawnSync('docker', containerArgs(managed, { tty, args }), options);
const current = runtime(dataRoot, managed.id);
// An interrupted Docker client is not proof its container exited. Keep that receipt for inspection.
if (!proc.error && proc.status === 0) updateRuntime(dataRoot, managed.id, { phase: 'ended' });
return { proc, managed, haltGeneration: current.haltGeneration ?? null };
}
export function createContainerStopper({ dataRoot = null, docker = async args => {
const r = await execute('docker', args, { encoding: 'utf8', timeout: 2000, maxBuffer: 131072 });
return r.stdout;
} } = {}) {
return async (record, { graceMs = 5000, killWaitMs = 2000 } = {}) => {
const signals = [];
let id = record.containerId ?? null, seen = false;
const start = Date.now(), termEnd = start + graceMs, end = termEnd + killWaitMs;
async function inspect(target) {
const doc = JSON.parse(await docker(['inspect', '--format', '{{json .}}', target]));
if (!doc || !/^[a-f0-9]{64}$/.test(doc.Id) || doc.Name !== '/' + record.containerName ||
doc.Config?.Labels?.['mosaic.root'] !== record.root || doc.Config?.Labels?.['mosaic.runtime'] !== record.id ||
typeof doc.State?.Running !== 'boolean' || (id && doc.Id !== id)) {
const error = new Error('container identity mismatch'); error.code = 'identity-mismatch'; throw error;
}
if (!id) {
id = doc.Id;
if (dataRoot) updateRuntime(dataRoot, record.id, { containerId: id });
}
seen = true;
return doc;
}
while (Date.now() <= end) {
try {
const doc = await inspect(id ?? record.containerName);
if (!doc.State.Running) return { outcome: signals.includes('SIGKILL') ? 'killed' : signals.length ? 'terminated' : 'already-exited', signals };
const next = !signals.length ? 'TERM' : Date.now() >= termEnd && !signals.includes('SIGKILL') ? 'KILL' : null;
if (next) {
// Revalidate immediately before each signal, targeting the immutable inspected id.
const checked = await inspect(id);
if (!checked.State.Running) continue;
await docker(['kill', '--signal', next, id]);
signals.push('SIG' + next);
}
} catch (e) {
// A missing name can still be a queued create. Only a container observed before a signal
// and then removed by --rm is known to have finished. Never signal on an inspect failure.
if (e.code === 'identity-mismatch') return { outcome: 'unknown', signals };
if (id && seen && signals.length) {
try {
const listed = await docker(['ps', '--all', '--no-trunc', '--filter', `id=${id}`, '--format', '{{.ID}}']);
if (listed.trim() === '') return { outcome: signals.includes('SIGKILL') ? 'killed' : 'terminated', signals };
} catch { /* a failed Docker request proves nothing about the workload */ }
}
if (dataRoot) {
try {
if (runtime(dataRoot, record.id).phase === 'ended')
return { outcome: signals.includes('SIGKILL') ? 'killed' : signals.length ? 'terminated' : 'already-exited', signals };
} catch { return { outcome: 'unknown', signals }; }
}
}
await pause(Math.min(20, Math.max(1, end - Date.now())));
}
return { outcome: 'unknown', signals };
};
}
@@ -1,74 +0,0 @@
import { initializeControl, processingStatus, transition, ProcessingError } from './state.mjs';
import { readStops, sweepRuntimes } from './runtimes.mjs';
import { withProcessing, currentProcessing } from './effects.mjs';
// The control bus stays reachable. Only its processing, supervised workloads and background effects stop.
export function createProcessing({ dataRoot, graceMs = 5000, killWaitMs = 2000, containerStop = null,
onTransition = () => {}, onStop = () => {}, beforeStop = () => {}, onResume = () => {}, log = () => {} }) {
try { initializeControl(dataRoot); } catch (e) {
if (!e.code?.startsWith('processing-')) throw e;
}
let active = null, timer = null, closed = false;
let last = { quiescent: true, remaining: 0, stopped: 0 };
let settledGeneration = null;
const read = () => {
const state = processingStatus(dataRoot);
const counts = state.invalid || (state.halted && state.generation !== settledGeneration)
? { quiescent: false, remaining: null, stopped: 0 } : last;
return { ...state, ...counts, stopping: active !== null };
};
function reconcile() {
if (active) return active;
const state = processingStatus(dataRoot);
if (state.invalid) return Promise.resolve({ quiescent: false, remaining: null, stopped: 0 });
active = Promise.resolve().then(async () => {
await onTransition(state);
if (state.halted) {
const result = await sweepRuntimes(dataRoot, state.generation, { graceMs, killWaitMs, containerStop, beforeStop });
for (const stop of readStops(dataRoot, state.generation)) await onStop(stop);
const { results, ...summary } = result;
last = summary;
settledGeneration = state.generation;
} else last = { quiescent: true, remaining: 0, stopped: 0 };
return last;
}).finally(() => { active = null; });
return active;
}
return {
read,
work: fn => withProcessing(dataRoot, fn),
guard: () => currentProcessing()?.guard(),
signal: () => currentProcessing()?.signal,
stopReceipts: generation => readStops(dataRoot, generation),
// Publication is synchronous: no timer/model/HTTP wait can delay the admission fence.
halt(actor) {
const published = transition(dataRoot, { ...actor, halted: true });
return reconcile().then(summary => ({ ...published, ...summary }));
},
async resume(actor) {
if (active) throw new ProcessingError('processing-stop-pending');
const state = processingStatus(dataRoot);
if (state.invalid) throw new ProcessingError(state.code);
if (state.generation !== actor.generation) throw new ProcessingError('processing-generation-changed');
const summary = await reconcile();
if (!summary.quiescent) throw new ProcessingError('processing-stop-pending');
await onResume(state.generation);
const published = transition(dataRoot, { ...actor, halted: false });
await onTransition(published);
last = { quiescent: true, remaining: 0, stopped: 0 };
return { ...published, ...last };
},
reconcile,
start() {
if (timer || closed) return;
const tick = () => {
if (!active && processingStatus(dataRoot).halted) reconcile().catch(() => log('processing-reconcile-failed'));
};
tick(); timer = setInterval(tick, 250); timer.unref();
},
async close() {
closed = true; clearInterval(timer); timer = null;
await active;
},
};
}
@@ -1,61 +0,0 @@
import { AsyncLocalStorage } from 'node:async_hooks';
import { randomUUID } from 'node:crypto';
import { assertProcessing } from './state.mjs';
import { registerRuntime, updateRuntime, unregisterRuntime, processIdentity, createRuntimeSignaller, runtimeGroupState, runtime } from './runtimes.mjs';
const contexts = new AsyncLocalStorage();
export const currentProcessing = () => contexts.getStore();
// Trusted compensation/temporary-file cleanup only; never an agent work entry point.
export const withoutProcessing = fn => contexts.run(undefined, fn);
export async function withProcessing(dataRoot, fn) {
const parent = currentProcessing();
const generation = assertProcessing(dataRoot, parent?.dataRoot === dataRoot ? parent.generation : null).generation;
const pending = registerRuntime(dataRoot, { kind: 'effect', business: 'instance', run: randomUUID() });
const controller = new AbortController();
const children = [];
const guard = () => assertProcessing(dataRoot, generation);
const poll = setInterval(() => {
try { guard(); } catch (e) { controller.abort(e); }
}, 25);
poll.unref();
const scope = {
dataRoot, generation, signal: controller.signal, guard,
track(child) {
const identity = processIdentity(child.pid);
if (!identity) return;
children.push(new Promise(resolve => child.once('close', resolve)));
let record;
try { record = registerRuntime(dataRoot, { kind: 'effect', business: 'instance', run: randomUUID() }); }
catch (e) {
createRuntimeSignaller({ ...identity, kind: 'effect', group: true })('SIGKILL');
throw e;
}
const running = updateRuntime(dataRoot, record.id, { ...identity, group: true, phase: 'running' });
const signal = createRuntimeSignaller(running, members => updateRuntime(dataRoot, record.id, { members }));
let kill = null;
const cancel = () => {
signal('SIGTERM');
kill = setTimeout(() => signal('SIGKILL'), 5000);
kill.unref();
};
controller.signal.addEventListener('abort', cancel, { once: true });
child.once('close', () => {
controller.signal.removeEventListener('abort', cancel); clearTimeout(kill);
try {
if (runtimeGroupState(runtime(dataRoot, record.id)) === false) unregisterRuntime(dataRoot, record.id);
} catch { /* keep unverifiable receipts for reconciliation */ }
});
try { guard(); } catch (e) { controller.abort(e); }
if (controller.signal.aborted && kill === null) cancel();
},
};
try {
guard();
const result = await contexts.run(scope, () => fn(scope));
guard();
return result;
} finally {
clearInterval(poll);
await Promise.all(children);
unregisterRuntime(dataRoot, pending.id);
}
}
@@ -1,7 +0,0 @@
// Image-baked final admission, using the same strict state reader as the host.
import { assertProcessing } from './state.mjs';
try { assertProcessing(process.argv[2], process.env.MOSAIC_PROCESSING_GENERATION ?? null); }
catch (e) {
process.stderr.write(`run-agent: ${e.code?.startsWith('processing-') ? e.code : 'processing-state-invalid'}\n`);
process.exit(3);
}
@@ -1,405 +0,0 @@
// Only host-side starters publish these identities. A receipt is never a request from a worker.
import { randomUUID, createHash } from 'node:crypto';
import { constants, closeSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, linkSync,
readdirSync, readSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { controlDirectory, readControl, assertProcessing, ProcessingError } from './state.mjs';
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
const ID = /^[a-zA-Z0-9][a-zA-Z0-9_.:@/-]{0,159}$/;
const fail = () => { throw new ProcessingError('processing-runtime-invalid'); };
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
export const rootTag = root => createHash('sha256').update(dirname(controlDirectory(root))).digest('hex');
export function processIdentity(pid) {
try {
const raw = readFileSync(`/proc/${pid}/stat`, 'utf8');
const fields = raw.slice(raw.lastIndexOf(')') + 2).split(' ');
if (fields[0] === 'Z') return null;
return { pid, startTime: fields[19], boot: readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim() };
} catch { return null; }
}
function alive(identity) {
if (!identity || !Number.isSafeInteger(identity.pid) || identity.pid < 2 ||
typeof identity.startTime !== 'string' || !UUID.test(identity.boot)) return null;
const current = processIdentity(identity.pid);
if (current) return current.boot === identity.boot && current.startTime === identity.startTime;
try {
const stat = readFileSync(`/proc/${identity.pid}/stat`, 'utf8');
if (stat.slice(stat.lastIndexOf(')') + 2).split(' ')[0] === 'Z') return false;
process.kill(identity.pid, 0); return null;
}
catch (e) {
if (e.code === 'ESRCH') return false;
if (e.code === 'ENOENT') {
try { process.kill(identity.pid, 0); } catch (gone) { if (gone.code === 'ESRCH') return false; }
}
return null;
}
}
function directory(root, ...segments) {
// Checking the state also validates the service-owned control parent. A halted state is fine here.
readControl(root);
let dir = controlDirectory(root);
for (const name of segments) {
if (!['runtimes', 'stops', 'run-stops'].includes(name) && !UUID.test(name)) fail();
const parent = dir;
dir = join(dir, name);
let made = false;
try { mkdirSync(dir, { mode: 0o700 }); made = true; }
catch (e) { if (e.code !== 'EEXIST') fail(); }
const st = lstatSync(dir);
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o7777) !== 0o700) fail();
if (made) {
const fd = openSync(parent, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
try { fsyncSync(fd); } finally { closeSync(fd); }
}
}
return dir;
}
function read(file) {
let fd;
try {
fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
const st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o7777) !== 0o600 || st.size > 16384) fail();
const buffer = Buffer.alloc(16385), n = readSync(fd, buffer, 0, buffer.length, 0);
if (n > 16384) fail();
return JSON.parse(new TextDecoder('utf8', { fatal: true }).decode(buffer.subarray(0, n)));
} catch { fail(); }
finally { if (fd !== undefined) closeSync(fd); }
}
function write(file, doc, once = false) {
let fd, dirfd;
const temp = join(dirname(file), `.receipt-${randomUUID()}`);
try {
fd = openSync(temp, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
writeFileSync(fd, JSON.stringify(doc) + '\n'); fsyncSync(fd); closeSync(fd); fd = undefined;
if (once) linkSync(temp, file);
else renameSync(temp, file);
dirfd = openSync(dirname(file), constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
fsyncSync(dirfd);
return doc;
} catch (e) {
if (once && e.code === 'EEXIST') return read(file);
fail();
} finally {
if (fd !== undefined) closeSync(fd);
if (dirfd !== undefined) closeSync(dirfd);
try { unlinkSync(temp); } catch (e) { if (e.code !== 'ENOENT') fail(); }
}
}
const fields = ['version', 'id', 'root', 'kind', 'business', 'run', 'generation', 'phase', 'supervisor',
'pid', 'startTime', 'boot', 'group', 'runnerPid', 'runnerStartTime', 'containerName', 'containerId', 'haltGeneration', 'members'];
function validate(r) {
if (!r || typeof r !== 'object' || Array.isArray(r) || Object.keys(r).some(k => !fields.includes(k)) ||
r.version !== 1 || !UUID.test(r.id) || !/^[0-9a-f]{64}$/.test(r.root) ||
!['session', 'task', 'container', 'discord', 'effect'].includes(r.kind) ||
!UUID.test(r.generation) || !['starting', 'running', 'ended'].includes(r.phase) ||
typeof r.business !== 'string' || !ID.test(r.business) || typeof r.run !== 'string' || !ID.test(r.run) ||
!r.supervisor || !Number.isSafeInteger(r.supervisor.pid) || !UUID.test(r.supervisor.boot)) fail();
if (r.pid !== undefined && (!Number.isSafeInteger(r.pid) || r.pid < 2 ||
typeof r.startTime !== 'string' || !/^\d+$/.test(r.startTime) || !UUID.test(r.boot))) fail();
if (r.group !== undefined && typeof r.group !== 'boolean') fail();
if (r.runnerPid !== undefined && (!Number.isSafeInteger(r.runnerPid) || r.runnerPid < 2 ||
typeof r.runnerStartTime !== 'string' || !/^\d+$/.test(r.runnerStartTime))) fail();
if (r.haltGeneration !== undefined && !UUID.test(r.haltGeneration)) fail();
if (r.members !== undefined && (!r.group || !Array.isArray(r.members) || r.members.length > 64 ||
r.members.some(m => !m || Object.keys(m).sort().join(',') !== 'boot,pid,startTime' ||
!Number.isSafeInteger(m.pid) || m.pid < 2 || typeof m.startTime !== 'string' ||
!/^\d+$/.test(m.startTime) || m.boot !== r.boot))) fail();
if (r.containerName !== undefined && !/^mosaic-[a-f0-9]{12}-[a-f0-9-]{36}$/.test(r.containerName)) fail();
if (r.containerId !== undefined && !/^[a-f0-9]{64}$/.test(r.containerId)) fail();
return r;
}
export function registerRuntime(root, record) {
const admission = assertProcessing(root);
const r = validate({ ...record, version: 1, id: randomUUID(), root: rootTag(root),
generation: admission.generation, phase: 'starting', supervisor: processIdentity(process.pid) });
const file = join(directory(root, 'runtimes'), r.id + '.json');
write(file, r, true);
try { assertProcessing(root, admission.generation); }
catch (e) { unlinkSync(file); throw e; }
return r;
}
export function runtime(root, id) {
if (!UUID.test(id)) fail();
const r = validate(read(join(directory(root, 'runtimes'), id + '.json')));
if (r.id !== id || r.root !== rootTag(root)) fail();
return r;
}
export function updateRuntime(root, id, update) {
if (Object.keys(update).some(k => !['pid', 'startTime', 'boot', 'group', 'runnerPid', 'runnerStartTime',
'containerId', 'phase', 'haltGeneration', 'members'].includes(k))) fail();
const r = validate({ ...runtime(root, id), ...update });
return write(join(directory(root, 'runtimes'), id + '.json'), r);
}
export function unregisterRuntime(root, id) {
runtime(root, id);
unlinkSync(join(directory(root, 'runtimes'), id + '.json'));
}
export function listRuntimes(root) {
return readdirSync(directory(root, 'runtimes')).filter(n => !n.startsWith('.receipt-')).map(name => {
if (!name.endsWith('.json') || !UUID.test(name.slice(0, -5))) fail();
return runtime(root, name.slice(0, -5));
});
}
export function readStops(root, generation) {
if (!UUID.test(generation)) fail();
return readdirSync(directory(root, 'stops', generation)).filter(name => !name.startsWith('.receipt-')).map(name => read(join(directory(root, 'stops', generation), name)))
.sort((a, b) => a.at.localeCompare(b.at) || a.id.localeCompare(b.id));
}
function stopRecord(root, generation, r, outcome, signals, problem = null, receipt = null) {
const doc = { version: 1, id: randomUUID(), generation, runtime: r.id, kind: r.kind,
business: r.business, run: r.run, ...(receipt ? { stop: receipt.id, reason: receipt.reason, owner: receipt.owner } : {}), at: new Date().toISOString(), outcome, signals, ...(problem ? { problem } : {}),
identity: r.pid ? { pid: r.pid, startTime: r.startTime, boot: r.boot } : null };
return write(join(directory(root, 'stops', generation), `${r.id}.${outcome}.json`), doc, true);
}
function groupOf(pid) {
try {
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
return Number(stat.slice(stat.lastIndexOf(')') + 2).split(' ')[2]);
} catch { return null; }
}
function groupMembers(pgid) {
return readdirSync('/proc').filter(n => /^\d+$/.test(n)).map(Number)
.filter(pid => groupOf(pid) === pgid).map(processIdentity).filter(Boolean);
}
function signal(r, sig, members) {
if (sig === 'SIGTERM' && r.kind === 'session' && r.runnerPid !== undefined) {
const runner = { pid: r.runnerPid, startTime: r.runnerStartTime, boot: r.boot };
if (alive(r) !== true || alive(runner) !== true || groupOf(runner.pid) !== r.pid) return false;
try { process.kill(runner.pid, sig); return true; } catch { return false; }
}
// A surviving, start-time-checked member anchors the original group even after its leader exits.
// Never signal a recycled group merely because its numeric id matches.
const verified = r.group
? members.some(m => alive(m) === true && groupOf(m.pid) === r.pid)
: alive(r) === true;
if (!verified) return false;
try { process.kill(r.group ? -r.pid : r.pid, sig); return true; } catch { return false; }
}
export function createRuntimeSignaller(record, onMembers = () => {}) {
let members = record.group && alive(record) === true && groupOf(record.pid) === record.pid
? groupMembers(record.pid) : record.members ?? [];
return sig => {
if (record.group && members.some(m => alive(m) === true && groupOf(m.pid) === record.pid)) {
const found = groupMembers(record.pid);
members = [...members, ...found.filter(m => !members.some(old => old.pid === m.pid && old.startTime === m.startTime))];
}
try { if (record.group) onMembers(members.slice(0, 64)); } catch { return false; }
return signal(record, sig, members);
};
}
export function runtimeGroupState(record) {
if (!record.group) return alive(record);
if (alive(record) === true && groupOf(record.pid) === record.pid) return true;
if ((record.members ?? []).some(m => alive(m) === true && groupOf(m.pid) === record.pid)) return true;
// An unknown survivor may be ours or a recycled group. Neither permits a signal or quiescence.
return groupMembers(record.pid).length ? null : false;
}
// One first-owner stop operation per canonical root/business/run. Never async-wrap the
// exported function: callers joining an in-flight stop must receive the identical Promise.
const stopping = new Map();
const STOP_GRACE_MS = 5000, STOP_OBSERVE_MS = 2000;
function stopKey(root, business, run) {
const hex = createHash('sha256').update(JSON.stringify([rootTag(root), business, run])).digest('hex');
return `${hex.slice(0,8)}-${hex.slice(8,12)}-${hex.slice(12,16)}-${hex.slice(16,20)}-${hex.slice(20,32)}`;
}
function stopPaths(root, key) {
const dir = directory(root, 'run-stops');
return { claim: join(dir, key + '.claim.json'), result: join(dir, key + '.result.json') };
}
function optionalRead(file) {
try { lstatSync(file); } catch (e) { if (e.code === 'ENOENT') return null; throw e; }
return read(file);
}
function stopResult(doc, expected) {
const fields = ['version','id','root','business','run','reason','owner','at','runtime','kind','outcome','signals','identity'];
if (!doc || typeof doc !== 'object' || Array.isArray(doc) ||
Object.keys(doc).length !== fields.length || Object.keys(doc).some(k => !fields.includes(k)) ||
doc.version !== 1 || doc.business !== expected.business || doc.run !== expected.run ||
doc.root !== expected.root || !UUID.test(doc.id) || !['fire','kill-switch'].includes(doc.reason) ||
typeof doc.at !== 'string' || !Number.isFinite(Date.parse(doc.at)) || new Date(doc.at).toISOString() !== doc.at ||
(doc.runtime !== null && !UUID.test(doc.runtime)) ||
(doc.kind !== null && !['session','task','container','discord','effect'].includes(doc.kind)) ||
!['stopped','killed','alive','identity-changed','unknown'].includes(doc.outcome) ||
!Array.isArray(doc.signals) || doc.signals.length > 2 ||
doc.signals.some(s => !['SIGTERM','SIGKILL'].includes(s)) || new Set(doc.signals).size !== doc.signals.length ||
(doc.signals.length === 2 && doc.signals.join(',') !== 'SIGTERM,SIGKILL') ||
!doc.owner || typeof doc.owner !== 'object' || Array.isArray(doc.owner)) fail();
if (doc.reason === 'fire' ? Object.keys(doc.owner).join(',') !== 'decision' || typeof doc.owner.decision !== 'string' || !ID.test(doc.owner.decision) :
Object.keys(doc.owner).join(',') !== 'generation' || !UUID.test(doc.owner.generation)) fail();
if (doc.identity !== null && (!doc.identity || Object.keys(doc.identity).sort().join(',') !== 'boot,pid,startTime' ||
!Number.isSafeInteger(doc.identity.pid) || doc.identity.pid < 2 ||
typeof doc.identity.startTime !== 'string' || !/^\d+$/.test(doc.identity.startTime) || !UUID.test(doc.identity.boot))) fail();
return { run: doc.run, outcome: doc.outcome, pid: doc.identity?.pid ?? null,
signals: doc.signals, receipt: doc };
}
export function stopRun(args, options = {}) {
try {
if (!args || Object.keys(args).sort().join(',') !== 'business,owner,reason,root,run' ||
typeof args.root !== 'string' || typeof args.business !== 'string' || !ID.test(args.business) ||
typeof args.run !== 'string' || !ID.test(args.run) ||
!args.owner || typeof args.owner !== 'object' || Array.isArray(args.owner)) fail();
if (args.reason === 'fire') {
if (Object.keys(args.owner).join(',') !== 'decision' || typeof args.owner.decision !== 'string' || !ID.test(args.owner.decision)) fail();
} else if (args.reason === 'kill-switch') {
if (Object.keys(args.owner).join(',') !== 'generation' || !UUID.test(args.owner.generation)) fail();
const state = readControl(args.root);
if (!state.halted || state.generation !== args.owner.generation) throw new ProcessingError('processing-generation-changed');
} else fail();
const root = rootTag(args.root), key = stopKey(args.root, args.business, args.run);
const local = root + ':' + key;
if (stopping.has(local)) return stopping.get(local);
const pending = Promise.resolve().then(() => ownStop({ ...args, rootHash: root }, key, options)).catch(e => {
if (!e.code?.startsWith('processing-')) throw e;
return { run: args.run, outcome: 'unknown', pid: null, signals: [], receipt: null };
});
stopping.set(local, pending);
pending.then(() => stopping.delete(local), () => stopping.delete(local));
return pending;
} catch (e) { return Promise.reject(e); }
}
async function ownStop(args, key, options) {
const { root, rootHash, business, run, reason, owner } = args;
const files = stopPaths(root, key), expected = { root: rootHash, business, run };
const old = optionalRead(files.result);
if (old) return stopResult(old, expected);
const now = options.now ?? Date.now, sleep = options.sleep ?? wait;
const grace = options.graceMs ?? STOP_GRACE_MS, observe = options.killWaitMs ?? STOP_OBSERVE_MS;
const started = now();
const claim = { version: 1, id: randomUUID(), ...expected, reason, owner,
supervisor: processIdentity(process.pid), at: new Date().toISOString(), deadline: Date.now() + grace + observe };
const winner = write(files.claim, claim, true);
if (winner.root !== rootHash || winner.business !== business || winner.run !== run || !UUID.test(winner.id) ||
!Number.isFinite(winner.deadline) || winner.deadline > Date.now() + STOP_GRACE_MS + STOP_OBSERVE_MS + 1000) fail();
if (winner.id !== claim.id) {
// Another host owns delivery. No lease stealing or fresh deadlines on owner loss.
while (Date.now() <= winner.deadline) {
const result = optionalRead(files.result);
if (result) return stopResult(result, expected);
if (alive(winner.supervisor) !== true) break;
await sleep(20);
}
return { run, outcome: 'unknown', pid: null, signals: [], receipt: null };
}
let record = null, signals = [], initialIdentity = null;
const finish = outcome => {
const doc = { version: 1, id: claim.id, ...expected, reason, owner, at: new Date().toISOString(),
runtime: record?.id ?? null, kind: record?.kind ?? null, outcome, signals: [...signals],
identity: initialIdentity };
return stopResult(write(files.result, doc, true), expected);
};
const matches = listRuntimes(root).filter(r => r.business === business && r.run === run);
if (matches.length !== 1) return finish('unknown');
record = matches[0];
if (reason === 'kill-switch') record = updateRuntime(root, record.id, { haltGeneration: owner.generation });
if (record.pid) initialIdentity = { pid: record.pid, startTime: record.startTime, boot: record.boot };
if (record.containerName) {
let containerStop = options.containerStop;
if (!containerStop) {
const { createContainerStopper } = await import('./containers.mjs');
containerStop = createContainerStopper({ dataRoot: root });
}
const result = await containerStop(record, { graceMs: grace, killWaitMs: observe });
signals = result.signals;
return finish(result.outcome === 'killed' ? 'killed' :
['terminated','already-exited'].includes(result.outcome) ? 'stopped' : 'unknown');
}
const identity = options.identity ?? processIdentity;
let members = record.group && alive(record) === true && groupOf(record.pid) === record.pid
? groupMembers(record.pid) : record.members ?? [];
const status = r => {
if (!r.pid) return r.phase === 'ended' ? false : null;
const current = identity(r.pid);
if (current && (current.pid !== r.pid || current.startTime !== r.startTime || current.boot !== r.boot))
return 'identity-changed';
if (initialIdentity && (initialIdentity.pid !== r.pid || initialIdentity.startTime !== r.startTime || initialIdentity.boot !== r.boot))
return 'identity-changed';
if (options.state) {
const observed = options.state(r);
return observed === true && !current && !r.group ? null : observed;
}
return runtimeGroupState({ ...r, members });
};
const deliver = (r, sig) => {
// Re-check immediately before the actual signal, including fixture delivery.
if (status(r) !== true) return false;
if (r.group) updateRuntime(root, r.id, { members: members.slice(0,64) });
return options.signal ? options.signal(r, sig) : signal(r, sig, members);
};
const termEnd = started + grace, deadline = termEnd + observe;
let termTried = false, killTried = false;
while (now() <= deadline) {
try { record = runtime(root, record.id); }
catch {
const present = readdirSync(directory(root, 'runtimes')).includes(record.id + '.json');
return finish(present ? 'unknown' : signals.includes('SIGKILL') ? 'killed' : 'stopped');
}
if (!initialIdentity && record.pid) initialIdentity = { pid: record.pid, startTime: record.startTime, boot: record.boot };
if (record.group && members.some(m => alive(m) === true && groupOf(m.pid) === record.pid)) {
const found = groupMembers(record.pid);
members = [...members, ...found.filter(m => !members.some(old => old.pid === m.pid && old.startTime === m.startTime))];
}
let live = status(record);
if (live === 'identity-changed') return finish('identity-changed');
if ((record.phase === 'ended' && !record.group) || live === false)
return finish(signals.includes('SIGKILL') ? 'killed' : 'stopped');
if (record.pid && live === true && !termTried) {
termTried = true;
if (deliver(record, 'SIGTERM')) signals.push('SIGTERM');
}
if (record.pid && live === true && now() >= termEnd && !killTried) {
killTried = true;
if (deliver(record, 'SIGKILL')) signals.push('SIGKILL');
}
if (now() >= deadline) {
live = status(record);
return finish(live === 'identity-changed' ? 'identity-changed' : live === true ? 'alive' :
live === false ? signals.includes('SIGKILL') ? 'killed' : 'stopped' : 'unknown');
}
await sleep(Math.min(20, Math.max(1, deadline - now())));
}
return finish('unknown');
}
// A later exit is new evidence, never a replacement for a fire caller's first result.
function laterExit(root, first, receipt, generation) {
try {
const current = runtime(root, first.id);
if (current.pid) {
const identity = processIdentity(current.pid);
if (identity && (identity.startTime !== current.startTime || identity.boot !== current.boot)) return false;
if (runtimeGroupState(current) !== false) return false;
} else if (current.phase !== 'ended') return false;
const key = stopKey(root, first.business, first.run);
write(join(directory(root, 'run-stops'), key + '.' + generation + '.observation.json'), {
version: 1, id: randomUUID(), stop: receipt.id, generation, runtime: first.id,
root: rootTag(root), business: first.business, run: first.run, outcome: 'exited', at: new Date().toISOString(),
}, true);
return true;
} catch { return false; }
}
export async function sweepRuntimes(root, generation, { graceMs = 5000, killWaitMs = 2000, containerStop = null,
beforeStop = () => {}, stopOptions = {} } = {}) {
const state = readControl(root);
if (!state.halted || state.generation !== generation) throw new ProcessingError('processing-generation-changed');
const prior = readStops(root, generation);
const results = await Promise.all(listRuntimes(root).map(async first => {
const complete = prior.find(s => s.runtime === first.id && s.outcome !== 'unknown');
if (complete) return complete;
let problem = null;
try { await beforeStop(first, generation); } catch { problem = 'brake-unconfirmed'; }
const result = await stopRun({ root, business: first.business, run: first.run,
reason: 'kill-switch', owner: { generation } }, { graceMs, killWaitMs, containerStop, ...stopOptions });
let outcome = result.outcome === 'stopped' ? 'terminated' : result.outcome === 'killed' ? 'killed' : 'unknown';
if (outcome === 'unknown' && result.receipt && !problem && laterExit(root, first, result.receipt, generation))
outcome = 'terminated';
if (problem) outcome = 'unknown';
return stopRecord(root, generation, first, outcome, result.signals, problem, result.receipt);
}));
const remaining = results.filter(r => r.outcome === 'unknown').length;
return { quiescent: remaining === 0, remaining, stopped: results.length - remaining, results };
}
-129
View File
@@ -1,129 +0,0 @@
// Host-owned, per-data-root admission state. No model, git or credential imports.
import { randomUUID } from 'node:crypto';
import { constants, closeSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readSync, realpathSync,
renameSync, unlinkSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
export class ProcessingError extends Error {
constructor(code) { super(code); this.name = 'ProcessingError'; this.code = code; }
}
const fail = code => { throw new ProcessingError(code); };
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
const ID = /^[a-zA-Z0-9][a-zA-Z0-9_.:@/-]{0,159}$/;
const identity = value => typeof value === 'string' && ID.test(value);
const FIELDS = ['version', 'generation', 'operation', 'halted', 'at', 'business', 'human', 'recovered'];
const MAX_BYTES = 8192;
export function controlDirectory(dataRoot) {
try {
const root = realpathSync(dataRoot), st = lstatSync(root);
if (!st.isDirectory() || st.uid !== process.getuid()) fail('processing-control-unsafe');
return join(root, 'control');
} catch { fail('processing-control-unsafe'); }
}
function parent(dataRoot) {
const dir = controlDirectory(dataRoot);
let st;
try { st = lstatSync(dir); }
catch (e) {
if (e.code === 'ENOENT') fail('processing-uninitialized');
fail('processing-control-unsafe');
}
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid() || (st.mode & 0o7777) !== 0o700)
fail('processing-control-unsafe');
return dir;
}
function valid(doc) {
if (!doc || typeof doc !== 'object' || Array.isArray(doc) ||
Object.keys(doc).length !== FIELDS.length || Object.keys(doc).some(k => !FIELDS.includes(k)) ||
doc.version !== 1 || !UUID.test(doc.generation) || typeof doc.halted !== 'boolean' ||
typeof doc.recovered !== 'boolean' || typeof doc.at !== 'string' ||
!Number.isFinite(Date.parse(doc.at)) || new Date(doc.at).toISOString() !== doc.at)
fail('processing-state-invalid');
if (doc.operation === null) {
if (doc.halted || doc.business !== null || doc.human !== null || doc.recovered) fail('processing-state-invalid');
} else if (!UUID.test(doc.operation) || !identity(doc.business) || !identity(doc.human)) fail('processing-state-invalid');
return Object.freeze(doc);
}
export function readControl(dataRoot) {
const file = join(parent(dataRoot), 'processing.json');
let fd;
try {
fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
const st = fstatSync(fd);
if (!st.isFile() || st.uid !== process.getuid() || (st.mode & 0o7777) !== 0o600 || st.size > MAX_BYTES)
fail('processing-state-invalid');
const buf = Buffer.alloc(MAX_BYTES + 1);
let size = 0, n;
while ((n = readSync(fd, buf, size, buf.length - size, null)) > 0) {
size += n;
if (size > MAX_BYTES) fail('processing-state-invalid');
}
return valid(JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(buf.subarray(0, size))));
} catch { fail('processing-state-invalid'); }
finally { if (fd !== undefined) closeSync(fd); }
}
function publish(dataRoot, doc) {
const dir = parent(dataRoot), file = join(dir, 'processing.json'), temp = join(dir, `.state-${randomUUID()}`);
let fd, dirfd;
try {
fd = openSync(temp, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
writeFileSync(fd, JSON.stringify(valid(doc)) + '\n');
fsyncSync(fd); closeSync(fd); fd = undefined;
renameSync(temp, file);
dirfd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
fsyncSync(dirfd);
} catch { fail('processing-state-invalid'); }
finally {
if (fd !== undefined) closeSync(fd);
if (dirfd !== undefined) closeSync(dirfd);
try { unlinkSync(temp); } catch (e) { if (e.code !== 'ENOENT') fail('processing-state-invalid'); }
}
}
// First-use host bootstrap only. A present directory with no file is not a new installation.
export function initializeControl(dataRoot) {
const dir = controlDirectory(dataRoot);
try { mkdirSync(dir, { mode: 0o700 }); }
catch (e) {
if (e.code === 'EEXIST') return readControl(dataRoot);
fail('processing-control-unsafe');
}
const rootfd = openSync(realpathSync(dataRoot), constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
try { fsyncSync(rootfd); } finally { closeSync(rootfd); }
publish(dataRoot, { version: 1, generation: randomUUID(), operation: null, halted: false,
at: new Date().toISOString(), business: null, human: null, recovered: false });
return readControl(dataRoot);
}
export function assertProcessing(dataRoot, generation = null) {
const state = readControl(dataRoot);
if (state.halted) fail('processing-halted');
if (generation !== null && state.generation !== generation) fail('processing-generation-changed');
return state;
}
// Called only by the coordinator after the broker has proved a human.
export function transition(dataRoot, { halted, business, human, generation = null }) {
if (typeof halted !== 'boolean' || !identity(business) || !identity(human)) fail('invalid-request');
parent(dataRoot);
let current, recovered = false;
try { current = readControl(dataRoot); }
catch (e) {
if (!halted || e.code !== 'processing-state-invalid') throw e;
recovered = true;
}
if (!halted && generation !== current.generation) fail('processing-generation-changed');
if (current?.halted === halted) return { ...current, changed: false };
const operation = randomUUID();
const state = { version: 1, generation: operation, operation, halted,
at: new Date().toISOString(), business, human, recovered };
publish(dataRoot, state);
return { ...state, changed: true };
}
export function processingStatus(dataRoot) {
try { return readControl(dataRoot); }
catch (e) {
if (!(e instanceof ProcessingError)) throw e;
return { halted: e.code === 'processing-uninitialized' ? null : true, invalid: true, code: e.code };
}
}
@@ -1,85 +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 { initializeControl, transition } from '../src/state.mjs';
const api = await import('../src/containers.mjs').catch(e => { if (e.code === 'ERR_MODULE_NOT_FOUND') return {}; throw e; });
function fixture(t) { const root = mkdtempSync(join(tmpdir(), 'processing-container-')); initializeControl(root); t.after(() => rmSync(root, { recursive: true, force: true })); return root; }
const actor = { business: 'demo', human: 'jason', halted: true };
test('container admission publishes its generated name and root/runtime labels before dispatch', t => {
assert.equal(typeof api.prepareContainer, 'function');
const root = fixture(t), r = api.prepareContainer(root, { kind: 'task', business: 'instance', run: 'run-1' });
const args = api.containerArgs(r, { args: ['request'] });
assert.match(r.containerName, /^mosaic-[a-f0-9]{12}-[a-f0-9-]{36}$/);
assert.ok(args.includes(r.containerName)); assert.ok(args.includes(`mosaic.root=${r.root}`));
assert.ok(args.includes(`mosaic.runtime=${r.id}`)); assert.ok(args.includes(`MOSAIC_PROCESSING_GENERATION=${r.generation}`));
assert.deepEqual(args.slice(-2), ['mosaic-agent', 'request']);
transition(root, actor); assert.throws(() => api.prepareContainer(root, { kind: 'task', business: 'instance', run: 'new' }), e => e.code === 'processing-halted');
});
function inspected(r, running = true, id = 'a'.repeat(64)) {
return { Id: id, Name: '/' + r.containerName, Config: { Labels: { 'mosaic.root': r.root, 'mosaic.runtime': r.id } }, State: { Running: running } };
}
test('container sweep sends TERM then KILL only after verifying exact id and both labels', async t => {
assert.equal(typeof api.createContainerStopper, 'function');
const root = fixture(t), r = api.prepareContainer(root, { kind: 'task', business: 'instance', run: 'one' });
const calls = []; let running = true;
const stop = api.createContainerStopper({ docker: async args => {
calls.push(args);
if (args[0] === 'inspect') return JSON.stringify(inspected(r, running));
if (args.includes('KILL')) running = false;
return '';
} });
const result = await stop(r, { graceMs: 30, killWaitMs: 100 });
assert.equal(result.outcome, 'killed'); assert.deepEqual(result.signals, ['SIGTERM', 'SIGKILL']);
assert.deepEqual(calls.filter(c => c[0] === 'kill'), [['kill', '--signal', 'TERM', 'a'.repeat(64)], ['kill', '--signal', 'KILL', 'a'.repeat(64)]]);
});
test('foreign labels and a replaced container id stay unknown and are never signalled', async t => {
assert.equal(typeof api.createContainerStopper, 'function');
const root = fixture(t), r = api.prepareContainer(root, { kind: 'task', business: 'instance', run: 'one' });
const calls = [];
const stop = api.createContainerStopper({ docker: async args => {
calls.push(args); const doc = inspected(r); doc.Config.Labels['mosaic.root'] = 'b'.repeat(64); return JSON.stringify(doc);
} });
assert.equal((await stop(r, { graceMs: 1, killWaitMs: 1 })).outcome, 'unknown');
assert.equal(calls.filter(c => c[0] === 'kill').length, 0);
let reads = 0; calls.length = 0;
const replacement = api.createContainerStopper({ docker: async args => {
calls.push(args); if (args[0] !== 'inspect') return '';
return JSON.stringify(inspected(r, true, (++reads > 1 ? 'b' : 'a').repeat(64)));
} });
assert.equal((await replacement(r, { graceMs: 1, killWaitMs: 1 })).outcome, 'unknown');
assert.equal(calls.filter(c => c[0] === 'kill').length, 0);
});
test('an unverified missing container remains unknown rather than claiming a pending start ended', async t => {
assert.equal(typeof api.createContainerStopper, 'function');
const root = fixture(t), r = api.prepareContainer(root, { kind: 'container', business: 'instance', run: 'one' });
const stop = api.createContainerStopper({ docker: async () => { throw new Error('no container'); } });
assert.equal((await stop(r, { graceMs: 5, killWaitMs: 5 })).outcome, 'unknown');
});
test('a removed --rm container is settled only by its trusted owner completion receipt', async t => {
const { updateRuntime } = await import('../src/runtimes.mjs');
const root = fixture(t), r = api.prepareContainer(root, { kind: 'task', business: 'instance', run: 'one' });
let removed = false;
const stop = api.createContainerStopper({ dataRoot: root, docker: async args => {
if (args[0] === 'kill') { removed = true; updateRuntime(root, r.id, { phase: 'ended' }); return ''; }
if (removed) throw Error('removed by --rm');
return JSON.stringify(inspected(r));
} });
const result = await stop(r, { graceMs: 30, killWaitMs: 100 });
assert.equal(result.outcome, 'terminated'); assert.deepEqual(result.signals, ['SIGTERM']);
});
test('after signalling a verified container, successful empty Docker listing proves --rm removed that id', async t => {
const root = fixture(t), r = api.prepareContainer(root, { kind: 'task', business: 'instance', run: 'one' });
let removed = false;
const stop = api.createContainerStopper({ docker: async args => {
if (args[0] === 'kill') { removed = true; return ''; }
if (args[0] === 'ps') return '';
if (removed) throw Error('removed');
return JSON.stringify(inspected(r));
} });
const result = await stop(r, { graceMs: 20, killWaitMs: 100 });
assert.equal(result.outcome, 'terminated'); assert.deepEqual(result.signals, ['SIGTERM']);
});
@@ -1,67 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { readControl, assertProcessing } from '../src/state.mjs';
import { registerRuntime, updateRuntime, processIdentity, unregisterRuntime } from '../src/runtimes.mjs';
const { createProcessing } = await import('../src/coordinator.mjs').catch(e => { if (e.code === 'ERR_MODULE_NOT_FOUND') return {}; throw e; });
const actor = { business: 'demo', human: 'jason' };
function fixture(t) {
assert.equal(typeof createProcessing, 'function');
const root = mkdtempSync(join(tmpdir(), 'processing-coordinator-'));
const c = createProcessing({ dataRoot: root, graceMs: 80, killWaitMs: 1000 });
t.after(async () => { await c.close(); rmSync(root, { recursive: true, force: true }); });
return { root, c };
}
test('halt publishes before awaiting stops, resume refuses pending, and only clear permits new work', async t => {
const { root, c } = fixture(t);
const p = spawn(process.execPath, ['-e', "process.on('SIGTERM',()=>{});console.log('ready');setInterval(()=>{},1000)"], { detached: true, stdio: ['ignore', 'pipe', 'ignore'] });
const exit = once(p, 'exit');
t.after(async () => { if (p.signalCode === null && p.exitCode === null) { try { process.kill(-p.pid, 'SIGKILL'); } catch {} await exit; } });
await once(p.stdout, 'data');
const r = registerRuntime(root, { business: 'demo', run: 'standin', kind: 'session' });
updateRuntime(root, r.id, { ...processIdentity(p.pid), group: true, phase: 'running' });
const halted = c.halt(actor);
assert.equal(readControl(root).halted, true, 'durable gate is set before any stop await');
await assert.rejects(c.resume({ ...actor, generation: readControl(root).generation }), { code: 'processing-stop-pending' });
const result = await halted;
assert.equal(result.quiescent, true);
assert.deepEqual(await exit, [null, 'SIGKILL']);
assert.throws(() => assertProcessing(root), { code: 'processing-halted' });
const resumed = await c.resume({ ...actor, generation: result.generation });
assert.equal(resumed.halted, false);
assert.doesNotThrow(() => assertProcessing(root));
});
test('a new coordinator replays a persisted halt and an unresolved start keeps clear refused', async t => {
const { root, c } = fixture(t);
const pending = registerRuntime(root, { business: 'demo', run: 'starting', kind: 'session' });
const result = await c.halt(actor);
assert.equal(result.quiescent, false);
await c.close();
const next = createProcessing({ dataRoot: root, graceMs: 10, killWaitMs: 10 });
try {
assert.equal(next.read().halted, true);
await assert.rejects(next.resume({ ...actor, generation: result.generation }), { code: 'processing-stop-pending' });
unregisterRuntime(root, pending.id);
const resumed = await next.resume({ ...actor, generation: result.generation });
assert.equal(resumed.halted, false);
} finally { await next.close(); }
});
test('a new halt generation never inherits a previous quiescent status before its sweep', async t => {
const { mkdtempSync, rmSync } = await import('node:fs');
const { tmpdir } = await import('node:os');
const { join } = await import('node:path');
const { transition } = await import('../src/state.mjs');
const root = mkdtempSync(join(tmpdir(), 'processing-counts-'));
const processing = createProcessing({ dataRoot: root });
t.after(async () => { await processing.close(); rmSync(root, { recursive: true, force: true }); });
transition(root, { business: 'demo', human: 'jason', halted: true });
assert.equal(processing.read().quiescent, false);
await processing.reconcile(); assert.equal(processing.read().quiescent, true);
});
@@ -1,74 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { initializeControl, transition } from '../src/state.mjs';
const api = await import('../src/effects.mjs').catch(e => { if (e.code === 'ERR_MODULE_NOT_FOUND') return {}; throw e; });
function fixture(t) { const root = mkdtempSync(join(tmpdir(), 'processing-effect-')); initializeControl(root); t.after(() => rmSync(root, { recursive: true, force: true })); return root; }
const actor = { business: 'demo', human: 'jason', halted: true };
test('halt aborts an active local request and prevents a subsequent effect after halt/resume', { timeout: 4000 }, async t => {
assert.equal(typeof api.withProcessing, 'function');
const root = fixture(t); let started;
const ready = new Promise(resolve => { started = resolve; });
const work = api.withProcessing(root, async scope => {
await new Promise(resolve => { scope.signal.addEventListener('abort', resolve, { once: true }); started(); });
scope.guard();
});
await ready; const halted = transition(root, actor);
transition(root, { ...actor, halted: false, generation: halted.generation });
await assert.rejects(work, e => e.code === 'processing-generation-changed');
});
test('processing scopes isolate roots and cancel only the registered child group', async t => {
assert.equal(typeof api.withProcessing, 'function');
const a = fixture(t), b = fixture(t);
let admitted;
const ready = new Promise(resolve => { admitted = resolve; });
const task = api.withProcessing(a, async scope => {
const child = spawn(process.execPath, ['-e', "process.on('SIGTERM',()=>process.exit(0));console.log('ready');setInterval(()=>{},1000)"], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
const exit = once(child, 'exit'); t.after(() => { try { process.kill(-child.pid, 'SIGKILL'); } catch {} });
scope.track(child); await once(child.stdout, 'data'); admitted();
const outcome = await exit; assert.deepEqual(outcome, [0, null]); scope.guard();
});
await ready; transition(b, actor);
await api.withProcessing(a, async scope => scope.guard());
transition(a, actor);
await assert.rejects(task, e => e.code === 'processing-halted');
});
test('halted scopes never invoke their body and unscoped human work has no processing context', async t => {
assert.equal(typeof api.withProcessing, 'function');
const root = fixture(t); transition(root, actor); let called = false;
await assert.rejects(api.withProcessing(root, async () => { called = true; }), e => e.code === 'processing-halted');
assert.equal(called, false); assert.equal(api.currentProcessing(), undefined);
});
test('host git is stopped promptly by the active scope rather than waiting for its long deadline', async t => {
const { mkdirSync, writeFileSync } = await import('node:fs');
const { runGit, gitEnv } = await import('../../seat/src/checkout.mjs');
const root = fixture(t), bin = join(root, 'bin'); mkdirSync(bin);
writeFileSync(join(bin, 'git'), `#!${process.execPath}\nprocess.on('SIGTERM',()=>process.exit(0));setInterval(()=>{},1000);\n`, { mode: 0o755 });
const begin = Date.now();
const task = api.withProcessing(root, async () => runGit(['status'], { ...gitEnv(), PATH: bin + ':' + process.env.PATH }, { timeoutMs: 10000, graceMs: 100 }));
await new Promise(resolve => setTimeout(resolve, 100)); transition(root, actor);
await assert.rejects(task, e => e.code === 'processing-halted');
assert.ok(Date.now() - begin < 2000, 'the halt cancels the actual git process');
});
test('a child leader exiting does not erase an unverified surviving group receipt', async t => {
const { listRuntimes } = await import('../src/runtimes.mjs');
const root = fixture(t);
let child;
await api.withProcessing(root, async scope => {
const script = `const {spawn}=require('node:child_process');spawn(process.execPath,['-e','setInterval(()=>{},1000)'],{stdio:'ignore'});console.log('ready');setTimeout(()=>process.exit(0),80);`;
child = spawn(process.execPath, ['-e', script], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { try { process.kill(-child.pid, 'SIGKILL'); } catch {} });
const closed = once(child, 'close');
scope.track(child); await once(child.stdout, 'data'); await closed;
});
const records = listRuntimes(root);
assert.equal(records.length, 1, 'unknown group remains discoverable to the independent halt sweep');
assert.equal(records[0].pid, child.pid);
assert.equal(records[0].group, true);
});
@@ -1,36 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { initializeControl, transition } from '../src/state.mjs';
import { sandboxProblem } from '../../seat/src/sandbox.mjs';
const skip = sandboxProblem() ?? false;
test('the actual nested read-only control mount refuses unlink, replacement and parent removal', { skip }, t => {
const root = mkdtempSync(join(tmpdir(), 'processing-mount-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
initializeControl(root); transition(root, { business: 'demo', human: 'jason', halted: true });
const file = join(root, 'control', 'processing.json'), bytes = readFileSync(file), inode = statSync(file).ino;
writeFileSync(join(root, 'replacement'), 'off replacement');
const r = spawnSync('bwrap', ['--unshare-user', '--unshare-pid', '--die-with-parent',
'--ro-bind', '/usr', '/usr', '--symlink', 'usr/bin', '/bin', '--symlink', 'usr/lib', '/lib',
'--symlink', 'usr/lib64', '/lib64', '--proc', '/proc', '--dev', '/dev',
'--bind', root, '/fixture', '--ro-bind', join(root, 'control'), '/fixture/control',
'/bin/sh', '-c', 'rm /fixture/control/processing.json && exit 10; echo unlink-refused; mv /fixture/replacement /fixture/control/processing.json && exit 11; echo replace-refused; rm -rf /fixture/control && exit 12; echo parent-refused'],
{ encoding: 'utf8', timeout: 5000 });
assert.equal(r.status, 0, r.stderr);
assert.match(r.stdout, /unlink-refused/); assert.match(r.stdout, /replace-refused/); assert.match(r.stdout, /parent-refused/);
assert.deepEqual(readFileSync(file), bytes); assert.equal(statSync(file).ino, inode);
});
test('the image entrypoint refuses halted and stale generations using the shared reader', t => {
const root = mkdtempSync(join(tmpdir(), 'processing-entry-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const initial = initializeControl(root), entry = new URL('../src/entrypoint.mjs', import.meta.url).pathname;
const run = generation => spawnSync(process.execPath, [entry, root], { env: { ...process.env, MOSAIC_PROCESSING_GENERATION: generation }, encoding: 'utf8' });
assert.equal(run(initial.generation).status, 0);
const halt = transition(root, { business: 'demo', human: 'jason', halted: true });
assert.equal(run(initial.generation).status, 3);
transition(root, { business: 'demo', human: 'jason', halted: false, generation: halt.generation });
const stale = run(initial.generation); assert.equal(stale.status, 3); assert.match(stale.stderr, /processing-generation-changed/);
});
@@ -1,183 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { spawn, spawnSync } from 'node:child_process';
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { once } from 'node:events';
import { initializeControl, transition } from '../src/state.mjs';
const api = await import('../src/runtimes.mjs').catch(e => { if (e.code === 'ERR_MODULE_NOT_FOUND') return {}; throw e; });
const actor = { business: 'demo', human: 'jason', halted: true };
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
function fixture(t) {
const root = mkdtempSync(join(tmpdir(), 'processing-runtime-'));
initializeControl(root); t.after(() => rmSync(root, { recursive: true, force: true }));
return root;
}
async function child(t, stubborn = false) {
const p = spawn(process.execPath, ['-e', `process.on('SIGTERM',()=>{${stubborn ? '' : 'process.exit(0)'}});console.log('ready');setInterval(()=>{},1000)`], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
const ended = once(p, 'exit');
t.after(async () => { if (p.exitCode === null && p.signalCode === null) { try { process.kill(-p.pid, 'SIGKILL'); } catch {} await ended; } });
await once(p.stdout, 'data');
return { p, ended };
}
function receipt(root, p) {
const r = api.registerRuntime(root, { kind: 'session', business: 'demo', run: 'run' });
return api.updateRuntime(root, r.id, { ...api.processIdentity(p.pid), phase: 'running', group: true });
}
// Without TERM, a cooperative child would survive until unnecessary escalation.
test('halt terminates a tracked stand-in and leaves honest write-once stop evidence', async t => {
assert.equal(typeof api.registerRuntime, 'function');
const root = fixture(t), { p, ended } = await child(t);
const r = receipt(root, p);
const evidence = join(root, 'evidence.json'); writeFileSync(evidence, 'immutable existing result');
const state = transition(root, actor);
const result = await api.sweepRuntimes(root, state.generation, { graceMs: 150, killWaitMs: 1000 });
assert.equal(result.quiescent, true);
assert.equal(result.remaining, 0);
assert.deepEqual(await ended, [0, null]);
const stops = api.readStops(root, state.generation);
assert.equal(stops.length, 1);
assert.equal(stops[0].runtime, r.id);
assert.equal(stops[0].outcome, 'terminated');
assert.deepEqual(stops[0].signals, ['SIGTERM']);
assert.equal(readFileSync(evidence, 'utf8'), 'immutable existing result');
const bytes = JSON.stringify(stops);
await api.sweepRuntimes(root, state.generation, { graceMs: 10, killWaitMs: 1000 });
assert.equal(JSON.stringify(api.readStops(root, state.generation)), bytes);
});
// Dropping the KILL escalation leaves this actual child alive and fails quiescence.
test('a TERM-resistant workload is killed after the bounded wait', async t => {
assert.equal(typeof api.registerRuntime, 'function');
const root = fixture(t), { p, ended } = await child(t, true);
receipt(root, p);
const state = transition(root, actor), begin = Date.now();
const result = await api.sweepRuntimes(root, state.generation, { graceMs: 80, killWaitMs: 1000 });
assert.equal(result.quiescent, true);
assert.deepEqual(await ended, [null, 'SIGKILL']);
assert.ok(Date.now() - begin >= 80);
assert.deepEqual(api.readStops(root, state.generation)[0].signals, ['SIGTERM', 'SIGKILL']);
});
// An unchecked stale start identity would terminate a different process.
test('wrong start ticks or boot identity cannot signal a live replacement', async t => {
assert.equal(typeof api.registerRuntime, 'function');
const root = fixture(t), { p } = await child(t);
const r = receipt(root, p);
api.updateRuntime(root, r.id, { startTime: '0' });
const state = transition(root, actor);
await api.sweepRuntimes(root, state.generation, { graceMs: 30, killWaitMs: 30 });
await delay(30);
assert.equal(p.exitCode, null); assert.equal(p.signalCode, null);
assert.equal(api.readStops(root, state.generation)[0].outcome, 'unknown');
});
test('a pending start cannot be reported quiescent and a second root is never signalled', async t => {
assert.equal(typeof api.registerRuntime, 'function');
const a = fixture(t), b = fixture(t), { p } = await child(t);
api.registerRuntime(a, { kind: 'session', business: 'demo', run: 'pending' });
receipt(b, p);
const state = transition(a, actor);
const result = await api.sweepRuntimes(a, state.generation, { graceMs: 30, killWaitMs: 30 });
assert.equal(result.quiescent, false);
assert.equal(result.remaining, 1);
assert.equal(p.exitCode, null); assert.equal(p.signalCode, null);
});
// A group leader can exit on TERM while its child ignores it. Quiescence is the whole group.
test('escalation still kills the tracked group after its leader exits', async t => {
const root = fixture(t);
const script = `const {spawn}=require('node:child_process');
const c=spawn(process.execPath,['-e',"process.on('SIGTERM',()=>{});console.log('ready');setInterval(()=>{},1000)"],{stdio:['ignore','pipe','inherit']});
c.stdout.once('data',()=>console.log(c.pid));process.on('SIGTERM',()=>process.exit(0));setInterval(()=>{},1000);`;
const p = spawn(process.execPath, ['-e', script], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { try { process.kill(-p.pid, 'SIGKILL'); } catch {} });
const [line] = await once(p.stdout, 'data');
const descendant = Number(String(line).trim());
const r = receipt(root, p), state = transition(root, actor);
const outcome = await api.sweepRuntimes(root, state.generation, { graceMs: 80, killWaitMs: 1000 });
assert.equal(outcome.quiescent, true);
assert.equal(api.processIdentity(descendant), null, 'stubborn descendant must not survive the group leader');
assert.equal(api.readStops(root, state.generation).find(s => s.runtime === r.id).outcome, 'killed');
});
// Session TERM goes to the runner, leaving the outer namespace for forced escalation.
test('session TERM targets its verified runner before forcing the outer namespace', async t => {
const root = fixture(t);
const script = `const {spawn}=require('node:child_process');
process.on('SIGTERM',()=>require('node:fs').writeFileSync(${JSON.stringify(join(root, 'outer-term'))},'unexpected'));
const c=spawn(process.execPath,['-e',"process.on('SIGTERM',()=>process.exit(0));console.log('ready');setInterval(()=>{},1000)"],{stdio:['ignore','pipe','inherit']});
c.stdout.once('data',()=>console.log(c.pid));c.once('exit',()=>process.exit(0));setInterval(()=>{},1000);`;
const p = spawn(process.execPath, ['-e', script], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { try { process.kill(-p.pid, 'SIGKILL'); } catch {} });
const [line] = await once(p.stdout, 'data'), runner = api.processIdentity(Number(String(line).trim()));
const r = receipt(root, p);
api.updateRuntime(root, r.id, { runnerPid: runner.pid, runnerStartTime: runner.startTime });
const state = transition(root, actor);
const result = await api.sweepRuntimes(root, state.generation, { graceMs: 300, killWaitMs: 1000 });
assert.equal(result.quiescent, true);
assert.deepEqual(api.readStops(root, state.generation)[0].signals, ['SIGTERM']);
assert.equal(api.runtime(root, r.id).haltGeneration, state.generation);
assert.equal(existsSync(join(root, 'outer-term')), false);
});
// Publishing a receipt is not durable if its newly-created containing directory can vanish.
test('new runtime and stop directories sync their containing directories', t => {
const root = fixture(t);
const script = `import fs from 'node:fs';import {syncBuiltinESMExports} from 'node:module';
const real=fs.fsyncSync,seen=[];fs.fsyncSync=fd=>{seen.push(fs.readlinkSync('/proc/self/fd/'+fd));return real(fd)};
syncBuiltinESMExports();const api=await import(process.argv[1]);
const {transition}=await import(process.argv[2]);
const r=api.registerRuntime(process.argv[3],{kind:'task',business:'demo',run:'gone'});
api.updateRuntime(process.argv[3],r.id,{phase:'ended'});
const s=transition(process.argv[3],{business:'demo',human:'jason',halted:true});
await api.sweepRuntimes(process.argv[3],s.generation);
console.log(JSON.stringify({seen,generation:s.generation}));`;
const child = spawnSync(process.execPath, ['--input-type=module', '-e', script,
new URL('../src/runtimes.mjs', import.meta.url).href, new URL('../src/state.mjs', import.meta.url).href, root], { encoding: 'utf8' });
assert.equal(child.status, 0, child.stderr);
const { seen, generation } = JSON.parse(child.stdout);
assert.ok(seen.filter(p => p === join(root, 'control')).length >= 3, 'runtime mkdir, halt rename and stops mkdir sync control');
assert.ok(seen.includes(join(root, 'control', 'stops')), 'generation directory creation syncs stops');
assert.ok(seen.includes(join(root, 'control', 'stops', generation)), 'stop receipt syncs generation directory');
});
test('a vanished group leader with unverified surviving members stays unknown, never quiescent', async t => {
const root = fixture(t);
const script = `const {spawn}=require('node:child_process');const c=spawn(process.execPath,['-e',"console.log('ready');setInterval(()=>{},1000)"],{stdio:['ignore','pipe','inherit']});c.stdout.once('data',()=>console.log(c.pid));process.on('SIGTERM',()=>process.exit(0));setInterval(()=>{},1000)`;
const p = spawn(process.execPath, ['-e', script], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { try { process.kill(-p.pid, 'SIGKILL'); } catch {} });
const [line] = await once(p.stdout, 'data'); const descendant = Number(String(line).trim());
receipt(root, p); const exited = once(p, 'exit'); p.kill('SIGTERM'); await exited;
const state = transition(root, actor);
const result = await api.sweepRuntimes(root, state.generation, { graceMs: 20, killWaitMs: 30 });
assert.equal(result.quiescent, false); assert.equal(result.remaining, 1);
assert.notEqual(api.processIdentity(descendant), null, 'never signal a group with no verified surviving anchor');
});
test('persisted member identity anchors a group after the leader exited before the sweep', async t => {
const root = fixture(t);
const script = `const {spawn}=require('node:child_process');const c=spawn(process.execPath,['-e',"process.on('SIGTERM',()=>{});console.log('ready');setInterval(()=>{},1000)"],{stdio:['ignore','pipe','inherit']});c.stdout.once('data',()=>console.log(c.pid));process.on('SIGTERM',()=>process.exit(0));setInterval(()=>{},1000)`;
const p = spawn(process.execPath, ['-e', script], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { try { process.kill(-p.pid, 'SIGKILL'); } catch {} });
const [line] = await once(p.stdout, 'data'), descendant = Number(String(line).trim()), r = receipt(root, p);
api.updateRuntime(root, r.id, { members: [api.processIdentity(descendant)] });
const exited = once(p, 'exit'); p.kill('SIGTERM'); await exited;
const state = transition(root, actor);
const result = await api.sweepRuntimes(root, state.generation, { graceMs: 30, killWaitMs: 1000 });
assert.equal(result.quiescent, true); assert.equal(api.processIdentity(descendant), null);
assert.deepEqual(api.readStops(root, state.generation)[0].signals, ['SIGTERM', 'SIGKILL']);
});
test('an unconfirmed secondary brake does not leave a verified workload running or claim success', async t => {
const root = fixture(t), { p, ended } = await child(t);
receipt(root, p); const state = transition(root, actor);
const result = await api.sweepRuntimes(root, state.generation, { graceMs: 60, killWaitMs: 1000,
beforeStop: () => { throw Error('fixture journal unavailable'); } });
await ended;
assert.equal(api.processIdentity(p.pid), null);
assert.equal(result.quiescent, false); assert.equal(result.remaining, 1);
const stops = api.readStops(root, state.generation); assert.equal(stops[0].outcome, 'unknown');
assert.equal(stops[0].problem, 'brake-unconfirmed');
});
@@ -1,165 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
const api = await import('../src/state.mjs').catch(e => {
if (e.code === 'ERR_MODULE_NOT_FOUND') return {};
throw e;
});
const root = t => {
const dir = mkdtempSync(join(tmpdir(), 'processing-state-'));
t.after(() => rmSync(dir, { recursive: true, force: true }));
return dir;
};
const actor = { business: 'demo', human: 'jason' };
const stateFile = dir => join(dir, 'control', 'processing.json');
const refusal = code => e => e.code === code;
// Removing durable publication must let a new reader launch after halt.
test('halt persists across readers and resume needs the current generation', t => {
assert.equal(typeof api.initializeControl, 'function');
const dir = root(t);
const initial = api.initializeControl(dir);
assert.equal(initial.halted, false);
const halted = api.transition(dir, { ...actor, halted: true });
assert.equal(halted.halted, true);
assert.notEqual(halted.generation, initial.generation);
assert.equal(api.readControl(dir).generation, halted.generation);
assert.throws(() => api.assertProcessing(dir), refusal('processing-halted'));
assert.throws(() => api.transition(dir, { ...actor, halted: false, generation: initial.generation }), refusal('processing-generation-changed'));
const resumed = api.transition(dir, { ...actor, halted: false, generation: halted.generation });
assert.equal(resumed.halted, false);
assert.doesNotThrow(() => api.assertProcessing(dir));
// A start that waited across halt+resume must not use its old admission.
assert.throws(() => api.assertProcessing(dir, initial.generation), refusal('processing-generation-changed'));
assert.equal(lstatSync(stateFile(dir)).mode & 0o777, 0o600);
assert.equal(lstatSync(join(dir, 'control')).mode & 0o777, 0o700);
});
// Reinitializing a missing file as off would bypass a previously published halt.
test('first initialization is explicit; missing state after control exists fails closed and halt repairs it', t => {
assert.equal(typeof api.initializeControl, 'function');
const dir = root(t);
assert.throws(() => api.readControl(dir), refusal('processing-uninitialized'));
const initial = api.initializeControl(dir);
assert.equal(api.initializeControl(dir).generation, initial.generation);
unlinkSync(stateFile(dir));
assert.throws(() => api.initializeControl(dir), refusal('processing-state-invalid'));
assert.throws(() => api.assertProcessing(dir), refusal('processing-state-invalid'));
assert.throws(() => api.transition(dir, { ...actor, halted: false, generation: initial.generation }), refusal('processing-state-invalid'));
const halted = api.transition(dir, { ...actor, halted: true });
assert.equal(halted.recovered, true);
assert.equal(halted.halted, true);
assert.equal(api.transition(dir, { ...actor, halted: false, generation: halted.generation }).halted, false);
});
// A read error, permissive mode or unbounded payload must never look like off.
for (const [name, edit] of [
['invalid JSON', file => writeFileSync(file, '{')],
['unknown keys', file => writeFileSync(file, JSON.stringify({ ...JSON.parse(readFileSync(file)), extra: true }))],
['bad timestamp', file => writeFileSync(file, JSON.stringify({ ...JSON.parse(readFileSync(file)), at: 'yesterday' }))],
['nonboolean state', file => writeFileSync(file, JSON.stringify({ ...JSON.parse(readFileSync(file)), halted: 0 }))],
['bad generation', file => writeFileSync(file, JSON.stringify({ ...JSON.parse(readFileSync(file)), generation: 'bad' }))],
['loose mode', file => chmodSync(file, 0o644)],
['oversized file', file => writeFileSync(file, ' '.repeat(8193))],
]) test(`refuses ${name} instead of enabling work`, t => {
assert.equal(typeof api.initializeControl, 'function');
const dir = root(t); api.initializeControl(dir); edit(stateFile(dir));
assert.throws(() => api.assertProcessing(dir), refusal('processing-state-invalid'));
});
test('a symlink leaf never follows its target; human halt replaces only the leaf', t => {
assert.equal(typeof api.initializeControl, 'function');
const dir = root(t); api.initializeControl(dir);
const outside = join(dir, 'outside'), target = readFileSync(stateFile(dir));
writeFileSync(outside, target, { mode: 0o600 });
unlinkSync(stateFile(dir)); symlinkSync(outside, stateFile(dir));
assert.throws(() => api.readControl(dir), refusal('processing-state-invalid'));
api.transition(dir, { ...actor, halted: true });
assert.deepEqual(readFileSync(outside), target);
assert.equal(lstatSync(stateFile(dir)).isSymbolicLink(), false);
});
test('unsafe parent and interrupted initialization never get repaired as off', t => {
assert.equal(typeof api.initializeControl, 'function');
const dir = root(t); mkdirSync(join(dir, 'control'), { mode: 0o700 });
assert.throws(() => api.initializeControl(dir), refusal('processing-state-invalid'));
chmodSync(join(dir, 'control'), 0o777);
assert.throws(() => api.transition(dir, { ...actor, halted: true }), refusal('processing-control-unsafe'));
});
test('repeated halt is a no-op and a different root keeps running', t => {
assert.equal(typeof api.initializeControl, 'function');
const a = root(t), b = root(t); api.initializeControl(a); api.initializeControl(b);
const first = api.transition(a, { ...actor, halted: true });
const before = readFileSync(stateFile(a));
assert.equal(api.transition(a, { ...actor, halted: true }).generation, first.generation);
assert.deepEqual(readFileSync(stateFile(a)), before);
assert.doesNotThrow(() => api.assertProcessing(b));
});
// Coercing undefined/numbers through a regex would publish unverifiable actor metadata.
test('transition rejects nonstring identity before publishing state', t => {
const dir = root(t); api.initializeControl(dir);
const before = readFileSync(stateFile(dir));
for (const bad of [undefined, null, 123, ['jason']]) {
assert.throws(() => api.transition(dir, { ...actor, human: bad, halted: true }), refusal('invalid-request'));
assert.deepEqual(readFileSync(stateFile(dir)), before);
}
});
// A lost control directory after power failure could permit a later off bootstrap.
test('bootstrap syncs the containing root as well as the state and control directory', t => {
const dir = root(t);
const script = `import fs from 'node:fs'; import {syncBuiltinESMExports} from 'node:module';
const real=fs.fsyncSync, seen=[];
fs.fsyncSync=fd=>{seen.push(fs.readlinkSync('/proc/self/fd/'+fd));return real(fd)};
syncBuiltinESMExports();
const {initializeControl}=await import(process.argv[1]);initializeControl(process.argv[2]);
console.log(JSON.stringify(seen));`;
const result = spawnSync(process.execPath, ['--input-type=module', '-e', script,
new URL('../src/state.mjs', import.meta.url).href, dir], { encoding: 'utf8' });
assert.equal(result.status, 0, result.stderr);
const seen = JSON.parse(result.stdout);
assert.ok(seen.includes(dir), 'control mkdir must be durable in the root');
assert.ok(seen.includes(join(dir, 'control')), 'state rename must be durable');
assert.ok(seen.some(p => p.startsWith(join(dir, 'control', '.state-'))), 'state bytes must be durable');
});
// Each fstat/read guard is exercised independently, rather than hidden by a later parse refusal.
test('state ownership and bounded read guards refuse before trusting a document', t => {
const dir = root(t); api.initializeControl(dir); const file = stateFile(dir);
const script = `import fs from 'node:fs';import {syncBuiltinESMExports} from 'node:module';
const stat=fs.fstatSync,read=fs.readSync;let reads=0;
const file=process.argv[2],which=process.argv[3],valid=fs.readFileSync(file);
if(which==='oversize')fs.writeFileSync(file,Buffer.concat([valid,Buffer.alloc(8193,32)]));
fs.fstatSync=fd=>{const s=stat(fd);if(fs.readlinkSync('/proc/self/fd/'+fd)===file){
if(which==='owner')s.uid++;
if(which==='grow')fs.writeFileSync(file,Buffer.concat([valid,Buffer.alloc(8193-valid.length,32)]));
}return s};
fs.readSync=(...args)=>{if(fs.readlinkSync('/proc/self/fd/'+args[0])===file)reads++;return read(...args)};syncBuiltinESMExports();
const {readControl}=await import(process.argv[1]);let code=null;
try{readControl(process.argv[4])}catch(e){code=e.code}console.log(JSON.stringify({code,reads}));`;
for (const which of ['owner', 'oversize', 'grow']) {
const valid = readFileSync(file);
const result = spawnSync(process.execPath, ['--input-type=module', '-e', script,
new URL('../src/state.mjs', import.meta.url).href, file, which, dir], { encoding: 'utf8' });
writeFileSync(file, valid);
assert.equal(result.status, 0, result.stderr);
const answer = JSON.parse(result.stdout); assert.equal(answer.code, 'processing-state-invalid', which);
if (which !== 'grow') assert.equal(answer.reads, 0, `${which}: refuse before reading bytes`);
else assert.ok(answer.reads > 0);
}
});
test('canonical root aliases address one switch and never create independent control state', t => {
const a = root(t), b = root(t), alias = join(b, 'alias');
api.initializeControl(a); symlinkSync(a, alias, 'dir');
assert.equal(api.initializeControl(alias).generation, api.readControl(a).generation);
const halted = api.transition(alias, { ...actor, halted: true });
assert.equal(api.readControl(a).generation, halted.generation);
assert.equal(api.readControl(a).halted, true);
});
@@ -1,183 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { initializeControl, readControl, transition } from '../src/state.mjs';
import { registerRuntime, updateRuntime, runtime, stopRun, sweepRuntimes, readStops } from '../src/runtimes.mjs';
const boot = '12345678-1234-4234-8234-123456789abc';
const decision = 'fire-decision';
function setup(t, run = 'unbound-run') {
const root = mkdtempSync(join(tmpdir(), 'stop-run-')); initializeControl(root);
t.after(() => rmSync(root, { recursive: true, force: true }));
const r = registerRuntime(root, { kind: 'session', business: 'demo', run });
updateRuntime(root, r.id, { pid: 2000000001, startTime: '42', boot, phase: 'running', group: false });
let time = 0, live = true, changed = false;
const signals = [], sleeps = [];
const options = {
now: () => time,
sleep: async ms => { sleeps.push(ms); time += ms; },
identity: pid => live ? { pid, startTime: changed ? '43' : '42', boot } : null,
state: () => live,
signal: (_r, sig) => { signals.push(sig); if (sig === 'SIGKILL') live = false; return true; },
};
return { root, r, signals, sleeps, options,
setLive: value => { live = value; }, change: () => { changed = true; },
args: { root, business: 'demo', run, reason: 'fire', owner: { decision } } };
}
test('unbound run stops by verified registry identity, fixed 5s grace and 2s observe policy', async t => {
const f = setup(t), before = readControl(f.root);
const result = await stopRun(f.args, f.options);
assert.equal(result.outcome, 'killed'); assert.equal(result.pid, 2000000001);
assert.deepEqual(result.signals, ['SIGTERM', 'SIGKILL']);
assert.ok(f.sleeps.reduce((a,b)=>a+b,0) >= 5000);
assert.equal(result.receipt.reason, 'fire'); assert.deepEqual(result.receipt.owner, { decision });
assert.deepEqual(readControl(f.root), before, 'fire never halts the instance');
});
test('same run joins the identical promise and completed repeats preserve owner and send no signals', async t => {
const f = setup(t);
const first = stopRun(f.args, f.options), second = stopRun(f.args, f.options);
assert.equal(second, first);
const result = await first;
const later = await stopRun({ ...f.args, owner: { decision: 'other' } }, f.options);
assert.deepEqual(later, result); assert.deepEqual(f.signals, ['SIGTERM', 'SIGKILL']);
});
test('identity changed before TERM or between TERM and KILL refuses the changed signal', async t => {
for (const phase of ['before', 'after-term']) {
const f = setup(t, phase);
if (phase === 'before') f.change();
f.options.signal = (_r, sig) => { f.signals.push(sig); f.change(); return true; };
const result = await stopRun(f.args, f.options);
assert.equal(result.outcome, 'identity-changed');
assert.deepEqual(f.signals, phase === 'before' ? [] : ['SIGTERM']);
assert.deepEqual(result.signals, f.signals);
}
});
test('failed signal delivery observes alive, records no invented signals and never restarts escalation', async t => {
const f = setup(t); f.options.signal = () => false;
const result = await stopRun(f.args, f.options);
assert.equal(result.outcome, 'alive'); assert.deepEqual(result.signals, []);
assert.equal(f.sleeps.reduce((a,b)=>a+b,0), 7000);
assert.deepEqual(await stopRun(f.args, f.options), result);
});
test('missing run and another business never borrow a pid or signal', async t => {
const f = setup(t);
for (const args of [{ ...f.args, run: 'missing' }, { ...f.args, business: 'other' }]) {
const result = await stopRun(args, f.options);
assert.equal(result.outcome, 'unknown'); assert.equal(result.pid, null); assert.deepEqual(result.signals, []);
}
assert.deepEqual(f.signals, []);
});
test('halt joins an in-progress fire without another TERM or deadline and links its first receipt', async t => {
const f = setup(t); let entered, release;
const ready = new Promise(resolve => { entered = resolve; });
const gate = new Promise(resolve => { release = resolve; });
let blocked = false;
const sleep = f.options.sleep;
f.options.sleep = async ms => { if (!blocked) { blocked = true; entered(); await gate; } await sleep(ms); };
const first = stopRun(f.args, f.options); await ready;
const halted = transition(f.root, { business: 'demo', human: 'jason', halted: true });
const sweep = sweepRuntimes(f.root, halted.generation, { stopOptions: f.options });
release(); const result = await first, summary = await sweep;
assert.equal(summary.quiescent, true); assert.deepEqual(f.signals, ['SIGTERM', 'SIGKILL']);
const [link] = readStops(f.root, halted.generation);
assert.equal(link.stop, result.receipt.id); assert.equal(link.reason, 'fire');
assert.equal(runtime(f.root, f.r.id).haltGeneration, undefined, 'halt does not relabel a fire-owned stop');
assert.deepEqual(link.owner, { decision });
});
test('a separate host joins durable first ownership without duplicate signals', async t => {
const { spawn } = await import('node:child_process');
const { once } = await import('node:events');
const f = setup(t), source = new URL('../src/runtimes.mjs', import.meta.url).href;
const script = `import {stopRun} from ${JSON.stringify(source)};
let time=0,live=true,waiting=true;
const args=JSON.parse(process.argv[1]);
const result=await stopRun(args,{
now:()=>time, identity:pid=>live?{pid,startTime:'42',boot:${JSON.stringify(boot)}}:null,
state:()=>live,signal:(_r,sig)=>{process.send({signal:sig});if(sig==='SIGKILL')live=false;return true},
sleep:async ms=>{if(waiting){waiting=false;await new Promise(r=>process.once('message',r))}time+=ms}
});process.send({result});process.disconnect();`;
const child = spawn(process.execPath, ['--input-type=module', '-e', script, JSON.stringify(f.args)],
{ detached: true, stdio: ['ignore', 'pipe', 'pipe', 'ipc'] });
t.after(() => { if (child.exitCode === null) { try { process.kill(-child.pid, 'SIGKILL'); } catch {} } });
const messages = []; child.on('message', value => messages.push(value));
await once(child, 'message');
const joined = stopRun({ ...f.args, owner: { decision: 'second-host' } },
{ ...f.options, sleep: ms => new Promise(resolve => setTimeout(resolve, ms)) });
setTimeout(() => child.send('continue'), 30);
const result = await joined;
const exit = child.exitCode === null ? await once(child, 'exit') : [child.exitCode];
assert.equal(exit[0], 0);
assert.deepEqual(f.signals, [], 'the second host sends no signal');
assert.deepEqual(messages.filter(m => m.signal).map(m => m.signal), ['SIGTERM', 'SIGKILL']);
assert.equal(result.receipt.id, messages.find(m => m.result).result.receipt.id);
assert.deepEqual(result.receipt.owner, { decision });
});
test('corrupt and symlinked final receipts refuse as unknown rather than signalling again', async t => {
const { readdirSync, writeFileSync, symlinkSync, unlinkSync } = await import('node:fs');
const f = setup(t); const complete = await stopRun(f.args, f.options);
const dir = join(f.root, 'control', 'run-stops');
const name = readdirSync(dir).find(n => n.endsWith('.result.json'));
writeFileSync(join(dir, name), '{broken', { mode: 0o600 });
assert.equal((await stopRun(f.args, f.options)).outcome, 'unknown');
writeFileSync(join(dir, name), JSON.stringify({ ...complete.receipt, outcome: 'invented' }), { mode: 0o600 });
assert.equal((await stopRun(f.args, f.options)).outcome, 'unknown');
const target = join(f.root, 'outside'); writeFileSync(target, JSON.stringify(complete.receipt), { mode: 0o600 });
unlinkSync(join(dir, name)); symlinkSync(target, join(dir, name));
assert.equal((await stopRun(f.args, f.options)).outcome, 'unknown');
assert.deepEqual(f.signals, ['SIGTERM', 'SIGKILL']);
});
test('a later exit gets separate halt evidence while the original alive result stays immutable', async t => {
const { spawn } = await import('node:child_process');
const { once } = await import('node:events');
const { processIdentity } = await import('../src/runtimes.mjs');
const f = setup(t);
const child = spawn(process.execPath, ['-e', "console.log('ready');setInterval(()=>{},1000)"],
{ detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
t.after(() => { try { process.kill(-child.pid, 'SIGKILL'); } catch {} });
await once(child.stdout, 'data');
const identity = processIdentity(child.pid);
updateRuntime(f.root, f.r.id, { ...identity, group: false });
f.options.identity = () => identity; f.options.signal = () => false;
const first = await stopRun(f.args, f.options);
assert.equal(first.outcome, 'alive');
const bytes = JSON.stringify(first);
const ended = once(child, 'exit'); child.kill('SIGKILL'); await ended;
const state = transition(f.root, { business: 'demo', human: 'jason', halted: true });
assert.equal((await sweepRuntimes(f.root, state.generation)).quiescent, true);
assert.equal(JSON.stringify(await stopRun(f.args, f.options)), bytes);
const { readdirSync } = await import('node:fs');
assert.equal(readdirSync(join(f.root, 'control', 'run-stops')).filter(n => n.endsWith('.observation.json')).length, 1);
});
test('root aliases join but a different root never shares the run stop', async t => {
const { symlinkSync } = await import('node:fs');
const a = setup(t), b = setup(t);
const alias = a.root + '-alias'; symlinkSync(a.root, alias); t.after(() => rmSync(alias));
const first = stopRun(a.args, a.options);
assert.equal(stopRun({ ...a.args, root: alias }, a.options), first);
const other = stopRun(b.args, b.options);
assert.notEqual(other, first);
const [one, two] = await Promise.all([first, other]);
assert.notEqual(one.receipt.id, two.receipt.id);
assert.deepEqual(a.signals, ['SIGTERM', 'SIGKILL']); assert.deepEqual(b.signals, ['SIGTERM', 'SIGKILL']);
});
test('a changed runtime record cannot replace the original stop identity after TERM', async t => {
const f = setup(t);
f.options.signal = (_r, sig) => {
f.signals.push(sig); f.change(); updateRuntime(f.root, f.r.id, { startTime: '43' }); return true;
};
const result = await stopRun(f.args, f.options);
assert.equal(result.outcome, 'identity-changed'); assert.deepEqual(f.signals, ['SIGTERM']);
});
test('identity is rechecked at actual signal delivery, not just at the loop observation', async t => {
const f = setup(t); let reads = 0;
f.options.identity = pid => ({ pid, boot, startTime: ++reads === 1 ? '42' : '43' });
const result = await stopRun(f.args, f.options);
assert.equal(result.outcome, 'identity-changed'); assert.deepEqual(f.signals, []);
});
Loaded 100 of 134 files, more files were not shown because too many files have changed in this diff. Show more