fix(agent-management): open a worktree for a repository with no commits - #45
Merged
Merged
Conversation
Creating a subagent in a repository that had been initialised and never committed to failed outright. prepareWorkdir read the base commit with a plain rev-parse HEAD and treated any failure as fatal, so an unborn HEAD -- a branch that exists in name only -- took down the whole creation. The judgement behind it was that being a git repository implies having a commit to branch from. It does not, and git has a shape for exactly this case: worktree add --orphan starts a branch with no history and needs no base. The result is empty, which is correct, since nothing is committed to check out. The user's repository is left untouched -- HEAD stays unborn and the commit count stays at zero. rev-parse --verify -q separates the three outcomes: 0 resolves, 1 is the unborn branch and says so with an empty stderr, and anything else stays the hard error it was, so a broken repository is not quietly downgraded. --orphan arrived in git 2.42 while Ubuntu 22.04 and Debian 12 are both supported with older ones. Rather than parse git --version, whose release suffixes are their own trap, the call is attempted and an empty workspace serves where it fails -- what a project without git already gets, and a repository with nothing committed is in the same position. The reason travels back in a new optional AgentWorkdir.note and reaches the tool's output, so a git project producing no worktree is explained rather than silently downgraded. note is a field rather than another WorkdirSource: that value decides which opening instruction the subagent receives, and a fallback workspace needs exactly the instruction an empty one already gets, so a new value would have to be collapsed back into the old one at its only reader.
|
The following comment was made by an LLM, it may be inaccurate: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #44
Type of change
What does this PR do?
Creating a subagent in a repository that had been
git init-ed and never committed to failed outright.prepareWorkdirread the base commit with a plainrev-parse HEADand treated any failure as fatal, so an unborn HEAD — a branch that exists in name only — took the whole creation down.The judgement behind it was that being a git repository implies having a commit to branch from. It does not, and git has a shape for exactly this case:
worktree add --orphanstarts a branch with no history and needs no base. The resulting worktree is empty, which is correct — nothing is committed to check out — and the user's repository is left untouched: HEAD stays unborn, the commit count stays at zero.rev-parse --verify -qseparates the three outcomes.0resolves to a commit.1is the unborn branch, and says so with an empty stderr. Anything else keeps the hard error it always had, so a broken repository is not quietly downgraded into an empty workspace.--orphanarrived in git 2.42, while Ubuntu 22.04 and Debian 12 are both still supported with older ones. Rather than parsegit --version, whose release suffixes and2.42.0.windows.1forms are their own trap, the call is attempted and an empty workspace serves where it fails — that is what a project without git already gets, and a repository with nothing committed is in the same position. The reason travels back in a new optionalAgentWorkdir.noteand reaches the tool's output, so a git project producing no worktree is explained rather than silently downgraded.noteis a field rather than anotherWorkdirSource. That value decides which opening instruction the subagent receives, and a fallback workspace needs exactly the instruction an empty one already gets — a new value would have to be collapsed back into the old one at its only reader.Two alternatives were considered and rejected, both recorded in the fix document: creating an empty commit so HEAD resolves writes to history the caller never asked to change, and telling the model to commit is an instruction to mutate the user's repository that it may well carry out as
git add -A.How did you verify your code works?
Two tests, both against real git, and the first checked to fail without the fix:
git initleaves it — now yields a worktree, and the repository still reports zero commits afterwards, so the fix is not buying success by writing to historyagent-management101/101 and the worktree suites 22/22 pass; typecheck 30/30.One case is deliberately not automated, with the reason recorded: "old git without
--orphan" cannot be simulated under the realGitlayer without faking a binary on PATH. The fallback path itself is covered by failingcreateForAgentdirectly, which is where that branch can actually go wrong.Checklist