Skip to content

The agent that grows with you

fix(ink,desktop): any other verb retires a session's lost-reply controls

Tekniumb44a2b34a509
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index 400c8bca05d..b38f335fe4c 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -217,6 +217,7 @@ export class CanonicalDesktopProtocol {
private retainedMutation(sessionId: unknown, profile: unknown, operation: string, payload: Record<string, unknown>, withGeneration: boolean): Record<string, unknown> {
const owner = canonicalSessionKey(sessionId, profile)
const key = JSON.stringify([owner, operation, payload])
+ this.retireMutations(owner, key)
const retained = this.mutations.get(key)
if (retained) { return retained }
@@ -243,11 +244,18 @@ export class CanonicalDesktopProtocol {
return method !== 'session.create' && params.profile !== undefined ? { ...prepared, profile: params.profile } : prepared
}
+ // Only a session's very next control may retry an ambiguous one: any other verb retires the rest.
+ private retireMutations(owner: string, keep?: string) {
+ for (const key of [...this.mutations.keys()]) { if (key !== keep && JSON.parse(key)[0] === owner) { this.mutations.delete(key) } }
+ }
+
private preparePayload(method: string, params: Record<string, unknown>): Record<string, unknown> {
const mutation = this.prepareMutation(method, params)
if (mutation) { return mutation }
+ if (method === 'prompt.submit') { this.retireMutations(canonicalSessionKey(params.session_id, params.profile)) }
+
if (method === 'session.create') { return this.prepareCreate(params) }
if (method === 'session.activate') {
diff --git a/apps/desktop/src/api/canonical-retained-control-retire.test.ts b/apps/desktop/src/api/canonical-retained-control-retire.test.ts
new file mode 100644
index 00000000000..a951d81c2c5
--- /dev/null
+++ b/apps/desktop/src/api/canonical-retained-control-retire.test.ts
@@ -0,0 +1,30 @@
+import { expect, test } from 'vitest'
+
+import { CanonicalDesktopProtocol } from './canonical-protocol'
+
+// A retained control (reply lost) may be retried only as the session's very next control. Once
+// another verb reached the session, re-issuing the old control must mint a fresh identity: replaying
+// the stale receipt left the session on the intervening value.
+test('another control on the session retires a lost-reply control instead of replaying it', () => {
+ const protocol = new CanonicalDesktopProtocol()
+ protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 7, execution_generation: 3 })
+ const firstA = protocol.prepare('session.title', { session_id: 's', title: 'A' }) // reply lost
+ protocol.prepare('session.title', { session_id: 's', title: 'B' })
+ const againA = protocol.prepare('session.title', { session_id: 's', title: 'A' })
+ expect(againA.request_id).not.toBe(firstA.request_id)
+})
+
+test('a prompt submit retires the session lost-reply controls', () => {
+ const protocol = new CanonicalDesktopProtocol()
+ protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 7, execution_generation: 3 })
+ const firstA = protocol.prepare('session.title', { session_id: 's', title: 'A' }) // reply lost
+ protocol.prepare('prompt.submit', { session_id: 's', text: 'go', submission_id: 'x' })
+ expect(protocol.prepare('session.title', { session_id: 's', title: 'A' }).request_id).not.toBe(firstA.request_id)
+})
+
+test('the immediate retry of the same control keeps its identity', () => {
+ const protocol = new CanonicalDesktopProtocol()
+ protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 7, execution_generation: 3 })
+ const first = protocol.prepare('session.title', { session_id: 's', title: 'A' })
+ expect(protocol.prepare('session.title', { session_id: 's', title: 'A' })).toEqual(first)
+})
diff --git a/ui-tui/src/__tests__/canonicalRetainedControlRetire.test.ts b/ui-tui/src/__tests__/canonicalRetainedControlRetire.test.ts
new file mode 100644
index 00000000000..2b5409c200a
--- /dev/null
+++ b/ui-tui/src/__tests__/canonicalRetainedControlRetire.test.ts
@@ -0,0 +1,56 @@
+import { describe, expect, it } from 'vitest'
+
+import { mutateCanonical, retireCanonicalControls } from '../app/slash/canonicalSessionControls.js'
+
+// A lost-reply control may be retried only as the session's very next control: after another verb,
+// re-issuing it must mint a fresh request_id instead of replaying the stale receipt.
+function gateway() {
+ const mutations: Array<Record<string, unknown>> = []
+ let lose = true
+
+ return {
+ mutations,
+ deliver: () => (lose = false),
+ request: async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'session.resume') {
+ return { revision: 7, execution_generation: 3 }
+ }
+
+ mutations.push(params!)
+
+ if (lose) {
+ throw new Error('transport closed') // a lost reply: not a structured refusal
+ }
+
+ return { revision: 8, execution_generation: 3 }
+ }
+ }
+}
+
+describe('retained canonical controls', () => {
+ it('another control retires a lost-reply control', async () => {
+ const gw = gateway()
+ await expect(mutateCanonical(gw, 's', 'model', { model: 'A' })).rejects.toThrow()
+ gw.deliver()
+ await mutateCanonical(gw, 's', 'model', { model: 'B' })
+ await mutateCanonical(gw, 's', 'model', { model: 'A' })
+ expect(gw.mutations[2].request_id).not.toBe(gw.mutations[0].request_id)
+ })
+
+ it('a prompt submit retires the session lost-reply controls', async () => {
+ const gw = gateway()
+ await expect(mutateCanonical(gw, 's', 'model', { model: 'A' })).rejects.toThrow()
+ gw.deliver()
+ retireCanonicalControls(gw, 's')
+ await mutateCanonical(gw, 's', 'model', { model: 'A' })
+ expect(gw.mutations[1].request_id).not.toBe(gw.mutations[0].request_id)
+ })
+
+ it('the immediate retry of the same control keeps its identity', async () => {
+ const gw = gateway()
+ await expect(mutateCanonical(gw, 's', 'model', { model: 'A' })).rejects.toThrow()
+ gw.deliver()
+ await mutateCanonical(gw, 's', 'model', { model: 'A' })
+ expect(gw.mutations[1].request_id).toBe(gw.mutations[0].request_id)
+ })
+})
diff --git a/ui-tui/src/app/slash/canonicalSessionControls.ts b/ui-tui/src/app/slash/canonicalSessionControls.ts
index 39e1e266360..aa5c7e88aff 100644
--- a/ui-tui/src/app/slash/canonicalSessionControls.ts
+++ b/ui-tui/src/app/slash/canonicalSessionControls.ts
@@ -58,6 +58,17 @@ export interface MutationSnapshot {
* fresh `session.resume`; `payload` may be a function of that same snapshot (a rewind names its
* target row from it) and return null for "nothing to do". Metadata edits (`rename`) carry no
* generation fence, matching Desktop's `retainedMutation(..., withGeneration=false)`. */
+/** Only a session's very next control may retry an ambiguous one: any other verb retires the rest. */
+export function retireCanonicalControls(gw: MutationGateway, sid: string, keep?: string) {
+ const requests = pending.get(gw)
+
+ for (const key of [...(requests?.keys() ?? [])]) {
+ if (key !== keep && JSON.parse(key)[0] === sid) {
+ requests!.delete(key)
+ }
+ }
+}
+
export async function mutateCanonical(
gw: MutationGateway,
sid: string,
@@ -75,6 +86,7 @@ export async function mutateCanonical(
// A derived payload is keyed by operation alone: a lost-reply retry must re-present the
// original target, never re-derive one from a transcript the first attempt already rewound.
const key = JSON.stringify([sid, operation, typeof payload === 'function' ? null : payload])
+ retireCanonicalControls(gw, sid, key)
let params = requests.get(key)
if (!params) {
diff --git a/ui-tui/src/app/submissionCore.ts b/ui-tui/src/app/submissionCore.ts
index 821afc81c6c..4712feedddc 100644
--- a/ui-tui/src/app/submissionCore.ts
+++ b/ui-tui/src/app/submissionCore.ts
@@ -7,6 +7,7 @@ import { pendingInputOwner, savePendingInput } from '../lib/pendingInputs.js'
import type { Msg } from '../types.js'
import { markBubbleShown } from './pendingBubbles.js'
+import { retireCanonicalControls } from './slash/canonicalSessionControls.js'
import { captureDestination, isCurrentDestination, type SubmissionDestination } from './submissionDestination.js'
import { turnController } from './turnController.js'
import { getUiState, patchUiState } from './uiStore.js'
@@ -340,6 +341,7 @@ export function submitPrompt(
return
}
+ retireCanonicalControls(deps.gw, sid)
deps.gw
.request<PromptSubmitResponse>(
item?.controlMethod ?? 'prompt.submit',