Skip to content

Commit 62270db

Browse files
committed
fix(server): cap the first snapshot when older turns exist
The connect snapshot read every payload in the last user-turn window. A long tool tail held the SQLite lock long enough for the shell request to be cancelled. The first snapshot now stays on the row cap. An older page that already has a cursor still returns its turn window whole.
1 parent 4f2b7f4 commit 62270db

2 files changed

Lines changed: 40 additions & 18 deletions

File tree

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
491491
}),
492492
);
493493

494-
it.effect("pages complete user turns through SQL regardless of tool count or payload size", () =>
494+
it.effect("caps the first snapshot at the row window and pages omitted turns whole", () =>
495495
Effect.gen(function* () {
496496
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
497497
const sql = yield* SqlClient.SqlClient;
@@ -607,34 +607,48 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
607607
),
608608
(parse) => Effect.sync(() => parse.mockRestore()),
609609
);
610-
// Only the selected turn cohort and two lookahead anchors are decoded.
611-
assert.lengthOf(initial.projection.turnItems, 12 * 102);
610+
// The first connect reads the row cap, not every tool row in the turn window.
611+
assert.isTrue(initial.olderHistoryExists);
612+
assert.lengthOf(initial.projection.turnItems, 77);
613+
assert.strictEqual(initial.projection.turnItems.at(-1)?.id, allIds.at(-1));
614+
assert.isFalse(initial.projection.turnItems.some((item) => item.id === allIds[0]));
612615
const bounded = buildBoundedThreadProjection({
613616
projection: initial.projection,
614617
snapshotSequence: 0,
618+
olderHistoryExists: initial.olderHistoryExists,
619+
olderHistoryThreadId: initial.olderHistoryThreadId,
615620
});
616-
assert.lengthOf(bounded.projection.visibleTurnItems, 10 * 102);
617-
assert.strictEqual(bounded.projection.visibleTurnItems[0]?.sourceItemId, allIds[35 * 102]);
621+
assert.isTrue(bounded.hasMoreHistory);
618622
const loaded = bounded.projection.visibleTurnItems.map((row) => String(row.sourceItemId));
619623
let cursor = bounded.historyCursor;
620-
for (const turns of [20, 15]) {
621-
assert.isNotNull(cursor);
622-
const anchor = decodeThreadHistoryCursor(cursor!);
624+
let sawWholeTurnPage = false;
625+
let pages = 1;
626+
while (cursor !== null) {
627+
const anchor = decodeThreadHistoryCursor(cursor);
623628
const snapshot = yield* projectionStore.getThreadSnapshotWindow(threadId, {
624629
rowLimit: 77,
625630
userTurnLimit: 20,
626631
anchorItemId: TurnItemId.make(anchor.si),
627632
anchorThreadId: ThreadId.make(anchor.st),
628633
});
634+
if (snapshot.projection.turnItems.length > 77) sawWholeTurnPage = true;
629635
const page = selectHistoryPageFromCursor({
630636
items: snapshot.projection.visibleTurnItems,
631-
cursor: cursor!,
637+
cursor,
638+
snapshotSequence: 0,
639+
});
640+
const revealed = revealOlderSqlHistory({
641+
page,
642+
olderHistoryExists: snapshot.olderHistoryExists,
643+
olderHistoryThreadId: snapshot.olderHistoryThreadId,
632644
snapshotSequence: 0,
633645
});
634-
assert.lengthOf(page.items, turns * 102);
635646
loaded.unshift(...page.items.map((row) => String(row.sourceItemId)));
636-
cursor = page.nextCursor;
647+
cursor = revealed.nextCursor;
648+
pages += 1;
649+
if (pages > 20) break;
637650
}
651+
assert.isTrue(sawWholeTurnPage);
638652
assert.isNull(cursor);
639653
assert.deepEqual(loaded, allIds);
640654
}),

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,12 +2801,17 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28012801
WHERE ordinal >= (SELECT ordinal FROM boundary)
28022802
ORDER BY ordinal DESC, turn_item_id DESC
28032803
-- Sentinel 0 is one open turn, or no user turn. LIMIT -1
2804-
-- would read every payload in that turn. Cap it at rowLimit.
2805-
-- Any positive boundary is a turn window and stays whole.
2804+
-- would read every payload in that turn. The first snapshot
2805+
-- has the same problem when earlier turns exist: the turn
2806+
-- window can still be tens of megabytes and holds the write
2807+
-- lock long enough for the shell request to be cancelled.
2808+
-- Cap that first read. An older page already has a cursor,
2809+
-- so its turn window stays whole.
28062810
LIMIT CASE
28072811
WHEN ${window.rowLimit} = 0 THEN 0
28082812
WHEN (SELECT ordinal FROM boundary) > 0
2809-
AND (SELECT COUNT(*) FROM user_anchors) > 0 THEN -1
2813+
AND (SELECT COUNT(*) FROM user_anchors) > 0
2814+
AND ${window.anchorItemId ?? null} IS NOT NULL THEN -1
28102815
ELSE ${window.rowLimit}
28112816
END
28122817
), older_history AS (
@@ -6379,17 +6384,20 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
63796384
(index) => index >= rawStart && isThreadHistoryUserTurn(candidates[index]!.item),
63806385
);
63816386
const anchorLimit = (options.userTurnLimit ?? 0) + 2;
6382-
// Match the SQL window. A full user-turn window or a raw-turn cap
6383-
// keeps that span. A shorter multi-turn remainder starts at its
6384-
// oldest user turn. One open turn, or none, keeps the rowLimit tail.
6385-
const start =
6387+
// Match the SQL window. A paged turn window stays whole. The first
6388+
// snapshot keeps the rowLimit tail even when older turns exist.
6389+
const turnStart =
63866390
anchors.length >= anchorLimit
63876391
? anchors.at(-anchorLimit)!
63886392
: anchors.length > 0 && rawStart > 0
63896393
? rawStart
63906394
: anchors.length > 1
63916395
? anchors[0]!
63926396
: Math.max(0, anchorIndex - options.rowLimit);
6397+
const start =
6398+
options.anchorItemId === undefined
6399+
? Math.max(turnStart, Math.max(0, anchorIndex - options.rowLimit))
6400+
: turnStart;
63936401
const visibleTurnItems = candidates.slice(start);
63946402
return {
63956403
...snapshot,

0 commit comments

Comments
 (0)