The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 08:15 UTC
perf(server): stop summarizing side chat context at its cap (#28266)
Yuruzuu committed
Nov 1, 2026 at 10:49 UTC
13c3c2e43b6b98929812ddd559c0f2194482f321
2 files changed
+58
-33
apps/server/src/orchestration-v2/ContextHandoffService.test.ts
+44
-21
@@ -1,5 +1,6 @@
1
import { assert, it } from "@effect/vitest";
2
import {
3
+ ContextTransferId,
4
MessageId,
5
ProjectId,
6
ProviderInstanceId,
@@ -160,34 +161,56 @@ it.layer(layerTest)("ContextHandoffService legacy import", (it) => {
161
);
162
});
163
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({
170
role: index % 2 === 0 ? "user" : "assistant",
171
id: `parent-${index}`,
172
text: `item-${index} ${"x".repeat(300)}`,
173
ordinal: index,
174
}),
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);
195
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
+ );
205
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
+);
214
215
it("covers the run range of a very long delta", () => {
216
const runs = [
apps/server/src/orchestration-v2/ContextHandoffService.ts
+14
-12
@@ -202,7 +202,7 @@ export const readForkHistoryItems = Effect.fn(
202
];
203
});
204
205
-export function summarizeDeltaItem(item: OrchestrationV2TurnItem): string | null {
205
+function summarizeDeltaItem(item: OrchestrationV2TurnItem): string | null {
206
switch (item.type) {
207
case "user_message":
208
return `- User: ${compactText(item.text)}`;
@@ -244,10 +244,10 @@ function makeForkDeltaSummary(input: {
244
245
const SIDE_CHAT_CONTEXT_MAX_CHARS = 8_000;
246
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. */
248
export function makeSideChatContext(input: {
249
readonly parentThreadId: ThreadId;
250
- readonly digest: ReadonlyArray<string>;
250
+ readonly digest: ReadonlyArray<string | OrchestrationV2TurnItem>;
251
}): string {
252
const framing =
253
"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.";
@@ -255,19 +255,24 @@ export function makeSideChatContext(input: {
255
const truncated = "- Earlier items truncated.";
256
const footer = `Use t3_thread_read on thread ${input.parentThreadId} to see the main thread's live state.`;
257
const kept: Array<string> = [];
258
+ let omitted = false;
259
// The cap covers the whole block, so the fixed lines are reserved before any digest line.
260
let remaining =
261
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
+ }
270
kept.unshift(line);
271
remaining -= line.length + 1;
272
}
273
return [
274
framing,
268
- ...(kept.length === 0
269
- ? []
270
- : ["", header, ...(kept.length < input.digest.length ? [truncated] : []), ...kept]),
275
+ ...(kept.length === 0 ? [] : ["", header, ...(omitted ? [truncated] : []), ...kept]),
276
"",
277
footer,
278
].join("\n");
@@ -588,10 +593,7 @@ const makeContextHandoffService = Effect.fn("orchestrationV2.ContextHandoffServi
593
summaryMessageId: null,
594
summaryText: makeSideChatContext({
595
parentThreadId: input.parentThreadId,
591
- digest: input.parentItems.flatMap((item) => {
592
- const line = summarizeDeltaItem(item);
593
- return line === null ? [] : [line];
594
- }),
596
+ digest: input.parentItems,
597
}),
598
createdByProviderInstanceId: null,
599
createdAt: input.createdAt,