Skip to content

Commit 8c98dc0

Browse files
committed
fix(daemon): disclose system surfaces on sessionless selector routes; pin disclosure composition (PR #1301 review)
Sessionless find/wait never store the consumed capture on a session record, so the disclosure read from the session store returned nothing. The selector capture runtime now reports every consumed snapshot through a shared slot on the runtime params, and disclosure reads prefer it; the session-store read remains only as a fallback for pre-captured snapshots. Regressions pin the sessionless route end-to-end and that the disclosure appends after existing success warnings and failure hints instead of replacing them.
1 parent bbbac6e commit 8c98dc0

4 files changed

Lines changed: 89 additions & 7 deletions

File tree

‎src/daemon/handlers/__tests__/system-surface-disclosure.test.ts‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ vi.mock('../../../core/dispatch.ts', async (importOriginal) => {
1111
return {
1212
...actual,
1313
dispatchCommand: vi.fn(async () => ({})),
14-
resolveTargetDevice: actual.resolveTargetDevice,
14+
resolveTargetDevice: vi.fn(actual.resolveTargetDevice),
1515
};
1616
});
1717

18-
import { dispatchCommand } from '../../../core/dispatch.ts';
18+
vi.mock('../../device-ready.ts', () => ({
19+
ensureDeviceReady: vi.fn(async () => {}),
20+
}));
21+
22+
import { dispatchCommand, resolveTargetDevice } from '../../../core/dispatch.ts';
23+
import { ANDROID_EMULATOR } from '../../../__tests__/test-utils/index.ts';
24+
import { withSystemSurfaceDisclosure } from '../system-surface-disclosure.ts';
1925

2026
const mockDispatch = vi.mocked(dispatchCommand);
2127

@@ -126,3 +132,63 @@ test('wait timeout for app text hidden behind a system surface discloses the occ
126132
expect(response.error.message).toMatch(/wait timed out for text: Bakery list/);
127133
expect(String(response.error.details?.hint)).toContain(ANDROID_SYSTEM_SURFACE_DISCLOSURE);
128134
});
135+
136+
test('sessionless read-only find still discloses the occluding system surface', async () => {
137+
vi.mocked(resolveTargetDevice).mockResolvedValueOnce(ANDROID_EMULATOR);
138+
const sessionStore = makeSessionStore();
139+
140+
const response = await dispatchFindReadOnlyViaRuntime({
141+
req: {
142+
token: 't',
143+
session: 'default',
144+
command: 'find',
145+
positionals: ['Internet', 'exists'],
146+
flags: {},
147+
} as DaemonRequest,
148+
sessionName: 'default',
149+
logPath: '/tmp/test.log',
150+
sessionStore,
151+
});
152+
153+
expect(response?.ok).toBe(true);
154+
if (!response?.ok) return;
155+
const data = response.data as Record<string, unknown>;
156+
expect(data.found).toBe(true);
157+
// No session record exists to read the capture back from: the disclosure must come from the
158+
// consumed capture itself.
159+
expect(sessionStore.get('default')).toBeUndefined();
160+
expect(String(data.warning)).toContain(ANDROID_SYSTEM_SURFACE_DISCLOSURE);
161+
});
162+
163+
test('disclosure appends after an existing success warning instead of replacing it', () => {
164+
const response = withSystemSurfaceDisclosure(
165+
{ ok: true, data: { found: true, warning: 'prior warning text' } },
166+
{ systemSurfaceOnly: true },
167+
);
168+
expect(response.ok).toBe(true);
169+
if (!response.ok) return;
170+
const warning = String((response.data as Record<string, unknown>).warning);
171+
expect(warning).toContain('prior warning text');
172+
expect(warning).toContain(ANDROID_SYSTEM_SURFACE_DISCLOSURE);
173+
expect(warning.indexOf('prior warning text')).toBeLessThan(
174+
warning.indexOf(ANDROID_SYSTEM_SURFACE_DISCLOSURE),
175+
);
176+
});
177+
178+
test('disclosure appends after an existing failure hint instead of replacing it', () => {
179+
const response = withSystemSurfaceDisclosure(
180+
{
181+
ok: false,
182+
error: { code: 'NOT_FOUND', message: 'no match', details: { hint: 'prior hint text' } },
183+
},
184+
{ systemSurfaceOnly: true },
185+
);
186+
expect(response.ok).toBe(false);
187+
if (response.ok) return;
188+
const hint = String(response.error.details?.hint);
189+
expect(hint).toContain('prior hint text');
190+
expect(hint).toContain(ANDROID_SYSTEM_SURFACE_DISCLOSURE);
191+
expect(hint.indexOf('prior hint text')).toBeLessThan(
192+
hint.indexOf(ANDROID_SYSTEM_SURFACE_DISCLOSURE),
193+
);
194+
});

‎src/daemon/selector-capture-runtime.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type SelectorCaptureRuntimeParams = {
2121
sessionName: string;
2222
req: DaemonRequest;
2323
logPath?: string;
24+
// Sessionless routes have no session record to read the consumed capture back from, so the
25+
// capture runtime reports every consumed snapshot here for response-level disclosures.
26+
consumedSnapshot?: { state?: SnapshotState };
2427
};
2528

2629
/**
@@ -62,6 +65,11 @@ export function createSelectorCaptureRuntime(params: SelectorCaptureRuntimeParam
6265
let lastSnapshotResult: SelectorCaptureResult | undefined;
6366
let lastSnapshotCacheKey: string | undefined;
6467

68+
const remember = (result: SelectorCaptureResult): SelectorCaptureResult => {
69+
if (params.consumedSnapshot) params.consumedSnapshot.state = result.snapshot;
70+
return result;
71+
};
72+
6573
const capture = async (request: SelectorCaptureRequest): Promise<SelectorCaptureResult> => {
6674
const timestamp = Date.now();
6775
const cacheKey = selectorCaptureCacheKey(request, params.req.flags?.out);
@@ -75,15 +83,15 @@ export function createSelectorCaptureRuntime(params: SelectorCaptureRuntimeParam
7583
cacheKey,
7684
});
7785
if (reusableLastSnapshot) {
78-
return reusableLastSnapshot;
86+
return remember(reusableLastSnapshot);
7987
}
8088

8189
const sessionSnapshot = reusableSessionSnapshot({ session, timestamp, request });
8290
if (sessionSnapshot) {
8391
lastSnapshotAt = sessionSnapshot.createdAt;
8492
lastSnapshotResult = { snapshot: sessionSnapshot };
8593
lastSnapshotCacheKey = cacheKey;
86-
return lastSnapshotResult;
94+
return remember(lastSnapshotResult);
8795
}
8896

8997
const snapshot = await captureSelectorSnapshot({ params, request });
@@ -92,7 +100,7 @@ export function createSelectorCaptureRuntime(params: SelectorCaptureRuntimeParam
92100
lastSnapshotAt = timestamp;
93101
lastSnapshotResult = result;
94102
lastSnapshotCacheKey = cacheKey;
95-
return result;
103+
return remember(result);
96104
};
97105

98106
return { capture };

‎src/daemon/selector-runtime-backend.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { resolveTargetDevice, type CommandFlags } from '../core/dispatch.ts';
77
import { createAgentDevice } from '../runtime.ts';
88
import { isMacOs, isApplePlatform, publicPlatformString } from '../kernel/device.ts';
99
import { noActiveSessionError, requireCommandSupported } from './handlers/response.ts';
10-
import type { SnapshotNode } from '../kernel/snapshot.ts';
10+
import type { SnapshotState, SnapshotNode } from '../kernel/snapshot.ts';
1111
import { findNodeByLabel } from '../snapshot/snapshot-processing.ts';
1212
import { runAppleRunnerCommand } from '../platforms/apple/core/runner/runner-client.ts';
1313
import { buildAppleRunnerRequestOptions } from './apple-runner-options.ts';
@@ -29,6 +29,9 @@ export type SelectorRuntimeParams = {
2929
logPath?: string;
3030
sessionStore: SessionStore;
3131
contextFromFlags?: ContextFromFlags;
32+
// Filled by the capture runtime with the snapshot each selector command actually consumed;
33+
// sessionless routes disclose from here because no session record stores the capture.
34+
consumedSnapshot?: { state?: SnapshotState };
3235
};
3336

3437
type SelectorRuntimeDeviceParams = SelectorRuntimeParams & {
@@ -77,6 +80,7 @@ export async function createSelectorRuntime(
7780
| { ok: true; runtime: ReturnType<typeof createSelectorRuntimeForDevice> }
7881
| { ok: false; response: DaemonResponse }
7982
> {
83+
params.consumedSnapshot ??= {};
8084
const session = params.sessionStore.get(params.sessionName);
8185
if (!session && options.requireSession) {
8286
return {
@@ -106,6 +110,7 @@ function createSelectorBackend(params: SelectorRuntimeDeviceParams): AgentDevice
106110
sessionStore,
107111
sessionName,
108112
req,
113+
consumedSnapshot: params.consumedSnapshot,
109114
logPath,
110115
});
111116
return {
@@ -236,6 +241,7 @@ async function captureWaitSnapshot(params: SelectorRuntimeDeviceParams) {
236241
sessionName: params.sessionName,
237242
req: params.req,
238243
logPath: params.logPath,
244+
consumedSnapshot: params.consumedSnapshot,
239245
});
240246
const { snapshot } = await captureRuntime.capture({
241247
flags: {

‎src/daemon/selector-runtime.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ export async function dispatchFindReadOnlyViaRuntime(
129129
}
130130

131131
function consumedSessionSnapshot(params: SelectorRuntimeParams) {
132-
return params.sessionStore.get(params.sessionName)?.snapshot;
132+
// The capture runtime reports the consumed snapshot directly; sessionless selector routes have
133+
// no session record, so the stored-session read is only a fallback for pre-captured snapshots.
134+
return params.consumedSnapshot?.state ?? params.sessionStore.get(params.sessionName)?.snapshot;
133135
}
134136

135137
export async function dispatchGetViaRuntime(

0 commit comments

Comments
 (0)