@@ -145,7 +145,7 @@ function deriveKnowledgeFiles(
145145}
146146
147147export async function initialSessionState (
148- cwd : string ,
148+ cwd : string | undefined ,
149149 options : {
150150 projectFiles ?: Record < string , string >
151151 knowledgeFiles ?: Record < string , string >
@@ -157,28 +157,34 @@ export async function initialSessionState(
157157 let { projectFiles, agentDefinitions = [ ] } = options
158158 let { knowledgeFiles } = options
159159
160- // Auto-discover project files if not provided
161- if ( projectFiles === undefined ) {
160+ // Auto-discover project files if not provided and cwd is available
161+ if ( projectFiles === undefined && cwd ) {
162162 projectFiles = discoverProjectFiles ( cwd )
163163 }
164164 if ( knowledgeFiles === undefined ) {
165- knowledgeFiles = deriveKnowledgeFiles ( projectFiles )
165+ knowledgeFiles = projectFiles ? deriveKnowledgeFiles ( projectFiles ) : { }
166166 }
167167
168168 const processedAgentTemplates = processAgentDefinitions ( agentDefinitions )
169169 const processedCustomToolDefinitions = processCustomToolDefinitions (
170170 options . customToolDefinitions ?? [ ] ,
171171 )
172172
173- // Generate file tree and token scores from projectFiles
174- const { fileTree, fileTokenScores, tokenCallers } = await computeProjectIndex (
175- cwd ,
176- projectFiles ,
177- )
173+ // Generate file tree and token scores from projectFiles if available
174+ let fileTree : FileTreeNode [ ] = [ ]
175+ let fileTokenScores : Record < string , any > = { }
176+ let tokenCallers : Record < string , any > = { }
177+
178+ if ( cwd && projectFiles ) {
179+ const result = await computeProjectIndex ( cwd , projectFiles )
180+ fileTree = result . fileTree
181+ fileTokenScores = result . fileTokenScores
182+ tokenCallers = result . tokenCallers
183+ }
178184
179185 const initialState = getInitialSessionState ( {
180- projectRoot : cwd ,
181- cwd,
186+ projectRoot : cwd ?? process . cwd ( ) ,
187+ cwd : cwd ?? process . cwd ( ) ,
182188 fileTree,
183189 fileTokenScores,
184190 tokenCallers,
@@ -276,7 +282,7 @@ export function withMessageHistory({
276282 * even when continuing from a previous run.
277283 */
278284export async function applyOverridesToSessionState (
279- cwd : string ,
285+ cwd : string | undefined ,
280286 baseSessionState : SessionState ,
281287 overrides : {
282288 projectFiles ?: Record < string , string >
@@ -298,11 +304,18 @@ export async function applyOverridesToSessionState(
298304
299305 // Apply projectFiles override (recomputes file tree and token scores)
300306 if ( overrides . projectFiles !== undefined ) {
301- const { fileTree, fileTokenScores, tokenCallers } =
302- await computeProjectIndex ( cwd , overrides . projectFiles )
303- sessionState . fileContext . fileTree = fileTree
304- sessionState . fileContext . fileTokenScores = fileTokenScores
305- sessionState . fileContext . tokenCallers = tokenCallers
307+ if ( cwd ) {
308+ const { fileTree, fileTokenScores, tokenCallers } =
309+ await computeProjectIndex ( cwd , overrides . projectFiles )
310+ sessionState . fileContext . fileTree = fileTree
311+ sessionState . fileContext . fileTokenScores = fileTokenScores
312+ sessionState . fileContext . tokenCallers = tokenCallers
313+ } else {
314+ // If projectFiles are provided but no cwd, reset file context fields
315+ sessionState . fileContext . fileTree = [ ]
316+ sessionState . fileContext . fileTokenScores = { }
317+ sessionState . fileContext . tokenCallers = { }
318+ }
306319
307320 // Auto-derive knowledgeFiles if not explicitly provided
308321 if ( overrides . knowledgeFiles === undefined ) {
0 commit comments