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/retained-session-stages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@schlessera/brain-ui-server": patch
---

A session's turns now run through named lifecycle stages: slot setup, host and governed admission, backend entry, outcome classification and teardown. Turn behaviour, ordering and the wire protocol are unchanged.
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:378`), so
`packages/ui-server/src/ws/session-run/admission.ts:47`), 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
2 changes: 1 addition & 1 deletion docs/decisions/governed-writable-workers.md
Original file line number Diff line number Diff line change
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:924`). The refresh usually finishes
`packages/ui-server/src/ws/session-run/teardown.ts:54`). 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
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:560-572`;
(`// Managed worktrees (#2430): a journaled turn's workers bind one empty generation.`, `packages/ui-server/src/ws/session-run/admission.ts:191-203`;
`// 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:573-579`).
(`// Reference tools (#2432): the turn's workers bind the principal's source`, `packages/ui-server/src/ws/session-run/admission.ts:204-210`).
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
6 changes: 3 additions & 3 deletions docs/decisions/session-principals.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Both reviewers verified every row below against the source.
| 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:1112` | 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:260` | 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,8 +178,8 @@ 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:209`; `const billing = await resolveRunBilling`,
`ws/run-session.ts:401`), and queued follow-ups execute later. Each connection holds a
(`resolveTurnTarget`, `ws/run-session.ts:105`; `const billing = await resolveRunBilling`,
`ws/session-run/turn.ts:35`), 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
Expand Down
4 changes: 2 additions & 2 deletions docs/decisions/voice-permission.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ 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:391` →
(`host.coordinator.drainPendingForTurn`, `packages/ui-server/src/ws/session-run/admission.ts:56` →
`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
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:386`; `drainPendingForTurn`, `run-session.ts:391`),
(`abortController.abort()`, `session-run/admission.ts:55`; `drainPendingForTurn`, `session-run/admission.ts:56`),
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 Down
4 changes: 2 additions & 2 deletions docs/plans/async-collaboration-requirements.md
Original file line number Diff line number Diff line change
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:1323-1335`), and an autonomous
starting WS sessions (`const cap = host.maxConcurrentSessions();`, `packages/ui-server/src/ws/run-session.ts:471-483`), 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:386-392`). Aborting does **not** undo completed tool side
path's abort-then-drain order (`abortController.abort();`, `packages/ui-server/src/ws/session-run/admission.ts:55-57`). 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
10 changes: 5 additions & 5 deletions docs/plans/async-collaboration.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ system gate. It is not a prerequisite to rebuild or a new independent package.
(`export function authGuard(`, `packages/ui-server/src/middleware/auth.ts:184-244`) binds principals in each auth mode.
An internal poke needs independent token authorization before this guard.
- **Permission parking:** (`requestPermission: (req) => {`, `packages/ui-server/src/ws/bridge.ts:362-456`). Timeout unwind is
(`abortController.abort();`, `packages/ui-server/src/ws/run-session.ts:386-392`). Durable escalation must checkpoint
(`abortController.abort();`, `packages/ui-server/src/ws/session-run/admission.ts:55-57`). Durable escalation must checkpoint
before unwinding; the existing ordinary bridge does not do that.
- **Tool enforcement:** (`const enforcementHook: HookCallback`, `packages/ui-backend-claude/src/permission-hooks.ts:200-229`) closes measured bypasses.
(`export const DEFAULT_ALLOWED_TOOLS = [`, `packages/ui-backend-claude/src/tool-policy.ts:27-80`) is still a broad interactive
Expand All @@ -156,7 +156,7 @@ system gate. It is not a prerequisite to rebuild or a new independent package.
- **Share provenance/limits:** (`const result = await stageShareAt(`, `packages/ui-server/src/inbox/intake.ts:101`) assigns the source in server code.
(`export const SHARE_MAX_TEXT_BYTES =`, `packages/ui-sdk/src/protocol.ts:2401`) bounds text, not binary uploads;
extracted T1 context needs its own byte/token bound.
- **Interactive locks:** (`const cap = host.maxConcurrentSessions();`, `packages/ui-server/src/ws/run-session.ts:1323-1335`) gates WS starts;
- **Interactive locks:** (`const cap = host.maxConcurrentSessions();`, `packages/ui-server/src/ws/run-session.ts:471-483`) gates WS starts;
(`export function createTurnLockBinding(`, `packages/ui-backend-claude/src/turn-lock.ts:27-119`) owns tool locks.
Shared-target priority and cooperative yield belong to the keyed lock and backend lifecycle.

Expand Down Expand Up @@ -884,7 +884,7 @@ AE3, AE4, AE8
**Approach:**
- **Escalation** is one server-side step: capture the checkpoint → create the Action with
validated effects → transition the Queue item to `blocked` → unwind without parking. The timeout path aborts then drains permissions at
`abortController.abort()`, (`abortController.abort();`, `packages/ui-server/src/ws/run-session.ts:386-392`)
`abortController.abort()`, (`abortController.abort();`, `packages/ui-server/src/ws/session-run/admission.ts:55-57`)
- **Resolution** is one transaction: record resolution (unique) → validate the effect against
its schema again → apply it → transition the blocked item to `superseded` → for `enqueue`
only, mint one follow-up with `dedup_key` from `(action_id, option_id)`
Expand Down Expand Up @@ -1274,7 +1274,7 @@ full-v1 enablement; predicates and schema tests cannot substitute for it.

**Approach:** **hybrid — reserve capacity normally, yield only at denial risk.**
`MAX_AUTONOMOUS_RUNS` (default 2) is necessary but not sufficient: the host cap applies only at
WS session start (`const cap = host.maxConcurrentSessions();`, `packages/ui-server/src/ws/run-session.ts:1323-1335`) and an autonomous turn can hold a path write lock
WS session start (`const cap = host.maxConcurrentSessions();`, `packages/ui-server/src/ws/run-session.ts:471-483`) and an autonomous turn can hold a path write lock
while an interactive turn waits or is denied at 30s
(`export function createTurnLockBinding(`, `packages/ui-backend-claude/src/turn-lock.ts:27-119`).

Expand All @@ -1285,7 +1285,7 @@ The hybrid has three pieces:
waiter's continued wait signals the holder. The number is a constant, not a judgement call,
so both edges are testable
- **A yield channel into a running autonomous turn** — the signal aborts the holder through
the same unwind order as the timeout path (`abortController.abort();`, `packages/ui-server/src/ws/run-session.ts:386-392`), returning its item
the same unwind order as the timeout path (`abortController.abort();`, `packages/ui-server/src/ws/session-run/admission.ts:55-57`), returning its item
to `ready` and releasing its reservation

Below the threshold nothing yields, so the common case costs nothing. Above it, one autonomous
Expand Down
2 changes: 1 addition & 1 deletion docs/plans/navigation-recovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ implementation. Any PR moving these lines owns their citation repairs.

- API authorization: (`app.use("/api/*", authGuard(authMode, auth, db));`, `packages/ui-server/src/app.ts:912`).

- Queued acceptance: (`// Queue it as the session's next turn; report queued immediately.`, `packages/ui-server/src/ws/run-session.ts:1089-1134`).
- Queued acceptance: (`// Queue it as the session's next turn; report queued immediately.`, `packages/ui-server/src/ws/run-session.ts:237-282`).

- Approval target: (`function mutateLastAssistant(`, `packages/ui-react/src/stores/chat-slices/buffers.ts:142-172`).

Expand Down
Loading
Loading