| fix(preflight): also gate on JSON body bytes, not just tokens (#1451)
A user reported a cryptic `DeepSeek 400: messages[156].content: unexpected
end of hex escape at line 1 column 884819` mid-session. Reproducing against
real session logs: Node's JSON.stringify can't emit a malformed \u
escape, and no lone surrogates exist in any of 10 saved sessions — the
gateway is truncating the request body mid-escape because it exceeds the
DeepSeek API's body-byte limit, which is independent of the 1 M-token
context window the existing preflight checks.
Token preflight stays at 95% of ctxMax. New byte preflight trips when the
stringified messages array exceeds MAX_BODY_BYTES (700 KB, ~80% of the
observed 884 KB failure point). Either signal triggers the existing
mechanicalTruncate path; the truncate now bounds output by tokens AND
bytes so byte-triggered folds actually shrink the body.
The trigger field on PreflightDecision records which signal fired so
follow-up diagnostics (and the user-facing warning, now showing body KB)
can distinguish the two paths.
Two tests/loop.test.ts fold cases used 2 MB seed logs and a 1 M-token
ctxMax to verify fold tier behavior. With the new 700 KB byte gate the
preflight emptied those logs before fold could run; switched both to a
100 K test ctx + proportionally scaled seed sizes so they exercise fold
tiers within both windows.
Two reusable tools added under tools/ (analyze-session-body.mjs,
scan-all-sessions.mjs) so future "DeepSeek 400 from a long session"
reports can be diagnosed from the user's local session files without
guessing.
Co-authored-by: reasonix <reasonix@deepseek.com> | 8 天前 |