Skip to content

perf(/branch): batch unique-title lookup in computeUniqueBranchTitle to avoid up to 99 session scans #3961

@qqqys

Description

@qqqys

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:

  1. Add a SessionService helper that returns all titles matching prefix <base> (Branch (one scan).
  2. 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

  • computeUniqueBranchTitle() issues at most one SessionService query per call.
  • Existing unit tests in useBranchCommand.test.ts still pass; add a regression test for high-collision case (e.g. 50 pre-existing (Branch N) siblings) asserting a single query.
  • No behavior change in the chosen suffix vs. current implementation.

Source

Raised by gpt-5.5 via /review on #3539: #3539 (comment)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions