@@ -23,6 +23,7 @@ import * as fs from 'node:fs'
2323
2424import type { HookEvent } from '../src/hook_registry.js'
2525import { preReadHandler } from '../src/hooks_read.js'
26+ import { postReadHandler } from '../src/hooks_read_post.js'
2627import { normalizePath } from '../src/paths.js'
2728import { clearModuleCaches } from '../src/reset.js'
2829import { makeHookEvent } from './helpers/hook-event.js'
@@ -67,6 +68,16 @@ function makeTmpXml(totalBytes: number): string {
6768 return p
6869}
6970
71+ function makeTmpSource ( lineCount : number ) : string {
72+ const content = Array . from ( { length : lineCount } , ( _ , i ) =>
73+ `// synthetic source line ${ i + 1 } : enough content to clear the hint savings floor` ,
74+ ) . join ( '\n' ) + '\n'
75+ const p = path . join ( os . tmpdir ( ) , `tg-read-post-${ process . pid } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } .ts` )
76+ fs . writeFileSync ( p , content )
77+ tmpFiles . push ( p )
78+ return p
79+ }
80+
7081describe ( 'universal file-type handler readFileSync guard (regression)' , ( ) => {
7182 beforeEach ( ( ) => {
7283 clearModuleCaches ( )
@@ -110,7 +121,24 @@ describe('universal file-type handler readFileSync guard (regression)', () => {
110121 const output = preReadHandler ( readEventWithRange ( p , 1 , 50 ) )
111122
112123 expect ( output . hookType ) . not . toBe ( 'deny' )
113- // One requested-window estimate plus one full preimage; the line-count nudge must not reopen it.
114- expect ( mockState . callCount ) . toBe ( 2 )
124+ // The type scan's bounded full preimage is shared with the line-count nudge.
125+ expect ( mockState . callCount ) . toBe ( 1 )
126+ } )
127+
128+ it ( 'reuses the source preimage for snapshot and structural-navigation checks after a Read' , ( ) => {
129+ const p = makeTmpSource ( 250 )
130+ mockState . watchedPath = normalizePath ( p )
131+
132+ postReadHandler ( makeHookEvent ( {
133+ eventName : 'post_tool_use' ,
134+ toolName : 'Read' ,
135+ toolInput : { file_path : p } ,
136+ sessionId : `post-read-preimage-${ process . pid } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } ` ,
137+ raw : { tool_response : 'source read result' } ,
138+ } ) )
139+
140+ // The fold scan and served-window reconciliation each read independently; the snapshot and
141+ // structural-navigation gates share their single preimage.
142+ expect ( mockState . callCount ) . toBe ( 3 )
115143 } )
116144} )
0 commit comments