Skip to content

Commit defd554

Browse files
airoweclaude
andcommitted
fix(android): bound the dumpsys reads behind dialog and foreground checks
The blocking-dialog and foreground observations ran `adb shell dumpsys` with no timeout before and after every Android interaction, so a hung read waited for the client envelope and reset the daemon. Each dump read now carries a 10 s bound. A timed-out read counts as unanswered, the same `unknown` a read that printed nothing produces, so a slow probe still never refuses the command; the reader then skips its remaining variants, so one observation costs at most one bound. This unblocks the longpress envelope ledger in #3326, which cannot prove an envelope while these reads are unbounded. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c48d27a commit defd554

3 files changed

Lines changed: 94 additions & 6 deletions

File tree

‎packages/contracts/src/android-observation.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type AndroidObservationHost = Readonly<{
2727
runAdb(
2828
device: DeviceInfo,
2929
args: readonly string[],
30-
options?: Readonly<{ allowFailure?: boolean }>,
30+
options?: Readonly<{ allowFailure?: boolean; timeoutMs?: number }>,
3131
): Promise<AndroidObservationCommandResult>;
3232
readSnapshotNodes(device: DeviceInfo): Promise<SnapshotNode[]>;
3333
openApp(device: DeviceInfo, appBundleId: string): Promise<void>;

‎packages/platform-android/src/observation.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect, test, vi } from 'vitest';
22
import type { AndroidObservationHost } from '@agent-device/contracts/android-observation';
33
import type { DeviceInfo } from '@agent-device/kernel/device';
4+
import { AppError } from '@agent-device/kernel/errors';
45
import { createAndroidObservationAdapter } from './observation.ts';
56

67
const device = {
@@ -98,3 +99,70 @@ test('a transient empty dump never demotes a variant that previously answered',
9899

99100
expect(calls[thirdReadStart]).toBe('shell dumpsys window windows');
100101
});
102+
103+
function timedOutAdb(args: readonly string[]): AppError {
104+
return new AppError('COMMAND_FAILED', 'adb timed out', { cmd: 'adb', args, timeoutMs: 1 });
105+
}
106+
107+
test('bounds every dump read the foreground question makes', async () => {
108+
const host = hostFor(new Map());
109+
const observer = createAndroidObservationAdapter(host);
110+
111+
await observer.readAppState({ ...device, id: 'emulator-bounded' });
112+
113+
const calls = vi.mocked(host.runAdb).mock.calls;
114+
expect(calls.map(([, args]) => args.join(' '))).toEqual([
115+
'shell dumpsys window windows',
116+
'shell dumpsys window',
117+
'shell dumpsys activity activities',
118+
'shell dumpsys activity',
119+
]);
120+
for (const [, , options] of calls) {
121+
expect(options).toEqual({ allowFailure: true, timeoutMs: expect.any(Number) });
122+
expect(options?.timeoutMs).toBeGreaterThan(0);
123+
}
124+
});
125+
126+
test.each([
127+
{
128+
question: 'foreground',
129+
ask: (observer: ReturnType<typeof createAndroidObservationAdapter>, target: DeviceInfo) =>
130+
observer.readAppState(target),
131+
unanswered: {},
132+
},
133+
{
134+
question: 'blocking dialog',
135+
ask: (observer: ReturnType<typeof createAndroidObservationAdapter>, target: DeviceInfo) =>
136+
observer.readBlockingDialog(target),
137+
unanswered: { status: 'unknown' },
138+
},
139+
])(
140+
'a timed-out dump leaves the $question question unanswered and reads no further variant',
141+
async ({ question, ask, unanswered }) => {
142+
const host: AndroidObservationHost = {
143+
...hostFor(new Map()),
144+
runAdb: vi.fn(async (_device, args) => {
145+
throw timedOutAdb(args);
146+
}),
147+
};
148+
149+
await expect(
150+
ask(createAndroidObservationAdapter(host), { ...device, id: `emulator-hung-${question}` }),
151+
).resolves.toEqual(unanswered);
152+
expect(host.runAdb).toHaveBeenCalledTimes(1);
153+
},
154+
);
155+
156+
test('a dump that fails without timing out still fails the observation', async () => {
157+
const failure = new AppError('COMMAND_FAILED', 'adb: device offline', { cmd: 'adb' });
158+
const host: AndroidObservationHost = {
159+
...hostFor(new Map()),
160+
runAdb: vi.fn(async () => {
161+
throw failure;
162+
}),
163+
};
164+
165+
await expect(
166+
createAndroidObservationAdapter(host).readAppState({ ...device, id: 'emulator-offline' }),
167+
).rejects.toBe(failure);
168+
});

‎packages/platform-android/src/observation.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
} from '@agent-device/contracts/android-observation';
66
import type { AppStateRuntimeResult } from '@agent-device/contracts/app-state-runtime';
77
import type { DeviceInfo } from '@agent-device/kernel/device';
8+
import { isCommandTimeoutError } from '@agent-device/host-kit/command';
89
import { deviceShellArgv } from '@agent-device/kernel/device-shell';
910
import { AppError } from '@agent-device/kernel/errors';
1011
import {
@@ -32,6 +33,8 @@ const PERMISSION_PACKAGES = new Set([
3233
'com.google.android.packageinstaller',
3334
]);
3435

36+
const DUMP_READ_TIMEOUT_MS = 10_000;
37+
3538
type Question = 'foreground' | 'blockingDialog';
3639
type DumpReader = (words: readonly string[]) => Promise<string>;
3740
const everAnswered = new Map<string, Map<string, boolean>>();
@@ -79,18 +82,35 @@ export function createAndroidObservationAdapter(
7982
});
8083
}
8184

85+
/**
86+
* A dump that times out is an unanswered read, like one that printed nothing: the observation
87+
* reports `unknown` and never refuses the command. After one timeout the device is not answering,
88+
* so the reader skips its remaining variants and an observation costs at most one bound.
89+
*/
8290
function createDumpReader(host: AndroidObservationHost, device: DeviceInfo): DumpReader {
8391
const dumps = new Map<string, Promise<string>>();
92+
let timedOut = false;
8493
return (words) => {
94+
if (timedOut) return Promise.resolve('');
8595
const key = words.join(' ');
8696
const pending =
8797
dumps.get(key) ??
8898
host
89-
.runAdb(device, deviceShellArgv('adb', 'shell', words), { allowFailure: true })
90-
.then((result) => {
91-
recordSections(device, key, result.stdout);
92-
return result.stdout;
93-
});
99+
.runAdb(device, deviceShellArgv('adb', 'shell', words), {
100+
allowFailure: true,
101+
timeoutMs: DUMP_READ_TIMEOUT_MS,
102+
})
103+
.then(
104+
(result) => {
105+
recordSections(device, key, result.stdout);
106+
return result.stdout;
107+
},
108+
(error: unknown) => {
109+
if (!isCommandTimeoutError(error)) throw error;
110+
timedOut = true;
111+
return '';
112+
},
113+
);
94114
dumps.set(key, pending);
95115
return pending;
96116
};

0 commit comments

Comments
 (0)