Skip to content

Commit 481b9b7

Browse files
authored
fix(server): compact superseded turn-item snapshots (#20990)
1 parent 796fc0a commit 481b9b7

3 files changed

Lines changed: 500 additions & 6 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
931931
messageEvent("message-1", "streaming"),
932932
nodeEvent("node-1", "running"),
933933
itemEvent("item-1", "streaming"),
934+
itemEvent("item-middle", "still streaming"),
934935
...Array.from({ length: 501 }, (_, index) =>
935936
threadStateEvent(`history-${index}`, "thread.visited"),
936937
),

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,12 @@ export const layer: Layer.Layer<
222222

223223
const supersedableThreadEventTypes = new Set(SUPERSEDABLE_THREAD_EVENT_TYPES);
224224
const readStateThreadEventTypes = new Set(READ_STATE_THREAD_EVENT_TYPES);
225-
// Keep subagent.updated and turn-item.updated rows: slimmed payloads reference their prompts.
226-
// Bound each reader request and writer delete, including when the reader
227-
// worker is unavailable and discovery falls back to the writer.
225+
// Subagent snapshots stay intact because later rows can reference an earlier prompt.
226+
// Commanded turn items stay for command-result replay.
227+
// Small pages bound work on the reader worker and synchronous fallback.
228228
const COMPACTION_PAGE_SIZE = 50;
229229

230-
/** Retain newest and oldest keyed events to preserve client insertion order; page all events to bound sparse scans.
231-
* Keep turn-item.updated because replay assigns positions on first write. */
230+
// Keep first/latest keyed events for replay order; page every event to bound sparse scans.
232231
const compactEventStore = Effect.fn("ProjectionMaintenance.compactEventStore")(function* (
233232
betweenPages: Effect.Effect<void> = Effect.void,
234233
) {
@@ -269,6 +268,11 @@ export const layer: Layer.Layer<
269268
WHEN event.application_event_version = 2
270269
AND event.event_type IN ('message.updated', 'node.updated')
271270
THEN json_extract(event.payload_json, '$.id')
271+
WHEN event.application_event_version = 2
272+
AND event.event_type = 'turn-item.updated'
273+
AND event.command_id IS NULL
274+
AND json_extract(event.payload_json, '$.type') <> 'subagent'
275+
THEN json_extract(event.payload_json, '$.id')
272276
WHEN event.application_event_version = 2
273277
AND event.event_type = 'thread.pull-request-link-updated'
274278
THEN json_array(json_extract(event.payload_json, '$.host'),
@@ -321,7 +325,8 @@ export const layer: Layer.Layer<
321325
} else if (
322326
row.event_type === "thread.pull-request-link-updated" ||
323327
row.event_type === "message.updated" ||
324-
row.event_type === "node.updated"
328+
row.event_type === "node.updated" ||
329+
(row.event_type === "turn-item.updated" && row.entity_id !== null)
325330
) {
326331
const key = encodeEntityKey([row.event_type, row.stream_id, row.entity_id]);
327332
const candidate = oldestCandidateByEntityKey.get(key);

0 commit comments

Comments
 (0)