Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/turn-coordinator-ownership.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@schlessera/brain-ui-server": patch
---

Closing the host now rejects every pending location and mask request and denies every pending approval or question, including ones whose turn had already stopped. Previously those requests could stay unsettled. Turn state is now owned by the coordinator; the ask-user duplicate-id rules are unchanged.
6 changes: 3 additions & 3 deletions docs/decisions/answer-delivery.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ carries, no longer by position.

The host also re-sends a session's pending questions after `session_resume`
(`resendPendingAsks(host, ws, msg.sessionId)`,
`packages/ui-server/src/ws/dispatch.ts:677`). Before this, a resume replaced
`packages/ui-server/src/ws/dispatch.ts:651`). Before this, a resume replaced
the transcript and left only a history card behind, which was the second
symptom.

Expand All @@ -91,7 +91,7 @@ symptom.
Each answer carries a client-minted `submissionId`, reused for every retry of
that answer. The host settles the request at most once, records the outcome,
and answers the sender with `ask_answer_receipt` (`handleAskAnswer`,
`packages/ui-server/src/ws/ask-answers.ts:105-181`). A repeat of the same
`packages/ui-server/src/ws/ask-answers.ts:84-138`). A repeat of the same
submission gets the same receipt and settles nothing.

Revision 3's echo is negotiated per connection: a client that declares
Expand All @@ -106,7 +106,7 @@ capability flag is what peers check, because a revision number alone was
ruled not to establish receipt support.

Outcomes are remembered in memory for 24 hours, at most 1,024 of them
(`ASK_OUTCOME_TTL_MS`, `packages/ui-server/src/ws/turns.ts:374`). That is the
(`ASK_OUTCOME_TTL_MS`, `packages/ui-server/src/ws/turns.ts:412`). That is the
client's maximum replay age, so any answer a client may still replay finds
its outcome. Persisting outcomes would not help after a restart: the
requests themselves are gone then, and an answer for them must stop, not
Expand Down
2 changes: 1 addition & 1 deletion docs/decisions/design-kit/show-block-brief.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Four rules decide what counts, and each of them changed a number:
frames a subagent produced, and the chat adapter keeps those off the surface.
- **The turn budget is enforced, not just advertised.** Production aborts a
turn at `turnTimeoutMs` (`timeoutHandle = setTimeout`,
`packages/ui-server/src/ws/run-session.ts:388`), so
`packages/ui-server/src/ws/run-session.ts:378`), so
the harness aborts at the same 180 s. Without it an answer no reader could
have received still scored: an earlier run had five turns of 190–306 s.
- **A turn that did not complete is excluded from every rate**, in both
Expand Down
4 changes: 2 additions & 2 deletions docs/decisions/governed-writable-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ still holds.
**Voice.** Ruling 1 makes voice `governed` too. Voice keeps its narrower
membership (`VOICE_ALLOWED_TOOLS`, `packages/ui-backend-claude/src/tool-policy.ts:120`):
no Bash, no raw `Write`/`Edit`, no archive, and approval returns false
(`const apply = createBrainApplication`, `packages/ui-server/src/ws/bridge.ts:666-671`).
(`const apply = createBrainApplication`, `packages/ui-server/src/ws/bridge.ts:632-637`).
A voice turn therefore writes only through curated capture and update, now
with a journal behind it. The writable mount gives voice no tool it lacked.
Pi still refuses voice turns.
Expand Down Expand Up @@ -1520,7 +1520,7 @@ unmet rather than relaxed.

**Why.** Every governed turn queues an automatic index refresh after it
(`host.governedTurns?.refresh(host.workerLock)`,
`packages/ui-server/src/ws/run-session.ts:934`). The refresh usually finishes
`packages/ui-server/src/ws/run-session.ts:924`). The refresh usually finishes
while the next turn runs, so that turn's finish observation found a rewritten
`brain.db` and copied a whole new version of it into retained objects, inside
the observation's worker hold. #2831 measured 36 versions of a ~120 MB index in
Expand Down
8 changes: 4 additions & 4 deletions docs/decisions/http-api-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ explicit supported rows, not silently excluded by this distinction.

The source audit on 2026-09-30 used
`e0875269b21b707482545be9e94901acb897893b`. The public health mount precedes auth
(`createHealthRoutes`, `packages/ui-server/src/app.ts:884`), the API guard follows
public ceremonies (`authGuard`, `packages/ui-server/src/app.ts:905`), and the
WebSocket admission is separate (`"/ws"`, `packages/ui-server/src/app.ts:1095`).
(`createHealthRoutes`, `packages/ui-server/src/app.ts:891`), the API guard follows
public ceremonies (`authGuard`, `packages/ui-server/src/app.ts:912`), and the
WebSocket admission is separate (`"/ws"`, `packages/ui-server/src/app.ts:1098`).
Static serving is conditional (`if (options.staticRoot)`,
`packages/ui-server/src/app.ts:1115-1136`). That order establishes access;
`packages/ui-server/src/app.ts:1118-1139`). That order establishes access;
it does not select compatibility status.

A hermetic real-app launch, with temporary brain root, in-memory database,
Expand Down
2 changes: 1 addition & 1 deletion docs/decisions/live-conversation.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ posture. A speech provider cannot reinterpret a visual grant, widen an
allowlist, or settle an approval because it generated an answer.

The host resolves pending exchanges when a turn ends
(`drainPendingForTurn`, `packages/ui-server/src/ws/turns.ts:704-738`). The
(`drainPendingForTurn`, `packages/ui-server/src/ws/turns.ts:993-1012`). The
conversation integration must also reconcile client cards and audible claims;
that server method is not proof of a client receipt. Completion requires host
terminal state plus local playback drained or discarded. Provider generation
Expand Down
4 changes: 2 additions & 2 deletions docs/decisions/managed-worktrees.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ unknown; what happens next is in [hygiene and cleanup](#hygiene-and-cleanup).
Each governed or strict turn whose journal is recording opens one worker
generation, and every worker of that turn binds the generation's root-owned
directory whole at `/tmp/brain-worktrees`, inside the turn's own `/tmp`
(`// Managed worktrees (#2430): a journaled turn's workers bind one empty generation.`, `packages/ui-server/src/ws/run-session.ts:570-582`;
(`// Managed worktrees (#2430): a journaled turn's workers bind one empty generation.`, `packages/ui-server/src/ws/run-session.ts:560-572`;
`// Managed worktrees (#2430): the whole access generation`, `packages/ui-sdk/src/server/worker-launcher.ts:852-863`).
A granted worktree appears there as `/tmp/brain-worktrees/<worktree id>/` on the
worker's next path lookup; the worker cannot create entries beside it, and a
Expand Down Expand Up @@ -199,7 +199,7 @@ The host side is `createReferenceTools`
start, the host records its principal and what those workers bind: the public
view, the principal's private view and the turn's worker generation, if one
opened
(`// Reference tools (#2432): the turn's workers bind the principal's source`, `packages/ui-server/src/ws/run-session.ts:583-589`).
(`// Reference tools (#2432): the turn's workers bind the principal's source`, `packages/ui-server/src/ws/run-session.ts:573-579`).
A call carries only a repository, a revision or a worktree identity. The bridge
adds the turn's own principal, and a call for a turn the host did not prepare,
or under another principal, refuses
Expand Down
14 changes: 7 additions & 7 deletions docs/decisions/session-principals.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ Both reviewers verified every row below against the source.
| Cookie verify | `auth.ts:265-296` | strict `^(\d+)\.(\d+)$`, safe integers, future/expired rejection, `cookieEpoch === sessionsEpoch(db)` |
| Global epoch | `auth.ts:302-318` | `settings` row; missing = 0; non-integer **throws** |
| Global revoke | `auth.ts:321-330` | `setSetting` (overflow-guarded) + `clients.closeAll(1008, …)` |
| Auth routes | `authRoutes`, `app.ts:890` | mounted **before** the guard at `authGuard(authMode`, `app.ts:905`; logout guards itself (`resolveCookiePrincipal(c, auth, deps.db)`, `auth.ts:798`) |
| Passkey management | `passkeyManagementRoutes`, `app.ts:915` | mounted **after** the guard ("Mount AFTER the auth guard") |
| WS guard | `"/ws"`, `app.ts:1095-1112` | origin → `isWsAuthorized` (returns a boolean, `auth.ts:221-237`) → capacity → upgrade |
| Auth routes | `authRoutes`, `app.ts:897` | mounted **before** the guard at `authGuard(authMode`, `app.ts:912`; logout guards itself (`resolveCookiePrincipal(c, auth, deps.db)`, `auth.ts:798`) |
| Passkey management | `passkeyManagementRoutes`, `app.ts:922` | mounted **after** the guard ("Mount AFTER the auth guard") |
| WS guard | `"/ws"`, `app.ts:1098-1115` | origin → `isWsAuthorized` (returns a boolean, `auth.ts:221-237`) → capacity → upgrade |
| WS upgrade | `ws/connection.ts:262-264` | `createWsUpgrade` ignores the request context |
| WS admission | `ws/connection.ts:84` | `clients.add(ws)` — the socket's identity is unknown |
| Turn record | `ws/run-session.ts:136-144` | recorder built from `{turnId, sessionId, billing}`; `RunningTurn` (`ws/turns.ts:55-77`) has no actor |
| Follow-up queue | `QueuedFollowUp`, `ws/turns.ts:61-103`; `slot.queue.push(entry)`, `run-session.ts:1125` | queued entries re-mint `turnId` in the same slot and may come from another socket |
| Follow-up queue | `QueuedFollowUp`, `ws/turns.ts:61-103`; `slot.queue.push(entry)`, `run-session.ts:1112` | queued entries re-mint `turnId` in the same slot and may come from another socket |
| Password login | `acquirePasswordVerification(key)`, `auth.ts:739` | argon2id verify, failure counting, in-flight reservation, then `issueSessionCookie` |
| Passkey login | `issueLoginSession`, `packages/ui-server/src/middleware/passkeys.ts:441` | assertion verified, then `issueSessionCookie`; `row.id` is in scope |
| Passkey **registration** | `INSERT INTO passkey_credentials`, `packages/ui-server/src/db/account-writes.ts:31-36` | inserts a credential; calls **neither** helper |
Expand Down Expand Up @@ -178,13 +178,13 @@ ceremony in flight can mint a session for a credential deleted meanwhile.
**5. Revocation is a boundary that outlives admission.** Closing a socket is not
revocation: `onMessage` dispatches without re-checking (`ws/connection.ts:173`),
turn startup awaits routing and billing before `startTurn`
(`resolveTurnTarget`, `ws/run-session.ts:215`; `const billing = await resolveRunBilling`,
`ws/run-session.ts:411`), and queued follow-ups execute later. Each connection holds a
(`resolveTurnTarget`, `ws/run-session.ts:209`; `const billing = await resolveRunBilling`,
`ws/run-session.ts:401`), and queued follow-ups execute later. Each connection holds a
server-resolved authorization context; revocation marks it invalid
synchronously, refuses later frames, drops that principal's queued unstarted
follow-ups, and leaves running work running (cancelling a slot would take other
principals' queued work with it — `drop its queued follow-ups`,
`ws/turns.ts:670`). The revocation itself is recorded in the activity record.
`ws/turns.ts:959`). The revocation itself is recorded in the activity record.

> **2026-09-30 — Implementation context (decision 5's missing re-check).** The
> [old dispatch](https://github.com/schlessera/brain-kit/blob/ea2c3d840920a4e73adc650566a6cfc110e9646e/packages/ui-server/src/ws/connection.ts#L162-L220)
Expand Down
16 changes: 8 additions & 8 deletions docs/decisions/voice-permission.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ budget. That is already fail-closed: nothing runs. The user hears nothing
further, because a model that nags about a request the user ignored is worse
than one that lets the turn lapse. When the budget expires the host drains every
pending approval for that turn as a denial and deletes it
(`coordinator.drainPendingForTurn`, `packages/ui-server/src/ws/run-session.ts:401` →
`drainPendingForTurn`, `packages/ui-server/src/ws/turns.ts:704-738`),
(`coordinator.drainPendingForTurn`, `packages/ui-server/src/ws/run-session.ts:391` →
`drainPendingForTurn`, `packages/ui-server/src/ws/turns.ts:993-1012`),
so **on the server nothing is left waiting**: the request is resolved, not
parked. The model therefore says
that it stopped and that the thing has to be asked for again, never that it is
Expand All @@ -411,7 +411,7 @@ those two citations and is left to #54. The chat store clears a pending
approval on `tool_result`
(`packages/ui-react/src/hooks/websocket-handlers/chat.ts`), and on the timeout
path `abortController.abort()` fires before the drain
(`abortController.abort()`, `run-session.ts:396`; `drainPendingForTurn`, `run-session.ts:401`),
(`abortController.abort()`, `run-session.ts:386`; `drainPendingForTurn`, `run-session.ts:391`),
so whether a `tool_result` still streams for that tool use is a question a live
turn has to answer. It matters only for the
wording: if a dead card can survive on screen, the spoken line above is right
Expand All @@ -426,7 +426,7 @@ exchange over a security decision is a second chance for noise to produce a
grant.

**"Always allow" cannot be given by voice.** It is a persistent policy change
(the block computing `remembers`, `packages/ui-server/src/ws/dispatch.ts:449-455`)
(the block computing `remembers`, `packages/ui-server/src/ws/dispatch.ts:437-443`)
and it is the one decision on
the card with no keyboard shortcut, by D37's ruling 5, and the reason given
there is exactly the one that applies here: *"a letter that grants standing
Expand All @@ -435,7 +435,7 @@ permission by reflex is the one footgun in the vocabulary"*
A microphone is a reflex surface with worse recognition than a keyboard. This
costs nothing measurable:
the server already refuses `always` for kind `command` requests
(the block computing `remembers`, `dispatch.ts:449-455`, and the lookup computing
(the block computing `remembers`, `dispatch.ts:437-443`, and the lookup computing
`remembered`, `ws/bridge.ts:372-392`), and 192 of 192 measured
approvals were kind `command`.

Expand Down Expand Up @@ -493,7 +493,7 @@ the request kind, and it patches the span
(`onApprovalDecision`, `packages/ui-server/src/activity/recorder.ts:674-689`),
fed from the bridge's
`recorded()` wrapper
(`const recorded`, `packages/ui-server/src/ws/bridge.ts:414-429`).
(`const recorded`, `packages/ui-server/src/ws/bridge.ts:410-425`).

One thing is missing and is a follow-up: **the event does not record the
modality.** A denial decided by a phrase a microphone heard and one decided by a
Expand All @@ -505,7 +505,7 @@ provenance.

The wire needs nothing new: a spoken refusal is an ordinary `tool_denial`
(in `handleClientMessage`, the arm `case "tool_denial"`,
`packages/ui-server/src/ws/dispatch.ts:496-519`) with a message naming the
`packages/ui-server/src/ws/dispatch.ts:480-493`) with a message naming the
phrase that produced it.

## Containment: shared with #51, deliberately not identical
Expand Down Expand Up @@ -640,7 +640,7 @@ matters because a reader cannot otherwise tell a live hazard from a fixed one.
left out (the lookup computing `remembered`,
`packages/ui-server/src/ws/bridge.ts:372-392`,
and the block computing `remembers`,
`packages/ui-server/src/ws/dispatch.ts:449-455`).
`packages/ui-server/src/ws/dispatch.ts:437-443`).
The evaluation happens
before the lookup, which is what this record asked for.

Expand Down
4 changes: 2 additions & 2 deletions docs/investigations/live-conversation-investigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,12 @@ matcher. An isolated ambiguous refusal denies all **current** pending requests;
an exact refusal can name one. With none pending, it is ordinary input. Bind
each denial to existing IDs, echoed turn and `channel:"voice"`; the server still
rejects voice grants
(`if (msg.channel === "voice")`, `packages/ui-server/src/ws/dispatch.ts:409-432`).
(`if (msg.channel === "voice")`, `packages/ui-server/src/ws/dispatch.ts:397-420`).
Never add a spoken approve or always-allow path.

On the evidence date the denial handler removed/resolved a matched request but
provided no dedicated denial receipt at that branch; #957 added one
(`case "tool_denial"`, `packages/ui-server/src/ws/dispatch.ts:496-519`). The
(`case "tool_denial"`, `packages/ui-server/src/ws/dispatch.ts:480-493`). The
integration therefore needs host-authoritative resolution/replay: denied,
already granted, expired, or unknown/disconnected. Speak a denial confirmation
only for a confirmed denied outcome. A visual grant winning the race remains
Expand Down
6 changes: 3 additions & 3 deletions docs/plans/async-collaboration-requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Stated before the requirements because five of them derive from it.
that is the shape to copy, including its `close()` lifecycle.
- R19. **The cron backstop has independent authorization before the general guard.**
Mount it on the existing listener before
(`app.use("/api/*", authGuard(`, `packages/ui-server/src/app.ts:905`).
(`app.use("/api/*", authGuard(`, `packages/ui-server/src/app.ts:912`).
Authorize a boot-minted ephemeral token, rotated each boot and stored in a
0600 runtime file, with the actual socket address as an additional check.
Proxy headers cannot authorize it. The poke succeeds in every auth mode
Expand Down Expand Up @@ -256,7 +256,7 @@ Stated before the requirements because five of them derive from it.
(`export function createTurnRecorder(`, `packages/ui-server/src/activity/recorder.ts:125-203`).
- R25. **Autonomous work gets its own pool** (`MAX_AUTONOMOUS_RUNS`, default 2) — but a second
counter alone does not deliver "interactive always wins". The host cap applies only when
starting WS sessions (`const cap = host.maxConcurrentSessions();`, `packages/ui-server/src/ws/run-session.ts:1336-1348`), and an autonomous
starting WS sessions (`const cap = host.maxConcurrentSessions();`, `packages/ui-server/src/ws/run-session.ts:1323-1335`), and an autonomous
turn can hold a path write lock while an interactive turn waits or is denied at 30 seconds
(`export function createTurnLockBinding(`, `packages/ui-backend-claude/src/turn-lock.ts:27-119`). Required: an admission controller
with reserved interactive capacity and hybrid yield at an explicit denial-risk
Expand All @@ -266,7 +266,7 @@ Stated before the requirements because five of them derive from it.
promise (`return new Promise<PermissionDecision>((resolve) => {`, `packages/ui-server/src/ws/bridge.ts:406-464`) — while blocked on it the model cannot write anything, so
"writes its findings, then aborts" has nowhere to run. The autonomous bridge must, in one
server-side step: capture the checkpoint, create the Action and block the item, unwind without a live approval promise, preserving the tested timeout
path's abort-then-drain order (`abortController.abort();`, `packages/ui-server/src/ws/run-session.ts:396-402`). Aborting does **not** undo completed tool side
path's abort-then-drain order (`abortController.abort();`, `packages/ui-server/src/ws/run-session.ts:386-392`). Aborting does **not** undo completed tool side
effects, so every attempt gets an isolated staging directory with idempotent cleanup. This
does not reopen the abort-and-redo decision; it corrects revision 1's claim that the
decision needed no new machinery.
Expand Down
Loading
Loading