Skip to content

Commit b27e310

Browse files
committed
fix(daemon): keep the whole replay lock probe advisory
Making the Maestro engine lazy hoisted the bundle-entry read and format resolution out of the try that tolerates invalid replay plans (#1802). The HTTP boundary validates flags only as an object, so a wire request can carry a bundle whose entry is missing or non-string, and path.extname would then reject the request with a TypeError during advisory lock selection instead of falling back to normal device resolution. Wrap the whole probe in the advisory guard again; the engine import stays inside it.
1 parent f9c9b82 commit b27e310

2 files changed

Lines changed: 46 additions & 14 deletions

File tree

‎src/daemon/__tests__/replay-device-selection.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { test, expect } from 'vitest';
22
import fs from 'node:fs';
33
import path from 'node:path';
44
import { parseReplayInput } from '@agent-device/ad-script';
5+
import type { ReplayScriptSourceBundle } from '@agent-device/contracts/replay';
56
import { buildReplayTargetDeviceResolution } from '../replay-device-selection.ts';
67
import {
78
appTargetResolutionOptions,
@@ -65,6 +66,36 @@ test('native replay uses its authored Android runtime setting without an iOS app
6566
expect(resolution?.options).toBeUndefined();
6667
});
6768

69+
test('a replay whose wire bundle has a malformed entry stays advisory', async () => {
70+
// The HTTP boundary validates `flags` only as an object, so a remote caller
71+
// can deliver a bundle without a string `entry`. #1802 keeps lock binding
72+
// advisory: the probe must fall back to normal device resolution, and the
73+
// replay handler stays the one to reject the request.
74+
const malformedBundles = [
75+
// A missing entry fails the bundle read itself.
76+
{ entry: undefined, files: {} },
77+
// A non-string entry survives the read — `files` is keyed by the coerced
78+
// string — and reaches format resolution, where `path.extname` rejects it.
79+
{ entry: 42, files: { '42': 'open demo://checkout\n' } },
80+
] as unknown as ReplayScriptSourceBundle[];
81+
82+
for (const bundle of malformedBundles) {
83+
await expect(
84+
buildReplayTargetDeviceResolution({
85+
token: 'test-token',
86+
session: 'default',
87+
command: 'replay',
88+
positionals: [],
89+
flags: {
90+
replayBackend: 'maestro',
91+
replayScriptSource: bundle,
92+
},
93+
meta: { cwd: mkdtempForTestSync('agent-device-replay-device-selection-') },
94+
}),
95+
).resolves.toBeUndefined();
96+
}
97+
});
98+
6899
test('a Maestro flow pre-binds an iOS replay to its static appId', async () => {
69100
const root = mkdtempForTestSync('agent-device-replay-device-selection-');
70101
const flowPath = path.join(root, 'checkout.yaml');

‎src/daemon/replay-device-selection.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,21 @@ export async function buildReplayTargetDeviceResolution(
3434
const bundle = req.flags?.replayScriptSource;
3535
if (!bundle) return undefined;
3636

37-
const resolved = bundle.entry;
38-
if (resolveReplayFormat(resolved, req.flags?.replayBackend) === 'maestro') {
39-
const { inspectMaestroFlow } = await import('@agent-device/maestro');
40-
return readAdvisoryResolution(() => {
41-
const flow = inspectMaestroFlow(readReplayScriptSourceFile(bundle, resolved), resolved);
37+
return readAdvisoryResolution(async () => {
38+
const resolved = bundle.entry;
39+
const source = readReplayScriptSourceFile(bundle, resolved);
40+
if (resolveReplayFormat(resolved, req.flags?.replayBackend) === 'maestro') {
41+
const { inspectMaestroFlow } = await import('@agent-device/maestro');
42+
const flow = inspectMaestroFlow(source, resolved);
4243
return {
4344
flags: req.flags ?? {},
4445
options: buildMaestroReplayTargetDeviceResolutionOptions(
4546
flow.appTarget,
4647
req.flags?.platform,
4748
),
4849
};
49-
});
50-
}
51-
return readAdvisoryResolution(() => {
52-
const parsed = parseReplayInput(readReplayScriptSourceFile(bundle, resolved), req.flags);
50+
}
51+
const parsed = parseReplayInput(source, req.flags);
5352
const selection = readScriptReplaySelection(parsed.actions);
5453
if (!selection.appTarget) return undefined;
5554
const scriptFlags = buildReplayScriptPlatformFlags(req.flags, parsed.actions);
@@ -62,14 +61,16 @@ export async function buildReplayTargetDeviceResolution(
6261
});
6362
}
6463

65-
function readAdvisoryResolution(
66-
read: () => ReplayTargetDeviceResolution | undefined,
67-
): ReplayTargetDeviceResolution | undefined {
64+
async function readAdvisoryResolution(
65+
read: () => Promise<ReplayTargetDeviceResolution | undefined>,
66+
): Promise<ReplayTargetDeviceResolution | undefined> {
6867
try {
69-
return read();
68+
return await read();
7069
} catch {
7170
// Parsing and validation stay in the replay handler. Lock binding is only
72-
// advisory, so an unreadable/invalid plan must not mask its real error.
71+
// advisory, so an unreadable/invalid plan must not mask its real error. The
72+
// whole probe sits behind this guard, including the wire bundle's entry and
73+
// format, which the request boundary only checks as an object.
7374
return undefined;
7475
}
7576
}

0 commit comments

Comments
 (0)