Skip to content

The agent that grows with you

fix(desktop): a reconnecting Screen pane keeps its viewer id; unchanged leases still advance the epoch

teknium136e2d92c2d82
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx b/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx
index 87221bb08d2..bf7509b4c80 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane-lifecycle.test.tsx
@@ -177,6 +177,15 @@ it('pins the bot socket for the attach lifetime and lets go on unmount', async (
})
it('does not hand back while replacing a stream to reconnect the same viewer', async () => {
+ // The server mints a fresh id for a bare observe; only an observe that re-presents the minted id
+ // keeps it. Model that, so a pane that forgets its id visibly loses the lease it holds.
+ let minted = 0
+ vi.mocked(displayRequest).mockImplementation(async (_bot, method, params) => {
+ if (method !== 'display.observe') return { ...status, ticket: 'test-ticket', viewer_id: 'this-viewer' }
+ const presented = (params as { viewer_id?: string } | undefined)?.viewer_id
+ const viewer_id = presented === 'this-viewer' ? 'this-viewer' : minted++ === 0 ? 'this-viewer' : 'replacement-viewer'
+ return { ...status, ticket: 'test-ticket', viewer_id }
+ })
const view = render(<BotScreenPane bot={bot} />)
await waitFor(() => expect(sockets).toHaveLength(1))
await act(async () => {})
@@ -184,6 +193,8 @@ it('does not hand back while replacing a stream to reconnect the same viewer', a
await waitFor(() => expect(sockets).toHaveLength(2))
expect(sockets[0].closeCodes).toEqual([1005])
expect(sockets[1].closed).toBe(false)
+ // Same viewer after the reconnect: the human's lease still belongs to this pane and it can hand back.
+ expect(rfbs[1].viewOnly).toBe(false)
view.unmount()
})
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx b/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx
index 08730758923..eb7c1eca18c 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx
@@ -164,7 +164,14 @@ export function BotScreenPane({ bot }: { bot: RosterRow }) {
}
retention.current = retain
- const observe = await displayRequest<DisplayObserveResult>(bot, 'display.observe')
+ // Re-present the id this window already minted: a reconnect (network blip, 4000 eviction, Reconnect
+ // button) must keep the human's lease bound to THIS pane, or the new stream is watch-only and the
+ // old lease can only be cleared by force. The server honours a minted id only on the connection
+ // that minted it; a foreign or stale id is silently replaced.
+ const priorViewer = screenStateFor($screenState.get(), bot)?.viewer?.id
+ const observe = await displayRequest<DisplayObserveResult>(
+ bot, 'display.observe', priorViewer ? { viewer_id: priorViewer } : {},
+ )
const minted = { id: observe.viewer_id, hash: await viewerHash(observe.viewer_id) }
setScreenStatus(bot, observe)
const url = await resolveScreenWsUrl(bot, observe.ticket)
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-state.test.ts b/apps/desktop/src/plugins/hermes-bots/screen-state.test.ts
index 2d0f5422aa1..f0681c22654 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-state.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/screen-state.test.ts
@@ -58,6 +58,17 @@ describe('lease epoch ordering', () => {
setScreenLease(bot, legacy)
expect(screenStateFor($screenState.get(), bot)?.lease).toEqual(legacy)
})
+
+ it('an unchanged-looking lease still advances the cached epoch, so a delayed older takeover is rejected', () => {
+ // agent@0 → agent@2 (a take-over and hand-back that both happened before we looked) must leave epoch 2
+ // recorded even though nothing visible changed; otherwise the late human@1 event wins and the pane shows
+ // a human holding a screen the agent already has back.
+ setScreenLease(bot, { ...agent, epoch: 0 })
+ setScreenLease(bot, { ...agent, epoch: 2 })
+ setScreenLease(bot, { ...human, epoch: 1 })
+ expect(screenStateFor($screenState.get(), bot)?.lease?.holder).toBe('agent')
+ expect(screenStateFor($screenState.get(), bot)?.lease?.epoch).toBe(2)
+ })
})
describe('status request ordering', () => {
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-state.ts b/apps/desktop/src/plugins/hermes-bots/screen-state.ts
index 2f4045c460a..fb0a6dddd76 100644
--- a/apps/desktop/src/plugins/hermes-bots/screen-state.ts
+++ b/apps/desktop/src/plugins/hermes-bots/screen-state.ts
@@ -91,11 +91,14 @@ export function setScreenLease(bot: RosterRow, lease: DisplayLease): void {
prev?.lease &&
prev.lease.holder === lease.holder &&
prev.lease.viewer_id === lease.viewer_id &&
- prev.lease.viewer_hash === lease.viewer_hash
+ prev.lease.viewer_hash === lease.viewer_hash &&
+ prev.lease.epoch === lease.epoch
) {
return
}
+ // Same presentation, newer epoch still has to be recorded: otherwise a delayed older event (human@1
+ // after agent@0 → agent@2) compares against the stale epoch and rolls the pane back.
$screenState.set({ ...current, [key]: { status: prev?.status ?? null, lease, viewer: prev?.viewer ?? null } })
}