Skip to content

Commit 7c3b2f1

Browse files
committed
fix(server): keep the snapshot cap on an initial fork read
The row cap treated every anchor as a client older page. The anchor used to bound inherited fork history is not that cursor, so an initial fork snapshot could still read a large source turn. Only a client page of the thread being read keeps the whole turn window. The in-memory window now points the older-history cursor at the omitted row's source thread.
1 parent 62270db commit 7c3b2f1

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4110,6 +4110,16 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
41104110
`;
41114111
}
41124112

4113+
const cappedInitialFork = yield* projectionStore.getThreadSnapshotWindow(targetThreadId, {
4114+
rowLimit: 2,
4115+
userTurnLimit: 10,
4116+
});
4117+
assert.isAtMost(
4118+
cappedInitialFork.projection.visibleTurnItems.filter(
4119+
(row) => row.visibility === "inherited",
4120+
).length,
4121+
2,
4122+
);
41134123
const boundedFork = yield* projectionStore.getThreadSnapshotWindow(targetThreadId, {
41144124
rowLimit: 2,
41154125
});

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,6 +2661,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
26612661
readonly anchorItemId?: TurnItemId | undefined;
26622662
readonly requiredRunId?: RunId | undefined;
26632663
readonly suppressLocal?: boolean | undefined;
2664+
/** Client asked for an older page of this thread. Fork bounds are not one. */
2665+
readonly olderPage?: boolean | undefined;
26642666
},
26652667
fields?: ReadonlyArray<ProjectionRecordField>,
26662668
filter?: ProjectionRecordFilter,
@@ -2805,13 +2807,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
28052807
-- has the same problem when earlier turns exist: the turn
28062808
-- window can still be tens of megabytes and holds the write
28072809
-- 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.
2810+
-- Cap that first read. A client older page already has a
2811+
-- cursor, so its turn window stays whole. The anchor used to
2812+
-- bound inherited fork history is not that cursor.
28102813
LIMIT CASE
28112814
WHEN ${window.rowLimit} = 0 THEN 0
28122815
WHEN (SELECT ordinal FROM boundary) > 0
28132816
AND (SELECT COUNT(*) FROM user_anchors) > 0
2814-
AND ${window.anchorItemId ?? null} IS NOT NULL THEN -1
2817+
AND ${window.olderPage === true ? 1 : 0} = 1 THEN -1
28152818
ELSE ${window.rowLimit}
28162819
END
28172820
), older_history AS (
@@ -3303,11 +3306,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
33033306
},
33043307
): Effect.Effect<ReadProjectionResult, ProjectionStoreV2Error> =>
33053308
Effect.gen(function* () {
3309+
const olderPage = window?.historyAnchor?.threadId === threadId;
33063310
const localWindow =
33073311
window?.suppressLocal === true ||
33083312
(window?.historyAnchor !== undefined && window.historyAnchor.threadId !== threadId)
3309-
? { ...window, rowLimit: 0, anchorItemId: undefined }
3310-
: window;
3313+
? { ...window, rowLimit: 0, anchorItemId: undefined, olderPage }
3314+
: window === undefined
3315+
? undefined
3316+
: { ...window, olderPage };
33113317
const read = yield* readCanonicalProjection(threadId, localWindow);
33123318
const projection = read.projection;
33133319
const finish = (visible: OrchestrationV2ThreadProjection): ReadProjectionResult => ({
@@ -6403,7 +6409,7 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
64036409
...snapshot,
64046410
projection: { ...snapshot.projection, visibleTurnItems },
64056411
olderHistoryExists: start > 0,
6406-
olderHistoryThreadId: start > 0 ? threadId : null,
6412+
olderHistoryThreadId: start > 0 ? candidates[start - 1]!.sourceThreadId : null,
64076413
};
64086414
}),
64096415
),

0 commit comments

Comments
 (0)