Skip to content

Commit 2097502

Browse files
committed
fix(daemon): sleep a sessionless duration wait without resolving a device
wait <ms> without a session resolved a target device through device inventory before sleeping, although a sleep observes no device and records nothing. It paid discovery latency and failed with DEVICE_NOT_FOUND or an ambiguous-device error when no device was selectable. A sessionless sleep now sleeps on the request signal directly; a named session that does not exist is still refused. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4
1 parent e01b160 commit 2097502

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎packages/daemon/src/execution/wait-runtime.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ import { handleSnapshotCommands } from '../sessions/handlers/snapshot.ts';
3333
import { resolveBoundSelectorCapture } from './selector-capture-binding.ts';
3434
import type { DaemonRequest } from '../index.ts';
3535

36+
vi.mock('@agent-device/device-selection/dispatch-resolve', async (importOriginal) => {
37+
const actual =
38+
await importOriginal<typeof import('@agent-device/device-selection/dispatch-resolve')>();
39+
return { ...actual, resolveTargetDevice: vi.fn(actual.resolveTargetDevice) };
40+
});
41+
42+
import { resolveTargetDevice } from '@agent-device/device-selection/dispatch-resolve';
43+
3644
const testCreateCommandSurface = commandSurfaceWithMembers((config) => ({
3745
selectors: {
3846
wait: async ({ target, session, requestId, signal }) => {
@@ -289,6 +297,31 @@ test('a duration wait inspects no facts and binds no device', async () => {
289297
}
290298
});
291299

300+
test('a sessionless duration wait sleeps without resolving a target device', async () => {
301+
vi.mocked(resolveTargetDevice).mockRejectedValue(
302+
new AppError('DEVICE_NOT_FOUND', 'No device is selectable'),
303+
);
304+
try {
305+
const harness = waitRuntimeHarness({ capture: captureUnavailable });
306+
const response = await handleSnapshotCommands({
307+
createCommandSurface: testCreateCommandSurface,
308+
platformServices: daemonPlatformServicesFixture(),
309+
req: { command: 'wait', positionals: ['1'], token: 't', flags: {} } as DaemonRequest,
310+
sessionName: 'default',
311+
logPath: '/tmp/daemon.log',
312+
sessionStore: makeSessionStore('agent-device-wait-runtime-sessionless-'),
313+
inspectFacts: harness.inspectFacts,
314+
bindDevice: harness.bindDevice,
315+
});
316+
317+
expect(response).toEqual({ ok: true, data: { waitedMs: 1 } });
318+
expect(resolveTargetDevice).not.toHaveBeenCalled();
319+
expect(harness.inspectFacts).not.toHaveBeenCalled();
320+
} finally {
321+
vi.mocked(resolveTargetDevice).mockReset();
322+
}
323+
});
324+
292325
test('an observing wait binds through the shared selector entry', async () => {
293326
const harness = waitRuntimeHarness({ findText: available });
294327

‎packages/daemon/src/execution/wait-runtime.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import {
3838
type SelectorRuntimeParams,
3939
} from './selector-runtime-backend.ts';
4040
import { errorResponse } from '@agent-device/kernel/contracts';
41+
import { getRequestSignal } from '@agent-device/host-kit/request';
42+
import { sleep } from '@agent-device/host-kit/retry';
4143

4244
type DispatchWaitParams = SelectorRuntimeParams &
4345
Readonly<{ inspectFacts?: InspectDeviceRuntimeFacts; bindDevice?: BindDeviceRuntime }>;
@@ -47,6 +49,9 @@ export async function dispatchWaitViaRuntime(params: DispatchWaitParams): Promis
4749
const parsedOrResponse = parseWaitRequest(req);
4850
if ('ok' in parsedOrResponse) return parsedOrResponse;
4951
const parsed = parsedOrResponse;
52+
if (parsed.kind === 'sleep' && !sessionStore.lookup(sessionName)) {
53+
return await sleepWithoutSession(req, sessionName, parsed.durationMs);
54+
}
5055
const { ref, session, device } = await resolveSessionDevice(sessionStore, sessionName, req);
5156
// ADR 0019: facts are the only support authority, through the selector family's one
5257
// admit-then-bind entry. A duration wait observes nothing, so it never asks for a binding —
@@ -98,6 +103,27 @@ export async function dispatchWaitViaRuntime(params: DispatchWaitParams): Promis
98103
});
99104
}
100105

106+
/**
107+
* A sessionless duration wait observes no device and records nothing, so it never resolves a
108+
* target device: device discovery would only add latency, or fail when no device is selectable.
109+
* A named session that does not exist is still refused.
110+
*/
111+
async function sleepWithoutSession(
112+
req: DaemonRequest,
113+
sessionName: string,
114+
durationMs: number,
115+
): Promise<DaemonResponse> {
116+
const { requireNamedSessionHoldsDevice } = await import('../core/explicit-session-resolution.ts');
117+
requireNamedSessionHoldsDevice({ req, sessionAddress: sessionName, session: undefined });
118+
return await toDaemonResponse(async () => {
119+
const signal = getRequestSignal(req.meta?.requestId);
120+
signal?.throwIfAborted();
121+
await sleep(durationMs, signal);
122+
signal?.throwIfAborted();
123+
return toDaemonWaitData({ waitedMs: durationMs });
124+
});
125+
}
126+
101127
function parseWaitRequest(
102128
req: DaemonRequest,
103129
): Exclude<WaitParsed, { kind: 'invalid' }> | DaemonResponse {

0 commit comments

Comments
 (0)