1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
package main
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"mm/internal/config"
"mm/internal/decide"
"mm/internal/engine"
)
const deciderAnswer = `{"model":"d1","answers":{"destructive":{"type":"noul","noul":0.91}}}`
// deciderServer answers status + body to every request.
func deciderServer(t *testing.T, status int, body string) string {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(status)
_, _ = io.WriteString(w, body)
}))
t.Cleanup(srv.Close)
return srv.URL + "/v1"
}
// deciderConfig makes config.Cfg a decider agent pointing at baseURL.
func deciderConfig(t *testing.T, baseURL string) {
none := ""
withConfig(t, func(c *config.Config) {
c.Provider, c.Model, c.BaseURL, c.Fallback = "llamacpp", "d1", baseURL, &none
c.Decision = &config.DecisionConfig{Questions: []decide.Question{{Name: "destructive", Type: decide.Noul, Instructions: "It deletes data."}}}
})
}
func TestDecisionPromptPrintsTheServersJSONAlone(t *testing.T) {
deciderConfig(t, deciderServer(t, http.StatusOK, deciderAnswer))
e, err := engine.New(context.Background())
if err != nil {
t.Fatal(err)
}
var out bytes.Buffer
if code := runDecisionPrompt(context.Background(), e, "rm -rf ~/data", &out); code != 0 {
t.Fatalf("exit %d", code)
}
if out.String() != deciderAnswer+"\n" {
t.Errorf("stdout = %q, want the JSON and a newline", out.String())
}
}
func TestAFailedDecisionPromptExitsOneWithTheReasonOnStderr(t *testing.T) {
deciderConfig(t, deciderServer(t, http.StatusNotImplemented, `{"error":{"message":"This model is not a decision model"}}`))
e, err := engine.New(context.Background())
if err != nil {
t.Fatal(err)
}
var stderr, out bytes.Buffer
saved := errOut
errOut = &stderr
t.Cleanup(func() { errOut = saved })
if code := runDecisionPrompt(context.Background(), e, "ls", &out); code != 1 {
t.Errorf("exit %d, want 1", code)
}
if out.Len() != 0 || !strings.Contains(stderr.String(), "not a decision model") {
t.Errorf("stdout = %q, stderr = %q", out.String(), stderr.String())
}
}
// A decider declares no tool; unload_model is on by default and would make
// DecisionConflict refuse every decider that does not turn it off.
func TestADeciderDeclaresNoUnloadTool(t *testing.T) {
withConfig(t, func(c *config.Config) {
c.BashTool, c.EditTools, c.PlanTool, c.FetchContentTool, c.AskUserTool = false, false, false, false, false
c.Decision = &config.DecisionConfig{}
})
if names := assembledNames(t); slices.Contains(names, "unload_model") || config.DecisionConflict(names) != nil {
t.Errorf("a decider declares %v", names)
}
}
func TestInstallRouterReadsTheDeciderAndNamesItInTheBanner(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "agent.decider.yaml")
body := "provider: llmman\nmodel: hf.co/LiquidAI/d1-3B-GGUF:Q4_K_M\nbaseUrl: http://127.0.0.1:1/v1\nfallback: \"\"\n"
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
withConfig(t, func(c *config.Config) { c.SkillRouting.Decider = path })
t.Cleanup(func() { decide.SetRouter(nil); routing = "" })
if err := installRouter(); err != nil {
t.Fatal(err)
}
if routing != " | routing: llmman/hf.co/LiquidAI/d1-3B-GGUF:Q4_K_M ≥ 0.60" {
t.Errorf("routing column = %q", routing)
}
}
func TestInstallRouterDoesNothingWithoutADecider(t *testing.T) {
withConfig(t, func(*config.Config) {})
if err := installRouter(); err != nil || routing != "" {
t.Errorf("err = %v, routing = %q", err, routing)
}
}
func TestAnUnusableDeciderStopsTheStart(t *testing.T) {
path := filepath.Join(t.TempDir(), "dmr.yaml")
if err := os.WriteFile(path, []byte("provider: dmr\nmodel: m\n"), 0o644); err != nil {
t.Fatal(err)
}
withConfig(t, func(c *config.Config) { c.SkillRouting.Decider = path })
if err := installRouter(); err == nil || !strings.Contains(err.Error(), "skillRouting.decider") {
t.Errorf("err = %v", err)
}
}
func TestInstallRouterInstallsTheToolRouterAndNamesItInTheBanner(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "agent.decider.yaml")
body := "provider: llmman\nmodel: hf.co/LiquidAI/d1-3B-GGUF:Q4_K_M\nbaseUrl: http://127.0.0.1:1/v1\nfallback: \"\"\n"
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
withConfig(t, func(c *config.Config) {
c.ToolRouting = config.ToolRoutingConfig{Decider: path, DropBelow: 0.2, Timeout: time.Second,
Groups: []config.ToolGroup{{Name: "web", When: "w", Matcher: "^fetch_content$"}}}
})
t.Cleanup(func() { decide.SetToolRouter(nil); routing = "" })
if err := installRouter(); err != nil {
t.Fatal(err)
}
if routing != " | tool routing: llmman/hf.co/LiquidAI/d1-3B-GGUF:Q4_K_M, 1 group(s), drop < 0.20" {
t.Errorf("routing column = %q", routing)
}
}
|