Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(canvas): keep the base path on links that leave the app (#17617)

Tim O'Farrelle4197c043f8a
diff --git a/__tests__/components/shared/navigation-link.test.tsx b/__tests__/components/shared/navigation-link.test.tsx
index ddaa3edb21..e8aeec2b13 100644
--- a/__tests__/components/shared/navigation-link.test.tsx
+++ b/__tests__/components/shared/navigation-link.test.tsx
@@ -1,5 +1,5 @@
import { render, screen, fireEvent } from "@testing-library/react";
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import {
NavigationProvider,
type NavigationContextValue,
@@ -32,6 +32,52 @@ function renderNavigationLink(
}
describe("NavigationLink", () => {
+ afterEach(() => {
+ vi.unstubAllEnvs();
+ });
+
+ it("prefixes the href with the Canvas base path", () => {
+ // A modified click and the browser's own context menu follow `href`
+ // directly, so it must resolve inside Canvas rather than against the
+ // origin root, which may serve a different app.
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ renderNavigationLink("/settings/mcp");
+
+ expect(screen.getByRole("link", { name: "MCP" })).toHaveAttribute(
+ "href",
+ "/canvas/settings/mcp",
+ );
+ });
+
+ it("keeps the base path and query string together on the href", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ renderNavigationLink(
+ "/conversations/abc",
+ {},
+ "/conversations/abc?backend=local-1",
+ );
+
+ expect(screen.getByRole("link", { name: "MCP" })).toHaveAttribute(
+ "href",
+ "/canvas/conversations/abc?backend=local-1",
+ );
+ });
+
+ it("keeps the base path in the href while still marking the link active", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ renderNavigationLink("/settings/mcp");
+
+ // Active state is computed from the router's base-path-stripped pathname
+ // (`/settings/mcp`), not the base-prefixed href.
+ expect(screen.getByRole("link", { name: "MCP" })).toHaveAttribute(
+ "aria-current",
+ "page",
+ );
+ });
+
it("renders the destination href and active state from navigation context", () => {
renderNavigationLink("/settings/mcp");
diff --git a/__tests__/hooks/use-conversation-name-context-menu.test.tsx b/__tests__/hooks/use-conversation-name-context-menu.test.tsx
index 75fd0dd061..9dfeebc8c8 100644
--- a/__tests__/hooks/use-conversation-name-context-menu.test.tsx
+++ b/__tests__/hooks/use-conversation-name-context-menu.test.tsx
@@ -437,6 +437,23 @@ describe("useConversationNameContextMenu", () => {
);
});
+ it("keeps the Canvas base path on share links", () => {
+ // A bare `/shared/conversations/...` link resolves against the origin root,
+ // which on the cloud host serves the enterprise app and reports the
+ // conversation as missing.
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ const { result } = renderHook(() =>
+ useConversationNameContextMenu({ conversationId: "conv-shared" }),
+ );
+
+ expect(result.current.shareUrl).toBe(
+ `${window.location.origin}/canvas/shared/conversations/conv-shared`,
+ );
+
+ vi.unstubAllEnvs();
+ });
+
it("keeps populated agent metadata hidden while options are closed", () => {
harness.systemMessage = {
content: "system",
diff --git a/__tests__/utils/base-path.test.ts b/__tests__/utils/base-path.test.ts
index 08a5145887..f8faa71952 100644
--- a/__tests__/utils/base-path.test.ts
+++ b/__tests__/utils/base-path.test.ts
@@ -1,7 +1,9 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import {
buildAgentCanvasPath,
+ buildAgentCanvasUrl,
getAgentCanvasBasePath,
+ getAgentCanvasBaseUrl,
} from "#/utils/base-path";
afterEach(() => {
@@ -31,4 +33,84 @@ describe("Agent Canvas base path", () => {
expect(getAgentCanvasBasePath()).toBe("/canvas");
expect(buildAgentCanvasPath("settings")).toBe("/canvas/settings");
});
+
+ it("leaves paths that carry their own origin untouched", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ // An absolute or protocol-relative path addresses another document; only
+ // app-relative paths belong under the Canvas mount point.
+ expect(buildAgentCanvasPath("https://example.com/settings")).toBe(
+ "https://example.com/settings",
+ );
+ expect(buildAgentCanvasPath("//example.com/settings")).toBe(
+ "//example.com/settings",
+ );
+ });
+});
+
+describe("Agent Canvas absolute URLs", () => {
+ it("joins an app-relative path onto the current origin", () => {
+ expect(buildAgentCanvasUrl("/shared/conversations/abc")).toBe(
+ `${window.location.origin}/shared/conversations/abc`,
+ );
+ });
+
+ it("carries the base path so the URL stays inside Canvas", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ expect(buildAgentCanvasUrl("/shared/conversations/abc")).toBe(
+ `${window.location.origin}/canvas/shared/conversations/abc`,
+ );
+ });
+
+ it("uses an explicit origin, such as a cloud backend host", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ expect(
+ buildAgentCanvasUrl(
+ "/shared/conversations/abc",
+ "https://app.example.com",
+ ),
+ ).toBe("https://app.example.com/canvas/shared/conversations/abc");
+ });
+
+ it("does not double-prefix an origin that already ends in the base path", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ expect(
+ buildAgentCanvasUrl("/settings", "https://app.example.com/canvas"),
+ ).toBe("https://app.example.com/canvas/settings");
+ });
+
+ it("returns a bare path when there is no origin available", () => {
+ expect(buildAgentCanvasUrl("/settings", "")).toBe("/settings");
+ });
+});
+
+describe("Agent Canvas base URL", () => {
+ it("is the plain origin when Canvas is mounted at the root", () => {
+ expect(getAgentCanvasBaseUrl()).toBe(window.location.origin);
+ });
+
+ it("includes the base path, without a trailing slash", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ expect(getAgentCanvasBaseUrl()).toBe(`${window.location.origin}/canvas`);
+ });
+
+ it("does not add the base path twice when the origin already has it", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ expect(getAgentCanvasBaseUrl("https://app.example.com/canvas")).toBe(
+ "https://app.example.com/canvas",
+ );
+ });
+
+ it("is the base an exported conversation path appends to", () => {
+ vi.stubEnv("VITE_BASE_PATH", "/canvas");
+
+ expect(`${getAgentCanvasBaseUrl()}/conversations/abc`).toBe(
+ `${window.location.origin}/canvas/conversations/abc`,
+ );
+ });
});
diff --git a/src/components/features/automations/detail/activity-log-item.tsx b/src/components/features/automations/detail/activity-log-item.tsx
index 1463b65f87..c7b45ae4dd 100644
--- a/src/components/features/automations/detail/activity-log-item.tsx
+++ b/src/components/features/automations/detail/activity-log-item.tsx
@@ -12,6 +12,7 @@ import { getAutomationRunDisplay } from "#/utils/automation-run-display";
import { RunStatusBadge } from "./run-status-badge";
import { RunPhase, shouldShowRunPhase } from "./run-phase";
import { RunLogsModal } from "./run-logs-modal";
+import { buildAgentCanvasPath } from "#/utils/base-path";
interface ActivityLogItemProps {
run: AutomationRun;
@@ -31,8 +32,9 @@ function formatRunTimestamp(dateStr: string, locale: string): string {
}
function getConversationUrl(conversationId: string): string {
- // In agent-canvas, conversations are at /conversations/:id
- return `/conversations/${conversationId}`;
+ // In agent-canvas, conversations are at /conversations/:id, under the base
+ // path when Canvas is mounted on a subpath.
+ return buildAgentCanvasPath(`/conversations/${conversationId}`);
}
/**
diff --git a/src/components/features/automations/detail/activity-log-section.tsx b/src/components/features/automations/detail/activity-log-section.tsx
index 7012d97f8f..1c9d974a48 100644
--- a/src/components/features/automations/detail/activity-log-section.tsx
+++ b/src/components/features/automations/detail/activity-log-section.tsx
@@ -9,6 +9,7 @@ import { cn } from "#/utils/utils";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
import { getApiErrorMessage } from "#/utils/api-error-message";
import { downloadActivityLogExport } from "#/utils/automation-activity-log-export";
+import { getAgentCanvasBaseUrl } from "#/utils/base-path";
import type { ActivityLogExportFormat, Automation } from "#/types/automation";
import { ActivityLogItem } from "./activity-log-item";
@@ -52,7 +53,7 @@ export function ActivityLogSection({
await downloadActivityLogExport({
automation,
format,
- conversationBaseUrl: window.location.origin,
+ conversationBaseUrl: getAgentCanvasBaseUrl(),
});
trackAutomationActivityLogExported({
backendKind: active.backend.kind,
diff --git a/src/components/features/command-menu/command-menu.tsx b/src/components/features/command-menu/command-menu.tsx
index 3b48db2963..68373b5dd8 100644
--- a/src/components/features/command-menu/command-menu.tsx
+++ b/src/components/features/command-menu/command-menu.tsx
@@ -7,6 +7,7 @@ import { useNavigation } from "#/context/navigation-context";
import { useCommandMenuStore } from "#/stores/command-menu-store";
import { useSidebarStore } from "#/stores/sidebar-store";
import { cn } from "#/utils/utils";
+import { buildAgentCanvasPath } from "#/utils/base-path";
import {
COMMAND_MENU_GROUP_LABELS,
COMMAND_MENU_GROUP_ORDER,
@@ -340,7 +341,7 @@ export function CommandMenu() {
key={item.id}
ref={assignRef}
id={getOptionId(item)}
- href={to}
+ href={buildAgentCanvasPath(to)}
role="option"
aria-selected={isActive}
onMouseEnter={() => setActiveIndex(itemIndex)}
diff --git a/src/components/shared/navigation-link.tsx b/src/components/shared/navigation-link.tsx
index 5ec90b6666..06e3a2fe25 100644
--- a/src/components/shared/navigation-link.tsx
+++ b/src/components/shared/navigation-link.tsx
@@ -3,6 +3,7 @@ import {
useNavigation,
type NavigationOptions,
} from "#/context/navigation-context";
+import { buildAgentCanvasPath } from "#/utils/base-path";
interface NavigationLinkClassNameState {
isActive: boolean;
@@ -91,7 +92,7 @@ export const NavigationLink = React.forwardRef<
<a
{...props}
ref={ref}
- href={to}
+ href={buildAgentCanvasPath(to)}
target={target}
rel={rel}
onClick={handleClick}
diff --git a/src/hooks/use-conversation-name-context-menu.ts b/src/hooks/use-conversation-name-context-menu.ts
index f7715d142c..533646813f 100644
--- a/src/hooks/use-conversation-name-context-menu.ts
+++ b/src/hooks/use-conversation-name-context-menu.ts
@@ -10,6 +10,7 @@ import { useEventStore } from "#/stores/use-event-store";
import { displaySuccessToast } from "#/utils/custom-toast-handlers";
import { I18nKey } from "#/i18n/declaration";
import { getStoredConversationMetadata } from "#/api/conversation-metadata-store";
+import { buildAgentCanvasUrl } from "#/utils/base-path";
import { useDownloadConversation } from "./use-download-conversation";
import {
@@ -159,7 +160,13 @@ export function useConversationNameContextMenu({
backend.kind === "cloud"
? backend.host.replace(/\/+$/, "")
: window.location.origin;
- return `${origin}/shared/conversations/${conversationId}`;
+ // The base path matters here for the same reason it does on anchors: on a
+ // host that also serves the enterprise app, a bare `/shared/conversations`
+ // link resolves against that app and reports the conversation missing.
+ return buildAgentCanvasUrl(
+ `/shared/conversations/${conversationId}`,
+ origin,
+ );
}, [conversationId, backend.kind, backend.host]);
const handleCopyShareLink = (event: React.MouseEvent<HTMLButtonElement>) => {
diff --git a/src/services/child-conversation-launch.ts b/src/services/child-conversation-launch.ts
index f6a4ff1f66..a07ae4ccc4 100644
--- a/src/services/child-conversation-launch.ts
+++ b/src/services/child-conversation-launch.ts
@@ -27,7 +27,7 @@ import {
} from "#/constants/child-conversation";
import { useGoalStore } from "#/stores/goal-store";
import type { LaunchChildConversationAction } from "#/types/agent-server/core";
-import { buildAgentCanvasPath } from "#/utils/base-path";
+import { buildAgentCanvasUrl } from "#/utils/base-path";
import {
displayErrorToast,
displaySuccessToastWithLink,
@@ -227,9 +227,7 @@ function claimToolCall(parentConversationId: string, toolCallId: string) {
}
function absoluteCanvasUrl(path: string) {
- const canvasPath = buildAgentCanvasPath(path);
- if (typeof window === "undefined") return canvasPath;
- return new URL(canvasPath, window.location.origin).toString();
+ return buildAgentCanvasUrl(path);
}
/**
diff --git a/src/utils/base-path.ts b/src/utils/base-path.ts
index f2769d179e..8d81695de7 100644
--- a/src/utils/base-path.ts
+++ b/src/utils/base-path.ts
@@ -24,10 +24,69 @@ export function getAgentCanvasBasePath(): string {
return "";
}
+/**
+ * Whether `path` already carries its own origin (an absolute URL or a
+ * protocol-relative one). Such paths address another document and must never
+ * have the Canvas base path spliced into them.
+ */
+function hasOwnOrigin(path: string): boolean {
+ return /^[a-z][a-z0-9+.-]*:/i.test(path) || path.startsWith("//");
+}
+
+/**
+ * Prefix an app-relative path with the Canvas base path.
+ *
+ * Canvas may be mounted under a subpath (e.g. `/canvas`) on a host that also
+ * serves other apps, so any URL that leaves the app β€” an `href` the browser
+ * follows itself, a share link, an exported record β€” must carry the base path.
+ * Bare paths resolve against the origin root and land on whatever other app
+ * owns that path.
+ */
export function buildAgentCanvasPath(path: string): string {
- const basePath = getAgentCanvasBasePath();
- if (!basePath) return path.startsWith("/") ? path : `/${path}`;
+ if (hasOwnOrigin(path)) return path;
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
+ const basePath = getAgentCanvasBasePath();
+ if (!basePath) return normalizedPath;
+
return `${basePath}${normalizedPath}`;
}
+
+function resolveOrigin(origin?: string): string {
+ if (origin !== undefined) return origin.replace(/\/+$/, "");
+ return typeof window !== "undefined" ? window.location.origin : "";
+}
+
+/**
+ * Drop a trailing base path so an origin that already names the Canvas mount
+ * point (`https://host/canvas`) is not prefixed with it a second time.
+ */
+function stripBasePath(origin: string): string {
+ const basePath = getAgentCanvasBasePath();
+ if (!basePath || !origin.endsWith(basePath)) return origin;
+ return origin.slice(0, -basePath.length);
+}
+
+/**
+ * The absolute URL of the Canvas app root β€” origin plus base path, with no
+ * trailing slash (`https://host/canvas`). Use this as the base for records that
+ * append their own app paths.
+ */
+export function getAgentCanvasBaseUrl(origin?: string): string {
+ return `${stripBasePath(resolveOrigin(origin))}${getAgentCanvasBasePath()}`;
+}
+
+/**
+ * Build an absolute, base-path-aware URL for an app-relative path.
+ *
+ * `origin` defaults to the current document's origin and may itself include the
+ * base path, which is not then applied twice.
+ */
+export function buildAgentCanvasUrl(path: string, origin?: string): string {
+ if (hasOwnOrigin(path)) return path;
+
+ const originRoot = stripBasePath(resolveOrigin(origin));
+ if (!originRoot) return buildAgentCanvasPath(path);
+
+ return `${originRoot}${buildAgentCanvasPath(path)}`;
+}
diff --git a/src/utils/custom-toast-handlers.tsx b/src/utils/custom-toast-handlers.tsx
index 6ffc37f824..bea1fe3c86 100644
--- a/src/utils/custom-toast-handlers.tsx
+++ b/src/utils/custom-toast-handlers.tsx
@@ -10,6 +10,7 @@ import {
isBackendRequestTimeoutMessage,
isCorsOrNetworkErrorMessage,
} from "./user-facing-error";
+import { buildAgentCanvasPath } from "#/utils/base-path";
// react-hot-toast accepts only CSSProperties via the style option β€” cannot use className
const TOAST_STYLE: CSSProperties = {
@@ -126,7 +127,10 @@ export const displaySuccessToastWithLink = (
toast.success(
<span className="[word-break:break-word] [overflow-wrap:anywhere]">
{message}{" "}
- <a className="underline hover:no-underline" href={href}>
+ <a
+ className="underline hover:no-underline"
+ href={buildAgentCanvasPath(href)}
+ >
{linkLabel}
</a>
</span>,