Skip to content

Commit 3afc9ea

Browse files
author
PR Batch Tester
committed
fix(server): retry deferred linked tool refreshes
1 parent e92181c commit 3afc9ea

2 files changed

Lines changed: 52 additions & 33 deletions

File tree

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,6 +3257,7 @@ it.effect(
32573257
Effect.gen(function* () {
32583258
const state = yield* Ref.make(emptyState);
32593259
const pendingWork = yield* Ref.make<Effect.Effect<boolean>>(Effect.succeed(false));
3260+
const beforeUnload = yield* Ref.make<Effect.Effect<void>>(Effect.void);
32603261
const settings = yield* makeTestSettings;
32613262
yield* Effect.gen(function* () {
32623263
const eventSink = yield* EventSink.EventSinkV2;
@@ -3303,11 +3304,11 @@ it.effect(
33033304
modelSelection,
33043305
runtimePolicy,
33053306
});
3306-
const resume = (threadId: ThreadId) =>
3307+
const resume = (threadId: ThreadId, selection = modelSelection) =>
33073308
runtime.resumeThread({
33083309
providerThread: providerThreads[threadId]!,
33093310
threadId,
3310-
modelSelection,
3311+
modelSelection: selection,
33113312
runtimePolicy,
33123313
});
33133314
const observed = Ref.get(state).pipe(
@@ -3323,17 +3324,18 @@ it.effect(
33233324
yield* settings.updateSettings({ linkedEnvironmentsEnabled: true });
33243325
// Background work, or a probe that cannot tell, keeps the stale thread loaded.
33253326
yield* Ref.set(pendingWork, Effect.succeed(true));
3327+
yield* resume(threadA, { ...modelSelection, model: "gpt-5.4-mini" });
33263328
yield* resume(threadA);
33273329
yield* Ref.set(pendingWork, Effect.die("probe failed"));
33283330
yield* resume(threadA);
3329-
assert.deepEqual(yield* observed, { resumeCount: 2, unloaded: [] });
3331+
assert.deepEqual(yield* observed, { resumeCount: 4, unloaded: [] });
33303332

33313333
yield* Ref.set(pendingWork, Effect.succeed(false));
33323334
const reloaded = yield* resume(threadA);
33333335
assert.deepEqual(reloaded.nativeThreadRef, providerThreads[threadA]!.nativeThreadRef);
3334-
assert.deepEqual(yield* observed, { resumeCount: 3, unloaded: ["native-links-a"] });
3336+
assert.deepEqual(yield* observed, { resumeCount: 5, unloaded: ["native-links-a"] });
33353337
yield* resume(threadA);
3336-
assert.deepEqual(yield* observed, { resumeCount: 3, unloaded: ["native-links-a"] });
3338+
assert.deepEqual(yield* observed, { resumeCount: 5, unloaded: ["native-links-a"] });
33373339

33383340
// A thread with a live turn keeps its process until that turn ends.
33393341
const runId = idAllocator.derive.run({ threadId: threadB, ordinal: 1 });
@@ -3357,14 +3359,27 @@ it.effect(
33573359
runtimePolicy,
33583360
});
33593361
yield* resume(threadB);
3360-
assert.deepEqual(yield* observed, { resumeCount: 3, unloaded: ["native-links-a"] });
3362+
assert.deepEqual(yield* observed, { resumeCount: 5, unloaded: ["native-links-a"] });
33613363

33623364
yield* settings.updateSettings({ linkedEnvironmentsEnabled: false });
33633365
yield* resume(threadA);
33643366
assert.deepEqual(yield* observed, {
3365-
resumeCount: 4,
3367+
resumeCount: 6,
3368+
unloaded: ["native-links-a", "native-links-a"],
3369+
});
3370+
yield* settings.updateSettings({ linkedEnvironmentsEnabled: true });
3371+
yield* Ref.set(beforeUnload, Effect.die("unload failed"));
3372+
yield* resume(threadA);
3373+
assert.deepEqual(yield* observed, {
3374+
resumeCount: 6,
33663375
unloaded: ["native-links-a", "native-links-a"],
33673376
});
3377+
yield* Ref.set(beforeUnload, Effect.void);
3378+
yield* resume(threadA);
3379+
assert.deepEqual(yield* observed, {
3380+
resumeCount: 7,
3381+
unloaded: ["native-links-a", "native-links-a", "native-links-a"],
3382+
});
33683383
const final = yield* Ref.get(state);
33693384
assert.equal(final.openCount, 1);
33703385
assert.equal(final.closeCount, 0);
@@ -3373,6 +3388,7 @@ it.effect(
33733388
layerTest({
33743389
state,
33753390
idleTimeoutMs: 1000,
3391+
beforeUnload: Effect.flatten(Ref.get(beforeUnload)),
33763392
hasPendingBackgroundWorkForThread: Effect.flatten(Ref.get(pendingWork)),
33773393
serverSettingsLayer: Layer.succeed(ServerSettings.ServerSettingsService, settings),
33783394
}),

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,10 @@ export const layerWithOptions = (
13011301
const loadedProviderThreadKeyByThread = new Map(entry.loadedProviderThreadKeyByThread);
13021302
loadedProviderThreadKeyByThread.set(input.threadId, input.providerThreadKey);
13031303
const linkedEnvironmentsByThread = new Map(entry.linkedEnvironmentsByThread);
1304-
if (input.linkedEnvironments !== undefined) {
1304+
if (
1305+
input.linkedEnvironments !== undefined &&
1306+
!linkedEnvironmentsByThread.has(input.threadId)
1307+
) {
13051308
linkedEnvironmentsByThread.set(input.threadId, input.linkedEnvironments);
13061309
}
13071310
const updated = new Map(current);
@@ -1661,33 +1664,33 @@ export const layerWithOptions = (
16611664
})
16621665
.pipe(Effect.catchCause(() => Effect.succeed(true)));
16631666
if (hasPendingWork) return;
1664-
const unloading = yield* Ref.modify(sessions, (current) => {
1665-
const latest = current.get(key);
1666-
if (
1667-
latest?.runtime !== entry.runtime ||
1668-
latest.linkedEnvironmentsByThread.get(input.threadId) !== loadedWith ||
1669-
hasBusyTurn(latest, input.providerThread.id)
1670-
) {
1671-
return [false, current] as const;
1672-
}
1673-
const loadedProviderThreadKeyByThread = new Map(
1674-
latest.loadedProviderThreadKeyByThread,
1675-
);
1676-
loadedProviderThreadKeyByThread.delete(input.threadId);
1677-
const linkedEnvironmentsByThread = new Map(latest.linkedEnvironmentsByThread);
1678-
linkedEnvironmentsByThread.delete(input.threadId);
1679-
return [
1680-
true,
1681-
new Map(current).set(key, {
1682-
...latest,
1683-
loadedProviderThreadKeyByThread,
1684-
linkedEnvironmentsByThread,
1685-
}),
1686-
] as const;
1687-
});
1688-
if (!unloading) return;
1667+
const latest = (yield* Ref.get(sessions)).get(key);
1668+
if (
1669+
latest?.runtime !== entry.runtime ||
1670+
latest.linkedEnvironmentsByThread.get(input.threadId) !== loadedWith ||
1671+
hasBusyTurn(latest, input.providerThread.id)
1672+
) {
1673+
return;
1674+
}
16891675
yield* unloadThread({ providerThread: input.providerThread }).pipe(
16901676
Effect.timeout(UNLOAD_THREAD_TIMEOUT_MS),
1677+
Effect.tap(() =>
1678+
Ref.update(sessions, (current) => {
1679+
const latest = current.get(key);
1680+
if (latest?.runtime !== entry.runtime) return current;
1681+
const loadedProviderThreadKeyByThread = new Map(
1682+
latest.loadedProviderThreadKeyByThread,
1683+
);
1684+
loadedProviderThreadKeyByThread.delete(input.threadId);
1685+
const linkedEnvironmentsByThread = new Map(latest.linkedEnvironmentsByThread);
1686+
linkedEnvironmentsByThread.delete(input.threadId);
1687+
return new Map(current).set(key, {
1688+
...latest,
1689+
loadedProviderThreadKeyByThread,
1690+
linkedEnvironmentsByThread,
1691+
});
1692+
}),
1693+
),
16911694
Effect.catchCause((cause) =>
16921695
Effect.logWarning("orchestration-v2.driver-session.links-unload-failed", {
16931696
providerSessionId: input.providerSessionId,

0 commit comments

Comments
 (0)