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:
1 parent
39952ceb02
commit
59735c3b24
16 files changed
+904
-38
No files matched your search
@@ -0,0 +1,76 @@
|
||||
package labels
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseAndSelect(t *testing.T) {
|
||||
f, err := Parse([]byte(`{"version":1,"labels":{"sales":["crm","quote"],"finance":["model","crm"]}}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cases := []struct {
|
||||
item string
|
||||
client []string
|
||||
want bool
|
||||
}{
|
||||
{"unlabeled", nil, true},
|
||||
{"unlabeled", []string{}, true},
|
||||
{"crm", nil, false},
|
||||
{"crm", []string{"sales"}, true},
|
||||
{"crm", []string{"finance"}, true},
|
||||
{"quote", []string{"finance"}, false},
|
||||
{"quote", []string{"finance", "sales"}, true},
|
||||
{"model", []string{"other"}, false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := f.Selected(c.item, c.client); got != c.want {
|
||||
t.Errorf("Selected(%q, %v) = %v, want %v", c.item, c.client, got, c.want)
|
||||
}
|
||||
}
|
||||
if got := strings.Join(f.Names(), ","); got != "crm,model,quote" {
|
||||
t.Errorf("Names() = %s", got)
|
||||
}
|
||||
if got := strings.Join(f.LabelsOf("crm"), ","); got != "finance,sales" {
|
||||
t.Errorf("LabelsOf(crm) = %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilFileRestrictsNothing(t *testing.T) {
|
||||
var f *File
|
||||
if !f.Selected("anything", nil) {
|
||||
t.Error("a nil file must select every item")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyLabelsMapIsValid(t *testing.T) {
|
||||
f, err := Parse([]byte(`{"version":1,"labels":{}}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !f.Selected("x", nil) {
|
||||
t.Error("no labels declared means no restrictions")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRejects(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"not json": `{`,
|
||||
"unknown key": `{"version":1,"labels":{},"extra":true}`,
|
||||
"wrong version": `{"version":2,"labels":{}}`,
|
||||
"missing version": `{"labels":{}}`,
|
||||
"missing labels": `{"version":1}`,
|
||||
"upper label": `{"version":1,"labels":{"Sales":["a"]}}`,
|
||||
"space label": `{"version":1,"labels":{"sales team":["a"]}}`,
|
||||
"leading dash": `{"version":1,"labels":{"-sales":["a"]}}`,
|
||||
"empty list": `{"version":1,"labels":{"sales":[]}}`,
|
||||
"empty name": `{"version":1,"labels":{"sales":[""]}}`,
|
||||
"trailing data": `{"version":1,"labels":{}} {}`,
|
||||
}
|
||||
for name, data := range cases {
|
||||
if _, err := Parse([]byte(data)); err == nil {
|
||||
t.Errorf("%s: expected an error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user