Skip to content

Commit 401a856

Browse files
fix(server): detaching the last thread cancels its pending approvals
Detaching the last thread from a single-thread provider session removed the thread before releasing the session, so the runtime-request release scanned only the remaining (empty) attached set. The detached thread's pending approvals stayed pending on a session that no longer existed: respond failed and settle refused. The release now covers the detached thread for runtime requests the same way it already did for background tasks, including on retry. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d644148 commit 401a856

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

‎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)