Files
agent-lib/internal/client/status.go
T
m3ta-chironandClaude Opus 5.5 59735c3b24 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>
2026-11-14 10:36:07 +01:00

111 lines
3.3 KiB
Go

package client
import (
"fmt"
"io"
"github.com/m3tam3re/agent-lib/internal/version"
)
// StatusEntry names one item in a status section.
type StatusEntry struct {
Item string `json:"item"`
Reason string `json:"reason,omitempty"`
}
// StatusReport is the machine-readable local-state report.
type StatusReport struct {
Version string `json:"agent_lib_version"`
DeployedRev string `json:"deployed_revision"`
RepoRev string `json:"repository_revision"`
InSync bool `json:"in_sync"`
Drift bool `json:"problematic_drift"`
Pending []StatusEntry `json:"pending_changes,omitempty"`
Skipped []StatusEntry `json:"skipped_items,omitempty"`
Unrecognized []StatusEntry `json:"unrecognized_items,omitempty"`
Labels *LabelSelection `json:"labels,omitempty"`
}
// Status reports the local state without mutating anything. Drift is true
// when local modifications block repository updates (or occupy names the
// repository needs) — the state headless drift checks flag with a non-zero
// exit code.
func Status(opts SyncOptions) (*StatusReport, error) {
p, err := prepare(opts)
if err != nil {
return nil, err
}
report := &StatusReport{
Version: version.Version,
DeployedRev: p.manifest.Revision,
RepoRev: p.pull.Rev,
Labels: p.labels,
}
for _, e := range p.plan {
item := e.Type + "/" + e.Name
switch e.Action {
case ActDeploy, ActUpdate, ActRemove:
report.Pending = append(report.Pending, StatusEntry{Item: item, Reason: string(e.Action)})
case ActSkip:
blocked := true
if rec := p.manifest.Items[manifestKey(e.Type, e.Name)]; rec != nil && rec.Hash == e.NewHash {
blocked = false
}
if blocked {
report.Drift = true
}
report.Skipped = append(report.Skipped, StatusEntry{Item: item, Reason: e.Reason})
case ActLeave:
report.Unrecognized = append(report.Unrecognized, StatusEntry{Item: item, Reason: e.Reason})
}
}
report.InSync = report.DeployedRev == report.RepoRev && len(report.Pending) == 0
return report, nil
}
// PrintStatus renders the human-readable report and returns the drift
// verdict so the caller can set the exit code.
func PrintStatus(w io.Writer, r *StatusReport) bool {
fmt.Fprintf(w, "agent-lib %s\n", r.Version)
fmt.Fprintf(w, "deployed revision: %s\n", revOrNone(r.DeployedRev))
fmt.Fprintf(w, "repository revision: %s\n", revOrNone(r.RepoRev))
if r.InSync {
fmt.Fprintln(w, "state: in sync")
} else {
fmt.Fprintln(w, "state: out of sync")
}
printSection := func(title string, entries []StatusEntry) {
if len(entries) == 0 {
return
}
fmt.Fprintf(w, "%s:\n", title)
for _, e := range entries {
fmt.Fprintf(w, " %s", e.Item)
if e.Reason != "" {
fmt.Fprintf(w, " — %s", e.Reason)
}
fmt.Fprintln(w)
}
}
if r.Labels != nil {
fmt.Fprintln(w, r.Labels.Summary())
for _, n := range r.Labels.Unknown {
fmt.Fprintf(w, " warning: labels file names %q, which matches no item\n", n)
}
}
printSection("pending changes", r.Pending)
printSection("skipped (local modifications)", r.Skipped)
printSection("unrecognized local items", r.Unrecognized)
if r.Drift {
fmt.Fprintln(w, "drift: local modifications block repository updates")
}
return r.Drift
}
func revOrNone(rev string) string {
if rev == "" {
return "(never synced)"
}
return rev
}