Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Add tests: pages derived from a private page stay private after it is deleted
garrytancf782eb314ed
diff --git a/test/derived-origin-carry-managed.test.ts b/test/derived-origin-carry-managed.test.tsnew file mode 100644index 0000000000..38d8809984--- /dev/null+++ b/test/derived-origin-carry-managed.test.ts@@ -0,0 +1,191 @@+/**+ * GST-2689 on managed brains with a Git checkout: a derived page whose only privacy is its private origin keeps it once+ * the origin row is gone, and the label it gets is one changed line in its file, published through the Git door.+ *+ * 1. Protects: a `git rm` of a private origin followed by sync (the managed sync soft-deletes the page) and the consented+ * retention purge, an explicit local `delete_page purge: true`, and a remote delete each stamp `visibility: private`+ * on every derived_from page, atom and concept that would gain a reader, before the origin row is removed; each file+ * changes only in that label line, untouched pages keep their bytes, and an unbound remote caller reads none of them+ * through get_page, get_versions, list_pages or search, before or after. A remote delete's stamp is a maintenance+ * write whose batch names the delete's request id.+ * 2. Fails when: a derived page opens once the origin row goes, a file changes beyond its label line, or the remote+ * stamp is not a maintenance write tied to its delete.+ * 3. Real seams: a claimed worktree with Git and managed persistence (helpers/managed-brain.ts), managed sync, the+ * `gbrain pages purge-deleted` command entry, MCP dispatch with a minted owner token; PGLite always, Postgres with DATABASE_URL.+ */+import { describe, expect, spyOn, test } from 'bun:test';+import { execFileSync } from 'node:child_process';+import { randomUUID } from 'node:crypto';+import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import type { AuthInfo, OperationContext } from '../src/core/ops/contract.ts';+import { operationsByName } from '../src/core/operations.ts';+import { performManagedSync } from '../src/core/persistence/sync-run.ts';+import { SYNC_DELETE_TOMBSTONE_OP } from '../src/core/persistence/sync-delete-tombstones.ts';+import { runPages } from '../src/commands/pages.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 { dispatchToolCall } from '../src/mcp/dispatch.ts';+import { setOwner } from '../src/core/access/owner.ts';+import { bindCredential } from '../src/core/access/members.ts';+import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/private-visibility.ts';+import { managedBrain, settleManagedGitEffects } from './helpers/managed-brain.ts';+import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';+import { withEnv } from './helpers/with-env.ts';++const git = (root: string, ...args: string[]) => execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();+const commit = (root: string, message: string) => { git(root, 'add', '-A'); git(root, '-c', 'user.name=Example', '-c', '[email protected]', 'commit', '-qm', message); };+const OPTS = { sourceId: 'default', noPull: true, noEmbed: true, noExtract: true };+const OWNER = 'owner-example';++const CARRIED = ['digest', 'atom', 'concept'] as const;+const UNTOUCHED = ['own', 'plain'] as const;+type Shape = 'origin' | typeof CARRIED[number] | typeof UNTOUCHED[number];+const SHAPES: Shape[] = ['origin', ...CARRIED, ...UNTOUCHED];+const slugOf = (k: string, shape: Shape) => `notes/${k}-${shape}`;+const canaryOf = (k: string, shape: Shape) => `canary${k}${shape}`;+function fileText(k: string, shape: Shape): string {+ const origin = slugOf(k, 'origin');+ const fm: Record<Shape, string[]> = {+ origin: ['type: note', 'visibility: private'],+ digest: ['type: note', `derived_from: ${origin}.md`],+ atom: ['type: atom', 'visibility: world', `source_slug: ${origin}`],+ concept: ['type: concept', 'synthesized_by: test', 'visibility: world'],+ own: ['type: note', 'visibility: private', `derived_from: [${origin}]`],+ plain: ['type: note'],+ };+ return ['---', `title: ${k} ${shape}`, ...fm[shape], '---', '', `Harbor ledger planning notes ${canaryOf(k, shape)} for the roadmap.`, ''].join('\n');+}+const KEYS = ['gitrm', 'purge', 'remote'];+const setup = ({ root }: { root: string }) => {+ mkdirSync(join(root, 'notes'), { recursive: true }); git(root, 'init', '-q', '-b', 'main');+ for (const k of KEYS) for (const shape of SHAPES) writeFileSync(join(root, `${slugOf(k, shape)}.md`), fileText(k, shape));+ commit(root, 'notes');+};++/** A fixture write outside coordination (links have no file), as the managed suites age tombstones. */+async function fixtureWrite(engine: BrainEngine, fn: () => Promise<unknown>): Promise<void> {+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ try { await fn(); } finally { await engine.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1'); }+}+const provenance = (engine: BrainEngine, k: string) => fixtureWrite(engine, () => engine.executeRaw(`INSERT INTO links (from_page_id, to_page_id, link_type, link_source)+ SELECT c.id, a.id, 'synthesized_from', 'concept-provenance' FROM pages c, pages a WHERE c.source_id='default' AND a.source_id='default' AND c.slug=$1 AND a.slug=$2`,+[slugOf(k, 'concept'), slugOf(k, 'atom')]));+async function age(engine: BrainEngine, hours: number): Promise<void> {+ await fixtureWrite(engine, async () => {+ await engine.executeRaw(`UPDATE pages SET deleted_at = deleted_at - ($1 || ' hours')::interval WHERE source_id='default' AND deleted_at IS NOT NULL`, [String(hours)]);+ await engine.executeRaw(`UPDATE op_checkpoints c SET completed_keys = jsonb_set(c.completed_keys, '{0,deleted_at}', to_jsonb(p.deleted_at)) FROM pages p+ WHERE c.op=$1 AND p.id::text = c.fingerprint`, [SYNC_DELETE_TOMBSTONE_OP]);+ });+}+async function cli(engine: BrainEngine, args: string[]): Promise<void> {+ const log = spyOn(console, 'log').mockImplementation(() => {});+ const err = spyOn(console, 'error').mockImplementation(() => {});+ try { await withEnv({ GBRAIN_CONSENT: undefined }, () => runPages(engine, args)); }+ finally { log.mockRestore(); err.mockRestore(); }+}+const page = async (engine: BrainEngine, slug: string) => (await engine.executeRaw<{ deleted_at: string | null; frontmatter: unknown }>(+ "SELECT deleted_at, frontmatter FROM pages WHERE source_id='default' AND slug=$1", [slug]))[0];+const fmOf = async (engine: BrainEngine, slug: string) => { const fm = (await page(engine, slug))!.frontmatter; return (typeof fm === 'string' ? JSON.parse(fm) : fm) as Record<string, unknown>; };++/** What an unbound remote caller reads of each page, per read path. */+async function unboundReads(engine: BrainEngine, k: string, shapes: readonly Shape[]) {+ __resetPrivateVisibilityCacheForTests();+ const ctx = { engine, config: { engine: engine.kind }, logger: { info() {}, warn() {}, error() {} }, dryRun: false, remote: true, sourceId: 'default' } as unknown as OperationContext;+ const text = (value: unknown) => JSON.stringify(value);+ const attempt = async (fn: () => Promise<unknown>) => { try { return text(await fn()); } catch { return ''; } };+ const out: Record<string, unknown> = {};+ for (const shape of shapes) {+ const slug = slugOf(k, shape), canary = canaryOf(k, shape);+ out[shape] = {+ get_page: (await attempt(() => operationsByName.get_page!.handler(ctx, { slug }))).includes(canary),+ get_versions: (await attempt(() => operationsByName.get_versions!.handler(ctx, { slug, include_body: true }))).includes(canary),+ list_pages: (await attempt(() => operationsByName.list_pages!.handler(ctx, { limit: 100 }))).includes(`"${slug}"`),+ search: (await attempt(() => operationsByName.search!.handler(ctx, { query: canary, match: 'keyword', fields: 'full', limit: 20 }))).includes(canary),+ };+ }+ return out;+}+const hidden = { get_page: false, get_versions: false, list_pages: false, search: false };++/** The contract after the origin row is gone: label line only, untouched bytes kept, nothing opens. */+async function expectCarried(engine: BrainEngine, root: string, k: string, files: Record<string, string>): Promise<void> {+ await settleManagedGitEffects(engine);+ expect(await page(engine, slugOf(k, 'origin'))).toBeUndefined();+ for (const shape of CARRIED) {+ const slug = slugOf(k, shape);+ expect({ shape, visibility: (await fmOf(engine, slug)).visibility }).toEqual({ shape, visibility: 'private' });+ const after = readFileSync(join(root, `${slug}.md`), 'utf8');+ const lines = (text: string) => text.split('\n').filter(line => !line.startsWith('visibility:'));+ expect({ shape, label: after.split('\n').filter(line => line.startsWith('visibility:')) }).toEqual({ shape, label: ['visibility: private'] });+ expect({ shape, rest: lines(after) }).toEqual({ shape, rest: lines(files[shape]!) });+ }+ for (const shape of UNTOUCHED) expect({ shape, bytes: readFileSync(join(root, `${slugOf(k, shape)}.md`), 'utf8') }).toEqual({ shape, bytes: files[shape] });+ const others = SHAPES.filter(s => s !== 'origin' && s !== 'plain');+ expect(await unboundReads(engine, k, others)).toEqual(Object.fromEntries(others.map(s => [s, hidden])));+}++async function before(engine: BrainEngine, root: string, k: string): Promise<Record<string, string>> {+ await provenance(engine, k);+ expect(await unboundReads(engine, k, CARRIED)).toEqual(Object.fromEntries(CARRIED.map(s => [s, hidden])));+ return Object.fromEntries(SHAPES.map(s => [s, readFileSync(join(root, `${slugOf(k, s)}.md`), 'utf8')]));+}++for (const backend of testBackends()) {+ const databaseUrl = backend === 'postgres' ? requirePostgresTestDatabase() : undefined;+ describe(`${backend}: managed repo brain, derived pages keep their origin's privacy`, () => {+ test('git rm of the origin, sync, then the consented retention purge: the derived files gain the label line before the row goes', () => managedBrain(async ({ engine, root }) => {+ await performManagedSync(engine, OPTS);+ const k = 'gitrm', origin = slugOf(k, 'origin');+ const files = await before(engine, root, k);+ git(root, 'rm', '-q', `${origin}.md`); commit(root, 'remove the origin');+ await performManagedSync(engine, OPTS);+ expect((await page(engine, origin))?.deleted_at).not.toBeNull();+ await age(engine, 100);+ await cli(engine, ['purge-deleted', '--json', '--yes']);+ await expectCarried(engine, root, k, files);+ }, { setup, databaseUrl }), 300_000);++ test('an explicit local purge: the derived files gain the label line through the Git door before the row goes', () => managedBrain(async ({ engine, root, ctx }) => {+ await performManagedSync(engine, OPTS);+ const k = 'purge', origin = slugOf(k, 'origin');+ const files = await before(engine, root, k);+ const snapshot = await engine.readPageSnapshot(origin, { sourceId: 'default' });+ const result = await operationsByName.delete_page!.handler(ctx, { slug: origin, purge: true, request_id: randomUUID(), expected_revision: snapshot!.revision }) as { status: string };+ expect(result.status).toBe('purged');+ await expectCarried(engine, root, k, files);+ }, { setup, databaseUrl }), 300_000);++ test('a remote delete: the stamp is a maintenance write whose batch names the delete request id, and its response names no stamped page', () => managedBrain(async ({ engine, root }) => {+ await performManagedSync(engine, OPTS);+ const k = 'remote', origin = slugOf(k, 'origin');+ const files = await before(engine, root, k);+ await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1)', [OWNER]);+ const minted = await mintLegacyToken(engine, { name: 'owner-writer', scopes: ['read', 'write'], takesHolders: ['world'], sourceGrant: ['default'] });+ await bindCredential(engine, { kind: 'token', id: minted.id }, { member: OWNER });+ await setOwner(engine, OWNER);+ const auth = await new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) })+ .verifyAccessToken(minted.token) as unknown as AuthInfo;+ const requestId = randomUUID();+ const snapshot = await engine.readPageSnapshot(origin, { sourceId: 'default' });+ // A remote write reaches a private page only while the operator relaxes remote privacy (search.remote_private_pages).+ await engine.setConfig('search.remote_private_pages', 'visible');+ __resetPrivateVisibilityCacheForTests();+ const out = await dispatchToolCall(engine, 'delete_page', { slug: origin, request_id: requestId, expected_revision: snapshot!.revision },+ { remote: true, transport: 'http', auth, sourceId: 'default', takesHoldersAllowList: ['world'] });+ await engine.setConfig('search.remote_private_pages', 'hidden');+ const text = out.content.map(c => c.text).join('\n');+ expect(out.isError ? text : '').toBe('');+ for (const shape of [...CARRIED, ...UNTOUCHED]) expect({ shape, named: text.includes(slugOf(k, shape)) }).toEqual({ shape, named: false });+ const stamps = await engine.executeRaw<{ slug: string; principal_kind: string; state: string }>(`SELECT slug, principal_kind, state FROM persistence_requests+ WHERE intent->>'maintenance_batch' = $1 ORDER BY slug`, [`derived-origin-carry:${requestId}`]);+ expect(stamps.map(s => s.slug)).toEqual(CARRIED.map(s => slugOf(k, s)).sort());+ expect(stamps.every(s => s.principal_kind === 'local_cli' && s.state === 'committed')).toBe(true);+ await age(engine, 100);+ await cli(engine, ['purge-deleted', '--json', '--yes']);+ await expectCarried(engine, root, k, files);+ }, { setup, databaseUrl }), 300_000);+ });+}diff --git a/test/derived-origin-carry-private.test.ts b/test/derived-origin-carry-private.test.tsnew file mode 100644index 0000000000..c6b2877579--- /dev/null+++ b/test/derived-origin-carry-private.test.ts@@ -0,0 +1,286 @@+/**+ * GST-2689: a page that reads as world by its own label but is hidden from non-owners because its origin is private+ * (a declared derived_from origin, an atom's source_slug origin, or a concept whose provenance atom has such an origin)+ * stays hidden once that origin's row is gone.+ *+ * 1. Protects: before an origin row is removed (an explicit `delete_page purge: true`, the backdated retention purge,+ * and after a remote delete), every live same-source dependent that would gain a reader gets `visibility: private`+ * through the label door, so a non-owner reads it neither before nor after through get_page, get_versions,+ * list_pages and search. The owner, a granted_sources credential and a listed member read the same before and after.+ * Pages already private, naming readers, pinned or not lineage-dependent are never changed; trust tiers are kept and+ * no lowering proposal is filed. A remote delete's response names no stamped page and counts none. A page that names+ * readers is never changed, so its listed reader gains it once the origin goes (the one read that changes). When the stamp+ * fails, the origin is parked (never purged), doctor names it with the page that blocks it, and the derived page+ * stays hidden; once the cause is fixed the next purge stamps and purges. Nothing of a purged origin is kept.+ * 2. Fails when: any of those reads opens to a non-owner after the origin goes, a read changes for a reader who had it,+ * an untouched page changes, the remote response leaks a stamped page, or a parked origin is purged or silent.+ * 3. Real seams: tokens minted by the token service and verified as serve --http does, dispatched through MCP dispatch+ * (remote: true), on a repo-less brain without managed persistence; PGLite always, Postgres with DATABASE_URL.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { mkdtempSync, rmSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { importFromContent } from '../src/core/import-file.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 { setOwner } from '../src/core/access/owner.ts';+import { bindCredential } from '../src/core/access/members.ts';+import { purgeDeletedPagesCoordinated } from '../src/core/persistence/purge-deleted.ts';+import { checkDerivedOriginParked } from '../src/commands/doctor/checks/purge-parks.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 { readsUnscopedOnPurpose } from './helpers/reads-unscoped.ts';+import { withEnv } from './helpers/with-env.ts';++const SRC = 'source-a';+const OWNER = 'owner-example';+const ALICE = 'alice-example';+/** Pages that gain a reader once the origin goes, then the pages the stamp must never change. */+const CARRIED = ['digest', 'atom', 'concept'] as const;+const UNTOUCHED = ['own', 'listed', 'pinned', 'plain'] as const;+type Shape = 'origin' | typeof CARRIED[number] | typeof UNTOUCHED[number];+const SHAPES: Shape[] = ['origin', ...CARRIED, ...UNTOUCHED];+const CALLERS = ['owner', 'granted', 'alice', 'unbound'] as const;+type Caller = typeof CALLERS[number];++const slugOf = (k: string, shape: Shape) => `${k}/${shape}`;+const canaryOf = (k: string, shape: Shape) => `canary${k}${shape}`;+function frontmatter(k: string, shape: Shape): string[] {+ const origin = slugOf(k, 'origin');+ switch (shape) {+ case 'origin': return ['type: note', 'visibility: private'];+ case 'digest': return ['type: note', `derived_from: ${origin}.md`];+ case 'atom': return ['type: atom', 'visibility: world', `source_slug: ${origin}`];+ case 'concept': return ['type: concept', 'synthesized_by: test', 'visibility: world'];+ case 'own': return ['type: note', 'visibility: private', `derived_from: [${origin}]`];+ case 'listed': return ['type: note', `readable_by: [${ALICE}]`, `derived_from: [${origin}]`];+ case 'pinned': return ['type: note', 'pinned_question: true', `derived_from: [${origin}]`];+ case 'plain': return ['type: note'];+ }+}+const pageText = (k: string, shape: Shape) => ['---', `title: ${k} ${shape}`, ...frontmatter(k, shape), '---', `# ${k} ${shape}`, '',+ `Harbor ledger planning notes ${canaryOf(k, shape)} for the roadmap.`].join('\n');++for (const kind of testBackends()) {+ describe(`derived pages stay private when their origin row goes (${kind}, repo-less)`, () => {+ let engine: BrainEngine;+ let close: () => Promise<void>;+ const home = mkdtempSync(join(tmpdir(), 'gbrain-origin-carry-'));+ const tokens: Record<string, string> = {};+ const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });+ const local = { remote: false, sourceId: SRC } as const;++ async function call(who: string, op: string, params: Record<string, unknown>): Promise<{ isError: boolean; text: string }> {+ 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 withEnv({ GBRAIN_HOME: home }, () => dispatchToolCall(engine, op, { ...params }, {+ remote: true, transport: 'http', auth, sourceId, takesHoldersAllowList: auth.takesHoldersAllowList ?? ['world'],+ ...(localFederated ? { localFederatedSourceIds: localFederated } : {}),+ }));+ return { isError: out.isError === true, text: out.content.map(c => c.text).join('\n') };+ }+ const localCall = async (op: string, params: Record<string, unknown>) => {+ const out = await withEnv({ GBRAIN_HOME: home }, () => dispatchToolCall(engine, op, params, local));+ return { isError: out.isError === true, text: out.content.map(c => c.text).join('\n') };+ };++ /** What `who` reads of one page on each read path; the content canary decides (a not_found echoes the slug). */+ async function seen(who: Caller, k: string, shape: Shape) {+ const slug = slugOf(k, shape), canary = canaryOf(k, shape);+ return {+ get_page: (await call(who, 'get_page', { slug })).text.includes(canary),+ get_versions: (await call(who, 'get_versions', { slug, include_body: true })).text.includes(canary),+ list_pages: (await call(who, 'list_pages', { limit: 100 })).text.includes(`"${slug}"`),+ search: (await call(who, 'search', { query: canary, match: 'keyword', fields: 'full', limit: 20 })).text.includes(canary),+ };+ }+ async function readsOf(k: string, shapes: readonly Shape[]) {+ const out: Record<string, unknown> = {};+ for (const who of CALLERS) for (const shape of shapes) out[`${who}:${shape}`] = await seen(who, k, shape);+ return out;+ }+ const none = { get_page: false, get_versions: false, list_pages: false, search: false };+ const every = { get_page: true, get_versions: true, list_pages: true, search: true };++ const row = async (slug: string) => (await engine.executeRaw<{ id: number; frontmatter: Record<string, unknown> | string; trust_tier: string; deleted_at: string | null }>(+ 'SELECT id, frontmatter, trust_tier, deleted_at FROM pages WHERE source_id=$1 AND slug=$2', [SRC, slug]))[0];+ const fm = async (slug: string) => { const r = await row(slug); return typeof r!.frontmatter === 'string' ? JSON.parse(r!.frontmatter) : r!.frontmatter; };+ const state = async (k: string) => Object.fromEntries(await Promise.all(SHAPES.filter(s => s !== 'origin').map(async s => [s, { fm: await fm(slugOf(k, s)), tier: (await row(slugOf(k, s)))!.trust_tier }])));+ const proposals = async () => Number((await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM trust_proposals WHERE source_id=$1', [SRC]))[0]!.n);++ async function seed(k: string): Promise<void> {+ for (const shape of SHAPES) {+ const result = await importFromContent(engine, slugOf(k, shape), pageText(k, shape), { sourceId: SRC, noEmbed: true });+ expect(result.status).toBe('imported');+ await engine.createVersion(slugOf(k, shape), { sourceId: SRC });+ }+ await engine.executeRaw(`INSERT INTO links (from_page_id, to_page_id, link_type, link_source)+ SELECT c.id, a.id, 'synthesized_from', 'concept-provenance' FROM pages c, pages a WHERE c.source_id=$1 AND a.source_id=$1 AND c.slug=$2 AND a.slug=$3`,+ [SRC, slugOf(k, 'concept'), slugOf(k, 'atom')]);+ }+ const backdate = (slug: string) => engine.executeRaw("UPDATE pages SET deleted_at = now() - interval '100 hours' WHERE source_id=$1 AND slug=$2 AND deleted_at IS NOT NULL", [SRC, slug]);+ const softDelete = async (slug: string) => { expect(await engine.softDeletePages([slug], { sourceId: SRC })).toEqual([slug]); };+ const revision = async (slug: string) => (await engine.readPageSnapshot(slug, { sourceId: SRC, includeDeleted: true }))!.revision;+ /** Nothing that names the purged origin is kept: its row, and any park or checkpoint row naming its slug. */+ async function nothingKept(slug: string): Promise<void> {+ expect(await row(slug)).toBeUndefined();+ expect((await engine.executeRaw('SELECT 1 FROM op_checkpoints WHERE completed_keys::text LIKE $1', [`%${slug}%`])).length).toBe(0);+ }++ /** The before/after contract shared by every removal path. */+ async function expectCarried(k: string, before: Record<string, unknown>, beforeState: Record<string, any>, beforeProposals: number): Promise<void> {+ __resetPrivateVisibilityCacheForTests();+ const others = SHAPES.filter(s => s !== 'origin');+ // Pages that name readers are never stamped: the listed member reads 'listed' once its private origin is gone.+ const expected = { ...Object.fromEntries(Object.entries(before).filter(([key]) => !key.endsWith(':origin'))), 'alice:listed': every };+ expect(before['alice:listed']).toEqual(none);+ expect(await readsOf(k, others)).toEqual(expected);+ const after = await state(k);+ for (const shape of CARRIED) {+ expect({ shape, visibility: after[shape].fm.visibility }).toEqual({ shape, visibility: 'private' });+ expect({ shape, tier: after[shape].tier }).toEqual({ shape, tier: beforeState[shape].tier });+ }+ for (const shape of UNTOUCHED) expect({ shape, ...after[shape] }).toEqual({ shape, ...beforeState[shape] });+ expect(await proposals()).toBe(beforeProposals);+ }++ async function baseline(k: string) {+ __resetPrivateVisibilityCacheForTests();+ const before = await readsOf(k, SHAPES);+ for (const shape of CARRIED) {+ for (const who of ['unbound', 'alice'] as const) expect({ who, shape, seen: before[`${who}:${shape}`] }).toEqual({ who, shape, seen: none });+ for (const who of ['owner', 'granted'] as const) expect({ who, shape, seen: before[`${who}:${shape}`] }).toEqual({ who, shape, seen: every });+ }+ return { before, state: await state(k), proposals: await proposals() };+ }++ 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.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [SRC]);+ for (const id of [OWNER, ALICE]) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1)', [id]);+ const mint = async (name: string, scopes: string[], member?: string) => {+ const minted = await mintLegacyToken(engine, { name, scopes, takesHolders: ['world'], sourceGrant: [SRC] });+ tokens[name] = minted.token;+ if (member) await bindCredential(engine, { kind: 'token', id: minted.id }, { member });+ else await readsUnscopedOnPurpose(engine, minted.id);+ return minted.id;+ };+ await mint('owner', ['read'], OWNER);+ await mint('owner-writer', ['read', 'write'], OWNER);+ await mint('alice', ['read'], ALICE);+ await mint('unbound', ['read']);+ const granted = await mint('granted', ['read']);+ await engine.executeRaw("UPDATE access_tokens SET private_pages = 'granted_sources' WHERE id = $1::uuid", [granted]);+ await setOwner(engine, OWNER);+ }, 240_000);++ afterAll(async () => { await close?.(); rmSync(home, { recursive: true, force: true }); }, 60_000);++ test('an explicit purge of a private origin stamps its derived pages private first', async () => {+ const k = 'purge';+ await seed(k);+ const base = await baseline(k);+ const origin = slugOf(k, 'origin');+ const out = await localCall('delete_page', { slug: origin, purge: true, request_id: randomUUID(), expected_revision: await revision(origin) });+ expect(out.isError).toBe(false);+ expect(JSON.parse(out.text).status).toBe('purged');+ await nothingKept(origin);+ await expectCarried(k, base.before, base.state, base.proposals);+ }, 240_000);++ test('the retention purge of a backdated tombstone stamps its derived pages private before the row goes', async () => {+ const k = 'expiry';+ await seed(k);+ const base = await baseline(k);+ const origin = slugOf(k, 'origin');+ await softDelete(origin);+ await backdate(origin);+ const result = await purgeDeletedPagesCoordinated(engine, 72, { automatic: true });+ expect(result.slugs).toContain(origin);+ await nothingKept(origin);+ await expectCarried(k, base.before, base.state, base.proposals);+ }, 240_000);++ test('a remote delete stamps the derived pages as maintenance and its response names none of them and counts none', async () => {+ const k = 'remote';+ await seed(k);+ const base = await baseline(k);+ const origin = slugOf(k, 'origin'), control = 'remote-control/plain';+ expect((await importFromContent(engine, control, ['---', 'title: control', 'type: note', '---', '# control', '', 'Nothing derives from this page.'].join('\n'), { sourceId: SRC, noEmbed: true })).status).toBe('imported');+ // A remote write reaches a private page only while the operator relaxes remote privacy (search.remote_private_pages).+ await engine.setConfig('search.remote_private_pages', 'visible');+ __resetPrivateVisibilityCacheForTests();+ const response = await call('owner-writer', 'delete_page', { slug: origin, request_id: randomUUID(), expected_revision: await revision(origin) });+ await engine.setConfig('search.remote_private_pages', 'hidden');+ __resetPrivateVisibilityCacheForTests();+ expect(response.isError ? response.text : '').toBe('');+ for (const shape of [...CARRIED, ...UNTOUCHED]) expect({ shape, named: response.text.includes(slugOf(k, shape)) }).toEqual({ shape, named: false });+ for (const shape of CARRIED) expect({ shape, visibility: (await fm(slugOf(k, shape))).visibility }).toEqual({ shape, visibility: 'private' });+ // Same response shape as the delete of a page nothing derives from: no extra field, so no count.+ const controlResponse = await call('owner-writer', 'delete_page', { slug: control, request_id: randomUUID(), expected_revision: await revision(control) });+ const keys = (text: string) => Object.keys(JSON.parse(text)).sort();+ expect(keys(response.text)).toEqual(keys(controlResponse.text));+ expect(response.text).not.toMatch(/carried|stamped|dependent/i);+ await backdate(origin);+ const result = await purgeDeletedPagesCoordinated(engine, 72, { automatic: true });+ expect(result.slugs).toContain(origin);+ await nothingKept(origin);+ await expectCarried(k, base.before, base.state, base.proposals);+ }, 240_000);++ test('a stamp that fails parks the origin: never purged, named by doctor with the page that blocks it, the derived page stays hidden; fixed, the next purge stamps and purges', async () => {+ const k = 'park';+ await seed(k);+ const base = await baseline(k);+ const origin = slugOf(k, 'origin'), blocking = slugOf(k, 'digest');+ // A page with a file in its source's checkout that this host cannot write (sync.write_through off): the label door holds it.+ const checkout = mkdtempSync(join(tmpdir(), 'gbrain-origin-carry-checkout-'));+ await engine.executeRaw('UPDATE sources SET local_path=$2 WHERE id=$1', [SRC, checkout]);+ await engine.executeRaw('UPDATE pages SET source_path=$3 WHERE source_id=$1 AND slug=$2', [SRC, blocking, `${blocking}.md`]);+ await engine.setConfig('sync.write_through', 'false');+ try {+ const out = await localCall('delete_page', { slug: origin, purge: true, request_id: randomUUID(), expected_revision: await revision(origin) });+ expect(out.isError).toBe(false);+ const receipt = JSON.parse(out.text);+ expect(receipt.status).not.toBe('purged');+ expect(receipt.purge_held?.code).toBe('derived_origin_parked');+ expect((await row(origin))?.deleted_at).not.toBeNull();+ await backdate(origin);+ const held = await purgeDeletedPagesCoordinated(engine, 72, { automatic: true });+ expect(held.slugs).not.toContain(origin);+ expect(held.blocked.find(b => b.slug === origin)?.code).toBe('derived_origin_parked');+ expect((await row(origin))?.deleted_at).not.toBeNull();+ const doctor = await checkDerivedOriginParked(engine);+ expect(doctor.status).toBe('warn');+ expect(doctor.message).toContain(`${SRC}/${origin}`);+ expect(doctor.message).toContain(blocking);+ expect((doctor.details as { origins: Array<{ slug: string; blocking_slug: string; reason: string }> }).origins)+ .toEqual([expect.objectContaining({ slug: origin, blocking_slug: blocking })]);+ __resetPrivateVisibilityCacheForTests();+ for (const who of ['unbound', 'alice'] as const) expect({ who, seen: await seen(who, k, 'digest') }).toEqual({ who, seen: none });+ } finally {+ await engine.setConfig('sync.write_through', 'true');+ await engine.executeRaw('UPDATE pages SET source_path=NULL WHERE source_id=$1 AND slug=$2', [SRC, blocking]);+ await engine.executeRaw('UPDATE sources SET local_path=NULL WHERE id=$1', [SRC]);+ rmSync(checkout, { recursive: true, force: true });+ }+ const result = await purgeDeletedPagesCoordinated(engine, 72, { automatic: true });+ expect(result.slugs).toContain(origin);+ await nothingKept(origin);+ expect((await checkDerivedOriginParked(engine)).status).toBe('ok');+ await expectCarried(k, base.before, base.state, base.proposals);+ }, 240_000);+ });+}diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex fa87849485..f30fb2a512 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -88,6 +88,8 @@ test/decide/conflict-proposals-managed.test.tstest/declared-lineage-visibility.test.tstest/degraded-engine-recovered-surface-9224.test.tstest/derived-link-reconciliation.test.ts+test/derived-origin-carry-managed.test.ts+test/derived-origin-carry-private.test.tstest/direct-pool-profile.test.tstest/doctor-attest-read-shared-scope.test.tstest/doctor-database-encoding.test.ts