@@ -10436,7 +10436,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio |
10436 | 10436 | Effect.gen(function* () { |
10437 | 10437 | const childControls = yield* projectionStore.getThreadRecords( |
10438 | 10438 | childThreadId, |
10439 | | - ["runs", "messages", "subagents", "providerThreads", "providerTurns", "attempts"], |
| 10439 | + [ |
| 10440 | + "runs", |
| 10441 | + "messages", |
| 10442 | + "subagents", |
| 10443 | + "providerThreads", |
| 10444 | + "providerTurns", |
| 10445 | + "attempts", |
| 10446 | + "runtimeRequests", |
| 10447 | + ], |
10440 | 10448 | { messageRoles: ["user"] }, |
10441 | 10449 | ); |
10442 | 10450 | const parentThreadId = childControls.thread.lineage.parentThreadId; |
@@ -10470,6 +10478,35 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio |
10470 | 10478 | ...(progress.resultRun === undefined ? {} : { resultRun: progress.resultRun }), |
10471 | 10479 | }); |
10472 | 10480 | } |
| 10481 | + |
| 10482 | + const { resultRun } = progress; |
| 10483 | + if (progress.state !== "result_available" || resultRun === undefined) return; |
| 10484 | + const status = delegatedTaskTerminalStatus(runOutcome(resultRun)); |
| 10485 | + if (status !== "cancelled" && status !== "interrupted") return; |
| 10486 | + if ( |
| 10487 | + yield* childAwaitsRestartContinuation( |
| 10488 | + childControls.runs, |
| 10489 | + resultRun, |
| 10490 | + options?.settledContinuationOf, |
| 10491 | + ) |
| 10492 | + ) |
| 10493 | + return; |
| 10494 | + // Completed or failed tasks keep async questions: answering resumes the child. |
| 10495 | + // Callers hold the parent lock; dismiss takes the child lock, as follow-up intake does. |
| 10496 | + for (const request of childControls.runtimeRequests) { |
| 10497 | + if ( |
| 10498 | + request.status === "pending" && |
| 10499 | + request.kind === "user_input" && |
| 10500 | + request.responseCapability.type === "message" |
| 10501 | + ) { |
| 10502 | + yield* dispatchWithReceipt({ |
| 10503 | + type: "thread.user-input.dismiss", |
| 10504 | + commandId: CommandId.make(`command:system:delegated-task-question:${request.id}`), |
| 10505 | + threadId: childThreadId, |
| 10506 | + requestId: request.id, |
| 10507 | + }); |
| 10508 | + } |
| 10509 | + } |
10473 | 10510 | }); |
10474 | 10511 |
|
10475 | 10512 | const finalizeDelegatedCompletionDelivery = (threadId: ThreadId, runId: RunId) => |
@@ -11252,85 +11289,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio |
11252 | 11289 | : threadDispatch.withLock(parentThreadId, dispatch); |
11253 | 11290 | }); |
11254 | 11291 |
|
11255 | | - const delegatedTaskResultPending = ( |
11256 | | - childThreadId: ThreadId, |
11257 | | - taskId?: NodeId, |
11258 | | - settledContinuationOf?: RunId, |
11259 | | - ) => |
11260 | | - Effect.gen(function* () { |
11261 | | - const child = yield* projectionStore.getThreadRecords( |
11262 | | - childThreadId, |
11263 | | - ["runs", "messages", "subagents", "providerThreads", "providerTurns", "attempts"], |
11264 | | - { messageRoles: ["user"] }, |
11265 | | - ); |
11266 | | - if (taskId !== undefined) { |
11267 | | - const runs = child.runs.filter((run) => run.delegatedTaskId === taskId); |
11268 | | - const run = runs.toSorted((a, b) => b.ordinal - a.ordinal)[0]; |
11269 | | - if (run === undefined || delegatedTaskTerminalStatus(run.status) === null) return true; |
11270 | | - return yield* childAwaitsRestartContinuation(runs, run, settledContinuationOf); |
11271 | | - } |
11272 | | - const progress = delegatedTaskProgress(child); |
11273 | | - // A caller's older read saw a result; newer work since then means it is not final. |
11274 | | - if (progress.state !== "result_available") return true; |
11275 | | - if (progress.resultRun === undefined) return false; |
11276 | | - return yield* childAwaitsRestartContinuation( |
11277 | | - child.runs, |
11278 | | - progress.resultRun, |
11279 | | - settledContinuationOf, |
11280 | | - ); |
11281 | | - }).pipe( |
11282 | | - Effect.mapError( |
11283 | | - (cause) => new OrchestratorProjectionError({ threadId: childThreadId, cause }), |
11284 | | - ), |
11285 | | - ); |
11286 | | - |
11287 | | - // Async questions do not stop a delegated child's run. A cancelled or |
11288 | | - // interrupted task's questions close, since nobody wants that work anymore. |
11289 | | - // A finished task keeps them: answering one resumes the child. The parent |
11290 | | - // lock is held throughout, so a follow-up run cannot start between the |
11291 | | - // check and the dismissal. |
11292 | | - const finishAppOwnedSubagent = ( |
11293 | | - parentThreadId: ThreadId, |
11294 | | - childThreadId: ThreadId, |
11295 | | - options?: { readonly settledContinuationOf?: RunId }, |
11296 | | - ) => |
11297 | | - threadDispatch.withLock( |
11298 | | - parentThreadId, |
11299 | | - Effect.gen(function* () { |
11300 | | - yield* finalizeAppOwnedSubagent(childThreadId, options); |
11301 | | - const settledContinuationOf = options?.settledContinuationOf; |
11302 | | - if (yield* delegatedTaskResultPending(childThreadId, undefined, settledContinuationOf)) |
11303 | | - return; |
11304 | | - const child = yield* projectionStore.getThreadRecords( |
11305 | | - childThreadId, |
11306 | | - ["runtimeRequests", "runs", "messages", "subagents", "providerThreads"], |
11307 | | - { messageRoles: ["user"] }, |
11308 | | - ); |
11309 | | - const { resultRun } = delegatedTaskProgress(child); |
11310 | | - const status = |
11311 | | - resultRun === undefined ? null : delegatedTaskTerminalStatus(runOutcome(resultRun)); |
11312 | | - if (status !== "cancelled" && status !== "interrupted") return; |
11313 | | - for (const request of child.runtimeRequests) { |
11314 | | - if ( |
11315 | | - request.status !== "pending" || |
11316 | | - request.kind !== "user_input" || |
11317 | | - request.responseCapability.type !== "message" |
11318 | | - ) |
11319 | | - continue; |
11320 | | - // Takes the child lock inside the parent's, the order follow-up intake uses. |
11321 | | - yield* threadDispatch.withLock( |
11322 | | - childThreadId, |
11323 | | - dispatchWithReceiptEffect({ |
11324 | | - type: "thread.user-input.dismiss", |
11325 | | - commandId: CommandId.make(`command:system:delegated-task-question:${request.id}`), |
11326 | | - threadId: childThreadId, |
11327 | | - requestId: request.id, |
11328 | | - }), |
11329 | | - ); |
11330 | | - } |
11331 | | - }), |
11332 | | - ); |
11333 | | - |
11334 | 11292 | const handleTerminalRun = (stored: OrchestrationV2StoredEvent) => |
11335 | 11293 | Effect.gen(function* () { |
11336 | 11294 | const threadId = stored.event.threadId; |
@@ -11370,7 +11328,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio |
11370 | 11328 | // wakes, and only then is the failed run the task's result. |
11371 | 11329 | const parentThreadId = yield* appOwnedSubagentParentThreadId(threadId); |
11372 | 11330 | if (parentThreadId !== undefined) { |
11373 | | - yield* finishAppOwnedSubagent(parentThreadId, threadId); |
| 11331 | + yield* threadDispatch.withLock(parentThreadId, finalizeAppOwnedSubagent(threadId)); |
11374 | 11332 | } |
11375 | 11333 | }).pipe( |
11376 | 11334 | Effect.catchCause((cause) => |
@@ -11420,7 +11378,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio |
11420 | 11378 | const thread = yield* projectionStore.getThreadShell(threadId); |
11421 | 11379 | const parentThreadId = thread?.lineage.parentThreadId; |
11422 | 11380 | if (parentThreadId === undefined || parentThreadId === null) return; |
11423 | | - yield* finishAppOwnedSubagent(parentThreadId, threadId); |
| 11381 | + yield* threadDispatch.withLock(parentThreadId, finalizeAppOwnedSubagent(threadId)); |
11424 | 11382 | }).pipe( |
11425 | 11383 | Effect.catchCause((cause) => |
11426 | 11384 | Effect.logWarning("Failed to recover terminal app-owned subagent", { |
@@ -11499,13 +11457,38 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio |
11499 | 11457 | ); |
11500 | 11458 | }); |
11501 | 11459 |
|
| 11460 | + const delegatedTaskResultPending = (childThreadId: ThreadId, taskId?: NodeId) => |
| 11461 | + Effect.gen(function* () { |
| 11462 | + const child = yield* projectionStore.getThreadRecords( |
| 11463 | + childThreadId, |
| 11464 | + ["runs", "messages", "subagents", "providerThreads", "providerTurns", "attempts"], |
| 11465 | + { messageRoles: ["user"] }, |
| 11466 | + ); |
| 11467 | + if (taskId !== undefined) { |
| 11468 | + const runs = child.runs.filter((run) => run.delegatedTaskId === taskId); |
| 11469 | + const run = runs.toSorted((a, b) => b.ordinal - a.ordinal)[0]; |
| 11470 | + if (run === undefined || delegatedTaskTerminalStatus(run.status) === null) return true; |
| 11471 | + return yield* childAwaitsRestartContinuation(runs, run); |
| 11472 | + } |
| 11473 | + const progress = delegatedTaskProgress(child); |
| 11474 | + // A caller's older read saw a result; newer work since then means it is not final. |
| 11475 | + if (progress.state !== "result_available") return true; |
| 11476 | + if (progress.resultRun === undefined) return false; |
| 11477 | + return yield* childAwaitsRestartContinuation(child.runs, progress.resultRun); |
| 11478 | + }).pipe( |
| 11479 | + Effect.mapError( |
| 11480 | + (cause) => new OrchestratorProjectionError({ threadId: childThreadId, cause }), |
| 11481 | + ), |
| 11482 | + ); |
| 11483 | + |
11502 | 11484 | const recoverDelegatedTask = (threadId: ThreadId, sourceRunId: RunId) => |
11503 | 11485 | Effect.gen(function* () { |
11504 | 11486 | const parentThreadId = yield* appOwnedSubagentParentThreadId(threadId); |
11505 | 11487 | if (parentThreadId === undefined) return; |
11506 | | - yield* finishAppOwnedSubagent(parentThreadId, threadId, { |
11507 | | - settledContinuationOf: sourceRunId, |
11508 | | - }); |
| 11488 | + yield* threadDispatch.withLock( |
| 11489 | + parentThreadId, |
| 11490 | + finalizeAppOwnedSubagent(threadId, { settledContinuationOf: sourceRunId }), |
| 11491 | + ); |
11509 | 11492 | }).pipe( |
11510 | 11493 | Effect.catchCause((cause) => |
11511 | 11494 | Effect.logWarning("Failed to recover delegated task after restart", { |
|