返回 DeepSeek-Reasonix
goal_work_duration.go
根目录 / internal / control / goal_work_duration.go
1 package control
2
3 import (
4 "context"
5 "errors"
6 "log/slog"
7 "sync"
8 "time"
9
10 goaldomain "reasonix/internal/goal"
11 "reasonix/internal/session"
12 )
13
14 // goalWorkSpan belongs to one foreground Run, not to the lifetime of a goal.
15 // Its process-local clock is never restored, so pauses and downtime add no work.
16 type goalWorkSpan struct {
17 runtime *session.Runtime
18 epoch string
19 token uint64
20 goalID string
21 started time.Time
22 once sync.Once
23 err error
24 }
25
26 func (c *Controller) beginGoalWork() *goalWorkSpan {
27 _, runtime, exclusive := c.v3Binding()
28 if !exclusive || runtime == nil {
29 return nil
30 }
31 view, err := c.goalLifecycleView()
32 if err != nil || view == nil || view.Phase != goaldomain.PhaseActive || view.Activation != goaldomain.ActivationArmed {
33 return nil
34 }
35 snapshot := runtime.StateSnapshot()
36 if snapshot.Phase != session.RuntimeRunning {
37 return nil
38 }
39 // Runtime activity revisions also advance on cancellation; the turn token
40 // stays stable until this body returns, including its cancelled exit.
41 token, _, active := c.currentTurnToken()
42 if !active {
43 return nil
44 }
45 return &goalWorkSpan{runtime: runtime, epoch: snapshot.Epoch, token: token, goalID: view.ID, started: time.Now()}
46 }
47
48 func (c *Controller) finishGoalWork(span *goalWorkSpan, ended time.Time) error {
49 if span == nil {
50 return nil
51 }
52 span.once.Do(func() {
53 duration := max(int64(1), ended.Sub(span.started).Milliseconds())
54 _, span.err = c.applyHostGoalMutationForRuntime(context.Background(), "work-duration", span.runtime, func(machine *goaldomain.Machine) (*goaldomain.View, error) {
55 _, runtime, exclusive := c.v3Binding()
56 if !exclusive || runtime != span.runtime {
57 return nil, errStaleGoalWork
58 }
59 snapshot := runtime.StateSnapshot()
60 if snapshot.Epoch != span.epoch ||
61 (snapshot.Phase != session.RuntimeRunning && snapshot.Phase != session.RuntimeCancelling) {
62 return nil, errStaleGoalWork
63 }
64 token, _, active := c.currentTurnToken()
65 if !active || token != span.token {
66 return nil, errStaleGoalWork
67 }
68 current := machine.Get()
69 if current == nil || current.ID != span.goalID {
70 return nil, errStaleGoalWork
71 }
72 // Pausing/completing may change the lifecycle revision inside this Run.
73 // It must still receive its work, but a replacement goal must never do so.
74 view, err := machine.AddWorkDuration(current.Ref(), duration)
75 return &view, err
76 })
77 if errors.Is(span.err, errStaleGoalWork) {
78 span.err = nil
79 }
80 })
81 return span.err
82 }
83
84 var errStaleGoalWork = errors.New("goal work belongs to a retired activity")
85
86 // recordGoalWork keeps observational persistence failures out of the Run result.
87 // The failed candidate stays unpublished; core turn durability is unchanged.
88 func (c *Controller) recordGoalWork(span *goalWorkSpan, ended time.Time) {
89 if err := c.finishGoalWork(span, ended); err != nil {
90 slog.Warn("controller: persist goal work duration", "err", err)
91 }
92 }
93
93 lines GO