Follow-up from #3539 review.
Problem
computeUniqueBranchTitle() in packages/cli/src/ui/hooks/useBranchCommand.ts:83 resolves (Branch N) collisions by calling SessionService.findSessionsByTitle() once per candidate suffix. That helper rescans the project's session files on every call, so on the interactive /branch path it can do up to 99 full scans (Branch 2 through Branch 100) when many existing chats share the same base name. Visibly stalls the command in projects with heavy session history.
Proposed fix
Replace the loop with a single batched/prefix lookup:
- Add a
SessionService helper that returns all titles matching prefix <base> (Branch (one scan).
- In
computeUniqueBranchTitle(), parse the returned titles to extract used suffix numbers, then pick the smallest free N in memory.
Net effect: one filesystem scan per /branch invocation regardless of collision depth.
Acceptance
Source
Raised by gpt-5.5 via /review on #3539: #3539 (comment)
Follow-up from #3539 review.
Problem
computeUniqueBranchTitle()inpackages/cli/src/ui/hooks/useBranchCommand.ts:83resolves(Branch N)collisions by callingSessionService.findSessionsByTitle()once per candidate suffix. That helper rescans the project's session files on every call, so on the interactive/branchpath it can do up to 99 full scans (Branch 2throughBranch 100) when many existing chats share the same base name. Visibly stalls the command in projects with heavy session history.Proposed fix
Replace the loop with a single batched/prefix lookup:
SessionServicehelper that returns all titles matching prefix<base> (Branch(one scan).computeUniqueBranchTitle(), parse the returned titles to extract used suffix numbers, then pick the smallest freeNin memory.Net effect: one filesystem scan per
/branchinvocation regardless of collision depth.Acceptance
computeUniqueBranchTitle()issues at most oneSessionServicequery per call.useBranchCommand.test.tsstill pass; add a regression test for high-collision case (e.g. 50 pre-existing(Branch N)siblings) asserting a single query.Source
Raised by
gpt-5.5via/reviewon #3539: #3539 (comment)