The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): shared HTTP mutation identity retires superseded and refused intents
Teknium82301e869894
diff --git a/apps/desktop/src/api/session-http-mutation-identity.test.ts b/apps/desktop/src/api/session-http-mutation-identity.test.tsnew file mode 100644index 00000000000..70726ec0ce0--- /dev/null+++ b/apps/desktop/src/api/session-http-mutation-identity.test.ts@@ -0,0 +1,41 @@+import { expect, test } from 'vitest'++import { createSessionMutationClient } from '../../../shared/src/session-http-mutations'++const key = (field: string, value: unknown) => JSON.stringify([{ connectionId: null, profile: 'p' }, 's', 'PATCH', { [field]: value }])++// N101: A:X (lost ACK) -> B:Y (acknowledged) -> A:X must read a fresh precondition; the retained+// A identity is superseded by B's committed revision. B2-22: definitive refusals drop the identity.+test.each([+ ['an acknowledged write on the same session', async (mutate: ReturnType<typeof createSessionMutationClient>, read: () => Promise<any>) => {+ await mutate(key('hidden', true), read, async () => 'ok')+ }],+ ...['unknown_execution', 'not_found', 'mutation_identity_required'].map(reason => [`a definitive ${reason} refusal`,+ async (mutate: ReturnType<typeof createSessionMutationClient>, read: () => Promise<any>) => {+ await expect(mutate(key('title', 'A'), read, async () => { throw new Error(`409 ${reason}`) })).rejects.toThrow(reason)+ }] as const)+] as const)('%s retires the retained ambiguous identity', async (_label, intervene) => {+ const mutate = createSessionMutationClient()+ let revision = 7+ const read = async () => ({ session_id: 's', exists: true, runtime_revision: revision, runtime_generation: 1 })+ const sent: number[] = []+ await expect(mutate(key('title', 'A'), read, async id => { sent.push(id.expected_revision); throw new Error('timeout') })).rejects.toThrow('timeout')+ revision = 8+ await intervene(mutate, read)+ revision = 9+ await mutate(key('title', 'A'), read, async id => { sent.push(id.expected_revision); return 'ok' })+ expect(sent).toEqual([7, 9])+})++test('a retained identity on another session survives an acknowledged write', async () => {+ const mutate = createSessionMutationClient()+ let revision = 3+ const read = async () => ({ session_id: 'x', exists: true, runtime_revision: revision, runtime_generation: 1 })+ const other = JSON.stringify([{ connectionId: null, profile: 'p' }, 'other', 'PATCH', { title: 'A' }])+ const sent: number[] = []+ await expect(mutate(other, read, async id => { sent.push(id.expected_revision); throw new Error('timeout') })).rejects.toThrow('timeout')+ revision = 4+ await mutate(key('hidden', true), read, async () => 'ok')+ await mutate(other, read, async id => { sent.push(id.expected_revision); return 'ok' })+ expect(sent).toEqual([3, 3])+})diff --git a/apps/shared/src/session-http-mutations.ts b/apps/shared/src/session-http-mutations.tsindex d60149f6fa3..621d98363c2 100644--- a/apps/shared/src/session-http-mutations.ts+++ b/apps/shared/src/session-http-mutations.ts@@ -14,6 +14,9 @@ export interface SessionMutationIdentity {/** Keep an ambiguous write's identity until the same intent is acknowledged.* Keys include the captured destination and payload; neither retries nor a* profile switch may silently refresh the precondition of a possibly committed edit.+ * A key is a JSON array whose first two entries are the destination scope and the session id:+ * an acknowledged write retires that session's other retained identities (their revision is+ * superseded, so replaying them could only conflict or return a stale receipt).*/export function createSessionMutationClient() {const pending = new Map<string, Promise<SessionMutationIdentity>>()@@ -37,15 +40,23 @@ export function createSessionMutationClient() {try { identity = await prepared } catch (error) { pending.delete(key); throw error }try {const result = await send(identity)- pending.delete(key)+ const owner = sessionOwner(key)+ for (const other of [...pending.keys()]) { if (other === key || (owner && sessionOwner(other) === owner)) { pending.delete(other) } }return result} catch (error) {// These responses definitively rejected the write. Show the conflict;// only a later explicit user action may read a new precondition.- if (/revision_conflict|stale_generation|permission_denied|profile_mismatch|invalid_params|admission_conflict|session_busy|retained_receipts|runtime_coordination_required/.test(String(error))) {+ if (/revision_conflict|stale_generation|permission_denied|profile_mismatch|invalid_params|admission_conflict|session_busy|retained_receipts|runtime_coordination_required|unknown_execution|not_found|mutation_identity_required/.test(String(error))) {pending.delete(key)}throw error}}}++function sessionOwner(key: string): string | null {+ try {+ const parsed = JSON.parse(key)+ return Array.isArray(parsed) && parsed.length > 2 ? JSON.stringify(parsed.slice(0, 2)) : null+ } catch { return null }+}