(record: WorkspaceTurnTaskHandleRecord)
| 6601 | } |
| 6602 | |
| 6603 | private async settleStaleWorkspaceTurn(record: WorkspaceTurnTaskHandleRecord): Promise<void> { |
| 6604 | if (record.status !== "queued" && record.status !== "starting" && record.status !== "running") { |
| 6605 | return; |
| 6606 | } |
| 6607 | const recovered = await this.recoverTerminalWorkspaceTurnFromHistory(record); |
| 6608 | if (recovered != null) { |
| 6609 | await this.settleWorkspaceTurn({ |
| 6610 | record, |
| 6611 | next: recovered, |
| 6612 | waiterSettlement: |
| 6613 | recovered.status === "completed" |
| 6614 | ? { status: "completed", result: this.buildWorkspaceTurnWaitResult(recovered) } |
| 6615 | : { status: "error", error: new Error(recovered.error ?? "Workspace turn failed") }, |
| 6616 | }); |
| 6617 | return; |
| 6618 | } |
| 6619 | |
| 6620 | // Same-process deferred stream-ends can be observed before the final assistant message is |
| 6621 | // readable from history. Keep the handle alive in that narrow window; after restart the active |
| 6622 | // map is empty, so unrecoverable deferred handles still settle terminally instead of leaking. |
| 6623 | const active = this.activeWorkspaceTurnHandleByWorkspaceId.get(record.workspaceId); |
| 6624 | if ( |
| 6625 | (record.deferredMessageIds?.length ?? 0) > 0 && |
| 6626 | active?.handleId === record.handleId && |
| 6627 | active.ownerWorkspaceId === record.ownerWorkspaceId |
| 6628 | ) { |
| 6629 | return; |
| 6630 | } |
| 6631 | |
| 6632 | const next: WorkspaceTurnTaskHandleRecord = { |
| 6633 | ...record, |
| 6634 | status: "interrupted", |
| 6635 | updatedAt: getIsoNow(), |
| 6636 | error: "Workspace turn interrupted after restart", |
| 6637 | }; |
| 6638 | await this.settleWorkspaceTurn({ |
| 6639 | record, |
| 6640 | next, |
| 6641 | waiterSettlement: { |
| 6642 | status: "error", |
| 6643 | error: new Error("Workspace turn interrupted after restart"), |
| 6644 | }, |
| 6645 | }); |
| 6646 | } |
| 6647 | |
| 6648 | private async countActiveWorkspaceTurns( |
| 6649 | records?: readonly WorkspaceTurnTaskHandleRecord[] |
no test coverage detected