The best way to control Claude Code, Codex and more from any computer or phone
fix(server): detaching a session no longer strands its pending approval (#33535)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 9, 2026 at 23:55 UTC
577c524089368176e00b331a8217b642fd6d06a5
2 files changed
+81
-7
apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
+61
@@ -3599,6 +3599,67 @@ it.effect.each(["manual_shutdown", "server_shutdown"] as const)(
3599
}),
3600
);
3601
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
+
3663
it.effect("ProviderSessionManagerV2 persists session-scoped runtime requests without a run", () =>
3664
Effect.gen(function* () {
3665
const state = yield* Ref.make(emptyState);
apps/server/src/orchestration-v2/ProviderSessionManager.ts
+20
-7
@@ -665,12 +665,23 @@ export const layerWithOptions = (
665
});
666
});
667
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
+
678
const writeReleasedRuntimeRequestEvents = Effect.fn(
679
"ProviderSessionManager.writeReleasedRuntimeRequestEvents",
680
)(function* (input: {
681
readonly entry: LiveSessionEntry;
682
readonly reason: ProviderSessionReleaseReason;
683
readonly releasedAt: DateTime.Utc;
684
+ readonly detachedThreadId?: ThreadId;
685
}) {
686
const providerSessionId = input.entry.runtime.providerSessionId;
687
const now = yield* DateTime.now;
@@ -681,7 +692,7 @@ export const layerWithOptions = (
692
: "Provider session was closed before this runtime request was resolved.";
693
694
const events: Array<OrchestrationV2DomainEvent> = [];
684
- for (const threadId of input.entry.attachedThreadIds) {
695
+ for (const threadId of releasedThreadIds(input)) {
696
const projection = yield* projectionStore.getThreadRecords(
697
threadId,
698
["runtimeRequests"],
@@ -800,10 +811,8 @@ export const layerWithOptions = (
811
return [];
812
}
813
const providerSessionId = input.entry.runtime.providerSessionId;
803
- const threadIds = new Set(input.entry.attachedThreadIds);
804
- if (input.detachedThreadId !== undefined) threadIds.add(input.detachedThreadId);
814
const released = [];
806
- for (const threadId of threadIds) {
815
+ for (const threadId of releasedThreadIds(input)) {
816
const projection = yield* projectionStore.getThreadRecords(threadId, [
817
"providerThreads",
818
]);
@@ -892,6 +901,7 @@ export const layerWithOptions = (
901
readonly detail?: string;
902
readonly releasedBackgroundTasks: ReadonlyArray<ReleasedBackgroundTasks>;
903
readonly releasedAt: DateTime.Utc;
904
+ readonly detachedThreadId?: ThreadId;
905
readonly replaced: boolean;
906
}) =>
907
Effect.all(
@@ -1068,12 +1078,14 @@ export const layerWithOptions = (
1078
onNone: () => Effect.void,
1079
onSome: (entry) =>
1080
Effect.gen(function* () {
1081
+ const detachedThread =
1082
+ input.detachedThreadId === undefined
1083
+ ? {}
1084
+ : { detachedThreadId: input.detachedThreadId };
1085
const releasedBackgroundTasks = yield* readReleasedBackgroundTasks({
1086
entry,
1087
reason: input.reason,
1074
- ...(input.detachedThreadId === undefined
1075
- ? {}
1076
- : { detachedThreadId: input.detachedThreadId }),
1088
+ ...detachedThread,
1089
});
1090
if (input.cancelIdleFiber !== false) {
1091
yield* cancelIdleFiber(entry.idleFiber);
@@ -1098,6 +1110,7 @@ export const layerWithOptions = (
1110
...(input.detail === undefined ? {} : { detail: input.detail }),
1111
releasedBackgroundTasks,
1112
releasedAt,
1113
+ ...detachedThread,
1114
};
1115
const recorded = yield* Effect.exit(
1116
writeReleaseRecords({ ...records, replaced: false }),