Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: commit-push refuses an enclosing repository even when an older gbrain left a stale marker in it

garrytan67667e8a92a9
diff --git a/src/commands/git.ts b/src/commands/git.ts
index 1ec2af5d9e..443ff2bbed 100644
--- a/src/commands/git.ts
+++ b/src/commands/git.ts
@@ -22,6 +22,7 @@ import { writeStdoutFinal } from '../core/cli-force-exit.ts';
import { execFileSync } from '../core/spawn.ts';
import { gitChildEnv } from '../core/git-env.ts';
import { GIT_ENV } from '../core/git-transport.ts';
+import { staleEnclosingMarker } from '../core/persistence/filesystem-guard.ts';
import { canonicalFilesystemPath, checkoutGitMetadata, managedRootMarkerFor, registeredManagedRootRecords } from '../core/persistence/root-registry.ts';
const HELP = `Usage: gbrain git commit-push [--path <dir>] -m <msg> [--json] [--] <paths...>
@@ -143,11 +144,12 @@ function parseResolutions(specs: string[]): Map<string, Resolution> {
* gbrain.sourceRoot or gbrain.cliPath entry, or the committed scripts/brain-commit-push.sh (whose own calls pass no --path).
* An explicit --path names its checkout and is not checked here. GST-1623: a registered checkout must contain the resolved
* one, or be a directory of it that its HEAD tracks; a repository that only encloses a registered checkout (a nested clone)
- * is not a source checkout.
+ * is not a source checkout, even with the stale marker an older gbrain wrote in its git dir (#7988).
*/
function assertSourceCheckout(repo: string): void {
const registered = registeredManagedRootRecords();
- if (!registered.length || managedRootMarkerFor(repo)) return;
+ const marker = managedRootMarkerFor(repo);
+ if (!registered.length || (marker && !staleEnclosingMarker({ root: marker.root, evidence: marker.marker }))) return;
const top = canonicalFilesystemPath(repo);
const within = (root: string, path: string) => { const rel = relative(root, path); return !isAbsolute(rel) && rel !== '..' && !rel.startsWith(`..${sep}`); };
const metadata = checkoutGitMetadata(top);
diff --git a/src/core/persistence/filesystem-guard.ts b/src/core/persistence/filesystem-guard.ts
index 0b3b98b3ff..3b670340ef 100644
--- a/src/core/persistence/filesystem-guard.ts
+++ b/src/core/persistence/filesystem-guard.ts
@@ -73,7 +73,7 @@ export function assertManagedFilesystemWrite(path: string): void {
throw error;
}
/** #7988: a Git-metadata marker whose checkout encloses registered roots but tracks none of them. */
-function staleEnclosingMarker(managed: ManagedFilesystemRoot): boolean {
+export function staleEnclosingMarker(managed: ManagedFilesystemRoot): boolean {
if (basename(managed.evidence) !== 'gbrain-managed.json') return false;
const roots = registeredManagedRootRecords().map(record => record.root).filter(root => root !== managed.root && encloses(managed.root, root));
return roots.length > 0 && roots.every(root => checkoutGitMetadata(root) !== dirname(managed.evidence));
diff --git a/test/git-commit-push-not-a-source-8662.test.ts b/test/git-commit-push-not-a-source-8662.test.ts
index f0aae25190..05ce52e064 100644
--- a/test/git-commit-push-not-a-source-8662.test.ts
+++ b/test/git-commit-push-not-a-source-8662.test.ts
@@ -127,6 +127,24 @@ test('GST-1623: a repository that only encloses a registered checkout is refused
expect(JSON.parse(admitted.stdout)).toMatchObject({ outcome: 'committed_local', paths: ['notes/a.md'] });
}, 60_000);
+test('GST-1623: a stale gbrain-managed.json an older gbrain left in the enclosing repository does not admit it', async () => {
+ const outer = otherRepo('stale-marker-code');
+ const home = join(dir, 'stale-marker-home'), brain = join(outer, 'brain');
+ mkdirSync(join(brain, 'people'), { recursive: true });
+ execFileSync('git', ['init', '-q', '-b', 'main', brain]);
+ writeFileSync(join(brain, 'README.md'), 'brain\n');
+ git(brain, 'add', 'README.md'); git(brain, 'commit', '-qm', 'init');
+ withEnv({ GBRAIN_HOME: home }, () => { recordManagedRoots('00000000-0000-4000-8000-000000001625', [{ local_path: brain, source_id: 'source-a' }]); });
+ // The marker gbrain before #7988 wrote into an enclosing repository for a root it does not track (filesystem-guard.ts: stale).
+ writeFileSync(join(outer, '.git', 'gbrain-managed.json'), JSON.stringify({ root: canonicalFilesystemPath(brain) }));
+ const before = git(outer, 'rev-parse', 'HEAD');
+ const refused = await commitPush(outer, home, '--', 'notes/a.md');
+ expect(refused.exitCode).toBe(1);
+ expect((JSON.parse(refused.stdout) as { code: string }).code).toBe('checkout_not_a_source');
+ expect(git(outer, 'rev-parse', 'HEAD')).toBe(before);
+ expect(git(outer, 'diff', '--cached', '--name-only')).toBe('');
+}, 60_000);
+
test('GST-1623: a managed-root registry that cannot be listed is refused with a typed error, not a raw one', async () => {
const other = otherRepo('registry-unreadable');
const home = join(dir, 'registry-unreadable-home');