@@ -8,8 +8,10 @@ import {
88} from '@agent-device/kernel/device' ;
99import {
1010 classifyOwnerLivenessFromObservation ,
11+ type HostProcessIdentityObservation ,
1112 type OwnerLiveness ,
1213 readHostProcessIdentityObservations ,
14+ readHostProcessIdentityObservationsAsync ,
1315} from '@agent-device/host-kit/process' ;
1416
1517import { 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
5954function 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+
73140function 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
0 commit comments