Skip to content

fix(guardrails): align worktree creation with upstream pattern (Issue #144)#152

Merged
terisuke merged 2 commits intodevfrom
fix/worktree-empty-init
Apr 9, 2026
Merged

fix(guardrails): align worktree creation with upstream pattern (Issue #144)#152
terisuke merged 2 commits intodevfrom
fix/worktree-empty-init

Conversation

@terisuke
Copy link
Copy Markdown

@terisuke terisuke commented Apr 9, 2026

Summary

  • Replace git worktree add --detach with --detach --no-checkout + git reset --hard (upstream pattern)
  • Add git worktree remove --force cleanup on all failure paths (Step 1, 2, and 3)
  • Keep git ls-files --cached verification as safety net

Closes #144

Review findings addressed

  • MEDIUM-1: Missing cleanup on Step 1 failure when directory partially created

Test plan

  • 34/34 scenario tests pass
  • Typecheck passes
  • Manual: trigger team delegation → verify worktree has files

🤖 Generated with Claude Code

terisuke and others added 2 commits April 10, 2026 00:16
…144)

Replace --detach with --detach --no-checkout + git reset --hard to reliably
populate worktree working directories. On failure, cleanup broken worktrees
with git worktree remove --force.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add git worktree remove --force cleanup when git worktree add
itself fails, preventing orphaned partial worktrees.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 9, 2026 15:35
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

New PR opened -- automated review will run on the next push.

To trigger a manual review, comment /review on this PR.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

The following comment was made by an LLM, it may be inaccurate:

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns delegated-agent worktree creation with an upstream Git pattern to avoid empty/incorrect worktrees during team/subagent delegation (Issue #144), while improving cleanup on error paths.

Changes:

  • Switch worktree creation to git worktree add --detach --no-checkout followed by git reset --hard to populate files.
  • Add git worktree remove --force cleanup on failures in steps 1–3.
  • Keep a post-creation verification via git ls-files --cached to detect empty worktrees.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +282 to +285
const made = await git(dir, ["worktree", "add", "--detach", "--no-checkout", next, "HEAD"])
if (made.code !== 0) {
await git(dir, ["worktree", "remove", "--force", next]).catch(() => {})
throw new Error(made.err || made.out || "Failed to create git worktree")
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup on Step 1 failure isn’t guaranteed to remove a partially-created directory: git worktree remove only works for worktrees that were successfully registered, so if worktree add fails before registration you can still end up with a leftover next/ directory that will break subsequent retries. Consider adding a filesystem fallback (scoped to the yard(dir) base) to delete next when worktree remove fails/no-ops, and/or explicitly handle the “not a working tree” case.

Copilot uses AI. Check for mistakes.
throw new Error(`Worktree created but population failed: ${populated.err || populated.out}`)
}

// Step 3: Verify files are actually present in the working directory
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Step 3 comment says this verifies files “present in the working directory”, but git ls-files --cached checks the index (tracked paths), not the filesystem contents. Either adjust the comment to match what’s being checked, or switch to a check that actually validates working tree files if that’s the intent.

Suggested change
// Step 3: Verify files are actually present in the working directory
// Step 3: Verify the worktree has tracked files in Git after reset

Copilot uses AI. Check for mistakes.
@terisuke terisuke merged commit a783a26 into dev Apr 9, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: agent worktrees created as empty git init — team/subagent delegation non-functional

2 participants