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

+103 -2
View File
@@ -1,12 +1,17 @@
package client
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/m3tam3re/agent-lib/internal/gitsource"
"github.com/m3tam3re/agent-lib/internal/labels"
"github.com/m3tam3re/agent-lib/internal/target"
)
@@ -29,6 +34,18 @@ type prepared struct {
manifest *Manifest
items []PlannedItem
plan []PlanEntry
labels *LabelSelection
}
// LabelSelection describes an active label filter for one run: the client
// labels, where they came from, which items were left out and which names
// in the labels file match no item. nil when the client carries no labels.
type LabelSelection struct {
Client []string `json:"client"`
Source string `json:"source"`
File bool `json:"labels_file"`
NotSelected []string `json:"not_selected,omitempty"`
Unknown []string `json:"unknown_names,omitempty"`
}
// prepare pulls, inventories, hashes and plans — the complete read-only
@@ -66,11 +83,29 @@ func prepare(opts SyncOptions) (*prepared, error) {
return nil, err
}
lf, err := readLabelsFile(pull.Tree)
if err != nil {
return nil, err
}
var sel *LabelSelection
if cfg.Labels != nil {
sel = &LabelSelection{Client: append([]string{}, (*cfg.Labels)...), Source: cfg.LabelSource, File: lf != nil}
if lf != nil {
sel.Unknown = unknownLabelNames(lf, inv)
}
}
items := make([]PlannedItem, 0, len(inv.Items))
deselected := map[string]bool{}
for _, it := range inv.Deployable() {
if _, ok := oc.DeployPath(it.Type, it.Name); !ok {
continue
}
if sel != nil && !lf.Selected(it.Name, sel.Client) {
deselected[manifestKey(it.Type, it.Name)] = true
sel.NotSelected = append(sel.NotSelected, it.Type+"/"+it.Name)
continue
}
hash, err := HashItem(pull.Tree, it)
if err != nil {
return nil, fmt.Errorf("hashing %s/%s: %w", it.Type, it.Name, err)
@@ -82,13 +117,48 @@ func prepare(opts SyncOptions) (*prepared, error) {
path, _ := oc.DeployPath(typ, name)
return path
}
plan := Plan(items, manifest, diskFacts(oc, manifest, items), pathFor, opts.Force)
plan := Plan(items, manifest, diskFacts(oc, manifest, items), pathFor, PlanOptions{Force: opts.Force, Deselected: deselected})
for _, e := range plan {
if !safePath(e.DstPath) {
return nil, fmt.Errorf("refusing unsafe deploy path %q", e.DstPath)
}
}
return &prepared{paths: paths, pull: pull, manifest: manifest, items: items, plan: plan}, nil
return &prepared{paths: paths, pull: pull, manifest: manifest, items: items, plan: plan, labels: sel}, nil
}
// readLabelsFile loads the labels file from the pulled revision. A missing
// file restricts nothing (nil); a file that exists but does not validate
// aborts the run before any mutation — never fall back to "everything".
func readLabelsFile(tree *gitsource.GitTree) (*labels.File, error) {
data, err := tree.ReadFile(labels.FileName)
if err != nil {
if errors.Is(err, object.ErrFileNotFound) {
return nil, nil
}
return nil, fmt.Errorf("reading %s: %w", labels.FileName, err)
}
lf, err := labels.Parse(data)
if err != nil {
return nil, fmt.Errorf("work repository %w — sync aborted, nothing changed", err)
}
return lf, nil
}
// unknownLabelNames lists names in the labels file that match no inventory
// item of any type (MCP fragments included). Not fatal on the client: the
// curator-side validate is the hard gate.
func unknownLabelNames(lf *labels.File, inv *Inventory) []string {
known := map[string]bool{}
for _, it := range inv.Items {
known[it.Name] = true
}
var out []string
for _, n := range lf.Names() {
if !known[n] {
out = append(out, n)
}
}
return out
}
// resolveRunConfig applies the --repo inline override to the config chain:
@@ -101,6 +171,9 @@ func resolveRunConfig(opts SyncOptions) (*ClientConfig, error) {
return nil, err
}
cfg.FillTokenAuth()
if err := ApplyAdminLabels(cfg, DefaultConfigPath()); err != nil {
return nil, err
}
return cfg, nil
}
cfg, err := LoadOptionalConfig(opts.ConfigPath)
@@ -115,6 +188,9 @@ func resolveRunConfig(opts SyncOptions) (*ClientConfig, error) {
cfg.Ref = opts.Ref
}
cfg.FillTokenAuth()
if err := ApplyAdminLabels(cfg, DefaultConfigPath()); err != nil {
return nil, err
}
return cfg, nil
}
@@ -127,6 +203,7 @@ func Sync(opts SyncOptions, stdout io.Writer) (*SyncReport, error) {
if err != nil {
return nil, err
}
printLabelSelection(stdout, p.labels)
return ExecutePlan(p.pull.Tree, p.plan, p.manifest, p.paths, p.pull.Rev, stdout)
}
@@ -145,3 +222,27 @@ func safePath(dst string) bool {
}
return rel != ".." && rel != "." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
}
// printLabelSelection reports an active label filter in one line, plus a
// warning line per labels-file name that matches no item.
func printLabelSelection(w io.Writer, sel *LabelSelection) {
if sel == nil {
return
}
fmt.Fprintln(w, sel.Summary())
for _, n := range sel.Unknown {
fmt.Fprintf(w, "warning: %s names %q, which matches no item in the repository\n", labels.FileName, n)
}
}
// Summary renders the selection as one human-readable line.
func (s *LabelSelection) Summary() string {
client := strings.Join(s.Client, ", ")
if client == "" {
client = "(none — unlabeled items only)"
}
if !s.File {
return fmt.Sprintf("labels: %s (%s) — repository has no %s, nothing filtered", client, s.Source, labels.FileName)
}
return fmt.Sprintf("labels: %s (%s) — %d item(s) not selected", client, s.Source, len(s.NotSelected))
}