返回 DeepSeek-Reasonix
goal_lifecycle.go
根目录 / internal / tool / goal_lifecycle.go
1 package tool
2
3 import (
4 "context"
5
6 "reasonix/internal/goal"
7 )
8
9 type GoalSource string
10
11 const (
12 GoalSourceDirectHuman GoalSource = "direct-human"
13 GoalSourceGoalRound GoalSource = "goal-round"
14 )
15
16 type GoalAction string
17
18 const (
19 GoalActionCreate GoalAction = "create"
20 GoalActionEdit GoalAction = "edit"
21 GoalActionPause GoalAction = "pause"
22 GoalActionResume GoalAction = "resume"
23 GoalActionComplete GoalAction = "complete"
24 GoalActionBlocked GoalAction = "blocked"
25 GoalActionClear GoalAction = "clear"
26 )
27
28 // GoalAuthority contains only host-attested execution identity. Model text and
29 // persisted messages cannot construct one; the top-level runtime supplies it
30 // after admitting a direct human turn or an exact autonomous goal round.
31 type GoalAuthority struct {
32 Source GoalSource
33 SessionID string
34 RuntimeEpoch string
35 ActivityID uint64
36 GoalID string
37 Revision uint64
38 Round uint64
39 }
40
41 func (a GoalAuthority) Allows(action GoalAction) bool {
42 switch a.Source {
43 case GoalSourceDirectHuman:
44 return action == GoalActionCreate || action == GoalActionEdit ||
45 action == GoalActionPause || action == GoalActionResume ||
46 action == GoalActionComplete || action == GoalActionBlocked ||
47 action == GoalActionClear
48 case GoalSourceGoalRound:
49 return action == GoalActionComplete || action == GoalActionBlocked
50 default:
51 return false
52 }
53 }
54
55 type GoalUpdateRequest struct {
56 Ref goal.Ref
57 Action GoalAction
58 Objective *string
59 MaxGoalRounds goal.RoundLimitChange
60 BlockedReason *goal.BlockReason
61 }
62
63 // GoalLifecycleOwner is the single host-owned bridge between model-facing
64 // tools and the durable goal service. Implementations must revalidate Authority
65 // against the current session runtime immediately before accepting a mutation.
66 type GoalLifecycleOwner interface {
67 GetGoal(context.Context) (*goal.View, error)
68 CreateGoal(context.Context, goal.CreateRequest, GoalAuthority) (goal.View, error)
69 UpdateGoal(context.Context, GoalUpdateRequest, GoalAuthority) (goal.View, error)
70 }
71
72 type GoalLifecycleBinding struct {
73 Owner GoalLifecycleOwner
74 Authority GoalAuthority
75 }
76
77 type goalLifecycleKey struct{}
78 type noGoalLifecycle struct{}
79
80 func WithGoalLifecycle(ctx context.Context, owner GoalLifecycleOwner, authority GoalAuthority) context.Context {
81 if ctx == nil {
82 ctx = context.Background()
83 }
84 if owner == nil {
85 return ctx
86 }
87 return context.WithValue(ctx, goalLifecycleKey{}, GoalLifecycleBinding{Owner: owner, Authority: authority})
88 }
89
90 func WithoutGoalLifecycle(ctx context.Context) context.Context {
91 if ctx == nil {
92 ctx = context.Background()
93 }
94 return context.WithValue(ctx, goalLifecycleKey{}, noGoalLifecycle{})
95 }
96
97 func GoalLifecycleFromContext(ctx context.Context) (GoalLifecycleBinding, bool) {
98 if ctx == nil {
99 return GoalLifecycleBinding{}, false
100 }
101 binding, ok := ctx.Value(goalLifecycleKey{}).(GoalLifecycleBinding)
102 return binding, ok && binding.Owner != nil
103 }
104
104 lines GO