Files
archie/internal/skill/tooldef_test.go
T
sam 170fe0250b fix: close the duplicate-work loop and the dead paths around it
Applies the four-reviewer pass over the forge/dashboard-actions work.
The through-line is that several capabilities were wired but inert, and
one of them could cost real work.

DUPLICATE WORK. "Closes #N" was dropped from PR bodies in favour of
LinkBranch, which is sidebar linkage on Gitea and a no-op stub on
GitHub -- neither closes anything, and nothing else did either. So a
merged task left its issue open, labelled and assigned; the next poll
re-enqueued it, and once the dashboard's Clear removed the task row that
became a second implementation and a second PR for work already merged.
reconcilePRs now closes the issue on merge, and a rejected task closes
its issue too. A PR closed without merging deliberately leaves the issue
open: that usually means "try again".

MAX RETRIES. Configured, defaulted and reported by /api/config, and
enforced nowhere -- markDead was unreachable and EffectiveMaxRetries had
no callers, so the dashboard's retry incremented retry_count forever.
The retry action now retires a task to dead at the cap.

DEAD ON ARRIVAL. Tasks() never selected plan or source, so the
"Decision required" panel could not render and chat tasks -- whose issue
number is synthetic -- linked to forge issues that do not exist.

COSMETIC FAILURE, FATAL RESULT. OpenPR failed the whole stage when
LinkBranch errored, so the pull request was never opened and the task
parked; a retry re-links the same branch, which Gitea answers with a
conflict, so every retry parked again. It is best-effort now.

INVISIBLE ACTIONS. Approve/retry/reject wrote store rows and nothing
else, so the timeline and activity stream never showed that a human had
intervened. They emit events now.

Also: store write failures answer 500 rather than 409; the dashboard
surfaces the server's actual reason instead of "409 Conflict"; action
buttons stop propagation (acting on a task no longer also expands it),
disable in flight, keep per-task errors instead of one shared string,
and confirm before rejecting.

Removed: the maybeRetryParked shim and markDead with their now-vacuous
tests, WaitingTasks (no consumers; its tests were rewritten to assert
the same routing through TaskByID), write-only Daemon.Runtime, and
dir/dirs response bloat from /api/skills. skillSource checked workDir
before sharedDir, mislabelling every shared skill when the two are equal.
DefaultRoots now dedupes roots differing only by a trailing separator.

The air build rule claiming to keep ui/dist current never ran -- it
required one file to satisfy include_dir and include_file together, and
ui/ is excluded from the walk. Deleted with the comment that promised it.
task test:ui joins task check and CI; it ran nowhere before, so the
dashboard suite could rot silently.

Docs corrected where they described behaviour that no longer exists:
parks no longer comment on the issue, state labels no longer drive
retries, and forgerpc's doc claimed four proxied methods while listing
three of five.
2026-08-05 03:54:25 +10:00

182 lines
5.2 KiB
Go

package skill
import (
"context"
"os"
"path/filepath"
"testing"
)
// ── progressive disclosure: catalog → tool bridge ────────────────────
//
// A chat turn's tool set should offer ONE tool (not one per skill) that
// lets the model activate any cataloged skill by name -- mirroring the
// two-tier disclosure Catalog/LoadBody already implement: the tool's
// schema enumerates every catalog entry's name+description (Tier 1,
// cheap), and calling it returns the full SKILL.md body (Tier 2, loaded
// only on activation).
func writeSkill(t *testing.T, dir, name, description, body string) {
t.Helper()
skillDir := filepath.Join(dir, ".agents", "skills", name)
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
content := "---\nname: " + name + "\ndescription: " + description + "\n---\n" + body + "\n"
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestActivateToolNameAndSchema(t *testing.T) {
dir := t.TempDir()
writeSkill(t, dir, "alpha", "does alpha things", "alpha body")
writeSkill(t, dir, "beta", "does beta things", "beta body")
catalog, err := Catalog(dir)
if err != nil {
t.Fatal(err)
}
entry := ActivateTool(dir, catalog)
if entry.Name != "skill_activate" {
t.Errorf("Name = %q, want skill_activate", entry.Name)
}
if entry.Toolset != "skill" {
t.Errorf("Toolset = %q, want skill", entry.Toolset)
}
if entry.Handler == nil {
t.Fatal("Handler is nil")
}
enumRaw, ok := enumFromSchema(entry.Schema)
if !ok {
t.Fatal("schema does not expose a name enum")
}
if len(enumRaw) != 2 {
t.Fatalf("schema enum has %d entries, want 2", len(enumRaw))
}
}
func TestActivateToolHandlerReturnsFullBody(t *testing.T) {
dir := t.TempDir()
writeSkill(t, dir, "alpha", "does alpha things", "This is the full alpha body.")
catalog, err := Catalog(dir)
if err != nil {
t.Fatal(err)
}
entry := ActivateTool(dir, catalog)
out, err := entry.Handler(context.Background(), map[string]any{"name": "alpha"})
if err != nil {
t.Fatal(err)
}
body, ok := out.(string)
if !ok {
t.Fatalf("output type = %T, want string", out)
}
if body != "This is the full alpha body." {
t.Errorf("body = %q", body)
}
}
func TestActivateToolHandlerLoadsFromCatalogEntryRoot(t *testing.T) {
global := t.TempDir()
writeSkill(t, global, "global-only", "global skill", "global body")
catalog, err := CatalogRoots(global)
if err != nil {
t.Fatal(err)
}
entry := ActivateTool(t.TempDir(), catalog)
out, err := entry.Handler(context.Background(), map[string]any{"name": "global-only"})
if err != nil {
t.Fatal(err)
}
if out != "global body" {
t.Errorf("body = %v, want global body", out)
}
}
func TestActivateToolHandlerRejectsUnknownSkill(t *testing.T) {
dir := t.TempDir()
writeSkill(t, dir, "alpha", "does alpha things", "alpha body")
catalog, _ := Catalog(dir)
entry := ActivateTool(dir, catalog)
_, err := entry.Handler(context.Background(), map[string]any{"name": "nonexistent"})
if err == nil {
t.Fatal("expected error for unknown skill name")
}
}
func TestActivateToolHandlerRejectsMissingName(t *testing.T) {
dir := t.TempDir()
writeSkill(t, dir, "alpha", "does alpha things", "alpha body")
catalog, _ := Catalog(dir)
entry := ActivateTool(dir, catalog)
_, err := entry.Handler(context.Background(), map[string]any{})
if err == nil {
t.Fatal("expected error for missing name argument")
}
}
func TestActivateToolWithEmptyCatalogReturnsNilEntry(t *testing.T) {
// No skills configured -- don't clutter the tool set with a dead tool.
dir := t.TempDir()
entry := ActivateTool(dir, nil)
if entry != nil {
t.Fatalf("expected nil entry for empty catalog, got %+v", entry)
}
}
func TestActivateToolHandlerUsesSkillDirNotDisplayName(t *testing.T) {
// CatalogEntry.Name comes from frontmatter and CatalogEntry.Dir is the
// directory name -- they can differ. LoadBody keys off the directory
// name, so the tool's activation argument must match what the catalog
// exposes as selectable (Name), and the handler must resolve it back
// to the correct Dir internally.
dir := t.TempDir()
skillDir := filepath.Join(dir, ".agents", "skills", "dir-name-differs")
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
content := "---\nname: Display Name\ndescription: d\n---\nfull body here\n"
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
catalog, err := Catalog(dir)
if err != nil {
t.Fatal(err)
}
entry := ActivateTool(dir, catalog)
out, err := entry.Handler(context.Background(), map[string]any{"name": "Display Name"})
if err != nil {
t.Fatal(err)
}
if out != "full body here" {
t.Errorf("body = %v", out)
}
}
// enumFromSchema digs the "name" property's enum out of a JSON-Schema-shaped
// map, tolerating the concrete map/slice types produced by our own
// ActivateTool (no external JSON round-trip in this test).
func enumFromSchema(schema map[string]any) ([]any, bool) {
props, ok := schema["properties"].(map[string]any)
if !ok {
return nil, false
}
nameProp, ok := props["name"].(map[string]any)
if !ok {
return nil, false
}
enum, ok := nameProp["enum"].([]any)
return enum, ok
}