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
+61
@@ -0,0 +1,61 @@
|
||||
package vendor
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/m3tam3re/agent-lib/internal/lockfile"
|
||||
)
|
||||
|
||||
func labelsWorkDir(t *testing.T, labelsJSON string) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
write := func(rel, content string) {
|
||||
p := filepath.Join(dir, rel)
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
write("skills/alpha/SKILL.md", "---\nname: alpha\ndescription: a\n---\n")
|
||||
write("agents/beta.md", "---\ndescription: b\n---\n")
|
||||
write("mcp/websearch.yaml", "websearch: {}\n")
|
||||
if labelsJSON != "" {
|
||||
write("agent-lib.labels.json", labelsJSON)
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
func labelErrors(v Validation) []string {
|
||||
var out []string
|
||||
for _, e := range v.Errors {
|
||||
if e.Rule == "labels" {
|
||||
out = append(out, e.Message)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestValidateLabels(t *testing.T) {
|
||||
lf := lockfile.New()
|
||||
if errs := labelErrors(Validate(labelsWorkDir(t, ""), lf)); len(errs) != 0 {
|
||||
t.Errorf("no labels file is fine, got %v", errs)
|
||||
}
|
||||
ok := `{"version":1,"labels":{"sales":["alpha","websearch"],"dev":["beta"]}}`
|
||||
if errs := labelErrors(Validate(labelsWorkDir(t, ok), lf)); len(errs) != 0 {
|
||||
t.Errorf("valid file reported errors: %v", errs)
|
||||
}
|
||||
unknown := `{"version":1,"labels":{"sales":["alpha","gamma"]}}`
|
||||
errs := labelErrors(Validate(labelsWorkDir(t, unknown), lf))
|
||||
if len(errs) != 1 || !strings.Contains(errs[0], `"gamma"`) {
|
||||
t.Errorf("unknown name must be reported once, got %v", errs)
|
||||
}
|
||||
bad := `{"version":1,"labels":{"Sales":["alpha"]}}`
|
||||
if errs := labelErrors(Validate(labelsWorkDir(t, bad), lf)); len(errs) != 1 {
|
||||
t.Errorf("malformed label must be reported, got %v", errs)
|
||||
}
|
||||
}
|
||||
Vendored
+37
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/m3tam3re/agent-lib/internal/discovery"
|
||||
"github.com/m3tam3re/agent-lib/internal/labels"
|
||||
"github.com/m3tam3re/agent-lib/internal/lockfile"
|
||||
)
|
||||
|
||||
@@ -34,6 +35,7 @@ func Validate(workDir string, lf *lockfile.Lockfile) Validation {
|
||||
v.checkExternalArea(workDir, name, src)
|
||||
}
|
||||
v.Errors = append(v.Errors, validateCollisions(workDir, lf)...)
|
||||
v.checkLabels(workDir, lf)
|
||||
sort.Slice(v.Errors, func(i, j int) bool {
|
||||
if v.Errors[i].Source != v.Errors[j].Source {
|
||||
return v.Errors[i].Source < v.Errors[j].Source
|
||||
@@ -44,6 +46,41 @@ func Validate(workDir string, lf *lockfile.Lockfile) Validation {
|
||||
return v
|
||||
}
|
||||
|
||||
// checkLabels validates the optional labels file: format rules (via the
|
||||
// labels parser) and that every name resolves to an item of any type —
|
||||
// own items, vendored items under their deployed names, MCP fragments.
|
||||
func (v *Validation) checkLabels(workDir string, lf *lockfile.Lockfile) {
|
||||
data, err := os.ReadFile(filepath.Join(workDir, labels.FileName))
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
v.fail("", "labels", "reading %s: %v", labels.FileName, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
file, err := labels.Parse(data)
|
||||
if err != nil {
|
||||
v.fail("", "labels", "%v", err)
|
||||
return
|
||||
}
|
||||
own, err := ownDeployed(workDir)
|
||||
if err != nil {
|
||||
v.fail("", "labels", "scanning own items: %v", err)
|
||||
return
|
||||
}
|
||||
known := map[string]bool{}
|
||||
for _, m := range mergeIndex(own, lockfileDeployed(lf, "")) {
|
||||
for name := range m {
|
||||
known[name] = true
|
||||
}
|
||||
}
|
||||
for _, name := range file.Names() {
|
||||
if !known[name] {
|
||||
v.fail("", "labels", "%s: %q (labels %s) matches no item in the work repository",
|
||||
labels.FileName, name, strings.Join(file.LabelsOf(name), ", "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validation) fail(source, rule, format string, args ...any) {
|
||||
v.Errors = append(v.Errors, ValidationError{
|
||||
Source: source,
|
||||
|
||||
Reference in new issue
Block a user