-
Notifications
You must be signed in to change notification settings - Fork 115
Fix Context Builder model authority hydration #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -529,7 +529,7 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| selectedModel = AgentModel.resolvedModel(forRaw: selectedModelRaw, agentKind: selectedAgent) ?? .defaultModel | ||
| isRestoringState = false | ||
| } | ||
| updateDynamicModelPolling() | ||
| updateDynamicModelPolling(allowUnvalidatedDynamicPolling: true) | ||
| persistAgentModelToEffectiveProfile() | ||
| if let session = activeSession { | ||
| persistSessionConfig(session) | ||
|
|
@@ -1003,7 +1003,10 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| guard let self else { return } | ||
| await handleComposeTabsWillClose(tabIDs) | ||
| } | ||
| updateDynamicModelPolling(startCursorPolling: false) | ||
| updateDynamicModelPolling( | ||
| startCursorPolling: false, | ||
| allowUnvalidatedDynamicPolling: false | ||
| ) | ||
| } | ||
|
|
||
| func prepareForWindowClose() { | ||
|
|
@@ -1040,18 +1043,25 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| } | ||
|
|
||
| private func resolvedPersistedContextBuilderSelection(workspaceID: UUID? = nil) -> AgentModelCatalog.NormalizedAgentSelection? { | ||
| guard let apiSettingsViewModel = promptManager.apiSettingsViewModel, | ||
| apiSettingsViewModel.isContextBuilderProviderValidationComplete | ||
| else { | ||
| guard let apiSettingsViewModel = promptManager.apiSettingsViewModel else { | ||
| return nil | ||
| } | ||
| let profile = settingsManager.effectiveAgentModelsProfile(workspaceID: workspaceID ?? currentWorkspaceID) | ||
| let agentRaw = profile.contextBuilderAgentRaw | ||
| let modelRaw = agentRaw.flatMap { profile.contextBuilderModelsByAgent?[$0] } | ||
| let availability = apiSettingsViewModel.contextBuilderRestorationAvailabilityContext | ||
| guard apiSettingsViewModel.isContextBuilderProviderValidationComplete else { | ||
| return AgentModelCatalog.normalizePersistedSelection( | ||
| agentRaw: agentRaw, | ||
| modelRaw: modelRaw, | ||
| availability: availability, | ||
| codexDynamicModels: codexDynamicModels | ||
| ) | ||
| } | ||
| return AutoRecommendationEngine.resolveContextBuilderSelection( | ||
| persistedAgentRaw: agentRaw, | ||
| persistedModelRaw: modelRaw, | ||
| availability: apiSettingsViewModel.contextBuilderRestorationAvailabilityContext, | ||
| availability: availability, | ||
| enabledRecommendationProviders: settingsManager.globalRecommendationProviderFilter() | ||
| ) | ||
| } | ||
|
|
@@ -1083,21 +1093,34 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| private func handleAgentProviderAvailabilityChanged() { | ||
| refreshAvailableAgents() | ||
| guard let normalized = resolvedPersistedContextBuilderSelection() else { return } | ||
| guard normalized.agent != selectedAgent || normalized.modelRaw.caseInsensitiveCompare(selectedModelRaw) != .orderedSame else { | ||
| return | ||
| let selectionChanged = normalized.agent != selectedAgent || | ||
| normalized.modelRaw.caseInsensitiveCompare(selectedModelRaw) != .orderedSame | ||
| if selectionChanged { | ||
| isRestoringState = true | ||
| selectedAgent = normalized.agent | ||
| selectedModelRaw = normalized.modelRaw | ||
| selectedModel = AgentModel.resolvedModel(forRaw: normalized.modelRaw, agentKind: normalized.agent) ?? .defaultModel | ||
| isRestoringState = false | ||
| } | ||
| isRestoringState = true | ||
| selectedAgent = normalized.agent | ||
| selectedModelRaw = normalized.modelRaw | ||
| selectedModel = AgentModel.resolvedModel(forRaw: normalized.modelRaw, agentKind: normalized.agent) ?? .defaultModel | ||
| isRestoringState = false | ||
| updateDynamicModelPolling() | ||
| updateDynamicModelPolling( | ||
| startCursorPolling: false, | ||
| allowUnvalidatedDynamicPolling: false | ||
| ) | ||
|
Comment on lines
+1105
to
+1108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: After provider validation, Suggested FixIn Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| } | ||
|
|
||
| private func updateDynamicModelPolling(startCursorPolling: Bool = true) { | ||
| updateCodexModelPolling() | ||
| updateOpenCodeModelPolling() | ||
| updateCursorModelPolling(startPolling: startCursorPolling) | ||
| private func updateDynamicModelPolling( | ||
| startCursorPolling: Bool = true, | ||
| allowUnvalidatedDynamicPolling: Bool = false | ||
| ) { | ||
| if allowUnvalidatedDynamicPolling || promptManager.apiSettingsViewModel?.isContextBuilderProviderValidationComplete == true { | ||
| updateCodexModelPolling() | ||
| updateOpenCodeModelPolling() | ||
| updateCursorModelPolling(startPolling: startCursorPolling) | ||
| } else { | ||
| stopCodexModelsSubscription() | ||
| stopOpenCodeModelsSubscription() | ||
| stopCursorModelsSubscription() | ||
| } | ||
| } | ||
|
|
||
| private func updateCodexModelPolling() { | ||
|
|
@@ -1138,6 +1161,26 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| codexModelsSubscriptionTask != nil | ||
| } | ||
|
|
||
| var test_hasCursorModelsSubscriptionTask: Bool { | ||
| cursorModelsSubscriptionTask != nil | ||
| } | ||
|
|
||
| var test_hasOpenCodeModelsSubscriptionTask: Bool { | ||
| openCodeModelsSubscriptionTask != nil | ||
| } | ||
|
|
||
| func test_applyEffectiveAgentModelForLifecycle() { | ||
| applyEffectiveAgentModel() | ||
| } | ||
|
|
||
| func test_handleAgentProviderAvailabilityChangedForLifecycle() { | ||
| handleAgentProviderAvailabilityChanged() | ||
| } | ||
|
|
||
| func test_updateDynamicModelPollingForLifecycle() { | ||
| updateDynamicModelPolling() | ||
| } | ||
|
|
||
| func test_cancelAndDrainCodexModelsSubscription() async { | ||
| let task = codexModelsSubscriptionTask | ||
| task?.cancel() | ||
|
|
@@ -1352,7 +1395,10 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| } | ||
|
|
||
| isRestoringState = false | ||
| updateDynamicModelPolling(startCursorPolling: false) | ||
| updateDynamicModelPolling( | ||
| startCursorPolling: false, | ||
| allowUnvalidatedDynamicPolling: false | ||
| ) | ||
| } | ||
|
|
||
| private func applySessionToBindings(_ session: TabSession) { | ||
|
|
@@ -1381,7 +1427,10 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| contextBuilderInstructions = session.contextBuilderInstructions | ||
| selectedContextBuilderPromptIDs = session.selectedContextBuilderPromptIDs | ||
| isRestoringState = false | ||
| updateDynamicModelPolling(startCursorPolling: false) | ||
| updateDynamicModelPolling( | ||
| startCursorPolling: false, | ||
| allowUnvalidatedDynamicPolling: false | ||
| ) | ||
| } | ||
|
|
||
| private func clearBindings() { | ||
|
|
@@ -1421,7 +1470,10 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| questionTimeoutSeconds = ContextBuilderDefaults.questionTimeoutSeconds | ||
| planTokenBudget = ContextBuilderDefaults.planTokenBudget | ||
| isRestoringState = false | ||
| updateDynamicModelPolling(startCursorPolling: false) | ||
| updateDynamicModelPolling( | ||
| startCursorPolling: false, | ||
| allowUnvalidatedDynamicPolling: false | ||
| ) | ||
| } | ||
|
|
||
| private func updateRuntimeBindings(from session: TabSession) { | ||
|
|
@@ -1448,7 +1500,7 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| // Per-tab selected follow-up type | ||
| selectedFollowUpType = session.selectedFollowUpType | ||
| isRestoringState = false | ||
| updateDynamicModelPolling() | ||
| updateDynamicModelPolling(allowUnvalidatedDynamicPolling: false) | ||
| } | ||
|
|
||
| /// Lightweight binding update for streaming hot path - only updates agentLog and toolCallCount. | ||
|
|
@@ -1585,7 +1637,10 @@ final class ContextBuilderAgentViewModel: ObservableObject { | |
| selectedModel = AgentModel.resolvedModel(forRaw: normalized.modelRaw, agentKind: normalized.agent) ?? .defaultModel | ||
| isRestoringState = false | ||
| refreshAvailableAgents() | ||
| updateDynamicModelPolling(startCursorPolling: false) | ||
| updateDynamicModelPolling( | ||
| startCursorPolling: false, | ||
| allowUnvalidatedDynamicPolling: false | ||
| ) | ||
| } | ||
|
|
||
| /// Load workspace-scoped discovery defaults (token budget, enhancement mode, clarifying questions, plan budget). | ||
|
|
||
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.
Bug: In
selectedAgent.didSet, dynamic model polling is started but then immediately cancelled by a subsequent settings persistence flow that runs on the next run loop.Severity: MEDIUM
Suggested Fix
Refactor the logic to prevent the immediate cancellation of polling. One option is to delay the
persistAgentModelToEffectiveProfile()call or modify theapplyEffectiveAgentModelfunction to not unconditionally stop polling. Alternatively, the state management could be adjusted so that the settings application does not override the intent to start polling.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.