From e7d4df456a2fa28fedc44f96342adaac6b992b0e Mon Sep 17 00:00:00 2001 From: Nina Chikanov Date: Fri, 20 Nov 2026 16:10:02 -0800 Subject: [PATCH] FEAT: Add CBRN scenario trials and run artifacts Add population-specific CBRN scoring and configurable independent trials, expose trial ASR in the GUI, and export run-scoped artifacts with seed metadata snapshots and browser download feedback. Include a five-objective public dataset and focused coverage. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../attackAttemptFormatting.test.ts | 34 +- .../AttackResults/attackAttemptFormatting.ts | 9 +- .../src/components/Chat/ChatScorerPicker.tsx | 2 +- .../Scenarios/ScenarioDetail.test.tsx | 71 ++ .../components/Scenarios/ScenarioDetail.tsx | 11 +- .../Scenarios/ScenarioRunPage.test.tsx | 112 +++ .../components/Scenarios/ScenarioRunPage.tsx | 66 +- .../Scenarios/ScenarioTechniqueSection.tsx | 13 +- .../ScenarioTechniqueSelector.test.tsx | 35 + .../Scenarios/ScenarioTechniqueSelector.tsx | 10 +- .../Scenarios/scenarioConfigForm.ts | 11 +- frontend/src/services/api.ts | 8 + frontend/src/types/index.ts | 5 + pyrit/analytics/scenario_statistics.py | 17 +- pyrit/backend/README.md | 26 + pyrit/backend/routes/scenarios.py | 31 + .../services/scenario_progress_read_model.py | 55 +- .../backend/services/scenario_run_service.py | 18 +- .../true_false_question/cbrn_operational.yaml | 32 + .../seed_datasets/local/airt/cbrn.prompt | 49 ++ pyrit/executor/attack/__init__.py | 2 + pyrit/executor/attack/compound/__init__.py | 2 + .../attack/compound/repeated_attack.py | 149 ++++ pyrit/memory/memory_interface.py | 26 +- pyrit/models/catalog/scenario.py | 1 + pyrit/models/scenario_progress.py | 5 + .../registry/components/scenario_registry.py | 1 + pyrit/scenario/core/scenario.py | 48 +- .../scenario/scenarios/_dynamic_techniques.py | 2 + pyrit/scenario/scenarios/airt/__init__.py | 4 + pyrit/scenario/scenarios/airt/cbrn.py | 421 +++++++++++ .../test_scenario_statistics_parity.py | 64 +- .../test_scenario_progress_read_model.py | 93 ++- .../unit/backend/test_scenario_run_routes.py | 48 ++ .../unit/backend/test_scenario_run_service.py | 20 + .../test_local_prompt_dataset_semantics.py | 17 + .../attack/compound/test_repeated_attack.py | 165 +++++ tests/unit/registry/test_scenario_registry.py | 5 + tests/unit/scenario/airt/test_cbrn.py | 682 ++++++++++++++++++ tests/unit/scenario/core/test_scenario.py | 59 +- 40 files changed, 2377 insertions(+), 52 deletions(-) create mode 100644 frontend/src/components/Scenarios/ScenarioTechniqueSelector.test.tsx create mode 100644 pyrit/datasets/score/true_false_question/cbrn_operational.yaml create mode 100644 pyrit/datasets/seed_datasets/local/airt/cbrn.prompt create mode 100644 pyrit/executor/attack/compound/repeated_attack.py create mode 100644 pyrit/scenario/scenarios/airt/cbrn.py create mode 100644 tests/unit/executor/attack/compound/test_repeated_attack.py create mode 100644 tests/unit/scenario/airt/test_cbrn.py diff --git a/frontend/src/components/AttackResults/attackAttemptFormatting.test.ts b/frontend/src/components/AttackResults/attackAttemptFormatting.test.ts index 7d7e20833d..c90d145f48 100644 --- a/frontend/src/components/AttackResults/attackAttemptFormatting.test.ts +++ b/frontend/src/components/AttackResults/attackAttemptFormatting.test.ts @@ -1,7 +1,39 @@ -import { objectivePreview } from './attackAttemptFormatting' +import type { OutcomeStatistics, ScenarioSeedGroupProgress } from '@/types' + +import { formatSeedGroupSuccess, objectivePreview } from './attackAttemptFormatting' const LONE_SURROGATE_REGEX = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(? { + const seed: ScenarioSeedGroupProgress = { + id: 'fixture', completed: 1, planned: 1, succeeded: 1, success_percentage: 100, errors: 0, retries: 0, + } + const trials: OutcomeStatistics = { + successes: 3, failures: 2, errors: 0, undetermined: 0, + total_results: 5, total_decided: 5, decided_share: 1, + success_rate: 0.6, success_rate_decided: 0.6, success_rate_all: 0.6, + outcome_shares: { success: 0.6, failure: 0.4, error: 0, undetermined: 0 }, + } + + it('uses the trial fraction instead of the successful parent outcome', () => { + expect(formatSeedGroupSuccess({ ...seed, trial_outcomes: trials })).toBe('60% (3/5 trials)') + }) + + it.each(['errors', 'undetermined'] as const)('does not present partial trial ASR with %s', (outcome: 'errors' | 'undetermined') => { + expect(formatSeedGroupSuccess({ + ...seed, trial_outcomes: { + ...trials, [outcome]: 1, failures: 1, total_decided: 4, decided_share: 0.8, + success_rate: 0.75, success_rate_decided: 0.75, + outcome_shares: { success: 0.6, failure: 0.2, error: 0, undetermined: 0, [outcome]: 0.2 }, + }, + })).toBe('Undetermined') + }) + + it('preserves ordinary and older result formatting without independent trials', () => { + expect(formatSeedGroupSuccess(seed)).toBe('100%') + }) +}) + describe('objectivePreview', () => { it('truncates supplementary Unicode without splitting surrogate pairs (issue #3870 reproduction)', () => { const objective = `${'a'.repeat(94)}😀 trailing text` diff --git a/frontend/src/components/AttackResults/attackAttemptFormatting.ts b/frontend/src/components/AttackResults/attackAttemptFormatting.ts index 65862007f9..9b65f1280a 100644 --- a/frontend/src/components/AttackResults/attackAttemptFormatting.ts +++ b/frontend/src/components/AttackResults/attackAttemptFormatting.ts @@ -1,4 +1,4 @@ -import type { ScenarioProgressCounts, ScenarioProgressResult } from '@/types' +import type { ScenarioProgressCounts, ScenarioProgressResult, ScenarioSeedGroupProgress } from '@/types' import { formatAnalyticsPercent } from '@/utils/attackAnalytics' const OBJECTIVE_PREVIEW_LENGTH = 96 @@ -11,6 +11,13 @@ export function formatProgressSuccess(counts: ScenarioProgressCounts): string { return counts.success_percentage === null ? 'Undetermined' : `${counts.success_percentage}%` } +export function formatSeedGroupSuccess(seed: ScenarioSeedGroupProgress): string { + const trials = seed.trial_outcomes + if (!trials) return formatProgressSuccess(seed) + if (trials.total_results === 0 || trials.total_decided !== trials.total_results) return 'Undetermined' + return `${formatAnalyticsPercent(trials.success_rate_all)} (${trials.successes}/${trials.total_results} trials)` +} + /** * Formatting for a single attack attempt. * diff --git a/frontend/src/components/Chat/ChatScorerPicker.tsx b/frontend/src/components/Chat/ChatScorerPicker.tsx index 691abfbf31..a3073a10dd 100644 --- a/frontend/src/components/Chat/ChatScorerPicker.tsx +++ b/frontend/src/components/Chat/ChatScorerPicker.tsx @@ -28,9 +28,9 @@ export default function ChatScorerPicker({ }: ChatScorerPickerProps) { const styles = useChatScorerPickerStyles() const selectedScorer = findRegisteredScorer(value ?? null, scorers, inherited) - const available = !value || selectedScorer !== undefined const eligibleScorers = scorers.filter((scorer: ScorerInstance) => usage === 'scenario' ? scorer.identifier.scorer_type === 'true_false' : isEligibleChatScorer(scorer)) + const available = !value || (selectedScorer !== undefined && eligibleScorers.includes(selectedScorer)) const selected = value === undefined && onUseLastUsed ? 'last-used' : selectedScorer && eligibleScorers.includes(selectedScorer) ? `registered:${selectedScorer.scorer_registry_name}` : value ? 'unavailable' : '' diff --git a/frontend/src/components/Scenarios/ScenarioDetail.test.tsx b/frontend/src/components/Scenarios/ScenarioDetail.test.tsx index 12e5fdd3a1..1214820f91 100644 --- a/frontend/src/components/Scenarios/ScenarioDetail.test.tsx +++ b/frontend/src/components/Scenarios/ScenarioDetail.test.tsx @@ -272,6 +272,77 @@ function renderDetail( } describe('ScenarioDetail', () => { + it('uses one primary refusal scorer for CBRN and can return to the scenario default', async () => { + const user = userEvent.setup() + mockGetScenario.mockResolvedValue(makeScenario({ + scenario_name: 'airt.cbrn', supports_objective_scorer: true, + baseline_policy: 'forbidden', uses_default_adversarial_target: false, + supported_parameters: [{ name: 'num_trials', type_name: 'int', required: false, default: 5 }], + })) + renderDetail('/scanner/launch/airt.cbrn', { scorers: [ + { scorer_registry_name: 'refusal', identifier: { + class_name: 'SelfAskRefusalScorer', hash: 'refusal-hash', scorer_type: 'true_false', + } }, + { scorer_registry_name: 'inverted', identifier: { + class_name: 'TrueFalseInverterScorer', hash: 'inverted-hash', scorer_type: 'true_false', + } }, + { scorer_registry_name: 'substring', identifier: { + class_name: 'SubStringScorer', hash: 'substring-hash', scorer_type: 'true_false', + } }, + { scorer_registry_name: 'float-only', identifier: { + class_name: 'SelfAskLikertScorer', hash: 'float-hash', scorer_type: 'float_scale', + } }, + ] }) + const picker = await screen.findByRole('combobox', { name: 'Objective scorer' }) + expect(picker).toHaveValue('') + expect(screen.queryByRole('combobox', { name: 'allow_scorer' })).not.toBeInTheDocument() + expect(screen.queryByRole('combobox', { name: 'refuse_scorer' })).not.toBeInTheDocument() + expect(within(picker).queryByRole('option', { name: /float-only/ })).not.toBeInTheDocument() + expect(within(picker).getByRole('option', { name: /refusal - SelfAskRefusalScorer/ })).toBeInTheDocument() + await user.selectOptions(picker, 'registered:refusal') + const reference = { registry_name: 'refusal', identifier_hash: 'refusal-hash' } + await waitFor(() => { + expect(mockEstimateRun.mock.calls.at(-1)?.[1].objective_scorer).toEqual(reference) + }) + await user.selectOptions(picker, '') + await waitFor(() => { + expect(mockEstimateRun.mock.calls.at(-1)?.[1]).not.toHaveProperty('objective_scorer') + }) + await user.selectOptions(picker, 'registered:refusal') + await confirmRunPreview(user) + expect(mockStartRun.mock.calls[0][0]).toMatchObject({ + scenario_name: 'airt.cbrn', + objective_scorer: reference, + scenario_params: { num_trials: 5 }, + }) + }) + + it('disables scorer-incompatible techniques and excludes them from tag selections', async () => { + const user = userEvent.setup() + const reason = 'This technique cannot accept the population-specific scoring configuration.' + mockGetScenario.mockResolvedValue(makeScenario({ + all_techniques: ['default_technique', 'crescendo', 'tap'], + technique_summaries: [ + { name: 'default_technique', description: null, tags: ['single_turn'] }, + { name: 'crescendo', description: null, tags: ['single_turn'] }, + { name: 'tap', description: null, tags: ['single_turn'], unavailable_reason: reason }, + ], + })) + renderDetail('/scanner/launch/foundry.red_team_agent') + const tap = await screen.findByRole('checkbox', { name: 'tap', exact: true }) + expect(tap).toBeDisabled() + expect(tap).toHaveAccessibleDescription(reason) + await user.click(tap) + expect(tap).not.toBeChecked() + await user.click(screen.getAllByRole('button', { name: 'Select Single-turn techniques' })[0]) + expect(screen.getByTestId('technique-default_technique')).toBeChecked() + expect(screen.getByTestId('technique-crescendo')).toBeChecked() + expect(tap).not.toBeChecked() + await waitFor(() => { + expect(mockEstimateRun.mock.calls.at(-1)?.[1].techniques).toEqual(['default_technique', 'crescendo']) + }) + }) + it('keeps the existing component selectors without inline creation or readiness fields', async () => { const user = userEvent.setup() mockGetScenario.mockResolvedValue(makeScenario({ supports_objective_scorer: true })) diff --git a/frontend/src/components/Scenarios/ScenarioDetail.tsx b/frontend/src/components/Scenarios/ScenarioDetail.tsx index 9c79e2ae00..deb184b54e 100644 --- a/frontend/src/components/Scenarios/ScenarioDetail.tsx +++ b/frontend/src/components/Scenarios/ScenarioDetail.tsx @@ -689,11 +689,12 @@ function ScenarioLaunchForm({ ...BASELINE_TECHNIQUE, isBaseline: true, disabled: isBaselineForbidden, + unavailable_reason: isBaselineForbidden ? 'This scenario does not support a baseline comparison.' : null, }] : []), ...(supportsTechniques ? techniqueOptions.map((technique) => ({ ...technique, isBaseline: false, - disabled: false, + disabled: Boolean(technique.unavailable_reason), })) : []), ], [isBaselineForbidden, supportsBaseline, supportsTechniques, techniqueOptions], @@ -740,7 +741,11 @@ function ScenarioLaunchForm({ const techniqueSelectionOverLimit = supportsTechniques && scenario.max_concrete_techniques != null && selectedTechniques.length > scenario.max_concrete_techniques - const techniqueSelectionError = supportsTechniques && selectedTechniques.length === 0 + const hasIncompatibleSelection = selectedTechniques.some((name) => + techniqueOptions.some((technique) => technique.name === name && technique.unavailable_reason)) + const techniqueSelectionError = hasIncompatibleSelection + ? 'Clear incompatible techniques before launching.' + : supportsTechniques && selectedTechniques.length === 0 ? 'Select at least one attack technique.' : techniqueSelectionOverLimit ? `Select at most ${scenario.max_concrete_techniques} concrete technique(s).` @@ -1061,7 +1066,7 @@ function ScenarioLaunchForm({ className={styles.launchButton} appearance="primary" type="submit" - disabled={!runtime.ready || !defaultsReady || staleSelection || unavailableSelection || submitting || conversationBusy || Boolean(techniqueEditError) || techniqueSelectionError !== null || datasetMetadata.preparing || (supportsDatasets && datasetError !== null)} + disabled={!runtime.ready || !defaultsReady || staleSelection || unavailableSelection || submitting || conversationBusy || Boolean(techniqueEditError) || Boolean(scorerError) || techniqueSelectionError !== null || datasetMetadata.preparing || (supportsDatasets && datasetError !== null)} data-testid="launch-scenario-btn" > Launch scan diff --git a/frontend/src/components/Scenarios/ScenarioRunPage.test.tsx b/frontend/src/components/Scenarios/ScenarioRunPage.test.tsx index b819cce051..e553e84faa 100644 --- a/frontend/src/components/Scenarios/ScenarioRunPage.test.tsx +++ b/frontend/src/components/Scenarios/ScenarioRunPage.test.tsx @@ -13,15 +13,18 @@ import type { } from '@/types' import { scenarioRunAttackRoutePath, scenarioRunRoutePath } from '@/utils/routeParams' import { INITIAL_SCENARIO_RUN_PROGRESS_STATE, type ScenarioRunProgressState } from '@/utils/scenarioRunProgress' +import { downloadFile } from '@/utils/downloadFile' import ScenarioRunPage from './ScenarioRunPage' jest.mock('@/hooks/useScenarioRunProgress', () => ({ useScenarioRunProgress: jest.fn() })) jest.mock('@/hooks/useScenarioQueue', () => ({ useScenarioQueue: jest.fn() })) +jest.mock('@/utils/downloadFile', () => ({ downloadFile: jest.fn() })) jest.mock('@/services/api', () => ({ scenariosApi: { cancelRun: jest.fn(), resumeRun: jest.fn(), getLaunchConfiguration: jest.fn(), startRun: jest.fn(), getQueue: jest.fn(), getRunHierarchy: jest.fn(() => new Promise(() => {})), getRunHierarchyNode: jest.fn(), getPairedOutcomes: jest.fn(), getPairedOutcomeDetails: jest.fn(), + getRunArtifacts: jest.fn(), } })) const mockUseScenarioQueue = useScenarioQueue as jest.Mock @@ -131,6 +134,91 @@ beforeEach(() => { }) describe('ScenarioRunPage', () => { + it.each(['COMPLETED', 'FAILED', 'CANCELLED'] as const)('downloads artifacts to disk for a %s run beside Rerun', async (status) => { + const user = userEvent.setup() + state = makeState({ run: { ...RUN, status } }) + const artifact = new Blob([JSON.stringify({ metadata: { expected: 'ALLOW' }, attack_results: {} })], { type: 'application/json' }) + jest.mocked(scenariosApi.getRunArtifacts).mockResolvedValue(artifact) + render() + const button = screen.getByRole('button', { name: 'Download artifacts' }) + expect(button.nextElementSibling).toBe(screen.getByRole('button', { name: 'Rerun' })) + await user.click(button) + expect(scenariosApi.getRunArtifacts).toHaveBeenCalledWith(RUN_ID, expect.any(AbortSignal)) + await waitFor(() => expect(downloadFile).toHaveBeenCalledWith(artifact, `copyrit-scenario-${RUN_ID}.json`)) + const confirmation = screen.getByLabelText('Artifact download confirmation') + expect(within(confirmation).getByText(`copyrit-scenario-${RUN_ID}.json`)).toBeInTheDocument() + expect(confirmation).toHaveTextContent('Download started:') + expect(confirmation).toHaveTextContent("Check your browser's download location") + expect(confirmation).toHaveTextContent('The browser does not expose the exact saved path to this page.') + expect(scenariosApi.startRun).not.toHaveBeenCalled() + }) + + it('disables artifacts while the run is active', async () => { + const user = userEvent.setup() + render() + const button = screen.getByRole('button', { name: 'Download artifacts' }) + expect(button).toBeDisabled() + await user.click(button) + expect(scenariosApi.getRunArtifacts).not.toHaveBeenCalled() + }) + + it('reports artifacts errors without downloading and allows retry', async () => { + const user = userEvent.setup() + state = makeState({ run: { ...RUN, status: 'COMPLETED' } }) + jest.mocked(scenariosApi.getRunArtifacts).mockRejectedValueOnce(new Error('Artifacts unavailable')) + render() + await user.click(screen.getByRole('button', { name: 'Download artifacts' })) + expect(await screen.findByText('Artifacts unavailable')).toBeInTheDocument() + expect(downloadFile).not.toHaveBeenCalled() + expect(screen.queryByLabelText('Artifact download confirmation')).not.toBeInTheDocument() + expect(screen.getByRole('button', { name: 'Download artifacts' })).toBeEnabled() + }) + + it('allows dismissing the artifacts download confirmation', async () => { + const user = userEvent.setup() + state = makeState({ run: { ...RUN, status: 'COMPLETED' } }) + jest.mocked(scenariosApi.getRunArtifacts).mockResolvedValue(new Blob(['{}'], { type: 'application/json' })) + render() + await user.click(screen.getByRole('button', { name: 'Download artifacts' })) + expect(await screen.findByLabelText('Artifact download confirmation')).toBeInTheDocument() + await user.click(screen.getByRole('button', { name: 'Dismiss download confirmation' })) + expect(screen.queryByLabelText('Artifact download confirmation')).not.toBeInTheDocument() + }) + + it('does not confirm artifacts when the browser cannot start the download', async () => { + const user = userEvent.setup() + state = makeState({ run: { ...RUN, status: 'COMPLETED' } }) + jest.mocked(scenariosApi.getRunArtifacts).mockResolvedValue(new Blob(['{}'], { type: 'application/json' })) + jest.mocked(downloadFile).mockImplementationOnce(() => { throw new Error('Downloads blocked') }) + render() + await user.click(screen.getByRole('button', { name: 'Download artifacts' })) + expect(await screen.findByText('Downloads blocked')).toBeInTheDocument() + expect(screen.queryByLabelText('Artifact download confirmation')).not.toBeInTheDocument() + expect(screen.getByRole('button', { name: 'Download artifacts' })).toBeEnabled() + }) + + it.each(['Leave run', 'Open attempt'])('aborts artifacts and ignores a late download after %s', async (navigation) => { + const user = userEvent.setup() + state = makeState({ run: { ...RUN, status: 'COMPLETED' } }) + let resolveArtifact: ((blob: Blob) => void) | undefined + let requestSignal: AbortSignal | undefined + jest.mocked(scenariosApi.getRunArtifacts).mockImplementation((_id, signal) => new Promise((resolve) => { + resolveArtifact = resolve + requestSignal = signal + })) + render() + await user.click(screen.getByRole('button', { name: 'Download artifacts' })) + expect(screen.getByRole('button', { name: 'Downloading...' })).toBeDisabled() + await user.click(screen.getByRole('button', { name: navigation })) + expect(requestSignal?.aborted).toBe(true) + const resolve = resolveArtifact + if (!resolve) throw new Error('The artifact request did not start.') + await act(async () => { resolve(new Blob(['{}'], { type: 'application/json' })) }) + expect(downloadFile).not.toHaveBeenCalled() + expect(screen.queryByLabelText('Artifact download confirmation')).not.toBeInTheDocument() + if (navigation === 'Open attempt') expect(screen.getByRole('button', { name: 'Download artifacts' })).toBeEnabled() + }) + it('keeps comparison optional on completed scans and cancels it when collapsed', async () => { const user = userEvent.setup() state = makeState({ @@ -313,6 +401,30 @@ describe('ScenarioRunPage', () => { expect(screen.queryByRole('heading', { name: 'Paired outcomes' })).not.toBeInTheDocument() }) + it('shows trial ASR for objectives without changing the group attack outcome or completion', () => { + state = makeState({ + summary: { + ...SUMMARY, + seed_groups: [{ + ...SUMMARY.seed_groups[0], + trial_outcomes: { + successes: 3, failures: 2, errors: 0, undetermined: 0, + total_results: 5, total_decided: 5, decided_share: 1, + success_rate: 0.6, success_rate_decided: 0.6, success_rate_all: 0.6, + outcome_shares: { success: 0.6, failure: 0.4, error: 0, undetermined: 0 }, + }, + }], + }, + }) + render() + const objectives = within(screen.getByRole('table', { name: 'Datasets and objectives' })) + const row = within(objectives.getByRole('row', { name: /First objective/ })) + expect(row.getByText('60% (3/5 trials)', { exact: true })).toBeInTheDocument() + expect(row.getByText('1 / 1', { exact: true })).toBeInTheDocument() + const attacks = within(screen.getByRole('table', { name: 'Direct attack results' })) + expect(attacks.getByText('True', { exact: true })).toBeInTheDocument() + }) + it('keeps interrupted saved conversations reachable without inventing an outcome row', () => { state = makeState({ results: [], activeConversations: [], diff --git a/frontend/src/components/Scenarios/ScenarioRunPage.tsx b/frontend/src/components/Scenarios/ScenarioRunPage.tsx index 7ace137a32..cc02e007cc 100644 --- a/frontend/src/components/Scenarios/ScenarioRunPage.tsx +++ b/frontend/src/components/Scenarios/ScenarioRunPage.tsx @@ -3,14 +3,14 @@ import { useEffect, useMemo, useRef, useState } from 'react' import { Accordion, AccordionHeader, AccordionItem, AccordionPanel, Badge, Button, Dialog, DialogActions, DialogBody, DialogContent, DialogSurface, DialogTitle, - MessageBar, MessageBarBody, ProgressBar, Spinner, Table, TableBody, TableCell, + MessageBar, MessageBarActions, MessageBarBody, ProgressBar, Spinner, Table, TableBody, TableCell, TableHeader, TableHeaderCell, TableRow, Text, } from '@fluentui/react-components' -import { ArrowLeftRegular, ArrowClockwiseRegular, ChevronDownRegular, ChevronRightRegular, DismissRegular, PlayRegular, StopRegular } from '@fluentui/react-icons' +import { ArrowLeftRegular, ArrowClockwiseRegular, ArrowDownloadRegular, ChevronDownRegular, ChevronRightRegular, DismissRegular, PlayRegular, StopRegular } from '@fluentui/react-icons' import { Link, useLocation, useNavigate, useParams } from 'react-router' import AttackAttemptDetails from '@/components/AttackResults/AttackAttemptDetails' -import { formatProgressSuccess, objectivePreview } from '@/components/AttackResults/attackAttemptFormatting' +import { formatProgressSuccess, formatSeedGroupSuccess, objectivePreview } from '@/components/AttackResults/attackAttemptFormatting' import RegistryIdentityRow from '@/components/Registry/RegistryIdentityRow' import { suiteExecutionRoutePath } from '@/components/ScenarioSuites/suiteRoutes' import { useScenarioRunProgress } from '@/hooks/useScenarioRunProgress' @@ -36,6 +36,7 @@ import { scenarioRunRoutePath, } from '@/utils/routeParams' import { isTerminalRunState } from '@/utils/scenarioRunProgress' +import { downloadFile } from '@/utils/downloadFile' import AttackExecutionTable from './AttackExecutionTable' import PairedOutcomes from './PairedOutcomes' @@ -62,6 +63,14 @@ interface ScenarioRunContentProps { readonly attackResultId: string | null } +interface ArtifactDownloadState { + readonly pathname: string + readonly request: AbortController + readonly pending: boolean + readonly filename?: string + readonly error?: string +} + function seedDatasetNames(identities: ScenarioComponentIdentity[]): string { return [...new Set(identities.flatMap((identity: ScenarioComponentIdentity) => typeof identity.parameters.dataset_name === 'string' ? [identity.parameters.dataset_name] : []))].join(', ') @@ -102,6 +111,9 @@ function ScenarioRunContent({ scenarioResultId, attackResultId }: ScenarioRunCon const [rerunPending, setRerunPending] = useState(false) const [rerunError, setRerunError] = useState(null) const rerunRequest = useRef(null) + const [downloadState, setDownloadState] = useState(null) + const download = downloadState?.pathname === location.pathname ? downloadState : null + const downloadRequest = useRef(null) const detailsTrigger = useRef(null) const objectiveTrigger = useRef(null) const overviewTarget = useRef(null) @@ -121,7 +133,10 @@ function ScenarioRunContent({ scenarioResultId, attackResultId }: ScenarioRunCon useEffect(() => () => { rerunRequest.current?.abort() rerunRequest.current = null + downloadRequest.current?.abort() + downloadRequest.current = null }, [location.pathname]) + const seedObjectives = useMemo( () => new Map(state.plan?.seed_groups.map((seed) => [seed.id, seed.objective]) ?? []), [state.plan], @@ -267,6 +282,30 @@ function ScenarioRunContent({ scenarioResultId, attackResultId }: ScenarioRunCon } } + async function downloadArtifacts(): Promise { + downloadRequest.current?.abort() + const controller = new AbortController() + downloadRequest.current = controller + const requestState = { pathname: location.pathname, request: controller, pending: true } + setDownloadState(requestState) + try { + const artifact = await scenariosApi.getRunArtifacts(scenarioResultId, controller.signal) + if (downloadRequest.current !== controller || controller.signal.aborted) return + const filename = `copyrit-scenario-${scenarioResultId.replace(/[^a-zA-Z0-9_-]/g, '_')}.json` + downloadFile(artifact, filename) + setDownloadState({ ...requestState, pending: false, filename }) + } catch (error: unknown) { + if (downloadRequest.current !== controller || controller.signal.aborted) return + setDownloadState({ ...requestState, pending: false, error: toApiError(error).detail }) + } finally { + if (downloadRequest.current === controller) { + downloadRequest.current = null + } + setDownloadState((current: ArtifactDownloadState | null) => + current?.request === controller && current.pending ? { ...current, pending: false } : current) + } + } + async function cancel(): Promise { setCancelling(true) setCancelError(null) @@ -329,12 +368,29 @@ function ScenarioRunContent({ scenarioResultId, attackResultId }: ScenarioRunCon
{active && View queue} + {active ? : (run.status === 'FAILED' || run.status === 'CANCELLED') && }
+ {download?.filename && + + Download started: {download.filename}. + Check your browser's download location (usually Downloads), or the folder you chose in a Save as dialog. + The browser does not expose the exact saved path to this page. + + +