|
| 1 | +/** |
| 2 | + * #2833: the request path reports session activity by finishing UNDER the session's execution lock — |
| 3 | + * the same lock an expiry must take before settling. That is what makes the stamp and the verdict |
| 4 | + * one clock: a session in use is never caught mid-command, and the deadline an expiry reads is the |
| 5 | + * one the last finished command set. Commands that never take that lock (the registry's |
| 6 | + * lock-exempt inventory surface) report nothing, so a bystander polling `devices` on a shared host |
| 7 | + * cannot keep another agent's abandoned claim alive; and a request whose client hung up preserves |
| 8 | + * nothing, exactly as a canceled request renews no lease (ADR 0007). |
| 9 | + */ |
| 10 | +import { afterAll, test, expect, vi } from 'vitest'; |
| 11 | +import fs from 'node:fs'; |
| 12 | +import path from 'node:path'; |
| 13 | +import { withDiagnosticsScope } from '@agent-device/host-kit/diagnostics'; |
| 14 | +import { clearRequestCanceled, markRequestCanceled } from '@agent-device/host-kit/request'; |
| 15 | +import { makeIosSession } from '../../__tests__/test-utils/session-factories.ts'; |
| 16 | +import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts'; |
| 17 | +import { LeaseRegistry } from '../lease-registry.ts'; |
| 18 | +import { |
| 19 | + existingSessionExecutionLockKeys, |
| 20 | + resolveRequestExecutionLockPlan, |
| 21 | +} from '../request-binding.ts'; |
| 22 | +import { |
| 23 | + createRequestExecutionScope, |
| 24 | + getLeaseRegistryExecutionLocks, |
| 25 | +} from '../request-execution-scope.ts'; |
| 26 | +import { withKeyedLock } from '@agent-device/kernel/keyed-lock'; |
| 27 | +import type { DaemonRequest } from '../daemon-request.ts'; |
| 28 | +import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts'; |
| 29 | + |
| 30 | +const TEST_ROOT = mkdtempForTestSync('agent-device-session-idle-activity-'); |
| 31 | +const LOG_PATH = path.join(TEST_ROOT, 'diagnostics.log'); |
| 32 | + |
| 33 | +afterAll(() => { |
| 34 | + fs.rmSync(TEST_ROOT, { recursive: true, force: true }); |
| 35 | +}); |
| 36 | + |
| 37 | +function makeRequest(overrides: Partial<DaemonRequest> = {}): DaemonRequest { |
| 38 | + return { |
| 39 | + token: 'test-token', |
| 40 | + session: 'default', |
| 41 | + command: 'snapshot', |
| 42 | + positionals: [], |
| 43 | + ...overrides, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +function storeWithSession() { |
| 48 | + const sessionStore = makeSessionStore('agent-device-idle-activity-'); |
| 49 | + sessionStore.set('default', makeIosSession('default')); |
| 50 | + return sessionStore; |
| 51 | +} |
| 52 | + |
| 53 | +test('a command that completes under the session lock stamps the session it ran on', async () => { |
| 54 | + const sessionStore = storeWithSession(); |
| 55 | + const scope = await withDiagnosticsScope({ command: 'snapshot', logPath: LOG_PATH }, () => |
| 56 | + createRequestExecutionScope({ |
| 57 | + req: makeRequest({ command: 'snapshot' }), |
| 58 | + sessionStore, |
| 59 | + leaseRegistry: new LeaseRegistry(), |
| 60 | + }), |
| 61 | + ); |
| 62 | + |
| 63 | + await scope.runLocked(async () => 'ran'); |
| 64 | + |
| 65 | + expect(sessionStore.get('default')?.lastActivityAtMs).toBeGreaterThan(0); |
| 66 | +}); |
| 67 | + |
| 68 | +test('a lock-exempt inventory command reports no activity, even against a claim-holding session', async () => { |
| 69 | + const sessionStore = storeWithSession(); |
| 70 | + const scope = await withDiagnosticsScope({ command: 'devices', logPath: LOG_PATH }, () => |
| 71 | + createRequestExecutionScope({ |
| 72 | + req: makeRequest({ command: 'devices' }), |
| 73 | + sessionStore, |
| 74 | + leaseRegistry: new LeaseRegistry(), |
| 75 | + }), |
| 76 | + ); |
| 77 | + |
| 78 | + await scope.runLocked(async () => 'ran'); |
| 79 | + |
| 80 | + // `devices` resolves a session address only to locate its own artifacts. On the shared host this |
| 81 | + // feature exists for, stamping there would let one agent's polling extend another's deadline. |
| 82 | + expect(sessionStore.get('default')?.lastActivityAtMs).toBeUndefined(); |
| 83 | +}); |
| 84 | + |
| 85 | +test('a command whose client hung up mid-flight preserves no activity', async () => { |
| 86 | + const sessionStore = storeWithSession(); |
| 87 | + const requestId = 'canceled-mid-command'; |
| 88 | + const scope = await withDiagnosticsScope( |
| 89 | + { command: 'snapshot', requestId, logPath: LOG_PATH }, |
| 90 | + () => |
| 91 | + createRequestExecutionScope({ |
| 92 | + req: makeRequest({ command: 'snapshot', meta: { requestId } }), |
| 93 | + sessionStore, |
| 94 | + leaseRegistry: new LeaseRegistry(), |
| 95 | + }), |
| 96 | + ); |
| 97 | + |
| 98 | + // The request was admitted, then the client hung up while the handler worked. A handler that |
| 99 | + // ignores its cancellation still reaches the stamp site — and must not renew anything there, the |
| 100 | + // same rule that stops a canceled request renewing a remote lease (ADR 0007). An agent that |
| 101 | + // timed out and left a hung handler is precisely what this deadline exists to catch. |
| 102 | + try { |
| 103 | + await scope.runLocked(async () => { |
| 104 | + markRequestCanceled(requestId); |
| 105 | + return 'ran to completion despite the disconnect'; |
| 106 | + }); |
| 107 | + expect(sessionStore.get('default')?.lastActivityAtMs).toBeUndefined(); |
| 108 | + } finally { |
| 109 | + clearRequestCanceled(requestId); |
| 110 | + } |
| 111 | +}); |
| 112 | + |
| 113 | +test('a command still running when the expiry arrives blocks it until the stamp lands', async () => { |
| 114 | + const sessionStore = storeWithSession(); |
| 115 | + const leaseRegistry = new LeaseRegistry(); |
| 116 | + const locks = getLeaseRegistryExecutionLocks(leaseRegistry); |
| 117 | + const scope = await withDiagnosticsScope({ command: 'snapshot', logPath: LOG_PATH }, () => |
| 118 | + createRequestExecutionScope({ |
| 119 | + req: makeRequest({ command: 'snapshot' }), |
| 120 | + sessionStore, |
| 121 | + leaseRegistry, |
| 122 | + }), |
| 123 | + ); |
| 124 | + |
| 125 | + let release!: () => void; |
| 126 | + const running = new Promise<void>((resolve) => { |
| 127 | + release = resolve; |
| 128 | + }); |
| 129 | + const command = scope.runLocked(async () => { |
| 130 | + await running; |
| 131 | + return 'ran'; |
| 132 | + }); |
| 133 | + |
| 134 | + // An expiry that starts while the command holds the lock cannot settle — and once the command |
| 135 | + // finishes, its stamp is already visible to whoever finally holds the lock. |
| 136 | + let settledFirst = true; |
| 137 | + const expiry = withKeyedLock(locks, 'session:default', async () => { |
| 138 | + settledFirst = sessionStore.get('default')?.lastActivityAtMs === undefined; |
| 139 | + }); |
| 140 | + |
| 141 | + await new Promise((resolve) => setTimeout(resolve, 5)); |
| 142 | + release(); |
| 143 | + await Promise.all([command, expiry]); |
| 144 | + |
| 145 | + expect(settledFirst).toBe(false); |
| 146 | +}); |
| 147 | + |
| 148 | +test('the request plan for an existing session keeps the canonical session-first lock pair', async () => { |
| 149 | + const sessionStore = storeWithSession(); |
| 150 | + const session = sessionStore.get('default')!; |
| 151 | + const requestPlan = await resolveRequestExecutionLockPlan({ |
| 152 | + req: makeRequest({ command: 'snapshot' }), |
| 153 | + sessionName: 'default', |
| 154 | + sessionStore, |
| 155 | + }); |
| 156 | + |
| 157 | + // What this pins is the PLANNER: an existing session's request takes `existingSessionExecutionLockKeys` |
| 158 | + // with the session first, which is the order a reaper must match to avoid deadlocking against it. The |
| 159 | + // reaper's own pair is exercised against a live request in server/daemon-session-idle-expiry.test.ts; |
| 160 | + // comparing this function with itself would prove nothing about that side. |
| 161 | + expect(requestPlan.keys).toEqual(existingSessionExecutionLockKeys('default', session.device.id)); |
| 162 | +}); |
| 163 | + |
| 164 | +test('the reaper never observes a mid-command session through the shared map', async () => { |
| 165 | + const sessionStore = storeWithSession(); |
| 166 | + const leaseRegistry = new LeaseRegistry(); |
| 167 | + const scope = await withDiagnosticsScope({ command: 'snapshot', logPath: LOG_PATH }, () => |
| 168 | + createRequestExecutionScope({ |
| 169 | + req: makeRequest({ command: 'snapshot' }), |
| 170 | + sessionStore, |
| 171 | + leaseRegistry, |
| 172 | + }), |
| 173 | + ); |
| 174 | + |
| 175 | + const inside = vi.fn(); |
| 176 | + await scope.runLocked(async () => { |
| 177 | + inside(getLeaseRegistryExecutionLocks(leaseRegistry).has('session:default')); |
| 178 | + return 'ran'; |
| 179 | + }); |
| 180 | + |
| 181 | + expect(inside).toHaveBeenCalledWith(true); |
| 182 | + expect(getLeaseRegistryExecutionLocks(leaseRegistry).has('session:default')).toBe(false); |
| 183 | +}); |
0 commit comments