@@ -331,6 +331,114 @@ describe("loadControlUiSessionPullRequests", () => {
331331 expect ( fetchImpl . mock . calls ) . toHaveLength ( 0 ) ;
332332 } ) ;
333333
334+ it ( "caches git context through repeated GitHub failures, then expires it" , async ( ) => {
335+ const fetchImpl = routedFetch ( [
336+ {
337+ match : "/pulls?head=" ,
338+ response : ( ) => githubJson ( { message : "unavailable" } , 503 ) ,
339+ } ,
340+ ] ) ;
341+ const gitOutputImpl = vi . fn ( async ( _root : string , args : string [ ] ) => {
342+ if ( args [ 0 ] === "rev-parse" ) {
343+ return "feature" ;
344+ }
345+ if ( args [ 0 ] === "remote" ) {
346+ return "[email protected] :openclaw/openclaw.git" ; 347+ }
348+ return "origin/main" ;
349+ } ) ;
350+ const load = ( root = "/repo/context-cache" ) =>
351+ loadControlUiSessionPullRequests (
352+ { sessionKey : "agent:main:main" } ,
353+ {
354+ fetchImpl,
355+ resolveGitRoot : async ( ) => root ,
356+ gitOutput : gitOutputImpl ,
357+ } ,
358+ ) ;
359+
360+ await expect ( load ( ) ) . rejects . toBeInstanceOf ( Error ) ;
361+ await expect ( load ( ) ) . rejects . toBeInstanceOf ( Error ) ;
362+ expect ( gitOutputImpl ) . toHaveBeenCalledTimes ( 3 ) ;
363+ expect ( fetchImpl . mock . calls ) . toHaveLength ( 1 ) ;
364+
365+ await expect ( load ( "/repo/other-context" ) ) . rejects . toBeInstanceOf ( Error ) ;
366+ expect ( gitOutputImpl ) . toHaveBeenCalledTimes ( 6 ) ;
367+
368+ vi . advanceTimersByTime ( 10_001 ) ;
369+ await expect ( load ( ) ) . rejects . toBeInstanceOf ( Error ) ;
370+ expect ( gitOutputImpl ) . toHaveBeenCalledTimes ( 9 ) ;
371+ // The longer GitHub failure cache remains independent of local Git expiry.
372+ expect ( fetchImpl . mock . calls ) . toHaveLength ( 1 ) ;
373+ } ) ;
374+
375+ it ( "caches branch facts by root while refresh only bypasses the GitHub cache" , async ( ) => {
376+ let pulls : Record < string , unknown > [ ] = [ ] ;
377+ const fetchImpl = routedFetch ( [
378+ { match : "/pulls?head=" , response : ( ) => githubJson ( pulls ) } ,
379+ { match : "/repos/openclaw/openclaw" , response : ( ) => githubJson ( { fork : false } ) } ,
380+ ] ) ;
381+ const resolveBranchLanding = vi . fn ( async ( ) => ( {
382+ pushedSha : "a" . repeat ( 40 ) ,
383+ statsBase : "base" ,
384+ hasLandedPullRequest : false ,
385+ provenNewPushedWork : false ,
386+ } ) ) ;
387+ const gitOutputImpl = vi . fn ( async ( _root : string , args : string [ ] ) =>
388+ args [ 0 ] === "rev-list" ? "1" : null ,
389+ ) ;
390+ const runGitImpl = vi . fn ( async ( root : string ) => ( {
391+ stdout :
392+ root === "/repo/b" ? " 1 file changed, 2 insertions(+)" : " 1 file changed, 1 insertion(+)" ,
393+ stderr : "" ,
394+ code : 0 ,
395+ } ) ) ;
396+ const load = ( sessionKey : string , refresh = false ) =>
397+ loadControlUiSessionPullRequests (
398+ { sessionKey, ...( refresh ? { refresh : true } : { } ) } ,
399+ {
400+ fetchImpl,
401+ resolveGitContext : async ( ) => ( {
402+ ...context ,
403+ branch : "cache/test" ,
404+ root : sessionKey . endsWith ( ":b" ) ? "/repo/b" : "/repo/a" ,
405+ defaultBranch : "main" ,
406+ } ) ,
407+ gitOutput : gitOutputImpl ,
408+ runGit : runGitImpl ,
409+ resolveBranchLanding,
410+ } ,
411+ ) ;
412+
413+ expect ( ( await load ( "agent:main:a" ) ) . branch ?. additions ) . toBe ( 1 ) ;
414+ expect ( ( await load ( "agent:main:a" , true ) ) . branch ?. additions ) . toBe ( 1 ) ;
415+ expect ( resolveBranchLanding ) . toHaveBeenCalledTimes ( 1 ) ;
416+ expect ( runGitImpl ) . toHaveBeenCalledTimes ( 1 ) ;
417+ expect ( gitOutputImpl ) . toHaveBeenCalledTimes ( 2 ) ;
418+ expect (
419+ fetchImpl . mock . calls . filter ( ( call ) =>
420+ requestUrl ( call [ 0 ] as RequestInfo | URL ) . includes ( "/pulls?head=" ) ,
421+ ) ,
422+ ) . toHaveLength ( 2 ) ;
423+
424+ pulls = [ pullListItem ( { merged_at : "2026-07-09T10:00:00Z" } ) ] ;
425+ expect ( ( await load ( "agent:main:a" , true ) ) . branch ?. additions ) . toBe ( 1 ) ;
426+ expect ( resolveBranchLanding ) . toHaveBeenCalledTimes ( 2 ) ;
427+ expect ( runGitImpl ) . toHaveBeenCalledTimes ( 2 ) ;
428+ expect ( gitOutputImpl ) . toHaveBeenCalledTimes ( 4 ) ;
429+
430+ vi . advanceTimersByTime ( 10_001 ) ;
431+ expect ( ( await load ( "agent:main:a" ) ) . branch ?. additions ) . toBe ( 1 ) ;
432+ expect ( resolveBranchLanding ) . toHaveBeenCalledTimes ( 3 ) ;
433+ expect ( runGitImpl ) . toHaveBeenCalledTimes ( 3 ) ;
434+ expect ( gitOutputImpl ) . toHaveBeenCalledTimes ( 6 ) ;
435+
436+ expect ( ( await load ( "agent:main:b" ) ) . branch ?. additions ) . toBe ( 2 ) ;
437+ expect ( resolveBranchLanding ) . toHaveBeenCalledTimes ( 4 ) ;
438+ expect ( runGitImpl ) . toHaveBeenCalledTimes ( 4 ) ;
439+ expect ( gitOutputImpl ) . toHaveBeenCalledTimes ( 8 ) ;
440+ } ) ;
441+
334442 it ( "refreshes a cached empty result after the assistant creates a PR" , async ( ) => {
335443 let pulls : Record < string , unknown > [ ] = [ ] ;
336444 const fetchImpl = routedFetch ( [
0 commit comments