TypeScript
fix(server): delegated children keep replies after their parent settles (#23456)
Julius Marminge0ad3dc18644d
diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.tsindex d0c64f04ac4..d8c046c0186 100644--- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts+++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts@@ -313,6 +313,102 @@ function makeProviderThread(input: {};}+// A Codex native subagent's provider thread is owned by its child app thread+// but reachable from the parent's projection through the subagent record.+const makeNativeChildFixture = Effect.fnUntraced(function* () {+ const idAllocator = yield* IdAllocator.IdAllocatorV2;+ const eventSink = yield* EventSink.EventSinkV2;+ const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;+ const now = yield* DateTime.now;+ const parentThreadId = ThreadId.make("native-parent");+ const childThreadId = ThreadId.make("native-child");+ const providerSessionId = idAllocator.derive.providerSession({+ providerInstanceId: modelSelection.instanceId,+ });+ const childThreadEvent = yield* makeThreadCreatedEvent({+ idAllocator,+ threadId: childThreadId,+ now,+ });+ const childProviderThread = makeProviderThread({+ idAllocator,+ threadId: childThreadId,+ providerSessionId,+ nativeThreadId: "native-child-thread",+ now,+ });+ const parentRunId = idAllocator.derive.run({ threadId: parentThreadId, ordinal: 1 });+ yield* eventSink.write({+ events: [+ yield* makeThreadCreatedEvent({ idAllocator, threadId: parentThreadId, now }),+ {+ ...childThreadEvent,+ payload: {+ ...childThreadEvent.payload,+ createdBy: "agent",+ creationSource: "provider",+ activeProviderThreadId: null,+ lineage: {+ parentThreadId,+ relationshipToParent: "subagent",+ rootThreadId: parentThreadId,+ },+ },+ },+ {+ id: yield* idAllocator.allocate.event({ threadId: childThreadId }),+ type: "provider-thread.updated",+ threadId: childThreadId,+ occurredAt: now,+ payload: childProviderThread,+ },+ {+ id: yield* idAllocator.allocate.event({ threadId: parentThreadId }),+ type: "subagent.updated",+ threadId: parentThreadId,+ occurredAt: now,+ payload: {+ id: idAllocator.derive.nodeFromProviderItem({+ driver: CODEX_DRIVER,+ nativeItemId: "native-child-task",+ }),+ threadId: parentThreadId,+ runId: parentRunId,+ parentNodeId: idAllocator.derive.rootNode({ runId: parentRunId }),+ origin: "provider_native",+ createdBy: "agent",+ driver: CODEX_DRIVER,+ providerInstanceId: modelSelection.instanceId,+ providerThreadId: childProviderThread.id,+ childThreadId,+ nativeTaskRef: {+ driver: CODEX_DRIVER,+ nativeId: "native-child-task",+ strength: "strong",+ },+ prompt: "Reply READY",+ title: "Native child",+ model: null,+ status: "completed",+ result: "READY",+ startedAt: now,+ completedAt: now,+ updatedAt: now,+ },+ },+ ],+ });+ const parent = { threadId: parentThreadId, providerSessionId };+ const runtime = yield* manager.open({ ...parent, modelSelection, runtimePolicy });+ const resumeChild = runtime.resumeThread({+ providerThread: childProviderThread,+ threadId: childThreadId,+ modelSelection,+ runtimePolicy,+ });+ return { manager, parent, resumeChild };+});+function unimplemented(detail: string) {return Effect.fail(new ProviderAdapter.ProviderAdapterProtocolError({@@ -3533,6 +3629,40 @@ it.effect(}),);+it.effect(+ "ProviderSessionManagerV2 keeps an attached native child loaded when its parent detaches",+ () =>+ Effect.gen(function* () {+ const state = yield* Ref.make(emptyState);+ yield* Effect.gen(function* () {+ const { manager, parent, resumeChild } = yield* makeNativeChildFixture();+ yield* resumeChild;+ yield* manager.detach(parent);++ assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, []);+ // The child's next turn trusts the manager's loaded cache, so the+ // native thread must still be loaded for that turn to be heard.+ yield* resumeChild;+ assert.equal((yield* Ref.get(state)).resumeCount, 1);+ assert.isTrue(yield* manager.isLive(parent.providerSessionId));+ }).pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1000 })), Effect.scoped);+ }),+);++it.effect(+ "ProviderSessionManagerV2 unloads an unattached native child when its parent detaches",+ () =>+ Effect.gen(function* () {+ const state = yield* Ref.make(emptyState);+ yield* Effect.gen(function* () {+ const { manager, parent } = yield* makeNativeChildFixture();+ yield* manager.detach(parent);++ assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, ["native-child-thread"]);+ }).pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1000 })), Effect.scoped);+ }),+);+it.effect("ProviderSessionManagerV2 re-attaching a thread waits for its in-flight unload, then reloads it",() =>diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.tsindex 03a7278774a..f06011676c6 100644--- a/apps/server/src/orchestration-v2/ProviderSessionManager.ts+++ b/apps/server/src/orchestration-v2/ProviderSessionManager.ts@@ -2509,38 +2509,38 @@ export const layerWithOptions = (}const unloadThread = detached.value.exposedRuntime.unloadThread;if (detached.value.supportsMultipleProviderThreads && unloadThread !== undefined) {- // Serialize unload with attachment so attached threads stay loaded and later attaches resume after unload.- yield* threadAttachment.withLock(- threadAttachmentKey(input),- Effect.gen(function* () {- const entry = (yield* Ref.get(sessions)).get(key);- if (- entry?.runtime !== detached.value.runtime ||- entry.attachedThreadIds.has(input.threadId)- ) {- return;- }- yield* Effect.forEach(- detachedProviderThreads.filter((thread) => thread.nativeThreadRef !== null),- (providerThread) =>- unloadThread({ providerThread }).pipe(- Effect.timeout(UNLOAD_THREAD_TIMEOUT_MS),- Effect.catchCause((cause) =>- Effect.logWarning(- "orchestration-v2.driver-session.detach-unload-failed",- {- providerSessionId: input.providerSessionId,- threadId: input.threadId,- providerThreadId: providerThread.id,- cause,- },- ),- ),+ // The projection includes native children owned by other app threads;+ // lock each owner so an attached child stays loaded and later attaches wait.+ for (const providerThread of detachedProviderThreads) {+ if (providerThread.nativeThreadRef === null) continue;+ const ownerThreadId = providerThread.appThreadId ?? input.threadId;+ yield* threadAttachment.withLock(+ threadAttachmentKey({+ providerSessionId: input.providerSessionId,+ threadId: ownerThreadId,+ }),+ Effect.gen(function* () {+ const entry = (yield* Ref.get(sessions)).get(key);+ if (+ entry?.runtime !== detached.value.runtime ||+ entry.attachedThreadIds.has(ownerThreadId)+ ) {+ return;+ }+ yield* unloadThread({ providerThread }).pipe(+ Effect.timeout(UNLOAD_THREAD_TIMEOUT_MS),+ Effect.catchCause((cause) =>+ Effect.logWarning("orchestration-v2.driver-session.detach-unload-failed", {+ providerSessionId: input.providerSessionId,+ threadId: ownerThreadId,+ providerThreadId: providerThread.id,+ cause,+ }),),- { concurrency: 1, discard: true },- );- }),- );+ );+ }),+ );+ }}yield* scheduleIdleRelease(input.providerSessionId);},