Skip to content

Commit 2eb4681

Browse files
committed
Skip oversized discovered files before scoring
1 parent f9841a1 commit 2eb4681

2 files changed

Lines changed: 55 additions & 15 deletions

File tree

sdk/src/__tests__/initial-session-state.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,18 @@ describe('Initial Session State', () => {
121121
return ['src', '.git', 'knowledge.md', 'README.md', '.gitignore']
122122
}
123123
if (dirPath === '/test-project/src') {
124-
return ['index.ts', 'utils.ts']
124+
return ['index.ts', 'utils.ts', 'generated.ts']
125125
}
126126
return []
127127
}) as CodebuffFileSystem['readdir']
128-
mockFs.stat = (async (filePath: string): Promise<MockStatResult> => ({
129-
isDirectory: () =>
130-
filePath === '/test-project/src' || filePath === '/test-project/.git',
131-
isFile: () =>
132-
filePath !== '/test-project/src' && filePath !== '/test-project/.git',
133-
})) as CodebuffFileSystem['stat']
128+
mockFs.stat = (async (filePath: string) =>
129+
({
130+
isDirectory: () =>
131+
filePath === '/test-project/src' || filePath === '/test-project/.git',
132+
isFile: () =>
133+
filePath !== '/test-project/src' && filePath !== '/test-project/.git',
134+
size: filePath.endsWith('generated.ts') ? 1_000_001 : 100,
135+
}) as MockStatResult & { size: number }) as CodebuffFileSystem['stat']
134136

135137
const readFilePaths: string[] = []
136138
const originalReadFile = mockFs.readFile
@@ -151,6 +153,9 @@ describe('Initial Session State', () => {
151153
expect(sessionState.mainAgentState.messageHistory).toEqual([])
152154
expect(readFilePaths.some((p) => p.endsWith('src/index.ts'))).toBe(true)
153155
expect(readFilePaths.some((p) => p.endsWith('src/utils.ts'))).toBe(true)
156+
expect(readFilePaths.some((p) => p.endsWith('src/generated.ts'))).toBe(
157+
false,
158+
)
154159
expect(readFilePaths.some((p) => p.endsWith('README.md'))).toBe(false)
155160
expect(readFilePaths.some((p) => p.endsWith('knowledge.md'))).toBe(true)
156161
})

sdk/src/run-state.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
144146
async 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

Comments
 (0)