Skip to content

Commit 2f355d6

Browse files
fix(server): drop suspended time from idle waits at any overshoot
Wait in one-minute slices and discard slices that overran, so a suspension just past the idle deadline no longer releases the session on wake. AI-assisted; verified with the tests and tools listed above.
1 parent 3650558 commit 2f355d6

2 files changed

Lines changed: 44 additions & 25 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderSessionManager.test.ts‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,10 +3076,13 @@ it.effect(
30763076
}),
30773077
);
30783078

3079-
// Models the idle timer's view of two hours of host sleep, five awake minutes after open.
3080-
// Libuv's clock keeps counting while Windows and macOS sleep, so the timer fires on resume far past
3081-
// its due time; Linux pauses the timer and the monotonic clock while only wall time jumps.
3082-
const idleAcrossHostSleep = (input: { readonly timerCountsSleep: boolean }) =>
3079+
// Models the idle timer's view of host sleep starting awakeMs after open. Where the timer clock counts
3080+
// sleep, the timer fires on resume far past its due time; where it pauses, only wall time jumps.
3081+
const idleAcrossHostSleep = (input: {
3082+
readonly timerCountsSleep: boolean;
3083+
readonly awakeMs: number;
3084+
readonly sleepMs: number;
3085+
}) =>
30833086
Effect.gen(function* () {
30843087
const state = yield* Ref.make(emptyState);
30853088
const testClock = yield* TestClock.testClockWith(Effect.succeed);
@@ -3112,10 +3115,11 @@ const idleAcrossHostSleep = (input: { readonly timerCountsSleep: boolean }) =>
31123115
});
31133116
yield* manager.open({ threadId, providerSessionId, modelSelection, runtimePolicy });
31143117

3115-
yield* TestClock.adjust("5 minutes");
3116-
// A counting timer spends 25 of the two hours reaching its due time, then fires on resume.
3117-
sleptMs = input.timerCountsSleep ? 2 * 60 * 60_000 - 25 * 60_000 : 2 * 60 * 60_000;
3118-
yield* TestClock.adjust("25 minutes");
3118+
const untilDeadlineMs = 30 * 60_000 - input.awakeMs;
3119+
yield* TestClock.adjust(input.awakeMs);
3120+
// A counting timer spends part of the sleep reaching its due time, then fires on resume.
3121+
sleptMs = input.timerCountsSleep ? input.sleepMs - untilDeadlineMs : input.sleepMs;
3122+
yield* TestClock.adjust(untilDeadlineMs);
31193123
yield* Effect.yieldNow;
31203124
const releasedAtFirstDeadline = !(yield* manager.isLive(providerSessionId));
31213125
const closeCountAtFirstDeadline = (yield* Ref.get(state)).closeCount;
@@ -3139,22 +3143,32 @@ const idleAcrossHostSleep = (input: { readonly timerCountsSleep: boolean }) =>
31393143
// regression: #11805 sessions were released right after the computer woke from sleep
31403144
it.effect("ProviderSessionManagerV2 does not count host sleep toward the idle timeout", () =>
31413145
Effect.gen(function* () {
3142-
const result = yield* idleAcrossHostSleep({ timerCountsSleep: true });
3143-
assert.isFalse(
3144-
result.releasedAtFirstDeadline,
3145-
"session was idle-released on resume after only 5 awake minutes of a 30 minute timeout",
3146-
);
3147-
assert.equal(result.closeCountAtFirstDeadline, 0);
3148-
assert.isTrue(result.releasedAtSecondDeadline);
3149-
assert.equal(result.closeCountAtSecondDeadline, 1);
3146+
for (const { awakeMs, sleepMs } of [
3147+
{ awakeMs: 5 * 60_000, sleepMs: 2 * 60 * 60_000 },
3148+
// Overshoots the deadline by under a minute, so deadline lateness alone misses it.
3149+
{ awakeMs: 1_000, sleepMs: 30 * 60_000 + 30_000 },
3150+
]) {
3151+
const result = yield* idleAcrossHostSleep({ timerCountsSleep: true, awakeMs, sleepMs });
3152+
assert.isFalse(
3153+
result.releasedAtFirstDeadline,
3154+
`session was idle-released on resume after ${awakeMs} awake ms and ${sleepMs} ms asleep`,
3155+
);
3156+
assert.equal(result.closeCountAtFirstDeadline, 0);
3157+
assert.isTrue(result.releasedAtSecondDeadline);
3158+
assert.equal(result.closeCountAtSecondDeadline, 1);
3159+
}
31503160
}),
31513161
);
31523162

31533163
it.effect(
31543164
"ProviderSessionManagerV2 releases on time when the idle timer already paused during host sleep",
31553165
() =>
31563166
Effect.gen(function* () {
3157-
const result = yield* idleAcrossHostSleep({ timerCountsSleep: false });
3167+
const result = yield* idleAcrossHostSleep({
3168+
timerCountsSleep: false,
3169+
awakeMs: 5 * 60_000,
3170+
sleepMs: 2 * 60 * 60_000,
3171+
});
31583172
assert.isTrue(
31593173
result.releasedAtFirstDeadline,
31603174
"session outlived its 30 awake idle minutes because wall time jumped during sleep",

‎apps/server/src/orchestration-v2/ProviderSessionManager.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,21 @@ const WORKING_RUN_STATUSES: ReadonlySet<string> = new Set([
7474
"running",
7575
"waiting",
7676
]);
77-
const HOST_SUSPEND_GRACE_MS = 60 * 1000;
77+
const AWAKE_SLICE_MS = 60 * 1000;
78+
const SUSPEND_TOLERANCE_MS = 10 * 1000;
7879

79-
// Where the timer clock counts host sleep, a timer fires on wake far past due. The monotonic clock is
80-
// that same clock, so an overshoot past the grace means the host slept and the wait restarts; where
81-
// both pause, it never restarts. Untraced: every provider event re-arms the idle timer.
80+
// Where the timer clock counts host sleep, a timer fires on wake far past due. Waiting in short slices
81+
// and dropping any slice that overran its tolerance keeps suspended time out of the count, whatever its
82+
// length. Where both clocks pause, no slice overruns. Untraced: every provider event re-arms the timer.
8283
const sleepWhileAwake = Effect.fnUntraced(function* (ms: number): Effect.fn.Return<void> {
83-
const startedAtNanos = yield* Clock.monotonicTimeNanos;
84-
yield* Effect.sleep(Duration.millis(ms));
85-
const elapsedMs = Number((yield* Clock.monotonicTimeNanos) - startedAtNanos) / 1_000_000;
86-
if (elapsedMs > ms + HOST_SUSPEND_GRACE_MS) return yield* sleepWhileAwake(ms);
84+
let remainingMs = ms;
85+
while (remainingMs > 0) {
86+
const sliceMs = Math.min(remainingMs, AWAKE_SLICE_MS);
87+
const startedAtNanos = yield* Clock.monotonicTimeNanos;
88+
yield* Effect.sleep(Duration.millis(sliceMs));
89+
const elapsedMs = Number((yield* Clock.monotonicTimeNanos) - startedAtNanos) / 1_000_000;
90+
if (elapsedMs <= sliceMs + SUSPEND_TOLERANCE_MS) remainingMs -= sliceMs;
91+
}
8792
});
8893

8994
export const ProviderSessionReleaseReason = Schema.Literals([

0 commit comments

Comments
 (0)