bots-garden/mini-mepublic Fork
⑂ main
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/mini-me.git
git clone ssh://git@rickub.com/bots-garden/mini-me.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
🛟 Updated. 79606ef · on main · k33g · 5d ago
decision_wiring_test.go · 223 lines · 7.8 KBGo Blame HistoryRaw
  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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"
	"mm/internal/skills"
)

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())
	}
}

// decision.display: true marks the JSON for the hook that runs -p; the
// server's answer follows unchanged.
func TestDecisionPromptMarksTheJSONWhenDisplayIsOn(t *testing.T) {
	deciderConfig(t, deciderServer(t, http.StatusOK, deciderAnswer))
	config.Cfg.Decision.Display = true
	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)
	}
	want := `{"display":true,` + strings.TrimPrefix(deciderAnswer, "{") + "\n"
	if out.String() != want {
		t.Errorf("stdout = %q, want %q", out.String(), want)
	}
}

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())
	}
}

// Ticket 141: a decider gets no tool and no skill catalogue, even with every
// built-in switched on and a skills directory that holds a skill — its file
// needs no `bashTool: false` lines.
func TestADeciderGetsNoToolWhateverTheBuiltInSwitchesSay(t *testing.T) {
	skillsDir := t.TempDir()
	if err := os.WriteFile(filepath.Join(skillsDir, "go-rename.md"), []byte("---\nname: go-rename\ndescription: rename a symbol\n---\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	withConfig(t, func(c *config.Config) {
		c.BashTool, c.EditTools, c.PlanTool, c.AskUserTool = true, true, true, true
		c.FetchContentTool, c.SaveAnswerTool, c.UnloadModels = true, true, true
		c.Decision = &config.DecisionConfig{}
	})
	config.Cfg.SkillsDir = skillsDir
	if names := assembledNames(t); len(names) != 0 {
		t.Errorf("a decider declares %v", names)
	}
	if declared := skills.Declared(); len(declared) != 0 {
		t.Errorf("a decider declares the skills %v", declared)
	}
}

// The same switches on a chat agent do declare tools: the short-circuit is
// for deciders only.
func TestAChatAgentStillGetsItsBuiltInTools(t *testing.T) {
	withConfig(t, func(c *config.Config) { c.BashTool, c.PlanTool = true, true })
	if names := assembledNames(t); !slices.Contains(names, "bash") || !slices.Contains(names, "plan") {
		t.Errorf("a chat agent declares %v", names)
	}
}

// Ticket 141: the decider an agent routes with is not the running agent.
// A chat agent whose skillRouting.decider is a decider file written the new
// way (no `bashTool: false` lines) keeps every tool and its skill catalogue,
// in the order main runs the checks.
func TestAChatAgentRoutingThroughADeciderKeepsItsTools(t *testing.T) {
	dir := t.TempDir()
	decider := filepath.Join(dir, "agent.decider.yaml")
	body := "provider: llmman\nmodel: d1\nbaseUrl: http://127.0.0.1:1/v1\nfallback: \"\"\n" +
		"decision:\n  questions:\n    destructive:\n      type: noul\n      instructions: It deletes data.\n"
	if err := os.WriteFile(decider, []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
	skillsDir := t.TempDir()
	if err := os.WriteFile(filepath.Join(skillsDir, "go-rename.md"), []byte("---\nname: go-rename\ndescription: rename a symbol\n---\n"), 0o644); err != nil {
		t.Fatal(err)
	}
	withConfig(t, func(c *config.Config) {
		c.BashTool, c.EditTools, c.PlanTool = true, true, true
		c.SkillRouting.Decider = decider
	})
	config.Cfg.SkillsDir = skillsDir
	t.Cleanup(func() { decide.SetRouter(nil); routing = "" })

	if err := config.DecisionConflict(); err != nil {
		t.Fatal(err)
	}
	names := assembledNames(t)
	if err := installRouter(); err != nil {
		t.Fatal(err)
	}
	for _, want := range []string{"bash", "read_file", "edit_file", "plan", "read_skill"} {
		if !slices.Contains(names, want) {
			t.Errorf("the chat agent lost %s: %v", want, names)
		}
	}
	if len(skills.Declared()) != 1 || config.Cfg.Decision != nil {
		t.Errorf("skills = %v, Decision = %v", skills.Declared(), config.Cfg.Decision)
	}
}

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)
	}
}