shared/nextedit: trim before-context in a few encodes instead of one per line
ci / build (push) Canceled after 0s

Same output on 397/398 test prompts, ~6x faster prompt building (daemon and export).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J39imRDT8L7xYbWzsVWd8A
This commit is contained in:
2026-10-03 18:17:56 +00:00
co-authored by Claude Opus 5.5
parent 4c86c2a703
commit f92341d1a2
+7 -2
View File
@@ -124,11 +124,16 @@ export function buildNextEditPrompt(input: NextEditInput, encode: Encode, ids: N
out.push(ids.fileSep, ...encode(`${input.path}\n`))
// Before the region: last lines that fit; after: first lines that fit.
// (Window to ~6 chars/token first, then drop whole lines in proportion to the excess: a few encodes, not one per line.)
let before = input.file.slice(0, rStart)
if (before.length > beforeTokens * 6) before = before.slice(before.indexOf('\n', before.length - beforeTokens * 6) + 1)
let beforeIds = encode(before)
while (beforeIds.length > beforeTokens && before.includes('\n')) {
before = before.slice(before.indexOf('\n') + 1)
beforeIds = beforeIds.length > beforeTokens * 2 ? encode(before.slice(-beforeTokens * 6)) : encode(before)
const excess = beforeIds.length - beforeTokens // undershoot (×0.8), then single lines: same cut as line-by-line
const drop = excess > 32 ? Math.floor(0.8 * before.length * excess / beforeIds.length) : 1
const nl = before.indexOf('\n', drop - 1)
before = nl < 0 ? '' : before.slice(nl + 1)
beforeIds = encode(before)
}
let after = input.file.slice(rEnd)
let afterIds = encode(after.slice(0, afterTokens * 8))