diff --git a/apps/backend/internal/backendapp/services.go b/apps/backend/internal/backendapp/services.go index cdf793d83d..37b4ab9a88 100644 --- a/apps/backend/internal/backendapp/services.go +++ b/apps/backend/internal/backendapp/services.go @@ -1093,12 +1093,22 @@ func initPluginsService( return svc } +type pluginHostUtilityManager interface { + ExecuteProfilePrompt(ctx context.Context, profileID, prompt string) (*hostutility.PromptResult, error) +} + type pluginsHostUtilityAdapter struct { - mgr *hostutility.Manager + mgr pluginHostUtilityManager } func (a pluginsHostUtilityAdapter) ExecuteProfilePrompt(ctx context.Context, profileID, prompt string) (string, error) { res, err := a.mgr.ExecuteProfilePrompt(ctx, profileID, prompt) + if errors.Is(err, profilebinding.ErrProfileNotFound) { + return "", plugins.ErrAgentProfileNotFound + } + if errors.Is(err, profilebinding.ErrProfileIneligible) { + return "", plugins.ErrAgentProfileIneligible + } if err != nil { return "", err } diff --git a/apps/backend/internal/backendapp/services_plugin_utility_test.go b/apps/backend/internal/backendapp/services_plugin_utility_test.go index ed7383f156..b70855405a 100644 --- a/apps/backend/internal/backendapp/services_plugin_utility_test.go +++ b/apps/backend/internal/backendapp/services_plugin_utility_test.go @@ -6,6 +6,7 @@ import ( "errors" "testing" + "github.com/kandev/kandev/internal/agent/hostutility" agentsettingsmodels "github.com/kandev/kandev/internal/agent/settings/models" "github.com/kandev/kandev/internal/common/logger" "github.com/kandev/kandev/internal/events/bus" @@ -19,6 +20,14 @@ import ( utilitystore "github.com/kandev/kandev/internal/utility/store" ) +type pluginHostUtilityManagerStub struct { + err error +} + +func (s pluginHostUtilityManagerStub) ExecuteProfilePrompt(context.Context, string, string) (*hostutility.PromptResult, error) { + return nil, s.err +} + type pluginUtilityAgentRepository struct { utilitystore.Repository err error @@ -124,6 +133,15 @@ func TestPluginsAgentProfileAdapter_MapsIneligibleProfile(t *testing.T) { } } +func TestPluginsHostUtilityAdapter_MapsRevalidationIneligibleProfile(t *testing.T) { + adapter := pluginsHostUtilityAdapter{mgr: pluginHostUtilityManagerStub{err: profilebinding.ErrProfileIneligible}} + + _, err := adapter.ExecuteProfilePrompt(context.Background(), "profile-1", "prompt") + if !errors.Is(err, plugins.ErrAgentProfileIneligible) { + t.Fatalf("ExecuteProfilePrompt() error = %v, want plugin ineligible error", err) + } +} + type utilityAgentRepositoryStub struct { utilitystore.Repository agent *utilitymodels.UtilityAgent diff --git a/apps/backend/internal/plugins/host_utility.go b/apps/backend/internal/plugins/host_utility.go index 2a51f83b90..464aa6ba02 100644 --- a/apps/backend/internal/plugins/host_utility.go +++ b/apps/backend/internal/plugins/host_utility.go @@ -167,7 +167,14 @@ func (h *pluginHost) invokeConfiguredAgentProfile( if !profile.Enabled || profile.CLIPassthrough || profile.WorkspaceID != "" || !profile.InferenceCapable { return "", status.Errorf(codes.FailedPrecondition, "configured agent profile %q is not eligible for utility execution", profileID) } - return runner.ExecuteProfilePrompt(ctx, profileID, prompt) + response, err := runner.ExecuteProfilePrompt(ctx, profileID, prompt) + if errors.Is(err, ErrAgentProfileNotFound) { + return "", status.Errorf(codes.FailedPrecondition, "configured agent profile %q not found", profileID) + } + if errors.Is(err, ErrAgentProfileIneligible) { + return "", status.Errorf(codes.FailedPrecondition, "configured agent profile %q is not eligible for utility execution", profileID) + } + return response, err } func hasAgentProfileConfig(schema map[string]any) bool { diff --git a/apps/backend/internal/plugins/host_utility_test.go b/apps/backend/internal/plugins/host_utility_test.go index ad544d840f..ae694fe601 100644 --- a/apps/backend/internal/plugins/host_utility_test.go +++ b/apps/backend/internal/plugins/host_utility_test.go @@ -272,6 +272,42 @@ func TestPluginHost_InvokeUtilityAgent_PreservesDirectProfileRunnerFailure(t *te } } +func TestPluginHost_InvokeUtilityAgent_MapsDirectProfileRevalidationFailure(t *testing.T) { + for _, tc := range []struct { + name string + err error + want string + }{ + { + name: "deleted", + err: ErrAgentProfileNotFound, + want: `configured agent profile "profile-99" not found`, + }, + { + name: "ineligible", + err: ErrAgentProfileIneligible, + want: `configured agent profile "profile-99" is not eligible for utility execution`, + }, + } { + t.Run(tc.name, func(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.configs = &fakeConfigReader{configs: map[string]any{agentProfileConfigKey: "profile-99"}} + d.profiles.profilesByID = map[string]*AgentProfile{"profile-99": {Enabled: true, InferenceCapable: true}} + d.utilRun.err = tc.err + + _, err := d.host.InvokeUtilityAgent(context.Background(), "summarize") + if status.Code(err) != codes.FailedPrecondition || status.Convert(err).Message() != tc.want { + t.Fatalf("InvokeUtilityAgent() error = %v, want FailedPrecondition %q", err, tc.want) + } + }) + } +} + func TestPluginHost_InvokeUtilityAgent_DirectProfile_PreservesStoreFailure(t *testing.T) { storeErr := status.Error(codes.Unavailable, "agent profile store unavailable") d := configuredUtilityHost(t) diff --git a/apps/web/hooks/domains/settings/use-settings-data.test.tsx b/apps/web/hooks/domains/settings/use-settings-data.test.tsx index 0241720d23..b0e6222423 100644 --- a/apps/web/hooks/domains/settings/use-settings-data.test.tsx +++ b/apps/web/hooks/domains/settings/use-settings-data.test.tsx @@ -17,6 +17,8 @@ vi.mock("@/lib/api", () => ({ import { useSettingsData } from "./use-settings-data"; +const MOCK_AGENT_NAME = "Mock Agent"; + function wrapper({ children }: { children: ReactNode }) { return createElement(StateProvider, null, children); } @@ -25,7 +27,8 @@ function useSettingsSnapshot() { useSettingsData(); const agentsLoaded = useAppStore((state) => state.settingsData.agentsLoaded); const agentProfiles = useAppStore((state) => state.agentProfiles.items); - return { agentsLoaded, agentProfiles }; + const setAgentProfiles = useAppStore((state) => state.setAgentProfiles); + return { agentsLoaded, agentProfiles, setAgentProfiles }; } beforeEach(() => { @@ -44,12 +47,12 @@ describe("useSettingsData", () => { it("retries an empty agent list before declaring that no profiles exist", async () => { const profile = { id: "profile-1", - agentDisplayName: "Mock Agent", + agentDisplayName: MOCK_AGENT_NAME, name: "Default", cliPassthrough: false, }; mocks.listAgents.mockResolvedValueOnce({ agents: [], total: 0 }).mockResolvedValueOnce({ - agents: [{ id: "agent-1", name: "Mock Agent", profiles: [profile] }], + agents: [{ id: "agent-1", name: MOCK_AGENT_NAME, profiles: [profile] }], total: 1, }); @@ -79,4 +82,53 @@ describe("useSettingsData", () => { expect(result.current.agentsLoaded).toBe(true); expect(result.current.agentProfiles).toEqual([]); }); + + it("keeps a profile created by WebSocket while the initial list request is pending", async () => { + let resolveAgents: (response: { agents: unknown[]; total: number }) => void; + mocks.listAgents.mockReturnValue( + new Promise((resolve) => { + resolveAgents = resolve; + }), + ); + const { result } = renderHook(() => useSettingsSnapshot(), { wrapper }); + + await act(async () => { + await Promise.resolve(); + }); + act(() => { + result.current.setAgentProfiles([ + { + id: "profile-live", + label: `${MOCK_AGENT_NAME} • Live profile`, + agent_id: "agent-1", + agent_name: MOCK_AGENT_NAME, + cli_passthrough: false, + inference_capable: true, + updatedAt: "2026-08-26T18:00:01Z", + }, + ]); + }); + await act(async () => { + resolveAgents!({ + agents: [ + { + id: "agent-1", + name: MOCK_AGENT_NAME, + profiles: [ + { + id: "profile-stale", + agentDisplayName: MOCK_AGENT_NAME, + name: "Stale profile", + cliPassthrough: false, + }, + ], + }, + ], + total: 1, + }); + await Promise.resolve(); + }); + + expect(result.current.agentProfiles.map((profile) => profile.id)).toContain("profile-live"); + }); }); diff --git a/apps/web/hooks/domains/settings/use-settings-data.ts b/apps/web/hooks/domains/settings/use-settings-data.ts index 528d8cc431..222937f54a 100644 --- a/apps/web/hooks/domains/settings/use-settings-data.ts +++ b/apps/web/hooks/domains/settings/use-settings-data.ts @@ -1,7 +1,11 @@ import { useEffect, useRef } from "react"; -import { useAppStore } from "@/components/state-provider"; +import { useAppStore, useAppStoreApi } from "@/components/state-provider"; import { listAgents, listAvailableAgents, listExecutors } from "@/lib/api"; -import { toAgentProfileOption } from "@/lib/state/slices/settings/types"; +import { + mergeOptionsByNewest, + toAgentProfileOption, + type AgentProfileOption, +} from "@/lib/state/slices/settings/types"; const AGENT_LIST_RETRY_DELAYS_MS = [100, 250, 500, 1_000] as const; @@ -40,14 +44,14 @@ export async function listAgentsUntilSettled(): Promise { function applyAgentList( response: AgentListResponse, setSettingsAgents: (agents: AgentListResponse["agents"]) => void, - setAgentProfiles: (profiles: ReturnType[]) => void, + setAgentProfiles: (profiles: AgentProfileOption[]) => void, + previousProfiles: AgentProfileOption[], ): void { setSettingsAgents(response.agents); - setAgentProfiles( - response.agents.flatMap((agent) => - agent.profiles.map((profile) => toAgentProfileOption(agent, profile)), - ), + const rebuiltProfiles = response.agents.flatMap((agent) => + agent.profiles.map((profile) => toAgentProfileOption(agent, profile)), ); + setAgentProfiles(mergeOptionsByNewest(previousProfiles, rebuiltProfiles)); } export function useSettingsData(enabled = true) { @@ -61,6 +65,7 @@ export function useSettingsData(enabled = true) { const setAvailableAgents = useAppStore((state) => state.setAvailableAgents); const setAvailableAgentsLoading = useAppStore((state) => state.setAvailableAgentsLoading); const setSettingsData = useAppStore((state) => state.setSettingsData); + const storeApi = useAppStoreApi(); useEffect(() => { if (!enabled) return; @@ -80,7 +85,14 @@ export function useSettingsData(enabled = true) { if (settingsData.agentsLoaded) return; if (settingsAgents.length === 0) { listAgentsUntilSettled() - .then((response) => applyAgentList(response, setSettingsAgents, setAgentProfiles)) + .then((response) => + applyAgentList( + response, + setSettingsAgents, + setAgentProfiles, + storeApi.getState().agentProfiles.items, + ), + ) .catch(() => { setSettingsAgents([]); setAgentProfiles([]); @@ -131,7 +143,14 @@ export function useSettingsData(enabled = true) { if (reconciledRef.current) return; reconciledRef.current = true; listAgentsUntilSettled() - .then((response) => applyAgentList(response, setSettingsAgents, setAgentProfiles)) + .then((response) => + applyAgentList( + response, + setSettingsAgents, + setAgentProfiles, + storeApi.getState().agentProfiles.items, + ), + ) .catch(() => { // Best-effort reconcile; keep prior (possibly stale) profiles rather // than wiping the dialog state on a transient error. @@ -142,5 +161,6 @@ export function useSettingsData(enabled = true) { settingsData.agentsLoaded, setAgentProfiles, setSettingsAgents, + storeApi, ]); }