Skip to content

fix(git): sweep untracked leftovers in resetToCleanMaster - #1253

Merged
SaulMoro merged 1 commit into
Tencent:mainfrom
hobostay:fix/reset-clean-untracked
Oct 21, 2026
Merged

SaulMoro merged 1 commit into
Tencent:mainfrom
hobostay:fix/reset-clean-untracked

Conversation

@hobostay

Copy link
Copy Markdown
Contributor

Problem

resetToCleanMaster (src/utils/git.ts) counts untracked files as dirty (status.not_added feeds isDirty, and the debug line even prints "N untracked"), but git reset --hard HEAD leaves untracked files untouched — so the team repo's designated self-heal never actually sweeps them. pushRepoBranch's own comments ("a skipped switch self-heals via resetToCleanMaster on the next run") promise recovery the function cannot deliver for untracked leftovers.

An untracked leftover then breaks the steps the reset precedes:

  • Branch switch: switchToDefaultBranch → git checkout <default> rejects with error: The following untracked working tree files would be overwritten by checkout when the leftover sits at a path the default branch tracks (e.g. a teammate's merged PR added that path after a crashed push left the clone on a stale push branch). The throw propagates: push reports Pull failed: ..., sets teamRepoStale, and aborts. Nothing ever clears the file — every retry hits the same wall until the user manually runs the git reset --hard && git clean -fd that push.ts's own rollback warning already recommends.
  • Pull: pullRepo's ff-only pull hits the same "would be overwritten by merge" error and detours through its destructive fetch + reset --hard origin/<branch> recovery, clobbering the file mid-flow. The env snapshot-restore in pushCore exists precisely to survive resets like that ([bug] push refuses the env files env add leaves in a standalone clone #881); the pull should not need it.

Every other clean-the-team-repo site already pairs the reset with the sweep: pushRepoBranch (twice), push.ts's failed-push rollback, and pullRepo's recovery even computes its dirty count excluding not_added because it knows reset --hard cannot sweep them. resetToCleanMaster — whose documented contract is "Reset the team repo to a clean default-branch state" — was the one site missing it.

Fix

Mirror the established idiom: when the repo is dirty, run git.clean('f', ['-d']) right after git reset --hard HEAD. Without -x, ignored files stay. Both callers keep their isDedicatedRepoRoot guards, and push.ts's unsafe-dirty gate still refuses unrelated user changes before resetToCleanMaster runs — the sweep only reaches teamai-owned paths (env edits, which pushCore re-writes from its snapshot) and crash residue. The two skill-data passages that say TeamAI "refuses to reset a team-repo clone with unrelated modified, staged, untracked, or conflicted files" stay true.

Testing

  • Unit (mocked): the resetToCleanMaster cases in git.test.ts now assert the clean call, plus a new case for a not_added-only dirty state; the clean-repo case asserts no clean.
  • Unit (real git): new regression test in team-ref.test.ts — a crashed push left the clone on a stale branch whose tree lacks votes/stray.yaml, a later crashed run left that path behind untracked, and upstream tracks it. resetToCleanMaster must resolve, land on main, and leave upstream's content. Fails on main with The following untracked working tree files would be overwritten by checkout.
  • Suite: npx vitest run → 11461 passed, 1 failed (hooks-reconcile-scope.test.ts, the known ENOTDIR artifact of running inside a git worktree — .git is a file there; passes in a plain checkout). npx tsc --noEmit and npm run lint clean.

E2E with the real CLI (dist/index.js, provider git, agent Claude)

Setup: team repo with projects/roles manifests; a teammate's merged PR adds env/backend/env.yaml to main; the member's clone is stuck on a stale teamai/push/... branch cut from pre-env main while local main is up to date; env add left its edit behind untracked at that path; a local skill/rule/agent await push.

Baseline (upstream main, fa6a361):

== pre-run state ==
## teamai/push/e2e-user/20260411-225746
?? env/
== teamai push (real CLI) ==
- Pulling latest changes...
⚠ Pull failed: error: The following untracked working tree files would be overwritten by checkout:
        env/backend/env.yaml
Please move or remove them before you switch branches.
Aborting

✖ Cannot resolve --project destinations: the team repo could not be refreshed, so manifest/projects.yaml may be stale. Fix the pull and retry, or name the namespace with --role <ns>.
== exit: 1 ==
== post-run state ==
HEAD: teamai/push/e2e-user/20260411-225746   # still wedged on the stale branch

This PR:

== teamai push (real CLI) ==
- Pulling latest changes...
✔ Up to date
- Scanning local resources...
Found 4 resource(s) to push:
    1. [skills] my-skill (new) ... to: skills/fe-skills/my-skill
    2. [rules] my-rule (new)
    3. [env] backend/env.yaml (new)
    4. [agents] vr (new)
- Pushing resources...
✔ Pushed branch teamai/push/e2e-user/20261021-125756
== exit: 1 ==  (only the PR step: provider git cannot open a PR for a local bare path — same expectation as the existing push e2e suite; the branch push is the success signal)
== post-run state ==
HEAD: main                                   # self-healed

The pushed branch carries the member's env edit (API_URL: https://mine.example.com, TOKEN: s3cr3t) — verified with git show <branch>:env/backend/env.yaml — so the captured env edit survived the sweep exactly as the #881 snapshot-restore intends, and this time the pull itself was a clean fast-forward instead of the destructive reset-recovery detour.

🤖 Generated with Claude Code

resetToCleanMaster counts untracked files as dirty (status.not_added),
but git reset --hard HEAD leaves them in place. An untracked leftover
therefore survives the team repo's self-heal and breaks the steps it
precedes: git checkout <default> rejects with "untracked working tree
files would be overwritten by checkout" when the leftover sits at a path
the default branch tracks, and the error propagates to push as
"Pull failed", wedging the clone until the user runs the manual
"reset --hard && git clean -fd" that push.ts's rollback message already
recommends.

Mirror the idiom every other clean-the-team-repo site already uses
(pushRepoBranch, push.ts's failed-push rollback): clean -fd after the
reset, still without -x so ignored files stay. Callers keep their
isDedicatedRepoRoot guards, and push.ts's unsafe-dirty gate still
refuses unrelated user changes before the reset runs.

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

Copy link
Copy Markdown

No findings in the specified diff.

The PR description documents sufficient testing: mocked unit tests, a real-Git regression test, and a representative real-CLI run with provider git and agent Claude. The CLI record verifies branch recovery, successful push, and preservation of the env edit; the documented exit code 1 comes from the unsupported PR-creation step against a local bare remote. No additional provider × agent coverage is required.

@SaulMoro
SaulMoro self-requested a review October 21, 2026 07:54

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

The PR adds git clean -fd after reset --hard HEAD in resetToCleanMaster, so an untracked leftover no longer blocks the switch back to the default branch. The earlier bot review reported no findings. I checked both callers. pushCore (src/push.ts:1162) refuses any untracked path except the ones it has already captured and restores afterwards (src/push.ts:1146-1168), so the sweep can only remove paths TeamAI puts back. The other caller (src/team-push.ts:1156) has no such gate. It is not reached in production, though: its only caller (src/pull.ts:3191) always passes selfConfig for non-HTTP repos, which takes the reports-branch path. Without -x, ignored files stay. Nested repos and linked worktrees inside the clone also stay, and the side-branch worktrees live next to the clone, not inside it (src/types.ts:2886-2894).

Local checks, run on the merge of 064a6bd into origin/main 0da2be76 (clean merge):

  • npx tsc --noEmit: OK. npm run lint: exit 0. npm run build: OK.
  • npx vitest run on the touched and neighbouring tests (git, team-ref, push*, team-push*): 326 passed. Full npx vitest run: 11469 passed, 1 failed (hooks-reconcile-scope.test.ts ENOTDIR). That failure also happens on an origin/main worktree, so it comes from the worktree environment, not from this PR.
  • Red/green: the new team-ref test fails on origin/main's git.ts with "untracked working tree files would be overwritten by checkout" and passes with the PR.
  • Real CLI (node dist/index.js push --all, provider git, agent Claude, project scope, throwaway HOME). The clone was stuck on a stale push branch, upstream main tracked env/backend/env.yaml, and the member's edit sat untracked at that path. On origin/main the push printed Pull failed: ... would be overwritten by checkout. With the PR it printed Up to date, pushed the branch carrying the member's env edit, and left HEAD on main. Two safety runs: with an unrelated untracked notes/mine.md, push still refused before resetting and kept the file. With an ignored file (.git/info/exclude), push succeeded and kept the file.

Findings

  • None.

Not covered

  • Providers gitlab/github and agents other than Claude were not run. The change sits below provider and agent code.
  • Windows was not tested.

Verdict: Approve.

@SaulMoro
SaulMoro merged commit 2598121 into Tencent:main Oct 21, 2026
17 checks passed
legend91019 added a commit to legend91019/teamai-cli that referenced this pull request Oct 21, 2026
SaulMoro added a commit that referenced this pull request Oct 26, 2026
…eToTeam (#1293)

reportUsageToTeam now requires selfConfig and always writes through the teamai-reports branch. The legacy branch that reset the team-repo clone had no caller, and since #1253 it could delete untracked files in the clone. An HTTP repo returns false before any report I/O. The updateReports callback keeps returning the files on every attempt, so a retry still pushes a commit left unpushed (#1291).

Closes #1259

Co-authored-by: Saul Moro <[email protected]>
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
resetToCleanMaster counts untracked files as dirty (status.not_added),
but git reset --hard HEAD leaves them in place. An untracked leftover
therefore survives the team repo's self-heal and breaks the steps it
precedes: git checkout <default> rejects with "untracked working tree
files would be overwritten by checkout" when the leftover sits at a path
the default branch tracks, and the error propagates to push as
"Pull failed", wedging the clone until the user runs the manual
"reset --hard && git clean -fd" that push.ts's rollback message already
recommends.

Mirror the idiom every other clean-the-team-repo site already uses
(pushRepoBranch, push.ts's failed-push rollback): clean -fd after the
reset, still without -x so ignored files stay. Callers keep their
isDedicatedRepoRoot guards, and push.ts's unsafe-dirty gate still
refuses unrelated user changes before the reset runs.

Co-authored-by: Claude Code <[email protected]>
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…eToTeam (Tencent#1293)

reportUsageToTeam now requires selfConfig and always writes through the teamai-reports branch. The legacy branch that reset the team-repo clone had no caller, and since Tencent#1253 it could delete untracked files in the clone. An HTTP repo returns false before any report I/O. The updateReports callback keeps returning the files on every attempt, so a retry still pushes a commit left unpushed (Tencent#1291).

Closes Tencent#1259

Co-authored-by: Saul Moro <[email protected]>
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