The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 19:45 UTC
fix(server): Stop interrupts a provider turn that starts while it lands (#34912)
Julius Marminge committed
Nov 12, 2026 at 00:21 UTC
2de337003dd8dec7d33d117175bca654fa2a006f
4 files changed
+113
-1
apps/server/src/orchestration-v2/RunExecutionService.test.ts
+66
@@ -60,6 +60,7 @@ import * as McpAppModelContext from "../mcpApps/McpAppModelContext.ts";
60
import * as PreviewAgentActivity from "../preview/PreviewAgentActivity.ts";
61
import * as RunExecutionService from "./RunExecutionService.ts";
62
import * as RunFinalizationService from "./RunFinalizationService.ts";
63
+import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
64
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
65
66
const mockEventSink = (implementation: Partial<EventSink.EventSinkV2["Service"]>) =>
@@ -93,6 +94,7 @@ const layerRunExecutionTest = RunExecutionService.layer.pipe(
94
layerTurnItemPositionsTest,
95
mockEventSink({}),
96
IdAllocator.layer,
97
+ ThreadCommandExecutor.layer,
98
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
99
ingestNormalized: () => Effect.succeed([]),
100
}),
@@ -920,6 +922,7 @@ it.effect("passes the thread's MCP app context to the provider under a safe key"
922
layerTurnItemPositionsTest,
923
mockEventSink({}),
924
IdAllocator.layer,
925
+ ThreadCommandExecutor.layer,
926
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
927
ingestNormalized: () => Effect.succeed([]),
928
}),
@@ -954,6 +957,7 @@ it.effect("fails the run when its ownership check cannot be read before calling
957
),
958
}),
959
IdAllocator.layer,
960
+ ThreadCommandExecutor.layer,
961
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
962
ingestNormalized: () => Effect.succeed([]),
963
}),
@@ -1166,6 +1170,7 @@ it.effect("starts the provider when checkpoint baseline capture fails", () =>
1170
]).pipe(Effect.as([])),
1171
}),
1172
IdAllocator.layer,
1173
+ ThreadCommandExecutor.layer,
1174
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
1175
ingestNormalized: () => Effect.succeed([]),
1176
}),
@@ -1288,6 +1293,7 @@ it.effect.each(["failure", "interruption", "stale-attempt", "start-guard"] as co
1293
}),
1294
}),
1295
IdAllocator.layer,
1296
+ ThreadCommandExecutor.layer,
1297
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
1298
ingestNormalized: () => Effect.succeed([]),
1299
}),
@@ -1468,6 +1474,7 @@ it.effect("ingests new member threads of an owned child after the root turn term
1474
writeIfRunCurrent: () => Effect.succeed({ committed: true, storedEvents: [] }),
1475
}),
1476
IdAllocator.layer,
1477
+ ThreadCommandExecutor.layer,
1478
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
1479
ingestNormalized: (input) =>
1480
Effect.gen(function* () {
@@ -1723,6 +1730,7 @@ it.effect("ends a nested native subagent and its thread when the root run is int
1730
),
1731
}),
1732
IdAllocator.layer,
1733
+ ThreadCommandExecutor.layer,
1734
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
1735
ingestNormalized: () => Effect.succeed([]),
1736
}),
@@ -2169,6 +2177,7 @@ it.effect(
2177
writeIfRunCurrent: () => Effect.succeed({ committed: true, storedEvents: [] }),
2178
}),
2179
IdAllocator.layer,
2180
+ ThreadCommandExecutor.layer,
2181
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
2182
ingestNormalized: (input) =>
2183
Ref.update(ingested, (current) => [...current, input.event]).pipe(Effect.as([])),
@@ -2605,6 +2614,7 @@ it.effect("settles native subagents when a workspace handoff completes the root
2614
),
2615
}),
2616
IdAllocator.layer,
2617
+ ThreadCommandExecutor.layer,
2618
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
2619
ingestNormalized: () => Effect.succeed([]),
2620
}),
@@ -3204,6 +3214,60 @@ it.effect("does not interrupt a provider when ingestion fails before the root tu
3214
}),
3215
);
3216
3217
+it.effect("interrupts a provider turn that starts after Stop settled its attempt", () =>
3218
+ Effect.gen(function* () {
3219
+ const now = yield* DateTime.now;
3220
+ const key = "turn-after-stop";
3221
+ const ids = backgroundScenarioIds(key);
3222
+ const stopped = yield* Ref.make(false);
3223
+ const accepted = yield* Deferred.make<void>();
3224
+ const interrupted = yield* Deferred.make<ProviderAdapter.ProviderAdapterV2InterruptInput>();
3225
+ yield* captureRootRunTermination({
3226
+ key,
3227
+ initialProviderTurnId: null,
3228
+ shouldFinalizeRun: () => Ref.get(stopped).pipe(Effect.map((value) => !value)),
3229
+ shouldStartProviderTurn: () => Ref.get(stopped).pipe(Effect.map((value) => !value)),
3230
+ // Stop commits while the provider accepts the prompt.
3231
+ startTurn: () =>
3232
+ Ref.set(stopped, true).pipe(Effect.andThen(Deferred.succeed(accepted, undefined))),
3233
+ events: () =>
3234
+ Stream.unwrap(
3235
+ Deferred.await(accepted).pipe(
3236
+ Effect.as(
3237
+ Stream.make({
3238
+ type: "provider_turn.updated" as const,
3239
+ driver,
3240
+ providerTurn: {
3241
+ id: ids.rootProviderTurnId,
3242
+ providerThreadId: ids.providerThreadId,
3243
+ nodeId: ids.rootNodeId,
3244
+ runAttemptId: ids.attemptId,
3245
+ nativeTurnRef: null,
3246
+ ordinal: 1,
3247
+ status: "running" as const,
3248
+ startedAt: now,
3249
+ completedAt: null,
3250
+ },
3251
+ }).pipe(
3252
+ Stream.concat(
3253
+ Stream.fromEffect(
3254
+ Deferred.await(interrupted).pipe(
3255
+ Effect.as(rootTerminalEvent(ids, "interrupted")),
3256
+ ),
3257
+ ),
3258
+ ),
3259
+ ),
3260
+ ),
3261
+ ),
3262
+ ),
3263
+ interruptTurn: (input) => Deferred.succeed(interrupted, input).pipe(Effect.asVoid),
3264
+ });
3265
+ const call = yield* Deferred.await(interrupted);
3266
+ assert.equal(call.providerTurnId, ids.rootProviderTurnId);
3267
+ assert.equal(call.providerThread.id, ids.providerThreadId);
3268
+ }),
3269
+);
3270
+
3271
// The server browser keeps an agent's tab focused for exactly this span.
3272
it.effect.each(["completed", "failed"] as const)(
3273
"reports the thread executing until its %s run's event consumer ends",
@@ -3463,6 +3527,7 @@ function captureRootRunTermination(input: {
3527
}),
3528
}),
3529
IdAllocator.layer,
3530
+ ThreadCommandExecutor.layer,
3531
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
3532
ingestNormalized: input.ingestNormalized ?? (() => Effect.succeed([])),
3533
}),
@@ -4001,6 +4066,7 @@ function runBackgroundItemScenario(
4066
writeIfRunCurrent: () => Effect.succeed({ committed: true, storedEvents: [] }),
4067
}),
4068
IdAllocator.layer,
4069
+ ThreadCommandExecutor.layer,
4070
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
4071
ingestNormalized: (input) => {
4072
const write = Effect.gen(function* () {
apps/server/src/orchestration-v2/RunExecutionService.ts
+45
-1
@@ -55,6 +55,7 @@ import {
55
makeProviderFailureTurnItem,
56
} from "@t3tools/provider-core/server/failure";
57
import * as RunFinalizationService from "./RunFinalizationService.ts";
58
+import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
59
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
60
import { userFacingDatabaseErrorMessage } from "./UserFacingErrors.ts";
61
@@ -614,6 +615,7 @@ export const layer: Layer.Layer<
615
| McpAppModelContext.McpAppModelContext
616
| PreviewAgentActivity.PreviewAgentActivity
617
| TurnItemPositionStore.TurnItemPositionStoreV2
618
+ | ThreadCommandExecutor.ThreadCommandExecutor
619
> = Layer.effect(
620
RunExecutionServiceV2,
621
Effect.gen(function* () {
@@ -628,6 +630,7 @@ export const layer: Layer.Layer<
630
const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
631
const finalizationObserver = yield* RunFinalizationService.RunFinalizationObserver;
632
const turnAnalytics = yield* ProviderUsageAnalytics.ProviderTurnAnalytics;
633
+ const threadCommands = yield* ThreadCommandExecutor.ThreadCommandExecutor;
634
635
const writeFinalRunEvents = Effect.fn("RunExecutionService.writeFinalRunEvents")(
636
function* (input: {
@@ -1104,6 +1107,7 @@ export const layer: Layer.Layer<
1107
const openRunOwnedSubagents = yield* Ref.make(emptyOpenRunOwnedSubagentProjection());
1108
// The last stored root snapshot's pendingBackgroundWork; adapters that omit it leave it false.
1109
let rootSnapshotPendingWork = false;
1110
+ let rootTurnOwnershipChecked = false;
1111
const assistantStreaming = makeAssistantStreamingFilter(responseStreamingMode);
1112
// Set when ingestion ends or finalization settles the run's open items, which a later
1113
// streaming snapshot must not reopen.
@@ -1589,7 +1593,47 @@ export const layer: Layer.Layer<
1593
event,
1594
DateTime.toEpochMillis(yield* DateTime.now),
1595
);
1592
- if (deliveredEvent) {
1596
+ const startedRootTurn =
1597
+ event.type === "provider_turn.updated" &&
1598
+ event.providerTurn.runAttemptId === input.attempt.id &&
1599
+ event.providerTurn.providerThreadId === input.providerThread.id &&
1600
+ !isTerminalProviderTurnStatus(event.providerTurn.status) &&
1601
+ !rootTurnOwnershipChecked
1602
+ ? event.providerTurn
1603
+ : undefined;
1604
+ const shouldStartProviderTurn = input.shouldStartProviderTurn;
1605
+ if (deliveredEvent && startedRootTurn !== undefined && shouldStartProviderTurn) {
1606
+ rootTurnOwnershipChecked = true;
1607
+ // Stop plans under this lock from stored turns. Storing the first turn and rechecking
1608
+ // the attempt under it means Stop either saw the turn or already settled the run.
1609
+ const [stored, owned] = yield* threadCommands.withLock(
1610
+ input.run.threadId,
1611
+ Effect.all([
1612
+ ingestDelivered(event, deliveredEvent),
1613
+ shouldStartProviderTurn().pipe(Effect.orElseSucceed(() => true)),
1614
+ ]),
1615
+ );
1616
+ storedEventCount = stored;
1617
+ if (!owned) {
1618
+ yield* Ref.get(latestProviderThread).pipe(
1619
+ Effect.flatMap((providerThread) =>
1620
+ input.session.interruptTurn({
1621
+ providerThread,
1622
+ providerTurnId: startedRootTurn.id,
1623
+ }),
1624
+ ),
1625
+ Effect.catchCauseIf(
1626
+ (cause) => !Cause.hasInterruptsOnly(cause),
1627
+ (cause) =>
1628
+ Effect.logWarning(
1629
+ "orchestration V2 failed to interrupt a provider turn that started after Stop",
1630
+ { runId: input.run.id, cause },
1631
+ ),
1632
+ ),
1633
+ Effect.forkIn(scope),
1634
+ );
1635
+ }
1636
+ } else if (deliveredEvent) {
1637
storedEventCount = yield* ingestDelivered(event, deliveredEvent);
1638
} else if (
1639
event.type === "turn_item.updated" &&
apps/server/src/orchestration-v2/runtimeLayer.ts
+1
@@ -160,6 +160,7 @@ const layerRunExecutionServiceProvided = RunExecutionService.layer.pipe(
160
IdAllocator.layer,
161
layerProviderEventIngestorProvided,
162
TurnItemPositionStore.layer,
163
+ ThreadCommandExecutor.layer,
164
),
165
),
166
);
apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts
+1
@@ -429,6 +429,7 @@ export function layerWithRegistry<Error>(
429
layerProviderEventIngestorProvided,
430
layerServerSettings,
431
layerStores,
432
+ ThreadCommandExecutor.layer,
433
),
434
),
435
);