Skip to content

Commit 15d6e5c

Browse files
committed
perf: reduce lookup and compression overhead
1 parent b637139 commit 15d6e5c

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎src/path_suggest.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,14 @@ function nearNameFiles(missingAbs: string, rootDir: string, files: readonly Inde
174174
scored.push({ abs, rank: sameDir ? 0 : 1, distance })
175175
}
176176
for (const name of entriesOf(dir, scope)) consider(path.join(dir, name), true)
177-
for (const file of files ?? []) consider(resolveIndexed(file.filePath, rootDir), false)
177+
for (const file of files ?? []) {
178+
const name = file.base.toLowerCase()
179+
const distance = levenshteinDistance(name, lower, budget)
180+
if (distance > budget) continue
181+
// The same-name rung already offered exact-case matches. Resolve only plausible typos instead of normalizing every indexed path.
182+
if (distance === 0 && path.basename(file.filePath) === base) continue
183+
scored.push({ abs: resolveIndexed(file.filePath, rootDir), rank: 1, distance })
184+
}
178185
return scored.sort((a, b) => a.rank - b.rank || a.distance - b.distance || a.abs.localeCompare(b.abs)).map((entry) => entry.abs).filter((abs) => !samePath(abs, missingAbs) && !fileIsAbsent(abs) && permitted(abs, scope))
179186
}
180187

‎src/tool_filters/helpers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ export function capBytes(text: string, maxBytes: number): string {
464464

465465
/** Truncate `text` to approximately `maxTokens` tokens, measured on the ANSI-stripped string so escape sequences don't trip the cap early. */
466466
export function capTokens(text: string, maxTokens: number): string {
467+
// Stripping ANSI can only shorten the text, so avoid scanning common under-budget output.
468+
if (text.length / 3.5 <= maxTokens) return text
467469
const clean = stripAnsiEscapes(text)
468470
if (clean.length / 3.5 <= maxTokens) return text
469471
const maxBytes = Math.floor(maxTokens * 3.5)

‎tests/filter_input_cap_keeps_ends.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe('filter input cap keeps both ends', () => {
7171
expect(capped).toContain('[token-goat: output capped at ~2000 tokens]')
7272
})
7373

74+
it('leaves under-budget ANSI output unchanged', () => {
75+
const body = '\u001b[31mshort output\u001b[0m'
76+
expect(capTokens(body, 2000)).toBe(body)
77+
})
78+
7479
it('delivers the vitest verdict through the real filter, not just the clamp', () => {
7580
// The unit above tests the helper; this drives the shipping path the hook actually uses, because the cap is applied inside `apply` and a filter that dropped the summary downstream would still pass the helper's tests.
7681
const filter = filterByName('vitest')

0 commit comments

Comments
 (0)