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: retry stale quality snapshot leases once (#8255)
[._.]/ Adam Eivy4bdf82d0df2b
diff --git a/server/services/appQualitySnapshotFile.js b/server/services/appQualitySnapshotFile.jsindex 081c96a84c..df5cf756b6 100644--- a/server/services/appQualitySnapshotFile.js+++ b/server/services/appQualitySnapshotFile.js@@ -265,10 +265,36 @@ async function landOrReuse(app, git, defaultBranch, deps, next, count, options =if (reused.published) lastPublishedBody.set(app.repoPath, next);return reused;}- const result = await landSnapshotPr(app, {- body: next, count, defaultBranch, git, deps,- message: options.message, removeLegacy: options.removeLegacy === true,- });+ let result;+ try {+ result = await landSnapshotPr(app, {+ body: next, count, defaultBranch, git, deps,+ message: options.message, removeLegacy: options.removeLegacy === true,+ });+ } catch (error) {+ // Another install can advance the shared publish branch after our ref read.+ // The lease must reject that push; rebuild from fresh refs exactly once.+ if (options.leaseRetry || error?.qualitySnapshotStaleLease !== true) throw error;+ await git.fetchOrigin(app.repoPath, { prune: true });+ const freshDefaultBranch = await git.getDefaultBranch(app.repoPath);+ if (!freshDefaultBranch) throw new Error('Quality snapshot lease retry: default branch unavailable');+ const onDefault = await readGitSnapshot(+ git, app.repoPath, `origin/${freshDefaultBranch}:${APP_QUALITY_SNAPSHOT_FILENAME}`,+ );+ if (REWRITE_BLOCKED.has(onDefault.status)) return untouched(app, onDefault.status);+ if (sameSnapshot(onDefault, next)) {+ lastPublishedBody.set(app.repoPath, next);+ return skipped(app, 'no-changes');+ }+ const probe = deps.probePrForBranch || (await import('./prProbe.js')).probePrForBranch;+ const pr = await probe(app.repoPath, QUALITY_SNAPSHOT_BRANCH);+ if (!pr?.readable || (pr.prState && !['OPEN', 'MERGED', 'CLOSED'].includes(String(pr.prState).toUpperCase()))) {+ throw new Error('Quality snapshot lease retry: publish PR state unavailable');+ }+ return landOrReuse(app, git, freshDefaultBranch, deps, next, count, {+ ...options, leaseRetry: true,+ });+ }lastPublishedBody.set(app.repoPath, next);return result;}@@ -340,7 +366,10 @@ async function landSnapshotPr(app, { body, count, defaultBranch, git, deps, mess).catch(err => git.unstageFiles(worktreePath, commitPaths).catch(() => {}).then(() => Promise.reject(err)));- await pushSnapshotBranch(git, app.repoPath, worktreePath);+ await pushSnapshotBranch(git, app.repoPath, worktreePath).catch(error => {+ if (/stale info/i.test(error?.message || '')) error.qualitySnapshotStaleLease = true;+ throw error;+ });const pr = await openOrAdoptPr(git, worktreePath, defaultBranch, deps);if (!pr?.url) {throw new Error(pr?.error || 'quality snapshot pull request was not created');diff --git a/server/services/appQualitySnapshotFile.test.js b/server/services/appQualitySnapshotFile.test.jsindex 41451fa5e5..4cb412cc2d 100644--- a/server/services/appQualitySnapshotFile.test.js+++ b/server/services/appQualitySnapshotFile.test.js@@ -262,6 +262,103 @@ it('force-with-lease updates an existing snapshot branch rather than committingexpect(deps.writeFile.mock.calls[0][0]).toBe(join(worktreePath, '.quality.json'));});+it('refreshes refs and rebuilds a detached worktree once after a stale lease', async () => {+ let remoteSha = 'aa'.repeat(20);+ let pushes = 0;+ const git = gitDouble({+ fetchOrigin: vi.fn(async () => { if (pushes) remoteSha = 'bb'.repeat(20); }),+ execGit: vi.fn(async (args) => {+ if (args[0] === 'show') return missingShow;+ if (args[0] === 'rev-parse') return { exitCode: 0, stdout: remoteSha, stderr: '' };+ if (args[0] === 'push' && ++pushes === 1) throw new Error('! [rejected] (stale info)');+ return { exitCode: 0, stdout: '', stderr: '' };+ }),+ });+ const deps = testDeps({ git });+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ published: true, prUrl });+ expect(git.fetchOrigin).toHaveBeenCalledTimes(2);+ expect(git.fetchOrigin).toHaveBeenLastCalledWith(app.repoPath, { prune: true });+ expect(deps.probePrForBranch).toHaveBeenCalledWith(app.repoPath, QUALITY_SNAPSHOT_BRANCH);+ expect(deps.addWorktree).toHaveBeenCalledTimes(2);+ expect(deps.addWorktree).toHaveBeenLastCalledWith(+ ['worktree', 'add', '--detach', worktreePath, 'origin/main'], app.repoPath,+ );+ expect(git.execGit.mock.calls.filter(([args]) => args[0] === 'push').map(([args]) => args[1])).toEqual([+ `--force-with-lease=refs/heads/${QUALITY_SNAPSHOT_BRANCH}:${'aa'.repeat(20)}`,+ `--force-with-lease=refs/heads/${QUALITY_SNAPSHOT_BRANCH}:${'bb'.repeat(20)}`,+ ]);+ expect(deps.writeFile.mock.calls.every(([path]) => path === join(worktreePath, '.quality.json'))).toBe(true);+ expect(git.createPR).toHaveBeenCalledTimes(1);+});++it.each(['future', 'malformed'])('refuses a %s snapshot found after a stale lease', async kind => {+ let fetches = 0;+ const body = kind === 'future'+ ? JSON.stringify({ schemaVersion: 3, repository: 'a'.repeat(64) })+ : '{invalid json';+ const git = gitDouble({+ fetchOrigin: vi.fn(async () => { fetches++; }),+ execGit: vi.fn(async (args) => {+ if (args[0] === 'show') {+ return fetches > 1 && String(args[1]).includes(`${QUALITY_SNAPSHOT_BRANCH}:`)+ ? { exitCode: 0, stdout: body, stderr: '' }+ : missingShow;+ }+ if (args[0] === 'rev-parse') return { exitCode: 0, stdout: 'aa'.repeat(20), stderr: '' };+ if (args[0] === 'push') throw new Error('! [rejected] (stale info)');+ return { exitCode: 0, stdout: '', stderr: '' };+ }),+ });+ const deps = testDeps({ git });+ expect(await publishAppQualitySnapshot(app, deps)).toEqual({+ published: false, reason: 'unsupported-format', path: '.quality.json',+ });+ expect(git.fetchOrigin).toHaveBeenCalledTimes(2);+ expect(deps.addWorktree).toHaveBeenCalledTimes(1);+ expect(git.createPR).not.toHaveBeenCalled();+ expect(deps.writeFile.mock.calls.every(([path]) => path === join(worktreePath, '.quality.json'))).toBe(true);+});++it('leaves a second stale lease rejection visible and retryable', async () => {+ let remoteSha = 'aa'.repeat(20);+ const git = gitDouble({+ fetchOrigin: vi.fn(async () => { remoteSha = remoteSha === 'aa'.repeat(20) ? 'bb'.repeat(20) : 'cc'.repeat(20); }),+ execGit: vi.fn(async (args) => {+ if (args[0] === 'show') return missingShow;+ if (args[0] === 'rev-parse') return { exitCode: 0, stdout: remoteSha, stderr: '' };+ if (args[0] === 'push') throw new Error('! [rejected] (stale info)');+ return { exitCode: 0, stdout: '', stderr: '' };+ }),+ });+ const deps = testDeps({ git });+ await expect(publishAppQualitySnapshot(app, deps)).rejects.toThrow('stale info');+ expect(git.execGit.mock.calls.filter(([args]) => args[0] === 'push')).toHaveLength(2);+ expect(deps.addWorktree).toHaveBeenCalledTimes(2);+ expect(git.createPR).not.toHaveBeenCalled();+ // A failed publish cannot poison the in-process queue or mark these bytes as landed.+ await expect(publishAppQualitySnapshot(app, deps)).rejects.toThrow('stale info');+ expect(deps.addWorktree).toHaveBeenCalledTimes(4);+});++it('does not retry a stale lease when the refreshed PR state cannot be read', async () => {+ const git = gitDouble({+ execGit: vi.fn(async (args) => {+ if (args[0] === 'show') return missingShow;+ if (args[0] === 'rev-parse') return { exitCode: 0, stdout: 'aa'.repeat(20), stderr: '' };+ if (args[0] === 'push') throw new Error('! [rejected] (stale info)');+ return { exitCode: 0, stdout: '', stderr: '' };+ }),+ });+ const deps = testDeps({+ git,+ probePrForBranch: vi.fn(async () => ({ readable: false })),+ });+ await expect(publishAppQualitySnapshot(app, deps)).rejects.toThrow('publish PR state unavailable');+ expect(deps.probePrForBranch).toHaveBeenCalledWith(app.repoPath, QUALITY_SNAPSHOT_BRANCH);+ expect(deps.addWorktree).toHaveBeenCalledTimes(1);+ expect(git.createPR).not.toHaveBeenCalled();+});+it('adopts an already-open PR when createPR reports a conflict and merges it immediately', async () => {const deps = testDeps({git: gitDouble({