Skip to content

CONTRACT: fix(ui-server): keep a chat's safety history with its account after the starting login is pruned (#3962) - #4128

Merged
schlessera merged 4 commits into
mainfrom
fix/3962-durable-safety-ownership
Nov 17, 2026
Merged

schlessera merged 4 commits into
mainfrom
fix/3962-durable-safety-ownership

Conversation

@schlessera

@schlessera schlessera commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Closes #3962

What changed

A chat now stays continuable after the owner login that started it is pruned. Principal cleanup deletes a login 30 days after it expires or is revoked (prunePrincipals). The chat's safety records then named a principal nobody could vouch for, so #3952's sameAccount treated it as unknown and refused every later turn by every login (Safety principal mismatch, or Safety history ownership mismatch once the chat had a voice owner), for good. Under the maintainer's ruling (option A, 2026-11-11), the host now stores the account with the safety history. Migration 089 adds principal_account beside principal_id on safety_sessions, safety_collections and dialogue_owners. The server writes it from the principal's own row when it writes the record (principalAccount): owner for an owner login, NULL for a principal that is only itself. Another owner login keeps continuing, binding, forking, reading and reviewing that history and its taint. principal_id attribution, records, verdicts, gaps and generations are not rewritten.

Check Where Bound evidence passed
Safety history ownership mismatch beginSafetyCollection (dialogue owner) dialogue_owners
Safety principal mismatch beginSafetyCollection safety_sessions
Safety fork history ownership mismatch beginSafetyCollection (fork source) safety_sessions
Safety binding owner mismatch bindSafetyCollection dialogue owner, session, or the catalog owner's live row; and the turn's collection
Safety retained owner mismatch bindSafetyCollection dialogue_owners and the turn's collection
session, turn and dialogue-owner reads SafetyHistory.authorize session, collection, dialogue owner
review target and eligibility safetyReviewTarget, eligible session, dialogue owner, occurrence's collection
safety_decision_identity trigger migration 089 (replaces 087's) session, collection, dialogue owner, in SQL

Why this shape

The rule. sameAccount(db, bound, principal) now takes a record { principalId, account } as well as a bare id. A live principal row decides by itself, exactly as in #3952. The stored account counts only when the row is gone. Evidence that is corrupt (a value no host writes), or that a live row contradicts (owner stored, but the row is an agent), matches nobody, not even its own principal. The caller is always a bare id, so it proves its account only by its own current row. The stored account is evidence, never a credential: a pruned, expired or revoked login regains nothing, and SafetyHistory's valid check and the host's auth still gate the caller. durableAccount is the account accountScope gives in every auth mode (today only owner), so the stored value never depends on the mode the host runs in now, and an ambient, agent or system principal stays only itself.

Columns on the history rows, not a principal→account table. A table keyed by principal id that outlives prunePrincipals is a login tombstone, which is option B and was not selected. Columns on the records keep the evidence with the history, and they disappear when chat deletion removes the history. dialogue_owners gets the column too, because the safety checks compare against the dialogue owner's principal; otherwise any chat with a voice owner would stay stuck after pruning. #3963 (voice continuation across logins) will reuse the same column and rule; this PR does not change voice's exact-principal comparisons.

Legacy rows. 089 sets owner only where a principal row still proves it at upgrade (kind='owner'). A row whose principal is already gone keeps NULL and stays unknown: deleted proof is not reconstructed, and there is no owner-by-default backfill. 081's safety_catalog_discovery trigger inserted by position, so 089 recreates it with a column list.

Contract. This is additive. docs/integration-contract/http.md gains the #3962 section, and docs/http-api.md and the decision record (session-safety-reviews.md) state the rule, so the commit is CONTRACT:. No CLI, MCP or HTTP shape changes. One narrowing is named there: a record with contradictory evidence is refused even for its own principal, but no released host writes such a record. The durable decision record itself is #3978.

Proof

Failing first. The new continuation tests were run against the base code (git checkout origin/main -- packages/ui-server, without 089, the new tests kept):

(fail) after the starting login is revoked and pruned, another owner login continues, binds, reads, forks and reviews the chat
  SafetyUnavailable: Safety principal mismatch
(fail) after the starting login is expired and pruned, another owner login continues, binds, reads, forks and reviews the chat and its voice owner
  SafetyUnavailable: Safety history ownership mismatch
 1 pass / 2 fail

With the change: safety-account-partition.test.ts 17 pass, 0 fail.

Mutations, each restored (cmp against the backup):

Mutation Fails
M1, remove durable ownership: a pruned principal ignores its stored account (account: null) both pruned-continuation tests (Safety principal mismatch, Safety history ownership mismatch) and the sameAccount table (pruned {ship, owner} / home: expected true)
M2, admit an unknown account: an unresolvable principal defaults to owner SAFETY_PRUNED_FOREIGN_REFUSED: unknown-login, SAFETY_PRUNED_LEGACY_OWNER_IS_UNKNOWN, SAFETY_FOREIGN_PRINCIPAL_REFUSED, SAFETY_FOREIGN_FORK_REFUSED, the sameAccount tables (6 tests)
M3, the trigger skips the session's evidence-consistency clause SAFETY_BAD_ACCOUNT_TRIGGER_REFUSED: corrupt

What the tests cover (packages/ui-server/tests/safety-account-partition.test.ts, real revokePrincipal/prunePrincipals path):

  • After revoke and prune, and after expiry and prune with a voice owner, the other owner login is admitted and joins the same history_owner_id. It inherits the unresolved occurrence and restriction, late-binds an unbound turn, reads the chat and the pruned login's turn, forks with the taint, and prepares and commits a review on the pruned login's occurrence through the trigger. Session, collection and record rows are unchanged. The stale pruned Principal object is refused as unauthorized.
  • After pruning, an agent, a proxy user, a system principal, an unknown id and null are refused at begin (with and without a voice owner), reads and review target. A refused turn leaves no collection, and an unrelated chat begins normal.
  • Corrupt (admiral) and contradicted (owner on an agent's row) evidence is refused for another owner login and for the recorded principal itself, at begin, read, review target and the trigger. An unrelated chat stays normal.
  • The account is written from the principal row. A safetyBegin input carrying principalAccount/principal_account/account: "owner" for an agent, a proxy user, a system or an unknown principal stores NULL.
  • Upgrade: the tables are rebuilt as before 089 and the ship login is pruned before the migration. Only the still-live home login's collection gains owner. The ship's session, collection and dialogue owner stay NULL, and the records are unchanged.
  • The legacy rule: with no stored account, a resolvable login continues as before, and once pruned it is refused at begin and read (the ui-server: an owner continuing a chat from another device is refused as a safety principal mismatch #3952 test, updated).

Other suites. Every safety-*, dialogue-*, principal*, chat-deletion*, chat-lifecycle, chat-session-owner, session-routes-*, session-safety-boundaries, owner-safety-review, live-safety-admission, http-principal-boundaries, host-publication-safety, app-writer-cleanup, ws-conversation, ws-chat-deletion and db test passes. Two existing tests needed updates: safety-ledger.test.ts inserted dialogue_owners rows by position (now with a column list), and its upgrade replay now replays 089 with the 081/085 tables it extends.

Provider-history guard. packages/ui-backend-{claude,pi}/tests/provider-history.test.ts cannot run on this host. Its bwrap is refused user namespaces (No permissions to create a new namespace), and it fails identically on the base code. No ownership metadata is on the provider path: the change touches only host SQLite rows and the checks that read them, and hosted CI runs the guard.

bun run typecheck, bun run lint (all gates clean) and bun run build pass.

bun run check:pr --base origin/main
{"version":1,"kind":"brain-check-pr-receipt","head":"b0dec6e6150add5da4865a1f0f71765b8a83f1f9","base":"origin/main","mode":"focused","startedAt":"2026-11-17T02:04:00.791Z","finishedAt":"2026-11-17T02:05:37.624Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":16840.763912000002},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":116.92536000000109},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":109.04503400000249},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":24.874313000000257},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":79722.988186}]}

Independent review follow-up

The review of 1bcb99fda had one finding, fixed in 5a5b7b5d359c8ec1b0b5f7fa3ae40e16c2b3a1a6. Its disposition is posted at the reviewed head. In 089's safety_decision_identity trigger, inside the dialogue owner's NOT EXISTS, d.principal_account='owner' is NULL when the account is NULL. That made the whole rejection term NULL, so a dialogue owner naming a foreign agent or a pruned unknown principal with no stored account did not refuse the verdict.

  • Failing first: the new test the decision trigger refuses a verdict when the chat's dialogue owner is another account or unknown, with no stored account failed on 1bcb99fda with SAFETY_DECISION_TRIGGER_REFUSES_NULL_ACCOUNT_DIALOGUE_OWNER: foreign agent / Received function did not throw.
  • Fix: every account comparison in the trigger uses IS 'owner', and the dialogue owner's principal and generation comparisons use IS / IS NOT, so no term in the negated clause can be NULL.
  • Same-class audit: the session and collection clauses are positive EXISTS conditions, where a NULL only refuses, and they now use IS too. kind<>'owner' inside NOT EXISTS is safe because principals.kind is NOT NULL (011). The TypeScript sameAccount has no three-valued logic.
  • Mutation: restoring d.principal_account='owner' on that one line turns the same test red on the same assertion (17 pass / 1 fail). Restored: safety-account-partition, safety-ledger, owner-safety-review, safety-reviews and safety-history-review give 119 pass / 0 fail.

origin/main was merged in (clean, no rebase).

Proportionate containment (changed guard)

  • Trigger. A safety record whose principal row is gone and that stores no account, or one whose stored account is corrupt or contradicted by a live row, against any principal other than (for the first case) its own id. Not covered: a live, consistent record, which follows ui-server: an owner continuing a chat from another device is refused as a safety principal mismatch #3952 unchanged.
  • Restriction. Only that record's chat history (one turn's admission, one read, one review) is refused. Nothing is latched, and other chats are untouched (SAFETY_REFUSAL_CONTAINED_TO_ITS_CHAT, SAFETY_BAD_ACCOUNT_CONTAINED).
  • Restricted mode considered. Admitting the turn restricted would let an unproven caller extend or read another account's history, so ownership stays a refusal, as before. The turn's existing restriction machinery still applies to admitted turns.
  • Clearing. Owner-proved history clears itself: new writes store the account, and a live row decides immediately. Already-pruned legacy history cannot be recovered without guessing, which the ruling forbids; the owner starts a new chat. That needs no new action.
  • Cause recorded. The existing durable refusal path: the turn is refused before delivery with the safety recording refused one turn before delivery record, naming the check (Safety principal mismatch and so on).
  • Scope test. The two tests named above begin an unrelated chat after the refusal and assert it is normal.

Collisions

bun scripts/collisions.ts --json --pr 4128 at 1bcb99fda: one textual collision, with draft #3622 (6a814ee73) in docs/http-api.md. git merge-tree of #3622 gives 4 conflict hunks in docs/http-api.md against both this head and origin/main, with the same conflicted file set (docs/http-api.md, docs/integration-contract/package-api.md, packages/ui-server/src/app.ts, packages/ui-server/tests/http-inventory.test.ts). They already exist against main, and this PR adds none. Every other report is closure-only (shared test-closure paths, no shared files): #3056, #3059, #3069, #3581, #4002, #4036, #4102, #4106, #4112, #4113, #4115, #4118, #4122, #4123, #4124, #4126. Migration numbering: #4002 adds 089_reference_acquisitions.sql. The runner keys on filename, and the two 089 files touch independent tables, so either order is safe.

Checklist

  • Focused local preflight/behavioral receipts passed; hosted proof runs on the ready candidate.
  • Acceptance criteria from the issue are all met.
  • The PR and ui-server: a chat whose starting owner login was pruned can never be continued #3962 carry milestone 0.45.0.
  • A changeset is included (.changeset/3962-safety-durable-account.md, minor).
  • No personal data anywhere in the diff.
  • The integration contract component is updated in the same CONTRACT: commit.
  • Docs under docs/ reflect the change.
  • The changed guard answers the proportionate-containment checklist above.
  • Provider history stays append-only: nothing on the provider path changed; the guard is env-blocked locally (see Proof) and runs in hosted CI.

Anything left open

🤖 Generated with Claude Code

…nt after the starting login is pruned (#3962)

Principal cleanup deletes a login 30 days after it expires or is revoked.
A chat's safety records then named a principal nobody could vouch for, so
`sameAccount` refused every later login and the chat could never be
continued. Under the maintainer's ruling (option A), the account is stored
with the safety history.

Migration 089 adds `principal_account` beside `principal_id` on
safety_sessions, safety_collections and dialogue_owners. The server writes
it from the principal's own row when it writes the record
(`principalAccount`), never from a request: 'owner' for an owner login,
NULL for a principal that is only itself. Legacy rows gain it only where a
principal row still proves it at upgrade; already-pruned history stays
unknown.

`sameAccount` takes a record's stored account and uses it only when the
principal row is gone. Corrupt evidence, or a stored account a live row
contradicts, matches nobody. The caller is still a bare id that proves its
account by its own current row, so a pruned, expired or revoked login gains
nothing. Admission, fork, binding, reads, review target/eligibility and the
safety_decision_identity trigger all pass the stored evidence. Attribution,
records, verdicts, gaps and generations are not rewritten, and nothing
enters the provider-facing conversation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera schlessera added this to the 0.45.0 milestone Nov 17, 2026
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. contract Touches the integration contract: CONTRACT: commit, contract doc updated in the same commit. labels Nov 17, 2026
@schlessera

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 1bcb99f against base 40779d4. Reviewer: agent. 1 finding: 0 fixed, 0 filed, 0 rejected, 1 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4128,
  "head": "1bcb99fdaafe450c2ef86e47bee2ec71b7c4a309",
  "base": "40779d4983def309fc5a036ce585c581a68839c1",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/migrations/089_safety_durable_account.sql:75: The dialogue-owner rejection evaluates to NULL when principal_account is NULL and its principal is foreign or pruned. NOT EXISTS then ignores that owner, allowing a verdict the ownership guard should refuse. Reproduced with the actual trigger for both a live foreign agent and a pruned unknown principal. Coalesce the account comparison before negating it."
    }
  ]
}

schlessera and others added 2 commits November 17, 2026 02:03
…ccount in the decision trigger (#3962)

Inside the dialogue owner's NOT EXISTS, d.principal_account='owner' is NULL
when the account is NULL, so the whole rejection term was NULL and a
foreign or pruned dialogue owner no longer refused the verdict. Every
account comparison in 089's trigger now uses IS, as do the dialogue
owner's principal and generation comparisons, so no term in a negated
clause can be NULL. The positive session and collection clauses only ever
refused on NULL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head 1bcb99f against base 40779d4. Reviewer: agent. 1 finding: 1 fixed, 0 filed, 0 rejected, 0 without a disposition.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4128,
  "head": "1bcb99fdaafe450c2ef86e47bee2ec71b7c4a309",
  "base": "40779d4983def309fc5a036ce585c581a68839c1",
  "reviewer": "agent",
  "outcome": "findings",
  "findings": [
    {
      "summary": "packages/ui-server/migrations/089_safety_durable_account.sql:75: The dialogue-owner rejection evaluates to NULL when principal_account is NULL and its principal is foreign or pruned. NOT EXISTS then ignores that owner, allowing a verdict the ownership guard should refuse. Reproduced with the actual trigger for both a live foreign agent and a pruned unknown principal. Coalesce the account comparison before negating it.",
      "disposition": "fixed",
      "commit": "5a5b7b5d359c8ec1b0b5f7fa3ae40e16c2b3a1a6"
    }
  ]
}

@schlessera

Copy link
Copy Markdown
Owner Author

Independent review

Reviewed head b0dec6e against base 7dfadc2. Reviewer: agent. No findings.

{
  "version": 1,
  "kind": "brain-review-receipt",
  "pr": 4128,
  "head": "b0dec6e6150add5da4865a1f0f71765b8a83f1f9",
  "base": "7dfadc217c406255935f7fc626594acf2c2e8b9a",
  "reviewer": "agent",
  "outcome": "no-findings",
  "findings": []
}

@schlessera
schlessera marked this pull request as ready for review November 17, 2026 04:08
@schlessera
schlessera merged commit 16dfb9c into main Nov 17, 2026
54 of 56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. contract Touches the integration contract: CONTRACT: commit, contract doc updated in the same commit. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ui-server: a chat whose starting owner login was pruned can never be continued

1 participant