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

address review (codex): resume tasks isolated only by conflict auto-detection

[._.]/ Adam Eivy9688ff97ddc1
diff --git a/server/services/agentWorkspacePrep.js b/server/services/agentWorkspacePrep.js
index 2fa6d01ade..ca0b7b99e9 100644
--- a/server/services/agentWorkspacePrep.js
+++ b/server/services/agentWorkspacePrep.js
@@ -86,6 +86,15 @@ export async function prepareAgentWorkspace({ agentId, task }) {
let worktreeInfo = null;
const explicitOpenPR = isTruthyMeta(task.metadata?.openPR);
const explicitWorktree = isTruthyMeta(task.metadata?.useWorktree) || explicitOpenPR;
+ // A task pointed at an existing branch must run in a worktree whatever isolated
+ // the ORIGINAL run, because that is the only way to reach the work: the branch
+ // has to be checked out somewhere, and an adoptable tree has to be attached.
+ // Keyed on `existingBranch` so it covers both producers — a resume pointer and
+ // the review-loop follow-up. Without it, a run that got its worktree from
+ // conflict AUTO-detection resumes into the shared workspace on retry (conflict
+ // detection returns `proceed` once the dead agent is gone) and silently
+ // abandons the work the pointer was recorded to save.
+ const wantsWorktree = explicitWorktree || !!task.metadata?.existingBranch;
if (!isReadOnly) {
// Pull latest from git before starting work
@@ -202,7 +211,7 @@ export async function prepareAgentWorkspace({ agentId, task }) {
}
}
- if (explicitWorktree && !jiraBranchName) {
+ if (wantsWorktree && !jiraBranchName) {
const existingBranch = task.metadata?.existingBranch || null;
const { baseBranch: detectedBase } = await git.getRepoBranches(workspacePath).catch(() => ({ baseBranch: null }));
if (existingBranch) {
@@ -254,6 +263,13 @@ export async function prepareAgentWorkspace({ agentId, task }) {
emitLog('success', `🌳 Agent ${agentId} will work in worktree: ${worktreeInfo.branchName} (${origin})`, {
agentId, worktreePath: worktreeInfo.worktreePath, branchName: worktreeInfo.branchName, baseBranch: worktreeInfo.baseBranch
});
+ } else if (!explicitWorktree) {
+ // Reached here only for the resume pointer, on a task that never asked for
+ // isolation. Blocking would be STRICTER than the task's own contract — it
+ // would have run in the shared workspace before the pointer existed — so
+ // degrade to that instead. The resume is lost (the leftover work stays on
+ // disk for the next attempt), but the task still runs.
+ emitLog('warn', `🌳 Worktree creation failed for task ${task.id}; resuming is not possible, continuing in the shared workspace`, { taskId: task.id });
} else {
// Isolation was EXPLICITLY requested (useWorktree/openPR) but the
// worktree couldn't be created. Falling back to the shared workspace
diff --git a/server/services/agentWorkspacePrep.test.js b/server/services/agentWorkspacePrep.test.js
index 08e4c1bb65..e6e7b65359 100644
--- a/server/services/agentWorkspacePrep.test.js
+++ b/server/services/agentWorkspacePrep.test.js
@@ -196,6 +196,49 @@ describe('prepareAgentWorkspace — resuming an interrupted run', () => {
expect(r.outcome).toBe('ready');
});
+ // The run that died may have been isolated by conflict AUTO-detection rather than
+ // useWorktree/openPR. Its retry must still take the worktree path — conflict
+ // detection returns `proceed` once the dead agent is gone, so the old gate sent
+ // the retry into the shared workspace and abandoned the work on disk.
+ it('takes the worktree path for a resume even when the task never asked for isolation', async () => {
+ adoptWorktree.mockResolvedValue({
+ worktreePath: '/mock/worktrees/agent-new', branchName: 'cos/t-resume/agent-dead',
+ baseBranch: null, existingBranch: true, adopted: true
+ });
+ const task = resumeTask();
+ delete task.metadata.useWorktree;
+
+ const r = await prepareAgentWorkspace({ agentId: 'agent-new', task });
+
+ expect(adoptWorktree).toHaveBeenCalled();
+ expect(detectConflicts).not.toHaveBeenCalled();
+ expect(r.workspacePath).toBe('/mock/worktrees/agent-new');
+ });
+
+ // Blocking a task that never opted into isolation would be stricter than its own
+ // contract — it ran in the shared workspace before the pointer existed.
+ it('degrades to the shared workspace when a resume-only task cannot get a worktree', async () => {
+ adoptWorktree.mockResolvedValue(null);
+ createWorktree.mockResolvedValue(null);
+ const task = resumeTask();
+ delete task.metadata.useWorktree;
+
+ const r = await prepareAgentWorkspace({ agentId: 'agent-new', task });
+
+ expect(r.outcome).toBe('ready');
+ expect(r.worktreeInfo).toBeNull();
+ });
+
+ // ...but a task that DID request isolation still fails closed.
+ it('still blocks when an explicitly-isolated resume cannot get a worktree', async () => {
+ adoptWorktree.mockResolvedValue(null);
+ createWorktree.mockResolvedValue(null);
+
+ const r = await prepareAgentWorkspace({ agentId: 'agent-new', task: resumeTask() });
+
+ expect(r.outcome).toBe('blocked');
+ });
+
it('ignores a stale worktree pointer with no branch to resume', async () => {
createWorktree.mockResolvedValue({
worktreePath: '/mock/worktrees/agent-new', branchName: 'cos/t-resume/agent-new', baseBranch: 'main'