perf(server): prune events the projections already cover #29806
Mergedjuliusmarmingemerged 5 commits into
main fromrefs/pull/29806/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/orchestration-v2/ProjectionMaintenance.ts line 102Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -147,15 +97,6 @@147- if (stored.event.type === "turn-item.updated") {148- yield* sql`149- INSERT INTO orchestration_v2_turn_item_positions (150- thread_id,151- turn_item_id,152- ordinal153- )154- VALUES (155- ${stored.event.threadId},156- ${stored.event.payload.id},157- ${stored.event.payload.ordinal}158- )159- ON CONFLICT(thread_id, turn_item_id) DO UPDATE SET160- ordinal = excluded.ordinal161- `;97+ const pruneEvents = Effect.fnUntraced(function* (98+ throughSequence: number,99+ head: number,100+ betweenPages: Effect.Effect<void>,101+ ) {102+ const referenced = yield* readPromptReferences(throughSequence, head, betweenPages);@@ -783,3 +783,4 @@783783 if (replayGap < 0 || replayGap > maxGap) {784784 return false;785785 }786+ if (afterSequence < (yield* applicationEvents.prunedThroughSequence)) return false;
- apps/server/src/orchestration-v2/ProjectionMaintenance.ts lines 121 to 139Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -161,0 +105,21 @@105+ let cursor = 0;106+ while (cursor < throughSequence) {107+ const rows = yield* sql<{ readonly sequence: number; readonly prunable: number }>`108+ SELECT109+ event.sequence,110+ CASE111+ WHEN event.application_event_version = 1 THEN 0112+ -- The legacy importer treats a thread without thread.created as not yet imported.113+ WHEN event.event_type = 'thread.created' THEN 0114+ -- Codex usage history reads the selection a run attempt started with.115+ WHEN event.event_type = 'run.updated'116+ AND json_extract(event.payload_json, '$.status') = 'running' THEN 0117+ -- The newest row keeps each stream's high water, stream_version, and snapshot sequence.118+ WHEN event.sequence = (119+ SELECT MAX(newer.sequence) FROM orchestration_events AS newer120+ WHERE newer.aggregate_kind = event.aggregate_kind121+ AND newer.stream_id = event.stream_id122+ ) THEN 0123+ ELSE 1124+ END AS prunable125+ FROM orchestration_events AS eventapps/server/src/ws.ts lines 783 to 789Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -783,6 +783,7 @@783783 if (replayGap < 0 || replayGap > maxGap) {784784 return false;785785 }786+ if (afterSequence < (yield* applicationEvents.prunedThroughSequence)) return false;786787 const stats = yield* applicationEvents.getReplayStats({787788 afterSequence,788789 throughSequence: headSequence,apps/server/src/ws.ts lines 975 to 985OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -971,4 +972,14 @@971972 }),972973 ),973974 );974- if (input.afterSequence > highWater) {975+ const prunedThrough = yield* applicationEvents.prunedThroughSequence.pipe(976+ Effect.mapError(977+ (cause) =>978+ new OrchestrationV2GetThreadProjectionError({979+ threadId: input.threadId,980+ message: `Failed to prepare orchestration V2 thread ${input.threadId} replay`,981+ cause,982+ }),983+ ),984+ );985+ if (input.afterSequence > highWater || input.afterSequence < prunedThrough) {
- apps/server/src/ws.test.ts line 1126OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1108,20 +1126 @@1108- Array.from(items, (item) =>1109- item.kind === "event"1110- ? [item.sequence, item.event.type === "message.updated" ? item.event.payload.text : null]1111- : [item.kind],1112- ),1113- [1114- [first!.sequence, "a created"],1115- [second!.sequence, "b created"],1116- [head!.sequence, "a final"],1117- ["synchronized"],1118- ],1119- );1120- }).pipe(1121- Effect.provide(1122- ProjectionMaintenance.layer.pipe(1123- Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),1124- Layer.provideMerge(ReplayTestLayer),1125- ),1126- ),1127- ),1126+ assert.notDeepEqual(Exit.isFailure(exit) ? Cause.squash(exit.cause) : "resumed", "snapshot");
- docs/internals/overview.md lines 71 to 72OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -58,4 +58,15 @@5858 finished. Keep external I/O out of command decisions and the database transaction. Effects tied to5959 a lost provider process cannot simply replay; recovery retires them before admitting new work.606061-Persisted events must remain decodable on replay. Changing a schema affects old environments at61+The event log only keeps what live readers need: about a day of history for reconnect replay and62+command-id retries.63+[ProjectionMaintenance](../../apps/server/src/orchestration-v2/ProjectionMaintenance.ts) deletes64+older events the projections cover, so projections cannot be rebuilt from the log. It keeps each65+stream's newest event (high-water marks, stream versions, snapshot sequences), `thread.created`66+(the legacy importer's marker), every `running` run update (Codex usage history reads the first67+of each attempt), and prompt anchors that retained subagent rows reference. A client whose cursor68+is below both its thread's newest event and the recorded pruned-through sequence missed pruned69+events, so it resumes from a snapshot; an idle thread's cursor resumes without one. Resumes70+recheck that sequence after reading the replay, since a pass can prune during the read. Command71+receipts stay for idempotency: a retry older than the retained history is still deduplicated,72+but its replay comes back without the pruned events.
juliusmarminge merged this550c5fa
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed