Skip to content

The open-source app everyone uses to manage agents at work

Fix agent detail URL after agent rename (#9340)

Devin Foleyd1f6a6850a3b
diff --git a/ui/src/pages/AgentDetail.progress.test.ts b/ui/src/pages/AgentDetail.progress.test.ts
index 03c62e3722..83c2623556 100644
--- a/ui/src/pages/AgentDetail.progress.test.ts
+++ b/ui/src/pages/AgentDetail.progress.test.ts
@@ -1,8 +1,11 @@
-import { describe, expect, it } from "vitest";
+import { QueryClient } from "@tanstack/react-query";
+import { describe, expect, it, vi } from "vitest";
+import { queryKeys } from "../lib/queryKeys";
import {
buildHeartbeatProgressLogLine,
heartbeatProgressLogLineKey,
+ syncAgentRouteAfterRename,
} from "./AgentDetail";
describe("buildHeartbeatProgressLogLine", () => {
@@ -59,3 +62,43 @@ describe("heartbeatProgressLogLineKey", () => {
);
});
});
+
+describe("syncAgentRouteAfterRename", () => {
+ it("replaces stale agent routes after a rename changes the URL key", () => {
+ const queryClient = new QueryClient();
+ const navigate = vi.fn();
+ queryClient.setQueryData(queryKeys.agents.detail("old-agent"), { id: "agent-1" });
+ queryClient.setQueryData(queryKeys.agents.detail("renamed-agent"), { id: "agent-1" });
+
+ const redirected = syncAgentRouteAfterRename(
+ queryClient,
+ navigate,
+ { id: "agent-1", name: "Old Agent", urlKey: "old-agent" },
+ { id: "agent-1", name: "Renamed Agent", urlKey: "renamed-agent" },
+ "configuration",
+ );
+
+ expect(redirected).toBe(true);
+ expect(navigate).toHaveBeenCalledWith("/agents/renamed-agent/configuration", { replace: true });
+ expect(queryClient.getQueryData(queryKeys.agents.detail("old-agent"))).toBeUndefined();
+ expect(queryClient.getQueryData(queryKeys.agents.detail("renamed-agent"))).toEqual({ id: "agent-1" });
+ });
+
+ it("does not redirect when the canonical route ref stays the same", () => {
+ const queryClient = new QueryClient();
+ const navigate = vi.fn();
+ queryClient.setQueryData(queryKeys.agents.detail("same-agent"), { id: "agent-1" });
+
+ const redirected = syncAgentRouteAfterRename(
+ queryClient,
+ navigate,
+ { id: "agent-1", name: "Same Agent", urlKey: "same-agent" },
+ { id: "agent-1", name: "Same Agent", urlKey: "same-agent" },
+ "configuration",
+ );
+
+ expect(redirected).toBe(false);
+ expect(navigate).not.toHaveBeenCalled();
+ expect(queryClient.getQueryData(queryKeys.agents.detail("same-agent"))).toEqual({ id: "agent-1" });
+ });
+});
diff --git a/ui/src/pages/AgentDetail.tsx b/ui/src/pages/AgentDetail.tsx
index 0a8720d52d..ab60cae756 100644
--- a/ui/src/pages/AgentDetail.tsx
+++ b/ui/src/pages/AgentDetail.tsx
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo, useState, useRef } from "react";
-import { useParams, useNavigate, Link, Navigate, useBeforeUnload } from "@/lib/router";
-import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
+import { useParams, useNavigate, Link, Navigate, useBeforeUnload, type NavigateFunction } from "@/lib/router";
+import { useQuery, useMutation, useQueryClient, type QueryClient } from "@tanstack/react-query";
import {
agentsApi,
type AgentKey,
@@ -1638,6 +1638,29 @@ function CostsSection({
/* ---- Agent Configure Page ---- */
+/**
+ * Agent detail URLs use a name-derived key, so updates that change the agent's
+ * name (a rename or a config-revision rollback) can invalidate the reference
+ * currently in the URL. When that happens, refetching the old reference would
+ * 404 with "Agent not found". Instead, drop the stale cached queries and
+ * replace the URL with the new canonical reference. Returns true when a
+ * redirect happened.
+ */
+export function syncAgentRouteAfterRename(
+ queryClient: QueryClient,
+ navigate: NavigateFunction,
+ previous: { id: string; urlKey?: string | null; name?: string | null },
+ updated: { id: string; urlKey?: string | null; name?: string | null },
+ tab: string,
+): boolean {
+ const previousRef = agentRouteRef(previous);
+ const nextRef = agentRouteRef(updated);
+ if (nextRef === previousRef) return false;
+ queryClient.removeQueries({ queryKey: queryKeys.agents.detail(previousRef) });
+ navigate(`/agents/${nextRef}/${tab}`, { replace: true });
+ return true;
+}
+
function AgentConfigurePage({
agent,
agentId,
@@ -1658,6 +1681,8 @@ function AgentConfigurePage({
updatePermissions: { mutate: (permissions: AgentPermissionUpdate) => void; isPending: boolean };
}) {
const queryClient = useQueryClient();
+ const navigate = useNavigate();
+ const { tab: urlTab } = useParams<{ tab?: string }>();
const [revisionsOpen, setRevisionsOpen] = useState(false);
const { data: configRevisions } = useQuery({
@@ -1667,10 +1692,12 @@ function AgentConfigurePage({
const rollbackConfig = useMutation({
mutationFn: (revisionId: string) => agentsApi.rollbackConfigRevision(agent.id, revisionId, companyId),
- onSuccess: () => {
+ onSuccess: (updated) => {
queryClient.invalidateQueries({ queryKey: queryKeys.agents.detail(agent.id) });
- queryClient.invalidateQueries({ queryKey: queryKeys.agents.detail(agent.urlKey) });
queryClient.invalidateQueries({ queryKey: queryKeys.agents.configRevisions(agent.id) });
+ if (!syncAgentRouteAfterRename(queryClient, navigate, agent, updated, urlTab ?? "configuration")) {
+ queryClient.invalidateQueries({ queryKey: queryKeys.agents.detail(agent.urlKey) });
+ }
},
});
@@ -1770,6 +1797,8 @@ function ConfigurationTab({
hideInstructionsFile?: boolean;
}) {
const queryClient = useQueryClient();
+ const navigate = useNavigate();
+ const { tab: urlTab } = useParams<{ tab?: string }>();
const { pushToast } = useToastActions();
const [awaitingRefreshAfterSave, setAwaitingRefreshAfterSave] = useState(false);
const lastAgentRef = useRef(agent);
@@ -1804,11 +1833,13 @@ function ConfigurationTab({
onMutate: () => {
setAwaitingRefreshAfterSave(true);
},
- onSuccess: () => {
+ onSuccess: (updated) => {
queryClient.invalidateQueries({ queryKey: queryKeys.agents.detail(agent.id) });
- queryClient.invalidateQueries({ queryKey: queryKeys.agents.detail(agent.urlKey) });
queryClient.invalidateQueries({ queryKey: queryKeys.agents.configRevisions(agent.id) });
queryClient.invalidateQueries({ queryKey: queryKeys.agents.list(agent.companyId) });
+ if (!syncAgentRouteAfterRename(queryClient, navigate, agent, updated, urlTab ?? "configuration")) {
+ queryClient.invalidateQueries({ queryKey: queryKeys.agents.detail(agent.urlKey) });
+ }
pushToast({ title: "Agent saved", tone: "success" });
},
onError: (err) => {