Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion apps/web/src/lib/composerContextRecords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe("composerContextRecords", () => {
expect(removeInlineContextReference(prompt, reference.contextId).prompt).toBe("prose tail");
});

it("clamps an oversized review selection so the record still encodes", () => {
it("clamps oversized review and terminal selections so the records still encode", () => {
const build = (diffLength: number) =>
reviewCommentContextRecord({
id: "rc-big",
Expand All @@ -276,6 +276,21 @@ describe("composerContextRecords", () => {
expect(overLimit.diff.endsWith("… truncated …")).toBe(true);
expect(() => decodeMessageContext({ version: 1, records: [atLimit] })).not.toThrow();
expect(() => decodeMessageContext({ version: 1, records: [overLimit] })).not.toThrow();

const terminal = terminalContextRecord({
id: "term-big",
threadId: ThreadId.make("t"),
createdAt: "2026-01-01T00:00:00.000Z",
terminalId: "default",
terminalLabel: "Terminal 1",
lineStart: 1,
lineEnd: 2_000,
text: `${"t".repeat(63_985)}${"😀".repeat(8)}`,
});
expect(terminal.text).toBe(`${"t".repeat(63_985)}\n… truncated …`);
expect(decodeMessageContext({ version: 1, records: [terminal] }).records).toMatchObject([
{ kind: "terminal", text: terminal.text },
]);
});

it("finds the destination collision for different legacy terminal messages", () => {
Expand Down
12 changes: 10 additions & 2 deletions apps/web/src/lib/composerContextRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import {
COMPOSER_CONTEXT_REVIEW_DIFF_MAX_CHARS,
COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS,
COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS,
} from "@t3tools/contracts";
import type {
ComposerContextId,
Expand Down Expand Up @@ -52,7 +53,11 @@ const TRUNCATION_MARKER = "\n… truncated …";

function clampContextText(value: string, max: number): string {
if (value.length <= max) return value;
return `${value.slice(0, Math.max(0, max - TRUNCATION_MARKER.length))}${TRUNCATION_MARKER}`;
let end = Math.max(0, max - TRUNCATION_MARKER.length);
// Never end on a lone high surrogate.
const last = value.charCodeAt(end - 1);
if (last >= 0xd800 && last <= 0xdbff) end -= 1;
return `${value.slice(0, end)}${TRUNCATION_MARKER}`;
}

type ReviewCommentPresentation = ReviewCommentContext | ReviewCommentContextRecord;
Expand Down Expand Up @@ -187,7 +192,10 @@ export function terminalContextRecord(context: TerminalContextDraft): TerminalCo
terminalLabel: context.terminalLabel,
lineStart: context.lineStart,
lineEnd: context.lineEnd,
text: normalizeTerminalContextText(context.text),
text: clampContextText(
normalizeTerminalContextText(context.text),
COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS,
),
};
}

Expand Down
Loading