Skip to content

Commit a7f42df

Browse files
fix(evals): apply earlier launcher commands before routing an open
opensIn checked each open in a new call against the sessions from before the call, so `close --session <owner>; open …` was told DEVICE_IN_USE. Replay the call's launcher commands in order instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 99fd128 commit a7f42df

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎evals/mcp-tool-use/devices.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,13 @@ it.effect(
854854
);
855855
// An open in the other thread's own session is not refused, and gets no route.
856856
assert.notInclude(told(commandLine(held, "open com.acme.app", owner)), "In the new call");
857+
// An open after the same call closes the other thread's session finds the device free.
858+
const reopen = told(`${commandLine(held, "close", owner)}; ${piped}`);
859+
assert.include(
860+
reopen,
861+
`\`${piped}\` drives Acme iPhone, ${IPHONE}, on build-mac, which no other session holds: it prints no DEVICE_IN_USE`,
862+
);
863+
assert.notInclude(reopen, `which session ${owner} holds`);
857864
// Neither do commands other than open.
858865
assert.notInclude(
859866
told(`${commandLine(spare, "screenshot /tmp/a.png")} && echo ok`),

‎evals/mcp-tool-use/devices.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,34 @@ export const agentDeviceSessions = (
366366

367367
/**
368368
* Each launcher `open` a new call makes, read as the tracker reads calls, with the other thread's
369-
* session that holds its device. agent-device refuses an open that has one with DEVICE_IN_USE.
369+
* session that holds its device and whether another session holds it at all. agent-device refuses
370+
* an open that has a holder with DEVICE_IN_USE. Each launcher command runs in order, so an earlier
371+
* `close --session <owner>` in the same call frees the device for a later open.
370372
*/
371373
const opensIn = (
372374
targets: ReadonlyArray<LauncherTarget>,
373375
{ sessions, variables }: ReturnType<typeof followSessions>,
374376
command: string,
375377
owners: ReadonlyArray<AgentDeviceOwner>,
376-
) =>
377-
shellInvocations(
378+
) => {
379+
const current = new Map(sessions);
380+
return shellInvocations(
378381
withAliases(command, launcherAliases(command, targets)),
379382
new Map(variables),
380383
false,
381384
true,
382385
).flatMap((action) => {
383386
const call = action.remote ? undefined : launcherCall(targets, action.words);
384-
return call?.command === "open" && call.tracked !== undefined
385-
? [{ call, words: action.words, owner: holder(call, call.tracked, sessions, owners) }]
386-
: [];
387+
if (call?.tracked === undefined) return [];
388+
const owner = holder(call, call.tracked, current, owners);
389+
const held = owners.some(
390+
(candidate) =>
391+
candidate.deviceId === call.deviceId && current.get(candidate.session) !== undefined,
392+
);
393+
if (owner === undefined) current.set(call.tracked, afterCall(call, current.get(call.tracked)));
394+
return call.command === "open" ? [{ call, words: action.words, owner, held }] : [];
387395
});
396+
};
388397

389398
/**
390399
* What the shell simulator is told of the sessions the launchers name, and other threads', with
@@ -891,18 +900,13 @@ export const makeDeviceTools = Effect.fn("evaluateMcpToolUse.makeDeviceTools")(f
891900
const opens =
892901
command === undefined || owners.length === 0
893902
? []
894-
: opensIn(targets, followed, command, owners).flatMap(({ call, words, owner }) => {
903+
: opensIn(targets, followed, command, owners).flatMap(({ call, words, owner, held }) => {
895904
const opened = `\`${words.join(" ")}\` drives ${deviceName(call.deviceId)}`;
896905
const error = owner && inUseError(call, owner);
897906
if (owner !== undefined && error !== undefined)
898907
return [
899908
`${opened}, which session ${owner.session} holds: it fails with exit code 1, printing "Error (${error.code}): ${error.message}" and "Hint: ${error.hint}"`,
900909
];
901-
const held = owners.some(
902-
(candidate) =>
903-
candidate.deviceId === call.deviceId &&
904-
followed.sessions.get(candidate.session) !== undefined,
905-
);
906910
return held
907911
? []
908912
: [`${opened}, which no other session holds: it prints no DEVICE_IN_USE`];

0 commit comments

Comments
 (0)