package curator import ( "context" "strings" "testing" "time" domainmemory "github.com/samcharles93/archie-core/internal/domain/memory" "github.com/samcharles93/archie-core/internal/tools" ) // fakeLLM records every chat request and returns a canned result. type fakeLLM struct { requests []ChatRequest } func (f *fakeLLM) Chat(_ context.Context, req ChatRequest) (ChatResult, error) { f.requests = append(f.requests, req) return ChatResult{Text: "done"}, nil } // stubToolBuilder, stubSkillStore, stubMemorySource and stubConversationSource // stand in for host capability services so tests can prove the registrar view // filters them out for a curator that does not declare them. type stubToolBuilder struct{} func (stubToolBuilder) Build(context.Context, []string) ([]tools.ToolEntry, error) { return nil, nil } type stubSkillStore struct{} func (stubSkillStore) List(context.Context) ([]SkillRef, error) { return nil, nil } func (stubSkillStore) Read(context.Context, string) (Skill, error) { return Skill{}, nil } func (stubSkillStore) Write(context.Context, Skill) error { return nil } func (stubSkillStore) Delete(context.Context, string) error { return nil } type stubMemorySource struct{} func (stubMemorySource) Get(string) (domainmemory.MemoryEngine, bool) { return nil, false } type stubConversationSource struct{} func (stubConversationSource) RecentSessions(context.Context, time.Time) ([]SessionRef, error) { return nil, nil } func (stubConversationSource) Messages(context.Context, string, int) ([]ConversationMessage, error) { return nil, nil } // fullHost returns a registrar carrying every host service, so a test can // prove what the filtered view keeps and drops. func fullHost(llm LLMRunner) Registrar { return Registrar{ Model: "test/model", LLM: llm, Tools: stubToolBuilder{}, Skills: stubSkillStore{}, MemoryEngines: stubMemorySource{}, Conversations: stubConversationSource{}, } } func instructionOnlyDefinition() Definition { return Definition{ Name: "custom", Enabled: true, Instructions: "You are a tidy curator.", Manifest: Manifest{Interval: time.Hour}, } } // TestInstructionOnlyDefinitionPass runs an instruction-only custom curator // end to end through the registry/engine path: registration binds a filtered // view, and the pass reaches the model. Before the filter granted the // definition engine its model runner, this pass failed with "no model runner // is bound". func TestInstructionOnlyDefinitionPass(t *testing.T) { llm := &fakeLLM{} registry := NewRegistry(fullHost(llm)) engine := NewDefinitionEngine(instructionOnlyDefinition()) if err := registry.Register(engine); err != nil { t.Fatal(err) } if _, err := engine.Pass(t.Context(), PassInput{Reason: "test"}); err != nil { t.Fatalf("instruction-only pass: %v", err) } if len(llm.requests) != 1 { t.Fatalf("model calls = %d, want 1", len(llm.requests)) } req := llm.requests[0] if req.Model != "test/model" { t.Errorf("model = %q, want the registrar default %q", req.Model, "test/model") } if len(req.Tools) != 0 { t.Errorf("model received %d tools, want none: undeclared tools must stay unavailable", len(req.Tools)) } var system string for _, msg := range req.Messages { if msg.Role == "system" { system = msg.Content } } if !strings.Contains(system, "You are a tidy curator.") { t.Errorf("system prompt %q does not carry the definition's instructions", system) } } // TestInstructionOnlyRegistrarViewFiltered pins capability filtering for an // instruction-only manifest: the bound registrar view exposes the model // runner but no tools, skills, memory or conversation access. func TestInstructionOnlyRegistrarViewFiltered(t *testing.T) { registry := NewRegistry(fullHost(&fakeLLM{})) engine := NewDefinitionEngine(instructionOnlyDefinition()) if err := registry.Register(engine); err != nil { t.Fatal(err) } host := engine.host if host.LLM == nil { t.Error("registrar view has no model runner; the definition engine always calls the model") } if host.Tools != nil { t.Error("registrar view exposes tools the manifest does not declare") } if host.Skills != nil { t.Error("registrar view exposes the skills capability the manifest does not declare") } if host.MemoryEngines != nil { t.Error("registrar view exposes memory engines the manifest does not declare") } if host.Conversations != nil { t.Error("registrar view exposes conversation history the manifest does not declare") } if host.Clock == nil { t.Error("registrar view has no clock; time is always bound") } } // TestDefinitionPassRecordsSummary pins that a pass's final text is recorded // as a pass.summary action, so a definition's report stays inspectable in the // activity trail instead of being dropped when the model returns. func TestDefinitionPassRecordsSummary(t *testing.T) { llm := &fakeLLM{} registry := NewRegistry(fullHost(llm)) engine := NewDefinitionEngine(instructionOnlyDefinition()) if err := registry.Register(engine); err != nil { t.Fatal(err) } res, err := engine.Pass(t.Context(), PassInput{Reason: "test"}) if err != nil { t.Fatalf("pass: %v", err) } var summary *Action for i, a := range res.Actions { if a.Type == ActionSummary { summary = &res.Actions[i] } } if summary == nil { t.Fatalf("no %s action recorded; actions: %+v", ActionSummary, res.Actions) } if summary.Detail != "done" { t.Errorf("summary detail = %q, want the model's final text %q", summary.Detail, "done") } }