返回 DeepSeek-Reasonix
goal_lifecycle_binding.go
根目录 / internal / control / goal_lifecycle_binding.go
1 package control
2
3 import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "fmt"
8 "strings"
9 "time"
10
11 goaldomain "reasonix/internal/goal"
12 "reasonix/internal/session"
13 )
14
15 type legacyGoalProjection struct {
16 Goal string `json:"goal"`
17 Status string `json:"status"`
18 TurnsUsed int `json:"turnsUsed"`
19 WorkDurationMs int64 `json:"workDurationMs,omitempty"`
20 Block string `json:"block"`
21 }
22
23 // goalLifecycleFromProjection is the only compatibility boundary between
24 // imported legacy goal sidecars and the current versioned goal domain. It does
25 // not write during cold open and never restores todo or process activation.
26 func goalLifecycleFromProjection(raw json.RawMessage, sessionID string, createdAt time.Time) (*goaldomain.Machine, error) {
27 machine := goaldomain.NewMachine(nil, nil)
28 if len(raw) == 0 {
29 return machine, nil
30 }
31 var header struct {
32 Version *int `json:"version"`
33 }
34 if err := json.Unmarshal(raw, &header); err != nil {
35 return machine, fmt.Errorf("decode goal projection: %w", err)
36 }
37 if header.Version != nil {
38 if _, err := machine.Restore(raw); err != nil {
39 return machine, err
40 }
41 return machine, nil
42 }
43 return importLegacyGoalProjection(machine, raw, sessionID, createdAt)
44 }
45
46 func importLegacyGoalProjection(machine *goaldomain.Machine, raw json.RawMessage, sessionID string, createdAt time.Time) (*goaldomain.Machine, error) {
47 var legacy legacyGoalProjection
48 if err := json.Unmarshal(raw, &legacy); err != nil {
49 return machine, fmt.Errorf("decode legacy goal projection: %w", err)
50 }
51 legacy.Goal = strings.TrimSpace(legacy.Goal)
52 if legacy.Goal == "" {
53 return machine, nil
54 }
55 if legacy.TurnsUsed < 0 {
56 return machine, fmt.Errorf("legacy goal has negative admitted rounds")
57 }
58 var phase goaldomain.Phase
59 var blockedReason *goaldomain.BlockReason
60 switch strings.TrimSpace(legacy.Status) {
61 case GoalStatusRunning:
62 phase = goaldomain.PhaseActive
63 case GoalStatusComplete:
64 phase = goaldomain.PhaseComplete
65 case GoalStatusBlocked:
66 phase = goaldomain.PhaseBlocked
67 message := strings.TrimSpace(legacy.Block)
68 if message == "" {
69 message = "legacy goal stopped without a recorded reason"
70 }
71 blockedReason = &goaldomain.BlockReason{Code: "legacy-blocked", Message: message}
72 case "", GoalStatusStopped:
73 phase = goaldomain.PhasePaused
74 default:
75 return machine, fmt.Errorf("legacy goal has unsupported status %q", legacy.Status)
76 }
77 if createdAt.IsZero() {
78 createdAt = time.Unix(0, 0).UTC()
79 } else {
80 createdAt = createdAt.UTC()
81 }
82 hash := sha256.Sum256(append(append([]byte(sessionID), 0), raw...))
83 id := "legacy-" + hex.EncodeToString(hash[:12])
84 document := map[string]any{
85 "version": goaldomain.StateVersion,
86 "current": goaldomain.Snapshot{
87 ID: id, Revision: 1, Objective: legacy.Goal, Phase: phase,
88 MaxGoalRounds: nil, RoundsStarted: uint64(legacy.TurnsUsed), WorkDurationMs: legacy.WorkDurationMs,
89 BlockedReason: blockedReason, CreatedAt: createdAt, UpdatedAt: createdAt,
90 },
91 "legacyState": json.RawMessage(append([]byte(nil), raw...)),
92 }
93 encoded, err := json.Marshal(document)
94 if err != nil {
95 return machine, err
96 }
97 if _, err := machine.Restore(encoded); err != nil {
98 return machine, err
99 }
100 return machine, nil
101 }
102
103 func (c *Controller) installGoalLifecycle(runtime *session.Runtime) {
104 machine := goaldomain.NewMachine(nil, nil)
105 var loadErr error
106 if runtime != nil && runtime.Session() != nil {
107 snapshot := runtime.Session().ExecutionSnapshot()
108 createdAt := runtime.Session().Handle().Manifest().CreatedAt
109 machine, loadErr = goalLifecycleFromProjection(snapshot.Projection.GoalState, runtime.Ref().SessionID, createdAt)
110 }
111 c.goalLifecycleMu.Lock()
112 c.goalLifecycle = machine
113 c.goalLifecycleLoadErr = loadErr
114 c.goalLifecycleMu.Unlock()
115 }
116
117 func (c *Controller) goalLifecycleView() (*goaldomain.View, error) {
118 if c == nil {
119 return nil, nil
120 }
121 c.goalLifecycleMu.RLock()
122 machine, loadErr := c.goalLifecycle, c.goalLifecycleLoadErr
123 c.goalLifecycleMu.RUnlock()
124 if loadErr != nil {
125 return nil, loadErr
126 }
127 if machine == nil {
128 return nil, nil
129 }
130 return machine.Get(), nil
131 }
132
132 lines GO