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.
This commit is contained in:
Mañjūṣaka
2026-11-11 15:37:29 +08:00
committed by GitHub
parent 1e060e6163
commit fcde82d39f
3 changed files with 60 additions and 2 deletions
+40
View File
@@ -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" }]
}
}
]
@@ -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) {
+1 -1
View File
@@ -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"],