diff --git a/tool/skills/tool.go b/tool/skills/tool.go new file mode 100644 index 0000000..d51daeb --- /dev/null +++ b/tool/skills/tool.go @@ -0,0 +1,290 @@ +package skills + +import ( + "context" + "encoding/json" + "fmt" + "strings" + + "github.com/sausheong/harness/tool" +) + +// maxBodyBytes is the server-side cap on skill body length. Mirrors the +// schema's maxLength so the model and the server agree. +const maxBodyBytes = 32000 + +// SkillTool is the action-discriminated tool the agent calls to manage +// skills. Wraps any SkillStore; the default disk implementation is in +// tool/skills/disk/. +// +// Schema is one tool with an "action" enum (vs six separate tools) to +// keep the request prefix small (less schema bloat → better +// prompt-cache hits). +type SkillTool struct { + Store SkillStore +} + +// Name returns "skill_manage" (matches the Hermes naming for parity +// with the spec's reference implementation). +func (t *SkillTool) Name() string { return "skill_manage" } + +// Description is shown to the model in the tool list. +func (t *SkillTool) Description() string { + return "Create, patch, replace, remove, list, or get skills — durable " + + "procedural knowledge that survives across sessions. Use to " + + "capture workflows, conventions, or reusable techniques worth " + + "keeping." +} + +// Parameters returns the JSON-Schema for the tool input. +func (t *SkillTool) Parameters() json.RawMessage { + return json.RawMessage(`{ + "type": "object", + "properties": { + "action": { + "type": "string", + "enum": ["create", "patch", "replace", "remove", "list", "get"], + "description": "Operation to perform." + }, + "name": { + "type": "string", + "pattern": "^[a-z0-9][a-z0-9-]{0,63}$", + "description": "Skill name (kebab-case, 1-64 chars). Required for create/patch/replace/remove/get." + }, + "body": { + "type": "string", + "maxLength": 32000, + "description": "Full SKILL.md content for create/replace. May include YAML-ish frontmatter (---\ndescription: ...\n---\n) at the top. Max 32000 characters." + }, + "old_string": { + "type": "string", + "description": "Required for patch. The string to replace. Must occur exactly once in the existing body." + }, + "new_string": { + "type": "string", + "description": "Required for patch. The replacement string." + } + }, + "required": ["action"] + }`) +} + +// IsConcurrencySafe returns false — every action that modifies the +// store mutates shared state. Even read-only actions (list, get) touch +// the same on-disk hierarchy and are best serialized. +func (t *SkillTool) IsConcurrencySafe(_ json.RawMessage) bool { return false } + +// skillInput is the parsed tool input. +type skillInput struct { + Action string `json:"action"` + Name string `json:"name"` + Body string `json:"body"` + OldString string `json:"old_string"` + NewString string `json:"new_string"` +} + +// actionResult is the canonical envelope every successful or failed +// SkillTool call returns. runtime.Review reads this shape (target + +// success + message + name) to extract human-readable summaries. +// +// Same shape as memory.actionResult; we keep it duplicated rather than +// importing tool/memory for one struct so the two tool packages remain +// independent. +type actionResult struct { + Success bool `json:"success"` + Message string `json:"message,omitempty"` + Error string `json:"error,omitempty"` + Target string `json:"target,omitempty"` + Name string `json:"name,omitempty"` +} + +func (r actionResult) toToolResult() tool.ToolResult { + b, _ := json.Marshal(r) + return tool.ToolResult{Output: string(b)} +} + +func successResult(message, name string) tool.ToolResult { + return actionResult{ + Success: true, + Message: message, + Target: "skill", + Name: name, + }.toToolResult() +} + +func errorResult(errMsg, name string) tool.ToolResult { + return actionResult{ + Success: false, + Error: errMsg, + Target: "skill", + Name: name, + }.toToolResult() +} + +// truncatePreview returns content suitable for inclusion in a success +// message — truncated to ~60 chars with an ellipsis. +func truncatePreview(s string) string { + const max = 60 + s = strings.TrimSpace(s) + if len(s) <= max { + return s + } + return s[:max] + "…" +} + +// originFromContext returns the OriginKey value or "agent" as default. +func originFromContext(ctx context.Context) string { + if v := ctx.Value(OriginKey); v != nil { + if s, ok := v.(string); ok && s != "" { + return s + } + } + return "agent" +} + +// Execute dispatches on the action enum. +func (t *SkillTool) Execute(ctx context.Context, input json.RawMessage) (tool.ToolResult, error) { + if t.Store == nil { + return errorResult("skill tool: no store configured", ""), nil + } + var in skillInput + if err := json.Unmarshal(input, &in); err != nil { + return errorResult(fmt.Sprintf("invalid input: %v", err), ""), nil + } + switch in.Action { + case "create": + return t.executeCreate(ctx, in), nil + case "patch": + return t.executePatch(ctx, in), nil + case "replace": + return t.executeReplace(ctx, in), nil + case "remove": + return t.executeRemove(ctx, in), nil + case "list": + return t.executeList(ctx, in), nil + case "get": + return t.executeGet(ctx, in), nil + case "": + return errorResult("action is required", ""), nil + default: + return errorResult(fmt.Sprintf("unknown action %q", in.Action), ""), nil + } +} + +func (t *SkillTool) executeCreate(ctx context.Context, in skillInput) tool.ToolResult { + if in.Name == "" { + return errorResult("name is required for create", "") + } + if !ValidName(in.Name) { + return errorResult(fmt.Sprintf("invalid name %q (must be kebab-case, 1-64 chars)", in.Name), in.Name) + } + body := in.Body + if strings.TrimSpace(body) == "" { + return errorResult("body is required for create", in.Name) + } + if len(body) > maxBodyBytes { + return errorResult(fmt.Sprintf("body exceeds max length (%d > %d)", len(body), maxBodyBytes), in.Name) + } + + created, err := t.Store.Create(ctx, Skill{ + Name: in.Name, + Body: body, + Origin: originFromContext(ctx), + }) + if err != nil { + return errorResult(err.Error(), in.Name) + } + return successResult(fmt.Sprintf("created skill: %s", truncatePreview(created.Name)), created.Name) +} + +func (t *SkillTool) executePatch(ctx context.Context, in skillInput) tool.ToolResult { + if in.Name == "" { + return errorResult("name is required for patch", "") + } + if in.OldString == "" { + return errorResult("old_string is required for patch", in.Name) + } + updated, err := t.Store.Patch(ctx, in.Name, in.OldString, in.NewString) + if err != nil { + // All three Patch sentinels are returned via err.Error(); we + // surface them verbatim so the model can recognize the category + // from the message. + return errorResult(err.Error(), in.Name) + } + return successResult(fmt.Sprintf("patched skill: %s", truncatePreview(updated.Name)), updated.Name) +} + +func (t *SkillTool) executeReplace(ctx context.Context, in skillInput) tool.ToolResult { + if in.Name == "" { + return errorResult("name is required for replace", "") + } + body := in.Body + if strings.TrimSpace(body) == "" { + return errorResult("body is required for replace", in.Name) + } + if len(body) > maxBodyBytes { + return errorResult(fmt.Sprintf("body exceeds max length (%d > %d)", len(body), maxBodyBytes), in.Name) + } + updated, err := t.Store.Replace(ctx, in.Name, body) + if err != nil { + return errorResult(err.Error(), in.Name) + } + return successResult(fmt.Sprintf("replaced skill: %s", truncatePreview(updated.Name)), updated.Name) +} + +// executeList returns the skills as a raw JSON array (not the +// action-result envelope) so the model receives a list-shaped value it +// can iterate over directly. Body is stripped — the model can call +// action=get to fetch a specific body. +func (t *SkillTool) executeList(ctx context.Context, _ skillInput) tool.ToolResult { + all, err := t.Store.List(ctx) + if err != nil { + return errorResult(err.Error(), "") + } + if all == nil { + all = []Skill{} + } + stripped := make([]Skill, len(all)) + for i, sk := range all { + sk.Body = "" // index only + stripped[i] = sk + } + b, err := json.Marshal(stripped) + if err != nil { + return errorResult(err.Error(), "") + } + return tool.ToolResult{Output: string(b)} +} + +// executeGet returns the raw Skill as JSON on success; the action-result +// envelope on failure. +func (t *SkillTool) executeGet(ctx context.Context, in skillInput) tool.ToolResult { + if in.Name == "" { + return errorResult("name is required for get", "") + } + sk, ok, err := t.Store.Get(ctx, in.Name) + if err != nil { + return errorResult(err.Error(), in.Name) + } + if !ok { + return errorResult("not found", in.Name) + } + b, err := json.Marshal(sk) + if err != nil { + return errorResult(err.Error(), in.Name) + } + return tool.ToolResult{Output: string(b)} +} + +func (t *SkillTool) executeRemove(ctx context.Context, in skillInput) tool.ToolResult { + if in.Name == "" { + return errorResult("name is required for remove", "") + } + if err := t.Store.Remove(ctx, in.Name); err != nil { + return errorResult(err.Error(), in.Name) + } + return successResult(fmt.Sprintf("removed skill: %s", in.Name), in.Name) +} + +// Compile-time assertion that *SkillTool satisfies tool.Tool. +var _ tool.Tool = (*SkillTool)(nil) diff --git a/tool/skills/tool_test.go b/tool/skills/tool_test.go new file mode 100644 index 0000000..65d5764 --- /dev/null +++ b/tool/skills/tool_test.go @@ -0,0 +1,524 @@ +package skills + +import ( + "context" + "encoding/json" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSkillTool_NameDescriptionSchema(t *testing.T) { + tool := &SkillTool{} + + assert.Equal(t, "skill_manage", tool.Name()) + assert.NotEmpty(t, tool.Description()) + + var schema struct { + Type string `json:"type"` + Properties map[string]interface{} `json:"properties"` + Required []string `json:"required"` + } + require.NoError(t, json.Unmarshal(tool.Parameters(), &schema)) + assert.Equal(t, "object", schema.Type) + for _, key := range []string{"action", "name", "body", "old_string", "new_string"} { + assert.Contains(t, schema.Properties, key, "missing %q in schema", key) + } + assert.Equal(t, []string{"action"}, schema.Required) +} + +func TestSkillTool_IsConcurrencySafeFalse(t *testing.T) { + tool := &SkillTool{} + assert.False(t, tool.IsConcurrencySafe(nil)) +} + +// fakeStore is an in-memory SkillStore for tool tests. +type fakeStore struct { + skills map[string]Skill +} + +func newFakeStore() *fakeStore { return &fakeStore{skills: map[string]Skill{}} } + +func (f *fakeStore) Create(_ context.Context, s Skill) (Skill, error) { + if !ValidName(s.Name) { + return Skill{}, ErrInvalidName + } + if _, ok := f.skills[s.Name]; ok { + return Skill{}, ErrAlreadyExists + } + now := time.Now() + s.CreatedAt = now + s.UpdatedAt = now + f.skills[s.Name] = s + return s, nil +} + +func (f *fakeStore) Patch(_ context.Context, name, oldC, newC string) (Skill, error) { + if oldC == newC { + return Skill{}, ErrPatchIdentical + } + s, ok := f.skills[name] + if !ok { + return Skill{}, ErrNotFound + } + if !contains(s.Body, oldC) { + return Skill{}, ErrPatchNoMatch + } + s.Body = replaceFirst(s.Body, oldC, newC) + s.UpdatedAt = time.Now() + f.skills[name] = s + return s, nil +} + +func contains(haystack, needle string) bool { + for i := 0; i+len(needle) <= len(haystack); i++ { + if haystack[i:i+len(needle)] == needle { + return true + } + } + return false +} + +func replaceFirst(haystack, old, new string) string { + for i := 0; i+len(old) <= len(haystack); i++ { + if haystack[i:i+len(old)] == old { + return haystack[:i] + new + haystack[i+len(old):] + } + } + return haystack +} + +func (f *fakeStore) Replace(_ context.Context, name, body string) (Skill, error) { + s, ok := f.skills[name] + if !ok { + return Skill{}, ErrNotFound + } + s.Body = body + s.UpdatedAt = time.Now() + f.skills[name] = s + return s, nil +} + +func (f *fakeStore) Remove(_ context.Context, name string) error { + delete(f.skills, name) + return nil +} + +func (f *fakeStore) List(_ context.Context) ([]Skill, error) { + out := make([]Skill, 0, len(f.skills)) + for _, s := range f.skills { + out = append(out, s) + } + return out, nil +} + +func (f *fakeStore) Get(_ context.Context, name string) (Skill, bool, error) { + s, ok := f.skills[name] + return s, ok, nil +} + +func TestSkillTool_CreateHappyPath(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{ + Action: "create", + Name: "my-skill", + Body: "---\ndescription: A skill.\n---\n\nbody", + }) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.True(t, ar.Success) + assert.Equal(t, "skill", ar.Target) + assert.Equal(t, "my-skill", ar.Name) + assert.Contains(t, ar.Message, "my-skill") + + require.Len(t, store.skills, 1) +} + +func TestSkillTool_CreateRejectsMissingName(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "create", Body: "x"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "name") +} + +func TestSkillTool_CreateRejectsInvalidName(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "create", Name: "Bad Name", Body: "x"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "invalid") +} + +func TestSkillTool_CreateRejectsEmptyBody(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "create", Name: "x", Body: ""}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "body") +} + +func TestSkillTool_CreateRejectsOversizedBody(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + big := make([]byte, 32001) + for i := range big { + big[i] = 'x' + } + in, _ := json.Marshal(skillInput{Action: "create", Name: "big", Body: string(big)}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "32000") +} + +func TestSkillTool_CreateOriginFromContext(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + ctx := context.WithValue(context.Background(), OriginKey, "review") + in, _ := json.Marshal(skillInput{Action: "create", Name: "from-review", Body: "x"}) + _, err := tool.Execute(ctx, in) + require.NoError(t, err) + + require.Len(t, store.skills, 1) + assert.Equal(t, "review", store.skills["from-review"].Origin) +} + +func TestSkillTool_CreateOriginDefault(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "create", Name: "from-fg", Body: "x"}) + _, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + require.Len(t, store.skills, 1) + assert.Equal(t, "agent", store.skills["from-fg"].Origin) +} + +func TestSkillTool_CreateAlreadyExistsRetainsOriginal(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{Action: "create", Name: "dup", Body: "first"}) + _, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + in, _ = json.Marshal(skillInput{Action: "create", Name: "dup", Body: "second"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "exists") + + // Original still intact. + assert.Equal(t, "first", store.skills["dup"].Body) +} + +func TestSkillTool_PatchHappyPath(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "p", Body: "find me here"}) + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{ + Action: "patch", Name: "p", OldString: "find me", NewString: "FOUND", + }) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.True(t, ar.Success) + assert.Equal(t, "p", ar.Name) + assert.Contains(t, ar.Message, "patched") + assert.Equal(t, "FOUND here", store.skills["p"].Body) +} + +func TestSkillTool_PatchRejectsMissingName(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "patch", OldString: "a", NewString: "b"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "name") +} + +func TestSkillTool_PatchRejectsMissingOldString(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "p", Body: "x"}) + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "patch", Name: "p", NewString: "b"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "old_string") +} + +func TestSkillTool_PatchSurfacesNoMatchError(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "p", Body: "hello world"}) + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{ + Action: "patch", Name: "p", OldString: "missing", NewString: "x", + }) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "not found") +} + +func TestSkillTool_PatchSurfacesIdenticalError(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "p", Body: "x"}) + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{ + Action: "patch", Name: "p", OldString: "same", NewString: "same", + }) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "identical") +} + +func TestSkillTool_PatchUnknownNameReturnsErrNotFound(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{ + Action: "patch", Name: "ghost", OldString: "a", NewString: "b", + }) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "not found") +} + +func TestSkillTool_ReplaceHappyPath(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "r", Body: "v1"}) + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{Action: "replace", Name: "r", Body: "v2"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.True(t, ar.Success) + assert.Contains(t, ar.Message, "replaced") + assert.Equal(t, "v2", store.skills["r"].Body) +} + +func TestSkillTool_ReplaceRejectsMissingName(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "replace", Body: "v2"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "name") +} + +func TestSkillTool_ReplaceRejectsEmptyBody(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "r", Body: "v1"}) + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "replace", Name: "r", Body: ""}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) +} + +func TestSkillTool_ReplaceRejectsOversizedBody(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "r", Body: "v1"}) + tool := &SkillTool{Store: store} + big := make([]byte, 32001) + for i := range big { + big[i] = 'x' + } + in, _ := json.Marshal(skillInput{Action: "replace", Name: "r", Body: string(big)}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "32000") +} + +func TestSkillTool_ReplaceUnknownReturnsErrNotFound(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "replace", Name: "ghost", Body: "x"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "not found") +} + +func TestSkillTool_RemoveHappyPath(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "doomed", Body: "x"}) + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{Action: "remove", Name: "doomed"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.True(t, ar.Success) + assert.Equal(t, "doomed", ar.Name) + assert.Contains(t, ar.Message, "removed") + assert.Empty(t, store.skills) +} + +func TestSkillTool_RemoveRejectsMissingName(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "remove"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "name") +} + +func TestSkillTool_RemoveUnknownIsSuccessful(t *testing.T) { + // Idempotent. + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "remove", Name: "ghost"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.True(t, ar.Success) +} + +func TestSkillTool_ListReturnsAllAsJSON(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "alpha", Body: "alpha body", Description: "first"}) + _, _ = store.Create(context.Background(), Skill{Name: "bravo", Body: "bravo body", Description: "second"}) + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{Action: "list"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + assert.Empty(t, res.Error) + + // List output is the raw skills array (without bodies — this is an + // index, not a content dump). + var entries []Skill + require.NoError(t, json.Unmarshal([]byte(res.Output), &entries)) + require.Len(t, entries, 2) + for _, e := range entries { + assert.Empty(t, e.Body, "list output must omit Body to keep the index small") + } +} + +func TestSkillTool_ListEmpty(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "list"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + assert.Equal(t, "[]", res.Output) +} + +func TestSkillTool_GetHappyPath(t *testing.T) { + store := newFakeStore() + _, _ = store.Create(context.Background(), Skill{Name: "g", Body: "the body"}) + tool := &SkillTool{Store: store} + + in, _ := json.Marshal(skillInput{Action: "get", Name: "g"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var sk Skill + require.NoError(t, json.Unmarshal([]byte(res.Output), &sk)) + assert.Equal(t, "g", sk.Name) + assert.Equal(t, "the body", sk.Body) +} + +func TestSkillTool_GetUnknownReturnsError(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "get", Name: "ghost"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "not found") +} + +func TestSkillTool_GetRejectsMissingName(t *testing.T) { + store := newFakeStore() + tool := &SkillTool{Store: store} + in, _ := json.Marshal(skillInput{Action: "get"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + + var ar actionResult + require.NoError(t, json.Unmarshal([]byte(res.Output), &ar)) + assert.False(t, ar.Success) + assert.Contains(t, ar.Error, "name") +}