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

+25
View File
@@ -0,0 +1,25 @@
package vendor
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestRenameLabelEntriesKeepsLabels(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "agent-lib.labels.json")
os.WriteFile(p, []byte(`{"version":1,"labels":{"sales":["old-name","other"],"dev":["old-name"]}}`), 0o644)
n, err := renameLabelEntries(dir, map[string]string{"old-name": "new-name"})
if err != nil || n != 2 {
t.Fatalf("want 2 entries renamed, got %d, %v", n, err)
}
data, _ := os.ReadFile(p)
if strings.Contains(string(data), "old-name") || strings.Count(string(data), "new-name") != 2 {
t.Errorf("entries not rewritten:\n%s", data)
}
if n, err := renameLabelEntries(t.TempDir(), map[string]string{"a": "b"}); err != nil || n != 0 {
t.Errorf("missing labels file must be a no-op, got %d, %v", n, err)
}
}
+55
View File
@@ -1,6 +1,7 @@
package vendor
import (
"encoding/json"
"fmt"
"io"
"os"
@@ -9,6 +10,7 @@ import (
"github.com/m3tam3re/agent-lib/internal/discovery"
"github.com/m3tam3re/agent-lib/internal/gitsource"
"github.com/m3tam3re/agent-lib/internal/labels"
"github.com/m3tam3re/agent-lib/internal/lockfile"
)
@@ -128,6 +130,17 @@ func Rename(workDir, name string, opts RenameOptions, stdout io.Writer) error {
if err := lf.Save(lockPath); err != nil {
return err
}
renamed := map[string]string{}
for _, id := range ids {
if before, after := deployedName(id, oldRenames), deployedName(id, newRenames); before != after {
renamed[before] = after
}
}
if n, err := renameLabelEntries(workDir, renamed); err != nil {
fmt.Fprintf(stdout, "warning: %s not updated: %v — fix the entries by hand\n", labels.FileName, err)
} else if n > 0 {
fmt.Fprintf(stdout, "%s: %d entries renamed so the items keep their labels\n", labels.FileName, n)
}
inSelection := idSet(selected)
fmt.Fprintf(stdout, "source %s: renames updated at pinned rev %s\n", name, short(src.Rev))
@@ -178,3 +191,45 @@ func boolSet(ids []string) map[string]bool {
}
return out
}
// renameLabelEntries rewrites labels-file entries from old to new deployed
// names. Without it a rename would silently drop an item's labels and widen
// its delivery to every client. Returns how many entries changed; a missing
// file is not an error.
func renameLabelEntries(workDir string, renamed map[string]string) (int, error) {
if len(renamed) == 0 {
return 0, nil
}
path := filepath.Join(workDir, labels.FileName)
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
return 0, nil
}
if err != nil {
return 0, err
}
file, err := labels.Parse(data)
if err != nil {
return 0, err
}
changed := 0
for label, names := range file.Labels {
for i, n := range names {
if to, ok := renamed[n]; ok {
file.Labels[label][i] = to
changed++
}
}
}
if changed == 0 {
return 0, nil
}
out, err := json.MarshalIndent(struct {
Version int `json:"version"`
Labels map[string][]string `json:"labels"`
}{file.Version, file.Labels}, "", " ")
if err != nil {
return 0, err
}
return changed, os.WriteFile(path, append(out, '\n'), 0o644)
}