@@ -19,6 +19,7 @@ import {
1919} from "../../../packages/gateway-protocol/src/index.js" ;
2020import { resolveAgentWorkspaceDir , resolveDefaultAgentId } from "../../agents/agent-scope.js" ;
2121import { resolveToCwd as resolveSessionToolPathToCwd } from "../../agents/sessions/tools/path-utils.js" ;
22+ import { runGit } from "../../agents/worktrees/git.js" ;
2223import { FsSafeError } from "../../infra/fs-safe.js" ;
2324import { normalizeAgentId , parseAgentSessionKey } from "../../routing/session-key.js" ;
2425import { visitSessionMessagesAsync } from "../session-transcript-readers.js" ;
@@ -59,6 +60,7 @@ type TouchedFile = {
5960type LoadedSessionFiles = {
6061 root ?: string ;
6162 fileRoot ?: string ;
63+ diffCwd ?: string ;
6264 files : TouchedFile [ ] ;
6365} ;
6466
@@ -359,15 +361,21 @@ function loadSessionFileRoot(params: { sessionKey: string; agentId?: string }) {
359361 resolveDefaultAgentId ( loaded . cfg ) ,
360362 ) ;
361363 const spawnedCwd = normalizePathValue ( loaded . entry . spawnedCwd ) ;
362- const root =
363- normalizePathValue ( loaded . entry . spawnedWorkspaceDir ) ??
364- spawnedCwd ??
365- normalizePathValue ( resolveAgentWorkspaceDir ( loaded . cfg , agentId ) ) ;
364+ const spawnedWorkspaceDir = normalizePathValue ( loaded . entry . spawnedWorkspaceDir ) ;
365+ const configuredWorkspaceDir =
366+ spawnedCwd || spawnedWorkspaceDir
367+ ? undefined
368+ : normalizePathValue ( resolveAgentWorkspaceDir ( loaded . cfg , agentId ) ) ;
369+ // Keep this cwd precedence aligned with sessions.diff so the advertised
370+ // checkout state cannot disagree with the panel's fallback result.
371+ const diffCwd = spawnedCwd ?? spawnedWorkspaceDir ?? configuredWorkspaceDir ;
372+ const root = spawnedWorkspaceDir ?? spawnedCwd ?? configuredWorkspaceDir ;
366373 return {
367374 ...loaded ,
368375 agentId,
369376 root,
370377 fileRoot : resolveFileRoot ( { root, spawnedCwd } ) ,
378+ diffCwd,
371379 } ;
372380}
373381
@@ -542,6 +550,7 @@ async function loadSessionFiles(params: {
542550 return {
543551 root : loaded . root ,
544552 fileRoot : loaded . fileRoot ,
553+ diffCwd : loaded . diffCwd ,
545554 files : [ ...files . values ( ) ] . toSorted ( ( a , b ) => {
546555 if ( a . kind !== b . kind ) {
547556 return a . kind === "modified" ? - 1 : 1 ;
@@ -556,9 +565,23 @@ async function buildListResult(params: {
556565 agentId ?: string ;
557566 path ?: string ;
558567 search ?: string ;
559- } ) : Promise < { root ?: string ; files : SessionFileEntry [ ] ; browser ?: SessionFileBrowserResult } > {
568+ } ) : Promise < {
569+ root ?: string ;
570+ gitCheckout ?: boolean ;
571+ files : SessionFileEntry [ ] ;
572+ browser ?: SessionFileBrowserResult ;
573+ } > {
560574 const loaded = await loadSessionFiles ( params ) ;
561575 const root = loaded . root ;
576+ let gitCheckout : boolean | undefined ;
577+ if ( loaded . diffCwd ) {
578+ try {
579+ const result = await runGit ( loaded . diffCwd , [ "rev-parse" , "--show-toplevel" ] ) ;
580+ gitCheckout = result . code === 0 && Boolean ( result . stdout . trim ( ) ) ;
581+ } catch {
582+ gitCheckout = false ;
583+ }
584+ }
562585 const workspaceFiles = root
563586 ? loaded . files . filter ( ( file ) =>
564587 Boolean ( resolveTouchedFilePath ( { root, fileRoot : loaded . fileRoot , filePath : file . path } ) ) ,
@@ -576,6 +599,7 @@ async function buildListResult(params: {
576599 } ) ;
577600 return {
578601 ...( root ? { root } : { } ) ,
602+ ...( gitCheckout === undefined ? { } : { gitCheckout } ) ,
579603 files,
580604 ...( browser ? { browser } : { } ) ,
581605 } ;
0 commit comments