fix(server): queue messages during context compaction #11107
Mergedmaria-rcksmerged 5 commits into
main fromrefs/pull/11107/headmerged by maria-rcks 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:L
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts lines 1887 to 1887Resolvedby maria-rcks@@ -1761,3 +1880,4 @@1761188017621881 const processDomainEventSafely = (event: ProviderIntentEvent) =>17631882 processDomainEvent(event).pipe(1883+ Effect.ensuring(apps/server/src/orchestration/Layers/ProviderCommandReactor.ts lines 467 to 467OutdatedResolvedby maria-rcks@@ -390,0 +447,21 @@447+ .dispatch({448+ type: "thread.turn.start",449+ commandId,450+ threadId,451+ message: {452+ messageId: event.payload.messageId,453+ role: "user",454+ text: turnStart.value.message.text,455+ attachments: turnStart.value.message.attachments ?? [],456+ },457+ ...(event.payload.modelSelection !== undefined458+ ? { modelSelection: event.payload.modelSelection }459+ : {}),460+ ...(event.payload.sourceProposedPlan !== undefined461+ ? { sourceProposedPlan: event.payload.sourceProposedPlan }462+ : {}),463+ interactionMode: event.payload.interactionMode,464+ runtimeMode: event.payload.runtimeMode,465+ createdAt: event.payload.createdAt,466+ })467+ .pipe(Effect.onError(() => Effect.sync(() => void resumedTurnStarts.delete(commandId))));apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 1513OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1410,5 +1498,16 @@14101498 );14111499 }).pipe(14121500 Effect.andThen(restoreCompaction(event.payload.threadId, true)),1413- Effect.catchCause(recoverCompactionFailure),1501+ Effect.andThen(Effect.sync(() => void compactingThreadIds.delete(event.payload.threadId))),1502+ Effect.andThen(resumeTurnsAfterCompaction(event.payload.threadId)),1503+ Effect.catchCause((cause) =>1504+ recoverCompactionFailure(cause).pipe(1505+ Effect.andThen(1506+ cancelTurnsAfterCompaction(1507+ event.payload.threadId,1508+ "Context compaction failed. Send this message again to continue.",1509+ ),1510+ ),1511+ ),1512+ ),14141513 Effect.ensuring(Effect.sync(() => void compactingThreadIds.delete(event.payload.threadId))),apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 1763OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1642,3 +1757,4 @@16421757 const now = event.payload.createdAt;16431758 const wasCompacting = compactingThreadIds.has(thread.id);16441759 stoppingThreadIds.add(thread.id);1760+ yield* cancelTurnsAfterCompaction(
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts lines 1275 to 1277OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1187,3 +1271,7 @@11871271 if (yield* hasHandledTurnStartRecently(key)) {11881272 return;11891273 }1274+ const resumed = event.commandId !== null ? resumedTurnStarts.get(event.commandId) : undefined;1275+ if (resumed && turnsAfterCompaction.get(event.payload.threadId) !== resumed.queued) {1276+ return;1277+ }apps/server/src/orchestration/Layers/ProviderCommandReactor.ts lines 1760 to 1763OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1642,3 +1757,7 @@16421757 const now = event.payload.createdAt;16431758 const wasCompacting = compactingThreadIds.has(thread.id);16441759 stoppingThreadIds.add(thread.id);1760+ yield* cancelTurnsAfterCompaction(1761+ thread.id,1762+ "The session was stopped during context compaction. Send this message again to continue.",1763+ );
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 430OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -390,0 +410,21 @@410+ turnsAfterCompaction.delete(threadId);411+ for (const event of queued) {412+ yield* appendProviderFailureActivity({413+ threadId,414+ kind: "provider.turn.start.failed",415+ summary: "Queued message was not sent",416+ detail,417+ turnId: null,418+ createdAt: DateTime.formatIso(yield* DateTime.now),419+ requestId: event.payload.messageId,420+ }).pipe(Effect.ignore({ log: true, message: "failed to report canceled queued message" }));421+ }422+ });423+424+ const resumeTurnsAfterCompaction = Effect.fn("resumeTurnsAfterCompaction")(function* (425+ threadId: ThreadId,426+ ) {427+ const queued = turnsAfterCompaction.get(threadId) ?? [];428+ while (queued.length > 0 && turnsAfterCompaction.get(threadId) === queued) {429+ // In flight from here on: a cancellation reports it when the replay runs, not from the queue.430+ const event = queued.shift()!;
maria-rcks merged this57aee3e
maria-rcks closed this
Sign in to join this conversation.



macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed