Skip to content

The agent that grows with you

test(desktop): move the turn-lease tests to the end of the lifecycle file

teknium18bbc02a02a4c
diff --git a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
index b712b897a98..6b2c7fa3821 100644
--- a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
+++ b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
@@ -106,70 +106,6 @@ afterEach(() => {
delete (window as unknown as { hermesDesktop?: unknown }).hermesDesktop
})
-describe('retainGatewayForSessionTurn', () => {
- it('lets the next turn take a real hold after a turn that rode the primary socket', async () => {
- // A routed prompt holds one lease per (route, runtime session) until the turn settles, and for a
- // streaming turn only a Secondary's terminal-event listener ends it. When the route is served by
- // the primary socket there is no Secondary, so a lease registered then can never be released —
- // and the map is keyed per session, so it silently suppresses every later hold on that session,
- // including after the route is dialed as a real secondary. The socket is then free to be reaped
- // mid-turn, which is the interruption this whole mechanism exists to prevent.
- installDesktop({ getConnection: vi.fn() }) // no getConnectionFor: nothing to hold, no Secondary
-
- // The streaming turn deliberately does NOT release: its release would arrive as a terminal event.
- await retainGatewayForSessionTurn('homelab', 'writer', 'session-1')
-
- installDesktop({
- getConnection: vi.fn(),
- getConnectionFor: vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>
- descriptorFor(connectionId, profile)
- )
- })
- const dialedBefore = gatewayMocks.instances.length
-
- await retainGatewayForSessionTurn('homelab', 'writer', 'session-1')
-
- expect(gatewayMocks.instances.length).toBeGreaterThan(dialedBefore)
- })
-
- it('does not orphan a hold when two submits for one session race the dial', async () => {
- // The duplicate-lease guard runs BEFORE the retain awaits, and the map is written after it, so
- // two submits for the same (route, session) can both pass. Only the mapped release is ever
- // invoked — releaseTerminalTurnLease does `g.turnLeases.get(key)?.()` — so the loser's hold is
- // never released and the socket can never be reclaimed.
- let openDial = () => {}
-
- const dialed = new Promise<void>(resolve => {
- openDial = resolve
- })
-
- gatewayMocks.connect.mockImplementationOnce(async () => dialed)
- installDesktop({
- getConnectionFor: vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>
- descriptorFor(connectionId, profile)
- )
- })
-
- const both = Promise.all([
- retainGatewayForSessionTurn('homelab', 'writer', 'session-2'),
- retainGatewayForSessionTurn('homelab', 'writer', 'session-2')
- ])
-
- openDial()
- await both
-
- // The terminal event releases the one lease the map holds, exactly as the gateway's own
- // listener does; a leaked second hold would keep activeRequests above zero.
- for (const handler of gatewayMocks.eventHandlers) {
- handler({ session_id: 'session-2', type: 'session.reclaimed' })
- }
-
- pruneSecondaryGateways(new Set())
-
- expect(gatewayMocks.instances[0].close).toHaveBeenCalled()
- })
-})
-
describe('a redial of the active route', () => {
it('is not cancelled by a prune that runs while it is dialing', async () => {
// Editing the connection you are viewing defers the redial until its lease drops, then
@@ -1029,3 +965,67 @@ it('does not let a removed connection repopulate the auth rejection', async () =
await expect(requestGatewayForAgent('cloud', 'default', 'session.list')).rejects.toThrow()
expect(getGatewayWsUrlFor).toHaveBeenCalledTimes(2)
})
+
+describe('retainGatewayForSessionTurn', () => {
+ it('lets the next turn take a real hold after a turn that rode the primary socket', async () => {
+ // A routed prompt holds one lease per (route, runtime session) until the turn settles, and for a
+ // streaming turn only a Secondary's terminal-event listener ends it. When the route is served by
+ // the primary socket there is no Secondary, so a lease registered then can never be released —
+ // and the map is keyed per session, so it silently suppresses every later hold on that session,
+ // including after the route is dialed as a real secondary. The socket is then free to be reaped
+ // mid-turn, which is the interruption this whole mechanism exists to prevent.
+ installDesktop({ getConnection: vi.fn() }) // no getConnectionFor: nothing to hold, no Secondary
+
+ // The streaming turn deliberately does NOT release: its release would arrive as a terminal event.
+ await retainGatewayForSessionTurn('homelab', 'writer', 'session-1')
+
+ installDesktop({
+ getConnection: vi.fn(),
+ getConnectionFor: vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>
+ descriptorFor(connectionId, profile)
+ )
+ })
+ const dialedBefore = gatewayMocks.instances.length
+
+ await retainGatewayForSessionTurn('homelab', 'writer', 'session-1')
+
+ expect(gatewayMocks.instances.length).toBeGreaterThan(dialedBefore)
+ })
+
+ it('does not orphan a hold when two submits for one session race the dial', async () => {
+ // The duplicate-lease guard runs BEFORE the retain awaits, and the map is written after it, so
+ // two submits for the same (route, session) can both pass. Only the mapped release is ever
+ // invoked — releaseTerminalTurnLease does `g.turnLeases.get(key)?.()` — so the loser's hold is
+ // never released and the socket can never be reclaimed.
+ let openDial = () => {}
+
+ const dialed = new Promise<void>(resolve => {
+ openDial = resolve
+ })
+
+ gatewayMocks.connect.mockImplementationOnce(async () => dialed)
+ installDesktop({
+ getConnectionFor: vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>
+ descriptorFor(connectionId, profile)
+ )
+ })
+
+ const both = Promise.all([
+ retainGatewayForSessionTurn('homelab', 'writer', 'session-2'),
+ retainGatewayForSessionTurn('homelab', 'writer', 'session-2')
+ ])
+
+ openDial()
+ await both
+
+ // The terminal event releases the one lease the map holds, exactly as the gateway's own
+ // listener does; a leaked second hold would keep activeRequests above zero.
+ for (const handler of gatewayMocks.eventHandlers) {
+ handler({ session_id: 'session-2', type: 'session.reclaimed' })
+ }
+
+ pruneSecondaryGateways(new Set())
+
+ expect(gatewayMocks.instances[0].close).toHaveBeenCalled()
+ })
+})