Skip to content

fix(push): reuse an existing --branch destination - #1827

Merged
SaulMoro merged 1 commit into
Tencent:mainfrom
hobostay:fix/push-reuse-explicit-branch
Nov 11, 2026
Merged

SaulMoro merged 1 commit into
Tencent:mainfrom
hobostay:fix/push-reuse-explicit-branch

Conversation

@hobostay

@hobostay hobostay commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

teamai push --branch <name> could only be used once per destination. The explicit name reached pushRepoBranch's non-reuse path, which always ran git checkout -b. A branch an earlier push left locally made the next run fail with git's a branch named '<name>' already exists; when only origin had the branch, the new commit was based on the default branch and the push was rejected non-fast-forward.

The destination is now prepared before callers copy the payload into the working tree, so the checkout can never collide with a file the branch already holds. Any pending config or env edit already in the working tree is carried aside and restored after the switch:

  • fetch origin; fast-forward a local destination that has no unpushed commit, or rebase the commits it does have onto origin's tip (a conflict fails before any payload is copied);
  • create a missing local branch from origin, or a fresh one;
  • on a retry whose tree holds nothing new, publish the destination's unpushed commit instead of deleting the branch, and keep the destination when there is genuinely nothing to send;
  • refuse a destination that is the team repo's default branch, and, in self mode, any existing local destination branch holding a commit not on origin, so --branch cannot publish the member's work directly.

Generated teamai/push/... branches and pending-PR reuse groups keep their old path.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

Test Plan

  • npx vitest run src/__tests__/git.test.ts — 76 pass. Two new unit cases: a local destination is checked out instead of checkout -b; a destination only on origin is fetched and created from origin/<name>.
  • npx vitest run src/__tests__/team-ref.test.ts — 61 pass. Real-git cases push twice to release/shared with a replaced file, with a pending edit in the working tree, with staged renames (including a recreated source path), with a dangling symlink, with a commit whose push failed (pre-receive hook), with origin advanced between pushes, and refuse a self-mode destination holding a real unpushed commit.
  • npx vitest run src/__tests__/push-pending-pr.test.ts src/__tests__/push-team-config.test.ts src/__tests__/push-env.test.ts — pass; 213 tests across the five push files. New unit cases refuse --branch main and, in self mode, a local destination branch with unpushed commits while the member is on main. Four rollback tests in push-env.test.ts used an existing local teamai/taken branch to make branch creation fail; they now use an invalid branch name for the same injection, since an existing destination is supported after this fix.
  • npm run typecheck passes.
  • npm run lint passes, including the documentation checks.

Real CLI after npm run build, in a temporary HOME with a user-scope config, a local team repo, and --provider git (so PR creation fails after the branch is pushed; the branch content is the assertion):

  1. Push skill alpha with body v1.
  2. From a second clone, advance the destination's teamai.yaml with a member note.
  3. Rewrite alpha's SKILL.md to body v2, add skill beta, edit the clone's teamai.yaml (the payload carried across the branch switch), and run the same command again.
  • origin/main (e30376d): the second push fails with a branch named 'release/knowledge' already exists, and the branch holds only the v1 alpha skill.
  • This branch (mixed run at 45ed604; later commits only harden refusals, staged renames, symlinks, failed-restore backups, and the self-mode count): the second push succeeds and the branch holds the member's alpha v2, beta, and the teamai.yaml edit:
    alpha v2 on branch: true
    beta on branch: true
    member config edit on branch: true

Notes for Reviewers

  • explicitBranch is set only when --branch names the destination of a group with no pending PR record; a reuse group continues to use its recorded branch.
  • Reusing the local branch rather than resetting it is deliberate: a push that committed but failed to reach origin must not lose that commit.
  • The destination is checked out before pushItem copies resources, which is what lets a second push replace a file the first push committed. Pending config/env edits in the tree are copied aside before the switch and restored after it, so the checkout neither loses nor collides with them.
  • The carry keeps a staged rename's source path as a removal, but preserves a replacement file created at that source path; it uses lstat so a dangling symlink is backed up rather than treated as absent. A failed restore keeps the backup directory and names it instead of deleting the only remaining copy; the backup path is also logged before the reset, so a killed process leaves a trace in debug.log.
  • --branch main is refused before any payload is copied. In self mode the named local branch is checked even when the member is on another branch: any commit it holds beyond origin refuses the push, naming the branch and count. A real-git row pins the rev-list count against an actual unpushed commit, and the stray whitespace line from review is gone.
  • The four push-env.test.ts rollback tests were updated only in their failure injection (invalid destination instead of a pre-existing valid one); their assertions are unchanged.
  • No tracked issue; found by auditing the explicit branch path.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown
  • [P1 blocking] src/utils/git.ts:1277 — Existing destinations cannot receive successive edits to the same file. Callers copy edits onto the default branch before checkout. If teamai.yaml is A on main, B on the destination, and the next push writes C, checkout aborts because it would overwrite local changes. Preserve the payload, switch branches, then apply it; the new tests only add distinct files.

  • [P1 blocking] src/utils/git.ts:1198 — Reusing a local destination makes the existing no-change cleanup unsafe. After a commit succeeds but its push fails, retrying with files already matching that commit produces no staged changes. The helper then force-deletes the destination and returns false without publishing the outstanding commit. Preserve existing explicit branches and push unpublished commits even when no new commit is needed.

  • [P2 non-blocking] src/utils/git.ts:1276 — The local-branch path never fetches or incorporates origin. If another member advances the destination after the first push, the next push extends the stale local tip and is rejected non-fast-forward. Incorporate remote advances while preserving unpublished local commits.

The description includes a representative real-CLI verification record, so there is no missing-testing finding. No PR code was executed.

@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.

Reviewed at 8dfe3cc6, with the CLI built at this head and at main (107afae0) and run in verify worlds. The case in the body works: pushing twice to release/knowledge leaves both skills on the branch. But reusing any existing local branch, without checking what that branch is, opens up three paths main refused.

[P1 blocking] --branch main now pushes straight to the default branch. checkoutExplicitBranch finds main among the local branches, checks it out, commits, and pushes it. On main the same command fails with "a branch named 'main' already exists", so nothing lands without a PR. #663 settled this: --branch on push, and no --direct. Refuse the default branch (and any branch the team repo protects by convention) as a destination.

[P1 blocking] In self mode, --branch <member's own branch> publishes that branch's unpushed commits. The team checkout shares refs with the member's repo. Repro: init --self; the member has feature/mine with an unpushed commit; teamai push --all --branch feature/mine. The run exits 1, but origin/feature/mine now holds the member's commit and teamai's. On main it fails before pushing anything. Reuse a local branch only when every commit beyond origin/<name> (or the default branch, when origin has none) is one teamai wrote; otherwise refuse and name the branch.

[P1 blocking] A destination that already has a file the push writes can't take the next push. The payload is written on the default branch before the checkout. If teamai.yaml (or a skill file) differs between main and the destination, git checkout <name> aborts with "untracked working tree files would be overwritten". So a second edit to the same skill fails, which is the common way to reuse a branch. The automated review found the same thing. Both new team-ref cases write distinct files (a.txt, b.txt), so neither hits it. Switch branches first, then apply the payload, and add a row that edits the same file twice.

[P2 non-blocking]

  • The local-branch path never looks at origin. After a colleague pushes to the destination, every later --branch push is rejected non-fast-forward until the member deletes the local branch by hand. Nothing is lost, but the error doesn't point there.
  • With a reused local branch, the no-change cleanup can force-delete a destination that still holds a commit that never reached origin: a commit succeeded, its push failed, and the retry stages nothing new. I couldn't reach this through the CLI, but the code path is there (the automated review rates it P1).
  • The git.test.ts cases assert which git calls ran. The real-git rows in team-ref.test.ts are what proves this; extend those.

Not from this PR, for a separate issue: after any failed push the team checkout stays on the unpushed branch, and teamai pull then fails with "couldn't find remote ref". This also happens on main. The pending-PR reuse path also force-pushes over a colleague's commit on the destination, with a lease. That too is main's behaviour.

@hobostay
hobostay force-pushed the fix/push-reuse-explicit-branch branch from 8dfe3cc to 28965e0 Compare November 11, 2026 03:21
@github-actions

Copy link
Copy Markdown

All three earlier findings remain unresolved in the current diff:

  • [P1 blocking] src/utils/git.ts:1277 — Successive edits to the same file still fail. If teamai.yaml is A on main, B on the destination, and the next push copies C onto main before checkout, Git refuses to switch because checkout would overwrite C. Preserve the payload, switch branches, then apply it. The added tests only push distinct files.

  • [P1 blocking] src/utils/git.ts:1197 — Reusing an existing destination still reaches the destructive no-change cleanup. If a commit succeeds but its push fails, retrying with payload files matching that commit leaves nothing staged; the helper deletes the destination and returns false without publishing the outstanding commit. Preserve existing explicit branches and push unpublished commits even when no new commit is needed.

  • [P2 non-blocking] src/utils/git.ts:1276 — The local-destination path returns without fetching or incorporating origin. When another member advances the remote destination between pushes, the next push extends the stale local branch and is rejected non-fast-forward. Incorporate remote advances while preserving unpublished local commits.

The description contains a representative real-CLI verification record, so there is no missing-testing finding. Its older recorded commit is not blocking under the review rules. No PR code was executed.

@hobostay
hobostay force-pushed the fix/push-reuse-explicit-branch branch from 28965e0 to 8dd7e37 Compare November 11, 2026 03:29
@github-actions

Copy link
Copy Markdown
  • [P1 blocking] src/push.ts:872 — The resource-group path still prepares the destination with pending config/env edits in the working tree: pushCore restores them before calling pushGroup. If main’s teamai.yaml is A, the destination’s is B, and the member edits it to C while adding a skill, checkout refuses to overwrite C and the push fails. Preserve these clone-resident payloads across preparation, as the config-only path now does.

Earlier findings: unpublished-commit cleanup and remote advancement are resolved. Successive file replacement is fixed for copied resources and config-only pushes, but remains broken for the mixed config/resource case above.

The description includes a representative real-CLI verification record, so there is no missing-testing finding. No PR code was executed.

@hobostay
hobostay force-pushed the fix/push-reuse-explicit-branch branch from 8dd7e37 to 45ed604 Compare November 11, 2026 03:33
@github-actions

Copy link
Copy Markdown
  • [P2 non-blocking] src/utils/git.ts:1401 — Carrying a staged rename preserves only file.path, not the deleted file.from. For git mv old.yaml new.yaml, the hard reset recreates old.yaml, and restoration adds new.yaml without removing the old file. Preserve both sides so preparation does not turn a rename into a duplicate.

  • [P2 non-blocking] src/utils/git.ts:1445 — Restoration deletes the backup even when copying an entry fails. Since preparation already discarded the original working-tree changes, a failed restore permanently loses the remaining edits. The cleanup-error path at src/utils/git.ts:1418 has the same problem after a successful reset. Retain the backup on failure and report its location.

All earlier findings are resolved, including the mixed config/resource checkout collision.

The description includes a representative real-CLI verification record, so there is no missing-testing finding. 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.

Re-reviewed 45ed604b with the CLI built at this head and run in verify worlds. Two of the earlier findings are fixed. A second push that edits the same file now succeeds, for both teamai.yaml and a skill file. A retry with nothing new now publishes the commit an earlier push couldn't, instead of deleting the branch. Two blockers from the last round are still open, and the new carry/restore adds a third.

[P1 blocking] --branch main still pushes straight to the default branch. Reproduced: origin's main gained the skill, with no PR. Main refuses this. Refuse the team repo's default branch as a destination (per #663: --branch, no --direct).

[P1 blocking] Self mode, --branch <member's own branch> still publishes the member's unpushed commit. Reproduced with the member on main and feature/mine holding an unpushed commit. The member's files are untouched; the push runs in a throwaway worktree. But origin/feature/mine now carries their commit. With the member checked out on feature/mine, the push fails with a worktree collision instead of a clear refusal. Reuse a local branch only when every commit beyond origin/<name> (or the default branch) is one teamai wrote; otherwise refuse and name the branch.

[P1 blocking] carryWorkingTree/restoreWorkingTree can lose the team checkout's uncommitted edits. The edits only exist in the teamai-push-carry-* backup between the hard reset and the restore, and three paths drop it:

  • carryWorkingTree catches any error after reset --hard (for example a failed clean) and removes the backup before rethrowing. The reset has already discarded the edits.
  • restoreWorkingTree removes the backup in finally, even when a copy fails partway.
  • A process killed during the destination fetch leaves the edits only in the temp backup. Nothing prints its path, and a retry doesn't recover it. Reproduced with a pending teamai.yaml edit.

Keep the backup whenever the restore didn't complete, and print its path. Better still, don't move the edits out of git at all: git stash push --include-untracked, then stash pop, keeps them recoverable through git stash list even after a crash.

[P2 non-blocking] A staged rename comes back as a duplicate: only file.path is carried, and the hard reset recreates the old name. This is from the automated review.

Not from this PR: a second --branch push for resources already pushed goes through the pending-record reuse path. That path force-pushes with a lease it has just refreshed, so it overwrites a colleague's commit on the destination and reports ✔ Pushed branch. Main does the same. But the body says a second push "reuses the destination", so either narrow that claim or leave a note that this path is separate.

@hobostay
hobostay force-pushed the fix/push-reuse-explicit-branch branch from 45ed604 to d3774cb Compare November 11, 2026 04:10
@github-actions

Copy link
Copy Markdown
  • [P2 non-blocking] src/utils/git.ts:1409 — A rename’s source may also contain a replacement file. After git mv a.yaml z.yaml, creating a new a.yaml and staging both produces an added source followed by the rename. Setting file.from to false overwrites the source’s recorded presence, so preparation deletes the replacement without backing it up. Preserve the source’s actual working-tree state.

  • [P2 non-blocking] src/utils/git.ts:1407 — pathExists follows symlinks, so a newly added dangling symlink is recorded as absent. The reset/clean removes it, and restoration treats it as a deletion; no backup exists. Use lstat to distinguish an existing symlink from a missing entry.

All earlier findings are resolved in the current diff.

The description includes a representative real-CLI verification record, so there is no missing-testing finding. No PR code was run, built, or installed.

@SaulMoro

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Thanks. d3774cba closes two of the three carry/restore paths: a failure after the reset, and a failed restore, now keep the backup and print its path. The rename source is carried too. Still open from the review above:

  • --branch main still pushes straight to the default branch. No change in this commit.
  • Self mode, --branch <member's own branch> still publishes the member's unpushed commit. No change.
  • A killed process between the reset and the restore leaves the edits in a teamai-push-carry-* directory under the OS temp dir, and nothing ever names it. git stash push --include-untracked / stash pop would cover this case too, since git stash list survives a crash. It would also handle the two new automated-review points (a replacement file at a rename's source, and a dangling symlink recorded as absent) without carrying files by hand.

@hobostay
hobostay force-pushed the fix/push-reuse-explicit-branch branch from d3774cb to a6c1d08 Compare November 11, 2026 04:15
@github-actions

Copy link
Copy Markdown

No new findings in the current diff. All earlier findings are resolved, including recreated rename sources and dangling symlinks.

The description includes a representative real-CLI verification record, so there is no missing-testing finding. Its older recorded commit is not blocking under the review rules.

Reviewed only the specified diff; no PR code was run, built, or installed.

@hobostay
hobostay force-pushed the fix/push-reuse-explicit-branch branch from a6c1d08 to 926fc9e Compare November 11, 2026 04:19
@hobostay

Copy link
Copy Markdown
Contributor Author

Addressed at 926fc9e:

  • --branch naming the team repo's default branch is refused before any payload is copied. In self mode, a --branch naming the branch checked out in the member's checkout is refused as well, so the push cannot publish their unpushed commits.
  • A staged rename's replacement source and a dangling symlink are carried correctly (real-git tests for both).
  • The carry directory is logged before the reset, and a failed restore keeps it and prints its path. I kept the file-copy carry rather than a stash push/pop because the destination may hold a different version of the same file: the copy overwrites it deliberately, where a stash pop would conflict on exactly the payload this fix exists to move.

Tests at this head: 212 across the five push suites, including refusals for the default branch and the self-mode checkout; typecheck and lint pass.

@github-actions

Copy link
Copy Markdown

No new findings in the specified diff. All earlier findings are resolved, including recreated rename sources, dangling symlinks, and backup retention after restoration failures.

The PR description includes a representative real-CLI verification record, so there is no missing-testing finding. Its older recorded commit is not blocking under the trusted review rules.

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.

926fc9e1. The default-branch refusal is right. So is the reason for keeping the file-copy carry over a stash: a pop would conflict on exactly the payload this moves. With the carry directory now named in debug.log before the reset, I'm fine with the crash path.

One blocker left. [P1 blocking] The self-mode check only compares --branch with the branch checked out right now. In the case I reproduced last round, the member is on main and feature/mine holds an unpushed commit. teamai push --all --branch feature/mine passes this check, checkoutExplicitBranch reuses feature/mine in the throwaway worktree, and origin/feature/mine ends up with the member's commit. The member's checkout is only one way to hold their work; in self mode every local branch is theirs.

The refusal belongs where the branch is reused, not where it's named. In self mode, when a local <name> exists, refuse if it has any commit that's not on origin (git rev-list --count <name> --not --remotes=origin > 0). Name the branch and how many commits it holds. A <name> with no local branch, or one that matches origin, can go ahead as it does now. A row in the self-mode push tests with the member on main and an unpushed commit on the named branch would pin it.

@hobostay
hobostay force-pushed the fix/push-reuse-explicit-branch branch from 926fc9e to d40bde3 Compare November 11, 2026 04:35
@github-actions

Copy link
Copy Markdown

No new findings in the specified diff. All earlier findings are resolved, including mixed config/resource checkout collisions, unpublished commits, remote advancement, rename handling, dangling symlinks, and backup retention after restoration failures.

The PR description includes a representative real-CLI verification record, so there is no missing-testing finding. Its older recorded commit is not blocking under the trusted review rules.

Reviewed only the specified diff against the base-version review criteria. 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.

Approving d40bde31. The self-mode check now runs on the named branch itself: an existing local <name> with any commit not on origin is refused, and an unreadable count is refused too. That covers the member-on-main case from the earlier rounds. With the default-branch refusal and the carry fixes, every blocking point from the earlier rounds is resolved.

  • [P3 nit] The new self-mode test mocks git.raw. A real-git row in team-ref.test.ts would prove the rev-list count against an actual unpushed commit. There's also a stray whitespace-only line after that test.
  • One side effect worth knowing: in self mode, a destination left behind by an earlier push that committed but never reached origin is now refused too. It's still the safe side, and the message tells the member what to do.

I'll merge once CI is green.

An explicit `teamai push --branch <name>` passed the name to
pushRepoBranch, whose non-reuse path ran `git checkout -b`. A branch an
earlier push left locally made that fail with "a branch named ... already
exists"; when only origin had the branch, the new commit was based on the
default branch and the push was rejected non-fast-forward.

Prepare the named destination before callers copy the payload, so the
checkout cannot collide with a file the destination already holds. Pending
config or env edits are carried aside and restored: a replacement at a
renamed source, a dangling symlink, and a failed restore's backup are all
preserved, and the backup path is logged before the reset so a killed
process leaves a trace. Fetch origin, fast-forward a local branch with no
unpushed commit, rebase one that has them, and create a missing branch from
origin. On a retry whose tree holds nothing new, publish the unpushed
commit instead of deleting the destination.

Refuse a destination that is the default branch. In self mode, refuse a
local destination branch holding any commit not on origin: every local
branch there is the member's, so reusing it would publish their work.

Tests: unit cases for both destinations and both refusals, real-git cases
for a replaced file, a pending edit, staged renames (including a recreated
source), a dangling symlink, an unpushed retry, an origin advance, and a
self-mode refusal against a real unpushed commit, plus a real-CLI
before/after on e30376d and this head.

Co-Authored-By: Claude Code <[email protected]>
@hobostay
hobostay force-pushed the fix/push-reuse-explicit-branch branch from d40bde3 to 791cfad Compare November 11, 2026 05:09
@hobostay

Copy link
Copy Markdown
Contributor Author

Addressed the P3 note at 791cfad:

  • Added a real-git row in team-ref.test.ts: a self-mode destination branch with an actual unpushed commit refuses, a clone-mode destination is allowed, and the default branch refuses.
  • Removed the stray whitespace-only line.
  • The guard moved into explicitBranchRefusal() so the real-git test exercises the same count push() uses; behaviour is unchanged.

All five push suites pass (213 tests), plus typecheck, lint and build. The earlier approval was at d40bde3; this commit adds the real-git row and the extraction only.

@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.

Approving 791cfadb. explicitBranchRefusal is a straight extraction of the two checks, with the same messages and order. The new real-git row covers the member on main with an unpushed commit on feature/mine, the same branch outside self mode, and the default branch. I'll merge once CI is green.

@github-actions

Copy link
Copy Markdown

No new findings in the specified diff. All earlier findings are resolved.

The PR description includes a representative real-CLI verification record, so there is no missing-testing finding. Its older recorded commit is not blocking under the trusted review rules.

Reviewed against the base-version Code Review Rules. No PR code was run, built, or installed.

@SaulMoro
SaulMoro merged commit 03099c9 into Tencent:main Nov 11, 2026
20 checks passed
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