Swarms: session metrics are project-wide, not scoped to the run
- Dominant language
- TypeScript
- Stars
- 2.2k
- Forks
- 283
- Avg merge
- 11h 47m
- Merged PRs (30d)
- 737
Description
Split out of #5184.
## What happens
The session metrics header on a swarm run reports a cohort far larger than the run:
| Where | Header says | Run actually has |
|---|---|---|
| prod `f2d291df` | 214 sessions in scope | 14 |
| prod, Sep 8 run | 30 sessions in scope | 15 |
| staging `c35bfd32` | 83 sessions in scope | 15 |
Tool errors, P50/P95 latency, tool calls per session and tokens are all computed over that wrong cohort, so every tile on a run page describes the project rather than the run being looked at.
## Cause
`client/src/components/swarms/swarm-sessions-metric-strip.tsx` calls the query with no run filter:
```ts
const metrics = useQuery(
SWARM_QUERIES.getSwarmSessionMetrics as any,
(projectId ? { projectId, ...(personaRefId ? { personaRefId } : {}) } : "skip") as any
);
```
The mount site (`SwarmsSessionsPanel.tsx`) already holds `journeyRunIds` and passes only `projectId` and `personaRefId`.
It cannot be fixed at the call site alone. `mcpjam-backend convex/journeyRuns.ts` `getSwarmSessionMetrics` accepts `{ projectId, personaRefId? }` and queries `chatSessions` by the `by_project_sourceType` index, capped at `SWARM_SESSION_METRICS_MAX` (2000) — that cap is why the number looks arbitrary rather than obviously wrong.
## Fix
Add an optional `journeyRunIds` arg to `getSwarmSessionMetrics` and pass the ids the panel already has. The query name is a public wire contract, but adding an optional arg is backward compatible (there is a comment in `journeyRuns.ts` to that effect).
Note `analyzedCount` feeds the "N of M sessions" line under the tokens tile from the same rows, so it is fixed by the same change.
## Related
`runSwarmClusterRebuild` in `convex/scenarioClusters.ts` is also scoped `{ projectId }` while the Insights *read* is wave-filtered. Same class, worth checking in the same pass.
Contributor guide
Research direction
Read client/src/components/swarms/swarm-sessions-metric-strip.tsx and SwarmsSessionsPanel.tsx first, then inspect mcpjam-backend convex/journeyRuns.ts and its getSwarmSessionMetrics entry point. Trace how journeyRunIds reaches the panel and how sessions are selected; done means every run-page metric, including analyzedCount, uses only that run’s sessions while preserving the optional query argument contract. Also review runSwarmClusterRebuild in convex/scenarioClusters.ts for the related project-wide scoping.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- analytics, backend-api-design, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100