v0.27.1 131edba

profiles_test.go RawHistoryFolder

1package subagents
2
3import (
4	"fmt"
5	"os"
6	"path/filepath"
7	"strings"
8	"testing"
9)
10
11func TestDiscoverLoadsGlobalAgentsProfilesAndPiFallback(t *testing.T) {
12	home := t.TempDir()
13	agentsDir := filepath.Join(home, ".agents", "agents")
14	piDir := filepath.Join(home, ".pi", "agent", "agents")
15	if err := os.MkdirAll(agentsDir, 0o755); err != nil {
16		t.Fatal(err)
17	}
18	if err := os.MkdirAll(piDir, 0o755); err != nil {
19		t.Fatal(err)
20	}
21	writeProfile(t, filepath.Join(agentsDir, "reviewer.md"), `---
22name: reviewer
23description: Read-only code reviewer
24tools: read
25model: openai-codex/gpt-5.6-luna
26thinking: max
27systemPromptMode: replace
28inheritProjectContext: false
29inheritSkills: false
30fastMode: false
31---
32Review the requested scope without editing it.
33`)
34	writeProfile(t, filepath.Join(piDir, "implementer.md"), `---
35name: implementer
36description: Primary implementation agent
37tools: [read, bash, edit, write]
38model: openai-codex/gpt-5.6-luna
39thinking: xhigh
40---
41Implement the requested change.
42`)
43
44	profiles, errs := Discover("", home)
45	if len(errs) != 0 {
46		t.Fatalf("Discover errors = %v", errs)
47	}
48	if len(profiles) != 2 {
49		t.Fatalf("profiles = %d, want 2: %#v", len(profiles), profiles)
50	}
51	if profiles[0].Name != "implementer" || profiles[1].Name != "reviewer" {
52		t.Fatalf("profiles not sorted by name: %#v", profiles)
53	}
54	reviewer := Find(profiles, "reviewer")
55	if reviewer == nil {
56		t.Fatal("reviewer profile missing")
57	}
58	if reviewer.SystemPromptMode != "replace" {
59		t.Fatalf("system prompt mode = %q, want replace", reviewer.SystemPromptMode)
60	}
61	if reviewer.Model != "openai-codex/gpt-5.6-luna" || reviewer.Thinking != "max" {
62		t.Fatalf("profile metadata = %#v", reviewer)
63	}
64	if reviewer.InheritProjectContext == nil || *reviewer.InheritProjectContext {
65		t.Fatalf("inherit project context = %#v, want false", reviewer.InheritProjectContext)
66	}
67	if reviewer.InheritSkills == nil || *reviewer.InheritSkills {
68		t.Fatalf("inherit skills = %#v, want false", reviewer.InheritSkills)
69	}
70	if reviewer.FastMode == nil || *reviewer.FastMode {
71		t.Fatalf("fast mode = %#v, want false", reviewer.FastMode)
72	}
73	implementer := Find(profiles, "implementer")
74	if implementer == nil || implementer.FastMode != nil {
75		t.Fatalf("unset fast mode = %#v, want nil", implementer)
76	}
77}
78
79func TestDiscoverResolvesConfiguredRelativePathAgainstCWD(t *testing.T) {
80	first := t.TempDir()
81	second := t.TempDir()
82	t.Setenv("ZUT_AGENT_PROFILES", ".agents")
83	writeProfile(t, filepath.Join(first, ".agents", "reviewer.md"), `---
84name: reviewer
85description: First session profile
86---
87First.
88`)
89	writeProfile(t, filepath.Join(second, ".agents", "reviewer.md"), `---
90name: reviewer
91description: Second session profile
92---
93Second.
94`)
95
96	for _, tc := range []struct {
97		cwd         string
98		description string
99	}{
100		{cwd: first, description: "First session profile"},
101		{cwd: second, description: "Second session profile"},
102	} {
103		profiles, errs := Discover(tc.cwd, t.TempDir())
104		if len(errs) != 0 {
105			t.Fatalf("Discover(%q) errors = %v", tc.cwd, errs)
106		}
107		if len(profiles) != 1 || profiles[0].Description != tc.description {
108			t.Fatalf("Discover(%q) = %#v, want %q", tc.cwd, profiles, tc.description)
109		}
110	}
111}
112
113func TestDiscoverConfiguredDirectoryWinsAndRejectsUnsafeNames(t *testing.T) {
114	configured := t.TempDir()
115	home := t.TempDir()
116	t.Setenv("ZUT_AGENT_PROFILES", configured)
117	writeProfile(t, filepath.Join(configured, "reviewer.md"), `---
118name: reviewer
119description: Configured reviewer
120---
121Configured instructions.
122`)
123	writeProfile(t, filepath.Join(filepath.Join(home, ".agents", "agents"), "reviewer.md"), `---
124name: reviewer
125description: Global reviewer
126---
127Global instructions.
128`)
129	writeProfile(t, filepath.Join(configured, "unsafe.md"), `---
130name: ../unsafe
131description: Should not load
132---
133No.
134`)
135
136	profiles, errs := Discover("", home)
137	if len(errs) != 0 {
138		t.Fatalf("Discover errors = %v", errs)
139	}
140	if len(profiles) != 1 || profiles[0].Description != "Configured reviewer" {
141		t.Fatalf("configured precedence = %#v", profiles)
142	}
143}
144
145func TestLoadRejectsUnclosedFrontmatter(t *testing.T) {
146	path := filepath.Join(t.TempDir(), "profile.md")
147	if err := os.WriteFile(path, []byte("---\nname: reviewer\nInstructions without a closing delimiter.\n"), 0o600); err != nil {
148		t.Fatal(err)
149	}
150	if _, err := load(path, "test"); err == nil || !strings.Contains(err.Error(), "missing closing delimiter") {
151		t.Fatalf("load error = %v, want missing closing delimiter", err)
152	}
153}
154
155func TestLoadRejectsInvalidClosedSchemaMetadata(t *testing.T) {
156	cases := []struct {
157		name  string
158		field string
159	}{
160		{name: "invalid-thinking", field: "thinking: extreme"},
161		{name: "invalid-reasoning", field: "reasoning: extreme"},
162		{name: "qualified-model-with-provider", field: "provider: openai\nmodel: openai-codex/gpt-5"},
163		{name: "invalid-prompt-mode", field: "systemPromptMode: merge"},
164		{name: "invalid-project-context", field: "inheritProjectContext: sometimes"},
165		{name: "invalid-skills", field: "inheritSkills: sometimes"},
166		{name: "invalid-fast-mode", field: "fastMode: sometimes"},
167		{name: "invalid-budget-ratio", field: "budgetRatio: 1.2"},
168		{name: "invalid-budget-tokens", field: "budgetTokens: 0"},
169		{name: "conflicting-budget", field: "budgetRatio: 0.5\nbudgetTokens: 1000"},
170	}
171	for _, tc := range cases {
172		t.Run(tc.name, func(t *testing.T) {
173			path := filepath.Join(t.TempDir(), "profile.md")
174			writeProfile(t, path, fmt.Sprintf("---\nname: test\n%s\n---\nInstructions.\n", tc.field))
175			if _, err := load(path, "test"); err == nil {
176				t.Fatalf("load succeeded for invalid metadata %q", tc.field)
177			}
178		})
179	}
180}
181
182func TestLoadProfileBudgetOverride(t *testing.T) {
183	path := filepath.Join(t.TempDir(), "profile.md")
184	writeProfile(t, path, "---\nname: reviewer\nbudgetRatio: 0.6\n---\nInstructions.\n")
185	profile, err := load(path, "test")
186	if err != nil {
187		t.Fatal(err)
188	}
189	if profile.BudgetRatio == nil || *profile.BudgetRatio != 0.6 || profile.BudgetTokens != nil {
190		t.Fatalf("profile budget = ratio %v tokens %v", profile.BudgetRatio, profile.BudgetTokens)
191	}
192}
193
194func TestProfileModelSelection(t *testing.T) {
195	profile := &Profile{Model: "openrouter/anthropic/claude-sonnet"}
196	provider, model := profile.ModelSelection()
197	if provider != "openrouter" || model != "anthropic/claude-sonnet" {
198		t.Fatalf("selection = (%q, %q)", provider, model)
199	}
200
201	profile = &Profile{Provider: "openai", Model: "gpt-5"}
202	provider, model = profile.ModelSelection()
203	if provider != "openai" || model != "gpt-5" {
204		t.Fatalf("explicit selection = (%q, %q)", provider, model)
205	}
206}
207
208func TestProfileToolsPreserveDeclarationAndResolve(t *testing.T) {
209	dir := t.TempDir()
210	writeProfile(t, filepath.Join(dir, "absent.md"), "---\nname: absent\n---\nInstructions.\n")
211	writeProfile(t, filepath.Join(dir, "empty.md"), "---\nname: empty\ntools: []\n---\nInstructions.\n")
212	writeProfile(t, filepath.Join(dir, "listed.md"), "---\nname: listed\ntools: [read, bash]\n---\nInstructions.\n")
213
214	for _, tc := range []struct {
215		name     string
216		declared bool
217		want     []string
218	}{
219		{name: "absent", declared: false, want: []string{"read", "bash", "lsp"}},
220		{name: "empty", declared: true, want: []string{}},
221		{name: "listed", declared: true, want: []string{"read", "bash"}},
222	} {
223		t.Run(tc.name, func(t *testing.T) {
224			profile, err := load(filepath.Join(dir, tc.name+".md"), "test")
225			if err != nil {
226				t.Fatal(err)
227			}
228			if profile.ToolsDeclared != tc.declared {
229				t.Fatalf("ToolsDeclared = %v, want %v", profile.ToolsDeclared, tc.declared)
230			}
231			got, err := ResolveProfileTools(profile, []string{"read", "bash", "lsp"}, func(name string) bool { return name != "lsp" })
232			if err != nil {
233				t.Fatal(err)
234			}
235			if strings.Join(got, ",") != strings.Join(tc.want, ",") {
236				t.Fatalf("resolved tools = %#v, want %#v", got, tc.want)
237			}
238		})
239	}
240}
241
242func TestResolveProfileToolsIgnoresUnknownAndDeniedNames(t *testing.T) {
243	profile := &Profile{Name: "reviewer", ToolsDeclared: true, Tools: []string{"read", "grep", "bash", "tasked_phases"}}
244	got, err := ResolveProfileTools(profile, []string{"read", "bash"}, func(name string) bool { return name != "bash" })
245	if err != nil {
246		t.Fatal(err)
247	}
248	if want := []string{"read"}; strings.Join(got, ",") != strings.Join(want, ",") {
249		t.Fatalf("resolved tools = %#v, want %#v", got, want)
250	}
251}
252
253func TestSystemPromptAddendumIsCompactAndDoesNotExposeBodyOrPath(t *testing.T) {
254	profiles := []*Profile{{
255		Name:         "reviewer",
256		Description:  "Read-only\nreviewer",
257		SystemPrompt: "secret instructions that belong only to the child",
258		Model:        "openai-codex/gpt-5",
259		Thinking:     "max",
260		FastMode:     boolPtr(false),
261		Tools:        []string{"read", "bash"},
262		Path:         "/private/profile.md",
263	}}
264	got := SystemPromptAddendum(profiles)
265	for _, want := range []string{"[subagents_list]", "[/subagents_list]", "reviewer", "Read-only reviewer", "model=openai-codex/gpt-5", "thinking=max", "fastMode=false"} {
266		if !strings.Contains(got, want) {
267			t.Fatalf("addendum missing %q:\n%s", want, got)
268		}
269	}
270	for _, unwanted := range []string{"secret instructions", "/private/profile.md", "\nreviewer"} {
271		if strings.Contains(got, unwanted) {
272			t.Fatalf("addendum contains %q:\n%s", unwanted, got)
273		}
274	}
275}
276
277func boolPtr(value bool) *bool { return &value }
278
279func writeProfile(t *testing.T, path, content string) {
280	t.Helper()
281	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
282		t.Fatal(err)
283	}
284	if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
285		t.Fatal(err)
286	}
287}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close