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/3952-safety-account-partition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@schlessera/brain-ui-server": minor
---

The owner can continue a chat from another device (#3952). Every sign-in mints its own owner principal, and a chat's safety history was bound to the one that started it, so a turn from any other login was refused as a safety principal mismatch. The history now belongs to the account: any owner login continues the chat, its turns join the same safety history and taint, and it can read and review that history. An agent, a proxy user or any other account is still refused. Stored rows are not rewritten.
19 changes: 19 additions & 0 deletions docs/decisions/session-safety-reviews.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ quiescence precondition. Only a capacity check classifies a failure as
`capacity`. No gap ever clears, and nothing here lets an unrecorded input
become clean.

### A chat's safety history belongs to its account (#3952)

Ruled by the maintainer on 2026-11-10. The owner mints a principal at every
sign-in, so a chat's safety history binds to the account, not to the login
that started it: another device's owner login continues the chat, reads its
history and reviews its occurrences, and its turns join the same history and
taint. `sameAccount` (`packages/ui-server/src/middleware/account-partition.ts`)
decides it. A principal id always matches itself. Two different ids match
only when both resolve to principals whose `accountScope` is the same
partition in every auth mode, which today means two owner logins. An agent,
ambient or system principal therefore matches only itself, and a missing or
unresolvable principal (a pruned row, say) matches no other id. The decision
trigger (`087_safety_account_partition.sql`) applies the same rule.

Rows keep the principal that wrote them; nothing is rewritten. What belongs to
one login stays exact: a turn's own collection, its catalog creation, read
snapshots and cursors, review handles and their receipts, per-principal
limits, and the principal a verdict names.

## Human authority and policy publication

Initially **only a live password-mode human owner** may commit a verdict or
Expand Down
13 changes: 9 additions & 4 deletions docs/http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ client code has a gap. Source owners are listed after the table.
| GET | `/api/journal/configuration-coverage` | S | Read the brain's current untraced configuration-input set | Latest complete inventoried turn only; explicit `unknown`, never an empty list. |
| GET | `/api/agent-policy` | I | Read shared admission policy and reported enforcing limits | Read-only Devices & agents facts; no editor or duplicate policy cache. |
| GET | `/api/policy-proposals/:turnId/:requestId` | I | Read one principal-owned policy request receipt | Same-request reconciliation only; never writes or reactivates approval. |
| GET | `/api/sessions/:id/safety` | S | Read bounded revisioned host safety evidence | Exact authenticated history owner; immutable records, snapshot aggregate/high-water, scoped cursor, explicit coverage/unavailable, no mutation (#3604). |
| GET | `/api/sessions/:id/safety` | S | Read bounded revisioned host safety evidence | Authenticated principal of the history owner's account (#3952); immutable records, snapshot aggregate/high-water, scoped cursor, explicit coverage/unavailable, no mutation (#3604). |
| POST | `/api/sessions/:id/safety/reviews/prepare` | S | Prepare review of one finalized safety occurrence | Live password-mode human owner, exact Origin and bounded JSON; host-issued body/brain/principal/generation-bound handle (#3603). |
| POST | `/api/sessions/:id/safety/reviews/:requestId/commit` | S | Commit or read back one individual safe verdict | Live owner rechecked on the acquired FULL transaction and final response; immutable receipt plus fresh aggregate, no bulk clear or standing rule (#3603). |
| GET | `/api/sessions/:id/attachment-reads` | S | Read a session's recorded attachment reads, outcome and scope only | Sent-row read states (#3589, #3155); preserve the session and message-reference filters, no text and no provider path. |
Expand Down Expand Up @@ -2057,9 +2057,14 @@ and `detailPruned`. Responses are `Cache-Control: no-store` and at most 262144
UTF-8 bytes. No request invokes a verdict, policy writer, backend turn or
provider transcript write.

The exact live principal must own the retained history. There is no equivalence
between separate owner logins, delegated credentials or ambient identities.
The existing native-history and dialogue APIs keep their visibility rules.
The live principal must belong to the retained history's account (#3952). A
principal id always matches itself. Two different ids match only when both
resolve to the same account partition in every auth mode, which today means
two owner logins; a delegated, ambient or system principal matches only
itself, and a missing or unresolvable principal matches no other id. A cursor
stays with the one login that read it: another login's cursor is refused as
expired (409). The existing native-history and dialogue APIs keep their
visibility rules.
Unknown/unowned legacy safety collections return explicit unavailable; known
legacy collection gaps never imply normal. Revoked/expired callers receive
401, malformed queries 400, expired/generation-changed or membership-gap cursors 409, capacity
Expand Down
17 changes: 17 additions & 0 deletions docs/integration-contract/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,21 @@ retryable: true }`, changing nothing, while any safety producer or collection
is open. Each recovery is recorded with its trigger, actor, time and the cause
it cleared. Affected chats keep their gaps.

<a id="account-bound-safety-history-additive-3952"></a>

### A chat's safety history belongs to its account (additive, #3952)

A turn, a safety read (`GET /api/sessions/:id/safety`, WS `safety_read`) and
an owner review of a chat now accept any principal of the chat's account, not
only the principal that started it. A principal id always matches itself.
Two different ids match only when both resolve to the same account partition,
which today means two owner logins, so an agent, ambient or system principal
matches only itself; a missing or unresolvable principal matches no other id.
Another account's principal is refused exactly as before. Read
snapshots, review handles, receipts and the verdict's `principalId` stay bound
to the exact principal. Nothing previously accepted is refused, and stored
rows are not rewritten ([decision](../decisions/session-safety-reviews.md#a-chats-safety-history-belongs-to-its-account-3952)).

<a id="root-server-warning-additive-1638"></a>

### Root server warning (additive, #1638)
Expand Down Expand Up @@ -472,6 +487,8 @@ Queued continuations retain that snapshot without recopying source evidence or
importing later source activity. Actual catalog creator, principal,
history owner, aliases and generation govern binding. Retired session/owner/turn
identities refuse late callbacks even after private detail is removed.
The principal binds as its account
([additive, #3952](#account-bound-safety-history-additive-3952)).

Effective admission is restricted for any unresolved occurrence, incomplete or
unknown coverage, pending recording, failure or exhausted capacity. Only a
Expand Down
42 changes: 42 additions & 0 deletions packages/ui-server/migrations/087_safety_account_partition.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- A chat's safety history belongs to its account, not to the one login that
-- started it (#3952). The owner mints a principal at every sign-in, so another
-- device's owner login may review the chat's occurrences too. The verdict
-- still names the exact principal that gave it.
--
-- Same account, as `sameAccount` in `src/middleware/account-partition.ts`
-- decides it: a principal id always matches itself, and two different ids
-- match only when both resolve to owner logins. An agent, ambient or system
-- principal matches only itself, and a missing or unresolvable principal
-- matches no other id. Every other boundary of 084 is unchanged, and no
-- existing row is rewritten.
DROP TRIGGER safety_decision_identity;
CREATE TRIGGER safety_decision_identity BEFORE INSERT ON safety_decisions
WHEN NOT EXISTS (
SELECT 1 FROM safety_records r JOIN safety_collections c ON c.id=r.collection_id
JOIN safety_sessions s ON s.session_id=NEW.session_id
WHERE r.id=NEW.occurrence_id AND r.kind='occurrence'
AND s.history_owner_id=NEW.history_owner_id AND s.generation=NEW.generation
AND (s.principal_id=NEW.principal_id OR (
EXISTS (SELECT 1 FROM principals WHERE id=s.principal_id AND kind='owner')
AND EXISTS (SELECT 1 FROM principals WHERE id=NEW.principal_id AND kind='owner')))
AND (c.principal_id=NEW.principal_id OR (
EXISTS (SELECT 1 FROM principals WHERE id=c.principal_id AND kind='owner')
AND EXISTS (SELECT 1 FROM principals WHERE id=NEW.principal_id AND kind='owner')))
AND c.generation=s.generation
AND (c.session_id=s.session_id OR (c.generation=s.generation AND (
c.history_owner_id=COALESCE((SELECT owner_id FROM dialogue_aliases WHERE id=s.history_owner_id),s.history_owner_id)
OR c.history_owner_id IN (SELECT id FROM dialogue_aliases WHERE owner_id=
COALESCE((SELECT owner_id FROM dialogue_aliases WHERE id=s.history_owner_id),s.history_owner_id)))))
AND NOT EXISTS (SELECT 1 FROM safety_retired_turns WHERE turn_id=c.turn_id)
AND NOT EXISTS (SELECT 1 FROM chat_retired_identities WHERE kind='session' AND id=s.session_id)
AND NOT EXISTS (SELECT 1 FROM chat_retired_identities WHERE kind='dialogue_owner' AND id=s.history_owner_id)
AND NOT EXISTS (SELECT 1 FROM chat_retired_identities WHERE kind='dialogue_owner' AND id=
COALESCE((SELECT owner_id FROM dialogue_aliases WHERE id=s.history_owner_id),s.history_owner_id))
AND NOT EXISTS (SELECT 1 FROM dialogue_owners d WHERE d.id=
COALESCE((SELECT owner_id FROM dialogue_aliases WHERE id=s.history_owner_id),s.history_owner_id)
AND (CAST(d.generation AS TEXT)!=NEW.generation OR NOT (d.principal_id=NEW.principal_id OR (
EXISTS (SELECT 1 FROM principals WHERE id=d.principal_id AND kind='owner')
AND EXISTS (SELECT 1 FROM principals WHERE id=NEW.principal_id AND kind='owner')))))
AND COALESCE((SELECT x.state FROM safety_records x WHERE x.occurrence_id=r.id AND x.kind='exposure'
ORDER BY length(x.sequence) DESC,x.sequence DESC LIMIT 1),r.state)='finalized'
) BEGIN SELECT RAISE(ABORT,'safety decision identity or exposure mismatch'); END;
10 changes: 6 additions & 4 deletions packages/ui-server/src/governance/safety-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Database } from "bun:sqlite";
import type { SafetyIdentity, SafetyTarget, SafetyPage, SafetyRecord, SafetyUpdate, SafetyUnavailable as Unavailable } from "@schlessera/brain-ui-sdk/protocol";
import { safetyPageSchema, safetyUpdateSchema } from "@schlessera/brain-ui-sdk/schemas";
import type { Principal } from "../db/principals.js";
import { sameAccount } from "../middleware/account-partition.js";
import { assertLive } from "../ws/chat-lifecycle.js";
import { safetyProjection } from "./safety-store.js";
import { safetyQueue } from "./safety-queue.js";
Expand Down Expand Up @@ -45,18 +46,19 @@ export class SafetyHistory {
if("sessionId" in target){
const s=this.db.query("SELECT session_id,history_owner_id,generation,principal_id FROM safety_sessions WHERE session_id=?").get(target.sessionId) as
{session_id:string;history_owner_id:string;generation:string;principal_id:string|null}|null;
if(!s||s.principal_id!==principal.id)throw new SafetyReadUnavailable();
// Another login of the chat's account reads it; nobody else does (#3952).
if(!s||!sameAccount(this.db,s.principal_id,principal.id))throw new SafetyReadUnavailable();
const owner=this.db.query("SELECT principal_id,generation FROM dialogue_owners WHERE id=?").get(s.history_owner_id) as {principal_id:string;generation:number}|null;
if(owner&&(owner.principal_id!==principal.id||String(owner.generation)!==s.generation))throw new SafetyReadUnavailable("generation_changed");
if(owner&&(!sameAccount(this.db,owner.principal_id,principal.id)||String(owner.generation)!==s.generation))throw new SafetyReadUnavailable("generation_changed");
try{assertLive(this.db,[["session",s.session_id],["dialogue_owner",s.history_owner_id]]);}catch{throw new SafetyReadUnavailable();}
return {sessionId:s.session_id,historyOwnerId:s.history_owner_id,historyGeneration:s.generation};
}
const c=this.db.query("SELECT session_id,principal_id,history_owner_id,generation FROM safety_collections WHERE id=? AND turn_id=?").get(target.bindingId,target.turnId) as
{session_id:string|null;principal_id:string|null;history_owner_id:string;generation:string}|null;
if(!c||c.principal_id!==principal.id||this.db.query("SELECT 1 FROM safety_retired_turns WHERE turn_id=?").get(target.turnId))throw new SafetyReadUnavailable();
if(!c||!sameAccount(this.db,c.principal_id,principal.id)||this.db.query("SELECT 1 FROM safety_retired_turns WHERE turn_id=?").get(target.turnId))throw new SafetyReadUnavailable();
if(c.session_id)return this.authorize(principal,{sessionId:c.session_id});
const owner=this.db.query("SELECT generation,principal_id FROM dialogue_owners WHERE id=?").get(c.history_owner_id) as {generation:number;principal_id:string}|null;
if(owner&&(String(owner.generation)!==c.generation||owner.principal_id!==principal.id))throw new SafetyReadUnavailable("generation_changed");
if(owner&&(String(owner.generation)!==c.generation||!sameAccount(this.db,owner.principal_id,principal.id)))throw new SafetyReadUnavailable("generation_changed");
try{assertLive(this.db,[["dialogue_owner",c.history_owner_id]]);}catch{throw new SafetyReadUnavailable();}
return {turnId:target.turnId,bindingId:target.bindingId};
}
Expand Down
10 changes: 6 additions & 4 deletions packages/ui-server/src/governance/safety-reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Database } from "bun:sqlite";
import { z } from "zod";
import { safetyReviewBodySchema as bodySchema } from "@schlessera/brain-ui-sdk/schemas";
import { isUsablePrincipal, resolvePrincipal, type Principal } from "../db/principals.js";
import { sameAccount } from "../middleware/account-partition.js";
import { assertLive, ChatRetiredError } from "../ws/chat-lifecycle.js";
import { advance as advanceSafetyRevision, safetyProjection, safetyScopeTurns, SafetyUnavailable, type SafetyProjection } from "./safety-store.js";

Expand Down Expand Up @@ -57,16 +58,17 @@ function receipt(body: SafetyReviewBody, requestId: string | null, operationId:
stage: committed ? "verdict_committed" : reason ? "refused" : "prepared",
verdict: committed ? "committed" : "unresolved", projection, reason };
}
/** Unknown/foreign identity never creates a session or reveals its projection. */
/** Unknown/foreign identity never creates a session or reveals its projection.
* A chat belongs to its account: any owner login reviews it (#3952). */
export function safetyReviewTarget(db: Database, body: SafetyReviewBody, principalId: string): "retired" | "unavailable" | null {
const s = db.query(`SELECT principal_id FROM safety_sessions WHERE session_id=? AND history_owner_id=? AND generation=?`)
.get(body.session.sessionId, body.session.historyOwnerId, body.session.historyGeneration) as { principal_id: string | null } | null;
if (!s || s.principal_id !== principalId) return "unavailable";
if (!s || !sameAccount(db, s.principal_id, principalId)) return "unavailable";
const canonical = (db.query("SELECT owner_id FROM dialogue_aliases WHERE id=?").get(body.session.historyOwnerId) as {owner_id:string}|null)?.owner_id ?? body.session.historyOwnerId;
try { assertLive(db, [["session", body.session.sessionId], ["dialogue_owner", body.session.historyOwnerId], ["dialogue_owner",canonical]]); }
catch (error) { if (error instanceof ChatRetiredError) return "retired"; throw error; }
const owner = db.query("SELECT generation,principal_id FROM dialogue_owners WHERE id=?").get(canonical) as { generation: number; principal_id: string } | null;
if (owner && (String(owner.generation) !== body.session.historyGeneration || owner.principal_id !== principalId)) return "unavailable";
if (owner && (String(owner.generation) !== body.session.historyGeneration || !sameAccount(db, owner.principal_id, principalId))) return "unavailable";
return null;
}
function eligible(db: Database, body: SafetyReviewBody, actor: SafetyReviewer): string | null {
Expand All @@ -77,7 +79,7 @@ function eligible(db: Database, body: SafetyReviewBody, actor: SafetyReviewer):
.get(body.occurrenceId) as { turn_id: string; principal_id: string | null; generation: string; final_state: string } | null;
// The aggregate also includes adopted provisional collections. Use its
// canonical membership; a direct session_id filter strands those events.
if (!r || r.principal_id !== actor.principalId || r.generation !== body.session.historyGeneration || !safetyScopeTurns(db, null, body.session.sessionId).has(r.turn_id)
if (!r || !sameAccount(db, r.principal_id, actor.principalId) || r.generation !== body.session.historyGeneration || !safetyScopeTurns(db, null, body.session.sessionId).has(r.turn_id)
|| db.query("SELECT 1 FROM safety_retired_turns WHERE turn_id=?").get(r.turn_id)) return "unavailable";
if (db.query("SELECT 1 FROM safety_decisions WHERE occurrence_id=?").get(body.occurrenceId)) return "already_resolved";
return r.final_state === "finalized" ? null : "not_finalized";
Expand Down
Loading
Loading