Skip to content

Commit 9ffcb69

Browse files
committed
fix(daemon): settle implicit session ambiguity on the whole workspace
Review found two routes #2580's routing still chose a platform by accident. The platform-less branch returned the `default` leaf before consulting the workspace, so opening iOS without `--platform` and then Android with it left a bare `press` or `close` on the iOS device instead of refusing with AMBIGUOUS_MATCH. The `default` leaf is now one candidate among the workspace's sessions rather than a short-circuit, and it is still where a lone session lands. Candidate matching read `req.flags` only, so a request whose platform came from `--session-lock` claimed no platform and the workspace's opposite-platform session appeared to agree with it. The lock policy then leaves the platform unset because a session is already bound, so nothing downstream corrected the choice. Candidate matching now sees the effective platform.
1 parent 3cfc48e commit 9ffcb69

2 files changed

Lines changed: 80 additions & 7 deletions

File tree

‎src/daemon/__tests__/session-routing.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,47 @@ test('refuses a platform-less request when the workspace holds several implicit
298298
);
299299
});
300300

301+
test('routes a platform-less request to a sole default-leaf session', (t) => {
302+
const { cwd, scopeId } = makeWorkspaceCwd(t);
303+
const store = makeStore(t);
304+
const openedWithoutPlatform = `cwd:${scopeId}:default`;
305+
store.set(openedWithoutPlatform, makeWorkspaceSession(scopeId, IOS_SIMULATOR));
306+
307+
const resolved = resolveEffectiveSessionName(implicitRequest(cwd, {}, 'press'), store, {
308+
attachesToSession: true,
309+
});
310+
311+
assert.equal(resolved, openedWithoutPlatform);
312+
});
313+
314+
test('refuses a platform-less request when a default-leaf session shares the workspace with a platform session', (t) => {
315+
const { cwd, scopeId } = makeWorkspaceCwd(t);
316+
const store = makeStore(t);
317+
// The #2580 shape: iOS was opened without `--platform`, so it owns the `default` leaf, and
318+
// Android then took its own platform leaf. Preferring `default` here would run a bare `press`
319+
// on the iOS device the caller is no longer addressing.
320+
store.set(`cwd:${scopeId}:default`, makeWorkspaceSession(scopeId, IOS_SIMULATOR));
321+
store.set(`cwd:${scopeId}:android`, makeWorkspaceSession(scopeId, ANDROID_EMULATOR));
322+
323+
assert.throws(
324+
() =>
325+
resolveEffectiveSessionName(implicitRequest(cwd, {}, 'close'), store, {
326+
attachesToSession: true,
327+
}),
328+
(error: unknown) => {
329+
assert.ok(error instanceof AppError);
330+
assert.equal(error.code, 'AMBIGUOUS_MATCH');
331+
const sessions = (error.details?.sessions as string[] | undefined) ?? [];
332+
assert.deepEqual(sessions.slice().sort(), [
333+
`cwd:${scopeId}:android`,
334+
`cwd:${scopeId}:default`,
335+
]);
336+
assert.match(String(error.details?.hint), /--platform/);
337+
return true;
338+
},
339+
);
340+
});
341+
301342
test('keeps inventory commands routable across implicit session ambiguity', (t) => {
302343
const { cwd, scopeId } = makeWorkspaceCwd(t);
303344
const store = makeStore(t);
@@ -470,6 +511,25 @@ test('routes a session-lock platform into the implicit session key', (t) => {
470511
assert.equal(resolved, `cwd:${scopeId}:android`);
471512
});
472513

514+
test('routes a session-lock platform past a workspace session bound to another platform', (t) => {
515+
const { cwd, scopeId } = makeWorkspaceCwd(t);
516+
const store = makeStore(t);
517+
store.set(`cwd:${scopeId}:ios`, makeWorkspaceSession(scopeId, IOS_SIMULATOR));
518+
519+
// Candidate matching that reads only `flags` sees no platform here, so the iOS session appears
520+
// to agree and the request runs on it; the lock policy then leaves the platform unset because a
521+
// session is already bound, so nothing downstream corrects the choice.
522+
const request = implicitRequest(cwd, {}, 'press');
523+
524+
const resolved = resolveEffectiveSessionName(
525+
{ ...request, meta: { ...request.meta, lockPolicy: 'reject', lockPlatform: 'android' } },
526+
store,
527+
{ attachesToSession: true },
528+
);
529+
530+
assert.equal(resolved, `cwd:${scopeId}:android`);
531+
});
532+
473533
test('counts one session reachable under two addresses once', (t) => {
474534
const { cwd, scopeId } = makeWorkspaceCwd(t);
475535
const store = makeStore(t);

‎src/daemon/session-routing.ts‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function resolveImplicitWorkspaceSession(
128128
const platform = resolveImplicitPlatformSelector(req);
129129
return platform
130130
? resolvePlatformSessionAddress(req, sessionStore, scopeId, platform, candidates, options)
131-
: resolveUnselectedSessionAddress(sessionStore, scopeId, candidates, options);
131+
: resolveUnselectedSessionAddress(scopeId, candidates, options);
132132
}
133133

134134
type PlatformSessionMismatch = {
@@ -152,9 +152,10 @@ function resolvePlatformSessionAddress(
152152
): string {
153153
const platformAddress = formatScopedSessionName(scopeId, platform);
154154
if (sessionStore.get(platformAddress)) return platformAddress;
155+
const claimedFlags = withImplicitPlatform(req.flags, platform);
155156
const mismatches = candidates.map((ref) => ({
156157
ref,
157-
conflicts: listSessionSelectorConflicts(ref.session, req.flags),
158+
conflicts: listSessionSelectorConflicts(ref.session, claimedFlags),
158159
}));
159160
const agreeing = mismatches.filter((candidate) => candidate.conflicts.length === 0);
160161
const [soleAgreeing] = agreeing;
@@ -198,23 +199,35 @@ function disagreesOnPlatform(conflicts: SessionSelectorConflict[]): boolean {
198199
return conflicts.some((conflict) => conflict.key === 'platform');
199200
}
200201

202+
/**
203+
* The selectors this request claims, with the platform it named filled in. A `--session-lock`
204+
* platform reaches `flags` only after the key is chosen, so matching on `flags` alone would let an
205+
* Android lock join this workspace's iPhone session — and the lock policy leaves the platform unset
206+
* once a session is bound, so nothing downstream corrects the choice.
207+
*/
208+
function withImplicitPlatform(
209+
flags: CommandFlags | undefined,
210+
platform: PlatformSelector,
211+
): CommandFlags {
212+
return { ...flags, platform };
213+
}
214+
201215
/**
202216
* A request that names no platform has nothing to disambiguate with, so it joins the workspace's
203217
* only implicit session — what every single-platform caller does today — and refuses when the
204-
* workspace holds several.
218+
* workspace holds several. The `default` leaf is not preferred: opening iOS without `--platform`
219+
* leaves it owning that leaf beside a platform-keyed session, and a caller who stopped naming a
220+
* platform has expressed no preference for either.
205221
*/
206222
function resolveUnselectedSessionAddress(
207-
sessionStore: SessionStore,
208223
scopeId: string,
209224
candidates: SessionRef[],
210225
options: ImplicitSessionRoutingOptions,
211226
): string {
212-
const defaultAddress = formatScopedSessionName(scopeId, DEFAULT_SESSION_NAME);
213-
if (sessionStore.get(defaultAddress)) return defaultAddress;
214227
const [soleCandidate] = candidates;
215228
if (soleCandidate && candidates.length === 1) return soleCandidate.address;
216229
throwIfAmbiguousWorkspaceSession(options, candidates);
217-
return defaultAddress;
230+
return formatScopedSessionName(scopeId, DEFAULT_SESSION_NAME);
218231
}
219232

220233
/**

0 commit comments

Comments
 (0)