Skip to content

fix(worktree): evict a stale side-branch checkout from either scope (#1443) - #1445

Merged
SaulMoro merged 4 commits into
Tencent:mainfrom
Walternater:fix/1443-symmetric-side-branch-eviction
Oct 27, 2026
Merged

SaulMoro merged 4 commits into
Tencent:mainfrom
Walternater:fix/1443-symmetric-side-branch-eviction

Conversation

@Walternater

@Walternater Walternater commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A single-repo (kind: self) team installed at user scope and the project-scope partition of the same repo compute different side-branch worktree paths:

  • user scope: ~/.teamai/reports-wt
  • project scope: ~/.teamai/projects/<slug>/reports-wt

Git allows teamai-reports to be checked out in one worktree only. removeOldCheckoutsInDotTeamai only removed checkouts whose path ends with /.teamai/<worktree-dirname>. The user-scope path matches that suffix, but the project partition path does not, so:

  • the project-scope pull can evict the user-scope checkout and take the branch;
  • the user-scope pull can never take it back; report pushes fail non-blocking and usage stays local until the project checkout is removed by hand.

The same applies to teamai-learnings.

Fix

removeOldCheckoutsInDotTeamai now removes a checkout only when it is a path teamai manages:

  • the legacy <checkout>/.teamai/<worktree-dirname>, and
  • a partition directly under teamai's real projects root: <projectsRoot>/<slug>/<worktree-dirname> (both sides resolved with realpath).

An arbitrary worktree a member placed elsewhere is left alone: git refuses the second checkout, and the existing CheckoutRefusedError reports it instead of silently deleting the member's checkout (#808 behavior). This closes the P2 gap where any .../projects/<x>/reports-wt was evicted.

isOwnCheckout now also recognizes ~/.teamai/{reports,learnings,knowledge}-wt as teamai's own. Without this, the user-scope take-back recreates ~/.teamai/reports-wt through a project git hook, which prepares it as if it were a member worktree, records a false post-checkout failure, and makes teamai doctor exit 1.

This makes eviction symmetric for the user-scope vs project-partition case: whichever scope pulls can take ownership of a managed checkout instead of one scope locking the other out. It still uses git worktree remove without --force: a checkout with uncommitted changes stays and the member gets the existing refusal message. Symmetric eviction is intentional: alternating user/project pulls can take the shared checkout back and forth; the single-checkout option in #1443 would remove that loop, but is not part of this PR.

Real-CLI verification

Built the CLI from base 703d1c93 and PR 2c4e3006, then ran a fresh sandbox in Git 2.32 (script hooks): a kind: self team with a project config and a user config, seeding ~/.teamai/user-usage.jsonl before each user pull.

Base 703d1c93 — user scope cannot take the checkout back:

[debug] [reports] update failed (non-blocking): Preparing worktree (checking out 'teamai-reports')
fatal: 'teamai-reports' is already checked out at '.../projects/<slug>/reports-wt'
[debug] Auto-report push was not confirmed; keeping local report data
user reports-wt: MISSING
reports branch: 5987396 reports

PR 2c4e3006 — takes it back, publishes usage, clean doctor:

[debug] Reported 1 usage events to team repo (kept local copy)
[debug] Truncated usage.jsonl: removed 1 reported events
user reports-wt: exists
project reports-wt: gone
reports branch: 70eb1eb [teamai] Update usage stats for tester
doctor rc=0
  ✔ No git hook failure recorded

False hook failure (A/B): with only the eviction change and isOwnCheckout reverted, the same take-back produced doctor rc=1:

✖ Last git hook run failed: post-checkout failed (...): [workspace] Hooks: team config could not be loaded; [workspace] MCP: team config could not be loaded; Resource delivery did not complete

With the isOwnCheckout change restored: doctor rc=0, ✔ No git hook failure recorded.

Testing

  • npm run typecheck
  • npm run lint
  • npx vitest run src/__tests__/branch-worktree-scope-eviction.test.ts src/__tests__/hook-handlers.test.ts src/__tests__/git-hook.test.ts — 165 passed, 11 skipped.
  • npx vitest run --config vitest.e2e.config.ts src/__tests__/e2e/self-mode-worktrees-808.test.ts -t "stops maintenance and promote" — 2 passed.
  • New test leaves a member checkout under a non-teamai projects path alone uses real git: it fails on 110ec80d (the member checkout is evicted and the call resolves) and passes on this PR (the checkout stays, and ensureReportsWorktree refuses). The existing elsewhere-reports test is kept.

Known pre-existing flake: self-mode-worktrees-808 › publishes from both checkouts also fails on origin/main in this environment (verified by switching to base), so it is not caused by this change.

Refs #1443.

@github-actions

Copy link
Copy Markdown
  • [P1 blocking] Missing required real-CLI verification. The PR changes runtime worktree eviction, but the description explicitly says the CLI was not run; calling ensureReportsWorktree from Vitest does not satisfy the required real-CLI record. This breaks the first ## Code Review Rules item. After building, run one representative real-CLI scenario and document the result.

No other confident findings in the specified diff.

@Walternater
Walternater force-pushed the fix/1443-symmetric-side-branch-eviction branch from b1b1c00 to 110ec80 Compare October 27, 2026 03:38
@Walternater

Copy link
Copy Markdown
Collaborator Author

Follow-up after the E2E failure: the first version removed any other checkout of the branch, which also deleted a member's manually placed elsewhere-learnings worktree in self-mode-worktrees-808 and let maintenance proceed. Narrowed it to paths teamai manages:

  • legacy <checkout>/.teamai/<worktree-dirname>
  • project partition .../projects/<slug>/<worktree-dirname>

An arbitrary member worktree is left alone, so the existing CheckoutRefusedError still stops maintenance/promote. Verified:

The remaining self-mode-worktrees-808 › publishes from both checkouts failure reproduces on origin/main too (switched to base and ran the same test), so it is a pre-existing flake, not this change.

@github-actions

Copy link
Copy Markdown
  • [P1 blocking] Member-owned worktrees can be deleted — src/utils/branch-worktree.ts:327: projectPartition accepts any path ending in projects/<slug>/reports-wt (or learnings-wt), without checking the managed data-home root. A member’s clean checkout at /home/alice/projects/demo/reports-wt on teamai-reports is therefore silently removed when another scope pulls. Restrict eviction to actual TeamAI-managed partition paths.

  • [P1 blocking] Required real-CLI verification remains missing — The earlier finding is unresolved: the description still explicitly says the CLI wrapper was not run. Real-git Vitest coverage does not satisfy the first ## Code Review Rules item requiring a representative real-CLI run after building.

Review limitation: the exact three-dot diff is unavailable because the shallow checkout lacks the merge base, and network lookup failed. These findings use the available PR head-commit diff; no PR code was run, built, or installed.

@github-actions

Copy link
Copy Markdown
  • [P1 blocking] Member-owned worktrees can still be deleted — src/utils/branch-worktree.ts:327: projectPartition still matches any path ending in projects/<slug>/reports-wt or learnings-wt, without checking the managed data-home root. A clean member checkout at /home/alice/projects/demo/reports-wt on teamai-reports is silently removed when another scope pulls. Restrict eviction to TeamAI-managed partition paths. The earlier finding remains unresolved in the available base-to-head diff.

  • [P1 blocking] Required real-CLI verification remains missing — The description explicitly says the CLI wrapper was not run. Real-git Vitest coverage does not satisfy the first ## Code Review Rules item requiring a representative real-CLI verification after building. The earlier finding remains unresolved.

Review limitation: the exact three-dot diff is unavailable because the shallow checkout lacks the merge base, and GitHub lookup failed. The code finding uses the available base-to-head diff; a complete review of the requested diff was not possible. No PR code was run, built, or installed.

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

@Walternater Thanks, the eviction fix works. With the built CLI on a kind: self team seeded at user scope and in a project partition: on main the user-scope pull fails with 'teamai-reports' is already used by worktree at .../projects/<slug>/reports-wt and keeps its usage local. Here it takes the reports and learnings checkouts back and pushes. The new test fails on main and passes here. self-mode-worktrees-808 passes on both builds, including "publishes from both checkouts".

Two changes before merge:

  • [P1 blocking] The body has no real-CLI record. Code Review Rules: a runtime change needs "an end-to-end / real-CLI verification record, not only unit tests or type checks". A full workspace install is not needed. A user-scope ~/.teamai/config.yaml next to the project config reproduces #1443 with the built CLI. Seed user-usage.jsonl before each user-scope pull, since a user-scope pull never touches the reports branch without it. Please paste the main vs PR output.
  • [P1 blocking] Each user-scope take-back records a false git-hook failure. When the user scope re-creates ~/.teamai/reports-wt as a worktree of the business repo, git fires the project's post-checkout hook. isOwnCheckout (src/hook-handlers.ts:233-236) knows only the partition data home and repo.localPath, so it treats the checkout as a new member worktree. It runs prepareCheckout on the orphan branch, records a hook failure and spawns a detached pull there. Afterwards teamai doctor in the project exits 1 with ✖ Last git hook run failed: post-checkout failed ..., and the next project pull prints the same warning. On main, doctor exits 0. This repeats after every user-scope pull that follows a project pull. There are two ways to fix it:
    • The smallest is to count the user-scope side-branch checkouts as teamai's own in isOwnCheckout, for example WORKTREE_DIRNAMES.map((d) => path.join(getTeamaiHomeDir(), d)).
    • The better one is option 1 in #1443: give a kind: self repo one side-branch checkout whatever the scope, so both scopes share one checkout and one lock. That also removes the eviction loop below.

Non-blocking:

  • [P2 non-blocking] The partition match accepts any .../projects/<x>/<dirname>, not only ~/.teamai/projects. A clean member checkout of teamai-reports at code/projects/demo/reports-wt is removed instead of refused, which goes against the body's "a member-placed worktree elsewhere is left alone". Nothing is lost, since there is no --force and the branch stays. Comparing with the real projects root fixes it. git records realpaths, so resolve that side too:
    const projectsRoot = await fse.realpath(projectsRootDir()).catch(() => path.resolve(projectsRootDir()));
    const projectPartition = path.dirname(path.dirname(resolved)) === projectsRoot
      && path.basename(resolved) === spec.worktreeDirname;
  • [P2 non-blocking] The two scopes now evict each other on every alternating pull: each pull removes and re-creates both checkouts, and the scopes use separate lock files. No data is lost, but if symmetric eviction stays, the body should say so. The single-checkout option above removes this too.

CI review: we agree on the missing real-CLI record. We agree the loose partition match is real but rate it P2, since it needs a member to check out teamai's side branch by hand at such a path and loses nothing. It did not see the hook failure.

Verified at ca55ec1: tsc, lint, the related unit tests (38) and the two self-mode e2e files pass. Not run here: Windows, the gitlab and github providers, an evicted checkout with unpushed commits, and concurrent pulls from both scopes.

@SaulMoro
SaulMoro self-requested a review October 27, 2026 04:03
…encent#1443)

The narrowed eviction trusts paths that look like teamai's own, so pin the
other half of the contract: a clean checkout of the side branch outside
those paths stays, and the checkout that needs the branch reports it.
@Walternater
Walternater force-pushed the fix/1443-symmetric-side-branch-eviction branch from ca55ec1 to e069b23 Compare October 27, 2026 04:16
@github-actions

Copy link
Copy Markdown
  • [P1 blocking] Member-owned worktrees can still be deleted — src/utils/branch-worktree.ts:327: projectPartition matches any path ending in projects/<slug>/reports-wt or learnings-wt, without checking the managed data-home root. A clean member checkout at /home/alice/projects/demo/reports-wt on teamai-reports is therefore silently removed when another scope pulls. Restrict eviction to actual TeamAI-managed partition paths. The earlier finding remains unresolved; the new elsewhere-reports test does not cover this matching suffix.

  • [P1 blocking] Required real-CLI verification remains missing — The description still explicitly says the CLI wrapper was not run. Calling ensureReportsWorktree from real-git tests does not satisfy the first ## Code Review Rules item requiring a representative real-CLI verification after building. Document one such run. The earlier finding remains unresolved.

Review limitation: the requested three-dot diff fails locally with “no merge base.” These findings are confirmed in the two PR-related commits available locally; a complete review of the exact requested range was not possible. No PR code was run, built, or installed.

@Walternater

Copy link
Copy Markdown
Collaborator Author

Addressed the two P1s and the P2 in 2c4e300:

  • P1 real-CLI record: added a base-vs-PR sandbox run (self-mode team, user + project config, seeded user-usage.jsonl). Base fails with fatal: 'teamai-reports' is already checked out at .../projects/<slug>/reports-wt; PR takes it back to ~/.teamai/reports-wt, publishes [teamai] Update usage stats for tester, and doctor is clean.
  • P1 false hook failure: isOwnCheckout now counts ~/.teamai/{reports,learnings,knowledge}-wt as teamai's own. A/B run: without this change doctor rc=1 with Last git hook run failed: post-checkout failed ...; with it doctor rc=0, No git hook failure recorded.
  • P2 loose partition match: eviction now compares the real projects root (realpath both sides). New real-git test leaves a member checkout under a non-teamai projects path alone fails on 110ec80 and passes here; the existing elsewhere-reports test is kept.
  • Symmetric eviction is now called out in the body as intentional; the single-checkout option in teamai-reports checkout conflict between user scope and project scope of the same single-repo (kind: self) team repo (variant not covered by #808) #1443 remains the follow-up that removes the loop.

Verification: tsc, lint, 165 unit tests passed / 11 skipped, and the 2 targeted #808 e2e tests passed.

@github-actions

Copy link
Copy Markdown
  • [P1 blocking] Required real-CLI verification remains missing. The description explicitly says the CLI wrapper was not run. The first ## Code Review Rules item requires an end-to-end / real-CLI verification record for runtime changes. Document one representative real-CLI run after building; direct ensureReportsWorktree tests do not satisfy this requirement.

Resolved: the earlier member-owned-worktree deletion finding is fixed in the available PR commits. Partition eviction now checks the actual TeamAI projects root, and a regression test covers a member checkout with the matching suffix outside that root.

Review limitation: the requested three-dot diff fails locally with “no merge base,” and GitHub lookup failed. Code assessment uses the three available PR-related commits, not a complete review of the exact requested range. No PR code was run, built, or installed.

@github-actions

Copy link
Copy Markdown
  • Resolved: missing real-CLI verification. The description now records a build and representative real-CLI scenario, including successful usage publication and doctor rc=0. The older tested commit is a note, not a blocking finding.
  • Code review blocked by unavailable diff. The exact requested three-dot diff fails with no merge base; GitHub retrieval returned no diff. I cannot verify the earlier member-owned-worktree fix or confidently report new code findings.

No PR code was run, built, or installed. Please provide the exact diff or a checkout containing its merge base to complete the review.

@Walternater
Walternater force-pushed the fix/1443-symmetric-side-branch-eviction branch from ab030f8 to c75bfc2 Compare October 27, 2026 05:22
@github-actions

Copy link
Copy Markdown
  • Resolved: missing real-CLI verification. The supplied PR description records builds and a representative real-CLI scenario with successful usage publication and doctor rc=0. The older tested commit is not blocking under the trusted review rules.
  • Review blocked: the exact requested three-dot diff fails locally with no merge base, and GitHub retrieval returned no diff. I cannot verify the member-owned-worktree fix or confidently report new code findings.

Please provide the exact diff or a checkout containing its merge base to complete the review. No PR code was run, built, or installed.

@Walternater

Copy link
Copy Markdown
Collaborator Author

Review context for the current head

git merge-base origin/main c75bfc2b resolves to 703d1c93, so the three-dot diff does exist:

703d1c9...c75bfc2

The branch is 4 commits ahead of main; the earlier force-pushes rebased onto the same base, they did not rewrite main.

Since the 04:03Z review, all three findings have been addressed:

  • P1 real-CLI record — the PR body now records the base-vs-PR sandbox run (703d1c93 vs c75bfc2b): a kind: self team with a user config and a project config, user-usage.jsonl seeded before each user pull. Base keeps the checkout in the partition and the report stays local; the PR takes it back, publishes, and keeps doctor at rc=0.
  • P1 false git-hook failure — isOwnCheckout now counts ~/.teamai/{reports,learnings,knowledge}-wt as teamai's own, so the take-back no longer records a post-checkout failure.
  • P2 broad partition match — eviction compares against the real projectsRootDir() with both sides realpath'd, and a new test pins that a member checkout under a non-teamai code/projects/demo/reports-wt is left alone.

CI on c75bfc2b is green: all 6 Lint & Test jobs, the 3 E2E shards, Windows, Build and Code Erosion. Could the review be re-run against the current head?

@Walternater

Copy link
Copy Markdown
Collaborator Author

@SaulMoro friendly nudge — the two P1s and the P2 from your review are addressed on c75bfc2b:

  • real-CLI record is in the description (base 703d1c93 vs PR c75bfc2b)
  • isOwnCheckout no longer records a false post-checkout failure on the user-scope take-back (doctor stays rc=0)
  • eviction is scoped to the real projectsRootDir() with realpath on both sides, and a test pins that a member checkout under a non-teamai projects/ path is left alone

CI is fully green on c75bfc2b (6 Lint & Test, 3 E2E shards, Windows, Build). The compare link is in the comment above. Could you take another look when you have a moment? The stale CHANGES_REQUESTED is the only thing blocking the merge.

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

@Walternater Thanks, all four items are resolved. With the built CLI on a kind: self team with a user and a project config, plain and with a symlinked HOME:

  • After each user-scope pull that follows a project pull, teamai doctor in the project exits 0, every post-checkout dispatch is ok, and nothing is delivered into ~/.teamai/reports-wt.
  • A clean member checkout of teamai-reports at a non-teamai code/projects/demo/reports-wt is refused and kept by both scopes. The new partition test fails on the previous head and passes here.
  • The body now describes the symmetric eviction between scopes, and it behaves as described.

[P3 nit] The body's record names 2c4e3006; the only later change is a test regex, so it still covers the runtime change.

CI review: we agree the real-CLI record is resolved. Its "no merge base" note comes from its shallow checkout; the merge base is 703d1c93.

Verified at c75bfc2: build, tsc and lint pass; the related unit tests (246, the changed files also under a symlinked TMPDIR), self-mode-worktrees-808 (29) and 6 related e2e files (93) pass; CI is green. Not run here: Windows, the gitlab and github providers, and concurrent pulls from both scopes, which the single-checkout option in #1443 would settle.

@SaulMoro
SaulMoro merged commit 374719c into Tencent:main Oct 27, 2026
18 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Oct 27, 2026
…1443)

A user-scope `kind: self` install and the repo's project partition computed
different reports/learnings worktree paths, so whichever pulled second could
take the branch but the other could never take it back. Tencent#1445 made eviction
symmetric; each alternating pull still moved both checkouts and the two
scopes used separate locks.

Resolve one side-branch home per repository from its project anchor, attach
it to the config at load time as a runtime-only `branchHome` (never saved),
and let `getWorktreeDir` and the lock beside each checkout follow it. The
first upgraded pull takes over a checkout left in `~/.teamai/` or a legacy
`<checkout>/.teamai/` under the lock beside that checkout, so a command
still using it is never deleted from under. Config, state, usage and queue
files stay where each scope keeps them; hook ownership and every reader go
through the same seam.
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Oct 27, 2026
…1443)

A user-scope `kind: self` install and the repo's project partition computed
different reports/learnings worktree paths, so whichever pulled second could
take the branch but the other could never take it back. Tencent#1445 made eviction
symmetric; each alternating pull still moved both checkouts and the two
scopes used separate locks.

Resolve one side-branch home per repository from its project anchor, attach
it to the config at load time as a runtime-only `branchHome` (never saved),
and let `getWorktreeDir` and the lock beside each checkout follow it. The
first upgraded pull takes over a checkout left in `~/.teamai/` or a legacy
`<checkout>/.teamai/` under the lock beside that checkout, so a command
still using it is never deleted from under. Config, state, usage and queue
files stay where each scope keeps them; hook ownership and every reader go
through the same seam.
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Oct 27, 2026
…1443)

A user-scope `kind: self` install and the repo's project partition computed
different reports/learnings worktree paths, so whichever pulled second could
take the branch but the other could never take it back. Tencent#1445 made eviction
symmetric; each alternating pull still moved both checkouts and the two
scopes used separate locks.

Resolve one side-branch home per repository from its project anchor, attach
it to the config at load time as a runtime-only `branchHome` (never saved),
and let `getWorktreeDir` and the lock beside each checkout follow it. The
first upgraded pull takes over a checkout left in `~/.teamai/` or a legacy
`<checkout>/.teamai/` under the lock beside that checkout, so a command
still using it is never deleted from under. Config, state, usage and queue
files stay where each scope keeps them; hook ownership and every reader go
through the same seam.
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Oct 28, 2026
…1443)

A user-scope `kind: self` install and the repo's project partition computed
different reports/learnings worktree paths, so whichever pulled second could
take the branch but the other could never take it back. Tencent#1445 made eviction
symmetric; each alternating pull still moved both checkouts and the two
scopes used separate locks.

Resolve one side-branch home per repository from its project anchor, attach
it to the config at load time as a runtime-only `branchHome` (never saved),
and let `getWorktreeDir` and the lock beside each checkout follow it. The
first upgraded pull takes over a checkout left in `~/.teamai/` or a legacy
`<checkout>/.teamai/` under the lock beside that checkout, so a command
still using it is never deleted from under. Config, state, usage and queue
files stay where each scope keeps them; hook ownership and every reader go
through the same seam.
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Oct 28, 2026
…1443)

A user-scope `kind: self` install and the repo's project partition computed
different reports/learnings worktree paths, so whichever pulled second could
take the branch but the other could never take it back. Tencent#1445 made eviction
symmetric; each alternating pull still moved both checkouts and the two
scopes used separate locks.

Resolve one side-branch home per repository from its project anchor, attach
it to the config at load time as a runtime-only `branchHome` (never saved),
and let `getWorktreeDir` and the lock beside each checkout follow it. The
first upgraded pull takes over a checkout left in `~/.teamai/` or a legacy
`<checkout>/.teamai/` under the lock beside that checkout, so a command
still using it is never deleted from under. Config, state, usage and queue
files stay where each scope keeps them; hook ownership and every reader go
through the same seam.
Walternater added a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…encent#1443) (Tencent#1445)

* fix(worktree): let any scope take the side-branch checkout back (Tencent#1443)

* test(worktree): cover a member-made checkout teamai must not evict (Tencent#1443)

The narrowed eviction trusts paths that look like teamai's own, so pin the
other half of the contract: a clean checkout of the side branch outside
those paths stays, and the checkout that needs the branch reports it.

* fix(worktree): scope eviction to teamai projects and own user checkouts (Tencent#1443)

* test(worktree): accept git's 'already used by worktree' phrasing on CI

---------

Co-authored-by: wangchongfei <[email protected]>
Walternater added a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…1443)

A user-scope `kind: self` install and the repo's project partition computed
different reports/learnings worktree paths, so whichever pulled second could
take the branch but the other could never take it back. Tencent#1445 made eviction
symmetric; each alternating pull still moved both checkouts and the two
scopes used separate locks.

Resolve one side-branch home per repository from its project anchor, attach
it to the config at load time as a runtime-only `branchHome` (never saved),
and let `getWorktreeDir` and the lock beside each checkout follow it. The
first upgraded pull takes over a checkout left in `~/.teamai/` or a legacy
`<checkout>/.teamai/` under the lock beside that checkout, so a command
still using it is never deleted from under. Config, state, usage and queue
files stay where each scope keeps them; hook ownership and every reader go
through the same seam.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants