@@ -13,13 +13,15 @@ import {
1313 BASE ,
1414 MAX_NAMED ,
1515 checkSession ,
16+ recheckCommand ,
1617 report ,
1718} from "./session-instructions-check.mjs" ;
1819
1920const SHA = "0123456789abcdef" . repeat ( 2 ) + "01234567" ;
2021const fresh = {
2122 fetched : true ,
2223 baseSha : SHA ,
24+ startSha : null ,
2325 stale : false ,
2426 changed : [ ] ,
2527 primary : false ,
@@ -31,6 +33,22 @@ test("report: fresh, in a worktree, prints one confirming line", () => {
3133 ] ) ;
3234} ) ;
3335
36+ test ( "report: a known start revision adds the re-check, anchored at the root" , ( ) => {
37+ const lines = report ( { ...fresh , startSha : SHA } ) ;
38+ assert . equal ( lines . length , 2 ) ;
39+ assert . ok ( lines [ 1 ] . includes ( recheckCommand ( SHA ) ) , lines [ 1 ] ) ;
40+ assert . equal (
41+ recheckCommand ( SHA ) ,
42+ `git fetch origin v2/main && git diff --quiet ${ SHA } ...origin/v2/main -- ':/AGENTS.md' ':/.claude/' || echo STALE` ,
43+ ) ;
44+ // A stale session stops; it is not told how to re-check.
45+ assert . equal (
46+ report ( { ...fresh , startSha : SHA , stale : true , changed : [ "AGENTS.md" ] } )
47+ . length ,
48+ 1 ,
49+ ) ;
50+ } ) ;
51+
3452test ( "report: stale names what changed and says to stop" , ( ) => {
3553 const [ line ] = report ( {
3654 ...fresh ,
@@ -133,7 +151,10 @@ test("checkSession: a primary checkout at origin/v2/main is fresh, with the prim
133151 t . after ( s . done ) ;
134152 const lines = checkSession ( { cwd : s . work } ) ;
135153 assert . match ( lines [ 0 ] , / m a t c h o r i g i n \/ v 2 \/ m a i n / ) ;
136- assert . match ( lines [ 1 ] , / s t a r t e d i n t h e p r i m a r y c h e c k o u t / ) ;
154+ assert . ok (
155+ lines . some ( ( l ) => / s t a r t e d i n t h e p r i m a r y c h e c k o u t / . test ( l ) ) ,
156+ lines . join ( "\n" ) ,
157+ ) ;
137158} ) ;
138159
139160test ( "checkSession: the fetch finds an instruction change made behind the checkout's back" , ( t ) => {
@@ -170,8 +191,45 @@ test("checkSession: a fresh worktree is fresh, with no primary note", (t) => {
170191 const wt = path . join ( s . work , ".." , "wt" ) ;
171192 s . git ( "worktree" , "add" , "-q" , "--detach" , wt , "origin/v2/main" ) ;
172193 const lines = checkSession ( { cwd : wt } ) ;
173- assert . equal ( lines . length , 1 , lines . join ( "\n" ) ) ;
194+ assert . equal ( lines . length , 2 , lines . join ( "\n" ) ) ;
174195 assert . match ( lines [ 0 ] , / m a t c h o r i g i n \/ v 2 \/ m a i n / ) ;
196+ assert . match ( lines [ 1 ] , / l o a d e d i t s i n s t r u c t i o n s a t [ 0 - 9 a - f ] { 40 } / ) ;
197+ } ) ;
198+
199+ test ( "checkSession: run from a subdirectory, it still checks the root instructions (#5219 review)" , ( t ) => {
200+ const s = setup ( ) ;
201+ t . after ( s . done ) ;
202+ s . write ( "src/git/README.md" , "a server\n" ) ;
203+ s . git ( "add" , "-A" ) ;
204+ s . git ( "commit" , "-q" , "-m" , "a server directory" ) ;
205+ s . advance ( "AGENTS.md" , "rules v2\n" ) ;
206+ const lines = checkSession ( { cwd : path . join ( s . work , "src" , "git" ) } ) ;
207+ assert . match ( lines [ 0 ] , / ^ S T A L E : .* c h a n g e d A G E N T S \. m d s i n c e / ) ;
208+ } ) ;
209+
210+ test ( "checkSession: the printed re-check catches stale rules after moving to a newer worktree (#5219 review)" , ( t ) => {
211+ const s = setup ( ) ;
212+ t . after ( s . done ) ;
213+ // The session starts here, fresh, and is told its start revision.
214+ const started = checkSession ( { cwd : s . work } ) ;
215+ const recheck = started . find ( ( l ) => l . includes ( "Re-check from" ) ) ;
216+ assert . ok ( recheck , started . join ( "\n" ) ) ;
217+ const cmd = / c h e c k o u t : ( .* ) $ / . exec ( recheck ) [ 1 ] ;
218+
219+ // v2/main changes the rules; the session cuts a new worktree from it.
220+ s . advance ( "AGENTS.md" , "rules v2\n" ) ;
221+ s . git ( "fetch" , "-q" , "origin" , "v2/main" ) ;
222+ const wt = path . join ( s . work , ".." , "newer" ) ;
223+ s . git ( "worktree" , "add" , "-q" , "--detach" , wt , "origin/v2/main" ) ;
224+
225+ // A HEAD-based check in the new worktree passes; the printed one does not.
226+ const sh = ( command ) =>
227+ spawnSync ( "sh" , [ "-c" , command ] , { cwd : wt , env, encoding : "utf8" } ) ;
228+ assert . equal (
229+ sh ( "git diff --quiet HEAD...origin/v2/main -- ':/AGENTS.md'" ) . status ,
230+ 0 ,
231+ ) ;
232+ assert . equal ( sh ( cmd ) . stdout . trim ( ) , "STALE" ) ;
175233} ) ;
176234
177235test ( "checkSession: a HEAD with no history in common with v2/main is stale" , ( t ) => {
0 commit comments