Phase 1 of dynamic curator definitions (archie-core-trnd.1): - ToolBuilder implementation resolves declared tool names from the daemon's process-wide tool registry, returning exactly the declared set and failing closed on an unknown name. - curatorLLMRunner now carries ChatRequest.Tools and MaxSteps into core.GenerateOptions and widens ChatResult with the executed tool calls so a pass can attribute its actions. - One generic DefinitionEngine interprets a Definition (Manifest + name/enabled/instructions): its pass composes instructions with the built declared tool set, runs the model loop, and records an action per tool invocation. - [[curators]] config definitions carry the Manifest shape verbatim plus name/enabled/instructions; a definition missing interval is refused, never defaulted. - Idle curators are observable: the runtime logs a debug line when Check reports not-due.
184 lines
5.5 KiB
Go
184 lines
5.5 KiB
Go
package curator
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
)
|
|
|
|
// recordingLLM captures the ChatRequest a definition pass builds and returns
|
|
// a fixed result, so tests assert exactly what reached the model.
|
|
type recordingLLM struct {
|
|
req ChatRequest
|
|
result ChatResult
|
|
err error
|
|
}
|
|
|
|
func (l *recordingLLM) Chat(_ context.Context, req ChatRequest) (ChatResult, error) {
|
|
l.req = req
|
|
return l.result, l.err
|
|
}
|
|
|
|
// mapBuilder resolves declared names from a fixed map: the same resolution
|
|
// contract as the infrastructure ToolBuilder, without domain tests importing
|
|
// infrastructure.
|
|
type mapBuilder struct {
|
|
entries map[string]tools.ToolEntry
|
|
}
|
|
|
|
func (b mapBuilder) Build(_ context.Context, declared []string) ([]tools.ToolEntry, error) {
|
|
out := make([]tools.ToolEntry, 0, len(declared))
|
|
for _, name := range declared {
|
|
entry, ok := b.entries[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("declared tool %q has no implementation", name)
|
|
}
|
|
out = append(out, entry)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func definitionTool(name string) tools.ToolEntry {
|
|
return tools.ToolEntry{
|
|
Name: name,
|
|
Description: name + " tool",
|
|
Handler: func(context.Context, map[string]any) (any, error) { return nil, nil },
|
|
}
|
|
}
|
|
|
|
func TestDefinitionEnginePassReachesExactlyDeclaredTools(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
def := Definition{
|
|
Name: "project-curator",
|
|
Enabled: true,
|
|
Instructions: "Review the repository and report what you changed.",
|
|
Manifest: Manifest{
|
|
Interval: time.Hour,
|
|
Model: "provider/config-model",
|
|
Tools: []string{"read"},
|
|
},
|
|
}
|
|
llm := &recordingLLM{result: ChatResult{
|
|
Text: "done",
|
|
ToolCalls: []ToolCall{{Name: "read", Input: "{}"}},
|
|
}}
|
|
engine := NewDefinitionEngine(def)
|
|
engine.Bind(Registrar{
|
|
LLM: llm,
|
|
Model: "provider/default-model",
|
|
Tools: mapBuilder{entries: map[string]tools.ToolEntry{
|
|
"read": definitionTool("read"),
|
|
"write": definitionTool("write"),
|
|
}},
|
|
Clock: testClock{now: time.Unix(0, 0)},
|
|
})
|
|
|
|
res, err := engine.Pass(context.Background(), PassInput{Reason: "check-in"})
|
|
if err != nil {
|
|
t.Fatalf("Pass() error = %v, want nil", err)
|
|
}
|
|
|
|
if got := len(llm.req.Tools); got != 1 {
|
|
t.Fatalf("model received %d tools, want exactly the declared 1 (never a broader registry)", got)
|
|
}
|
|
if llm.req.Tools[0].Name != "read" {
|
|
t.Fatalf("model received tool %q, want the declared %q", llm.req.Tools[0].Name, "read")
|
|
}
|
|
if llm.req.Model != "provider/config-model" {
|
|
t.Fatalf("model reference = %q, want the definition's %q (no fallback to the default)", llm.req.Model, "provider/config-model")
|
|
}
|
|
if llm.req.MaxSteps <= 1 {
|
|
t.Fatalf("MaxSteps = %d, want more than one so the tool loop can run", llm.req.MaxSteps)
|
|
}
|
|
if len(llm.req.Messages) == 0 || llm.req.Messages[0].Role != "system" {
|
|
t.Fatalf("messages = %+v, want a leading system prompt", llm.req.Messages)
|
|
}
|
|
if sys := llm.req.Messages[0].Content; !strings.Contains(sys, def.Instructions) || !strings.Contains(sys, "read") {
|
|
t.Fatalf("system prompt = %q, want the definition instructions plus the declared tool summary", sys)
|
|
}
|
|
|
|
if len(res.Actions) != 1 {
|
|
t.Fatalf("Pass() actions = %d, want one per tool call", len(res.Actions))
|
|
}
|
|
a := res.Actions[0]
|
|
if a.Type != ActionToolCalled || a.Detail != "read" || a.Reason != "check-in" {
|
|
t.Fatalf("action = %+v, want the invoked tool attributed with the pass reason", a)
|
|
}
|
|
}
|
|
|
|
func TestDefinitionEnginePassFailsOnUnknownDeclaredTool(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
def := Definition{
|
|
Name: "project-curator",
|
|
Manifest: Manifest{
|
|
Interval: time.Hour,
|
|
Tools: []string{"gh.issue.create"},
|
|
},
|
|
}
|
|
llm := &recordingLLM{}
|
|
engine := NewDefinitionEngine(def)
|
|
engine.Bind(Registrar{
|
|
LLM: llm,
|
|
Tools: mapBuilder{entries: map[string]tools.ToolEntry{}},
|
|
Clock: testClock{now: time.Unix(0, 0)},
|
|
})
|
|
|
|
if _, err := engine.Pass(context.Background(), PassInput{Reason: "check-in"}); err == nil {
|
|
t.Fatal("Pass() with an unknown declared tool = nil error, want the build failure to fail the pass")
|
|
}
|
|
if llm.req.Messages != nil {
|
|
t.Fatalf("model was called despite the failed build: %+v", llm.req)
|
|
}
|
|
}
|
|
|
|
func TestDefinitionEngineManifestIsTheDefinitionNoCodeFallback(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
def := Definition{
|
|
Name: "config-defined",
|
|
Enabled: true,
|
|
Instructions: "do the thing",
|
|
Manifest: Manifest{
|
|
Interval: 17 * time.Minute,
|
|
Cooldown: 3 * time.Minute,
|
|
OnInput: true,
|
|
Tools: []string{"read", "shell"},
|
|
Model: "provider/config-model",
|
|
},
|
|
}
|
|
engine := NewDefinitionEngine(def)
|
|
|
|
got := engine.Manifest()
|
|
if got.Interval != 17*time.Minute {
|
|
t.Fatalf("Manifest().Interval = %v, want the definition's %v (no code constant)", got.Interval, 17*time.Minute)
|
|
}
|
|
if got.Cooldown != 3*time.Minute || !got.OnInput {
|
|
t.Fatalf("Manifest() = %+v, want the definition's cooldown/on_input", got)
|
|
}
|
|
if len(got.Tools) != 2 || got.Tools[0] != "read" || got.Tools[1] != "shell" {
|
|
t.Fatalf("Manifest().Tools = %v, want the definition's declared set", got.Tools)
|
|
}
|
|
if got.Model != "provider/config-model" {
|
|
t.Fatalf("Manifest().Model = %q, want the definition's model", got.Model)
|
|
}
|
|
}
|
|
|
|
func TestDefinitionEngineRegistrationRequiresToolBuilder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
reg := NewRegistry(Registrar{})
|
|
err := reg.Register(NewDefinitionEngine(Definition{
|
|
Name: "project-curator",
|
|
Manifest: Manifest{Interval: time.Hour, Tools: []string{"read"}},
|
|
}))
|
|
if err == nil {
|
|
t.Fatal("Register(tools-declaring definition) = nil, want the existing no-ToolBuilder refusal")
|
|
}
|
|
}
|