Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
GBRA-218 A1 enforcement: readers on every remote read and write path, spawn isolation, doctor checks
garrytan36b4293f1623
diff --git a/docs/guides/error-codes.md b/docs/guides/error-codes.mdindex af64295cb7..72992a8b1d 100644--- a/docs/guides/error-codes.md+++ b/docs/guides/error-codes.md@@ -3285,6 +3285,16 @@ More: [docs/guides/graph-ranking.md#rank_entities_timeout](../../docs/guides/graMore: [docs/guides/dream-patterns.md#child-read-budget](../../docs/guides/dream-patterns.md#child-read-budget)+### readable_by_invalid++<a id="readable_by_invalid"></a>++| Meaning | Why | Next step | Who acts · consent | Verify | Exit | Retryable |+|---|---|---|---|---|---|---|+| A remote write's readable_by is malformed, removes the field from a page that names readers, or would leave the page unreadable by the writer; nothing was written. | The request itself was wrong or no longer matches the brain; nothing was changed. | Correct the request using the message above, then retry. | agent | `repeat the read that failed` | 1 | no |++More: [docs/guides/write-refusals.md#readable_by_invalid](../../docs/guides/write-refusals.md#readable_by_invalid)+### receipt_conflict<a id="receipt_conflict"></a>diff --git a/docs/guides/write-refusals.md b/docs/guides/write-refusals.mdindex 0b17ccaedf..829f061596 100644--- a/docs/guides/write-refusals.md+++ b/docs/guides/write-refusals.md@@ -97,6 +97,7 @@ of making a duplicate.| <a id="no_context_ceiling"></a>`no_context_ceiling` | `no_context_ceiling` | A queued job under a cost cap you set stopped before a provider call: the model has a registered price, but gbrain knows no context-token ceiling for it, so it cannot reserve the most one call could cost. OpenRouter models hit this, because that provider declares no context window per model. The message names the model, and the JSON `fix` asks you before it reruns the command without your cap. Re-registering the price does not help. | Choose one. Rerun the command without your cap (the `fix` command, which replaces `--max-usd` with `--yes`): the default cap applies and this model runs unmetered with a warning. Or keep your cap: look up the model's context window on the provider's model page (OpenRouter lists it as `context_length`) and give it to the brain-host operator. `gbrain jobs list --group <id> --json` shows the stopped job. || <a id="no_pricing"></a>`no_pricing` | `no_pricing` | A cost cap you set (`--max-cost`, `--max-usd`, `--max-cost-usd`, or an explicit cap in config such as `cycle.extract_atoms.budget_usd`) cannot be enforced, because gbrain has no price for the model the run was about to call. The run stops before that call. Under a default cap an unpriced model warns and runs instead. The message names the model, provider and kind, and JSON output carries the same guidance as fields: `code`, `model`, `provider`, `kind`, `units`, `lookup`, `register_command`, `register_scope` and `docs`. | Look up the provider's current price for the model, for example by searching the web for its pricing page. A chat model needs USD per 1M input tokens and USD per 1M output tokens; an embedding or reranker model needs USD per 1M tokens. On the brain host, run `gbrain pricing set <model> --input <usd> --output <usd> --source <pricing-page-url>` (or `--rate <usd>`), then retry. An agent connected over MCP cannot register prices: give the command to the brain's operator. See [registering a model price](../operations/spend-controls.md#registering-a-model-price). || <a id="managed_raw_upload"></a>raw upload on a managed brain | `writer_coordinator_required` | `gbrain files upload-raw` of a small (git-storage) file whose `.raw/` destination is inside a managed canonical worktree. The persistence coordinator does not publish raw files yet, so the worktree refuses writes from outside it; starting a `gbrain serve` owner does not change that. The refusal comes before anything is created: no `.raw/` directory and no `files` row. With no storage backend, `file_upload` for such a page no longer suggests `upload-raw`. | Configure `storage` (supabase, s3 or local) in the gbrain config, then run the printed `gbrain files upload <file> --page <slug>`; check it with `gbrain files list <slug>`. |+| <a id="readable_by_invalid"></a>`readable_by_invalid` | `readable_by_invalid` | A remote write (MCP over HTTP or stdio) set frontmatter `readable_by` to a value that names no valid reader (an entry that is not a member id or `group:<id>`, an empty list, a non-string), removed `readable_by` from a page that names readers, or set readers that leave the page unreadable by the writing connection's member. The message names the bad entry; nothing was written. | Write the field as a list of member ids and `group:<id>` entries, for example `readable_by: [alice-example, group:team-a]`, keep your own member or group in it, and submit again with a new `request_id`. To widen or hand a page to other readers only, ask the user to change it on the brain host. || <a id="timeline_rows_would_be_removed"></a>`timeline_rows_would_be_removed` | `timeline_rows_would_be_removed` | A remote `put_page` (or a `put_pages` entry) has no Timeline section at all — no `<!-- timeline -->` sentinel and no `## Timeline` or `## History` heading — and would delete dated entries the page has, including rows `add_timeline_entry` added. `put_page` content replaces the whole page, so the omission must be explicit. Nothing was written. A section that is present but empty is a deliberate delete and is not refused; a local `gbrain put` without `--expected-revision` keeps the rows instead. | Re-read with `get_page include_content:true`, keep the Timeline section and resubmit with a new `request_id`; to remove every dated entry on purpose, resubmit the same content with `drop_timeline: true` (on put_pages, per entry). A successful write that deletes rows reports `timeline_rows_removed {count, earliest, latest, warning, fix}` (dates only); `get_versions` then `revert_version` restores them as new rows. || <a id="purge_lookup_timeout"></a>`purge_lookup_timeout` | `purge_lookup_timeout` | A page purge (`gbrain delete <slug> --purge`) ran one of the journal lookups that still execute inside its publication transaction (the lookup of stored intents for the slug, the re-check of requests admitted after its preparation scan or pending at that scan, the locking read of the requests to redact) and that statement hit the transaction's 5-second statement budget on a large `persistence_requests` table. The request failed terminally instead of being released as `database_contention` and claimed again: nothing of the page was removed or tombstoned, nothing in the journal was redacted, and later writes to the source move again. Do not assume the requested content was erased. The substring search for the page's claims no longer runs here: it runs at preparation in bounded keyset chunks under the preparation budget, and a sweep that outruns that budget twice ends [`preparation_stalled`](#preparation_stalled) instead. A committed purge's receipt reports `journal_sweep` (`scanned`, `ceiling`, `tail`, `pending_rechecked`, `complete`); `complete: false` means a cap was hit (the 10,000-match cap or the tail bound) and some requests were not tested. | If the receipt says the slug index `persistence_requests_slug_intent` is missing or INVALID, run `gbrain repair request-indexes --apply` (the `persistence_request_slug_intent_index` migration builds it online; the index serves the slug lookup, not the substring sweep), then submit the purge again with a new `request_id`. Otherwise submit it again with a new `request_id` (a retry with the same id returns this failure); if it times out again, report the request id with the `persistence_request_indexes` and `persistence_request_growth` entries of `gbrain doctor --json`. || <a id="purge_mirror_unwritable"></a>`purge_mirror_unwritable` | `purge_mirror_unwritable` | A fact or page purge appends a text-free `intent` line to the purge mirror (`<brain home>/.gbrain/purge-events.jsonl`) and fsyncs it before it commits, so a later restore of an older backup can replay the purge. The mirror could not be written (a directory in its place, a read-only home, a full disk), so the purge was refused: nothing was removed, tombstoned or redacted. Do not assume the requested content was erased. | Make the mirror a writable regular file owned by the brain's user (mode 0600), then run the purge again with a new `request_id`. |diff --git a/package.json b/package.jsonindex 607ce91985..2306c6e75d 100644--- a/package.json+++ b/package.json@@ -168,6 +168,7 @@"check:chunk-visibility": "bun scripts/check-chunk-visibility.ts","check:page-body-readers": "bun scripts/check-page-body-readers.ts","check:engine-sql-dynamic": "bun scripts/check-engine-sql-dynamic.ts",+ "check:remote-privacy-reads": "bun scripts/check-remote-privacy-reads.ts","check:engine-sql-brands": "bun scripts/check-engine-sql-brands.ts","check:sync-run-state": "bun scripts/check-sync-run-state.ts","check:schema-migrations": "bash scripts/check-schema-migrations-fresh.sh",diff --git a/scripts/check-engine-sql-dynamic.ts b/scripts/check-engine-sql-dynamic.tsindex e6b104cd95..35e0f829c1 100644--- a/scripts/check-engine-sql-dynamic.ts+++ b/scripts/check-engine-sql-dynamic.ts@@ -78,6 +78,9 @@ const VETTED_BUILDERS: Record<string, string> = {privateSnapshotFilterFragment: 'src/core/search/private-visibility.ts: constant snapshot visibility predicate over a caller alias',rederiveHiddenSql: 'src/core/eligibility/sql.ts: constant needs_rederive NOT EXISTS predicate over a literal table name and caller alias; no values',privateProvenanceFilterFragment: 'src/core/search/private-visibility.ts: constant fact-provenance visibility predicate over a caller alias',+ readersAllowedSql: 'src/core/search/private-visibility.ts: GBRA-218 readers predicate over a caller alias; splices only identities that pass validIdentity (member:/group: plus [a-z0-9._-]), else none',+ privacyForCaller: 'src/core/search/private-visibility.ts: the caller privacy arm (TRUE, or the given predicate ORed with private-readable source ids checked by isValidSourceId); callers pass a constant alias and a vetted predicate builder',+ privateProvenanceFilterUnderPolicy: 'src/core/search/private-visibility.ts: constant fact-provenance predicate (readers part with no identity, privacy part dropped only under an operator relaxation); no values',stagingNeedsTextSql: 'src/core/page-body/schema.ts: constant live-staging text-reference EXISTS predicate over a caller text alias; no values (Wave B purge)',atomRetirementPurgeHold: 'src/core/persistence/atom-retirement-hold.ts: constant #7013 restorable-atom purge-hold predicate over a caller pages alias; no values',derivedRetirementPurgeHold: 'src/core/persistence/derived-retirement-hold.ts: constant GBRA-161 derived-retirement purge-hold predicate over a caller pages alias; no values',diff --git a/scripts/check-remote-privacy-reads.ts b/scripts/check-remote-privacy-reads.tsnew file mode 100644index 0000000000..5529fe0b3b--- /dev/null+++ b/scripts/check-remote-privacy-reads.ts@@ -0,0 +1,92 @@+#!/usr/bin/env bun+/**+ * Remote-privacy resolver guard (GBRA-218, plan section 2.1). This header is+ * its documentation; `bun run check:remote-privacy-reads`, in `bun run verify`.+ *+ * The operator relaxations of remote privacy (the env hatch+ * GBRAIN_REMOTE_PRIVATE_PAGES and the config key search.remote_private_pages)+ * are read in exactly one file, src/core/search/private-visibility.ts, whose+ * resolver keeps the member-access part mandatory on a restricted brain. A+ * second reader would relax privacy without it. This guard parses every+ * src/ TypeScript file (compiler API) except that one and fails on:+ *+ * 1. a property access `<x>.GBRAIN_REMOTE_PRIVATE_PAGES`, an element access+ * `<x>['GBRAIN_REMOTE_PRIVATE_PAGES']`, or a destructured binding of it;+ * 2. the string 'GBRAIN_REMOTE_PRIVATE_PAGES' anywhere but as an element of+ * a declared array literal (name lists such as env-trust.ts are not+ * reads; an array passed straight to a call, such as query parameters, is);+ * 3. the string 'search.remote_private_pages' (exactly, or quoted inside+ * SQL text), or a reference to REMOTE_PRIVATE_PAGES_KEY, anywhere but as+ * an array-literal element or an import specifier (config.ts's key list).+ *+ * Prose that merely mentions the key (a message telling an operator which+ * key to set) is not flagged. There is no baseline: the count is zero.+ * Seam: GBRAIN_GUARD_ROOT (fixture tree root).+ */+import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';+import { join, relative, sep } from 'node:path';+import ts from 'typescript';++const ROOT = process.env.GBRAIN_GUARD_ROOT ?? join(import.meta.dir, '..');+const RESOLVER = 'src/core/search/private-visibility.ts';+const ENV = 'GBRAIN_REMOTE_PRIVATE_PAGES';+const KEY = 'search.remote_private_pages';+const KEY_CONST = 'REMOTE_PRIVATE_PAGES_KEY';++export interface Violation { path: string; line: number; what: string }++function tsFiles(dir: string): string[] {+ if (!existsSync(dir)) return [];+ const out: string[] = [];+ for (const entry of readdirSync(dir).sort()) {+ const full = join(dir, entry);+ if (statSync(full).isDirectory()) out.push(...tsFiles(full));+ else if (entry.endsWith('.ts') && !entry.endsWith('.d.ts')) out.push(full);+ }+ return out;+}++/** An element of a declared name list (an array literal that is not a call or constructor argument, such as a query's parameters). */+function inArrayLiteral(node: ts.Node): boolean {+ return ts.isArrayLiteralExpression(node.parent) && !ts.isCallExpression(node.parent.parent) && !ts.isNewExpression(node.parent.parent);+}++export function scanSource(path: string, text: string): Violation[] {+ const sf = ts.createSourceFile(path, text, ts.ScriptTarget.Latest, true);+ const out: Violation[] = [];+ const flag = (node: ts.Node, what: string) => out.push({ path, line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1, what });+ const visit = (node: ts.Node): void => {+ if (ts.isPropertyAccessExpression(node) && node.name.text === ENV) flag(node, 'reads the env hatch ' + ENV);+ else if (ts.isBindingElement(node) && [node.propertyName, node.name].some(n => n && ts.isIdentifier(n) && n.text === ENV)) flag(node, 'destructures the env hatch ' + ENV);+ else if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {+ const value = node.text;+ if (value === ENV && !inArrayLiteral(node)) flag(node, 'names the env hatch ' + ENV + ' outside a name list');+ else if ((value === KEY && !inArrayLiteral(node)) || value.includes("'" + KEY + "'")) flag(node, 'reads the config key ' + KEY);+ } else if (ts.isTemplateExpression(node) && [node.head.text, ...node.templateSpans.map(s => s.literal.text)].some(t => t.includes("'" + KEY + "'"))) {+ flag(node, 'reads the config key ' + KEY + ' in SQL text');+ } else if (ts.isIdentifier(node) && node.text === KEY_CONST && !ts.isImportSpecifier(node.parent) && !inArrayLiteral(node)+ && !(ts.isVariableDeclaration(node.parent) && node.parent.name === node)) {+ flag(node, 'uses ' + KEY_CONST + ' (a config read of the opt-out)');+ }+ ts.forEachChild(node, visit);+ };+ visit(sf);+ return out;+}++export function scan(root = ROOT): Violation[] {+ return tsFiles(join(root, 'src')).flatMap(file => {+ const path = relative(root, file).split(sep).join('/');+ return path === RESOLVER ? [] : scanSource(path, readFileSync(file, 'utf8'));+ });+}++if (import.meta.main) {+ const violations = scan();+ if (violations.length) {+ for (const v of violations) console.error(v.path + ':' + v.line + ': ' + v.what + '. Route it through ' + RESOLVER + ' (remotePrivacyRelaxed, readRemotePolicyNow or resolveExcludePrivatePages).');+ console.error('check-remote-privacy-reads: ' + violations.length + ' violation(s); see scripts/check-remote-privacy-reads.ts (header)');+ process.exit(1);+ }+ console.log('check-remote-privacy-reads: ok');+}diff --git a/scripts/guards-manifest.tsv b/scripts/guards-manifest.tsvindex b8baae764d..10007c8593 100644--- a/scripts/guards-manifest.tsv+++ b/scripts/guards-manifest.tsv@@ -69,6 +69,7 @@ check-item-allowlist-catches.ts scanner yes GBRA-130 per-item allowlist-catch racheck-engine-sql-ratchet.ts scanner yes goal (a) engine-sql ratchet (TS AST): SQL-bearing engine members (façade classes + pglite-engine/ and postgres-engine/ modules) need a baseline row or an engine-sql-ok marker; stale/duplicate rows and unbacked migrated domains fail; baseline scripts/engine-sql-baseline.tsv; --prune drops stale rows onlycheck-page-body-readers.ts scanner yes Wave B segmented storage (spec 1.5 layer 4, TS AST): src units reading .compiled_truth/.timeline, SQL literals naming the body columns or p.* from pages, and whole-body calls (serializePageToMarkdown, putPage, ...) need a baseline row with an action code; stale/duplicate/malformed rows fail; boundedBodyPrefixSql allowed by name; baseline scripts/page-body-readers-baseline.tsv; --prune drops stale rows onlycheck-engine-sql-dynamic.ts scanner yes CQ3/EO17 dynamic SQL in src/core/engine-sql/** (TS AST; fragment.ts exempt as the renderer): trustedSql args and composed .query/.unsafe/.executeRaw/executeRawJsonb SQL splice only literals, as-const/allowlisted constants, VETTED_BUILDERS calls or Number.isFinite-checked numbers; no $<digit> in composed strings; no expanded IN lists+check-remote-privacy-reads.ts scanner yes GBRA-218 (TS AST): the remote-privacy relaxations (GBRAIN_REMOTE_PRIVATE_PAGES, search.remote_private_pages / REMOTE_PRIVATE_PAGES_KEY) are read only in src/core/search/private-visibility.ts, whose resolver keeps the member-access part mandatory; name lists in array literals and import specifiers are not readscheck-engine-sql-brands.ts scanner yes EO4/AR2 RLS read brands (TS AST + text): brand-key mentions outside engine-sql/brands.ts; casts onto ScopedRead/LegacyUnscopedRead or `as unknown as` executor types in src/; unscopedExecutor/LegacyUnscopedRead/scopedRead imports outside their allowlists, src/core/ops/** always deniedcheck-sync-run-state.ts scanner yes A17 (W4 sync): SyncRun mutable fields (non-readonly members of interface SyncRun) are never destructured or aliased into locals under src/commands/sync/; @checkpoint fields are assigned only in sync-run.ts (single owner); fixtures cover destructuring, aliasing, a parameter pattern in a nested dir, and an out-of-owner checkpoint writecheck-layering.ts scanner yes EO10 layering (TS AST, every import form): src/core/engine-sql/** imports no engine façade; src/core/schema-migrations/** imports no migrate.ts; src/core/persistence/** imports no ai/gateway.ts (P8 write-inference contract)diff --git a/scripts/module-size-limits.tsv b/scripts/module-size-limits.tsvindex e65aea211a..308284cac9 100644--- a/scripts/module-size-limits.tsv+++ b/scripts/module-size-limits.tsv@@ -40,8 +40,8 @@ src/core/config.ts 2012 ratchet +2 W5: search.page_rank_cut / page_rank_cut_factsrc/core/persistence/sync-run.ts 1686 ratchet new row: crossed the 1500 unlisted cap in #6643 (knock-on re-freeze, checkpoint revival, root cause and the truthful --retry-failed wait all drive the private cursor/gate machinery: holdUnderGate, freezeEntry, saveCursor); classification lives in sync-knock-on.ts, the checkpoint clause in checkpoint-validation.ts; peel the settle path (settleFailedRequest and its recoveries) into a sibling module next; +3 #6722: committed pages' size_state tallied into the cursor and the sync receipt size_states (logic in page-embedding-budget.ts addSizeState); merge v0.60.240.0: large pages and master growth compose at 1687; +2 #6694: the slug-mode checkpoint revival import and its doc line (predicate lives in persistence/sync-slug-mode.ts); +11 #6712 PR 3 (L7): a plain sync retries the recorded failure --retry-failed used to (retryRecordedFailure; the unchanged-refusal replay lives in persistence/sync-stop-class.ts replaysUnchangedRefusal), the moved-checkpoint and company content-refusal exceptions; #6712 PR 3 integration: lanes L7 (retryRecordedFailure, +19) and L8 (#6727 legacy-slug refusal line, #6735 timeout revival, +6) compose at 1762; +2 wave 18a integration: #6729 (lane 2, written against the 1745 ceiling) records identity downgrades in performManagedPass through noteIdentityDowngrades (logic in core/sync-identity-downgrades.ts), composed with master 1762; +14 Wave B PR 2: freezeEntry freezes a Markdown file over the read bound by reference (body_ref) and holdFailedFenceRequest holds a streamed refusal; both set freezeEntry/holdFailedFenceRequest locals (blob, oversize, rawHash) so they stay here, the hashing and preparation live in persistence/sync-streamed.ts and the hold in sync-screen.ts streamedRefusalHold; integrated at the master 4e29d0904 merge: master 1764 + Wave B PR 2 14 = 1778 measured; +4 #6948: freezeEntry holds an entry several live pages claim (hold in sync-screen.ts originConflictHold, verdict in persistence/origin-twins.ts); +1 #7167: managedDryRun reports the writer state (the import of persistence/sync-dry-run-writer.ts, where the read lives); +4 G3 git-canonical: freezeEntry reads a strict source's entry from its commit (pinnedBlob) and carries committedOnly to publication; both use freezeEntry locals (rule in persistence/sync-classify.ts)src/core/bootstrap/harness.ts 2592 ratchet +29 live-PGLite-serve lane: LIVE_SERVE refusal ahead of source resolution + unpinned --token hooks (CI e2e fix); +46 wave review: resolveHookSource dep (serve-parity resolver + federated grant), --source shape validation, loud SOURCE_UNRESOLVED instead of a silent 'default'; +10: #4574 parseCodexBlockBearer http_headers shape + legacy bearer_token fallback; +4 at megawave/wave-k merge; +codex hooks lane: step-7 codex hooks.json + config.toml trust pair, removeHarness arm; +234 ambient-writeback WP3: kind:'instructions' targets (plan/apply/converge-off/remove/status probes, OV-A4 override pre-check) — splice/render mechanics live in instructions-block.ts; +59 adversarial fix wave: registrar-mode block suppression, converge-failure receipt tracking, MCP-confirmed guard before block install; +26 round-3 fixes: codex-lane MCP-confirmed guard + smoke-rollback block strip; master: | merged v0.48.1.0 true-up; +41 #4897 resolveImplicitSource dep + defaultResolveImplicitSource: hooks/token/receipt bind to the serve's implicit default source when --source is omitted; +23 #4586 statusHarness live claude-code mcp verdict (URL mismatch or same-scope non-HTTP registration reads failed, receipt untouched); shared-skills integration on company-ingestion base; +18 #5671: logAmbientPostureNotes helper (registrar note moved out of applyHarness + pre-consent warning when an explicit private facts default meets the HTTP harness; text shared with doctor in facts/writeback-config.ts); +24 #5893: rotation grant carry wiring and its rotationCarry/withheldOperationsNote helpers (carry rules live in token-mint.ts carryLegacyGrant); +6 #5878: --refresh-skills flag and status epoch line (refresh logic lives in harness-skills.ts); +7 #4618 seat: HarnessFlags.seat field, --seat/--no-seat parse via hooks.ts parseSeatFlags, GBRAIN_SEAT in the hook env; -119 #6082: plugin-lane detectors moved to plugin-lanes.ts (re-exported); +18 #6092/#6171: hook ownership identity on write/remove/converge, permission ownership carry, --remove --dry-run and the --status hook census (logic in hooks.ts + harness-hooks.ts); +5 D8 Codex slice: the Codex SessionEnd capture hook obeys the recorded consent (wireCodexCapture: hooks on, capture accepted, never registrar mode) and is disclosed in the consent block; -31 #6743: cleanup gate moved to harness-cleanup.ts; +8 #6798: supersededEnrollments dep (interface field + default) and the --status epoch hint printed only for a superseded enrollment (the brain read lives in harness-skills.ts brainSupersededEnrollments)src/core/oauth-provider.ts 1236 ratchet new row: crossed the 1500 unlisted cap absorbing #3819 (DCR 400 invalid_client_metadata + scope filtering + custom-scheme redirect_uris + pseudo-scheme guard); merged v0.49: transactional grant enforcement shared with hosted grant policies; +1 #5222 resourceUrl option-src/core/ops/pages.ts 728 ratchet crossed the unlisted 1500 cap in the fix wave: pack-vocabulary enforcement on capture (#4721) + get_page alias follow (#4275) landed together; next growth should peel a pages-ops submodule; +3: alias hop reads the canonical in the owning source (ship-review); +5: explicit source_id liveness check in get_page/list_pages (#4620); +3: #4857 total-order slug sort + list_pages source_id parse; +5: #4620 explicit removed-source id fails loudly; master integration: +4: authenticated delegated private-write boundary and auto-link suppression; #5105: canonical page mutations peeled into persistence modules; +2 v0.50.5.0 purge parameter and shared preflight call; preparation composes through the persistence modules; +7: source-qualified deep-research fetch authorization and ambiguity refusal (codec lives in deep-research-id.ts); +1 #5567: remote get_page keeps materialized markers; +1 #5622: trusted local capture file parameter; +5 #5709: get_page include_timeline_entries (rows via get_timeline's read policy); +8 GBRA-36 security wave: required outputRedaction policy on each pages op; +1 #5154: list_pages passes listColumnsOnly; +17 #5641: list_pages exposes the updated_after_slug keyset cursor and updated_at_iso; +8 agent operator wave A2: every op declares mutating + idempotent (tags only); +7 F3b (#5954): list_pages pagination meta + listing_truncated notice for MCP callers (logic in ops/list-pages-pagination.ts); +4 #5988: get_page file_held (one indexed hold lookup; logic in persistence/held-reads.ts); +6 #6103: list_pages maps an unreadable updated_after (SQLSTATE 22007/22008) to invalid_params; wave 10 collector: #6103 (+6) and #4807 (+2) compose; +1 wave 11 W4.3 (#5969 D3): put_page drop_timeline param; +1 wave 11 security review: purge_deleted_pages yes param + trusted-CLI guard (consent logic in persistence/purge-deleted.ts); +1 Q2 parser gaps: get_page grammar_diagnostics param (report logic in core/line-grammar-report.ts); master v0.60.136.0 merge into #5575 memory trust (both sides kept); +21 large pages (Wave A): get_page body-mode dispatch, fetch window branch, put_page large-page admission and allow_shrink param, description text (logic in ops/get-page-body.ts and persistence/large-page-guard.ts); +1 D8 funnel: committed put_page records a save when the opt-in funnel is on; +1 #6609: restore_page detach param (logic in persistence/detached-pages.ts); +1 Wave B gem A: post-batch auto-link reads a segmented page materialized (helpers in persistence/segmented-apply.ts); merge v0.60.249.0: composes at 707; +4 Wave B PR 2: get_page refuses a segmented page whole and fetch windows one (logic in ops/get-page-body.ts and page-window/segmented.ts); +2 Wave B PR 2 lane F: get_page and fetch pin segmented reads to the authorized snapshot (pin logic in ops/get-page-body.ts authorizedReadPin); +3 W2 page-at-time: get_page known_as_of param and dispatch (logic in ops/known-as-of.ts and belief/page-at-time.ts); GBRA-151 retrieval P0 x operability wave merge: both sides grew the file, re-summed (721 measured); GBRA-151 #7147 follow-up re-summed at the v0.60.334.0 merge (727 measured); +1 #7305: capture dry run applies the hyphen-run slug admission (logic in persistence/suffixed-slug.ts)-src/core/search/mode.ts 1709 ratchet +53 W3 confidence-gated reranking: reranker_gate / reranker_gate_min_gap bundle fields, overrides, config parse and KNOB_CONFIG_KEY rows, knobsHash v=31 rrg=/rrgg=/ecf= parts (parse contracts in crag.ts); +10 System One v1: decide.* snapshot subset in loadSearchModeConfig, append-only dec= knobsHash part; +5 #3617 follow-up: v27 same-knobs behavioral-change note (relaxed-row fusion demotion rides the bump); new row: crossed the 1500 unlisted cap in the 2026-08 eval fix wave — KNOBS_HASH v=27 adaptive-return fold (ar/arem/arom/armk/ari parts + KnobsHashContext.adaptiveReturn field + contamination rationale comments) | merged v0.48.1.0 (+#4787) true-up; +master v0.48 merge true-up (reranker succession: no_key preflight, reranker_skipped stage, readiness); ranker wave v0.48.4.0: four knobs (expansion_variant_budget, relational_rerank_pin, keyword_arm_confidence_floor, metadata_boost_gate) + v29 hash parts + doc receipts (+218); #4781 loadSearchModeConfig reads the config table once via loadConfigSnapshot (+6); +4 #4359: update loadSearchModeConfig docstring — the cached path now reads config once (threaded snapshot), not twice; retired provider integration removed; +12 fw3-search: search.source_boosts + search.alias_token_hop read in the mode snapshot; +10 #5691: KnobsHashContext.queryPrefix and its append-only qp= part; P7 relational_planner / relational_orient_onehop knobs (interface docs, bundles, overrides, config parse, hash parts); +18 P7 relational_chain_slots knob (interface doc, bundles, override, config parse, rcs= hash part); +22 GBRA-60 S1 diverse_pool_depth knob (interface doc, bundles, overrides, pick, config parse, append-only dpd= hash part); +35 GBRA-154: collapse and rank_priors knobs (interface docs, bundles, overrides, pick, config parse, append-only col=/rpr= hash parts); GBRA-151 retrieval P0 x operability wave merge: both sides grew the file, re-summed (1704 measured); +5 GBRA-156: append-only xpr= knobsHash part (private-readable set)+src/core/ops/pages.ts 729 ratchet crossed the unlisted 1500 cap in the fix wave: pack-vocabulary enforcement on capture (#4721) + get_page alias follow (#4275) landed together; next growth should peel a pages-ops submodule; +3: alias hop reads the canonical in the owning source (ship-review); +5: explicit source_id liveness check in get_page/list_pages (#4620); +3: #4857 total-order slug sort + list_pages source_id parse; +5: #4620 explicit removed-source id fails loudly; master integration: +4: authenticated delegated private-write boundary and auto-link suppression; #5105: canonical page mutations peeled into persistence modules; +2 v0.50.5.0 purge parameter and shared preflight call; preparation composes through the persistence modules; +7: source-qualified deep-research fetch authorization and ambiguity refusal (codec lives in deep-research-id.ts); +1 #5567: remote get_page keeps materialized markers; +1 #5622: trusted local capture file parameter; +5 #5709: get_page include_timeline_entries (rows via get_timeline's read policy); +8 GBRA-36 security wave: required outputRedaction policy on each pages op; +1 #5154: list_pages passes listColumnsOnly; +17 #5641: list_pages exposes the updated_after_slug keyset cursor and updated_at_iso; +8 agent operator wave A2: every op declares mutating + idempotent (tags only); +7 F3b (#5954): list_pages pagination meta + listing_truncated notice for MCP callers (logic in ops/list-pages-pagination.ts); +4 #5988: get_page file_held (one indexed hold lookup; logic in persistence/held-reads.ts); +6 #6103: list_pages maps an unreadable updated_after (SQLSTATE 22007/22008) to invalid_params; wave 10 collector: #6103 (+6) and #4807 (+2) compose; +1 wave 11 W4.3 (#5969 D3): put_page drop_timeline param; +1 wave 11 security review: purge_deleted_pages yes param + trusted-CLI guard (consent logic in persistence/purge-deleted.ts); +1 Q2 parser gaps: get_page grammar_diagnostics param (report logic in core/line-grammar-report.ts); master v0.60.136.0 merge into #5575 memory trust (both sides kept); +21 large pages (Wave A): get_page body-mode dispatch, fetch window branch, put_page large-page admission and allow_shrink param, description text (logic in ops/get-page-body.ts and persistence/large-page-guard.ts); +1 D8 funnel: committed put_page records a save when the opt-in funnel is on; +1 #6609: restore_page detach param (logic in persistence/detached-pages.ts); +1 Wave B gem A: post-batch auto-link reads a segmented page materialized (helpers in persistence/segmented-apply.ts); merge v0.60.249.0: composes at 707; +4 Wave B PR 2: get_page refuses a segmented page whole and fetch windows one (logic in ops/get-page-body.ts and page-window/segmented.ts); +2 Wave B PR 2 lane F: get_page and fetch pin segmented reads to the authorized snapshot (pin logic in ops/get-page-body.ts authorizedReadPin); +3 W2 page-at-time: get_page known_as_of param and dispatch (logic in ops/known-as-of.ts and belief/page-at-time.ts); GBRA-151 retrieval P0 x operability wave merge: both sides grew the file, re-summed (721 measured); GBRA-151 #7147 follow-up re-summed at the v0.60.334.0 merge (727 measured); +1 #7305: capture dry run applies the hyphen-run slug admission (logic in persistence/suffixed-slug.ts); +1 GBRA-218: put_page checks a remote readable_by before admission (logic in persistence/page-visibility.ts assertSubmittedReaders)+src/core/search/mode.ts 1716 ratchet +53 W3 confidence-gated reranking: reranker_gate / reranker_gate_min_gap bundle fields, overrides, config parse and KNOB_CONFIG_KEY rows, knobsHash v=31 rrg=/rrgg=/ecf= parts (parse contracts in crag.ts); +10 System One v1: decide.* snapshot subset in loadSearchModeConfig, append-only dec= knobsHash part; +5 #3617 follow-up: v27 same-knobs behavioral-change note (relaxed-row fusion demotion rides the bump); new row: crossed the 1500 unlisted cap in the 2026-08 eval fix wave — KNOBS_HASH v=27 adaptive-return fold (ar/arem/arom/armk/ari parts + KnobsHashContext.adaptiveReturn field + contamination rationale comments) | merged v0.48.1.0 (+#4787) true-up; +master v0.48 merge true-up (reranker succession: no_key preflight, reranker_skipped stage, readiness); ranker wave v0.48.4.0: four knobs (expansion_variant_budget, relational_rerank_pin, keyword_arm_confidence_floor, metadata_boost_gate) + v29 hash parts + doc receipts (+218); #4781 loadSearchModeConfig reads the config table once via loadConfigSnapshot (+6); +4 #4359: update loadSearchModeConfig docstring — the cached path now reads config once (threaded snapshot), not twice; retired provider integration removed; +12 fw3-search: search.source_boosts + search.alias_token_hop read in the mode snapshot; +10 #5691: KnobsHashContext.queryPrefix and its append-only qp= part; P7 relational_planner / relational_orient_onehop knobs (interface docs, bundles, overrides, config parse, hash parts); +18 P7 relational_chain_slots knob (interface doc, bundles, override, config parse, rcs= hash part); +22 GBRA-60 S1 diverse_pool_depth knob (interface doc, bundles, overrides, pick, config parse, append-only dpd= hash part); +35 GBRA-154: collapse and rank_priors knobs (interface docs, bundles, overrides, pick, config parse, append-only col=/rpr= hash parts); GBRA-151 retrieval P0 x operability wave merge: both sides grew the file, re-summed (1704 measured); +5 GBRA-156: append-only xpr= knobsHash part (private-readable set); +7 GBRA-218 A1: append-only xid=/xhp= knobsHash parts (member identity, operator privacy relaxation)src/commands/eval-longmemeval.ts 1544 ratchet new row: crossed the 1500 unlisted cap in fw3-search (#5092 benchmark-brain recycling: configureBrain split + recycler wiring + test seams; the recycler itself lives in src/eval/longmemeval/harness.ts); +1 P6: tokenmax synopsis-tier wiring (logic in eval/longmemeval/synopsis-tier.ts); +13 GBRA-81 lane A: --via-operation query|search (flags, per-call expansion pin, the run's expectExpansion, the op branch in runOneQuestion, row field, via_operation_summary and its FAIL line; logic in eval/longmemeval/via-operation.ts); +12 --call-plan FILE / --call-plan-gate CMD: two LME_FLAGS entries (CLI help lines), two ParsedArgs fields, the parseArgs pairing check and the header invariant (planner lives in eval/longmemeval/call-plan.ts; runEvalLongMemEval is one line shorter)src/commands/doctor/checks/extraction-sync.ts 1500 ratchet +7 GBRA-130 A-1: links_extraction_lag held bucket and its retry fix (logic in core/extraction-lag.ts heldExtractionReport); +1 undeclared_db_only_pages computes the source Git scope once per source (logic in core/markdown.ts sourceGitScope); new row: crossed the 1500 unlisted cap at the fix wave 9 merge: +7 lane W #5028 (extract_atoms freshness judged by the phase itself) and +3 lane F #5863 (extract_health dates a high halt rate by its last halt), each under the cap alone; +7 #5019: content_hash_duplicates fix pins the pairs' source (GBRAIN_SOURCE) and names a delete that runs; +1 fix wave 10 lane L1 #5556: links_extraction_lag denominator excludes quarantined pages (quarantineFilterFragment import); +11 fix wave 15 P2.8 (#5495): extract_health reads the 7-day halt reasons per kind (halt_reasons, top_halt_reason in details and the top reason in the warn line); the read, the unknown bucket and the ranking live in core/extract/rollup-writer.ts; +20 wave 18b lane 1: links_extraction_lag names the columns its pre-migration degrade reads (#6599 strict predicate), sync_freshness separates sync outcome from freshness for a source with no successful sync (#4960; verdict text, fixes and the attempt read live in doctor/sync-outcome.ts and core/sync-attempt.ts) and exempts read-only skill-pack catalogs (#6076; core/shared-skills/adopted-packs.ts); -51 #6803 (D1): links_extraction_lag reads its counts and verdict from core/extraction-lag.ts (shared with the sync nudge); +1 #7145: content_hash_duplicates reads one group past its list to report a floor ("at least 50")src/core/search/evidence-delivery.ts 1733 ratchet +5 W5: EvidencePlan.rankCut resolved with the plan (logic in search/page-rank-cut.ts); +20 W5 whole_pages: wholeCost prices a block as delivery would emit it whole (needs the private Block/enrichBlock internals) and the cut is wired before allocate (cutWholePages lives in search/page-rank-cut.ts); new row: crossed the 1500 unlisted cap at the #6367 merge (master 1479, its explicit-budget cap) by the C1 date header threaded through every block path, cap included; pagePlanHits (search/page-plan-hits.ts) and the header loader (evidence-date.ts blockDateHeaders) were peeled first; peel candidate: allocateCapped; +8 at the #6396 merge: page evidence text cut through the fence-chunk trust overlay and the caller minTrust floor (re-summed: our 1504 + master +8); +119 Cat 40 next wave F2: the search.long_page_unit policy (withLongPagePolicy, selectLongPages size preflight with page/byte/deadline limits) and the long-page fetch and fallback in deliverEvidence, kept in the shared seam the plan names (gbrain-evals docs/plans/2026-10-11-cat40-next-wave, F2); +17 F2 rework to the frozen-comparison policy: search.long_page_min_chars (rendered-body characters, default 20,000) read in withLongPagePolicy, a character count in the preflight, the rendered-body confirmation in deliverEvidence, and the long-page row exemption in capDeliveredSnippets (re-summed at the 0.60.225.0 merge: master 1537 + F2 136); +39 Wave B PR 2 lane 2b: planSegmentedBlock joins the windows search/segmented-evidence.ts reads around a segmented page's hits into one run-marked document (needs the private Doc/Piece/buildDoc/locateAnchor internals), planBlock's unit cut split into blockFromDoc for both, sections bounded by their run, selectionCost counts a gap between runs, and the fetch skips the whole-body fence overlay for a segmented page; +4 segmented pages read at once before planning (segmentedReads; the read is search/segmented-evidence.ts, the statements page-body/read-range.ts); +17 GBRA-156: tail trim plan flag, DeliverOptions.query, DeliveryMeta.tail_trim, the trim call and the frozen-hit score/query library fields (logic in search/evidence-tail-trim.ts; interface fields cannot move)diff --git a/scripts/run-verify-parallel.sh b/scripts/run-verify-parallel.shindex 9b10122ad5..d61b59d532 100755--- a/scripts/run-verify-parallel.sh+++ b/scripts/run-verify-parallel.sh@@ -199,6 +199,8 @@ CHECKS=(# CQ3 / EO17 (refactor wave 1): engine-sql splices only constant text, no# composed $n, no expanded IN lists."check:engine-sql-dynamic"+ # GBRA-218: the remote-privacy relaxations are read only in private-visibility.ts.+ "check:remote-privacy-reads"# EO4 (refactor wave 1): RLS read brands stay unforgeable; brand factories# importable only from their allowlists (never src/core/ops/**)."check:engine-sql-brands"diff --git a/src/commands/doctor/checks/member-access.ts b/src/commands/doctor/checks/member-access.tsnew file mode 100644index 0000000000..3fe8ca66d7--- /dev/null+++ b/src/commands/doctor/checks/member-access.ts@@ -0,0 +1,139 @@+/**+ * GBRA-218 member-access doctor checks (plan section 5). Each reads the brain+ * once, is bounded by SAMPLE in its details, and fixes with a read-only or+ * preview-first command whose `verify` is the read-only check itself.+ *+ * - access_readers_malformed: pages whose `readable_by` normalizes to nobody+ * (gbrain_access_readers() = {!nobody}); remote callers cannot read them.+ * - access_unknown_readers: readers naming no member or no group (a typo, or+ * a member not added yet); a group with no members is reported apart.+ * - access_recent_narrowings: pages whose live revision gained readers in the+ * last NARROWING_DAYS days (the previous snapshot named none), with the+ * principal that wrote the revision when recorded.+ * - access_page_readers_backfill: pages whose derived `page_readers` differs+ * from their frontmatter (written before the migration); they read as+ * nobody until `gbrain repair access` derives them.+ *+ * A brain that holds no member restriction (config access.restrictions_present)+ * is not applicable for all four; nothing is read.+ */+import type { Check } from '../../doctor.ts';+import type { BrainEngine } from '../../../core/engine.ts';+import { agentFix, checkError, doctorVerify, infoCheck } from '../check-fix.ts';+import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';+import { accessRestrictionsPresent } from '../../../core/search/private-visibility.ts';++const SAMPLE = 20;+const NARROWING_DAYS = 7;+const DOCS = 'docs/guides/write-refusals.md#readable_by_invalid';++type PageRef = { source_id: string; slug: string };+const ref = (p: PageRef) => `${p.source_id}:${p.slug}`;+const showPage = (p: PageRef, name: string, why: string) => agentFix(['gbrain', 'get', '--source', p.source_id, '--', p.slug], why, name, { docs: DOCS });++async function malformed(engine: BrainEngine): Promise<Check> {+ const name = 'access_readers_malformed';+ const rows = await engine.executeRaw<PageRef & { total: number }>(+ `SELECT source_id, slug, count(*) OVER ()::int AS total FROM pages+ WHERE deleted_at IS NULL AND frontmatter->'readable_by' IS NOT NULL AND gbrain_access_readers(frontmatter) = ARRAY['!nobody']::text[]+ ORDER BY source_id, slug LIMIT ${SAMPLE}`);+ const total = rows[0]?.total ?? 0;+ if (!total) return { name, status: 'ok', message: 'Every page that names readers names them validly.', details: { count: 0 } };+ return { name, status: 'warn', details: { count: total, samples: rows.map(ref), truncated: total > rows.length },+ message: `${total} page(s) carry a readable_by that names no valid reader, so no remote caller can read them (e.g. ${rows.slice(0, 5).map(ref).join(', ')}). `+ + 'Write readable_by as a list of member ids and group:<id> entries, for example readable_by: [alice-example, group:team-a].',+ fix: { ...showPage(rows[0]!, name, 'Shows the first page with its readable_by; correct the field (member ids and group:<id> entries) and save the page, then re-run the check.'), actor: 'user', ask_user: true } };+}++async function unknownReaders(engine: BrainEngine): Promise<Check> {+ const name = 'access_unknown_readers';+ const rows = await engine.executeRaw<{ reader: string; kind: 'unknown' | 'retired' | 'empty_group'; pages: number; sample: PageRef }>(+ `WITH named AS (+ SELECT p.source_id, p.slug, r AS reader FROM pages p, unnest(gbrain_access_readers(p.frontmatter)) AS r+ WHERE p.deleted_at IS NULL AND p.frontmatter->'readable_by' IS NOT NULL AND r <> '!nobody'),+ classified AS (+ SELECT n.*, CASE+ WHEN n.reader LIKE 'member:%' THEN (SELECT CASE WHEN m.id IS NULL THEN 'unknown' WHEN m.tombstoned_at IS NOT NULL THEN 'retired' END+ FROM (SELECT 1) one LEFT JOIN access_members m ON m.id = substr(n.reader, 8))+ ELSE (SELECT CASE WHEN g.id IS NULL THEN 'unknown' WHEN NOT EXISTS (SELECT 1 FROM access_group_members gm WHERE gm.group_id = g.id) THEN 'empty_group' END+ FROM (SELECT 1) one LEFT JOIN access_groups g ON g.id = substr(n.reader, 7)) END AS kind+ FROM named n)+ SELECT reader, kind, count(*)::int AS pages, jsonb_build_object('source_id', min(source_id), 'slug', min(slug)) AS sample+ FROM classified WHERE kind IS NOT NULL GROUP BY reader, kind ORDER BY kind, reader LIMIT ${SAMPLE}`);+ const unknown = rows.filter(r => r.kind !== 'empty_group');+ const empty = rows.filter(r => r.kind === 'empty_group');+ const details = { unknown: unknown.map(r => ({ reader: r.reader, kind: r.kind, pages: r.pages, sample: ref(r.sample) })),+ empty_groups: empty.map(r => ({ reader: r.reader, pages: r.pages })), truncated: rows.length === SAMPLE };+ const emptyNote = empty.length ? ` ${empty.length} group(s) exist with no members, so they match nobody yet (${empty.map(r => r.reader).join(', ')}).` : '';+ if (!unknown.length) return { name, status: 'ok', details, message: `Every reader a page names is a known member or group.${emptyNote}` };+ const first = unknown[0]!;+ const id = first.reader.replace(/^(member|group):/, '');+ return { name, status: 'warn', details,+ message: `${unknown.length} reader(s) named in readable_by match no live member or group, so they let nobody in: `+ + `${unknown.slice(0, 5).map(r => `${r.reader} (${r.kind}, ${r.pages} page(s), e.g. ${ref(r.sample)})`).join('; ')}. `+ + `Fix the typo in the page, or add the member or group if it is meant to read.${emptyNote}`,+ fix: { argv: ['gbrain', 'access', first.reader.startsWith('group:') ? 'group' : 'member', 'add', id], consent: [], actor: 'user', ask_user: true, requires_exclusive: false,+ why: `Adds ${first.reader} so the pages naming it become readable by it; this widens access, so ask the user first, and fix the page instead when the name is a typo.`,+ verify: doctorVerify(name), docs: DOCS } };+}++async function recentNarrowings(engine: BrainEngine): Promise<Check> {+ const name = 'access_recent_narrowings';+ const rows = await engine.executeRaw<PageRef & { readers: string[]; writer: string | null; updated_at: string; total: number }>(+ `SELECT p.source_id, p.slug, gbrain_access_readers(p.frontmatter) AS readers, p.updated_at::text AS updated_at,+ CASE WHEN p.revision_principal_kind IS NULL THEN NULL ELSE p.revision_principal_kind || ':' || p.revision_principal_id END AS writer,+ count(*) OVER ()::int AS total+ FROM pages p+ JOIN LATERAL (SELECT v.frontmatter FROM page_versions v WHERE v.page_id = p.id ORDER BY v.snapshot_at DESC, v.id DESC LIMIT 1) prev ON true+ WHERE p.deleted_at IS NULL AND p.frontmatter->'readable_by' IS NOT NULL AND prev.frontmatter->'readable_by' IS NULL+ AND p.updated_at > now() - make_interval(days => ${NARROWING_DAYS})+ ORDER BY p.updated_at DESC LIMIT ${SAMPLE}`);+ const total = rows[0]?.total ?? 0;+ const details = { count: total, window_days: NARROWING_DAYS, narrowings: rows.map(r => ({ page: ref(r), readers: r.readers, writer: r.writer ?? 'unrecorded', at: r.updated_at })), truncated: total > rows.length };+ if (!total) return { name, status: 'ok', details, message: `No page gained readers in the last ${NARROWING_DAYS} days.` };+ return { name, status: 'warn', details,+ message: `${total} page(s) gained readers in the last ${NARROWING_DAYS} days and are now hidden from every other remote caller: `+ + `${rows.slice(0, 5).map(r => `${ref(r)} -> ${r.readers.join(', ')} (by ${r.writer ?? 'an unrecorded writer'})`).join('; ')}. `+ + 'Confirm each was intended; get_versions shows the earlier revision and revert_version restores it.',+ fix: { ...showPage(rows[0]!, name, 'Shows the most recently narrowed page and its readers, read-only; ask the user whether the narrowing was intended before reverting anything.'), actor: 'user', ask_user: true } };+}++async function backfill(engine: BrainEngine): Promise<Check> {+ const name = 'access_page_readers_backfill';+ const [row] = await engine.executeRaw<{ total: number }>(+ `SELECT count(*)::int AS total FROM pages+ WHERE (frontmatter->'readable_by' IS NOT NULL OR page_readers IS NOT NULL)+ AND page_readers IS DISTINCT FROM gbrain_access_readers(frontmatter)`);+ const total = row?.total ?? 0;+ if (!total) return { name, status: 'ok', details: { count: 0 }, message: 'Every page that names readers has its derived readers.' };+ return { name, status: 'warn', details: { count: total, repair: 'access' },+ message: `${total} page(s) name readers in frontmatter but have no derived readers yet (written before member access), so they read as nobody for every remote caller, listed members included. `+ + 'Preview with gbrain repair access, then apply the printed command after the user agrees; it never widens anything.',+ fix: agentFix(['gbrain', 'repair', 'access', '--apply'], 'Derives the missing readers; run the preview first and apply after the user agrees. It only restores reads for listed members and never widens anything.', name,+ { preview_argv: ['gbrain', 'repair', 'access', '--json'], ask_user: true, docs: DOCS }) };+}++/** One check's result, or the unknown-convention error when its read failed. */+async function guarded(name: string, run: (engine: BrainEngine) => Promise<Check>, engine: BrainEngine, applicable: boolean): Promise<Omit<Check, 'name'>> {+ const { name: _name, ...check } = applicable+ ? await run(engine).catch(error => checkError(name, `run ${name}`, error))+ : infoCheck(name, 'Not applicable: no page names readers and no member exists.', 'not_applicable');+ return check;+}++async function runMemberAccess(ctx: DoctorContext): Promise<Check[]> {+ const engine = connectedEngine(ctx);+ const applicable = await accessRestrictionsPresent(engine).catch(() => true);+ const checks: Check[] = [];+ checks.push({ name: 'access_readers_malformed', ...await guarded('access_readers_malformed', malformed, engine, applicable) });+ checks.push({ name: 'access_unknown_readers', ...await guarded('access_unknown_readers', unknownReaders, engine, applicable) });+ checks.push({ name: 'access_recent_narrowings', ...await guarded('access_recent_narrowings', recentNarrowings, engine, applicable) });+ checks.push({ name: 'access_page_readers_backfill', ...await guarded('access_page_readers_backfill', backfill, engine, applicable) });+ return checks;+}++export const memberAccessEntry: DoctorEntry = {+ name: 'access_readers_malformed',+ emits: ['access_readers_malformed', 'access_unknown_readers', 'access_recent_narrowings', 'access_page_readers_backfill'],+ run: runMemberAccess,+};diff --git a/src/commands/doctor/registry.ts b/src/commands/doctor/registry.tsindex 796ad5c074..071ee6f68b 100644--- a/src/commands/doctor/registry.ts+++ b/src/commands/doctor/registry.ts@@ -126,6 +126,7 @@ import { dbAheadOfGitEntry } from './checks/db-ahead-of-git.ts';import { gitIndexLagEntry } from './checks/git-index-lag.ts';import { syncConfigKeysEntry } from './checks/sync-config-keys.ts';import { ghostPagesEntry } from './checks/ghost-pages.ts';+import { memberAccessEntry } from './checks/member-access.ts';import { originTwinsEntry } from './checks/origin-twins.ts';import { slugPathMismatchEntry } from './checks/slug-path-mismatch.ts';import { renameIdentityDowngradesEntry } from './checks/rename-identity-downgrades.ts';@@ -204,6 +205,7 @@ export const DOCTOR_CHECK_REGISTRY: readonly DoctorEntry[] = [pgvectorEntry,// #7276: the privacy check next, with its own entry budget, before any full scan.derivedVisibilityEntry,+ memberAccessEntry,rlsEntry,schemaVersionEntry,rlsEventTriggerEntry,diff --git a/src/core/access/caller.ts b/src/core/access/caller.tsindex e1dbc4bf23..2f02ee17f1 100644--- a/src/core/access/caller.ts+++ b/src/core/access/caller.ts@@ -17,10 +17,13 @@ const MEMBER_SQL = (credential: string) => `SELECT m.id, COALESCE(array_agg(g.grWHERE m.id = (${credential}) AND m.disabled_at IS NULL AND m.tombstoned_at IS NULLGROUP BY m.id`;-/** The member identity bound to `auth`'s principal; empty when unbound, unknown or unreadable. */-export async function memberIdentityFor(engine: BrainEngine, auth: AuthInfo | undefined): Promise<string[]> {+/**+ * The member identity bound to `auth`'s principal; empty when unbound, unknown or unreadable. A write's publication+ * passes its stored principal here, so a member disabled or a binding removed after submission takes effect.+ */+export async function memberIdentityFor(engine: Pick<BrainEngine, 'executeRaw'>, auth: { principal?: { kind: string; id: string } } | undefined): Promise<string[]> {const principal = auth?.principal;- if (!principal) return [];+ if (!principal || (principal.kind !== 'legacy_token' && principal.kind !== 'oauth_client')) return [];const credential = principal.kind === 'legacy_token'? 'SELECT member_id FROM access_tokens WHERE id::text = $1': 'SELECT member_id FROM oauth_clients WHERE client_id = $1';diff --git a/src/core/doctor-categories.ts b/src/core/doctor-categories.tsindex 863bfa37a4..3c5fe3c29f 100644--- a/src/core/doctor-categories.ts+++ b/src/core/doctor-categories.ts@@ -80,6 +80,11 @@ export const BRAIN_CHECK_NAMES: ReadonlySet<string> = new Set(['trust_sources_unclaimed','credential_projection_pending','derived_visibility',+ // GBRA-218: member read access (doctor/checks/member-access.ts).+ 'access_readers_malformed',+ 'access_unknown_readers',+ 'access_recent_narrowings',+ 'access_page_readers_backfill','extractor_facts_expired','loop_facts_drift','ontology_facts_fenced',diff --git a/src/core/engine-sql/facts.ts b/src/core/engine-sql/facts.tsindex eb099c15cc..e7750b9844 100644--- a/src/core/engine-sql/facts.ts+++ b/src/core/engine-sql/facts.ts@@ -29,7 +29,7 @@ import { sqlFragment, trustedSql } from './fragment.ts';import { compileRowNormalizer } from './normalize.ts';import { projectionEligibleSql } from '../eligibility/sql.ts';import { FACT_WITHDRAWAL_STAMP_DEFERRED_SETTING } from '../facts/insert-guard-schema.ts';-import { privateProvenanceFilterFragment } from '../search/private-visibility.ts';+import { privateProvenanceFilterUnderPolicy } from '../search/private-visibility.ts';import { storedTrustTier } from '../trust/tier.ts';import { ensureWellFormed } from '../text-safe.ts';@@ -58,7 +58,7 @@ function eligibilityClause(opts: FactListOpts | undefined) {}function privacyClause(opts: { excludePrivate?: boolean } | undefined) {- return opts?.excludePrivate ? sqlFragment` AND ${trustedSql(privateProvenanceFilterFragment('facts'))}` : sqlFragment``;+ return opts?.excludePrivate ? sqlFragment` AND ${trustedSql(privateProvenanceFilterUnderPolicy('facts'))}` : sqlFragment``;}function grepPattern(opts: FactListOpts | undefined): string | null {diff --git a/src/core/error-catalogue.ts b/src/core/error-catalogue.tsindex 6c4d6f4d56..8c20e94cce 100644--- a/src/core/error-catalogue.ts+++ b/src/core/error-catalogue.ts@@ -70,6 +70,8 @@ export const ERROR_CATALOGUE = {no_pricing: { code: 'no_pricing', docs: 'docs/guides/write-refusals.md#no_pricing' },timeline_comment_markup: { code: 'timeline_comment_markup', docs: 'docs/guides/repair.md#timeline-comments' },timeline_rows_would_be_removed: { code: 'timeline_rows_would_be_removed', docs: 'docs/guides/write-refusals.md#timeline_rows_would_be_removed' },+ // GBRA-218: a remote write's frontmatter readable_by is malformed, drops the field, or locks the writer out.+ readable_by_invalid: { code: 'readable_by_invalid', docs: 'docs/guides/write-refusals.md#readable_by_invalid' },reconcile_private_facts: { code: 'permission_denied', docs: 'docs/guides/write-refusals.md#reconcile-private-facts' },maintenance_backpressure: { code: 'maintenance_backpressure', docs: 'docs/guides/write-refusals.md#maintenance_backpressure' },// #6696: cumulative write caps (A5 background pause, A6 refusal at the cap, A7 bulk-run preflight).diff --git a/src/core/error-registry.ts b/src/core/error-registry.tsindex 24e4090ac1..3dea086775 100644--- a/src/core/error-registry.ts+++ b/src/core/error-registry.ts@@ -730,6 +730,7 @@ export const CODES = {take_row_collision: { class: 'caller', summary: "A takes fence row number is already used by a different take that is not in this page's canonical fence." },target_escape: { class: 'caller', summary: "The skill target must remain within its selected root." },timeline_comment_markup: { class: 'caller', summary: "A stored timeline row carries HTML comment markup, so gbrain keeps it database-side and never writes it back into the page; clean it with gbrain repair timeline-comments.", docs: 'docs/guides/repair.md#timeline-comments' },+ readable_by_invalid: { class: 'caller', summary: 'A remote write\'s readable_by is malformed, removes the field from a page that names readers, or would leave the page unreadable by the writer; nothing was written.', docs: 'docs/guides/write-refusals.md#readable_by_invalid' },timeline_rows_would_be_removed: { class: 'caller', summary: "A remote put_page or put_pages entry has no Timeline section and would delete the page's dated entries; resend it with the section or with drop_timeline: true.", docs: 'docs/guides/write-refusals.md#timeline_rows_would_be_removed' },timeout: { class: 'retryable', summary: "The operation did not finish within its time bound.", exit: 124 },topology_change_required: { class: 'host_only', summary: "Adding this source would replace or overlap another owner root." },diff --git a/src/core/facts/meta-hook.ts b/src/core/facts/meta-hook.tsindex 02a3ed66c3..011d969036 100644--- a/src/core/facts/meta-hook.ts+++ b/src/core/facts/meta-hook.ts@@ -42,7 +42,7 @@ import { proactiveEligibility } from '../eligibility/registry.ts';import { partitionForActivation, suppressionSummary } from '../eligibility/activation.ts';import { trustFields } from '../eligibility/labels.ts';import { eligibilityCacheField, readTrustGeneration } from '../eligibility/generation.ts';-import { resolveExcludePrivatePages } from '../search/private-visibility.ts';+import { callerRelaxesPrivacy, resolveExcludePrivatePages } from '../search/private-visibility.ts';import { isConfigTruthy } from '../config.ts';const DEFAULT_TTL_MS = 30_000;@@ -152,7 +152,8 @@ export async function getBrainHotMemoryMeta(// The operator opt-out (search.remote_private_pages) changes what a world// read returns, so it is part of the tier too.const excludePrivate = await resolveExcludePrivatePages(ctx.engine, ctx.remote);- const tier = ctx.remote === false ? 'all' : excludePrivate ? 'world' : 'world+private-pages';+ // GBRA-218: on a brain with member restrictions a relaxed read still applies the readers part, so it has its own tier.+ const tier = ctx.remote === false ? 'all' : !excludePrivate ? 'world+private-pages' : callerRelaxesPrivacy() ? 'world+private-pages-readers' : 'world';// encodeCacheField (F5): source_id / session_id are caller-controlled and// may contain the '::' delimiter; percent-encode ':' so bumpHotMemoryCache's// split('::') can never mis-slice a component.diff --git a/src/core/facts/queue.ts b/src/core/facts/queue.tsindex 4c6b284a66..46d78cebc6 100644--- a/src/core/facts/queue.ts+++ b/src/core/facts/queue.ts@@ -18,6 +18,7 @@* concurrency + dropping under load.*/+import { withoutCallerAccess } from '../search/private-visibility.ts';import { currentAIAttribution, withAIAttribution, type AIAttribution } from '../ai/invocation-guard.ts';import { registerBackgroundWorkDrainer } from '../background-work.ts';@@ -105,8 +106,8 @@ export class FactsQueue {}this.pending.push({ job, sessionId, enqueuedAt: Date.now(), attribution: { ...currentAIAttribution(), effect: 'facts-queue' } });this.counters.enqueued += 1;- // Non-blocking pump: schedule on microtask so callers stay sync.- queueMicrotask(() => { void this.pump(); });+ // Non-blocking pump: schedule on microtask so callers stay sync. GBRA-218: outside the enqueuing caller's access record.+ withoutCallerAccess(() => queueMicrotask(() => { void this.pump(); }));return this.pending.length;}diff --git a/src/core/facts/similar-active.ts b/src/core/facts/similar-active.tsindex 3a2b3ae5fb..02f75c15e8 100644--- a/src/core/facts/similar-active.ts+++ b/src/core/facts/similar-active.ts@@ -11,7 +11,7 @@ import { readDecideConfig, providerKind } from '../ai/decide/config.ts';import { hasTypesafeKey } from '../ai/decide/index.ts';import { CONFLICT_MIN_COSINE } from '../ai/decide/conflict.ts';import { REVIEW_WITHDRAW_KEY, reviewWithdrawOn } from './withdrawal.ts';-import { privateProvenanceFilterFragment } from '../search/private-visibility.ts';+import { privateProvenanceFilterUnderPolicy } from '../search/private-visibility.ts';export const SIMILAR_ACTIVE_LIMIT = 5;@@ -61,7 +61,7 @@ export async function similarActiveAfterForget(engine: BrainEngine, opts: {WHERE n.id = $1 AND n.source_id = $2AND c.expired_at IS NULL AND (c.valid_until IS NULL OR c.valid_until > now())AND c.embedding IS NOT NULL AND c.embedding_model = n.embedding_model- AND ($3::boolean = false OR c.visibility = 'world')${opts.excludePrivate ? ` AND ${privateProvenanceFilterFragment('c')}` : ''}+ AND ($3::boolean = false OR c.visibility = 'world')${opts.excludePrivate ? ` AND ${privateProvenanceFilterUnderPolicy('c')}` : ''}AND (1 - (c.embedding <=> n.embedding)) >= $4ORDER BY c.embedding <=> n.embedding, c.idLIMIT $5`,diff --git a/src/core/ops/context.ts b/src/core/ops/context.tsindex 2cc8490fa9..61cc2a5fed 100644--- a/src/core/ops/context.ts+++ b/src/core/ops/context.ts@@ -25,6 +25,7 @@ import { stampEvidence } from '../search/evidence.ts';import { captureEvalCandidate, isEvalCaptureEnabled, isEvalScrubEnabled } from '../eval-capture.ts';import type { SearchResult, HybridSearchMeta, PageReadScope, PageReadPolicy } from '../types.ts';import { resolveExcludePrivatePages, isPrivatePage } from '../search/private-visibility.ts';+import { assertRemoteTargetReadable, remoteTargetHidden } from '../persistence/page-visibility.ts';// --- Agent-contract fixes shared by the op error sites ---@@ -1067,6 +1068,7 @@ export async function requireWritablePage(throw opError('permission_denied', `${operation}: this page is outside your write visibility.`,'The page is private and this delegated write cannot see it; choose a different slug, or report the refusal to the user.');}+ await assertRemoteTargetReadable(ctx.engine, ctx.remote, writeSource, slug);return;}if (allowCreate) return;@@ -1077,7 +1079,7 @@ export async function requireWritablePage((visibleScope.sourceId !== undefined && visibleScope.sourceId !== writeSource);if (spansAnotherSource) {const visible = await ctx.engine.getPage(slug, visibleScope);- if (visible && visible.source_id !== writeSource) {+ if (visible && visible.source_id !== writeSource && !await remoteTargetHidden(ctx.engine, ctx.remote, visible.source_id, slug)) {throw opError('permission_denied',`${operation}${endpoint === 'page' ? '' : ` ${endpoint}`} page "${slug}" is readable from source "${visible.source_id}" but this client writes to source "${writeSource}".`,diff --git a/src/core/ops/files.ts b/src/core/ops/files.tsindex 9700bb2963..1639f4dfd0 100644--- a/src/core/ops/files.ts+++ b/src/core/ops/files.ts@@ -5,10 +5,11 @@* ../operations.ts. Never import from '../operations.ts' here (cycle).*/-import type { Operation } from './contract.ts';+import type { Operation, OperationContext } from './contract.ts';import { opError } from './contract.ts';import { hostFix, readFix } from './op-fix.ts';import { validateFilename, validatePageSlug, validateUploadPath } from './context.ts';+import { privatePagesFilterForCaller, resolveExcludePrivatePages } from '../search/private-visibility.ts';// --- File Operations ---@@ -17,6 +18,15 @@ import { validateFilename, validatePageSlug, validateUploadPath } from './contex// by targeting a page with many attachments.const FILE_LIST_LIMIT = 100;+/**+ * GBRA-218: a caller under the read policy (every remote caller, stdio included) sees no file attached to a page it+ * cannot read, by the page's id or by its slug in any source (a file row names its page loosely, so it fails closed).+ */+async function fileReadableSql(ctx: OperationContext): Promise<string> {+ if (!await resolveExcludePrivatePages(ctx.engine, ctx.remote)) return 'TRUE';+ return `NOT EXISTS (SELECT 1 FROM pages fp WHERE (fp.id = f.page_id OR fp.slug = f.page_slug) AND NOT ${privatePagesFilterForCaller('fp')})`;+}+const file_list: Operation = {name: 'file_list',mutating: false,@@ -29,12 +39,10 @@ const file_list: Operation = {scope: 'admin',localOnly: true,handler: async (ctx, p) => {- const { sqlQueryForEngine } = await import('../sql-query.ts');- const sql = sqlQueryForEngine(ctx.engine);const slug = p.slug as string | undefined;- const rows = slug- ? await sql`SELECT id, page_slug, filename, storage_path, mime_type, size_bytes, content_hash, created_at FROM files WHERE page_slug = ${slug} ORDER BY filename LIMIT ${FILE_LIST_LIMIT}`- : await sql`SELECT id, page_slug, filename, storage_path, mime_type, size_bytes, content_hash, created_at FROM files ORDER BY page_slug, filename LIMIT ${FILE_LIST_LIMIT}`;+ const rows = await ctx.engine.executeRaw<Record<string, unknown>>(`SELECT id, page_slug, filename, storage_path, mime_type, size_bytes, content_hash, created_at+ FROM files f WHERE (${slug === undefined ? 'TRUE' : 'f.page_slug = $2'}) AND ${await fileReadableSql(ctx)}+ ORDER BY ${slug === undefined ? 'page_slug, filename' : 'filename'} LIMIT $1`, slug === undefined ? [FILE_LIST_LIMIT] : [FILE_LIST_LIMIT, slug]);// Postgres returns size_bytes (BIGINT) as native BigInt — JSON.stringify// throws on those, breaking MCP callers. PGLite returns Number already.// 9 PB ceiling (2^53 bytes) is far above any plausible file size.@@ -175,9 +183,8 @@ const file_url: Operation = {scope: 'admin',localOnly: true,handler: async (ctx, p) => {- const { sqlQueryForEngine } = await import('../sql-query.ts');- const sql = sqlQueryForEngine(ctx.engine);- const rows = await sql`SELECT storage_path, mime_type, size_bytes FROM files WHERE storage_path = ${p.storage_path as string}`;+ const rows = await ctx.engine.executeRaw<Record<string, unknown>>(`SELECT storage_path, mime_type, size_bytes FROM files f+ WHERE storage_path = $1 AND ${await fileReadableSql(ctx)}`, [p.storage_path as string]);if (rows.length === 0) {throw opError('storage_error', `File not found: ${p.storage_path}`, 'No file row has that storage_path. List stored files (fix) and use a storage_path from that list.',{ fix: readFix('Lists stored files with their storage_path.', { argv: ['gbrain', 'files', 'list'], mcp: { tool: 'file_list', arguments: {} } }) });diff --git a/src/core/ops/pages.ts b/src/core/ops/pages.tsindex 4771c31999..b061e699e4 100644--- a/src/core/ops/pages.ts+++ b/src/core/ops/pages.ts@@ -367,6 +367,7 @@ const put_page: Operation = {return { dry_run: true, action: 'put_page', slug: p.slug };}if (ctx.remote !== false && typeof p.slug === 'string') await assertLargePageAdmission(ctx.engine, p.slug, sourceId, p);+ (await import('../persistence/page-visibility.ts')).assertSubmittedReaders(ctx.remote, p.slug, p.content);return submitPageMutation(ctx, { operation: 'put_page', params: p }).then(funnelPassThrough('save')).catch(error => rethrowNamingRevisionSource(ctx, sourceId, p, error));},cliHints: { name: 'put', positional: ['slug'], stdin: 'content' },diff --git a/src/core/ops/raw-data.ts b/src/core/ops/raw-data.tsindex 24d36daed6..315337ade5 100644--- a/src/core/ops/raw-data.ts+++ b/src/core/ops/raw-data.ts@@ -8,6 +8,7 @@import type { Operation } from './contract.ts';import { readPolicyOpts } from './context.ts';import { enforceClientSlugFence } from './context.ts';+import { assertRemoteTargetReadable } from '../persistence/page-visibility.ts';// --- Raw Data ---@@ -28,6 +29,7 @@ const put_raw_data: Operation = {if (ctx.dryRun) return { dry_run: true, action: 'put_raw_data', slug: p.slug, source: p.source };// v0.31.8 (D7 + D21): thread ctx.sourceId.const sourceOpts = ctx.sourceId ? { sourceId: ctx.sourceId } : {};+ await assertRemoteTargetReadable(ctx.engine, ctx.remote, ctx.sourceId || 'default', p.slug as string);await ctx.engine.putRawData(p.slug as string, p.source as string, p.data as object, sourceOpts);return { status: 'ok' };},diff --git a/src/core/persistence/consumer.ts b/src/core/persistence/consumer.tsindex 73e05465cc..d9067b1b12 100644--- a/src/core/persistence/consumer.ts+++ b/src/core/persistence/consumer.ts@@ -1,4 +1,5 @@import type { BrainEngine, ReservedConnection } from '../engine.ts';+import { withoutCallerAccess } from '../search/private-visibility.ts';import type { GBrainConfig } from '../config.ts';import { claimableWriteSql, foregroundPrioritySql, claimGroupFollowers, claimNextLaneHead, claimNextWrite, publicationGroupKey, syncGroup, floorPreparationAttempts, hasClaimableWrite, getWriteRequestById, releaseUnpublishedClaim, renewWriteClaim, vacuumPersistenceQueues, independentGroup, sweepSegmentStorage, yieldToEventLoop } from './journal.ts';import { connectionCapFailure, finishPreparationStalled, finishUnpublishedFailure, preparationAbortReason, recoverPublication, transientDatabaseFailure, type PreparedMutation } from './coordinator.ts';@@ -313,12 +314,13 @@ export class PersistenceConsumer implements PersistenceConsumerLike {if (this.timer && ms !== 0 && this.timerDueAt <= Date.now() + ms) return;if (this.timer) clearTimeout(this.timer);this.timerDueAt = Date.now() + ms;- this.timer = setTimeout(() => {+ // GBRA-218: a wake from inside a request must not run publication, effects or scans under that caller's access record.+ this.timer = withoutCallerAccess(() => setTimeout(() => {this.timer = undefined;const afterProgress = this.progressWake;this.progressWake = false;void this.tick(afterProgress).finally(() => { const next = this.nextDelayMs ?? this.pollMs; this.nextDelayMs = undefined; this.schedule(next); });- }, ms);+ }, ms));this.timer.unref?.();}/**diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.tsindex 4d556ca299..8362b2f472 100644--- a/src/core/persistence/page-prepare.ts+++ b/src/core/persistence/page-prepare.ts@@ -37,6 +37,7 @@ import { recordedPathFromFileUri, scannerSlugRootMode, scannerSourcePath } fromimport { engineMutationPrecondition, parseMutationPrecondition } from './preconditions.ts';import { assertPurgeParams } from './purge-params.ts';import { authorizeWrite, slugDisclosable } from './authority.ts';+import { remoteReadersChecked } from './page-visibility.ts';import { findLiveFileClaimants, type FileClaimant, type FileClaimTarget } from './file-claims.ts';import { digest, sha256 } from './digest.ts';import { replaceableFile } from '../facts/file-body-guard.ts';@@ -886,7 +887,7 @@ async function prepareInlineOrSegmentedMutation(unbounded: BrainEngine, row: Wri{ legacy_error: 'invalid_params' });throw opError('invalid_params', 'The content was rejected before publication.', 'Check the content and frontmatter, then submit the corrected content with a new request_id.');}- const ready = prepared;+ const ready = await remoteReadersChecked(engine, row.authority, snapshot?.page.frontmatter, prepared);if (ready.observedRevision !== observedRevision) throw pageRefusal('revision_conflict', 'The page changed during import preparation.', row,`Page ${row.slug} was written by another request while this one was being prepared, so it published nothing. Read the current page and revision, then submit the intended change against it.`,pageFix(row.source_id, row.slug));diff --git a/src/core/persistence/page-visibility.ts b/src/core/persistence/page-visibility.tsindex e047af07ba..d794d85e4f 100644--- a/src/core/persistence/page-visibility.ts+++ b/src/core/persistence/page-visibility.ts@@ -1,25 +1,95 @@import { opError } from '../ops/contract.ts';-import { REMOTE_PRIVATE_PAGES_KEY, privatePagesFilterFragment } from '../search/private-visibility.ts';+import { callerAccessAllowedSql, callerIdentity, readPolicyFragment, readRemotePolicyNow } from '../search/private-visibility.ts';+import { parseMarkdown } from '../markdown.ts';+import { memberIdentityFor } from '../access/caller.ts';+import { hasReaders, normalizePrincipal, normalizeReaders, readersAllow, NOBODY } from '../access/principals.ts';+import { catalogueError } from '../error-catalogue.ts';import type { SqlEngine, WriteAuthority } from './model.ts';-/** Publication uses fresh policy, independent of the read-side telemetry cache. */+/** Publication uses fresh policy, independent of the read-side telemetry cache (read through the resolver). */export async function excludesPrivateWrites(engine: SqlEngine, remote: boolean): Promise<boolean> {- if (!remote || process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1') return false;- const [row] = await engine.executeRaw<{ value: string }>('SELECT value FROM config WHERE key=$1', [REMOTE_PRIVATE_PAGES_KEY]);- return !['visible', 'true', '1'].includes(row?.value ?? '');+ if (!remote) return false;+ return !(await readRemotePolicyNow(engine)).privacyRelaxed;}-/** Recheck after page guards at publication; also hide inaccessible receipt targets. */+/**+ * Recheck after page guards at publication; also hide inaccessible receipt targets. GBRA-218: on a brain with+ * member-access restrictions the writer's identity is re-resolved from the stored principal on every check, so a+ * member disabled or unbound after submission (a queued job, a replay) loses the page before commit, and the+ * member-access part applies even when an operator relaxed privacy.+ */export async function authorizePageVisibility(engine: SqlEngine, authority: WriteAuthority, slug: string): Promise<void> {if (!authority.remote) return;- if (!(authority.excludePrivate ?? true) && !await excludesPrivateWrites(engine, true)) return;+ const policy = await readRemotePolicyNow(engine);+ const hidePrivate = (authority.excludePrivate ?? true) || !policy.privacyRelaxed;+ if (!hidePrivate && !policy.restricted) return;+ const identity = policy.restricted ? await memberIdentityFor(engine, authority) : [];const rows = await engine.executeRaw(`SELECT 1 FROM pages WHERE source_id=$1 AND slug=$2- AND NOT (${privatePagesFilterFragment('pages')}) LIMIT 1`, [authority.sourceId, slug]);+ AND NOT (${readPolicyFragment('pages', identity, hidePrivate)}) LIMIT 1`, [authority.sourceId, slug]);if (rows.length) throw pageNotVisibleError();}+/**+ * GBRA-218: a remote write that touches an existing page outside the durable write path (graph edges, raw data)+ * must be able to read it under the member-access part; privacy keeps the rule each operation already had.+ */+export async function assertRemoteTargetReadable(engine: SqlEngine, remote: boolean | undefined, sourceId: string, slug: string): Promise<void> {+ if (await remoteTargetHidden(engine, remote, sourceId, slug)) throw pageNotVisibleError();+}++/** Whether an existing page is hidden from the current remote caller by the member-access part (never for a trusted local caller). */+export async function remoteTargetHidden(engine: SqlEngine, remote: boolean | undefined, sourceId: string, slug: string): Promise<boolean> {+ if (remote === false) return false;+ const rows = await engine.executeRaw(`SELECT 1 FROM pages WHERE source_id=$1 AND slug=$2 AND NOT ${callerAccessAllowedSql('pages')} LIMIT 1`, [sourceId, slug]);+ return rows.length > 0;+}++/**+ * GBRA-218: a remote write's `readable_by` is well formed (readable_by_invalid names the bad entry and the corrected+ * YAML form) and keeps the page readable by the writer's `identity`. Checked at submission with the request's+ * identity and again at preparation with the identity re-resolved from the stored principal.+ */+export function assertRemoteReadersValid(frontmatter: unknown, identity: readonly string[]): void {+ if (!hasReaders(frontmatter)) return;+ const value = (frontmatter as Record<string, unknown>).readable_by;+ if (normalizeReaders(frontmatter)?.[0] === NOBODY) {+ const entries = typeof value === 'string' ? [value] : Array.isArray(value) ? value : [];+ const bad = entries.find(e => typeof e !== 'string' || normalizePrincipal(e) === null);+ const kept = entries.filter((e): e is string => typeof e === 'string' && normalizePrincipal(e) !== null).map(e => e.trim().toLowerCase());+ const example = `readable_by: [${(kept.length ? kept : ['alice-example', 'group:team-a']).join(', ')}]`;+ const what = bad !== undefined ? `the entry ${JSON.stringify(bad)} is not a member id or group:<id>`+ : entries.length === 0 ? 'it names no reader' : 'it is not a string or a list of strings (at most 256)';+ throw catalogueError('readable_by_invalid', `readable_by is invalid: ${what}.`,+ `Nothing was written. Member and group ids are lower-case letters, digits, '.', '_' and '-' (1-64 characters); write the field as ${example} and submit again with a new request_id.`);+ }+ if (!readersAllow(frontmatter, identity)) {+ throw catalogueError('readable_by_invalid', 'readable_by would leave this page unreadable by this connection.',+ `Nothing was written. Keep this connection's member${identity.length ? ` (${identity.join(', ')})` : ' (this connection is bound to none)'} or one of its groups in readable_by, or ask the user to change the readers on the brain host.`);+ }+}++/** A remote put_page's readable_by, checked with the request's identity before anything is admitted (preparation checks it again). */+export function assertSubmittedReaders(remote: boolean | undefined, slug: unknown, content: unknown): void {+ if (remote === false || typeof content !== 'string' || !content.includes('readable_by')) return;+ assertRemoteReadersValid(parseMarkdown(content, typeof slug === 'string' ? slug : 'page').frontmatter, callerIdentity());+}++/**+ * The preparation-time check, returning the prepared import it was given: the stored principal's live identity, and a+ * remote write never drops readable_by from a page that names readers.+ */+export async function remoteReadersChecked<T extends { parsedPage: { frontmatter: unknown } }>(engine: SqlEngine, authority: WriteAuthority, before: unknown, prepared: T): Promise<T> {+ const after = prepared.parsedPage.frontmatter;+ if (!authority.remote || (!hasReaders(before) && !hasReaders(after))) return prepared;+ if (!hasReaders(after)) {+ throw catalogueError('readable_by_invalid', 'This write removes readable_by from a page that names readers.',+ 'Nothing was written. A remote write keeps the readable_by line: read the page with get_page include_content:true and keep its frontmatter, or ask the user to widen the page on the brain host.');+ }+ assertRemoteReadersValid(after, await memberIdentityFor(engine, authority));+ return prepared;+}+/** The one refusal a remote writer gets for a target it may not see; other visibility refusals reuse it verbatim. */export function pageNotVisibleError() {- return opError('page_not_found', 'Page not found.', 'This page does not exist or is not visible to this connection, so nothing was written. Write to a different slug, or confirm the slug with a search first.');+ return opError('page_not_found', 'Page not found.', 'This page does not exist or is not visible to this connection, so nothing was written. Do not retry this slug as a create; ask the user whether the page exists and who can read it, or write to a different slug.');}-diff --git a/src/core/search/private-visibility.ts b/src/core/search/private-visibility.tsindex be02e466e2..557ecee963 100644--- a/src/core/search/private-visibility.ts+++ b/src/core/search/private-visibility.ts@@ -228,6 +228,21 @@ export function privatePagesFilterForCaller(pageAlias: string): string {return `(${accessAllowedSql(pageAlias, callerIdentity())} AND ${callerPrivacySql(pageAlias, privacyPartSql(pageAlias))})`;}+/** The mandatory member-access part alone (readers and lineage) for the current caller; privacy is not tested. */+export function callerAccessAllowedSql(pageAlias: string): string {+ return accessAllowedSql(pageAlias, callerIdentity());+}++/**+ * The read policy for an explicit identity, outside any caller record (a+ * write's publication re-resolves its writer's identity): the member-access+ * part, then the privacy part unless `hidePrivate` is false. With an empty+ * identity and `hidePrivate` it equals privatePagesFilterFragment.+ */+export function readPolicyFragment(pageAlias: string, identity: readonly string[], hidePrivate: boolean): string {+ return `(${accessAllowedSql(pageAlias, identity)} AND ${hidePrivate ? privacyPartSql(pageAlias) : 'TRUE'})`;+}+/** privatePagesFilterFragmentFast for a remote read. */export function privatePagesFilterFastForCaller(pageAlias: string): string {return `(${accessAllowedSql(pageAlias, callerIdentity())} AND ${callerPrivacySql(pageAlias, privacyPartFastSql(pageAlias))})`;@@ -450,6 +465,23 @@ export function privateTimelineEventFilterForCaller(timelineAlias: string): stri* consulted — fail-open for cross-source provenance, acceptable under source* isolation because ontology_propose stamps the fact with ctx.sourceId.*/+/**+ * GBRA-218: privateProvenanceFilterFragment for the fact reads a caller gates on `excludePrivate`. The readers part+ * always applies (a world fact held in or extracted from a page that names readers stays hidden from every remote+ * caller, as with the plain fragment); the privacy part is dropped when an operator relaxed privacy for this remote+ * read, which is what the hatches did to these reads before member access (`excludePrivate` was false then).+ * Outside a remote record nothing relaxes it.+ */+export function privateProvenanceFilterUnderPolicy(factAlias: string): string {+ return `NOT EXISTS (SELECT 1 FROM pages pp WHERE pp.source_id = ${factAlias}.source_id ` ++ `AND pp.slug = ${factAlias}.source_markdown_slug AND NOT ${readPolicyFragment('pp', [], !callerRelaxesPrivacy())})`;+}++/** True only inside a remote caller record whose read an operator relaxed (the env hatch or the config opt-out). */+export function callerRelaxesPrivacy(): boolean {+ return callerAccessStore.getStore()?.hidePrivate === false;+}+export function privateProvenanceFilterFragment(factAlias: string): string {return `NOT EXISTS (SELECT 1 FROM pages pp WHERE pp.source_id = ${factAlias}.source_id ` +`AND pp.slug = ${factAlias}.source_markdown_slug AND NOT (${privatePagesFilterFragment('pp')}))`;@@ -565,6 +597,24 @@ export async function remotePrivacyRelaxed(engine: BrainEngine): Promise<booleanreturn expose;}+/**+ * The remote read policy read fresh, for publication: whether an operator+ * relaxed privacy (the env hatch or the config opt-out, uncached) and whether+ * the brain holds member-access restrictions. Takes any executeRaw engine (a+ * transaction included). An unreadable config counts as not opted out and+ * restricted (fail closed); the env hatch needs no read.+ */+export async function readRemotePolicyNow(engine: Pick<BrainEngine, 'executeRaw'>): Promise<{ privacyRelaxed: boolean; restricted: boolean }> {+ const hatch = process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1';+ try {+ const rows = await engine.executeRaw<{ key: string; value: string }>('SELECT key, value FROM config WHERE key = ANY($1::text[])', [[REMOTE_PRIVATE_PAGES_KEY, ACCESS_RESTRICTIONS_KEY]]);+ const value = (key: string) => rows.find(r => r.key === key)?.value ?? '';+ return { privacyRelaxed: hatch || ['visible', 'true', '1'].includes(value(REMOTE_PRIVATE_PAGES_KEY)), restricted: value(ACCESS_RESTRICTIONS_KEY) === '1' };+ } catch {+ return { privacyRelaxed: hatch, restricted: true };+ }+}+/** GBRA-218: does this brain hold member-access restrictions? Read live (the request snapshot answers it inside a read); unreadable counts as yes. */export async function accessRestrictionsPresent(engine: BrainEngine): Promise<boolean> {try {diff --git a/test/access-enforcement-doctor.test.ts b/test/access-enforcement-doctor.test.tsnew file mode 100644index 0000000000..41f1f47881--- /dev/null+++ b/test/access-enforcement-doctor.test.ts@@ -0,0 +1,63 @@+/**+ * GBRA-218 member-access doctor checks on a repo-less PGLite brain:+ * access_readers_malformed, access_unknown_readers, access_recent_narrowings+ * and access_page_readers_backfill each find their case, carry a fix whose+ * verify is the read-only check, and are not applicable on a brain with no+ * member restriction.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+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 { memberAccessEntry } from '../src/commands/doctor/checks/member-access.ts';+import type { DoctorContext } from '../src/commands/doctor/context.ts';++const SRC = 'ad-team';+const page = (title: string, readers?: string) => ['---', 'type: note', 'title: ' + title, ...(readers ? ['readable_by: ' + readers] : []), '---', '# ' + title, '', 'Body.'].join('\n');++describe('member-access doctor checks (pglite, repo-less)', () => {+ let engine: BrainEngine;+ const run = async () => Object.fromEntries((await memberAccessEntry.run({ engine } as unknown as DoctorContext) as Array<{ name: string }>).map(c => [c.name, c])) as Record<string, any>;++ beforeAll(async () => {+ engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema();+ await engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1) ON CONFLICT (id) DO NOTHING', [SRC]);+ }, 120_000);+ afterAll(async () => { await engine.disconnect(); });++ test('a brain with no member restriction is not applicable for every check', async () => {+ await importFromContent(engine, 'notes/open', page('Open'), { sourceId: SRC, noEmbed: true });+ const checks = await run();+ expect(Object.keys(checks).sort()).toEqual(['access_page_readers_backfill', 'access_readers_malformed', 'access_recent_narrowings', 'access_unknown_readers']);+ for (const c of Object.values(checks)) expect(c).toMatchObject({ status: 'ok', readiness_state: 'not_applicable' });+ }, 60_000);++ test('each check finds its case', async () => {+ for (const id of ['alice-example', 'bob-example']) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1)', [id]);+ await engine.executeRaw("INSERT INTO access_groups (id) VALUES ('team-a'), ('team-empty')");+ await engine.executeRaw("INSERT INTO access_group_members (group_id, member_id) VALUES ('team-a', 'bob-example')");+ await importFromContent(engine, 'notes/fine', page('Fine', '[alice-example, group:team-a]'), { sourceId: SRC, noEmbed: true });+ await importFromContent(engine, 'notes/odd', page('Odd', '[alice-example, Not Valid]'), { sourceId: SRC, noEmbed: true });+ await importFromContent(engine, 'notes/typo', page('Typo', '[alice-exmaple, group:team-b, group:team-empty]'), { sourceId: SRC, noEmbed: true });+ await engine.createVersion('notes/open', { sourceId: SRC });+ await importFromContent(engine, 'notes/open', page('Open', '[bob-example]'), { sourceId: SRC, noEmbed: true });+ const checks = await run();++ expect(checks.access_readers_malformed).toMatchObject({ status: 'warn', details: { count: 1, samples: [SRC + ':notes/odd'] } });+ expect(checks.access_readers_malformed.fix).toMatchObject({ argv: ['gbrain', 'get', '--source', SRC, '--', 'notes/odd'], verify: { argv: ['gbrain', 'doctor', '--only', 'access_readers_malformed', '--json'] } });++ expect(checks.access_unknown_readers.status).toBe('warn');+ expect(checks.access_unknown_readers.details.unknown.map((u: { reader: string }) => u.reader).sort()).toEqual(['group:team-b', 'member:alice-exmaple']);+ expect(checks.access_unknown_readers.details.empty_groups).toEqual([{ reader: 'group:team-empty', pages: 1 }]);+ expect(checks.access_unknown_readers.fix).toMatchObject({ ask_user: true, verify: { argv: ['gbrain', 'doctor', '--only', 'access_unknown_readers', '--json'] } });++ expect(checks.access_recent_narrowings).toMatchObject({ status: 'warn', details: { count: 1, narrowings: [{ page: SRC + ':notes/open', readers: ['member:bob-example'] }] } });+ expect(checks.access_recent_narrowings.fix.verify).toEqual({ argv: ['gbrain', 'doctor', '--only', 'access_recent_narrowings', '--json'] });++ expect(checks.access_page_readers_backfill).toMatchObject({ status: 'ok', details: { count: 0 } });+ await engine.executeRaw('ALTER TABLE pages DISABLE TRIGGER pages_access_derive_readers');+ try { await engine.executeRaw("UPDATE pages SET page_readers = NULL WHERE slug = 'notes/fine'"); } finally { await engine.executeRaw('ALTER TABLE pages ENABLE TRIGGER pages_access_derive_readers'); }+ const after = await run();+ expect(after.access_page_readers_backfill).toMatchObject({ status: 'warn', details: { count: 1 }, fix: { argv: ['gbrain', 'repair', 'access', '--apply'], preview_argv: ['gbrain', 'repair', 'access', '--json'], ask_user: true, verify: { argv: ['gbrain', 'doctor', '--only', 'access_page_readers_backfill', '--json'] } } });+ }, 60_000);+});diff --git a/test/access-enforcement-guard.test.ts b/test/access-enforcement-guard.test.tsnew file mode 100644index 0000000000..e14df731ca--- /dev/null+++ b/test/access-enforcement-guard.test.ts@@ -0,0 +1,48 @@+/**+ * GBRA-218: scripts/check-remote-privacy-reads.ts fails on any read of the+ * remote-privacy hatch (GBRAIN_REMOTE_PRIVATE_PAGES) or opt-out+ * (search.remote_private_pages) outside the resolver, and passes the live tree.+ */+import { describe, expect, test } from 'bun:test';+import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { scan, scanSource } from '../scripts/check-remote-privacy-reads.ts';++describe('check-remote-privacy-reads', () => {+ test('the live tree reads the hatch and the opt-out only in the resolver', () => {+ expect(scan()).toEqual([]);+ });++ test('every read shape outside the resolver is flagged', () => {+ const reads = [+ "const on = process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1';",+ "const on = process.env['GBRAIN_REMOTE_PRIVATE_PAGES'];",+ 'const { GBRAIN_REMOTE_PRIVATE_PAGES } = process.env;',+ "const v = await engine.getConfig('search.remote_private_pages');",+ "await engine.executeRaw(\"SELECT value FROM config WHERE key = 'search.remote_private_pages'\");",+ "await engine.executeRaw(`SELECT value FROM config WHERE key = 'search.remote_private_pages' AND ${'$'}{x}`);",+ "import { REMOTE_PRIVATE_PAGES_KEY } from './private-visibility.ts';\nawait engine.getConfig(REMOTE_PRIVATE_PAGES_KEY);",+ "import { REMOTE_PRIVATE_PAGES_KEY } from './private-visibility.ts';\nawait engine.executeRaw('SELECT value FROM config WHERE key=$1', [REMOTE_PRIVATE_PAGES_KEY]);",+ ];+ for (const source of reads) expect({ source, flagged: scanSource('src/core/x.ts', source).length > 0 }).toEqual({ source, flagged: true });+ });++ test('name lists, imports and prose are not reads', () => {+ const fine = [+ "export const NAMES: readonly string[] = ['GBRAIN_REMOTE_PRIVATE_PAGES', 'GBRAIN_OTHER'];",+ "import { REMOTE_PRIVATE_PAGES_KEY } from './private-visibility.ts';\nexport const KEYS = ['a', REMOTE_PRIVATE_PAGES_KEY];",+ "throw new Error('An operator sets search.remote_private_pages=visible to show private pages.');",+ ];+ for (const source of fine) expect({ source, hits: scanSource('src/core/x.ts', source) }).toEqual({ source, hits: [] });+ });++ test('a fixture tree with a second reader fails, and the resolver itself is exempt', () => {+ const root = mkdtempSync(join(tmpdir(), 'gbrain-privacy-reads-'));+ mkdirSync(join(root, 'src/core/search'), { recursive: true });+ writeFileSync(join(root, 'src/core/search/private-visibility.ts'), "export const on = process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1';");+ expect(scan(root)).toEqual([]);+ writeFileSync(join(root, 'src/core/page-visibility.ts'), "export const on = process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1';");+ expect(scan(root)).toEqual([{ path: 'src/core/page-visibility.ts', line: 1, what: 'reads the env hatch GBRAIN_REMOTE_PRIVATE_PAGES' }]);+ });+});diff --git a/test/access-enforcement-spawn.test.ts b/test/access-enforcement-spawn.test.tsnew file mode 100644index 0000000000..90e3d1a73c--- /dev/null+++ b/test/access-enforcement-spawn.test.ts@@ -0,0 +1,51 @@+/**+ * GBRA-218: work a remote request spawns does not run under that caller's+ * access record. The spawn sites:+ * - the persistence consumer's tick timer (publication, post-commit effects,+ * recovery scans): a request's wake() schedules it (consumer.ts schedule);+ * - the facts queue pump (facts/queue.ts enqueue), fed by post-write+ * extraction backstops;+ * Each is entered through withoutCallerAccess, so a tick or job runs with the+ * empty identity and the process's privacy default, never with the member+ * identity (or relaxation) of whichever request woke it.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { currentCallerAccess, withCallerAccess } from '../src/core/search/private-visibility.ts';+import { startPersistenceConsumer, disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { FactsQueue } from '../src/core/facts/queue.ts';++const ALICE = { identity: ['member:alice-example', 'group:team-a'], hidePrivate: false, privateReadableSources: ['source-a'] };+const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));++describe('spawned work runs outside the caller access record', () => {+ let engine: BrainEngine;+ beforeAll(async () => {+ engine = new PGLiteEngine(); await engine.connect({ engine: 'pglite' } as never); await engine.initSchema();+ }, 120_000);+ afterAll(async () => { await disposePersistenceConsumer(engine); await engine.disconnect(); });++ test('the persistence consumer, started and woken inside a member request, runs its ticks with no caller record', async () => {+ const seen: string[][] = [];+ await withCallerAccess(ALICE, async () => {+ const consumer = startPersistenceConsumer(engine, { engine: 'pglite' } as never) as unknown as { tick(afterProgress?: boolean): Promise<void>; wake(progress?: boolean): void };+ const tick = consumer.tick.bind(consumer);+ consumer.tick = (afterProgress?: boolean) => { seen.push([...currentCallerAccess().identity]); return tick(afterProgress); };+ consumer.wake(true);+ });+ await sleep(800);+ expect(seen.length).toBeGreaterThan(0);+ expect(seen.filter(identity => identity.length > 0)).toEqual([]);+ }, 60_000);++ test('a facts job enqueued inside a member request runs with no caller record', async () => {+ const queue = new FactsQueue();+ let inside: ReturnType<typeof currentCallerAccess> | undefined;+ const outside = currentCallerAccess();+ withCallerAccess(ALICE, () => queue.enqueue(async () => { inside = currentCallerAccess(); }, 'session-a'));+ await queue.drainPending({ timeout: 2000 });+ expect(inside).toEqual(outside);+ expect(inside?.identity).toEqual([]);+ }, 30_000);+});diff --git a/test/access-writes.test.ts b/test/access-writes.test.tsnew file mode 100644index 0000000000..dd7c3cfadb--- /dev/null+++ b/test/access-writes.test.ts@@ -0,0 +1,214 @@+/**+ * GBRA-218: remote writes under per-member read access (frontmatter readable_by).+ *+ * 1. Protects: a remote writer cannot touch an existing page it cannot read on+ * any mutating op; it gets the canonical page_not_found ("do not retry this+ * slug as a create") and the page is unchanged. A listed member writes its+ * own page. A remote readable_by is validated before anything is written+ * (readable_by_invalid names the bad entry and the corrected form), and a+ * remote write can neither lock its writer out nor drop readable_by.+ * Publication re-resolves the writer's member from the stored principal, so+ * a member disabled after submission (a queued job, a replay) loses the page+ * before commit; no operator hatch relaxes that.+ * 2. Fails when: add_link, put_raw_data or any persistence write lands on an+ * unreadable page, a listed member is refused its own page, a malformed or+ * lock-out readable_by is written, or a stored write still publishes after+ * its member was disabled.+ * 3. Existing coverage (member-read-access) pins reads only.+ * 4. Real seams: tokens minted by the token service and bound with SQL, verified+ * by the serve --http verifier, dispatched through MCP dispatch; the durable+ * write path of a repo-less brain (no git checkout).+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+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 { dispatchToolCall } from '../src/mcp/dispatch.ts';+import { __resetPrivateVisibilityCacheForTests, withCallerAccess } from '../src/core/search/private-visibility.ts';+import { remoteCallerAccess } from '../src/core/access/caller.ts';+import { submissionAuthority, authorizeStoredRequest } from '../src/core/persistence/authority.ts';+import { admitWrite } from '../src/core/persistence/journal.ts';+import type { AuthInfo, OperationContext } from '../src/core/ops/contract.ts';+import { testBackends } from './helpers/test-backends.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { withEnv } from './helpers/with-env.ts';+import { randomUUID } from 'node:crypto';++const SRC = 'aw-team';+const ALICE_PAGE = 'notes/alice-ledger';+const OPEN_PAGE = 'notes/open-ledger';+const CANARY = 'canaryawalice';+const alicePage = (body = 'Ledger ' + CANARY + '.', readers = '[alice-example]') =>+ ['---', 'type: note', 'title: Alice ledger', ...(readers ? ['readable_by: ' + readers] : []), '---', '# Alice ledger', '', body].join('\n');++for (const kind of testBackends()) {+ describe('remote writes under member read access (' + kind + ', repo-less)', () => {+ let engine: BrainEngine;+ let close: () => Promise<void>;+ const tokens: Record<string, string> = {};+ const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });+ const verify = async (who: string) => await provider().verifyAccessToken(tokens[who]) as unknown as AuthInfo;++ async function call(who: string, op: string, params: Record<string, unknown>) {+ const auth = await verify(who);+ const out = await dispatchToolCall(engine, op, { ...params }, { remote: true, transport: 'http', auth, sourceId: auth.sourceId ?? 'default', takesHoldersAllowList: ['world'] });+ const text = out.content.map(c => c.text).join('\n');+ let json: Record<string, unknown> = {};+ try { json = JSON.parse(text); } catch { /* prose */ }+ return { isError: out.isError === true, text, json };+ }+ async function local(op: string, params: Record<string, unknown>) {+ const out = await dispatchToolCall(engine, op, { ...params }, { remote: false, sourceId: SRC });+ return out.content.map(c => c.text).join('\n');+ }+ async function state(slug = ALICE_PAGE) {+ const [page] = await engine.executeRaw<{ body: string; deleted: boolean; frontmatter: Record<string, unknown> }>(+ 'SELECT compiled_truth AS body, deleted_at IS NOT NULL AS deleted, frontmatter FROM pages WHERE source_id=$1 AND slug=$2', [SRC, slug]);+ const [counts] = await engine.executeRaw<{ tags: number; links: number; raw: number; timeline: number; takes: number }>(+ 'SELECT (SELECT count(*)::int FROM tags t JOIN pages p ON p.id=t.page_id WHERE p.slug=$1) AS tags,'+ + ' (SELECT count(*)::int FROM links l JOIN pages p ON p.id IN (l.from_page_id, l.to_page_id) WHERE p.slug=$1) AS links,'+ + ' (SELECT count(*)::int FROM raw_data r JOIN pages p ON p.id=r.page_id WHERE p.slug=$1) AS raw,'+ + ' (SELECT count(*)::int FROM timeline_entries e JOIN pages p ON p.id=e.page_id WHERE p.slug=$1) AS timeline,'+ + ' (SELECT count(*)::int FROM takes k JOIN pages p ON p.id=k.page_id WHERE p.slug=$1) AS takes', [slug]);+ return { ...page, ...counts };+ }+ async function revision(slug = ALICE_PAGE): Promise<string> {+ return (JSON.parse(await local('get_page', { slug, include_content: true })) as { revision: string }).revision;+ }++ 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]);+ await importFromContent(engine, ALICE_PAGE, alicePage(), { sourceId: SRC, noEmbed: true });+ await importFromContent(engine, ALICE_PAGE, alicePage('Ledger ' + CANARY + '. Second draft.'), { sourceId: SRC, noEmbed: true });+ await engine.createVersion(ALICE_PAGE, { sourceId: SRC });+ await importFromContent(engine, OPEN_PAGE, '---\ntype: note\ntitle: Open ledger\n---\n# Open ledger\n\nLedger canaryawopen.', { sourceId: SRC, noEmbed: true });+ for (const id of ['alice-example', 'bob-example']) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1)', [id]);+ for (const [name, member] of [['alice', 'alice-example'], ['bob', 'bob-example'], ['unbound', null]] as const) {+ const minted = await mintLegacyToken(engine, { name, scopes: ['read', 'write'], takesHolders: ['world'], sourceGrant: [SRC] });+ tokens[name] = minted.token;+ if (member) await engine.executeRaw('UPDATE access_tokens SET member_id = $2 WHERE id = $1::uuid', [minted.id, member]);+ }+ }, 240_000);+ afterAll(async () => { await close?.(); }, 60_000);++ /** Every mutating op with arguments that touch ALICE_PAGE; the put_page content passes the writer's own readers check, so only the page's readers refuse it. */+ const mutations = async (who = 'bob'): Promise<Array<[string, string, Record<string, unknown>]>> => [+ ['put_page', 'put_page', { slug: ALICE_PAGE, content: alicePage('Rewritten.', who === 'bob' ? '[alice-example, bob-example]' : ''), expected_revision: await revision() }],+ ['edit_page', 'edit_page', { slug: ALICE_PAGE, expected_revision: await revision(), edits: [{ old_text: 'Second draft', new_text: 'Edited' }] }],+ ['append_page', 'append_page', { slug: ALICE_PAGE, text: 'Appended.', request_id: randomUUID() }],+ ['add_link from', 'add_link', { from: ALICE_PAGE, to: OPEN_PAGE }],+ ['add_link to', 'add_link', { from: OPEN_PAGE, to: ALICE_PAGE }],+ ['add_tag', 'add_tag', { slug: ALICE_PAGE, tag: 'x-tag' }],+ ['add_timeline_entry', 'add_timeline_entry', { slug: ALICE_PAGE, date: '2026-01-01', summary: 'Planning call' }],+ ['put_raw_data', 'put_raw_data', { slug: ALICE_PAGE, source: 'acme-example', data: { a: 1 } }],+ ['revert_version', 'revert_version', { slug: ALICE_PAGE, version_id: 1 }],+ ['takes_add', 'takes_add', { slug: ALICE_PAGE, claim: 'A take', holder: 'world', kind: 'take' }],+ ['remove_tag', 'remove_tag', { slug: ALICE_PAGE, tag: 'x-tag' }],+ ['delete_page', 'delete_page', { slug: ALICE_PAGE }],+ ];++ test('an unlisted member and an unbound token get the canonical page_not_found on every mutating op, and the page is unchanged', async () => {+ const before = await state();+ for (const who of ['bob', 'unbound']) {+ for (const [label, op, params] of await mutations(who)) {+ const out = await call(who, op, params);+ expect({ who, label, code: out.json.code, suggestion: out.json.suggestion }).toEqual({ who, label, code: 'page_not_found',+ suggestion: expect.stringContaining('Do not retry this slug as a create; ask the user') });+ expect(out.text).not.toContain(CANARY);+ }+ }+ expect(await state()).toEqual(before);+ }, 240_000);++ test('under GBRAIN_REMOTE_PRIVATE_PAGES=1 an unlisted member is still refused', async () => {+ const before = await state();+ await withEnv({ GBRAIN_REMOTE_PRIVATE_PAGES: '1' }, async () => {+ for (const [label, op, params] of (await mutations()).filter(([l]) => ['put_page', 'add_link from', 'add_tag', 'put_raw_data', 'delete_page'].includes(l))) {+ expect({ label, code: (await call('bob', op, params)).json.code }).toEqual({ label, code: 'page_not_found' });+ }+ });+ expect(await state()).toEqual(before);+ }, 240_000);++ test('a listed member writes its own page through the same ops', async () => {+ const put = await call('alice', 'put_page', { slug: ALICE_PAGE, content: alicePage('Ledger ' + CANARY + '. Second draft. Alice wrote this.'), expected_revision: await revision() });+ expect({ isError: put.isError, text: put.text.slice(0, 200) }).toEqual({ isError: false, text: expect.not.stringContaining('page_not_found') });+ for (const [op, params] of [['add_tag', { slug: ALICE_PAGE, tag: 'x-tag' }], ['add_link', { from: ALICE_PAGE, to: OPEN_PAGE }],+ ['put_raw_data', { slug: ALICE_PAGE, source: 'acme-example', data: { a: 1 } }], ['add_timeline_entry', { slug: ALICE_PAGE, date: '2026-01-02', summary: 'Alice call' }]] as const) {+ const out = await call('alice', op, params as Record<string, unknown>);+ expect({ op, isError: out.isError, code: out.json.code }).toEqual({ op, isError: false, code: undefined });+ }+ const after = await state();+ expect(after.body).toContain('Alice wrote this.');+ expect({ tags: after.tags, raw: after.raw, timeline: after.timeline > 0, links: after.links > 0, readers: after.frontmatter.readable_by })+ .toEqual({ tags: 1, raw: 1, timeline: true, links: true, readers: ['alice-example'] });+ }, 240_000);++ test('a malformed readable_by is refused before anything is written, naming the entry and the corrected form', async () => {+ const out = await call('alice', 'put_page', { slug: 'notes/alice-new', content: alicePage('New.', '[alice-example, Not Valid]') });+ expect(out.json).toMatchObject({ code: 'readable_by_invalid', message: expect.stringContaining('"Not Valid"'),+ suggestion: expect.stringContaining('readable_by: [alice-example]'), docs: expect.stringContaining('write-refusals.md#readable_by_invalid') });+ for (const value of ['[]', '[alice-example, 7]']) {+ expect((await call('alice', 'put_page', { slug: 'notes/alice-new', content: alicePage('New.', value) })).json.code).toBe('readable_by_invalid');+ }+ const [rows] = await engine.executeRaw<{ n: number }>("SELECT count(*)::int AS n FROM pages WHERE slug = 'notes/alice-new'");+ expect(rows.n).toBe(0);+ const [requests] = await engine.executeRaw<{ n: number }>("SELECT count(*)::int AS n FROM persistence_requests WHERE slug = 'notes/alice-new'");+ expect(requests.n).toBe(0);+ }, 120_000);++ test('a remote write cannot lock its writer out or drop readable_by', async () => {+ const lockout = await call('alice', 'put_page', { slug: 'notes/alice-new', content: alicePage('New.', '[bob-example]') });+ expect(lockout.json).toMatchObject({ code: 'readable_by_invalid', message: expect.stringContaining('unreadable by this connection') });+ const unbound = await call('unbound', 'put_page', { slug: 'notes/unbound-new', content: alicePage('New.', '[alice-example]') });+ expect(unbound.json.code).toBe('readable_by_invalid');+ const edit = await call('alice', 'edit_page', { slug: ALICE_PAGE, expected_revision: await revision(), edits: [{ old_text: '- alice-example', new_text: '- bob-example' }] });+ expect({ code: edit.json.code, text: edit.text.slice(0, 300) }).toEqual({ code: 'readable_by_invalid', text: expect.stringContaining('unreadable by this connection') });+ expect((await state()).frontmatter.readable_by).toEqual(['alice-example']);+ const ok = await call('alice', 'put_page', { slug: 'notes/alice-new', content: alicePage('New.', '[alice-example, group:team-a]') });+ expect(ok.isError).toBe(false);+ const drop = await call('alice', 'put_page', { slug: 'notes/alice-new', content: alicePage('New, now open.', ''), expected_revision: await revision('notes/alice-new') });+ expect(drop.json).toMatchObject({ code: 'readable_by_invalid', message: expect.stringContaining('removes readable_by') });+ expect((await state('notes/alice-new')).frontmatter.readable_by).toEqual(['alice-example', 'group:team-a']);+ }, 120_000);++ test('publication re-resolves the writer: a stored write whose member is disabled before commit is refused, and a hatch does not relax it', async () => {+ const auth = await verify('alice');+ const ctx = { engine, config: { engine: 'pglite' }, sourceId: SRC, auth, remote: true, dryRun: false, takesHoldersAllowList: ['world'],+ logger: { info() {}, warn() {}, error() {} } } as unknown as OperationContext;+ const [source] = await engine.executeRaw<{ incarnation: string }>('SELECT incarnation FROM sources WHERE id=$1', [SRC]);+ const authority = await withCallerAccess(await remoteCallerAccess(engine, auth, undefined),+ () => submissionAuthority(ctx, 'add_tag', SRC, source.incarnation, ALICE_PAGE));+ const row = await admitWrite(engine, { principal: authority.principal, authority, operation: 'add_tag', sourceId: SRC,+ sourceIncarnation: source.incarnation, slug: ALICE_PAGE, requestId: randomUUID(), callerIntent: {}, intent: {} });+ await authorizeStoredRequest(engine, row);+ await engine.executeRaw("UPDATE access_members SET disabled_at = now() WHERE id = 'alice-example'");+ try {+ await expect(authorizeStoredRequest(engine, row)).rejects.toMatchObject({ code: 'page_not_found' });+ await withEnv({ GBRAIN_REMOTE_PRIVATE_PAGES: '1' }, () => expect(authorizeStoredRequest(engine, row)).rejects.toMatchObject({ code: 'page_not_found' }));+ } finally {+ await engine.executeRaw("UPDATE access_members SET disabled_at = NULL WHERE id = 'alice-example'");+ }+ await authorizeStoredRequest(engine, row);+ await engine.executeRaw("UPDATE access_tokens SET member_id = NULL WHERE name = 'alice'");+ try {+ await expect(authorizeStoredRequest(engine, row)).rejects.toMatchObject({ code: 'page_not_found' });+ } finally {+ await engine.executeRaw("UPDATE access_tokens SET member_id = 'alice-example' WHERE name = 'alice'");+ }+ }, 120_000);++ test('extract_facts treats a restricted source_slug exactly like a missing one', async () => {+ const hidden = await call('bob', 'extract_facts', { turn_text: 'Bob likes tea.', source_slug: ALICE_PAGE, visibility: 'world' });+ const missing = await call('bob', 'extract_facts', { turn_text: 'Bob likes tea.', source_slug: 'notes/never-written', visibility: 'world' });+ expect(hidden.text.replaceAll(ALICE_PAGE, '<slug>')).toEqual(missing.text.replaceAll('notes/never-written', '<slug>'));+ expect(hidden.text).not.toContain(CANARY);+ }, 120_000);+ });+}diff --git a/test/fixtures/goldens/doctor/registry-build-checks.json b/test/fixtures/goldens/doctor/registry-build-checks.jsonindex 19502ddb58..e915286550 100644--- a/test/fixtures/goldens/doctor/registry-build-checks.json+++ b/test/fixtures/goldens/doctor/registry-build-checks.json@@ -353,6 +353,22 @@"category": "brain","name": "derived_visibility"},+ {+ "category": "brain",+ "name": "access_readers_malformed"+ },+ {+ "category": "brain",+ "name": "access_unknown_readers"+ },+ {+ "category": "brain",+ "name": "access_recent_narrowings"+ },+ {+ "category": "brain",+ "name": "access_page_readers_backfill"+ },{"category": "ops","name": "rls"diff --git a/test/fixtures/guards/check-remote-privacy-reads.ts/bad-config-key/src/core/context/brief.ts b/test/fixtures/guards/check-remote-privacy-reads.ts/bad-config-key/src/core/context/brief.tsnew file mode 100644index 0000000000..5545edd4de--- /dev/null+++ b/test/fixtures/guards/check-remote-privacy-reads.ts/bad-config-key/src/core/context/brief.ts@@ -0,0 +1 @@+export const KEY_SQL = "SELECT value FROM config WHERE key = 'search.remote_private_pages'";diff --git a/test/fixtures/guards/check-remote-privacy-reads.ts/bad/src/core/search/hybrid.ts b/test/fixtures/guards/check-remote-privacy-reads.ts/bad/src/core/search/hybrid.tsnew file mode 100644index 0000000000..7fd38451bd--- /dev/null+++ b/test/fixtures/guards/check-remote-privacy-reads.ts/bad/src/core/search/hybrid.ts@@ -0,0 +1,3 @@+export function relaxed(): boolean {+ return process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1';+}diff --git a/test/fixtures/guards/check-remote-privacy-reads.ts/good/src/core/env-trust.ts b/test/fixtures/guards/check-remote-privacy-reads.ts/good/src/core/env-trust.tsnew file mode 100644index 0000000000..85413758fa--- /dev/null+++ b/test/fixtures/guards/check-remote-privacy-reads.ts/good/src/core/env-trust.ts@@ -0,0 +1 @@+export const TRUSTED_ENV_NAMES = ['GBRAIN_REMOTE_PRIVATE_PAGES', 'GBRAIN_HOME'];diff --git a/test/fixtures/guards/check-remote-privacy-reads.ts/good/src/core/search/private-visibility.ts b/test/fixtures/guards/check-remote-privacy-reads.ts/good/src/core/search/private-visibility.tsnew file mode 100644index 0000000000..7fd38451bd--- /dev/null+++ b/test/fixtures/guards/check-remote-privacy-reads.ts/good/src/core/search/private-visibility.ts@@ -0,0 +1,3 @@+export function relaxed(): boolean {+ return process.env.GBRAIN_REMOTE_PRIVATE_PAGES === '1';+}diff --git a/test/member-read-access.test.ts b/test/member-read-access.test.tsindex 984e44755e..67bd9285d3 100644--- a/test/member-read-access.test.ts+++ b/test/member-read-access.test.ts@@ -30,6 +30,8 @@ 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 type { AuthInfo } from '../src/core/ops/contract.ts';+import { authorizePageVisibility } from '../src/core/persistence/page-visibility.ts';+import type { WriteAuthority } from '../src/core/persistence/model.ts';import { testBackends } from './helpers/test-backends.ts';import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';import { withEnv } from './helpers/with-env.ts';@@ -63,9 +65,19 @@ function page(key: PageKey): string {`# ${key} plan`, '', `Harbor ledger planning notes ${p.canary} for the roadmap.`].join('\n');}-/** Every remote read path, with arguments a restricted page in SRC answers. */-function reads(slug: string): Array<[string, string, Record<string, unknown>]> {+/** Every remote read path, with arguments a restricted page in SRC answers (and the transport, when not http). */+function reads(slug: string): Array<[string, string, Record<string, unknown>, ('http' | 'stdio')?]> {return [+ ['fetch', 'fetch', { id: slug }],+ ['get_raw_data', 'get_raw_data', { slug }],+ ['file_list', 'file_list', { slug }, 'stdio'],+ ['file_list all', 'file_list', {}, 'stdio'],+ ['takes_list', 'takes_list', { page_slug: slug }],+ ['takes_list all', 'takes_list', {}],+ ['takes_search', 'takes_search', { query: 'harbor take' }],+ ['facts list', 'recall', { entity: slug }],+ ['facts since', 'recall', { since: '2000-01-01' }],+ ['facts search', 'recall', { question: 'harbor fact' }],['search hybrid', 'search', { query: 'harbor ledger planning notes', fields: 'full', limit: 20 }],['search keyword', 'search', { query: 'planning notes', match: 'keyword', fields: 'full', limit: 20 }],['query', 'query', { query: 'harbor ledger planning notes', expand: false, fields: 'full', limit: 20 }],@@ -81,7 +93,7 @@ function reads(slug: string): Array<[string, string, Record<string, unknown>]> {['resolve_slugs', 'resolve_slugs', { partial: slug.slice('notes/'.length) }],];}-const ECHOES_SLUG = new Set(['get_page', 'get_chunks', 'get_versions', 'get_timeline', 'get_links', 'traverse_graph']);+const ECHOES_SLUG = new Set(['get_page', 'get_chunks', 'get_versions', 'get_timeline', 'get_links', 'traverse_graph', 'fetch', 'get_raw_data', 'takes_list', 'facts list']);const LINK_PATHS = new Set(['get_links', 'traverse_graph']);const CORE = new Set(['search hybrid', 'search keyword', 'get_page', 'list_pages', 'get_chunks', 'get_timeline']);@@ -94,12 +106,12 @@ for (const kind of testBackends()) {const provider = () => new GBrainOAuthProvider({ sql: sqlQueryForEngine(engine), transaction: fn => engine.transaction(tx => fn(sqlQueryForEngine(tx))) });- async function call(who: string, op: string, params: Record<string, unknown>): Promise<{ isError: boolean; text: string }> {+ async function call(who: string, op: string, params: Record<string, unknown>, transport: 'http' | 'stdio' = 'http'): 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 dispatchToolCall(engine, op, { ...params }, {- remote: true, transport: 'http', auth, sourceId, takesHoldersAllowList: auth.takesHoldersAllowList ?? ['world'],+ remote: true, transport, auth, sourceId, takesHoldersAllowList: auth.takesHoldersAllowList ?? ['world'],...(localFederated ? { localFederatedSourceIds: localFederated } : {}),});return { isError: out.isError === true, text: out.content.map(c => c.text).join('\n') };@@ -112,14 +124,30 @@ for (const kind of testBackends()) {async function visibleTo(who: string, keys: readonly PageKey[]): Promise<Record<string, PageKey[]>> {const out: Record<string, PageKey[]> = {};for (const key of keys) {- for (const [label, op, params] of reads(PAGES[key].slug)) {- const { text } = await call(who, op, params);+ for (const [label, op, params, transport] of reads(PAGES[key].slug)) {+ const { text } = await call(who, op, params, transport);if (seen(text, key, label)) (out[label] ??= []).push(key);}}return out;}+ /** The three operator widenings: each relaxes privacy only. granted_sources is applied by rescoping every member token to it. */+ const WIDENING_ARMS: Array<{ name: string; run: (fn: () => Promise<void>) => Promise<void> }> = [+ { name: 'GBRAIN_REMOTE_PRIVATE_PAGES=1', run: fn => withEnv({ GBRAIN_REMOTE_PRIVATE_PAGES: '1' }, fn) },+ { name: 'search.remote_private_pages=visible', run: async fn => {+ await engine.setConfig('search.remote_private_pages', 'visible');+ __resetPrivateVisibilityCacheForTests();+ try { await fn(); } finally { await engine.setConfig('search.remote_private_pages', 'hidden'); __resetPrivateVisibilityCacheForTests(); }+ } },+ { name: 'granted_sources', run: async fn => {+ for (const who of ['alice', 'bob', 'dan', 'unbound']) await rescopeLegacyToken(engine, parseRescopeTokenArgs([who, '--private-pages', 'granted-sources']));+ try { await fn(); } finally {+ for (const who of ['alice', 'bob', 'dan', 'unbound']) await rescopeLegacyToken(engine, parseRescopeTokenArgs([who, '--private-pages', 'none']));+ }+ } },+ ];+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);@@ -136,6 +164,13 @@ for (const kind of testBackends()) {await engine.addLink(slug, TARGET, 'roadmap', 'mentions', 'manual', undefined, undefined, { fromSourceId: SRC, toSourceId: SRC });await engine.addTimelineEntry(slug, { date: '2026-09-01', summary: `Planning call ${canary}` }, { sourceId: SRC });await engine.createVersion(slug, { sourceId: SRC });+ for (const [op, params] of [['remember', { fact: `Harbor fact ${canary}`, provenance: 'fixture', entity: slug, visibility: 'world' }],+ ['takes_add', { slug, claim: `Harbor take ${canary}`, kind: 'take', holder: 'world' }]] as const) {+ const out = await dispatchToolCall(engine, op, { ...params }, { remote: false, sourceId: SRC });+ if (out.isError) throw new Error(`fixture ${op}: ${out.content.map(c => c.text).join(' ')}`);+ }+ await engine.putRawData(slug, 'acme-example', { note: canary }, { sourceId: SRC });+ await engine.executeRaw("INSERT INTO files (source_id, page_slug, filename, storage_path, content_hash) VALUES ($1, $2, $3, $4, 'h')", [SRC, slug, `${canary}.pdf`, `mr/${canary}.pdf`]);}for (const id of ['alice-example', 'bob-example', 'dan-example']) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1)', [id]);await engine.executeRaw("INSERT INTO access_groups (id) VALUES ('team-a')");@@ -219,6 +254,85 @@ for (const kind of testBackends()) {}, 240_000);}+ /** Which restricted pages the publication gate (persistence/page-visibility.ts) lets `who` write as a stored remote authority. */+ async function publishable(who: string, excludePrivate = true): Promise<PageKey[]> {+ const authority = { version: 1, principal: { kind: 'legacy_token', id: tokenIds[who] }, remote: true, excludePrivate, sourceId: SRC,+ sourceIncarnation: 'unused', scopes: ['read', 'write'], operations: null, slugPrefixes: null } as WriteAuthority;+ const out: PageKey[] = [];+ for (const key of Object.keys(PAGES) as PageKey[]) {+ if (await authorizePageVisibility(engine, authority, PAGES[key].slug).then(() => true, () => false)) out.push(key);+ }+ return out;+ }++ test('the publication gate applies the readers test to the stored principal, under every widening arm', async () => {+ const expected: Record<string, PageKey[]> = { alice: ['alice'], bob: ['team'], dan: [], unbound: [], bot: [], ghost: [], 'alice-granted': ['alice'] };+ for (const [who, keys] of Object.entries(expected)) expect({ who, keys: await publishable(who) }).toEqual({ who, keys });+ for (const arm of WIDENING_ARMS) {+ await arm.run(async () => {+ for (const [who, keys] of Object.entries(expected)) {+ const relaxed = arm.name === 'granted_sources' ? keys : who.startsWith('alice') ? [...keys, 'privateAlice' as const] : keys;+ expect({ arm: arm.name, who, keys: await publishable(who, arm.name === 'granted_sources') }).toEqual({ arm: arm.name, who, keys: relaxed });+ }+ });+ }+ }, 120_000);++ test('a derivative or fact made while its origin was world stays hidden from every remote caller once the origin names readers', async () => {+ const ORIGIN = 'notes/retro-origin';+ const DERIVED = { atom: 'atoms/retro-atom', summary: 'digests/retro-summary', concept: 'concepts/retro-concept' };+ await importFromContent(engine, ORIGIN, '---\ntype: note\ntitle: Retro origin\n---\n# Retro origin\n\nHarbor ledger canaryretroorigin.', { sourceId: SRC });+ await importFromContent(engine, DERIVED.atom, '---\ntype: atom\ntitle: Retro atom\nvisibility: world\nsource_slug: notes/retro-origin\n---\n# Retro atom\n\nHarbor ledger canaryretroatom.', { sourceId: SRC });+ await importFromContent(engine, DERIVED.summary, '---\ntype: note\ntitle: Retro summary\nderived_from: [notes/retro-origin]\n---\n# Retro summary\n\nHarbor ledger canaryretrosummary.', { sourceId: SRC });+ await importFromContent(engine, DERIVED.concept, '---\ntype: concept\ntitle: Retro concept\nvisibility: world\nsynthesized_by: fixture\n---\n# Retro concept\n\nHarbor ledger canaryretroconcept.', { sourceId: SRC });+ await engine.addLink(DERIVED.concept, DERIVED.atom, 'input', 'synthesized_from', 'concept-provenance', undefined, undefined, { fromSourceId: SRC, toSourceId: SRC });+ const remembered = await dispatchToolCall(engine, 'remember', { fact: 'Retro fact canaryretrofact', provenance: 'fixture', entity: ORIGIN, visibility: 'world' }, { remote: false, sourceId: SRC });+ expect(remembered.isError).not.toBe(true);+ const derivedSeen = async (who: string) => {+ const out: string[] = [];+ for (const [name, slug] of Object.entries(DERIVED)) {+ const page = await call(who, 'get_page', { slug });+ if (page.text.includes(`canaryretro${name}`)) out.push(`get_page:${name}`);+ const hits = await call(who, 'search', { query: `canaryretro${name}`, match: 'keyword', fields: 'full' });+ if (hits.text.includes(`canaryretro${name}`)) out.push(`search:${name}`);+ }+ for (const [arm, params] of [['entity', { entity: ORIGIN }], ['since', { since: '2000-01-01' }], ['question', { question: 'retro fact canaryretrofact' }]] as const) {+ if ((await call(who, 'recall', params)).text.includes('canaryretrofact')) out.push(`facts:${arm}`);+ }+ return out;+ };+ expect(await derivedSeen('unbound')).toEqual(['get_page:atom', 'search:atom', 'get_page:summary', 'search:summary', 'get_page:concept', 'search:concept', 'facts:entity', 'facts:since', 'facts:question']);+ await importFromContent(engine, ORIGIN, '---\ntype: note\ntitle: Retro origin\nreadable_by: [alice-example]\n---\n# Retro origin\n\nHarbor ledger canaryretroorigin.', { sourceId: SRC });+ expect((await call('alice', 'get_page', { slug: ORIGIN })).text).toContain('canaryretroorigin');+ const callers = ['alice', 'alice-granted', 'bob', 'unbound', 'bot'];+ for (const who of callers) expect({ who, seen: await derivedSeen(who) }).toEqual({ who, seen: [] });+ for (const arm of WIDENING_ARMS) {+ await arm.run(async () => {+ for (const who of callers) expect({ arm: arm.name, who, seen: await derivedSeen(who) }).toEqual({ arm: arm.name, who, seen: [] });+ });+ }+ }, 240_000);++ test('slug enumeration answers a restricted page exactly like a missing one', async () => {+ const MISSING = 'notes/never-written';+ const shape = (text: string, slug: string) => text.replaceAll(slug, '<slug>').replace(/"(?:request_id|took_ms|elapsed_ms)":[^,}]*/g, '');+ await engine.addLink(OPEN.slug, PAGES.alice.slug, 'see', 'mentions', 'manual', undefined, undefined, { fromSourceId: SRC, toSourceId: SRC });+ for (const who of ['bob', 'unbound']) {+ for (const [op, params] of [['get_page', {}], ['get_page', { fuzzy: true }], ['get_page', { include_deleted: true }], ['get_chunks', {}],+ ['get_backlinks', {}], ['get_links', {}], ['get_timeline', {}], ['get_versions', {}], ['traverse_graph', { depth: 1 }]] as const) {+ const hidden = await call(who, op, { slug: PAGES.alice.slug, ...params });+ const missing = await call(who, op, { slug: MISSING, ...params });+ expect({ who, op, params, text: shape(hidden.text, PAGES.alice.slug) }).toEqual({ who, op, params, text: shape(missing.text, MISSING) });+ }+ for (const [op, params] of [['get_links', { slug: OPEN.slug }], ['traverse_graph', { slug: OPEN.slug, depth: 2 }], ['resolve_slugs', { partial: 'alice-pl' }],+ ['resolve_slugs', { partial: 'alice plan' }], ['get_page', { slug: 'notes/alice-pla', fuzzy: true }], ['list_pages', { slug_prefix: 'notes/alice' }]] as const) {+ const out = await call(who, op, params);+ expect({ who, op, params, leaked: out.text.includes(PAGES.alice.slug) || out.text.includes('alice plan') }).toEqual({ who, op, params, leaked: false });+ }+ }+ expect((await call('alice', 'get_links', { slug: OPEN.slug })).text).toContain(PAGES.alice.slug);+ }, 240_000);+test('a disabled member loses access on the next request', async () => {await engine.executeRaw("UPDATE access_members SET disabled_at = now() WHERE id = 'alice-example'");try {diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex 015b3eeec9..8f475c2065 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -11,6 +11,7 @@# file is a one-line edit: insert its path in sorted order. Shards balance# other Postgres lane runs. The unit, serial and slow lanes unset# themselves from scripts/postgres-arm-weights.json.+test/access-writes.test.tstest/activation-unbound-rooted-6449.test.tstest/alias-source-cascade-5094.test.tstest/attribution-backfill-history.test.ts@@ -244,6 +245,7 @@ test/managed-sync-preparation-stalled-holds.test.tstest/managed-sync-row-parity.test.tstest/managed-sync-stale-group-head.test.tstest/mcp-unreadable-sources-notice.test.ts+test/member-read-access.test.tstest/migration-persistence-request-id-index.test.tstest/migration-persistence-request-slug-index.test.tstest/on-commit-embedding.serial.test.ts