Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions packages/daemon/src/core/session-recovery-hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import { errorResponse, type DaemonFailureResponse } from '@agent-device/kernel/

export type SessionRecoveryContext = 'device-in-use' | 'selector-conflict';

/**
* How much a reply may say about the session that holds a device. `named` reports that session's
* address; `anonymous` answers the same reply without it. The caller passes `anonymous` for the
* session of another client on a proxy shared under one token — a leased or tenant-scoped session
* its request may not see — where an address is another client's session handle and naming it
* would confirm that the session exists and holds this device (#4923). A local session of another
* workspace keeps its address: that is the handle `close --session` takes from there (#2580).
*/
export type HeldSessionNaming = 'named' | 'anonymous';

export type SessionRecoveryOptions = {
/** Whether the request disagrees with the bound session on *platform*, the one conflict another
* platform's implicit session actually answers. */
Expand Down Expand Up @@ -51,16 +61,26 @@ export function buildSessionRecoveryHint(
export function buildDeviceInUseBySessionError(
inUse: SessionRef,
device: DeviceInfo,
options: SessionRecoveryOptions = {},
options: SessionRecoveryOptions,
naming: HeldSessionNaming,
): DaemonFailureResponse {
return errorResponse('DEVICE_IN_USE', `Device is already in use by session "${inUse.address}".`, {
session: inUse.address,
deviceId: device.id,
deviceName: device.name,
...(options.waitedMs === undefined ? {} : { waitedMs: options.waitedMs }),
hint: buildSessionRecoveryHint(inUse, 'device-in-use', options),
dispatched: 'no',
});
return errorResponse(
'DEVICE_IN_USE',
naming === 'named'
? `Device is already in use by session "${inUse.address}".`
: GENERIC_IN_USE_MESSAGE,
{
...(naming === 'named' ? { session: inUse.address } : {}),
deviceId: device.id,
deviceName: device.name,
...(options.waitedMs === undefined ? {} : { waitedMs: options.waitedMs }),
hint:
naming === 'named'
? buildSessionRecoveryHint(inUse, 'device-in-use', options)
: buildAnonymousInUseHint(options),
dispatched: 'no',
},
);
}

/**
Expand Down Expand Up @@ -160,6 +180,20 @@ const HOLDER_MAY_BE_FOREIGN = 'The holding session may belong to another agent o
const CHOOSE_ANOTHER_DEVICE =
'Run agent-device devices and open another device with a device selector. ';

/** What every caller may learn about a holder it may not see: that there is one (#4923). */
export const GENERIC_IN_USE_MESSAGE = 'Device is already in use by another session.';

/**
* The recovery text when the holder stays unnamed: the device and the wait are still the caller's
* moves; the session is not.
*/
function buildAnonymousInUseHint(options: SessionRecoveryOptions): string {
return (
`${describeOpenWaitAttempt(options)}${HOLDER_MAY_BE_FOREIGN}${CHOOSE_ANOTHER_DEVICE}${waitForDeviceHint(options)}` +
'Run agent-device session list to see the sessions this request can act on.'
);
}

/**
* The record of a spent `--wait` budget, phrased so a caller that already waited is not told to
* wait again. Only an `open` can wait, so this is the only place the budget is spoken of.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ test('an owner reopen that lost the device mid-flight reports the loss instead o
expect(reopened.ok).toBe(false);
if (!reopened.ok) {
expect(reopened.error.code).toBe('DEVICE_IN_USE');
// The claim belongs to another daemon's state dir, so its recorded owner stays: the caller
// has to reach it through that workspace (#4923 keeps the cross-worktree recovery).
expect(reopened.error.message).toContain('shared');
}
expect(inspectDeviceClaims({ udid: device.id }, process.env)[0]?.claim?.ownerToken).toBe(
Expand Down
Loading
Loading