Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

Entity chips that 404, six metrics published as zero, and a panel labelled Demo with nothing in it (#798)

Beenu Arora648bb06bcd3e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bd73a7224..70e5df64e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -225,6 +225,72 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
both the shipped range and the next major so a future bump fails loudly
instead of quietly downgrading isolation.
+- **Every entity chip in the Investigation Rail was a 404.**
+ `alert_rail.py` built its pivots as `/attack-graph?entity=…` and there has
+ never been an `attack-graph` route, so host, user, asset, IP and domain chips
+ all failed before the query parameter mattered. The rail's own docstring
+ described the working behaviour ("the same `?entity=` query the
+ AttackGraphView already parses"), and `pivot.ts` states as fact that the rail
+ emits `/graph?entity=…`; the code agreed with neither. Pivots now target
+ `/graph`, which reads the parameter and selects the node.
+
+ The existing test pinned all six strings under a docstring claiming the pin
+ "prevents an accidental rename". It cannot — it compares the producer against
+ a copy of itself, which is how a route that never existed stayed asserted.
+ `services/api/tests/test_pivot_routes_resolve.py` derives the route table
+ from `apps/web/src/app` and checks it against what `build_related_entities`
+ actually returns, so the comparison now runs in the direction that drifts.
+
+ The same test caught a second defect in the producer: values went into the
+ URL unencoded, so an asset named `Finance & Legal #2` pivoted to
+ `Finance & Legal` and looked like it had worked. Values are now encoded the
+ way `pivot.ts` encodes them.
+
+- **Six funnel metrics were published as zero whatever the database held.**
+ `_funnel_window` merges `_triage_quality`'s output into the dict it returns,
+ but the `FunnelMetrics(...)` call never named `triaged_alerts`,
+ `abstentions`, `abstention_rate`, `ungrounded_demotions`,
+ `mean_groundedness` or `scored_verdicts`, so all six fell back to their
+ field defaults on every response. A published zero is a stronger claim than
+ silence: it reads as "this tenant never abstained, and nothing was ever
+ demoted for being ungrounded". The existing test asserted the six names were
+ present in `model_fields`, which they were — declaring a field and forwarding
+ it are different things. The replacement drives the endpoint and fails for
+ any field the window computes and the response drops, including ones added
+ later.
+
+- **The Live Feed labelled an empty panel "Demo".** The seeded events were
+ gated behind `canUseDemoData()` in an earlier pass but `statusToLabel` was
+ not, so outside the hosted demo the panel rendered nothing at all under a
+ "Demo" pill whose tooltip read "showing demo data" — asserting the presence
+ of sample data that had just been correctly withheld. The pill now describes
+ what is on screen (`Live`, `Connected`, `Connecting…`, `Reconnecting…`,
+ `Offline`) and can only say `Demo` when seeded events are actually rendered,
+ and the idle panel carries an empty state naming what would fill it.
+
+- **The welcome banner quoted counts it had no source for and linked to a case
+ most deployments do not have.** It advertised "26 vendors" against a registry
+ of 84 and "25 named runbooks" with nothing holding either to the tree. The
+ connector figure now comes from `CONNECTOR_COUNT`, which is generated from
+ the connector registry and held to it by `scripts/generate_connector_count.py
+ --check`; the playbook figure is gone rather than guessed, because no
+ equivalent source exists. Its second call to action linked to
+ `/cases/INC-RT-001`, which exists only after the demo seed has run, so on
+ every other deployment the banner's own CTA was a dead link — that tip is now
+ gated on demo mode, takes its href from `demoDeeplink()` and says on its face
+ that it is sample data.
+
+- **`SavedViewsBar` updated its parent while rendering.** The auto-apply of a
+ default saved view ran in the render body and called `onApply`, which for
+ every caller is a setState on the page component. React rejects that outright
+ and makes no promise about processing the update, so the filters an analyst
+ expected restored were not reliably applied. The comment described a ref-flag
+ that did not exist; it was `useState` with no effect anywhere. The chip
+ highlight is now derived rather than stored, and the one genuine side effect
+ — handing the filters to the page — happens in an effect. The existing
+ "exactly once" test could not catch this because its `onApply` was a bare
+ spy that set no state; the new test uses a real parent.
+
- **The alerts list was empty on every deployment, under a row count that was
real.** `AlertListResponse` returns the rows under `items`; the web client
read `raw.alerts`, which is never present, so `Array.isArray(undefined)` was
diff --git a/apps/docs/docs/console/investigation-rail.md b/apps/docs/docs/console/investigation-rail.md
index 03f105e7c..218cd5d27 100644
--- a/apps/docs/docs/console/investigation-rail.md
+++ b/apps/docs/docs/console/investigation-rail.md
@@ -54,7 +54,9 @@ The rail groups related entities into four buckets so the analyst can scan them
| Workflow | case ID, playbook run ID, ticket key |
| Tenant | tenant slug, business unit, environment tag |
-Each entry has a `kind`, a `value`, an optional `displayLabel` (for example `[email protected] (Okta)`), and — most importantly — an optional `pivotPath`. When present, the rail renders the row as a `next/link`: clicking it sends the analyst straight to `AttackGraphView` or the corresponding workbench, with the entity prefocused. The whole point of this section is to make the queue a launchpad: every entity is a *verb*, not a label.
+Each entry has a `kind`, a `value`, an optional `displayLabel` (for example `[email protected] (Okta)`), and — most importantly — an optional `pivotPath`. When present, the rail renders the row as a `next/link`: clicking it sends the analyst straight to `AttackGraphView` or the corresponding workbench, with the entity prefocused. The whole point of this section is to make the queue a launchpad: every entity is a *verb*, not a label.
+
+Entity pivots target `/graph?entity=<kind>:<value>`, url-encoded. `/graph` is the only route that reads the parameter and selects the node, so a pivot pointing anywhere else either 404s or silently drops the entity and lands the analyst on a generic page. `services/api/tests/test_pivot_routes_resolve.py` checks what the rail emits against the routes `apps/web` actually defines, in both directions.
The grouping is computed server-side in `services/api/app/services/alert_rail.py` so the frontend never has to re-sort the list, and so two analysts looking at the same alert see exactly the same buckets in the same order.
@@ -111,15 +113,15 @@ The response is the `AlertDetailResponse` Pydantic contract:
// ─── rail envelope (added in v1.5) ─────────────────────────────
"narrative": "Fusion linked an Okta impossible-travel signal …",
"related_entities": [
- { "kind": "principal", "value": "[email protected]",
- "display_label": "[email protected] (Okta)",
- "pivot_path": "/graph?focus=user:analyst%40tryaisoc.com" },
+ { "kind": "principal", "value": "[email protected]",
+ "display_label": "[email protected] (Okta)",
+ "pivot_path": "/graph?entity=user%3Aanalyst%40example.com" },
{ "kind": "network", "value": "203.0.113.42",
- "pivot_path": "/graph?focus=ip:203.0.113.42" }
+ "pivot_path": "/graph?entity=ip%3A203.0.113.42" }
],
"mini_timeline": [
{ "id": "evt-1", "kind": "comment", "title": "Initial triage",
- "actor": "[email protected]", "occurred_at": "2026-05-13T08:31:00Z" }
+ "actor": "[email protected]", "occurred_at": "2026-05-13T08:31:00Z" }
],
"recommended_actions": [
{ "priority": "critical",
diff --git a/apps/web/src/components/alerts/InvestigationRail.test.tsx b/apps/web/src/components/alerts/InvestigationRail.test.tsx
index ec27e0318..bdba08c30 100644
--- a/apps/web/src/components/alerts/InvestigationRail.test.tsx
+++ b/apps/web/src/components/alerts/InvestigationRail.test.tsx
@@ -126,7 +126,7 @@ const RELATED_ENTITIES: RelatedEntity[] = [
type: 'user',
label: 'Alice (Finance)',
- pivotPath: '/graph/user/alice%40example.com',
+ pivotPath: '/graph?entity=user%3Aalice%40example.com',
},
{
kind: 'principal',
@@ -137,7 +137,7 @@ const RELATED_ENTITIES: RelatedEntity[] = [
kind: 'network',
type: 'ip',
value: '10.0.0.7',
- pivotPath: '/graph/ip/10.0.0.7',
+ pivotPath: '/graph?entity=ip%3A10.0.0.7',
},
{
kind: 'workflow',
@@ -437,7 +437,7 @@ describe('InvestigationRail — related entities section', () => {
expect(aliceLink).not.toBeNull();
expect(aliceLink).toHaveAttribute(
'href',
- '/graph/user/alice%40example.com',
+ '/graph?entity=user%3Aalice%40example.com',
);
// The non-pivotable host does NOT render an anchor — `closest('a')` should
@@ -447,7 +447,7 @@ describe('InvestigationRail — related entities section', () => {
// The network IP chip renders an anchor too.
const ipLink = screen.getByText('10.0.0.7').closest('a');
- expect(ipLink).toHaveAttribute('href', '/graph/ip/10.0.0.7');
+ expect(ipLink).toHaveAttribute('href', '/graph?entity=ip%3A10.0.0.7');
});
it('omits the section entirely when no related entities are returned', () => {
diff --git a/apps/web/src/components/dashboard/DashboardWelcome.test.tsx b/apps/web/src/components/dashboard/DashboardWelcome.test.tsx
new file mode 100644
index 000000000..ab966caf2
--- /dev/null
+++ b/apps/web/src/components/dashboard/DashboardWelcome.test.tsx
@@ -0,0 +1,97 @@
+/**
+ * The welcome banner is the first thing a new operator reads, and it was
+ * wrong in both directions a coaching card can be wrong.
+ *
+ * It advertised "26 vendors" against a registry of 84, and "25 named
+ * runbooks" with nothing holding that to the pack on disk. And its second
+ * CTA linked to `/cases/INC-RT-001`, a case that exists only after the demo
+ * seed has run — so on every other deployment the banner's own call to action
+ * was a 404, which is the dead-control rule as much as the honesty one.
+ *
+ * These assert on first paint with `?welcome=1`, which is the only state the
+ * banner has.
+ */
+
+import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
+import { cleanup, render, screen } from '@testing-library/react';
+import { __setDemoModeForTests } from '@/lib/demoMode';
+import { CONNECTOR_COUNT } from '@/data/connectorCount';
+
+vi.mock('next/link', () => ({
+ __esModule: true,
+ default: ({ children, href }: { children: React.ReactNode; href: string }) => (
+ <a href={href}>{children}</a>
+ ),
+}));
+
+const replaceMock = vi.hoisted(() => vi.fn());
+vi.mock('next/navigation', () => ({
+ __esModule: true,
+ useRouter: () => ({ push: vi.fn(), replace: replaceMock, refresh: vi.fn() }),
+ usePathname: () => '/dashboard',
+ useSearchParams: () => new URLSearchParams('welcome=1'),
+}));
+
+import { DashboardWelcome } from './DashboardWelcome';
+
+function hrefs(): string[] {
+ return Array.from(document.querySelectorAll('a')).map((a) => a.getAttribute('href') ?? '');
+}
+
+beforeEach(() => {
+ __setDemoModeForTests(false);
+ replaceMock.mockReset();
+});
+
+afterEach(() => {
+ cleanup();
+ __setDemoModeForTests(null);
+});
+
+describe('DashboardWelcome outside demo mode', () => {
+ it('quotes the connector count from the generated registry, not a literal', () => {
+ render(<DashboardWelcome />);
+
+ expect(screen.getByTestId('dashboard-welcome')).toBeTruthy();
+ expect(screen.getByText(new RegExp(`Pick from ${CONNECTOR_COUNT} vendors`))).toBeTruthy();
+ // The stale literal must be gone rather than merely nudged.
+ expect(screen.queryByText(/26 vendors/)).toBeNull();
+ });
+
+ it('publishes no playbook count it cannot source', () => {
+ render(<DashboardWelcome />);
+
+ expect(screen.queryByText(/25 named runbooks/i)).toBeNull();
+ expect(screen.getByText(/named runbooks for ransomware/i)).toBeTruthy();
+ });
+
+ it('offers no link to a case that only the demo seed creates', () => {
+ render(<DashboardWelcome />);
+
+ for (const href of hrefs()) {
+ expect(href, 'seeded-case deeplink must be gated behind demo mode').not.toMatch(
+ /INC-RT-001/,
+ );
+ }
+ expect(screen.queryByText(/sample case/i)).toBeNull();
+ });
+
+ it('every remaining call to action points at a route the console defines', () => {
+ render(<DashboardWelcome />);
+
+ // Both survive on any deployment: the connector gallery and the playbook
+ // gallery are static routes, not seeded content.
+ expect(hrefs()).toEqual(['/onboarding', '/playbooks']);
+ });
+});
+
+describe('DashboardWelcome in demo mode', () => {
+ it('restores the sample-case tip and marks it as sample data', () => {
+ __setDemoModeForTests(true);
+
+ render(<DashboardWelcome />);
+
+ expect(screen.getByText(/sample data, not tenant data/i)).toBeTruthy();
+ expect(hrefs()).toContain('/cases/INC-RT-001?tab=ledger');
+ });
+});
diff --git a/apps/web/src/components/dashboard/DashboardWelcome.tsx b/apps/web/src/components/dashboard/DashboardWelcome.tsx
index ebe976f1a..e8774050e 100644
--- a/apps/web/src/components/dashboard/DashboardWelcome.tsx
+++ b/apps/web/src/components/dashboard/DashboardWelcome.tsx
@@ -3,15 +3,33 @@
import Link from 'next/link';
import { useEffect, useState } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
+import clsx from 'clsx';
+import { CONNECTOR_COUNT } from '@/data/connectorCount';
+import { demoDeeplink, isDemoMode } from '@/lib/demoMode';
/**
* Empty-state coaching banner shown after the "Skip & explore" CTA on the
* onboarding root page (WS-A2). Triggered by `?welcome=1`.
*
- * The banner explains the three highest-leverage next steps an operator can
- * take from a cold dashboard, then self-clears the querystring so a refresh
- * doesn't re-trigger it. We deliberately keep this lightweight — WS-F5 owns
- * the deep empty-state polish for every list view.
+ * The banner explains the highest-leverage next steps an operator can take
+ * from a cold dashboard, then self-clears the querystring so a refresh doesn't
+ * re-trigger it. We deliberately keep this lightweight — WS-F5 owns the deep
+ * empty-state polish for every list view.
+ *
+ * Two things the copy is not allowed to do, both of which it did.
+ *
+ * It claimed "26 vendors" and "25 named runbooks". The first was wrong by a
+ * factor of three; the second happened to be right and had no mechanism to
+ * stay that way. The connector figure now comes from `CONNECTOR_COUNT`, which
+ * is generated from the connector registry and held to it by
+ * `scripts/generate_connector_count.py --check` in CI, so it cannot drift. The
+ * playbook figure has no such source, so the number is gone rather than
+ * guessed — the tip reads fine without it.
+ *
+ * And it linked to `/cases/INC-RT-001`, a case that only exists once the demo
+ * seed has run. On any other deployment the banner's own CTA was a 404, so the
+ * whole tip is now gated on demo mode and takes its href from `demoDeeplink()`
+ * rather than hard-coding the seeded id.
*/
export function DashboardWelcome() {
const router = useRouter();
@@ -36,6 +54,29 @@ export function DashboardWelcome() {
if (!open) return null;
+ const tips: TipContent[] = [
+ {
+ title: 'Connect a source',
+ body: `Pick from ${CONNECTOR_COUNT} vendors. EDR + cloud + IAM gives the agent enough signal to start triaging.`,
+ cta: { label: 'Open the connector gallery →', href: '/onboarding' },
+ },
+ // Seeded data, and a link that only resolves once it has been seeded.
+ ...(isDemoMode()
+ ? [
+ {
+ title: 'Open the sample investigation',
+ body: 'Sample data, not tenant data: an in-flight ransomware case seeded for this demo.',
+ cta: { label: 'Open the sample case →', href: demoDeeplink() },
+ },
+ ]
+ : []),
+ {
+ title: 'Browse playbooks',
+ body: 'Named runbooks for ransomware, BEC, account takeover, cloud takeover, and more.',
+ cta: { label: 'Open the playbook gallery →', href: '/playbooks' },
+ },
+ ];
+
return (
<section
role="region"
@@ -64,41 +105,22 @@ export function DashboardWelcome() {
</button>
</div>
- <ul className="mt-4 grid gap-3 sm:grid-cols-3">
- <Tip
- step="1"
- title="Connect a source"
- body="Pick from 26 vendors. EDR + cloud + IAM gives the agent enough signal to start triaging."
- cta={{ label: 'Open the connector gallery →', href: '/onboarding' }}
- />
- <Tip
- step="2"
- title="Or load the demo seed"
- body="Run pnpm seed:demo and refresh. You'll get an in-flight LockBit case to investigate."
- cta={{ label: 'Open a sample case →', href: '/cases/INC-RT-001?tab=ledger' }}
- />
- <Tip
- step="3"
- title="Browse playbooks"
- body="25 named runbooks for ransomware, BEC, account takeover, cloud-TO, and more."
- cta={{ label: 'Open the playbook gallery →', href: '/playbooks' }}
- />
+ <ul className={clsx('mt-4 grid gap-3', tips.length === 3 ? 'sm:grid-cols-3' : 'sm:grid-cols-2')}>
+ {tips.map((tip, i) => (
+ <Tip key={tip.title} step={String(i + 1)} {...tip} />
+ ))}
</ul>
</section>
);
}
-function Tip({
- step,
- title,
- body,
- cta,
-}: {
- step: string;
+interface TipContent {
title: string;
body: string;
cta: { label: string; href: string };
-}) {
+}
+
+function Tip({ step, title, body, cta }: TipContent & { step: string }) {
return (
<li className="rounded-lg border border-surface-border bg-surface-card/60 p-4">
<div className="flex items-center gap-2">
diff --git a/apps/web/src/components/dashboard/LiveFeedPanel.test.tsx b/apps/web/src/components/dashboard/LiveFeedPanel.test.tsx
new file mode 100644
index 000000000..4bd6ebcf7
--- /dev/null
+++ b/apps/web/src/components/dashboard/LiveFeedPanel.test.tsx
@@ -0,0 +1,134 @@
+/**
+ * The Live Feed's status pill has to describe what is on screen.
+ *
+ * The seeded events were gated behind `canUseDemoData()` in an earlier pass,
+ * but `statusToLabel` was not. So outside the hosted demo the panel rendered
+ * an empty box labelled "Demo", with a tooltip reading "showing demo data" —
+ * asserting the presence of sample data that had just been correctly withheld.
+ * Two different wrong answers to "is any of this real?".
+ *
+ * Every assertion here is on first paint. There is no error branch to hide
+ * behind: an idle socket is the steady state for a fresh install, which is
+ * exactly the reader most likely to be misled.
+ */
+
+import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
+import { cleanup, render, screen } from '@testing-library/react';
+import { __setDemoModeForTests } from '@/lib/demoMode';
+
+const channelState = vi.hoisted(() => ({
+ status: 'open' as string,
+ last: null as unknown,
+}));
+
+vi.mock('@/lib/realtime', () => ({
+ __esModule: true,
+ useRealtimeChannel: () => ({
+ status: channelState.status,
+ last: channelState.last,
+ history: [],
+ send: vi.fn(),
+ reset: vi.fn(),
+ }),
+}));
+
+import { LiveFeedPanel, statusToLabel } from './LiveFeedPanel';
+
+/** A line from the seeded set, verbatim from `DEMO_EVENTS`. */
+const SEEDED_TEXT = /Ransomware indicators detected on DESKTOP-7892/i;
+
+beforeEach(() => {
+ channelState.status = 'open';
+ channelState.last = null;
+ __setDemoModeForTests(false);
+});
+
+afterEach(() => {
+ cleanup();
+ __setDemoModeForTests(null);
+});
+
+describe('LiveFeedPanel outside demo mode', () => {
+ it('does not label an empty feed "Demo" when the socket is open', () => {
+ render(<LiveFeedPanel />);
+
+ expect(screen.queryByText(SEEDED_TEXT)).toBeNull();
+ expect(screen.getByTestId('live-feed-status').textContent).toBe('Connected');
+ });
+
+ it('does not label an empty feed "Demo" when the socket is down', () => {
+ channelState.status = 'closed';
+
+ render(<LiveFeedPanel />);
+
+ expect(screen.queryByText(SEEDED_TEXT)).toBeNull();
+ expect(screen.getByTestId('live-feed-status').textContent).toBe('Offline');
+ });
+
+ it('names what would fill the panel instead of leaving it blank', () => {
+ render(<LiveFeedPanel />);
+
+ const empty = screen.getByTestId('live-feed-empty');
+ expect(empty.textContent).toMatch(/no alerts yet/i);
+ expect(empty.textContent).toMatch(/connectors poll/i);
+ });
+
+ it('says the socket is down rather than that there is nothing to report', () => {
+ channelState.status = 'error';
+
+ render(<LiveFeedPanel />);
+
+ expect(screen.getByTestId('live-feed-empty').textContent).toMatch(
+ /not receiving events/i,
+ );
+ });
+
+ it('never claims to be showing demo data in its tooltip', () => {
+ for (const status of ['open', 'connecting', 'closing', 'closed', 'error']) {
+ channelState.status = status;
+ render(<LiveFeedPanel />);
+ const pill = screen.getByTestId('live-feed-status');
+ expect(pill.textContent, status).not.toBe('Demo');
+ expect(pill.getAttribute('title') ?? '', status).not.toMatch(/demo data/i);
+ cleanup();
+ }
+ });
+});
+
+describe('LiveFeedPanel in demo mode', () => {
+ it('shows the seeded events and labels them as such', () => {
+ __setDemoModeForTests(true);
+
+ render(<LiveFeedPanel />);
+
+ // The gate is "sample data is labelled", not "sample data is banned" —
+ // the hosted demo has no backend and these events are the whole point.
+ expect(screen.getByText(SEEDED_TEXT)).toBeTruthy();
+ const pill = screen.getByTestId('live-feed-status');
+ expect(pill.textContent).toBe('Demo');
+ expect(pill.getAttribute('title')).toMatch(/seeded sample events, not tenant data/i);
+ expect(screen.queryByTestId('live-feed-empty')).toBeNull();
+ });
+});
+
+describe('statusToLabel', () => {
+ it('only ever returns "Demo" when seeded events are actually rendered', () => {
+ for (const status of ['open', 'connecting', 'closing', 'closed', 'error'] as const) {
+ for (const hasReal of [true, false]) {
+ const { label, tone } = statusToLabel(status, hasReal, false);
+ expect(label, `${status}/${hasReal}`).not.toBe('Demo');
+ expect(tone, `${status}/${hasReal}`).not.toBe('demo');
+ }
+ }
+ expect(statusToLabel('closed', false, true).label).toBe('Demo');
+ });
+
+ it('reports a live socket carrying real events as live', () => {
+ expect(statusToLabel('open', true, false).label).toBe('Live');
+ });
+
+ it('distinguishes a dropped socket that had events from one that never did', () => {
+ expect(statusToLabel('closed', true, false).label).toBe('Reconnecting…');
+ expect(statusToLabel('closed', false, false).label).toBe('Offline');
+ });
+});
diff --git a/apps/web/src/components/dashboard/LiveFeedPanel.tsx b/apps/web/src/components/dashboard/LiveFeedPanel.tsx
index e72dbe17b..8357fa4ff 100644
--- a/apps/web/src/components/dashboard/LiveFeedPanel.tsx
+++ b/apps/web/src/components/dashboard/LiveFeedPanel.tsx
@@ -4,15 +4,21 @@
* LiveFeedPanel
*
* Subscribes to the realtime `alerts` channel and renders the most recent
- * fused alerts as a live-streaming list. If the realtime service is not
- * reachable (common in dev when only the web app is running), the panel
- * gracefully falls back to a small set of demo events so the UI never
- * appears broken.
+ * fused alerts as a live-streaming list.
*
- * The status pill reflects the actual WebSocket state:
- * - "Live" → connected and receiving
- * - "Reconnecting" → connecting / closing / closed (auto-retry)
- * - "Demo" → no real events received yet, showing seeded data
+ * Outside the hosted demo an idle feed renders an empty state naming what
+ * would fill it. It used to render seeded events instead; that was gated on
+ * `canUseDemoData()`, but the pill was not, so a self-hoster with a healthy
+ * socket and no traffic yet saw an empty panel labelled "Demo" over a tooltip
+ * reading "showing demo data" — a claim about data that was not on screen.
+ *
+ * The pill now reports the state the panel is actually in:
+ * - "Live" → connected, real events received
+ * - "Connected" → socket open, nothing has arrived yet
+ * - "Connecting…" → handshake in flight
+ * - "Reconnecting…" → dropped after receiving real events (auto-retry)
+ * - "Offline" → socket down and nothing to show
+ * - "Demo" → and only ever → seeded events are on screen
*/
import { useEffect, useMemo, useRef, useState } from 'react';
@@ -151,17 +157,29 @@ function relativeTime(receivedAt: number, now: number): string {
return `${days}d ago`;
}
-function statusToLabel(status: RealtimeStatus, hasReal: boolean): {
+/**
+ * The pill describes what is on screen, not what we wish were on screen.
+ *
+ * `showingDemo` is the only input that may produce "Demo", so the label can
+ * never assert seeded data while the list is empty.
+ */
+export function statusToLabel(
+ status: RealtimeStatus,
+ hasReal: boolean,
+ showingDemo: boolean,
+): {
label: string;
- tone: 'live' | 'reconnect' | 'demo';
+ tone: 'live' | 'reconnect' | 'demo' | 'idle';
} {
- if (status === 'open' && hasReal) return { label: 'Live', tone: 'live' };
- if (status === 'open') return { label: 'Demo', tone: 'demo' };
- if (status === 'connecting') return { label: 'Connecting…', tone: 'reconnect' };
- if (status === 'closing' || status === 'closed' || status === 'error') {
- return { label: hasReal ? 'Reconnecting…' : 'Demo', tone: hasReal ? 'reconnect' : 'demo' };
+ if (showingDemo) return { label: 'Demo', tone: 'demo' };
+ if (hasReal) {
+ return status === 'open'
+ ? { label: 'Live', tone: 'live' }
+ : { label: 'Reconnecting…', tone: 'reconnect' };
}
- return { label: 'Demo', tone: 'demo' };
+ if (status === 'open') return { label: 'Connected', tone: 'idle' };
+ if (status === 'connecting') return { label: 'Connecting…', tone: 'reconnect' };
+ return { label: 'Offline', tone: 'idle' };
}
function eventFromMessage(msg: RealtimeAlertMessage, fallbackId: number): LiveEvent | null {
@@ -239,7 +257,8 @@ export function LiveFeedPanel() {
}));
}, [events, hasReal, now]);
- const pill = statusToLabel(status, hasReal);
+ const showingDemo = !hasReal && visible.length > 0;
+ const pill = statusToLabel(status, hasReal, showingDemo);
return (
<div className="bg-[#111620] border border-gray-800/60 rounded-lg p-4">
@@ -250,21 +269,23 @@ export function LiveFeedPanel() {
'w-2 h-2 rounded-full',
pill.tone === 'live' && 'bg-emerald-400 animate-pulse',
pill.tone === 'reconnect' && 'bg-amber-400 animate-pulse',
- pill.tone === 'demo' && 'bg-gray-500',
+ (pill.tone === 'demo' || pill.tone === 'idle') && 'bg-gray-500',
)}
/>
Live Feed
</h3>
<span
+ data-testid="live-feed-status"
className={clsx(
'text-[10px] uppercase tracking-wide px-2 py-0.5 rounded-full border',
pill.tone === 'live' && 'bg-emerald-500/10 text-emerald-300 border-emerald-500/30',
pill.tone === 'reconnect' && 'bg-amber-500/10 text-amber-300 border-amber-500/30',
- pill.tone === 'demo' && 'bg-gray-500/10 text-gray-400 border-gray-700',
+ (pill.tone === 'demo' || pill.tone === 'idle') &&
+ 'bg-gray-500/10 text-gray-400 border-gray-700',
)}
title={
pill.tone === 'demo'
- ? 'Realtime service unreachable or no events received yet — showing demo data'
+ ? 'Showing seeded sample events, not tenant data'
: `WebSocket: ${status}`
}
>
@@ -273,6 +294,13 @@ export function LiveFeedPanel() {
</div>
<div className="space-y-2.5 overflow-y-auto max-h-52 pr-1">
+ {visible.length === 0 && (
+ <p data-testid="live-feed-empty" className="text-xs text-gray-500 leading-relaxed">
+ {status === 'open'
+ ? 'No alerts yet. Fused alerts stream in here as your connectors poll and detections fire.'
+ : 'Not receiving events. This panel streams fused alerts from the realtime service; check that it is running and reachable.'}
+ </p>
+ )}
{visible.map((event) => (
<div key={event.id} className="flex items-start gap-2">
<span
diff --git a/apps/web/src/components/saved-views/SavedViewsBar.test.tsx b/apps/web/src/components/saved-views/SavedViewsBar.test.tsx
index 734c3ed6b..cfadc8d8f 100644
--- a/apps/web/src/components/saved-views/SavedViewsBar.test.tsx
+++ b/apps/web/src/components/saved-views/SavedViewsBar.test.tsx
@@ -1,6 +1,7 @@
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import { useState } from 'react';
// WS-F3 — pin the SavedViewsBar wiring. We care about four behaviors the
// downstream pages actually rely on:
@@ -172,6 +173,49 @@ describe('SavedViewsBar', () => {
expect(onDefaultLoaded).toHaveBeenCalledTimes(1);
});
+ it('applies the default without updating its parent mid-render', async () => {
+ // The auto-apply used to run in the render body, so `onApply` — which for
+ // every real caller is a setState on the *page* — fired while this
+ // component was rendering. React rejects that outright and does not
+ // guarantee the parent's update is processed.
+ //
+ // The existing "exactly once" test cannot catch it: its `onApply` is a
+ // bare `vi.fn()` that sets no state, so React has nothing to complain
+ // about. It takes a real parent to reproduce.
+ seedViews('alerts', [
+ makeView({ id: 'v-1', is_default: true, filters: { status: 'open' } }),
+ ]);
+
+ const consoleErrors: string[] = [];
+ const spy = vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
+ consoleErrors.push(args.map((a) => String(a)).join(' '));
+ });
+
+ function Page() {
+ const [applied, setApplied] = useState<TestFilters | null>(null);
+ return (
+ <>
+ <SavedViewsBar<TestFilters> viewType="alerts" filters={{}} onApply={setApplied} />
+ <span data-testid="applied-status">{applied?.status ?? 'none'}</span>
+ </>
+ );
+ }
+
+ try {
+ render(<Page />);
+ await waitFor(() => {
+ expect(screen.getByTestId('applied-status')).toHaveTextContent('open');
+ });
+ } finally {
+ spy.mockRestore();
+ }
+
+ const renderPhaseUpdates = consoleErrors.filter((line) =>
+ /Cannot update a component/.test(line),
+ );
+ expect(renderPhaseUpdates).toEqual([]);
+ });
+
it('does not auto-apply when no default view exists', () => {
seedViews('alerts', [makeView({ id: 'v-2', is_default: false })]);
const onApply = vi.fn();
diff --git a/apps/web/src/components/saved-views/SavedViewsBar.tsx b/apps/web/src/components/saved-views/SavedViewsBar.tsx
index 4d166c3cf..cb07473bb 100644
--- a/apps/web/src/components/saved-views/SavedViewsBar.tsx
+++ b/apps/web/src/components/saved-views/SavedViewsBar.tsx
@@ -23,7 +23,7 @@
* views) and would deserve its own controller.
*/
-import { useCallback, useState } from 'react';
+import { useCallback, useEffect, useRef, useState } from 'react';
import useSWR from 'swr';
import toast from 'react-hot-toast';
import { clsx } from 'clsx';
@@ -82,34 +82,62 @@ export function SavedViewsBar<TFilters extends object>({
revalidateOnFocus: false,
});
- // Track which preset is currently "active" so the chip can light up. We
- // identify by id rather than deep-comparing filters (which would be
- // brittle once columns join the picture).
- const [activeViewId, setActiveViewId] = useState<string | null>(null);
+ // Which preset is lit up. Identified by id rather than by deep-comparing
+ // filters, which would be brittle once columns join the picture.
+ //
+ // Derived, not stored: until the analyst picks something the highlight
+ // follows the default view, so loading it needs no state write at all. The
+ // wrapper object distinguishes "has not picked yet" (`null`) from "picked
+ // nothing", which is the state a delete leaves behind — collapsing the two
+ // would re-light the default chip the moment its row was removed.
+ const [picked, setPicked] = useState<{ id: string | null } | null>(null);
+ const defaultViewId = isLoading ? null : (views.find((v) => v.is_default)?.id ?? null);
+ const activeViewId = picked ? picked.id : defaultViewId;
+
const [openMenuId, setOpenMenuId] = useState<string | null>(null);
const [isSaveDialogOpen, setSaveDialogOpen] = useState(false);
const [renamingViewId, setRenamingViewId] = useState<string | null>(null);
- // Auto-apply the default view on first load. We use a ref-flag instead of
- // a useEffect dependency on ``views`` because we only ever want to fire
- // once per mount; subsequent SWR revalidations shouldn't re-clobber the
- // analyst's current filters.
- const [defaultApplied, setDefaultApplied] = useState(false);
- if (!defaultApplied && !isLoading && views.length > 0) {
+ // Keep the latest callbacks reachable from the effect below without putting
+ // them in its dependency list. Callers pass inline arrows, so a new identity
+ // arrives on every parent render; depending on them would re-run the effect
+ // constantly and reading a stale one would apply the wrong page's filters.
+ const onApplyRef = useRef(onApply);
+ const onDefaultLoadedRef = useRef(onDefaultLoaded);
+ useEffect(() => {
+ onApplyRef.current = onApply;
+ onDefaultLoadedRef.current = onDefaultLoaded;
+ });
+
+ // Hand the default view's filters to the page, once per mount.
+ //
+ // This ran in the render body until it was found to call `onApply` — which
+ // for every real caller is a setState on the page component — while this
+ // component was still rendering. React refuses that ("Cannot update a
+ // component while rendering a different component") and makes no promise
+ // about processing the parent's update, so the filters an analyst expected
+ // restored were not reliably applied. The ref-flag the old comment described
+ // did not exist; it was `useState`, with no effect anywhere.
+ //
+ // Notifying the page is the only thing left here, because the chip highlight
+ // is derived above. A ref for the flag so it cannot itself trigger a render,
+ // and so later SWR revalidations do not re-clobber filters the analyst has
+ // since changed.
+ const defaultAppliedRef = useRef(false);
+ useEffect(() => {
+ if (defaultAppliedRef.current || isLoading || views.length === 0) return;
+ defaultAppliedRef.current = true;
+
const def = views.find((v) => v.is_default);
- if (def) {
- setDefaultApplied(true);
- setActiveViewId(def.id);
- onApply(def.filters as TFilters);
- onDefaultLoaded?.(def);
- } else {
- setDefaultApplied(true);
- }
- }
+ if (!def) return;
+
+ onApplyRef.current(def.filters as TFilters);
+ onDefaultLoadedRef.current?.(def);
+ }, [views, isLoading]);
const apply = useCallback(
(view: SavedView) => {
- setActiveViewId(view.id);
+ setPicked({ id: view.id });
setOpenMenuId(null);
onApply(view.filters as TFilters);
},
@@ -126,7 +154,7 @@ export function SavedViewsBar<TFilters extends object>({
is_default: makeDefault,
});
await mutate();
- setActiveViewId(created.id);
+ setPicked({ id: created.id });
toast.success(`Saved view "${created.name}"`);
setSaveDialogOpen(false);
} catch (err) {
@@ -184,7 +212,7 @@ export function SavedViewsBar<TFilters extends object>({
filters: filters as Record<string, unknown>,
});
await mutate();
- setActiveViewId(view.id);
+ setPicked({ id: view.id });
toast.success(`Updated "${view.name}"`);
setOpenMenuId(null);
} catch (err) {
@@ -201,7 +229,7 @@ export function SavedViewsBar<TFilters extends object>({
try {
await savedViewsApi.delete(view.id);
await mutate();
- if (activeViewId === view.id) setActiveViewId(null);
+ if (activeViewId === view.id) setPicked({ id: null });
setOpenMenuId(null);
toast.success(`Deleted "${view.name}"`);
} catch (err) {
diff --git a/services/api/app/api/v1/endpoints/metrics.py b/services/api/app/api/v1/endpoints/metrics.py
index fa0f6fec3..7bf2df0b8 100644
--- a/services/api/app/api/v1/endpoints/metrics.py
+++ b/services/api/app/api/v1/endpoints/metrics.py
@@ -1109,4 +1109,10 @@ async def get_funnel_metrics(
generated_at=now,
repeat_alerts_suppressed=current.get("repeat_alerts_suppressed", 0),
repeat_suppression_rate=current.get("repeat_suppression_rate", 0.0),
+ triaged_alerts=current.get("triaged_alerts", 0),
+ abstentions=current.get("abstentions", 0),
+ abstention_rate=current.get("abstention_rate", 0.0),
+ ungrounded_demotions=current.get("ungrounded_demotions", 0),
+ mean_groundedness=current.get("mean_groundedness"),
+ scored_verdicts=current.get("scored_verdicts", 0),
)
diff --git a/services/api/app/services/alert_rail.py b/services/api/app/services/alert_rail.py
index 054e6b526..30dacfdff 100644
--- a/services/api/app/services/alert_rail.py
+++ b/services/api/app/services/alert_rail.py
@@ -36,6 +36,7 @@ from collections.abc import Iterable
from dataclasses import dataclass
from datetime import datetime
from typing import Any
+from urllib.parse import quote
from pydantic import BaseModel, Field
from sqlalchemy import select
@@ -182,6 +183,23 @@ class _EntityBucket:
self.entities.append(RelatedEntity(group=group, kind=kind, value=v, label=label, pivot=pivot))
+def _graph_pivot(kind: str, value: str) -> str:
+ """Deep-link into the entity graph with the node pre-selected.
+
+ The route is ``/graph`` — ``apps/web/src/app/(app)/graph/page.tsx``. There
+ has never been an ``/attack-graph`` route, so the earlier prefix 404'd
+ before the query string mattered; ``tests/test_pivot_routes_resolve.py``
+ now checks this against the routes the console actually defines.
+
+ The value is encoded exactly as the federated-search helper encodes it
+ (``apps/web/src/components/federated/pivot.ts``), because an asset named
+ ``Finance & Legal`` otherwise truncates at the ampersand and pivots to a
+ different entity while looking like it worked. ``AttackGraphView`` splits
+ on the first colon after Next has decoded the parameter.
+ """
+ return f"/graph?entity={quote(f'{kind}:{value}', safe='')}"
+
+
def build_related_entities(alert: Alert) -> list[RelatedEntity]:
"""Produce the rail's Related Entities list from an ``Alert`` row.
@@ -203,21 +221,21 @@ def build_related_entities(alert: Alert) -> list[RelatedEntity]:
group="principal",
kind="host",
value=host,
- pivot=f"/attack-graph?entity=host:{host}",
+ pivot=_graph_pivot("host", host),
)
for user in _dedup_strs(alert.affected_users or ()):
bucket.add(
group="principal",
kind="user",
value=user,
- pivot=f"/attack-graph?entity=user:{user}",
+ pivot=_graph_pivot("user", user),
)
for asset in _dedup_strs(alert.affected_assets or ()):
bucket.add(
group="principal",
kind="asset",
value=asset,
- pivot=f"/attack-graph?entity=asset:{asset}",
+ pivot=_graph_pivot("asset", asset),
)
# ── Network ─────────────────────────────────────────────────────────
@@ -226,7 +244,7 @@ def build_related_entities(alert: Alert) -> list[RelatedEntity]:
group="network",
kind="ip",
value=ip,
- pivot=f"/attack-graph?entity=ip:{ip}",
+ pivot=_graph_pivot("ip", ip),
)
dst_ip = _from_blob(raw_event, ("dst_ip", "destination_ip", "remote_ip"))
if dst_ip:
@@ -235,7 +253,7 @@ def build_related_entities(alert: Alert) -> list[RelatedEntity]:
kind="ip",
value=dst_ip,
label="destination",
- pivot=f"/attack-graph?entity=ip:{dst_ip}",
+ pivot=_graph_pivot("ip", dst_ip),
)
domain = _from_blob(raw_event, ("domain", "target_domain", "host_domain"))
if domain:
@@ -243,7 +261,7 @@ def build_related_entities(alert: Alert) -> list[RelatedEntity]:
group="network",
kind="domain",
value=domain,
- pivot=f"/attack-graph?entity=domain:{domain}",
+ pivot=_graph_pivot("domain", domain),
)
url = _from_blob(raw_event, ("url", "request_url", "uri"))
if url:
diff --git a/services/api/tests/api/v1/endpoints/test_alerts_detail_envelope.py b/services/api/tests/api/v1/endpoints/test_alerts_detail_envelope.py
index b15a64c5c..112400e2d 100644
--- a/services/api/tests/api/v1/endpoints/test_alerts_detail_envelope.py
+++ b/services/api/tests/api/v1/endpoints/test_alerts_detail_envelope.py
@@ -255,7 +255,7 @@ class TestAlertDetailResponseContract:
"kind": "host",
"value": "web-01",
"label": None,
- "pivot": "/attack-graph?entity=host:web-01",
+ "pivot": "/graph?entity=host%3Aweb-01",
}
],
mini_timeline=[
@@ -392,7 +392,7 @@ class TestGetAlertEndpoint:
kind="host",
value="web-01",
label=None,
- pivot="/attack-graph?entity=host:web-01",
+ pivot="/graph?entity=host%3Aweb-01",
)
],
mini_timeline=[],
@@ -600,7 +600,7 @@ class TestGetAlertEndpoint:
kind="ip",
value="10.0.0.1",
label=None,
- pivot="/attack-graph?entity=ip:10.0.0.1",
+ pivot="/graph?entity=ip%3A10.0.0.1",
),
],
mini_timeline=[
diff --git a/services/api/tests/services/test_alert_rail.py b/services/api/tests/services/test_alert_rail.py
index 35c5c5a98..ea5e8b313 100644
--- a/services/api/tests/services/test_alert_rail.py
+++ b/services/api/tests/services/test_alert_rail.py
@@ -213,8 +213,13 @@ class TestBuildRelatedEntities:
def test_pivot_routes_are_built_per_kind(self) -> None:
"""The pivot URL contract is part of the public API.
- The frontend keys off the pivot to decide whether a chip is
- clickable. Pinning the routes prevents an accidental rename.
+ The frontend keys off the pivot to decide whether a chip is clickable.
+
+ This pin cannot tell you the route exists — it compares the producer
+ against a copy of itself, which is how ``/attack-graph`` survived here
+ while ``apps/web`` defined no such route. ``test_pivot_routes_resolve.py``
+ checks these against the console's actual route table; this test only
+ guards the shape.
"""
entities = build_related_entities(
_alert(
@@ -225,9 +230,9 @@ class TestBuildRelatedEntities:
)
)
by_kind = {e.kind: e for e in entities}
- assert by_kind["host"].pivot == "/attack-graph?entity=host:h1"
- assert by_kind["user"].pivot == "/attack-graph?entity=user:u1"
- assert by_kind["ip"].pivot == "/attack-graph?entity=ip:1.2.3.4"
+ assert by_kind["host"].pivot == "/graph?entity=host%3Ah1"
+ assert by_kind["user"].pivot == "/graph?entity=user%3Au1"
+ assert by_kind["ip"].pivot == "/graph?entity=ip%3A1.2.3.4"
# ─── build_recommended_actions ───────────────────────────────────────────────
@@ -496,7 +501,7 @@ class TestPydanticContracts:
kind="host",
value="win-finance-07",
label="primary",
- pivot="/attack-graph?entity=host:win-finance-07",
+ pivot="/graph?entity=host%3Awin-finance-07",
)
dump = e.model_dump(mode="json")
assert set(dump) == {"group", "kind", "value", "label", "pivot"}
diff --git a/services/api/tests/test_pivot_routes_resolve.py b/services/api/tests/test_pivot_routes_resolve.py
new file mode 100644
index 000000000..b57fa161b
--- /dev/null
+++ b/services/api/tests/test_pivot_routes_resolve.py
@@ -0,0 +1,195 @@
+"""Every pivot the Investigation Rail emits must resolve to a real console route.
+
+``test_alert_rail.py`` already pins the pivot strings, with a docstring saying
+the pin "prevents an accidental rename". It cannot: it compares the producer
+against a copy of itself. For months it asserted ``/attack-graph?entity=…``
+while ``apps/web`` defined no ``attack-graph`` route at all, so every entity
+chip in the rail 404'd — and the pin reported OK the whole time, because
+nothing ever compared the emitted path against the routes that exist.
+
+This gate closes that direction. It derives the route table from the Next.js
+app directory on disk and checks it against what ``build_related_entities``
+*actually returns* for a fully-populated alert, rather than against a regex
+over the producer's source. A pivot that stops resolving fails here whether
+the producer moved or the route did.
+
+AiSOC — open-source AI Security Operations Center (MIT License)
+"""
+
+from __future__ import annotations
+
+import uuid
+from pathlib import Path
+from types import SimpleNamespace
+from typing import Any
+from urllib.parse import unquote, urlsplit
+
+import pytest
+from app.services.alert_rail import build_related_entities
+
+# services/api/tests/test_pivot_routes_resolve.py → repo root is four up.
+REPO_ROOT = Path(__file__).resolve().parents[3]
+APP_DIR = REPO_ROOT / "apps" / "web" / "src" / "app"
+
+_PAGE_SUFFIXES = {".tsx", ".ts", ".jsx", ".js"}
+
+
+def _route_patterns(app_dir: Path) -> set[tuple[str, ...]]:
+ """The addressable routes ``apps/web`` defines, as segment tuples.
+
+ Next.js App Router conventions: ``(group)`` segments are organisational and
+ contribute nothing to the URL, ``[param]`` matches one segment, ``[...rest]``
+ one or more, ``[[...rest]]`` zero or more. Directories under a ``@slot``
+ parallel route are not addressable on their own and are skipped.
+ """
+ patterns: set[tuple[str, ...]] = set()
+ for page in app_dir.rglob("page.*"):
+ if page.suffix not in _PAGE_SUFFIXES:
+ continue
+ segments: list[str] = []
+ for part in page.parent.relative_to(app_dir).parts:
+ if part.startswith("@"):
+ break
+ if part.startswith("(") and part.endswith(")"):
+ continue
+ segments.append(part)
+ else:
+ patterns.add(tuple(segments))
+ return patterns
+
+
+def _pattern_matches(pattern: tuple[str, ...], segments: tuple[str, ...]) -> bool:
+ index = 0
+ for part in pattern:
+ if part.startswith("[[...") and part.endswith("]]"):
+ return True
+ if part.startswith("[...") and part.endswith("]"):
+ return index < len(segments)
+ if index >= len(segments):
+ return False
+ if not (part.startswith("[") and part.endswith("]")) and part != segments[index]:
+ return False
+ index += 1
+ return index == len(segments)
+
+
+def _resolves(path: str, patterns: set[tuple[str, ...]]) -> bool:
+ segments = tuple(s for s in unquote(urlsplit(path).path).split("/") if s)
+ return any(_pattern_matches(p, segments) for p in patterns)
+
+
+def _fully_populated_alert() -> SimpleNamespace:
+ """One alert carrying every field the rail knows how to pivot on.
+
+ Populated rather than minimal on purpose: a pivot that is only built for
+ (say) a destination IP is exactly the one a sparse fixture never exercises.
+ """
+ base: dict[str, Any] = {
+ "id": uuid.uuid4(),
+ "tenant_id": uuid.uuid4(),
+ "severity": "high",
+ "title": "Suspicious authentication",
+ "connector_type": "okta",
+ "case_id": uuid.uuid4(),
+ "ai_recommendations": [],
+ "affected_ips": ["10.0.0.7"],
+ "affected_hosts": ["WIN-DC01"],
+ "affected_users": ["[email protected]"],
+ "affected_assets": ["Finance File Server"],
+ "mitre_tactics": [{"id": "TA0006", "name": "Credential Access"}],
+ "mitre_techniques": [{"id": "T1110", "name": "Brute Force"}],
+ "raw_event": {
+ "dst_ip": "198.51.100.9",
+ "domain": "updates.example.test",
+ "url": "https://updates.example.test/payload",
+ "rule_name": "Impossible travel",
+ },
+ "enrichment_data": {"rba_top_promotion": {"entity": "host:WIN-DC01"}},
+ }
+ return SimpleNamespace(**base)
+
+
[email protected](scope="module")
+def route_patterns() -> set[tuple[str, ...]]:
+ assert APP_DIR.is_dir(), (
+ f"Next.js app directory not found at {APP_DIR}. This gate compares the "
+ "rail's pivots against the routes the console defines; without the web "
+ "tree it would pass by doing nothing."
+ )
+ patterns = _route_patterns(APP_DIR)
+ assert patterns, f"no page.tsx found under {APP_DIR}"
+ return patterns
+
+
+def test_the_route_table_is_parsed_the_way_next_resolves_it(route_patterns):
+ """Guard the parser itself, so a silent mis-parse cannot make the gate vacuous.
+
+ If ``_route_patterns`` returned nothing useful every assertion below would
+ pass trivially, which is the failure mode this whole file exists to remove.
+ """
+ assert _resolves("/graph", route_patterns)
+ assert _resolves("/alerts", route_patterns)
+ # A route group contributes nothing to the URL: /graph lives at (app)/graph.
+ assert not _resolves("/(app)/graph", route_patterns)
+ # Dynamic segments match one segment, and only one.
+ assert _resolves("/cases/INC-1", route_patterns)
+ assert not _resolves("/cases/INC-1/extra", route_patterns)
+ # A route nobody defines must not resolve, or the gate proves nothing.
+ assert not _resolves("/attack-graph", route_patterns)
+ assert not _resolves("/no-such-route", route_patterns)
+
+
+def test_every_rail_pivot_resolves_to_a_defined_route(route_patterns):
+ entities = build_related_entities(_fully_populated_alert())
+ pivots = [(e.kind, e.pivot) for e in entities if e.pivot]
+ assert pivots, "fixture produced no pivots — the gate would pass vacuously"
+
+ broken = [(kind, pivot) for kind, pivot in pivots if not _resolves(pivot, route_patterns)]
+ assert not broken, "these pivots 404 — no route in apps/web/src/app matches them: " f"{broken}"
+
+
+def test_the_entity_pivots_target_the_graph_route_that_reads_the_parameter(
+ route_patterns,
+):
+ """The chip has to land on the page that honours ``?entity=``.
+
+ Resolving to *some* route is not enough. ``AttackGraphView`` is the only
+ view that reads the parameter and selects the node; any other resolving
+ path would silently drop the entity and land the analyst on a generic page,
+ which is the softer version of the same bug.
+ """
+ entities = build_related_entities(_fully_populated_alert())
+ graph_kinds = {"host", "user", "asset", "ip", "domain"}
+ targeted = [e for e in entities if e.kind in graph_kinds and e.pivot]
+ assert targeted, "fixture produced no entity pivots"
+
+ for entity in targeted:
+ split = urlsplit(entity.pivot)
+ assert split.path == "/graph", f"{entity.kind} pivot targets {split.path!r}; only /graph parses ?entity="
+ assert split.query.startswith("entity="), entity.pivot
+ assert _resolves(entity.pivot, route_patterns)
+
+
+def test_a_value_with_url_metacharacters_survives_the_round_trip():
+ """An unencoded value truncates the parameter at the first ``&`` or ``#``.
+
+ ``AttackGraphView`` splits on the first colon and uses the remainder, so the
+ producer has to encode the value the same way the federated-search helper
+ (``apps/web/src/components/federated/pivot.ts``) does — otherwise an asset
+ named ``Finance & Legal`` pivots to ``Finance`` and looks like it worked.
+ """
+ alert = _fully_populated_alert()
+ alert.affected_assets = ["Finance & Legal #2"]
+ alert.affected_hosts = []
+ alert.affected_users = []
+ alert.affected_ips = []
+ alert.raw_event = {}
+ alert.enrichment_data = {}
+
+ asset = next(e for e in build_related_entities(alert) if e.kind == "asset")
+ query = urlsplit(asset.pivot).query
+ assert query.count("=") == 1, f"value leaked out of the parameter: {asset.pivot}"
+
+ # Decoding the parameter has to give the value back unchanged.
+ raw = unquote(query.removeprefix("entity="))
+ assert raw == "asset:Finance & Legal #2"
diff --git a/services/api/tests/test_triage_quality_metrics.py b/services/api/tests/test_triage_quality_metrics.py
index c61b9cf3f..5f6e15234 100644
--- a/services/api/tests/test_triage_quality_metrics.py
+++ b/services/api/tests/test_triage_quality_metrics.py
@@ -24,6 +24,8 @@ flattering direction:
from __future__ import annotations
+from types import SimpleNamespace
+
import pytest
metrics = pytest.importorskip(
@@ -167,3 +169,61 @@ def test_the_funnel_model_exposes_every_field():
"scored_verdicts",
):
assert name in fields, f"{name} is computed but not exposed on FunnelMetrics"
+
+
+async def test_every_computed_window_field_reaches_the_response(monkeypatch):
+ """Declaring a field is not the same as forwarding it.
+
+ ``_funnel_window`` merges ``_triage_quality``'s output into the dict it
+ returns, but every field has to be named again in the ``FunnelMetrics(...)``
+ call to reach the client. Six were not, so the endpoint answered 0 for all
+ of them whatever the database held — and a published zero reads as "this
+ tenant never abstained, and nothing was ever demoted for being ungrounded",
+ which is a stronger claim than saying nothing at all.
+
+ The loop is deliberately generic rather than a list of the six: any field
+ the window computes and the model declares must survive the trip, including
+ ones added after this test was written.
+ """
+ window = {
+ "events_of_interest": 1_000,
+ "correlation_instances": 100,
+ "alerts_generated": 40,
+ "signal_to_noise": 0.6,
+ "mttd_seconds": 12.5,
+ "analyst_queue_depth": 7,
+ "correlation_efficiency": 0.1,
+ "alert_yield": 0.04,
+ "mitre_coverage": metrics.MitreCoverage(covered=3, total=10, ratio=0.3),
+ "repeat_alerts_suppressed": 5,
+ "repeat_suppression_rate": 0.1111,
+ "triaged_alerts": 100,
+ "abstentions": 25,
+ "abstention_rate": 0.25,
+ "ungrounded_demotions": 4,
+ "mean_groundedness": 0.91,
+ "scored_verdicts": 80,
+ }
+
+ async def _window(*_a, **_k):
+ return dict(window)
+
+ monkeypatch.setattr(metrics, "_funnel_window", _window)
+
+ out = await metrics.get_funnel_metrics(
+ user=SimpleNamespace(tenant_id="t-1"),
+ db=object(),
+ period="24h",
+ )
+
+ declared = metrics.FunnelMetrics.model_fields
+ unforwarded = [name for name, expected in window.items() if name in declared and getattr(out, name) != expected]
+ assert not unforwarded, f"computed but not forwarded to the response: {unforwarded}"
+
+ # Serialisation is what a client actually sees; a field can be set on the
+ # model and still be dropped by an alias or exclude rule.
+ payload = out.model_dump()
+ assert payload["abstention_rate"] == 0.25
+ assert payload["mean_groundedness"] == 0.91
+ assert payload["scored_verdicts"] == 80