- 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>
167 lines
5.2 KiB
Go
167 lines
5.2 KiB
Go
package e2e
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func (e *syncEnv) writeLabelConfig(t *testing.T, labelsJSON string) {
|
|
t.Helper()
|
|
e.config = filepath.Join(e.configDir, "config.json")
|
|
body := `{"repo_url":"` + filepath.ToSlash(e.workRepo) + `","ref":"main"`
|
|
if labelsJSON != "" {
|
|
body += `,"labels":` + labelsJSON
|
|
}
|
|
body += "}"
|
|
if err := os.WriteFile(e.config, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func commitLabels(t *testing.T, h *harness, dir, content string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(dir, "agent-lib.labels.json"), []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h.git(t, dir, "add", "-A")
|
|
h.git(t, dir, "commit", "-m", "labels")
|
|
}
|
|
|
|
func exists(p string) bool {
|
|
_, err := os.Stat(p)
|
|
return err == nil
|
|
}
|
|
|
|
func TestSyncLabelsFilterAndRemove(t *testing.T) {
|
|
h := newHarness(t)
|
|
env := newSyncEnv(t, h)
|
|
buildWorkRepo(t, h, env.workRepo)
|
|
commitLabels(t, h, env.workRepo, `{"version":1,"labels":{"ops":["deploy"],"dev":["helper","own-skill"]}}`)
|
|
|
|
skill := filepath.Join(env.home, ".agents/skills/own-skill")
|
|
cmd := filepath.Join(env.home, ".config/opencode/commands/deploy.md")
|
|
agent := filepath.Join(env.home, ".config/opencode/agents/helper.md")
|
|
|
|
// No labels field: no filtering at all (backward compatible).
|
|
env.writeLabelConfig(t, "")
|
|
if out, err := env.run(t); err != nil {
|
|
t.Fatalf("sync failed: %v\n%s", err, out)
|
|
}
|
|
if !exists(skill) || !exists(cmd) || !exists(agent) {
|
|
t.Fatal("without client labels every item must deploy")
|
|
}
|
|
|
|
// labels ["ops"]: dev items go away, ops item stays.
|
|
env.writeLabelConfig(t, `["ops"]`)
|
|
out, err := env.run(t)
|
|
if err != nil {
|
|
t.Fatalf("sync failed: %v\n%s", err, out)
|
|
}
|
|
if !strings.Contains(out, "labels: ops") || !strings.Contains(out, "2 item(s) not selected") {
|
|
t.Errorf("sync must report the active selection, got:\n%s", out)
|
|
}
|
|
if exists(skill) || exists(agent) {
|
|
t.Error("deselected unmodified items must be removed")
|
|
}
|
|
if !exists(cmd) {
|
|
t.Error("selected item must stay")
|
|
}
|
|
|
|
// Second identical run is a no-op.
|
|
out, err = env.run(t)
|
|
if err != nil {
|
|
t.Fatalf("sync failed: %v\n%s", err, out)
|
|
}
|
|
if !strings.Contains(out, "deployed: 0") || !strings.Contains(out, "removed: 0") || !strings.Contains(out, "updated: 0") {
|
|
t.Errorf("second run must change nothing, got:\n%s", out)
|
|
}
|
|
|
|
// [] = unlabeled items only: everything here is labeled.
|
|
env.writeLabelConfig(t, `[]`)
|
|
if out, err := env.run(t); err != nil {
|
|
t.Fatalf("sync failed: %v\n%s", err, out)
|
|
}
|
|
if exists(cmd) {
|
|
t.Error("labels [] must deliver unlabeled items only")
|
|
}
|
|
}
|
|
|
|
func TestSyncLabelsKeepModifiedCopy(t *testing.T) {
|
|
h := newHarness(t)
|
|
env := newSyncEnv(t, h)
|
|
buildWorkRepo(t, h, env.workRepo)
|
|
commitLabels(t, h, env.workRepo, `{"version":1,"labels":{"dev":["helper"]}}`)
|
|
|
|
env.writeLabelConfig(t, `["dev"]`)
|
|
if out, err := env.run(t); err != nil {
|
|
t.Fatalf("sync failed: %v\n%s", err, out)
|
|
}
|
|
agent := filepath.Join(env.home, ".config/opencode/agents/helper.md")
|
|
if err := os.WriteFile(agent, []byte("my edit\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
env.writeLabelConfig(t, `["ops"]`)
|
|
if out, err := env.run(t); err != nil {
|
|
t.Fatalf("sync failed: %v\n%s", err, out)
|
|
}
|
|
if !exists(agent) {
|
|
t.Fatal("a locally modified copy must survive deselection")
|
|
}
|
|
log, _ := os.ReadFile(filepath.Join(env.stateDir, "sync.log"))
|
|
if !strings.Contains(string(log), "not selected for this client's labels") {
|
|
t.Errorf("sync log must explain the label-based skip, got:\n%s", log)
|
|
}
|
|
|
|
if out, err := env.run(t, "--force"); err != nil {
|
|
t.Fatalf("sync --force failed: %v\n%s", err, out)
|
|
}
|
|
if exists(agent) {
|
|
t.Error("--force must remove the deselected modified copy")
|
|
}
|
|
}
|
|
|
|
func TestSyncInvalidLabelsFileAbortsWithoutChange(t *testing.T) {
|
|
h := newHarness(t)
|
|
env := newSyncEnv(t, h)
|
|
buildWorkRepo(t, h, env.workRepo)
|
|
env.writeLabelConfig(t, `["ops"]`)
|
|
if out, err := env.run(t); err != nil {
|
|
t.Fatalf("sync failed: %v\n%s", err, out)
|
|
}
|
|
manifestBefore, _ := os.ReadFile(filepath.Join(env.stateDir, "manifest.json"))
|
|
|
|
commitLabels(t, h, env.workRepo, `{"version":1,"labels":{"Ops":["deploy"]}}`)
|
|
out, err := env.run(t)
|
|
if err == nil {
|
|
t.Fatalf("an invalid labels file must fail the sync, got:\n%s", out)
|
|
}
|
|
if !strings.Contains(out, "agent-lib.labels.json") || !strings.Contains(out, "nothing changed") {
|
|
t.Errorf("error must name the file and promise no change, got:\n%s", out)
|
|
}
|
|
manifestAfter, _ := os.ReadFile(filepath.Join(env.stateDir, "manifest.json"))
|
|
if string(manifestBefore) != string(manifestAfter) {
|
|
t.Error("an aborted sync must not touch the manifest")
|
|
}
|
|
if !exists(filepath.Join(env.home, ".config/opencode/commands/deploy.md")) {
|
|
t.Error("an aborted sync must not remove anything")
|
|
}
|
|
}
|
|
|
|
func TestSyncLabelsUnknownNameWarns(t *testing.T) {
|
|
h := newHarness(t)
|
|
env := newSyncEnv(t, h)
|
|
buildWorkRepo(t, h, env.workRepo)
|
|
commitLabels(t, h, env.workRepo, `{"version":1,"labels":{"ops":["deploy","no-such-item"]}}`)
|
|
env.writeLabelConfig(t, `["ops"]`)
|
|
out, err := env.run(t)
|
|
if err != nil {
|
|
t.Fatalf("an unknown name must not fail the client sync: %v\n%s", err, out)
|
|
}
|
|
if !strings.Contains(out, `"no-such-item"`) {
|
|
t.Errorf("sync must warn about the unknown name, got:\n%s", out)
|
|
}
|
|
}
|