build: 内置 norma 模块,构建不再依赖上游仓库
把 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
This commit is contained in:
1 parent
a819fa3230
commit
f782b612ec
213 files changed
+37401
-2
No files matched your search
+179
@@ -0,0 +1,179 @@
|
||||
// Package subagent lets the agent delegate to isolated child agents (FR-09).
|
||||
// A subagent reuses the same harness loop with its own prompt, tool subset, and
|
||||
// permission mode, giving context isolation. It is exposed to the parent as an
|
||||
// "Agent" CoreTool; recursion is bounded by a depth guard carried on the
|
||||
// context.
|
||||
package subagent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/Autumn-27/norma/harness"
|
||||
"github.com/Autumn-27/norma/llm"
|
||||
"github.com/Autumn-27/norma/permission"
|
||||
"github.com/Autumn-27/norma/tool"
|
||||
"github.com/Autumn-27/norma/transcript"
|
||||
)
|
||||
|
||||
// Definition configures a named subagent (FR-09.1).
|
||||
type Definition struct {
|
||||
Description string // shown to the parent model in the Agent tool prompt
|
||||
Prompt []string // the subagent's system prompt
|
||||
Tools []string // tool names it may use (empty = inherit all)
|
||||
PermissionMode permission.Mode // its permission mode (default: parent's)
|
||||
MaxTurns int
|
||||
}
|
||||
|
||||
// Config wires the Agent tool.
|
||||
type Config struct {
|
||||
Provider llm.Provider
|
||||
ParentTools *tool.Registry
|
||||
Agents map[string]Definition
|
||||
MaxDepth int // default 3
|
||||
CanUseTool permission.CanUseTool
|
||||
Hooks harness.HookRunner
|
||||
// Transcript, when non-nil, persists each subagent run to its own sidechain
|
||||
// file under the parent session (whose id is read from the context). Subagent
|
||||
// tokens are recorded there, separate from the parent's totals.
|
||||
Transcript *transcript.Store
|
||||
}
|
||||
|
||||
type ctxKey int
|
||||
|
||||
const depthKey ctxKey = 0
|
||||
|
||||
func depth(ctx context.Context) int {
|
||||
v, _ := ctx.Value(depthKey).(int)
|
||||
return v
|
||||
}
|
||||
|
||||
// NewTool builds the "Agent" CoreTool that dispatches to a named subagent.
|
||||
func NewTool(cfg Config) tool.CoreTool {
|
||||
if cfg.MaxDepth <= 0 {
|
||||
cfg.MaxDepth = 3
|
||||
}
|
||||
var names string
|
||||
for n, d := range cfg.Agents {
|
||||
names += fmt.Sprintf("\n- %s: %s", n, d.Description)
|
||||
}
|
||||
return tool.Build(tool.Spec{
|
||||
Name: "Agent",
|
||||
Description: "Delegates a self-contained sub-task to an isolated subagent that runs with its own prompt and tools, returning only its final answer. Use it to parallelize independent work or to keep large intermediate output out of the main context.",
|
||||
Prompt: "Available agent_type values:" + names,
|
||||
Schema: map[string]any{
|
||||
"type": "object",
|
||||
"properties": map[string]any{
|
||||
"agent_type": map[string]any{"type": "string", "description": "Which subagent to run."},
|
||||
"prompt": map[string]any{"type": "string", "description": "The task for the subagent."},
|
||||
},
|
||||
"required": []any{"agent_type", "prompt"},
|
||||
},
|
||||
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
|
||||
return permission.Allowed() // the subagent's own tool calls are gated internally
|
||||
},
|
||||
Run: func(ctx context.Context, input json.RawMessage, tc *tool.ToolContext) (tool.Result, error) {
|
||||
var in struct {
|
||||
AgentType string `json:"agent_type"`
|
||||
Prompt string `json:"prompt"`
|
||||
}
|
||||
if err := json.Unmarshal(input, &in); err != nil {
|
||||
return tool.Result{}, err
|
||||
}
|
||||
if depth(ctx) >= cfg.MaxDepth {
|
||||
return tool.Errorf(fmt.Sprintf("Error: subagent recursion depth %d exceeded", cfg.MaxDepth)), nil
|
||||
}
|
||||
def, ok := cfg.Agents[in.AgentType]
|
||||
if !ok {
|
||||
return tool.Errorf(fmt.Sprintf("Error: unknown agent_type %q", in.AgentType)), nil
|
||||
}
|
||||
text, err := run(ctx, cfg, def, in.AgentType, in.Prompt, tc.WorkingDir)
|
||||
if err != nil {
|
||||
return tool.Errorf("Error: subagent failed: " + err.Error()), nil
|
||||
}
|
||||
return tool.Text(text), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Run executes the named agent from cfg.Agents to completion and returns its
|
||||
// final text. It is the programmatic entry point (the Agent tool uses the same
|
||||
// machinery); callers like the coordinator use it to fan out work directly.
|
||||
func Run(ctx context.Context, cfg Config, agentType, prompt, workDir string) (string, error) {
|
||||
def, ok := cfg.Agents[agentType]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("subagent: unknown agent_type %q", agentType)
|
||||
}
|
||||
return run(ctx, cfg, def, agentType, prompt, workDir)
|
||||
}
|
||||
|
||||
// run executes a subagent to completion and returns its final text.
|
||||
func run(ctx context.Context, cfg Config, def Definition, agentType, prompt, workDir string) (string, error) {
|
||||
if cfg.Hooks != nil {
|
||||
if hs, ok := cfg.Hooks.(interface {
|
||||
FireSubagentStart(context.Context, string)
|
||||
}); ok {
|
||||
hs.FireSubagentStart(ctx, agentType)
|
||||
}
|
||||
}
|
||||
registry := filterTools(cfg.ParentTools, def.Tools)
|
||||
childCtx := context.WithValue(ctx, depthKey, depth(ctx)+1)
|
||||
|
||||
seed := llm.UserText(prompt)
|
||||
in := harness.QueryInput{
|
||||
Provider: cfg.Provider,
|
||||
System: def.Prompt,
|
||||
Messages: []llm.Message{seed},
|
||||
Tools: registry,
|
||||
MaxTurns: def.MaxTurns,
|
||||
WorkingDir: workDir,
|
||||
AgentID: agentType,
|
||||
PermissionMode: def.PermissionMode,
|
||||
CanUseTool: cfg.CanUseTool,
|
||||
Hooks: cfg.Hooks,
|
||||
}
|
||||
// Persist this run to its own sidechain under the parent session.
|
||||
if cfg.Transcript != nil {
|
||||
parentSID := transcript.SessionIDFrom(ctx)
|
||||
if parentSID == "" {
|
||||
parentSID = transcript.NewSessionID()
|
||||
}
|
||||
w := cfg.Transcript.NewWriter(parentSID, transcript.NewAgentID())
|
||||
w.RecordMessage(seed, llm.Usage{})
|
||||
in.Recorder = w
|
||||
}
|
||||
var final string
|
||||
var rerr error
|
||||
for ev, err := range harness.Query(childCtx, in, harness.QueryDeps{}) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ev.Kind == harness.KindResult && ev.Terminal != nil {
|
||||
final = ev.Terminal.Text
|
||||
rerr = ev.Terminal.Err
|
||||
}
|
||||
}
|
||||
return final, rerr
|
||||
}
|
||||
|
||||
// filterTools returns a registry with only the named tools (all if names empty).
|
||||
func filterTools(parent *tool.Registry, names []string) *tool.Registry {
|
||||
if parent == nil {
|
||||
return tool.NewRegistry()
|
||||
}
|
||||
if len(names) == 0 {
|
||||
return parent
|
||||
}
|
||||
keep := map[string]bool{}
|
||||
for _, n := range names {
|
||||
keep[n] = true
|
||||
}
|
||||
var sel []tool.CoreTool
|
||||
for _, t := range parent.List() {
|
||||
if keep[t.Name()] {
|
||||
sel = append(sel, t)
|
||||
}
|
||||
}
|
||||
return tool.NewRegistry(sel...)
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package subagent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"iter"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Autumn-27/norma/llm"
|
||||
"github.com/Autumn-27/norma/permission"
|
||||
"github.com/Autumn-27/norma/tool"
|
||||
)
|
||||
|
||||
// fakeProvider always answers with a fixed line of text and ends the turn.
|
||||
type fakeProvider struct{ reply string }
|
||||
|
||||
func (f fakeProvider) Stream(_ context.Context, _ llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
|
||||
return func(yield func(llm.StreamEvent, error) bool) {
|
||||
yield(llm.StreamEvent{Type: llm.SETextDelta, Text: f.reply}, nil)
|
||||
yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: "end_turn"}, nil)
|
||||
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func (f fakeProvider) Complete(ctx context.Context, req llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
|
||||
acc := llm.NewAccumulator()
|
||||
for ev, err := range f.Stream(ctx, req) {
|
||||
if err != nil {
|
||||
return llm.Message{}, "", llm.Usage{}, err
|
||||
}
|
||||
acc.Add(ev)
|
||||
}
|
||||
return acc.Message(), acc.StopReason, acc.Usage, nil
|
||||
}
|
||||
|
||||
func TestSubagentDispatch(t *testing.T) {
|
||||
agentTool := NewTool(Config{
|
||||
Provider: fakeProvider{reply: "subagent answer"},
|
||||
ParentTools: tool.NewRegistry(tool.DefaultTools()...),
|
||||
Agents: map[string]Definition{
|
||||
"researcher": {Description: "researches", Prompt: []string{"You research."}, PermissionMode: permission.ModeBypass},
|
||||
},
|
||||
})
|
||||
res, err := agentTool.Call(context.Background(), []byte(`{"agent_type":"researcher","prompt":"find x"}`), &tool.ToolContext{})
|
||||
if err != nil {
|
||||
t.Fatalf("call: %v", err)
|
||||
}
|
||||
if !strings.Contains(res.Flatten(), "subagent answer") {
|
||||
t.Fatalf("subagent result: %q", res.Flatten())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownAgentType(t *testing.T) {
|
||||
agentTool := NewTool(Config{Provider: fakeProvider{}, Agents: map[string]Definition{}})
|
||||
res, _ := agentTool.Call(context.Background(), []byte(`{"agent_type":"nope","prompt":"x"}`), &tool.ToolContext{})
|
||||
if !res.IsError {
|
||||
t.Fatal("expected error for unknown agent_type")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecursionDepthGuard(t *testing.T) {
|
||||
agentTool := NewTool(Config{
|
||||
Provider: fakeProvider{reply: "x"},
|
||||
MaxDepth: 2,
|
||||
Agents: map[string]Definition{"a": {Prompt: []string{"p"}, PermissionMode: permission.ModeBypass}},
|
||||
})
|
||||
// Simulate being already at max depth.
|
||||
ctx := context.WithValue(context.Background(), depthKey, 2)
|
||||
res, _ := agentTool.Call(ctx, []byte(`{"agent_type":"a","prompt":"x"}`), &tool.ToolContext{})
|
||||
if !res.IsError || !strings.Contains(res.Flatten(), "recursion depth") {
|
||||
t.Fatalf("expected depth guard, got %q", res.Flatten())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user