Coalesce high-frequency assistant streaming deltas #4323
Closedcolonelpanic8(GitHub profile, opens in a new tab)wants to merge 21 commits into
main fromrefs/pull/4323/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 939OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -901,0 +919,21 @@919+ const nextText = `${Option.getOrElse(pendingText, () => "")}${delta}`;920+ const lastFlushedAt = yield* Cache.getOption(921+ streamingAssistantLastFlushAtByMessageId,922+ messageId,923+ );924+ const occurredAtMillis = Date.parse(occurredAt);925+ const elapsedSinceFlush = Option.match(lastFlushedAt, {926+ onNone: () => Number.POSITIVE_INFINITY,927+ onSome: (value) =>928+ Number.isFinite(occurredAtMillis) ? Math.max(0, occurredAtMillis - value) : 0,929+ });930+ const shouldFlush =931+ elapsedSinceFlush >= STREAMING_ASSISTANT_FLUSH_INTERVAL_MILLIS ||932+ nextText.length >= MAX_COALESCED_STREAMING_ASSISTANT_CHARS;933+934+ if (!shouldFlush) {935+ yield* Cache.set(bufferedAssistantTextByMessageId, messageId, nextText);936+ return "";937+ }938+939+ yield* Cache.invalidate(bufferedAssistantTextByMessageId, messageId);
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1554OutdatedResolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1496,5 +1539,16 @@1496- messageId: assistantMessageId,1497- delta: assistantDelta,1498- ...(turnId ? { turnId } : {}),1499- createdAt: now,1500- });1539+ const flushChunk = yield* appendCoalescedStreamingAssistantText(1540+ assistantMessageId,1541+ assistantDelta,1542+ now,1543+ );1544+ if (flushChunk.length > 0) {1545+ yield* orchestrationEngine.dispatch({1546+ type: "thread.message.assistant.delta",1547+ commandId: yield* providerCommandId(event, "assistant-delta"),1548+ threadId: thread.id,1549+ messageId: assistantMessageId,1550+ delta: flushChunk,1551+ ...(turnId ? { turnId } : {}),1552+ createdAt: now,1553+ });1554+ }
colonelpanic8(GitHub profile, opens in a new tab) reopened this
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1219OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1163,3 +1216,4 @@11631216 yield* clearBufferedProposedPlan(input.planId);11641217 });116512181219+ const finalizeAssistantMessagesForSessionExit = (input: {
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1671OutdatedResolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1496,5 +1656,16 @@1496- messageId: assistantMessageId,1497- delta: assistantDelta,1498- ...(turnId ? { turnId } : {}),1499- createdAt: now,1500- });1656+ const flushChunk = yield* appendCoalescedStreamingAssistantText(1657+ assistantMessageId,1658+ assistantDelta,1659+ now,1660+ );1661+ if (flushChunk.length > 0) {1662+ yield* orchestrationEngine.dispatch({1663+ type: "thread.message.assistant.delta",1664+ commandId: yield* providerCommandId(event, "assistant-delta"),1665+ threadId: thread.id,1666+ messageId: assistantMessageId,1667+ delta: flushChunk,1668+ ...(turnId ? { turnId } : {}),1669+ createdAt: now,1670+ });1671+ yield* commitStreamingAssistantFlush(assistantMessageId, now);
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1126Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -902,0 +959,21 @@959+ const occurredAtMillis = Date.parse(occurredAt);960+ const elapsedSinceFlush = Option.match(lastFlushedAt, {961+ onNone: () => Number.POSITIVE_INFINITY,962+ onSome: (value) =>963+ Number.isFinite(occurredAtMillis) ? Math.max(0, occurredAtMillis - value) : 0,964+ });965+ const shouldFlush =966+ elapsedSinceFlush >= STREAMING_ASSISTANT_FLUSH_INTERVAL_MILLIS ||967+ nextText.length >= MAX_COALESCED_STREAMING_ASSISTANT_CHARS;968+969+ if (!shouldFlush) {970+ yield* Cache.set(bufferedAssistantTextByMessageId, messageId, nextText);971+ return "";972+ }973+974+ // Keep the pending text buffered until the caller confirms dispatch. If975+ // dispatch fails, a later delta or terminal event can retry the complete976+ // chunk instead of silently dropping it.977+ yield* Cache.set(bufferedAssistantTextByMessageId, messageId, nextText);978+ return nextText;979+ });apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1126Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -902,0 +959,21 @@959+ const occurredAtMillis = Date.parse(occurredAt);960+ const elapsedSinceFlush = Option.match(lastFlushedAt, {961+ onNone: () => Number.POSITIVE_INFINITY,962+ onSome: (value) =>963+ Number.isFinite(occurredAtMillis) ? Math.max(0, occurredAtMillis - value) : 0,964+ });965+ const shouldFlush =966+ elapsedSinceFlush >= STREAMING_ASSISTANT_FLUSH_INTERVAL_MILLIS ||967+ nextText.length >= MAX_COALESCED_STREAMING_ASSISTANT_CHARS;968+969+ if (!shouldFlush) {970+ yield* Cache.set(bufferedAssistantTextByMessageId, messageId, nextText);971+ return "";972+ }973+974+ // Keep the pending text buffered until the caller confirms dispatch. If975+ // dispatch fails, a later delta or terminal event can retry the complete976+ // chunk instead of silently dropping it.977+ yield* Cache.set(bufferedAssistantTextByMessageId, messageId, nextText);978+ return nextText;979+ });apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2491Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1671,3 +1862,8 @@16711862 }1672186316731864 if (event.type === "session.exited") {1865+ yield* finalizeAssistantMessagesForSessionExit({1866+ event,1867+ threadId: thread.id,1868+ createdAt: now,1869+ });apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1646Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1041,3 +1145,4 @@10411145 });10421146 }10431147 yield* clearAssistantMessageState(input.messageId);1148+ yield* forgetAssistantMessageIdForThread(input.threadId, input.messageId);
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1986OutdatedResolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1668,3 +1976,11 @@16681976 updatedAt: now,16691977 });16701978 }1979+1980+ // Providers can omit turnId on content deltas even when the terminal1981+ // turn event is scoped. Finalize any such thread-tracked remainder.1982+ yield* finalizeAssistantMessagesForTerminalEvent({1983+ event,1984+ threadId: thread.id,1985+ createdAt: now,1986+ });apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1319Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -939,0 +1069,21 @@1069+ commandTag: string;1070+ }) =>1071+ Effect.gen(function* () {1072+ const commandId = yield* providerCommandId(input.event, input.commandTag);1073+ yield* dispatchRetainedAssistantDelta(1074+ {1075+ type: "thread.message.assistant.delta",1076+ commandId,1077+ threadId: input.threadId,1078+ messageId: input.messageId,1079+ delta: input.delta,1080+ ...(input.turnId ? { turnId: input.turnId } : {}),1081+ createdAt: input.createdAt,1082+ },1083+ commitStreamingAssistantFlush(input.messageId, input.createdAt),1084+ ).pipe(1085+ // Holding the worker on failure backpressures subsequent provider1086+ // deltas, keeping the retained chunk bounded without dropping text.1087+ // Reusing one command id also makes an ambiguous retry idempotent.1088+ Effect.retry(Schedule.spaced(Duration.millis(STREAMING_ASSISTANT_FLUSH_INTERVAL_MILLIS))),1089+ );
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2790Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1786,0 +2329,21 @@2329+ ) =>2330+ Effect.gen(function* () {2331+ yield* Cache.invalidate(streamingAssistantFlushScheduledByMessageId, input.messageId);2332+ const bufferedText = yield* peekBufferedAssistantText(input.messageId);2333+ if (bufferedText.length === 0) {2334+ if (input.isDeferred === true) {2335+ yield* completeDeferredAssistantDelta(input.messageId);2336+ }2337+ return;2338+ }2339+2340+ const dispatchExit = yield* Effect.exit(2341+ dispatchStreamingAssistantDelta({2342+ event: input.event,2343+ threadId: input.threadId,2344+ messageId: input.messageId,2345+ ...(input.turnId ? { turnId: input.turnId } : {}),2346+ delta: bufferedText,2347+ createdAt: input.event.createdAt,2348+ commandTag: "assistant-delta-timer-flush",2349+ }),apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1646Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1041,3 +1315,4 @@10411315 });10421316 }10431317 yield* clearAssistantMessageState(input.messageId);1318+ yield* forgetAssistantMessageIdForThread(input.threadId, input.messageId);apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1188Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -936,4 +1075,8 @@9361075 Cache.invalidate(bufferedProposedPlanById, planId);93710769381077 const clearAssistantMessageState = (messageId: MessageId) =>939- clearBufferedAssistantText(messageId);1078+ Effect.all([1079+ clearBufferedAssistantText(messageId),1080+ Cache.invalidate(streamingAssistantLastFlushAtByMessageId, messageId),1081+ Cache.invalidate(streamingAssistantFlushScheduledByMessageId, messageId),1082+ ]).pipe(Effect.asVoid);
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1411OutdatedResolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1031,10 +1393,19 @@1393+ ...(input.turnId ? { turnId: input.turnId } : {}),1394+ deliveryMode: "streaming",1395+ delta: "",1396+ flushPendingFirst: true,1397+ isDeferred: true,1398+ });1399+ yield* scheduleRuntimeEventRetry(input.event);1400+ return yield* Effect.failCause(dispatchExit.cause);1401+ }10311402 }103214031033- if (input.hasProjectedMessage || hasRenderableText) {1404+ if (input.hasProjectedMessage || shouldDispatchText) {10341405 yield* orchestrationEngine.dispatch({10351406 type: "thread.message.assistant.complete",10361407 commandId: yield* providerCommandId(input.event, input.commandTag),10371408 threadId: input.threadId,10381409 messageId: input.messageId,10391410 ...(input.turnId ? { turnId: input.turnId } : {}),1040- createdAt: input.createdAt,1411+ createdAt,apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1400OutdatedResolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1030,0 +1380,21 @@1380+ {1381+ eventId: input.event.eventId,1382+ messageId: input.messageId,1383+ cause: Cause.pretty(dispatchExit.cause),1384+ },1385+ );1386+ yield* Cache.set(bufferedAssistantTextByMessageId, input.messageId, text);1387+ yield* markDeferredAssistantDelta(input.messageId);1388+ yield* scheduleAssistantDeltaRetry({1389+ source: "assistant-delta",1390+ event: input.event,1391+ threadId: input.threadId,1392+ messageId: input.messageId,1393+ ...(input.turnId ? { turnId: input.turnId } : {}),1394+ deliveryMode: "streaming",1395+ delta: "",1396+ flushPendingFirst: true,1397+ isDeferred: true,1398+ });1399+ yield* scheduleRuntimeEventRetry(input.event);1400+ return yield* Effect.failCause(dispatchExit.cause);
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1585Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1029,0 +1402,21 @@1402+ Effect.gen(function* () {1403+ yield* Effect.logWarning(1404+ "provider runtime ingestion deferred assistant finalization after retries",1405+ {1406+ eventId: input.event.eventId,1407+ messageId: input.messageId,1408+ cause: Cause.pretty(cause),1409+ },1410+ );1411+ if (retainText && shouldDispatchText) {1412+ yield* Cache.set(bufferedAssistantTextByMessageId, input.messageId, text);1413+ }1414+ if (input.isDeferred !== true) {1415+ yield* markDeferredAssistantDelta(input.messageId);1416+ }1417+ yield* scheduleAssistantFinalizationRetry({1418+ ...input,1419+ source: "assistant-finalize",1420+ hasProjectedMessage: shouldComplete,1421+ isDeferred: true,1422+ });
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2441Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1634,4 +2203,4 @@16342203 });16352204 }163622051637- if (event.type === "turn.completed") {2206+ if (event.type === "turn.completed" || event.type === "turn.aborted") {apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2669Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1786,0 +2375,21 @@2375+2376+ const deferRemainder = (cause: Cause.Cause<unknown>) =>2377+ Effect.gen(function* () {2378+ yield* Effect.logWarning(2379+ "provider runtime ingestion deferred assistant delta after retries",2380+ {2381+ eventId: input.event.eventId,2382+ messageId: input.messageId,2383+ deliveryMode: input.deliveryMode,2384+ cause: Cause.pretty(cause),2385+ },2386+ );2387+ if (input.isDeferred !== true) {2388+ yield* markDeferredAssistantDelta(input.messageId);2389+ }2390+ yield* scheduleAssistantDeltaRetry({2391+ ...input,2392+ delta: input.delta.slice(offset),2393+ flushPendingFirst: true,2394+ isDeferred: true,2395+ });
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2249OutdatedResolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1655,6 +2242,8 @@16552242 hasProjectedMessage: findMessageById(messages, assistantMessageId) !== undefined,16562243 }),16572244 { concurrency: 1 },1658- ).pipe(Effect.asVoid);1659- yield* clearAssistantMessageIdsForTurn(thread.id, turnId);1660- yield* clearAssistantSegmentStateForTurn(thread.id, turnId);2245+ );2246+ if (finalizedAssistantMessages.every(Boolean)) {2247+ yield* clearAssistantMessageIdsForTurn(thread.id, turnId);2248+ yield* clearAssistantSegmentStateForTurn(thread.id, turnId);2249+ }
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1845Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1165,0 +1667,21 @@1667+ assistantMessageIdsByThreadId,1668+ input.threadId,1669+ );1670+ if (Option.isSome(threadMessageIds)) {1671+ yield* Effect.forEach(1672+ threadMessageIds.value,1673+ (messageId) =>1674+ finalizedMessageIds.has(messageId)1675+ ? Effect.void1676+ : finalizeAssistantMessage({1677+ event: input.event,1678+ threadId: input.threadId,1679+ messageId,1680+ createdAt: input.createdAt,1681+ commandTag: "assistant-complete-on-session-exit",1682+ finalDeltaCommandTag: "assistant-delta-finalize-on-session-exit",1683+ hasProjectedMessage: findMessageById(messages, messageId) !== undefined,1684+ }),1685+ { concurrency: 1 },1686+ ).pipe(Effect.asVoid);1687+ }apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1646Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1042,2 +1476,21 @@1476+ type: "thread.message.assistant.complete",1477+ commandId,1478+ threadId: input.threadId,1479+ messageId: input.messageId,1480+ ...(input.turnId ? { turnId: input.turnId } : {}),1481+ createdAt,1482+ }),1483+ ).pipe(1484+ Effect.retry({1485+ schedule: Schedule.spaced(Duration.millis(STREAMING_ASSISTANT_FLUSH_INTERVAL_MILLIS)),1486+ times: 2,1487+ }),1488+ ),1489+ );1490+ if (Exit.isFailure(dispatchExit)) {1491+ yield* deferFinalization(dispatchExit.cause, false);1492+ return false;1493+ }10421494 }10431495 yield* clearAssistantMessageState(input.messageId);1496+ yield* forgetAssistantMessageIdForThread(input.threadId, input.messageId);
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2273OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1655,6 +2266,8 @@16552266 hasProjectedMessage: findMessageById(messages, assistantMessageId) !== undefined,16562267 }),16572268 { concurrency: 1 },1658- ).pipe(Effect.asVoid);1659- yield* clearAssistantMessageIdsForTurn(thread.id, turnId);1660- yield* clearAssistantSegmentStateForTurn(thread.id, turnId);2269+ );2270+ if (finalizedAssistantMessages.every(Boolean)) {2271+ yield* clearAssistantMessageIdsForTurn(thread.id, turnId);2272+ yield* clearAssistantSegmentStateForTurn(thread.id, turnId);2273+ }
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2230Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1453,3 +2070,4 @@14532070 }14542071 }145520722073+ if (apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1585Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1029,0 +1446,21 @@1446+ cause: Cause.pretty(cause),1447+ },1448+ );1449+ if (retainText && shouldDispatchText) {1450+ yield* Cache.set(bufferedAssistantTextByMessageId, input.messageId, text);1451+ }1452+ if (input.isDeferred !== true) {1453+ yield* markDeferredAssistantDelta(input.messageId);1454+ }1455+ yield* scheduleAssistantFinalizationRetry({1456+ source: "assistant-finalize",1457+ event: input.event,1458+ threadId: input.threadId,1459+ messageId: input.messageId,1460+ ...(input.turnId ? { turnId: input.turnId } : {}),1461+ createdAt: input.createdAt,1462+ commandTag: input.commandTag,1463+ finalDeltaCommandTag: input.finalDeltaCommandTag,1464+ hasProjectedMessage: shouldComplete,1465+ isDeferred: true,1466+ });
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2430OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1764,3 +2414,17 @@17642414 }17652415 }176624162417+ if (deferredThreadSession !== null) {2418+ if (yield* deferBoundaryEventForAssistantDeltas(event, thread.id)) {2419+ return;2420+ }2421+ yield* orchestrationEngine.dispatch({2422+ type: "thread.session.set",2423+ commandId: yield* providerCommandId(2424+ event,2425+ event.type === "runtime.error" ? "runtime-error-session-set" : "thread-session-set",2426+ ),2427+ threadId: thread.id,2428+ session: deferredThreadSession,2429+ createdAt: now,2430+ });
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1422Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -955,9 +1366,12 @@955- yield* orchestrationEngine.dispatch({956- type: "thread.message.assistant.delta",957- commandId: yield* providerCommandId(input.event, input.commandTag),958- threadId: input.threadId,959- messageId: input.messageId,960- delta: bufferedText,961- ...(input.turnId ? { turnId: input.turnId } : {}),962- createdAt: input.createdAt,963- });1366+ yield* dispatchRetainedAssistantDelta(1367+ {1368+ type: "thread.message.assistant.delta",1369+ commandId: yield* providerCommandId(input.event, input.commandTag),1370+ threadId: input.threadId,1371+ messageId: input.messageId,1372+ delta: bufferedText,1373+ ...(input.turnId ? { turnId: input.turnId } : {}),1374+ createdAt: input.createdAt,1375+ },1376+ clearBufferedAssistantText(input.messageId),1377+ );apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2697Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1786,0 +2490,21 @@2490+ (flushPendingFirst || pendingText.length >= maxRetainedChars)2491+ ) {2492+ const dispatchExit = yield* Effect.exit(2493+ input.deliveryMode === "streaming"2494+ ? dispatchStreamingAssistantDelta({2495+ event: input.event,2496+ threadId: input.threadId,2497+ messageId: input.messageId,2498+ ...(input.turnId ? { turnId: input.turnId } : {}),2499+ delta: pendingText,2500+ createdAt: input.event.createdAt,2501+ commandTag: "assistant-delta-deferred-retry",2502+ })2503+ : dispatchBufferedAssistantDelta({2504+ event: input.event,2505+ threadId: input.threadId,2506+ messageId: input.messageId,2507+ ...(input.turnId ? { turnId: input.turnId } : {}),2508+ delta: pendingText,2509+ createdAt: input.event.createdAt,2510+ }),apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2944Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1786,0 +2635,21 @@2635+ return processScheduledAssistantFlush(input);2636+ case "assistant-delta":2637+ return processAssistantDelta(input);2638+ case "assistant-finalize":2639+ return Effect.gen(function* () {2640+ yield* Cache.invalidate(assistantFinalizationRetryScheduledByMessageId, input.messageId);2641+ if (yield* hasDeferredAssistantDeltaAheadOfFinalization(input.messageId)) {2642+ yield* scheduleAssistantFinalizationRetry(input);2643+ return;2644+ }2645+ const finalized = yield* finalizeAssistantMessage(input);2646+ if (finalized && input.turnId) {2647+ yield* releaseFinalizedAssistantMessageForTurn(2648+ input.threadId,2649+ input.turnId,2650+ input.messageId,2651+ );2652+ }2653+ });2654+ }2655+ };
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2877Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1786,0 +2678,21 @@2678+ case "domain":2679+ return processDomainEvent(input.event);2680+ case "assistant-flush":2681+ return processScheduledAssistantFlush(input);2682+ case "assistant-delta":2683+ return processAssistantDelta(input);2684+ case "assistant-finalize":2685+ return Effect.gen(function* () {2686+ yield* Cache.invalidate(assistantFinalizationRetryScheduledByMessageId, input.messageId);2687+ if (yield* hasDeferredAssistantDeltaAheadOfFinalization(input.messageId)) {2688+ yield* scheduleAssistantFinalizationRetry(input);2689+ return;2690+ }2691+ const finalized = yield* finalizeAssistantMessage(input);2692+ if (finalized && input.turnId) {2693+ yield* releaseFinalizedAssistantMessageForTurn(2694+ input.threadId,2695+ input.turnId,2696+ input.messageId,2697+ );2698+ }apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2226Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1440,13 +2103,9 @@2103+ } else {2104+ yield* orchestrationEngine.dispatch({2105+ type: "thread.session.set",2106+ commandId: yield* providerCommandId(event, "thread-session-set"),14402107 threadId: thread.id,1441- status,1442- providerName: event.provider,1443- ...(event.providerInstanceId !== undefined1444- ? { providerInstanceId: event.providerInstanceId }1445- : {}),1446- runtimeMode: thread.session?.runtimeMode ?? "full-access",1447- activeTurnId: nextActiveTurnId,1448- lastError,1449- updatedAt: now,1450- },1451- createdAt: now,1452- });2108+ session: nextSession,2109+ createdAt: now,2110+ });2111+ }
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2721OutdatedResolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1786,0 +2701,21 @@2701+2702+ const processInput = (input: RuntimeIngestionInput) => {2703+ switch (input.source) {2704+ case "runtime":2705+ return processRuntimeEvent(input.event);2706+ case "domain":2707+ return processDomainEvent(input.event);2708+ case "assistant-flush":2709+ return processScheduledAssistantFlush(input);2710+ case "assistant-delta":2711+ return processAssistantDelta(input);2712+ case "assistant-finalize":2713+ return Effect.gen(function* () {2714+ yield* Cache.invalidate(assistantFinalizationRetryScheduledByMessageId, input.messageId);2715+ if (yield* hasDeferredAssistantDeltaAheadOfFinalization(input.messageId)) {2716+ yield* scheduleAssistantFinalizationRetry(input);2717+ return;2718+ }2719+ const finalized = yield* finalizeAssistantMessage(input);2720+ if (finalized && input.turnId) {2721+ yield* resetFinalizedAssistantMessageForTurn(
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2377OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1654,7 +2365,13 @@16542365 finalDeltaCommandTag: "assistant-delta-finalize-fallback",16552366 hasProjectedMessage: findMessageById(messages, assistantMessageId) !== undefined,16562367 }),1657- { concurrency: 1 },1658- ).pipe(Effect.asVoid);1659- yield* clearAssistantMessageIdsForTurn(thread.id, turnId);1660- yield* clearAssistantSegmentStateForTurn(thread.id, turnId);2368+ (assistantMessageId) =>2369+ releaseFinalizedAssistantMessageForTurn(thread.id, turnId, assistantMessageId),2370+ );2371+ if (finalizedAssistantMessages.every(Boolean)) {2372+ yield* clearAssistantMessageIdsForTurn(thread.id, turnId);2373+ yield* clearAssistantSegmentStateForTurn(thread.id, turnId);2374+ if (event.type === "turn.completed" && (yield* flushDeferredThreadSession())) {2375+ return;2376+ }2377+ }
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2482Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1661,9 +2374,13 @@2374+ if (event.type === "turn.completed" && (yield* flushDeferredThreadSession())) {2375+ return;2376+ }166123771662- yield* finalizeBufferedProposedPlan({1663- event,1664- threadId: thread.id,1665- threadProposedPlans: proposedPlans,1666- planId: proposedPlanIdForTurn(thread.id, turnId),1667- turnId,1668- updatedAt: now,1669- });2378+ yield* finalizeBufferedProposedPlan({2379+ event,2380+ threadId: thread.id,2381+ threadProposedPlans: proposedPlans,2382+ planId: proposedPlanIdForTurn(thread.id, turnId),2383+ turnId,2384+ updatedAt: now,2385+ });2386+ }
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1585Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1029,0 +1480,21 @@1480+ },1481+ );1482+ if (retainedText !== undefined && shouldDispatchText) {1483+ yield* Cache.set(bufferedAssistantTextByMessageId, input.messageId, retainedText);1484+ }1485+ if (input.isDeferred !== true) {1486+ yield* rememberAssistantMessageIdForThread(input.threadId, input.messageId);1487+ yield* markDeferredAssistantDelta(input.messageId);1488+ }1489+ yield* scheduleAssistantFinalizationRetry({1490+ source: "assistant-finalize",1491+ event: input.event,1492+ threadId: input.threadId,1493+ messageId: input.messageId,1494+ ...(input.turnId ? { turnId: input.turnId } : {}),1495+ createdAt: input.createdAt,1496+ commandTag: input.commandTag,1497+ finalDeltaCommandTag: input.finalDeltaCommandTag,1498+ hasProjectedMessage: shouldComplete,1499+ isDeferred: true,1500+ });
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2661Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1786,0 +2592,21 @@2592+ }2593+ const retries = yield* Cache.getOption(2594+ deferredAssistantDeltaRetriesByMessageId,2595+ input.messageId,2596+ ).pipe(Effect.map((count) => Option.getOrElse(count, () => 0)));2597+ if (retries >= MAX_DEFERRED_ASSISTANT_DELTA_RETRIES) {2598+ yield* Effect.logError(2599+ "provider runtime ingestion abandoned deferred assistant delta",2600+ {2601+ eventId: input.event.eventId,2602+ messageId: input.messageId,2603+ retries,2604+ cause: Cause.pretty(cause),2605+ },2606+ );2607+ // Do not retain an ordering marker once a typed dispatch failure2608+ // has exhausted its retry budget. A finalization already waiting2609+ // for this delta can then release its lifecycle boundary.2610+ yield* completeDeferredAssistantDelta(input.messageId);2611+ yield* Cache.invalidate(deferredAssistantDeltaRetriesByMessageId, input.messageId);2612+ return;apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1531OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1029,0 +1511,21 @@1511+ yield* clearAssistantMessageState(input.messageId);1512+ yield* forgetAssistantMessageIdForThread(input.threadId, input.messageId);1513+ if (input.turnId) {1514+ if (1515+ input.event.type === "request.opened" ||1516+ input.event.type === "user-input.requested"1517+ ) {1518+ yield* releaseFinalizedAssistantMessageForTurn(1519+ input.threadId,1520+ input.turnId,1521+ input.messageId,1522+ );1523+ } else {1524+ yield* resetFinalizedAssistantMessageForTurn(1525+ input.threadId,1526+ input.turnId,1527+ input.messageId,1528+ );1529+ }1530+ }1531+ return;apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2661Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1786,0 +2592,21 @@2592+ }2593+ const retries = yield* Cache.getOption(2594+ deferredAssistantDeltaRetriesByMessageId,2595+ input.messageId,2596+ ).pipe(Effect.map((count) => Option.getOrElse(count, () => 0)));2597+ if (retries >= MAX_DEFERRED_ASSISTANT_DELTA_RETRIES) {2598+ yield* Effect.logError(2599+ "provider runtime ingestion abandoned deferred assistant delta",2600+ {2601+ eventId: input.event.eventId,2602+ messageId: input.messageId,2603+ retries,2604+ cause: Cause.pretty(cause),2605+ },2606+ );2607+ // Do not retain an ordering marker once a typed dispatch failure2608+ // has exhausted its retry budget. A finalization already waiting2609+ // for this delta can then release its lifecycle boundary.2610+ yield* completeDeferredAssistantDelta(input.messageId);2611+ yield* Cache.invalidate(deferredAssistantDeltaRetriesByMessageId, input.messageId);2612+ return;
- apps/server/src/orchestration/decider.ts line 1039OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1036,4 +1036,4 @@10361036 threadId: command.threadId,10371037 messageId: command.messageId,10381038 role: "assistant",1039- text: "",1039+ text: command.finalText ?? "",
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1532Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1029,0 +1509,21 @@1509+ `${input.commandTag}-terminal-fallback`,1510+ );1511+ const fallbackExit = shouldComplete1512+ ? yield* Effect.exit(1513+ Effect.suspend(() =>1514+ orchestrationEngine.dispatch({1515+ type: "thread.message.assistant.complete",1516+ commandId: fallbackCommandId,1517+ threadId: input.threadId,1518+ messageId: input.messageId,1519+ ...(finalText.length > 0 ? { finalText } : {}),1520+ ...(input.turnId ? { turnId: input.turnId } : {}),1521+ createdAt,1522+ }),1523+ ),1524+ )1525+ : Exit.succeed({ sequence: 0 });1526+ if (Exit.isSuccess(fallbackExit)) {1527+ yield* clearAssistantMessageState(input.messageId);1528+ yield* forgetAssistantMessageIdForThread(input.threadId, input.messageId);1529+ return true;
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2414OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1611,9 +2406,9 @@16112406 : {}),16122407 });161324081614- if (turnId) {2409+ if (turnId && assistantFinalized) {16152410 yield* forgetAssistantMessageId(thread.id, turnId, assistantMessageId);16162411 }16172412 }161824131619- if (turnId) {2414+ if (turnId && assistantFinalized) {
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2421Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -1611,8 +2406,11 @@16112406 : {}),16122407 });161324081614- if (turnId) {2409+ if (turnId && assistantFinalized) {16152410 yield* forgetAssistantMessageId(thread.id, turnId, assistantMessageId);16162411 }16172412 }161824132414+ if (!assistantFinalized) {2415+ yield* scheduleRuntimeEventRetry(event);2416+ return;
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
3fe7ef91-a42a-4214-8c87-5f18c363cdb0You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share