@@ -6,8 +6,10 @@ import { afterEach, test, vi } from 'vitest';
66import * as hostProcess from '@agent-device/host-kit/process' ;
77import {
88 acquireDeviceClaim as acquireProductionDeviceClaim ,
9+ acquireTransientDeviceClaim ,
910 canonicalLocalDeviceKey ,
1011 inspectDeviceClaims ,
12+ renewDeviceClaim ,
1113} from '@agent-device/daemon-contracts/device-claims' ;
1214import type { DeviceBootObservationService } from '@agent-device/contracts/device-boot' ;
1315import type { DeviceInfo } from '@agent-device/kernel/device' ;
@@ -94,10 +96,134 @@ test('preserves and reports a live foreign claim without overwriting it', async
9496 assert . equal ( inspectDeviceClaims ( { serial : device . id } ) [ 0 ] ?. claim ?. session , 'first' ) ;
9597} ) ;
9698
99+ // The maintainer's trace on #3714: `install` holds a transient claim under the label
100+ // `transient:install`; an `open` from the same workspace addressed at that label must not be
101+ // handed the command's ownership token. Any session string can be typed into `--session`; only
102+ // the stored kind says who holds the claim, so an open-session acquirer never holds a
103+ // transient-command record and vice versa.
104+ test ( 'an open session addressed at a command label never adopts the in-flight command claim' , async ( ) => {
105+ const root = useClaimsRoot ( ) ;
106+ const holder = await acquireTransientDeviceClaim ( {
107+ device,
108+ command : 'install' ,
109+ coveredBySession : 'open-a' ,
110+ coveredClaimIsOwnedByRequest : ( ) => false ,
111+ workspace : '/worktrees/first' ,
112+ stateDir : root ,
113+ reconcileOrphanedDeviceClaim : async ( ) => ( { status : 'retained' , reason : 'test-no-recovery' } ) ,
114+ } ) ;
115+ assert . equal ( holder . status , 'acquired' ) ;
116+ if ( holder . status !== 'acquired' ) return ;
117+
118+ const open = await acquireDeviceClaim ( {
119+ device,
120+ session : 'transient:install' ,
121+ workspace : '/worktrees/first' ,
122+ stateDir : root ,
123+ } ) ;
124+
125+ assert . equal ( open . status , 'conflict' ) ;
126+ if ( open . status !== 'conflict' ) return ;
127+ assert . equal ( open . conflict . classification , 'live' ) ;
128+ // The refusal wrote nothing: the command still holds the device with its own token.
129+ assert . equal ( storedClaim ( ) . ownerToken , holder . ownership . ownerToken ) ;
130+ } ) ;
131+
132+ // The mirror direction, pinned at the settlement boundary every acquire route goes through. Two
133+ // live requests can never share one claim: a second `reinstall` or `install` reaching the running
134+ // `install`'s claim — same workspace, label as session — is refused, and only the request that
135+ // took the claim re-enters it, through the token the admission layer already pins.
136+ test ( 'a second request never holds an in-flight command claim, under its own label or another’s' , async ( ) => {
137+ const root = useClaimsRoot ( ) ;
138+ const holder = await acquireTransientDeviceClaim ( {
139+ device,
140+ command : 'install' ,
141+ coveredBySession : 'open-a' ,
142+ coveredClaimIsOwnedByRequest : ( ) => false ,
143+ workspace : '/worktrees/first' ,
144+ stateDir : root ,
145+ reconcileOrphanedDeviceClaim : async ( ) => ( { status : 'retained' , reason : 'test-no-recovery' } ) ,
146+ } ) ;
147+ assert . equal ( holder . status , 'acquired' ) ;
148+ if ( holder . status !== 'acquired' ) return ;
149+
150+ const intruder = await acquireTransientDeviceClaim ( {
151+ device,
152+ command : 'reinstall' ,
153+ coveredBySession : 'transient:install' ,
154+ coveredClaimIsOwnedByRequest : ( ) => true ,
155+ workspace : '/worktrees/first' ,
156+ stateDir : root ,
157+ reconcileOrphanedDeviceClaim : async ( ) => ( { status : 'retained' , reason : 'test-no-recovery' } ) ,
158+ } ) ;
159+ assert . equal ( intruder . status , 'conflict' ) ;
160+ assert . equal ( storedClaim ( ) . ownerToken , holder . ownership . ownerToken ) ;
161+
162+ const sameCommand = await acquireTransientDeviceClaim ( {
163+ device,
164+ command : 'install' ,
165+ coveredBySession : 'some-other-session' ,
166+ coveredClaimIsOwnedByRequest : ( ) => false ,
167+ workspace : '/worktrees/first' ,
168+ stateDir : root ,
169+ reconcileOrphanedDeviceClaim : async ( ) => ( { status : 'retained' , reason : 'test-no-recovery' } ) ,
170+ } ) ;
171+ // A different request of the SAME command still refuses: only the request holding the claim's
172+ // release token re-enters it (the admission layer pins that), so a live command claim is never
173+ // disposed from under its holder by a second same-named request.
174+ assert . equal ( sameCommand . status , 'conflict' ) ;
175+ assert . equal ( storedClaim ( ) . ownerToken , holder . ownership . ownerToken ) ;
176+ } ) ;
177+
178+ // A migrated record states no kind, so no acquirer's kind can equal it: even the same principal
179+ // in the same workspace cannot hold it at acquisition, and a record whose start time is unreadable
180+ // (`null === null` against this process) is the case that most needs the refusal. Acquisition
181+ // never renews it either — the token the record itself names is the only renewal route left.
182+ test ( 'a migrated record states no kind, so acquisition never holds it' , async ( ) => {
183+ for ( const startTimeUnavailable of [ false , true ] ) {
184+ const root = useClaimsRoot ( ) ;
185+ const identity = startTimeUnavailable
186+ ? { pid : process . pid , startTime : null }
187+ : hostProcess . readCurrentOwnerIdentity ( ) ;
188+ const owner = vi . spyOn ( hostProcess , 'readCurrentOwnerIdentity' ) . mockReturnValue ( identity ) ;
189+ try {
190+ fs . writeFileSync (
191+ claimPath ( root ) ,
192+ JSON . stringify ( {
193+ schemaVersion : 2 ,
194+ deviceKey : canonicalLocalDeviceKey ( device ) ,
195+ device : { family : 'android' , id : device . id , name : device . name , kind : device . kind } ,
196+ session : 'legacy-owner' ,
197+ workspace : '/worktrees/legacy' ,
198+ stateDir : root ,
199+ ownerPid : identity . pid ,
200+ ownerStartTime : identity . startTime ,
201+ ownerToken : 'legacy-token' ,
202+ createdAtMs : 1 ,
203+ updatedAtMs : 1 ,
204+ } ) ,
205+ ) ;
206+
207+ const held = await acquireDeviceClaim ( {
208+ device,
209+ session : 'legacy-owner' ,
210+ workspace : '/worktrees/legacy' ,
211+ stateDir : root ,
212+ } ) ;
213+
214+ assert . equal ( held . status , 'conflict' ) ;
215+ assert . equal ( storedClaim ( ) . ownerToken , 'legacy-token' ) ;
216+ assert . equal ( storedClaim ( ) . updatedAtMs , 1 ) ;
217+ } finally {
218+ owner . mockRestore ( ) ;
219+ }
220+ }
221+ } ) ;
222+
97223// Renewal, abandon, and clear write a decoded claim back. A migrated record must therefore carry
98224// the current schema version: a legacy version on the migrated device shape reads back as
99- // inconsistent and locks the owner out of its own next command .
100- test ( 'a renewal of a migrated record writes back the shape the reader expects' , async ( ) => {
225+ // inconsistent and locks the device out of any settlement path that reads it .
226+ test ( 'a renewal through a migrated record’s own token writes back the shape the reader expects' , async ( ) => {
101227 const root = useClaimsRoot ( ) ;
102228 fs . writeFileSync (
103229 claimPath ( root ) ,
@@ -116,13 +242,13 @@ test('a renewal of a migrated record writes back the shape the reader expects',
116242 } ) ,
117243 ) ;
118244
119- const held = await acquireDeviceClaim ( {
120- device,
121- session : 'legacy-owner ' ,
122- workspace : '/worktrees/legacy' ,
123- stateDir : root ,
245+ const renewal = await renewDeviceClaim ( {
246+ deviceKey : canonicalLocalDeviceKey ( device ) ,
247+ ownerToken : 'legacy-token ' ,
248+ ownerPid : process . pid ,
249+ ownerStartTime : 'test-process-start-time' ,
124250 } ) ;
125- assert . equal ( held . status , 'acquired ' ) ;
251+ assert . equal ( renewal . status , 'renewed ' ) ;
126252
127253 const persisted = JSON . parse ( fs . readFileSync ( claimPath ( root ) , 'utf8' ) ) as Record < string , unknown > ;
128254 assert . equal ( persisted . schemaVersion , 3 ) ;
0 commit comments