Skip to content

fix(push): give every push branch a name nothing has yet - #1759

Merged
SaulMoro merged 1 commit into
Tencent:mainfrom
Walternater:fix/remove-branch-collision
Nov 6, 2026
Merged

SaulMoro merged 1 commit into
Tencent:mainfrom
Walternater:fix/remove-branch-collision

Conversation

@Walternater

Copy link
Copy Markdown
Collaborator

Problem

Two teamai remove runs within the same second fail the second one:

✖ Git push failed: fatal: a branch named 'teamai/push/TeamAI-CI/20261106-065036' already exists

generateBranchName (src/utils/git.ts:1219) stamps a name to the second, and pushRepoBranch leaves that branch behind on its success path — checkoutMaster only switches away. So the second run's git.checkoutLocalBranch(<same name>) throws.

The repo already has unusedBranchName for exactly this. Its doc comment says it: "Names are per second: a push that opens two PRs, or follows another within the second, would otherwise reuse one and fail." But only push.ts:917 and promotion-publish.ts used it. Four call sites that create a push branch did not.

This is what made git-exclude-delivered-915.test.ts fail on CI shard 3/3 of #1756: that case removes a skill and then a rule, back to back.

Change

The four call sites now use unusedBranchName, which returns the generated name or the first free -2, -3… of it:

  • src/remove.ts — teamai remove
  • src/manifest-edit.ts — roles/projects add, update and remove
  • src/push.ts (pushTeamConfigOnly) — a push whose only change is teamai.yaml
  • src/utils/git.ts (autoPushViaMR) — the import flows

Each already had the repo path in scope, and each is already async.

Test mocks that replace ../utils/git.js gained unusedBranchName; manifest-edit.test.ts spreads the real module, so it needed the entry to stop the real one running against a fake path.

Tests

New, in remove-command.test.ts — the removal pushes onto whatever free name unusedBranchName answers, not the second-stamped one:

gitMocks.unusedBranchName.mockResolvedValueOnce('teamai/push/alice/1-2');
await remove('agents', ['vr'], { force: true });
expect(gitMocks.unusedBranchName).toHaveBeenCalledWith('/tmp/team-repo', 'alice');
expect(gitMocks.pushRepoBranch.mock.calls[0]?.[3]).toBe('teamai/push/alice/1-2');

Without the change the case fails — the name comes from generateBranchName and unusedBranchName is never called:

× pushes onto the free branch name, not the one a removal in the same second already took
AssertionError: expected "vi.fn()" to be called with arguments: [ '/tmp/team-repo', 'alice' ]

unusedBranchName's own behaviour — the free name, the -2/-3 suffix, and falling back when git cannot list branches — is already covered in git.test.ts.

Real CLI: the E2E file CI failed on, run whole against the built CLI:

$ npm run test:e2e -- src/__tests__/e2e/git-exclude-delivered-915.test.ts
Test Files  1 passed (1)
     Tests  54 passed (54)
  Duration  208.39s

Push, remove, rules, manifest, roles, import, init and git unit tests: 820 passed across 37 files. tsc --noEmit and npm run lint pass.

Related Issues

Fixes #1758

Limitations

  • The name is chosen from the local and last-fetched origin refs, so it is as fresh as this clone's last fetch. A branch another machine pushed in the same second and this clone has never fetched could still collide; that is the same window push.ts has always had.
  • generateBranchName stays exported and unchanged — unusedBranchName calls it.

🤖 Generated with Claude Code

Two `teamai remove` runs within one second fail the second one:

    ✖ Git push failed: fatal: a branch named 'teamai/push/…' already exists

`generateBranchName` stamps the name to the second, and `pushRepoBranch`
leaves that branch behind on its success path — `checkoutMaster` only
switches away. So the second run's `checkoutLocalBranch` throws.

`unusedBranchName` exists for exactly this, and its comment says so, but
only `push.ts` and `maintenance/promotion-publish.ts` used it. The four
other call sites that create a push branch — `remove`, `manifest-edit`,
the team-config push and `autoPushViaMR` — now use it too.

This is what made `git-exclude-delivered-915.test.ts` fail on CI shard
3/3 of Tencent#1756: that case removes a skill and then a rule.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

No findings in the scoped diff.

The changes consistently replace generated branch names with unusedBranchName at the four call sites and update the affected test mocks.

The PR description documents sufficient testing: one representative built-CLI E2E run (54 tests passed), targeted unit tests, type checking, and lint. No additional provider × agent matrix is required.

I only read the requested diff; I did not run, build, or install anything.

@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 1d27f7fc. It fixes #1758. We built this head, the merge-base and current main, and froze the CLI clock so two teamai remove runs fell in the same second. On the merge-base and on main, the second run exits 1 with a branch named 'teamai/push/<user>/<stamp>' already exists. On this head both runs exit 0, on <stamp> and <stamp>-2. Main still has the four call sites that use generateBranchName directly, so main does not already fix this. No other open PR touches these files.

The exit code changes only for a run that used to fail, so we read it as a bug fix, not a breaking change.

Non-blocking:

  • [P2 non-blocking] There is no CHANGELOG.md line under Bug Fixes. Recent fixes add one, and this one is visible to anyone who runs two removals or role edits back to back.
  • [P3 nit] The new test comment at src/__tests__/remove-command.test.ts:286 cites #1740, an unrelated benchmark PR. #1756 is probably the one meant.

Only remove was run on the built CLI. The roles/projects edits, the teamai.yaml-only push and the import flows changed the same way and are covered by unit tests.

The CI review (#1759 (comment)) reports no findings. We agree. CI is green at this head.

@SaulMoro
SaulMoro merged commit e4f36ac into Tencent:main Nov 6, 2026
18 checks passed
Walternater added a commit to Walternater/teamai-cli that referenced this pull request Nov 6, 2026
…sue in its test

SaulMoro's review of Tencent#1759 asked for both: the Bug Fixes line recent fixes
carry, and the test comment cited Tencent#1740, an unrelated benchmark PR.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@Walternater

Copy link
Copy Markdown
Collaborator Author

Both non-blocking points are in e6caaea0.

[P2] CHANGELOG — added under Unreleased → 🐛 Bug Fixes, naming the four call sites and the -2/-3 suffix:

A second push within the same second no longer fails on a branch name the first one left behind. … They now take the generated name or the first free -2, -3… of it, as teamai push already did (for #1758).

I put it under Bug Fixes rather than Breaking Changes, matching your reading that the exit code only changes for a run that used to fail.

[P3] the comment — right, #1740 is the benchmark PR. It now cites #1758, the issue that describes the failure.

CI is green again at e6caaea0 (17 passing, 1 skipped).

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.

A second teamai remove in the same second fails with "a branch named … already exists"

3 participants