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
6 changes: 6 additions & 0 deletions .changeset/contain-single-host-failures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@schlessera/brain-ui-sdk": minor
"@schlessera/brain-ui-server": minor
---

Recover failed writer-gate deletes and disposable native indexes automatically; quarantine corrupt history and unreadable native intents without refusing host startup. Preserve each surviving stage's historical storage bound and expose native recovery restrictions in authenticated status.
5 changes: 5 additions & 0 deletions api-report/ui-sdk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12190,6 +12190,11 @@ signatures (every public export and the types it is made of)
}
SystemStatus (packages/ui-sdk/src/protocol.ts)
export interface SystemStatus {
nativeRecovery?: {
quarantinedIntents: number;
stagingChargedBytes: number | null;
diagnosticsPath: string;
};
healthy: boolean;
uptime: number;
version: string;
Expand Down
5 changes: 5 additions & 0 deletions api-report/ui-server.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,11 @@ signatures (every public export and the types it is made of)
availableBytes: number;
recoveryReserveBytes: number;
reserveAllocatedBytes: number;
recovery?: {
quarantinedIntents: number;
stagingChargedBytes: number | null;
diagnosticsPath: string;
};
}
NativePoolAccess (packages/ui-sdk/src/server/native-brain-manager-protocol.ts)
export interface NativePoolAccess {
Expand Down
2 changes: 1 addition & 1 deletion docs/decisions/bounded-multi-read.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ parallel default, with concurrency 1.
proves only `read_file`, `write_file` and `edit_file`. So
(`toolObservation`, `packages/ui-server/src/brain/tool-observation.ts:37-42`)
calls it `unproven`, and
(`toolEnded`, `packages/ui-server/src/brain/turn-journal.ts:551-558`)
(`toolEnded`, `packages/ui-server/src/brain/turn-journal.ts:555-562`)
observes every result and marks the turn lasting. Four reads made four
observations, sequential or parallel. One batch call made one. That is a
real host-side saving, small in time (8.3 ms against 1.4 ms on the fixture).
Expand Down
2 changes: 1 addition & 1 deletion docs/decisions/claude-code-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ than refuses on a mismatch.**
first proof of that.
- **Where it shows.** `/api/status`, which is behind the auth guard and already
reports the source commit (`Operational status`,
`packages/ui-server/src/routes/health.ts:61-79`), and the run record. Not
`packages/ui-server/src/routes/health.ts:62-80`), and the run record. Not
`/api/health`: it is public and deliberately carries no version
(`Public liveness probe`, `health.ts:15-17`). Not `brain doctor`: it runs on
the user's machine against a different binary.
Expand Down
28 changes: 14 additions & 14 deletions docs/decisions/governed-writable-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,7 @@ starts one per result and carries on
(`void host.journal?.toolEnded`, `packages/ui-server/src/ws/bridge-capabilities/emission.ts:272`).
The wait is the observation's hold. `held` takes it synchronously, before it
queues the capture
(`function held<T>`, `packages/ui-server/src/brain/turn-journal.ts:246-267`),
(`function held<T>`, `packages/ui-server/src/brain/turn-journal.ts:250-271`),
and the scope monitor's hold freezes every active turn's productive workers
until the capture completes (`before() {`,
`packages/ui-server/src/brain/turn-scope.ts:158-166`). Pi's agent loop runs in
Expand All @@ -1481,7 +1481,7 @@ tracked the hold, not the bridge call.
proven read-only, no other call of an active turn is still running, and no
active turn has run a call whose effects may outlast it
(`toolEnded(turnId: string, toolUseId: string)`,
`packages/ui-server/src/brain/turn-journal.ts:551-558`). Admission, turn end,
`packages/ui-server/src/brain/turn-journal.ts:555-562`). Admission, turn end,
the 15-second timer, scope-limit observations, approved policy writes and
removal coverage observe exactly as before.

Expand Down Expand Up @@ -1602,11 +1602,11 @@ with retained bytes.
**What the journal records.** Observation and before-version capture classify
these versions as metadata-only and never open them for retention
(`const retained = (entry`,
`packages/ui-server/src/brain/native-journal-capture.ts:40`). A version keeps
`packages/ui-server/src/brain/native-journal-capture.ts:41`). A version keeps
kind, mode, size and its ignored flag, carries `metadataOnly: true` and records
its opaque change stamp, with no content hash and no symlink target. Two
versions of equal size are told apart by that stamp, as ignored ones are
(`const identity`, `packages/ui-server/src/brain/turn-journal.ts:81`),
(`const identity`, `packages/ui-server/src/brain/turn-journal.ts:82`),
so every observed refresh remains a recorded change. A metadata-only version is an intentional
observation state: it adds no `missing` entry and does not make the turn
partial. Removal coverage never treats the family as eligible, and history
Expand All @@ -1618,7 +1618,7 @@ stays now counts its largest size once too, rather than each version.
**History.** History presents every index-family version as metadata-only
however it was recorded, so a version an earlier release retained is no
longer read
(`const presented`, `packages/ui-server/src/brain/turn-journal.ts:74-79`).
(`const presented`, `packages/ui-server/src/brain/turn-journal.ts:75-80`).
The reader groups these entries as the search index, recorded by size and
timestamps only, with no review action. Preview reports `metadata_only` and
download has nothing to serve. History revert and Undo read that same
Expand Down Expand Up @@ -1675,11 +1675,11 @@ keeps its bytes.

**The purge.** It runs once, on the journal's write worker, when the journal
opens, after interrupted turns have been ended
(`while (await ledger.call`, `packages/ui-server/src/brain/turn-journal.ts:121`),
(`while (await ledger.call`, `packages/ui-server/src/brain/turn-journal.ts:122`),
in at most two accounted commits whose progress is recorded in
`turn_journal_allocation.index_purge`
(`purgeIndexVersions(): IndexPurgeResult`,
`packages/ui-server/src/brain/journal-store.ts:184-223`):
`packages/ui-server/src/brain/journal-store.ts:186-225`):

1. Every recorded index-family version, in history and in revert records, is
rewritten to the metadata-only form a new one has: kind, mode, size and the
Expand Down Expand Up @@ -1743,7 +1743,7 @@ migrations before accounting can admit them.
**Admission before storage.** The upgrade runs on the journal write worker,
through the same `BEGIN IMMEDIATE`, `synchronous=FULL` accounting as its data
writes (`function ensureObjectRefIndex`,
`packages/ui-server/src/brain/journal-accounting.ts:164-193`). Under that lock,
`packages/ui-server/src/brain/journal-accounting.ts:169-198`). Under that lock,
it reserves a conservative allowance for index records, b-tree fill/interior
pages and schema/pointer-map overhead, plus the WAL capacity over the enlarged
file and all existing WAL debt, objects and reservations. It assumes no reuse
Expand Down Expand Up @@ -1773,7 +1773,7 @@ attempts it without making recovery depend on success. Ordinary collection
tries before deleting, runs the existing integrity-checked path if it was
refused, and retries after the freed object bytes make room
(`collectObjects(): void`,
`packages/ui-server/src/brain/journal-store.ts:157-163`). Freeing rows alone
`packages/ui-server/src/brain/journal-store.ts:159-165`). Freeing rows alone
does not credit persistent pages: they remain charged until reclamation is
observed. Every pin and reservation keeps its object throughout success,
refusal, failure and restart, and foreign keys remain enabled on every ordinary
Expand Down Expand Up @@ -1816,7 +1816,7 @@ fresh globals per request, with the path as its only argument, and answers
with the program's output, its exit status and the traceback a fresh launch
printed, without the loop's own frame. The manager turns a failure into the
same refusal a failed launch produced (`async function inspection`,
`packages/ui-sdk/src/server/native-brain-manager.ts:186-190`). Every
`packages/ui-sdk/src/server/native-brain-manager.ts:189-193`). Every
`qualify` and `verifyOwned` still inspects the live path on every operation;
nothing is cached between requests. An interrupted, timed-out, oversized or
malformed exchange, or one with an interpreter that died, kills the
Expand Down Expand Up @@ -2535,7 +2535,7 @@ A peer whose plan only lacks a root is waited for, up to
are planned only once a turn has translated it under the current policy hash,
so writers admitted before that translation predate them, and every later
writer inherits them from the held plan
(`waitForPeerFolderPlans`, `packages/ui-server/src/brain/writer-gate.ts:342-353`).
(`waitForPeerFolderPlans`, `packages/ui-server/src/brain/writer-gate.ts:381-392`).
The translation is recorded in the UI database, so after the first one under a
policy hash, writers in every process, the cron runner's and a restarted
server's included, plan its folders without waiting (#2836).
Expand Down Expand Up @@ -3170,7 +3170,7 @@ index refresh or a cron job, retains them and the next turn's peer check
passes. Admission records that translation in the UI database, one row per
canonical brain holding the exact policy hash and its folders, so it reaches
the cron runner's process and survives a restart (#2836;
`function recordedTypeFolders`, `packages/ui-server/src/brain/writer-gate.ts:173-182`).
`function recordedTypeFolders`, `packages/ui-server/src/brain/writer-gate.ts:175-184`).
The host never translates it itself: only admission does, under its worker
boundary. A missing, unreadable or other-hash record plans no type folder, and
a stale one only retains an extra folder. Admission still refuses a peer
Expand Down Expand Up @@ -4319,7 +4319,7 @@ attribution. If an existing same-session/turn taint event precedes an agent
observation, the journal retains its source ID with that change. Replay never
infers taint from a later event, and absence of evidence does not assert complete
untrusted-input detection. User history operations are not tainted agent writes
(`async function observe`, `packages/ui-server/src/brain/turn-journal.ts:380-445`).
(`async function observe`, `packages/ui-server/src/brain/turn-journal.ts:384-449`).

Claude auto-memory persists outside the authoritative brain. Its validated
native copy-out wraps each changed direct `.md` note in host journal retention:
Expand All @@ -4342,7 +4342,7 @@ memory completion of the earlier 0.42.0 steering slice, not a widening of memory
providers or a claim of capturing intervening external writes. The copy-out
boundary is `const write`, `packages/ui-backend-claude/src/worker-state.ts:210-224`;
retention/finalization is `persistClaudeMemory`,
`packages/ui-server/src/brain/turn-journal.ts:657-687`; destination-bound recovery
`packages/ui-server/src/brain/turn-journal.ts:662-692`; destination-bound recovery
is `const target`, `packages/ui-server/src/brain/turn-revert.ts:113-123`.

### Concurrent edits
Expand Down
4 changes: 2 additions & 2 deletions docs/decisions/http-api-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ The source audit on 2026-09-30 used
`e0875269b21b707482545be9e94901acb897893b`. The public health mount precedes auth
(`createHealthRoutes`, `packages/ui-server/src/app.ts:1019`), the API guard follows
public ceremonies (`authGuard`, `packages/ui-server/src/app.ts:1040`), and the
WebSocket admission is separate (`"/ws"`, `packages/ui-server/src/app.ts:1260`).
WebSocket admission is separate (`"/ws"`, `packages/ui-server/src/app.ts:1261`).
Static serving is conditional (`if (options.staticRoot)`,
`packages/ui-server/src/app.ts:1280-1301`). That order establishes access;
`packages/ui-server/src/app.ts:1281-1302`). That order establishes access;
it does not select compatibility status.

A hermetic real-app launch, with temporary brain root, in-memory database,
Expand Down
27 changes: 24 additions & 3 deletions docs/decisions/journal-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ that no pre-charge covers. The evidence is on the issue.
`DB_PATH` names one journal.
Every page in it is the journal's, so the measurement is the file's page
count and the journal's own WAL, read without walking
(`function measure(`, `packages/ui-server/src/brain/journal-accounting.ts:67-81`).
(`function measure(`, `packages/ui-server/src/brain/journal-accounting.ts:72-86`).
Other components' pages and WAL frames are no longer the journal's charge.
- **The tables move on start.** Migrations up to `063` create journal tables
in the UI database. Before any later migration, and again after the last,
Expand Down Expand Up @@ -93,11 +93,11 @@ that no pre-charge covers. The evidence is on the issue.
capture took an inventory at all, which it does only for an observation that
saw a change. Per-turn arrays from earlier releases move into sets at open, a
bounded batch per accounted commit, because SQLite cannot hash
(`migrateConfigurationSets(limit`, `packages/ui-server/src/brain/journal-store.ts:230-238`);
(`migrateConfigurationSets(limit`, `packages/ui-server/src/brain/journal-store.ts:232-240`);
readers understand the arrays until then, and an empty legacy array stays
"no set recorded". The brain's current set is the one the most recent complete,
inventoried turn recorded
(`configurationCoverage(): ConfigurationCoverageRead`, `packages/ui-server/src/brain/turn-journal.ts:738-744`).
(`configurationCoverage(): ConfigurationCoverageRead`, `packages/ui-server/src/brain/turn-journal.ts:743-749`).
The sets table is an ordinary rowid table, as every journal table is: the
operational backup copies and compares journal rows in rowid order.

Expand All @@ -111,3 +111,24 @@ and the ledger's attach is then a no-op.
A `roadblock` event a journal-reader failure records is not replayed: its
payload carries timings that the journal does not keep. The journal's `missing`
record of that path stays authoritative.

## Corrupt interrupted publication (#3816)

Recovery quarantines a published object that fails verification in
`turn_journal_object_quarantine`. The cause includes error class, message, time
and call site. The bytes stay at their original hash and remain charged, while
read leases and new reservations for that hash refuse. The interrupted writer's
reservation is cleared in the same accounted transaction that records quarantine
and marks every referencing history path incomplete. Other history stays usable.
The history details expose `retained_object_corrupt`; normal expiry eventually
collects the quarantined bytes and their diagnostic row. A later disk repair
does not silently turn the old incomplete history into complete history.

Later captures of a quarantined hash record only the affected path as missing
(`retained_object_corrupt`); the shared observation and other active turns
continue. No replacement is trusted and old gap facts stay intact. An I/O error
while testing existence or verification proves neither corruption nor absence:
the reservation, object state and charge remain unchanged for the next recovery
pass. Only a verified mismatch or confirmed absence of retained content can
quarantine it. Recovery marks live owners' paths without changing their running
state; their normal completion preserves the gap.
4 changes: 2 additions & 2 deletions docs/decisions/managed-worktrees.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ A managed worktree is not inside the brain, so the brain's checkpoints never
cover it. The turn journal observes each covered worktree as its own root,
through that worktree's read-only checkpoints and deltas and the same observer
and capture as the brain
(`attachWorktree(turnId: string`, `packages/ui-server/src/brain/turn-journal.ts:563-575`;
`// Each managed worktree is observed through its own checkpoints`, `packages/ui-server/src/brain/turn-journal.ts:397-411`).
(`attachWorktree(turnId: string`, `packages/ui-server/src/brain/turn-journal.ts:567-579`;
`// Each managed worktree is observed through its own checkpoints`, `packages/ui-server/src/brain/turn-journal.ts:401-415`).
Coverage starts with a baseline checkpoint and inventory before the grant, and
every journal observation, periodic, at a scope cap or at the turn's end, then
observes the worktree too.
Expand Down
49 changes: 46 additions & 3 deletions docs/decisions/native-brain-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ individual page with mutations. Pages do not promise a multi-request snapshot:
an identity inserted below an already-consumed cursor is not returned later.

Intent rename and parent-directory fsync precede index insertion. If insertion
fails, the mutation has an unknown outcome and the index refuses subsequent
pages until restart rebuilds it. Losing the in-memory index or a transport reply
does not lose the durable identity. Deleted intents remain present and continue
fails, the manager builds a complete replacement from those durable intents
before continuing. A failed rebuild refuses dependent admission and pages;
each later request retries the rebuild in the same process. Losing the
in-memory index or a transport reply does not lose the durable identity. Deleted intents remain present and continue
to fence delayed create requests; no identity retirement or tombstone lifetime
policy is introduced.

Expand Down Expand Up @@ -208,3 +209,45 @@ Reduced undo remains a distinct product choice: committed-version restoration
or mediated-tool-only undo can avoid universal preimages. Removing only the
whole-turn revert action cannot. This ruling keeps existing observed-version
undo and changes its storage mechanism instead.

## Containing recovery failures (#3816)

An unreadable native intent stays in place as an identity fence. Startup records
its error class, message, site and timestamp under the manager's private
`recovery-diagnostics/` directory, and continues. `ready` reports the count,
diagnostic directory and conservatively charged staging bytes; authenticated
`/api/status` exposes that boot report as `nativeRecovery`. An invalid UUID
cannot own a stage and has no staging charge. A confirmed absent stage directory
also charges zero: the directory precedes namespace setup and is removed only
after confirmed close. The malformed receipt remains quarantined in both cases.

Before starting a stage, the manager durably records its exact tmpfs allowance
in a separate root-owned `stages/<id>.allowance` file. A surviving or unreadable
directory retains that cap even if today's configured allowance is smaller.
The allowance record is removed only after close and durable directory removal;
neither a failed close nor an I/O error releases it. Healthy stages can use the
remaining aggregate budget. An old stage without this independent record, or a
damaged/unreadable record, has no provable historical bound. Its charge stays
unknown (`null`), new staging returns an explicit recovery refusal instead of a
retryable capacity error, and authenticated status reports `healthy: false`.
The cause stays in the private diagnostics directory. Repair requires proof of
the historical cap or confirmed cleanup, never a guess based on current config.
No historical checkpoint dependency is released while a quarantined receipt
could own it.
Healthy checkpoint creation and reads remain available;
new roots created after recovery can also be released, since a historical stage
cannot refer to those native identities. A recovery
page touching that identity refuses explicitly. Repairing an authoritative
receipt requires owner judgment; restart after that repair reconstructs the
known reservation. Unknown identities are never adopted or deleted.

Index rebuilds use the same diagnostics directory; they need no owner action.
The reference pool refreshes its accounting map from the same durable intents
before it reopens dependent admission.

Diagnostic writes are best-effort and cannot gate mechanical recovery. If the
private diagnostic directory refuses a write, the structured stderr record
retains both the original cause and that write failure; the authoritative bad
intent remains on disk. A disposable index rebuild publishes only its complete
replacement, and retries pending diagnostic writes on later requests separately
from admission.
2 changes: 1 addition & 1 deletion docs/decisions/proportionate-containment.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ design, not an afterthought.
non-restorable.
- Disproportionate: one corrupt undo object stops the server from starting
(`recover(objects)`,
`packages/ui-server/src/brain/journal-accounting.ts:225-239`).
`packages/ui-server/src/brain/journal-accounting.ts:231-245`).
5. **Diagnosable.** Every latch, give-up, quarantine or restricted boot records
its cause durably (error class, message, call site, scope, time), writes one
structured log line, and is visible to the owner. A cause that is only in a
Expand Down
Loading
Loading