@@ -6,7 +6,7 @@ import { computeDaemonCodeSignature } from '@agent-device/host-kit/code-signatur
66import {
77 resolveDaemonLaunchSpec ,
88 resolveDaemonTakeoverReason ,
9- resolveLocalDaemonCodeSignature ,
9+ resolveLocalDaemonCodeIdentity ,
1010} from '../daemon-launch-spec.ts' ;
1111import { isSourceCheckoutProjectRoot , readVersion } from '@agent-device/host-kit/version' ;
1212import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo' ;
@@ -26,10 +26,17 @@ afterEach(() => {
2626 vi . mocked ( isSourceCheckoutProjectRoot ) . mockReset ( ) ;
2727} ) ;
2828
29+ /** This tree's fingerprint, or `undefined` when the tree is an installed package. */
30+ function ownCheckoutCodeSignature ( ) : Promise < string | undefined > {
31+ return resolveLocalDaemonCodeIdentity ( ) . then ( ( identity ) =>
32+ identity . origin === 'checkout' ? identity . codeSignature : undefined ,
33+ ) ;
34+ }
35+
2936// A source checkout re-reads the launch entry and its code signature on every
3037// command. The entry is fixed for the process; the signature is not, so it stays
31- // live and leans on the stat-validated cache instead. An installed package has no
32- // signature to re-read : its version pins its bytes (`#2458`).
38+ // live and leans on the stat-validated cache instead. An installed package reports
39+ // an origin and no signature at all : its version pins its bytes (`#2458`).
3340
3441test ( 'resolveDaemonLaunchSpec probes the entry candidates once per process' , ( ) => {
3542 resetAllProcessMemosForTests ( ) ;
@@ -44,12 +51,13 @@ test('resolveDaemonLaunchSpec probes the entry candidates once per process', ()
4451 assert . equal ( existsSpy . mock . calls . length , probeCalls ) ;
4552} ) ;
4653
47- test ( 'resolveLocalDaemonCodeSignature re-reads the filesystem on every call' , async ( ) => {
54+ test ( 'the local code identity re-reads the filesystem on every call' , async ( ) => {
4855 resetAllProcessMemosForTests ( ) ;
4956
50- const first = await resolveLocalDaemonCodeSignature ( ) ;
57+ const first = await ownCheckoutCodeSignature ( ) ;
58+ assert . ok ( first ) ;
5159 const statSpy = vi . spyOn ( fs , 'statSync' ) ;
52- const second = await resolveLocalDaemonCodeSignature ( ) ;
60+ const second = await ownCheckoutCodeSignature ( ) ;
5361
5462 assert . equal ( second , first ) ;
5563 // Not memoized in either mode: a long-lived client (the MCP server) has to
@@ -58,15 +66,12 @@ test('resolveLocalDaemonCodeSignature re-reads the filesystem on every call', as
5866 assert . ok ( statSpy . mock . calls . length > 0 ) ;
5967} ) ;
6068
61- test ( 'resolveLocalDaemonCodeSignature agrees with the uncached walk over the launch entry' , async ( ) => {
69+ test ( 'the local signature agrees with the uncached walk over the launch entry' , async ( ) => {
6270 resetAllProcessMemosForTests ( ) ;
6371 const spec = resolveDaemonLaunchSpec ( ) ;
6472 const entryPath = spec . useSrc ? spec . srcPath : spec . distPath ;
6573
66- assert . equal (
67- await resolveLocalDaemonCodeSignature ( ) ,
68- computeDaemonCodeSignature ( entryPath , spec . root ) ,
69- ) ;
74+ assert . equal ( await ownCheckoutCodeSignature ( ) , computeDaemonCodeSignature ( entryPath , spec . root ) ) ;
7075} ) ;
7176
7277test ( 'a source client fingerprints the source entry through the stat-validated cache' , async ( ) => {
@@ -80,10 +85,10 @@ test('a source client fingerprints the source entry through the stat-validated c
8085 const spec = resolveDaemonLaunchSpec ( ) ;
8186 assert . equal ( spec . useSrc , true ) ;
8287 const expected = computeDaemonCodeSignature ( spec . srcPath , spec . root ) ;
83- assert . equal ( await resolveLocalDaemonCodeSignature ( ) , expected ) ;
88+ assert . equal ( await ownCheckoutCodeSignature ( ) , expected ) ;
8489
8590 const readSpy = vi . spyOn ( fs , 'readFileSync' ) ;
86- assert . equal ( await resolveLocalDaemonCodeSignature ( ) , expected ) ;
91+ assert . equal ( await ownCheckoutCodeSignature ( ) , expected ) ;
8792 const sourceReads = readSpy . mock . calls
8893 . map ( ( [ target ] ) => target )
8994 . filter ( ( target ) : target is string => typeof target === 'string' )
@@ -98,67 +103,123 @@ test('a source client fingerprints the source entry through the stat-validated c
98103/**
99104 * Which daemon a command keeps. `daemon-client-lifecycle.test.ts` pins the same
100105 * decision end to end from a source checkout, which is what this test process runs
101- * in; these cases flip the one input that separates a checkout from an installed
102- * package of a published version (#2458).
106+ * in; these cases flip the two inputs that separate the trees — which tree this client
107+ * runs from, and which tree the running daemon says it was started from (#2458).
103108 */
104109function useClientTree ( sourceCheckout : boolean ) : void {
105110 vi . mocked ( isSourceCheckoutProjectRoot ) . mockReturnValue ( sourceCheckout ) ;
106111}
107112
108- function runningDaemon ( info : { version ?: string ; codeSignature ?: string } ) {
113+ function runningDaemon ( info : {
114+ version ?: string ;
115+ codeOrigin ?: 'installed' | 'checkout' ;
116+ codeSignature ?: string ;
117+ } ) {
109118 return {
110119 token : 'local-secret' ,
111120 pid : 999_999 ,
112121 httpPort : 41_234 ,
113122 transport : 'http' as const ,
114123 version : info . version ?? readVersion ( ) ,
124+ codeOrigin : info . codeOrigin ,
115125 codeSignature : info . codeSignature ,
116126 } ;
117127}
118128
119- test ( 'an installed client fingerprints nothing , because its version pins its bytes (#2458)' , async ( ) => {
129+ test ( 'an installed client reports an origin and no signature , because its version pins its bytes (#2458)' , async ( ) => {
120130 // Two installs of one published version stamp identical bytes with different
121131 // mtimes, so a signature can only ever say "different" about the same code.
122132 resetAllProcessMemosForTests ( ) ;
123133 useClientTree ( false ) ;
124134 const statSpy = vi . spyOn ( fs , 'statSync' ) ;
125135
126- assert . equal ( await resolveLocalDaemonCodeSignature ( ) , undefined ) ;
136+ assert . deepEqual ( await resolveLocalDaemonCodeIdentity ( ) , { origin : 'installed' } ) ;
127137 assert . equal ( statSpy . mock . calls . length , 0 ) ;
128138} ) ;
129139
130- test ( 'an installed client keeps a reachable daemon whose code signature differs from its own (#2458)' , async ( ) => {
140+ test ( 'an installed client keeps an installed daemon whose code signature differs (#2458)' , async ( ) => {
141+ // Both sides are installs of the version they report, so that version is the
142+ // whole of the identity either can offer, and the session on the daemon stands.
131143 useClientTree ( false ) ;
132144
133145 assert . equal (
134- await resolveDaemonTakeoverReason ( runningDaemon ( { codeSignature : 'some-other-install' } ) , true ) ,
146+ await resolveDaemonTakeoverReason (
147+ runningDaemon ( { codeOrigin : 'installed' , codeSignature : 'some-other-install' } ) ,
148+ true ,
149+ ) ,
135150 undefined ,
136151 ) ;
137152} ) ;
138153
139- test ( 'a source checkout keeps a reachable daemon whose code signature matches its own' , async ( ) => {
154+ test ( 'an installed client replaces a daemon that reports a source checkout (#2458)' , async ( ) => {
155+ // The bypass is pairwise. A checkout beside a shared `--state-dir` can publish any
156+ // code it likes under the published version string, and an installed client has no
157+ // fingerprint of its own to notice.
158+ useClientTree ( false ) ;
159+
160+ assert . equal (
161+ await resolveDaemonTakeoverReason (
162+ runningDaemon ( { codeOrigin : 'checkout' , codeSignature : 'edited-checkout' } ) ,
163+ true ,
164+ ) ,
165+ 'code origin mismatch (daemon checkout, client installed)' ,
166+ ) ;
167+ } ) ;
168+
169+ test ( 'an installed client replaces a daemon that predates the code origin field (#2458)' , async ( ) => {
170+ useClientTree ( false ) ;
171+
172+ assert . equal (
173+ await resolveDaemonTakeoverReason ( runningDaemon ( { codeSignature : 'any' } ) , true ) ,
174+ 'code origin mismatch (daemon unreported, client installed)' ,
175+ ) ;
176+ } ) ;
177+
178+ test ( 'a source checkout keeps a daemon that reports the same code signature' , async ( ) => {
140179 useClientTree ( true ) ;
141- const ownCodeSignature = await resolveLocalDaemonCodeSignature ( ) ;
180+ const ownCodeSignature = await ownCheckoutCodeSignature ( ) ;
142181 assert . ok ( ownCodeSignature ) ;
143182
144183 assert . equal (
145- await resolveDaemonTakeoverReason ( runningDaemon ( { codeSignature : ownCodeSignature } ) , true ) ,
184+ await resolveDaemonTakeoverReason (
185+ runningDaemon ( { codeOrigin : 'checkout' , codeSignature : ownCodeSignature } ) ,
186+ true ,
187+ ) ,
146188 undefined ,
147189 ) ;
148190} ) ;
149191
150- test ( 'a source checkout replaces a reachable daemon whose code signature differs' , async ( ) => {
192+ test ( 'a source checkout replaces a daemon whose code signature differs' , async ( ) => {
151193 // A rebuild leaves the version alone, so the signature is the only thing that can
152194 // notice a daemon serving code its client no longer has.
153195 useClientTree ( true ) ;
154196
155197 assert . equal (
156- await resolveDaemonTakeoverReason ( runningDaemon ( { codeSignature : 'an-older-build' } ) , true ) ,
198+ await resolveDaemonTakeoverReason (
199+ runningDaemon ( { codeOrigin : 'checkout' , codeSignature : 'an-older-build' } ) ,
200+ true ,
201+ ) ,
157202 'code-signature mismatch' ,
158203 ) ;
159204} ) ;
160205
161- test ( 'a source checkout replaces a daemon that reported no code signature at all' , async ( ) => {
206+ test ( 'a source checkout replaces a daemon that reports an installed package' , async ( ) => {
207+ // The pair #2458 lets through is two installs; a checkout can still neither prove
208+ // nor disprove what an install holds, and must not run it on faith.
209+ useClientTree ( true ) ;
210+
211+ assert . equal (
212+ await resolveDaemonTakeoverReason (
213+ runningDaemon ( { codeOrigin : 'installed' , codeSignature : 'the-published-artifact' } ) ,
214+ true ,
215+ ) ,
216+ 'code origin mismatch (daemon installed, client checkout)' ,
217+ ) ;
218+ } ) ;
219+
220+ test ( 'a source checkout judges a daemon that predates the code origin field by its signature' , async ( ) => {
221+ // Every daemon published before the field answered with a signature, which is the
222+ // comparison they were reused under until now.
162223 useClientTree ( true ) ;
163224
164225 assert . equal (
@@ -174,27 +235,33 @@ test('a mismatched version replaces the daemon whichever tree the client runs fr
174235
175236 assert . equal (
176237 await resolveDaemonTakeoverReason (
177- runningDaemon ( { version : '0.0.0-mismatch' , codeSignature : 'any' } ) ,
238+ runningDaemon ( { version : '0.0.0-mismatch' , codeOrigin : 'installed' , codeSignature : 'any' } ) ,
178239 true ,
179240 ) ,
180241 expected ,
181242 ) ;
182243 }
183244} ) ;
184245
185- test ( 'a reachable daemon of a matching version survives, an unreachable one does not' , async ( ) => {
246+ test ( 'a reachable daemon of a matching identity survives, an unreachable one does not' , async ( ) => {
186247 useClientTree ( false ) ;
187248
188- assert . equal ( await resolveDaemonTakeoverReason ( runningDaemon ( { } ) , true ) , undefined ) ;
189- assert . equal ( await resolveDaemonTakeoverReason ( runningDaemon ( { } ) , false ) , 'unreachable' ) ;
249+ assert . equal (
250+ await resolveDaemonTakeoverReason ( runningDaemon ( { codeOrigin : 'installed' } ) , true ) ,
251+ undefined ,
252+ ) ;
253+ assert . equal (
254+ await resolveDaemonTakeoverReason ( runningDaemon ( { codeOrigin : 'installed' } ) , false ) ,
255+ 'unreachable' ,
256+ ) ;
190257} ) ;
191258
192259test ( 'the tree shape is asked per call, not memoized with the signature' , async ( ) => {
193260 // A long-lived client (the MCP server) must notice a daemon rebuilt underneath it,
194261 // so nothing about this answer is cached — including which tree it came from.
195262 useClientTree ( false ) ;
196- assert . equal ( await resolveLocalDaemonCodeSignature ( ) , undefined ) ;
263+ assert . deepEqual ( await resolveLocalDaemonCodeIdentity ( ) , { origin : 'installed' } ) ;
197264
198265 useClientTree ( true ) ;
199- assert . ok ( await resolveLocalDaemonCodeSignature ( ) ) ;
266+ assert . ok ( await ownCheckoutCodeSignature ( ) ) ;
200267} ) ;
0 commit comments