Skip to content

Commit 3fb0c4b

Browse files
committed
fix(limrun): report a lost session with the documented reason
A provider session lost after admission now reports owner-capability-missing, as a session lost before admission does. capabilities no longer advertises appstate when the owner's facts cannot be inspected, because appstate fails in that case too. It also walks appStateUse directly, so it follows the same order as admission.
1 parent 99e3dcf commit 3fb0c4b

5 files changed

Lines changed: 45 additions & 30 deletions

File tree

‎packages/daemon/src/sessions/session-lifecycle/internal/__tests__/session-capabilities.test.ts‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ test.each([
463463
});
464464
// The projection still reads one sessionless snapshot, because open/close/prepare/runtime
465465
// availability is a runtime fact. What the session owns is the appstate answer itself, so a
466-
// snapshot that reports appstate unavailable must not remove the command.
466+
// snapshot that reports appstate unavailable must not remove the command. The provider mode
467+
// matters: it reports no live read for this device, not that the owner has no live session.
467468
const inspectFacts: InspectDeviceRuntimeFacts = vi.fn(
468469
async (inspected: DeviceInfo) =>
469470
createAdmissionBinding(inspected, {
@@ -503,7 +504,30 @@ test.each([
503504
},
504505
);
505506

506-
test('capabilities drops session-owned appstate once the owner holds no live session', async () => {
507+
const ownerWithNoLiveSession: InspectDeviceRuntimeFacts = async (inspected: DeviceInfo) => {
508+
const { facts } = createAdmissionBinding(inspected, {
509+
appLogAvailable: false,
510+
networkAvailable: false,
511+
providerMode: 'provider-runtime',
512+
});
513+
return {
514+
...facts,
515+
operations: {
516+
...facts.operations,
517+
ensureReady: { available: false, reason: 'owner-capability-missing' },
518+
appState: { available: false, reason: 'owner-capability-missing' },
519+
},
520+
};
521+
};
522+
523+
const ownerThatCannotBeInspected: InspectDeviceRuntimeFacts = async () => {
524+
throw new Error('provider facts unavailable');
525+
};
526+
527+
test.each([
528+
['holds no live session', ownerWithNoLiveSession],
529+
['cannot be inspected', ownerThatCannotBeInspected],
530+
])('capabilities drops session-owned appstate when the owner %s', async (_label, inspectFacts) => {
507531
const device: DeviceInfo = {
508532
platform: 'apple',
509533
appleOs: 'ios',
@@ -523,22 +547,6 @@ test('capabilities drops session-owned appstate once the owner holds no live ses
523547
appName: 'Settings',
524548
appBundleId: 'com.example.settings',
525549
});
526-
const inspectFacts: InspectDeviceRuntimeFacts = async (inspected: DeviceInfo) => {
527-
const { facts } = createAdmissionBinding(inspected, {
528-
appLogAvailable: false,
529-
networkAvailable: false,
530-
providerMode: 'provider-runtime',
531-
});
532-
return {
533-
...facts,
534-
operations: {
535-
...facts.operations,
536-
ensureReady: { available: false, reason: 'owner-capability-missing' },
537-
appState: { available: false, reason: 'owner-capability-missing' },
538-
},
539-
};
540-
};
541-
542550
const response = await withTargetDeviceResolutionScope(
543551
async () => [device],
544552
async () =>

‎packages/daemon/src/sessions/session-lifecycle/internal/inventory.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ import {
4747
selectorTargetsSessionDevice,
4848
} from '../../../resources/session-device-resolution.ts';
4949
import { resolveSessionScope, sessionMatchesInventoryScope } from '../../session-routing.ts';
50-
import type { BoundDeviceRuntime, RuntimeFacts } from '@agent-device/contracts/platform-runtime';
50+
import type {
51+
BoundDeviceRuntime,
52+
RuntimeFacts,
53+
RuntimeOperationKey,
54+
} from '@agent-device/contracts/platform-runtime';
5155
import {
5256
type PlatformRuntimeOperations,
57+
appStateUse,
5358
appsRuntimeUse,
5459
} from '@agent-device/contracts/platform-runtime-operations';
5560
import { ensureAppsRuntimeReady, listAppsFromRuntime } from '../../../execution/apps-runtime.ts';
@@ -313,11 +318,11 @@ async function capabilitiesInventoryResponse(params: {
313318
function ownerRefusesSessionAppState(
314319
facts: RuntimeFacts<PlatformRuntimeOperations> | undefined,
315320
): boolean {
316-
if (!facts) return false;
317-
// In appStateUse order, as admission walks it: the first unavailable cell is what appstate reports.
318-
const unavailable = [facts.operations.ensureReady, facts.operations.appState].find(
319-
(fact) => !fact.available,
320-
);
321+
if (!facts) return true;
322+
const required: readonly RuntimeOperationKey<PlatformRuntimeOperations>[] = appStateUse.required;
323+
const unavailable = required
324+
.map((operation) => facts.operations[operation])
325+
.find((fact) => !fact.available);
321326
return unavailable?.available === false && ownerHoldsNoLiveSession(unavailable.reason);
322327
}
323328

‎packages/provider-limrun/src/app-log-runtime.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ test('serves provider-owned network from the canonical session app log without l
258258
expect(commandRun).not.toHaveBeenCalled();
259259
});
260260

261+
const ANDROID_APP_STATE = { package: 'com.example.app', activity: '.MainActivity' };
262+
261263
test.each([
262264
['iOS simulator', device],
263265
[
@@ -275,6 +277,7 @@ test.each([
275277
const owner = createLimrunPlatformRuntimeOwner(
276278
limrunOwnerOptions({
277279
getInteractor: () => ({}) as never,
280+
getAppState: async () => (runtimeDevice.platform === 'android' ? ANDROID_APP_STATE : {}),
278281
}),
279282
);
280283
const binding = await owner.bind({ device: runtimeDevice, intent: { kind: 'ordinary' }, scope });
@@ -319,10 +322,9 @@ test.each([
319322
id: runtimeDevice.id,
320323
booted: true,
321324
});
322-
await expect(binding.operations.appState?.()).resolves.toEqual({
323-
package: 'com.example.app',
324-
activity: '.MainActivity',
325-
});
325+
await expect(binding.operations.appState?.()).resolves.toEqual(
326+
runtimeDevice.platform === 'android' ? ANDROID_APP_STATE : {},
327+
);
326328
});
327329

328330
test('fails closed for a stale Android identity before exposing facts or binding operations', async () => {

‎packages/provider-limrun/src/app-state-read.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ test('a provider session lost before the read is refused with a typed reason', a
6464
await expect(readLimrunAppState(undefined, liveSignal())).rejects.toMatchObject({
6565
code: 'UNSUPPORTED_OPERATION',
6666
message: 'Limrun appstate requires an active provider session',
67-
details: { reason: 'limrun_lease_not_live' },
67+
details: { reason: 'owner-capability-missing' },
6868
});
6969
});

‎packages/provider-limrun/src/app-state-read.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function readLimrunAppState(
1717
throw new AppError(
1818
'UNSUPPORTED_OPERATION',
1919
'Limrun appstate requires an active provider session',
20-
{ reason: 'limrun_lease_not_live' },
20+
{ reason: 'owner-capability-missing' },
2121
);
2222
}
2323
if (session.platform === 'ios') {

0 commit comments

Comments
 (0)