@@ -3,6 +3,7 @@ import path from 'node:path';
33import os from 'node:os' ;
44import type { CliFlags } from '@agent-device/contracts/command' ;
55import type { DeviceInfo } from '@agent-device/kernel/device' ;
6+ import type { HostEnvironment } from '@agent-device/kernel/source-value' ;
67import { AppError } from '@agent-device/kernel/errors' ;
78import {
89 acquireWrapperDeviceClaim ,
@@ -28,6 +29,8 @@ export type ExecCommandOptions = Readonly<{
2829 flags : CliFlags ;
2930 /** The CLI's resolved daemon state dir; the wrapper's claim lives under it. */
3031 stateDir : string ;
32+ /** The host captured at the CLI entry point; it locates the claim store and the workspace. */
33+ host : HostEnvironment ;
3134} > ;
3235
3336/**
@@ -68,12 +71,12 @@ export async function runExecCommand(
6871 }
6972 const termination = attachSignalForwarding ( ) ;
7073 try {
71- const device = await resolveExecDevice ( options . flags ) ;
74+ const device = await resolveExecDevice ( options . flags , options . host ) ;
7275 if ( termination . signal ) return signalExitCode ( termination . signal ) ;
7376 const ownership = await acquireExecDeviceClaim ( device , command , options ) ;
7477 try {
7578 if ( termination . signal ) return signalExitCode ( termination . signal ) ;
76- return await runClaimedChild ( command , args , termination ) ;
79+ return await runClaimedChild ( command , args , termination , options . host ) ;
7780 } catch ( error ) {
7881 // A command missing from PATH is the child failing to start; shells report
7982 // that as 127, and `exec` promises the command's exit code.
@@ -83,21 +86,21 @@ export async function runExecCommand(
8386 }
8487 throw error ;
8588 } finally {
86- await releaseExecDeviceClaim ( ownership ) ;
89+ await releaseExecDeviceClaim ( ownership , options . host ) ;
8790 }
8891 } finally {
8992 termination . dispose ( ) ;
9093 }
9194}
9295
93- async function resolveExecDevice ( flags : CliFlags ) : Promise < DeviceInfo > {
96+ async function resolveExecDevice ( flags : CliFlags , host : HostEnvironment ) : Promise < DeviceInfo > {
9497 if ( hasDeviceInventoryContext ( ) ) return await resolveTargetDevice ( flags ) ;
9598 // Claims exist only for local devices, so the wrapper composes the local
9699 // inventory itself; a scope installed by a host (a test or embedding caller)
97100 // is reused as-is. Platform modules are named by the gateway composition, so
98101 // it is imported only on this path.
99102 const { createPlatformDeviceInventoryGateways } = await import ( '../../platform-runtime.ts' ) ;
100- const gateways = await createPlatformDeviceInventoryGateways ( ) ;
103+ const gateways = await createPlatformDeviceInventoryGateways ( host ) ;
101104 return await withDeviceInventoryContext (
102105 { ...gateways , requestScope : createRecoveryPlatformScope ( ) } ,
103106 async ( ) => await resolveTargetDevice ( flags ) ,
@@ -114,15 +117,17 @@ async function acquireExecDeviceClaim(
114117 // common claim-free path stays as light as `device release --stale` keeps it.
115118 const { createOwnerScopedDeviceClaimReconciler } =
116119 await import ( '@agent-device/daemon-contracts/device-claim-recovery' ) ;
117- const { createClaimRecoveryGateway } = await import ( '../../platform-runtime-claim-recovery.ts' ) ;
120+ const { createClaimRecoveryGatewayFor } =
121+ await import ( '../../platform-runtime-claim-recovery.ts' ) ;
118122 const result = await acquireWrapperDeviceClaim ( {
119123 device,
120124 command,
121- workspace : process . cwd ( ) ,
125+ workspace : options . host . cwd ,
122126 stateDir : prepareExecClaimStateDir ( options . stateDir ) ,
127+ env : options . host . env ,
123128 reconcileOrphanedDeviceClaim : createOwnerScopedDeviceClaimReconciler ( {
124129 scope : createRecoveryPlatformScope ( ) ,
125- composeGateway : createClaimRecoveryGateway ,
130+ composeGateway : createClaimRecoveryGatewayFor ( options . host ) ,
126131 } ) ,
127132 } ) ;
128133 if ( result . status === 'conflict' ) throw deviceClaimConflictError ( device , result . conflict ) ;
@@ -145,20 +150,19 @@ async function runClaimedChild(
145150 command : string ,
146151 args : readonly string [ ] ,
147152 termination : SignalForwarding ,
153+ host : HostEnvironment ,
148154) : Promise < number > {
149155 // A build tool named with a path (`./gradlew`, `./scripts/build.sh`) is how the
150156 // fence is used day to day; resolve it against the cwd the way a shell would,
151157 // because the spawn helper keeps bare PATH names and absolute paths only.
152158 const executable =
153- ! path . isAbsolute ( command ) && command . includes ( '/' )
154- ? path . resolve ( process . cwd ( ) , command )
155- : command ;
159+ ! path . isAbsolute ( command ) && command . includes ( '/' ) ? path . resolve ( host . cwd , command ) : command ;
156160 // The child leads its own process group so a forwarded signal reaches the
157161 // build tools it spawns — the grandchildren the fence exists to cover — not
158162 // just its direct process.
159163 const child = runCmdBackground ( executable , args , {
160- cwd : process . cwd ( ) ,
161- env : process . env ,
164+ cwd : host . cwd ,
165+ env : host . env ,
162166 stdio : 'inherit' ,
163167 captureOutput : false ,
164168 allowFailure : true ,
@@ -231,10 +235,13 @@ function signalExitCode(signal: NodeJS.Signals): number {
231235 return 128 + ( signum ?? 0 ) ;
232236}
233237
234- async function releaseExecDeviceClaim ( ownership : DeviceClaimSessionOwnership ) : Promise < void > {
238+ async function releaseExecDeviceClaim (
239+ ownership : DeviceClaimSessionOwnership ,
240+ host : HostEnvironment ,
241+ ) : Promise < void > {
235242 let outcome : DeviceClaimClearOutcome ;
236243 try {
237- outcome = await clearDeviceClaim ( ownership ) ;
244+ outcome = await clearDeviceClaim ( ownership , host . env ) ;
238245 } catch ( error ) {
239246 process . stderr . write (
240247 `agent-device exec: could not release the device claim: ${
0 commit comments