Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(prompts): reserve 'metrics' and 'prompt-detail' as prompt names (#15775)

Nikita Kabardin331c4aa314c4
diff --git a/packages/shared/src/features/prompts/constants.ts b/packages/shared/src/features/prompts/constants.ts
index 84cc441ca0..e8d1ddd19b 100644
--- a/packages/shared/src/features/prompts/constants.ts
+++ b/packages/shared/src/features/prompts/constants.ts
@@ -5,6 +5,15 @@ export const COMMIT_MESSAGE_MAX_LENGTH = 500;
// Prompt name validation
export const PROMPT_NAME_MAX_LENGTH = 255;
export const RESERVED_PROMPT_NAME_NEW = "new";
+// Static sibling pages of the /prompts/[[...folder]] catch-all route
+// (web/src/pages/project/[projectId]/prompts/). Next.js resolves static
+// routes before the catch-all, so a prompt with one of these exact names
+// would have every link to it render the static page instead.
+export const RESERVED_PROMPT_NAMES = [
+ RESERVED_PROMPT_NAME_NEW,
+ "metrics",
+ "prompt-detail",
+] as const;
export const PROMPT_NAME_PIPE_RESTRICTION_REGEX = /^[^|]*$/;
export const PROMPT_NAME_PIPE_RESTRICTION_ERROR =
"Prompt name cannot contain '|' character";
diff --git a/packages/shared/src/features/prompts/validation.test.ts b/packages/shared/src/features/prompts/validation.test.ts
new file mode 100644
index 0000000000..5b8cf751ff
--- /dev/null
+++ b/packages/shared/src/features/prompts/validation.test.ts
@@ -0,0 +1,64 @@
+import { describe, expect, it } from "vitest";
+
+import { RESERVED_PROMPT_NAMES } from "./constants";
+import { PromptNameSchema } from "./validation";
+
+describe("PromptNameSchema", () => {
+ // The static /prompts/* pages (new, metrics, prompt-detail) beat the
+ // [[...folder]] catch-all in Next.js routing, so a prompt with one of these
+ // exact names would have every link to it resolve to the static page.
+ it.each(RESERVED_PROMPT_NAMES)("rejects the reserved name '%s'", (name) => {
+ const result = PromptNameSchema.safeParse(name);
+
+ expect(result.success).toBe(false);
+ expect(result.error?.issues[0]?.message).toBe(
+ `Prompt name cannot be '${name}'`,
+ );
+ });
+
+ it.each(RESERVED_PROMPT_NAMES)(
+ "rejects '%s' with surrounding whitespace (compared after trim)",
+ (name) => {
+ expect(PromptNameSchema.safeParse(` ${name} `).success).toBe(false);
+ },
+ );
+
+ // Only the exact single-segment name collides with the static routes:
+ // "metrics/foo" (folder named metrics) and "foo/new" (leaf named new)
+ // resolve through the catch-all. Deliberately not over-blocked.
+ // Note: "folder/metrics" stays valid here; the catch-all's `/metrics`
+ // URL-suffix handling is a separate, pre-existing quirk documented in
+ // web/src/pages/project/[projectId]/prompts/[[...folder]].tsx.
+ it.each(RESERVED_PROMPT_NAMES)(
+ "allows '%s' as a folder name or leaf segment",
+ (name) => {
+ expect(PromptNameSchema.safeParse(`${name}/foo`).success).toBe(true);
+ expect(PromptNameSchema.safeParse(`folder/${name}`).success).toBe(true);
+ },
+ );
+
+ it("accepts ordinary prompt names", () => {
+ for (const name of [
+ "my-prompt",
+ "metrics-v2",
+ "new-onboarding",
+ "folder/sub-folder/prompt",
+ ]) {
+ const result = PromptNameSchema.safeParse(name);
+ expect(result.success).toBe(true);
+ expect(result.data).toBe(name);
+ }
+ });
+
+ it("still enforces the existing rules (pipe, slashes, emptiness)", () => {
+ expect(PromptNameSchema.safeParse("a|b").success).toBe(false);
+ expect(PromptNameSchema.safeParse("/a").success).toBe(false);
+ expect(PromptNameSchema.safeParse("a/").success).toBe(false);
+ expect(PromptNameSchema.safeParse("a//b").success).toBe(false);
+ expect(PromptNameSchema.safeParse("").success).toBe(false);
+ });
+
+ it("trims surrounding whitespace from valid names", () => {
+ expect(PromptNameSchema.safeParse(" my-prompt ").data).toBe("my-prompt");
+ });
+});
diff --git a/packages/shared/src/features/prompts/validation.ts b/packages/shared/src/features/prompts/validation.ts
index 712532a5ed..2433e000eb 100644
--- a/packages/shared/src/features/prompts/validation.ts
+++ b/packages/shared/src/features/prompts/validation.ts
@@ -3,7 +3,7 @@ import { withFolderPathValidation } from "../folders/validation";
import {
PROMPT_NAME_PIPE_RESTRICTION_REGEX,
PROMPT_NAME_PIPE_RESTRICTION_ERROR,
- RESERVED_PROMPT_NAME_NEW,
+ RESERVED_PROMPT_NAMES,
} from "./constants";
/**
@@ -15,8 +15,10 @@ export const PromptNameSchema = withFolderPathValidation(
// Note: pipe character is used for prompt composition
PROMPT_NAME_PIPE_RESTRICTION_ERROR,
),
- // Note: we use "new" as a special name for the new prompt form
).refine(
- (name) => name !== RESERVED_PROMPT_NAME_NEW,
- `Prompt name cannot be '${RESERVED_PROMPT_NAME_NEW}'`,
+ // Reserved: names of the static /prompts/* pages (see RESERVED_PROMPT_NAMES).
+ // Only the exact name collides — "metrics/foo" or "foo/metrics" route
+ // through the [[...folder]] catch-all.
+ (name) => !(RESERVED_PROMPT_NAMES as readonly string[]).includes(name),
+ { error: (issue) => `Prompt name cannot be '${issue.input}'` },
);
diff --git a/web/src/features/prompts/server/routers/promptRouter.ts b/web/src/features/prompts/server/routers/promptRouter.ts
index feed41abeb..87f6e882f1 100644
--- a/web/src/features/prompts/server/routers/promptRouter.ts
+++ b/web/src/features/prompts/server/routers/promptRouter.ts
@@ -20,6 +20,7 @@ import {
optionalPaginationZod,
paginationZod,
PromptLabelSchema,
+ PromptNameSchema,
promptsTableCols,
PromptType,
StringNoHTMLNonEmpty,
@@ -356,7 +357,9 @@ export const promptRouter = createTRPCRouter({
z.object({
projectId: z.string(),
promptId: z.string(),
- name: StringNoHTMLNonEmpty,
+ // Same rules as create: a duplicate is a new prompt, so reserved
+ // names / pipes / malformed paths must not slip in through this path.
+ name: PromptNameSchema,
isSingleVersion: z.boolean(),
}),
)