| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | |
| 7 | "reasonix/internal/event" |
| 8 | "reasonix/internal/provider" |
| 9 | "reasonix/internal/secrets" |
| 10 | "reasonix/internal/session" |
| 11 | ) |
| 12 | |
| 13 | // Preparation can fail before Agent.Run writes an interrupted record. Keep |
| 14 | // that cause in the existing optional display format, atomically with turn/end. |
| 15 | // Agent failures already have a canonical local-only notice; never duplicate it. |
| 16 | func (c *Controller) terminalFailureDisplayEvents(e event.Event, projection session.Projection) ([]session.Event, error) { |
| 17 | if e.Err == nil || e.Cancelled || errors.Is(e.Err, context.Canceled) || e.TurnID == "" { |
| 18 | return nil, nil |
| 19 | } |
| 20 | for _, m := range projection.Messages { |
| 21 | if c.turnEvents.turnMessageIDs[m.ID] && hasFailureCause(m) { |
| 22 | return nil, nil |
| 23 | } |
| 24 | } |
| 25 | if p := c.turnEvents.pendingTermination; p != nil && p.TurnID == e.TurnID { |
| 26 | for _, m := range p.Messages { |
| 27 | if c.turnEvents.turnMessageIDs[m.ID] && hasFailureCause(m) { |
| 28 | return nil, nil |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | cause := secrets.FailureDetail(e.Err) |
| 33 | return displayNoticeEvents(event.Event{Kind: event.Notice, Level: event.LevelWarn, Code: event.NoticeCodeProviderRequestFailed, |
| 34 | MessageID: "failure:" + e.TurnID, TurnID: e.TurnID, Text: cause, Detail: cause, Diagnostic: provider.DiagnoseFailure(e.Err)}) |
| 35 | } |
| 36 | |
| 37 | func hasFailureCause(m provider.Message) bool { |
| 38 | return m.LocalOnly && m.InterruptedTurn != nil && m.InterruptedTurn.TerminalStatus == "failed" && m.InterruptedTurn.Cause != "" |
| 39 | } |
| 40 |