返回 DeepSeek-Reasonix
submit_plan.go
根目录 / internal / agent / submit_plan.go
1 package agent
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7
8 "reasonix/internal/plancontract"
9 "reasonix/internal/tool"
10 )
11
12 // SubmitPlanTool is the planner's structured exit: it hands the host a plan as
13 // data instead of prose the host would have to parse back. Validation runs here,
14 // so a malformed plan returns an actionable tool error the planner can fix in
15 // the next round rather than a silent misparse downstream.
16 type SubmitPlanTool struct{}
17
18 // finalizesTurn marks submit_plan as a host-consumed terminal tool. The marker
19 // is deliberately package-private: arbitrary plugin tools cannot opt into
20 // ending an Agent.Run without an owning host contract.
21 func (*SubmitPlanTool) finalizesTurn() {}
22
23 func NewSubmitPlanTool() *SubmitPlanTool { return &SubmitPlanTool{} }
24
25 func (*SubmitPlanTool) Name() string { return tool.HostSubmitPlan }
26
27 func (*SubmitPlanTool) Description() string {
28 return "Submit your finished plan as structured data. This is how a plan reaches the host — the host renders it for the user and hands it to the executor, so do NOT also restate the plan in prose. Every step needs a `title`; a step with a `parent_id` is a sub-step of that phase (two levels, keep phases few). Record what you actually READ as `verified_files` and what you only INFERRED as `candidate_files` — never present a guess as a verified path. Attach `acceptance` criteria and command-level `verification` to the steps they belong to, mark must-keep-passing behavior with `regression`, and label anything unproven in `assumptions`. Set `requires_approval` when execution should stop for the user first; the host decides whether it actually gates."
29 }
30
31 func (*SubmitPlanTool) Schema() json.RawMessage {
32 return json.RawMessage(`{
33 "type":"object",
34 "properties":{
35 "objective":{"type":"string","description":"What this plan achieves, in one sentence."},
36 "assumptions":{
37 "type":"array",
38 "description":"Premises the plan rests on that you did NOT verify. Label them here instead of stating them as facts.",
39 "items":{
40 "type":"object",
41 "properties":{
42 "text":{"type":"string","description":"The unverified premise."},
43 "confirm":{"type":"string","description":"The cheapest check that would settle it."}
44 },
45 "required":["text"]
46 }
47 },
48 "non_goals":{"type":"array","items":{"type":"string"},"description":"Explicitly out of scope."},
49 "steps":{
50 "type":"array",
51 "minItems":1,
52 "description":"Ordered steps. Top-level steps are phases; give a step a parent_id to make it a sub-step of that phase.",
53 "items":{
54 "type":"object",
55 "properties":{
56 "id":{"type":"string","description":"Short id for this step, e.g. \"s1\". Only needed when another step references it."},
57 "parent_id":{"type":"string","description":"The id of the phase this step belongs to. Omit for a phase."},
58 "title":{"type":"string","description":"Imperative description of the step."},
59 "depends_on":{"type":"array","items":{"type":"string"},"description":"Ids of sibling steps that must happen first."},
60 "verified_files":{"type":"array","items":{"type":"string"},"description":"Paths you actually opened and read."},
61 "candidate_files":{"type":"array","items":{"type":"string"},"description":"Paths you inferred but did NOT read. Never list an unread path as verified."},
62 "acceptance":{
63 "type":"array",
64 "description":"Checkable conditions this step must meet.",
65 "items":{
66 "type":"object",
67 "properties":{
68 "text":{"type":"string","description":"The condition, stated so it can be checked."},
69 "regression":{"type":"boolean","description":"True when this is existing behavior that must keep working."},
70 "optional":{"type":"boolean","description":"True for a nice-to-have that must never block completion."}
71 },
72 "required":["text"]
73 }
74 },
75 "verification":{
76 "type":"array",
77 "description":"Commands that prove the step.",
78 "items":{
79 "type":"object",
80 "properties":{
81 "command":{"type":"string","description":"The command as the executor should run it."},
82 "expect":{"type":"string","description":"What a pass looks like."}
83 }
84 }
85 },
86 "risks":{"type":"array","items":{"type":"string"},"description":"What could go wrong in this step."}
87 },
88 "required":["title"]
89 }
90 },
91 "requires_approval":{"type":"boolean","description":"Request that execution stop for explicit user approval. The host owns the final decision."}
92 },
93 "required":["objective","steps"]
94 }`)
95 }
96
97 // ReadOnly is true: submitting a plan records a proposal and touches nothing.
98 func (*SubmitPlanTool) ReadOnly() bool { return true }
99
100 // ProviderVisible gates on the host having armed a planning turn. The schema
101 // stays constant for cache stability and availability is decided when the call
102 // runs.
103 func (*SubmitPlanTool) ProviderVisible(ctx context.Context) bool {
104 _, ok := planSubmissionFromContext(ctx)
105 return ok
106 }
107
108 func (*SubmitPlanTool) Execute(ctx context.Context, args json.RawMessage) (string, error) {
109 submission, ok := planSubmissionFromContext(ctx)
110 if !ok {
111 return "", fmt.Errorf("submit_plan is only available while planning; there is no plan to submit in this phase")
112 }
113 var plan plancontract.Plan
114 if err := json.Unmarshal(args, &plan); err != nil {
115 return "", fmt.Errorf("invalid args: %w", err)
116 }
117 plan = plan.Normalize()
118 if err := plan.Validate(); err != nil {
119 return "", fmt.Errorf("plan not accepted: %w", err)
120 }
121 plan = submission.record(plan)
122 phases, subSteps := 0, 0
123 for _, step := range plan.Steps {
124 if step.ParentID == "" {
125 phases++
126 continue
127 }
128 subSteps++
129 }
130 approval := ""
131 if plan.RequiresApproval {
132 approval = " Approval was requested; the host decides whether execution gates."
133 }
134 // A revision is told what it changed. Left to describe its own edit a model
135 // reports intent, not effect, and a step it dropped by accident reads the
136 // same as one it kept.
137 revised := ""
138 if diff, ok := submission.Revised(); ok && diff.Moved() {
139 revised = "\n\n" + plancontract.RenderDiff(diff)
140 if diff.NeedsApproval() {
141 revised += "\n\nThis revision expands the approved scope; the host may gate it again."
142 }
143 }
144 return fmt.Sprintf(
145 "Plan revision %d accepted: %d phase(s), %d sub-step(s). The host renders it for the user and hands it to the executor — do not restate it.%s%s",
146 plan.Revision, phases, subSteps, approval, revised), nil
147 }
148
148 lines GO