@@ -2,7 +2,11 @@ import assert from 'node:assert/strict';
22import fs from 'node:fs' ;
33import { afterEach , test , vi } from 'vitest' ;
44import { readCurrentOwnerIdentity } from '@agent-device/host-kit/process' ;
5- import { tryAcquireDaemonRegistration } from '../daemon-registration-owner.ts' ;
5+ import {
6+ tryAcquireDaemonRegistration ,
7+ stopAndRetireDaemon ,
8+ recoverAbandonedDaemonRegistration ,
9+ } from '../daemon-registration-owner.ts' ;
610import { resolveDaemonPaths , type DaemonPaths } from '../daemon-resolution.ts' ;
711import { readRegisteredDaemonOwnership } from '../daemon-registration.ts' ;
812import { readDaemonShutdownReport } from '../daemon-shutdown-report.ts' ;
@@ -159,3 +163,131 @@ test('unlink disappearance is settled, other failures remain primary even when r
159163 assert . equal ( fs . existsSync ( second . paths . infoPath ) , true ) ;
160164 assert . match ( fs . readFileSync ( second . paths . logPath , 'utf8' ) , / d a e m o n _ r e g i s t r a t i o n _ r e l e a s e _ f a i l e d / ) ;
161165} ) ;
166+
167+ const deadIdentity = { pid : 999_999_999 , startTime : 'dead-start' } ;
168+ for ( const scenario of [
169+ {
170+ name : 'missing-start' ,
171+ observed : { pid : process . pid , startTime : null } ,
172+ published : ownIdentity ,
173+ retired : false ,
174+ } ,
175+ { name : 'live-unverified' , observed : ownIdentity , published : ownIdentity , retired : false } ,
176+ { name : 'no-observation' , observed : null , published : deadIdentity , retired : false } ,
177+ { name : 'replaced' , observed : deadIdentity , published : ownIdentity , retired : false } ,
178+ { name : 'dead' , observed : deadIdentity , published : deadIdentity , retired : true } ,
179+ ] as const ) {
180+ test ( `retirement respects the captured lifetime (${ scenario . name } )` , async ( ) => {
181+ const paths = resolveDaemonPaths ( mkdtempForTestSync ( 'agent-device-retirement-' ) ) ;
182+ replaceInfo ( paths , scenario . published . pid , scenario . published . startTime ) ;
183+ const before = fs . readFileSync ( paths . infoPath , 'utf8' ) ;
184+ const result = await stopAndRetireDaemon ( {
185+ paths,
186+ observed : scenario . observed ,
187+ mode : 'graceful' ,
188+ lockTimeoutMs : 0 ,
189+ } ) ;
190+ if ( scenario . retired ) {
191+ assert . equal ( result . status , 'retired' ) ;
192+ assert . equal ( result . removedInfo , true ) ;
193+ assert . equal ( fs . existsSync ( paths . infoPath ) , false ) ;
194+ } else {
195+ assert . equal ( result . status , 'retained' ) ;
196+ assert . equal ( fs . readFileSync ( paths . infoPath , 'utf8' ) , before ) ;
197+ }
198+ } ) ;
199+ }
200+
201+ test ( 'abandoned recovery never signals a live owner and absence is inspected under the lock' , async ( ) => {
202+ const { paths, owner } = await acquire ( ) ;
203+ owner . publish ( fields ) ;
204+ const before = fs . readFileSync ( paths . infoPath , 'utf8' ) ;
205+ const result = await recoverAbandonedDaemonRegistration ( { paths, observed : ownIdentity } ) ;
206+ assert . equal ( result . status , 'retained' ) ;
207+ assert . equal ( fs . readFileSync ( paths . infoPath , 'utf8' ) , before ) ;
208+ await owner . finish ( ) ;
209+ assert . deepEqual ( await recoverAbandonedDaemonRegistration ( { paths, observed : null } ) , {
210+ status : 'absent' ,
211+ removedInfo : false ,
212+ } ) ;
213+ } ) ;
214+
215+ test ( 'retirement preserves a winner holding the startup lock instead of stopping or unlinking it' , async ( ) => {
216+ const { paths, owner } = await acquire ( ) ;
217+ owner . publish ( { ...fields , token : 'winning-token' } ) ;
218+ const before = fs . readFileSync ( paths . infoPath , 'utf8' ) ;
219+ const result = await stopAndRetireDaemon ( {
220+ paths,
221+ observed : { pid : 999_999_999 , startTime : 'dead-start' } ,
222+ mode : 'force' ,
223+ lockTimeoutMs : 0 ,
224+ } ) ;
225+ assert . equal ( result . status , 'retained' ) ;
226+ if ( result . status !== 'retained' ) throw new Error ( 'retirement unexpectedly completed' ) ;
227+ assert . equal ( result . reason , 'lock-busy' ) ;
228+ assert . equal ( result . error ?. details ?. reason , 'process_lock_timeout' ) ;
229+ assert . match ( result . error ?. hint ?? '' , / R e s t o r e p r o c e s s i n s p e c t i o n / ) ;
230+ assert . equal ( fs . readFileSync ( paths . infoPath , 'utf8' ) , before ) ;
231+ assert . equal ( ( await tryAcquireDaemonRegistration ( paths ) ) . status , 'busy' ) ;
232+ await owner . finish ( ) ;
233+ } ) ;
234+
235+ test ( 'metadata failure remains primary and normalized through a secondary release failure' , async ( ) => {
236+ const paths = resolveDaemonPaths ( mkdtempForTestSync ( 'agent-device-retirement-failures-' ) ) ;
237+ const observed = { pid : 999_999_999 , startTime : 'dead-start' } ;
238+ replaceInfo ( paths , observed . pid , observed . startTime ) ;
239+ const primary = Object . assign ( new Error ( 'metadata failure' ) , { code : 'EIO' } ) ;
240+ const originalUnlink = fs . unlinkSync ;
241+ vi . spyOn ( fs , 'unlinkSync' ) . mockImplementation ( ( target ) => {
242+ if ( target === paths . infoPath ) throw primary ;
243+ return originalUnlink ( target ) ;
244+ } ) ;
245+ const originalRmdir = fs . rmdirSync ;
246+ vi . spyOn ( fs , 'rmdirSync' ) . mockImplementation ( ( target , options ) => {
247+ if ( target === paths . lockPath ) throw Object . assign ( new Error ( 'busy' ) , { code : 'EBUSY' } ) ;
248+ return originalRmdir ( target , options ) ;
249+ } ) ;
250+ const result = await stopAndRetireDaemon ( { paths, observed, mode : 'force' } ) ;
251+ assert . equal ( result . status , 'retained' ) ;
252+ if ( result . status !== 'retained' ) throw new Error ( 'retirement unexpectedly completed' ) ;
253+ assert . equal ( result . error ?. message , 'metadata failure' ) ;
254+ assert . equal ( result . error ?. code , 'UNKNOWN' ) ;
255+ assert . equal ( result . error ?. cause ?. code , 'EIO' ) ;
256+ assert . equal ( fs . existsSync ( paths . infoPath ) , true ) ;
257+ } ) ;
258+
259+ test ( 'release failure reports partial retirement rather than success' , async ( ) => {
260+ const paths = resolveDaemonPaths ( mkdtempForTestSync ( 'agent-device-retirement-partial-' ) ) ;
261+ const observed = { pid : 999_999_999 , startTime : 'dead-start' } ;
262+ replaceInfo ( paths , observed . pid , observed . startTime ) ;
263+ const original = fs . rmdirSync ;
264+ vi . spyOn ( fs , 'rmdirSync' ) . mockImplementation ( ( target , options ) => {
265+ if ( target === paths . lockPath ) throw Object . assign ( new Error ( 'busy' ) , { code : 'EBUSY' } ) ;
266+ return original ( target , options ) ;
267+ } ) ;
268+ const result = await stopAndRetireDaemon ( { paths, observed, mode : 'force' } ) ;
269+ assert . equal ( result . status , 'retained' ) ;
270+ assert . equal ( result . removedInfo , true ) ;
271+ assert . equal ( fs . existsSync ( paths . infoPath ) , false ) ;
272+ assert . equal ( fs . existsSync ( paths . lockPath ) , true ) ;
273+ } ) ;
274+
275+ test . skipIf ( process . getuid ?.( ) === 0 ) (
276+ 'an acquisition I/O failure retains metadata without reporting contention' ,
277+ async ( ) => {
278+ const paths = resolveDaemonPaths ( mkdtempForTestSync ( 'agent-device-retirement-permission-' ) ) ;
279+ replaceInfo ( paths , deadIdentity . pid , deadIdentity . startTime ) ;
280+ const before = fs . readFileSync ( paths . infoPath , 'utf8' ) ;
281+ fs . chmodSync ( paths . baseDir , 0o500 ) ;
282+ try {
283+ const result = await recoverAbandonedDaemonRegistration ( { paths, observed : deadIdentity } ) ;
284+ assert . equal ( result . status , 'retained' ) ;
285+ if ( result . status !== 'retained' ) throw new Error ( 'retirement unexpectedly completed' ) ;
286+ assert . equal ( result . reason , 'retirement-unconfirmed' ) ;
287+ assert . equal ( fs . readFileSync ( paths . infoPath , 'utf8' ) , before ) ;
288+ assert . equal ( fs . existsSync ( paths . lockPath ) , false ) ;
289+ } finally {
290+ fs . chmodSync ( paths . baseDir , 0o700 ) ;
291+ }
292+ } ,
293+ ) ;
0 commit comments