feat(client): deliver items only to clients with a matching label

- Add internal/labels: parse and validate the optional work-repo file
  agent-lib.labels.json (version 1; unlabeled items go to every client;
  names match across types)
- Add "labels" to the client config: absent = no filtering, [] = unlabeled
  items only; labels from the admin default config are pinned and cannot be
  overridden by user, env or flag configs; a broken admin config aborts
- Filter in prepare() before planning; deselected items follow the
  upstream-deletion rules (unmodified removed, modified kept + warn,
  --force removes) with reason "not selected for this client's labels"
- Abort sync before any change when the labels file does not validate;
  names that match no item only warn on the client
- Report the active selection in sync output and status (--json: labels
  object with client, source, labels_file, not_selected, unknown_names)
- validate checks the labels file, including that every name resolves to
  an own, vendored or MCP item
- Document in README ("Client labels"), fleet handoff and the proposal;
  revise the "client-side selection" non-goal
- Tests: labels parser, config precedence, planner deselection, e2e for
  filter/remove/keep-modified/abort/unknown names, validate

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 10:36:07 +01:00
1 parent 39952ceb02
commit 59735c3b24
16 files changed
+904 -38

No files matched your search

+100
View File
@@ -163,3 +163,103 @@ func TestFillTokenAuthEnvOnlyFillsEmptyToken(t *testing.T) {
t.Errorf("env must never override a config token: %+v", configured)
}
}
func writeRaw(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
}
func TestConfigLabelsAbsentEmptyAndSet(t *testing.T) {
dir := t.TempDir()
cases := map[string]struct {
body string
want *[]string
source string
}{
"absent": {`{"repo_url":"r"}`, nil, ""},
"empty": {`{"repo_url":"r","labels":[]}`, &[]string{}, "flag"},
"set": {`{"repo_url":"r","labels":["sales","lead"]}`, &[]string{"sales", "lead"}, "flag"},
}
for name, tc := range cases {
p := filepath.Join(dir, name+".json")
writeRaw(t, p, tc.body)
cfg, err := LoadClientConfig(p)
if err != nil {
t.Fatalf("%s: %v", name, err)
}
if (cfg.Labels == nil) != (tc.want == nil) {
t.Fatalf("%s: labels nil-ness mismatch: %v", name, cfg.Labels)
}
if tc.want != nil && strings.Join(*cfg.Labels, ",") != strings.Join(*tc.want, ",") {
t.Errorf("%s: labels = %v", name, *cfg.Labels)
}
if cfg.LabelSource != tc.source {
t.Errorf("%s: source = %q, want %q", name, cfg.LabelSource, tc.source)
}
}
}
func TestConfigRejectsMalformedLabel(t *testing.T) {
p := filepath.Join(t.TempDir(), "c.json")
writeRaw(t, p, `{"repo_url":"r","labels":["Sales"]}`)
if _, err := LoadClientConfig(p); err == nil {
t.Fatal("an uppercase label must be rejected")
}
}
func TestAdminLabelsWinOverUserLabels(t *testing.T) {
dir := t.TempDir()
admin := filepath.Join(dir, "admin.json")
writeRaw(t, admin, `{"repo_url":"r","labels":["finance"]}`)
user := []string{"sales", "it"}
cfg := &ClientConfig{RepoURL: "r", Labels: &user, LabelSource: "user"}
if err := ApplyAdminLabels(cfg, admin); err != nil {
t.Fatal(err)
}
if cfg.LabelSource != "admin" || strings.Join(*cfg.Labels, ",") != "finance" {
t.Fatalf("admin labels must replace user labels, got %v (%s)", *cfg.Labels, cfg.LabelSource)
}
}
func TestAdminEmptyLabelsAlsoPin(t *testing.T) {
admin := filepath.Join(t.TempDir(), "admin.json")
writeRaw(t, admin, `{"repo_url":"r","labels":[]}`)
user := []string{"sales"}
cfg := &ClientConfig{Labels: &user, LabelSource: "user"}
if err := ApplyAdminLabels(cfg, admin); err != nil {
t.Fatal(err)
}
if cfg.Labels == nil || len(*cfg.Labels) != 0 {
t.Fatalf("an admin [] must pin 'unlabeled only', got %v", cfg.Labels)
}
}
func TestAdminWithoutLabelsLeavesUserLabels(t *testing.T) {
dir := t.TempDir()
admin := filepath.Join(dir, "admin.json")
writeRaw(t, admin, `{"repo_url":"r"}`)
user := []string{"sales"}
cfg := &ClientConfig{Labels: &user, LabelSource: "user"}
if err := ApplyAdminLabels(cfg, admin); err != nil {
t.Fatal(err)
}
if cfg.LabelSource != "user" || strings.Join(*cfg.Labels, ",") != "sales" {
t.Fatalf("without admin labels the user's labels apply, got %v (%s)", *cfg.Labels, cfg.LabelSource)
}
if err := ApplyAdminLabels(cfg, filepath.Join(dir, "missing.json")); err != nil {
t.Fatalf("a missing admin config is not an error: %v", err)
}
}
func TestAdminConfigBrokenIsFatal(t *testing.T) {
admin := filepath.Join(t.TempDir(), "admin.json")
writeRaw(t, admin, `{`)
if err := ApplyAdminLabels(&ClientConfig{}, admin); err == nil {
t.Fatal("an unreadable admin config must abort: its label intent is unknown")
}
}