skills: add end-to-end integration tests + polish
This commit is contained in:
1 parent
7e394276da
commit
53a7986fa0
6 files changed
+181
-41
No files matched your search
@@ -6,9 +6,11 @@
|
||||
// The MemoryTool in tool.go wraps any MemoryStore implementation as a
|
||||
// tool.Tool the agent can call. The default disk-backed implementation
|
||||
// lives in tool/memory/jsonl/ — that package's *Store value satisfies
|
||||
// both MemoryStore (writes) and runtime.MemoryProvider (reads), so one
|
||||
// instance can serve as the system-prompt index source, the foreground
|
||||
// memory tool's backend, AND the review-pass tool's backend.
|
||||
// MemoryStore directly, and satisfies runtime.MemoryProvider via
|
||||
// Store.AsMemoryProvider() which returns a small wrapper that adapts
|
||||
// the read signature. One underlying *Store can therefore serve as the
|
||||
// system-prompt index source, the foreground memory tool's backend,
|
||||
// AND the review-pass tool's backend.
|
||||
package memory
|
||||
|
||||
import (
|
||||
|
||||
+17
-26
@@ -147,13 +147,9 @@ func stripFrontmatter(body string) string {
|
||||
return body
|
||||
}
|
||||
rest := body[4:]
|
||||
if i := strings.Index(rest, "\n---\n"); i >= 0 {
|
||||
s := rest[i+5:]
|
||||
if _, after, ok := strings.Cut(rest, "\n---\n"); ok {
|
||||
// Eat one optional leading blank line after the closing fence.
|
||||
if strings.HasPrefix(s, "\n") {
|
||||
s = s[1:]
|
||||
}
|
||||
return s
|
||||
return strings.TrimPrefix(after, "\n")
|
||||
}
|
||||
if i := strings.Index(rest, "\n---"); i >= 0 && i == len(rest)-4 {
|
||||
return ""
|
||||
@@ -223,32 +219,27 @@ func parseFrontmatter(body string) (frontmatter, string) {
|
||||
return frontmatter{}, body
|
||||
}
|
||||
rest := body[4:]
|
||||
end := strings.Index(rest, "\n---\n")
|
||||
var afterFM string
|
||||
var fmText string
|
||||
switch {
|
||||
case end >= 0:
|
||||
fmText = rest[:end]
|
||||
afterFM = rest[end+5:]
|
||||
case strings.HasSuffix(rest, "\n---"):
|
||||
var fmText, afterFM string
|
||||
if before, after, ok := strings.Cut(rest, "\n---\n"); ok {
|
||||
fmText = before
|
||||
afterFM = after
|
||||
} else if strings.HasSuffix(rest, "\n---") {
|
||||
fmText = rest[:len(rest)-4]
|
||||
afterFM = ""
|
||||
default:
|
||||
} else {
|
||||
return frontmatter{}, body
|
||||
}
|
||||
if strings.HasPrefix(afterFM, "\n") {
|
||||
afterFM = afterFM[1:]
|
||||
}
|
||||
afterFM = strings.TrimPrefix(afterFM, "\n")
|
||||
|
||||
var fm frontmatter
|
||||
for _, line := range strings.Split(fmText, "\n") {
|
||||
for line := range strings.SplitSeq(fmText, "\n") {
|
||||
line = strings.TrimRight(line, "\r")
|
||||
idx := strings.Index(line, ":")
|
||||
if idx < 0 {
|
||||
key, val, ok := strings.Cut(line, ":")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(line[:idx])
|
||||
val := strings.TrimSpace(line[idx+1:])
|
||||
key = strings.TrimSpace(key)
|
||||
val = strings.TrimSpace(val)
|
||||
switch key {
|
||||
case "description":
|
||||
fm.Description = val
|
||||
@@ -302,9 +293,6 @@ func (s *Store) Patch(ctx context.Context, name, oldContent, newContent string)
|
||||
if !skills.ValidName(name) {
|
||||
return skills.Skill{}, fmt.Errorf("%w: %q", skills.ErrInvalidName, name)
|
||||
}
|
||||
if oldContent == newContent {
|
||||
return skills.Skill{}, skills.ErrPatchIdentical
|
||||
}
|
||||
|
||||
lock := s.lockFor(name)
|
||||
lock.Lock()
|
||||
@@ -317,6 +305,9 @@ func (s *Store) Patch(ctx context.Context, name, oldContent, newContent string)
|
||||
if !ok {
|
||||
return skills.Skill{}, fmt.Errorf("%w: %q", skills.ErrNotFound, name)
|
||||
}
|
||||
if oldContent == newContent {
|
||||
return skills.Skill{}, skills.ErrPatchIdentical
|
||||
}
|
||||
|
||||
count := strings.Count(existing.Body, oldContent)
|
||||
switch count {
|
||||
|
||||
@@ -350,6 +350,16 @@ func TestStore_PatchUnknownReturnsErrNotFound(t *testing.T) {
|
||||
require.ErrorIs(t, err, skills.ErrNotFound)
|
||||
}
|
||||
|
||||
func TestStore_PatchUnknownTakesPrecedenceOverIdentical(t *testing.T) {
|
||||
// Patch on a missing skill with old==new must return ErrNotFound,
|
||||
// not ErrPatchIdentical — so the model gets the actionable error.
|
||||
dir := t.TempDir()
|
||||
s := NewStore(dir)
|
||||
_, err := s.Patch(context.Background(), "ghost", "x", "x")
|
||||
require.ErrorIs(t, err, skills.ErrNotFound)
|
||||
require.NotErrorIs(t, err, skills.ErrPatchIdentical)
|
||||
}
|
||||
|
||||
func TestStore_PatchRejectsInvalidName(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s := NewStore(dir)
|
||||
@@ -390,7 +400,7 @@ func TestStore_ConcurrentPatchesDoNotInterleave(t *testing.T) {
|
||||
// Body has 20 distinct sentinels: M00, M01, ..., M19.
|
||||
var b strings.Builder
|
||||
b.WriteString("---\ndescription: race test.\n---\n\n")
|
||||
for i := 0; i < 20; i++ {
|
||||
for i := range 20 {
|
||||
fmt.Fprintf(&b, "M%02d\n", i)
|
||||
}
|
||||
_, err := s.Create(context.Background(), skills.Skill{Name: "race", Body: b.String()})
|
||||
@@ -399,12 +409,12 @@ func TestStore_ConcurrentPatchesDoNotInterleave(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(20)
|
||||
errs := make(chan error, 20)
|
||||
for i := 0; i < 20; i++ {
|
||||
for i := range 20 {
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
old := fmt.Sprintf("M%02d", i)
|
||||
new := fmt.Sprintf("X%02d", i)
|
||||
if _, err := s.Patch(context.Background(), "race", old, new); err != nil {
|
||||
replacement := fmt.Sprintf("X%02d", i)
|
||||
if _, err := s.Patch(context.Background(), "race", old, replacement); err != nil {
|
||||
errs <- err
|
||||
}
|
||||
}(i)
|
||||
@@ -418,7 +428,7 @@ func TestStore_ConcurrentPatchesDoNotInterleave(t *testing.T) {
|
||||
got, ok, err := s.Get(context.Background(), "race")
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
for i := 0; i < 20; i++ {
|
||||
for i := range 20 {
|
||||
assert.NotContains(t, got.Body, fmt.Sprintf("M%02d", i),
|
||||
"sentinel M%02d should have been patched", i)
|
||||
assert.Contains(t, got.Body, fmt.Sprintf("X%02d", i),
|
||||
@@ -436,7 +446,7 @@ func TestStore_ConcurrentCreatesDoNotDoubleWrite(t *testing.T) {
|
||||
wg.Add(10)
|
||||
successes := make(chan struct{}, 10)
|
||||
conflicts := make(chan struct{}, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
for range 10 {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_, err := s.Create(context.Background(), skills.Skill{
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
package skills_test
|
||||
|
||||
// Separate _test package to verify the public surface wires together
|
||||
// without internal fakes.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/sausheong/harness/tool/skills"
|
||||
"github.com/sausheong/harness/tool/skills/disk"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEndToEnd_CreatePatchReplaceRemoveRoundTrip(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
store := disk.NewStore(filepath.Join(dir, "skills"))
|
||||
tool := &skills.SkillTool{Store: store}
|
||||
|
||||
// Create two skills.
|
||||
for _, name := range []string{"writing-clearly", "git-bisect"} {
|
||||
body := "---\ndescription: " + name + " skill.\n---\n\nThe " + name + " body.\n"
|
||||
in, _ := json.Marshal(map[string]any{
|
||||
"action": "create",
|
||||
"name": name,
|
||||
"body": body,
|
||||
})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, res.Error)
|
||||
}
|
||||
|
||||
// List both back (index-only — bodies stripped).
|
||||
in, _ := json.Marshal(map[string]any{"action": "list"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
var entries []skills.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 strip body")
|
||||
assert.NotEmpty(t, e.Description, "list output must include description")
|
||||
}
|
||||
|
||||
// Patch one.
|
||||
in, _ = json.Marshal(map[string]any{
|
||||
"action": "patch",
|
||||
"name": "writing-clearly",
|
||||
"old_string": "writing-clearly body.",
|
||||
"new_string": "writing-clearly body, revised.",
|
||||
})
|
||||
res, err = tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
var ar struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &ar))
|
||||
assert.True(t, ar.Success)
|
||||
|
||||
// Verify via the provider adapter (the system-prompt path).
|
||||
provider := store.AsSkillProvider()
|
||||
body, ok := provider.Get("writing-clearly")
|
||||
assert.True(t, ok)
|
||||
assert.Contains(t, body, "writing-clearly body, revised.")
|
||||
|
||||
// FormatIndex includes both skills with descriptions, no body content.
|
||||
idx := provider.FormatIndex()
|
||||
assert.Contains(t, idx, "writing-clearly")
|
||||
assert.Contains(t, idx, "git-bisect")
|
||||
assert.Contains(t, idx, "writing-clearly skill.")
|
||||
assert.NotContains(t, idx, "writing-clearly body, revised.",
|
||||
"FormatIndex must not leak body content")
|
||||
|
||||
// Replace the other.
|
||||
newBody := "---\ndescription: New body.\n---\n\nFresh content.\n"
|
||||
in, _ = json.Marshal(map[string]any{
|
||||
"action": "replace",
|
||||
"name": "git-bisect",
|
||||
"body": newBody,
|
||||
})
|
||||
res, err = tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
var rr struct {
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &rr))
|
||||
assert.True(t, rr.Success)
|
||||
|
||||
got, ok := provider.Get("git-bisect")
|
||||
assert.True(t, ok)
|
||||
assert.Contains(t, got, "Fresh content.")
|
||||
|
||||
// Remove one.
|
||||
in, _ = json.Marshal(map[string]any{"action": "remove", "name": "writing-clearly"})
|
||||
_, err = tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
in, _ = json.Marshal(map[string]any{"action": "list"})
|
||||
res, err = tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &entries))
|
||||
require.Len(t, entries, 1)
|
||||
assert.Equal(t, "git-bisect", entries[0].Name)
|
||||
}
|
||||
|
||||
func TestEndToEnd_GetReturnsFullBody(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
store := disk.NewStore(filepath.Join(dir, "skills"))
|
||||
tool := &skills.SkillTool{Store: store}
|
||||
|
||||
body := "---\ndescription: A test.\n---\n\nFull body content here.\n"
|
||||
in, _ := json.Marshal(map[string]any{
|
||||
"action": "create",
|
||||
"name": "fetch-me",
|
||||
"body": body,
|
||||
})
|
||||
_, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
in, _ = json.Marshal(map[string]any{"action": "get", "name": "fetch-me"})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
|
||||
var sk skills.Skill
|
||||
require.NoError(t, json.Unmarshal([]byte(res.Output), &sk))
|
||||
assert.Equal(t, "fetch-me", sk.Name)
|
||||
assert.Equal(t, "A test.", sk.Description)
|
||||
assert.True(t, strings.Contains(sk.Body, "Full body content here."))
|
||||
}
|
||||
@@ -6,9 +6,12 @@
|
||||
// The SkillTool in tool.go wraps any SkillStore implementation as a
|
||||
// tool.Tool the agent can call. The default disk-backed implementation
|
||||
// lives in tool/skills/disk/ — that package's *Store value satisfies
|
||||
// both SkillStore (writes) and runtime.SkillProvider (reads), so one
|
||||
// instance can serve as the system-prompt index source, the foreground
|
||||
// skill tool's backend, AND the review-pass tool's backend.
|
||||
// SkillStore directly, and satisfies runtime.SkillProvider via
|
||||
// Store.AsSkillProvider() which returns a small wrapper that adapts
|
||||
// the Get signature ((Skill, bool, error) → (string, bool)). One
|
||||
// underlying *Store can therefore serve as the system-prompt index
|
||||
// source, the foreground skill tool's backend, AND the review-pass
|
||||
// tool's backend.
|
||||
//
|
||||
// Skill names are kebab-case and validated by ValidName: lowercase
|
||||
// letters and digits, hyphen-separated, 1–64 chars, no leading hyphen,
|
||||
@@ -25,8 +28,9 @@ import (
|
||||
|
||||
// Skill is one durable procedural-knowledge record. Name is the stable
|
||||
// identifier (and primary key); Body is the full SKILL.md content
|
||||
// including any frontmatter. Description is auto-derived from
|
||||
// frontmatter on Save (read-only by callers); the store sets it.
|
||||
// including any frontmatter. Description is taken from the
|
||||
// caller-supplied field when set; otherwise the store derives it from
|
||||
// the body's frontmatter `description:` line.
|
||||
type Skill struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestSkillTool_NameDescriptionSchema(t *testing.T) {
|
||||
|
||||
var schema struct {
|
||||
Type string `json:"type"`
|
||||
Properties map[string]interface{} `json:"properties"`
|
||||
Properties map[string]any `json:"properties"`
|
||||
Required []string `json:"required"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(tool.Parameters(), &schema))
|
||||
|
||||
Reference in new issue
Block a user