(
sessionId: string,
decision: AgentRunRecoveryDecision,
inspected: AgentRunInspectModel,
policy: RecoveryPolicy = { kind: 'best_effort' },
)
| 5798 | } |
| 5799 | |
| 5800 | private async applyAgentRunRecovery( |
| 5801 | sessionId: string, |
| 5802 | decision: AgentRunRecoveryDecision, |
| 5803 | inspected: AgentRunInspectModel, |
| 5804 | policy: RecoveryPolicy = { kind: 'best_effort' }, |
| 5805 | ): Promise<boolean> { |
| 5806 | if (!this.deps.runStore || !this.deps.runtimeEventStore) return false; |
| 5807 | const ts = this.deps.now(); |
| 5808 | const terminalLedger = classifyTerminalRuntimeLedger(inspected.header, inspected.runtimeEvents); |
| 5809 | const existingTerminal = |
| 5810 | inspected.terminalRuntimeFact?.terminalEvent ?? |
| 5811 | (terminalLedger.kind === 'incomplete_single_terminal' |
| 5812 | ? terminalLedger.terminalEvent |
| 5813 | : undefined); |
| 5814 | const status = existingTerminal |
| 5815 | ? (terminalRunStatusFromRuntimeEvent(existingTerminal) ?? decision.status) |
| 5816 | : decision.status; |
| 5817 | const failureClass = |
| 5818 | status === 'failed' ? (decision.failureClass ?? 'app_restarted') : undefined; |
| 5819 | const abortSource = status === 'cancelled' ? (decision.abortSource ?? 'unknown') : undefined; |
| 5820 | const terminalEvent = |
| 5821 | existingTerminal ?? |
| 5822 | buildRecoveredTerminalRuntimeEvent({ |
| 5823 | id: this.deps.newId(), |
| 5824 | run: inspected.header, |
| 5825 | status, |
| 5826 | ts, |
| 5827 | recoveryReason: diagnosticRecoveryReason(decision.diagnostic), |
| 5828 | ...(inspected.runtimeEvents[0]?.invocationId |
| 5829 | ? { invocationId: inspected.runtimeEvents[0].invocationId } |
| 5830 | : {}), |
| 5831 | ...(failureClass ? { failureClass, message: failureClass } : {}), |
| 5832 | ...(abortSource ? { abortSource } : {}), |
| 5833 | ...(decision.diagnostic ? { diagnostic: decision.diagnostic } : {}), |
| 5834 | }); |
| 5835 | try { |
| 5836 | await commitTerminalRunWithRuntimeFact({ |
| 5837 | runStore: this.deps.runStore, |
| 5838 | runtimeEventStore: this.deps.runtimeEventStore, |
| 5839 | newId: this.deps.newId, |
| 5840 | sessionId, |
| 5841 | runId: decision.runId, |
| 5842 | turnId: decision.turnId, |
| 5843 | status, |
| 5844 | ts, |
| 5845 | terminalEvent, |
| 5846 | ...(failureClass ? { failureClass } : {}), |
| 5847 | ...(abortSource ? { abortSource } : {}), |
| 5848 | runEventData: { recovered: true, ...decision.diagnostic }, |
| 5849 | existingEvents: inspected.events, |
| 5850 | }); |
| 5851 | } catch (error) { |
| 5852 | if (policy.kind === 'strict') throw error; |
| 5853 | return false; |
| 5854 | } |
| 5855 | |
| 5856 | await recoverOr( |
| 5857 | policy, |
no test coverage detected