Skip to content

Commit fb036f5

Browse files
fix(server): reject thread forks on retired provider runtimes
1 parent b12f00d commit fb036f5

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ interface TestProviderRuntimeState {
186186
readonly closeCount: number;
187187
readonly interruptCount: number;
188188
readonly resumeCount: number;
189+
readonly forkCount: number;
189190
readonly unloadedNativeThreadIds: ReadonlyArray<string>;
190191
readonly eventQueues: ReadonlyMap<
191192
string,
@@ -198,6 +199,7 @@ const emptyState: TestProviderRuntimeState = {
198199
closeCount: 0,
199200
interruptCount: 0,
200201
resumeCount: 0,
202+
forkCount: 0,
201203
unloadedNativeThreadIds: [],
202204
eventQueues: new Map(),
203205
};
@@ -596,7 +598,16 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
596598
respondToRuntimeRequest: () => Effect.void,
597599
readThreadSnapshot: () => unimplemented("readThreadSnapshot unused in test"),
598600
rollbackThread: () => unimplemented("rollbackThread unused in test"),
599-
forkThread: () => unimplemented("forkThread unused in test"),
601+
forkThread: (threadInput) =>
602+
Ref.update(state, (current) => ({
603+
...current,
604+
forkCount: current.forkCount + 1,
605+
})).pipe(
606+
Effect.as({
607+
...threadInput.sourceProviderThread,
608+
appThreadId: threadInput.targetThreadId,
609+
}),
610+
),
600611
} satisfies ProviderAdapter.ProviderAdapterV2SessionRuntime;
601612
}),
602613
});
@@ -1485,7 +1496,7 @@ it.effect(
14851496
}),
14861497
);
14871498

1488-
it.effect.each(["ensure", "resume", "resume without app thread"] as const)(
1499+
it.effect.each(["ensure", "resume", "resume without app thread", "fork"] as const)(
14891500
"ProviderSessionManagerV2 rejects a retired runtime's thread load: %s",
14901501
(operation) =>
14911502
Effect.gen(function* () {
@@ -1522,12 +1533,18 @@ it.effect.each(["ensure", "resume", "resume without app thread"] as const)(
15221533
const load =
15231534
operation === "ensure"
15241535
? runtime.ensureThread({ threadId, modelSelection, runtimePolicy })
1525-
: runtime.resumeThread({
1526-
providerThread:
1527-
operation === "resume without app thread"
1528-
? { ...providerThread, appThreadId: null }
1529-
: providerThread,
1530-
});
1536+
: operation === "fork"
1537+
? runtime.forkThread({
1538+
sourceProviderThread: providerThread,
1539+
sourceProviderTurns: [],
1540+
targetThreadId: threadId,
1541+
})
1542+
: runtime.resumeThread({
1543+
providerThread:
1544+
operation === "resume without app thread"
1545+
? { ...providerThread, appThreadId: null }
1546+
: providerThread,
1547+
});
15311548
const closing = yield* manager
15321549
.close(providerSessionId)
15331550
.pipe(Effect.forkScoped({ startImmediately: true }));
@@ -1547,6 +1564,7 @@ it.effect.each(["ensure", "resume", "resume without app thread"] as const)(
15471564
["ProviderAdapterSessionStoppedError", "ProviderAdapterSessionStoppedError"],
15481565
);
15491566
assert.equal((yield* Ref.get(state)).resumeCount, 0);
1567+
assert.equal((yield* Ref.get(state)).forkCount, 0);
15501568
}).pipe(
15511569
Effect.provide(
15521570
layerTest({

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2276,9 +2276,10 @@ export const layerWithOptions = (
22762276
providerSessionId,
22772277
threadId: input.targetThreadId,
22782278
providerInstanceId: runtime.instanceId,
2279+
runtime,
22792280
}),
22802281
).pipe(
2281-
Effect.andThen(loadNatively(runtime.forkThread(input))),
2282+
Effect.andThen(loadNatively(runtime.forkThread(input), runtime)),
22822283
Effect.tap(({ loaded: providerThread, linkedEnvironments }) =>
22832284
markProviderThreadLoaded({
22842285
runtime,

0 commit comments

Comments
 (0)