Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions internal/registry/controller/plugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/agentregistry-dev/agentregistry/internal/cli/common/gitutil"
"github.com/agentregistry-dev/agentregistry/internal/registry/plugins/bundle"
"github.com/agentregistry-dev/agentregistry/internal/registry/plugins/format"
"github.com/agentregistry-dev/agentregistry/internal/registry/plugins/source"
"github.com/agentregistry-dev/agentregistry/pkg/api/v1alpha1"
pkgdb "github.com/agentregistry-dev/agentregistry/pkg/registry/database"
Expand Down Expand Up @@ -266,14 +267,12 @@ func (c *PluginController) enqueueAll(ctx context.Context) error {

const pluginReadyCondition = "Ready"

// pluginReconciled reports whether the controller has already acted on the
// plugin's current generation. It gates on ObservedGeneration ALONE (not
// Ready), because both success and terminal failure advance ObservedGeneration
// — a terminally-failed plugin must NOT be re-resolved on every resync tick.
// Retryable failures intentionally leave ObservedGeneration behind so they are
// re-enqueued (and the workqueue rate-limiter backs them off).
// pluginReconciled reports whether the current generation was scanned with
// the current rules. Retryable failures record neither, so they retry. Any
// other scan version rescans, including a newer one after a rollback.
func pluginReconciled(p *v1alpha1.Plugin) bool {
return p.Metadata.Generation > 0 && p.Status.ObservedGeneration >= p.Metadata.Generation
return p.Metadata.Generation > 0 && p.Status.ObservedGeneration >= p.Metadata.Generation &&
p.Status.ScanVersion == v1alpha1.PluginScanVersion
}

func (c *PluginController) reconcileKey(ctx context.Context, key pluginQueueKey) (outcome, message string, err error) {
Expand Down Expand Up @@ -320,33 +319,49 @@ func (c *PluginController) reconcile(ctx context.Context, p *v1alpha1.Plugin) (s
resolved, b, err := source.Resolve(ctx, p, c.Git)
if err != nil {
reason, terminal := classifyResolveErr(err)
bump := int64(0)
if terminal {
bump = gen
return "failed", reason, c.patchStatus(ctx, ns, name, tag, gen, terminalStatus(reason, err))
}
patchErr := c.patchStatus(ctx, ns, name, tag, bump, func(st *v1alpha1.PluginStatus) {
_ = c.patchStatus(ctx, ns, name, tag, 0, func(st *v1alpha1.PluginStatus) {
setReady(st, v1alpha1.ConditionFalse, reason, err.Error())
})
if terminal {
return "failed", reason, patchErr
}
return "", "", err // retryable
}

manifest, err := bundle.ParseManifest(b)
resolvedPatch, err := scanStatus(resolved, b)
if err != nil {
return "failed", "SourceInvalid", c.patchStatus(ctx, ns, name, tag, gen, func(st *v1alpha1.PluginStatus) {
setReady(st, v1alpha1.ConditionFalse, "SourceInvalid", err.Error())
})
return "failed", "SourceInvalid", c.patchStatus(ctx, ns, name, tag, gen, terminalStatus("SourceInvalid", err))
}
inventory := bundle.BuildInventory(b)
return "resolved", "", c.patchStatus(ctx, ns, name, tag, gen, resolvedPatch)
}

return "resolved", "", c.patchStatus(ctx, ns, name, tag, gen, func(st *v1alpha1.PluginStatus) {
st.ResolvedSource = resolved
st.Manifest = manifest
st.Inventory = inventory
// scanStatus scans a resolved bundle and returns the Ready=True status to
// record. Errors wrap bundle.ErrInvalidBundle.
func scanStatus(resolved *v1alpha1.PluginResolvedSource, b *bundle.CanonicalBundle) (func(*v1alpha1.PluginStatus), error) {
pluginFormat, manifestPath, err := format.Detect(b)
if err != nil {
return nil, err
}
manifest, err := bundle.ParseManifest(b, manifestPath)
if err != nil {
return nil, err
}
inventory := bundle.BuildInventory(b)
return func(st *v1alpha1.PluginStatus) {
st.ResolvedSource, st.Manifest, st.Inventory = resolved, manifest, inventory
st.Format, st.ScanVersion = pluginFormat, v1alpha1.PluginScanVersion
setReady(st, v1alpha1.ConditionTrue, "Resolved", "")
})
}, nil
}

// terminalStatus records a terminal failure at the current scan version. It
// keeps ResolvedSource, because Deployment fingerprints read it.
func terminalStatus(reason string, err error) func(*v1alpha1.PluginStatus) {
return func(st *v1alpha1.PluginStatus) {
st.Manifest, st.Inventory = nil, nil
st.Format, st.ScanVersion = "", v1alpha1.PluginScanVersion
setReady(st, v1alpha1.ConditionFalse, reason, err.Error())
}
}

// classifyResolveErr maps a resolver error to a status reason and whether it is
Expand Down
106 changes: 91 additions & 15 deletions internal/registry/controller/plugin_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package controller
import (
"errors"
"fmt"
"reflect"
"testing"
"time"

"github.com/agentregistry-dev/agentregistry/internal/registry/plugins/bundle"
"github.com/agentregistry-dev/agentregistry/internal/registry/plugins/source"
Expand Down Expand Up @@ -31,26 +33,100 @@ func TestClassifyResolveErr(t *testing.T) {
}
}

// TestPluginReconciled checks the gate on ObservedGeneration and ScanVersion.
// Ready true/false is irrelevant: a terminal failure must NOT re-resolve every tick.
func TestPluginReconciled(t *testing.T) {
plugin := func(observed, gen int64, ready v1alpha1.ConditionStatus) *v1alpha1.Plugin {
p := &v1alpha1.Plugin{}
p.Metadata.Generation = gen
p.Status.ObservedGeneration = observed
p.Status.SetCondition(v1alpha1.Condition{Type: pluginReadyCondition, Status: ready, Reason: "x"})
return p
const current = v1alpha1.PluginScanVersion
tests := []struct {
name string
observed, generation, scanVersion int64
ready v1alpha1.ConditionStatus
want bool
}{
{"success at current generation", 3, 3, current, v1alpha1.ConditionTrue, true},
{"terminal failure at current generation", 3, 3, current, v1alpha1.ConditionFalse, true},
{"retryable or pending", 2, 3, current, v1alpha1.ConditionFalse, false},
{"fresh plugin with generation 0", 0, 0, 0, "", false},
{"older scan version rescans", 3, 3, current - 1, v1alpha1.ConditionTrue, false},
{"newer scan version after a rollback rescans", 3, 3, current + 1, v1alpha1.ConditionTrue, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &v1alpha1.Plugin{}
p.Metadata.Generation = tt.generation
p.Status.ObservedGeneration = tt.observed
p.Status.ScanVersion = tt.scanVersion
p.Status.SetCondition(v1alpha1.Condition{Type: pluginReadyCondition, Status: tt.ready, Reason: "x"})
if got := pluginReconciled(p); got != tt.want {
t.Fatalf("pluginReconciled = %v, want %v", got, tt.want)
}
})
}
}

// Gates on ObservedGeneration only; Ready true/false is irrelevant.
if !pluginReconciled(plugin(3, 3, v1alpha1.ConditionTrue)) {
t.Fatal("observed==gen (success) should be reconciled")
func TestScanStatus(t *testing.T) {
resolved := &v1alpha1.PluginResolvedSource{Type: v1alpha1.PluginSourceTypeGit, Commit: "abc123"}
agentPlugins := &bundle.CanonicalBundle{Files: map[string][]byte{
"plugin.json": []byte(`{"$schema":"https://agent-plugins.org/schemas/1.0.0/plugin.schema.json","name":"acme.test","version":"1.0.0"}`),
"mcp.json": []byte(`{"$schema":"https://agent-plugins.org/schemas/1.0.0/mcp.schema.json","mcpServers":{"search":{"type":"streamable-http","url":"https://example.com/mcp"}}}`),
".mcp.json": []byte(`{"mcpServers":{"claude-only":{"url":"https://example.com/mcp"}}}`),
"skills/review/SKILL.md": []byte("---\nname: review\n---\n"),
}}
mutate, err := scanStatus(resolved, agentPlugins)
if err != nil {
t.Fatalf("scanStatus: %v", err)
}
if !pluginReconciled(plugin(3, 3, v1alpha1.ConditionFalse)) {
t.Fatal("observed==gen (terminal failure) should be reconciled — must NOT re-resolve every tick")
got := v1alpha1.PluginStatus{Format: v1alpha1.PluginFormatClaudePlugin}
mutate(&got)
want := v1alpha1.PluginStatus{
ResolvedSource: resolved,
Manifest: &v1alpha1.PluginManifest{Schema: "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", Name: "acme.test", Version: "1.0.0"},
Inventory: &v1alpha1.PluginInventory{Skills: []v1alpha1.PluginSkill{{Name: "review"}}, MCPServers: []string{"claude-only"}},
Format: v1alpha1.PluginFormatAgentPlugins,
ScanVersion: v1alpha1.PluginScanVersion,
}
if pluginReconciled(plugin(2, 3, v1alpha1.ConditionFalse)) {
t.Fatal("observed<gen (retryable / pending) should NOT be reconciled")
want.SetCondition(v1alpha1.Condition{Type: pluginReadyCondition, Status: v1alpha1.ConditionTrue, Reason: "Resolved"})
assertStatusEqual(t, want, got)

if _, err := scanStatus(resolved, &bundle.CanonicalBundle{Files: map[string][]byte{"SKILL.md": []byte("x")}}); !errors.Is(err, bundle.ErrInvalidBundle) {
t.Errorf("rules reject: err = %v, want ErrInvalidBundle", err)
}
// A key the format rules do not check must not fail the scan.
ignoredKey := &bundle.CanonicalBundle{Files: map[string][]byte{bundle.ManifestPath: []byte(`{"name":"a","hooks":5}`)}}
if _, err := scanStatus(resolved, ignoredKey); err != nil {
t.Errorf("unparsable ignored key: err = %v, want nil", err)
}
}

// TestTerminalStatus checks that a terminal failure records the scan version,
// clears the scan results, and keeps the resolved source.
func TestTerminalStatus(t *testing.T) {
resolved := &v1alpha1.PluginResolvedSource{Type: v1alpha1.PluginSourceTypeGit, Commit: "abc123"}
got := v1alpha1.PluginStatus{
ResolvedSource: resolved,
Manifest: &v1alpha1.PluginManifest{Name: "old"},
Inventory: &v1alpha1.PluginInventory{MCPServers: []string{"old"}},
Format: v1alpha1.PluginFormatClaudePlugin,
}
got.ObservedGeneration = 3
got.SetCondition(v1alpha1.Condition{Type: pluginReadyCondition, Status: v1alpha1.ConditionTrue, Reason: "Resolved"})
terminalStatus("SourceInvalid", errors.New("no manifest"))(&got)

want := v1alpha1.PluginStatus{ResolvedSource: resolved, ScanVersion: v1alpha1.PluginScanVersion}
want.ObservedGeneration = 3
want.SetCondition(v1alpha1.Condition{Type: pluginReadyCondition, Status: v1alpha1.ConditionFalse, Reason: "SourceInvalid", Message: "no manifest"})
assertStatusEqual(t, want, got)
}

// assertStatusEqual compares whole statuses, ignoring condition timestamps.
func assertStatusEqual(t *testing.T, want, got v1alpha1.PluginStatus) {
t.Helper()
for _, st := range []*v1alpha1.PluginStatus{&want, &got} {
for i := range st.Conditions {
st.Conditions[i].LastTransitionTime = time.Time{}
}
}
if pluginReconciled(&v1alpha1.Plugin{}) {
t.Fatal("a fresh plugin (generation 0 in this zero value) should NOT be reconciled")
if !reflect.DeepEqual(got, want) {
t.Fatalf("status = %+v, want %+v", got, want)
}
}
41 changes: 39 additions & 2 deletions internal/registry/controller/plugin_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"encoding/json"
"fmt"
"reflect"
"testing"

Expand Down Expand Up @@ -92,6 +93,36 @@ func TestEnqueueAllSkipsUndecodableRow(t *testing.T) {
}
}

// TestEnqueueAllRescansStaleScanVersion guards the rescan gate: a plugin whose
// generation is caught up but whose scan version is stale must be enqueued.
func TestEnqueueAllRescansStaleScanVersion(t *testing.T) {
rawOf := func(name, status string) *v1alpha1.RawObject {
return &v1alpha1.RawObject{
TypeMeta: v1alpha1.TypeMeta{APIVersion: v1alpha1.GroupVersion, Kind: v1alpha1.KindPlugin},
Metadata: v1alpha1.ObjectMeta{Namespace: "default", Name: name, Tag: "v1", Generation: 2},
Spec: json.RawMessage(`{"source":{"type":"git"}}`),
Status: json.RawMessage(status),
}
}
store := newFakePluginStore()
store.listRows = []*v1alpha1.RawObject{
rawOf("stale", `{"observedGeneration":2}`),
rawOf("current", fmt.Sprintf(`{"observedGeneration":2,"scanVersion":%d}`, v1alpha1.PluginScanVersion)),
}
c := &PluginController{Store: store}

if err := c.enqueueAll(context.Background()); err != nil {
t.Fatalf("enqueueAll: %v", err)
}
queue := c.workQueue()
if n := queue.Len(); n != 1 {
t.Fatalf("expected only the stale plugin enqueued, queue len = %d", n)
}
if key, _ := queue.Get(); key.Name != "stale" {
t.Fatalf("enqueued %q, want stale", key.Name)
}
}

// TestPluginReconcile drives reconcile against a real (anonymous) git source.
// Every case is chosen to fail before any network call: an unsupported source
// type never reaches git, a non-GitHub host is rejected at URL parse, and an
Expand Down Expand Up @@ -127,6 +158,9 @@ func TestPluginReconcile(t *testing.T) {
if got.Status.ObservedGeneration != 3 {
t.Errorf("terminal must bump observedGeneration, got %d", got.Status.ObservedGeneration)
}
if got.Status.ScanVersion != v1alpha1.PluginScanVersion {
t.Errorf("terminal must record the scan version, got %d", got.Status.ScanVersion)
}
if got.Status.IsConditionTrue(pluginReadyCondition) {
t.Error("must not be Ready")
}
Expand All @@ -144,6 +178,9 @@ func TestPluginReconcile(t *testing.T) {
if got.Status.ObservedGeneration != 0 {
t.Errorf("retryable must NOT bump observedGeneration, got %d", got.Status.ObservedGeneration)
}
if got.Status.ScanVersion != 0 {
t.Errorf("retryable must NOT record a scan version, got %d", got.Status.ScanVersion)
}
if r := readyReason(got); r != "SourceUnresolvable" {
t.Errorf("ready reason = %q, want SourceUnresolvable", r)
}
Expand All @@ -164,8 +201,8 @@ func TestPluginReconcile(t *testing.T) {
if outcome != "failed" || reason != "SourceUnsupported" {
t.Fatalf("got (%q, %q), want (failed, SourceUnsupported)", outcome, reason)
}
if got := store.plugin(t, ns, name, tag); got.Status.ObservedGeneration != 5 {
t.Errorf("observedGeneration = %d, want 5", got.Status.ObservedGeneration)
if got := store.plugin(t, ns, name, tag); got.Status.ObservedGeneration != 5 || got.Status.ScanVersion != v1alpha1.PluginScanVersion {
t.Errorf("observedGeneration, scanVersion = %d, %d, want 5, %d", got.Status.ObservedGeneration, got.Status.ScanVersion, v1alpha1.PluginScanVersion)
}
})
}
Loading
Loading