@@ -154,46 +154,46 @@ export function abandonedProviderTurns( |
154 | 154 | const CUT_TURN_SETTLED = "cut-turn-settled"; |
155 | 155 |
|
156 | 156 | /** |
157 | | - * The previous run's cancelled or stopped turn when no adapter said whether it kept the request. |
158 | | - * A restart cancels the turn with no adapter left to tell. A Stop leaves its request with the |
159 | | - * session, which is gone once the session closes. |
| 157 | + * The latest cancelled or stopped turn when no adapter said whether it kept the request. A restart |
| 158 | + * cancels the turn with no adapter left to tell. A Stop leaves its request with the session, which |
| 159 | + * carries it into the next turn and is gone once the session closes. Later runs whose request the |
| 160 | + * provider never got are passed over: a stopped request they carried was lost with them. |
160 | 161 | */ |
161 | 162 | function unsettledCutProviderTurn( |
162 | 163 | projection: Pick<OrchestrationV2ThreadProjection, "runs" | "providerTurns">, |
163 | 164 | run: Pick<OrchestrationV2Run, "ordinal">, |
164 | 165 | providerThread: Pick<OrchestrationV2ProviderThread, "id" | "nativeThreadRef">, |
165 | 166 | ) { |
166 | | - const previous = projection.runs.reduce<OrchestrationV2Run | undefined>( |
167 | | - (latest, candidate) => |
168 | | - candidate.ordinal < run.ordinal && |
169 | | - candidate.providerThreadId === providerThread.id && |
170 | | - candidate.startedAt !== null && |
171 | | - (latest === undefined || candidate.ordinal > latest.ordinal) |
172 | | - ? candidate |
173 | | - : latest, |
174 | | - undefined, |
175 | | - ); |
176 | | - if ( |
177 | | - (previous?.status !== "cancelled" && previous?.status !== "interrupted") || |
178 | | - previous.activeAttemptId === null |
179 | | - ) |
180 | | - return undefined; |
181 | | - const turn = projection.providerTurns.findLast( |
182 | | - (candidate) => |
183 | | - candidate.runAttemptId === previous.activeAttemptId && |
184 | | - candidate.providerThreadId === providerThread.id, |
185 | | - ); |
186 | | - const nativeTurnId = turn?.nativeTurnRef?.nativeId; |
187 | | - if ( |
188 | | - turn === undefined || |
189 | | - nativeTurnId == null || |
190 | | - (turn.status !== "cancelled" && turn.status !== "interrupted") || |
191 | | - turn.promptDropped === true || |
192 | | - (turn.nativeThreadRef !== undefined && |
193 | | - turn.nativeThreadRef.nativeId !== providerThread.nativeThreadRef?.nativeId) |
194 | | - ) |
195 | | - return undefined; |
196 | | - return { run: previous, turn, nativeTurnId }; |
| 167 | + const previousRuns = projection.runs |
| 168 | + .filter( |
| 169 | + (candidate) => |
| 170 | + candidate.ordinal < run.ordinal && |
| 171 | + candidate.providerThreadId === providerThread.id && |
| 172 | + candidate.startedAt !== null, |
| 173 | + ) |
| 174 | + .toSorted((left, right) => right.ordinal - left.ordinal); |
| 175 | + for (const previous of previousRuns) { |
| 176 | + const turn = |
| 177 | + previous.activeAttemptId === null |
| 178 | + ? undefined |
| 179 | + : projection.providerTurns.findLast( |
| 180 | + (candidate) => |
| 181 | + candidate.runAttemptId === previous.activeAttemptId && |
| 182 | + candidate.providerThreadId === providerThread.id, |
| 183 | + ); |
| 184 | + if (turn === undefined || !isDeliveredProviderTurn(turn)) continue; |
| 185 | + const nativeTurnId = turn.nativeTurnRef?.nativeId; |
| 186 | + if ( |
| 187 | + (previous.status !== "cancelled" && previous.status !== "interrupted") || |
| 188 | + nativeTurnId == null || |
| 189 | + (turn.status !== "cancelled" && turn.status !== "interrupted") || |
| 190 | + (turn.nativeThreadRef !== undefined && |
| 191 | + turn.nativeThreadRef.nativeId !== providerThread.nativeThreadRef?.nativeId) |
| 192 | + ) |
| 193 | + return undefined; |
| 194 | + return { run: previous, turn, nativeTurnId }; |
| 195 | + } |
| 196 | + return undefined; |
197 | 197 | } |
198 | 198 |
|
199 | 199 | export const isProviderTurnStartError = Schema.is(ProviderTurnStartError); |
@@ -1109,61 +1109,77 @@ export const layer: Layer.Layer< |
1109 | 1109 | const session = sessionResult.success; |
1110 | 1110 | if (!(yield* isCurrentAttemptInStatus("starting"))) return; |
1111 | 1111 | const cutTurn = unsettledCutProviderTurn(projection, run, providerThread); |
1112 | | - if ( |
1113 | | - cutTurn !== undefined && |
| 1112 | + // A cut turn the provider did not keep may itself have carried an earlier stopped request, |
| 1113 | + // which is lost with it, so keep asking back to a turn the provider kept. |
| 1114 | + const droppedCutTurns: Array<NonNullable<typeof cutTurn>> = []; |
| 1115 | + for ( |
| 1116 | + let candidate = cutTurn; |
| 1117 | + candidate !== undefined && |
1114 | 1118 | session.nativeTurnKept !== undefined && |
1115 | 1119 | !(yield* ( |
1116 | 1120 | session |
1117 | | - .nativeTurnKept({ providerThread, nativeTurnId: cutTurn.nativeTurnId }) |
| 1121 | + .nativeTurnKept({ providerThread, nativeTurnId: candidate.nativeTurnId }) |
1118 | 1122 | // As when the turn ends: an unknown answer resends the request rather than losing it. |
1119 | 1123 | .pipe(Effect.orElseSucceed(() => false)) |
1120 | | - )) |
| 1124 | + )); |
| 1125 | + candidate = unsettledCutProviderTurn(projection, candidate.run, providerThread) |
1121 | 1126 | ) { |
1122 | | - // Settle it as a failed turn is settled: mark the request dropped and take back its inline |
1123 | | - // context, so this turn resends both. Starting again reads the settled state. |
| 1127 | + droppedCutTurns.push(candidate); |
| 1128 | + } |
| 1129 | + const earliestCutTurn = droppedCutTurns.at(-1); |
| 1130 | + if (earliestCutTurn !== undefined) { |
| 1131 | + // Settle them as a failed turn is settled: mark each request dropped and take back their |
| 1132 | + // inline context, so this turn resends all of it. Starting again reads the settled state. |
1124 | 1133 | const occurredAt = yield* DateTime.now; |
1125 | 1134 | const allocateEventId = () => |
1126 | 1135 | idAllocator.allocate.event({ threadId: projection.thread.id }); |
1127 | 1136 | // Its attempt starts before it delivers anything, so later inline deliveries are its own, |
1128 | 1137 | // including handoffs carried from an earlier run that they still target. |
1129 | 1138 | const cutStartedAt = |
1130 | | - projection.attempts.find((candidate) => candidate.id === cutTurn.run.activeAttemptId) |
1131 | | - ?.startedAt ?? cutTurn.run.startedAt; |
| 1139 | + projection.attempts.find( |
| 1140 | + (candidate) => candidate.id === earliestCutTurn.run.activeAttemptId, |
| 1141 | + )?.startedAt ?? earliestCutTurn.run.startedAt; |
1132 | 1142 | const settled = yield* eventSink.writeIfRunCurrent({ |
1133 | 1143 | threadId: projection.thread.id, |
1134 | 1144 | runId: run.id, |
1135 | 1145 | activeAttemptId: attempt.id, |
1136 | 1146 | expectedStatus: "starting", |
1137 | 1147 | events: [ |
1138 | | - { |
1139 | | - id: yield* allocateEventId(), |
1140 | | - type: "provider-turn.updated" as const, |
1141 | | - threadId: projection.thread.id, |
1142 | | - runId: cutTurn.run.id, |
1143 | | - nodeId: cutTurn.turn.nodeId, |
1144 | | - providerInstanceId: cutTurn.run.providerInstanceId, |
1145 | | - occurredAt, |
1146 | | - payload: { ...cutTurn.turn, promptDropped: true as const }, |
1147 | | - }, |
| 1148 | + ...(yield* Effect.forEach(droppedCutTurns, (dropped) => |
| 1149 | + Effect.map(allocateEventId(), (id) => ({ |
| 1150 | + id, |
| 1151 | + type: "provider-turn.updated" as const, |
| 1152 | + threadId: projection.thread.id, |
| 1153 | + runId: dropped.run.id, |
| 1154 | + nodeId: dropped.turn.nodeId, |
| 1155 | + providerInstanceId: dropped.run.providerInstanceId, |
| 1156 | + occurredAt, |
| 1157 | + payload: { ...dropped.turn, promptDropped: true as const }, |
| 1158 | + })), |
| 1159 | + )), |
1148 | 1160 | ...(yield* Effect.forEach( |
1149 | 1161 | projection.contextHandoffs.filter( |
1150 | 1162 | (handoff) => |
1151 | 1163 | handoff.delivery?.status === "inline" && |
1152 | 1164 | handoff.delivery.nativeThreadId === providerThread.nativeThreadRef?.nativeId && |
1153 | | - (handoff.targetRunId === cutTurn.run.id || |
| 1165 | + (droppedCutTurns.some((dropped) => handoff.targetRunId === dropped.run.id) || |
1154 | 1166 | (cutStartedAt !== null && |
1155 | 1167 | DateTime.isGreaterThanOrEqualTo(handoff.updatedAt, cutStartedAt))), |
1156 | 1168 | ), |
1157 | | - ({ delivery: _delivery, ...handoff }) => |
1158 | | - Effect.map(allocateEventId(), (id) => ({ |
| 1169 | + ({ delivery: _delivery, ...handoff }) => { |
| 1170 | + const owner = |
| 1171 | + droppedCutTurns.find((dropped) => handoff.targetRunId === dropped.run.id) ?? |
| 1172 | + earliestCutTurn; |
| 1173 | + return Effect.map(allocateEventId(), (id) => ({ |
1159 | 1174 | id, |
1160 | 1175 | type: "context-handoff.updated" as const, |
1161 | 1176 | threadId: projection.thread.id, |
1162 | | - runId: cutTurn.run.id, |
1163 | | - providerInstanceId: cutTurn.run.providerInstanceId, |
| 1177 | + runId: owner.run.id, |
| 1178 | + providerInstanceId: owner.run.providerInstanceId, |
1164 | 1179 | occurredAt, |
1165 | 1180 | payload: { ...handoff, updatedAt: occurredAt }, |
1166 | | - })), |
| 1181 | + })); |
| 1182 | + }, |
1167 | 1183 | )), |
1168 | 1184 | ], |
1169 | 1185 | }); |
|