Skip to content

Commit 33abd62

Browse files
authored
fix(gateway): honor requested session preview length (#125201)
* fix(gateway): honor session preview length * fix(gateway): cap session preview budgets
1 parent 733d8f9 commit 33abd62

3 files changed

Lines changed: 59 additions & 9 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,42 @@ test("sessions.preview returns transcript previews", async () => {
7777
]);
7878
});
7979

80+
test("sessions.preview honors maxChars up to the shared cap", async () => {
81+
const { storePath } = await createSessionStoreDir();
82+
const sessionId = "sess-preview-explicit-budget";
83+
const maxChars = 800;
84+
85+
await writeSessionStore({
86+
entries: {
87+
"agent:main:main": sessionStoreEntry(sessionId),
88+
},
89+
});
90+
await seedSessionTranscript({
91+
sessionId,
92+
sessionKey: "agent:main:main",
93+
storePath,
94+
messages: [{ role: "assistant", content: "a".repeat(maxChars + 20) }],
95+
});
96+
97+
const preview = await directSessionReq<{
98+
previews: Array<{ items: Array<{ role: string; text: string }> }>;
99+
}>("sessions.preview", { keys: ["main"], limit: 1, maxChars });
100+
101+
expect(preview.ok).toBe(true);
102+
expect(preview.payload?.previews[0]?.items).toEqual([
103+
{ role: "assistant", text: `${"a".repeat(maxChars - 3)}...` },
104+
]);
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+
]);
114+
});
115+
80116
test("sessions.resolve by sessionId ignores fuzzy-search list limits and returns the exact match", async () => {
81117
await createSessionStoreDir();
82118
const now = Date.now();

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

Lines changed: 17 additions & 5 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,15 +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)}...`);
45+
});
46+
47+
test("honors explicit preview budgets up to the shared cap", () => {
48+
const maxChars = 800;
49+
const message = { role: "assistant", content: "a".repeat(maxChars + 20) };
50+
const preview = projectSessionDisplayMessage(message, { maxChars });
51+
52+
expect(preview?.text).toHaveLength(maxChars);
53+
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+
);
4557
});
4658

4759
test("flattens Markdown before bounding a preview", () => {
48-
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)}`;
4961
const preview = projectSessionDisplayMessage(
5062
{ role: "assistant", content: `Read the [deployment guide](${longUrl})` },
5163
{ flattenMarkdown: true },

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

Lines changed: 6 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 the bounded text shared by session-list consumers. */
43+
/** Projects one transcript row onto visible text within the shared display budget. */
4344
export function projectSessionDisplayMessage(
4445
message: unknown,
4546
options: SessionDisplayProjectionOptions = {},
@@ -67,9 +68,10 @@ export function projectSessionDisplayMessage(
6768
if (!text) {
6869
return null;
6970
}
71+
const requestedMaxChars = options.maxChars ?? SESSION_LAST_MESSAGE_PREVIEW_DEFAULT_CHARS;
7072
const limit = Math.min(
71-
SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS,
72-
Math.max(20, Math.floor(options.maxChars ?? SESSION_LAST_MESSAGE_PREVIEW_MAX_CHARS)),
73+
SESSION_DISPLAY_PROJECTION_MAX_CHARS,
74+
Math.max(20, Math.floor(requestedMaxChars)),
7375
);
7476
return {
7577
role,

0 commit comments

Comments
 (0)