Skip to content

The agent that grows with you

fix: retire desktop Stop for newer owner executions and fence old output

Teknium926d097dee9f
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/index.ts
index 048b6c3f7fd..36bf25a2c73 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/index.ts
@@ -174,8 +174,19 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
const sessionId = route.sessionId
const authorityKey = `${registryBackendScopeKey(event.connectionId ?? null, event.profile ?? null)}\u0000${sessionId}`
+ const previousAuthority = executionAuthorities.current.get(authorityKey)
+
if (sessionId && !acceptExecutionEvent(executionAuthorities.current, authorityKey, event.type, payload)) {return}
+ const authority = executionAuthorities.current.get(authorityKey)
+
+ if (sessionId && previousAuthority && authority && !authority.terminal &&
+ (authority.epoch !== previousAuthority.epoch || authority.generation > previousAuthority.generation)) {
+ // Stop belongs to the cancelled execution, not the shared session.
+ // Only an accepted newer owner start/snapshot may retire its latch.
+ deps.updateSessionState(sessionId, state => state.interrupted ? { ...state, interrupted: false } : state)
+ }
+
// Late stragglers: an unscoped stream event attributed via the
// active-session fallback (no pin) to a session that has no live turn
// belongs to a turn that already ended elsewhere. Dropping it keeps the
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index 0400d3a7f0d..3c3c436406b 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -440,6 +440,8 @@ describe('Stop and shared-owner execution', () => {
expect(handle!.state()).toMatchObject({ interrupted: false, busy: true, awaitingResponse: true })
send('message.delta', epoch, generation, { text: 'external answer' })
send('message.delta', 'owner-a', 4, { text: 'obsolete tail' })
+ send('message.interim', epoch, generation)
+ expect(handle!.state().messages.at(-1)?.parts).toContainEqual(expect.objectContaining({ text: 'external answer' }))
send('message.complete', 'owner-a', 4, { text: 'obsolete final' })
expect(handle!.state().busy).toBe(true)
send('message.complete', epoch, generation, { text: 'external answer' })
diff --git a/apps/desktop/src/lib/execution-authority.ts b/apps/desktop/src/lib/execution-authority.ts
index bff4a9854cd..2120a909ac6 100644
--- a/apps/desktop/src/lib/execution-authority.ts
+++ b/apps/desktop/src/lib/execution-authority.ts
@@ -12,12 +12,18 @@ export function acceptExecutionEvent(
type: string,
payload?: Record<string, unknown>
): boolean {
- if (!['session.info', 'message.start', 'message.complete', 'message.error'].includes(type)) {return true}
+ const lifecycle = ['session.info', 'message.start', 'message.complete', 'message.error'].includes(type)
const previous = authorities.get(key)
const epoch = payload?.execution_epoch
const generation = payload?.execution_generation
const versioned = typeof epoch === 'string' && epoch.length > 0 && typeof generation === 'number' && Number.isSafeInteger(generation) && generation >= 0
+ // Output may follow the current owner, but cannot establish another one.
+ // Keep legacy unversioned output compatible; fence stamped late frames.
+ if (!lifecycle) {
+ return !versioned || !previous || (previous.epoch === epoch && previous.generation === generation && !previous.terminal)
+ }
+
if (!versioned) {return !previous}
const terminal = type === 'message.complete' || type === 'message.error' || payload?.running === false