This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix race condition in resolveAutoModeEndpoint causing duplicate telem… #5027
Open
abadawi591
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
abadawi591:abadawi/fix-automode-resolution-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2aeea5
Fix race condition in resolveAutoModeEndpoint causing duplicate telem…
abadawi591 8c48421
Guard pending resolution cleanup against stale map entries
abadawi591 f8a5355
Use property assertions instead of referential equality in coalescing…
abadawi591 66897f7
Filter CAPI call assertion to AutoModels request type
abadawi591 59875e8
Extract coalescing into synchronous _singleFlight method
abadawi591 e315b72
Fix type error in CAPI call filter assertion
abadawi591 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, shouldn't the real test here be multiple different prompts being asked? Concurrent calls with the same conversationId only breaks our logic if they result in different answers.
My confusion may stem from my lack of understanding on what the
mockApiResponse(['gpt-4o', 'gpt-4o-mini']);line doesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! The concurrent calls in the real bug all carry the same prompt and same
chatRequest.they're the same user message being resolved multiple times within a single tool-calling round (for getAvailableTools, buildPrompt, token counting, fetch). They're not different user messages arriving at the same time.
So the test is accurate to the real scenario: same chatRequest, same sessionId, same prompt, fired concurrently.
To clarify mockApiResponse(['gpt-4o', 'gpt-4o-mini']): it stubs the CAPI token endpoint to return those as available_models. It controls which models the auto-mode service can choose from — it's not the router response (that would be mockRouterResponse).
Different prompts within the same conversation are already handled by the skipRouter cache (turnCount > 0 → skip), which is unrelated to this fix. This PR only addresses the race window on the first resolution of a conversation.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dug a little deeper into this one 👇
Duplicate
automode.routerFallbacktelemetry from concurrentgetChatEndpointcallsSymptom
automode.routerFallbackfires 5× per routing decision instead of once. Bursts appear at the same sub-second timestamp, inflating telemetry counts and causing redundant CAPI token fetches.Cause
toolCallingLoop.tscallsgetChatEndpoint(request)up to 5 times per tool-calling round (for tools, prompt, tokenizer, fetch). On the first resolution of a conversation, these concurrent async calls all see an empty_autoModelCache, all enter_tryRouterSelection, and all emit telemetry before any of them writes the cache.This pattern dates back to the initial repo when
getChatEndpointwas cheap and stateless. As auto-mode features were layered on, existing cross-turn dedup never covered the within-turn concurrent race:Minimal fix (this PR)
Promise coalescing in
resolveAutoModeEndpoint: if a resolution is already in-flight for aconversationId, subsequent callers share the existing promise instead of starting a new one.Proper fix (separate PR)
Resolve the endpoint once at the top of
runOne()and pass it down togetAvailableTools,buildPrompt, andfetch. Seeabadawi/resolve-endpoint-once-per-round. This was attempted in5388442bf(added_cachedEndpointForTurntoToolCallingLoop) but reverted in favor of dedup insideautomodeService. Now implemented as a standalone refactor.cc @lramos15 @TylerLeonhardt — adding you both since you've touched this area extensively. Would appreciate a sanity check on these findings.