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, 07:45 UTC
fix(server): give completed turns a full session idle window (#10689)
Wout Stiens committed
Sep 8, 2026 at 09:27 UTC
430fbd1ffc98378f6a6a41027ddc6004a79b2e7b
2 files changed
+139
-43
apps/server/src/provider/Layers/ProviderSessionReaper.test.ts
+127
-41
@@ -7,7 +7,7 @@ import {
7
ProviderInstanceId,
8
} from "@t3tools/contracts";
9
import * as Clock from "effect/Clock";
10
-import * as DateTime from "effect/DateTime";
10
+import * as Deferred from "effect/Deferred";
11
import * as Effect from "effect/Effect";
12
import * as Exit from "effect/Exit";
13
import * as Layer from "effect/Layer";
@@ -140,9 +140,36 @@ describe("ProviderSessionReaper", () => {
140
// Shared start sequence so each test adds no manual Effect runners
141
// (no-manual-effect-runtime-in-tests tracks this file's legacy count).
142
async function startReaper() {
143
- const reaper = await runtime!.runPromise(Effect.service(ProviderSessionReaper));
144
- scope = await Effect.runPromise(Scope.make("sequential"));
145
- await Effect.runPromise(reaper.start().pipe(Scope.provide(scope)));
143
+ await runtime!.runPromise(
144
+ Effect.gen(function* () {
145
+ const reaper = yield* ProviderSessionReaper;
146
+ scope = yield* Scope.make("sequential");
147
+ yield* reaper.start().pipe(Scope.provide(scope));
148
+ }),
149
+ );
150
+ }
151
+
152
+ async function sweepAt(nowMs: number) {
153
+ await runtime!.runPromise(
154
+ Effect.gen(function* () {
155
+ const reaper = yield* ProviderSessionReaper;
156
+ const clock = yield* Clock.Clock;
157
+ const swept = yield* Deferred.make<void>();
158
+ yield* reaper.start().pipe(
159
+ Effect.provideService(Clock.Clock, {
160
+ currentTimeMillis: Effect.succeed(nowMs),
161
+ currentTimeMillisUnsafe: () => nowMs,
162
+ currentTimeNanos: Effect.succeed(BigInt(nowMs) * 1_000_000n),
163
+ currentTimeNanosUnsafe: () => BigInt(nowMs) * 1_000_000n,
164
+ monotonicTimeNanos: clock.monotonicTimeNanos,
165
+ monotonicTimeNanosUnsafe: () => clock.monotonicTimeNanosUnsafe(),
166
+ // Reaching the next scheduled sleep proves this sweep has finished.
167
+ sleep: () => Deferred.succeed(swept, undefined).pipe(Effect.andThen(Effect.never)),
168
+ }),
169
+ );
170
+ yield* Deferred.await(swept);
171
+ }).pipe(Effect.scoped),
172
+ );
173
}
174
175
async function createHarness(input: {
@@ -383,52 +410,111 @@ describe("ProviderSessionReaper", () => {
410
expect(Option.isSome(remaining)).toBe(true);
411
});
412
386
- it("does not reap sessions that are still within the inactivity threshold", async () => {
387
- const threadId = ThreadId.make("thread-reaper-fresh");
388
- const now = DateTime.formatIso(await Effect.runPromise(DateTime.now));
389
- const harness = await createHarness({
390
- readModel: makeReadModel([
413
+ it.each(["ready", "interrupted", "error"] as const)(
414
+ "gives a long turn a full idle window after becoming %s",
415
+ async (status) => {
416
+ const threadId = ThreadId.make(`thread-reaper-long-turn-${status}`);
417
+ const startedAt = "2026-04-14T00:00:00.000Z";
418
+ const completedAt = "2026-04-14T01:00:00.000Z";
419
+ const completedAtMs = Date.parse(completedAt);
420
+ const readModel = makeReadModel([
421
{
422
id: threadId,
423
session: {
424
threadId,
395
- status: "ready",
425
+ status: "running",
426
providerName: "claudeAgent",
427
runtimeMode: "full-access",
398
- activeTurnId: null,
428
+ activeTurnId: TurnId.make("turn-reaper-long"),
429
lastError: null,
400
- updatedAt: now,
430
+ updatedAt: startedAt,
431
},
432
},
403
- ]),
404
- });
405
- const repository = await runtime!.runPromise(
406
- Effect.service(ProviderSessionRuntime.ProviderSessionRuntimeRepository),
407
- );
408
-
409
- await runtime!.runPromise(
410
- repository.upsert({
411
- threadId,
412
- providerName: "claudeAgent",
413
- providerInstanceId: null,
414
- adapterKey: "claudeAgent",
415
- runtimeMode: "full-access",
416
- status: "running",
417
- lastSeenAt: now,
418
- resumeCursor: {
419
- opaque: "resume-fresh",
420
- },
421
- runtimePayload: null,
422
- }),
423
- );
424
-
425
- await startReaper();
426
- await Effect.runPromise(drainFibers);
427
-
428
- expect(harness.stopSession).not.toHaveBeenCalled();
429
- const remaining = await runtime!.runPromise(repository.getByThreadId({ threadId }));
430
- expect(Option.isSome(remaining)).toBe(true);
431
- });
433
+ ]);
434
+ const harness = await createHarness({ readModel });
435
+ const repository = await runtime!.runPromise(
436
+ Effect.service(ProviderSessionRuntime.ProviderSessionRuntimeRepository),
437
+ );
438
+ await runtime!.runPromise(
439
+ repository.upsert({
440
+ threadId,
441
+ providerName: "claudeAgent",
442
+ providerInstanceId: null,
443
+ adapterKey: "claudeAgent",
444
+ runtimeMode: "full-access",
445
+ status: "running",
446
+ lastSeenAt: startedAt,
447
+ resumeCursor: { opaque: "resume-long-turn" },
448
+ runtimePayload: null,
449
+ }),
450
+ );
451
+
452
+ await sweepAt(completedAtMs);
453
+ expect(harness.stopSession).not.toHaveBeenCalled();
454
+
455
+ // Ingestion changes the timestamp and clears the active turn in the same session row.
456
+ readModel.threads[0]!.session = {
457
+ ...readModel.threads[0]!.session!,
458
+ status,
459
+ activeTurnId: null,
460
+ updatedAt: completedAt,
461
+ };
462
+ await sweepAt(completedAtMs);
463
+ expect(harness.stopSession).not.toHaveBeenCalled();
464
+ await sweepAt(completedAtMs + 999);
465
+ expect(harness.stopSession).not.toHaveBeenCalled();
466
+ await sweepAt(completedAtMs + 1_000);
467
+ expect(harness.stopSession).toHaveBeenCalledExactlyOnceWith({ threadId });
468
+ },
469
+ );
470
+
471
+ it.each([true, false])(
472
+ "uses the binding idle window when the session is older or missing, hasSession=%s",
473
+ async (hasSession) => {
474
+ const threadId = ThreadId.make("thread-reaper-fresh");
475
+ const now = "2026-04-14T01:00:00.000Z";
476
+ const nowMs = Date.parse(now);
477
+ const harness = await createHarness({
478
+ readModel: makeReadModel([
479
+ {
480
+ id: threadId,
481
+ session: hasSession
482
+ ? {
483
+ threadId,
484
+ status: "ready",
485
+ providerName: "claudeAgent",
486
+ runtimeMode: "full-access",
487
+ activeTurnId: null,
488
+ lastError: null,
489
+ updatedAt: "2026-04-14T00:00:00.000Z",
490
+ }
491
+ : null,
492
+ },
493
+ ]),
494
+ });
495
+ const repository = await runtime!.runPromise(
496
+ Effect.service(ProviderSessionRuntime.ProviderSessionRuntimeRepository),
497
+ );
498
+ await runtime!.runPromise(
499
+ repository.upsert({
500
+ threadId,
501
+ providerName: "claudeAgent",
502
+ providerInstanceId: null,
503
+ adapterKey: "claudeAgent",
504
+ runtimeMode: "full-access",
505
+ status: "running",
506
+ lastSeenAt: now,
507
+ resumeCursor: { opaque: "resume-fresh" },
508
+ runtimePayload: null,
509
+ }),
510
+ );
511
+
512
+ await sweepAt(nowMs + 999);
513
+ expect(harness.stopSession).not.toHaveBeenCalled();
514
+ await sweepAt(nowMs + 1_000);
515
+ expect(harness.stopSession).toHaveBeenCalledExactlyOnceWith({ threadId });
516
+ },
517
+ );
518
519
it("skips persisted sessions that are already marked stopped", async () => {
520
const threadId = ThreadId.make("thread-reaper-stopped");
apps/server/src/provider/Layers/ProviderSessionReaper.ts
+12
-2
@@ -54,14 +54,24 @@ const makeProviderSessionReaper = (options?: ProviderSessionReaperLiveOptions) =
54
continue;
55
}
56
57
- const idleDurationMs = now - lastSeenMs;
58
- if (idleDurationMs < inactivityThresholdMs) {
57
+ if (now - lastSeenMs < inactivityThresholdMs) {
58
continue;
59
}
60
61
const thread = yield* projectionSnapshotQuery
62
.getThreadShellById(binding.threadId)
63
.pipe(Effect.map(Option.getOrUndefined));
64
+ // Ingestion updates this timestamp alongside activeTurnId when a turn
65
+ // settles. Long turns must get a full idle window after that transition,
66
+ // even though the binding was last touched when the turn was sent.
67
+ const lastActivityMs = Math.max(
68
+ lastSeenMs,
69
+ Date.parse(thread?.session?.updatedAt ?? binding.lastSeenAt),
70
+ );
71
+ const idleDurationMs = now - lastActivityMs;
72
+ if (idleDurationMs < inactivityThresholdMs) {
73
+ continue;
74
+ }
75
if (thread?.session?.activeTurnId != null) {
76
yield* Effect.logDebug("provider.session.reaper.skipped-active-turn", {
77
threadId: binding.threadId,