Skip to content

Commit 577c524

Browse files
fix(server): detaching a session no longer strands its pending approval (pingdotgg#33535)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 25da7eb commit 577c524

2 files changed

Lines changed: 81 additions & 7 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3599,6 +3599,67 @@ it.effect.each(["manual_shutdown", "server_shutdown"] as const)(
35993599
}),
36003600
);
36013601

3602+
it.effect(
3603+
"ProviderSessionManagerV2 cancels a pending approval when a detach releases its session",
3604+
() =>
3605+
Effect.gen(function* () {
3606+
const state = yield* Ref.make(emptyState);
3607+
const effect = Effect.gen(function* () {
3608+
const eventSink = yield* EventSink.EventSinkV2;
3609+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
3610+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
3611+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
3612+
const now = yield* DateTime.now;
3613+
const projectId = yield* idAllocator.allocate.project({
3614+
fixtureName: "provider-session-manager-detach-request",
3615+
});
3616+
const threadId = yield* idAllocator.allocate.thread({
3617+
fixtureName: "provider-session-manager-detach-request",
3618+
projectId,
3619+
});
3620+
const providerSessionId = yield* idAllocator.allocate.providerSession({
3621+
providerInstanceId: modelSelection.instanceId,
3622+
threadId,
3623+
});
3624+
const providerThread = makeProviderThread({
3625+
idAllocator,
3626+
threadId,
3627+
providerSessionId,
3628+
now,
3629+
});
3630+
3631+
yield* eventSink.write({
3632+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
3633+
});
3634+
const pendingRequest = yield* makePendingRuntimeRequestEvents({
3635+
idAllocator,
3636+
threadId,
3637+
providerSessionId,
3638+
providerThread,
3639+
now,
3640+
});
3641+
yield* eventSink.write({ events: pendingRequest.events });
3642+
yield* manager.open({ threadId, providerSessionId, modelSelection, runtimePolicy });
3643+
3644+
yield* manager.detach({ providerSessionId, threadId, detail: "Workspace changed." });
3645+
3646+
assert.isTrue(Option.isNone(yield* manager.get(providerSessionId)));
3647+
const projection = yield* projectionStore.getThreadProjection(threadId);
3648+
const request = projection.runtimeRequests.find(
3649+
(candidate) => candidate.id === pendingRequest.requestId,
3650+
);
3651+
assert.equal(request?.status, "cancelled");
3652+
assert.equal(request?.responseCapability.type, "not_resumable");
3653+
});
3654+
3655+
yield* effect.pipe(
3656+
Effect.provide(
3657+
layerTest({ state, idleTimeoutMs: 60_000, capabilities: ExclusiveCapabilities }),
3658+
),
3659+
);
3660+
}),
3661+
);
3662+
36023663
it.effect("ProviderSessionManagerV2 persists session-scoped runtime requests without a run", () =>
36033664
Effect.gen(function* () {
36043665
const state = yield* Ref.make(emptyState);

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,23 @@ export const layerWithOptions = (
665665
});
666666
});
667667

668+
// A detach removes its thread before releasing, but that thread's records still need releasing.
669+
const releasedThreadIds = (input: {
670+
readonly entry: LiveSessionEntry;
671+
readonly detachedThreadId?: ThreadId;
672+
}) => {
673+
const threadIds = new Set(input.entry.attachedThreadIds);
674+
if (input.detachedThreadId !== undefined) threadIds.add(input.detachedThreadId);
675+
return threadIds;
676+
};
677+
668678
const writeReleasedRuntimeRequestEvents = Effect.fn(
669679
"ProviderSessionManager.writeReleasedRuntimeRequestEvents",
670680
)(function* (input: {
671681
readonly entry: LiveSessionEntry;
672682
readonly reason: ProviderSessionReleaseReason;
673683
readonly releasedAt: DateTime.Utc;
684+
readonly detachedThreadId?: ThreadId;
674685
}) {
675686
const providerSessionId = input.entry.runtime.providerSessionId;
676687
const now = yield* DateTime.now;
@@ -681,7 +692,7 @@ export const layerWithOptions = (
681692
: "Provider session was closed before this runtime request was resolved.";
682693

683694
const events: Array<OrchestrationV2DomainEvent> = [];
684-
for (const threadId of input.entry.attachedThreadIds) {
695+
for (const threadId of releasedThreadIds(input)) {
685696
const projection = yield* projectionStore.getThreadRecords(
686697
threadId,
687698
["runtimeRequests"],
@@ -800,10 +811,8 @@ export const layerWithOptions = (
800811
return [];
801812
}
802813
const providerSessionId = input.entry.runtime.providerSessionId;
803-
const threadIds = new Set(input.entry.attachedThreadIds);
804-
if (input.detachedThreadId !== undefined) threadIds.add(input.detachedThreadId);
805814
const released = [];
806-
for (const threadId of threadIds) {
815+
for (const threadId of releasedThreadIds(input)) {
807816
const projection = yield* projectionStore.getThreadRecords(threadId, [
808817
"providerThreads",
809818
]);
@@ -892,6 +901,7 @@ export const layerWithOptions = (
892901
readonly detail?: string;
893902
readonly releasedBackgroundTasks: ReadonlyArray<ReleasedBackgroundTasks>;
894903
readonly releasedAt: DateTime.Utc;
904+
readonly detachedThreadId?: ThreadId;
895905
readonly replaced: boolean;
896906
}) =>
897907
Effect.all(
@@ -1068,12 +1078,14 @@ export const layerWithOptions = (
10681078
onNone: () => Effect.void,
10691079
onSome: (entry) =>
10701080
Effect.gen(function* () {
1081+
const detachedThread =
1082+
input.detachedThreadId === undefined
1083+
? {}
1084+
: { detachedThreadId: input.detachedThreadId };
10711085
const releasedBackgroundTasks = yield* readReleasedBackgroundTasks({
10721086
entry,
10731087
reason: input.reason,
1074-
...(input.detachedThreadId === undefined
1075-
? {}
1076-
: { detachedThreadId: input.detachedThreadId }),
1088+
...detachedThread,
10771089
});
10781090
if (input.cancelIdleFiber !== false) {
10791091
yield* cancelIdleFiber(entry.idleFiber);
@@ -1098,6 +1110,7 @@ export const layerWithOptions = (
10981110
...(input.detail === undefined ? {} : { detail: input.detail }),
10991111
releasedBackgroundTasks,
11001112
releasedAt,
1113+
...detachedThread,
11011114
};
11021115
const recorded = yield* Effect.exit(
11031116
writeReleaseRecords({ ...records, replaced: false }),

0 commit comments

Comments
 (0)