fix: walk back to UTF-8 boundary on prompt truncation (Greptile P2)

Plain Buffer.subarray at MAX_USER_PROMPT_BYTES can land mid-codepoint,
which the utf8 decoder silently rewrites to U+FFFD. Walk back over any
continuation bytes (0b10xxxxxx) before decoding so the truncated prompt
ends on a valid sequence boundary instead of a replacement character.
This commit is contained in:
Alex Newman
2026-04-24 19:32:10 -07:00
parent 94a999dd08
commit 24dfa82194
2 changed files with 5 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@@ -944,7 +944,10 @@ export class SessionRoutes extends BaseRouteHandler {
maxBytes: MAX_USER_PROMPT_BYTES,
preview: prompt.slice(0, 200)
});
prompt = Buffer.from(prompt).subarray(0, MAX_USER_PROMPT_BYTES).toString('utf8');
const buf = Buffer.from(prompt, 'utf8');
let end = MAX_USER_PROMPT_BYTES;
while (end > 0 && (buf[end] & 0xc0) === 0x80) end--;
prompt = buf.subarray(0, end).toString('utf8');
}
logger.info('HTTP', 'SessionRoutes: handleSessionInitByClaudeId called', {