The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(usage-window): never retry another chat from the TUI limit card's model switch
Teknium6341ef329636
diff --git a/ui-tui/src/__tests__/createGatewayEventHandler.usageLimit.test.ts b/ui-tui/src/__tests__/createGatewayEventHandler.usageLimit.test.tsindex d72164b4410..165697cbc2d 100644--- a/ui-tui/src/__tests__/createGatewayEventHandler.usageLimit.test.ts+++ b/ui-tui/src/__tests__/createGatewayEventHandler.usageLimit.test.ts@@ -6,6 +6,35 @@ import { getUiState, patchUiState } from '../app/uiStore.js'const ref = <T>(current: T) => ({ current })+const handlerCtx = (+ request: (method: string, params?: Record<string, unknown>) => Promise<unknown>,+ submit: () => void+) =>+ ({+ composer: {+ dequeue: () => undefined,+ queueEditRef: ref<null | number>(null),+ sendQueued: vi.fn(),+ setInput: vi.fn()+ },+ gateway: {+ gw: { isCanonical: true, request },+ rpc: (method: string, params?: Record<string, unknown>) => request(method, params)+ },+ session: {+ STARTUP_RESUME_ID: '',+ colsRef: ref(80),+ newSession: vi.fn(),+ resetSession: vi.fn(),+ resumeById: vi.fn(),+ setCatalog: vi.fn()+ },+ submission: { submitRef: { current: submit } },+ system: { bellOnComplete: false, sys: vi.fn() },+ transcript: { appendMessage: vi.fn(), panel: vi.fn(), setHistoryItems: vi.fn() },+ voice: { setProcessing: vi.fn(), setRecording: vi.fn(), setVoiceEnabled: vi.fn() }+ }) as any+const limitEvent = {payload: {error_surface: {@@ -45,30 +74,7 @@ describe('the usage-limit card on a canonical connection', () => {throw Object.assign(new Error('use_session_mutation_model'), { data: { reason: 'use_session_mutation_model' } })})- const ctx = {- composer: {- dequeue: () => undefined,- queueEditRef: ref<null | number>(null),- sendQueued: vi.fn(),- setInput: vi.fn()- },- gateway: {- gw: { isCanonical: true, request },- rpc: (method: string, params?: Record<string, unknown>) => request(method, params)- },- session: {- STARTUP_RESUME_ID: '',- colsRef: ref(80),- newSession: vi.fn(),- resetSession: vi.fn(),- resumeById: vi.fn(),- setCatalog: vi.fn()- },- submission: { submitRef: { current: submit } },- system: { bellOnComplete: false, sys: vi.fn() },- transcript: { appendMessage: vi.fn(), panel: vi.fn(), setHistoryItems: vi.fn() },- voice: { setProcessing: vi.fn(), setRecording: vi.fn(), setVoiceEnabled: vi.fn() }- } as any+ const ctx = handlerCtx(request, submit)createGatewayEventHandler(ctx)(limitEvent as any)getOverlayState().confirm!.onConfirm()@@ -85,4 +91,30 @@ describe('the usage-limit card on a canonical connection', () => {)expect(getUiState().info?.model).toBe('free/model')})+ it('applies nothing to, and never retries, a chat the user opened while the switch was pending', async () => {+ patchUiState({ info: { model: 'paid/model' } as any, sid: 'sess-1' })+ const submit = vi.fn()+ let commit!: () => void++ const request = vi.fn(async (method: string, params?: Record<string, unknown>) => {+ if (method === 'session.resume') {+ return { execution_generation: 2, revision: 4 }+ }++ await new Promise<void>(resolve => (commit = resolve))++ return { execution_generation: 2, model: (params?.payload as { model: string }).model, status: 'applied' }+ })++ createGatewayEventHandler(handlerCtx(request, submit))(limitEvent as any)+ getOverlayState().confirm!.onConfirm()+ await vi.waitFor(() => expect(request).toHaveBeenCalledWith('session.mutate', expect.anything()))++ patchUiState({ info: { model: 'other/model' } as any, sid: 'sess-2' })+ commit()+ await new Promise(resolve => setTimeout(resolve, 0))++ expect(submit).not.toHaveBeenCalled()+ expect(getUiState().info?.model).toBe('other/model')+ })})diff --git a/ui-tui/src/app/createGatewayEventHandler.ts b/ui-tui/src/app/createGatewayEventHandler.tsindex c4c4c2c55df..c435d29b02f 100644--- a/ui-tui/src/app/createGatewayEventHandler.ts+++ b/ui-tui/src/app/createGatewayEventHandler.ts@@ -779,6 +779,11 @@ function confirmUsageLimit(usageLimit: UsageLimit, ctx: GatewayEventHandlerContevalue: dialog.switchValue}).then(r => {+ // `/retry` targets the active chat: never rewind one the user opened meanwhile.+ if (getUiState().sid !== sid) {+ return+ }+if (!r?.value) {return sys('error: model switch failed')}diff --git a/ui-tui/src/app/slash/canonicalSessionControls.ts b/ui-tui/src/app/slash/canonicalSessionControls.tsindex ff22c5d03c1..5c57b1d3157 100644--- a/ui-tui/src/app/slash/canonicalSessionControls.ts+++ b/ui-tui/src/app/slash/canonicalSessionControls.ts@@ -225,17 +225,18 @@ export function openModelConfirmation(detail: string | undefined, target: string})}+/** `baseline` is the session info the control started from: an epoch change or a newer turn wins. */function isSupersededControl(operation: 'model' | 'branch' | 'compress',result: CanonicalControlResult,expectedGeneration: number,- ctx: SlashRunCtx+ baseline: ReturnType<typeof getUiState>['info']) {const current = getUiState().inforeturn (operation !== 'branch' &&- (current?.execution_epoch !== ctx.ui.info?.execution_epoch ||+ (current?.execution_epoch !== baseline?.execution_epoch ||(current?.execution_generation ?? 0) > (result.execution_generation ?? expectedGeneration)))}@@ -296,7 +297,9 @@ function applyModelResult(result: CanonicalControlResult, sys: (text: string) =>/** A model switch made outside `/model` (a window plan's limit card): the same revision-fenced* `session.mutate` and guarded-target ask. `onApplied` runs only once the switch committed now,- * never for a refusal, a declined ask, or a pick deferred behind a running turn. */+ * never for a refusal, a declined ask, or a pick deferred behind a running turn. Like `/model`, a+ * reply that lands after the view moved to another chat (or a newer turn ran) applies nothing:+ * `onApplied` re-sends via the active chat, so it must still be `sid`. */export async function switchCanonicalModel(gw: MutationGateway,sid: string,@@ -305,9 +308,11 @@ export async function switchCanonicalModel(onApplied: () => void,confirm?: string): Promise<void> {- const mutation = await mutateCanonicalSession(gw, sid, 'model', arg, undefined, confirm)+ const baseline = getUiState().info+ const stale = () => getUiState().sid !== sid+ const mutation = await mutateCanonicalSession(gw, sid, 'model', arg, stale, confirm)- if (!mutation) {+ if (!mutation || stale() || isSupersededControl('model', mutation.result, mutation.expectedGeneration, baseline)) {return}@@ -399,7 +404,7 @@ export async function runCanonicalSessionControl(return}- if (isSupersededControl(operation, result, expectedGeneration, ctx)) {+ if (isSupersededControl(operation, result, expectedGeneration, ctx.ui.info)) {return}