Skip to content

Commit 13c3c2e

Browse files
authored
perf(server): stop summarizing side chat context at its cap (#28266)
1 parent 6d25fa1 commit 13c3c2e

2 files changed

Lines changed: 58 additions & 33 deletions

File tree

‎apps/server/src/orchestration-v2/ContextHandoffService.test.ts‎

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assert, it } from "@effect/vitest";
22
import {
3+
ContextTransferId,
34
MessageId,
45
ProjectId,
56
ProviderInstanceId,
@@ -160,34 +161,56 @@ it.layer(layerTest)("ContextHandoffService legacy import", (it) => {
160161
);
161162
});
162163

163-
it("caps the side chat context block, keeping the newest parent items", () => {
164-
const parentThreadId = ThreadId.make("thread:side-chat-parent");
165-
const digest = Array.from({ length: 60 }, (_, index) =>
166-
ContextHandoffService.summarizeDeltaItem(
167-
importedItem({
164+
it.effect("caps the side chat context block without summarizing discarded parent history", () =>
165+
Effect.gen(function* () {
166+
const parentThreadId = ThreadId.make("thread:side-chat-parent");
167+
let summarizedItems = 0;
168+
const parentItems = Array.from({ length: 60 }, (_, index) => ({
169+
...importedItem({
168170
role: index % 2 === 0 ? "user" : "assistant",
169171
id: `parent-${index}`,
170172
text: `item-${index} ${"x".repeat(300)}`,
171173
ordinal: index,
172174
}),
173-
)!,
174-
);
175-
const context = ContextHandoffService.makeSideChatContext({ parentThreadId, digest });
175+
get text() {
176+
summarizedItems++;
177+
return `item-${index} ${"x".repeat(300)}`;
178+
},
179+
}));
180+
const service = yield* ContextHandoffService.ContextHandoffServiceV2;
181+
const handoff = yield* service.prepareSideChatContext({
182+
parentThreadId,
183+
threadId: ThreadId.make("thread:side-chat"),
184+
targetRunId: RunId.make("run:side-chat"),
185+
transferId: ContextTransferId.make("transfer:side-chat"),
186+
toProviderThreadId: ProviderThreadId.make("provider-thread:side-chat"),
187+
fromProviderInstanceId: ProviderInstanceId.make("codex"),
188+
toProviderInstanceId: ProviderInstanceId.make("codex"),
189+
coveredRunOrdinals: { from: 1, to: 1 },
190+
parentItems,
191+
createdAt: DateTime.makeUnsafe(0),
192+
});
193+
const context = handoff.summaryText;
194+
assert.isBelow(summarizedItems, parentItems.length);
176195

177-
assert.include(context, "- Earlier items truncated.");
178-
assert.notInclude(context, "item-0 ");
179-
assert.include(context, "- Assistant: item-59 ");
180-
assert.isAtMost(context.length, 8_000);
181-
assert.isTrue(
182-
context.endsWith(
183-
`Use t3_thread_read on thread ${parentThreadId} to see the main thread's live state.`,
184-
),
185-
);
196+
assert.include(context, "- Earlier items truncated.");
197+
assert.notInclude(context, "item-0 ");
198+
assert.include(context, "- Assistant: item-59 ");
199+
assert.isAtMost(context.length, 8_000);
200+
assert.isTrue(
201+
context.endsWith(
202+
`Use t3_thread_read on thread ${parentThreadId} to see the main thread's live state.`,
203+
),
204+
);
186205

187-
const framingOnly = ContextHandoffService.makeSideChatContext({ parentThreadId, digest: [] });
188-
assert.isTrue(framingOnly.startsWith("You are in a side chat beside the main thread."));
189-
assert.notInclude(framingOnly, "Main thread activity");
190-
});
206+
const framingOnly = ContextHandoffService.makeSideChatContext({
207+
parentThreadId,
208+
digest: [],
209+
});
210+
assert.isTrue(framingOnly.startsWith("You are in a side chat beside the main thread."));
211+
assert.notInclude(framingOnly, "Main thread activity");
212+
}).pipe(Effect.provide(layerTest)),
213+
);
191214

192215
it("covers the run range of a very long delta", () => {
193216
const runs = [

‎apps/server/src/orchestration-v2/ContextHandoffService.ts‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export const readForkHistoryItems = Effect.fn(
202202
];
203203
});
204204

205-
export function summarizeDeltaItem(item: OrchestrationV2TurnItem): string | null {
205+
function summarizeDeltaItem(item: OrchestrationV2TurnItem): string | null {
206206
switch (item.type) {
207207
case "user_message":
208208
return `- User: ${compactText(item.text)}`;
@@ -244,30 +244,35 @@ function makeForkDeltaSummary(input: {
244244

245245
const SIDE_CHAT_CONTEXT_MAX_CHARS = 8_000;
246246

247-
/** `digest` holds `summarizeDeltaItem` lines for parent items after the fork point, oldest first. */
247+
/** Oldest first; raw parent items are summarized only until the context cap is reached. */
248248
export function makeSideChatContext(input: {
249249
readonly parentThreadId: ThreadId;
250-
readonly digest: ReadonlyArray<string>;
250+
readonly digest: ReadonlyArray<string | OrchestrationV2TurnItem>;
251251
}): string {
252252
const framing =
253253
"You are in a side chat beside the main thread. The main thread may still be running in the same worktree. Answer the user's questions; do not continue or redo the main thread's task.";
254254
const header = "Main thread activity since this side chat's starting point:";
255255
const truncated = "- Earlier items truncated.";
256256
const footer = `Use t3_thread_read on thread ${input.parentThreadId} to see the main thread's live state.`;
257257
const kept: Array<string> = [];
258+
let omitted = false;
258259
// The cap covers the whole block, so the fixed lines are reserved before any digest line.
259260
let remaining =
260261
SIDE_CHAT_CONTEXT_MAX_CHARS - [framing, "", header, truncated, "", footer].join("\n").length;
261-
for (const line of input.digest.toReversed()) {
262-
if (line.length + 1 > remaining) break;
262+
for (let index = input.digest.length - 1; index >= 0; index--) {
263+
const item = input.digest[index]!;
264+
const line = typeof item === "string" ? item : summarizeDeltaItem(item);
265+
if (line === null) continue;
266+
if (line.length + 1 > remaining) {
267+
omitted = true;
268+
break;
269+
}
263270
kept.unshift(line);
264271
remaining -= line.length + 1;
265272
}
266273
return [
267274
framing,
268-
...(kept.length === 0
269-
? []
270-
: ["", header, ...(kept.length < input.digest.length ? [truncated] : []), ...kept]),
275+
...(kept.length === 0 ? [] : ["", header, ...(omitted ? [truncated] : []), ...kept]),
271276
"",
272277
footer,
273278
].join("\n");
@@ -588,10 +593,7 @@ const makeContextHandoffService = Effect.fn("orchestrationV2.ContextHandoffServi
588593
summaryMessageId: null,
589594
summaryText: makeSideChatContext({
590595
parentThreadId: input.parentThreadId,
591-
digest: input.parentItems.flatMap((item) => {
592-
const line = summarizeDeltaItem(item);
593-
return line === null ? [] : [line];
594-
}),
596+
digest: input.parentItems,
595597
}),
596598
createdByProviderInstanceId: null,
597599
createdAt: input.createdAt,

0 commit comments

Comments
 (0)