| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | |
| 6 | "reasonix/internal/event" |
| 7 | "reasonix/internal/provider" |
| 8 | "reasonix/internal/secrets" |
| 9 | "reasonix/internal/session" |
| 10 | ) |
| 11 | |
| 12 | // One bounded, optional record shares the terminal commit. Cold exports can |
| 13 | // inspect it without a resident controller; older readers skip it safely. |
| 14 | func (c *Controller) providerDiagnosticEvent(e event.Event) (*session.Event, error) { |
| 15 | status := e.Status |
| 16 | if status == "" { |
| 17 | status = terminalTurnStatus(e) |
| 18 | } |
| 19 | if status != event.TurnFailed && status != event.TurnInterrupted && status != event.TurnRecoveryRequired { |
| 20 | return nil, nil |
| 21 | } |
| 22 | failure := e.Diagnostic |
| 23 | if failure == nil { |
| 24 | failure = provider.DiagnoseFailure(e.Err) |
| 25 | } |
| 26 | requests, dropped, truncated := c.providerDiagnosticTurnSnapshot(e.TurnID) |
| 27 | if len(requests) == 0 && failure == nil && (dropped == nil || *dropped == 0) { |
| 28 | return nil, nil |
| 29 | } |
| 30 | payload, err := json.Marshal(struct { |
| 31 | SchemaVersion int `json:"schemaVersion"` |
| 32 | Failure *provider.FailureDiagnostic `json:"failure,omitempty"` |
| 33 | RequestLimit int `json:"requestLimit"` |
| 34 | Requests []providerDiagnostic `json:"requests"` |
| 35 | Dropped *uint64 `json:"dropped,omitempty"` |
| 36 | Truncated bool `json:"truncated"` |
| 37 | }{1, failure, 128, requests, dropped, truncated}) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | payload, err = secrets.RedactJSON(payload) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | return &session.Event{Kind: "diagnostic/provider", Optional: true, Payload: payload}, nil |
| 46 | } |
| 47 |