Skip to content

An open-source, faster Devin alternative

TypeScript

fix(security): discover swarm cells by session id, not create-time labels

Mohamed Habib via Computereb1088d0c742
diff --git a/web/src/managed-agents/security/SecurityOnboarding.tsx b/web/src/managed-agents/security/SecurityOnboarding.tsx
index a9853a3d..2080af02 100644
--- a/web/src/managed-agents/security/SecurityOnboarding.tsx
+++ b/web/src/managed-agents/security/SecurityOnboarding.tsx
@@ -36,7 +36,6 @@ import {
import { installedTemplateAgentId } from '../template-continuation'
import {
cellDocumentId,
- cellLabels,
composeCellPrompt,
composeLaunchPrompt,
DEFAULT_SWARM_MODEL,
@@ -45,7 +44,7 @@ import {
SECURITY_SWARM_TEMPLATE_REPOSITORY,
SEED_CVES,
swarmCells,
- type SwarmCell,
+ type LaunchedCell,
type SwarmPayload,
} from './catalog'
import { SwarmGrid } from './SwarmGrid'
@@ -72,7 +71,7 @@ async function waitForInstallation(id: string) {
return installation
}
-type SwarmRun = { runId: string; cells: SwarmCell[] }
+type SwarmRun = { runId: string; cells: LaunchedCell[] }
export default function SecurityOnboarding() {
const [run, setRun] = useState<SwarmRun | null>(null)
@@ -186,8 +185,8 @@ export default function SecurityOnboarding() {
}
const runId = `run-${crypto.randomUUID()}`
const agentId = `${deployed.projectAgentId}@default`
- await Promise.all(
- cells.map(async (cell) => {
+ const launched = await Promise.all(
+ cells.map(async (cell): Promise<LaunchedCell> => {
const id = cellDocumentId(runId, cell)
await createManagedMemoryDocument({
projectId: deployed.projectId,
@@ -197,19 +196,23 @@ export default function SecurityOnboarding() {
title: `${cell.repository} · ${cell.cveId}`,
text: '{}',
})
- await startManagedAgentSession(agentId, composeCellPrompt(payload, cell), {
- memory: {
- [SECURITY_FINDINGS_RESOURCE]: {
- scope: 'document',
- id,
- access: 'read-write',
+ const { sessionId } = await startManagedAgentSession(
+ agentId,
+ composeCellPrompt(payload, cell),
+ {
+ memory: {
+ [SECURITY_FINDINGS_RESOURCE]: {
+ scope: 'document',
+ id,
+ access: 'read-write',
+ },
},
},
- labels: cellLabels(runId, cell),
- })
+ )
+ return { cell, sessionId }
}),
)
- return { runId, cells }
+ return { runId, cells: launched }
},
onSuccess: (started) => setRun(started),
onError: (error) => notifyError("Couldn't start the swarm.", error),
diff --git a/web/src/managed-agents/security/SwarmGrid.tsx b/web/src/managed-agents/security/SwarmGrid.tsx
index 1a5b7c95..8da71336 100644
--- a/web/src/managed-agents/security/SwarmGrid.tsx
+++ b/web/src/managed-agents/security/SwarmGrid.tsx
@@ -3,18 +3,16 @@ import { useQuery } from '@tanstack/react-query'
import { ExternalLink, Loader2, ShieldAlert, ShieldCheck, ShieldQuestion } from 'lucide-react'
import { cn } from '@/lib/utils'
import {
- getManagedAgentSessionsPage,
+ getManagedAgentSession,
getManagedMemoryDocument,
} from '../api'
import {
cellDocumentId,
- cellLabels,
parseCellFinding,
SECURITY_FINDINGS_RESOURCE,
- SWARM_CELL_LABEL,
- SWARM_RUN_LABEL,
type CellFinding,
type CveDisposition,
+ type LaunchedCell,
type SwarmCell,
} from './catalog'
@@ -67,23 +65,27 @@ export function SwarmGrid({
}: {
projectId: string
runId: string
- cells: readonly SwarmCell[]
+ cells: readonly LaunchedCell[]
}) {
- // The run's live sessions, keyed by their per-cell label so each tile can show
- // whether its worker is queued, running, or stopped.
+ // The run's live sessions, fetched by the ids the launch returned so each tile
+ // can show whether its worker is queued, running, or stopped.
const sessions = useQuery({
queryKey: ['swarm-sessions', projectId, runId],
queryFn: async () => {
- const page = await getManagedAgentSessionsPage({
- projectId,
- labels: { [SWARM_RUN_LABEL]: runId },
- })
- return page.sessions
+ const entries = await Promise.all(
+ cells.map(async ({ sessionId }) => {
+ const session = await getManagedAgentSession(sessionId)
+ return [sessionId, session.status] as const
+ }),
+ )
+ return Object.fromEntries(entries)
},
refetchInterval: (query) => {
const data = query.state.data
if (!data) return 3_000
- const allSettled = data.every((s) => isSessionSettled(s.status))
+ const allSettled = cells.every(({ sessionId }) =>
+ isSessionSettled(data[sessionId]),
+ )
return allSettled ? false : 3_000
},
})
@@ -94,7 +96,7 @@ export function SwarmGrid({
queryKey: ['swarm-findings', projectId, runId],
queryFn: async () => {
const entries = await Promise.all(
- cells.map(async (cell) => {
+ cells.map(async ({ cell }) => {
const read = await getManagedMemoryDocument({
projectId,
resource: SECURITY_FINDINGS_RESOURCE,
@@ -112,28 +114,23 @@ export function SwarmGrid({
refetchInterval: (query) => {
const data = query.state.data
if (!data) return 4_000
- const allFound = cells.every((cell) => data[cellDocumentId(runId, cell)])
+ const allFound = cells.every(
+ ({ cell }) => data[cellDocumentId(runId, cell)],
+ )
return allFound ? false : 4_000
},
})
- const sessionByCell = useMemo(() => {
- const map = new Map<string, string>()
- for (const session of sessions.data ?? []) {
- const key = session.labels[SWARM_CELL_LABEL]
- if (key) map.set(key, session.status)
- }
- return map
- }, [sessions.data])
+ const statusBySession = useMemo(
+ () => sessions.data ?? {},
+ [sessions.data],
+ )
- const states: CellState[] = cells.map((cell) => {
- const cellKey = cellLabels(runId, cell)[SWARM_CELL_LABEL]
- return {
- cell,
- sessionStatus: sessionByCell.get(cellKey),
- finding: findings.data?.[cellDocumentId(runId, cell)] ?? null,
- }
- })
+ const states: CellState[] = cells.map(({ cell, sessionId }) => ({
+ cell,
+ sessionStatus: statusBySession[sessionId],
+ finding: findings.data?.[cellDocumentId(runId, cell)] ?? null,
+ }))
const counts = states.reduce(
(acc, state) => {
diff --git a/web/src/managed-agents/security/catalog.test.ts b/web/src/managed-agents/security/catalog.test.ts
index 86fa24a5..ac5ea6e8 100644
--- a/web/src/managed-agents/security/catalog.test.ts
+++ b/web/src/managed-agents/security/catalog.test.ts
@@ -1,12 +1,9 @@
import { describe, expect, it } from 'vitest'
import {
cellDocumentId,
- cellLabels,
composeCellPrompt,
parseCellFinding,
swarmCells,
- SWARM_CELL_LABEL,
- SWARM_RUN_LABEL,
type SwarmPayload,
} from './catalog'
@@ -42,15 +39,6 @@ describe('swarm fan-out', () => {
expect(first).not.toBe(second)
})
- it('labels every cell with the run and a cell key', () => {
- const labels = cellLabels('run-abc', {
- repository: 'acme/web',
- cveId: 'CVE-2026-1',
- })
- expect(labels[SWARM_RUN_LABEL]).toBe('run-abc')
- expect(labels[SWARM_CELL_LABEL]).toBe('acme-web-cve-2026-1')
- })
-
it('narrows the prompt to a single repository and CVE', () => {
const prompt = composeCellPrompt(payload, {
repository: 'acme/web',
diff --git a/web/src/managed-agents/security/catalog.ts b/web/src/managed-agents/security/catalog.ts
index ac3c572a..040f1b5d 100644
--- a/web/src/managed-agents/security/catalog.ts
+++ b/web/src/managed-agents/security/catalog.ts
@@ -87,6 +87,14 @@ export type SwarmCell = {
readonly cveId: string
}
+// A cell once its session has been started: the launch pairs each cell with the
+// session id `startManagedAgentSession` returns, so the grid can poll that
+// session's liveness directly instead of discovering it by label.
+export type LaunchedCell = {
+ readonly cell: SwarmCell
+ readonly sessionId: string
+}
+
// Expand the payload into the matrix of cells. Fan-out needs explicit
// repositories and CVEs — the "all connected repositories" / "full feed"
// shorthands only make sense for the single-session controller, so a caller
@@ -118,21 +126,6 @@ export function cellDocumentId(runId: string, cell: SwarmCell): string {
return `${runId}__${slug(cell.repository)}__${slug(cell.cveId)}`
}
-// Labels every cell session carries, so the grid can list a run's sessions with
-// `label.swarmRun=<runId>` and map each back to its cell.
-export const SWARM_RUN_LABEL = 'swarmRun'
-export const SWARM_CELL_LABEL = 'swarmCell'
-
-export function cellLabels(
- runId: string,
- cell: SwarmCell,
-): Record<string, string> {
- return {
- [SWARM_RUN_LABEL]: runId,
- [SWARM_CELL_LABEL]: slug(`${cell.repository}--${cell.cveId}`),
- }
-}
-
// One cell's first-run input: a single repository checked against a single CVE.
// The worker agent already collapses to a 1×1 matrix when the payload names one
// of each, so this is the launch prompt narrowed to the cell.