Skip to content

The agent that grows with you

fix(desktop): only the still-current pick retries a stale fence; older refusals never rewind fences

Tekniumbf407a79f959
diff --git a/apps/desktop/src/api/canonical-live-pick.test.ts b/apps/desktop/src/api/canonical-live-pick.test.ts
index 78f5083ce51..58206d86c5c 100644
--- a/apps/desktop/src/api/canonical-live-pick.test.ts
+++ b/apps/desktop/src/api/canonical-live-pick.test.ts
@@ -55,3 +55,29 @@ test('a fence refusal naming fresh fences is retried once at them, never looped'
await expect(refused.request('config.set', pick)).rejects.toThrow('stale_generation')
expect(looping).toHaveLength(2)
})
+
+test('an older pick refused after a newer pick committed is not retried and never rewinds the fences', async () => {
+ let refuseA: (error: unknown) => void = () => {}
+
+ const { calls, client } = await attached(params => {
+ const model = (params.payload as { model: string }).model
+
+ if (model === 'a' && calls.filter(call => (call.params.payload as { model: string }).model === 'a').length === 1) {
+ return new Promise((_resolve, reject) => { refuseA = reject })
+ }
+
+ return { session_id: 's', operation: 'model', model, revision: 7, execution_generation: 4 }
+ })
+
+ const a = client.request('config.set', { ...pick, value: 'a --provider custom' })
+ await vi.waitFor(() => expect(calls).toHaveLength(1))
+ await expect(client.request('config.set', { ...pick, value: 'b --provider custom' })).resolves.toMatchObject({ model: 'b' })
+ // A's stale-fence reply arrives after B committed, naming the fences B moved past.
+ refuseA(new JsonRpcGatewayError('revision_conflict', { code: 4001, data: { reason: 'revision_conflict', revision: 5, execution_generation: 3 } }))
+ await expect(a).rejects.toThrow('revision_conflict')
+ expect(calls.map(call => (call.params.payload as { model: string }).model)).toEqual(['a', 'b'])
+
+ await client.request('config.set', { ...pick, value: 'c --provider custom' })
+ // B's receipt moved the revision to 7; A's late refusal naming 5 must not rewind it.
+ expect(calls[2].params).toMatchObject({ expected_revision: 7 })
+})
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index 13410e9bb52..47947035388 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -230,17 +230,22 @@ export class CanonicalDesktopProtocol {
const data = (error as { data?: { revision?: unknown; execution_generation?: unknown } })?.data
const owner = canonicalSessionKey(params.session_id, params.profile)
- if (typeof data?.revision === 'number') { this.revisions.set(owner, data.revision) }
+ // An older request's refusal can arrive after a newer edit committed: never move the fences backwards.
+ if (typeof data?.revision === 'number' && data.revision > (this.revisions.get(owner) ?? -1)) { this.revisions.set(owner, data.revision) }
- if (typeof data?.execution_generation === 'number') { this.generations.set(owner, data.execution_generation) }
+ if (typeof data?.execution_generation === 'number' && data.execution_generation > (this.generations.get(owner) ?? -1)) {
+ this.generations.set(owner, data.execution_generation)
+ }
}
// A fence refusal of a value pick (model / reasoning) that named fresh fences is retried once at them:
- // re-applying the same value is harmless, unlike a CAS edit of shared metadata.
+ // re-applying the same value is harmless, unlike a CAS edit of shared metadata. Only while it is still the
+ // session's current retained request: a newer pick retired it, and re-preparing it would undo that pick.
retryableFence(params: Record<string, unknown>, error: unknown): boolean {
const data = (error as { data?: { reason?: unknown; revision?: unknown } })?.data
+ const current = [...this.mutations.values()].some(mutation => mutation.request_id === params.request_id)
- return ['model', 'reasoning'].includes(params.operation as string) && typeof data?.revision === 'number'
+ return current && ['model', 'reasoning'].includes(params.operation as string) && typeof data?.revision === 'number'
&& (data.reason === 'revision_conflict' || data.reason === 'stale_generation')
}