@@ -14,7 +14,18 @@ import { screenshotRuntimeOperationFacts } from '@agent-device/contracts/screens
1414import { viewportRuntimeOperationFacts } from '@agent-device/contracts/viewport-runtime' ;
1515import type { DeviceInfo } from '@agent-device/kernel/device' ;
1616import { createLimrunRuntime } from '@agent-device/provider-limrun' ;
17- import { describe , expect , test , vi } from 'vitest' ;
17+ import { describe , expect , expectTypeOf , test , vi } from 'vitest' ;
18+ import type { AgentDeviceHost } from './sdk/host-types.ts' ;
19+ import fs from 'node:fs' ;
20+ import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution' ;
21+ import { createTestHostEnvironment , mkdtempForTestSync } from '@agent-device/test-kit' ;
22+ import { startDaemonRuntime } from '../packages/daemon/src/server/http/daemon-runtime.ts' ;
23+ import { describeUnfinishedShutdownWork } from '../packages/daemon/src/server/http/server-shutdown.ts' ;
24+ import {
25+ applicationLifecycleResourceFixture ,
26+ daemonRuntimeDepsFixture ,
27+ platformOwnerLifecycleFixture ,
28+ } from '../packages/daemon/src/__tests__/support/daemon-runtime-deps-fixture.ts' ;
1829import { createComposedPlatformRuntimeGateway } from './platform-runtime-gateway.ts' ;
1930import {
2031 gatewayFixture as gateway ,
@@ -28,6 +39,151 @@ import {
2839} from './platform-runtime-gateway.fixtures.ts' ;
2940
3041describe ( 'composed platform runtime gateway' , ( ) => {
42+ test ( 'the SDK close report exposes each platform owner outcome' , ( ) => {
43+ expectTypeOf < Awaited < ReturnType < AgentDeviceHost [ 'close' ] > > [ 'platformOwners' ] > ( ) . toEqualTypeOf <
44+ 'closed' | 'pending' | 'failed'
45+ > ( ) ;
46+ } ) ;
47+
48+ test ( 'shutdown waits for an owner load already in progress' , async ( ) => {
49+ const ref = providerRuntimeOwner ( 'limrun' , 'loading' ) ;
50+ const shutdown = vi . fn ( async ( ) => { } ) ;
51+ let finishLoad ! : ( ) => void ;
52+ const loadReady = new Promise < void > ( ( resolve ) => {
53+ finishLoad = resolve ;
54+ } ) ;
55+ const registration = providerRuntime ( {
56+ ref,
57+ load : async ( ) => {
58+ await loadReady ;
59+ return { ...runtimeOwner ( { ref } ) , shutdown } ;
60+ } ,
61+ } ) ;
62+ const runtimeGateway = gateway ( [ registration ] ) ;
63+ const binding = runtimeGateway . bind ( {
64+ device,
65+ intent : { kind : 'exact-owner' , owner : ref , fence : { token : 'fence' , generation : 1 } } ,
66+ scope,
67+ } ) ;
68+ const closing = runtimeGateway . shutdown ( ) ;
69+ finishLoad ( ) ;
70+ await binding ;
71+ await closing ;
72+ expect ( shutdown ) . toHaveBeenCalledOnce ( ) ;
73+ } ) ;
74+ test ( 'daemon reports a composed owner failure and retains registration while completing cleanup' , async ( ) => {
75+ const ref = providerRuntimeOwner ( 'limrun' , 'failed' ) ;
76+ const shutdown = vi . fn ( async ( ) => {
77+ throw new Error ( 'owner refused' ) ;
78+ } ) ;
79+ const registration = providerRuntime ( {
80+ ref,
81+ load : async ( ) => ( { ...runtimeOwner ( { ref } ) , shutdown } ) ,
82+ } ) ;
83+ const composed = gateway ( [ registration ] ) ;
84+ await composed . bind ( {
85+ device,
86+ intent : { kind : 'exact-owner' , owner : ref , fence : { token : 'fence' , generation : 1 } } ,
87+ scope,
88+ } ) ;
89+ const stateDir = mkdtempForTestSync ( 'agent-device-owner-failure-' ) ;
90+ const paths = resolveDaemonPaths ( stateDir ) ;
91+ const finalize = vi . fn ( async ( ) => [ 'runner' ] ) ;
92+ const pluginClose = vi . fn ( async ( ) => [ ] ) ;
93+ const clearLock = vi . fn ( async ( ) => { } ) ;
94+ const runtime = await startDaemonRuntime ( {
95+ host : createTestHostEnvironment ( {
96+ env : {
97+ ...process . env ,
98+ AGENT_DEVICE_STATE_DIR : stateDir ,
99+ AGENT_DEVICE_DAEMON_IDLE_TIMEOUT_MS : '0' ,
100+ AGENT_DEVICE_DAEMON_SERVER_MODE : 'http' ,
101+ } ,
102+ } ) ,
103+ stderr : { write : ( ) => { } } ,
104+ stdout : { write : ( ) => { } } ,
105+ composeDeps : daemonRuntimeDepsFixture ( {
106+ deviceRuntimeGateway : {
107+ ...composed ,
108+ applicationLifecycle : {
109+ ...applicationLifecycleResourceFixture ( ) ,
110+ finalizeDaemonShutdown : finalize ,
111+ } ,
112+ } ,
113+ shutdownProviderRuntimes : pluginClose ,
114+ ownerLifecycle : platformOwnerLifecycleFixture ( { clearDaemonLockConfiguration : clearLock } ) ,
115+ } ) ,
116+ } ) ;
117+ const report = await runtime . close ( ) ;
118+ expect ( report . platformOwners ) . toBe ( 'failed' ) ;
119+ expect ( report . runners . stopped ) . toEqual ( [ 'runner' ] ) ;
120+ expect ( describeUnfinishedShutdownWork ( report ) ) . toEqual ( [
121+ expect . stringContaining ( 'platform owner shutdown failed' ) ,
122+ ] ) ;
123+ expect ( fs . readFileSync ( paths . logPath , 'utf8' ) ) . toContain (
124+ 'daemon_shutdown_platform_owner_failed' ,
125+ ) ;
126+ expect ( fs . existsSync ( paths . infoPath ) ) . toBe ( true ) ;
127+ expect ( fs . existsSync ( paths . lockPath ) ) . toBe ( true ) ;
128+ expect ( clearLock ) . not . toHaveBeenCalled ( ) ;
129+ expect ( pluginClose ) . toHaveBeenCalledOnce ( ) ;
130+ expect ( finalize ) . toHaveBeenCalledOnce ( ) ;
131+ expect ( await runtime . close ( ) ) . toBe ( report ) ;
132+ expect ( shutdown ) . toHaveBeenCalledOnce ( ) ;
133+ } ) ;
134+ test ( 'settles all owners and preserves the failure across concurrent and repeated shutdown' , async ( ) => {
135+ const failedRef = providerRuntimeOwner ( 'limrun' , 'failed' ) ;
136+ const healthyRef = providerRuntimeOwner ( 'limrun' , 'healthy' ) ;
137+ const failure = new Error ( 'owner refused' ) ;
138+ const failedShutdown = vi . fn ( async ( ) => {
139+ throw failure ;
140+ } ) ;
141+ const healthyShutdown = vi . fn ( async ( ) => { } ) ;
142+ const failed = providerRuntime ( {
143+ ref : failedRef ,
144+ load : async ( ) => ( { ...runtimeOwner ( { ref : failedRef } ) , shutdown : failedShutdown } ) ,
145+ } ) ;
146+ const healthy = providerRuntime ( {
147+ ref : healthyRef ,
148+ load : async ( ) => ( { ...runtimeOwner ( { ref : healthyRef } ) , shutdown : healthyShutdown } ) ,
149+ } ) ;
150+ const runtimeGateway = gateway ( [ failed , healthy ] ) ;
151+ for ( const ref of [ failedRef , healthyRef ] ) {
152+ await runtimeGateway . bind ( {
153+ device,
154+ intent : { kind : 'exact-owner' , owner : ref , fence : { token : 'fence' , generation : 1 } } ,
155+ scope,
156+ } ) ;
157+ }
158+
159+ const first = runtimeGateway . shutdown ( ) ;
160+ const second = runtimeGateway . shutdown ( ) ;
161+ const results = await Promise . allSettled ( [ first , second ] ) ;
162+ for ( const result of results ) {
163+ expect ( result . status ) . toBe ( 'rejected' ) ;
164+ if ( result . status === 'rejected' ) {
165+ expect ( result . reason ) . toBeInstanceOf ( AggregateError ) ;
166+ expect ( result . reason . message ) . toContain ( 'failed' ) ;
167+ expect ( result . reason . errors [ 0 ] . cause ) . toBe ( failure ) ;
168+ }
169+ }
170+ await expect ( runtimeGateway . shutdown ( ) ) . rejects . toBe (
171+ ( results [ 0 ] as PromiseRejectedResult ) . reason ,
172+ ) ;
173+ expect ( failedShutdown ) . toHaveBeenCalledOnce ( ) ;
174+ expect ( healthyShutdown ) . toHaveBeenCalledOnce ( ) ;
175+ await expect (
176+ runtimeGateway . bind ( {
177+ device,
178+ intent : {
179+ kind : 'exact-owner' ,
180+ owner : healthyRef ,
181+ fence : { token : 'fence' , generation : 1 } ,
182+ } ,
183+ scope,
184+ } ) ,
185+ ) . rejects . toThrow ( 'shut down' ) ;
186+ } ) ;
31187 test ( 'loads only the selected Apple owner for screenshot facts and binding' , async ( ) => {
32188 const appleDevice : DeviceInfo = {
33189 platform : 'apple' ,
0 commit comments