Skip to content

Commit c939917

Browse files
committed
fix(history): honor stricter maxTurnItems on human-turn pages
Human-turn windows now use the smaller of maxItems and maxTurnItems, so a caller that sets a stricter maxTurnItems is respected while maxItems remains the upper bound. Adds a regression test for the stricter limit.
1 parent 1dc0468 commit c939917

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,31 @@ describe("threadHistoryPaging", () => {
201201
);
202202
});
203203

204+
it("honors a stricter maxTurnItems on human-turn windows", () => {
205+
const items = Array.from({ length: 60 }, (_, index) => makeRow(index));
206+
items[0] = {
207+
...items[0]!,
208+
item: {
209+
...items[0]!.item,
210+
type: "user_message",
211+
createdBy: "user",
212+
creationSource: "web",
213+
inputIntent: "turn_start",
214+
messageId: MessageId.make("stricter-turn-limit"),
215+
text: "Continue",
216+
attachments: [],
217+
},
218+
} as OrchestrationV2ProjectedTurnItem;
219+
220+
const recent = selectRecentTimelineWindow({
221+
items,
222+
snapshotSequence: 1,
223+
policy: { ...THREAD_HISTORY_PAGE_POLICY, maxItems: 100, maxTurnItems: 20 },
224+
});
225+
226+
expect(recent.items).toHaveLength(20);
227+
});
228+
204229
it("keeps background turns with user turns, with main's 150-turn fan-out ceiling", () => {
205230
const items = Array.from({ length: 161 }, (_, turn) => {
206231
const row = makeRow(turn * 2);
@@ -271,7 +296,12 @@ describe("threadHistoryPaging", () => {
271296
inRun(run * 4 + 2, run, notification(`Steer ${run}`)),
272297
inRun(run * 4 + 3, run),
273298
]).flat();
274-
const policy = { ...THREAD_HISTORY_PAGE_POLICY, maxItems: 1_000, maxEncodedBytes: 10_000_000 };
299+
const policy = {
300+
...THREAD_HISTORY_PAGE_POLICY,
301+
maxItems: 1_000,
302+
maxTurnItems: 1_000,
303+
maxEncodedBytes: 10_000_000,
304+
};
275305
const first = selectRecentTimelineWindow({ items, snapshotSequence: 1, policy });
276306
expect(first.items).toHaveLength(150 * 4 - 1);
277307
expect(first.items[0]?.sourceItemId).toBe(`item-${11 * 4 + 1}`);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,16 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
218218
const turnLimit = candidates.some((row) => isThreadHistoryUserTurn(row.item))
219219
? policy.maxUserTurns
220220
: undefined;
221+
const maxItems =
222+
turnLimit === undefined
223+
? policy.maxItems
224+
: Math.min(policy.maxItems, policy.maxTurnItems ?? policy.maxItems);
221225
for (let index = end - 1; index >= 0; index -= 1) {
222226
const row = input.items[index]!;
223227
const rowBytes = rowCost(row);
224228
if (
225229
selected.length > 0 &&
226-
(selected.length >= policy.maxItems ||
230+
(selected.length >= maxItems ||
227231
encodedBytes + rowBytes > policy.maxEncodedBytes ||
228232
(turnLimit !== undefined &&
229233
(userTurns >= turnLimit || rawTurns >= THREAD_HISTORY_MAX_RAW_TURNS)))
@@ -238,7 +242,7 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
238242
// Inside a turn longer than the window, no selected row starts a user turn.
239243
// Later pages and the snapshot query see only such rows and use `maxItems`,
240244
// so this page uses it too.
241-
if (userTurns === 0) selected.splice(policy.maxItems);
245+
if (userTurns === 0) selected.splice(maxItems);
242246
selected.reverse();
243247

244248
const oldest = selected[0];

0 commit comments

Comments
 (0)