Skip to content

[bug] Periodic worktree tasks silently lose isolation from cycle 2 on: the successor inherits a branch that is still checked out in the previous worktree #94

Closed

Description

@timacek

What happens

spawnNextCycle copies the finished card's pinned branch (and branches map) into the new todo card:

src/shared/protocol.ts:840-841:

...(source.branch !== undefined ? { branch: source.branch } : {}),
...(source.branches !== undefined ? { branches: { ...source.branches } } : {}),

The predecessor card stays in in_review and its worktree stays on disk until a human merges and
removes it. The successor is a scheduled run, so run() prepares isolation with reuse: false
(src/host/execution.ts:655), i.e. prepareWorktree(..., 'fresh'):

src/host/git.ts:317-325:

const exists = await quick(['show-ref', '--verify', `refs/heads/${branch}`], root)
if (exists.ok) {
  await heavy(['worktree', 'remove', '--force', path], root)
  await heavy(['worktree', 'prune'], root)
  const moved = await heavy(['branch', '-f', branch, 'HEAD'], root)   // fatal while checked out elsewhere
  if (!moved.ok) return undefined                                     // -> whole run degrades
  ...
}

Cycle 2 targets a different path (<project>/.dsh-worktrees/<new taskId>), so the remove --force/prune pair does not free the branch — the predecessor's worktree still holds it and
git branch -f fails with fatal: cannot force update the branch ... used by worktree at ....
prepareWorktree returns undefined, prepareMirror degrades the whole run
(src/host/isolation.ts:202-203), and the execution proceeds in the user's main checkout with
isolation: 'none', isolationNote: 'worktree 准备失败(git 报错或目录被占用),已在原目录执行'.

Impact

  • The task's isolation: 'worktree' promise is silently gone from the second cron window onwards
    (the default periodicCompletion is spawn), and it stays gone: cycle 3 inherits the same
    branch once more.
  • The agent edits the user's main working tree instead of a task branch.
  • Because there is no prepared mirror, settleExecution records no headCommit, commits,
    dirtyFiles or diffStat (execution.ts:928) — the reviewer sees a card "with no evidence"
    for work that did happen.
  • rearm cards are unaffected (same path is reused and reset), which is why this is easy to miss.

Verification

Reproduced end-to-end with a real repository, real createGitFace()/createRepoScanner(), real
TaskStore and ExecutionService:

  • cycle 1: isolation: 'worktree', worktree present on disk;
  • successor minted with branch === 'task/Nightly-patrol+t-cycle1' (also asserted by
    tests/periodic-once.spec.ts:82);
  • cycle 2 dispatched as the scheduler does: isolation: 'none' with the degrade note above;
  • the raw git behaviour was confirmed separately: branch -f → exit 128 while the old worktree
    holds the branch.

Environment

  • dsh-taskboard 0.9.9 / dev @ 140e763 (originally reproduced on 0.9.6 @ 3054eba;
    code paths unchanged in v0.9.8/v0.9.9)
  • DSH 0.2.1-rc.2, Windows 11, git 2.51

Suggested fix

Pick one deliberately:

  1. Do not copy branch/branches into the successor — let it derive
    task/<title>+<newTaskId> on its first run; update the existing test expectation.
  2. Or make prepareWorktree('fresh') fall back to a per-run branch when the pinned branch is
    checked out elsewhere, and surface a note instead of degrading to in-place execution.
  3. Or, if a shared branch across cycles is intended, run the successor in the predecessor's
    worktree path (reuse) and make the handoff explicit.

Whichever direction, a regression test that dispatches cycle 2 with the predecessor's worktree
still alive would pin it.

Dedupe

#15 is multi-repo worktree support, #35 is the periodic completion policy, #30 is the cron overflow
queue. None covers the branch inheritance colliding with the predecessor's live worktree — this is
an interaction bug between two individually tested features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions