Skip to content

Commit 5e5c2e0

Browse files
committed
fix(server): protect active compaction during auth recovery
1 parent 28ea258 commit 5e5c2e0

3 files changed

Lines changed: 59 additions & 54 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,6 @@ describe("CodexAdapterV2 post-settle continuation", () => {
11661166
"Your access token could not be refreshed because your refresh token has expired. Please log out and sign in again.",
11671167
stale: false,
11681168
},
1169-
{ code: "internalServerError", message: "Service unavailable", stale: false },
11701169
])(
11711170
"marks only external account mismatch for idle recovery: $message",
11721171
({ code, message, stale }) =>
@@ -1175,7 +1174,6 @@ describe("CodexAdapterV2 post-settle continuation", () => {
11751174
const nativeThreadId = "auth-change-thread";
11761175
const nativeTurnId = "auth-change-turn";
11771176
const error = { message, codexErrorInfo: code, additionalDetails: null };
1178-
const requests: string[] = [];
11791177
const harness = yield* makeCodexReplayHarness(
11801178
makeCodexReplayTranscript({
11811179
scenario: "external-auth-change",
@@ -1210,11 +1208,6 @@ describe("CodexAdapterV2 post-settle continuation", () => {
12101208
},
12111209
],
12121210
}),
1213-
() => Effect.void,
1214-
(method) =>
1215-
Effect.sync(() => {
1216-
requests.push(method);
1217-
}),
12181211
);
12191212
yield* harness.runtime.startTurn(
12201213
makeCodexTestTurnInput({
@@ -1229,7 +1222,6 @@ describe("CodexAdapterV2 post-settle continuation", () => {
12291222
assert.equal(yield* harness.runtime.hasStaleCredentials ?? Effect.succeed(false), stale);
12301223
assert.equal(harness.terminalEvents()[0]?.status, "failed");
12311224
assert.include(harness.terminalEvents()[0]?.failure?.message, message);
1232-
assert.equal(requests.filter((method) => method === "turn/start").length, 1);
12331225
if (stale) {
12341226
const replacement = yield* harness.adapter.openSession({
12351227
threadId: harness.threadId,

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
335335
readonly backgroundTasksEndWithProcess?: boolean;
336336
readonly hangSessionScopeClose?: boolean;
337337
readonly startTurn?: Effect.Effect<void>;
338+
readonly compactThread?: Effect.Effect<void>;
338339
readonly beforeUnload?: Effect.Effect<void>;
339340
readonly spawnBeforeOpen?: boolean;
340341
readonly scopeCloseReached?: Deferred.Deferred<void>;
@@ -435,6 +436,9 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
435436
Effect.as(threadInput.providerThread),
436437
),
437438
startTurn: () => options.startTurn ?? Effect.void,
439+
...(options.compactThread === undefined
440+
? {}
441+
: { compactThread: () => options.compactThread! }),
438442
steerTurn: () => Effect.void,
439443
interruptTurn: () =>
440444
Ref.update(state, (current) => ({
@@ -490,6 +494,7 @@ function layerTest(input: {
490494
readonly backgroundTasksEndWithProcess?: boolean;
491495
readonly hangSessionScopeClose?: boolean;
492496
readonly startTurn?: Effect.Effect<void>;
497+
readonly compactThread?: Effect.Effect<void>;
493498
readonly beforeUnload?: Effect.Effect<void>;
494499
readonly spawnBeforeOpen?: boolean;
495500
readonly scopeCloseReached?: Deferred.Deferred<void>;
@@ -529,6 +534,7 @@ function layerTest(input: {
529534
? {}
530535
: { hangSessionScopeClose: input.hangSessionScopeClose }),
531536
...(input.startTurn === undefined ? {} : { startTurn: input.startTurn }),
537+
...(input.compactThread === undefined ? {} : { compactThread: input.compactThread }),
532538
...(input.beforeUnload === undefined ? {} : { beforeUnload: input.beforeUnload }),
533539
...(input.spawnBeforeOpen === undefined ? {} : { spawnBeforeOpen: input.spawnBeforeOpen }),
534540
...(input.scopeCloseReached === undefined
@@ -3557,9 +3563,9 @@ it.effect(
35573563
}),
35583564
);
35593565

3560-
it.effect(
3561-
"ProviderSessionManagerV2 replaces stale credentials only after shared work settles and resumes native history",
3562-
() =>
3566+
it.effect.each(["startTurn", "compactThread"] as const)(
3567+
"ProviderSessionManagerV2 replaces stale credentials only after shared work settles and resumes native history with %s",
3568+
(operation) =>
35633569
Effect.gen(function* () {
35643570
const state = yield* Ref.make(emptyState);
35653571
const stale = yield* Ref.make(false);
@@ -3633,7 +3639,7 @@ it.effect(
36333639
modelSelection,
36343640
runtimePolicy,
36353641
} as const;
3636-
yield* first.startTurn(startInput);
3642+
yield* first[operation]!(startInput);
36373643
yield* Ref.set(stale, true);
36383644
assert.strictEqual(yield* open(b), first);
36393645
assert.equal((yield* Ref.get(state)).closeCount, 0);
@@ -3669,14 +3675,14 @@ it.effect(
36693675
yield* Fiber.join(pending);
36703676
yield* manager.detach({ providerSessionId, threadId: a });
36713677
yield* Ref.set(pauseAttachWrite.armed, true);
3672-
const pendingStart = yield* first.startTurn(startInput).pipe(Effect.forkChild);
3678+
const pendingStart = yield* first[operation]!(startInput).pipe(Effect.forkChild);
36733679
yield* Deferred.await(pauseAttachWrite.paused);
36743680
assert.strictEqual(yield* open(b), first);
36753681
yield* Fiber.interrupt(pendingStart);
36763682
yield* Ref.set(pauseProbe, true);
36773683
const reopening = yield* open(b).pipe(Effect.forkChild);
36783684
yield* Deferred.await(probeEntered);
3679-
yield* first.startTurn({ ...startInput, runOrdinal: 2 });
3685+
yield* first[operation]!({ ...startInput, runOrdinal: 2 });
36803686
yield* Deferred.succeed(probeContinue, undefined);
36813687
assert.strictEqual(yield* Fiber.join(reopening), first);
36823688
const finished = yield* first.subscribeEvents!;
@@ -3693,22 +3699,22 @@ it.effect(
36933699
(thread) => thread.id === providerThread.id,
36943700
)!;
36953701
assert.equal(saved.nativeThreadRef?.nativeId, "persisted-native-history");
3696-
const resumed = yield* replacement.resumeThread({
3702+
yield* replacement.resumeThread({
36973703
threadId: a,
36983704
providerThread: saved,
36993705
modelSelection,
37003706
runtimePolicy,
37013707
});
3702-
assert.equal(resumed.nativeThreadRef?.nativeId, "persisted-native-history");
37033708
assert.equal((yield* Ref.get(state)).resumeCount, 2);
3704-
assert.equal((yield* first.startTurn(startInput).pipe(Effect.result))._tag, "Failure");
3709+
assert.equal((yield* first[operation]!(startInput).pipe(Effect.result))._tag, "Failure");
37053710
assert.strictEqual(yield* open(a), replacement);
37063711
assert.equal((yield* Ref.get(state)).openCount, 2);
37073712
}).pipe(
37083713
Effect.provide(
37093714
layerTest({
37103715
state,
37113716
pauseAttachWrite,
3717+
compactThread: Effect.void,
37123718
idleTimeoutMs: 60_000,
37133719
beforeOpen: () => Ref.set(stale, false),
37143720
hasStaleCredentials: Ref.get(stale),

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

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,45 @@ export const layerWithOptions = (
17211721
modelFamily: normalizeModelMetricLabel(model),
17221722
},
17231723
});
1724+
const withTurnActivity =
1725+
(start: typeof runtime.startTurn, operation = "send"): typeof runtime.startTurn =>
1726+
(input) =>
1727+
observeActivity(
1728+
providerSessionId,
1729+
ensureThreadAttached({
1730+
providerSessionId,
1731+
threadId: input.threadId,
1732+
providerInstanceId: runtime.instanceId,
1733+
}),
1734+
).pipe(
1735+
// Failed or stopped starts may omit turn.terminal; clear their turns so sessions can idle.
1736+
Effect.andThen(
1737+
holdThreadLoaded({
1738+
providerSessionId,
1739+
threadId: input.threadId,
1740+
providerThread: input.providerThread,
1741+
}),
1742+
),
1743+
Effect.andThen(
1744+
Effect.acquireUseRelease(
1745+
observeActivity(
1746+
providerSessionId,
1747+
markBusy(
1748+
providerSessionId,
1749+
busyTurnKey(input.providerThread.id, input.runOrdinal),
1750+
),
1751+
),
1752+
() => start(input).pipe(turnMetrics(operation, input.modelSelection.model)),
1753+
(_, exit) =>
1754+
Exit.isFailure(exit)
1755+
? observeActivity(
1756+
providerSessionId,
1757+
markIdle(providerSessionId, input.providerThread.id, input.runOrdinal),
1758+
)
1759+
: Effect.void,
1760+
),
1761+
),
1762+
);
17241763
const exposed: ProviderAdapter.ProviderAdapterV2SessionRuntime = {
17251764
...runtime,
17261765
subscribeEvents,
@@ -1825,44 +1864,12 @@ export const layerWithOptions = (
18251864
}),
18261865
),
18271866
),
1828-
startTurn: (input) =>
1829-
observeActivity(
1830-
providerSessionId,
1831-
ensureThreadAttached({
1832-
providerSessionId,
1833-
threadId: input.threadId,
1834-
providerInstanceId: runtime.instanceId,
1867+
startTurn: withTurnActivity(runtime.startTurn),
1868+
...(runtime.compactThread === undefined
1869+
? {}
1870+
: {
1871+
compactThread: withTurnActivity(runtime.compactThread, "compact"),
18351872
}),
1836-
).pipe(
1837-
// Failed or stopped starts may omit turn.terminal; clear their turns so sessions can idle.
1838-
Effect.andThen(
1839-
holdThreadLoaded({
1840-
providerSessionId,
1841-
threadId: input.threadId,
1842-
providerThread: input.providerThread,
1843-
}),
1844-
),
1845-
Effect.andThen(
1846-
Effect.acquireUseRelease(
1847-
observeActivity(
1848-
providerSessionId,
1849-
markBusy(
1850-
providerSessionId,
1851-
busyTurnKey(input.providerThread.id, input.runOrdinal),
1852-
),
1853-
),
1854-
() =>
1855-
runtime.startTurn(input).pipe(turnMetrics("send", input.modelSelection.model)),
1856-
(_, exit) =>
1857-
Exit.isFailure(exit)
1858-
? observeActivity(
1859-
providerSessionId,
1860-
markIdle(providerSessionId, input.providerThread.id, input.runOrdinal),
1861-
)
1862-
: Effect.void,
1863-
),
1864-
),
1865-
),
18661873
steerTurn: (input) =>
18671874
observeActivity(providerSessionId, touchActivity(providerSessionId)).pipe(
18681875
Effect.andThen(runtime.steerTurn(input).pipe(turnMetrics("steer"))),

0 commit comments

Comments
 (0)