Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Hide pages a caller can't read from sources_resolve_path, and keep printed access rule hints accepted (GST-2128) (#9379)
Garry Tan4e2da480aa0e
diff --git a/changelog.d/capy-gst-2128-access-hints-resolve-path.md b/changelog.d/capy-gst-2128-access-hints-resolve-path.mdnew file mode 100644index 0000000000..d59de49069--- /dev/null+++ b/changelog.d/capy-gst-2128-access-hints-resolve-path.md@@ -0,0 +1,6 @@+---+important: security+---+- A member's connection can no longer use `sources_resolve_path` to learn that a page it may not read exists. A guessed path for a restricted or owner-private page now gets the same answer as a path with no file and no page, even when an older readable copy of that path was deleted. The brain owner's own connections read exactly what they read before. (#8761)+- The `gbrain access rule add` commands printed by `gbrain doctor`, the labelled-only create refusal, `gbrain access serves` and the federation exposure check now include the owner in `--readers`, so they are accepted on a brain with an owner set. Re-running `gbrain access serves` after applying its printed fix now reports the rule as already in place.+- `sources_resolve_path` on a plain directory with no Git history now refuses with `sync_not_applicable` (reason `plain_directory`) and a next step, instead of a raw Git error. Contributed by @Kkkkkuro.diff --git a/docs/guides/resolve-paths.md b/docs/guides/resolve-paths.mdindex 793aed57c8..232436eef1 100644--- a/docs/guides/resolve-paths.md+++ b/docs/guides/resolve-paths.md@@ -22,7 +22,9 @@ git diff --name-only HEAD~1 | gbrain sources resolve-path default --stdin --jsonMCP: `sources_resolve_path` with `{ id, paths }` (read scope, confined to the caller's sources). At most 200 paths per call. Pathsare relative to the source root, or to the Git top level when they start with the source's subpath. Read-only: Git object reads-and bounded lookups on existing indexes; nothing is scheduled or written.+and bounded lookups on existing indexes; nothing is scheduled or written. Over MCP the caller's read policy applies: a path+whose page the caller may not read (live or deleted) answers exactly as a path with no file and no page, and `slug_owner` never+names a page it may not read. A plain directory with no Git history refuses `sync_not_applicable` (reason `plain_directory`).Each path returns the `slug` sync gives it, `in_synced_commit` / `at_head`, `skipped` (with `typed_frontmatter` for a metafile),`hold`, `page` (id, chunks, unembedded), `embedding_hold` (a large page's held or capped embedding budget), `slug_owner` (a live page that owns the slug with another origin), `frontmatter_slug`diff --git a/src/commands/access-serves.ts b/src/commands/access-serves.tsindex 8cec5b4377..8f02de459e 100644--- a/src/commands/access-serves.ts+++ b/src/commands/access-serves.ts@@ -26,7 +26,7 @@ import { usageError } from '../cli/cli-error.ts';import { setCliExitVerdict } from '../core/cli-force-exit.ts';import { normalizeRule, type FolderRule } from '../core/access/folder-rules.ts';import { addMember, getMember, parseIdList } from '../core/access/members.ts';-import { ownerMemberId } from '../core/access/owner.ts';+import { hintReaders, ownerMemberId } from '../core/access/owner.ts';import { stdioMemberId } from '../core/access/caller.ts';import { brainServes, SERVES_KEY, unlabeledPeopleFolders, type PeopleFolder, type ServesView } from '../core/access/serves.ts';import { setMemberLabelledOnly } from '../core/access/labelled-only.ts';@@ -114,15 +114,19 @@ async function plan(engine: BrainEngine, roster: string[], sourceId: string, labmembers.push(entry);}const base = await currentFolderRules(engine, sourceId);- const rules: ServesReceipt['rules'] = roster.map((id) => {+ const hinted = await Promise.all(roster.map(id => hintReaders(engine, [id])));+ const rules: ServesReceipt['rules'] = roster.map((id, i) => {const r = normalizeRule({ prefix: `people/${id}/`, readable_by: [id] });if ('problem' in r) return { id, prefix: `people/${id}/`, readers: [id], action: 'blocked', detail: r.problem };const clash = base.find(b => b.prefix === r.rule.prefix || b.id === r.rule.id);if (!clash) return { id: r.rule.id, prefix: r.rule.prefix, readers: r.rule.readable_by, action: 'add' };- if (clash.prefix === r.rule.prefix && clash.readable_by.length === 1 && clash.readable_by[0] === `member:${id}`) return { id: clash.id, prefix: clash.prefix, readers: clash.readable_by, action: 'keep' };+ const hint = new Set(hinted[i]!.split(',').map(reader => `member:${reader}`));+ const settled = (clash.readable_by.length === 1 && clash.readable_by[0] === `member:${id}`)+ || (clash.readable_by.length === hint.size && clash.readable_by.every(p => hint.has(p)));+ if (clash.prefix === r.rule.prefix && settled) return { id: clash.id, prefix: clash.prefix, readers: clash.readable_by, action: 'keep' };const readers = clash.readable_by.map(p => p.replace(/^member:/, '')).join(', ');return { id: r.rule.id, prefix: r.rule.prefix, readers: r.rule.readable_by, action: 'blocked',- detail: `rule "${clash.id}" on ${clash.prefix} already exists, readable by ${readers}, not by ${id} alone; change it with gbrain access rule add --id ${clash.id} --prefix ${clash.prefix} --readers ${id} --source ${sourceId}` };+ detail: `rule "${clash.id}" on ${clash.prefix} already exists, readable by ${readers}, not by ${id} alone; change it with gbrain access rule add --id ${clash.id} --prefix ${clash.prefix} --readers ${hinted[i]} --source ${sourceId}` };});const keys: ServesReceipt['keys'] = [];for (const id of roster) {diff --git a/src/commands/doctor/checks/unlabeled-people-folders.ts b/src/commands/doctor/checks/unlabeled-people-folders.tsindex 83c10481b8..1ad5b80142 100644--- a/src/commands/doctor/checks/unlabeled-people-folders.ts+++ b/src/commands/doctor/checks/unlabeled-people-folders.ts@@ -9,6 +9,7 @@ import type { Check } from '../../doctor.ts';import { agentFix, checkError, infoCheck } from '../check-fix.ts';import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';import { brainServes, unlabeledPeopleFolders } from '../../../core/access/serves.ts';+import { hintReaders } from '../../../core/access/owner.ts';const NAME = 'unlabeled_people_folders';@@ -29,11 +30,12 @@ async function runUnlabeledPeopleFolders(ctx: DoctorContext): Promise<Check[]> {}const first = unlabeled[0]!;const member = first.prefix.slice('people/'.length, -1);+ const readers = await hintReaders(engine, [member]);checks.push({ name: 'unlabeled_people_folders', status: 'fail', details: { serves: view, folders: unlabeled },message: `This brain serves several people (${why}), but ${unlabeled.length} people folder(s) hold pages with no folder rule naming their readers: `+ `${unlabeled.map(u => `${u.source}:${u.prefix} (${u.pages} page(s))`).join(', ')}. Every caller entitled to the source reads them, so one person's bot can read another person's notes.`,- fix: agentFix(['gbrain', 'access', 'rule', 'add', '--prefix', first.prefix, '--readers', member, '--source', first.source],- `Previews a rule keeping ${first.prefix} to ${member} (writes nothing without --yes). Ask the user who should read each folder; a folder that is not one person's goes to its own readers or a group.`, NAME,+ fix: agentFix(['gbrain', 'access', 'rule', 'add', '--prefix', first.prefix, '--readers', readers, '--source', first.source],+ `Previews a rule keeping ${first.prefix} to ${readers.split(',').join(', ')} (writes nothing without --yes). Ask the user who should read each folder; a folder that is not one person's goes to its own readers or a group.`, NAME,{ ask_user: true, docs: 'docs/guides/setup.md#a-brain-that-serves-several-people' }) });} catch (error) {checks.push(checkError(NAME, `run ${NAME}`, error));diff --git a/src/core/access/federation-exposure.ts b/src/core/access/federation-exposure.tsindex cd9ea9c823..aa2d5060af 100644--- a/src/core/access/federation-exposure.ts+++ b/src/core/access/federation-exposure.ts@@ -17,6 +17,7 @@import { shellQuote } from '../agent-output.ts';import type { BrainEngine } from '../engine.ts';import { privacyPartSql, remotePrivacyRelaxed } from '../search/private-visibility.ts';+import { hintReaders } from './owner.ts';export interface PagesExposed {count: number;@@ -49,7 +50,7 @@ export async function remotePagesFederationExposes(engine: BrainEngine, sourceIdexamples: rows.map(r => ({ slug: r.slug, member: r.member })),check: shellQuote(['gbrain', 'access', 'who-can-read', first.slug, '--source', sourceId]),restrict: bound- ? shellQuote(['gbrain', 'access', 'rule', 'add', '--prefix', bound.slug.slice(0, bound.slug.lastIndexOf('/') + 1), '--readers', bound.member!, '--source', sourceId])+ ? shellQuote(['gbrain', 'access', 'rule', 'add', '--prefix', bound.slug.slice(0, bound.slug.lastIndexOf('/') + 1), '--readers', await hintReaders(engine, [bound.member!]), '--source', sourceId]): `gbrain access rule add --prefix <folder/> --readers <member> --source ${shellQuote([sourceId])}`,};}diff --git a/src/core/access/owner.ts b/src/core/access/owner.tsindex 7ccc246611..9968870eee 100644--- a/src/core/access/owner.ts+++ b/src/core/access/owner.ts@@ -74,6 +74,15 @@ export function readersOmitOwnerError(owner: string, live: boolean, principals:{ reason: 'readers_omit_owner' });}+/**+ * GST-2128: the reader list a printed `gbrain access rule add` command names: `readers` plus the owner readersOwner names,+ * when one is set and not listed, so pasting it is not refused readers_omit_owner. An unreadable owner prints `readers`.+ */+export async function hintReaders(engine: BrainEngine, readers: string[]): Promise<string> {+ const owner = await readersOwner(engine).then(r => r.owner, () => null);+ return (owner && !readers.includes(owner) ? [...readers, owner] : readers).join(',');+}+/** GST-2490: is access.owner_reads_restricted on? Unset, anything but '1', or unreadable is off (nothing widens). */export async function ownerReadsRestrictedOn(engine: Pick<BrainEngine, 'getConfig'>): Promise<boolean> {try {diff --git a/src/core/ops/sources.ts b/src/core/ops/sources.tsindex fd74444f6e..a940dd7325 100644--- a/src/core/ops/sources.ts+++ b/src/core/ops/sources.ts@@ -10,7 +10,7 @@ import type { Operation, OperationContext } from './contract.ts';import { authTransport, opError } from './contract.ts';import { hostFix, hostOnlyError, paramUse } from './op-fix.ts';import { isValidSourceId } from '../source-id.ts';-import { assertSourceInCallerScope, assertSourceInCallerWriteScope, sourceScopeOpts } from './context.ts';+import { assertSourceInCallerScope, assertSourceInCallerWriteScope, readPolicyOpts, sourceScopeOpts } from './context.ts';import { resolveAuthCapabilities, resolveGrantDiagnosis } from '../harness/capabilities.ts';// --- v0.28: whoami + sources management ---@@ -379,7 +379,7 @@ const sources_resolve_path: Operation = {handler: async (ctx, p) => {assertSourceInCallerScope(ctx, p.id as string);const { resolveSourcePaths } = await import('../persistence/resolve-paths.ts');- return resolveSourcePaths(ctx.engine, p.id as string, (p.paths as unknown[] ?? []).map(String));+ return resolveSourcePaths(ctx.engine, p.id as string, (p.paths as unknown[] ?? []).map(String), await readPolicyOpts(ctx, { sourceId: p.id as string }));},cliHints: { name: 'sources_resolve_path', hidden: true },};diff --git a/src/core/persistence/page-visibility.ts b/src/core/persistence/page-visibility.tsindex 77e16cfb22..ca26f531c7 100644--- a/src/core/persistence/page-visibility.ts+++ b/src/core/persistence/page-visibility.ts@@ -133,9 +133,12 @@ export async function assertLabelledOnlyCreate(engine: SqlEngine, authority: Wri[authority.sourceId, slug, folder.satisfiedRuleIds]);if (row?.named === true) return;const reader = [...named].sort().find(p => p.startsWith('member:'))?.slice('member:'.length) ?? [...named].sort()[0] ?? 'alice-example';+ const [owner] = await engine.executeRaw<{ value: string }>('SELECT value FROM config WHERE key = $1', [OWNER_MEMBER_KEY]).catch(() => []);+ const ownerId = owner?.value?.trim();+ const ruleReaders = ownerId && ACCESS_ID_RE.test(ownerId) && ownerId !== reader ? `${reader},${ownerId}` : reader;throw opError('access_labelled_only_unnamed', `This connection is labelled-only, so a page it creates must name it: add readable_by: [${reader}] (or one of its groups) to the frontmatter of ${slug}, or ask the user for a page or folder rule over it that lists them; nothing was written.`,`Add readable_by: [${reader}] (or one of its groups) to the frontmatter and submit again with a new request_id, or ask the user to add a page or folder rule that lists it on the brain host.`,- { fix: hostFix({ remote: true, transport: 'http' }, ['gbrain', 'access', 'rule', 'add', '--slug', slug, '--readers', reader, '--source', authority.sourceId],+ { fix: hostFix({ remote: true, transport: 'http' }, ['gbrain', 'access', 'rule', 'add', '--slug', slug, '--readers', ruleReaders, '--source', authority.sourceId],'A page rule over this slug that lists the labelled-only member, so the page it creates names it. Adding readable_by to the page itself needs no operator.') });}diff --git a/src/core/persistence/resolve-paths.ts b/src/core/persistence/resolve-paths.tsindex 25938b1271..9b46c9ddaa 100644--- a/src/core/persistence/resolve-paths.ts+++ b/src/core/persistence/resolve-paths.ts@@ -23,10 +23,17 @@* (`embedding-state.ts`); `pending_embedding` is searchable by keyword on a brain keyless by choice.* - `fix`: the existing command that moves the path on (never run here), or null when nothing is owed.* Read-only: Git object reads and bounded lookups on existing indexes; nothing is scheduled or written.+ *+ * #8761: under a caller's read `policy` (the remote operation passes it), a path whose page rows the caller may not+ * read, live or deleted, answers exactly as a path with no file and no page, and a slug owner it may not read is not+ * named, so a guessed path never tells a member that someone else's restricted or private page exists.*/import type { BrainEngine } from '../engine.ts';+import type { PageReadPolicy } from '../types.ts';+import { pageReadFilter } from '../search/read-policy-sql.ts';import type { Action } from '../agent-output.ts';import { literalPathspecs } from '../git-env.ts';+import { classifyGitCheckout } from '../git-checkout.ts';import { existsSync, realpathSync } from 'node:fs';import { isHostAbsolutePath } from '../path-confine.ts';import { opError } from '../ops/contract.ts';@@ -108,7 +115,7 @@ function headCommit(gitRoot: string): string | null {const readFix = (why: string, argv: string[], verify: string[]): Action => ({ argv, consent: [], actor: 'agent', requires_exclusive: false, why, verify: { argv: verify }, docs: DOCS });/** Resolves `paths` of one source; refuses more than RESOLVE_PATHS_MAX paths and a source without a local Git checkout. */-export async function resolveSourcePaths(engine: BrainEngine, sourceId: string, paths: string[]): Promise<ResolvedPaths> {+export async function resolveSourcePaths(engine: BrainEngine, sourceId: string, paths: string[], policy?: PageReadPolicy): Promise<ResolvedPaths> {if (!paths.length) throw opError('invalid_params', 'Pass at least one file path.', 'Name the repo-relative paths a commit changed, for example projects/acme-example/index.md.');if (paths.length > RESOLVE_PATHS_MAX) throw opError('invalid_params', `At most ${RESOLVE_PATHS_MAX} paths per call (got ${paths.length}).`, 'Split the paths into batches.');const [source] = await engine.executeRaw<{ local_path: string | null; last_commit: string | null; config: Record<string, unknown> | null; archived: boolean | null }>(@@ -125,7 +132,17 @@ export async function resolveSourcePaths(engine: BrainEngine, sourceId: string,if (!source.local_path || !existsSync(source.local_path)) throw opError('not_found', `Source ${sourceId} has no Git checkout on this host, so its paths cannot be resolved here.`,'Run this on the host that holds the checkout (gbrain sources status names its path).', { fix: readFix('Shows where the source checkout lives.', status, status) });const root = realpathSync(source.local_path);- const gitRoot = realpathSync(syncGit(root, ['rev-parse', '--show-toplevel']).trim());+ let top: string;+ try {+ top = syncGit(root, ['rev-parse', '--show-toplevel']);+ } catch (error) {+ const gitExpected = source.last_commit !== null || source.config?.canonical === 'git' || source.config?.managed_clone === true || !!source.config?.remote_url;+ if (gitExpected || classifyGitCheckout(root) !== 'not_git') throw error;+ throw opError('sync_not_applicable', `Source ${sourceId} is a plain directory with no Git history, so its paths cannot be resolved.`,+ 'For a Git landing check, pass a source that is a Git checkout. To read an indexed page, use get_page with its slug and source.',+ { reason: 'plain_directory', fix: readFix('Shows how the source is registered.', status, status) });+ }+ const gitRoot = realpathSync(top.trim());const context = { root, gitRoot, sourceId, source: { last_commit: source.last_commit, config: source.config ?? {} } };const { scope, eligible, sourcePath } = await loadSyncEligibility(engine, context);const managed = await persistenceEnabledForHealth(engine).catch(() => false);@@ -142,13 +159,21 @@ export async function resolveSourcePaths(engine: BrainEngine, sourceId: string,const atHead = head ? lsTree(gitRoot, head, gitPaths) : null;const heads = readBlobContents(gitRoot, [...synced.values()].filter(row => row.size <= 256 * 1024));const origins = normalized.map(p => p.origin);- const pages = await engine.executeRaw<{ id: number; slug: string; source_path: string; deleted: boolean }>(- 'SELECT id, slug, source_path, deleted_at IS NOT NULL AS deleted FROM pages WHERE source_id=$1 AND source_path = ANY($2::text[]) ORDER BY deleted_at IS NOT NULL, id DESC', [sourceId, origins]);+ const pageParams: unknown[] = [sourceId, origins];+ const pages = await engine.executeRaw<{ id: number; slug: string; source_path: string; deleted: boolean; readable: boolean }>(+ `SELECT id, slug, source_path, deleted_at IS NOT NULL AS deleted, COALESCE(${pageReadFilter('pages', policy, pageParams)}, false) AS readable+ FROM pages WHERE source_id=$1 AND source_path = ANY($2::text[]) ORDER BY deleted_at IS NOT NULL, id DESC`, pageParams);const byOrigin = new Map<string, typeof pages[number]>();- for (const page of pages) if (!byOrigin.has(page.source_path)) byOrigin.set(page.source_path, page);+ const hidden = new Set<string>();+ for (const page of pages) {+ if (byOrigin.has(page.source_path) || hidden.has(page.source_path)) continue;+ if (page.readable === true) byOrigin.set(page.source_path, page);+ else hidden.add(page.source_path);+ }const slugs = [...new Set(origins.map(origin => resolveSlugForPath(origin)).filter(Boolean))];+ const ownerParams: unknown[] = [sourceId, slugs];const owners = new Map((await engine.executeRaw<{ slug: string; source_path: string | null }>(- 'SELECT slug, source_path FROM pages WHERE source_id=$1 AND deleted_at IS NULL AND slug = ANY($2::text[])', [sourceId, slugs])).map(row => [row.slug, row]));+ `SELECT slug, source_path FROM pages WHERE source_id=$1 AND deleted_at IS NULL AND slug = ANY($2::text[]) AND COALESCE(${pageReadFilter('pages', policy, ownerParams)}, false)`, ownerParams)).map(row => [row.slug, row]));const liveSlugs = [...new Set([...byOrigin.values()].filter(page => !page.deleted).map(page => page.slug))];const searchability = await readPageSearchability(engine, sourceId, liveSlugs);const embedding = new Map([...searchability].filter(([, page]) => page.embedding_state).map(([slug, page]) => [slug, { state: page.embedding_state!, chunks: page.chunks, unembedded: page.unembedded }]));@@ -159,8 +184,8 @@ export async function resolveSourcePaths(engine: BrainEngine, sourceId: string,const out: ResolvedPath[] = [];for (const p of normalized) {const slug = resolveSlugForPath(p.origin);- const committed = synced.get(p.gitPath);- const current = atHead ? atHead.get(p.gitPath) : undefined;+ const committed = hidden.has(p.origin) ? undefined : synced.get(p.gitPath);+ const current = atHead && !hidden.has(p.origin) ? atHead.get(p.gitPath) : undefined;const page = byOrigin.get(p.origin) ?? null;const owner = owners.get(slug);const slugOwner = owner && owner.source_path !== p.origin ? { slug: owner.slug, source_path: owner.source_path } : null;diff --git a/test/access-rule-hints-owner-gst2128.test.ts b/test/access-rule-hints-owner-gst2128.test.tsnew file mode 100644index 0000000000..61558a7d3b--- /dev/null+++ b/test/access-rule-hints-owner-gst2128.test.ts@@ -0,0 +1,111 @@+/**+ * GST-2128 item 1: since GST-2698 (#9371), `gbrain access rule add` refuses a reader list that leaves out the brain owner+ * unless --allow-without-owner. Every hint that prints a rule-add command for a named member must print one that command+ * accepts.+ *+ * 1. Protects: with an owner set, the doctor check unlabeled_people_folders, the labelled-only create refusal's host fix+ * (access_labelled_only_unnamed), the serves receipt's clash line and the federation exposure's restrict command each+ * print readers that include the owner, and pasting the printed command previews without readers_omit_owner; with no+ * owner set each prints the member alone, as before.+ * 2. Fails when: a hint prints a list without the owner on a brain that has one, or adds an owner where none is set.+ * 3. Real seams: the doctor entry, assertLabelledOnlyCreate, labelSeveral and remotePagesFederationExposes on a+ * repo-less source, then the gbrain access rule CLI module on the printed argv, PGLite and Postgres.+ */+import { afterAll, beforeAll, describe, expect, spyOn, test } from 'bun:test';+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 { runAccessRules } from '../src/commands/access-rules.ts';+import { labelSeveral } from '../src/commands/access-serves.ts';+import { unlabeledPeopleFoldersEntry } from '../src/commands/doctor/checks/unlabeled-people-folders.ts';+import type { DoctorContext } from '../src/commands/doctor/context.ts';+import { assertLabelledOnlyCreate } from '../src/core/persistence/page-visibility.ts';+import type { WriteAuthority } from '../src/core/persistence/model.ts';+import { setMemberLabelledOnly } from '../src/core/access/labelled-only.ts';+import { remotePagesFederationExposes } from '../src/core/access/federation-exposure.ts';+import { importFromContent } from '../src/core/import-file.ts';+import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/private-visibility.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { testBackends } from './helpers/test-backends.ts';+import { withEnv } from './helpers/with-env.ts';++const OWNER = 'owner-example';++async function preview(engine: BrainEngine, argv: string[]): Promise<{ reason?: string } | null> {+ expect(argv.slice(0, 4)).toEqual(['gbrain', 'access', 'rule', 'add']);+ const spy = spyOn(console, 'log').mockImplementation(() => {});+ try { await runAccessRules(engine, 'rule', [...argv.slice(3), '--json'], true); return null; }+ catch (e: any) { return { reason: e.reason ?? e.code }; }+ finally { spy.mockRestore(); }+}++const readers = (argv: string[]) => argv[argv.indexOf('--readers') + 1];++for (const backend of testBackends()) {+ describe(`printed rule-add hints keep the owner (${backend}, repo-less)`, () => {+ let engine: BrainEngine;+ let close: () => Promise<void>;+ let home: string;+ let tokenId: string;+ beforeAll(async () => {+ if (backend === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));+ else { const pglite = new PGLiteEngine(); await pglite.connect({ engine: 'pglite' } as never); await pglite.initSchema(); engine = pglite; close = () => pglite.disconnect(); }+ await engine.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');+ home = mkdtempSync(join(tmpdir(), 'gbrain-hints-'));+ for (const id of [OWNER, 'alice-example', 'bob-example', 'bot-example']) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1) ON CONFLICT DO NOTHING', [id]);+ await setMemberLabelledOnly(engine, 'bot-example', true, { remote: false });+ await engine.setConfig('access.serves', 'several');+ for (const slug of ['people/alice-example/note-one', 'notes/federated-one']) await importFromContent(engine, slug, `---\ntitle: ${slug}\n---\nA synthetic note.\n`, { sourceId: 'default', noEmbed: true });+ [{ id: tokenId }] = await engine.executeRaw<{ id: string }>("INSERT INTO access_tokens (name, token_hash, scopes, member_id) VALUES ('alice-key', 'hash-alice-example', ARRAY['read','write']::text[], 'alice-example') RETURNING id::text AS id");+ await engine.executeRaw('INSERT INTO access_tokens (name, token_hash, scopes, member_id) VALUES ($1, $2, ARRAY[\'read\',\'write\']::text[], $3)', ['bot-key', 'hash-bot-example', 'bot-example']);+ await engine.transaction(async tx => {+ await tx.executeRaw("SELECT set_config('gbrain.write_principal_kind', 'legacy_token', true), set_config('gbrain.write_principal_id', $1, true)", [tokenId]);+ await tx.executeRaw("UPDATE pages SET title = 'Federated one' WHERE slug = 'notes/federated-one'");+ });+ __resetPrivateVisibilityCacheForTests();+ }, 180_000);+ afterAll(async () => { await close?.(); rmSync(home, { recursive: true, force: true }); });++ const hints = async (): Promise<Record<string, string[]>> => {+ const doctor = (await unlabeledPeopleFoldersEntry.run({ engine } as unknown as DoctorContext) as Array<Record<string, any>>)[0]!;+ const [{ id: botToken }] = await engine.executeRaw<{ id: string }>("SELECT id::text AS id FROM access_tokens WHERE name = 'bot-key'");+ const authority = { version: 1, principal: { kind: 'legacy_token', id: botToken }, remote: true, excludePrivate: true, sourceId: 'default',+ sourceIncarnation: 'unused', scopes: ['read', 'write'], operations: null, slugPrefixes: null } as unknown as WriteAuthority;+ const unnamed = await assertLabelledOnlyCreate(engine, authority, 'notes/new-example', null, {}).then(() => null, (e: any) => e);+ expect(unnamed?.code).toBe('access_labelled_only_unnamed');+ const exposed = await remotePagesFederationExposes(engine, 'default');+ return { doctor: doctor.fix.argv, labelled_only: unnamed.fix.argv, federation: exposed!.restrict.split(' ') };+ };++ test('with an owner set each hint names the owner and its command previews; with none each names the member alone', async () => {+ await engine.executeRaw("DELETE FROM config WHERE key = 'access.owner_member'");+ const before = await hints();+ expect(Object.fromEntries(Object.entries(before).map(([k, v]) => [k, readers(v)])))+ .toEqual({ doctor: 'alice-example', labelled_only: 'bot-example', federation: 'alice-example' });++ await engine.setConfig('access.owner_member', OWNER);+ const after = await hints();+ expect(Object.fromEntries(Object.entries(after).map(([k, v]) => [k, readers(v)])))+ .toEqual({ doctor: `alice-example,${OWNER}`, labelled_only: `bot-example,${OWNER}`, federation: `alice-example,${OWNER}` });+ for (const argv of Object.values(after)) expect(await preview(engine, argv)).toBeNull();+ for (const argv of Object.values(before)) expect(await preview(engine, argv)).toEqual({ reason: 'readers_omit_owner' });+ }, 120_000);++ test('the serves receipt line for a clashing people/ rule prints a command that previews with an owner set', async () => {+ await engine.setConfig('access.owner_member', OWNER);+ const spy = spyOn(console, 'log').mockImplementation(() => {});+ try { await runAccessRules(engine, 'rule', ['add', '--prefix', 'people/alice-example/', '--readers', `bob-example,${OWNER}`, '--yes', '--json'], true); } finally { spy.mockRestore(); }+ const receipt = await withEnv({ GBRAIN_HOME: home }, () => labelSeveral(engine, ['alice-example'], 'default', false, true));+ const detail = receipt.rules.find(r => r.prefix === 'people/alice-example/')!.detail!;+ const argv = detail.slice(detail.indexOf('gbrain access rule add')).split(' ');+ expect(readers(argv)).toBe(`alice-example,${OWNER}`);+ expect(await preview(engine, argv)).toBeNull();+ const quiet = spyOn(console, 'log').mockImplementation(() => {});+ try { await runAccessRules(engine, 'rule', [...argv.slice(3), '--yes', '--json'], true); } finally { quiet.mockRestore(); }+ const again = await withEnv({ GBRAIN_HOME: home }, () => labelSeveral(engine, ['alice-example'], 'default', false, true));+ expect(again.rules.find(r => r.prefix === 'people/alice-example/')!.action).toBe('keep');+ }, 120_000);+ });+}diff --git a/test/derived-origin-carry-managed.test.ts b/test/derived-origin-carry-managed.test.tsindex 701e325ee6..07f05970b2 100644--- a/test/derived-origin-carry-managed.test.ts+++ b/test/derived-origin-carry-managed.test.ts@@ -65,7 +65,7 @@ function fileText(k: string, shape: Shape): string {};return ['---', `title: ${k} ${shape}`, ...fm[shape], '---', '', `Harbor ledger planning notes ${canaryOf(k, shape)} for the roadmap.`, ''].join('\n');}-const KEYS = ['gitrm', 'purge', 'remote', 'midpurge', 'preview'];+const KEYS = ['gitrm', 'purge', 'remote', 'midpurge', 'preview', 'finish'];/** A private origin, a public meeting, an event derived from the origin whose depth is the meeting, and an event whose depth is the origin. */const EVENT_SLUGS = { origin: 'notes/event-origin', depth: 'notes/event-depth', event: 'notes/event-event', closed: 'notes/event-closed' };const EVENT_FILES: Record<keyof typeof EVENT_SLUGS, string[]> = {@@ -249,6 +249,31 @@ for (const backend of testBackends()) {await expectCarried(engine, root, k, files);}, { setup, databaseUrl }), 300_000);+ test('a held explicit purge finished as a new request writes its purge tombstones on a managed Git source (GST-2128, #9361 finding 4)', () => managedBrain(async ({ engine, root, ctx }) => {+ await performManagedSync(engine, OPTS);+ const k = 'finish', origin = slugOf(k, 'origin');+ const files = await before(engine, root, k);+ const tombstones = async () => (await engine.executeRaw<{ request_id: string }>(`SELECT r.request_id::text AS request_id FROM page_purges t+ JOIN persistence_requests r ON r.id = t.request_id WHERE t.source_id = 'default' AND t.slug = $1`, [origin])).map(r => r.request_id);+ const purge = async (requestId: string) => operationsByName.delete_page!.handler(ctx, { slug: origin, purge: true, request_id: requestId,+ expected_revision: (await engine.readPageSnapshot(origin, { sourceId: 'default', includeDeleted: true }))!.revision }) as Promise<Record<string, any>>;+ const failing = spyOn(preparedMaintenance, 'publishMaintenancePages').mockImplementation(async () => { throw new Error('forced label failure'); });+ let held: Record<string, any>;+ try { held = await purge(randomUUID()); } finally { failing.mockRestore(); }+ expect(held.purge_held?.code).toBe('derived_origin_parked');+ expect(held.purge_held.message).toContain(`gbrain delete ${origin} --purge --source-id default`);+ expect(await tombstones()).toEqual([]);+ expect((await page(engine, origin))?.deleted_at).not.toBeNull();+ const second = randomUUID();+ const done = await purge(second);+ expect(done.status).toBe('purged');+ expect(done.retention).toBeUndefined();+ const written = await tombstones();+ expect(written.length).toBeGreaterThan(0);+ expect(new Set(written)).toEqual(new Set([second]));+ 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');diff --git a/test/derived-origin-carry-private.test.ts b/test/derived-origin-carry-private.test.tsindex 912061501e..f9066d5b89 100644--- a/test/derived-origin-carry-private.test.ts+++ b/test/derived-origin-carry-private.test.ts@@ -526,6 +526,27 @@ for (const kind of testBackends()) {await expectCarriedHidden(k);}, 240_000);+ test('restoring a parked origin drops its park: doctor stops naming it, and the next purge run deletes the park row (GST-2128, #9347 finding 6)', async () => {+ const k = 'restorepark';+ await seed(k);+ const origin = slugOf(k, 'origin');+ const failing = spyOn(labelWrite, 'importAtRevision').mockImplementation(async () => { throw new Error('forced label failure'); });+ let held: Record<string, any>;+ try { held = JSON.parse((await localCall('delete_page', { slug: origin, purge: true, request_id: randomUUID(), expected_revision: await revision(origin) })).text); }+ finally { failing.mockRestore(); }+ expect(held.purge_held?.code).toBe('derived_origin_parked');+ const id = String((await row(origin))!.id);+ const parkRows = async () => (await engine.executeRaw('SELECT 1 FROM op_checkpoints WHERE op = $1 AND fingerprint = $2', ['derived-origin-park', id])).length;+ expect((await readDerivedOriginParks(engine)).map(park => park.slug)).toContain(origin);+ const restored = await localCall('restore_page', { slug: origin, request_id: randomUUID(), expected_revision: await revision(origin) });+ expect(restored.isError ? restored.text : '').toBe('');+ expect((await row(origin))?.deleted_at).toBeNull();+ expect((await readDerivedOriginParks(engine)).map(park => park.slug)).not.toContain(origin);+ await purgeDeletedPagesCoordinated(engine, 72, { automatic: true });+ expect(await parkRows()).toBe(0);+ expect((await row(origin))?.deleted_at).toBeNull();+ }, 240_000);+test('an explicit purge rechecks inside its transaction: a derived page written after the carry keeps the origin soft-deleted and parked', async () => {const k = 'midsingle';await seed(k);diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex 8641430c30..2a47a65941 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -35,6 +35,7 @@ test/access-page-rule-fileless.test.tstest/access-qm-actor.test.tstest/access-raw-only-rules.test.tstest/access-room.test.ts+test/access-rule-hints-owner-gst2128.test.tstest/access-rule-owner-gst2698.test.tstest/access-token-grant-columns.test.tstest/access-ungranted-member-writes.test.tsdiff --git a/test/sources-resolve-path.test.ts b/test/sources-resolve-path.test.tsindex 43ff36e570..2e0cb7f012 100644--- a/test/sources-resolve-path.test.ts+++ b/test/sources-resolve-path.test.ts@@ -17,7 +17,8 @@ import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { claimWorktree } from '../src/core/persistence/ownership.ts';import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';import { performManagedSync } from '../src/core/persistence/sync-run.ts';-import { resolveSourcePaths, RESOLVE_PATHS_MAX } from '../src/core/persistence/resolve-paths.ts';+import { resolveSourcePaths, RESOLVE_PATHS_MAX, type ResolvedPath } from '../src/core/persistence/resolve-paths.ts';+import { dispatchToolCall } from '../src/mcp/dispatch.ts';import { operationsByName } from '../src/core/operations.ts';import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';import { testBackends } from './helpers/test-backends.ts';@@ -174,3 +175,74 @@ test('a large page whose embedding budget is held reads pending_embedding with texpect(skipped).toMatchObject({ state: 'keyword_only', searchable: 'keyword', embedding_hold: null });}), 240_000);++const RESTRICTED = (fm: string, title: string) => note(title).replace('---\n', `---\n${fm}\n`);++test('a page the caller may not read answers exactly like a path with no file or page; the owner-bound caller reads it as the local operator does (#8761)', () => each(async engine => {+ for (const id of ['owner-example', 'alice-example', 'bob-example']) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1) ON CONFLICT DO NOTHING', [id]);+ await engine.setConfig('access.owner_member', 'owner-example');+ try {+ const s = await source(engine, {+ 'team/kept.md': RESTRICTED('readable_by: [alice-example, owner-example]', 'Kept'),+ 'team/private.md': RESTRICTED('visibility: private', 'Private'),+ 'team/open.md': note('Open'),+ });+ await s.sync();+ const columns = (await engine.executeRaw<{ name: string }>(+ "SELECT column_name AS name FROM information_schema.columns WHERE table_name = 'pages' AND column_name NOT IN ('id', 'slug', 'source_path', 'deleted_at') AND is_generated = 'NEVER' ORDER BY ordinal_position"))+ .map(c => c.name).join(', ');+ await unmanaged(engine, () => engine.executeRaw(+ `INSERT INTO pages (slug, source_path, deleted_at, ${columns}) SELECT 'team/old-kept', 'team/kept.md', now() - interval '1 day', ${columns} FROM pages WHERE source_id = $1 AND slug = 'team/open'`, [s.id]));+ const paths = ['team/kept.md', 'team/private.md', 'team/open.md', 'team/KEPT.md', 'team/nothing-here.md'];+ const call = async (stdioMember?: string) => {+ const result = await dispatchToolCall(engine, 'sources_resolve_path', { id: s.id, paths },+ { remote: true, transport: 'stdio', sourceId: s.id, ...(stdioMember ? { stdioMember } : {}) });+ expect(result.isError).toBeFalsy();+ return (JSON.parse(result.content[0]!.text) as Awaited<ReturnType<typeof resolveSourcePaths>>).paths;+ };+ const answer = ({ path: _p, source_path: _s, slug: _g, ...rest }: ResolvedPath) => rest;+ for (const caller of ['bob-example', undefined]) {+ const seen = await call(caller);+ const nothing = answer(seen[4]!);+ expect(nothing.state).toBe('deleted');+ expect(answer(seen[0]!)).toEqual(nothing);+ expect(answer(seen[1]!)).toEqual(nothing);+ expect(answer(seen[3]!)).toEqual(nothing);+ expect(seen[2]!.page?.slug).toBe('team/open');+ expect(JSON.stringify(seen)).not.toContain('Kept');+ }+ const alice = await call('alice-example');+ expect(alice[0]!.page?.slug).toBe('team/kept');+ expect(alice[3]!.slug_owner).toEqual({ slug: 'team/kept', source_path: 'team/kept.md' });+ expect(answer(alice[1]!)).toEqual(answer(alice[4]!));+ const local = await s.resolve(...paths);+ expect(local.paths[1]!.page?.slug).toBe('team/private');+ expect(await call('owner-example')).toEqual(local.paths);+ } finally {+ await engine.executeRaw("DELETE FROM config WHERE key = 'access.owner_member'");+ }+}), 240_000);+++test('a plain directory with no Git anywhere above it refuses sync_not_applicable plain_directory; a broken .git above it or a source that expects Git keeps the Git error', () => each(async engine => {+ const plain = mkdtempSync(join(tmpdir(), 'gbrain-resolve-plain-'));+ const broken = join(home, `broken-${randomUUID().slice(0, 8)}`);+ mkdirSync(join(broken, '.git'), { recursive: true }); mkdirSync(join(broken, 'notes'), { recursive: true });+ const register = async (dir: string, config: Record<string, unknown> = {}) => {+ const id = `rslv-plain-${randomUUID().replace(/-/g, '').slice(0, 12)}`;+ await unmanaged(engine, () => engine.executeRaw('INSERT INTO sources(id,name,local_path,config) VALUES($1,$1,$2,$3::text::jsonb)', [id, dir, JSON.stringify(config)]));+ return id;+ };+ try {+ const id = await register(plain);+ const refusal = await resolveSourcePaths(engine, id, ['notes/one.md']).then(() => null, (e: any) => e);+ expect(refusal).toMatchObject({ code: 'sync_not_applicable', reason: 'plain_directory', fix: { argv: ['gbrain', 'sources', 'status', id, '--json'] } });+ expect(refusal.message).not.toContain(plain);+ for (const [dir, config] of [[join(broken, 'notes'), {}], [plain, { remote_url: 'https://example.invalid/repo.git' }], [plain, { managed_clone: true }]] as const) {+ const other = await resolveSourcePaths(engine, await register(dir, config), ['one.md']).then(() => null, (e: any) => e);+ expect(other?.code).not.toBe('sync_not_applicable');+ expect(String(other?.stderr ?? other?.message)).toContain('not a git repository');+ }+ } finally { rmSync(plain, { recursive: true, force: true }); }+}), 240_000);+