fix(client): harden label pinning and close review findings

- Resolve the Windows admin config via the ProgramData known folder
  instead of the user-controlled ProgramData variable, so a user cannot
  redirect the admin lookup and drop pinned labels
- vendor rename rewrites matching agent-lib.labels.json entries, so a
  renamed item keeps its labels instead of going to every client
- validate checks the labels file in repositories without a lockfile
- A kept modified copy of a deselected item is no longer drift in status,
  and the toast explains it as "no longer assigned to you"
- Labels parser rejects trailing data such as an extra closing brace
- Tests: admin pin wiring (config chain, --repo, admin as chosen config),
  reselect redeploy with byte-level no-op, status labels JSON, validate
  without lockfile, rename keeps labels
- README: document unreadable admin config, drift semantics, rename

Refs: agent_lib-fh4

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
m3ta-chironandClaude Opus 5.5 committed 2026-11-14 11:00:41 +01:00
1 parent 59735c3b24
commit ae316860e5
17 files changed
+296 -17

No files matched your search

+100
View File
@@ -1,6 +1,7 @@
package e2e
import (
"encoding/json"
"os"
"path/filepath"
"strings"
@@ -164,3 +165,102 @@ func TestSyncLabelsUnknownNameWarns(t *testing.T) {
t.Errorf("sync must warn about the unknown name, got:\n%s", out)
}
}
func TestSyncLabelsReselectRedeploysAndIsByteStable(t *testing.T) {
h := newHarness(t)
env := newSyncEnv(t, h)
buildWorkRepo(t, h, env.workRepo)
commitLabels(t, h, env.workRepo, `{"version":1,"labels":{"dev":["helper"]}}`)
agent := filepath.Join(env.home, ".config/opencode/agents/helper.md")
env.writeLabelConfig(t, `["ops"]`)
if out, err := env.run(t); err != nil {
t.Fatalf("sync failed: %v\n%s", err, out)
}
if exists(agent) {
t.Fatal("helper is labeled dev and must not deploy for ops")
}
env.writeLabelConfig(t, `["ops","dev"]`)
if out, err := env.run(t); err != nil {
t.Fatalf("sync failed: %v\n%s", err, out)
}
if !exists(agent) {
t.Fatal("an item that becomes selected must deploy")
}
manifest := filepath.Join(env.stateDir, "manifest.json")
before, _ := os.ReadFile(manifest)
agentBefore, _ := os.ReadFile(agent)
if out, err := env.run(t); err != nil {
t.Fatalf("sync failed: %v\n%s", err, out)
}
after, _ := os.ReadFile(manifest)
agentAfter, _ := os.ReadFile(agent)
if string(before) != string(after) || string(agentBefore) != string(agentAfter) {
t.Error("a second run with labels must be a byte-level no-op")
}
}
func TestStatusLabelsJSONAndNoDriftForDeselectedCopy(t *testing.T) {
h := newHarness(t)
env := newSyncEnv(t, h)
buildWorkRepo(t, h, env.workRepo)
commitLabels(t, h, env.workRepo, `{"version":1,"labels":{"dev":["helper"]}}`)
env.writeLabelConfig(t, `["dev"]`)
if out, err := env.run(t); err != nil {
t.Fatalf("sync failed: %v\n%s", err, out)
}
agent := filepath.Join(env.home, ".config/opencode/agents/helper.md")
if err := os.WriteFile(agent, []byte("my edit\n"), 0o644); err != nil {
t.Fatal(err)
}
env.writeLabelConfig(t, `["ops"]`)
if out, err := env.run(t); err != nil {
t.Fatalf("sync failed: %v\n%s", err, out)
}
cmd := agentLibCmd("status", "--json", "--config", env.config)
cmd.Env = append(os.Environ(), "HOME="+env.home, "USERPROFILE="+env.home, "AGENT_LIB_STATE_DIR="+env.stateDir)
out, err := cmd.Output()
if err != nil {
t.Fatalf("a kept copy of a deselected item is not drift; status must exit 0: %v\n%s", err, out)
}
var report struct {
Drift bool `json:"problematic_drift"`
Labels *struct {
Client []string `json:"client"`
Source string `json:"source"`
File bool `json:"labels_file"`
NotSelected []string `json:"not_selected"`
} `json:"labels"`
}
if err := json.Unmarshal(out, &report); err != nil {
t.Fatalf("status --json not parseable: %v\n%s", err, out)
}
if report.Drift {
t.Error("deselected modified copy must not count as drift")
}
if report.Labels == nil || report.Labels.Source != "flag" || !report.Labels.File ||
strings.Join(report.Labels.Client, ",") != "ops" ||
strings.Join(report.Labels.NotSelected, ",") != "agents/helper" {
t.Errorf("unexpected labels object: %+v", report.Labels)
}
}
func TestValidateLabelsWithoutLockfile(t *testing.T) {
h := newHarness(t)
write := func(rel, content string) {
p := filepath.Join(h.workDir, rel)
os.MkdirAll(filepath.Dir(p), 0o755)
os.WriteFile(p, []byte(content), 0o644)
}
write("skills/alpha/SKILL.md", "---\nname: alpha\ndescription: a\n---\n")
write("agent-lib.labels.json", `{"version":1,"labels":{"dev":["alpha"]}}`)
if out, err := h.run(t, "validate"); err != nil || !strings.Contains(out, "agent-lib.labels.json valid") {
t.Fatalf("a repo without lockfile must still validate its labels: %v\n%s", err, out)
}
write("agent-lib.labels.json", `{"version":1,"labels":{"dev":["gamma"]}}`)
if out, err := h.run(t, "validate"); err == nil || !strings.Contains(out, `"gamma"`) {
t.Fatalf("an unknown name must fail validate: %v\n%s", err, out)
}
}