Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(sources): owner-path guard on a managed worktree
garrytan8a4c34d2132e
diff --git a/test/sources-guard.test.ts b/test/sources-guard.test.tsnew file mode 100644index 0000000000..79581afff7--- /dev/null+++ b/test/sources-guard.test.ts@@ -0,0 +1,119 @@+/**+ * PR B Part 5: `gbrain sources guard` on a managed source's canonical worktree. Owner host only; enforce mode refuses a+ * plain commit while the effect worker, the door and a refresh fast-forward pass; the install is worktree-local, so a+ * linked sibling worktree of the same repository keeps its own hooks. In-memory PGLite and real git repositories.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { createHash } from 'node:crypto';+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';+import { parseSourcesGuardArgs, sourcesGuard } from '../src/commands/sources-guard.ts';+import { renderGuardHook } from '../src/core/git/commit-guard.ts';+import { commitThroughDoor } from '../src/core/persistence/git-door.ts';+import { commitGitTargets } from '../src/core/persistence/effect-git.ts';+import { refreshWorktree } from '../src/core/persistence/worktree-refresh.ts';+import { foreignWritersEntry, type ForeignCommit } from '../src/commands/doctor/checks/foreign-writers.ts';+import type { DoctorContext } from '../src/commands/doctor/context.ts';+import type { Check } from '../src/commands/doctor.ts';+import { withEnv } from './helpers/with-env.ts';+import { git, page, withRefreshFixture } from './helpers/worktree-refresh-fixture.ts';++let engine: PGLiteEngine;+beforeAll(async () => {+ engine = new PGLiteEngine();+ await engine.connect({});+ await engine.initSchema();+}, 120_000);+afterAll(async () => {+ await disposePersistenceConsumer(engine);+ await engine.disconnect();+});++const refused = async (promise: Promise<unknown>) => promise.then(() => null, e => e as { code?: string; message?: string });++test('the opt-in hook is byte-identical in default mode; enforce drops only the rewrite exemptions', () => {+ expect(createHash('sha256').update(renderGuardHook()).digest('hex')).toBe('f5afdf64fd917d42c444c7e5b3f721670b2701a92047fceaa2e8fd2c0dac962d');+ expect(renderGuardHook('default')).toBe(renderGuardHook());+ const enforce = renderGuardHook('enforce');+ expect(enforce).toContain('for _op in MERGE_HEAD; do');+ expect(enforce).not.toContain('CHERRY_PICK_HEAD');+});++test('arguments: one source, one action, --mode only with --install', () => {+ expect(parseSourcesGuardArgs(['notes', '--install', '--mode', 'enforce'])).toEqual({ sourceId: 'notes', action: 'install', mode: 'enforce' });+ expect(parseSourcesGuardArgs(['notes'])).toEqual({ sourceId: 'notes', action: 'status', mode: 'default' });+ expect(() => parseSourcesGuardArgs(['notes', '--install', '--uninstall'])).toThrow();+ expect(() => parseSourcesGuardArgs(['notes', '--status', '--mode', 'enforce'])).toThrow();+ expect(() => parseSourcesGuardArgs(['--install'])).toThrow();+});++test('owner host only; enforce refuses a plain commit; refresh, effect and door commits pass; doctor names the bypass', () => withRefreshFixture(engine, async f => {+ const other = mkdtempSync(join(tmpdir(), 'gbrain-guard-other-home-'));+ try {+ await withEnv({ GBRAIN_HOME: other, HOME: other }, async () => {+ const error = await refused(sourcesGuard(f.engine, { sourceId: f.alpha, action: 'install', mode: 'enforce' }));+ expect(error?.code).toBe('permission_denied');+ });+ } finally { rmSync(other, { recursive: true, force: true }); }++ const installed = await sourcesGuard(f.engine, { sourceId: f.alpha, action: 'install', mode: 'enforce' });+ expect(installed).toMatchObject({ installed: true, mode: 'enforce', changed: true, source_id: f.alpha });+ expect(installed.hooks_path).toBe(join(git(f.root, 'rev-parse', '--absolute-git-dir'), 'gbrain-hooks'));+ expect((await sourcesGuard(f.engine, { sourceId: f.alpha, action: 'install', mode: 'enforce' })).changed).toBe(false);++ writeFileSync(join(f.root, 'alpha', 'stray.md'), page('Stray', 'A plain commit.'));+ git(f.root, 'add', 'alpha/stray.md');+ expect(() => git(f.root, 'commit', '-q', '-m', 'plain commit in the managed worktree')).toThrow(/gbrain commit guard/);+ expect(() => git(f.root, 'commit', '-q', '--no-verify', '-m', 'no-verify')).toThrow(/gbrain commit guard/);+ git(f.root, 'restore', '--staged', 'alpha/stray.md');+ rmSync(join(f.root, 'alpha', 'stray.md'));++ const target = f.push('alpha/upstream.md', page('Upstream', 'From the pushing clone.'));+ const refreshed = await refreshWorktree(f.engine, f.alpha, {});+ expect(refreshed.status).toBe('completed');+ expect(f.head()).toBe(target);++ writeFileSync(join(f.root, 'alpha', 'effect.md'), page('Effect', 'Committed by the effect worker.'));+ expect((await commitGitTargets(join(f.root, 'alpha'), ['effect.md'])).get('effect.md')).toEqual({ git: 'committed' });+ const doorBody = page('Door', 'Committed by the door.');+ writeFileSync(join(f.root, 'alpha', 'door.md'), doorBody);+ expect(await commitThroughDoor(f.root, [{ path: join(f.root, 'alpha', 'door.md'), planned: createHash('sha256').update(doorBody).digest('hex') }])).toBe(f.head());++ await new Promise(r => setTimeout(r, 1100));+ writeFileSync(join(f.root, 'alpha', 'bypass.md'), page('Bypass', 'Hooks turned off.'));+ git(f.root, 'add', 'alpha/bypass.md');+ git(f.root, '-c', 'core.hooksPath=/dev/null', 'commit', '-q', '-m', 'hooks off in the managed worktree');+ const bypass = f.head();+ const [check] = (await foreignWritersEntry.run({ engine: f.engine, progress: { heartbeat() {} } } as unknown as DoctorContext)) as Check[];+ expect(check!.status).toBe('fail');+ expect((check!.details!.foreign as ForeignCommit[]).map(c => [c.sha, c.kind, c.guard_bypassed])).toEqual([[bypass, 'plain', true]]);++ const removed = await sourcesGuard(f.engine, { sourceId: f.alpha, action: 'uninstall', mode: 'default' });+ expect(removed).toMatchObject({ installed: false, changed: true });+}), 120_000);++test('the install is worktree-local: a linked sibling keeps its hooks and commits freely; a foreign hook refuses', () => withRefreshFixture(engine, async f => {+ const sibling = join(f.dir, 'sibling');+ git(f.root, 'worktree', 'add', '-q', '-b', 'side', sibling);+ const installed = await sourcesGuard(f.engine, { sourceId: f.alpha, action: 'install', mode: 'enforce' });+ expect(installed).toMatchObject({ installed: true, worktree_config: true });+ expect(git(sibling, 'rev-parse', '--path-format=absolute', '--git-path', 'hooks')).not.toBe(installed.hooks_path);+ writeFileSync(join(sibling, 'alpha', 'side.md'), page('Side', 'A person commits in their own worktree.'));+ git(sibling, 'add', 'alpha/side.md');+ git(sibling, 'commit', '-q', '-m', 'commit in the sibling worktree');+ writeFileSync(join(f.root, 'alpha', 'stray.md'), page('Stray', 'Refused here.'));+ git(f.root, 'add', 'alpha/stray.md');+ expect(() => git(f.root, 'commit', '-q', '-m', 'refused in the managed worktree')).toThrow(/gbrain commit guard/);+ git(f.root, 'restore', '--staged', 'alpha/stray.md');++ const status = await sourcesGuard(f.engine, { sourceId: f.alpha, action: 'status', mode: 'default' });+ expect(status).toMatchObject({ installed: true, mode: 'enforce', changed: false });+ await sourcesGuard(f.engine, { sourceId: f.alpha, action: 'uninstall', mode: 'default' });+ writeFileSync(join(git(f.root, 'rev-parse', '--path-format=absolute', '--git-path', 'hooks'), 'pre-commit'), '#!/bin/sh\nexit 0\n');+ const error = await refused(sourcesGuard(f.engine, { sourceId: f.alpha, action: 'install', mode: 'enforce' }));+ expect(error?.code).toBe('git_unavailable');+ expect((await sourcesGuard(f.engine, { sourceId: f.alpha, action: 'status', mode: 'default' })).installed).toBe(false);+}), 120_000);