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:
wangdefa committed 2026-10-20 21:10:41 -07:00
1 parent a819fa3230
commit f782b612ec
213 files changed
+37401 -2

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
// Package hook provides Go-native lifecycle hooks (FR-08): callbacks fired at
// eight points in the agent loop that can observe state, block or rewrite tool
// calls, inject context, and gate the Stop transition. A Registry implements
// harness.HookRunner, so wiring it into a Session needs no glue.
package hook
import (
"context"
"encoding/json"
"github.com/Autumn-27/norma/llm"
)
// EventType enumerates the eight hook points (FR-08.2).
type EventType string
const (
PreToolUse EventType = "PreToolUse"
PostToolUse EventType = "PostToolUse"
PostToolUseFailure EventType = "PostToolUseFailure"
UserPromptSubmit EventType = "UserPromptSubmit"
SessionStart EventType = "SessionStart"
SessionEnd EventType = "SessionEnd"
Stop EventType = "Stop"
SubagentStart EventType = "SubagentStart"
)
// Event is the payload passed to a hook. Which fields are set depends on Type.
type Event struct {
Type EventType
ToolName string
Input json.RawMessage
Result json.RawMessage
IsError bool
Prompt string
AgentType string
Messages []llm.Message
}
// Result is a hook's response (FR-08.3).
type Result struct {
// Decision is "block" to halt the action, "approve"/"" to continue.
Decision string
// Message explains a block (shown to the model).
Message string
// UpdatedInput optionally rewrites a tool's input (PreToolUse).
UpdatedInput json.RawMessage
// AdditionalContext is injected into the conversation (UserPromptSubmit, Stop).
AdditionalContext string
// PreventContinuation, on a Stop hook, hard-terminates the loop
// (stop_hook_prevented) even if other logic would continue. A "block"
// decision instead forces the loop to continue with AdditionalContext
// injected (stop_hook_blocking).
PreventContinuation bool
}
func (r Result) blocked() bool { return r.Decision == "block" }
// Func is a single hook callback.
type Func func(ctx context.Context, ev Event) Result
// Registry holds hooks by event type and implements harness.HookRunner.
type Registry struct {
byType map[EventType][]Func
}
// NewRegistry builds an empty registry.
func NewRegistry() *Registry { return &Registry{byType: map[EventType][]Func{}} }
// On registers a hook for an event type.
func (r *Registry) On(t EventType, f Func) *Registry {
r.byType[t] = append(r.byType[t], f)
return r
}
func (r *Registry) run(ctx context.Context, t EventType, ev Event) []Result {
ev.Type = t
var out []Result
for _, f := range r.byType[t] {
out = append(out, f(ctx, ev))
}
return out
}
// --- harness.HookRunner implementation ---
// PreToolUse runs PreToolUse hooks. The first block halts; the last non-empty
// UpdatedInput wins.
func (r *Registry) PreToolUse(ctx context.Context, name string, input []byte) (bool, string, []byte) {
var updated []byte
for _, res := range r.run(ctx, PreToolUse, Event{ToolName: name, Input: input}) {
if res.blocked() {
return true, res.Message, nil
}
if len(res.UpdatedInput) > 0 {
updated = res.UpdatedInput
}
}
return false, "", updated
}
// PostToolUse runs PostToolUse hooks (and PostToolUseFailure on error).
func (r *Registry) PostToolUse(ctx context.Context, name string, input, result []byte, isErr bool) {
ev := Event{ToolName: name, Input: input, Result: result, IsError: isErr}
r.run(ctx, PostToolUse, ev)
if isErr {
r.run(ctx, PostToolUseFailure, ev)
}
}
// Stop runs Stop hooks and reports two distinct outcomes: a hook may
// hard-terminate the loop (preventContinuation), or it
// may force the loop to continue by emitting blocking errors injected as
// messages (FR-08.4). preventContinuation wins if any hook sets it.
func (r *Registry) Stop(ctx context.Context, messages []llm.Message) (prevent bool, blockingErrors []string, message string) {
for _, res := range r.run(ctx, Stop, Event{Messages: messages}) {
if res.PreventContinuation {
return true, nil, res.Message
}
if res.blocked() {
msg := res.AdditionalContext
if msg == "" {
msg = res.Message
}
blockingErrors = append(blockingErrors, msg)
}
}
return false, blockingErrors, ""
}
// --- fired by agentcore / subagent ---
// FireUserPromptSubmit runs UserPromptSubmit hooks and returns concatenated
// additional context to inject (empty if none).
func (r *Registry) FireUserPromptSubmit(ctx context.Context, prompt string) string {
var ctxAdd string
for _, res := range r.run(ctx, UserPromptSubmit, Event{Prompt: prompt}) {
if res.AdditionalContext != "" {
ctxAdd += res.AdditionalContext + "\n"
}
}
return ctxAdd
}
// FireSessionStart / FireSessionEnd are fire-and-forget notifications.
func (r *Registry) FireSessionStart(ctx context.Context) { r.run(ctx, SessionStart, Event{}) }
func (r *Registry) FireSessionEnd(ctx context.Context) { r.run(ctx, SessionEnd, Event{}) }
// FireSubagentStart notifies that a subagent is starting.
func (r *Registry) FireSubagentStart(ctx context.Context, agentType string) {
r.run(ctx, SubagentStart, Event{AgentType: agentType})
}
+54
View File
@@ -0,0 +1,54 @@
package hook
import (
"context"
"testing"
)
func TestPreToolUseBlockAndRewrite(t *testing.T) {
r := NewRegistry()
r.On(PreToolUse, func(_ context.Context, ev Event) Result {
if ev.ToolName == "Bash" {
return Result{Decision: "block", Message: "no bash"}
}
return Result{UpdatedInput: []byte(`{"x":1}`)}
})
if block, msg, _ := r.PreToolUse(context.Background(), "Bash", nil); !block || msg != "no bash" {
t.Fatalf("expected block, got %v %q", block, msg)
}
if block, _, upd := r.PreToolUse(context.Background(), "Read", nil); block || string(upd) != `{"x":1}` {
t.Fatalf("expected rewrite, got block=%v upd=%s", block, upd)
}
}
func TestStopHookBlockingContinues(t *testing.T) {
r := NewRegistry()
calls := 0
r.On(Stop, func(_ context.Context, _ Event) Result {
calls++
if calls == 1 {
return Result{Decision: "block", AdditionalContext: "keep going"}
}
return Result{}
})
// First call: a "block" decision → continue with an injected message.
prevent, blocking, _ := r.Stop(context.Background(), nil)
if prevent || len(blocking) != 1 || blocking[0] != "keep going" {
t.Fatalf("blocking path wrong: prevent=%v blocking=%v", prevent, blocking)
}
// Second call: no decision → allow stop.
if prevent, blocking, _ := r.Stop(context.Background(), nil); prevent || len(blocking) != 0 {
t.Fatalf("second Stop should allow: prevent=%v blocking=%v", prevent, blocking)
}
}
func TestStopHookPreventTerminates(t *testing.T) {
r := NewRegistry()
r.On(Stop, func(_ context.Context, _ Event) Result {
return Result{PreventContinuation: true, Message: "halt"}
})
prevent, _, msg := r.Stop(context.Background(), nil)
if !prevent || msg != "halt" {
t.Fatalf("prevent path wrong: prevent=%v msg=%q", prevent, msg)
}
}