@@ -15,6 +15,7 @@ import { screenshotRuntimeOperationFacts } from '@agent-device/contracts/screens
1515import { viewportRuntimeOperationFacts } from '@agent-device/contracts/viewport-runtime' ;
1616import type { DeviceInfo } from '@agent-device/kernel/device' ;
1717import { AppError , normalizeError } from '@agent-device/kernel/errors' ;
18+ import { withDiagnosticsScope } from '@agent-device/host-kit/diagnostics' ;
1819import { createLimrunRuntime } from '@agent-device/provider-limrun' ;
1920import { describe , expect , expectTypeOf , test , vi } from 'vitest' ;
2021import type { AgentDeviceHost } from './sdk/host-types.ts' ;
@@ -134,6 +135,97 @@ describe('composed platform runtime gateway', () => {
134135 expect ( readyShutdown ) . toHaveBeenCalledOnce ( ) ;
135136 expect ( lateShutdown ) . toHaveBeenCalledOnce ( ) ;
136137 } ) ;
138+ test ( 'records a known owner failure while another owner load remains pending' , async ( ) => {
139+ const failedRef = providerRuntimeOwner ( 'limrun' , 'failed' ) ;
140+ const loadingRef = providerRuntimeOwner ( 'limrun' , 'loading' ) ;
141+ const failedShutdown = vi . fn ( async ( ) => {
142+ throw new AppError ( 'COMMAND_FAILED' , 'owner refused' , {
143+ reason : 'owner-test-refusal' ,
144+ observedAtNs : 9_007_199_254_740_993n ,
145+ } ) ;
146+ } ) ;
147+ const lateShutdown = vi . fn ( async ( ) => { } ) ;
148+ let finishLoad ! : ( ) => void ;
149+ const loadReady = new Promise < void > ( ( resolve ) => {
150+ finishLoad = resolve ;
151+ } ) ;
152+ const runtimeGateway = gateway ( [
153+ providerRuntime ( {
154+ ref : failedRef ,
155+ load : async ( ) => ( { ...runtimeOwner ( { ref : failedRef } ) , shutdown : failedShutdown } ) ,
156+ } ) ,
157+ providerRuntime ( {
158+ ref : loadingRef ,
159+ load : async ( ) => {
160+ await loadReady ;
161+ return { ...runtimeOwner ( { ref : loadingRef } ) , shutdown : lateShutdown } ;
162+ } ,
163+ } ) ,
164+ ] ) ;
165+ await runtimeGateway . bind ( {
166+ device,
167+ intent : { kind : 'exact-owner' , owner : failedRef , fence : { token : 'failed' , generation : 1 } } ,
168+ scope,
169+ } ) ;
170+ const loading = runtimeGateway . bind ( {
171+ device,
172+ intent : {
173+ kind : 'exact-owner' ,
174+ owner : loadingRef ,
175+ fence : { token : 'loading' , generation : 1 } ,
176+ } ,
177+ scope,
178+ } ) ;
179+ const refused = expect ( loading ) . rejects . toMatchObject ( {
180+ details : { reason : 'runtime-gateway-shut-down' } ,
181+ } ) ;
182+ const logPath = `${ mkdtempForTestSync ( 'gateway-partial-close-' ) } /events.ndjson` ;
183+ await withDiagnosticsScope ( { debug : true , logPath } , async ( ) => {
184+ const closing = runtimeGateway . shutdown ( ) ;
185+ let settled = false ;
186+ void closing . then (
187+ ( ) => {
188+ settled = true ;
189+ } ,
190+ ( ) => {
191+ settled = true ;
192+ } ,
193+ ) ;
194+ const rejected = expect ( closing ) . rejects . toMatchObject ( {
195+ details : { reason : 'platform-owner-shutdown-failed' } ,
196+ } ) ;
197+ try {
198+ await vi . waitFor ( ( ) => {
199+ expect ( fs . existsSync ( logPath ) ) . toBe ( true ) ;
200+ const rows = fs
201+ . readFileSync ( logPath , 'utf8' )
202+ . trim ( )
203+ . split ( '\n' )
204+ . map ( ( line ) => JSON . parse ( line ) ) ;
205+ expect ( rows ) . toContainEqual (
206+ expect . objectContaining ( {
207+ phase : 'platform_runtime_owner_shutdown_failed' ,
208+ data : {
209+ owner : failedRef ,
210+ error : expect . objectContaining ( {
211+ code : 'COMMAND_FAILED' ,
212+ details : { reason : 'owner-test-refusal' , observedAtNs : '9007199254740993' } ,
213+ } ) ,
214+ } ,
215+ } ) ,
216+ ) ;
217+ } ) ;
218+ expect ( settled ) . toBe ( false ) ;
219+ expect ( failedShutdown ) . toHaveBeenCalledOnce ( ) ;
220+ expect ( lateShutdown ) . not . toHaveBeenCalled ( ) ;
221+ } finally {
222+ finishLoad ( ) ;
223+ await refused ;
224+ await rejected ;
225+ }
226+ } ) ;
227+ expect ( lateShutdown ) . toHaveBeenCalledOnce ( ) ;
228+ } ) ;
137229 test ( 'daemon reports a composed owner failure with bigint details and completes cleanup' , async ( ) => {
138230 const ref = providerRuntimeOwner ( 'limrun' , 'failed' ) ;
139231 const shutdown = vi . fn ( async ( ) => {
@@ -192,11 +284,19 @@ describe('composed platform runtime gateway', () => {
192284 expect ( report . platformOwners ) . toBe ( 'failed' ) ;
193285 expect ( report . runners . stopped ) . toEqual ( [ 'runner' ] ) ;
194286 const log = fs . readFileSync ( paths . logPath , 'utf8' ) ;
195- const diagnostic = log
287+ const diagnostics = log
196288 . split ( '\n' )
197289 . filter ( Boolean )
198- . map ( ( line ) => JSON . parse ( line ) )
199- . find ( ( { phase } ) => phase === 'daemon_shutdown_platform_owner_failed' ) ;
290+ . map ( ( line ) => JSON . parse ( line ) ) ;
291+ const diagnostic = diagnostics . find (
292+ ( { phase } ) => phase === 'daemon_shutdown_platform_owner_failed' ,
293+ ) ;
294+ expect ( diagnostics ) . toContainEqual (
295+ expect . objectContaining ( {
296+ phase : 'platform_runtime_owner_shutdown_failed' ,
297+ data : expect . objectContaining ( { owner : ref } ) ,
298+ } ) ,
299+ ) ;
200300 expect ( diagnostic . data . error ) . toMatchObject ( {
201301 code : 'COMMAND_FAILED' ,
202302 details : {
0 commit comments