返回 DeepSeek-Reasonix
provider_diagnostic_event.go
根目录 / internal / control / provider_diagnostic_event.go
1 package control
2
3 import (
4 "context"
5 "encoding/json"
6
7 "reasonix/internal/event"
8 "reasonix/internal/provider"
9 "reasonix/internal/secrets"
10 "reasonix/internal/session"
11 )
12
13 // Both synchronous and asynchronous turns publish through the same boundary.
14 // Keep failure classification there so live and reopened notices agree.
15 func withTerminalFailureDiagnostic(e event.Event) event.Event {
16 if e.Kind == event.TurnDone {
17 if e.Diagnostic == nil {
18 cause := e.Err
19 if e.Cancelled {
20 cause = context.Canceled
21 }
22 e.Diagnostic = provider.DiagnoseFailure(cause)
23 }
24 if e.Detail == "" {
25 e.Detail = secrets.FailureDetail(e.Err)
26 if metadata := provider.FailureDiagnosticDetail(e.Diagnostic); metadata != "" {
27 if e.Detail != "" {
28 e.Detail += "\n"
29 }
30 e.Detail += metadata
31 }
32 }
33 }
34 return e
35 }
36
37 // One bounded, optional record shares the terminal commit. Cold exports can
38 // inspect it without a resident controller; older readers skip it safely.
39 func (c *Controller) providerDiagnosticEvent(e event.Event) (*session.Event, error) {
40 status := e.Status
41 if status == "" {
42 status = terminalTurnStatus(e)
43 }
44 if status != event.TurnFailed && status != event.TurnInterrupted && status != event.TurnRecoveryRequired {
45 return nil, nil
46 }
47 failure := e.Diagnostic
48 if failure == nil {
49 failure = provider.DiagnoseFailure(e.Err)
50 }
51 requests, dropped, truncated := c.providerDiagnosticTurnSnapshot(e.TurnID)
52 if len(requests) == 0 && failure == nil && (dropped == nil || *dropped == 0) {
53 return nil, nil
54 }
55 payload, err := json.Marshal(struct {
56 SchemaVersion int `json:"schemaVersion"`
57 Failure *provider.FailureDiagnostic `json:"failure,omitempty"`
58 RequestLimit int `json:"requestLimit"`
59 Requests []providerDiagnostic `json:"requests"`
60 Dropped *uint64 `json:"dropped,omitempty"`
61 Truncated bool `json:"truncated"`
62 }{2, failure, 128, requests, dropped, truncated})
63 if err != nil {
64 return nil, err
65 }
66 payload, err = secrets.RedactJSON(payload)
67 if err != nil {
68 return nil, err
69 }
70 return &session.Event{Kind: "diagnostic/provider", Optional: true, Payload: payload}, nil
71 }
72
72 lines GO