(reason: 'completed' | 'aborted' = 'completed')
| 528 | } |
| 529 | |
| 530 | async endTurn(reason: 'completed' | 'aborted' = 'completed'): Promise<void> { |
| 531 | const turnId = this.turnId; |
| 532 | const boundaryRequests = this.sandboxBoundaryRequests.entries(); |
| 533 | const hasHostedBoundaryPending = boundaryRequests.some(([, request]) => request.hosted); |
| 534 | const boundarySettlementErrors: unknown[] = []; |
| 535 | const embeddedBoundaryRequests = boundaryRequests.filter(([, request]) => !request.hosted); |
| 536 | if (embeddedBoundaryRequests.length > 0) { |
| 537 | if (!this.input.settleSandboxBoundaryRequest) { |
| 538 | boundarySettlementErrors.push( |
| 539 | new Error('Sandbox boundary settlement is unavailable on this surface'), |
| 540 | ); |
| 541 | } else { |
| 542 | const results = await Promise.allSettled( |
| 543 | embeddedBoundaryRequests.map(async ([requestId, metadata]) => { |
| 544 | try { |
| 545 | await metadata.creation; |
| 546 | } catch { |
| 547 | return; |
| 548 | } |
| 549 | await this.input.settleSandboxBoundaryRequest?.({ |
| 550 | sessionId: this.input.sessionId, |
| 551 | requestId, |
| 552 | decision: 'deny', |
| 553 | }); |
| 554 | }), |
| 555 | ); |
| 556 | for (const result of results) { |
| 557 | if (result.status === 'rejected') boundarySettlementErrors.push(result.reason); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | const hasHostedPending = this.userQuestions.entries().some(([, question]) => question.hosted); |
| 563 | if (hasHostedBoundaryPending) { |
| 564 | this.sandboxBoundaryClosureDeferred = true; |
| 565 | this.finishDeferredSandboxBoundaryTurnClosure(); |
| 566 | } else { |
| 567 | this.sandboxBoundaryRequests.close( |
| 568 | (requestId) => |
| 569 | new Error(`Turn ${turnId} ${reason} before sandbox boundary ${requestId} was settled`), |
| 570 | ); |
| 571 | this.sandboxBoundaryClosureDeferred = false; |
| 572 | } |
| 573 | if (hasHostedPending) { |
| 574 | this.questionClosureDeferred = true; |
| 575 | this.finishDeferredQuestionTurnClosure(); |
| 576 | } else { |
| 577 | this.userQuestions.close( |
| 578 | (requestId) => |
| 579 | new Error(`Turn ${turnId} ${reason} before user question ${requestId} was answered`), |
| 580 | ); |
| 581 | this.questionClosureDeferred = false; |
| 582 | } |
| 583 | this.resetTurnState(); |
| 584 | // The stop path settles the run's terminal fact right after the |
| 585 | // backend's stop resolves, and that stop awaits this method. Unwinds |
| 586 | // already in flight commit their T2 outcomes on their own microtask |
| 587 | // chains, so wait for them here: the terminal event stays the ledger's |
no test coverage detected