Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: preserve local work when branch Sync fails (#13368)

Adam Eivy6e3e37d85a57
diff --git a/server/services/git.js b/server/services/git.js
index fc7b2d42e0..8f642c7780 100644
--- a/server/services/git.js
+++ b/server/services/git.js
@@ -1167,7 +1167,7 @@ async function updateDefaultBranchAdmitted(dir) {
* Sync branch - pull then push
*/
export async function syncBranch(dir, branch = null) {
- return withCheckoutMutation(dir, () => syncBranchAdmitted(dir, branch));
+ return withCheckoutMutation(dir, () => syncBranchAdmitted(dir, branch), { exclusive: true });
}
/** Reconcile local default-branch commits with origin without stashing or force-pushing. */
@@ -1230,37 +1230,48 @@ async function rebaseAndPushDefaultBranchAdmitted(dir) {
async function syncBranchAdmitted(dir, branch = null) {
if (branch) assertSafeRef(branch);
+ const refused = error => ({ success: false, pulled: false, pushed: false, error });
+ const gitDir = (await execGit(['rev-parse', '--absolute-git-dir'], dir)).stdout.trim();
+ if ([...SEQUENCER_STATE.map(([marker]) => marker), 'MERGE_HEAD', 'sequencer']
+ .some(marker => existsSync(join(gitDir, marker)))) {
+ return refused('Finish the Git operation already in progress before syncing.');
+ }
const currentBranch = branch || await getBranch(dir);
-
- // First pull with rebase
const pullResult = await execGit(['pull', '--rebase', '--autostash', 'origin', currentBranch], dir, { ignoreExitCode: true });
- const pullSuccess = !pullResult.stderr?.includes('fatal') && !pullResult.stderr?.includes('CONFLICT');
+ if (pullResult.exitCode !== 0 || pullResult.terminated) {
+ // Only a settled command can own recovery. An interrupted Git process may
+ // still have hooks/helpers running; leave its state for explicit recovery.
+ const settled = Number.isInteger(pullResult.exitCode) && !pullResult.terminated;
+ const clearedLock = settled && clearStaleGitLock(pullResult.stderr);
+ const aborted = settled && await rebaseInProgress(dir)
+ ? await execGit(['rebase', '--abort'], dir, { ignoreExitCode: true }) : null;
+ const recovery = aborted && aborted.exitCode !== 0
+ ? ` Rebase abort failed: ${gitResultError(aborted, 'Inspect the checkout before retrying.')}` : '';
+ const error = clearedLock
+ ? `Cleared an abandoned git lock (${clearedLock}) left by a killed git process. The pull did not complete — inspect the checkout before retrying the sync.`
+ : gitResultError(pullResult, 'Pull failed; nothing was pushed.');
+ return refused(`${error}${recovery}`);
+ }
- if (!pullSuccess) {
- // A rebase killed mid-flight can leave the repo in a rebase-in-progress
- // state, so a lock found here is cleared but NOT retried automatically —
- // surface it so the next explicit sync starts from a lock-free repo.
- const clearedLock = clearStaleGitLock(pullResult.stderr);
- return {
- success: false,
- error: clearedLock
- ? `Cleared an abandoned git lock (${clearedLock}) left by a killed git process. The pull did not complete — retry the sync now that the lock is clear.`
- : (pullResult.stderr || 'Pull failed'),
- pulled: false,
- pushed: false
- };
+ // Autostash reapplication can conflict after Git has exited successfully.
+ // Preserve the conflicted files and Git's saved stash rather than aborting
+ // an already-completed rebase or attempting to apply/drop the stash again.
+ const unmerged = await execGit(['diff', '--name-only', '--diff-filter=U'], dir, { ignoreExitCode: true });
+ if (unmerged.exitCode !== 0) {
+ return refused(gitResultError(unmerged, 'Could not verify local changes after pulling; nothing was pushed.'));
+ }
+ if (unmerged.stdout.trim()) {
+ return refused(`Re-applying autostashed local changes conflicted in: ${unmerged.stdout.trim()}. Resolve the conflicts before syncing again; nothing was pushed.`);
}
- // Then push
const pushResult = await execGit(['push', 'origin', currentBranch], dir, { ignoreExitCode: true });
- const pushSuccess = !pushResult.stderr?.includes('rejected') && !pushResult.stderr?.includes('fatal');
-
+ const pushSuccess = pushResult.exitCode === 0 && !pushResult.terminated;
return {
success: pushSuccess,
pulled: true,
pushed: pushSuccess,
- output: pullResult.stdout + pushResult.stdout,
- error: pushSuccess ? null : pushResult.stderr
+ output: gitResultOutput(pullResult) + gitResultOutput(pushResult),
+ error: pushSuccess ? null : gitResultError(pushResult, 'Push failed; local commits remain in the checkout.')
};
}
diff --git a/server/services/git.staleLock.test.js b/server/services/git.staleLock.test.js
index 2290dfba78..896dea5813 100644
--- a/server/services/git.staleLock.test.js
+++ b/server/services/git.staleLock.test.js
@@ -78,6 +78,31 @@ describe('syncBranch', () => {
);
});
+ it.each([
+ fail('error: could not apply example'),
+ { ...fail(), stdout: 'pull hook declined' },
+ { stdout: '', stderr: '', exitCode: null, signal: 'SIGTERM', terminated: true }
+ ])('stops before push for an unsuccessful pull result: %j', async pullResult => {
+ execGitMock.mockImplementation(async args => args[0] === 'pull' ? pullResult : ok());
+ expect(await syncBranch('/repo', 'feature/work')).toMatchObject({ success: false, pulled: false, pushed: false });
+ expect(commands().some(command => /^(push|rebase --abort)/.test(command))).toBe(false);
+ });
+
+ it.each([
+ fail('error: pre-push hook declined'),
+ { ...fail(), stdout: 'push hook declined' },
+ { stdout: '', stderr: '', exitCode: null, signal: 'SIGTERM', terminated: true }
+ ])('reports an unsuccessful push result: %j', async pushResult => {
+ execGitMock.mockImplementation(async args => args[0] === 'push' ? pushResult : ok());
+ expect(await syncBranch('/repo', 'feature/work')).toMatchObject({ success: false, pulled: true, pushed: false, error: expect.any(String) });
+ });
+
+ it('does not push when the post-pull conflict check fails', async () => {
+ execGitMock.mockImplementation(async args => args[0] === 'diff' ? fail('error: index unavailable') : ok());
+ expect(await syncBranch('/repo', 'feature/work')).toMatchObject({ success: false, pulled: false, pushed: false, error: 'error: index unavailable' });
+ expect(commands()).not.toContain('push origin feature/work');
+ });
+
it('clears an abandoned pull lock without retrying, and does not push', async () => {
execGitMock.mockImplementation((args) => {
if (args[0] === 'pull') return Promise.resolve(fail("fatal: Unable to create '/repo/.git/index.lock': File exists"));
diff --git a/server/services/git.syncBranch.test.js b/server/services/git.syncBranch.test.js
new file mode 100644
index 0000000000..16e79abc29
--- /dev/null
+++ b/server/services/git.syncBranch.test.js
@@ -0,0 +1,144 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { mkdir, readFile, writeFile } from 'fs/promises';
+import { existsSync } from 'fs';
+import { join } from 'path';
+
+const observation = vi.hoisted(() => ({ commands: [], afterPull: null, pullOverride: null, abortOverride: null, pullExitCode: null }));
+vi.mock('../lib/execGit.js', async importOriginal => {
+ const actual = await importOriginal();
+ return {
+ ...actual,
+ execGit: async (...args) => {
+ observation.commands.push(args[0]);
+ if (args[0].join(' ') === 'rebase --abort' && observation.abortOverride) return observation.abortOverride;
+ const result = await actual.execGit(...args);
+ if (args[0][0] === 'pull') {
+ observation.pullExitCode = result.exitCode;
+ await observation.afterPull?.();
+ return observation.pullOverride ?? result;
+ }
+ return result;
+ }
+ };
+});
+import { execGit } from '../lib/execGit.js';
+import { makeGitSandbox, destroyGitSandbox, SKIP_HEAVY_INTEGRATION } from '../lib/gitTestRepo.js';
+import { checkout, syncBranch } from './git.js';
+
+// Real Git pins autostash preservation and ownership across the entire Sync.
+describe.skipIf(SKIP_HEAVY_INTEGRATION)('branch Sync', () => {
+ let sandbox;
+ afterEach(async () => {
+ observation.afterPull = null;
+ observation.pullOverride = null;
+ observation.abortOverride = null;
+ observation.pullExitCode = null;
+ observation.commands = [];
+ await destroyGitSandbox(sandbox?.scratch);
+ sandbox = null;
+ });
+ const head = async dir => (await execGit(['rev-parse', 'HEAD'], dir)).stdout.trim();
+ const commitFile = async (dir, file, contents) => {
+ await writeFile(join(dir, file), contents);
+ await execGit(['add', file], dir);
+ await execGit(['commit', '-m', `Update ${file}`], dir);
+ };
+ const prepare = async ({ conflict = false, localCommit = true } = {}) => {
+ sandbox = await makeGitSandbox({ origin: true });
+ const { repo, origin, scratch } = sandbox;
+ const other = join(scratch, 'other');
+ await execGit(['clone', origin, other], scratch);
+ await execGit(['config', 'user.email', '[email protected]'], other);
+ await execGit(['config', 'user.name', 'Example Agent'], other);
+ await execGit(['config', 'commit.gpgsign', 'false'], other);
+ await commitFile(other, conflict ? 'initial.txt' : 'remote.txt', 'remote change\n');
+ await execGit(['push', 'origin', 'main'], other);
+ if (localCommit) await commitFile(repo, conflict ? 'initial.txt' : 'local.txt', 'local change\n');
+ observation.commands = [];
+ return repo;
+ };
+
+ it('publishes reconciled commits and preserves dirty work while refusing concurrent mutations', async () => {
+ const repo = await prepare();
+ await writeFile(join(repo, 'initial.txt'), 'uncommitted work\n');
+ let markPulled;
+ let releasePull;
+ const pulled = new Promise(resolve => { markPulled = resolve; });
+ const proceed = new Promise(resolve => { releasePull = resolve; });
+ observation.afterPull = () => { markPulled(); return proceed; };
+ const syncing = syncBranch(repo, 'main');
+ await pulled;
+ try {
+ await expect(syncBranch(repo, 'main')).rejects.toMatchObject({ code: 'GIT_BUSY' });
+ await expect(checkout(repo, 'feature/work')).rejects.toMatchObject({ code: 'GIT_BUSY' });
+ } finally {
+ releasePull();
+ await syncing;
+ }
+ expect(await syncing).toMatchObject({ success: true, pulled: true, pushed: true });
+ expect(await head(repo)).toBe(await head(sandbox.origin));
+ expect(await readFile(join(repo, 'initial.txt'), 'utf8')).toBe('uncommitted work\n');
+ expect(await readFile(join(repo, 'local.txt'), 'utf8')).toBe('local change\n');
+ expect(await readFile(join(repo, 'remote.txt'), 'utf8')).toBe('remote change\n');
+ observation.afterPull = null;
+ expect(await syncBranch(repo, 'main')).toMatchObject({ success: true });
+ });
+
+ it('aborts its own conflicting rebase and restores autostashed work without pushing', async () => {
+ const repo = await prepare({ conflict: true });
+ await writeFile(join(repo, 'dirty.txt'), 'tracked work\n');
+ await execGit(['add', 'dirty.txt'], repo);
+ const [local, remote] = await Promise.all([head(repo), head(sandbox.origin)]);
+ observation.commands = [];
+ expect(await syncBranch(repo, 'main')).toMatchObject({ success: false, pulled: false, pushed: false, error: expect.stringMatching(/CONFLICT|could not apply/) });
+ expect(observation.commands).toContainEqual(['rebase', '--abort']);
+ expect(observation.commands.some(args => args[0] === 'push')).toBe(false);
+ expect(await head(repo)).toBe(local);
+ expect(await head(sandbox.origin)).toBe(remote);
+ expect(existsSync(join(repo, '.git', 'rebase-merge'))).toBe(false);
+ expect(await readFile(join(repo, 'dirty.txt'), 'utf8')).toBe('tracked work\n');
+ });
+
+ it('preserves zero-exit autostash conflicts and their saved stash without pushing', async () => {
+ const repo = await prepare({ conflict: true, localCommit: false });
+ await writeFile(join(repo, 'initial.txt'), 'uncommitted local change\n');
+ const remote = await head(sandbox.origin);
+ observation.commands = [];
+ expect(await syncBranch(repo, 'main')).toMatchObject({ success: false, pulled: false, pushed: false, error: expect.stringContaining('autostashed local changes conflicted') });
+ expect(observation.pullExitCode).toBe(0);
+ expect(observation.commands.some(args => args[0] === 'push' || args[0] === 'rebase')).toBe(false);
+ expect((await execGit(['diff', '--name-only', '--diff-filter=U'], repo)).stdout.trim()).toBe('initial.txt');
+ expect((await execGit(['stash', 'show', '-p'], repo)).stdout).toContain('uncommitted local change');
+ expect(await readFile(join(repo, 'initial.txt'), 'utf8')).toContain('uncommitted local change');
+ expect(await head(sandbox.origin)).toBe(remote);
+ });
+
+ it.each([
+ { name: 'an interrupted pull', pullOverride: { exitCode: null, signal: 'SIGTERM', terminated: true, stdout: '', stderr: '' }, abortOverride: null, aborts: false, error: /Pull failed/ },
+ { name: 'a failed abort', pullOverride: null, abortOverride: { exitCode: 1, stdout: '', stderr: 'error: abort failed' }, aborts: true, error: /Rebase abort failed: error: abort failed/ }
+ ])('preserves recovery state after $name', async ({ pullOverride, abortOverride, aborts, error }) => {
+ const repo = await prepare({ conflict: true });
+ const remote = await head(sandbox.origin);
+ observation.pullOverride = pullOverride;
+ observation.abortOverride = abortOverride;
+ observation.commands = [];
+ expect(await syncBranch(repo, 'main')).toMatchObject({ success: false, pushed: false, error: expect.stringMatching(error) });
+ expect(observation.commands.some(args => args.join(' ') === 'rebase --abort')).toBe(aborts);
+ expect(observation.commands.some(args => args[0] === 'push')).toBe(false);
+ expect(existsSync(join(repo, '.git', 'rebase-merge'))).toBe(true);
+ expect(await head(sandbox.origin)).toBe(remote);
+ });
+
+ it.each(['rebase-merge', 'rebase-apply', 'MERGE_HEAD', 'CHERRY_PICK_HEAD', 'REVERT_HEAD', 'sequencer'])(
+ 'leaves an existing %s operation untouched', async marker => {
+ sandbox = await makeGitSandbox({ origin: true });
+ const markerPath = join(sandbox.repo, '.git', marker);
+ if (marker.includes('-') || marker === 'sequencer') await mkdir(markerPath);
+ else await writeFile(markerPath, `${await head(sandbox.repo)}\n`);
+ observation.commands = [];
+ expect(await syncBranch(sandbox.repo, 'main')).toMatchObject({ success: false, pulled: false, pushed: false, error: expect.stringContaining('already in progress') });
+ expect(existsSync(markerPath)).toBe(true);
+ expect(observation.commands.some(args => ['pull', 'push', 'rebase'].includes(args[0]))).toBe(false);
+ }
+ );
+});