runtime: implement Review with snapshot/build/run/extract/origin + spec-override tests
This commit is contained in:
1 parent
fd16dcefee
commit
d9580eee8f
2 files changed
+682
-2
No files matched your search
+237
-2
@@ -21,15 +21,25 @@
|
||||
//
|
||||
// Note: pass context.Background() — the parent's ctx is canceled by
|
||||
// the time OnStop fires.
|
||||
//
|
||||
// Concurrency: if your OnStop loop fires Review concurrently (e.g.,
|
||||
// back-to-back turns where reviewer N is still running when turn N+1
|
||||
// completes), ensure your MemoryStore/SkillStore are concurrency-safe.
|
||||
// The bundled tool/memory/jsonl and tool/skills/disk implementations
|
||||
// are; custom backends must guarantee safe concurrent writes.
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/sausheong/harness/llm"
|
||||
"github.com/sausheong/harness/session"
|
||||
"github.com/sausheong/harness/tool"
|
||||
"github.com/sausheong/harness/tool/memory"
|
||||
"github.com/sausheong/harness/tool/skills"
|
||||
)
|
||||
|
||||
// ReviewSpec describes one reviewer pass over the parent Runtime's
|
||||
@@ -113,7 +123,232 @@ var ErrReviewRecursion = errors.New("runtime: review recursion (parent is itself
|
||||
// to recurse.
|
||||
const reviewerAgentID = "__review__"
|
||||
|
||||
// Review is a stub that later tasks fill in.
|
||||
// Review runs a one-shot reviewer Runtime against parent's just-
|
||||
// finished session. See package doc for the typical OnStop wiring.
|
||||
//
|
||||
// Required spec fields: Prompt, Tools. All other fields have sensible
|
||||
// defaults (see ReviewSpec doc-comments).
|
||||
//
|
||||
// Returns ReviewResult with non-nil Err for setup failures (nil parent,
|
||||
// recursion guard, nil Tools, empty Prompt, build errors). Mid-Run
|
||||
// failures populate Turns/ToolCalls but leave Err nil.
|
||||
//
|
||||
// Cancelation: pass context.Background() when calling from OnStop —
|
||||
// the parent's ctx is canceled by the time OnStop fires.
|
||||
func Review(ctx context.Context, parent *Runtime, spec ReviewSpec) ReviewResult {
|
||||
return ReviewResult{Err: errors.New("not implemented")}
|
||||
start := time.Now()
|
||||
if parent == nil {
|
||||
return ReviewResult{Err: errors.New("runtime.Review: parent Runtime is nil"), Duration: time.Since(start)}
|
||||
}
|
||||
if parent.AgentID == reviewerAgentID {
|
||||
return ReviewResult{Err: ErrReviewRecursion, Duration: time.Since(start)}
|
||||
}
|
||||
if spec.Tools == nil {
|
||||
return ReviewResult{Err: errors.New("runtime.Review: spec.Tools is required"), Duration: time.Since(start)}
|
||||
}
|
||||
if spec.Prompt == "" {
|
||||
return ReviewResult{Err: errors.New("runtime.Review: spec.Prompt is required"), Duration: time.Since(start)}
|
||||
}
|
||||
|
||||
rt, err := buildReviewerRuntime(parent, spec)
|
||||
if err != nil {
|
||||
return ReviewResult{Err: err, Duration: time.Since(start)}
|
||||
}
|
||||
defer rt.Close()
|
||||
|
||||
// Capture the inherited prefix length BEFORE the reviewer runs so
|
||||
// extractActions can skip parent-session entries and only extract
|
||||
// tool-result envelopes appended by the reviewer itself.
|
||||
inheritedCount := len(rt.Session.View())
|
||||
|
||||
// Apply hard timeout. If spec.Timeout is 0, default to 60s.
|
||||
timeout := spec.Timeout
|
||||
if timeout == 0 {
|
||||
timeout = 60 * time.Second
|
||||
}
|
||||
runCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
// Provenance: tag every memory/skills write the reviewer makes
|
||||
// with origin "review". Both keys are independent (per-package
|
||||
// design); set both so whichever tool the reviewer calls picks up
|
||||
// the right value without coordination.
|
||||
runCtx = context.WithValue(runCtx, memory.OriginKey, "review")
|
||||
runCtx = context.WithValue(runCtx, skills.OriginKey, "review")
|
||||
|
||||
events, err := rt.Run(runCtx, spec.Prompt, nil)
|
||||
if err != nil {
|
||||
return ReviewResult{Err: err, Duration: time.Since(start)}
|
||||
}
|
||||
|
||||
// Drain events. Forward to OnEvent if set; count Turns and ToolCalls.
|
||||
var turns, toolCalls int
|
||||
for ev := range events {
|
||||
if spec.OnEvent != nil {
|
||||
spec.OnEvent(ev)
|
||||
}
|
||||
switch ev.Type {
|
||||
case EventToolCallStart:
|
||||
toolCalls++
|
||||
case EventDone:
|
||||
turns++
|
||||
}
|
||||
}
|
||||
|
||||
actions := extractActions(rt.Session, inheritedCount)
|
||||
|
||||
return ReviewResult{
|
||||
Actions: actions,
|
||||
Turns: turns,
|
||||
ToolCalls: toolCalls,
|
||||
Duration: time.Since(start),
|
||||
}
|
||||
}
|
||||
|
||||
// extractActions walks sess.View() starting at startIdx and returns
|
||||
// the Message field of every tool_result entry whose Output parses as
|
||||
// the canonical action-result envelope with Success=true.
|
||||
//
|
||||
// Convention is over schema: tools that don't emit the envelope
|
||||
// (e.g. list returns a raw array, get returns the entry struct, edit
|
||||
// tools return plain text) are silently skipped.
|
||||
//
|
||||
// Note: SessionEntry stores the per-type payload as a json.RawMessage
|
||||
// in the Data field, so we first unmarshal Data into ToolResultData to
|
||||
// recover Output, then unmarshal Output into the action envelope.
|
||||
func extractActions(sess *session.Session, startIdx int) []string {
|
||||
view := sess.View()
|
||||
if startIdx >= len(view) {
|
||||
return nil
|
||||
}
|
||||
var actions []string
|
||||
for _, e := range view[startIdx:] {
|
||||
if e.Type != session.EntryTypeToolResult {
|
||||
continue
|
||||
}
|
||||
var tr session.ToolResultData
|
||||
if err := json.Unmarshal(e.Data, &tr); err != nil {
|
||||
continue
|
||||
}
|
||||
var env struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(tr.Output), &env); err != nil {
|
||||
continue
|
||||
}
|
||||
if !env.Success || env.Message == "" {
|
||||
continue
|
||||
}
|
||||
actions = append(actions, env.Message)
|
||||
}
|
||||
return actions
|
||||
}
|
||||
|
||||
// buildReviewerRuntime constructs a Runtime configured for a one-shot
|
||||
// review pass over parent's just-finished session.
|
||||
//
|
||||
// Inheritance defaults:
|
||||
// - LLM: spec.Provider, falling back to parent.LLM
|
||||
// - Model: spec.Model, falling back to parent.Provider+"/"+parent.Model
|
||||
// - ContextWindow: spec.ContextWindow, falling back to parent.ContextWindow
|
||||
// - Workspace: parent.Workspace (for trace/spill paths)
|
||||
// - Memory: spec.Memory, falling back to parent.Memory (SHARED, not snapshotted)
|
||||
// - Skills: spec.Skills, falling back to parent.Skills (SHARED)
|
||||
// - Permission: spec.Permission (NOT inherited from parent — defaults to nil)
|
||||
// - SystemPrompt: spec.SystemPrompt (override) or empty (default identity built from tool names)
|
||||
// - MaxTurns: spec.MaxTurns or 16
|
||||
//
|
||||
// Always-set on reviewer:
|
||||
// - AgentID = "__review__" (recursion guard)
|
||||
// - IngestSource = "review" (KG ingest skips)
|
||||
// - Hooks zeroed (no OnStop reentry, no observation noise)
|
||||
// - KG nil (reviewer doesn't recall or ingest)
|
||||
// - Compaction nil (short-lived; MaxTurns 16 keeps context small)
|
||||
func buildReviewerRuntime(parent *Runtime, spec ReviewSpec) (*Runtime, error) {
|
||||
provider := spec.Provider
|
||||
if provider == nil {
|
||||
provider = parent.LLM
|
||||
}
|
||||
model := spec.Model
|
||||
if model == "" {
|
||||
// Re-compose parent.Provider/parent.Model since runtime stores
|
||||
// them split (after ParseProviderModel).
|
||||
if parent.Provider != "" && parent.Model != "" {
|
||||
model = parent.Provider + "/" + parent.Model
|
||||
} else {
|
||||
model = parent.Model // best-effort fallback when parent has no provider
|
||||
}
|
||||
}
|
||||
ctxWindow := spec.ContextWindow
|
||||
if ctxWindow == 0 {
|
||||
ctxWindow = parent.ContextWindow
|
||||
}
|
||||
mem := spec.Memory
|
||||
if mem == nil {
|
||||
mem = parent.Memory
|
||||
}
|
||||
skillProv := spec.Skills
|
||||
if skillProv == nil {
|
||||
skillProv = parent.Skills
|
||||
}
|
||||
maxTurns := spec.MaxTurns
|
||||
if maxTurns == 0 {
|
||||
maxTurns = 16
|
||||
}
|
||||
|
||||
deps := RuntimeDeps{
|
||||
Skills: skillProv,
|
||||
Memory: mem,
|
||||
Permission: spec.Permission, // defaults to nil = allow-all
|
||||
// KGFn nil — reviewer never recalls or ingests
|
||||
// AgentLoop zeroed — Hooks left nil so no OnStop reentry
|
||||
}
|
||||
inputs := RuntimeInputs{
|
||||
Provider: provider,
|
||||
Tools: spec.Tools,
|
||||
Session: snapshotSession(parent.Session),
|
||||
Compaction: nil, // reviewer is short-lived
|
||||
IngestSource: "review",
|
||||
}
|
||||
agentSpec := AgentSpec{
|
||||
ID: reviewerAgentID,
|
||||
Name: "Review",
|
||||
Model: model,
|
||||
Workspace: parent.Workspace,
|
||||
SystemPrompt: spec.SystemPrompt,
|
||||
MaxTurns: maxTurns,
|
||||
ContextWindow: ctxWindow,
|
||||
}
|
||||
rt, err := BuildRuntime(deps, inputs, agentSpec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rt, nil
|
||||
}
|
||||
|
||||
// snapshotSession returns a fresh *session.Session that contains a
|
||||
// copy of parent's current View. The clone preserves AgentID (so the
|
||||
// reviewer sees the same agent identity in entries) but rewrites Key
|
||||
// to "review-" + parent.Key — this ensures the reviewer's compaction
|
||||
// locks don't contend with the parent's (locks are keyed on
|
||||
// AgentID/Key) and that the JSONL store (if attached) writes to a
|
||||
// separate file.
|
||||
//
|
||||
// The clone is in-memory only; SetStore is NOT called on it.
|
||||
//
|
||||
// Mirror of inheritParentHistory in subagent.go: the FIRST inherited
|
||||
// entry gets its ParentID cleared so the fresh session's empty leaf
|
||||
// doesn't leave a dangling pointer; subsequent entries chain naturally
|
||||
// as Append re-stitches them.
|
||||
func snapshotSession(parent *session.Session) *session.Session {
|
||||
clone := session.NewSession(parent.AgentID, "review-"+parent.Key)
|
||||
view := parent.View()
|
||||
for i, e := range view {
|
||||
if i == 0 {
|
||||
e.ParentID = ""
|
||||
}
|
||||
clone.Append(e)
|
||||
}
|
||||
return clone
|
||||
}
|
||||
@@ -1,12 +1,20 @@
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sausheong/harness/llm"
|
||||
"github.com/sausheong/harness/llm/llmtest"
|
||||
"github.com/sausheong/harness/session"
|
||||
"github.com/sausheong/harness/tool"
|
||||
"github.com/sausheong/harness/tool/memory"
|
||||
"github.com/sausheong/harness/tool/skills"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestReviewSpec_Defaults(t *testing.T) {
|
||||
@@ -47,3 +55,440 @@ func TestReviewPrompts_Exported(t *testing.T) {
|
||||
assert.Contains(t, p, "skill")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReview_RecursionGuard(t *testing.T) {
|
||||
// A reviewer Runtime has AgentID = "__review__"; Review on it
|
||||
// must refuse with ErrReviewRecursion.
|
||||
parent := &Runtime{AgentID: reviewerAgentID}
|
||||
spec := ReviewSpec{Prompt: "x", Tools: tool.NewRegistry()}
|
||||
res := Review(context.Background(), parent, spec)
|
||||
assert.ErrorIs(t, res.Err, ErrReviewRecursion)
|
||||
}
|
||||
|
||||
func TestReview_NilToolsRejected(t *testing.T) {
|
||||
parent := &Runtime{AgentID: "main"}
|
||||
res := Review(context.Background(), parent, ReviewSpec{Prompt: "x", Tools: nil})
|
||||
assert.Error(t, res.Err)
|
||||
assert.Contains(t, res.Err.Error(), "Tools")
|
||||
}
|
||||
|
||||
func TestReview_EmptyPromptRejected(t *testing.T) {
|
||||
parent := &Runtime{AgentID: "main"}
|
||||
res := Review(context.Background(), parent, ReviewSpec{Prompt: "", Tools: tool.NewRegistry()})
|
||||
assert.Error(t, res.Err)
|
||||
assert.Contains(t, res.Err.Error(), "Prompt")
|
||||
}
|
||||
|
||||
func TestSnapshotSession_KeyPrefix(t *testing.T) {
|
||||
parent := session.NewSession("main-agent", "alpha")
|
||||
parent.Append(session.UserMessageEntry("first prompt"))
|
||||
parent.Append(session.AssistantMessageEntry("first reply"))
|
||||
|
||||
clone := snapshotSession(parent)
|
||||
assert.Equal(t, "main-agent", clone.AgentID, "AgentID should be preserved")
|
||||
assert.Equal(t, "review-alpha", clone.Key, "Key should be prefixed with review-")
|
||||
// Has the same number of entries.
|
||||
assert.Len(t, clone.View(), len(parent.View()))
|
||||
}
|
||||
|
||||
func TestSnapshotSession_DoesNotMutateParent(t *testing.T) {
|
||||
parent := session.NewSession("main-agent", "main")
|
||||
parent.Append(session.UserMessageEntry("hello"))
|
||||
originalCount := len(parent.View())
|
||||
|
||||
clone := snapshotSession(parent)
|
||||
clone.Append(session.AssistantMessageEntry("review note"))
|
||||
|
||||
// Parent's entry count must NOT have changed.
|
||||
assert.Equal(t, originalCount, len(parent.View()),
|
||||
"appending to clone must not affect parent")
|
||||
// Clone's entry count is original + 1.
|
||||
assert.Equal(t, originalCount+1, len(clone.View()))
|
||||
}
|
||||
|
||||
// fakeMemoryProvider is a minimal MemoryProvider for tests.
|
||||
type fakeMemoryProvider struct{ index string }
|
||||
|
||||
func (f *fakeMemoryProvider) FormatIndex() string { return f.index }
|
||||
func (f *fakeMemoryProvider) Get(id string) (string, bool) { return "", false }
|
||||
|
||||
// fakeSkillProvider is a minimal SkillProvider for tests.
|
||||
type fakeSkillProvider struct{ index string }
|
||||
|
||||
func (f *fakeSkillProvider) FormatIndex() string { return f.index }
|
||||
func (f *fakeSkillProvider) Get(name string) (string, bool) { return "", false }
|
||||
|
||||
// denyAllPermission is a PermissionChecker that denies everything.
|
||||
type denyAllPermission struct{}
|
||||
|
||||
func (denyAllPermission) Check(ctx context.Context, agentID, toolName string, input json.RawMessage) tool.Decision {
|
||||
return tool.Decision{Behavior: tool.DecisionDeny, Reason: "denied by test checker"}
|
||||
}
|
||||
func (denyAllPermission) FilterToolDefs(defs []llm.ToolDef, agentID string) []llm.ToolDef {
|
||||
return nil
|
||||
}
|
||||
|
||||
// newReviewTestParent constructs a minimal parent Runtime suitable for
|
||||
// build/run tests.
|
||||
func newReviewTestParent(t *testing.T) *Runtime {
|
||||
t.Helper()
|
||||
return &Runtime{
|
||||
AgentID: "main-agent",
|
||||
AgentName: "Main",
|
||||
Provider: "anthropic",
|
||||
Model: "claude-haiku-4-5",
|
||||
Workspace: t.TempDir(),
|
||||
Session: session.NewSession("main-agent", "main"),
|
||||
LLM: &llmtest.Stub{Text: "ok"},
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildReviewerRuntime_AgentIDIsReviewer(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
rt, err := buildReviewerRuntime(parent, ReviewSpec{
|
||||
Prompt: "x",
|
||||
Tools: tool.NewRegistry(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, reviewerAgentID, rt.AgentID)
|
||||
}
|
||||
|
||||
func TestBuildReviewerRuntime_InheritsParentLLMAndModel(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
rt, err := buildReviewerRuntime(parent, ReviewSpec{
|
||||
Prompt: "x", Tools: tool.NewRegistry(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Same(t, parent.LLM, rt.LLM, "LLM should inherit from parent when spec.Provider is nil")
|
||||
assert.NotEmpty(t, rt.Model, "Model should be inherited and parsed")
|
||||
}
|
||||
|
||||
func TestBuildReviewerRuntime_InheritsMemoryAndSkills(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
parent.Memory = &fakeMemoryProvider{index: "from parent memory"}
|
||||
parent.Skills = &fakeSkillProvider{index: "from parent skills"}
|
||||
rt, err := buildReviewerRuntime(parent, ReviewSpec{
|
||||
Prompt: "x", Tools: tool.NewRegistry(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, rt.Memory, "Memory should inherit from parent")
|
||||
assert.NotNil(t, rt.Skills, "Skills should inherit from parent")
|
||||
}
|
||||
|
||||
func TestBuildReviewerRuntime_HooksAreZeroed(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
parent.AgentLoop.Hooks.OnStop = func(ctx context.Context, reason string) {
|
||||
t.Errorf("parent OnStop should NOT propagate to reviewer")
|
||||
}
|
||||
rt, err := buildReviewerRuntime(parent, ReviewSpec{
|
||||
Prompt: "x", Tools: tool.NewRegistry(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, rt.AgentLoop.Hooks.OnStop)
|
||||
assert.Nil(t, rt.AgentLoop.Hooks.OnUserPromptSubmit)
|
||||
assert.Nil(t, rt.AgentLoop.Hooks.BeforeToolUse)
|
||||
assert.Nil(t, rt.AgentLoop.Hooks.AfterToolUse)
|
||||
assert.Nil(t, rt.AgentLoop.Hooks.OnSessionStart)
|
||||
}
|
||||
|
||||
func TestBuildReviewerRuntime_PermissionDefaultsToNil(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
parent.Permission = denyAllPermission{}
|
||||
rt, err := buildReviewerRuntime(parent, ReviewSpec{
|
||||
Prompt: "x", Tools: tool.NewRegistry(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, rt.Permission, "reviewer Permission should default to nil (allow-all)")
|
||||
}
|
||||
|
||||
func TestBuildReviewerRuntime_IngestSourceIsReview(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
rt, err := buildReviewerRuntime(parent, ReviewSpec{
|
||||
Prompt: "x", Tools: tool.NewRegistry(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "review", rt.IngestSource, `reviewer IngestSource must be "review" so KG ingest skips it`)
|
||||
}
|
||||
|
||||
// statefulReviewMock embeds llmtest.Base so we satisfy the LLMProvider
|
||||
// interface even as it grows; only ChatStream is custom.
|
||||
type statefulReviewMock struct {
|
||||
llmtest.Base
|
||||
responses [][]llm.ChatEvent
|
||||
callCount int
|
||||
}
|
||||
|
||||
func (m *statefulReviewMock) ChatStream(_ context.Context, _ llm.ChatRequest) (<-chan llm.ChatEvent, error) {
|
||||
idx := m.callCount
|
||||
m.callCount++
|
||||
if idx >= len(m.responses) {
|
||||
idx = len(m.responses) - 1
|
||||
}
|
||||
events := m.responses[idx]
|
||||
ch := make(chan llm.ChatEvent, len(events))
|
||||
for _, e := range events {
|
||||
ch <- e
|
||||
}
|
||||
close(ch)
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// reviewNoopTool emits a canonical action-result envelope so action
|
||||
// extraction (Task 7) has something to find.
|
||||
type reviewNoopTool struct{ name string }
|
||||
|
||||
func (n *reviewNoopTool) Name() string { return n.name }
|
||||
func (n *reviewNoopTool) Description() string { return "noop for review tests" }
|
||||
func (n *reviewNoopTool) Parameters() json.RawMessage { return json.RawMessage(`{"type":"object"}`) }
|
||||
func (n *reviewNoopTool) IsConcurrencySafe(_ json.RawMessage) bool { return true }
|
||||
func (n *reviewNoopTool) Execute(_ context.Context, _ json.RawMessage) (tool.ToolResult, error) {
|
||||
return tool.ToolResult{Output: `{"success":true,"message":"ok","target":"test"}`}, nil
|
||||
}
|
||||
|
||||
func TestReview_DrainsEventsSilently(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
parent.LLM = &statefulReviewMock{
|
||||
responses: [][]llm.ChatEvent{
|
||||
// Single response: text then done.
|
||||
{
|
||||
{Type: llm.EventTextDelta, Text: "review complete"},
|
||||
{Type: llm.EventDone},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res := Review(context.Background(), parent, ReviewSpec{
|
||||
Prompt: "review please",
|
||||
Tools: tool.NewRegistry(),
|
||||
})
|
||||
// Setup must succeed (no Err for guards).
|
||||
assert.NoError(t, res.Err, "Review should not error on happy path")
|
||||
assert.GreaterOrEqual(t, res.Turns, 1, "Turns counter should be populated")
|
||||
assert.Greater(t, res.Duration, time.Duration(0), "Duration must be populated")
|
||||
}
|
||||
|
||||
func TestReview_OnEventReceivesEvents(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
parent.LLM = &statefulReviewMock{
|
||||
responses: [][]llm.ChatEvent{
|
||||
{
|
||||
{Type: llm.EventTextDelta, Text: "hi"},
|
||||
{Type: llm.EventDone},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var captured []AgentEvent
|
||||
res := Review(context.Background(), parent, ReviewSpec{
|
||||
Prompt: "x",
|
||||
Tools: tool.NewRegistry(),
|
||||
OnEvent: func(ev AgentEvent) {
|
||||
captured = append(captured, ev)
|
||||
},
|
||||
})
|
||||
assert.NoError(t, res.Err)
|
||||
require.NotEmpty(t, captured, "OnEvent should have received at least one event")
|
||||
|
||||
var sawDone bool
|
||||
for _, ev := range captured {
|
||||
if ev.Type == EventDone {
|
||||
sawDone = true
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.True(t, sawDone, "OnEvent should have received EventDone")
|
||||
}
|
||||
|
||||
func TestReview_CountsToolCalls(t *testing.T) {
|
||||
parent := newReviewTestParent(t)
|
||||
parent.LLM = &statefulReviewMock{
|
||||
responses: [][]llm.ChatEvent{
|
||||
// First call: emit a tool call to "noop".
|
||||
{
|
||||
{Type: llm.EventToolCallStart, ToolCall: &llm.ToolCall{ID: "tc_1", Name: "noop"}},
|
||||
{Type: llm.EventToolCallDone, ToolCall: &llm.ToolCall{ID: "tc_1", Name: "noop", Input: json.RawMessage(`{}`)}},
|
||||
{Type: llm.EventDone},
|
||||
},
|
||||
// Second call: finish with text.
|
||||
{
|
||||
{Type: llm.EventTextDelta, Text: "done"},
|
||||
{Type: llm.EventDone},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
reg := tool.NewRegistry()
|
||||
reg.Register(&reviewNoopTool{name: "noop"})
|
||||
|
||||
res := Review(context.Background(), parent, ReviewSpec{
|
||||
Prompt: "x",
|
||||
Tools: reg,
|
||||
})
|
||||
assert.NoError(t, res.Err)
|
||||
assert.GreaterOrEqual(t, res.ToolCalls, 1, "ToolCalls should reflect the tool dispatch")
|
||||
}
|
||||
|
||||
func TestExtractActions_ParsesSuccessEnvelope(t *testing.T) {
|
||||
// Build a session with a snapshot of 2 entries (the inherited
|
||||
// prefix), then 2 tool-result entries (the reviewer's actions).
|
||||
sess := session.NewSession("main", "review-main")
|
||||
sess.Append(session.UserMessageEntry("user prompt"))
|
||||
sess.Append(session.AssistantMessageEntry("assistant reply"))
|
||||
inheritedCount := len(sess.View())
|
||||
|
||||
sess.Append(session.ToolResultEntry("call-1", `{"success":true,"message":"saved memory: user prefers terse","target":"memory","id":"mem_x"}`, "", nil))
|
||||
sess.Append(session.ToolResultEntry("call-2", `{"success":true,"message":"created skill: my-flow","target":"skill","name":"my-flow"}`, "", nil))
|
||||
|
||||
actions := extractActions(sess, inheritedCount)
|
||||
require.Len(t, actions, 2)
|
||||
assert.Equal(t, "saved memory: user prefers terse", actions[0])
|
||||
assert.Equal(t, "created skill: my-flow", actions[1])
|
||||
}
|
||||
|
||||
func TestExtractActions_SkipsFailures(t *testing.T) {
|
||||
sess := session.NewSession("main", "review-main")
|
||||
inheritedCount := 0
|
||||
|
||||
sess.Append(session.ToolResultEntry("call-1", `{"success":true,"message":"ok","target":"memory","id":"x"}`, "", nil))
|
||||
sess.Append(session.ToolResultEntry("call-2", `{"success":false,"error":"some error","target":"memory"}`, "", nil))
|
||||
sess.Append(session.ToolResultEntry("call-3", `{"success":true,"message":"another ok","target":"skill","name":"y"}`, "", nil))
|
||||
|
||||
actions := extractActions(sess, inheritedCount)
|
||||
require.Len(t, actions, 2, "only successful results should appear")
|
||||
assert.Equal(t, "ok", actions[0])
|
||||
assert.Equal(t, "another ok", actions[1])
|
||||
}
|
||||
|
||||
func TestExtractActions_SkipsNonEnvelopeOutputs(t *testing.T) {
|
||||
// Tools that don't follow the convention (e.g. return raw JSON
|
||||
// arrays for list, or plain text) should be silently skipped.
|
||||
sess := session.NewSession("main", "review-main")
|
||||
inheritedCount := 0
|
||||
|
||||
sess.Append(session.ToolResultEntry("call-1", `[]`, "", nil)) // list output (raw array)
|
||||
sess.Append(session.ToolResultEntry("call-2", `{"success":true,"message":"ok","target":"x"}`, "", nil))
|
||||
sess.Append(session.ToolResultEntry("call-3", `not json at all`, "", nil))
|
||||
|
||||
actions := extractActions(sess, inheritedCount)
|
||||
require.Len(t, actions, 1)
|
||||
assert.Equal(t, "ok", actions[0])
|
||||
}
|
||||
|
||||
func TestExtractActions_OnlyAfterInheritedPrefix(t *testing.T) {
|
||||
// Tool-result entries from BEFORE inheritedCount must NOT be
|
||||
// extracted — those came from the parent's session, not the
|
||||
// reviewer's actions.
|
||||
sess := session.NewSession("main", "review-main")
|
||||
sess.Append(session.ToolResultEntry("inherit-1", `{"success":true,"message":"OLD action","target":"memory"}`, "", nil))
|
||||
sess.Append(session.UserMessageEntry("review prompt"))
|
||||
inheritedCount := len(sess.View())
|
||||
|
||||
sess.Append(session.ToolResultEntry("review-1", `{"success":true,"message":"NEW action","target":"memory"}`, "", nil))
|
||||
|
||||
actions := extractActions(sess, inheritedCount)
|
||||
require.Len(t, actions, 1, "only NEW actions appended after inheritedCount should appear")
|
||||
assert.Equal(t, "NEW action", actions[0])
|
||||
}
|
||||
|
||||
func TestReview_SetsOriginToReviewOnContext(t *testing.T) {
|
||||
// Verify by registering a fake tool that captures the context's
|
||||
// OriginKey values and then asserting on them.
|
||||
|
||||
parent := newReviewTestParent(t)
|
||||
parent.LLM = &statefulReviewMock{
|
||||
responses: [][]llm.ChatEvent{
|
||||
// First call: tool call to capture_origin.
|
||||
{
|
||||
{Type: llm.EventToolCallStart, ToolCall: &llm.ToolCall{ID: "tc_1", Name: "capture_origin"}},
|
||||
{Type: llm.EventToolCallDone, ToolCall: &llm.ToolCall{ID: "tc_1", Name: "capture_origin", Input: json.RawMessage(`{}`)}},
|
||||
{Type: llm.EventDone},
|
||||
},
|
||||
// Second call: finish with text.
|
||||
{
|
||||
{Type: llm.EventTextDelta, Text: "done"},
|
||||
{Type: llm.EventDone},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
captured := make(chan struct {
|
||||
mem string
|
||||
ski string
|
||||
}, 1)
|
||||
reg := tool.NewRegistry()
|
||||
reg.Register(&originCapturingTool{captured: captured})
|
||||
|
||||
res := Review(context.Background(), parent, ReviewSpec{
|
||||
Prompt: "x",
|
||||
Tools: reg,
|
||||
})
|
||||
assert.NoError(t, res.Err)
|
||||
|
||||
select {
|
||||
case got := <-captured:
|
||||
assert.Equal(t, "review", got.mem, `memory.OriginKey should be "review"`)
|
||||
assert.Equal(t, "review", got.ski, `skills.OriginKey should be "review"`)
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatalf("originCapturingTool was never called")
|
||||
}
|
||||
}
|
||||
|
||||
// originCapturingTool reads both memory.OriginKey and skills.OriginKey
|
||||
// from the context and sends them to a channel.
|
||||
type originCapturingTool struct {
|
||||
captured chan struct {
|
||||
mem string
|
||||
ski string
|
||||
}
|
||||
}
|
||||
|
||||
func (o *originCapturingTool) Name() string { return "capture_origin" }
|
||||
func (o *originCapturingTool) Description() string { return "test" }
|
||||
func (o *originCapturingTool) Parameters() json.RawMessage { return json.RawMessage(`{"type":"object"}`) }
|
||||
func (o *originCapturingTool) IsConcurrencySafe(_ json.RawMessage) bool { return false }
|
||||
func (o *originCapturingTool) Execute(ctx context.Context, _ json.RawMessage) (tool.ToolResult, error) {
|
||||
mem, _ := ctx.Value(memory.OriginKey).(string)
|
||||
ski, _ := ctx.Value(skills.OriginKey).(string)
|
||||
o.captured <- struct {
|
||||
mem string
|
||||
ski string
|
||||
}{mem, ski}
|
||||
return tool.ToolResult{Output: `{"success":true,"message":"captured","target":"test"}`}, nil
|
||||
}
|
||||
|
||||
func TestBuildReviewerRuntime_SpecOverridesParent(t *testing.T) {
|
||||
// Verify that spec values WIN over parent values when set.
|
||||
// Inheritance fallback is covered by the InheritsParent* tests above;
|
||||
// this test covers the symmetric "override" path so future refactors
|
||||
// can't silently swap precedence.
|
||||
|
||||
parent := newReviewTestParent(t)
|
||||
parent.Memory = &fakeMemoryProvider{index: "parent-mem"}
|
||||
parent.Skills = &fakeSkillProvider{index: "parent-skills"}
|
||||
parent.ContextWindow = 100000
|
||||
|
||||
overrideMem := &fakeMemoryProvider{index: "override-mem"}
|
||||
overrideSkills := &fakeSkillProvider{index: "override-skills"}
|
||||
overrideLLM := &llmtest.Stub{Text: "override"}
|
||||
|
||||
rt, err := buildReviewerRuntime(parent, ReviewSpec{
|
||||
Prompt: "x",
|
||||
Tools: tool.NewRegistry(),
|
||||
Provider: overrideLLM,
|
||||
Model: "anthropic/claude-opus-4-7",
|
||||
ContextWindow: 200000,
|
||||
Memory: overrideMem,
|
||||
Skills: overrideSkills,
|
||||
MaxTurns: 32,
|
||||
SystemPrompt: "I AM THE OVERRIDE PROMPT",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Same(t, overrideLLM, rt.LLM, "spec.Provider should override parent.LLM")
|
||||
assert.Equal(t, "claude-opus-4-7", rt.Model, "spec.Model should override parent's recomposed model")
|
||||
assert.Equal(t, 200000, rt.ContextWindow, "spec.ContextWindow should override parent.ContextWindow")
|
||||
assert.NotSame(t, parent.Memory, rt.Memory, "spec.Memory should override parent.Memory")
|
||||
assert.NotSame(t, parent.Skills, rt.Skills, "spec.Skills should override parent.Skills")
|
||||
assert.Equal(t, 32, rt.MaxTurns, "spec.MaxTurns should override default 16")
|
||||
assert.Equal(t, "I AM THE OVERRIDE PROMPT", rt.SystemPrompt, "spec.SystemPrompt should be set on the reviewer")
|
||||
}
|
||||
Reference in new issue
Block a user