Skip to content

Commit c3678db

Browse files
fix(server): a restart prompt keeps a pointer to unsent summaries when lines overflow
At most three not-shown lines, nearest the present, give way from the far end until the block fits; when none fits, one line names t3_conversation_summary_list for the thread. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e150101 commit c3678db

1 file changed

Lines changed: 45 additions & 15 deletions

File tree

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

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,45 @@ function handoffReceiptLine(input: {
5353
}
5454

5555
const STUBBED_ITEMS = 5;
56+
const NOT_SHOWN_LINES = 3;
57+
58+
const summaryCount = (count: number, adjective: string) =>
59+
`${count} ${adjective} ${count === 1 ? "summary holds" : "summaries hold"} omitted items`;
60+
const listCall = (offer: HandoffSummary) =>
61+
`t3_conversation_summary_list({projectId:"${offer.projectId}",threadId:"${offer.threadId}"})`;
5662

5763
/**
58-
* Where to read omitted items held by a summary that did not fit. A delivered summary needs no
59-
* line: its header already names its expand call. Agents are not told their project id.
64+
* Where to read omitted items held by summaries that did not fit, nearest the present first. A
65+
* delivered summary needs no line: its header already names its expand call. Agents are not told
66+
* their project id. Lines give way from the far end; when none fits, one line names the list call.
6067
*/
68+
function notShownBlock(input: {
69+
readonly offers: ReadonlyArray<HandoffSummary>;
70+
readonly omitted: ReadonlySet<string>;
71+
readonly messages: ReadonlyArray<OrchestrationV2HistoricalMessage>;
72+
readonly fits: (block: string) => boolean;
73+
}): string {
74+
const lines = notShownLines(input);
75+
if (lines.length === 0) return "";
76+
for (let shown = Math.min(lines.length, NOT_SHOWN_LINES); shown > 0; shown--) {
77+
const rest = lines.slice(shown);
78+
const block = [
79+
...lines.slice(0, shown).map(({ line }) => line),
80+
...(rest.length === 0
81+
? []
82+
: [`${summaryCount(rest.length, "more")}; list them with ${listCall(rest[0]!.offer)}.`]),
83+
].join("\n");
84+
if (input.fits(block)) return block;
85+
}
86+
const fallback = `Not shown here: ${summaryCount(lines.length, "conversation")}; list them with ${listCall(lines[0]!.offer)}.`;
87+
return input.fits(fallback) ? fallback : "";
88+
}
89+
6190
function notShownLines(input: {
6291
readonly offers: ReadonlyArray<HandoffSummary>;
6392
readonly omitted: ReadonlySet<string>;
6493
readonly messages: ReadonlyArray<OrchestrationV2HistoricalMessage>;
65-
}): ReadonlyArray<string> {
94+
}): ReadonlyArray<{ readonly offer: HandoffSummary; readonly line: string }> {
6695
const byId = new Map(input.messages.map((message) => [message.itemId, message]));
6796
return input.offers.flatMap((offer) => {
6897
const covered = offer.coveredItemIds.filter((id) => input.omitted.has(id));
@@ -77,7 +106,10 @@ function notShownLines(input: {
77106
const more = covered.length - stubs.length;
78107
const ids = offer.summaryIds ?? [offer.id];
79108
return [
80-
`Not shown here: ${[...stubs, ...(more > 0 ? [`${more} ${stubs.length > 0 ? "more " : ""}omitted item${more === 1 ? "" : "s"}`] : [])].join(", ")}. ${ids.join(", ")} ${ids.length === 1 ? "holds" : "hold"} the original text; read it with t3_conversation_summary_expand({projectId:"${offer.projectId}",threadId:"${offer.threadId}",summaryId:"${ids[0]}"})${ids.length === 1 ? "" : " and the other ids"}.`,
109+
{
110+
offer,
111+
line: `Not shown here: ${[...stubs, ...(more > 0 ? [`${more} ${stubs.length > 0 ? "more " : ""}omitted item${more === 1 ? "" : "s"}`] : [])].join(", ")}. ${ids.join(", ")} ${ids.length === 1 ? "holds" : "hold"} the original text; read it with t3_conversation_summary_expand({projectId:"${offer.projectId}",threadId:"${offer.threadId}",summaryId:"${ids[0]}"})${ids.length === 1 ? "" : " and the other ids"}.`,
112+
},
81113
];
82114
});
83115
}
@@ -208,7 +240,7 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH
208240
});
209241
const summarized = new Set<TurnItemId>();
210242
const chosen: Array<HandoffSummary> = [];
211-
let offers: ReadonlyArray<HandoffSummary> = [];
243+
let offered: ReadonlyArray<HandoffSummary> = [];
212244
const priorOmitted = new Set<string>(
213245
pending.flatMap((handoff) => handoff.history?.omittedItemIds ?? []),
214246
);
@@ -230,7 +262,8 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH
230262
);
231263
// Most covered items per byte first, so coarse summaries of old history come before the
232264
// finer ones next to the tail. Ties keep the offered order, nearest the present first.
233-
offers = (yield* input.summaries(eligible)).toSorted(
265+
offered = yield* input.summaries(eligible);
266+
const offers = offered.toSorted(
234267
(a, b) =>
235268
b.coveredItemIds.length / Buffer.byteLength(b.text) -
236269
a.coveredItemIds.length / Buffer.byteLength(a.text),
@@ -307,7 +340,7 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH
307340
.map((message) => message.itemId),
308341
]);
309342
const omittedHaveSummaries = (ids: ReadonlySet<string>) =>
310-
offers.some((offer) => offer.coveredItemIds.some((id) => ids.has(id)));
343+
offered.some((offer) => offer.coveredItemIds.some((id) => ids.has(id)));
311344
const receiptFor = (handoff: OrchestrationV2ContextHandoff) =>
312345
withHandoffReceipt(
313346
handoff.summaryText,
@@ -316,16 +349,13 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH
316349
omittedHaveSummaries: omittedHaveSummaries(stillOmitted(handoff)),
317350
}),
318351
);
319-
const notShown = notShownLines({
320-
offers: offers.filter((offer) => !chosen.includes(offer)),
352+
const notShown = notShownBlock({
353+
offers: offered.filter((offer) => !chosen.includes(offer)),
321354
omitted: new Set(pending.flatMap((handoff) => [...stillOmitted(handoff)])),
322355
messages,
323-
}).join("\n");
324-
const context =
325-
notShown !== "" &&
326-
historyCost(selected.messages, `${selected.context}\n${notShown}`) <= budget
327-
? `${selected.context}\n${notShown}`
328-
: selected.context;
356+
fits: (block) => historyCost(selected.messages, `${selected.context}\n${block}`) <= budget,
357+
});
358+
const context = notShown === "" ? selected.context : `${selected.context}\n${notShown}`;
329359
const estimatedTokens = historyCost(selected.messages, context);
330360
if (estimatedTokens > budget) {
331361
if (input.deferInline) return empty;

0 commit comments

Comments
 (0)