@@ -327,66 +327,143 @@ function createPinningExecutor(runCalls: Array<{ name: string; input: unknown }>
327327 runCommand : async ( _client , name , input ) => {
328328 runCalls . push ( { name, input } ) ;
329329 if ( name === 'snapshot' ) {
330- return { nodes : [ ] , truncated : false , refsGeneration : 12 } ;
330+ // Issues refs e2 and e37 at generation 500012.
331+ return {
332+ nodes : [ { ref : 'e2' } , { ref : 'e37' } ] ,
333+ truncated : false ,
334+ refsGeneration : 500012 ,
335+ } ;
331336 }
332337 if ( name === 'find' ) {
333- return { ref : '@e3' , refsGeneration : 13 } ;
338+ // A later find capture replaced the tree and issued ONLY e5 at 500013.
339+ return { ref : '@e5' , refsGeneration : 500013 } ;
334340 }
335341 return { message : `Ran ${ name } ` } ;
336342 } ,
337343 } ) ;
338344}
339345
340- test ( 'MCP auto-pins a plain @ref after a snapshot response reported refsGeneration' , async ( ) => {
346+ test ( 'MCP keeps per-ref provenance: a pre-find snapshot ref stays pinned to ITS generation' , async ( ) => {
347+ // THE find-blessing scenario (#1076): snapshot issues e37 at G1, a later
348+ // find issues e5 at G2. A plain @e37 must forward pinned to G1 — pinning it
349+ // to G2 would make the daemon read it as current and silently re-bless it.
350+ // (The daemon side of this flow — precise warning for the G1 pin after the
351+ // find capture replaced the tree — is covered in the provider scenario.)
341352 const runCalls : Array < { name : string ; input : unknown } > = [ ] ;
342353 const executor = createPinningExecutor ( runCalls ) ;
343354
344355 await executor . execute ( 'snapshot' , { session : 'demo' } ) ;
345- await executor . execute ( 'press' , { session : 'demo' , target : { kind : 'ref' , ref : '@e2' } } ) ;
356+ await executor . execute ( 'find' , { session : 'demo' , query : 'Continue' } ) ;
357+ await executor . execute ( 'press' , { session : 'demo' , target : { kind : 'ref' , ref : '@e37' } } ) ;
346358
347- assert . deepEqual ( runCalls [ 1 ] , {
359+ assert . deepEqual ( runCalls [ 2 ] , {
348360 name : 'press' ,
349- input : { session : 'demo' , target : { kind : 'ref' , ref : '@e2~s12 ' } } ,
361+ input : { session : 'demo' , target : { kind : 'ref' , ref : '@e37~s500012 ' } } ,
350362 } ) ;
351363} ) ;
352364
353- test ( 'MCP auto- pins wait refs and get targets, and tracks find-issued generations ' , async ( ) => {
365+ test ( 'MCP pins the find-issued ref to the find generation ' , async ( ) => {
354366 const runCalls : Array < { name : string ; input : unknown } > = [ ] ;
355367 const executor = createPinningExecutor ( runCalls ) ;
356368
369+ await executor . execute ( 'snapshot' , { session : 'demo' } ) ;
357370 await executor . execute ( 'find' , { session : 'demo' , query : 'Continue' } ) ;
358- await executor . execute ( 'wait' , { session : 'demo' , ref : '@e3' } ) ;
371+ await executor . execute ( 'press' , { session : 'demo' , target : { kind : 'ref' , ref : '@e5' } } ) ;
372+
373+ assert . deepEqual ( runCalls [ 2 ] , {
374+ name : 'press' ,
375+ input : { session : 'demo' , target : { kind : 'ref' , ref : '@e5~s500013' } } ,
376+ } ) ;
377+ } ) ;
378+
379+ test ( 'MCP auto-pins wait refs and get targets from the per-ref map' , async ( ) => {
380+ const runCalls : Array < { name : string ; input : unknown } > = [ ] ;
381+ const executor = createPinningExecutor ( runCalls ) ;
382+
383+ await executor . execute ( 'snapshot' , { session : 'demo' } ) ;
384+ await executor . execute ( 'wait' , { session : 'demo' , ref : '@e2' } ) ;
359385 await executor . execute ( 'get' , {
360386 session : 'demo' ,
361387 format : 'text' ,
362- target : { kind : 'ref' , ref : '@e3 ' } ,
388+ target : { kind : 'ref' , ref : '@e37 ' } ,
363389 } ) ;
364390
365391 assert . deepEqual ( runCalls [ 1 ] , {
366392 name : 'wait' ,
367- input : { session : 'demo' , ref : '@e3~s13 ' } ,
393+ input : { session : 'demo' , ref : '@e2~s500012 ' } ,
368394 } ) ;
369395 assert . deepEqual ( runCalls [ 2 ] , {
370396 name : 'get' ,
371- input : { session : 'demo' , format : 'text' , target : { kind : 'ref' , ref : '@e3~s13' } } ,
397+ input : { session : 'demo' , format : 'text' , target : { kind : 'ref' , ref : '@e37~s500012' } } ,
398+ } ) ;
399+ } ) ;
400+
401+ test ( 'MCP merges digest-level snapshot refs too' , async ( ) => {
402+ const runCalls : Array < { name : string ; input : unknown } > = [ ] ;
403+ const executor = createCommandToolExecutor ( {
404+ createClient : ( ) => ( { } ) as AgentDeviceClient ,
405+ runCommand : async ( _client , name , input ) => {
406+ runCalls . push ( { name, input } ) ;
407+ return name === 'snapshot'
408+ ? { nodeCount : 1 , refs : [ { ref : 'e9' , label : 'Continue' } ] , refsGeneration : 41 }
409+ : { } ;
410+ } ,
372411 } ) ;
412+
413+ await executor . execute ( 'snapshot' , { responseLevel : 'digest' } ) ;
414+ await executor . execute ( 'press' , { target : { kind : 'ref' , ref : '@e9' } } ) ;
415+
416+ assert . deepEqual ( runCalls [ 1 ] ?. input , { target : { kind : 'ref' , ref : '@e9~s41' } } ) ;
417+ } ) ;
418+
419+ test ( 'MCP passes never-issued refs through unpinned (coarse floor, never guess)' , async ( ) => {
420+ const runCalls : Array < { name : string ; input : unknown } > = [ ] ;
421+ const executor = createPinningExecutor ( runCalls ) ;
422+
423+ await executor . execute ( 'snapshot' , { session : 'demo' } ) ;
424+ // e99 was never present in any issuing response for this scope.
425+ await executor . execute ( 'press' , { session : 'demo' , target : { kind : 'ref' , ref : '@e99' } } ) ;
426+
427+ assert . deepEqual ( runCalls [ 1 ] ?. input , { session : 'demo' , target : { kind : 'ref' , ref : '@e99' } } ) ;
373428} ) ;
374429
375- test ( 'MCP passes refs through unpinned when no generation is known for the session ' , async ( ) => {
430+ test ( 'MCP passes refs through unpinned when the pin scope has no history ' , async ( ) => {
376431 const runCalls : Array < { name : string ; input : unknown } > = [ ] ;
377432 const executor = createPinningExecutor ( runCalls ) ;
378433
379- // No snapshot/find ran for THIS session name — never guess .
434+ // Only OTHER session names have history .
380435 await executor . execute ( 'snapshot' , { session : 'other' } ) ;
381436 await executor . execute ( 'press' , { session : 'demo' , target : { kind : 'ref' , ref : '@e2' } } ) ;
382437
383- assert . deepEqual ( runCalls [ 1 ] , {
384- name : 'press' ,
385- input : { session : 'demo' , target : { kind : 'ref' , ref : '@e2' } } ,
438+ assert . deepEqual ( runCalls [ 1 ] ?. input , { session : 'demo' , target : { kind : 'ref' , ref : '@e2' } } ) ;
439+ } ) ;
440+
441+ test ( 'MCP pin scopes include the state dir: same-named sessions never cross-pollinate' , async ( ) => {
442+ const runCalls : Array < { name : string ; input : unknown } > = [ ] ;
443+ const executor = createPinningExecutor ( runCalls ) ;
444+
445+ await executor . execute ( 'snapshot' , { session : 'demo' , stateDir : '/state/a' } ) ;
446+ // Same session name against a DIFFERENT daemon state dir: no history there.
447+ await executor . execute ( 'press' , {
448+ session : 'demo' ,
449+ stateDir : '/state/b' ,
450+ target : { kind : 'ref' , ref : '@e2' } ,
451+ } ) ;
452+ // The original scope still pins.
453+ await executor . execute ( 'press' , {
454+ session : 'demo' ,
455+ stateDir : '/state/a' ,
456+ target : { kind : 'ref' , ref : '@e2' } ,
457+ } ) ;
458+
459+ assert . deepEqual ( runCalls [ 1 ] ?. input , { session : 'demo' , target : { kind : 'ref' , ref : '@e2' } } ) ;
460+ assert . deepEqual ( runCalls [ 2 ] ?. input , {
461+ session : 'demo' ,
462+ target : { kind : 'ref' , ref : '@e2~s500012' } ,
386463 } ) ;
387464} ) ;
388465
389- test ( 'MCP forgets the generation when a ref-issuing response stops carrying one ' , async ( ) => {
466+ test ( 'MCP clears the whole scope when a ref-issuing response stops carrying a generation ' , async ( ) => {
390467 const runCalls : Array < { name : string ; input : unknown } > = [ ] ;
391468 let issueGeneration = true ;
392469 const executor = createCommandToolExecutor ( {
@@ -395,17 +472,17 @@ test('MCP forgets the generation when a ref-issuing response stops carrying one'
395472 runCalls . push ( { name, input } ) ;
396473 if ( name === 'snapshot' ) {
397474 return issueGeneration
398- ? { nodes : [ ] , truncated : false , refsGeneration : 4 }
399- : { nodes : [ ] , truncated : false } ;
475+ ? { nodes : [ { ref : 'e2' } ] , truncated : false , refsGeneration : 4 }
476+ : { nodes : [ { ref : 'e2' } ] , truncated : false } ;
400477 }
401478 return { } ;
402479 } ,
403480 } ) ;
404481
405482 await executor . execute ( 'snapshot' , { } ) ;
406483 issueGeneration = false ;
407- // An older daemon (or a response view) without refsGeneration: the stale
408- // remembered generation must not leak onto new refs .
484+ // An older daemon without refsGeneration: the remembered pins must not
485+ // leak onto refs the response did not vouch for .
409486 await executor . execute ( 'snapshot' , { } ) ;
410487 await executor . execute ( 'press' , { target : { kind : 'ref' , ref : '@e2' } } ) ;
411488
@@ -432,7 +509,7 @@ test('MCP renders tool text from the unpinned input so the model never sees suff
432509 createClient : ( ) => ( { } ) as AgentDeviceClient ,
433510 runCommand : async ( _client , name ) =>
434511 name === 'snapshot'
435- ? { nodes : [ ] , truncated : false , refsGeneration : 9 }
512+ ? { nodes : [ { ref : 'e2' } ] , truncated : false , refsGeneration : 9 }
436513 : { message : 'Tapped @e2 (10, 20)' } ,
437514 } ) ;
438515
0 commit comments