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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: coordinate rebase and push with checkout mutations
Adam Eivyb3c953baf750
diff --git a/server/services/git.js b/server/services/git.jsindex b479455c77..e6e8c52da6 100644--- a/server/services/git.js+++ b/server/services/git.js@@ -1,8 +1,9 @@import { existsSync, realpathSync } from 'fs';import { randomUUID } from 'node:crypto';+import { AsyncLocalStorage } from 'node:async_hooks';import { mkdtemp } from 'fs/promises';import { tmpdir } from 'os';-import { delimiter, join, resolve } from 'path';+import { delimiter, dirname, join, resolve } from 'path';import { safeJSONParse, PATHS, sleep, rmGuarded } from '../lib/fileUtils.js';import { isGitLockError, listWorktrees } from './worktreeManager.js';import { execGit, execGitSafe } from '../lib/execGit.js';@@ -42,6 +43,42 @@ export {extractAgentSummary};+const checkoutMutations = new Map();+const checkoutMutationContext = new AsyncLocalStorage();++// Existing writers register so an exclusive rebase/push cannot overlap them.+// Nested calls retain their own publication lifetime, even if they outlive a parent.+async function withCheckoutMutation(dir, work, { exclusive = false } = {}) {+ let key;+ try { key = realpathSync(dir); }+ catch (error) {+ if (error.code !== 'ENOENT') throw error;+ key = resolve(dir);+ }+ // API paths may name a subdirectory or a symlink into the same checkout.+ for (let root = key; ; root = dirname(root)) {+ if (existsSync(join(root, '.git'))) { key = root; break; }+ if (dirname(root) === root) break;+ }+ const active = checkoutMutations.get(key);+ const parent = checkoutMutationContext.getStore();+ const inherited = parent?.active && parent.key === key && parent.state === active;+ if (!inherited && (active?.exclusive || (exclusive && active))) {+ throw new ServerError('A Git operation is already running for this checkout', { status: 409, code: 'GIT_BUSY' });+ }+ const state = active || { count: 0, exclusive };+ state.count++;+ checkoutMutations.set(key, state);+ const context = { key, state, active: true };+ try {+ const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');+ return await withCheckoutPublication(dir, () => checkoutMutationContext.run(context, work));+ } finally {+ context.active = false;+ if (--state.count === 0) checkoutMutations.delete(key);+ }+}+/*** Get git status for a directory.*@@ -139,8 +176,7 @@ export async function getDiffStats(dir) {* that exact file, and a crafted `*` can't expand to the whole tree.*/export async function stageFiles(dir, files) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => stageFilesAdmitted(dir, files));+ return withCheckoutMutation(dir, () => stageFilesAdmitted(dir, files));}async function stageFilesAdmitted(dir, files) {@@ -153,8 +189,7 @@ async function stageFilesAdmitted(dir, files) {* Unstage files (same literal-pathspec handling as stageFiles).*/export async function unstageFiles(dir, files) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => unstageFilesAdmitted(dir, files));+ return withCheckoutMutation(dir, () => unstageFilesAdmitted(dir, files));}async function unstageFilesAdmitted(dir, files) {@@ -171,8 +206,7 @@ async function unstageFilesAdmitted(dir, files) {* Same `:(literal)` pathspec handling as stageFiles. Omitted = commit the index as-is.*/export async function commit(dir, message, { paths = [] } = {}) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => commitAdmitted(dir, message, { paths }));+ return withCheckoutMutation(dir, () => commitAdmitted(dir, message, { paths }));}// Snapshot only read-only identities. Detached/unborn HEAD cannot verify an@@ -336,8 +370,7 @@ function fetchOriginAdmitted(dir, { prune = false } = {}) {* would swap files on disk and trigger HMR/server restarts).*/export async function updateBranches(dir) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => updateBranchesAdmitted(dir));+ return withCheckoutMutation(dir, () => updateBranchesAdmitted(dir));}async function updateBranchesAdmitted(dir) {@@ -402,8 +435,7 @@ export async function getBranchComparison(dir, baseBranch = 'main', headBranch =* Push to origin*/export async function push(dir, branch = null) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => pushAdmitted(dir, branch));+ return withCheckoutMutation(dir, () => pushAdmitted(dir, branch));}async function pushAdmitted(dir, branch = null) {@@ -418,8 +450,7 @@ async function pushAdmitted(dir, branch = null) {* Never uses --force. Returns per-branch results.*/export async function pushAll(dir) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => pushAllAdmitted(dir));+ return withCheckoutMutation(dir, () => pushAllAdmitted(dir));}async function pushAllAdmitted(dir) {@@ -454,8 +485,7 @@ async function pushAllAdmitted(dir) {* Create and switch to a new branch*/export async function createBranch(dir, branchName) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => createBranchAdmitted(dir, branchName));+ return withCheckoutMutation(dir, () => createBranchAdmitted(dir, branchName));}async function createBranchAdmitted(dir, branchName) {@@ -467,8 +497,7 @@ async function createBranchAdmitted(dir, branchName) {* Switch to an existing branch*/export async function checkout(dir, branchName) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => checkoutAdmitted(dir, branchName));+ return withCheckoutMutation(dir, () => checkoutAdmitted(dir, branchName));}async function checkoutAdmitted(dir, branchName) {@@ -898,8 +927,7 @@ export async function getBranches(dir, { strict = false } = {}) {* Pull changes from remote for current branch*/export async function pull(dir) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => pullAdmitted(dir));+ return withCheckoutMutation(dir, () => pullAdmitted(dir));}async function pullAdmitted(dir) {@@ -1002,8 +1030,7 @@ async function updatePinnedSubmodules(dir) {* @returns {Promise<{success: boolean, branch: string, output: string, conflict: boolean, rebased?: boolean, error?: string}>}*/export async function updateDefaultBranch(dir) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => updateDefaultBranchAdmitted(dir));+ return withCheckoutMutation(dir, () => updateDefaultBranchAdmitted(dir));}async function updateDefaultBranchAdmitted(dir) {@@ -1140,25 +1167,12 @@ async function updateDefaultBranchAdmitted(dir) {* Sync branch - pull then push*/export async function syncBranch(dir, branch = null) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => syncBranchAdmitted(dir, branch));+ return withCheckoutMutation(dir, () => syncBranchAdmitted(dir, branch));}-const rebasePushCheckouts = new Set();-/** Reconcile local default-branch commits with origin without stashing or force-pushing. */export async function rebaseAndPushDefaultBranch(dir) {- const key = realpathSync(dir);- if (rebasePushCheckouts.has(key)) {- throw new ServerError('Rebase & push is already running for this checkout', { status: 409, code: 'GIT_BUSY' });- }- rebasePushCheckouts.add(key);- try {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return await withCheckoutPublication(dir, () => rebaseAndPushDefaultBranchAdmitted(dir));- } finally {- rebasePushCheckouts.delete(key);- }+ return withCheckoutMutation(dir, () => rebaseAndPushDefaultBranchAdmitted(dir), { exclusive: true });}async function rebaseAndPushDefaultBranchAdmitted(dir) {@@ -1178,6 +1192,9 @@ async function rebaseAndPushDefaultBranchAdmitted(dir) {return refused('Commit or stash local changes before rebasing and pushing.');}await fetchOrigin(dir);+ if (await getBranch(dir) !== branch || !(await getStatus(dir)).clean) {+ return refused('The checkout changed while fetching. Check its branch and local changes before retrying.');+ }const remoteRef = `refs/remotes/origin/${branch}`;const counts = await execGit(['rev-list', '--left-right', '--count', `HEAD...${remoteRef}`], dir);if (!(Number(counts.stdout.trim().split(/\s+/)[0]) > 0)) {@@ -1187,13 +1204,16 @@ async function rebaseAndPushDefaultBranchAdmitted(dir) {// pull.ff / upstream configuration and never move dirty work via autostash.const rebased = await execGit(['rebase', '--no-autostash', remoteRef], dir, { ignoreExitCode: true });if (rebased.exitCode !== 0) {- const aborted = await rebaseInProgress(dir)+ const aborted = Number.isInteger(rebased.exitCode) && !rebased.terminated && 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.')}` : '';return refused(`${gitResultError(rebased, 'Rebase failed; nothing was pushed.')}${recovery}`);}- const pushed = await execGit(['push', 'origin', `HEAD:refs/heads/${branch}`], dir, { ignoreExitCode: true });+ if (await getBranch(dir) !== branch) return refused('The checked-out branch changed; nothing was pushed.');+ const verifiedHead = (await execGit(['rev-parse', '--verify', `refs/heads/${branch}^{commit}`], dir)).stdout.trim();+ if (!/^[a-f0-9]{40,64}$/.test(verifiedHead)) return refused('Could not verify the default-branch commit; nothing was pushed.');+ const pushed = await execGit(['push', 'origin', `${verifiedHead}:refs/heads/${branch}`], dir, { ignoreExitCode: true });return {success: pushed.exitCode === 0,branch,@@ -1272,8 +1292,7 @@ export async function ensureLatest(dir) {if (gitCheck.exitCode !== 0 || gitCheck.stdout.trim() !== 'true') {throw new Error(gitCheck.stderr?.trim() || 'Could not verify Git working tree');}- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => ensureLatestAdmitted(dir));+ return withCheckoutMutation(dir, () => ensureLatestAdmitted(dir));}function ensureLatestAdmitted(dir) {@@ -1551,8 +1570,7 @@ export async function getWorktreeBranches(dir) {* @param {Set<string>} options.excludeBranches - Additional branches to protect (e.g., active agent branches)*/export async function deleteBranch(dir, branchName, options = {}) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => deleteBranchAdmitted(dir, branchName, options));+ return withCheckoutMutation(dir, () => deleteBranchAdmitted(dir, branchName, options));}async function deleteBranchAdmitted(dir, branchName, { local = false, remote = false, excludeBranches } = {}) {@@ -1613,8 +1631,7 @@ async function deleteBranchAdmitted(dir, branchName, { local = false, remote = f* @returns {Promise<{success: boolean, output: string}>}*/export async function mergeBranch(dir, branchName) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => mergeBranchAdmitted(dir, branchName));+ return withCheckoutMutation(dir, () => mergeBranchAdmitted(dir, branchName));}async function mergeBranchAdmitted(dir, branchName) {@@ -1737,8 +1754,7 @@ export const isBranchMergedInto = hasBranchMergeEvidence;* @returns {Promise<{success: boolean, branch: string}>}*/export async function checkoutRemoteBranch(dir, branchName) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => checkoutRemoteBranchAdmitted(dir, branchName));+ return withCheckoutMutation(dir, () => checkoutRemoteBranchAdmitted(dir, branchName));}async function checkoutRemoteBranchAdmitted(dir, branchName) {@@ -1864,8 +1880,7 @@ export async function findActiveAgentInWorkspace(dir, { includePaused = false, f* fetched: boolean}>}*/export async function resetToDefaultBranch(dir) {- const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');- return withCheckoutPublication(dir, () => resetToDefaultBranchAdmitted(dir));+ return withCheckoutMutation(dir, () => resetToDefaultBranchAdmitted(dir));}async function resetToDefaultBranchAdmitted(dir) {diff --git a/server/services/git.rebasePushConcurrency.test.js b/server/services/git.rebasePushConcurrency.test.jsnew file mode 100644index 0000000000..9c8b4cb5bc--- /dev/null+++ b/server/services/git.rebasePushConcurrency.test.js@@ -0,0 +1,69 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';+import { tmpdir } from 'os';++const execGitMock = vi.hoisted(() => vi.fn());+vi.mock('../lib/execGit.js', () => ({ execGit: execGitMock, execGitSafe: execGitMock }));+vi.mock('../lib/checkoutRetirementAdmission.js', () => ({ withCheckoutPublication: (_dir, work) => work() }));++import { checkout, rebaseAndPushDefaultBranch, resetToDefaultBranch, syncBranch } from './git.js';++const SHA = 'a'.repeat(40);+const ok = stdout => ({ stdout: stdout || '', stderr: '', exitCode: 0 });++beforeEach(() => vi.resetAllMocks());++describe('rebase and push checkout ownership', () => {+ it('refuses to start while a checkout mutation is already running', async () => {+ let finish;+ const pending = new Promise(resolve => { finish = resolve; });+ execGitMock.mockImplementation(() => pending);+ const switching = checkout(tmpdir(), 'feature/work');+ try {+ await expect(rebaseAndPushDefaultBranch(tmpdir())).rejects.toMatchObject({ code: 'GIT_BUSY' });+ } finally { finish(ok()); }+ await switching;+ });++ it('refuses checkout, reset and sync during its fetch and pushes the verified default-branch SHA', async () => {+ let releaseFetch;+ let markFetching;+ const fetching = new Promise(resolve => { markFetching = resolve; });+ const fetchResult = new Promise(resolve => { releaseFetch = resolve; });+ execGitMock.mockImplementation(async (args) => {+ const command = args.join(' ');+ if (command === 'fetch origin') { markFetching(); return fetchResult; }+ if (command === 'rev-parse --absolute-git-dir') return ok(`${tmpdir()}/missing-git-fixture`);+ if (command === 'rev-parse --abbrev-ref HEAD') return ok('main');+ if (command === 'symbolic-ref --short refs/remotes/origin/HEAD') return ok('origin/main');+ if (command.startsWith('rev-parse --verify')) return ok(SHA);+ if (args[0] === 'rev-list') return ok('1\t2');+ return ok();+ });+ const result = rebaseAndPushDefaultBranch(tmpdir());+ await fetching;+ try {+ await expect(checkout(tmpdir(), 'feature/work')).rejects.toMatchObject({ code: 'GIT_BUSY' });+ await expect(resetToDefaultBranch(tmpdir())).rejects.toMatchObject({ code: 'GIT_BUSY' });+ await expect(syncBranch(tmpdir())).rejects.toMatchObject({ code: 'GIT_BUSY' });+ } finally { releaseFetch(ok()); }+ expect(await result).toMatchObject({ success: true });+ expect(execGitMock.mock.calls.map(([args]) => args)).toContainEqual(['push', 'origin', `${SHA}:refs/heads/main`]);+ expect(execGitMock.mock.calls.some(([args]) => args[0] === 'checkout')).toBe(false);+ await expect(checkout(tmpdir(), 'feature/work')).resolves.toMatchObject({ success: true });+ });++ it('stops if an external checkout changes the branch during fetch', async () => {+ let branch = 'main';+ execGitMock.mockImplementation(async (args) => {+ const command = args.join(' ');+ if (command === 'fetch origin') branch = 'feature/work';+ if (command === 'rev-parse --absolute-git-dir') return ok(`${tmpdir()}/missing-git-fixture`);+ if (command === 'rev-parse --abbrev-ref HEAD') return ok(branch);+ if (command === 'symbolic-ref --short refs/remotes/origin/HEAD') return ok('origin/main');+ if (command.startsWith('rev-parse --verify')) return ok(SHA);+ return ok();+ });+ expect(await rebaseAndPushDefaultBranch(tmpdir())).toMatchObject({ success: false, pushed: false, error: expect.stringContaining('checkout changed') });+ expect(execGitMock.mock.calls.some(([args]) => ['rebase', 'push'].includes(args[0]))).toBe(false);+ });+});