Repository navigation
fix(chat-runtime): keep the picked model and effort when a plan approval clears the context #9682
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
f9dd246
cf0992f
b7ae76c
f2cd816
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 |
|---|---|---|
|
|
@@ -203,6 +203,8 @@ export type AcpImageStoreContext = { | |
|
|
||
| const STARTUP_TIMEOUT_MS = 120_000; | ||
| const START_OPTIONS_GRACE_MS = 1_000; | ||
| // claude-agent-acp reports the default or settings model for the fresh context these plan approvals start, whatever it runs. | ||
| const CONTEXT_RESET_OPTION = /^exit-plan-clear-/; | ||
| const REQUEST_TIMEOUT_MS = 30_000; | ||
| const STDERR_TAIL_CHARS = 4000; | ||
| const ASK_MODE_ID = "default"; | ||
|
|
@@ -467,6 +469,7 @@ export class AcpAdapter implements HarnessAdapter { | |
| }, | ||
| ); | ||
| private configOptions: SessionConfigOption[] = []; | ||
| private picksBeforeContextReset: ChatSelections | null = null; | ||
| private replaying = false; | ||
| private agentName: string | null = null; | ||
| private quietReplay = false; | ||
|
|
@@ -624,10 +627,11 @@ export class AcpAdapter implements HarnessAdapter { | |
| return; | ||
| } | ||
| this.pendingApprovals.delete(approvalId); | ||
| this.client.respond( | ||
| pending.requestId, | ||
| this.approvalOutcome(pending, decision), | ||
| ); | ||
| const outcome = this.approvalOutcome(pending, decision); | ||
| if (CONTEXT_RESET_OPTION.test(selectedOptionId(outcome) ?? "")) { | ||
| this.picksBeforeContextReset = this.modelAndEffort(); | ||
| } | ||
| this.client.respond(pending.requestId, outcome); | ||
| if (this.currentTurn?.status !== "running") this.sendCustomAnswer(); | ||
| this.emitItem( | ||
| { | ||
|
|
@@ -708,6 +712,12 @@ export class AcpAdapter implements HarnessAdapter { | |
| const sessionId = this.sessionId; | ||
| if (!client || !sessionId) return Promise.resolve(false); | ||
| const previous = this.configOptions; | ||
| const changed = previous.find((option) => option.id === configId); | ||
| const category = changed && selectionCategoryOf(changed); | ||
| if (this.picksBeforeContextReset && category === "model") | ||
| this.picksBeforeContextReset.modelId = value; | ||
| if (this.picksBeforeContextReset && category === "thought_level") | ||
| this.picksBeforeContextReset.effortId = value; | ||
| const previousModeId = | ||
| configId === this.modeConfigId ? this.modeId : undefined; | ||
| this.configOptions = previous.map((option) => | ||
|
|
@@ -1218,6 +1228,7 @@ export class AcpAdapter implements HarnessAdapter { | |
| usage?: AcpPromptUsage, | ||
| ): void { | ||
| this.endCompactions(); | ||
| this.picksBeforeContextReset = null; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| this.main.flush(); | ||
| this.emitTurn({ | ||
| id: turnId, | ||
|
|
@@ -1246,6 +1257,7 @@ export class AcpAdapter implements HarnessAdapter { | |
| private failTurn(turnId: string, error: TurnError): void { | ||
| this.endCompactions(); | ||
| this.customAnswer = null; | ||
| this.picksBeforeContextReset = null; | ||
| this.main.flush(); | ||
| this.cancelPendingApprovals(turnId); | ||
| this.emitTurn({ | ||
|
|
@@ -1397,6 +1409,7 @@ export class AcpAdapter implements HarnessAdapter { | |
| return; | ||
| case "config_option_update": | ||
| this.handleConfigOptions(outer.data.update); | ||
| this.restorePicksAfterContextReset(); | ||
| return; | ||
| case "state_update": | ||
| this.handleStateUpdate(outer.data.update); | ||
|
|
@@ -1991,6 +2004,28 @@ export class AcpAdapter implements HarnessAdapter { | |
| } | ||
| } | ||
|
|
||
| private modelAndEffort(): ChatSelections { | ||
| const current = (category: SelectionCategory) => | ||
| this.configOptions.find( | ||
| (option) => selectionCategoryOf(option) === category, | ||
| )?.currentValue; | ||
| const modelId = current("model"); | ||
| const effortId = current("thought_level"); | ||
| return { | ||
| ...(modelId ? { modelId } : {}), | ||
| ...(effortId ? { effortId } : {}), | ||
| }; | ||
| } | ||
|
|
||
| private restorePicksAfterContextReset(): void { | ||
| const picks = this.picksBeforeContextReset; | ||
| if (!picks) return; | ||
| this.picksBeforeContextReset = null; | ||
| const now = this.modelAndEffort(); | ||
| if (now.modelId !== picks.modelId || now.effortId !== picks.effortId) | ||
| void this.applyConfigSelections(picks); | ||
|
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. Do not overwrite a newer effort choice while restoring the model. If restoring Sonnet is still pending when the user successfully selects high effort, Before sending the deferred effort RPC, check whether a newer user effort choice superseded the saved value. If no newer choice exists, continue restoring the saved effort after the model is available.
|
||
| } | ||
|
|
||
| private startModeId( | ||
| startOptions: HarnessStartOptions, | ||
| resumed: boolean, | ||
|
|
@@ -2837,6 +2872,12 @@ function listedSessions(response: unknown): DiscoverSessionsResult { | |
| }; | ||
| } | ||
|
|
||
| function selectedOptionId(outcome: unknown): string | undefined { | ||
| const optionId = (outcome as { outcome?: { optionId?: unknown } }).outcome | ||
| ?.optionId; | ||
| return typeof optionId === "string" ? optionId : undefined; | ||
| } | ||
|
|
||
| function flattenSelectOptions( | ||
| entries: AcpConfigSelectOption[], | ||
| labelOf: (name: string) => string, | ||
|
|
||
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.
Do not restore a selection that the agent rejected.
If the user selects Opus during reset and the agent rejects it,
setConfigOptionrestores Sonnet locally and returnsfalse. The snapshot still holds Opus. When the fresh context reports Default, the adapter sends Opus again instead of restoring Sonnet.On a definite rejection, roll back the corresponding snapshot field if it still belongs to that request. Keep newer choices intact. Preserve the existing behavior for timeouts, which do not confirm rejection.