@@ -33,6 +33,7 @@ import * as Option from "effect/Option"; |
33 | 33 | import * as Path from "effect/Path"; |
34 | 34 | import * as Predicate from "effect/Predicate"; |
35 | 35 | import * as Schema from "effect/Schema"; |
| 36 | +import * as Semaphore from "effect/Semaphore"; |
36 | 37 |
|
37 | 38 | import * as ServerConfig from "../config.ts"; |
38 | 39 | import { |
@@ -1092,6 +1093,11 @@ export const layer: Layer.Layer< |
1092 | 1093 | const deliveredAttemptIds = new Set( |
1093 | 1094 | projection.providerTurns.filter(isDeliveredProviderTurn).map((turn) => turn.runAttemptId), |
1094 | 1095 | ); |
| 1096 | + const droppedAttemptIds = new Set( |
| 1097 | + projection.providerTurns |
| 1098 | + .filter((turn) => !isDeliveredProviderTurn(turn)) |
| 1099 | + .map((turn) => turn.runAttemptId), |
| 1100 | + ); |
1095 | 1101 | const acceptedAttempts = projection.attempts.filter( |
1096 | 1102 | (source) => |
1097 | 1103 | source.providerThreadId === providerThread.id && deliveredAttemptIds.has(source.id), |
@@ -1162,7 +1168,9 @@ export const layer: Layer.Layer< |
1162 | 1168 | (source) => |
1163 | 1169 | source.ordinal < run.ordinal && |
1164 | 1170 | source.providerThreadId === providerThread.id && |
1165 | | - (source.status === "failed" || source.status === "interrupted") && |
| 1171 | + (source.status === "failed" || |
| 1172 | + source.status === "interrupted" || |
| 1173 | + (source.status === "cancelled" && droppedAttemptIds.has(source.activeAttemptId))) && |
1166 | 1174 | !deliveredAttemptIds.has(source.activeAttemptId), |
1167 | 1175 | ); |
1168 | 1176 | const missedRunIds = new Set(missedRuns.map((source) => source.id)); |
@@ -2034,6 +2042,7 @@ export const layer: Layer.Layer< |
2034 | 2042 | }); |
2035 | 2043 | let inlineDeliveredHandoffs: ReadonlyArray<OrchestrationV2ContextHandoff> = []; |
2036 | 2044 | let promptDroppedOnTerminal = false; |
| 2045 | + const inlineDeliveryLock = yield* Semaphore.make(1); |
2037 | 2046 | const retryHandoffIds = new Set(retryHandoff.handoffs.map((handoff) => handoff.id)); |
2038 | 2047 | const startWithHandoffs = Effect.fn("ProviderTurnStartService.startWithHandoffs")( |
2039 | 2048 | function* (turnInput: Parameters<typeof session.startTurn>[0], compact = false) { |
@@ -2120,32 +2129,39 @@ export const layer: Layer.Layer< |
2120 | 2129 | : Effect.void, |
2121 | 2130 | ), |
2122 | 2131 | ); |
2123 | | - if (promptDroppedOnTerminal) return; |
2124 | | - // After provider acceptance, failed delivery bookkeeping must not stop live ingestion. |
2125 | | - const nativeThreadId = runningProviderThread.nativeThreadRef?.nativeId; |
2126 | | - yield* Effect.forEach( |
2127 | | - [ |
2128 | | - delivery.delivered, |
2129 | | - ...(sideChatContext === "" || sideChatHandoff === undefined || nativeThreadId == null |
2130 | | - ? [] |
2131 | | - : [ |
2132 | | - persistHandoff({ |
2133 | | - ...sideChatHandoff, |
2134 | | - toProviderThreadId: runningProviderThread.id, |
2135 | | - delivery: { nativeThreadId, status: "inline" as const, itemIds: [] }, |
2136 | | - }), |
2137 | | - ]), |
2138 | | - ], |
2139 | | - (record) => |
2140 | | - record.pipe( |
2141 | | - Effect.catchCause(() => |
2142 | | - Effect.logWarning("Failed to record accepted context handoff delivery", { |
2143 | | - runId: run.id, |
2144 | | - deliveryStatus: "pending", |
2145 | | - }), |
2146 | | - ), |
2147 | | - ), |
2148 | | - { discard: true }, |
| 2132 | + // Serialize acceptance writes against dropped-terminal restoration so neither overwrites the other. |
| 2133 | + yield* inlineDeliveryLock.withPermits(1)( |
| 2134 | + Effect.gen(function* () { |
| 2135 | + if (promptDroppedOnTerminal) return; |
| 2136 | + // After provider acceptance, failed delivery bookkeeping must not stop live ingestion. |
| 2137 | + const nativeThreadId = runningProviderThread.nativeThreadRef?.nativeId; |
| 2138 | + yield* Effect.forEach( |
| 2139 | + [ |
| 2140 | + delivery.delivered, |
| 2141 | + ...(sideChatContext === "" || |
| 2142 | + sideChatHandoff === undefined || |
| 2143 | + nativeThreadId == null |
| 2144 | + ? [] |
| 2145 | + : [ |
| 2146 | + persistHandoff({ |
| 2147 | + ...sideChatHandoff, |
| 2148 | + toProviderThreadId: runningProviderThread.id, |
| 2149 | + delivery: { nativeThreadId, status: "inline" as const, itemIds: [] }, |
| 2150 | + }), |
| 2151 | + ]), |
| 2152 | + ], |
| 2153 | + (record) => |
| 2154 | + record.pipe( |
| 2155 | + Effect.catchCause(() => |
| 2156 | + Effect.logWarning("Failed to record accepted context handoff delivery", { |
| 2157 | + runId: run.id, |
| 2158 | + deliveryStatus: "pending", |
| 2159 | + }), |
| 2160 | + ), |
| 2161 | + ), |
| 2162 | + { discard: true }, |
| 2163 | + ); |
| 2164 | + }), |
2149 | 2165 | ); |
2150 | 2166 | }, |
2151 | 2167 | Effect.mapError((cause) => |
@@ -2206,10 +2222,13 @@ export const layer: Layer.Layer< |
2206 | 2222 | hasUnpairedRunInterruptRequest: runControls.hasUnpairedRunInterruptRequest, |
2207 | 2223 | loadRunInterruptReason: runControls.loadRunInterruptReason, |
2208 | 2224 | hasWorkspaceMoved: runControls.hasWorkspaceMoved, |
2209 | | - loadDroppedInlineHandoffs: () => { |
2210 | | - promptDroppedOnTerminal = true; |
2211 | | - return inlineDeliveredHandoffs; |
2212 | | - }, |
| 2225 | + loadDroppedInlineHandoffs: () => |
| 2226 | + inlineDeliveryLock.withPermits(1)( |
| 2227 | + Effect.sync(() => { |
| 2228 | + promptDroppedOnTerminal = true; |
| 2229 | + return inlineDeliveredHandoffs; |
| 2230 | + }), |
| 2231 | + ), |
2213 | 2232 | loadUnsettledWork: runControls.loadUnsettledWork, |
2214 | 2233 | message: { |
2215 | 2234 | messageId: message.id, |
|