@@ -141,6 +141,8 @@ type ProjectIndexInput = {
141141 readFile ?: ( filePath : string ) => string | null | Promise < string | null >
142142}
143143
144+ const DEFAULT_SYMBOL_PARSE_FILE_BYTES = 1_000_000
145+
144146async function computeProjectIndex ( params : ProjectIndexInput ) : Promise < {
145147 fileTree : FileTreeNode [ ]
146148 fileTokenScores : Record < string , any >
@@ -190,20 +192,53 @@ function getProjectIndexInput(params: {
190192 cwd,
191193 fileTree : discoveredProject . fileTree ,
192194 filePaths : discoveredProject . filePaths . sort ( ) ,
193- readFile : ( filePath : string ) =>
194- fs . readFile ( path . join ( cwd , filePath ) , 'utf8' ) . catch ( ( error ) => {
195- logger . debug ?.(
196- { filePath, error : getErrorObject ( error ) } ,
197- 'Failed to read discovered project file for symbol scoring' ,
198- )
199- return null
200- } ) ,
195+ readFile : createDiscoveredProjectReader ( { cwd, fs, logger } ) ,
201196 }
202197 }
203198
204199 return undefined
205200}
206201
202+ function createDiscoveredProjectReader ( params : {
203+ cwd : string
204+ fs : CodebuffFileSystem
205+ logger : Logger
206+ } ) : ( filePath : string ) => Promise < string | null > {
207+ const { cwd, fs, logger } = params
208+ const maxBytes = getSymbolParseFileByteLimit ( )
209+
210+ return async ( filePath : string ) => {
211+ const fullPath = path . join ( cwd , filePath )
212+ try {
213+ const stats = await fs . stat ( fullPath )
214+ if ( getFileSize ( stats ) > maxBytes ) {
215+ return null
216+ }
217+ return await fs . readFile ( fullPath , 'utf8' )
218+ } catch ( error ) {
219+ logger . debug ?.(
220+ { filePath, error : getErrorObject ( error ) } ,
221+ 'Failed to read discovered project file for symbol scoring' ,
222+ )
223+ return null
224+ }
225+ }
226+ }
227+
228+ function getFileSize ( stats : Awaited < ReturnType < CodebuffFileSystem [ 'stat' ] > > ) {
229+ return typeof stats . size === 'number' ? stats . size : 0
230+ }
231+
232+ function getSymbolParseFileByteLimit ( ) {
233+ const raw = process . env . CODEBUFF_MAX_PARSE_FILE_BYTES
234+ if ( ! raw ) return DEFAULT_SYMBOL_PARSE_FILE_BYTES
235+
236+ const parsed = Number . parseInt ( raw , 10 )
237+ return Number . isFinite ( parsed ) && parsed > 0
238+ ? parsed
239+ : DEFAULT_SYMBOL_PARSE_FILE_BYTES
240+ }
241+
207242/**
208243 * Helper to convert ChildProcess to Promise with stdout/stderr
209244 */
0 commit comments