Skip to content

Back off token recount selection retries#377

Open
baron wants to merge 1 commit into
mainfrom
wt/token-count-retry-backoff
Open

Back off token recount selection retries#377
baron wants to merge 1 commit into
mainfrom
wt/token-count-retry-backoff

Conversation

@baron

@baron baron commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add a one-shot, capped retry backoff for token recounts when selection changes during a heavy token calculation
  • Reset retry backoff on fresh selection projection changes and after a consistent recount
  • Add DEBUG-only coverage for retry delay gating, cap, and reset behavior

Validation

  • .agents/skills/rpce-contribution-check/scripts/preflight.sh commit
  • .agents/skills/rpce-contribution-check/scripts/preflight.sh push
  • swiftformat --config .swiftformat --lint Sources/RepoPrompt/Features/Prompt/ViewModels/TokenCountingViewModel.swift Tests/RepoPromptTests/MCP/MCPSelectionReplyFreshnessTests.swift
  • swiftlint lint --strict --config .swiftlint.yml Sources/RepoPrompt/Features/Prompt/ViewModels/TokenCountingViewModel.swift Tests/RepoPromptTests/MCP/MCPSelectionReplyFreshnessTests.swift

Notes:

  • make dev-lint was attempted but failed in the full-repo SwiftFormat lint phase with an unrelated environmentEntry rule timed out error in Sources/RepoPrompt/Infrastructure/WorkspaceContext/Models/WorkspaceCodemapLiveOverlayModels.swift.
  • Focused XCTest compile was previously attempted but did not reach test execution before being canceled due a very long fresh worktree compile.

Comment on lines +286 to +293
shouldUseSelectionConsistencyRetryBackoffForNextSelectionRecount = false
let debounceNanoseconds = tokenCountUpdateDebounceNanoseconds(
useSelectionConsistencyRetryBackoff: usesSelectionConsistencyRetryBackoff
)
tokenUpdateDebounceTask?.cancel()
tokenUpdateDebounceTask = Task { @MainActor [weak self] in
do {
try await Task.sleep(nanoseconds: Self.tokenUpdateDebounceNanoseconds)
try await Task.sleep(nanoseconds: debounceNanoseconds)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The selection consistency retry backoff can be canceled by concurrent non-selection dirty events (e.g., prompt text changes), defeating the purpose of the backoff delay.
Severity: MEDIUM

Suggested Fix

The shouldUseSelectionConsistencyRetryBackoffForNextSelectionRecount flag should not be reset until the backoff-delayed task actually begins execution. Alternatively, when scheduleTokenCountUpdateIfNeeded() is called, it should check if the pending tokenUpdateDebounceTask is a backoff task and avoid canceling it for non-critical dirty events.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
Sources/RepoPrompt/Features/Prompt/ViewModels/TokenCountingViewModel.swift#L286-L293

Potential issue: A race condition exists where the selection consistency retry backoff
is defeated. When a retry is scheduled, the
`shouldUseSelectionConsistencyRetryBackoffForNextSelectionRecount` flag is consumed and
a long debounce task is started. If a concurrent non-selection event (e.g., prompt text
change) triggers `markDirty()` before the backoff task executes, the pending backoff
task is canceled and replaced with a standard, shorter debounce task. This undermines
the feature's goal of reducing rapid recounts during periods of instability, leading to
degraded performance.

Did we get this right? 👍 / 👎 to inform future reviews.

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.

1 participant