Skip to content

mission control for agent skills

Prune lock entry automatically on skill remove

Alvin Unrealc472ff3dcb08
diff --git a/internal/locks/locks.go b/internal/locks/locks.go
index c2932aa..2d3aed8 100644
--- a/internal/locks/locks.go
+++ b/internal/locks/locks.go
@@ -79,3 +79,34 @@ func RemoveEntry(path, key string) error {
}
return os.WriteFile(path, append(out, '\n'), 0o644)
}
+
+// RemoveEntryIfExists deletes a lock entry when both the lock file and key are
+// present. Missing files and absent keys are treated as no-ops, making it safe
+// to run after external uninstall commands that may or may not update locks
+// themselves.
+func RemoveEntryIfExists(path, key string) (bool, error) {
+ b, err := os.ReadFile(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return false, nil
+ }
+ return false, err
+ }
+ var root map[string]any
+ if err := json.Unmarshal(b, &root); err != nil {
+ return false, err
+ }
+ skills, ok := root["skills"].(map[string]any)
+ if !ok {
+ return false, fmt.Errorf("lock file %s has no skills map", path)
+ }
+ if _, exists := skills[key]; !exists {
+ return false, nil
+ }
+ delete(skills, key)
+ out, err := json.MarshalIndent(root, "", " ")
+ if err != nil {
+ return false, err
+ }
+ return true, os.WriteFile(path, append(out, '\n'), 0o644)
+}
diff --git a/internal/locks/locks_test.go b/internal/locks/locks_test.go
index 640c339..84de1a5 100644
--- a/internal/locks/locks_test.go
+++ b/internal/locks/locks_test.go
@@ -39,3 +39,32 @@ func TestRemoveEntry(t *testing.T) {
t.Error("expected an error removing a missing key")
}
}
+
+func TestRemoveEntryIfExistsIgnoresMissingFileAndKey(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "skills-lock.json")
+
+ removed, err := RemoveEntryIfExists(path, "ghost")
+ if err != nil || removed {
+ t.Fatalf("expected missing lock to be ignored, removed=%v err=%v", removed, err)
+ }
+
+ 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)
+ }
+
+ removed, err = RemoveEntryIfExists(path, "missing")
+ if err != nil || removed {
+ t.Fatalf("expected missing key to be ignored, removed=%v err=%v", removed, err)
+ }
+
+ removed, err = RemoveEntryIfExists(path, "a")
+ if err != nil || !removed {
+ t.Fatalf("expected existing key to be removed, removed=%v err=%v", removed, err)
+ }
+ raw, _ := os.ReadFile(path)
+ if strings.Contains(string(raw), `"a"`) || !strings.Contains(string(raw), `"b"`) || !strings.Contains(string(raw), `"extra"`) {
+ t.Fatalf("expected a pruned while preserving b and extra, got %s", raw)
+ }
+}
diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go
index 36d286a..d09e0e3 100644
--- a/internal/tui/tui_test.go
+++ b/internal/tui/tui_test.go
@@ -849,6 +849,50 @@ func TestExecutePruneLockRemovesEntry(t *testing.T) {
}
}
+func TestExecuteRemovePrunesProjectLockEntry(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)
+ }
+
+ originalRunExec := runExec
+ runExec = func(spec runner.ExecSpec) runner.Result {
+ return runner.Result{Program: spec.Program, Args: spec.Args, Cwd: spec.Cwd, ExitCode: 0}
+ }
+ t.Cleanup(func() { runExec = originalRunExec })
+
+ m := appModel{cwd: dir, width: 120, height: 32}
+ action := actions.CommandPreview{
+ ID: "remove",
+ ConfirmValue: "ghost",
+ Mutates: true,
+ Exec: actions.ExecSpec{Program: "skills", Args: []string{"remove", "ghost", "--yes"}},
+ }
+ _, cmd := m.executeAction(action)
+ if cmd == nil {
+ t.Fatal("expected command")
+ }
+ msg, ok := cmd().(actionResultMsg)
+ if !ok {
+ t.Fatalf("expected actionResultMsg")
+ }
+ if msg.result.ExitCode != 0 || msg.result.Err != "" {
+ t.Fatalf("expected successful remove result, got %#v", msg.result)
+ }
+
+ data, err := os.ReadFile(lockPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(data), "ghost") {
+ t.Fatalf("expected 'ghost' entry pruned after remove, 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 ef56017..9a1df50 100644
--- a/internal/tui/update.go
+++ b/internal/tui/update.go
@@ -794,7 +794,8 @@ func (m appModel) executeAction(action actions.CommandPreview) (tea.Model, tea.C
result.ExitCode = -1
result.Err = err.Error()
}
- return actionResultMsg{result: result, mutates: action.Mutates}
+ partialSuccess := cleanupLockAfterRemove(action, m.cwd, &result)
+ return actionResultMsg{result: result, mutates: action.Mutates, partialSuccess: partialSuccess}
})
}
if len(action.Exec.Batch) > 0 {
@@ -820,8 +821,28 @@ func (m appModel) executeAction(action actions.CommandPreview) (tea.Model, tea.C
m.syncViewport()
return m, func() tea.Msg {
result := runExec(spec)
- return actionResultMsg{result: result, mutates: action.Mutates}
+ partialSuccess := cleanupLockAfterRemove(action, m.cwd, &result)
+ return actionResultMsg{result: result, mutates: action.Mutates, partialSuccess: partialSuccess}
+ }
+}
+
+func cleanupLockAfterRemove(action actions.CommandPreview, cwd string, result *runner.Result) bool {
+ if action.ID != "remove" || result == nil || result.ExitCode != 0 || result.Err != "" || action.ConfirmValue == "" {
+ return false
+ }
+ path := locks.ProjectLockPath(cwd)
+ for _, arg := range action.Exec.Args {
+ if arg == "-g" || arg == "--global" {
+ path = locks.GlobalLockPath()
+ break
+ }
+ }
+ if _, err := locks.RemoveEntryIfExists(path, action.ConfirmValue); err != nil {
+ result.ExitCode = -1
+ result.Err = "removed skill, but failed to update lock: " + compat.SanitizeMetadata(err.Error())
+ return true
}
+ return false
}
func (m appModel) runBatch(batch []actions.ExecSpec) (runner.Result, bool) {