返回 DeepSeek-Reasonix
session_diagnostics.go
根目录 / internal / control / session_diagnostics.go
1 package control
2
3 import (
4 "reasonix/internal/event"
5 "sync"
6 "time"
7 )
8
9 var diagnosticProcessStartedAt = time.Now().UTC()
10
11 type lifecycleDiagnostic struct {
12 ObservedAt time.Time `json:"observedAt"`
13 Action string `json:"action"`
14 Source string `json:"source"`
15 TurnID string `json:"turnId,omitempty"`
16 Sequence uint64 `json:"sequence,omitempty"`
17 State event.TurnStatus `json:"state,omitempty"`
18 }
19 type lifecycleDiagnosticBuffer struct {
20 mu sync.Mutex
21 events []lifecycleDiagnostic
22 dropped uint64
23 }
24
25 func (c *Controller) recordLifecycle(action, source, turnID string, sequence uint64, status event.TurnStatus) {
26 if c == nil {
27 return
28 }
29 buffer := &c.lifecycleDiagnostics
30 buffer.mu.Lock()
31 defer buffer.mu.Unlock()
32 if len(buffer.events) == 256 {
33 copy(buffer.events, buffer.events[1:])
34 buffer.events = buffer.events[:255]
35 buffer.dropped++
36 }
37 buffer.events = append(buffer.events, lifecycleDiagnostic{ObservedAt: time.Now().UTC(), Action: action, Source: source, TurnID: turnID, Sequence: sequence, State: status})
38 }
39 func (c *Controller) lifecycleDiagnosticSnapshot() any {
40 buffer := &c.lifecycleDiagnostics
41 buffer.mu.Lock()
42 defer buffer.mu.Unlock()
43 events := append([]lifecycleDiagnostic{}, buffer.events...)
44 return struct {
45 ProcessStartedAt time.Time `json:"processStartedAt"`
46 Events []lifecycleDiagnostic `json:"events"`
47 Dropped uint64 `json:"dropped"`
48 Scope string `json:"scope"`
49 }{diagnosticProcessStartedAt, events, buffer.dropped, "current controller lifetime; earlier and discarded observations are unavailable"}
50 }
51
52 func (c *Controller) recordTurnLifecycle(e event.Event) {
53 if e.Kind == event.TurnStarted || e.Kind == event.TurnDone || e.Kind == event.TurnStatusChanged {
54 c.recordLifecycle("turn_status", "controller", e.TurnID, e.Sequence, e.Status)
55 }
56 }
57
57 lines GO