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 follow-up work without confirmed merge state

[._.]/ Adam Eivy74a3fa02f123
diff --git a/server/services/agentWorktreeCleanup.js b/server/services/agentWorktreeCleanup.js
index 59366fe028..1754eaa89c 100644
--- a/server/services/agentWorktreeCleanup.js
+++ b/server/services/agentWorktreeCleanup.js
@@ -550,10 +550,10 @@ async function mergeOrPreserveWorktree({ agentId, sourceWorkspace, worktreeBranc
if (skipMerge) {
const pr = await import('./prProbe.js').then(({ probePrForBranch }) =>
probePrForBranch(sourceWorkspace, worktreeBranch)).catch(() => null);
- if (pr?.readable && pr.prState === 'OPEN') {
- // Failed CI or unpublished commits need recovery, which still needs this
+ if (!pr?.readable || (pr.prState && pr.prState !== 'MERGED')) {
+ // Failed/unreadable CI or unpublished commits need recovery, which needs this
// branch. Only a healthy handoff suppresses the post-cleanup audit.
- emitLog('info', `🌳 Preserving ${pr.autoMergeEnabled === true ? 'pending GitHub auto-merge' : 'unmerged PR recovery'} worktree for ${agentId}`, { agentId, branchName: worktreeBranch });
+ emitLog('info', `🌳 Preserving ${pr?.autoMergeEnabled === true ? 'pending GitHub auto-merge' : 'unconfirmed PR recovery'} worktree for ${agentId}`, { agentId, branchName: worktreeBranch });
return;
}
}
diff --git a/server/services/agentWorktreeCleanup.repoStateAudit.test.js b/server/services/agentWorktreeCleanup.repoStateAudit.test.js
index 808099922f..5ffb0024e0 100644
--- a/server/services/agentWorktreeCleanup.repoStateAudit.test.js
+++ b/server/services/agentWorktreeCleanup.repoStateAudit.test.js
@@ -172,6 +172,16 @@ describe('cleanupAgentWorktree → repo-state audit', () => {
await auditRan();
});
+ it.each(['unreadable', 'closed unmerged'])('preserves the worktree for %s PR state', async (state) => {
+ getAgent.mockResolvedValue(worktreeAgent);
+ if (state === 'unreadable') probePrForBranch.mockRejectedValue(new Error('forge unavailable'));
+ else probePrForBranch.mockResolvedValue({ readable: true, prState: 'CLOSED' });
+ const warnings = await cleanupAgentWorktree('agent-1', true, { skipMerge: true, originalTask: { id: 'task-1', metadata: { openPR: true } } });
+ expect(warnings).toEqual([]);
+ expect(removeWorktree).not.toHaveBeenCalled();
+ await auditRan();
+ });
+
it('returns the cleanup warnings unchanged, and survives an audit that throws', async () => {
// The audit is an observer. A failure in it must never swallow the warnings
// the caller uses to notify the user and spawn a merge recovery task.
diff --git a/server/services/cleanupAgentWorktree.test.js b/server/services/cleanupAgentWorktree.test.js
index be510606c4..94b9a63dd6 100644
--- a/server/services/cleanupAgentWorktree.test.js
+++ b/server/services/cleanupAgentWorktree.test.js
@@ -1273,6 +1273,7 @@ describe('cleanupAgentWorktree - PR-creation path', () => {
it('should pass merge: false in the auto-merge fallback when skipMerge is true (review-loop follow-up cleanup)', async () => {
// The follow-up agent already merged via `gh pr merge`; re-merging the worktree
// branch into source workspace would duplicate the squashed commits.
+ findPullRequestForBranchMock.mockResolvedValueOnce({ status: 'found', detail: 'MERGED' });
await cleanupAgentWorktree('agent-1', true, { prCreation: 'never', skipMerge: true });
expect(removeWorktree).toHaveBeenCalledWith(