Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: a rebuild replays the committed folder-rule history; derived pages follow a renamed origin through slug_aliases (GBRA-218)

garrytan433f658cd6ef
diff --git a/docs/architecture/key-files/core-search-1.md b/docs/architecture/key-files/core-search-1.md
index 95ffeeae6d..67e2568413 100644
--- a/docs/architecture/key-files/core-search-1.md
+++ b/docs/architecture/key-files/core-search-1.md
@@ -66,7 +66,7 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL
- `src/core/search/modes-report.ts` — `buildModesReport(engine)` → `SearchModesReport` (`schema_version: 2`), the read-only dashboard behind `gbrain search modes` and the `search_modes` MCP op: `active_mode` + validity, the per-knob `resolved` attribution (`attributeKnob` over `KNOB_DESCRIPTIONS`, including the five `reranker_*` knobs), the three frozen `bundles`, `config_keys`, and `reranker_readiness` — `{model, enabled, ready, required_key, key_present, self_hosted, fix}` from `rerankerReadinessForEngine` + `describeRerankerFix` (a thrown readiness check still yields a `ready: false` verdict whose `fix` says to run `gbrain doctor`; the block never vanishes silently). `redactReadinessForRemote(report)` is what the `search_modes` op returns when `ctx.remote !== false`: `required_key`, `key_present` and `fix` are dropped and `self_hosted` is forced false — which env vars exist on the host is fingerprinting data and the fix names them; `ready` stays because it is observable anyway (reranked results carry `rerank_score`). `src/commands/search.ts:formatModesText` renders the runtime `Reranker:` line (`off (resolved) — …` / `<model> (enabled) — <KEY> present` / `<model> (enabled but NOT running) — <fix>`) and a per-bundle `reranker=… topNIn=… autocut=…` line. Pinned by `test/modes-report-reranker.test.ts`. The CLI dashboard leads with command-specific expansion: `query` follows the mode's expansion default (off in all modes) unless the call passes `expand`, while `search` is off. It discloses provider prerequisites/text flow and distinguishes retained bundle configuration from effective command behavior.
- `src/core/search/private-visibility.ts` — shared page-visibility resolver and SQL predicates. Explicit local trust disables page filtering; operator env/config opt-outs keep their semantics; failed config reads enforce privacy. `privatePagesFilterFragment` authorizes concrete page rows and `isPrivatePage` is its row-side twin: explicit `private` hides a page, absent visibility is world for ordinary pages and private for extracted atoms (`type = 'atom'`) and synthesized concepts (`type = 'concept'` with `synthesized_by`), because their origin may be a private page or transcript. The page predicate also follows derived origins: an atom whose `source_slug` page is explicitly private, and a synthesized concept with a private input atom (or an input whose origin is private) over `concept-provenance` edges, are private whatever their own field says, so a later private flip of an origin reaches derived pages before any repair. Chronicle events follow `event.depth` (fail closed). Declared lineage matches `derived_from` slugs with `= ANY(ARRAY(...))`, not `IN (SELECT ... jsonb_array_elements_text(...))`: same rows, but the planner priced the IN form as a 100-row semi-join per page and pushed every read carrying the rule past `jit_above_cost` (`test/e2e/private-visibility-plan-postgres.test.ts`). `privateSnapshotFilterFragment` is the same rule on a row's own frontmatter for `page_versions`. `effectiveVisibility(origin)` (transcript or missing origin private, page origin per the read rule) and `strictestVisibility` are used by both `extract_atoms` paths, concept synthesis and `gbrain repair visibility`. Remote publication rechecks (`persistence/page-visibility.ts`) and the alias-inject recheck in `hybrid.ts` use the same rule. Tests: `test/private-visibility.test.ts`, `test/derived-page-visibility.test.ts` (PGLite and Postgres); link origins and timeline projections use independent ID-based predicates. `privateProvenanceFilterFragment` hides a fact whose provenance page (`s... (line truncated to 2000 chars)
-- `src/core/access/` (GBRA-218) — per-member read access inside one source. `principals.ts` normalizes frontmatter `readable_by` (bare id = `member:<id>`, `group:<g>`, ASCII case fold, space trim; anything malformed is `['!nobody']`); it is the row twin of the SQL `gbrain_access_readers()` (`test/access-principals.test.ts` holds them equal). A BEFORE trigger keeps `pages.page_readers` equal to that function (direct writes to the column are recomputed), and predicates test the array, never the function, which costs ~0.2 ms per row. A page that names readers with no derived value yet (written before the migration) reads as nobody until `gbrain repair access` (`repair/access.ts`). `members.ts` (members, groups, tombstoned ids never reused, bindings), `token-binding.ts` (the verifiers' join; a credential bound to a disabled, tombstoned or missing member is refused with `access_member_disabled`) and `caller.ts` (`remoteCallerAccess`: the identity dispatch puts on the caller-access record; stdio binds through `mcp.stdio_member`; publication re-resolves from the stored principal). In `search/private-visibility.ts` every page fragment is `accessAllowedSql` AND the privacy part: plain fragments use the empty identity (over-hide, never leak), `*ForCaller` fragments the caller's, and `GBRAIN_REMOTE_PRIVATE_PAGES`, `search.remote_private_pages` and `granted_sources` relax only the privacy part (`privacyForCaller`). A derivative of a page that names readers is hidden from every remote caller (`originAllowedSql`, which calls the SQL function `gbrain_access_origin_restricted()` only for atom, question, concept, event or `derived_from` rows, so each read plans one call instead of four subplans). Both parts lead with column tests (`page_readers IS NULL`, `type`) so row estimates follow column statistics; an expression test such as `->'readable_by' IS NULL` estimates almost no rows and sent restricted vector searches into nested-loop sequential scans. The index walk's page probe (`INDEX_WALK_PAGE_COLUMNS`) projects `page_readers`. Config `access.restrictions_present` (set by a trigger, sticky) keeps every pre-A1 path on a brain with no restriction. `check:remote-privacy-reads` keeps the two relaxations read only in `private-visibility.ts`. Tests: `test/member-read-access.test.ts` (remote matrix, both engines), `test/access-*.test.ts`. Folder rules (A2) keep whole folders of a source to named members. `folder-rules.ts` is the grammar (`.gbrain/access.yml`: `rules:` of `id`, `prefix`, `readable_by`), `classifyRulebook` (each change is narrowing or widening; the target installs only the narrowing part) and `matchingRuleIds`. `policy.ts` owns the one applied rulebook for both stores: `access_rules` (source-qualified ids), the append-only `access_policy_revisions` log, `access_pending_coverage` (prefixes that read as nobody while a narrowing restamps, or while a committed file is invalid) and `sources.config.access_store`, persisted at the first rule and changed only by `policy-migrate.ts` (journaled in `access_policy_migrations`). `proposeRevision` installs narrowing at once under a per-source row lock and holds widening for `applyApproved` (`plan_hash`, `preview_changed`); `restampSource` rewrites `pages.folder_rule_ids` in id windows, then clears pending coverage under the lock; re-proposing the same rulebook finishes an interrupted restamp. A trigger stamps `folder_rule_ids` from the SQL function `gbrain_access_folder_rules()` on insert and on slug or source change, never from git on the serve path. `policy-git.ts` is the git store (files committed through the commit door, applied by `syncAccessPolicyAt` from sync preflight and before and after every managed pass (`performManagedSync`: sync, drain, `sources refresh`, git conversion), read with `git show` at the synced commit); `policy-transfer.ts` carries the applied rulebook through `gbrain export` and installs it before any page in `gbrain import`; `policy-notice.ts` is the `access_policy_pending` degraded notice. In `private-visibility.ts`, `folderRulesAllowedSql` sits inside `accessAllowedSql` (`folder_rule_ids IS NULL OR folder_rule_ids <@ <satisfied ids>` plus coverage conjuncts), so no hatch, grant or `--as` relaxes it, and the unrestricted short form tests `folder_rule_ids IS NULL` next to `page_readers IS NULL` (v283 triggers set `access.restrictions_present` on the first rule or pending prefix); `caller.ts`'s `folderAccessFor` fails closed to whole-source coverage. CLI: `src/commands/access-rules.ts`; doctor: `src/commands/doctor/checks/folder-access.ts`. Tests: `test/access-folder-rules.test.ts` (database store, both engines), `test/access-folder-rules-git.test.ts`, `test/access-folder-transfer.test.ts`, `test/access-folder-doctor.test.ts`. Shared agents (B, GBRA-231): `audience.ts` parses the audience a harness asserts (`X-Gbrain-On-Behalf-Of` or request-level `_meta.on_behalf_of`, 1-50 principals, `guest` = the empty identity; the same key inside tool arguments is refused) and appends each assertion to `access-audience-YYYY-Www.jsonl`. Dispatch resolves it on every remote call through `caller.ts` (`resolveAudienceIdentities`: every entry must be in the credential's live `may_act_for`, then name an enabled member or existing group); the caller-access record then carries `audienceIdentities`, and `accessAllowedForAllSql` needs one readers conjunct per identity, folder rules the intersection of each entry's satisfied set (`folderAccessForAll`). Privacy relaxes only for a one-member audience. Writes carry the audience as `WriteAuthority.actingFor` (re-resolved at publication) and agent jobs as `grant.actingFor`. Every remote response carries `_meta.access` (`appliedAccessSummary`), which `whoami` returns as `access.applied`; doctor `access_may_act_for_wildcard` (`checks/shared-agent-access.ts`) warns on `*`. Tests: `test/access-on-behalf-of.test.ts` (both engines), `test/e2e/serve-http-on-behalf-of.test.ts`.
+- `src/core/access/` (GBRA-218) — per-member read access inside one source. `principals.ts` normalizes frontmatter `readable_by` (bare id = `member:<id>`, `group:<g>`, ASCII case fold, space trim; anything malformed is `['!nobody']`); it is the row twin of the SQL `gbrain_access_readers()` (`test/access-principals.test.ts` holds them equal). A BEFORE trigger keeps `pages.page_readers` equal to that function (direct writes to the column are recomputed), and predicates test the array, never the function, which costs ~0.2 ms per row. A page that names readers with no derived value yet (written before the migration) reads as nobody until `gbrain repair access` (`repair/access.ts`). `members.ts` (members, groups, tombstoned ids never reused, bindings), `token-binding.ts` (the verifiers' join; a credential bound to a disabled, tombstoned or missing member is refused with `access_member_disabled`) and `caller.ts` (`remoteCallerAccess`: the identity dispatch puts on the caller-access record; stdio binds through `mcp.stdio_member`; publication re-resolves from the stored principal). In `search/private-visibility.ts` every page fragment is `accessAllowedSql` AND the privacy part: plain fragments use the empty identity (over-hide, never leak), `*ForCaller` fragments the caller's, and `GBRAIN_REMOTE_PRIVATE_PAGES`, `search.remote_private_pages` and `granted_sources` relax only the privacy part (`privacyForCaller`). A derivative of a page that names readers is hidden from every remote caller (`originAllowedSql`, which calls the SQL function `gbrain_access_origin_restricted()` only for atom, question, concept, event or `derived_from` rows, so each read plans one call instead of four subplans). Both parts lead with column tests (`page_readers IS NULL`, `type`) so row estimates follow column statistics; an expression test such as `->'readable_by' IS NULL` estimates almost no rows and sent restricted vector searches into nested-loop sequential scans. The index walk's page probe (`INDEX_WALK_PAGE_COLUMNS`) projects `page_readers`. Config `access.restrictions_present` (set by a trigger, sticky) keeps every pre-A1 path on a brain with no restriction. `check:remote-privacy-reads` keeps the two relaxations read only in `private-visibility.ts`. Tests: `test/member-read-access.test.ts` (remote matrix, both engines), `test/access-*.test.ts`. Folder rules (A2) keep whole folders of a source to named members. `folder-rules.ts` is the grammar (`.gbrain/access.yml`: `rules:` of `id`, `prefix`, `readable_by`), `classifyRulebook` (each change is narrowing or widening; the target installs only the narrowing part) and `matchingRuleIds`. `policy.ts` owns the one applied rulebook for both stores: `access_rules` (source-qualified ids), the append-only `access_policy_revisions` log, `access_pending_coverage` (prefixes that read as nobody while a narrowing restamps, or while a committed file is invalid) and `sources.config.access_store`, persisted at the first rule and changed only by `policy-migrate.ts` (journaled in `access_policy_migrations`). `proposeRevision` installs narrowing at once under a per-source row lock and holds widening for `applyApproved` (`plan_hash`, `preview_changed`); `restampSource` rewrites `pages.folder_rule_ids` in id windows, then clears pending coverage under the lock; re-proposing the same rulebook finishes an interrupted restamp. A trigger stamps `folder_rule_ids` from the SQL function `gbrain_access_folder_rules()` on insert and on slug or source change, never from git on the serve path. `policy-git.ts` is the git store (files committed through the commit door, applied by `syncAccessPolicyAt` from sync preflight and before and after every managed pass (`performManagedSync`: sync, drain, `sources refresh`, git conversion), read with `git show` at the synced commit; a database with no revision yet replays each committed `access.yml` since the `access.approved` baseline, so a rebuild holds a removal a plain commit made); `policy-transfer.ts` carries the applied rulebook through `gbrain export` and installs it before any page in `gbrain import`; `policy-notice.ts` is the `access_policy_pending` degraded notice. In `private-visibility.ts`, `folderRulesAllowedSql` sits inside `accessAllowedSql` (`folder_rule_ids IS NULL OR folder_rule_ids <@ <satisfied ids>` plus coverage conjuncts), so no hatch, grant or `--as` relaxes it, and the unrestricted short form tests `folder_rule_ids IS NULL` next to `page_readers IS NULL` (v283 triggers set `access.restrictions_present` on the first rule or pending prefix); `caller.ts`'s `folderAccessFor` fails closed to whole-source coverage. CLI: `src/commands/access-rules.ts`; doctor: `src/commands/doctor/checks/folder-access.ts`. Tests: `test/access-folder-rules.test.ts` (database store, both engines), `test/access-folder-rules-git.test.ts`, `test/access-folder-transfer.test.ts`, `test/access-folder-doctor.test.ts`. Shared agents (B, GBRA-231): `audience.ts` parses the audience a harness asserts (`X-Gbrain-On-Behalf-Of` or request-level `_meta.on_behalf_of`, 1-50 principals, `guest` = the empty identity; the same key inside tool arguments is refused) and appends each assertion to `access-audience-YYYY-Www.jsonl`. Dispatch resolves it on every remote call through `caller.ts` (`resolveAudienceIdentities`: every entry must be in the credential's live `may_act_for`, then name an enabled member or existing group); the caller-access record then carries `audienceIdentities`, and `accessAllowedForAllSql` needs one readers conjunct per identity, folder rules the intersection of each entry's satisfied set (`folderAccessForAll`). Privacy relaxes only for a one-member audience. Writes carry the audience as `WriteAuthority.actingFor` (re-resolved at publication) and agent jobs as `grant.actingFor`. Every remote response carries `_meta.access` (`appliedAccessSummary`), which `whoami` returns as `access.applied`; doctor `access_may_act_for_wildcard` (`checks/shared-agent-access.ts`) warns on `*`. Tests: `test/access-on-behalf-of.test.ts` (both engines), `test/e2e/serve-http-on-behalf-of.test.ts`.
- `src/core/search/projection-readiness.ts` — one statement probes visible unsealed pages: ready, projection_pending, by_design (protected/archived only, if none pending) or unknown; LIMIT 1 arms use the v161 pending index. Cached per scope by DB generation (page clock, pages filenode, archive flags, no open tx); `GBRAIN_PROJECTION_READINESS_CACHE=0` disables.
diff --git a/src/core/access/policy-git.ts b/src/core/access/policy-git.ts
index e6b1183ea8..13dee929f4 100644
--- a/src/core/access/policy-git.ts
+++ b/src/core/access/policy-git.ts
@@ -13,7 +13,8 @@
* revision for the source yet (a rebuild or a second database, which then
* reaches the same applied rules from git alone) or when it matches the
* database's approved revision. An access.approved changed by a plain commit
- * is reported and ignored, so git write access alone never widens.
+ * is reported and ignored, so git write access alone never widens. A rebuild replays each committed access.yml since that
+ * baseline, so a narrowing a later plain commit removed is applied again with its removal held.
*/
import { spawnSync } from '../spawn.ts';
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
@@ -83,11 +84,13 @@ export async function syncGitPolicy(engine: BrainEngine, sourceId: string, root:
if (store === 'database') return { source_id: sourceId, state: 'skipped', reason: 'database_store' };
const files = await readCommittedPolicy(root, opts.rev ?? 'HEAD');
if (files.yml === null && files.approved === null && store !== 'git') return { source_id: sourceId, state: 'skipped', reason: 'no_policy' };
+ const rev = opts.rev ?? 'HEAD';
+ const rebuild = !(await getRevision(engine, sourceId));
let approvedFile: GitPolicySyncResult['approved_file'] = 'absent';
if (files.approved !== null) {
const approved = parsePolicyYaml(files.approved);
if (!approved.ok) approvedFile = 'invalid';
- else if (!(await getRevision(engine, sourceId))) {
+ else if (rebuild) {
await proposeRevision(engine, sourceId, approved.rules, { origin: 'git-approved', approved: true, store: 'git', proposedBy: 'git:access.approved', onProgress: opts.onProgress });
approvedFile = 'loaded';
} else {
@@ -95,6 +98,7 @@ export async function syncGitPolicy(engine: BrainEngine, sourceId: string, root:
approvedFile = lastApproved?.digest === rulebookDigest(approved.rules) ? 'matches' : 'untrusted';
}
}
+ if (rebuild) await replayPolicyHistory(engine, sourceId, root, rev, approvedFile === 'loaded', opts.onProgress);
const desired = files.yml === null ? { ok: true as const, rules: [] as FolderRule[] } : parsePolicyYaml(files.yml);
if (!desired.ok) {
await markPolicyInvalid(engine, sourceId, desired.prefixes, { problems: desired.problems, commit: files.commit, file: POLICY_FILE });
@@ -106,6 +110,21 @@ export async function syncGitPolicy(engine: BrainEngine, sourceId: string, root:
return { source_id: sourceId, state: proposal.state, proposal, commit: files.commit, approved_file: approvedFile };
}
+/**
+ * A rebuild replays every committed access.yml since the approved baseline (first-parent order), so a narrowing that a later
+ * plain commit removed is applied again and its removal held, as on the database that saw each commit.
+ */
+async function replayPolicyHistory(engine: BrainEngine, sourceId: string, root: string, rev: string, fromApproved: boolean, onProgress?: (p: RestampProgress) => void): Promise<void> {
+ const log = (args: string[]) => gitAsync(root, ['log', '--first-parent', '--format=%H', ...args], { silenceStderr: true }).catch(() => '');
+ const anchor = fromApproved ? await log(['-1', rev, '--', APPROVED_FILE]) : '';
+ const commits = (await log(['--reverse', anchor ? `${anchor}..${rev}` : rev, '--', POLICY_FILE])).split('\n').filter(Boolean);
+ for (const commit of commits.slice(0, -1)) {
+ const yml = await gitAsync(root, ['show', `${commit}:./${POLICY_FILE}`], { silenceStderr: true, raw: true }).catch(() => null);
+ const parsed = yml === null ? { ok: true as const, rules: [] as FolderRule[] } : parsePolicyYaml(yml);
+ if (parsed.ok) await proposeRevision(engine, sourceId, parsed.rules, { origin: 'git', store: 'git', proposedBy: `git:${commit.slice(0, 7)}`, onProgress });
+ }
+}
+
export interface GitPolicyReceipt {
outcome: CommitPushResult['outcome'] | 'committed_not_pushed';
commit: string | null;
diff --git a/src/core/schema-migrations/v283-folder-access-rules.ts b/src/core/schema-migrations/v283-folder-access-rules.ts
index 96515de9a1..e0860e7cc3 100644
--- a/src/core/schema-migrations/v283-folder-access-rules.ts
+++ b/src/core/schema-migrations/v283-folder-access-rules.ts
@@ -24,7 +24,8 @@ import { requireMaintainerPoliciesSql } from '../derived-state-maintainers.ts';
// read as nobody: a narrowing still being stamped ('pending') or a policy file
// that does not validate ('invalid'; prefix '' covers the whole source). The
// read-time lineage test treats an origin under a folder rule or a covered
-// prefix as restricted. Nothing is rewritten on upgrade: with no rule, every
+// prefix as restricted, and follows an origin renamed since the derivative
+// was written through slug_aliases. Nothing is rewritten on upgrade: with no rule, every
// page keeps folder_rule_ids NULL.
// A role that neither owns these tables nor bypasses RLS reads them empty, so
// nothing here may read empty as "no rule": the stamp refuses the write
@@ -127,7 +128,7 @@ CREATE OR REPLACE FUNCTION gbrain_access_origin_restricted(p_id INTEGER, p_sourc
LANGUAGE plpgsql STABLE PARALLEL SAFE SET search_path = pg_catalog, public, pg_temp AS $fn$
BEGIN
IF p_type = 'atom' THEN
- IF EXISTS (SELECT 1 FROM pages o WHERE o.source_id = p_source_id AND o.slug = fm->>'source_slug'
+ IF EXISTS (SELECT 1 FROM pages o WHERE o.source_id = p_source_id AND o.slug IN (fm->>'source_slug', (SELECT a.canonical_slug FROM slug_aliases a WHERE a.source_id = p_source_id AND a.alias_slug = fm->>'source_slug'))
AND (o.frontmatter ? 'readable_by' OR o.folder_rule_ids IS NOT NULL OR gbrain_access_pending_covers(o.source_id, o.slug))) THEN
RETURN true;
END IF;
@@ -135,12 +136,13 @@ BEGIN
IF EXISTS (SELECT 1 FROM links l JOIN pages i ON i.id = l.to_page_id
WHERE l.from_page_id = p_id AND l.link_source = 'concept-provenance' AND l.link_type = 'synthesized_from'
AND (i.frontmatter ? 'readable_by' OR i.folder_rule_ids IS NOT NULL OR gbrain_access_pending_covers(i.source_id, i.slug)
- OR EXISTS (SELECT 1 FROM pages io WHERE io.source_id = i.source_id AND io.slug = i.frontmatter->>'source_slug'
+ OR EXISTS (SELECT 1 FROM pages io WHERE io.source_id = i.source_id
+ AND io.slug IN (i.frontmatter->>'source_slug', (SELECT a.canonical_slug FROM slug_aliases a WHERE a.source_id = i.source_id AND a.alias_slug = i.frontmatter->>'source_slug'))
AND (io.frontmatter ? 'readable_by' OR io.folder_rule_ids IS NOT NULL OR gbrain_access_pending_covers(io.source_id, io.slug))))) THEN
RETURN true;
END IF;
ELSIF p_type = 'event' AND fm->>'captured_via' LIKE 'life-chronicle:%' THEN
- IF EXISTS (SELECT 1 FROM pages c WHERE c.source_id = p_source_id AND c.slug = fm->'event'->>'depth'
+ IF EXISTS (SELECT 1 FROM pages c WHERE c.source_id = p_source_id AND c.slug IN (fm->'event'->>'depth', (SELECT a.canonical_slug FROM slug_aliases a WHERE a.source_id = p_source_id AND a.alias_slug = fm->'event'->>'depth'))
AND (c.frontmatter ? 'readable_by' OR c.folder_rule_ids IS NOT NULL OR gbrain_access_pending_covers(c.source_id, c.slug))) THEN
RETURN true;
END IF;
@@ -148,8 +150,9 @@ BEGIN
IF jsonb_typeof(fm->'derived_from') IN ('array', 'string') THEN
RETURN EXISTS (SELECT 1 FROM pages d WHERE d.source_id = p_source_id
AND (d.frontmatter ? 'readable_by' OR d.folder_rule_ids IS NOT NULL OR gbrain_access_pending_covers(d.source_id, d.slug))
- AND d.slug = ANY(ARRAY(SELECT regexp_replace(declared.slug, '\\.md$', '') FROM jsonb_array_elements_text(
- CASE WHEN jsonb_typeof(fm->'derived_from') = 'array' THEN fm->'derived_from' ELSE jsonb_build_array(fm->'derived_from') END) AS declared(slug))));
+ AND d.slug = ANY(ARRAY(WITH declared(slug) AS (SELECT regexp_replace(x, '\\.md$', '') FROM jsonb_array_elements_text(
+ CASE WHEN jsonb_typeof(fm->'derived_from') = 'array' THEN fm->'derived_from' ELSE jsonb_build_array(fm->'derived_from') END) AS x)
+ SELECT slug FROM declared UNION SELECT a.canonical_slug FROM slug_aliases a JOIN declared ON a.alias_slug = declared.slug WHERE a.source_id = p_source_id)));
END IF;
RETURN false;
END
diff --git a/test/access-folder-rules-git.test.ts b/test/access-folder-rules-git.test.ts
index 5569d548d0..936b5583ba 100644
--- a/test/access-folder-rules-git.test.ts
+++ b/test/access-folder-rules-git.test.ts
@@ -196,11 +196,14 @@ describe('folder access rules, git store', () => {
expect(git(root, 'status', '--porcelain')).toBe('');
}, 60_000);
- test('a rebuilt database reaches the same applied rules from git alone', async () => {
+ test('a rebuilt database reaches the same applied rules from git alone, and holds the removal of a rule the live database never installed', async () => {
const before = (await policyStatus(engine, SRC)).rules;
const fresh = await seedBrain(spares[1]!, root);
await sync(fresh, root, true);
- expect((await policyStatus(fresh, SRC)).rules).toEqual(before);
+ const rebuilt = await policyStatus(fresh, SRC);
+ expect(rebuilt.rules.filter(r => r.id !== 'hr')).toEqual(before);
+ expect({ hr: rebuilt.rules.find(r => r.id === 'hr'), held: rebuilt.held.map(h => `${h.rule}:${h.kind}`) })
+ .toEqual({ hr: { id: 'hr', prefix: 'hr/', readable_by: ['member:alice-example'] }, held: ['hr:removed'] });
expect(await stamps(fresh)).toEqual(await stamps(engine));
}, 120_000);
@@ -306,3 +309,52 @@ describe('a moved prefix on a git source (L4)', () => {
expect((await policyStatus(engine, SRC)).rules.map(r => `${r.id}:${r.prefix}`)).toEqual(['mv:projects/new-example/']);
}, 120_000);
});
+
+
+describe('a rebuild keeps a held removal held (GBRA-218 Step 11)', () => {
+ let root: string;
+ const engines: PGLiteEngine[] = [];
+
+ beforeAll(async () => {
+ for (let i = 0; i < 2; i++) {
+ const e = new PGLiteEngine();
+ await e.connect({ engine: 'pglite' } as never);
+ await e.initSchema();
+ engines.push(e);
+ }
+ root = mkdtempSync(join(tmpdir(), 'gbrain-frgit-rebuild-'));
+ git(root, 'init', '-q', '-b', 'main');
+ git(root, 'config', 'user.email', '[email protected]');
+ git(root, 'config', 'user.name', 't');
+ for (const s of ['notes/open', 'partners/alice-example/plan']) writePage(root, s);
+ writePolicy(root, POLICY([['team', 'partners/', 'alice-example, bob-example']]));
+ git(root, 'add', '-A');
+ git(root, 'commit', '-q', '-m', 'pages and policy');
+ }, 120_000);
+
+ afterAll(async () => {
+ for (const e of engines) await e.disconnect();
+ rmSync(root, { recursive: true, force: true });
+ });
+
+ test('a rule committed and then removed by a plain commit stays applied on a rebuilt database', async () => {
+ const live = await seedBrain(engines[0]!, root);
+ await sync(live, root, true);
+ writePolicy(root, POLICY([['team', 'partners/', 'alice-example, bob-example'], ['alice', 'partners/alice-example/', 'alice-example']]));
+ git(root, 'commit', '-q', '-am', 'narrow');
+ await sync(live, root);
+ writePolicy(root, POLICY([['team', 'partners/', 'alice-example, bob-example']]));
+ git(root, 'commit', '-q', '-am', 'drop alice rule');
+ await sync(live, root);
+ const view = async (e: PGLiteEngine) => {
+ const s = await policyStatus(e, SRC);
+ return { rules: s.rules.map(r => r.id), held: s.held.map(h => `${h.rule}:${h.kind}`) };
+ };
+ expect(await view(live)).toEqual({ rules: ['alice', 'team'], held: ['alice:removed'] });
+ const fresh = await seedBrain(engines[1]!, root);
+ await sync(fresh, root, true);
+ expect(await view(fresh)).toEqual({ rules: ['alice', 'team'], held: ['alice:removed'] });
+ expect((await stamps(fresh))['partners/alice-example/plan']).toEqual([`${SRC}:alice`, `${SRC}:team`]);
+ }, 120_000);
+});
+
diff --git a/test/access-folder-rules.test.ts b/test/access-folder-rules.test.ts
index fee308027e..39694d9538 100644
--- a/test/access-folder-rules.test.ts
+++ b/test/access-folder-rules.test.ts
@@ -295,6 +295,26 @@ for (const kind of testBackends()) {
expect(bob.text).not.toContain('canaryfr');
}, 120_000);
+ test('a page renamed into a ruled folder takes its atoms, chronicle events and derived_from summaries with it', async () => {
+ const derived = (slug: string, canary: string, lines: string[]) => importFromContent(engine, slug,
+ ['---', ...lines, `title: ${slug}`, '---', `# ${slug}`, '', `Folder ledger notes ${canary} for the roadmap.`].join('\n'), { sourceId: SRC });
+ await importFromContent(engine, 'notes/roadmap', pageText('roadmap', 'canaryfrroadmap'), { sourceId: SRC });
+ await derived('atoms/roadmap-claim', 'canaryfrroadatom', ['type: atom', 'visibility: world', 'source_slug: notes/roadmap']);
+ await derived('events/roadmap-day', 'canaryfrroadevent', ['type: event', 'captured_via: life-chronicle:test', 'event:', ' depth: notes/roadmap']);
+ await derived('notes/roadmap-summary', 'canaryfrroadsum', ['type: note', 'derived_from: [notes/roadmap.md]']);
+ const seen = async (who: string) => {
+ const out: string[] = [];
+ for (const c of ['canaryfrroadmap', 'canaryfrroadatom', 'canaryfrroadevent', 'canaryfrroadsum']) {
+ if ((await call(who, 'search', { query: c, match: 'keyword', fields: 'full' })).text.includes(c)) out.push(c);
+ }
+ return out;
+ };
+ expect(await seen('bob')).toEqual(['canaryfrroadmap', 'canaryfrroadatom', 'canaryfrroadevent', 'canaryfrroadsum']);
+ await engine.updateSlug('notes/roadmap', 'partners/alice-example/roadmap', { sourceId: SRC } as never);
+ expect(await seen('bob')).toEqual([]);
+ expect((await call('alice', 'get_page', { slug: 'partners/alice-example/roadmap', source_id: SRC })).text).toContain('canaryfrroadmap');
+ }, 120_000);
+
test('an interrupted narrowing reads as nobody under its prefix (derivatives too) until resume finishes it', async () => {
await importFromContent(engine, 'clients/acme-example/brief', pageText('brief', 'canaryfrbrief'), { sourceId: SRC });
await importFromContent(engine, 'notes/brief-summary', pageText('brief summary', 'canaryfrbriefsum', 'derived_from: clients/acme-example/brief'), { sourceId: SRC });
diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-default.json b/test/fixtures/goldens/catalog/postgres-engine-init-default.json
index cc629078f6..7fe88a8457 100644
--- a/test/fixtures/goldens/catalog/postgres-engine-init-default.json
+++ b/test/fixtures/goldens/catalog/postgres-engine-init-default.json
@@ -574,7 +574,7 @@
"gbrain_access_folder_rules(p_source text, p_slug text)": "RETURNS text[] kind=f lang=sql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=2fcbf1744ede8ffc2ac16972f3d793109543710ecfe13ae9849eced94f347305",
"gbrain_access_mark_restricted()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=e45dc26421664fa3d409fba6c612af88905d2df35923f9592bcb759fc1ea618f",
"gbrain_access_norm_readers(vals text[])": "RETURNS text[] kind=f lang=sql volatility=i secdef=false config=[search_path=pg_catalog, pg_temp] body_sha256=b536c2604461abac62c90c090d127b71bdda8ed23d43ae25790606a8ee64f769",
- "gbrain_access_origin_restricted(p_id integer, p_source_id text, p_type text, fm jsonb)": "RETURNS boolean kind=f lang=plpgsql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=691cabb9e3951b1ac732fceaf826aaaa2e69cab650caaac6df2cbbaf47764ad4",
+ "gbrain_access_origin_restricted(p_id integer, p_source_id text, p_type text, fm jsonb)": "RETURNS boolean kind=f lang=plpgsql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=07a3e920f8833257a163dfe82eda3d6bc0fe8bfb00da8194901a3e184993200f",
"gbrain_access_pending_covers(p_source text, p_slug text)": "RETURNS boolean kind=f lang=sql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=3a5530179ad6f056532e398fb4de58610125625e09140340ff138a2e268772a8",
"gbrain_access_readers(fm jsonb)": "RETURNS text[] kind=f lang=sql volatility=i secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=18348cb56089106aaec6470076ee652d7ed3bfbcb20e950a24abd52a21e7a380",
"gbrain_access_stamp_folder_rules()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=7fb49ce62f4f6153ef0c844a1816f5f5bb7d765b8610ff66fec137283469fc09",
diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json b/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json
index cd8ba88911..f2947527ca 100644
--- a/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json
+++ b/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json
@@ -574,7 +574,7 @@
"gbrain_access_folder_rules(p_source text, p_slug text)": "RETURNS text[] kind=f lang=sql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=2fcbf1744ede8ffc2ac16972f3d793109543710ecfe13ae9849eced94f347305",
"gbrain_access_mark_restricted()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=e45dc26421664fa3d409fba6c612af88905d2df35923f9592bcb759fc1ea618f",
"gbrain_access_norm_readers(vals text[])": "RETURNS text[] kind=f lang=sql volatility=i secdef=false config=[search_path=pg_catalog, pg_temp] body_sha256=b536c2604461abac62c90c090d127b71bdda8ed23d43ae25790606a8ee64f769",
- "gbrain_access_origin_restricted(p_id integer, p_source_id text, p_type text, fm jsonb)": "RETURNS boolean kind=f lang=plpgsql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=691cabb9e3951b1ac732fceaf826aaaa2e69cab650caaac6df2cbbaf47764ad4",
+ "gbrain_access_origin_restricted(p_id integer, p_source_id text, p_type text, fm jsonb)": "RETURNS boolean kind=f lang=plpgsql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=07a3e920f8833257a163dfe82eda3d6bc0fe8bfb00da8194901a3e184993200f",
"gbrain_access_pending_covers(p_source text, p_slug text)": "RETURNS boolean kind=f lang=sql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=3a5530179ad6f056532e398fb4de58610125625e09140340ff138a2e268772a8",
"gbrain_access_readers(fm jsonb)": "RETURNS text[] kind=f lang=sql volatility=i secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=18348cb56089106aaec6470076ee652d7ed3bfbcb20e950a24abd52a21e7a380",
"gbrain_access_stamp_folder_rules()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=7fb49ce62f4f6153ef0c844a1816f5f5bb7d765b8610ff66fec137283469fc09",
diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json b/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json
index 6fccb4f8e5..140c01eb02 100644
--- a/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json
+++ b/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json
@@ -574,7 +574,7 @@
"gbrain_access_folder_rules(p_source text, p_slug text)": "RETURNS text[] kind=f lang=sql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=2fcbf1744ede8ffc2ac16972f3d793109543710ecfe13ae9849eced94f347305",
"gbrain_access_mark_restricted()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=e45dc26421664fa3d409fba6c612af88905d2df35923f9592bcb759fc1ea618f",
"gbrain_access_norm_readers(vals text[])": "RETURNS text[] kind=f lang=sql volatility=i secdef=false config=[search_path=pg_catalog, pg_temp] body_sha256=b536c2604461abac62c90c090d127b71bdda8ed23d43ae25790606a8ee64f769",
- "gbrain_access_origin_restricted(p_id integer, p_source_id text, p_type text, fm jsonb)": "RETURNS boolean kind=f lang=plpgsql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=691cabb9e3951b1ac732fceaf826aaaa2e69cab650caaac6df2cbbaf47764ad4",
+ "gbrain_access_origin_restricted(p_id integer, p_source_id text, p_type text, fm jsonb)": "RETURNS boolean kind=f lang=plpgsql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=07a3e920f8833257a163dfe82eda3d6bc0fe8bfb00da8194901a3e184993200f",
"gbrain_access_pending_covers(p_source text, p_slug text)": "RETURNS boolean kind=f lang=sql volatility=s secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=3a5530179ad6f056532e398fb4de58610125625e09140340ff138a2e268772a8",
"gbrain_access_readers(fm jsonb)": "RETURNS text[] kind=f lang=sql volatility=i secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=18348cb56089106aaec6470076ee652d7ed3bfbcb20e950a24abd52a21e7a380",
"gbrain_access_stamp_folder_rules()": "RETURNS trigger kind=f lang=plpgsql volatility=v secdef=false config=[search_path=pg_catalog, public, pg_temp] body_sha256=7fb49ce62f4f6153ef0c844a1816f5f5bb7d765b8610ff66fec137283469fc09",