Skip to content

Commit 977c0ab

Browse files
saphidclaude
authored andcommitted
fix(web): big terminal selections no longer break sending
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f6503f0 commit 977c0ab

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎apps/web/src/lib/composerContextRecords.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ describe("composerContextRecords", () => {
255255
expect(removeInlineContextReference(prompt, reference.contextId).prompt).toBe("prose tail");
256256
});
257257

258-
it("clamps an oversized review selection so the record still encodes", () => {
258+
it("clamps oversized review and terminal selections so the records still encode", () => {
259259
const build = (diffLength: number) =>
260260
reviewCommentContextRecord({
261261
id: "rc-big",
@@ -276,6 +276,19 @@ describe("composerContextRecords", () => {
276276
expect(overLimit.diff.endsWith("… truncated …")).toBe(true);
277277
expect(() => decodeMessageContext({ version: 1, records: [atLimit] })).not.toThrow();
278278
expect(() => decodeMessageContext({ version: 1, records: [overLimit] })).not.toThrow();
279+
280+
const terminal = terminalContextRecord({
281+
id: "term-big",
282+
threadId: ThreadId.make("t"),
283+
createdAt: "2026-01-01T00:00:00.000Z",
284+
terminalId: "default",
285+
terminalLabel: "Terminal 1",
286+
lineStart: 1,
287+
lineEnd: 2_000,
288+
text: `${"t".repeat(63_985)}${"😀".repeat(8)}`,
289+
});
290+
expect(terminal.text).toBe(`${"t".repeat(63_985)}\n… truncated …`);
291+
expect(() => decodeMessageContext({ version: 1, records: [terminal] })).not.toThrow();
279292
});
280293

281294
it("finds the destination collision for different legacy terminal messages", () => {

‎apps/web/src/lib/composerContextRecords.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
import {
66
COMPOSER_CONTEXT_REVIEW_DIFF_MAX_CHARS,
77
COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS,
8+
COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS,
89
} from "@t3tools/contracts";
910
import type {
1011
ComposerContextId,
@@ -52,7 +53,11 @@ const TRUNCATION_MARKER = "\n… truncated …";
5253

5354
function clampContextText(value: string, max: number): string {
5455
if (value.length <= max) return value;
55-
return `${value.slice(0, Math.max(0, max - TRUNCATION_MARKER.length))}${TRUNCATION_MARKER}`;
56+
let end = Math.max(0, max - TRUNCATION_MARKER.length);
57+
// Never end on a lone high surrogate.
58+
const last = value.charCodeAt(end - 1);
59+
if (last >= 0xd800 && last <= 0xdbff) end -= 1;
60+
return `${value.slice(0, end)}${TRUNCATION_MARKER}`;
5661
}
5762

5863
type ReviewCommentPresentation = ReviewCommentContext | ReviewCommentContextRecord;
@@ -187,7 +192,10 @@ export function terminalContextRecord(context: TerminalContextDraft): TerminalCo
187192
terminalLabel: context.terminalLabel,
188193
lineStart: context.lineStart,
189194
lineEnd: context.lineEnd,
190-
text: normalizeTerminalContextText(context.text),
195+
text: clampContextText(
196+
normalizeTerminalContextText(context.text),
197+
COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS,
198+
),
191199
};
192200
}
193201

0 commit comments

Comments
 (0)