Skip to content

ui-server: a latched safety recorder keeps no cause, mislabels errors as capacity, and has no recovery path #3810

Closed

Description

@schlessera

Context

A hosted instance refused every chat turn after a restart. The server log said turn failed code="BACKEND_ERROR" … error="Safety recording requires recovery", and the PWA showed only "The brain refused this message." (packages/ui-react/src/stores/draft-state.ts:902). The root cause could not be traced afterwards:

  • safety_meta said failed=1, reason='capacity', but usage was 304 used rows of a 1,000,000-row limit (SAFETY_LIMITS, packages/ui-server/src/governance/safety-store.ts:10-12). No limit was anywhere near breached.
  • One safety_collections row carried failure='capacity'. Its turn started more than two hours before the restart and was later closed with gap='host_restart_unknown'. The latch was therefore written while the previous process was running, and the restart only exposed it (beginSafetyCollection reads the durable flag, safety-store.ts:76).
  • The error that caused the latch was never stored or logged. That process's logs went with its container, and nothing durable recorded the cause.

The code paths that make this untraceable:

  1. Any error is classified as capacity. safetyBegin (packages/ui-server/src/governance/event-writes.ts:32) and safetyReserve (event-writes.ts:43) call latchSafety(…, "capacity") for every error that is not ChatRetiredError. That includes SQLite errors, bugs, and validation failures such as "Invalid bounded safety identity" (safety-store.ts:72-74). record and safetyCommit (event-writes.ts:30, :47) at least distinguish SafetyUnavailable from other errors.
  2. A bare catch {} at boot. migrateSafetyHistory (safety-store.ts:334-348) runs on every database open (packages/ui-server/src/db/client.ts:42). Any failure appending a legacy row latches capacity and discards the error (safety-store.ts:346).
  3. The latch keeps no cause. latchSafety (safety-store.ts:37-44) stores only reason and the turn's failure: no error name, message, call site or time. Nothing is logged when it fires.
  4. No recovery path. recoverSafetyRecording (safety-store.ts:376-395) exists, but nothing in production calls it. Recovering meant stopping the server, running a hand-written script against the database, and restarting.
  5. The client cannot tell this refusal apart. A turn refused because recording is latched reaches the user as a generic refusal. The user cannot see that the host needs operator recovery.

Related: #3596 (taint recording and review).

Out of scope

  • Changing the fail-closed policy. A latched recorder still refuses turns.
  • Changing SAFETY_LIMITS or what counts as a real capacity breach.
  • Root-causing this specific incident. The cause is gone; this issue makes the next one traceable.
  • The unrelated WebSocket target.sessionId parse rejections seen in the same log.
  • Log retention of a particular deployment.

Scope

  • Classify honestly. capacity is recorded only when a capacity check refused the write. Every other error latches recording_failed. This covers the four facades in event-writes.ts and migrateSafetyHistory.
  • Keep the cause durably. On the first latch, store the error class, message, a bounded stack, the call site (safetyBegin, safetyReserve, safetyCommit, record, migrate, boot-pending, …), the turn id and the time, in the database next to safety_meta. Later latches increment a counter and do not overwrite the first cause. Store no message content: identifiers and error text only.
  • Log at latch time. Write one structured stderr line with the same fields when the latch fires.
  • Surface it. The authenticated owner status (/api/status) shows whether recording is latched, since when, its reason and its first cause. The refused turn's error frame carries a distinct code, so the PWA can say recording is paused and needs host recovery.
  • Operator recovery. Add an owner-authenticated way to run recoverSafetyRecording inside the running server, with its existing preconditions and refusals. It records who recovered, when, and the cause it cleared. No restart is needed afterwards.

Acceptance criteria

  • A non-capacity error thrown inside safetyBegin, safetyReserve or migrateSafetyHistory latches recording_failed, not capacity. Shown by tests that fail on the current tree.
  • After any latch, the database holds the first cause (class, message, call site, turn id, time) and a latch count. A second latch leaves the first cause unchanged. Both are tested.
  • Each latch writes exactly one structured stderr line containing the call site and error message. Asserted in a test.
  • Owner /api/status reports latched, since, reason and cause. An unauthenticated request does not see them.
  • A turn refused by a latched recorder sends an error frame with a dedicated code, and the PWA shows copy that says host recovery is needed instead of "The brain refused this message."
  • The operator recovery succeeds on an idle latched host, and turns are accepted immediately afterwards without a restart. It refuses, without changing state, while a producer or collection is open. The recovery is recorded with actor, time and cleared cause.
  • recoverSafetyRecording has a production caller.

Verification

  • Unit tests in packages/ui-server/tests for classification, cause persistence, the log line and recovery, each shown failing first and with a mutation receipt.
  • A contract test for the status field and the new error code.
  • A browser test for the PWA copy on the new error code.

Contract impact

Additive. There is a new /api/status field, a new WebSocket error code and a new recovery route. Each needs a CONTRACT: commit and an update to the integration-contract component it touches.

Activity

  1. added
    type: fix
    area: ui-server
    area: ui-react
    priority: p1
    agent-ready
    on Nov 9, 2026
  2. added this to the 0.45.0 milestone on Nov 9, 2026
  3. self-assigned this
    on Nov 10, 2026
  4. schlessera commented on Nov 10, 2026

    @schlessera
    OwnerAuthor

    Claimed (claim-3812-1794279863). Working #3812 and #3810 together on branch fix/3812-proportionate-safety-recording, aiming for one PR that closes both. #3812's restrict-before-refuse ruling supersedes #3810's out-of-scope line "A latched recorder still refuses turns"; #3810's cause recording, honest classification, status surfacing, distinct error code, PWA copy and owner recovery fallback stay in scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: ui-reactarea: ui-serverpriority: p1type: fix

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions