From fcde82d39f359178ae865c08c99e03e2c09b7e34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=B1j=C5=AB=E1=B9=A3aka?= <153098568+manjughosa@users.noreply.github.com> Date: Wed, 11 Nov 2026 15:37:29 +0800 Subject: [PATCH] history-trim 0.1.2: trim trailing whitespace in linear time (#975) history-trim 0.1.2. The whitespace step trimmed trailing spaces with /[ \t]+$/gm, which takes quadratic time on a long run of spaces followed by a word: 1.2 s at 40,000 spaces on Node 22. A one-pass scan replaces it. It runs after the blank-line fold, as before, and trims before every line end that `$` sees under /m (\n, \r, U+2028, U+2029 and the end of the text). The output is the same as main's. Four cases.json regressions are added: CRLF, whitespace-only lines, U+2028, and a long run of spaces before a word. What was run at 0ad27d87, merged with origin/main: - `bun test packages/history-trim` (27 pass) and `bun scripts/check.mjs` (ok), on WSL Ubuntu 24.04 and on Windows 11, each with a fresh HOME (and USERPROFILE on Windows). - main's onRequest against the PR's, on 500,000 random strings over space, tab, \n, \r, U+2028, U+2029, U+0085, \v, \f, U+00A0, U+3000, U+FEFF, U+2000 and U+200A: 0 differ, on Node 22 and Bun. - Timing at 40,000 spaces + "a": main 1,223 ms, PR 0.28 ms; 1,000,000 spaces: 7 ms (Node 22, mote). - Broken by hand: treating only LF as a line end turns the CRLF and U+2028 cases red. Trimming before the fold turns the whitespace-only-lines case red. --- packages/history-trim/cases.json | 40 +++++++++++++++++++ .../history-trim/history-trim.middleware.js | 20 +++++++++- packages/history-trim/package.json | 2 +- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/packages/history-trim/cases.json b/packages/history-trim/cases.json index 2d1ac5e..48c1ee5 100644 --- a/packages/history-trim/cases.json +++ b/packages/history-trim/cases.json @@ -834,5 +834,45 @@ } ] } + }, + { + "name": "a space before CRLF is trimmed without changing the newline", + "request": { + "model": "m", + "messages": [{ "role": "user", "content": "a \r\nb" }, { "role": "assistant", "content": "ok" }] + }, + "expect_request": { + "model": "m", + "messages": [{ "role": "user", "content": "a\r\nb" }, { "role": "assistant", "content": "ok" }] + } + }, + { + "name": "whitespace-only lines do not become new blank lines before folding", + "request": { + "model": "m", + "messages": [{ "role": "user", "content": "a\n \n \n \nb" }, { "role": "assistant", "content": "ok" }] + }, + "expect_request": { + "model": "m", + "messages": [{ "role": "user", "content": "a\n\n\n\nb" }, { "role": "assistant", "content": "ok" }] + } + }, + { + "name": "a space before a Unicode line separator is trimmed", + "request": { + "model": "m", + "messages": [{ "role": "user", "content": "a 
b" }, { "role": "assistant", "content": "ok" }] + }, + "expect_request": { + "model": "m", + "messages": [{ "role": "user", "content": "a
b" }, { "role": "assistant", "content": "ok" }] + } + }, + { + "name": "a long trailing space run before a word is left whole", + "request": { + "model": "m", + "messages": [{ "role": "user", "content": " a" }, { "role": "assistant", "content": "ok" }] + } } ] diff --git a/packages/history-trim/history-trim.middleware.js b/packages/history-trim/history-trim.middleware.js index f2b7aa4..627f405 100644 --- a/packages/history-trim/history-trim.middleware.js +++ b/packages/history-trim/history-trim.middleware.js @@ -58,8 +58,26 @@ function turnStart(messages) { // trailing spaces and tabs on a line go. Words are untouched. // --------------------------------------------------------------------------- +// Trailing spaces and tabs go before every line end that /[ \t]+$/gm sees: +// \n, \r, U+2028, U+2029 and the end of the text. One pass, so a long run of +// spaces costs its length, not its square. +function trimLineEnds(text) { + let out = "", start = 0 + for (let i = 0; i <= text.length; i++) { + const c = i < text.length ? text.charCodeAt(i) : -1 + if (c === -1 || c === 10 || c === 13 || c === 0x2028 || c === 0x2029) { + let end = i + while (end > start && (text.charCodeAt(end - 1) === 32 || text.charCodeAt(end - 1) === 9)) end-- + out += text.slice(start, end) + if (c !== -1) out += text[i] + start = i + 1 + } + } + return out +} + function foldText(text) { - return text.replace(/\n{3,}/g, "\n\n").replace(/[ \t]+$/gm, "") + return trimLineEnds(text.replace(/\n{3,}/g, "\n\n")) } function foldWhitespace(messages, preserveSystemPrompt) { diff --git a/packages/history-trim/package.json b/packages/history-trim/package.json index 5ae0c22..abe034c 100644 --- a/packages/history-trim/package.json +++ b/packages/history-trim/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/middleware-history-trim", - "version": "0.1.1", + "version": "0.1.2", "description": "Send a request's history smaller: blank-line runs folded, a system prompt written twice and a message repeated right after itself dropped, a past tool result longer than maxToolLength cut, as magpie gateway middleware", "type": "module", "files": ["history-trim.middleware.js", "README.md", "LICENSE", "PROVENANCE.json"],