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

A chat stays continuable after the login that started it is pruned (#3962). Every owner sign-in is its own principal, and cleanup deletes a login 30 days after it expires or is revoked; the chat's safety history then named a principal nobody could vouch for, so every later turn was refused as a safety principal mismatch. The host now stores the account with the safety history when it writes it, from the principal's own row, and another owner login keeps continuing, reading and reviewing that history and its taint. The stored account is no credential: a pruned, expired or revoked login regains nothing, another account is still refused, corrupt or contradictory evidence is refused, and history whose login was already pruned before upgrading stays unknown. Attribution and safety records are not rewritten.
30 changes: 28 additions & 2 deletions docs/decisions/session-safety-reviews.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,40 @@ 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.
unresolvable principal matches no other id, unless its record stored its
account (#3962, below). The decision trigger
(`089_safety_durable_account.sql`, replacing 087's) 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.

### A chat's safety history outlives its login (#3962)

Ruled by the maintainer on 2026-11-11 (option A). Principal rows do not last:
cleanup deletes a login 30 days after it expires or is revoked, and the chat
that login started would then refuse every later login. So the host stores
the account with the safety history instead of keeping login rows longer.
`safety_sessions`, `safety_collections` and `dialogue_owners` carry
`principal_account` beside `principal_id`, written by the server from the
principal's own row when the record is written (`principalAccount` in
`account-partition.ts`): `owner` for an owner login, empty for a principal
that is only itself. A request, a client or a model never supplies it.

`sameAccount` reads a record's stored account only when its principal row is
gone. A live row decides by itself, and a live row that disagrees with the
stored account, or a stored value no host writes, makes the record match
nobody, its own principal included; that refusal stays within the record's
chat. The stored account is evidence, not a credential: the caller is passed
as a bare id and proves its account by its own current row, so a pruned,
expired or revoked login regains nothing. History written before 089 gained an
account only where a principal row still proved it at upgrade; proof already
deleted is not reconstructed, and that history stays unknown. Principal
retention is unchanged, and nothing reaches the provider-facing conversation.
The same stored account is the ownership evidence a voice conversation's
dialogue owner carries.

## Human authority and policy publication

Initially **only a live password-mode human owner** may commit a verdict or
Expand Down
4 changes: 3 additions & 1 deletion docs/http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,9 @@ 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
itself, and a missing or unresolvable principal matches no other id, unless
the history recorded the owner account beside it when it was written: an owner
login's history stays the owner's after that login is pruned (#3962). 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.
Expand Down
18 changes: 18 additions & 0 deletions docs/integration-contract/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,24 @@ 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="durable-safety-account-additive-3962"></a>

### A chat's safety history outlives the login that started it (additive, #3962)

The same turn, safety read and owner review keep accepting another owner
login after the login that wrote the history has been pruned. The host stores
each safety record's account beside its principal when it writes it, from the
principal's own row, so a pruned owner login's record still names the owner
account. That stored account is never a credential: the caller is still a
current, authorized login, and a pruned, expired or revoked principal gains
nothing. A record written before this change, whose principal is already
gone, stays unknown and refused. Stored evidence that is corrupt, or that a
live principal row contradicts, matches no principal at all, its own
included. Nothing previously accepted is refused except a record whose stored
evidence is contradictory, which no released host writes, and attribution,
records, verdicts, gaps and generations are not rewritten
([decision](../decisions/session-safety-reviews.md#a-chats-safety-history-outlives-its-login-3962)).

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

### Root server warning (additive, #1638)
Expand Down
82 changes: 82 additions & 0 deletions packages/ui-server/migrations/089_safety_durable_account.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
-- A chat's safety history keeps the account that owned it after the login
-- that wrote it is pruned (#3962). Principal rows do not last: cleanup deletes
-- a login 30 days after it expires or is revoked, and an owner login then
-- became an id nobody could vouch for, so no later login could continue the
-- chat it started.
--
-- `principal_account` sits beside each record's `principal_id`. The server
-- writes it from the principal row when the record is written: 'owner' for an
-- owner login, NULL for a principal whose account is only itself (an agent,
-- an ambient identity, a system principal). `principal_id` stays the
-- attribution and is never rewritten. The account is evidence, not a
-- credential: the caller still has to be a current, authorized login.
--
-- Legacy rows gain an account only from verified evidence: a principal row
-- that still exists and is an owner login. A row whose principal is already
-- gone keeps NULL and stays unknown. Nothing is owner by default, and no
-- record, verdict, gap or generation changes.
ALTER TABLE safety_sessions ADD COLUMN principal_account TEXT;
ALTER TABLE safety_collections ADD COLUMN principal_account TEXT;
ALTER TABLE dialogue_owners ADD COLUMN principal_account TEXT;
UPDATE safety_sessions SET principal_account='owner'
WHERE principal_id IN (SELECT id FROM principals WHERE kind='owner');
UPDATE safety_collections SET principal_account='owner'
WHERE principal_id IN (SELECT id FROM principals WHERE kind='owner');
UPDATE dialogue_owners SET principal_account='owner'
WHERE principal_id IN (SELECT id FROM principals WHERE kind='owner');

-- 081's discovery trigger inserted by position; name the columns now.
DROP TRIGGER IF EXISTS safety_catalog_discovery;
CREATE TRIGGER safety_catalog_discovery AFTER INSERT ON sessions BEGIN
INSERT INTO safety_sessions(session_id,history_owner_id,generation,principal_id,origin_turn,classification,backend_id)
VALUES(NEW.id,lower(hex(randomblob(16))),'1',NULL,NULL,'discovered',NEW.backend_id);
UPDATE safety_meta SET scope_rows=scope_rows+1,scope_bytes=scope_bytes+2048 WHERE singleton=1;
END;

-- Same account, as `sameAccount` in `src/middleware/account-partition.ts`
-- decides it. A record's evidence is consistent when its account is NULL, or
-- 'owner' with no principal row that says otherwise. Corrupt or
-- contradictory evidence matches nobody, not even its own principal. A
-- consistent record matches its exact principal, and an owner record (its
-- account is 'owner', or it has none and its principal row is an owner login)
-- matches any owner login. Every other boundary of 087 is unchanged.
-- Comparisons use IS: inside the dialogue owner's NOT EXISTS a NULL would
-- skip the rejection and admit the verdict, so no term there may be NULL.
DROP TRIGGER IF EXISTS 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_account IS NULL OR (s.principal_account IS 'owner'
AND NOT EXISTS (SELECT 1 FROM principals WHERE id=s.principal_id AND kind<>'owner')))
AND (s.principal_id=NEW.principal_id OR (
(s.principal_account IS 'owner' 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_account IS NULL OR (c.principal_account IS 'owner'
AND NOT EXISTS (SELECT 1 FROM principals WHERE id=c.principal_id AND kind<>'owner')))
AND (c.principal_id=NEW.principal_id OR (
(c.principal_account IS 'owner' 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) IS NOT NEW.generation
OR NOT (d.principal_account IS NULL OR (d.principal_account IS 'owner'
AND NOT EXISTS (SELECT 1 FROM principals WHERE id=d.principal_id AND kind<>'owner')))
OR NOT (d.principal_id IS NEW.principal_id OR (
(d.principal_account IS 'owner' 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;
23 changes: 12 additions & 11 deletions packages/ui-server/src/governance/safety-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ export class SafetyHistory {
if(this.closed)throw new SafetyReadUnavailable();
if(!this.valid(principal))throw new SafetyReadUnavailable("unauthorized");
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;
// 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&&(!sameAccount(this.db,owner.principal_id,principal.id)||String(owner.generation)!==s.generation))throw new SafetyReadUnavailable("generation_changed");
const s=this.db.query("SELECT session_id,history_owner_id,generation,principal_id,principal_account FROM safety_sessions WHERE session_id=?").get(target.sessionId) as
{session_id:string;history_owner_id:string;generation:string;principal_id:string|null;principal_account:string|null}|null;
// Another login of the chat's account reads it; nobody else does
// (#3952). The stored account outlives the login that wrote it (#3962).
if(!s||!sameAccount(this.db,{principalId:s.principal_id,account:s.principal_account},principal.id))throw new SafetyReadUnavailable();
const owner=this.db.query("SELECT principal_id,principal_account,generation FROM dialogue_owners WHERE id=?").get(s.history_owner_id) as {principal_id:string;principal_account:string|null;generation:number}|null;
if(owner&&(!sameAccount(this.db,{principalId:owner.principal_id,account:owner.principal_account},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||!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();
const c=this.db.query("SELECT session_id,principal_id,principal_account,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;principal_account:string|null;history_owner_id:string;generation:string}|null;
if(!c||!sameAccount(this.db,{principalId:c.principal_id,account:c.principal_account},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||!sameAccount(this.db,owner.principal_id,principal.id)))throw new SafetyReadUnavailable("generation_changed");
const owner=this.db.query("SELECT generation,principal_id,principal_account FROM dialogue_owners WHERE id=?").get(c.history_owner_id) as {generation:number;principal_id:string;principal_account:string|null}|null;
if(owner&&(String(owner.generation)!==c.generation||!sameAccount(this.db,{principalId:owner.principal_id,account:owner.principal_account},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
21 changes: 12 additions & 9 deletions packages/ui-server/src/governance/safety-reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,30 @@ function receipt(body: SafetyReviewBody, requestId: string | null, operationId:
verdict: committed ? "committed" : "unresolved", projection, reason };
}
/** Unknown/foreign identity never creates a session or reveals its projection.
* A chat belongs to its account: any owner login reviews it (#3952). */
* A chat belongs to its account: any owner login reviews it (#3952), after
* the login that wrote it is pruned too (#3962). */
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 || !sameAccount(db, s.principal_id, principalId)) return "unavailable";
const s = db.query(`SELECT principal_id,principal_account 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; principal_account: string | null } | null;
if (!s || !sameAccount(db, { principalId: s.principal_id, account: s.principal_account }, 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 || !sameAccount(db, owner.principal_id, principalId))) return "unavailable";
const owner = db.query("SELECT generation,principal_id,principal_account FROM dialogue_owners WHERE id=?").get(canonical) as
{ generation: number; principal_id: string; principal_account: string | null } | null;
if (owner && (String(owner.generation) !== body.session.historyGeneration
|| !sameAccount(db, { principalId: owner.principal_id, account: owner.principal_account }, principalId))) return "unavailable";
return null;
}
function eligible(db: Database, body: SafetyReviewBody, actor: SafetyReviewer): string | null {
const r = db.query(`SELECT c.turn_id,c.principal_id,c.generation,r.state,COALESCE((SELECT x.state FROM safety_records x
const r = db.query(`SELECT c.turn_id,c.principal_id,c.principal_account,c.generation,r.state,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) final_state
FROM safety_records r JOIN safety_collections c ON c.id=r.collection_id
WHERE r.id=? AND r.kind='occurrence'`)
.get(body.occurrenceId) as { turn_id: string; principal_id: string | null; generation: string; final_state: string } | null;
.get(body.occurrenceId) as { turn_id: string; principal_id: string | null; principal_account: 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 || !sameAccount(db, r.principal_id, actor.principalId) || r.generation !== body.session.historyGeneration || !safetyScopeTurns(db, null, body.session.sessionId).has(r.turn_id)
if (!r || !sameAccount(db, { principalId: r.principal_id, account: r.principal_account }, 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