Skip to content

The agent that grows with you

fix(desktop): a later acknowledged edit retires an older retained mutation

Teknium23e1dd2d58fa
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index becb05fec08..7ceca72ff63 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -111,6 +111,8 @@ function canonicalBotDelivery(params: Record<string, unknown>): Record<string, u
}
const BRANCH_METHODS = new Set(['session.branch', 'session.branch_stored', 'session.branch_whole'])
+// Value-setting mutations: re-applying one is harmless, so a later acknowledged edit retires it.
+const VALUE_OPERATIONS = new Set(['rename', 'archive', 'model'])
const MUTATION_METHODS = new Set(['session.title', 'session.archive', 'session.compress', ...BRANCH_METHODS])
export class CanonicalDesktopProtocol {
@@ -382,14 +384,17 @@ export class CanonicalDesktopProtocol {
result(method: string, params: Record<string, unknown>, value: any): any {
if (!value || typeof value !== 'object') { return value }
+ const mutation = MUTATION_METHODS.has(method) || ((method === 'slash.exec' || method === 'config.set') && typeof params.operation === 'string')
+
if (typeof value.session_id === 'string' && typeof value.revision === 'number') {
- this.revisions.set(canonicalSessionKey(value.session_id, params.profile), value.revision)
- }
+ const owner = canonicalSessionKey(value.session_id, params.profile)
- if (MUTATION_METHODS.has(method) || ((method === 'slash.exec' || method === 'config.set') && typeof params.operation === 'string')) {
- return this.mutationReceipt(method, params, value)
+ // An exact-retry receipt replays its ORIGINAL revision; never move the CAS value backwards.
+ if (!mutation || value.revision >= (this.revisions.get(owner) ?? -1)) { this.revisions.set(owner, value.revision) }
}
+ if (mutation) { return this.mutationReceipt(method, params, value) }
+
if (method === 'session.create') {
for (const [key, id] of this.creates) { if (id === params.request_id) { this.creates.delete(key) } }
}
@@ -428,6 +433,7 @@ export class CanonicalDesktopProtocol {
for (const [key, mutation] of this.mutations) { if (mutation.request_id === params.request_id) { this.mutations.delete(key) } }
this.settleModelConfirmation(params)
+ this.retireSupersededMutations(params, value.revision)
if (params.operation === 'model' && value.status === 'confirmation_required' && typeof value.confirm === 'string') {
// The owner wrote nothing: answer in the legacy handshake (`confirm_required` +
@@ -447,6 +453,24 @@ export class CanonicalDesktopProtocol {
return { ...value, ok: true }
}
+ // An acknowledged mutation at revision R settles every older retained value-setting request of
+ // the same owner: it either applied before R or can only be refused, so replaying its request
+ // id would return a stale receipt for a later, equal edit. Its confirmed model token goes with
+ // it. Branch/compress stay retained: a new request id there would execute a second time.
+ private retireSupersededMutations(params: Record<string, unknown>, revision: unknown): void {
+ if (typeof revision !== 'number') { return }
+ const owner = canonicalSessionKey(params.session_id, params.profile)
+
+ for (const [key, mutation] of this.mutations) {
+ const [keyOwner, operation] = JSON.parse(key)
+
+ if (keyOwner === owner && VALUE_OPERATIONS.has(operation) && (mutation.expected_revision as number) < revision) {
+ this.mutations.delete(key)
+ this.settleModelConfirmation({ ...mutation, profile: params.profile })
+ }
+ }
+ }
+
private snapshotResult(value: any, route: unknown): any {
const sid = value.session_id
const profile = canonicalProfile(route)
diff --git a/apps/desktop/src/api/canonical-retained-mutation-stale.test.ts b/apps/desktop/src/api/canonical-retained-mutation-stale.test.ts
new file mode 100644
index 00000000000..4ebb081d189
--- /dev/null
+++ b/apps/desktop/src/api/canonical-retained-mutation-stale.test.ts
@@ -0,0 +1,28 @@
+import { expect, test } from 'vitest'
+
+import { CanonicalDesktopProtocol } from './canonical-protocol'
+
+// dokterdok N34: a retained (reply-lost) rename must not be replayed for a LATER equal rename.
+test('an acknowledged later edit retires an older retained mutation; its stale receipt never rewinds the revision', () => {
+ const protocol = new CanonicalDesktopProtocol()
+ protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 7 })
+
+ // Rename to A: the reply is lost (no reason), so the request stays retained for an exact retry.
+ const lostA = protocol.prepare('session.title', { session_id: 's', title: 'A' })
+ protocol.failure(lostA, new Error('socket closed'))
+
+ // It did apply (revision 8, seen through session.info); then rename to B is acknowledged at 9.
+ protocol.event({ type: 'session.info', session_id: 's', payload: { revision: 8 } })
+ const renameB = protocol.prepare('session.title', { session_id: 's', title: 'B' })
+ expect(renameB.expected_revision).toBe(8)
+ protocol.result('session.title', renameB, { session_id: 's', revision: 9, title: 'B' })
+
+ // Renaming back to A is a NEW edit at revision 9, not a replay of the old request id.
+ const againA = protocol.prepare('session.title', { session_id: 's', title: 'A' })
+ expect(againA.request_id).not.toBe(lostA.request_id)
+ expect(againA.expected_revision).toBe(9)
+
+ // An exact-retry receipt replaying the original revision cannot move the CAS value backwards.
+ protocol.result('session.title', lostA, { session_id: 's', revision: 8, title: 'A' })
+ expect(protocol.prepare('session.archive', { session_id: 's', archived: true }).expected_revision).toBe(9)
+})