Skip to content

The agent that grows with you

fix(desktop): the frame that triggered owner reconciliation is delivered once routed

Teknium3c71c458824a
diff --git a/apps/desktop/src/api/canonical-ambiguous-owner-reconcile.test.ts b/apps/desktop/src/api/canonical-ambiguous-owner-reconcile.test.ts
index 4c040081243..8b792e352ee 100644
--- a/apps/desktop/src/api/canonical-ambiguous-owner-reconcile.test.ts
+++ b/apps/desktop/src/api/canonical-ambiguous-owner-reconcile.test.ts
@@ -46,6 +46,26 @@ test('a failed owner reconciliation is probed again by the next frame', async ()
client.close()
})
+test('the frame that triggered a successful reconciliation is delivered to its owner', async () => {
+ let restarted = false
+
+ const { client, emit } = await equalIdClient(async profile =>
+ snapshot(profile, restarted && profile === 'beta' ? 'beta-restarted' : `epoch-${profile}`))
+
+ await client.request('session.resume', { session_id: 'same', profile: 'alpha' })
+ await client.request('session.resume', { session_id: 'same', profile: 'beta' })
+ const named: any[] = []
+ const wildcard: any[] = []
+ client.on('message.complete', event => named.push(event))
+ client.onAny(event => { if (event.type === 'message.complete') { wildcard.push(event) } })
+ restarted = true
+ emit({ type: 'message.complete', session_id: 'same', replay_epoch: 'beta-restarted', payload: { text: 'done' } })
+ await vi.waitFor(() => expect(named).toHaveLength(1))
+ expect(named[0].profile).toBe('beta')
+ expect(wildcard.map(event => event.profile)).toEqual(['beta'])
+ client.close()
+})
+
test('a prompt declined locally is shown again while the authority still lists it', async () => {
const prompt = { kind: 'approval', prompt_id: 'pending', execution_generation: 2, command: 'build', choices: ['once'] }
const { calls, client } = await equalIdClient(async () => ({ session_id: 's', revision: 1, execution_generation: 2, prompts: [prompt] }))
diff --git a/apps/desktop/src/api/client.ts b/apps/desktop/src/api/client.ts
index 204094cc22b..4f96f7f0229 100644
--- a/apps/desktop/src/api/client.ts
+++ b/apps/desktop/src/api/client.ts
@@ -89,6 +89,8 @@ export class HermesGateway extends JsonRpcGatewayClient {
private readonly displayProfiles = new Map<string, string>()
private readonly eventRoutes = new WeakMap<GatewayEvent, string>()
private readonly reconciledEpochs = new Set<string>()
+ // Frames of an ambiguous (session, epoch) parked until its owner probes settle.
+ private readonly parkedFrames = new Map<string, Array<() => void>>()
private connectionGeneration = 0
override onRequest(handler: ServerRequestHandler): () => void {
@@ -180,26 +182,41 @@ export class HermesGateway extends JsonRpcGatewayClient {
this.replayProfiles.clear()
this.displayProfiles.clear()
this.reconciledEpochs.clear()
+ this.parkedFrames.clear()
this.attached.clear()
super.close()
}
override on<K extends GatewayEventName>(type: K, handler: (event: GatewayEvent<K>) => void): () => void {
- return super.on<K>(type, event => {
- // Named listeners run before wildcard listeners in the shared client.
- // Normalize before either kind sees the prompt, including replay delivery.
- if (this.canonical && !this.normalizeCanonicalEvent(event)) { return }
- handler(event)
- })
+ // Named listeners run before wildcard listeners in the shared client.
+ // Normalize before either kind sees the prompt, including replay delivery.
+ return super.on<K>(type, event => this.deliverNormalized(event, handler as (event: GatewayEvent) => void))
}
override onAny(handler: (event: GatewayEvent) => void): () => void {
- return super.onAny(event => {
- if (this.canonical && !this.normalizeCanonicalEvent(event)) { return }
+ return super.onAny(event => this.deliverNormalized(event, handler))
+ }
+
+ private deliverNormalized(event: GatewayEvent, handler: (event: GatewayEvent) => void): void {
+ const key = JSON.stringify([event.session_id, event.replay_epoch])
+ // While an owner probe for this (session, epoch) runs, its frames wait and deliver in order.
+ const parked = this.canonical && event.session_id ? this.parkedFrames.get(key) : undefined
+
+ const deliver = () => {
+ if (!this.canonical || this.normalizeCanonicalEvent(event)) { handler(event) }
+ }
+
+ if (parked) {
+ parked.push(deliver)
+ } else if (!this.canonical || this.normalizeCanonicalEvent(event)) {
handler(event)
- })
+ } else {
+ // The owner probe this frame just started may establish its route; it is the first frame parked.
+ this.parkedFrames.get(key)?.push(deliver)
+ }
}
+
private rememberSessionProfile(sessionId: string, profile: unknown): void {
const profiles = this.sessionProfiles.get(sessionId) ?? new Set<string>()
profiles.add(this.resolveSessionProfile(sessionId, profile))
@@ -265,6 +282,7 @@ export class HermesGateway extends JsonRpcGatewayClient {
if (this.reconciledEpochs.has(key)) { return }
this.reconciledEpochs.add(key)
+ this.parkedFrames.set(key, [])
const generation = this.connectionGeneration
// A new/unknown replay epoch cannot choose between equal stored IDs. One
@@ -273,10 +291,18 @@ export class HermesGateway extends JsonRpcGatewayClient {
this.request('session.resume', { session_id: sessionId, profile }))
void Promise.allSettled(probes).then(results => {
+ if (generation !== this.connectionGeneration) { return }
+ const parked = this.parkedFrames.get(key) ?? []
+ this.parkedFrames.delete(key)
+
// No owner answered: a later frame probes again instead of being dropped until reconnect.
- if (generation === this.connectionGeneration && !results.some(result => result.status === 'fulfilled')) {
+ if (!results.some(result => result.status === 'fulfilled')) {
this.reconciledEpochs.delete(key)
+
+ return
}
+
+ for (const deliver of parked) { deliver() }
})
}
@@ -290,6 +316,7 @@ export class HermesGateway extends JsonRpcGatewayClient {
this.replayProfiles.clear()
this.displayProfiles.clear()
this.reconciledEpochs.clear()
+ this.parkedFrames.clear()
return super.connect(wsUrl)
}