| 1 | package skill |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "reasonix/internal/event" |
| 14 | "reasonix/internal/tool" |
| 15 | ) |
| 16 | |
| 17 | func TestPreparePluginSkillBindsMCPNamesAndAllowedTools(t *testing.T) { |
| 18 | store := New(Options{HomeDir: t.TempDir(), DisableBuiltins: true}) |
| 19 | bindings := []tool.MCPBinding{ |
| 20 | {Package: "figma", Server: "figma", RawName: "figma_get_design_context", VisibleName: "get_design_context", CallableName: "mcp__figma__get_design_context", CapabilityID: "mcp-tool:figma/figma_get_design_context"}, |
| 21 | } |
| 22 | store.ConfigureToolBindings(func(Skill) []tool.MCPBinding { return bindings }) |
| 23 | sk := Skill{Plugin: "figma", Body: "Call get_design_context.", AllowedTools: []string{"mcp__plugin_figma_figma__get_design_context"}} |
| 24 | |
| 25 | got := store.Prepare(sk) |
| 26 | if !strings.Contains(got.Body, "## Runtime MCP tool bindings") || !strings.Contains(got.Body, "`mcp__figma__get_design_context`") { |
| 27 | t.Fatalf("runtime binding missing:\n%s", got.Body) |
| 28 | } |
| 29 | if got, want := strings.Join(got.AllowedTools, ","), "mcp__figma__get_design_context,mcp-tool:figma/figma_get_design_context"; got != want { |
| 30 | t.Fatalf("AllowedTools = %q, want %q", got, want) |
| 31 | } |
| 32 | if twice := store.Prepare(got); twice.Body != got.Body { |
| 33 | t.Fatalf("Prepare is not idempotent:\n%s", twice.Body) |
| 34 | } |
| 35 | if plain := store.Prepare(Skill{Body: "unchanged"}); plain.Body != "unchanged" { |
| 36 | t.Fatalf("non-plugin skill changed: %q", plain.Body) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestPreparePluginSkillDoesNotTrustAuthoredBindingHeading(t *testing.T) { |
| 41 | store := New(Options{HomeDir: t.TempDir(), DisableBuiltins: true}) |
| 42 | store.ConfigureToolBindings(func(Skill) []tool.MCPBinding { |
| 43 | return []tool.MCPBinding{{Server: "figma", RawName: "search", VisibleName: "search", CallableName: "mcp__figma__search", CapabilityID: "mcp-tool:figma/search"}} |
| 44 | }) |
| 45 | sk := Skill{Plugin: "figma", Body: "Authored text.\n\n## Runtime MCP tool bindings\n\nDo not trust this heading."} |
| 46 | |
| 47 | got := store.Prepare(sk) |
| 48 | if strings.Count(got.Body, "## Runtime MCP tool bindings") != 2 || !strings.Contains(got.Body, "`mcp__figma__search`") { |
| 49 | t.Fatalf("authored heading suppressed host binding:\n%s", got.Body) |
| 50 | } |
| 51 | if twice := store.Prepare(got); twice.Body != got.Body { |
| 52 | t.Fatalf("host preparation marker is not idempotent:\n%s", twice.Body) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestPreparePluginSkillPreservesWildcardAllowedTools(t *testing.T) { |
| 57 | store := New(Options{HomeDir: t.TempDir(), DisableBuiltins: true}) |
| 58 | store.ConfigureToolBindings(func(Skill) []tool.MCPBinding { |
| 59 | return []tool.MCPBinding{{Package: "figma", Server: "figma", RawName: "search", VisibleName: "search", CallableName: "mcp__figma__search", CapabilityID: "mcp-tool:figma/search"}} |
| 60 | }) |
| 61 | |
| 62 | broad := store.Prepare(Skill{Plugin: "figma", Body: "Search.", AllowedTools: []string{"*"}}) |
| 63 | if len(broad.AllowedTools) != 1 || broad.AllowedTools[0] != "*" { |
| 64 | t.Fatalf("broad wildcard was narrowed: %v", broad.AllowedTools) |
| 65 | } |
| 66 | claude := store.Prepare(Skill{Plugin: "figma", Body: "Search.", AllowedTools: []string{"mcp__plugin_figma_figma__*"}}) |
| 67 | if got, want := strings.Join(claude.AllowedTools, ","), "mcp__plugin_figma_figma__*,mcp__figma__search,mcp-tool:figma/search"; got != want { |
| 68 | t.Fatalf("Claude wildcard mapping = %q, want %q", got, want) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestPreparePluginSkillDoesNotWidenAmbiguousAllowedTool(t *testing.T) { |
| 73 | store := New(Options{HomeDir: t.TempDir(), DisableBuiltins: true}) |
| 74 | store.ConfigureToolBindings(func(Skill) []tool.MCPBinding { |
| 75 | return []tool.MCPBinding{ |
| 76 | {Server: "one", RawName: "search", VisibleName: "search", CallableName: "mcp__one__search", CapabilityID: "mcp-tool:one/search"}, |
| 77 | {Server: "two", RawName: "search", VisibleName: "search", CallableName: "mcp__two__search", CapabilityID: "mcp-tool:two/search"}, |
| 78 | } |
| 79 | }) |
| 80 | got := store.Prepare(Skill{Plugin: "pkg", Body: "Search.", AllowedTools: []string{"search"}}) |
| 81 | if len(got.AllowedTools) != 1 || got.AllowedTools[0] != "search" { |
| 82 | t.Fatalf("ambiguous literal widened permissions: %v", got.AllowedTools) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestRunSkillInline(t *testing.T) { |
| 87 | home := t.TempDir() |
| 88 | writeSkill(t, home, ".reasonix/skills/note.md", "---\ndescription: take a note\n---\nDo the thing.") |
| 89 | tl := NewRunSkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), nil) |
| 90 | |
| 91 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"note","arguments":"with args"}`)) |
| 92 | if err != nil { |
| 93 | t.Fatalf("execute: %v", err) |
| 94 | } |
| 95 | if !strings.HasPrefix(out, "<skill-pin name=\"note\">") || !strings.HasSuffix(out, "</skill-pin>") { |
| 96 | t.Errorf("inline skill should be skill-pin wrapped:\n%s", out) |
| 97 | } |
| 98 | if !strings.Contains(out, "Do the thing.") || !strings.Contains(out, "Arguments: with args") { |
| 99 | t.Errorf("body/args missing:\n%s", out) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestRunSkillUnknown(t *testing.T) { |
| 104 | tl := NewRunSkillTool(New(Options{HomeDir: t.TempDir(), DisableBuiltins: true}), nil) |
| 105 | if _, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"nope"}`)); err == nil { |
| 106 | t.Error("unknown skill should error") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestRunSkillDoesNotBlockOnDiagnosticProfiles(t *testing.T) { |
| 111 | home := t.TempDir() |
| 112 | writeSkill(t, home, ".reasonix/skills/delivery-only.md", "---\ndescription: ship it\nprofiles: delivery\n---\nDeliver it.") |
| 113 | store := New(Options{HomeDir: home, DisableBuiltins: true}) |
| 114 | store.ConfigureInvocationPolicy("economy", nil) |
| 115 | tl := NewRunSkillTool(store, nil) |
| 116 | |
| 117 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"delivery-only"}`)) |
| 118 | if err != nil { |
| 119 | t.Fatalf("profiles frontmatter must not block run_skill: %v", err) |
| 120 | } |
| 121 | if !strings.Contains(out, "Deliver it.") { |
| 122 | t.Fatalf("skill body missing:\n%s", out) |
| 123 | } |
| 124 | // AllowedInProfile remains accurate for doctor diagnostics. |
| 125 | sk, ok := store.Read("delivery-only") |
| 126 | if !ok { |
| 127 | t.Fatal("skill missing from store") |
| 128 | } |
| 129 | if AllowedInProfile(sk, "economy") { |
| 130 | t.Fatal("diagnostic AllowedInProfile(economy) should be false for delivery-only skill") |
| 131 | } |
| 132 | if !AllowedInProfile(sk, "delivery") { |
| 133 | t.Fatal("diagnostic AllowedInProfile(delivery) should be true") |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestRunSkillEnforcesRequiredCapabilities(t *testing.T) { |
| 138 | home := t.TempDir() |
| 139 | writeSkill(t, home, ".reasonix/skills/github-review.md", "---\ndescription: review github\nrequires: mcp-server:github, mcp-tool:github/search_issues\n---\nReview it.") |
| 140 | store := New(Options{HomeDir: home, DisableBuiltins: true}) |
| 141 | store.ConfigureInvocationPolicy("delivery", func(requires []string) []string { |
| 142 | return []string{"mcp-tool:github/search_issues"} |
| 143 | }) |
| 144 | tl := NewRunSkillTool(store, nil) |
| 145 | |
| 146 | _, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"github-review"}`)) |
| 147 | if err == nil || !errors.Is(err, ErrInvocationUnavailable) || !strings.Contains(err.Error(), "requires unavailable capabilities: mcp-tool:github/search_issues") { |
| 148 | t.Fatalf("requires-gated run_skill error = %v", err) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func TestRunSkillSubagentNeedsRunner(t *testing.T) { |
| 153 | home := t.TempDir() |
| 154 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 155 | tl := NewRunSkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), nil) // nil runner |
| 156 | if _, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"dig","arguments":"go"}`)); err == nil { |
| 157 | t.Error("subagent skill with no runner should error, not silently inline") |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestRunSkillSubagentRuns(t *testing.T) { |
| 162 | home := t.TempDir() |
| 163 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 164 | var gotTask string |
| 165 | runner := func(_ context.Context, sk Skill, task string, _ SubagentRunOptions) (string, error) { |
| 166 | gotTask = task |
| 167 | return "answer from " + sk.Name, nil |
| 168 | } |
| 169 | tl := NewRunSkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), runner) |
| 170 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"dig","arguments":"find X"}`)) |
| 171 | if err != nil { |
| 172 | t.Fatalf("execute: %v", err) |
| 173 | } |
| 174 | if gotTask != "find X" { |
| 175 | t.Errorf("runner got task %q", gotTask) |
| 176 | } |
| 177 | if out != "answer from dig" { |
| 178 | t.Errorf("runner output not returned: %q", out) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | type subagentOutputError struct{} |
| 183 | |
| 184 | func (subagentOutputError) Error() string { return "subagent paused" } |
| 185 | |
| 186 | func (subagentOutputError) SubagentOutput() string { return "status=partial ref=sa_test\nlast output" } |
| 187 | |
| 188 | func TestRunSkillSubagentPreservesOutputWhenRunnerReturnsTypedFailure(t *testing.T) { |
| 189 | home := t.TempDir() |
| 190 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 191 | runner := func(context.Context, Skill, string, SubagentRunOptions) (string, error) { |
| 192 | return "status=partial ref=sa_test\nlast output", subagentOutputError{} |
| 193 | } |
| 194 | tl := NewRunSkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), runner) |
| 195 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"dig","arguments":"find X"}`)) |
| 196 | if err == nil { |
| 197 | t.Fatal("expected typed subagent failure") |
| 198 | } |
| 199 | if !strings.Contains(out, "ref=sa_test") { |
| 200 | t.Fatalf("typed failure output was discarded: %q", out) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | func TestRunSkillSubagentResultWarnsOnHostDecisionLanguage(t *testing.T) { |
| 205 | home := t.TempDir() |
| 206 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 207 | runner := func(_ context.Context, sk Skill, task string, _ SubagentRunOptions) (string, error) { |
| 208 | return "等待用户批准后再执行 " + sk.Name + " " + task, nil |
| 209 | } |
| 210 | tl := NewRunSkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), runner) |
| 211 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"dig","arguments":"find X"}`)) |
| 212 | if err != nil { |
| 213 | t.Fatalf("execute: %v", err) |
| 214 | } |
| 215 | if !strings.Contains(out, "Subagent boundary") { |
| 216 | t.Fatalf("subagent skill output missing boundary warning:\n%s", out) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestRunSkillSubagentCancellationReachesRunner(t *testing.T) { |
| 221 | home := t.TempDir() |
| 222 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 223 | runner := func(ctx context.Context, _ Skill, _ string, _ SubagentRunOptions) (string, error) { |
| 224 | <-ctx.Done() |
| 225 | return "", ctx.Err() |
| 226 | } |
| 227 | tl := NewRunSkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), runner) |
| 228 | |
| 229 | ctx, cancel := context.WithCancel(context.Background()) |
| 230 | done := make(chan error, 1) |
| 231 | go func() { |
| 232 | _, err := tl.Execute(ctx, json.RawMessage(`{"name":"dig","arguments":"find X"}`)) |
| 233 | done <- err |
| 234 | }() |
| 235 | |
| 236 | time.Sleep(50 * time.Millisecond) |
| 237 | cancel() |
| 238 | |
| 239 | select { |
| 240 | case err := <-done: |
| 241 | if !errors.Is(err, context.Canceled) { |
| 242 | t.Fatalf("Execute error = %v, want context cancellation", err) |
| 243 | } |
| 244 | case <-time.After(500 * time.Millisecond): |
| 245 | t.Fatal("run_skill subagent runner did not observe cancellation promptly") |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | func TestReadOnlySkillInlineAndIsReadOnly(t *testing.T) { |
| 250 | home := t.TempDir() |
| 251 | writeSkill(t, home, ".reasonix/skills/note.md", "---\ndescription: take a note\n---\nDo the thing.") |
| 252 | tl := NewReadOnlySkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), nil) |
| 253 | |
| 254 | if !tl.ReadOnly() { |
| 255 | t.Fatal("read_only_skill must report ReadOnly for permission and restricted-runner classification") |
| 256 | } |
| 257 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"note","arguments":"with args"}`)) |
| 258 | if err != nil { |
| 259 | t.Fatalf("execute: %v", err) |
| 260 | } |
| 261 | if !strings.Contains(out, "Do the thing.") || !strings.Contains(out, "Arguments: with args") { |
| 262 | t.Errorf("inline body/args missing:\n%s", out) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | func TestReadOnlySkillSubagentRunsWithoutContinuation(t *testing.T) { |
| 267 | home := t.TempDir() |
| 268 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 269 | var gotTask string |
| 270 | var gotOpts SubagentRunOptions |
| 271 | runner := func(_ context.Context, sk Skill, task string, opts SubagentRunOptions) (string, error) { |
| 272 | gotTask = task |
| 273 | gotOpts = opts |
| 274 | return "read-only answer from " + sk.Name, nil |
| 275 | } |
| 276 | tl := NewReadOnlySkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), runner) |
| 277 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"dig","arguments":"find X"}`)) |
| 278 | if err != nil { |
| 279 | t.Fatalf("execute: %v", err) |
| 280 | } |
| 281 | if gotTask != "find X" { |
| 282 | t.Errorf("runner got task %q", gotTask) |
| 283 | } |
| 284 | if gotOpts.ContinueFrom != "" || gotOpts.ForkFrom != "" { |
| 285 | t.Fatalf("read_only_skill should not pass continuation opts, got %+v", gotOpts) |
| 286 | } |
| 287 | if out != "read-only answer from dig" { |
| 288 | t.Errorf("runner output not returned: %q", out) |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | func TestReadOnlySkillSubagentRequiresArgs(t *testing.T) { |
| 293 | home := t.TempDir() |
| 294 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 295 | runner := func(_ context.Context, _ Skill, _ string, _ SubagentRunOptions) (string, error) { |
| 296 | return "x", nil |
| 297 | } |
| 298 | tl := NewReadOnlySkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), runner) |
| 299 | if _, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"dig"}`)); err == nil { |
| 300 | t.Error("read_only_skill subagent should require arguments") |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | func TestReadOnlySkillSubagentResolvesProfile(t *testing.T) { |
| 305 | home := t.TempDir() |
| 306 | writeSkill(t, home, ".reasonix/skills/deep.md", "---\ndescription: deep\nrunAs: subagent\nmodel: deepseek-pro\neffort: max\n---\nbody") |
| 307 | tl := NewReadOnlySkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), nil) |
| 308 | |
| 309 | pr, ok := tl.(interface { |
| 310 | ResolveProfile(json.RawMessage) *event.Profile |
| 311 | }) |
| 312 | if !ok { |
| 313 | t.Fatal("read_only_skill should expose ResolveProfile") |
| 314 | } |
| 315 | got := pr.ResolveProfile(json.RawMessage(`{"name":"deep","arguments":"x"}`)) |
| 316 | if got == nil || got.Model != "deepseek-pro" || got.Effort != "max" { |
| 317 | t.Fatalf("profile = %+v, want deepseek-pro/max", got) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | func TestRunSkillSubagentResolvesProfile(t *testing.T) { |
| 322 | home := t.TempDir() |
| 323 | writeSkill(t, home, ".reasonix/skills/deep.md", "---\ndescription: deep\nrunAs: subagent\nmodel: deepseek-pro\neffort: max\n---\nbody") |
| 324 | tl := NewRunSkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), nil) |
| 325 | |
| 326 | pr, ok := tl.(interface { |
| 327 | ResolveProfile(json.RawMessage) *event.Profile |
| 328 | }) |
| 329 | if !ok { |
| 330 | t.Fatal("run_skill should expose ResolveProfile") |
| 331 | } |
| 332 | got := pr.ResolveProfile(json.RawMessage(`{"name":"deep","arguments":"x"}`)) |
| 333 | if got == nil || got.Model != "deepseek-pro" || got.Effort != "max" { |
| 334 | t.Fatalf("profile = %+v, want deepseek-pro/max", got) |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | func TestRunSkillSubagentRequiresArgs(t *testing.T) { |
| 339 | home := t.TempDir() |
| 340 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 341 | runner := func(_ context.Context, _ Skill, _ string, _ SubagentRunOptions) (string, error) { |
| 342 | return "x", nil |
| 343 | } |
| 344 | tl := NewRunSkillTool(New(Options{HomeDir: home, DisableBuiltins: true}), runner) |
| 345 | if _, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"dig"}`)); err == nil { |
| 346 | t.Error("subagent skill should require arguments") |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | func TestCleanSkillName(t *testing.T) { |
| 351 | cases := map[string]string{ |
| 352 | "explore": "explore", |
| 353 | "explore [🧬 subagent]": "explore", |
| 354 | "[🧬 subagent] explore": "explore", |
| 355 | " review ": "review", |
| 356 | "[only a tag]": "", |
| 357 | "": "", |
| 358 | } |
| 359 | for in, want := range cases { |
| 360 | if got := cleanSkillName(in); got != want { |
| 361 | t.Errorf("cleanSkillName(%q) = %q, want %q", in, got, want) |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | func TestBuiltinSubagentToolsRunner(t *testing.T) { |
| 367 | var ran string |
| 368 | runner := func(_ context.Context, sk Skill, task string, _ SubagentRunOptions) (string, error) { |
| 369 | ran = sk.Name + ":" + task |
| 370 | return "ok", nil |
| 371 | } |
| 372 | tools := BuiltinSubagentTools(New(Options{HomeDir: t.TempDir()}), runner) |
| 373 | var explore interface { |
| 374 | Name() string |
| 375 | Execute(context.Context, json.RawMessage) (string, error) |
| 376 | } |
| 377 | for _, tl := range tools { |
| 378 | if tl.Name() == "explore" { |
| 379 | explore = tl |
| 380 | } |
| 381 | } |
| 382 | if explore == nil { |
| 383 | t.Fatal("explore wrapper tool not built") |
| 384 | } |
| 385 | if _, err := explore.Execute(context.Background(), json.RawMessage(`{"task":"map the loop"}`)); err != nil { |
| 386 | t.Fatalf("execute: %v", err) |
| 387 | } |
| 388 | if ran != "explore:map the loop" { |
| 389 | t.Errorf("runner not invoked correctly: %q", ran) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | func TestBuiltinSubagentToolsPassContinuationOptions(t *testing.T) { |
| 394 | var got SubagentRunOptions |
| 395 | runner := func(_ context.Context, _ Skill, _ string, opts SubagentRunOptions) (string, error) { |
| 396 | got = opts |
| 397 | return "ok", nil |
| 398 | } |
| 399 | tools := BuiltinSubagentTools(New(Options{HomeDir: t.TempDir()}), runner) |
| 400 | var review interface { |
| 401 | Name() string |
| 402 | Execute(context.Context, json.RawMessage) (string, error) |
| 403 | } |
| 404 | for _, tl := range tools { |
| 405 | if tl.Name() == "review" { |
| 406 | review = tl |
| 407 | break |
| 408 | } |
| 409 | } |
| 410 | if review == nil { |
| 411 | t.Fatal("review wrapper tool not built") |
| 412 | } |
| 413 | if _, err := review.Execute(context.Background(), json.RawMessage(`{"task":"again","continue_from":"sa_prev"}`)); err != nil { |
| 414 | t.Fatalf("execute: %v", err) |
| 415 | } |
| 416 | if got.ContinueFrom != "sa_prev" { |
| 417 | t.Fatalf("continuation opts = %+v, want continue_from sa_prev", got) |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | func TestRunSkillToolPassesLegacyForkOption(t *testing.T) { |
| 422 | var got SubagentRunOptions |
| 423 | runner := func(_ context.Context, _ Skill, _ string, opts SubagentRunOptions) (string, error) { |
| 424 | got = opts |
| 425 | return "ok", nil |
| 426 | } |
| 427 | runSkill := NewRunSkillTool(New(Options{HomeDir: t.TempDir()}), runner) |
| 428 | if _, err := runSkill.Execute(context.Background(), json.RawMessage(`{"name":"review","arguments":"again","fork_from":"sa_prev"}`)); err != nil { |
| 429 | t.Fatalf("execute: %v", err) |
| 430 | } |
| 431 | if got.ForkFrom != "sa_prev" { |
| 432 | t.Fatalf("continuation opts = %+v, want fork_from sa_prev", got) |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | func TestBuiltinSubagentToolsPassLegacyForkOption(t *testing.T) { |
| 437 | var got SubagentRunOptions |
| 438 | runner := func(_ context.Context, _ Skill, _ string, opts SubagentRunOptions) (string, error) { |
| 439 | got = opts |
| 440 | return "ok", nil |
| 441 | } |
| 442 | tools := BuiltinSubagentTools(New(Options{HomeDir: t.TempDir()}), runner) |
| 443 | var review interface { |
| 444 | Name() string |
| 445 | Execute(context.Context, json.RawMessage) (string, error) |
| 446 | } |
| 447 | for _, tl := range tools { |
| 448 | if tl.Name() == "review" { |
| 449 | review = tl |
| 450 | break |
| 451 | } |
| 452 | } |
| 453 | if review == nil { |
| 454 | t.Fatal("review wrapper tool not built") |
| 455 | } |
| 456 | if _, err := review.Execute(context.Background(), json.RawMessage(`{"task":"again","fork_from":"sa_prev"}`)); err != nil { |
| 457 | t.Fatalf("execute: %v", err) |
| 458 | } |
| 459 | if got.ForkFrom != "sa_prev" { |
| 460 | t.Fatalf("continuation opts = %+v, want fork_from sa_prev", got) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | func TestSubagentSkillSchemasExposeOnlyContinueFromForPersistence(t *testing.T) { |
| 465 | runSkill := NewRunSkillTool(New(Options{HomeDir: t.TempDir(), DisableBuiltins: true}), nil) |
| 466 | runSchema := string(runSkill.Schema()) |
| 467 | if !strings.Contains(runSchema, `"continue_from"`) { |
| 468 | t.Fatalf("run_skill schema = %s, want continue_from", runSchema) |
| 469 | } |
| 470 | if strings.Contains(runSchema, "fork_from") { |
| 471 | t.Fatalf("run_skill schema = %s, want no fork_from", runSchema) |
| 472 | } |
| 473 | |
| 474 | tools := BuiltinSubagentTools(New(Options{HomeDir: t.TempDir()}), nil) |
| 475 | for _, tl := range tools { |
| 476 | schema := string(tl.Schema()) |
| 477 | if !strings.Contains(schema, `"continue_from"`) { |
| 478 | t.Fatalf("%s schema = %s, want continue_from", tl.Name(), schema) |
| 479 | } |
| 480 | if strings.Contains(schema, "fork_from") { |
| 481 | t.Fatalf("%s schema = %s, want no fork_from", tl.Name(), schema) |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | func TestBuiltinSubagentToolResolvesProfile(t *testing.T) { |
| 487 | store := New(Options{HomeDir: t.TempDir()}) |
| 488 | tools := BuiltinSubagentTools(store, nil, func(sk Skill) *event.Profile { |
| 489 | return &event.Profile{Model: sk.Name + "-model", Effort: "max"} |
| 490 | }) |
| 491 | var review interface { |
| 492 | ResolveProfile(json.RawMessage) *event.Profile |
| 493 | } |
| 494 | for _, tl := range tools { |
| 495 | if tl.Name() == "review" { |
| 496 | review = tl.(interface { |
| 497 | ResolveProfile(json.RawMessage) *event.Profile |
| 498 | }) |
| 499 | break |
| 500 | } |
| 501 | } |
| 502 | if review == nil { |
| 503 | t.Fatal("review tool not found") |
| 504 | } |
| 505 | got := review.ResolveProfile(json.RawMessage(`{"task":"general"}`)) |
| 506 | if got == nil || got.Model != "review-model" || got.Effort != "max" { |
| 507 | t.Fatalf("profile = %+v, want review-model/max", got) |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | func TestInstallSkill(t *testing.T) { |
| 512 | home := t.TempDir() |
| 513 | st := New(Options{HomeDir: home, DisableBuiltins: true}) |
| 514 | tl := NewInstallSkillTool(st, nil) |
| 515 | |
| 516 | out, err := tl.Execute(context.Background(), json.RawMessage( |
| 517 | `{"name":"deploy","description":"ship it","body":"steps","runAs":"subagent","model":"deepseek-pro","effort":"max","allowedTools":["bash","read_file"]}`)) |
| 518 | if err != nil { |
| 519 | t.Fatalf("execute: %v", err) |
| 520 | } |
| 521 | if !strings.Contains(out, `"ok":true`) { |
| 522 | t.Errorf("expected ok result, got %s", out) |
| 523 | } |
| 524 | var res struct { |
| 525 | Path string `json:"path"` |
| 526 | } |
| 527 | if err := json.Unmarshal([]byte(out), &res); err != nil { |
| 528 | t.Fatalf("result JSON: %v", err) |
| 529 | } |
| 530 | wantPath := filepath.Join(home, ".reasonix", "skills", "deploy", SkillFile) |
| 531 | if res.Path != wantPath { |
| 532 | t.Fatalf("install_skill should report canonical path %s, got %s", wantPath, res.Path) |
| 533 | } |
| 534 | if _, err := os.Stat(wantPath); err != nil { |
| 535 | t.Fatalf("install_skill should write canonical SKILL.md: %v", err) |
| 536 | } |
| 537 | if _, err := os.Stat(filepath.Join(home, ".reasonix", "skills", "deploy.md")); !errors.Is(err, os.ErrNotExist) { |
| 538 | t.Fatalf("install_skill should not write legacy flat deploy.md, stat err=%v", err) |
| 539 | } |
| 540 | // Round-trips through the store with the frontmatter we wrote. |
| 541 | sk, ok := st.Read("deploy") |
| 542 | if !ok { |
| 543 | t.Fatal("installed skill not readable") |
| 544 | } |
| 545 | if sk.RunAs != RunSubagent || sk.Model != "deepseek-pro" || sk.Effort != "max" || len(sk.AllowedTools) != 2 { |
| 546 | t.Errorf("frontmatter not round-tripped: runAs=%s model=%q effort=%q tools=%v", sk.RunAs, sk.Model, sk.Effort, sk.AllowedTools) |
| 547 | } |
| 548 | // Refuses overwrite. |
| 549 | if _, err := tl.Execute(context.Background(), json.RawMessage( |
| 550 | `{"name":"deploy","description":"again","body":"x"}`)); err == nil { |
| 551 | t.Error("install_skill should refuse to overwrite") |
| 552 | } |
| 553 | // Requires description. |
| 554 | if _, err := tl.Execute(context.Background(), json.RawMessage( |
| 555 | `{"name":"x","description":"","body":"b"}`)); err == nil { |
| 556 | t.Error("install_skill should require a description") |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | func TestRenderSkillFileEmitsColorAndInvocationWhenSet(t *testing.T) { |
| 561 | content := RenderSkillFile(SkillFileOptions{ |
| 562 | Name: "my-agent", |
| 563 | Description: "a private helper", |
| 564 | Body: "be helpful", |
| 565 | RunAs: RunSubagent, |
| 566 | Color: "amber", |
| 567 | Invocation: "manual", |
| 568 | }) |
| 569 | for _, want := range []string{"color: amber\n", "invocation: manual\n", "runAs: subagent\n"} { |
| 570 | if !strings.Contains(content, want) { |
| 571 | t.Errorf("rendered content missing %q:\n%s", want, content) |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | home := t.TempDir() |
| 576 | st := New(Options{HomeDir: home, DisableBuiltins: true}) |
| 577 | if _, err := st.CreateWithContent("my-agent", ScopeGlobal, content); err != nil { |
| 578 | t.Fatalf("CreateWithContent: %v", err) |
| 579 | } |
| 580 | sk, ok := st.Read("my-agent") |
| 581 | if !ok { |
| 582 | t.Fatal("skill not readable after CreateWithContent") |
| 583 | } |
| 584 | if sk.Color != "amber" || sk.Invocation != "manual" { |
| 585 | t.Errorf("round-trip mismatch: color=%q invocation=%q", sk.Color, sk.Invocation) |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | // TestRenderSkillFileEscapesYAMLMetacharacters pins the security contract the |
| 590 | // reviewer flagged: free text with YAML metacharacters must round-trip intact. |
| 591 | // Before the yaml.v3 renderer, a description like "Review code: focus on |
| 592 | // security" produced an unparseable block; frontmatter.Split then returned an |
| 593 | // EMPTY map and the loader silently fell back to runAs=inline + |
| 594 | // invocation=auto — dissolving both the isolation boundary and the |
| 595 | // no-autodiscovery guarantee. |
| 596 | func TestRenderSkillFileEscapesYAMLMetacharacters(t *testing.T) { |
| 597 | cases := []struct { |
| 598 | label string |
| 599 | desc string |
| 600 | }{ |
| 601 | {"colon", "Review code: focus on security"}, |
| 602 | {"hash", "Reviews #security and #perf tags"}, |
| 603 | {"double-quote", `Says "hello" politely`}, |
| 604 | {"single-quote", "Don't break on apostrophes"}, |
| 605 | {"newline", "First line\nsecond line"}, |
| 606 | {"leading-special", "- starts like a list item"}, |
| 607 | {"yaml-lookalike", "runAs: inline"}, |
| 608 | } |
| 609 | for _, tc := range cases { |
| 610 | t.Run(tc.label, func(t *testing.T) { |
| 611 | home := t.TempDir() |
| 612 | st := New(Options{HomeDir: home, DisableBuiltins: true}) |
| 613 | content := RenderSkillFile(SkillFileOptions{ |
| 614 | Name: "esc", |
| 615 | Description: tc.desc, |
| 616 | Body: "the body", |
| 617 | RunAs: RunSubagent, |
| 618 | Invocation: "manual", |
| 619 | }) |
| 620 | if _, err := st.CreateWithContent("esc", ScopeGlobal, content); err != nil { |
| 621 | t.Fatalf("CreateWithContent: %v", err) |
| 622 | } |
| 623 | sk, ok := st.Read("esc") |
| 624 | if !ok { |
| 625 | t.Fatalf("skill unreadable; rendered content:\n%s", content) |
| 626 | } |
| 627 | // The load-bearing assertions: the security-relevant fields must |
| 628 | // survive, never silently reset to their permissive defaults. |
| 629 | if sk.RunAs != RunSubagent { |
| 630 | t.Errorf("RunAs = %q, want subagent (isolation lost); content:\n%s", sk.RunAs, content) |
| 631 | } |
| 632 | if sk.Invocation != "manual" { |
| 633 | t.Errorf("Invocation = %q, want manual (autodiscovery re-enabled); content:\n%s", sk.Invocation, content) |
| 634 | } |
| 635 | wantDesc := strings.TrimSpace(tc.desc) |
| 636 | if tc.label == "newline" { |
| 637 | // frontmatter.Split returns the scalar as parsed; the multi-line |
| 638 | // value survives YAML round-trip intact. |
| 639 | wantDesc = "First line\nsecond line" |
| 640 | } |
| 641 | if sk.Description != wantDesc { |
| 642 | t.Errorf("Description = %q, want %q", sk.Description, wantDesc) |
| 643 | } |
| 644 | if sk.Body != "the body" { |
| 645 | t.Errorf("Body = %q, want %q", sk.Body, "the body") |
| 646 | } |
| 647 | }) |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | func TestRenderSkillFileOmitsColorAndInvocationByDefault(t *testing.T) { |
| 652 | content := RenderSkillFile(SkillFileOptions{ |
| 653 | Name: "plain-inline", |
| 654 | Description: "no extras", |
| 655 | Body: "body text", |
| 656 | RunAs: RunInline, |
| 657 | }) |
| 658 | for _, unwanted := range []string{"color:", "invocation:", "runAs:"} { |
| 659 | if strings.Contains(content, unwanted) { |
| 660 | t.Errorf("rendered content should omit %q when unset:\n%s", unwanted, content) |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | func TestReadSkillLoadsInlineAndIsReadOnly(t *testing.T) { |
| 666 | home := t.TempDir() |
| 667 | writeSkill(t, home, ".reasonix/skills/note.md", "---\ndescription: take a note\n---\nDo the thing.") |
| 668 | tl := NewReadSkillTool(New(Options{HomeDir: home, DisableBuiltins: true})) |
| 669 | |
| 670 | if !tl.ReadOnly() { |
| 671 | t.Fatal("read_skill must be ReadOnly so it works in plan mode") |
| 672 | } |
| 673 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"note","arguments":"with args"}`)) |
| 674 | if err != nil { |
| 675 | t.Fatalf("execute: %v", err) |
| 676 | } |
| 677 | if !strings.Contains(out, "Do the thing.") || !strings.Contains(out, "Arguments: with args") { |
| 678 | t.Errorf("inline body/args missing:\n%s", out) |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | func TestReadSkillRejectsSubagent(t *testing.T) { |
| 683 | home := t.TempDir() |
| 684 | writeSkill(t, home, ".reasonix/skills/dig.md", "---\ndescription: dig\nrunAs: subagent\n---\nbody") |
| 685 | tl := NewReadSkillTool(New(Options{HomeDir: home, DisableBuiltins: true})) |
| 686 | |
| 687 | if _, err := tl.Execute(context.Background(), json.RawMessage(`{"name":"dig"}`)); err == nil || !strings.Contains(err.Error(), "run_skill") { |
| 688 | t.Fatalf("read_skill on a subagent skill should point to run_skill, got %v", err) |
| 689 | } |
| 690 | } |
| 691 |