fix(plugins): bound legacy fallback to upgrades

This commit is contained in:
ayattara
2026-09-08 07:19:50 +00:00
parent 61c6bdfb91
commit b08e1d99d9
9 changed files with 109 additions and 39 deletions
+13 -1
View File
@@ -161,12 +161,24 @@ func TestUpdateConfigPreservesLegacyUtilityAgentForDirectProfileSchema(t *testin
}
merged = preserveLegacyUtilityAgentConfig(merged, map[string]any{
utilityAgentConfigKey: "utility-agent-1",
}, schema)
}, schema, true)
if merged[utilityAgentConfigKey] != "utility-agent-1" {
t.Fatalf("legacy selector = %v, want utility-agent-1", merged[utilityAgentConfigKey])
}
}
func TestPreserveLegacyUtilityAgentConfigRequiresUpgradeMarker(t *testing.T) {
schema := map[string]any{"properties": map[string]any{
agentProfileConfigKey: map[string]any{"type": "string", "format": "agent-profile"},
}}
merged := preserveLegacyUtilityAgentConfig(map[string]any{}, map[string]any{
utilityAgentConfigKey: "utility-agent-1",
}, schema, false)
if _, found := merged[utilityAgentConfigKey]; found {
t.Fatalf("preserveLegacyUtilityAgentConfig() retained undeclared selector: %v", merged)
}
}
func TestValidateConfigSchema(t *testing.T) {
schema := testConfigSchema()
cases := []struct {
+4
View File
@@ -49,6 +49,10 @@ type pluginHost struct {
// GetConfig to know which fields are secret (and therefore stored as
// vault references to resolve back to cleartext).
configSchema map[string]any
// legacyUtilityAgentFallback is persisted only for a verified manifest
// upgrade from the legacy selector. It prevents arbitrary undeclared config
// fields from selecting a utility agent.
legacyUtilityAgentFallback bool
state *state.Store
secrets SecretVault
@@ -94,11 +94,10 @@ func (h *pluginHost) InvokeUtilityAgent(ctx context.Context, prompt string) (str
return h.invokeConfiguredAgentProfile(ctx, profileID, profiles, runner, prompt)
}
}
// A direct-profile schema may belong to a plugin upgraded from the legacy
// utility-agent schema. Keep reading the persisted legacy key even though
// the new settings form no longer renders it; plugin config replacement does
// not migrate or delete unknown keys.
if hasUtilityAgentConfig(h.configSchema) || hasAgentProfileConfig(h.configSchema) {
// A verified manifest upgrade can retain its legacy selector even though the
// replacement schema no longer renders it. Do not treat arbitrary
// undeclared config as an agent selector.
if hasUtilityAgentConfig(h.configSchema) || h.legacyUtilityAgentFallback {
agentID, _ := config[utilityAgentConfigKey].(string)
if agentID != "" {
return h.invokeConfiguredUtilityAgent(ctx, agentID, agents, runner, prompt)
@@ -109,13 +109,14 @@ func TestPluginHost_InvokeUtilityAgent_UsesConfiguredAgentProfileBeforeUtilityAg
}
}
func TestPluginHost_InvokeUtilityAgent_FallsBackToUndeclaredLegacyUtilityAgentConfig(t *testing.T) {
func TestPluginHost_InvokeUtilityAgent_FallsBackToRetainedLegacyUtilityAgentConfig(t *testing.T) {
d := configuredUtilityHost(t)
d.host.configSchema = map[string]any{
"properties": map[string]any{
agentProfileConfigKey: map[string]any{"type": "string", "format": "agent-profile"},
},
}
d.host.legacyUtilityAgentFallback = true
got, err := d.host.InvokeUtilityAgent(context.Background(), "summarize yesterday")
if err != nil || got != "the summary" {
@@ -129,6 +130,23 @@ func TestPluginHost_InvokeUtilityAgent_FallsBackToUndeclaredLegacyUtilityAgentCo
}
}
func TestPluginHost_InvokeUtilityAgent_RejectsUndeclaredLegacyUtilityAgentConfig(t *testing.T) {
d := configuredUtilityHost(t)
d.host.configSchema = map[string]any{
"properties": map[string]any{
agentProfileConfigKey: map[string]any{"type": "string", "format": "agent-profile"},
},
}
_, err := d.host.InvokeUtilityAgent(context.Background(), "summarize yesterday")
if status.Code(err) != codes.FailedPrecondition || status.Convert(err).Message() != "no agent profile configured for this plugin" {
t.Fatalf("InvokeUtilityAgent() error = %v, want missing direct profile", err)
}
if d.utilAgents.calls != 0 || d.utilRun.calls != 0 {
t.Fatalf("undeclared legacy config touched utility lookup %d times and runner %d times", d.utilAgents.calls, d.utilRun.calls)
}
}
func TestPluginHost_InvokeUtilityAgent_FallsBackToLegacyUtilityAgentWhenDirectProfileIsUnset(t *testing.T) {
d := configuredUtilityHost(t)
d.host.configSchema = map[string]any{
+21 -20
View File
@@ -724,26 +724,27 @@ func (s *Service) hostForPlugin(pluginID string) pluginsdk.Host {
rec = &store.Record{} // every capability check below denies; should not happen in practice
}
return &pluginHost{
pluginID: pluginID,
capabilities: rec.Capabilities,
repositoryProviders: rec.RepositoryProviders,
configSchema: rec.ConfigSchema,
state: s.state,
secrets: s.secrets,
bus: s.eventBus,
configs: s.store,
taskData: s.taskData,
workflows: s.workflows,
workflowSteps: s.workflowSteps,
agentProfiles: s.agentProfiles,
sessionCodeStats: s.sessionCodeStats,
messageData: s.messageData,
interactionData: s.interactionData,
taskPRsDep: s.taskPRSourceDep,
taskWriter: s.taskWriter,
utilityDeps: s.utilityAgentDeps,
writeDeps: s.writeDependencies,
interactionDeps: s.interactionResponderDep,
pluginID: pluginID,
capabilities: rec.Capabilities,
repositoryProviders: rec.RepositoryProviders,
configSchema: rec.ConfigSchema,
legacyUtilityAgentFallback: rec.LegacyUtilityAgentFallback,
state: s.state,
secrets: s.secrets,
bus: s.eventBus,
configs: s.store,
taskData: s.taskData,
workflows: s.workflows,
workflowSteps: s.workflowSteps,
agentProfiles: s.agentProfiles,
sessionCodeStats: s.sessionCodeStats,
messageData: s.messageData,
interactionData: s.interactionData,
taskPRsDep: s.taskPRSourceDep,
taskWriter: s.taskWriter,
utilityDeps: s.utilityAgentDeps,
writeDeps: s.writeDependencies,
interactionDeps: s.interactionResponderDep,
}
}
@@ -36,11 +36,11 @@ func (s *Service) UpdateConfig(ctx context.Context, id string, config map[string
return err
}
merged := mergeMaskedSecrets(config, existing, rec.ConfigSchema)
// Direct-profile plugins can be upgraded from the legacy utility-agent
// selector. Keep that selector across the full-replacement settings write
// when the new form does not submit it, so host-side compatibility remains
// durable after the operator saves the new settings.
merged = preserveLegacyUtilityAgentConfig(merged, existing, rec.ConfigSchema)
// A direct-profile plugin upgraded from the legacy utility-agent selector
// carries a persisted provenance marker. Keep that selector across the
// full-replacement settings write when the new form does not submit it, so
// host-side compatibility remains durable after the operator saves.
merged = preserveLegacyUtilityAgentConfig(merged, existing, rec.ConfigSchema, rec.LegacyUtilityAgentFallback)
if err := validateConfigSchema(rec.ID, merged, rec.ConfigSchema); err != nil {
return err
}
@@ -69,8 +69,8 @@ func (s *Service) UpdateConfig(ctx context.Context, id string, config map[string
return s.restartForConfigChange(rec)
}
func preserveLegacyUtilityAgentConfig(config, existing, schema map[string]any) map[string]any {
if !hasAgentProfileConfig(schema) {
func preserveLegacyUtilityAgentConfig(config, existing, schema map[string]any, allowLegacyFallback bool) map[string]any {
if !allowLegacyFallback || !hasAgentProfileConfig(schema) {
return config
}
if _, submitted := config[utilityAgentConfigKey]; submitted {
@@ -116,6 +116,7 @@ func (s *Service) Install(ctx context.Context, r io.Reader) (*store.Record, erro
if hadOldRec {
rec.AutoUpdate = oldRec.AutoUpdate
}
rec.LegacyUtilityAgentFallback = legacyUtilityAgentFallback(oldRec, hadOldRec, rec.ConfigSchema)
if err := s.store.Save(rec); err != nil {
s.rollbackFailedInstall(result.InstallPath, oldRec, hadOldRec && wasRunning)
return nil, fmt.Errorf("plugins: persist installed record: %w", err)
@@ -143,6 +144,17 @@ func (s *Service) Install(ctx context.Context, r io.Reader) (*store.Record, erro
return installed, activateErr
}
// legacyUtilityAgentFallback retains an existing selector only across the
// manifest transition that removed its declaration. A direct-profile plugin
// may otherwise contain arbitrary undeclared settings, which must not choose
// an agent at execution time.
func legacyUtilityAgentFallback(oldRec *store.Record, hadOldRec bool, newSchema map[string]any) bool {
if !hadOldRec || oldRec == nil || !hasAgentProfileConfig(newSchema) || hasUtilityAgentConfig(newSchema) {
return false
}
return oldRec.LegacyUtilityAgentFallback || hasUtilityAgentConfig(oldRec.ConfigSchema)
}
// extractPackage runs pkgtar.Install and registers the extracted version
// directory as an in-flight install, both under extractingMu so the directory
// is never visible on disk without being marked. Install cannot take the
@@ -11,6 +11,7 @@ import (
"sync"
"testing"
"github.com/kandev/kandev/internal/plugins/manifest"
"github.com/kandev/kandev/internal/plugins/pkgtar"
"github.com/kandev/kandev/internal/plugins/pkgtar/pkgtartest"
"github.com/kandev/kandev/internal/plugins/store"
@@ -46,6 +47,24 @@ func TestServiceInstallDuplicateVersionReturnsErrVersionExists(t *testing.T) {
}
}
func TestLegacyUtilityAgentFallbackOnlyTracksManifestUpgrade(t *testing.T) {
legacySchema := map[string]any{"properties": map[string]any{
utilityAgentConfigKey: map[string]any{"type": "string", "format": "utility-agent"},
}}
directSchema := map[string]any{"properties": map[string]any{
agentProfileConfigKey: map[string]any{"type": "string", "format": "agent-profile"},
}}
if !legacyUtilityAgentFallback(&store.Record{Manifest: manifest.Manifest{ConfigSchema: legacySchema}}, true, directSchema) {
t.Fatal("legacy utility-agent manifest upgrade did not retain fallback")
}
if legacyUtilityAgentFallback(&store.Record{Manifest: manifest.Manifest{ConfigSchema: directSchema}}, true, directSchema) {
t.Fatal("direct-profile manifest unexpectedly retained fallback")
}
if legacyUtilityAgentFallback(nil, false, directSchema) {
t.Fatal("new direct-profile install unexpectedly retained fallback")
}
}
func TestServiceInstallSpawnFailureLeavesRecordInErrorStatus(t *testing.T) {
svc, fsStore, rt := newTestService(t)
rt.setStartErr("kandev-plugin-slack", errors.New("spawn failed"))
+10 -5
View File
@@ -48,11 +48,16 @@ type Record struct {
// and false forces it off — either override wins over the global default.
// Persisted so the choice survives restart and, unlike Status, is carried
// over verbatim when a plugin is upgraded in place (see Service.Install).
AutoUpdate *bool `yaml:"auto_update,omitempty" json:"auto_update,omitempty"`
InstalledAt time.Time `yaml:"installed_at" json:"installed_at"`
RestartCount int `yaml:"restart_count" json:"restart_count"`
LastError string `yaml:"last_error,omitempty" json:"last_error,omitempty"`
LastErrorAt *time.Time `yaml:"last_error_at,omitempty" json:"last_error_at,omitempty"`
AutoUpdate *bool `yaml:"auto_update,omitempty" json:"auto_update,omitempty"`
// LegacyUtilityAgentFallback is set only while upgrading a plugin from a
// declared utility-agent selector to a direct agent-profile selector. It
// authorizes the host to honor the retained legacy value after the new
// manifest no longer declares that field.
LegacyUtilityAgentFallback bool `yaml:"legacy_utility_agent_fallback,omitempty" json:"legacy_utility_agent_fallback,omitempty"`
InstalledAt time.Time `yaml:"installed_at" json:"installed_at"`
RestartCount int `yaml:"restart_count" json:"restart_count"`
LastError string `yaml:"last_error,omitempty" json:"last_error,omitempty"`
LastErrorAt *time.Time `yaml:"last_error_at,omitempty" json:"last_error_at,omitempty"`
}
// Store is the persistence interface for plugin installations and their