From 0d83233d4d6ac999a1516db9a9fb3ab8960d949f Mon Sep 17 00:00:00 2001 From: Cedric Vidal Date: Fri, 27 Feb 2026 12:22:32 -0800 Subject: [PATCH 1/2] feat(api): add scenarioCriteria and promptFeatures query params to GET /requests Add two new server-side filters to GET /api/v1/requests: - scenarioCriteria: comma-separated criteria IDs, filters runs whose scenario.criteria contains ALL specified IDs ($all) - promptFeatures: comma-separated feature IDs, resolves task prompts with ALL features detected, then filters runs by those taskPromptIds Extract filter-building logic into request-filters.ts for testability. Include 14 unit tests covering parsing and filter construction. Closes #163 (API layer) --- apps/api/src/index.ts | 26 ++++++ apps/api/src/request-filters.test.ts | 116 +++++++++++++++++++++++++++ apps/api/src/request-filters.ts | 42 ++++++++++ 3 files changed, 184 insertions(+) create mode 100644 apps/api/src/request-filters.test.ts create mode 100644 apps/api/src/request-filters.ts diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index f2b3946b1..7ccf2bfbf 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -26,6 +26,7 @@ import { } from "./prompt-feature-llm.js"; import { computeAnalysis, AnalysisResponse, AnalyzableRun } from "./analysis.js"; import { computeMdp, parseStateKey, type MdpAnalyzableRun } from "./criteria-mdp.js"; +import { parseCommaSeparatedIds, buildScenarioCriteriaFilter, buildPromptFeatureTaskPromptFilter } from "./request-filters.js"; import { TaskPromptStore, computeTaskPromptId, type TaskPromptDocument, SkillRevisionStore, SkillResolver, type SkillDocument, type SkillRevisionDocument, type SkillSearchResult } from "shared"; import { evaluateTrigger } from "shared"; import { checkMigrations } from "db-migrations/check-migrations"; @@ -950,6 +951,8 @@ app.get("/api/v1/requests", async (req: Request, res: Response, next: NextFuncti const workerFilter = req.query.worker as string; const taskPromptIdFilter = req.query.taskPromptId as string; const criteriaFilter = req.query.criteria as string; + const scenarioCriteriaFilter = req.query.scenarioCriteria as string; + const promptFeaturesFilter = req.query.promptFeatures as string; const includeDeleted = req.query.includeDeleted === "true"; const filter: Record = {}; @@ -984,6 +987,29 @@ app.get("/api/v1/requests", async (req: Request, res: Response, next: NextFuncti } } + // Filter by scenario criteria IDs (e.g. "has_azure,has_cloud") + // Matches runs whose scenario.criteria array contains ALL specified IDs. + const scenarioCriteriaIds = parseCommaSeparatedIds(scenarioCriteriaFilter); + const scenarioCriteriaClause = buildScenarioCriteriaFilter(scenarioCriteriaIds); + if (scenarioCriteriaClause) { + Object.assign(filter, scenarioCriteriaClause); + } + + // Filter by prompt features (e.g. "asks_for_api,asks_for_azure") + // First resolves task prompt IDs that have ALL specified features detected, + // then filters runs by those task prompt IDs. + const featureIds = parseCommaSeparatedIds(promptFeaturesFilter); + const featureTaskPromptFilter = buildPromptFeatureTaskPromptFilter(featureIds); + if (featureTaskPromptFilter) { + const matchingTaskPrompts = await taskPromptCollection + .find(featureTaskPromptFilter, { projection: { _id: 1 } }) + .toArray(); + const matchingIds = matchingTaskPrompts.map(tp => tp._id); + filter.taskPromptId = taskPromptIdFilter + ? (matchingIds.includes(taskPromptIdFilter) ? taskPromptIdFilter : "__no_match__") + : { $in: matchingIds }; + } + const resources = await collection .find(filter) .sort({ createdAt: -1 }) diff --git a/apps/api/src/request-filters.test.ts b/apps/api/src/request-filters.test.ts new file mode 100644 index 000000000..5b2ad0361 --- /dev/null +++ b/apps/api/src/request-filters.test.ts @@ -0,0 +1,116 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { describe, it, expect } from "vitest"; +import { + parseCommaSeparatedIds, + buildScenarioCriteriaFilter, + buildPromptFeatureTaskPromptFilter, +} from "./request-filters.js"; + +// ─── parseCommaSeparatedIds ────────────────────────────────────────────────── + +describe("parseCommaSeparatedIds", () => { + it("parses a single ID", () => { + expect(parseCommaSeparatedIds("has_azure")).toEqual(["has_azure"]); + }); + + it("parses multiple comma-separated IDs", () => { + expect(parseCommaSeparatedIds("has_azure,has_cloud,has_node")).toEqual([ + "has_azure", + "has_cloud", + "has_node", + ]); + }); + + it("trims whitespace from IDs", () => { + expect(parseCommaSeparatedIds(" has_azure , has_cloud ")).toEqual([ + "has_azure", + "has_cloud", + ]); + }); + + it("filters out empty segments", () => { + expect(parseCommaSeparatedIds(",has_azure,,has_cloud,")).toEqual([ + "has_azure", + "has_cloud", + ]); + }); + + it("returns empty array for undefined", () => { + expect(parseCommaSeparatedIds(undefined)).toEqual([]); + }); + + it("returns empty array for null", () => { + expect(parseCommaSeparatedIds(null)).toEqual([]); + }); + + it("returns empty array for empty string", () => { + expect(parseCommaSeparatedIds("")).toEqual([]); + }); + + it("returns empty array for whitespace-only string", () => { + expect(parseCommaSeparatedIds(" , , ")).toEqual([]); + }); +}); + +// ─── buildScenarioCriteriaFilter ───────────────────────────────────────────── + +describe("buildScenarioCriteriaFilter", () => { + it("returns undefined for empty IDs", () => { + expect(buildScenarioCriteriaFilter([])).toBeUndefined(); + }); + + it("builds $all filter for a single criterion", () => { + expect(buildScenarioCriteriaFilter(["has_azure"])).toEqual({ + "scenario.criteria": { $all: ["has_azure"] }, + }); + }); + + it("builds $all filter for multiple criteria", () => { + expect(buildScenarioCriteriaFilter(["has_azure", "has_cloud"])).toEqual({ + "scenario.criteria": { $all: ["has_azure", "has_cloud"] }, + }); + }); +}); + +// ─── buildPromptFeatureTaskPromptFilter ────────────────────────────────────── + +describe("buildPromptFeatureTaskPromptFilter", () => { + it("returns undefined for empty feature IDs", () => { + expect(buildPromptFeatureTaskPromptFilter([])).toBeUndefined(); + }); + + it("builds $and + $elemMatch filter for a single feature", () => { + const result = buildPromptFeatureTaskPromptFilter(["asks_for_api"]); + expect(result).toEqual({ + $and: [ + { + features: { + $elemMatch: { featureId: "asks_for_api", detected: true }, + }, + }, + ], + deletedAt: { $exists: false }, + }); + }); + + it("builds $and + $elemMatch filter for multiple features", () => { + const result = buildPromptFeatureTaskPromptFilter(["asks_for_api", "asks_for_azure"]); + expect(result).toEqual({ + $and: [ + { + features: { + $elemMatch: { featureId: "asks_for_api", detected: true }, + }, + }, + { + features: { + $elemMatch: { featureId: "asks_for_azure", detected: true }, + }, + }, + ], + deletedAt: { $exists: false }, + }); + }); +}); diff --git a/apps/api/src/request-filters.ts b/apps/api/src/request-filters.ts new file mode 100644 index 000000000..84bbfa2f8 --- /dev/null +++ b/apps/api/src/request-filters.ts @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * Utilities for building MongoDB filter objects for the /api/v1/requests endpoint. + * Extracted for testability. + */ + +/** + * Parse a comma-separated string of IDs into a trimmed, non-empty array. + * Returns an empty array if the input is falsy or contains only whitespace. + */ +export function parseCommaSeparatedIds(input: string | undefined | null): string[] { + if (!input) return []; + return input.split(",").map(s => s.trim()).filter(Boolean); +} + +/** + * Build a MongoDB filter clause for scenario.criteria containing ALL given IDs. + * Returns undefined if no IDs are provided. + */ +export function buildScenarioCriteriaFilter(ids: string[]): Record | undefined { + if (ids.length === 0) return undefined; + return { "scenario.criteria": { $all: ids } }; +} + +/** + * Build a MongoDB $and clause to match task prompts that have ALL given + * prompt features detected (featureId + detected: true). + * Returns undefined if no feature IDs are provided. + */ +export function buildPromptFeatureTaskPromptFilter(featureIds: string[]): Record | undefined { + if (featureIds.length === 0) return undefined; + return { + $and: featureIds.map(fid => ({ + features: { + $elemMatch: { featureId: fid, detected: true }, + }, + })), + deletedAt: { $exists: false }, + }; +} From 2d310f4428c83d119023e31275ffd062cffe2f9b Mon Sep 17 00:00:00 2001 From: Cedric Vidal Date: Fri, 27 Feb 2026 12:22:41 -0800 Subject: [PATCH 2/2] feat(portal): add criteria & prompt feature filter bars to runs page - Extend api.listRuns() to pass scenarioCriteria and promptFeatures params - Create PromptFeatureFilterBar component (mirrors CriteriaFilterBar) - Add CriteriaFilterBar for scenario criteria and PromptFeatureFilterBar to RunsList page with URL search param persistence - Both filters use AND semantics: selecting multiple items shows runs matching ALL of them Closes #163 --- .../src/components/PromptFeatureFilterBar.tsx | 84 ++++++++++++++ apps/portal/src/lib/api.ts | 6 +- apps/portal/src/pages/RunsList.tsx | 106 +++++++++++++++++- 3 files changed, 192 insertions(+), 4 deletions(-) create mode 100644 apps/portal/src/components/PromptFeatureFilterBar.tsx diff --git a/apps/portal/src/components/PromptFeatureFilterBar.tsx b/apps/portal/src/components/PromptFeatureFilterBar.tsx new file mode 100644 index 000000000..fbbaf4e56 --- /dev/null +++ b/apps/portal/src/components/PromptFeatureFilterBar.tsx @@ -0,0 +1,84 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { Filter, X } from "lucide-react"; +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; +import { Badge } from "@/components/ui/badge"; + +interface PromptFeatureFilterBarProps { + availableFeatures: string[]; + selectedFeatures: string[]; + onToggle: (id: string) => void; + onClear: () => void; + /** Override the card title (default: "Prompt Feature Filter") */ + title?: string; + /** Override the description when nothing is selected */ + emptyDescription?: string; + /** Override the description template when features are selected (receives count) */ + selectedDescription?: (count: number) => string; +} + +export function PromptFeatureFilterBar({ + availableFeatures, + selectedFeatures, + onToggle, + onClear, + title = "Prompt Feature Filter", + emptyDescription = "Click features to filter runs by task prompt characteristics.", + selectedDescription = (count: number) => + `Showing runs whose task prompt has ${count} selected feature${count !== 1 ? "s" : ""} detected.`, +}: PromptFeatureFilterBarProps) { + if (availableFeatures.length === 0) { + return null; + } + + const selectedSet = new Set(selectedFeatures); + + return ( + + +
+
+ + {title} +
+ {selectedFeatures.length > 0 && ( + + )} +
+ + {selectedFeatures.length === 0 + ? emptyDescription + : selectedDescription(selectedFeatures.length)} + +
+ +
+ {availableFeatures.map((id) => { + const isSelected = selectedSet.has(id); + return ( + onToggle(id)} + > + {id} + {isSelected && } + + ); + })} +
+
+
+ ); +} diff --git a/apps/portal/src/lib/api.ts b/apps/portal/src/lib/api.ts index 19c198264..ba60f27e5 100644 --- a/apps/portal/src/lib/api.ts +++ b/apps/portal/src/lib/api.ts @@ -19,12 +19,14 @@ async function request(path: string, init?: RequestInit): Promise { } export const api = { - /** List all runs, optionally filtered by worker, task prompt, and/or MDP criteria state */ - listRuns: (opts?: { worker?: string; taskPromptId?: string; criteria?: string }): Promise => { + /** List all runs, optionally filtered by worker, task prompt, criteria state, scenario criteria, and/or prompt features */ + listRuns: (opts?: { worker?: string; taskPromptId?: string; criteria?: string; scenarioCriteria?: string[]; promptFeatures?: string[] }): Promise => { const params = new URLSearchParams(); if (opts?.worker) params.set("worker", opts.worker); if (opts?.taskPromptId) params.set("taskPromptId", opts.taskPromptId); if (opts?.criteria) params.set("criteria", opts.criteria); + if (opts?.scenarioCriteria?.length) params.set("scenarioCriteria", opts.scenarioCriteria.join(",")); + if (opts?.promptFeatures?.length) params.set("promptFeatures", opts.promptFeatures.join(",")); const qs = params.toString(); return request(`/requests${qs ? `?${qs}` : ""}`); }, diff --git a/apps/portal/src/pages/RunsList.tsx b/apps/portal/src/pages/RunsList.tsx index 815beb9ea..fa94769cc 100644 --- a/apps/portal/src/pages/RunsList.tsx +++ b/apps/portal/src/pages/RunsList.tsx @@ -20,10 +20,12 @@ import { Badge } from "@/components/ui/badge"; import { Skeleton } from "@/components/ui/skeleton"; import { StatusBadge } from "@/components/StatusBadge"; import { ReportStatusBadge } from "@/components/ReportStatusBadge"; +import { CriteriaFilterBar } from "@/components/CriteriaFilterBar"; +import { PromptFeatureFilterBar } from "@/components/PromptFeatureFilterBar"; import { Trash2, Eye, Plus, RefreshCw, Repeat, FileText, X } from "lucide-react"; import { formatDate, formatId, truncate } from "@/lib/utils"; import { WORKER_TYPES, STATUS_LIST } from "@/types"; -import type { Run, BulkResubmitOverrides, McpServerDocument, CodingAgent } from "@/types"; +import type { Run, BulkResubmitOverrides, McpServerDocument, CodingAgent, CriteriaDocument, PromptFeatureDocument } from "@/types"; export function RunsList() { const [searchParams, setSearchParams] = useSearchParams(); @@ -32,6 +34,18 @@ export function RunsList() { const [workerFilter, setWorkerFilter] = useState("all"); const [statusFilter, setStatusFilter] = useState("all"); const [taskFilter, setTaskFilter] = useState("all"); + + // Scenario criteria filter (which criteria IDs are assigned to the run) + const selectedScenarioCriteria = useMemo(() => { + const raw = searchParams.get("scenarioCriteria"); + return raw ? raw.split(",").filter(Boolean) : []; + }, [searchParams]); + + // Prompt feature filter (which features the run's task prompt has) + const selectedPromptFeatures = useMemo(() => { + const raw = searchParams.get("promptFeatures"); + return raw ? raw.split(",").filter(Boolean) : []; + }, [searchParams]); const [selectedIds, setSelectedIds] = useState>(new Set()); const [resubmitCount, setResubmitCount] = useState(1); const [resubmitDialogOpen, setResubmitDialogOpen] = useState(false); @@ -39,15 +53,36 @@ export function RunsList() { const queryClient = useQueryClient(); const { data: runs = [], isLoading, isRefetching } = useQuery({ - queryKey: ["runs", workerFilter, taskPromptId, criteriaState], + queryKey: ["runs", workerFilter, taskPromptId, criteriaState, selectedScenarioCriteria, selectedPromptFeatures], queryFn: () => api.listRuns({ worker: workerFilter === "all" ? undefined : workerFilter, taskPromptId, criteria: criteriaState, + scenarioCriteria: selectedScenarioCriteria.length > 0 ? selectedScenarioCriteria : undefined, + promptFeatures: selectedPromptFeatures.length > 0 ? selectedPromptFeatures : undefined, }), refetchInterval: 10_000, }); + // Fetch available criteria and prompt features for the filter bars + const { data: allCriteria = [] } = useQuery({ + queryKey: ["criteria"], + queryFn: () => api.listCriteria(), + }); + const availableCriteriaIds = useMemo( + () => allCriteria.filter((c) => !c.deletedAt).map((c) => c.id).sort(), + [allCriteria], + ); + + const { data: allPromptFeatures = [] } = useQuery({ + queryKey: ["prompt-features"], + queryFn: () => api.listPromptFeatures(), + }); + const availableFeatureIds = useMemo( + () => allPromptFeatures.filter((f) => !f.deletedAt).map((f) => f.id).sort(), + [allPromptFeatures], + ); + // Fetch MCP servers for the resubmit dialog const { data: mcpServers = [] } = useQuery({ queryKey: ["mcp-servers"], @@ -193,6 +228,52 @@ export function RunsList() { }); }; + // Toggle a scenario criteria filter in URL search params + const toggleScenarioCriteria = (id: string) => { + const next = new URLSearchParams(searchParams); + const current = new Set(selectedScenarioCriteria); + if (current.has(id)) { + current.delete(id); + } else { + current.add(id); + } + if (current.size > 0) { + next.set("scenarioCriteria", [...current].join(",")); + } else { + next.delete("scenarioCriteria"); + } + setSearchParams(next); + }; + + const clearScenarioCriteria = () => { + const next = new URLSearchParams(searchParams); + next.delete("scenarioCriteria"); + setSearchParams(next); + }; + + // Toggle a prompt feature filter in URL search params + const togglePromptFeature = (id: string) => { + const next = new URLSearchParams(searchParams); + const current = new Set(selectedPromptFeatures); + if (current.has(id)) { + current.delete(id); + } else { + current.add(id); + } + if (current.size > 0) { + next.set("promptFeatures", [...current].join(",")); + } else { + next.delete("promptFeatures"); + } + setSearchParams(next); + }; + + const clearPromptFeatures = () => { + const next = new URLSearchParams(searchParams); + next.delete("promptFeatures"); + setSearchParams(next); + }; + return (
{/* Page header */} @@ -324,6 +405,27 @@ export function RunsList() {
)} + {/* Scenario criteria filter bar */} + + `Showing runs whose scenario includes ${count} selected criteri${count !== 1 ? "a" : "on"}.` + } + /> + + {/* Prompt feature filter bar */} + + {/* Bulk action bar */} {selectedIds.size > 0 && (