Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes
garrytanc0985fa68f62
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 97494369b9..53c2ca2556 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -19,7 +19,7 @@ identifiers and attribution are available in the pre-removal Git revision- `src/commands/serve-http-webhooks.ts`, `src/core/minions/handlers/ingest-capture.ts`: `POST /ingest` stores the caller's token or OAuth client on the job. The route answers `404 page_not_found` for an `X-Gbrain-Slug` the caller cannot read and `403 permission_denied` for a member fact page or the scope page, and queues nothing. The job checks the same writer again right before it writes, so a page restricted after the 202 is still refused, and a queued webhook job with no stored writer is checked as a guest. Fresh slugs and pages the caller can read import as before.- `src/core/ops/image.ts`, `src/core/search/image-loader.ts`: `search_by_image` accepts only an http(s) `image_url` from any caller that is not the trusted local CLI, and refuses `image_path` unless the context says the caller is local. The loader reads local paths only when its caller opts in, and checks for a regular file and the size cap before it reads.- `src/commands/serve-http-webhooks.ts`: a push event is verified against the source's secret before anything about the repository is answered. An unknown repository, a source with no secret and a bad signature all get the same `401 signature_mismatch`; the missing-secret case is logged on the server with the command that sets it.-- `src/core/git-remote.ts`, `src/core/sources-ops.ts`, `src/core/ops/sources.ts`, `src/commands/sync/preflight.ts`: a URL source a remote caller adds is cloned, re-cloned and pulled with git-lfs downloads off (large files stay pointer files), and the source records `added_by_remote_caller: true` so every later pull keeps it off. A remote caller whose clone fails gets a fixed message; git's own error goes to the brain host's log. Sources added from the local CLI keep git-lfs.+- `src/core/git-remote.ts`, `src/core/sources-ops.ts`, `src/core/ops/sources.ts`: a URL source a remote caller adds is cloned with git-lfs downloads off, written into the clone's own git config (`lfs.fetchexclude=*`, which outranks the repository's `.lfsconfig`), so every later pull, managed refresh and commit-push in that checkout keeps large files as pointer files. The source records `added_by_remote_caller: true` so a re-clone keeps it off. A remote caller whose clone fails gets a fixed message; git's own error goes to the brain host's log. Sources added from the local CLI keep git-lfs.- Tests: `test/serve-http-ingest-member-access.test.ts` (real token verifier and Express route, two members), `test/search-by-image-op.test.ts`, `test/cross-modal-phase2.test.ts`, `test/serve-http-github-push-webhook.test.ts` and `test/sources-remote-lfs.test.ts` (real git-lfs against a local LFS endpoint).## [0.60.600.0] - 2026-11-07diff --git a/src/commands/sync/preflight.ts b/src/commands/sync/preflight.tsindex 87fc6d079c..2010214c00 100644--- a/src/commands/sync/preflight.ts+++ b/src/commands/sync/preflight.ts@@ -630,9 +630,6 @@ async function pullAndResolveHead(// stop). Both stop the pull: a `git pull` still running after this// process exits could fast-forward the tree behind a released lock.const { pullRepo, isStoppedGitPull } = await import('../../core/git-remote.ts');- const { lfsDownloadsOff } = await import('../../core/sources-ops.ts');- const skipLfs = opts.sourceId ? lfsDownloadsOff((await engine.executeRaw<{ config: unknown }>(- 'SELECT config FROM sources WHERE id = $1', [opts.sourceId]))[0]?.config) : false;const shutdown = abortOnCleanupPass('sync-git-pull');if (opts.sourceId) await recordUpstreamBeforeFetch(engine, opts.sourceId, gitContextRoot);try {@@ -640,7 +637,7 @@ async function pullAndResolveHead(// --timeout through opts.signal, and by pullRepo's own 300s default// when no --timeout is set. Pull applies to the whole git repo// (gitContextRoot), not just the sync scope — git has no per-subdir pull.- await pullRepo(gitContextRoot, { signal: composeAbortSignals(opts.signal, shutdown.signal), skipLfs });+ await pullRepo(gitContextRoot, { signal: composeAbortSignals(opts.signal, shutdown.signal) });serr(`[gbrain phase] sync.git_pull done ${Date.now() - _t0}ms`);if (opts.sourceId) await settleUpstreamAfterPull(engine, opts.sourceId, gitContextRoot, true);} catch (e: unknown) {diff --git a/src/core/git-remote.ts b/src/core/git-remote.tsindex 42a5323dfa..ac90bbcc8a 100644--- a/src/core/git-remote.ts+++ b/src/core/git-remote.ts@@ -97,13 +97,13 @@ export interface CloneOpts {depth?: number; // default 1; 0 means full clonebranch?: string;timeoutMs?: number; // default 600_000 (10 min)- /** Keep git-lfs from downloading: the repository's own .lfsconfig picks the LFS endpoint, outside the URL gate. */+ /**+ * Keep git-lfs from downloading, in this clone and every later git command in it: the repository's own .lfsconfig+ * picks the LFS endpoint, outside the URL gate. Written to the clone's .git/config, which outranks .lfsconfig.+ */skipLfs?: boolean;}-/** git-lfs leaves pointer files in place and contacts no LFS endpoint. */-const LFS_DOWNLOADS_OFF = { GIT_LFS_SKIP_SMUDGE: '1' } as const;-export class GitOperationError extends Error {constructor(public op: 'clone' | 'pull' | 'fetch' | 'remote_get_url',@@ -168,13 +168,14 @@ export function cloneRepo(url: string, destDir: string, opts: CloneOpts = {}): vif (opts.branch) {args.push('--branch', opts.branch);}+ if (opts.skipLfs) args.push('--config', 'lfs.fetchexclude=*');args.push(url, destDir);try {execFileSync('git', args, {stdio: ['ignore', 'pipe', 'pipe'],timeout: opts.timeoutMs ?? 600_000,- env: gitChildEnv({ ...GIT_ENV, ...(opts.skipLfs ? LFS_DOWNLOADS_OFF : {}) }),+ env: gitChildEnv({ ...GIT_ENV }),});} catch (e) {throw new GitOperationError(@@ -198,7 +199,7 @@ export function cloneRepo(url: string, destDir: string, opts: CloneOpts = {}): v* `opts.timeoutMs` (default 5 min); `isStoppedGitPull` recognizes the error* that produces, so callers can tell "we stopped it" from "git failed".*/-export async function pullRepo(repoPath: string, opts: { timeoutMs?: number; signal?: AbortSignal; skipLfs?: boolean } = {}): Promise<void> {+export async function pullRepo(repoPath: string, opts: { timeoutMs?: number; signal?: AbortSignal } = {}): Promise<void> {assertManagedFilesystemWrite(repoPath);const fail = (reason: string, error: unknown): never => {throw new GitOperationError('pull', `git pull failed in ${repoPath}: ${reason}`, error);@@ -222,7 +223,7 @@ export async function pullRepo(repoPath: string, opts: { timeoutMs?: number; sigtry {const merge = await execFileBounded('git', ['-C', repoPath, '-c', 'core.hooksPath=/dev/null', '-c', 'merge.autoStash=false', 'merge', '--ff-only', '--quiet', '@{upstream}'], {timeout: opts.timeoutMs ?? 300_000,- env: gitChildEnv({ ...GIT_ENV, ...(opts.skipLfs ? LFS_DOWNLOADS_OFF : {}) }),+ env: gitChildEnv({ ...GIT_ENV }),signal: opts.signal,});if (merge.error) fail(merge.stderr.trim() || merge.error.message, merge.error);diff --git a/test/sources-remote-lfs.test.ts b/test/sources-remote-lfs.test.tsindex 66247a2971..8ad8a8aa50 100644--- a/test/sources-remote-lfs.test.ts+++ b/test/sources-remote-lfs.test.ts@@ -4,7 +4,7 @@* A repository's committed .lfsconfig names the LFS endpoint, and git-lfs contacts it outside the remote-URL gate.* On a host with git-lfs installed globally, a remote caller's sources_add could make the brain host send requests to* any HTTP endpoint the repository named, on the clone and on every later pull. Protects:- * - cloneRepo / pullRepo with skipLfs leave pointer files and contact no LFS endpoint (real git and git-lfs);+ * - a skipLfs clone, and every later pull or merge in it, leave pointer files and contact no LFS endpoint (real git and git-lfs);* - sources_add from a remote caller clones that way, records it on the source, and a re-clone follows the record;* - sources_add from the trusted local CLI keeps LFS downloads;* - a remote caller's failed clone gets a fixed message instead of git's own error.@@ -17,7 +17,7 @@ import { join } from 'node:path';import { cloneRepo, pullRepo } from '../src/core/git-remote.ts';import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { operationsByName } from '../src/core/operations.ts';-import { recloneIfMissing } from '../src/core/sources-ops.ts';+import { lfsDownloadsOff, recloneIfMissing } from '../src/core/sources-ops.ts';import { withEnv } from './helpers/with-env.ts';const hasLfs = spawnSync('git', ['lfs', 'version']).status === 0 && process.platform !== 'win32';@@ -80,7 +80,7 @@ console.log(git.port + ' ' + lfs.port);writeFileSync(authorConfig, '[user]\n\tname = example\n\temail = [email protected]\n');mkdirSync(work);git(work, 'init', '-q', '-b', 'main', '.');- writeFileSync(join(work, '.lfsconfig'), `[lfs]\n\turl = http://127.0.0.1:${lfsPort}/internal-lfs\n`);+ writeFileSync(join(work, '.lfsconfig'), `[lfs]\n\turl = http://127.0.0.1:${lfsPort}/internal-lfs\n\tfetchinclude = *\n\tfetchexclude =\n`);writeFileSync(join(work, 'x.bin'), pointer);writeFileSync(join(work, 'note.md'), '# note\n');git(work, 'add', '-A');@@ -101,7 +101,7 @@ console.log(git.port + ' ' + lfs.port);expect(hits().length).toBeGreaterThan(0);}, 90_000);- test('a skipLfs clone and a skipLfs pull leave pointers and contact no LFS endpoint', async () => {+ test('a skipLfs clone, a later pull and a plain git merge in it leave pointers and contact no LFS endpoint', async () => {const dest = join(ROOT, 'skipped');await withEnv(env(), async () => { cloneRepo(url, dest, { timeoutMs: 60_000, depth: 0, skipLfs: true }); });expect(readFileSync(join(dest, 'x.bin'), 'utf8')).toBe(pointer);@@ -110,8 +110,18 @@ console.log(git.port + ' ' + lfs.port);git(work, 'commit', '-q', '-m', 'second pointer');git(work, 'push', '-q', bare, 'main');git(bare, 'update-server-info');- await withEnv(env(), async () => { await pullRepo(dest, { timeoutMs: 60_000, skipLfs: true }); });+ await withEnv(env(), async () => { await pullRepo(dest, { timeoutMs: 60_000 }); });expect(readFileSync(join(dest, 'y.bin'), 'utf8')).toContain('size 6');+ // Managed refresh and commit-push move the same checkout with their own plain git calls.+ writeFileSync(join(work, 'z.bin'), pointer.replace('size 5', 'size 7'));+ git(work, 'add', 'z.bin');+ git(work, 'commit', '-q', '-m', 'third pointer');+ git(work, 'push', '-q', bare, 'main');+ git(bare, 'update-server-info');+ const plain = { ...process.env, ...env() };+ execFileSync('git', ['-C', dest, 'fetch', '-q', 'origin'], { env: plain, stdio: 'pipe' });+ execFileSync('git', ['-C', dest, 'merge', '--ff-only', '-q', 'origin/main'], { env: plain, stdio: 'pipe' });+ expect(readFileSync(join(dest, 'z.bin'), 'utf8')).toContain('size 7');expect(hits()).toEqual([]);}, 90_000);});@@ -129,7 +139,7 @@ describe('sources_add: a remote caller\'s URL source clones without LFS downloadwriteFileSync(recorder, `import { appendFileSync, mkdirSync, readFileSync } from 'node:fs';const args = process.argv.slice(2);-appendFileSync(${JSON.stringify(log)}, JSON.stringify({ args, skip: process.env.GIT_LFS_SKIP_SMUDGE ?? null }) + '\\n');+appendFileSync(${JSON.stringify(log)}, JSON.stringify({ args }) + '\\n');if (readFileSync(${JSON.stringify(mode)}, 'utf8') === 'fail') { console.error('fatal: unable to access internal-detail-example: 502'); process.exit(128); }if (args.includes('clone')) mkdirSync(args[args.length - 1]!, { recursive: true });`);@@ -144,7 +154,8 @@ if (args.includes('clone')) mkdirSync(args[args.length - 1]!, { recursive: truebeforeEach(() => { writeFileSync(log, ''); writeFileSync(mode, 'ok'); });const clones = () => readFileSync(log, 'utf8').trim().split('\n').filter(Boolean)- .map(line => JSON.parse(line) as { args: string[]; skip: string | null }).filter(call => call.args.includes('clone'));+ .map(line => JSON.parse(line) as { args: string[] }).filter(call => call.args.includes('clone'))+ .map(call => call.args.includes('lfs.fetchexclude=*'));const add = (remote: boolean, id: string) => withEnv({ PATH: `${fakeDir}:${process.env.PATH}`, GBRAIN_HOME: home }, () =>operationsByName.sources_add!.handler({ engine, remote, auth: remote ? { token: 't', clientId: 'c-admin', scopes: ['sources_admin'] } : undefined } as never,{ id, url: `https://example.com/acme-example/${id}.git` }));@@ -155,7 +166,7 @@ if (args.includes('clone')) mkdirSync(args[args.length - 1]!, { recursive: truetest('a remote caller\'s clone runs with LFS downloads off, and the source records it', async () => {await add(true, 'remote-added');- expect(clones().map(call => call.skip)).toEqual(['1']);+ expect(clones()).toEqual([true]);expect((await config('remote-added')).added_by_remote_caller).toBe(true);});@@ -163,12 +174,12 @@ if (args.includes('clone')) mkdirSync(args[args.length - 1]!, { recursive: trueconst [row] = await engine.executeRaw<{ local_path: string }>("SELECT local_path FROM sources WHERE id = 'remote-added'");rmSync(row!.local_path, { recursive: true, force: true });await withEnv({ PATH: `${fakeDir}:${process.env.PATH}`, GBRAIN_HOME: home }, () => recloneIfMissing(engine, 'remote-added'));- expect(clones().map(call => call.skip)).toEqual(['1']);+ expect(clones()).toEqual([true]);});test('the trusted local CLI keeps LFS downloads', async () => {await add(false, 'local-added');- expect(clones().map(call => call.skip)).toEqual([null]);+ expect(clones()).toEqual([false]);expect((await config('local-added')).added_by_remote_caller).toBeUndefined();});@@ -181,3 +192,11 @@ if (args.includes('clone')) mkdirSync(args[args.length - 1]!, { recursive: trueexpect((local as Error).message).toContain('internal-detail-example');});});++test('lfsDownloadsOff reads the record from an object or a JSON string and never throws', () => {+ expect(lfsDownloadsOff({ added_by_remote_caller: true })).toBe(true);+ expect(lfsDownloadsOff('{"added_by_remote_caller":true}')).toBe(true);+ expect(lfsDownloadsOff('{not json')).toBe(false);+ expect(lfsDownloadsOff(null)).toBe(false);+ expect(lfsDownloadsOff({ added_by_remote_caller: 'true' })).toBe(false);+});