Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,49 @@ it.effect("ProviderSessionManagerV2 drains subscribers when the provider stops",
}),
);

it.effect("ProviderSessionManagerV2 fails a subscription made after its provider died", () =>
Effect.gen(function* () {
const state = yield* Ref.make(emptyState);
const effect = Effect.gen(function* () {
const eventSink = yield* EventSink.EventSinkV2;
const idAllocator = yield* IdAllocator.IdAllocatorV2;
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
const now = yield* DateTime.now;
const threadId = ThreadId.make("thread-provider-session-manager-late-subscriber");
const providerSessionId = yield* idAllocator.allocate.providerSession({
providerInstanceId: modelSelection.instanceId,
threadId,
});
yield* eventSink.write({
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
});
const runtime = yield* manager.open({
threadId,
providerSessionId,
modelSelection,
runtimePolicy,
});
// A run holds the runtime it got at turn start and subscribes later.
const early = yield* runtime.subscribeEvents!;
const earlyExit = yield* early.events.pipe(Stream.runDrain, Effect.exit, Effect.forkScoped);
const adapterQueue = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId));
assert.isDefined(adapterQueue);
yield* Queue.end(adapterQueue!);
assert.isTrue(Exit.isFailure(yield* Fiber.join(earlyExit)));

const late = yield* runtime.subscribeEvents!;
const lateExit = yield* late.events.pipe(Stream.runDrain, Effect.exit);
assert.isTrue(Exit.isFailure(lateExit));
if (Exit.isFailure(lateExit)) {
const error = Cause.squash(lateExit.cause);
assert.instanceOf(error, ProviderAdapter.ProviderAdapterEventStreamError);
}
});

yield* effect.pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 60_000 })));
}),
);

it.effect("ProviderSessionManagerV2 fails a turn whose provider retry never resumes", () =>
Effect.gen(function* () {
const state = yield* Ref.make(emptyState);
Expand Down
28 changes: 25 additions & 3 deletions apps/server/src/orchestration-v2/ProviderSessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ export const layerWithOptions = (
subscribers: Ref.Ref<
ReadonlyMap<number, Queue.Queue<ProviderSessionEventSignal, Cause.Done>>
>,
runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime,
): Effect.fn.Return<ProviderAdapter.ProviderAdapterV2EventSubscription> {
const queue = yield* Queue.unbounded<ProviderSessionEventSignal, Cause.Done>();
const subscriberId = yield* Ref.getAndUpdate(nextSubscriberId, (value) => value + 1);
Expand All @@ -2278,14 +2279,35 @@ export const layerWithOptions = (
updated.set(subscriberId, queue);
return updated;
});
const close = Ref.modify(subscribers, (current) => {
const unregister = Ref.modify(subscribers, (current) => {
if (!current.has(subscriberId)) {
return [false, current] as const;
}
const updated = new Map(current);
updated.delete(subscriberId);
return [true, updated] as const;
}).pipe(
});
// Release signals its subscribers once; a run that subscribes after it would wait forever.
// Still registered here means release took the map first, so this queue is ours to end.
const live =
(yield* Ref.get(sessions)).get(sessionKey(runtime.providerSessionId))?.runtime ===
runtime;
if (!live && (yield* unregister)) {
yield* shutdownSignal.received
? Queue.interrupt(queue)
: Queue.offer(queue, {
type: "failure",
cause: Cause.fail(
new ProviderAdapter.ProviderAdapterEventStreamError({
driver: runtime.driver,
providerSessionId: runtime.providerSessionId,
raisedByT3: true,
cause: "The provider session was released before this subscription.",
}),
),
});
}
const close = unregister.pipe(
Effect.flatMap((removed) =>
removed
? Queue.clear(queue).pipe(Effect.andThen(Queue.end(queue)), Effect.asVoid)
Expand Down Expand Up @@ -2319,7 +2341,7 @@ export const layerWithOptions = (
>,
): ProviderAdapter.ProviderAdapterV2SessionRuntime => {
const providerSessionId = runtime.providerSessionId;
const subscribeEvents = makeEventSubscription(eventSubscribers);
const subscribeEvents = makeEventSubscription(eventSubscribers, runtime);
const turnMetrics = (operation: string, model?: string) =>
withMetrics({
counter: providerTurnsTotal,
Expand Down
Loading