The best way to control Claude Code, Codex and more from any computer or phone
fix(server): fail runs whose provider stream ends without a terminal (#19131)
Co-authored-by: GPT-6.1-Sol at high effort <noreply@openai.com> Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alb11747 committed
Oct 15, 2026 at 13:47 UTC
e77a48e81a6aaea575b0c7672ac2134cd6ee04aa
4 files changed
+161
-76
apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
+5
-2
@@ -1746,7 +1746,7 @@ it.effect("ProviderSessionManagerV2 releases sessions in parallel on server shut
1746
}),
1747
);
1748
1749
-it.effect("ProviderSessionManagerV2 closes event subscriptions normally on server shutdown", () =>
1749
+it.effect("ProviderSessionManagerV2 interrupts event subscriptions on server shutdown", () =>
1750
Effect.gen(function* () {
1751
const state = yield* Ref.make(emptyState);
1752
const effect = Effect.gen(function* () {
@@ -1781,7 +1781,10 @@ it.effect("ProviderSessionManagerV2 closes event subscriptions normally on serve
1781
1782
yield* manager.shutdown;
1783
1784
- assert.isEmpty(yield* bufferedSubscription.events.pipe(Stream.runCollect));
1784
+ // Buffered events are dropped and the consumer sees an interruption, not
1785
+ // a clean end, so it leaves run settlement to restart recovery.
1786
+ const exit = yield* bufferedSubscription.events.pipe(Stream.runCollect, Effect.exit);
1787
+ assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause));
1788
});
1789
1790
yield* effect.pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 60_000 })));
apps/server/src/orchestration-v2/ProviderSessionManager.ts
+17
-12
@@ -593,12 +593,15 @@ export const layerWithOptions = (
593
);
594
});
595
596
- const closeSubscribers = (entry: LiveSessionEntry) =>
596
+ // Server shutdown interrupts subscribers rather than ending them: a
597
+ // cleanly ended stream without a terminal fails its run, while an
598
+ // interrupted one leaves run settlement to restart recovery.
599
+ const interruptSubscribers = (entry: LiveSessionEntry) =>
600
Effect.gen(function* () {
601
const subscribers = yield* Ref.getAndSet(entry.eventSubscribers, new Map());
602
yield* Effect.forEach(
603
subscribers.values(),
601
- (queue) => Queue.clear(queue).pipe(Effect.andThen(Queue.end(queue))),
604
+ (queue) => Queue.clear(queue).pipe(Effect.andThen(Queue.interrupt(queue))),
605
{ discard: true },
606
);
607
});
@@ -1078,7 +1081,7 @@ export const layerWithOptions = (
1081
if (input.gracefulSubscribers === true) {
1082
yield* endSubscribers(entry);
1083
} else if (input.reason === "server_shutdown") {
1081
- yield* closeSubscribers(entry);
1084
+ yield* interruptSubscribers(entry);
1085
} else {
1086
yield* failSubscribers(
1087
entry,
@@ -2096,15 +2099,17 @@ export const layerWithOptions = (
2099
}).pipe(logReleaseFailure(entry.runtime.providerSessionId));
2100
return;
2101
}
2099
- const cause = Exit.isFailure(exit)
2100
- ? exit.cause
2101
- : Cause.fail(
2102
- new ProviderAdapter.ProviderAdapterEventStreamError({
2103
- driver: entry.runtime.driver,
2104
- providerSessionId: entry.runtime.providerSessionId,
2105
- cause: "Provider event stream ended unexpectedly.",
2106
- }),
2107
- );
2102
+ // Subscribers read an interrupt as server shutdown, so keep it for that.
2103
+ const cause =
2104
+ Exit.isFailure(exit) && !Cause.hasInterruptsOnly(exit.cause)
2105
+ ? exit.cause
2106
+ : Cause.fail(
2107
+ new ProviderAdapter.ProviderAdapterEventStreamError({
2108
+ driver: entry.runtime.driver,
2109
+ providerSessionId: entry.runtime.providerSessionId,
2110
+ cause: "Provider event stream ended unexpectedly.",
2111
+ }),
2112
+ );
2113
yield* publishToSubscribers(entry.eventSubscribers, {
2114
type: "failure",
2115
cause,
apps/server/src/orchestration-v2/RunExecutionService.test.ts
+66
-2
@@ -3742,9 +3742,11 @@ it.effect("does not refresh pull requests for auxiliary or stale provider termin
3742
{ ...ids, rootProviderTurnId: ProviderTurnId.make("turn:previous") },
3743
"interrupted",
3744
),
3745
+ rootTerminalEvent(ids, "cancelled"),
3746
]),
3747
});
3747
- assert.deepEqual(observed, []);
3748
+ // Only the root terminal settles the run and refreshes pull requests.
3749
+ assert.deepEqual(observed, ["run:cancelled", "pull-requests-refreshed"]);
3750
}),
3751
);
3752
@@ -3772,6 +3774,58 @@ it.effect("refreshes pull requests after a provider stream exits with an error",
3774
}),
3775
);
3776
3777
+it.effect("fails the run when its event stream ends without a root terminal", () =>
3778
+ Effect.gen(function* () {
3779
+ const { observed, finalEvents } = yield* captureRootRunTermination({
3780
+ key: "stream-end-without-terminal",
3781
+ shouldFinalizeRun: () => Effect.succeed(true),
3782
+ events: () => Stream.empty,
3783
+ });
3784
+ assert.deepEqual(observed, ["run:failed", "pull-requests-refreshed"]);
3785
+ assert.equal(
3786
+ finalEvents.find((event) => event.type === "provider-thread.updated")?.payload.status,
3787
+ "idle",
3788
+ );
3789
+ }),
3790
+);
3791
+
3792
+it.effect("leaves the run to restart recovery when shutdown interrupts its event stream", () =>
3793
+ Effect.gen(function* () {
3794
+ const { written, observed } = yield* captureRootRunTermination({
3795
+ key: "stream-end:shutdown",
3796
+ shouldFinalizeRun: () => Effect.succeed(true),
3797
+ events: () => Stream.failCause(Cause.interrupt()),
3798
+ });
3799
+ assert.deepEqual(written, []);
3800
+ assert.deepEqual(observed, []);
3801
+ }),
3802
+);
3803
+
3804
+it.effect("does not start a turn whose run settled during the app context read", () =>
3805
+ Effect.gen(function* () {
3806
+ const contextReadStarted = yield* Deferred.make<void>();
3807
+ const runFailed = yield* Deferred.make<void>();
3808
+ const current = yield* Ref.make(true);
3809
+ const providerCalls = yield* Ref.make(0);
3810
+ const { observed } = yield* captureRootRunTermination({
3811
+ key: "stream-end:context-read",
3812
+ shouldStartProviderTurn: () => Ref.get(current),
3813
+ shouldFinalizeRun: () => Ref.get(current),
3814
+ events: () => Stream.unwrap(Deferred.await(contextReadStarted).pipe(Effect.as(Stream.empty))),
3815
+ appContext: Deferred.succeed(contextReadStarted, undefined).pipe(
3816
+ Effect.andThen(Deferred.await(runFailed)),
3817
+ Effect.as([]),
3818
+ ),
3819
+ refreshAfterTurn: Ref.set(current, false).pipe(
3820
+ Effect.andThen(Deferred.succeed(runFailed, undefined)),
3821
+ ),
3822
+ startTurn: () => Ref.update(providerCalls, (count) => count + 1),
3823
+ });
3824
+ assert.deepEqual(observed, ["run:failed", "pull-requests-refreshed"]);
3825
+ assert.equal(yield* Ref.get(providerCalls), 0);
3826
+ }),
3827
+);
3828
+
3829
it.effect("refreshes pull requests only once when startup failure closes its event stream", () =>
3830
Effect.gen(function* () {
3831
const ingestionStarted = yield* Deferred.make<void>();
@@ -3828,6 +3882,8 @@ function captureRootRunTermination(input: {
3882
>;
3883
readonly startTurn?: ProviderAdapter.ProviderAdapterV2SessionRuntime["startTurn"];
3884
readonly refreshAfterTurn?: Effect.Effect<void>;
3885
+ readonly shouldStartProviderTurn?: RunExecutionService.RunExecutionServiceV2StartRootRunInput["shouldStartProviderTurn"];
3886
+ readonly appContext?: ReturnType<McpAppModelContext.McpAppModelContext["Service"]["forThread"]>;
3887
}) {
3888
return Effect.gen(function* () {
3889
const ids = backgroundScenarioIds(input.key);
@@ -3840,6 +3896,7 @@ function captureRootRunTermination(input: {
3896
status: "running",
3897
});
3898
const writtenItems = yield* Ref.make<ReadonlyArray<OrchestrationV2TurnItem>>([]);
3899
+ const finalEvents = yield* Ref.make<ReadonlyArray<OrchestrationV2DomainEvent>>([]);
3900
const observed = yield* Ref.make<ReadonlyArray<string>>([]);
3901
const submittedEffects = yield* Ref.make<ReadonlyArray<PendingOrchestrationEffectV2>>([]);
3902
const committedEffects = yield* Ref.make<ReadonlyArray<PendingOrchestrationEffectV2>>([]);
@@ -3848,6 +3905,7 @@ function captureRootRunTermination(input: {
3905
Ref.update(writtenItems, (current) => [...current, payload]);
3906
const captureFinalEvents = (events: ReadonlyArray<OrchestrationV2DomainEvent>) =>
3907
Effect.gen(function* () {
3908
+ yield* Ref.update(finalEvents, (current) => [...current, ...events]);
3909
for (const event of events) {
3910
if (event.type === "turn-item.updated") {
3911
yield* captureTurnItem(event.payload);
@@ -3860,7 +3918,9 @@ function captureRootRunTermination(input: {
3918
const layerTest = RunExecutionService.layer.pipe(
3919
Layer.provide(
3920
Layer.mergeAll(
3863
- McpAppModelContext.layerEmpty,
3921
+ Layer.mock(McpAppModelContext.McpAppModelContext)({
3922
+ forThread: () => input.appContext ?? Effect.succeed([]),
3923
+ }),
3924
Layer.mock(CheckpointService.CheckpointServiceV2)({ captureBaseline: () => Effect.void }),
3925
Layer.mock(EventSink.EventSinkV2)({
3926
write: (payload) =>
@@ -3970,6 +4030,9 @@ function captureRootRunTermination(input: {
4030
attemptId: ids.attemptId,
4031
providerTurnOrdinal: 1,
4032
shouldFinalizeRun: input.shouldFinalizeRun,
4033
+ ...(input.shouldStartProviderTurn === undefined
4034
+ ? {}
4035
+ : { shouldStartProviderTurn: input.shouldStartProviderTurn }),
4036
...(input.hasUnpairedRunInterruptRequest === undefined
4037
? {}
4038
: {
@@ -4000,6 +4063,7 @@ function captureRootRunTermination(input: {
4063
yield* Deferred.await(ingestionDone);
4064
return {
4065
written: yield* Ref.get(writtenItems),
4066
+ finalEvents: yield* Ref.get(finalEvents),
4067
observed: yield* Ref.get(observed),
4068
submittedEffects: yield* Ref.get(submittedEffects),
4069
committedEffects: yield* Ref.get(committedEffects),
apps/server/src/orchestration-v2/RunExecutionService.ts
+73
-60
@@ -1316,83 +1316,85 @@ export const layer: Layer.Layer<
1316
Effect.gen(function* () {
1317
const terminal = yield* Ref.get(terminalEvent);
1318
if (terminal === null) {
1319
- return;
1319
+ // A session release interrupts or fails the stream; a clean
1320
+ // end without a terminal means the run would never settle.
1321
+ return yield* new RunExecutionIngestError({
1322
+ runId: input.run.id,
1323
+ cause: new ProviderAdapter.ProviderAdapterEventStreamError({
1324
+ driver: input.providerThread.driver,
1325
+ providerSessionId: input.providerSessionId,
1326
+ cause: "Provider event stream ended without a root terminal.",
1327
+ }),
1328
+ });
1329
}
1330
yield* finalizeRootRun(terminal);
1331
}),
1332
),
1324
- Effect.catchCause((cause) =>
1325
- Ref.get(rootRunFinalized).pipe(
1326
- Effect.flatMap((finalized) =>
1327
- Effect.logWarning("orchestration V2 provider event ingestion failed", {
1328
- runId: input.run.id,
1329
- cause,
1330
- }).pipe(
1331
- Effect.andThen(
1332
- finalized
1333
- ? Effect.void
1334
- : Ref.get(latestProviderThread).pipe(
1335
- Effect.flatMap((providerThread) =>
1336
- Ref.get(latestTurnItemOrdinal).pipe(
1337
- Effect.flatMap((latestItemOrdinal) =>
1338
- Ref.get(openRunOwnedSubagents).pipe(
1339
- Effect.flatMap((openSubagents) =>
1340
- writeFinalRunEvents({
1341
- run: input.run,
1342
- rootNode: input.rootNode,
1343
- checkpointScope: input.checkpointScope,
1344
- providerThread,
1345
- attempt: input.attempt,
1346
- // The failure may be the ownership
1347
- // read itself, so check in the write.
1348
- writeIfRunCurrent: {
1349
- activeAttemptId: input.attempt.id,
1350
- expectedStatus: "running",
1351
- },
1352
- openRunOwnedSubagents: openSubagents,
1353
- terminal: makeFailedTerminalEvent(
1354
- makeProviderFailure({
1355
- cause: Cause.squash(cause),
1356
- class: "unknown",
1357
- }),
1358
- latestItemOrdinal + 1,
1359
- ),
1360
- failureItemPersisted: false,
1361
- refreshAfterTurn,
1362
- }),
1333
+ // Server shutdown interrupts the subscription and leaves the run to
1334
+ // restart recovery; every other failure fails the run.
1335
+ Effect.catchCauseIf(
1336
+ (cause) => !Cause.hasInterruptsOnly(cause),
1337
+ (cause) =>
1338
+ Ref.get(rootRunFinalized).pipe(
1339
+ Effect.flatMap((finalized) =>
1340
+ Effect.logWarning("orchestration V2 provider event ingestion failed", {
1341
+ runId: input.run.id,
1342
+ cause,
1343
+ }).pipe(
1344
+ Effect.andThen(
1345
+ finalized
1346
+ ? Effect.void
1347
+ : Ref.get(latestProviderThread).pipe(
1348
+ Effect.flatMap((providerThread) =>
1349
+ Ref.get(latestTurnItemOrdinal).pipe(
1350
+ Effect.flatMap((latestItemOrdinal) =>
1351
+ Ref.get(openRunOwnedSubagents).pipe(
1352
+ Effect.flatMap((openSubagents) =>
1353
+ writeFinalRunEvents({
1354
+ run: input.run,
1355
+ rootNode: input.rootNode,
1356
+ checkpointScope: input.checkpointScope,
1357
+ providerThread,
1358
+ attempt: input.attempt,
1359
+ // The failure may be the ownership
1360
+ // read itself, so check in the write.
1361
+ writeIfRunCurrent: {
1362
+ activeAttemptId: input.attempt.id,
1363
+ expectedStatus: "running",
1364
+ },
1365
+ openRunOwnedSubagents: openSubagents,
1366
+ terminal: makeFailedTerminalEvent(
1367
+ makeProviderFailure({
1368
+ cause: Cause.squash(cause),
1369
+ class: "unknown",
1370
+ }),
1371
+ latestItemOrdinal + 1,
1372
+ ),
1373
+ failureItemPersisted: false,
1374
+ refreshAfterTurn,
1375
+ }),
1376
+ ),
1377
),
1378
),
1379
),
1380
),
1381
),
1368
- ),
1369
- ),
1370
- Effect.mapError(
1371
- (writeCause) =>
1372
- new RunExecutionIngestError({
1373
- runId: input.run.id,
1374
- cause: { ingest: cause, write: writeCause },
1375
- }),
1382
+ ),
1383
+ Effect.mapError(
1384
+ (writeCause) =>
1385
+ new RunExecutionIngestError({
1386
+ runId: input.run.id,
1387
+ cause: { ingest: cause, write: writeCause },
1388
+ }),
1389
+ ),
1390
),
1391
),
1392
),
1379
- ),
1393
),
1394
Effect.ensuring(eventSubscription.close),
1395
Effect.forkDetach,
1396
);
1397
1385
- // A failed read fails the start below, so the run is recorded as
1386
- // failed instead of staying active with no provider turn.
1387
- const shouldStart =
1388
- input.shouldStartProviderTurn === undefined
1389
- ? Exit.succeed(true)
1390
- : yield* Effect.exit(input.shouldStartProviderTurn());
1391
- if (Exit.isSuccess(shouldStart) && !shouldStart.value) {
1392
- yield* Fiber.interrupt(providerEventFiber);
1393
- return;
1394
- }
1395
-
1398
// A provider turn is a sign that the session is still alive. Keep
1399
// its already-issued MCP credential valid even when the agent goes
1400
// a long time between browser-tool calls.
@@ -1436,6 +1438,17 @@ export const layer: Layer.Layer<
1438
runtimePolicy: input.runtimePolicy,
1439
...(appContext.length === 0 ? {} : { appContext }),
1440
};
1441
+ // Check after the context read: ingestion can settle the run while
1442
+ // it awaits. A failed ownership read fails the start below.
1443
+ const shouldStart =
1444
+ input.shouldStartProviderTurn === undefined
1445
+ ? Exit.succeed(true)
1446
+ : yield* Effect.exit(input.shouldStartProviderTurn());
1447
+ if (Exit.isSuccess(shouldStart) && !shouldStart.value) {
1448
+ yield* Fiber.interrupt(providerEventFiber);
1449
+ return;
1450
+ }
1451
+
1452
const compact =
1453
input.message.attachments.length === 0 &&
1454
input.message.text.trim().toLowerCase() === "/compact";