Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

divergenceSafePull: always --no-autostash so a rebase.autoStash setting cannot stash and replay edits under the pull; git-remote.ts launches through core/spawn.ts (#5429)

garrytan2c5cddfab475
diff --git a/scripts/check-windows-hide.ts b/scripts/check-windows-hide.ts
index b65c5285b9..a208f60297 100644
--- a/scripts/check-windows-hide.ts
+++ b/scripts/check-windows-hide.ts
@@ -96,7 +96,6 @@ const NOT_YET_MIGRATED: Record<string, string> = {
'src/core/fence-repair/uncommitted.ts': 'not yet migrated',
'src/core/git-first-commit.ts': 'not yet migrated',
'src/core/git-head.ts': 'not yet migrated',
- 'src/core/git-remote.ts': 'not yet migrated',
'src/core/git-visible-files.ts': 'not yet migrated',
'src/core/google/access.ts': 'not yet migrated',
'src/core/harness/install.ts': 'not yet migrated',
diff --git a/scripts/serial-files.tsv b/scripts/serial-files.tsv
index ec56d0c3a5..ffed55b8ef 100644
--- a/scripts/serial-files.tsv
+++ b/scripts/serial-files.tsv
@@ -390,3 +390,4 @@ test/writeback-nudge.serial.test.ts global-state global-state: patches process s
test/writeback-review-fixes.serial.test.ts global-state global-state: process-wide test seams __resetCheckpointHarvestForTests; patches process stdout/stderr/console
test/hook-push-banner-superseded.serial.test.ts R1 R1: mutates process.env (GBRAIN_HOME, GBRAIN_BACKUP_CHECK)
test/spawn-seam-batch1-4992.serial.test.ts R1 R1: mutates process.env (GBRAIN_HOME, GIT_DIR) and spies on node:child_process
+test/divergence-safe-pull-5429.serial.test.ts R1 R1: mutates process.env (GBRAIN_GIT_ALLOW_FILE_TRANSPORT) and spies on node:child_process
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 4521a3acc7..1e9dde73ae 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -439,6 +439,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText }
{ since: '0.60.169.0', text: '`recall` takes an optional `question` and returns saved facts ranked by relevance to it (facts below the match threshold are left out); calls without it still return the newest facts, every response now says `facts_order`, and a call that passes only `query` carries a `recall_hint` notice. `gbrain recall --question` passes the question instead of reading its value as an entity name. In `query`, the facts of an entity the question names come only from that entity page\'s own source, and the temporal fact reserve (`search.temporal_fact_reserve`, off by default) scores with the same question scorer: whole-word matches, and a long fact\'s word matches count for less.' },
// Fix wave 15 G5 (GBRA-79).
{ since: '0.60.176.0', text: '`gbrain backup create` on a PGLite brain now removes gbrain\'s own IPC socket files (`.gbrain-resolve.sock`, `.gbrain-persistence.sock`) from the data dir before the dump when nothing listens on them, so a `serve` that was killed no longer blocks every later backup; each removal is listed in the archive\'s `omitted`. Any other socket, FIFO or symlink in the data dir is left alone and the backup refuses with `backup_dump_failed` naming it.' },
+ { since: '0.60.176.0', text: 'The pull that `gbrain sources pull`, `sources push`, `sources harden` and the workspace push run before pushing now passes `--no-autostash`: a repository or user `rebase.autoStash=true` no longer stashes and replays edits made while the pull ran. A dirty tree still ends as `skipped_dirty`, the documented contract.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/git-remote.ts b/src/core/git-remote.ts
index 943d59c0e8..7a1d50a853 100644
--- a/src/core/git-remote.ts
+++ b/src/core/git-remote.ts
@@ -15,7 +15,7 @@ import { assertManagedFilesystemWrite } from './persistence/filesystem-guard.ts'
* reachable only via Tailscale, set GBRAIN_ALLOW_PRIVATE_REMOTES=1; loud
* stderr warning at use site is the operator's signal.
*/
-import { execFileSync } from 'child_process';
+import { execFileSync } from './spawn.ts';
import { lstatSync, existsSync, readdirSync } from 'fs';
import { join } from 'path';
import { isInternalUrl } from './url-safety.ts';
@@ -571,7 +571,10 @@ export type PullOutcome =
* Divergence-safe pull: `fetch` + `pull --rebase`, never leaving a mid-rebase.
*
* - Dirty working tree → `skipped_dirty` (NORMAL mid-session state, not an
- * error; never auto-stashes, never touches in-progress edits).
+ * error; never auto-stashes, never touches in-progress edits). The pull
+ * itself carries `--no-autostash` (#5429): a repository or user
+ * `rebase.autoStash=true` would otherwise stash edits made between the
+ * dirty check and the pull and replay them onto the rebased tree.
* - Rebase conflict → `git rebase --abort`, verify no rebase state remains,
* return `conflict_aborted` ("manual attention needed"). Never throws past
* this — the repo is always left clean (possibly un-advanced).
@@ -597,7 +600,7 @@ export function divergenceSafePull(
});
try {
- runGit(repoPath, ssrf, 'pull', [...GIT_SSRF_SUBCOMMAND_FLAGS, '--rebase', 'origin', branch], 'pull', {
+ runGit(repoPath, ssrf, 'pull', [...GIT_SSRF_SUBCOMMAND_FLAGS, '--rebase', '--no-autostash', 'origin', branch], 'pull', {
timeoutMs, env: { ...GIT_ENV_AUTH },
});
} catch (e) {
diff --git a/test/divergence-safe-pull-5429.serial.test.ts b/test/divergence-safe-pull-5429.serial.test.ts
new file mode 100644
index 0000000000..60a70eb52b
--- /dev/null
+++ b/test/divergence-safe-pull-5429.serial.test.ts
@@ -0,0 +1,117 @@
+/**
+ * #5429: `divergenceSafePull`'s documented contract is "never auto-stashes",
+ * but the pull it ran honoured a repository or user `rebase.autoStash=true`,
+ * so an edit made between the dirty check and the pull was stashed and
+ * replayed onto the rebased tree. The pull now carries `--no-autostash` in
+ * every mode.
+ *
+ * Local file transport is enabled via GBRAIN_GIT_ALLOW_FILE_TRANSPORT=1 (the
+ * durable SSRF flags refuse it otherwise); the spy sits on node:child_process,
+ * which the launch seam looks up at call time.
+ */
+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, spyOn, test } from 'bun:test';
+import * as childProcess from 'node:child_process';
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { divergenceSafePull } from '../src/core/git-remote.ts';
+
+const { execFileSync } = childProcess;
+
+function git(cwd: string, ...args: string[]): string {
+ return execFileSync('git', ['-C', cwd, '-c', 'protocol.file.allow=always', ...args], {
+ stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf-8', env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1' },
+ }).trim();
+}
+
+let root: string;
+
+/** A bare origin + a working clone with one commit on `main`. */
+function makePair(): { bare: string; work: string } {
+ const bare = mkdtempSync(join(root, 'origin-')) + '.git';
+ execFileSync('git', ['init', '-q', '--bare', '-b', 'main', bare], { stdio: 'ignore' });
+ const work = secondClone(bare, 'work-');
+ writeFileSync(join(work, 'README.md'), 'init\n');
+ writeFileSync(join(work, 'page.md'), 'v1\n');
+ git(work, 'add', '.');
+ git(work, 'commit', '-qm', 'init');
+ git(work, 'push', '-q', 'origin', 'main');
+ return { bare, work };
+}
+
+function secondClone(bare: string, prefix = 'work2-'): string {
+ const work = mkdtempSync(join(root, prefix));
+ execFileSync('git', ['-c', 'protocol.file.allow=always', 'clone', '-q', bare, work], { stdio: 'ignore' });
+ git(work, 'config', 'user.email', '[email protected]');
+ git(work, 'config', 'user.name', 'tester');
+ return work;
+}
+
+function commitFile(work: string, name: string, body: string, message: string): string {
+ writeFileSync(join(work, name), body);
+ git(work, 'add', name);
+ git(work, 'commit', '-qm', message);
+ return git(work, 'rev-parse', 'HEAD');
+}
+
+beforeAll(() => { process.env.GBRAIN_GIT_ALLOW_FILE_TRANSPORT = '1'; });
+afterAll(() => { delete process.env.GBRAIN_GIT_ALLOW_FILE_TRANSPORT; });
+beforeEach(() => { root = mkdtempSync(join(tmpdir(), 'gd-5429-')); });
+afterEach(() => { rmSync(root, { recursive: true, force: true }); });
+
+describe('--no-autostash (#5429, bug fix)', () => {
+ test('the pull git runs carries --no-autostash', () => {
+ const { bare, work } = makePair();
+ const other = secondClone(bare);
+ commitFile(other, 'remote.txt', 'r\n', 'remote');
+ git(other, 'push', '-q', 'origin', 'main');
+ const pulls: string[][] = [];
+ const spy = spyOn(childProcess, 'execFileSync');
+ try {
+ divergenceSafePull(work, 'main');
+ } finally {
+ for (const call of spy.mock.calls) {
+ const args = call[1] as string[] | undefined;
+ if (call[0] === 'git' && Array.isArray(args) && args.includes('pull')) pulls.push(args);
+ }
+ spy.mockRestore();
+ }
+ expect(pulls).toHaveLength(1);
+ expect(pulls[0]!.slice(pulls[0]!.indexOf('pull'))).toContain('--no-autostash');
+ expect(pulls[0]!.slice(pulls[0]!.indexOf('pull'))).toContain('--rebase');
+ });
+
+ test('git itself: with rebase.autoStash=true the old argv stashes and replays a dirty edit; --no-autostash refuses and leaves the stash list empty', () => {
+ for (const noAutostash of [false, true]) {
+ const { bare, work } = makePair();
+ const other = secondClone(bare);
+ commitFile(other, 'remote.txt', 'r\n', 'remote');
+ git(other, 'push', '-q', 'origin', 'main');
+ git(work, 'config', 'rebase.autoStash', 'true');
+ writeFileSync(join(work, 'README.md'), 'edited between the dirty check and the pull\n');
+ git(work, 'fetch', '-q', 'origin', 'main');
+ let exit = 0;
+ try { git(work, 'pull', '--rebase', ...(noAutostash ? ['--no-autostash'] : []), 'origin', 'main'); }
+ catch (error) { exit = (error as { status: number }).status; }
+ expect(readFileSync(join(work, 'README.md'), 'utf8')).toBe('edited between the dirty check and the pull\n');
+ expect(git(work, 'stash', 'list')).toBe('');
+ if (noAutostash) {
+ expect(exit).not.toBe(0);
+ expect(existsSync(join(work, 'remote.txt'))).toBe(false);
+ } else {
+ // The hole: git stashed the edit, rebased and replayed it, so the pull "succeeded" on a dirty tree.
+ expect(exit).toBe(0);
+ expect(existsSync(join(work, 'remote.txt'))).toBe(true);
+ }
+ }
+ });
+
+ test('a dirty tree with rebase.autoStash=true is skipped_dirty, nothing stashed', () => {
+ const { work } = makePair();
+ git(work, 'config', 'rebase.autoStash', 'true');
+ writeFileSync(join(work, 'README.md'), 'dirty\n');
+ expect(divergenceSafePull(work, 'main')).toEqual({ status: 'skipped_dirty' });
+ expect(git(work, 'stash', 'list')).toBe('');
+ expect(readFileSync(join(work, 'README.md'), 'utf8')).toBe('dirty\n');
+ });
+});