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 24, 20:15 UTC
[orchestration] Clear stale active turn when session becomes inactive (#3159)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Andrew Forster committed
Jul 20, 2026 at 02:04 UTC
501ce27b81826960dcbb9a93a6b3407efcf09366
2 files changed
+61
-6
apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
+47
@@ -448,6 +448,51 @@ describe("ProviderRuntimeIngestion", () => {
448
expect(thread.session?.lastError).toBeNull();
449
});
450
451
+ it("clears active turn when provider session becomes ready", async () => {
452
+ const harness = await createHarness();
453
+ const now = "2026-01-01T00:00:00.000Z";
454
+
455
+ harness.emit({
456
+ type: "turn.started",
457
+ eventId: asEventId("evt-turn-started-session-ready"),
458
+ provider: ProviderDriverKind.make("codex"),
459
+ createdAt: now,
460
+ threadId: asThreadId("thread-1"),
461
+ turnId: asTurnId("turn-session-ready"),
462
+ });
463
+
464
+ await waitForThread(
465
+ harness.readModel,
466
+ (thread) =>
467
+ thread.session?.status === "running" &&
468
+ thread.session?.activeTurnId === "turn-session-ready",
469
+ 10_000,
470
+ );
471
+
472
+ harness.emit({
473
+ type: "session.state.changed",
474
+ eventId: asEventId("evt-session-state-ready-with-active-turn"),
475
+ provider: ProviderDriverKind.make("codex"),
476
+ threadId: asThreadId("thread-1"),
477
+ createdAt: "2026-01-01T00:00:01.000Z",
478
+ payload: {
479
+ state: "ready",
480
+ },
481
+ });
482
+
483
+ const thread = await waitForThread(
484
+ harness.readModel,
485
+ (entry) =>
486
+ entry.session?.status === "ready" &&
487
+ entry.session?.activeTurnId === null &&
488
+ entry.session?.lastError === null,
489
+ 10_000,
490
+ );
491
+ expect(thread.session?.status).toBe("ready");
492
+ expect(thread.session?.activeTurnId).toBeNull();
493
+ expect(thread.session?.lastError).toBeNull();
494
+ });
495
+
496
it("does not clear active turn when session/thread started arrives mid-turn", async () => {
497
const harness = await createHarness();
498
const now = "2026-01-01T00:00:00.000Z";
@@ -466,6 +511,7 @@ describe("ProviderRuntimeIngestion", () => {
511
(thread) =>
512
thread.session?.status === "running" &&
513
thread.session?.activeTurnId === "turn-midturn-lifecycle",
514
+ 10_000,
515
);
516
517
harness.emit({
@@ -502,6 +548,7 @@ describe("ProviderRuntimeIngestion", () => {
548
await waitForThread(
549
harness.readModel,
550
(thread) => thread.session?.status === "ready" && thread.session?.activeTurnId === null,
551
+ 10_000,
552
);
553
});
554
apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
+14
-6
@@ -283,6 +283,12 @@ function orchestrationSessionStatusFromRuntimeState(
283
}
284
}
285
286
+function sessionStatusAllowsActiveTurn(
287
+ status: ReturnType<typeof orchestrationSessionStatusFromRuntimeState>,
288
+): boolean {
289
+ return status === "starting" || status === "running";
290
+}
291
+
292
function requestKindFromCanonicalRequestType(
293
requestType: string | undefined,
294
): "command" | "file-read" | "file-change" | undefined {
@@ -1362,12 +1368,6 @@ const make = Effect.gen(function* () {
1368
event.type === "turn.started" ||
1369
event.type === "turn.completed"
1370
) {
1365
- const nextActiveTurnId =
1366
- event.type === "turn.started"
1367
- ? (eventTurnId ?? null)
1368
- : event.type === "turn.completed" || event.type === "session.exited"
1369
- ? null
1370
- : activeTurnId;
1371
const status = (() => {
1372
switch (event.type) {
1373
case "session.state.changed":
@@ -1387,6 +1387,14 @@ const make = Effect.gen(function* () {
1387
return activeTurnId !== null ? "running" : "ready";
1388
}
1389
})();
1390
+ const nextActiveTurnId =
1391
+ event.type === "turn.started"
1392
+ ? (eventTurnId ?? null)
1393
+ : event.type === "turn.completed" || event.type === "session.exited"
1394
+ ? null
1395
+ : event.type === "session.state.changed" && !sessionStatusAllowsActiveTurn(status)
1396
+ ? null
1397
+ : activeTurnId;
1398
const lastError =
1399
event.type === "session.state.changed" && event.payload.state === "error"
1400
? (event.payload.reason ?? thread.session?.lastError ?? "Provider session error")