From f92341d1a2dd3c03afc59efa8c40679a99108b3f Mon Sep 17 00:00:00 2001 From: Harvmaster Date: Sat, 3 Oct 2026 18:17:56 +0000 Subject: [PATCH] shared/nextedit: trim before-context in a few encodes instead of one per line Same output on 397/398 test prompts, ~6x faster prompt building (daemon and export). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J39imRDT8L7xYbWzsVWd8A --- shared/nextedit.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/shared/nextedit.ts b/shared/nextedit.ts index 8b11f21..b0a32b7 100644 --- a/shared/nextedit.ts +++ b/shared/nextedit.ts @@ -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))