Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Conversation archive pages turn owner-only as soon as a brain becomes shared (#9563)
Garry Tand5e6c4144d64
diff --git a/behavior-changes.d/archive_rescope_on_shared_9563.md b/behavior-changes.d/archive_rescope_on_shared_9563.mdnew file mode 100644index 0000000000..8119345081--- /dev/null+++ b/behavior-changes.d/archive_rescope_on_shared_9563.md@@ -0,0 +1,4 @@+---+kind: fix+---+Conversation archive pages written while a brain was personal are now kept to the owner as soon as a command makes the brain shared: gbrain access member add and member enable (before the new member is added, enabled or bound to a token, so its credentials never read those pages), gbrain config set brain.audience shared and gbrain access owner set. Each prints one line on stderr per archive source saying the pages are kept to the owner or why they still wait. If the re-scope fails, member add and member enable change nothing and exit non-zero. Before, every member could read those pages until the next archive run.diff --git a/changelog.d/archive-rescope-on-shared.md b/changelog.d/archive-rescope-on-shared.mdnew file mode 100644index 0000000000..56355315b3--- /dev/null+++ b/changelog.d/archive-rescope-on-shared.md@@ -0,0 +1,10 @@+---+important: security+---+**Conversation archive pages written while a brain was personal are now kept to the owner as soon as a command makes the brain shared, instead of staying readable by every member until the next archive run.**++<!-- behavior-change: archive_rescope_on_shared_9563 -->++### Fixed+- `gbrain access member add` and `gbrain access member enable` (when they make two or more enabled members), `gbrain config set brain.audience shared` and `gbrain access owner set` now apply the owner-only rules to the conversation archive before they return. `member add` and `member enable` do it before the member is added, enabled or bound to a token, and change nothing if it fails; a `member add` refused for its own input (a bad kind, email or group) changes nothing either. Each command prints one line on stderr per archive source; `--json` output is unchanged. (#9563)+- Still waiting, and named by `gbrain transcripts archive status` and `gbrain doctor`: a brain that turns shared from client usage with no command run (`gbrain transcripts archive run` applies it), a shared brain with no owner (`gbrain access owner set <member>`), and access rules kept in git (add the `conversations/` owner-only rule to `.gbrain/access.yml`). Pages an archive run already in progress writes into new folders stay readable by members until the next run; with maintenance off, that run is manual.diff --git a/docs/guides/access-designations.md b/docs/guides/access-designations.mdindex ca9aa375bb..e453776915 100644--- a/docs/guides/access-designations.md+++ b/docs/guides/access-designations.md@@ -200,6 +200,12 @@ database, so a repo-less brain works the same. The owner relaxation applies to awhen it speaks for itself (no audience, or an audience of the owner alone); never to a shared-room audience or anattested call. `gbrain access owner clear` turns it off.+When `gbrain access owner set`, `gbrain access member add` or `member enable` (a second enabled member), or+`gbrain config set brain.audience shared` leaves the brain shared, the command also keeps the conversation archive+pages written while it was personal to the owner before it returns, and says so on stderr. Three cases wait instead+(the audience turning shared from client usage, a shared brain with no owner, and rules kept in git); see+[when a personal brain becomes shared](conversation-archive.md#when-a-personal-brain-becomes-shared).+**The owner's writes.** The owner's own credential with write scope, speaking for itself (no audience, not anattested call, not a subagent job, no slug fence, checked again when the write is published), may change a private pageit reads with `put_page`, `edit_page` or `append_page`, or with `put_pages` and staged uploads (`begin_page_upload`diff --git a/docs/guides/conversation-archive.md b/docs/guides/conversation-archive.mdindex ca95718b2f..66e3d445bf 100644--- a/docs/guides/conversation-archive.md+++ b/docs/guides/conversation-archive.md@@ -161,8 +161,18 @@ private conversations never land in the shared repository.### When a personal brain becomes shared-Pages archived while the brain was personal name no readers. The first archive run after the brain becomes shared-keeps them to the owner before it writes anything: every folder that holds an archive page at that moment (one per+Pages archived while the brain was personal name no readers. Until they are kept to the owner, every member reads+them. The command that makes the brain shared does that before it returns: `gbrain access member add` or+`gbrain access member enable` when it makes two or more enabled members, `gbrain config set brain.audience shared`,+and `gbrain access owner set` on a brain that is already shared. `member add` and `member enable` do it before the+member is added, enabled or bound to a token, so its credentials never read those pages; if that step fails, the+command changes nothing and exits non-zero. Each prints one line on stderr per archive source,+saying the pages are now kept to the owner or why they still wait (the codes below); `--json` output is unchanged.+When the brain turns shared some other way, the next archive run (`gbrain transcripts archive run`) or maintenance pass+does it, also before it writes anything. That includes members added or enabled from the admin page over HTTP or by+`gbrain access serves several --members <ids> --yes`. A command run while an archive run that started on the personal+brain is still writing the source keeps the pages archived so far to the owner at once; the pages that run still writes+wait for the run after it (`archive_rescope_pending`). Either way, every folder that holds an archive page at that moment (one perharness and day, such as `conversations/codex/2026-10-01/`, plus `conversations/days/`) gets an access rule readableonly by the owner. Other pages in the archive source, outside `conversations/`, keep the rules they had. This re-scope **replaces any existing rule on those folders, including one the owner made wider onpurpose**; re-add the wider readers afterwards (`gbrain access rule add`, which waits for `gbrain access apply`) if@@ -188,6 +198,22 @@ each say so in one line, when the shared brain names no owner (`archive_rescope_`gbrain access owner set <member>`) or when the source keeps its access rules in git (`archive_rescope_git_rules`:add a rule with prefix `conversations/` and the owner as its reader to the source's `.gbrain/access.yml`).+Three ways a brain becomes shared leave those earlier pages open to every member until you act. In each, as in the+paths above that wait for the next archive run,+`gbrain transcripts archive status` prints `not kept to the owner yet (<code>): <what to do>`, its `--json` carries+the same `rescope` object (`code` and `message`), and `gbrain doctor` warns on `transcript_archive_freshness` with that+message, whether the archive is on or off:++- **The audience flips from client usage, with no command.** With `brain.audience` unset, three or more people's MCP+ clients active in 30 days make the brain read as shared. No command runs, so the code is `archive_rescope_pending`+ until `gbrain transcripts archive run` or a maintenance pass runs; with automatic maintenance turned off+ (`maintenance.auto false`) no pass runs, so only the archive run does it. Declaring `gbrain config set brain.audience shared` also runs it at once.+- **A shared brain with no owner.** The re-scope has no one to keep the pages to, so it waits with+ `archive_rescope_owner_unset`. `gbrain access owner set <member>` runs it at once.+- **Rules kept in git.** gbrain never writes a source's `.gbrain/access.yml`, so it waits with+ `archive_rescope_git_rules` until you add the `conversations/` rule there and sync. A repo-less brain keeps its rules+ in the database and never hits this case.+## OpenClawCurrent OpenClaw keeps live sessions in a database owned by its Gateway. gbrain reads them withdiff --git a/src/commands/access.ts b/src/commands/access.tsindex fdb5804524..fe116e1a11 100644--- a/src/commands/access.ts+++ b/src/commands/access.ts@@ -17,6 +17,7 @@ import type { BrainEngine } from '../core/engine.ts';import { exitCliError, usageError } from '../cli/cli-error.ts';import { runAccessRules } from './access-rules.ts';import { runAccessServes } from './access-serves.ts';+import { rescopeArchiveAfter, rescopeArchiveBefore } from './archive-rescope.ts';import { opError } from '../core/ops/contract.ts';import { sqlQueryForEngine } from '../core/sql-query.ts';import { grantFromClient, type PrincipalGrant } from '../core/grants/model.ts';@@ -322,7 +323,8 @@ async function memberCmd(engine: BrainEngine, sub: string | undefined, args: strconst id = positionals(args)[0];switch (sub) {case 'add': {- const r = await addMember(engine, id, { name: flagValue(args, '--name'), email: flagValue(args, '--email'), groups: parseIdList(flagValue(args, '--groups'), 'group'), bindToken: flagValue(args, '--bind-token'), kind: flagValue(args, '--kind'), remote: false });+ const r = await addMember(engine, id, { name: flagValue(args, '--name'), email: flagValue(args, '--email'), groups: parseIdList(flagValue(args, '--groups'), 'group'), bindToken: flagValue(args, '--bind-token'), kind: flagValue(args, '--kind'), remote: false, beforeWrite: () => rescopeArchiveBefore(engine, id, 'add') });+ await rescopeArchiveAfter(engine);return print(json, r, () => [`Member added: ${r.member.id}`, memberLine(r.member),...(r.groups_created.length ? [` Groups created: ${r.groups_created.join(', ')}`] : []),...(r.bound ? [` Bound token: ${r.bound.name} (${r.bound.id})`] : [' Bind a credential: gbrain auth rescope --token <name> --member ' + r.member.id]),@@ -346,7 +348,9 @@ async function memberCmd(engine: BrainEngine, sub: string | undefined, args: str}case 'disable':case 'enable': {+ if (sub === 'enable') await rescopeArchiveBefore(engine, id, 'enable');const m = await (sub === 'disable' ? disableMember : enableMember)(engine, id);+ if (sub === 'enable') await rescopeArchiveAfter(engine);return print(json, { member: m }, () => [`Member ${sub}d: ${m.id}`, memberLine(m),sub === 'disable' ? 'Its credentials are refused on their next request; gbrain access member enable undoes this.' : 'Its credentials read again on their next request.']);}@@ -458,6 +462,7 @@ async function ownerCmd(engine: BrainEngine, sub: string | undefined, args: stri}case 'set': {const v = await setOwner(engine, positionals(args)[0]);+ await rescopeArchiveAfter(engine);return print(json, v, () => ownerLines(v));}case 'clear': {diff --git a/src/commands/archive-rescope.ts b/src/commands/archive-rescope.tsnew file mode 100644index 0000000000..7e534ff910--- /dev/null+++ b/src/commands/archive-rescope.ts@@ -0,0 +1,41 @@+/**+ * The commands that can turn a brain shared (gbrain access member add and member enable, gbrain access owner set,+ * gbrain config set brain.audience) keep conversation archive pages written while the brain was personal to the owner+ * before they return. member add and member enable do it before the member is added or enabled+ * (rescopeArchiveBefore), so the member's credentials never read those pages; every command then records it+ * (rescopeArchiveAfter, rescopeArchiveOnShared).+ */+import type { BrainEngine } from '../core/engine.ts';+import { setCliExitVerdict } from '../core/cli-force-exit.ts';+import { assertAccessId, getMember } from '../core/access/members.ts';++/**+ * Before gbrain access member add or member enable: when the member will be newly enabled, keeps the personal-era archive+ * pages to the owner first. member add calls it from addMember's beforeWrite, after every input check passed, so a+ * refused add changes nothing; member enable calls it for a disabled member only. A failure throws before the member is+ * added, enabled or bound, so nothing changes.+ */+export async function rescopeArchiveBefore(engine: BrainEngine, rawId: unknown, change: 'add' | 'enable'): Promise<void> {+ const id = assertAccessId(rawId);+ if (change === 'enable' && (await getMember(engine, id))?.status !== 'disabled') return;+ try {+ await (await import('../core/transcripts/archive.ts')).rescopeArchiveBeforeJoin(engine, id);+ } catch (e) {+ throw new Error('Nothing was changed: keeping the conversation archive pages written while this brain was personal to the owner failed before ' + id + ' was ' + (change === 'add' ? 'added' : 'enabled') + ' (' + (e instanceof Error ? e.message : String(e)) + '). Fix the cause, then rerun the command.');+ }+}++/** Runs the owner-only archive re-scope and says what happened on stderr, so stdout and --json stay the command's own. */+export async function rescopeArchiveAfter(engine: BrainEngine): Promise<void> {+ let results: Array<{ source_id: string; skipped: { message: string } | null }>;+ try {+ results = await (await import('../core/transcripts/archive.ts')).rescopeArchiveOnShared(engine);+ } catch (e) {+ setCliExitVerdict(1);+ console.error('Conversation archive: keeping the pages written while this brain was personal to the owner failed (' + (e instanceof Error ? e.message : String(e)) + '); until it succeeds every member may read them. Run gbrain transcripts archive run now.');+ return;+ }+ for (const r of results) {+ console.error('Conversation archive (source ' + r.source_id + '): ' + (r.skipped ? r.skipped.message : 'pages written while this brain was personal are now kept to the owner.'));+ }+}diff --git a/src/commands/config.ts b/src/commands/config.tsindex 916e37b737..68d3185b8d 100644--- a/src/commands/config.ts+++ b/src/commands/config.ts@@ -1062,7 +1062,7 @@ export async function runConfig(engine: BrainEngine, args: string[]) {process.exit(1);}console.log(`Set ${key} = ${audience} (${hostPlane ? 'file + db planes' : 'db plane only — mounted brain'})`);- return;+ return (await import('./archive-rescope.ts')).rescopeArchiveAfter(engine);}if (key === 'memory.agent_writeback') return (await import('./config-agent-writeback.ts')).setAgentWritebackFromCli(engine, value);if (MEMORY_DUAL_PLANE_KEYS.has(key)) {diff --git a/src/core/access/members.ts b/src/core/access/members.tsindex 000cfb6f79..72eaed586e 100644--- a/src/core/access/members.ts+++ b/src/core/access/members.ts@@ -206,7 +206,8 @@ export async function legacyTokenByName(engine: BrainEngine, name: string): Promexport interface AddMemberResult { member: MemberRecord; groups_created: string[]; bound: CredentialRef | null }-export async function addMember(engine: BrainEngine, rawId: unknown, opts: { name?: string; email?: unknown; groups?: readonly string[]; bindToken?: string; kind?: unknown; remote?: boolean } = {}): Promise<AddMemberResult> {+/** beforeWrite runs once every input check has passed and before anything is written; a throw from it changes nothing. */+export async function addMember(engine: BrainEngine, rawId: unknown, opts: { name?: string; email?: unknown; groups?: readonly string[]; bindToken?: string; kind?: unknown; remote?: boolean; beforeWrite?: () => Promise<void> } = {}): Promise<AddMemberResult> {const id = assertAccessId(rawId);const kind = opts.kind === undefined ? 'person' : assertMemberKind(opts.kind);if (kind !== 'person') assertLocalKindChange(opts, id, kind);@@ -224,6 +225,7 @@ export async function addMember(engine: BrainEngine, rawId: unknown, opts: { nam}if (email !== null) await assertEmailFree(engine, email, id);const bindTarget = opts.bindToken ? await legacyTokenByName(engine, opts.bindToken) : null;+ await opts.beforeWrite?.();let created: string[] = [];await engine.transaction(async tx => {await tx.executeRaw('INSERT INTO access_members (id, display_name, email, kind) VALUES ($1, $2, $3, $4)', [id, opts.name ?? null, email, kind]);diff --git a/src/core/transcripts/archive.ts b/src/core/transcripts/archive.tsindex f9bf4bbe47..3eed090338 100644--- a/src/core/transcripts/archive.ts+++ b/src/core/transcripts/archive.ts@@ -372,7 +372,7 @@ async function readersFor(engine: BrainEngine, privacy: Privacy, conv: ArchiveCo* conversations/ replaces them, so later group conversations stay owner-only too (archive_rescope_folder_cap). No other rule is* dropped, so a source that already held MAX_RULES other rules ends one rule over (rescopeCapped reports it). A source whose rules live in git is left to its access.yml.*/-async function reconcileAudience(engine: BrainEngine, sourceId: string, privacy: Privacy): Promise<void> {+async function reconcileAudience(engine: BrainEngine, sourceId: string, privacy: Privacy, record = true): Promise<void> {const now = privacy.shared ? 'shared' : 'personal';const recorded = await engine.getConfig(`${AUDIENCE_PREFIX}${sourceId}`);if (recorded === now) return;@@ -399,7 +399,7 @@ async function reconcileAudience(engine: BrainEngine, sourceId: string, privacy:}if (capped) await engine.setConfig(`${RESCOPE_CAPPED_PREFIX}${sourceId}`, String(folders.length));}- await engine.setConfig(`${AUDIENCE_PREFIX}${sourceId}`, now);+ if (record) await engine.setConfig(`${AUDIENCE_PREFIX}${sourceId}`, now);}export interface ArchiveRescopeSkip { code: 'archive_rescope_owner_unset' | 'archive_rescope_git_rules' | 'archive_rescope_pending' | 'archive_rescope_folder_cap'; message: string }@@ -417,6 +417,58 @@ export async function reconcileArchiveAudience(engine: BrainEngine, sourceId?: sreturn true;}+/**+ * The owner-only re-scope for every archive source written while the brain was personal, run by the commands that can+ * turn a brain shared (gbrain access member add and member enable, gbrain access owner set, gbrain config set+ * brain.audience) so earlier pages are kept to the owner before a second member reads them, not at the next archive+ * run or maintenance pass. Returns each source whose re-scope was due, with why it could not apply (null when it did).+ * It holds the source's archive run lock. While a run that started on the personal brain holds it, that run keeps+ * writing pages with no readers into folders no rule covers yet, so the pages archived so far are kept to the owner now+ * but the audience stays recorded personal and the next run re-scopes again (archive_rescope_pending).+ */+export async function rescopeArchiveOnShared(engine: BrainEngine): Promise<Array<{ source_id: string; skipped: ArchiveRescopeSkip | null }>> {+ const rows = await engine.executeRaw<{ key: string }>("SELECT key FROM config WHERE key LIKE $1 AND value = 'personal' ORDER BY key", [AUDIENCE_PREFIX + '%']);+ if (!rows.length) return [];+ const privacy = await resolvePrivacy(engine);+ if (!privacy.shared) return [];+ const { withRefreshingLock, LockUnavailableError } = await import('../db-lock.ts');+ const out: Array<{ source_id: string; skipped: ArchiveRescopeSkip | null }> = [];+ for (const { key } of rows) {+ const sourceId = key.slice(AUDIENCE_PREFIX.length);+ try {+ if (!await withRefreshingLock(engine, ARCHIVE_LOCK_PREFIX + sourceId, () => reconcileArchiveAudience(engine, sourceId))) continue;+ } catch (err) {+ if (!(err instanceof LockUnavailableError)) throw err;+ await reconcileAudience(engine, sourceId, privacy, false);+ const skipped = await archiveRescopeSkipped(engine, sourceId);+ out.push({ source_id: sourceId, skipped: skipped?.code !== 'archive_rescope_pending' ? skipped : { code: 'archive_rescope_pending', message: 'Conversation archive pages archived so far are kept to the owner, but an archive run that started while this brain was personal is writing source ' + sourceId + ' now and its new pages are not (gbrain status --section locks --json shows the holder); once it finishes, run gbrain transcripts archive run.' } });+ continue;+ }+ out.push({ source_id: sourceId, skipped: await archiveRescopeSkipped(engine, sourceId) });+ }+ return out;+}++/**+ * Before a member change that adds an enabled member (gbrain access member add, or member enable of a disabled one)+ * and so may make the brain shared: keeps the archive pages written while personal to the owner first, so the member's+ * credentials never read them, even for the moment between the change and rescopeArchiveOnShared. The audience is not+ * recorded here; rescopeArchiveOnShared does that once the change has committed. Throws when the re-scope fails, so the+ * caller makes no change.+ */+export async function rescopeArchiveBeforeJoin(engine: BrainEngine, memberId: string): Promise<void> {+ const rows = await engine.executeRaw<{ key: string }>("SELECT key FROM config WHERE key LIKE $1 AND value = 'personal' ORDER BY key", [AUDIENCE_PREFIX + '%']);+ if (!rows.length) return;+ let privacy = await resolvePrivacy(engine);+ if (!privacy.shared) {+ const enabled = (await listMembers(engine)).filter(m => m.status === 'enabled').map(m => m.id);+ if (enabled.includes(memberId) || enabled.length + 1 < 2) return;+ const view = await ownerView(engine);+ privacy = { shared: true, owner: view.owner ?? (view.inactive?.value === memberId ? memberId : null) };+ }+ for (const { key } of rows) await reconcileAudience(engine, key.slice(AUDIENCE_PREFIX.length), privacy, false);+}+/** Why the owner-only re-scope (reconcileAudience) has not run on a brain that became shared, or null. */export async function archiveRescopeSkipped(engine: BrainEngine, sourceId?: string): Promise<ArchiveRescopeSkip | null> {const configured = await configuredSource(engine, sourceId);@@ -424,7 +476,7 @@ export async function archiveRescopeSkipped(engine: BrainEngine, sourceId?: striconst privacy = await resolvePrivacy(engine);if (!privacy.shared) return null;if (!privacy.owner) {- return { code: 'archive_rescope_owner_unset', message: 'Conversation archive pages written while this brain was personal are not yet kept to the owner, because the shared brain names no owner; run gbrain access owner set <member> and the next archive run applies the owner-only rules.' };+ return { code: 'archive_rescope_owner_unset', message: 'Conversation archive pages written while this brain was personal are not yet kept to the owner, because the shared brain names no owner; run gbrain access owner set <member>, which applies the owner-only rules at once.' };}const policy = await import('../access/policy.ts');const pending: ArchiveRescopeSkip = { code: 'archive_rescope_pending', message: 'Conversation archive pages written while this brain was personal are not yet kept to the owner; the next maintenance pass applies the owner-only rules, or run gbrain transcripts archive run now.' };diff --git a/test/archive-rescope-on-shared.test.ts b/test/archive-rescope-on-shared.test.tsnew file mode 100644index 0000000000..feed2c7111--- /dev/null+++ b/test/archive-rescope-on-shared.test.ts@@ -0,0 +1,374 @@+/**+ * Conversation archive pages written while a brain was personal name no readers. The commands that turn the brain+ * shared (gbrain access member add and member enable to a second enabled member, gbrain config set brain.audience+ * shared, gbrain access owner set on a shared brain) keep those pages to the owner at once, so a second member never+ * reads them remotely in the window before the next archive run or maintenance pass (maintenance.auto is off here).+ * Each case drives the brain shared through the CLI handler alone, then reads over the http dispatch as the second+ * member, as the owner and with an unbound token. In the owner set case the second member is added first on a brain+ * with no owner, so member add says why the pages wait (archive_rescope_owner_unset) and owner set applies it.+ * A member add while an archive run holds the source keeps the pages archived so far to the owner at once and leaves the+ * audience recorded personal, so the source's next run re-scopes what the run in flight wrote with no readers. Repo-less; synthetic values only.+ */+import { afterAll, beforeAll, describe, expect, spyOn, test } from 'bun:test';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { configureGateway, resetGateway, __setEmbedTransportForTests } from '../src/core/ai/gateway.ts';+import { GBrainOAuthProvider } from '../src/core/oauth-provider.ts';+import { sqlQueryForEngine } from '../src/core/sql-query.ts';+import { mintLegacyToken } from '../src/core/token-mint.ts';+import { noGrantFederatedScope } from '../src/core/source-resolver.ts';+import { dispatchToolCall } from '../src/mcp/dispatch.ts';+import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/private-visibility.ts';+import { addMember, bindCredential, disableMember, getMember } from '../src/core/access/members.ts';+import * as policy from '../src/core/access/policy.ts';+import { setOwner } from '../src/core/access/owner.ts';+import { tryAcquireDbLock } from '../src/core/db-lock.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { ARCHIVE_TIMEZONE_KEY, archiveRescopeSkipped, runTranscriptArchive, setArchiveEnabled } from '../src/core/transcripts/archive.ts';+import { runAccess } from '../src/commands/access.ts';+import { runConfig } from '../src/commands/config.ts';+import type { AuthInfo } from '../src/core/ops/contract.ts';+import { testBackends } from './helpers/test-backends.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { emptyHome, withEnv } from './helpers/with-env.ts';+import { archivedPages, conversation, staticReader } from './helpers/transcript-archive.ts';++const SRC = 'conversations';+const OWNER = 'alice-example';+const OTHER = 'bob-example';+const CANARIES = ['canaryharborqzx', 'canaryledgerwvk'] as const;+/** Words only the page body or its derived fact holds, never a query, so an echoed query is never read as a leak. */+const BODY_ONLY = { canaryharborqzx: 'bodyonlyharborjtq', canaryledgerwvk: 'bodyonlyledgerpfn' } as const;+const FACT_ONLY = { canaryharborqzx: 'factonlyharborxkd', canaryledgerwvk: 'factonlyledgerhmz' } as const;+const DIMS = 1536;++function vectorFor(text: string): number[] {+ const v = new Array<number>(DIMS).fill(0);+ for (const w of text.toLowerCase().match(/[a-z]+/g) ?? []) {+ let h = 0;+ for (const ch of w) h = (h * 31 + ch.charCodeAt(0)) >>> 0;+ v[h % DIMS] += 1;+ }+ const n = Math.hypot(...v) || 1;+ return v.map(x => x / n);+}++const readsFor = (slug: string, canary: string): Array<[string, string, Record<string, unknown>]> => [+ ['get_page', 'get_page', { slug }],+ ['search keyword', 'search', { query: canary, match: 'keyword', fields: 'full', limit: 20 }],+ ['search hybrid', 'search', { query: 'planning notes ' + canary, fields: 'full', limit: 20 }],+ ['query', 'query', { query: 'planning notes ' + canary, expand: false, fields: 'full', limit: 20 }],+ ['recall (newest)', 'recall', { query: 'planning notes' }],+ ['recall (since)', 'recall', { since: '2000-01-01' }],+];++type Trigger = 'member add' | 'member enable' | 'config set brain.audience' | 'owner set' | 'member add during an archive run';+const TRIGGERS: Trigger[] = ['member add', 'member enable', 'config set brain.audience', 'owner set', 'member add during an archive run'];++for (const kind of testBackends()) {+ for (const trigger of TRIGGERS) {+ describe('archive pages turn owner-only when ' + trigger + ' makes the brain shared (' + kind + ')', () => {+ let engine: BrainEngine;+ let close: () => Promise<void>;+ const home = emptyHome();+ const pages: Array<{ slug: string; canary: typeof CANARIES[number] }> = [];+ const tokens: Record<string, string> = {};+ const ownerBefore: Record<string, boolean> = {};+ const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });+ const inHome = <T>(fn: () => Promise<T>) => withEnv({ GBRAIN_HOME: home }, fn);++ async function bind(name: string, member: string | null): Promise<void> {+ const minted = await mintLegacyToken(engine, { name: 'tok-' + name, scopes: ['read'], takesHolders: ['world'], sourceGrant: [SRC] });+ tokens[name] = minted.token;+ if (member) await bindCredential(engine, { kind: 'token', id: minted.id }, { member });+ }++ async function reads(who: string, canary: typeof CANARIES[number], op: string, params: Record<string, unknown>): Promise<boolean> {+ const auth = await provider().verifyAccessToken(tokens[who]!) as unknown as AuthInfo;+ const sourceId = auth.sourceId ?? 'default';+ const localFederated = await noGrantFederatedScope(engine, auth.hasSourceGrant, sourceId);+ const out = await dispatchToolCall(engine, op, { ...params }, {+ remote: true, transport: 'http', auth, sourceId, takesHoldersAllowList: auth.takesHoldersAllowList ?? ['world'],+ ...(localFederated ? { localFederatedSourceIds: localFederated } : {}),+ });+ const text = out.content.map(c => c.text ?? '').join('\n');+ return text.includes(BODY_ONLY[canary]) || text.includes(FACT_ONLY[canary]);+ }++ async function readMatrix(who: string): Promise<Record<string, boolean>> {+ __resetPrivateVisibilityCacheForTests();+ const out: Record<string, boolean> = {};+ for (const { slug, canary } of pages) {+ for (const [label, op, params] of readsFor(slug, canary)) out[canary + ' ' + label] = await reads(who, canary, op, params);+ }+ return out;+ }++ beforeAll(async () => {+ configureGateway({ embedding_model: 'openai:text-embedding-3-small', embedding_dimensions: DIMS, env: { OPENAI_API_KEY: 'test' } });+ __setEmbedTransportForTests((async ({ values }: { values: string[] }) => ({ embeddings: values.map(vectorFor) })) as never);+ if (kind === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));+ else { engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema(); close = () => engine.disconnect(); }+ __resetPrivateVisibilityCacheForTests();+ await engine.setConfig('maintenance.auto', 'false');+ await engine.setConfig(ARCHIVE_TIMEZONE_KEY, 'UTC');+ await setArchiveEnabled(engine, true);++ await addMember(engine, OWNER);+ if (trigger === 'member enable') { await addMember(engine, OTHER); await disableMember(engine, OTHER); }+ if (trigger !== 'owner set') await setOwner(engine, OWNER);++ const reader = staticReader('openclaw', [+ conversation('openclaw', 'agent:main:main', [['user', '2026-10-01T10:00:00.000Z', 'harbor planning notes ' + CANARIES[0] + ' private ' + BODY_ONLY[CANARIES[0]]]], { chatType: 'direct', verified: true }),+ conversation('openclaw', 'agent:main:other', [['user', '2026-10-02T10:00:00.000Z', 'ledger planning notes ' + CANARIES[1] + ' private ' + BODY_ONLY[CANARIES[1]]]], { chatType: 'direct', verified: true }),+ ]);+ const first = await inHome(() => runTranscriptArchive(engine, { mode: 'backfill', from: '2026-10-01', to: '2026-10-02', readers: [reader], now: new Date('2026-10-05T00:00:00Z') }));+ expect(first.privacy).toBe('personal');+ const archived = await archivedPages(engine, SRC);+ for (const canary of CANARIES) {+ const page = archived.find(p => p.body.includes(canary));+ expect(page?.frontmatter.readable_by).toBeUndefined();+ pages.push({ slug: page!.slug, canary });+ await engine.insertFact({ fact: 'planning notes ' + FACT_ONLY[canary], kind: 'fact', source: 'test', visibility: 'world' }, { source_id: SRC });+ await engine.executeRaw('UPDATE facts SET source_markdown_slug = $1 WHERE fact LIKE $2', [page!.slug, '%' + FACT_ONLY[canary]]);+ }++ await bind('owner', OWNER);+ if (trigger !== 'owner set') Object.assign(ownerBefore, await readMatrix('owner'));++ await inHome(async () => {+ if (trigger === 'member add') await runAccess(engine, ['member', 'add', OTHER]);+ if (trigger === 'member add during an archive run') {+ const lock = await tryAcquireDbLock(engine, 'transcripts-archive:' + SRC);+ const stderr: string[] = [];+ const spy = spyOn(console, 'error').mockImplementation((...line: unknown[]) => { stderr.push(line.join(' ')); });+ try { await runAccess(engine, ['member', 'add', OTHER]); } finally { spy.mockRestore(); await lock!.release(); }+ expect(stderr.join('\n')).toContain('pages archived so far are kept to the owner, but an archive run that started while this brain was personal is writing source ' + SRC + ' now');+ }+ if (trigger === 'member enable') await runAccess(engine, ['member', 'enable', OTHER]);+ if (trigger === 'config set brain.audience') await runConfig(engine, ['set', 'brain.audience', 'shared']);+ if (trigger === 'owner set') {+ const stderr: string[] = [];+ const spy = spyOn(console, 'error').mockImplementation((...line: unknown[]) => { stderr.push(line.join(' ')); });+ try { await runAccess(engine, ['member', 'add', OTHER]); } finally { spy.mockRestore(); }+ expect(stderr.join('\n')).toContain('the shared brain names no owner; run gbrain access owner set <member>, which applies the owner-only rules at once.');+ expect(await archiveRescopeSkipped(engine)).toMatchObject({ code: 'archive_rescope_owner_unset' });+ await runAccess(engine, ['owner', 'set', OWNER]);+ }+ });+ if (trigger === 'config set brain.audience') await addMember(engine, OTHER);+ await bind('other', OTHER);+ await bind('unbound', null);+ }, 240_000);++ afterAll(async () => {+ __setEmbedTransportForTests(null);+ resetGateway();+ await disposePersistenceConsumer(engine).catch(() => {});+ await close?.();+ }, 60_000);++ test('the re-scope has run: nothing pending and the archive records the brain as shared', async () => {+ if (trigger === 'member add during an archive run') {+ expect(await inHome(() => archiveRescopeSkipped(engine))).toMatchObject({ code: 'archive_rescope_pending' });+ expect(await engine.getConfig('transcripts.archive.audience.' + SRC)).toBe('personal');+ return;+ }+ expect(await inHome(() => archiveRescopeSkipped(engine))).toBeNull();+ expect(await engine.getConfig('transcripts.archive.audience.' + SRC)).toBe('shared');+ }, 60_000);++ test('the second member reads nothing from the personal-era pages through get_page, search, query or recall', async () => {+ const matrix = await readMatrix('other');+ expect(Object.entries(matrix).filter(([, read]) => read).map(([k]) => k)).toEqual([]);+ expect(Object.keys(matrix)).toHaveLength(CANARIES.length * 6);+ }, 240_000);++ test('a token bound to no member reads nothing from them either', async () => {+ const matrix = await readMatrix('unbound');+ expect(Object.entries(matrix).filter(([, read]) => read).map(([k]) => k)).toEqual([]);+ }, 240_000);++ test('the owner reads every archive page through every path, before and after', async () => {+ const after = await readMatrix('owner');+ expect(Object.entries(after).filter(([, read]) => !read).map(([k]) => k)).toEqual([]);+ if (trigger !== 'owner set') expect(ownerBefore).toEqual(after);+ }, 240_000);++ if (trigger === 'member add during an archive run') {+ test('the next archive run finishes the re-scope, and the second member still reads nothing', async () => {+ await inHome(() => runTranscriptArchive(engine, { mode: 'run', readers: [staticReader('openclaw', [])], now: new Date('2026-10-05T00:00:00Z') }));+ expect(await inHome(() => archiveRescopeSkipped(engine))).toBeNull();+ expect(await engine.getConfig('transcripts.archive.audience.' + SRC)).toBe('shared');+ const matrix = await readMatrix('other');+ expect(Object.entries(matrix).filter(([, read]) => read).map(([k]) => k)).toEqual([]);+ }, 240_000);+ }+ });+ }+}++/**+ * Order: access member add (--bind-token) and member enable keep the personal-era pages to the owner before the member+ * is added, enabled or bound, so its credentials never read them; a re-scope that throws leaves the member not enabled+ * and the token not bound. proposeRevision is the step that writes the owner-only rules.+ */+class Exited extends Error {}++for (const kind of testBackends()) {+ for (const door of ['member add --bind-token', 'member enable'] as const) {+ for (const mode of ['rules before the member', 'a failed re-scope changes nothing'] as const) {+ describe('archive re-scope order: ' + door + ', ' + mode + ' (' + kind + ')', () => {+ let engine: BrainEngine;+ let close: () => Promise<void>;+ let tokenId = '';+ const home = emptyHome();++ beforeAll(async () => {+ if (kind === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));+ else { engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema(); close = () => engine.disconnect(); }+ await engine.setConfig('maintenance.auto', 'false');+ await engine.setConfig(ARCHIVE_TIMEZONE_KEY, 'UTC');+ await setArchiveEnabled(engine, true);+ await addMember(engine, OWNER);+ await setOwner(engine, OWNER);+ const reader = staticReader('openclaw', [conversation('openclaw', 'agent:main:main', [['user', '2026-10-01T10:00:00.000Z', 'harbor planning notes ' + CANARIES[0]]], { chatType: 'direct', verified: true })]);+ await withEnv({ GBRAIN_HOME: home }, () => runTranscriptArchive(engine, { mode: 'backfill', from: '2026-10-01', to: '2026-10-01', readers: [reader], now: new Date('2026-10-05T00:00:00Z') }));+ const minted = await mintLegacyToken(engine, { name: 'tok-other', scopes: ['read'], takesHolders: ['world'], sourceGrant: [SRC] });+ tokenId = minted.id;+ if (door === 'member enable') {+ await addMember(engine, OTHER);+ await bindCredential(engine, { kind: 'token', id: minted.id }, { member: OTHER });+ await disableMember(engine, OTHER);+ }+ }, 240_000);++ afterAll(async () => {+ await disposePersistenceConsumer(engine).catch(() => {});+ await close?.();+ }, 60_000);++ const boundTo = async () => (await engine.executeRaw<{ member_id: string | null }>('SELECT member_id FROM access_tokens WHERE id::text = $1', [tokenId]))[0]?.member_id ?? null;+ const argv = door === 'member enable' ? ['member', 'enable', OTHER] : ['member', 'add', OTHER, '--bind-token', 'tok-other'];++ test(mode, async () => {+ const original = policy.proposeRevision;+ const seen: Array<{ status: string | null; bound: string | null }> = [];+ const revision = spyOn(policy, 'proposeRevision').mockImplementation((async (...a: Parameters<typeof original>) => {+ seen.push({ status: (await getMember(engine, OTHER))?.status ?? null, bound: await boundTo() });+ if (mode === 'a failed re-scope changes nothing') throw new Error('rule store unavailable');+ return original(...a);+ }) as typeof original);+ const exit = spyOn(process, 'exit').mockImplementation(((code?: number) => { throw new Exited(String(code)); }) as never);+ const stderr = spyOn(console, 'error').mockImplementation(() => {});+ let exited: unknown = null;+ try {+ await withEnv({ GBRAIN_HOME: home }, () => runAccess(engine, argv));+ } catch (e) {+ exited = e;+ } finally {+ revision.mockRestore();+ exit.mockRestore();+ stderr.mockRestore();+ }+ expect(seen.length).toBeGreaterThan(0);+ expect(seen[0]).toEqual(door === 'member enable' ? { status: 'disabled', bound: OTHER } : { status: null, bound: null });+ if (mode === 'rules before the member') {+ expect(exited).toBeNull();+ expect((await getMember(engine, OTHER))?.status).toBe('enabled');+ expect(await boundTo()).toBe(OTHER);+ expect(await withEnv({ GBRAIN_HOME: home }, () => archiveRescopeSkipped(engine))).toBeNull();+ return;+ }+ expect(exited).toBeInstanceOf(Exited);+ expect((exited as Exited).message).not.toBe('0');+ expect((await getMember(engine, OTHER))?.status ?? null).toBe(door === 'member enable' ? 'disabled' : null);+ expect(await boundTo()).toBe(door === 'member enable' ? OTHER : null);+ expect(await engine.getConfig('transcripts.archive.audience.' + SRC)).toBe('personal');+ }, 240_000);+ });+ }+ }+}++/**+ * A member add that its own input checks refuse (a bad --kind, a malformed --email, an ill-formed group) changes nothing:+ * the access rules, the archive pages, the members table and an unbound token's archive reads stay exactly as they were,+ * because addMember runs the re-scope (beforeWrite) only after every input check has passed.+ */+for (const kind of testBackends()) {+ for (const [reason, extra] of [['a bad --kind', ['--kind', 'typo']], ['a malformed --email', ['--email', 'not-an-email']], ['an ill-formed group', ['--groups', 'Bad Group!']]] as const) {+ describe('a refused member add changes nothing: ' + reason + ' (' + kind + ')', () => {+ let engine: BrainEngine;+ let close: () => Promise<void>;+ let token = '';+ let slug = '';+ const home = emptyHome();+ const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });++ async function unboundReads(): Promise<Record<string, boolean>> {+ __resetPrivateVisibilityCacheForTests();+ const auth = await provider().verifyAccessToken(token) as unknown as AuthInfo;+ const sourceId = auth.sourceId ?? 'default';+ const localFederated = await noGrantFederatedScope(engine, auth.hasSourceGrant, sourceId);+ const out: Record<string, boolean> = {};+ for (const [label, op, params] of readsFor(slug, CANARIES[0]).filter(([l]) => l === 'get_page' || l === 'search keyword')) {+ const res = await dispatchToolCall(engine, op, { ...params }, {+ remote: true, transport: 'http', auth, sourceId, takesHoldersAllowList: auth.takesHoldersAllowList ?? ['world'],+ ...(localFederated ? { localFederatedSourceIds: localFederated } : {}),+ });+ out[label] = res.content.map(c => c.text ?? '').join('\n').includes(BODY_ONLY[CANARIES[0]]);+ }+ return out;+ }++ const snapshot = async () => ({+ rules: JSON.stringify(await policy.appliedRules(engine, SRC)),+ pages: await engine.executeRaw<{ j: string }>("SELECT row_to_json(p)::text AS j FROM pages p WHERE slug LIKE 'conversations/%' ORDER BY slug"),+ members: await engine.executeRaw<{ j: string }>('SELECT row_to_json(m)::text AS j FROM access_members m ORDER BY id'),+ audience: await engine.getConfig('transcripts.archive.audience.' + SRC),+ reads: await unboundReads(),+ });++ beforeAll(async () => {+ if (kind === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));+ else { engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema(); close = () => engine.disconnect(); }+ __resetPrivateVisibilityCacheForTests();+ await engine.setConfig('maintenance.auto', 'false');+ await engine.setConfig(ARCHIVE_TIMEZONE_KEY, 'UTC');+ await setArchiveEnabled(engine, true);+ await addMember(engine, OWNER);+ await setOwner(engine, OWNER);+ const reader = staticReader('openclaw', [conversation('openclaw', 'agent:main:main', [['user', '2026-10-01T10:00:00.000Z', 'harbor planning notes ' + CANARIES[0] + ' private ' + BODY_ONLY[CANARIES[0]]]], { chatType: 'direct', verified: true })]);+ await withEnv({ GBRAIN_HOME: home }, () => runTranscriptArchive(engine, { mode: 'backfill', from: '2026-10-01', to: '2026-10-01', readers: [reader], now: new Date('2026-10-05T00:00:00Z') }));+ slug = (await archivedPages(engine, SRC)).find(pg => pg.body.includes(CANARIES[0]))!.slug;+ token = (await mintLegacyToken(engine, { name: 'tok-unbound', scopes: ['read'], takesHolders: ['world'], sourceGrant: [SRC] })).token;+ }, 240_000);++ afterAll(async () => {+ await disposePersistenceConsumer(engine).catch(() => {});+ await close?.();+ }, 60_000);++ test('the rules, the archive pages, the members and an unbound token\'s reads stay as they were', async () => {+ const before = await snapshot();+ expect(before.audience).toBe('personal');+ const exit = spyOn(process, 'exit').mockImplementation(((code?: number) => { throw new Exited(String(code)); }) as never);+ const stderr = spyOn(console, 'error').mockImplementation(() => {});+ let exited: unknown = null;+ try {+ await withEnv({ GBRAIN_HOME: home }, () => runAccess(engine, ['member', 'add', OTHER, ...extra]));+ } catch (e) {+ exited = e;+ } finally {+ exit.mockRestore();+ stderr.mockRestore();+ }+ expect(exited).toBeInstanceOf(Exited);+ expect((exited as Exited).message).not.toBe('0');+ expect(await getMember(engine, OTHER)).toBeNull();+ expect(await snapshot()).toEqual(before);+ }, 240_000);+ });+ }+}diff --git a/test/e2e/archive-rescope-on-shared-postgres.test.ts b/test/e2e/archive-rescope-on-shared-postgres.test.tsnew file mode 100644index 0000000000..7bd72d194c--- /dev/null+++ b/test/e2e/archive-rescope-on-shared-postgres.test.ts@@ -0,0 +1,3 @@+import { registerPostgresTests } from '../helpers/test-backends.ts';++await registerPostgresTests(() => import('../archive-rescope-on-shared.test.ts'));