Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sources): a relative clone path never makes sources remove or reclone delete the cwd (#7630)
garrytan816e851fe284
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6df270219a..9ea4026f6c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -52,6 +52,7 @@ gbrain config set sync.repo_path /absolute/path/to/brain # the legacy setting- `src/core/cycle.ts`: a cycle whose directory matches no source refuses its sync phase while `default`'s path is relative.- `src/core/write-through.ts`, `src/core/persistence/*`, `src/core/fence-repair/`, `src/core/minions/submission-authority.ts`, `src/commands/import.ts`, `src/commands/autopilot.ts`: the other readers skip or refuse a relative path; an absolute path written by another OS is not treated as relative by the write-through guard.- `src/commands/sources-set-path.ts`, `src/core/persistence/connector-path.ts`: `--clear` clears a relative path on any source.+- `src/core/sources-ops.ts`: `gbrain sources remove` never deletes, and a re-clone refuses, a gbrain-managed clone whose stored path is relative, so neither can remove the folder you run it from.- `src/commands/doctor/checks/default-source-path.ts`, `src/core/default-source-path-check.ts`: doctor warns on every relative stored path and names the fix.- `src/core/persistence/terminal-classify.ts`, `src/core/persistence/terminal-failures.ts`, `src/commands/persistence-admin.ts`: stalled-write diagnoses by operation, and `--operation` on `gbrain sources writer failures`.- `docs/guides/troubleshooting.md#relative-source-path` and the error-code catalogue.diff --git a/src/core/sources-ops.ts b/src/core/sources-ops.tsindex 2c27c4f644..5dd2863fbb 100644--- a/src/core/sources-ops.ts+++ b/src/core/sources-ops.ts@@ -960,7 +960,11 @@ export async function removeSource(const gManaged = ghCfg.kind === 'google' && ghCfg.g_managed === true;const cloneRoot = gbrainPath('clones');let cloneRemoved = false;- if (+ if (!opts.keepStorage && src.local_path && (remoteUrl || ghManaged || gManaged) && !isAbsolute(src.local_path)) {+ console.error(+ `[gbrain] WARN: clone cleanup skipped for "${opts.id}": its stored local_path "${src.local_path}" is relative, so it would name the current directory. Delete the clone by hand if you still want it gone.`,+ );+ } else if (!opts.keepStorage &&src.local_path &&(remoteUrl || ghManaged || gManaged) && // only auto-clean when gbrain managed the dir@@ -1078,6 +1082,10 @@ export async function recloneIfMissing(}const remoteUrl = getRemoteUrl(src.config);if (!remoteUrl || !src.local_path) return false;+ if (!isAbsolute(src.local_path)) {+ throw new SourceOpError('unmanaged_path',+ `Refusing to re-clone "${id}": its stored local_path "${src.local_path}" is relative, so it would name the current directory. Run gbrain sources set-path ${id} <absolute-directory>, then retry.`);+ }const state = validateRepoState(src.local_path, remoteUrl);if (state === 'healthy') return false;diff --git a/test/sources-ops.test.ts b/test/sources-ops.test.tsindex 93e5c786b6..2a91ef26a7 100644--- a/test/sources-ops.test.ts+++ b/test/sources-ops.test.ts@@ -1310,3 +1310,39 @@ describe('addSource github kind', () => {expect(status.clone_state).toBe('not-applicable');});});++describe('#7630 — a relative clone local_path never names the current directory', () => {+ async function insideRelativeClone(id: string, config: Record<string, unknown>, fn: (dir: string) => Promise<void>): Promise<void> {+ await withEnv2(async () => {+ const dir = join(CLONE_ROOT, `${id}-cwd`);+ mkdirSync(dir, { recursive: true });+ writeFileSync(join(dir, 'KEEP_ME.md'), 'another clone lives here');+ await engine.executeRaw(+ 'INSERT INTO sources (id, name, local_path, config) VALUES ($1, $1, $2, $3::jsonb)',+ [id, '.', JSON.stringify(config)],+ );+ const previous = process.cwd();+ process.chdir(dir);+ try { await fn(dir); } finally { process.chdir(previous); }+ });+ }++ test('removeSource skips clone cleanup and leaves the current directory', async () => {+ await insideRelativeClone('rel-remove', { remote_url: 'https://github.com/example/repo' }, async dir => {+ const result = await removeSource(engine, { id: 'rel-remove', confirmDestructive: true });+ expect(result.clone_removed).toBe(false);+ expect(existsSync(join(dir, 'KEEP_ME.md'))).toBe(true);+ });+ });++ test('recloneIfMissing refuses unmanaged_path and leaves the current directory', async () => {+ await insideRelativeClone('rel-reclone', { remote_url: 'https://github.com/example/repo', managed_clone: true }, async dir => {+ let threw: SourceOpError | null = null;+ try { await recloneIfMissing(engine, 'rel-reclone'); } catch (e) { threw = e as SourceOpError; }+ expect(threw?.code).toBe('unmanaged_path');+ expect(threw?.message).toContain('gbrain sources set-path rel-reclone');+ expect(existsSync(join(dir, 'KEEP_ME.md'))).toBe(true);+ expect(readdirSync(dir)).toEqual(['KEEP_ME.md']);+ });+ });+});