Skip to content

mission control for agent skills

Add Prune action to clear orphaned lock entries

Alvin Unreal0dcf12dba338
diff --git a/internal/actions/actions.go b/internal/actions/actions.go
index 45d3e49..9ac6a66 100644
--- a/internal/actions/actions.go
+++ b/internal/actions/actions.go
@@ -204,9 +204,44 @@ func ForSkillWithResolver(sk *model.Skill, resolve SkillsResolver) []CommandPrev
} else {
previews = append(previews, unavailablePreview("Remove selected skill", reasonRemove))
}
+ if hasOrphanedLock(sk) {
+ previews = append(previews, pruneLockPreview(sk))
+ }
return previews
}
+// hasOrphanedLock reports whether the skill is a lock entry whose files are
+// gone from disk (the scan flags this as lock_without_files).
+func hasOrphanedLock(sk *model.Skill) bool {
+ for _, issue := range sk.HealthIssues {
+ if issue.Type == "lock_without_files" {
+ return true
+ }
+ }
+ return false
+}
+
+// pruneLockPreview builds the internal action that removes an orphaned entry
+// from the project or global lock file. It is handled inside the TUI, which
+// owns the lock paths and triggers a rescan afterwards.
+func pruneLockPreview(sk *model.Skill) CommandPreview {
+ internal := "prune_project_lock"
+ if sk.Scope == model.ScopeGlobal {
+ internal = "prune_global_lock"
+ }
+ return CommandPreview{
+ ID: "prune_lock",
+ Title: "Prune stale lock entry",
+ Description: "Remove this orphaned entry from the lock file (its skill files are already gone).",
+ Command: "prune lock entry " + compat.SanitizeMetadata(sk.Name),
+ Exec: ExecSpec{Internal: internal},
+ Mutates: true,
+ RequiresConfirm: true,
+ ConfirmValue: sk.Name,
+ Available: true,
+ }
+}
+
func openEditorAction(sk *model.Skill) (CommandPreview, bool, string) {
editor := os.Getenv("EDITOR")
if editor == "" {
diff --git a/internal/actions/actions_test.go b/internal/actions/actions_test.go
index bc9affe..452bb6f 100644
--- a/internal/actions/actions_test.go
+++ b/internal/actions/actions_test.go
@@ -14,6 +14,35 @@ func init() {
}
}
+func TestForSkillPruneLockAction(t *testing.T) {
+ orphaned := &model.Skill{
+ Name: "ghost",
+ Scope: model.ScopeProject,
+ LocalLock: &model.LocalLockEntry{Source: "owner/repo"},
+ HealthIssues: []model.HealthIssue{{Type: "lock_without_files", Severity: "warning"}},
+ }
+ previews := ForSkill(orphaned)
+ var prune *CommandPreview
+ for i := range previews {
+ if previews[i].ID == "prune_lock" {
+ prune = &previews[i]
+ }
+ }
+ if prune == nil {
+ t.Fatal("expected a prune_lock action for an orphaned-lock skill")
+ }
+ if !prune.Available || prune.ConfirmValue != "ghost" || prune.Exec.Internal != "prune_project_lock" {
+ t.Fatalf("unexpected prune preview: %+v", *prune)
+ }
+
+ healthy := &model.Skill{Name: "ok", Scope: model.ScopeProject, CanonicalPath: "/tmp/ok", LocalLock: &model.LocalLockEntry{Source: "owner/repo"}}
+ for _, a := range ForSkill(healthy) {
+ if a.ID == "prune_lock" {
+ t.Fatal("did not expect a prune_lock action for a healthy skill")
+ }
+ }
+}
+
func TestForSkillBuildsStructuredSanitizedCommandPreviews(t *testing.T) {
t.Setenv("EDITOR", "")
sk := &model.Skill{
diff --git a/internal/locks/locks.go b/internal/locks/locks.go
index 1558a6f..c2932aa 100644
--- a/internal/locks/locks.go
+++ b/internal/locks/locks.go
@@ -2,6 +2,7 @@ package locks
import (
"encoding/json"
+ "fmt"
"os"
"path/filepath"
@@ -49,3 +50,32 @@ func ReadGlobal(path string) (model.GlobalLockFile, error) {
}
return lock, nil
}
+
+// RemoveEntry deletes a single skill entry by key from a lock file, preserving
+// all other entries and every top-level field, then rewrites it as indented
+// JSON. It errors if the file, its skills map, or the key is missing. Decoding
+// into a generic map (rather than the typed structs) avoids dropping fields the
+// official CLI may add that this tool does not model.
+func RemoveEntry(path, key string) error {
+ b, err := os.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ var root map[string]any
+ if err := json.Unmarshal(b, &root); err != nil {
+ return err
+ }
+ skills, ok := root["skills"].(map[string]any)
+ if !ok {
+ return fmt.Errorf("lock file %s has no skills map", path)
+ }
+ if _, exists := skills[key]; !exists {
+ return fmt.Errorf("lock entry %q not found", key)
+ }
+ delete(skills, key)
+ out, err := json.MarshalIndent(root, "", " ")
+ if err != nil {
+ return err
+ }
+ return os.WriteFile(path, append(out, '\n'), 0o644)
+}
diff --git a/internal/locks/locks_test.go b/internal/locks/locks_test.go
new file mode 100644
index 0000000..640c339
--- /dev/null
+++ b/internal/locks/locks_test.go
@@ -0,0 +1,41 @@
+package locks
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func TestRemoveEntry(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "skills-lock.json")
+ content := `{"version":1,"extra":"keep","skills":{"a":{"source":"o/r"},"b":{"source":"o/r2"}}}`
+ if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ if err := RemoveEntry(path, "a"); err != nil {
+ t.Fatalf("RemoveEntry: %v", err)
+ }
+
+ lock, err := ReadLocal(path)
+ if err != nil {
+ t.Fatalf("ReadLocal after prune: %v", err)
+ }
+ if _, gone := lock.Skills["a"]; gone {
+ t.Error("expected entry 'a' to be removed")
+ }
+ if _, kept := lock.Skills["b"]; !kept {
+ t.Error("expected entry 'b' to be preserved")
+ }
+
+ raw, _ := os.ReadFile(path)
+ if !strings.Contains(string(raw), `"extra"`) {
+ t.Errorf("expected unknown top-level field to be preserved, got %s", raw)
+ }
+
+ if err := RemoveEntry(path, "missing"); err == nil {
+ t.Error("expected an error removing a missing key")
+ }
+}
diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go
index 89924e3..36d286a 100644
--- a/internal/tui/tui_test.go
+++ b/internal/tui/tui_test.go
@@ -819,6 +819,36 @@ func TestCachedSourceCloneReuse(t *testing.T) {
}
}
+func TestExecutePruneLockRemovesEntry(t *testing.T) {
+ dir := t.TempDir()
+ lockPath := filepath.Join(dir, "skills-lock.json")
+ if err := os.WriteFile(lockPath, []byte(`{"version":1,"skills":{"ghost":{"source":"o/r"},"keep":{"source":"o/r"}}}`), 0o644); err != nil {
+ t.Fatal(err)
+ }
+
+ m := appModel{cwd: dir, width: 120, height: 32}
+ action := actions.CommandPreview{
+ ID: "prune_lock",
+ ConfirmValue: "ghost",
+ Exec: actions.ExecSpec{Internal: "prune_project_lock"},
+ }
+ _, cmd := m.executeAction(action)
+ if cmd == nil {
+ t.Fatal("expected a rescan command after pruning")
+ }
+
+ data, err := os.ReadFile(lockPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(data), "ghost") {
+ t.Fatalf("expected 'ghost' entry pruned, got %s", data)
+ }
+ if !strings.Contains(string(data), "keep") {
+ t.Fatalf("expected 'keep' entry preserved, got %s", data)
+ }
+}
+
func TestHumanizeSince(t *testing.T) {
now := time.Now()
cases := []struct {
diff --git a/internal/tui/update.go b/internal/tui/update.go
index 8606b1a..ef56017 100644
--- a/internal/tui/update.go
+++ b/internal/tui/update.go
@@ -10,6 +10,7 @@ import (
"github.com/alvinunreal/lazyskills/internal/actions"
"github.com/alvinunreal/lazyskills/internal/compat"
+ "github.com/alvinunreal/lazyskills/internal/locks"
"github.com/alvinunreal/lazyskills/internal/model"
"github.com/alvinunreal/lazyskills/internal/runner"
)
@@ -750,6 +751,29 @@ func (m appModel) executeAction(action actions.CommandPreview) (tea.Model, tea.C
m.actionResult = nil
return m.startDiscovery(action.ConfirmValue, true)
}
+ if action.Exec.Internal == "prune_project_lock" || action.Exec.Internal == "prune_global_lock" {
+ m.commands = false
+ m.confirming = false
+ m.confirmInput = ""
+ m.confirmError = ""
+ path := locks.ProjectLockPath(m.cwd)
+ if action.Exec.Internal == "prune_global_lock" {
+ path = locks.GlobalLockPath()
+ }
+ if err := locks.RemoveEntry(path, action.ConfirmValue); err != nil {
+ m.actionResult = &runner.Result{
+ Program: "prune-lock",
+ Args: []string{action.ConfirmValue},
+ ExitCode: -1,
+ Err: compat.SanitizeMetadata(err.Error()),
+ }
+ m.syncViewport()
+ return m, nil
+ }
+ // Success: rescan drops the now-pruned phantom from the list.
+ m.actionResult = nil
+ return m, loadSnapshot(m.cwd)
+ }
if action.Exec.Internal == "refresh" {
m.actionResult = nil
return m, loadSnapshot(m.cwd)