fix(backend): stop manual Retry from starting a second agent when a route already resumed (#3399)

Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com>
Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
Henry
2026-09-06 18:35:13 +08:00
committed by GitHub
parent b241649a71
commit 1b8f0f8ca9
11 changed files with 626 additions and 79 deletions
@@ -16,6 +16,7 @@ vi.mock("@/components/state-provider", () => ({
}));
import type { TaskSession } from "@/lib/types/http";
import { WebSocketRequestError } from "@/lib/ws/client";
import { DynamicRouteRecovery } from "./dynamic-route-recovery";
const session = {
@@ -65,4 +66,31 @@ describe("DynamicRouteRecovery", () => {
expect.objectContaining({ route_generation: 4, route_state: "starting" }),
);
});
it("applies the authoritative route from a conflict response", async () => {
mocks.request.mockRejectedValueOnce(
new WebSocketRequestError("route changed", "CONFLICT", {
route: {
execution_profile_id: "profile-3",
route_generation: 5,
state: "action_required",
reason: "route_action_launch_failed",
},
}),
);
render(<DynamicRouteRecovery session={session} />);
fireEvent.click(screen.getByTestId("dynamic-route-retry"));
await waitFor(() => {
expect(mocks.setTaskSession).toHaveBeenCalledWith(
expect.objectContaining({
execution_profile_id: "profile-3",
route_generation: 5,
route_state: "action_required",
route_reason: "route_action_launch_failed",
}),
);
});
});
});
@@ -8,6 +8,7 @@ import { useAppStore } from "@/components/state-provider";
import { agentProfileId as toAgentProfileId } from "@/lib/types/ids";
import type { TaskSession } from "@/lib/types/http";
import { getWebSocketClient } from "@/lib/ws/connection";
import { WebSocketRequestError } from "@/lib/ws/client";
const ROUTE_REASON_KEYS: Record<string, string> = {
candidate_order: "dynamicRouteReasonCandidateOrder",
@@ -30,6 +31,12 @@ type RouteActionResult = {
pending_outcome?: string;
};
function isRouteActionResult(value: unknown): value is RouteActionResult {
if (typeof value !== "object" || value === null) return false;
const result = value as Partial<RouteActionResult>;
return typeof result.route_generation === "number" && typeof result.state === "string";
}
async function applyRouteAction(
session: TaskSession,
action: RouteAction,
@@ -47,7 +54,11 @@ async function applyRouteAction(
30000,
);
return result;
} catch {
} catch (error) {
if (error instanceof WebSocketRequestError && error.code === "CONFLICT") {
const route = error.details?.route;
return isRouteActionResult(route) ? route : null;
}
return null;
}
}