An open-source, faster Devin alternative
TypeScript
Render a settled swarm run from its report, default to grid, show the run id
Mohamed Habib via Computer0253e504a99c
diff --git a/web/src/managed-agents/security/ProjectSwarm.tsx b/web/src/managed-agents/security/ProjectSwarm.tsxindex 1ad043d2..8b510993 100644--- a/web/src/managed-agents/security/ProjectSwarm.tsx+++ b/web/src/managed-agents/security/ProjectSwarm.tsx@@ -12,13 +12,21 @@ import {import {latestManifestId,parseRunManifest,+ parseRunReport,runManifestDocumentId,+ runReportDocumentId,SECURITY_FINDINGS_RESOURCE,type SwarmRunManifest,+ type SwarmRunReport,} from './catalog'import { RunReportCard, SwarmGrid } from './SwarmGrid'import { SwarmMap } from './SwarmMap'-import { useSwarmRun } from './useSwarmRun'+import {+ statesFromReport,+ useSwarmRun,+ type CellState,+ type SwarmCounts,+} from './useSwarmRun'// One swarm run. The route carries the run id; without one we fall back to the// latest manifest in the project so older `/swarm` links still land somewhere.@@ -56,13 +64,18 @@ export default function ProjectSwarm() {{/* This view runs outside the app shell, so it carries its own slim barback to the project's swarms list. */}<header className="bg-background/80 sticky top-0 z-10 border-b backdrop-blur">- <div className="mx-auto flex h-12 max-w-6xl items-center px-4">+ <div className="mx-auto flex h-12 max-w-6xl items-center justify-between gap-3 px-4"><Linkto={`/projects/${encodeURIComponent(projectId)}/swarms`}className="text-muted-foreground hover:text-foreground inline-flex items-center gap-1.5 text-sm"><ArrowLeft className="size-3.5" /> Back to swarms</Link>+ {manifest.data ? (+ <span className="text-muted-foreground truncate font-mono text-xs">+ {manifest.data.runId}+ </span>+ ) : null}</div></header><div className="mx-auto max-w-6xl px-4 py-6">@@ -88,6 +101,10 @@ export default function ProjectSwarm() {)}+// A settled run leaves a persisted report. Reading it back lets us render the+// whole run from one document — no per-cell session polling, each of which would+// wake a durable object. We only fall through to the live poll when no report+// exists yet, i.e. the run is still going.function RunView({projectId,manifest,@@ -95,7 +112,58 @@ function RunView({projectId: stringmanifest: SwarmRunManifest}) {- const [view, setView] = useState<'map' | 'grid'>('map')+ const report = useQuery({+ queryKey: ['swarm-report', projectId, manifest.runId],+ queryFn: async (): Promise<SwarmRunReport | null> => {+ try {+ const read = await getManagedMemoryDocument({+ projectId,+ resource: SECURITY_FINDINGS_RESOURCE,+ environment: 'default',+ id: runReportDocumentId(manifest.runId),+ })+ return parseRunReport(read.document.text)+ } catch {+ // No report document yet — the run is still in flight.+ return null+ }+ },+ })++ if (report.isPending) {+ return (+ <div className="text-muted-foreground inline-flex items-center gap-2 text-sm">+ <Loader2 className="size-4 animate-spin" /> Loading the run…+ </div>+ )+ }++ if (report.data) {+ const { states, counts } = statesFromReport(report.data)+ return (+ <RunBody+ projectId={projectId}+ manifest={manifest}+ states={states}+ counts={counts}+ settled+ report={report.data}+ />+ )+ }++ return <LiveRun projectId={projectId} manifest={manifest} />+}++// The run is still going: poll it live. Splitting this out keeps the hook off the+// settled path entirely, so a finished run never starts an interval.+function LiveRun({+ projectId,+ manifest,+}: {+ projectId: string+ manifest: SwarmRunManifest+}) {const run = useSwarmRun({projectId,runId: manifest.runId,@@ -103,6 +171,37 @@ function RunView({target: manifest.target,model: manifest.model,})+ return (+ <RunBody+ projectId={projectId}+ manifest={manifest}+ states={run.states}+ counts={run.counts}+ settled={run.settled}+ report={run.report}+ />+ )+}++function RunBody({+ projectId,+ manifest,+ states,+ counts,+ settled,+ report,+}: {+ projectId: string+ manifest: SwarmRunManifest+ states: CellState[]+ counts: SwarmCounts+ settled: boolean+ report: SwarmRunReport | null+}) {+ // While the run is live the map tells the story; once it settles the grid is+ // the artifact, so we default there. An explicit toggle overrides either.+ const [override, setOverride] = useState<'map' | 'grid' | null>(null)+ const view = override ?? (settled ? 'grid' : 'map')return (<div className="space-y-5">@@ -114,13 +213,13 @@ function RunView({<div className="bg-muted inline-flex items-center rounded-md p-0.5"><ToggleButtonactive={view === 'map'}- onClick={() => setView('map')}+ onClick={() => setOverride('map')}icon={Network}label="Map"/><ToggleButtonactive={view === 'grid'}- onClick={() => setView('grid')}+ onClick={() => setOverride('grid')}icon={Grid3x3}label="Grid"/>@@ -129,21 +228,19 @@ function RunView({{view === 'map' ? (<div className="space-y-4">- <SwarmMap- states={run.states}- counts={run.counts}- settled={run.settled}- />- {run.report ? <RunReportCard report={run.report} /> : null}+ <SwarmMap states={states} counts={counts} settled={settled} />+ {report ? (+ <RunReportCard report={report} projectId={projectId} />+ ) : null}</div>) : (<SwarmGridprojectId={projectId}runId={manifest.runId}- states={run.states}- counts={run.counts}- settled={run.settled}- report={run.report}+ states={states}+ counts={counts}+ settled={settled}+ report={report}/>)}</div>diff --git a/web/src/managed-agents/security/SwarmGrid.tsx b/web/src/managed-agents/security/SwarmGrid.tsxindex ce75f0c8..5923f541 100644--- a/web/src/managed-agents/security/SwarmGrid.tsx+++ b/web/src/managed-agents/security/SwarmGrid.tsx@@ -95,7 +95,7 @@ export function SwarmGrid({))}</div>- {report ? <RunReportCard report={report} /> : null}+ {report ? <RunReportCard report={report} projectId={projectId} /> : null}</div>)}@@ -104,7 +104,15 @@ export function SwarmGrid({// artifact — the actionable verdicts rolled up and ranked. Tier 1 computes it// from the cell findings the grid already holds; a later reducer agent can// replace this with a written narrative over the same persisted document.-export function RunReportCard({ report }: { report: SwarmRunReport }) {+export function RunReportCard({+ report,+ projectId,+}: {+ report: SwarmRunReport+ // When set, each row links to its cell's session — the way back into what the+ // agent actually did, which matters most for a failed cell.+ projectId?: string+}) {const actionable = report.cells.filter((c) => c.disposition !== 'not-exposed',)@@ -133,7 +141,11 @@ export function RunReportCard({ report }: { report: SwarmRunReport }) {) : (<ul className="divide-y">{actionable.map((c) => (- <ReportRow key={`${c.repository}__${c.cveId}`} cell={c} />+ <ReportRow+ key={`${c.repository}__${c.cveId}`}+ cell={c}+ projectId={projectId}+ />))}</ul>)}@@ -141,7 +153,13 @@ export function RunReportCard({ report }: { report: SwarmRunReport }) {)}-function ReportRow({ cell }: { cell: ReportCell }) {+function ReportRow({+ cell,+ projectId,+}: {+ cell: ReportCell+ projectId?: string+}) {const meta =cell.disposition === 'failed'? { label: 'Failed', className: 'text-muted-foreground border-border' }@@ -166,6 +184,15 @@ function ReportRow({ cell }: { cell: ReportCell }) {{cell.reasoning}</p>) : null}+ {projectId && cell.sessionId ? (+ <Link+ to={`/projects/${encodeURIComponent(projectId)}/sessions/${encodeURIComponent(cell.sessionId)}`}+ className="text-muted-foreground hover:text-foreground mt-1 inline-flex items-center gap-1 text-xs hover:underline"+ >+ <ArrowUpRight className="size-3" />+ Open session+ </Link>+ ) : null}</div>{cell.pullRequest ? (<adiff --git a/web/src/managed-agents/security/catalog.ts b/web/src/managed-agents/security/catalog.tsindex c7ca0681..08a99fad 100644--- a/web/src/managed-agents/security/catalog.ts+++ b/web/src/managed-agents/security/catalog.ts@@ -295,6 +295,9 @@ export type ReportCell = {readonly disposition: ReportDispositionreadonly reasoning?: stringreadonly pullRequest?: string+ // The cell's session, carried so a persisted report can still link each row+ // (especially a failed one) back to what the agent did.+ readonly sessionId?: string}export type SwarmReportCounts = {@@ -350,6 +353,62 @@ export function serializeRunReport(report: SwarmRunReport): string {return JSON.stringify(report)}+const REPORT_DISPOSITIONS: readonly ReportDisposition[] = [+ 'exposed',+ 'not-exposed',+ 'needs-review',+ 'failed',+]++// Read a persisted report back. Returns null for anything malformed so the+// caller falls back to recomputing the run live. A valid report lets the run+// page render without polling every cell's session (which would wake each one).+export function parseRunReport(text: string): SwarmRunReport | null {+ let parsed: unknown+ try {+ parsed = JSON.parse(text)+ } catch {+ return null+ }+ if (!parsed || typeof parsed !== 'object') return null+ const obj = parsed as Record<string, unknown>+ if (typeof obj.runId !== 'string' || !Array.isArray(obj.cells)) return null+ const cells: ReportCell[] = []+ for (const entry of obj.cells) {+ if (!entry || typeof entry !== 'object') return null+ const e = entry as Record<string, unknown>+ if (+ typeof e.repository !== 'string' ||+ typeof e.cveId !== 'string' ||+ typeof e.disposition !== 'string' ||+ !REPORT_DISPOSITIONS.includes(e.disposition as ReportDisposition)+ ) {+ return null+ }+ cells.push({+ repository: e.repository,+ cveId: e.cveId,+ disposition: e.disposition as ReportDisposition,+ ...(typeof e.reasoning === 'string' ? { reasoning: e.reasoning } : {}),+ ...(typeof e.pullRequest === 'string'+ ? { pullRequest: e.pullRequest }+ : {}),+ ...(typeof e.sessionId === 'string' ? { sessionId: e.sessionId } : {}),+ })+ }+ if (cells.length === 0) return null+ return buildRunReport(+ {+ runId: obj.runId,+ target: typeof obj.target === 'string' ? obj.target : '',+ model: typeof obj.model === 'string' ? obj.model : '',+ generatedAt:+ typeof obj.generatedAt === 'string' ? obj.generatedAt : '',+ },+ cells,+ )+}+// Compose the controller's first-run input from the payload. This is exactly// what the swarm agent receives as its opening instruction when the session// starts — the scan → prove → report pipeline, with fixes gated by thediff --git a/web/src/managed-agents/security/useSwarmRun.ts b/web/src/managed-agents/security/useSwarmRun.tsindex 60a63844..4eef1f06 100644--- a/web/src/managed-agents/security/useSwarmRun.ts+++ b/web/src/managed-agents/security/useSwarmRun.ts@@ -101,6 +101,42 @@ export type SwarmRunData = {report: SwarmRunReport | null}+function tallyCounts(states: readonly CellState[]): SwarmCounts {+ return states.reduce<SwarmCounts>(+ (acc, state) => {+ if (state.phase === 'done' && state.finding)+ acc[state.finding.disposition] += 1+ else if (state.phase === 'failed') acc.failed += 1+ else acc.pending += 1+ return acc+ },+ { exposed: 0, 'not-exposed': 0, 'needs-review': 0, failed: 0, pending: 0 },+ )+}++// Rebuild the cell grid/map straight from a persisted report, so a settled run+// renders without polling any session. Every cell is already resolved: a verdict+// is 'done', a verdict-less cell is 'failed'.+export function statesFromReport(report: SwarmRunReport): {+ states: CellState[]+ counts: SwarmCounts+} {+ const states: CellState[] = report.cells.map((c) => ({+ cell: { repository: c.repository, cveId: c.cveId },+ sessionId: c.sessionId ?? '',+ finding:+ c.disposition === 'failed'+ ? null+ : {+ disposition: c.disposition,+ ...(c.reasoning ? { reasoning: c.reasoning } : {}),+ ...(c.pullRequest ? { pullRequest: c.pullRequest } : {}),+ },+ phase: c.disposition === 'failed' ? 'failed' : 'done',+ }))+ return { states, counts: tallyCounts(states) }+}+// Poll one run's sessions (for liveness) and each cell's findings document (for// the verdict) until every cell settles, then roll the verdicts into the run// report and persist it once. Both the grid and the map view consume this, so@@ -194,16 +230,7 @@ export function useSwarmRun({phase: computeCellPhase(s.progress, s.finding, confirmed),}))- const counts: SwarmCounts = states.reduce(- (acc, state) => {- if (state.phase === 'done' && state.finding)- acc[state.finding.disposition] += 1- else if (state.phase === 'failed') acc.failed += 1- else acc.pending += 1- return acc- },- { exposed: 0, 'not-exposed': 0, 'needs-review': 0, failed: 0, pending: 0 },- )+ const counts = tallyCounts(states)const settled = confirmeduseEffect(() => {@@ -227,6 +254,7 @@ export function useSwarmRun({repository: s.cell.repository,cveId: s.cell.cveId,disposition,+ sessionId: s.sessionId,...(s.finding?.reasoning ? { reasoning: s.finding.reasoning } : {}),...(s.finding?.pullRequest? { pullRequest: s.finding.pullRequest }