Skip to content

Commit 594c4b9

Browse files
fix(server): restart stale Codex sessions despite background activity
1 parent 1020172 commit 594c4b9

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
331331
}) => Effect.Effect<void>;
332332
readonly hasPendingBackgroundWork?: Effect.Effect<boolean>;
333333
readonly credentialRevision?: Ref.Ref<number>;
334+
readonly beforeCredentialProbe?: Effect.Effect<void>;
334335
readonly hasPendingBackgroundWorkForThread?: Effect.Effect<boolean>;
335336
readonly backgroundTasksEndWithProcess?: boolean;
336337
readonly hangSessionScopeClose?: boolean;
@@ -425,7 +426,8 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
425426
...(credentialRevision === undefined
426427
? {}
427428
: {
428-
hasStaleCredentials: Ref.get(credentialRevision).pipe(
429+
hasStaleCredentials: (options.beforeCredentialProbe ?? Effect.void).pipe(
430+
Effect.andThen(Ref.get(credentialRevision)),
429431
Effect.map((revision) => revision !== launchedRevision),
430432
),
431433
}),
@@ -491,6 +493,7 @@ function layerTest(input: {
491493
};
492494
readonly hasPendingBackgroundWork?: Effect.Effect<boolean>;
493495
readonly credentialRevision?: Ref.Ref<number>;
496+
readonly beforeCredentialProbe?: Effect.Effect<void>;
494497
readonly hasPendingBackgroundWorkForThread?: Effect.Effect<boolean>;
495498
readonly backgroundTasksEndWithProcess?: boolean;
496499
readonly hangSessionScopeClose?: boolean;
@@ -523,6 +526,9 @@ function layerTest(input: {
523526
...(input.credentialRevision === undefined
524527
? {}
525528
: { credentialRevision: input.credentialRevision }),
529+
...(input.beforeCredentialProbe === undefined
530+
? {}
531+
: { beforeCredentialProbe: input.beforeCredentialProbe }),
526532
...(input.hasPendingBackgroundWorkForThread === undefined
527533
? {}
528534
: { hasPendingBackgroundWorkForThread: input.hasPendingBackgroundWorkForThread }),
@@ -3041,6 +3047,8 @@ it.effect(
30413047
Effect.gen(function* () {
30423048
const state = yield* Ref.make(emptyState);
30433049
const credentialRevision = yield* Ref.make(1);
3050+
const probeEntered = yield* Deferred.make<void>();
3051+
const probeGate = yield* Deferred.make<void>();
30443052
yield* Effect.gen(function* () {
30453053
const eventSink = yield* EventSink.EventSinkV2;
30463054
const idAllocator = yield* IdAllocator.IdAllocatorV2;
@@ -3128,7 +3136,25 @@ it.effect(
31283136
yield* Fiber.join(settled);
31293137

31303138
// Renewal currently takes precedence over background commands after the turn settles.
3131-
const replacement = yield* open(siblingId);
3139+
const reopening = yield* open(siblingId).pipe(Effect.forkScoped);
3140+
yield* Deferred.await(probeEntered);
3141+
const activitySubscription = yield* Effect.acquireRelease(
3142+
first.subscribeEvents!,
3143+
(sub) => sub.close,
3144+
);
3145+
const activityProcessed = yield* activitySubscription.events.pipe(
3146+
Stream.filter((event) => event.type === "provider_session.updated"),
3147+
Stream.runHead,
3148+
Effect.forkScoped,
3149+
);
3150+
yield* Queue.offer((yield* Ref.get(state)).eventQueues.get(String(providerSessionId))!, {
3151+
type: "provider_session.updated",
3152+
driver: CODEX_DRIVER,
3153+
providerSession: first.providerSession,
3154+
});
3155+
assert.isTrue(Option.isSome(yield* Fiber.join(activityProcessed)));
3156+
yield* Deferred.succeed(probeGate, undefined);
3157+
const replacement = yield* Fiber.join(reopening);
31323158
assert.notStrictEqual(replacement, first);
31333159
assert.equal((yield* Ref.get(state)).openCount, 2);
31343160
assert.equal((yield* Ref.get(state)).closeCount, 1);
@@ -3154,6 +3180,11 @@ it.effect(
31543180
state,
31553181
idleTimeoutMs: 60_000,
31563182
credentialRevision,
3183+
beforeCredentialProbe: Effect.gen(function* () {
3184+
if ((yield* Ref.get(credentialRevision)) === 1) return;
3185+
yield* Deferred.succeed(probeEntered, undefined);
3186+
yield* Deferred.await(probeGate);
3187+
}),
31573188
hasPendingBackgroundWork: Effect.succeed(true),
31583189
}),
31593190
),

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ export const layerWithOptions = (
910910
const removeLiveEntry = (input: {
911911
readonly providerSessionId: ProviderSessionId;
912912
readonly onlyIfIdleGeneration?: number;
913+
readonly onlyIfIdleRuntime?: ProviderAdapter.ProviderAdapterV2SessionRuntime;
913914
}): Effect.Effect<readonly [Option.Option<LiveSessionEntry>, DateTime.Utc]> =>
914915
Effect.gen(function* () {
915916
const key = sessionKey(input.providerSessionId);
@@ -923,6 +924,12 @@ export const layerWithOptions = (
923924
if (existing !== candidate) {
924925
return [existing === undefined ? "gone" : "changed", current] as const;
925926
}
927+
if (
928+
input.onlyIfIdleRuntime !== undefined &&
929+
(existing.runtime !== input.onlyIfIdleRuntime || existing.busyTurns.size > 0)
930+
) {
931+
return ["kept", current] as const;
932+
}
926933
if (
927934
input.onlyIfIdleGeneration !== undefined &&
928935
(existing.busyTurns.size > 0 ||
@@ -988,6 +995,7 @@ export const layerWithOptions = (
988995
readonly detachedThreadId?: ThreadId;
989996
readonly cancelIdleFiber?: boolean;
990997
readonly onlyIfIdleGeneration?: number;
998+
readonly onlyIfIdleRuntime?: ProviderAdapter.ProviderAdapterV2SessionRuntime;
991999
readonly gracefulSubscribers?: boolean;
9921000
}) =>
9931001
Effect.acquireUseRelease(
@@ -1176,7 +1184,7 @@ export const layerWithOptions = (
11761184
});
11771185

11781186
// Renewed launch credentials require a restart, even with background work.
1179-
// releaseEntry rechecks busy turns and idle generation after the probe yields.
1187+
// releaseEntry rechecks runtime identity and busy turns after the probe yields.
11801188
const releaseIfStaleCredentials = Effect.fnUntraced(
11811189
function* (entry: LiveSessionEntry) {
11821190
const runtime = entry.runtime;
@@ -1186,7 +1194,7 @@ export const layerWithOptions = (
11861194
providerSessionId: runtime.providerSessionId,
11871195
reason: "manual_shutdown",
11881196
detail: "Provider credentials were renewed.",
1189-
onlyIfIdleGeneration: entry.idleGeneration,
1197+
onlyIfIdleRuntime: runtime,
11901198
});
11911199
},
11921200
(effect, entry) => effect.pipe(logReleaseFailure(entry.runtime.providerSessionId)),

0 commit comments

Comments
 (0)