Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: a folder-rule rebuild covers a deleted policy file and the approved commit's own rules, reads as nobody until it finishes, and refuses a shallow checkout (GBRA-218)

garrytan54dd6490a288
diff --git a/src/core/access/policy-git.ts b/src/core/access/policy-git.ts
index 13dee929f4..04937ec546 100644
--- a/src/core/access/policy-git.ts
+++ b/src/core/access/policy-git.ts
@@ -27,11 +27,12 @@ import { gitChildEnv } from '../git-env.ts';
import { gitAsync } from '../sync-git.ts';
import { acquireCommitLock, checkoutRoot, commitAndPush, COMMIT_LOCK_ENV, type CommitPushResult } from '../git/commit-push.ts';
import { parsePolicyYaml, rulebookDigest, serializePolicyYaml, type FolderRule, type PolicyProblem } from './folder-rules.ts';
-import { clearPolicyInvalid, getRevision, listRevisions, markPolicyInvalid, proposeRevision, readAccessStore, writeAccessStore, type ProposalResult, type RestampProgress } from './policy.ts';
+import { clearPolicyInvalid, coverageRows, getRevision, listRevisions, markPolicyInvalid, proposeRevision, readAccessStore, writeAccessStore, type ProposalResult, type RestampProgress } from './policy.ts';
export const POLICY_DIR = '.gbrain';
export const POLICY_FILE = '.gbrain/access.yml';
export const APPROVED_FILE = '.gbrain/access.approved';
+const REBUILD_MARK = 'rebuild';
const POLICY_HEADER = 'Folder read rules for this source (gbrain access rule add|rm). Narrowing changes apply at the next sync;\nwidening changes wait for gbrain access apply --source <id> on the brain host.';
const APPROVED_HEADER = 'The folder rules an operator approved with gbrain access apply. Written by gbrain; do not edit.';
@@ -83,9 +84,14 @@ export async function syncGitPolicy(engine: BrainEngine, sourceId: string, root:
const store = await readAccessStore(engine, sourceId);
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));
+ if (files.yml === null && files.approved === null && store !== 'git'
+ && !(await gitAsync(root, ['log', '-1', '--first-parent', '--format=%H', rev, '--', POLICY_FILE], { silenceStderr: true }))) {
+ return { source_id: sourceId, state: 'skipped', reason: 'no_policy' };
+ }
+ const rebuild = !(await getRevision(engine, sourceId))
+ || (await coverageRows(engine, sourceId)).some(c => c.reason === 'invalid' && (c.detail as { file?: string } | null)?.file === REBUILD_MARK);
+ if (rebuild) await markPolicyInvalid(engine, sourceId, null, { problems: [{ line: null, message: 'The folder rules are being rebuilt from git history; the source reads as nobody until the rebuild finishes.' }], commit: files.commit, file: REBUILD_MARK });
let approvedFile: GitPolicySyncResult['approved_file'] = 'absent';
if (files.approved !== null) {
const approved = parsePolicyYaml(files.approved);
@@ -98,7 +104,14 @@ 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);
+ if (rebuild) {
+ const problem = await replayPolicyHistory(engine, sourceId, root, rev, approvedFile === 'loaded', opts.onProgress);
+ if (problem) {
+ const problems = [{ line: null, message: problem }];
+ await markPolicyInvalid(engine, sourceId, null, { problems, commit: files.commit, file: REBUILD_MARK });
+ return { source_id: sourceId, state: 'invalid', problems, commit: files.commit, approved_file: approvedFile };
+ }
+ }
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 });
@@ -111,18 +124,24 @@ export async function syncGitPolicy(engine: BrainEngine, sourceId: string, root:
}
/**
- * 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.
+ * A rebuild replays every committed access.yml from the approved baseline's commit on (first-parent order), so a narrowing
+ * that a later plain commit removed or deleted the file over is applied again and its removal held, as on the database that
+ * saw each commit. The whole source reads as nobody from the start of a rebuild until it finishes (an interrupted one is
+ * resumed by the next sync), and a shallow checkout leaves it so.
*/
-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(() => '');
+async function replayPolicyHistory(engine: BrainEngine, sourceId: string, root: string, rev: string, fromApproved: boolean, onProgress?: (p: RestampProgress) => void): Promise<string | null> {
+ if (await gitAsync(root, ['rev-parse', '--is-shallow-repository'], { silenceStderr: true }) === 'true') {
+ return `The checkout of source ${sourceId} is shallow, so its folder-rule history cannot be replayed; the source reads as nobody. Fetch the full history (git -C ${root} fetch --unshallow) and sync again.`;
+ }
+ const log = (args: string[]) => gitAsync(root, ['log', '--first-parent', '--format=%H', ...args], { silenceStderr: true });
const anchor = fromApproved ? await log(['-1', rev, '--', APPROVED_FILE]) : '';
- const commits = (await log(['--reverse', anchor ? `${anchor}..${rev}` : rev, '--', POLICY_FILE])).split('\n').filter(Boolean);
+ const commits = [...(anchor ? [anchor] : []), ...(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 });
}
+ return null;
}
export interface GitPolicyReceipt {
diff --git a/test/access-folder-rules-git.test.ts b/test/access-folder-rules-git.test.ts
index 936b5583ba..f5b1c95ed4 100644
--- a/test/access-folder-rules-git.test.ts
+++ b/test/access-folder-rules-git.test.ts
@@ -24,7 +24,7 @@ import { join } from 'node:path';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { performSync } from '../src/commands/sync.ts';
import { runAccessRules } from '../src/commands/access-rules.ts';
-import { policyStatus } from '../src/core/access/policy.ts';
+import { policySeams, policyStatus } from '../src/core/access/policy.ts';
import { readCommittedPolicy, syncAccessPolicyAt, commitPolicyFile, POLICY_FILE } from '../src/core/access/policy-git.ts';
import { acquireCommitLock } from '../src/core/git/commit-push.ts';
import { withEnv } from './helpers/with-env.ts';
@@ -358,3 +358,112 @@ describe('a rebuild keeps a held removal held (GBRA-218 Step 11)', () => {
}, 120_000);
});
+
+
+describe('a rebuild keeps a deletion or a pre-approval narrowing held (GBRA-218 fix-check)', () => {
+ const engines: PGLiteEngine[] = [];
+ const roots: string[] = [];
+ 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}`).sort() };
+ };
+ const repo = () => {
+ const root = mkdtempSync(join(tmpdir(), 'gbrain-frgit-replay-'));
+ roots.push(root);
+ 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);
+ return root;
+ };
+
+ beforeAll(async () => {
+ for (let i = 0; i < 8; i++) {
+ const e = new PGLiteEngine();
+ await e.connect({ engine: 'pglite' } as never);
+ await e.initSchema();
+ engines.push(e);
+ }
+ }, 180_000);
+
+ afterAll(async () => {
+ for (const e of engines) await e.disconnect();
+ for (const r of roots) rmSync(r, { recursive: true, force: true });
+ });
+
+ const narrowThenRemove = async (engine: PGLiteEngine) => {
+ const root = repo();
+ writePolicy(root, POLICY([['team', 'partners/', 'alice-example, bob-example']]));
+ git(root, 'add', '-A');
+ git(root, 'commit', '-q', '-m', 'pages and policy');
+ const live = await seedBrain(engine, 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);
+ expect(await view(live)).toEqual({ rules: ['alice', 'team'], held: ['alice:removed'] });
+ return root;
+ };
+
+ test('an interrupted rebuild reads as nobody and the next sync finishes the replay', async () => {
+ const root = await narrowThenRemove(engines[4]!);
+ const fresh = await seedBrain(engines[5]!, root);
+ let installs = 0;
+ policySeams.afterInstall = () => { if (++installs === 1) throw new Error('interrupted rebuild'); };
+ try {
+ await expect(sync(fresh, root, true)).rejects.toThrow('interrupted rebuild');
+ } finally { policySeams.afterInstall = undefined; }
+ expect((await policyStatus(fresh, SRC)).invalid.map(c => c.prefix)).toEqual(['']);
+ await sync(fresh, root, true);
+ const s = await policyStatus(fresh, SRC);
+ expect({ ...(await view(fresh)), invalid: s.invalid }).toEqual({ rules: ['alice', 'team'], held: ['alice:removed'], invalid: [] });
+ }, 120_000);
+
+ test('a shallow checkout cannot be rebuilt: the source reads as nobody until its history is fetched', async () => {
+ const root = await narrowThenRemove(engines[6]!);
+ const shallow = mkdtempSync(join(tmpdir(), 'gbrain-frgit-shallow-'));
+ roots.push(shallow);
+ execFileSync('git', ['clone', '-q', '--depth', '1', `file://${root}`, shallow], { stdio: 'ignore' });
+ const fresh = await seedBrain(engines[7]!, shallow);
+ expect((await syncAccessPolicyAt(fresh, SRC, shallow))?.state).toBe('invalid');
+ expect((await policyStatus(fresh, SRC)).invalid.map(c => c.prefix)).toEqual(['']);
+ }, 120_000);
+
+ test('access.yml deleted by a plain commit keeps its rules held on a rebuilt database', async () => {
+ const root = repo();
+ writePolicy(root, POLICY([['alice', 'partners/alice-example/', 'alice-example']]));
+ git(root, 'add', '-A');
+ git(root, 'commit', '-q', '-m', 'pages and policy');
+ const live = await seedBrain(engines[0]!, root);
+ await sync(live, root, true);
+ git(root, 'rm', '-q', POLICY_FILE);
+ git(root, 'commit', '-q', '-m', 'remove the policy file');
+ await sync(live, root);
+ expect(await view(live)).toEqual({ rules: ['alice'], held: ['alice:removed'] });
+ const fresh = await seedBrain(engines[1]!, root);
+ await sync(fresh, root, true);
+ expect(await view(fresh)).toEqual({ rules: ['alice'], held: ['alice:removed'] });
+ expect((await stamps(fresh))['partners/alice-example/plan']).toEqual([`${SRC}:alice`]);
+ }, 120_000);
+
+ test('a narrowing already in access.yml when an older rulebook was approved stays applied on a rebuilt database', async () => {
+ const root = repo();
+ writePolicy(root, POLICY([['team', 'partners/', 'alice-example, bob-example'], ['alice', 'partners/alice-example/', 'alice-example']]));
+ writePolicy(root, POLICY([['team', 'partners/', 'alice-example, bob-example']]), '.gbrain/access.approved');
+ git(root, 'add', '-A');
+ git(root, 'commit', '-q', '-m', 'pages, policy and an older approved rulebook');
+ const live = await seedBrain(engines[2]!, root);
+ await sync(live, root, true);
+ writePolicy(root, POLICY([['team', 'partners/', 'alice-example, bob-example']]));
+ git(root, 'commit', '-q', '-am', 'drop alice rule');
+ await sync(live, root);
+ expect(await view(live)).toEqual({ rules: ['alice', 'team'], held: ['alice:removed'] });
+ const fresh = await seedBrain(engines[3]!, root);
+ await sync(fresh, root, true);
+ expect(await view(fresh)).toEqual({ rules: ['alice', 'team'], held: ['alice:removed'] });
+ }, 120_000);
+});
+