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

+23 -4
View File
@@ -17,6 +17,16 @@ const (
ActKeep Action = "keep"
)
// ReasonNotSelected explains removals caused by the client's labels.
const ReasonNotSelected = "not selected for this client's labels"
// PlanOptions tune planning. Force overwrites/removes locally modified
// managed items; Deselected holds manifest keys filtered out by labels.
type PlanOptions struct {
Force bool
Deselected map[string]bool
}
// PlannedItem is one deployable inventory item with its repo-side hash.
type PlannedItem struct {
Type string
@@ -51,7 +61,12 @@ type PlanEntry struct {
// locally modified managed items are overwritten with the upstream version
// (and removed after upstream deletion) instead of being skipped; items
// agent-lib never managed are never touched, even with force.
func Plan(items []PlannedItem, manifest *Manifest, disk map[string]DiskFact, pathFor func(typ, name string) string, force bool) []PlanEntry {
//
// Deselected marks keys that exist upstream but are not selected for this
// client's labels. Planning treats them exactly like upstream deletions —
// only the reasons differ, so users learn why an item went away.
func Plan(items []PlannedItem, manifest *Manifest, disk map[string]DiskFact, pathFor func(typ, name string) string, opts PlanOptions) []PlanEntry {
force := opts.Force
invByKey := map[string]PlannedItem{}
for _, it := range items {
invByKey[manifestKey(it.Type, it.Name)] = it
@@ -87,6 +102,10 @@ func Plan(items []PlannedItem, manifest *Manifest, disk map[string]DiskFact, pat
it, inInv := invByKey[key]
rec := manifest.Items[key]
fact := disk[key]
gone := "upstream deleted"
if opts.Deselected[key] {
gone = ReasonNotSelected
}
switch {
case inInv && rec == nil:
@@ -102,12 +121,12 @@ func Plan(items []PlannedItem, manifest *Manifest, disk map[string]DiskFact, pat
case rec != nil && !fact.Exists:
emit(ActRemove, PlannedItem{Type: rec.Type, Name: rec.Name, Origin: rec.Origin}, "already gone from disk; dropping manifest record")
case rec != nil && fact.Hash == rec.Hash:
emit(ActRemove, PlannedItem{Type: rec.Type, Name: rec.Name, Origin: rec.Origin}, "upstream deleted")
emit(ActRemove, PlannedItem{Type: rec.Type, Name: rec.Name, Origin: rec.Origin}, gone)
case rec != nil && force:
emit(ActRemove, PlannedItem{Type: rec.Type, Name: rec.Name, Origin: rec.Origin}, "upstream deleted; forced removal of the locally modified copy")
emit(ActRemove, PlannedItem{Type: rec.Type, Name: rec.Name, Origin: rec.Origin}, gone+"; forced removal of the locally modified copy")
case rec != nil:
emit(ActSkip, PlannedItem{Type: rec.Type, Name: rec.Name, Origin: rec.Origin},
"upstream deleted but the local copy was modified; keeping it")
gone+" but the local copy was modified; keeping it")
default:
emit(ActLeave, PlannedItem{Type: typeOfKey(key), Name: nameOfKey(key)},
"unmanaged local item; left untouched")