把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到 third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或 改写 tag 都不再影响本仓库构建。 - 已验证 GOPROXY=off 下可完整编译并打出发行包 - go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希) - 升级方法与授权注意事项见 third_party/README.md
145 lines
5.3 KiB
Go
145 lines
5.3 KiB
Go
// Package plan implements plan mode: the agent first
|
|
// explores read-only and drafts a plan, then calls ExitPlanMode to present it
|
|
// for approval. On approval the session transitions out of the read-only "plan"
|
|
// permission mode so the agent can implement. A Controller holds the current
|
|
// mode; the harness reads it via QueryInput.PermissionModeFunc so the change
|
|
// takes effect mid-loop.
|
|
package plan
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"sync"
|
|
|
|
"github.com/Autumn-27/norma/permission"
|
|
"github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
// Approver is the host's plan-approval callback (a CLI prompt, a GUI, or an
|
|
// automated policy). It returns whether the plan is approved and, if not,
|
|
// optional feedback for the agent to revise against.
|
|
type Approver func(ctx context.Context, plan string) (approved bool, feedback string)
|
|
|
|
// Controller tracks the live permission mode for a planning session.
|
|
type Controller struct {
|
|
mu sync.Mutex
|
|
mode permission.Mode
|
|
prePlan permission.Mode
|
|
target permission.Mode
|
|
}
|
|
|
|
// NewController starts in `start` mode; on approval the mode becomes `target`
|
|
// (defaulting to acceptEdits when empty).
|
|
func NewController(start, target permission.Mode) *Controller {
|
|
if target == "" {
|
|
target = permission.ModeAcceptEdits
|
|
}
|
|
if start == "" {
|
|
start = permission.ModeDefault
|
|
}
|
|
return &Controller{mode: start, prePlan: start, target: target}
|
|
}
|
|
|
|
// Mode returns the current permission mode (thread-safe; read by the harness).
|
|
func (c *Controller) Mode() permission.Mode {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return c.mode
|
|
}
|
|
|
|
// InPlan reports whether the controller is currently in plan mode.
|
|
func (c *Controller) InPlan() bool {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return c.mode == permission.ModePlan
|
|
}
|
|
|
|
// Enter transitions into plan mode, remembering the mode to restore.
|
|
func (c *Controller) Enter() {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if c.mode != permission.ModePlan {
|
|
c.prePlan = c.mode
|
|
c.mode = permission.ModePlan
|
|
}
|
|
}
|
|
|
|
// approve exits plan mode into the post-approval target mode.
|
|
func (c *Controller) approve() {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.mode = c.target
|
|
}
|
|
|
|
// Tools returns the EnterPlanMode and ExitPlanMode tools bound to this
|
|
// controller and approver.
|
|
func Tools(c *Controller, approve Approver) []tool.CoreTool {
|
|
return []tool.CoreTool{EnterTool(c), ExitTool(c, approve)}
|
|
}
|
|
|
|
// EnterTool lets the agent switch itself into plan mode mid-session.
|
|
func EnterTool(c *Controller) tool.CoreTool {
|
|
return tool.Build(tool.Spec{
|
|
Name: "EnterPlanMode",
|
|
Description: "Switches into plan mode: explore and design before making any changes. While in plan mode you may only use read-only tools. Draft a plan, then call ExitPlanMode to request approval.",
|
|
Schema: map[string]any{"type": "object", "properties": map[string]any{}},
|
|
ReadOnly: func(json.RawMessage) bool { return true },
|
|
Concurrent: func(json.RawMessage) bool { return false },
|
|
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
|
|
return permission.Allowed()
|
|
},
|
|
Run: func(context.Context, json.RawMessage, *tool.ToolContext) (tool.Result, error) {
|
|
c.Enter()
|
|
return tool.Text("Entered plan mode. Explore the codebase read-only and design a plan. Do not modify anything until your plan is approved via ExitPlanMode."), nil
|
|
},
|
|
})
|
|
}
|
|
|
|
// ExitTool presents the agent's plan for approval and, on approval, exits plan
|
|
// mode so implementation can begin.
|
|
func ExitTool(c *Controller, approve Approver) tool.CoreTool {
|
|
return tool.Build(tool.Spec{
|
|
Name: "ExitPlanMode",
|
|
Description: "Call when you have finished planning and are ready to implement. Presents your plan to the user for approval; on approval you may begin making changes.",
|
|
Prompt: "Provide the complete plan as concise markdown steps in `plan`. Only call this after read-only exploration is done and the plan is ready.",
|
|
Schema: map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{"plan": map[string]any{"type": "string", "description": "The implementation plan, as markdown."}},
|
|
"required": []any{"plan"},
|
|
},
|
|
// Read-only for permission purposes (allowed while in plan mode); not
|
|
// concurrency-safe (it prompts and mutates mode), so it never runs early.
|
|
ReadOnly: func(json.RawMessage) bool { return true },
|
|
Concurrent: func(json.RawMessage) bool { return false },
|
|
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
|
|
return permission.Allowed()
|
|
},
|
|
Run: func(ctx context.Context, input json.RawMessage, _ *tool.ToolContext) (tool.Result, error) {
|
|
var in struct {
|
|
Plan string `json:"plan"`
|
|
}
|
|
if err := json.Unmarshal(input, &in); err != nil {
|
|
return tool.Result{}, err
|
|
}
|
|
if !c.InPlan() {
|
|
return tool.Text("Not currently in plan mode; proceeding with implementation."), nil
|
|
}
|
|
approved := true
|
|
feedback := ""
|
|
if approve != nil {
|
|
approved, feedback = approve(ctx, in.Plan)
|
|
}
|
|
if approved {
|
|
c.approve()
|
|
return tool.Text("The user approved the plan. Plan mode is now exited — implement the approved plan:\n\n" + in.Plan), nil
|
|
}
|
|
msg := "The user did NOT approve the plan."
|
|
if feedback != "" {
|
|
msg += " Feedback: " + feedback
|
|
}
|
|
msg += "\nRemain in plan mode, revise the plan accordingly, and call ExitPlanMode again when ready."
|
|
return tool.Errorf(msg), nil
|
|
},
|
|
})
|
|
}
|