Skip to content

Commit 218fd13

Browse files
committed
fix(gateway): cap session preview budgets
1 parent bd69181 commit 218fd13

3 files changed

Lines changed: 29 additions & 15 deletions

File tree

‎src/gateway/server.sessions.preview-resolve.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ test("sessions.preview returns transcript previews", async () => {
7777
]);
7878
});
7979

80-
test("sessions.preview honors a maxChars request above the default", async () => {
80+
test("sessions.preview honors maxChars up to the shared cap", async () => {
8181
const { storePath } = await createSessionStoreDir();
8282
const sessionId = "sess-preview-explicit-budget";
8383
const maxChars = 800;
@@ -102,6 +102,15 @@ test("sessions.preview honors a maxChars request above the default", async () =>
102102
expect(preview.payload?.previews[0]?.items).toEqual([
103103
{ role: "assistant", text: `${"a".repeat(maxChars - 3)}...` },
104104
]);
105+
106+
const capped = await directSessionReq<{
107+
previews: Array<{ items: Array<{ role: string; text: string }> }>;
108+
}>("sessions.preview", { keys: ["main"], limit: 1, maxChars: Number.MAX_SAFE_INTEGER });
109+
110+
expect(capped.ok).toBe(true);
111+
expect(capped.payload?.previews[0]?.items).toEqual([
112+
{ role: "assistant", text: `${"a".repeat(maxChars - 3)}...` },
113+
]);
105114
});
106115

107116
test("sessions.resolve by sessionId ignores fuzzy-search list limits and returns the exact match", async () => {

‎src/gateway/session-display-projection.test.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test } from "vitest";
22
import { projectSessionDisplayMessage } from "./session-display-projection.js";
33

4-
const SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS = 240;
4+
const SESSION_LAST_MESSAGE_PREVIEW_DEFAULT_CHARS = 240;
55

66
describe("projectSessionDisplayMessage", () => {
77
test("keeps visible user and assistant text while excluding non-display rows", () => {
@@ -37,26 +37,27 @@ describe("projectSessionDisplayMessage", () => {
3737
});
3838

3939
test("bounds previews without splitting surrogate pairs", () => {
40-
const longReply = `${"a".repeat(SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS - 2)}😊tail`;
40+
const longReply = `${"a".repeat(SESSION_LAST_MESSAGE_PREVIEW_DEFAULT_CHARS - 2)}😊tail`;
4141
const preview = projectSessionDisplayMessage({ role: "assistant", content: longReply });
4242

43-
expect(preview?.text).toHaveLength(SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS);
44-
expect(preview?.text).toBe(`${"a".repeat(SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS - 3)}...`);
43+
expect(preview?.text).toHaveLength(SESSION_LAST_MESSAGE_PREVIEW_DEFAULT_CHARS);
44+
expect(preview?.text).toBe(`${"a".repeat(SESSION_LAST_MESSAGE_PREVIEW_DEFAULT_CHARS - 3)}...`);
4545
});
4646

47-
test("honors an explicit preview budget above the default", () => {
47+
test("honors explicit preview budgets up to the shared cap", () => {
4848
const maxChars = 800;
49-
const preview = projectSessionDisplayMessage(
50-
{ role: "assistant", content: "a".repeat(maxChars + 20) },
51-
{ maxChars },
52-
);
49+
const message = { role: "assistant", content: "a".repeat(maxChars + 20) };
50+
const preview = projectSessionDisplayMessage(message, { maxChars });
5351

5452
expect(preview?.text).toHaveLength(maxChars);
5553
expect(preview?.text).toBe(`${"a".repeat(maxChars - 3)}...`);
54+
expect(projectSessionDisplayMessage(message, { maxChars: Number.MAX_SAFE_INTEGER })?.text).toBe(
55+
`${"a".repeat(maxChars - 3)}...`,
56+
);
5657
});
5758

5859
test("flattens Markdown before bounding a preview", () => {
59-
const longUrl = `https://example.com/${"x".repeat(SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS)}`;
60+
const longUrl = `https://example.com/${"x".repeat(SESSION_LAST_MESSAGE_PREVIEW_DEFAULT_CHARS)}`;
6061
const preview = projectSessionDisplayMessage(
6162
{ role: "assistant", content: `Read the [deployment guide](${longUrl})` },
6263
{ flattenMarkdown: true },

‎src/gateway/session-display-projection.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { extractAssistantPhaseText } from "../shared/chat-message-content.js";
55
import { stripEnvelope } from "./chat-sanitize.js";
66
import { isSuppressedControlReplyText } from "./control-reply-text.js";
77

8-
const SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS = 240;
8+
const SESSION_LAST_MESSAGE_PREVIEW_DEFAULT_CHARS = 240;
9+
const SESSION_DISPLAY_PROJECTION_MAX_CHARS = 800;
910

1011
type SessionDisplayProjection = {
1112
role: "user" | "assistant";
@@ -39,7 +40,7 @@ function extractUserText(message: Record<string, unknown>): string | undefined {
3940
return typeof message.text === "string" ? message.text : undefined;
4041
}
4142

42-
/** Projects one transcript row onto visible text; explicit callers own their budget. */
43+
/** Projects one transcript row onto visible text within the shared display budget. */
4344
export function projectSessionDisplayMessage(
4445
message: unknown,
4546
options: SessionDisplayProjectionOptions = {},
@@ -67,8 +68,11 @@ export function projectSessionDisplayMessage(
6768
if (!text) {
6869
return null;
6970
}
70-
const requestedMaxChars = options.maxChars ?? SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS;
71-
const limit = Math.max(20, Math.floor(requestedMaxChars));
71+
const requestedMaxChars = options.maxChars ?? SESSION_LAST_MESSAGE_PREVIEW_DEFAULT_CHARS;
72+
const limit = Math.min(
73+
SESSION_DISPLAY_PROJECTION_MAX_CHARS,
74+
Math.max(20, Math.floor(requestedMaxChars)),
75+
);
7276
return {
7377
role,
7478
text: text.length <= limit ? text : `${truncateUtf16Safe(text, limit - 3)}...`,

0 commit comments

Comments
 (0)