Skip to content

Commit 0a516a6

Browse files
strzhaoclaude
andcommitted
fix(daemon): session list claim advisory answers off the event loop
Review follow-up on the session list foreign-claim advisory. The claim read behind `session_list` and `devices` is a daemon request path, so it now waits on the async host-kit process snapshot instead of stalling the loop behind one `spawnSync` of the host process tool — `session_list` is the liveness probe's request, and a false "silent" verdict resets a live shared daemon. The in-scope exclusion gains a sibling negative: a named-local session answers the caller's cwd scope, so a device it holds is not foreign. Both route tests isolate claims through the shared `isolatedDeviceClaimStores` support fixture. `commands.md` documents `claimedByOtherSessions` on the raw daemon RPC response, which gateways and embedding hosts read; the Node client keeps the in-scope session array. Co-Authored-By: Claude Code <[email protected]>
1 parent 9e27a03 commit 0a516a6

6 files changed

Lines changed: 224 additions & 149 deletions

File tree

‎packages/daemon-contracts/src/device-claim-inspection.ts‎

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
} from '@agent-device/kernel/device';
99
import {
1010
classifyOwnerLivenessFromObservation,
11+
type HostProcessIdentityObservation,
1112
type OwnerLiveness,
1213
readHostProcessIdentityObservations,
14+
readHostProcessIdentityObservationsAsync,
1315
} from '@agent-device/host-kit/process';
1416

1517
import { isSupersededDaemonOwner } from './daemon-registration.ts';
@@ -46,14 +48,7 @@ export function inspectDeviceClaims(
4648
.map((entry) => readDeviceClaimFile(path.join(root, entry.name)))
4749
.filter((entry): entry is InspectedDeviceClaim => entry !== null)
4850
.filter((entry) => matchesClaimSelectors(entry.claim, selectors));
49-
const observations = readHostProcessIdentityObservations(
50-
parsed.flatMap((entry) => (entry.claim ? [entry.claim.ownerPid] : [])),
51-
);
52-
return parsed.map((entry) =>
53-
entry.claim
54-
? classifyInspectedClaim(entry, observations.get(entry.claim.ownerPid) ?? null)
55-
: entry,
56-
);
51+
return classifiedClaimEntries(parsed, readHostProcessIdentityObservations(ownerPidsOf(parsed)));
5752
}
5853

5954
function readClaimEntries(
@@ -62,14 +57,86 @@ function readClaimEntries(
6257
try {
6358
return { entries: fs.readdirSync(root, { withFileTypes: true }) };
6459
} catch (error) {
65-
const code = (error as NodeJS.ErrnoException).code;
66-
if (code === 'ENOENT' || code === 'ENOTDIR') return { claims: [] };
67-
return {
68-
claims: [{ fileName: path.basename(root), classification: 'unknown', error: String(error) }],
69-
};
60+
return unreadableClaimRoot(root, error);
61+
}
62+
}
63+
64+
/**
65+
* {@link inspectDeviceClaims} off the event loop, with the same classifications. Daemon request
66+
* paths answer through this one: the synchronous probe blocks the loop for the whole process-tool
67+
* snapshot, which stalls every other request, timer, and abort handler behind it.
68+
*/
69+
export async function inspectDeviceClaimsAsync(
70+
selectors: DeviceClaimSelectors,
71+
env: EnvMap,
72+
): Promise<InspectedDeviceClaim[]> {
73+
const root = resolveDeviceClaimRoot(env);
74+
const listed = await readClaimEntriesAsync(root);
75+
if ('claims' in listed) return listed.claims;
76+
const parsed = (
77+
await Promise.all(
78+
listed.entries
79+
.filter((entry) => entry.isFile() && entry.name.endsWith('.json'))
80+
.map((entry) => readDeviceClaimFileAsync(path.join(root, entry.name))),
81+
)
82+
)
83+
.filter((entry): entry is InspectedDeviceClaim => entry !== null)
84+
.filter((entry) => matchesClaimSelectors(entry.claim, selectors));
85+
return classifiedClaimEntries(
86+
parsed,
87+
await readHostProcessIdentityObservationsAsync(ownerPidsOf(parsed)),
88+
);
89+
}
90+
91+
async function readClaimEntriesAsync(
92+
root: string,
93+
): Promise<{ entries: fs.Dirent[] } | { claims: InspectedDeviceClaim[] }> {
94+
try {
95+
return { entries: await fs.promises.readdir(root, { withFileTypes: true }) };
96+
} catch (error) {
97+
return unreadableClaimRoot(root, error);
7098
}
7199
}
72100

101+
async function readDeviceClaimFileAsync(filePath: string): Promise<InspectedDeviceClaim | null> {
102+
const fileName = path.basename(filePath);
103+
try {
104+
return inspectClaimContents(fileName, await fs.promises.readFile(filePath, 'utf8'));
105+
} catch (error) {
106+
return unreadableClaimFile(fileName, error);
107+
}
108+
}
109+
110+
function ownerPidsOf(parsed: readonly InspectedDeviceClaim[]): number[] {
111+
return parsed.flatMap((entry) => (entry.claim ? [entry.claim.ownerPid] : []));
112+
}
113+
114+
function classifiedClaimEntries(
115+
parsed: readonly InspectedDeviceClaim[],
116+
observations: ReadonlyMap<number, HostProcessIdentityObservation>,
117+
): InspectedDeviceClaim[] {
118+
return parsed.map((entry) =>
119+
entry.claim
120+
? classifyInspectedClaim(entry, observations.get(entry.claim.ownerPid) ?? null)
121+
: entry,
122+
);
123+
}
124+
125+
/** An absent claim root holds no claims; anything else is one unknown-evidence entry. */
126+
function unreadableClaimRoot(root: string, error: unknown): { claims: InspectedDeviceClaim[] } {
127+
const code = (error as NodeJS.ErrnoException).code;
128+
if (code === 'ENOENT' || code === 'ENOTDIR') return { claims: [] };
129+
return {
130+
claims: [{ fileName: path.basename(root), classification: 'unknown', error: String(error) }],
131+
};
132+
}
133+
134+
function unreadableClaimFile(fileName: string, error: unknown): InspectedDeviceClaim | null {
135+
const code = (error as NodeJS.ErrnoException).code;
136+
if (code === 'ENOENT' || code === 'ENOTDIR') return null;
137+
return { fileName, classification: 'unknown', error: String(error) };
138+
}
139+
73140
function matchesClaimSelectors(
74141
claim: DeviceClaim | undefined,
75142
selectors: DeviceClaimSelectors,
@@ -110,13 +177,7 @@ function readDeviceClaimFile(filePath: string): InspectedDeviceClaim | null {
110177
try {
111178
return inspectClaimContents(fileName, fs.readFileSync(filePath, 'utf8'));
112179
} catch (error) {
113-
const code = (error as NodeJS.ErrnoException).code;
114-
if (code === 'ENOENT' || code === 'ENOTDIR') return null;
115-
return {
116-
fileName,
117-
classification: 'unknown',
118-
error: String(error),
119-
};
180+
return unreadableClaimFile(fileName, error);
120181
}
121182
}
122183

‎packages/daemon-contracts/src/device-claims.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export {
5252
deviceClaimOwnerCannotRelease,
5353
deviceClaimRequiresStaleInspection,
5454
inspectDeviceClaims,
55+
inspectDeviceClaimsAsync,
5556
} from './device-claim-inspection.ts';
5657
export type {
5758
DeviceClaimClassification,

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import {
2-
bindTestCommandHost,
3-
createTestHostEnvironment,
4-
mkdtempForTestSync,
5-
} from '@agent-device/test-kit';
1+
import { bindTestCommandHost, createTestHostEnvironment } from '@agent-device/test-kit';
62
import type { DaemonResponse } from '@agent-device/kernel/contracts';
73
import { daemonPlatformServicesFixture } from '../../../__tests__/support/platform-services-fixture.ts';
8-
import { afterAll, beforeEach, expect, test, vi } from 'vitest';
9-
import fs from 'node:fs';
4+
import { isolatedDeviceClaimStores } from '../../../__tests__/support/device-claim-store.ts';
5+
import { beforeEach, expect, test, vi } from 'vitest';
106
import type { DaemonRequest } from '../../../core/daemon-request.ts';
117
import { makeSessionStore } from '../../../__tests__/support/store-factory.ts';
128
import { sessionOrDeviceSelectorRequiredDetails } from '@agent-device/kernel/errors';
@@ -66,20 +62,12 @@ function request(command: DaemonRequest['command']): DaemonRequest {
6662
}
6763

6864
// Inventory responses read the host's device-claim root through the session store, whose fixture
69-
// host carries the live process.env. An empty pinned root keeps the host's live claims out of
65+
// host carries the live process.env. An isolated per-test root keeps the host's live claims out of
7066
// these route assertions instead of making them depend on what this machine holds.
71-
const isolatedClaimsDir = mkdtempForTestSync('agent-device-session-route-claims-');
72-
const previousClaimsDir = process.env.AGENT_DEVICE_CLAIMS_DIR;
73-
beforeEach(() => {
74-
process.env.AGENT_DEVICE_CLAIMS_DIR = isolatedClaimsDir;
75-
});
76-
afterAll(() => {
77-
if (previousClaimsDir === undefined) delete process.env.AGENT_DEVICE_CLAIMS_DIR;
78-
else process.env.AGENT_DEVICE_CLAIMS_DIR = previousClaimsDir;
79-
fs.rmSync(isolatedClaimsDir, { recursive: true, force: true });
80-
});
67+
const setupClaims = isolatedDeviceClaimStores('agent-device-session-route-claims-');
8168

8269
async function run(command: DaemonRequest['command']): Promise<DaemonResponse | null> {
70+
setupClaims();
8371
return await handleSessionCommands({
8472
host: createTestHostEnvironment(),
8573
createCommandSurface: fakeCreateCommandSurface,

0 commit comments

Comments
 (0)