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.

fix(agents,api): three investigation-depth gaps where the capability existed unused (#669)

Beenu Arorace6f24c94080
diff --git a/services/agents/app/agents/investigation_agent.py b/services/agents/app/agents/investigation_agent.py
index 8d142bd68..aa958c633 100644
--- a/services/agents/app/agents/investigation_agent.py
+++ b/services/agents/app/agents/investigation_agent.py
@@ -13,6 +13,7 @@ import structlog
from app.agents.dispositions import BENIGN, BENIGN_TRUE_POSITIVE, FALSE_POSITIVE, NEEDS_REVIEW
from app.confidence import score_investigation
+from app.context.bundle import ContextBundle
from app.investigator.splunk_evidence import collect_splunk_evidence
from app.models.state import ActionRisk, AgentStatus, InvestigationState, ProposedAction
from app.tools.mitre import lookup_technique
@@ -31,6 +32,32 @@ ATTRIBUTION_TIMEOUT_SECONDS = float(os.getenv("AISOC_ATTRIBUTION_TIMEOUT_SECONDS
THREAT_INTEL_SERVICE_TOKEN = os.getenv("AISOC_THREATINTEL_SERVICE_TOKEN", "").strip()
+def _bundle_findings(bundle: dict[str, Any] | None) -> list[str]:
+ """Salient facts from a ContextBundle, as investigation findings.
+
+ Deliberately narrow: only the summary fields the bundle itself declares
+ safe to surface, never raw OCSF or log payloads.
+ """
+ if not bundle:
+ return []
+ try:
+ summary = ContextBundle.model_validate(bundle).summary_for_llm()
+ except Exception as exc: # noqa: BLE001 — context is additive, never fatal
+ logger.debug("investigation.bundle_unreadable", error=str(exc))
+ return []
+
+ findings: list[str] = []
+ if summary.get("blast_radius_max"):
+ findings.append(f"Blast radius of the affected entities scores {summary['blast_radius_max']:.2f}.")
+ for line in (summary.get("neighborhood_summaries") or [])[:3]:
+ findings.append(f"Graph context: {line}")
+ historical = summary.get("historical_verdicts") or []
+ if historical:
+ verdicts = ", ".join(f"{h['verdict']} (similarity {h['similarity']:.2f})" for h in historical[:3])
+ findings.append(f"{len(historical)} similar historical case(s) resolved as: {verdicts}.")
+ return findings
+
+
async def run_investigation(state: InvestigationState) -> InvestigationState:
"""
Synthesize findings and generate an investigation report.
@@ -56,6 +83,14 @@ async def run_investigation(state: InvestigationState) -> InvestigationState:
# --- Perform threat actor attribution (best-effort, non-blocking on error) ---
await _perform_threat_actor_attribution(state, malicious_iocs)
+ # --- Pre-fetched context, when the escalation path supplied it ---
+ # This agent is deterministic, so the bundle is surfaced as findings
+ # rather than appended to a prompt. Without it, an auto-escalated alert
+ # was investigated with no knowledge of the entity's graph neighbourhood
+ # or how identical alerts had previously resolved.
+ for line in _bundle_findings(state.context_bundle):
+ state.add_finding(line)
+
# --- Generate narrative findings ---
if malicious_iocs:
state.add_finding(f"CONFIRMED THREAT: {len(malicious_iocs)} malicious IOC(s) identified. Immediate containment recommended.")
diff --git a/services/agents/app/agents/tool_loop.py b/services/agents/app/agents/tool_loop.py
index 736a9aa1a..aba405ba1 100644
--- a/services/agents/app/agents/tool_loop.py
+++ b/services/agents/app/agents/tool_loop.py
@@ -18,6 +18,7 @@ from typing import Any
import structlog
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
+from app.llm.contract import safe_ainvoke
from app.tools.registry import ToolRegistry
logger = structlog.get_logger()
@@ -41,7 +42,14 @@ async def run_with_tools(
trace: list[dict[str, Any]] = []
for iteration in range(1, max_iters + 1):
- response = await bound.ainvoke(messages)
+ # Routed through safe_ainvoke rather than calling bound.ainvoke
+ # directly. This loop feeds tool output straight back into the prompt,
+ # and that output is untrusted: a SIEM row, a graph property or a
+ # threat-intel record can carry text that reads as an instruction.
+ # Skipping the contract here skipped injection validation on precisely
+ # the highest-risk content in the system, and lost the token/cost
+ # telemetry for every tool-calling turn.
+ response = await safe_ainvoke(bound, messages)
messages.append(response)
tool_calls = getattr(response, "tool_calls", None) or []
if not tool_calls:
diff --git a/services/agents/app/models/state.py b/services/agents/app/models/state.py
index e165c302a..d389b6ade 100644
--- a/services/agents/app/models/state.py
+++ b/services/agents/app/models/state.py
@@ -84,6 +84,12 @@ class InvestigationState(BaseModel):
confidence_basis: list[str] = Field(default_factory=list)
verdict: str | None = None
+ # Pre-fetched investigation context (graph neighbourhood, blast radius,
+ # historical verdicts, UEBA baselines, TI matches). Built on the escalation
+ # path so an auto-escalated alert is investigated with the same context an
+ # analyst-initiated investigation of the same alert would have had.
+ context_bundle: dict[str, Any] | None = None
+
# Fraction of the concrete indicators cited in the reasoning that actually
# appear in the evidence (see app/confidence/groundedness.py). None means
# the verdict was not scored. Recorded alongside the verdict so an
diff --git a/services/agents/app/workers/fused_alert_consumer.py b/services/agents/app/workers/fused_alert_consumer.py
index 4d7224131..c0a06459b 100644
--- a/services/agents/app/workers/fused_alert_consumer.py
+++ b/services/agents/app/workers/fused_alert_consumer.py
@@ -49,6 +49,7 @@ from app.core.cost_governor import Decision, get_governor
from app.core.cost_telemetry import CostTracker
from app.graph.runner import default_budget, run_escalation
from app.investigator import ledger as ledger_module
+from app.investigator.bundle_prompt import prefetch_context_bundle_dict
from app.llm.factory import llm_override
from app.memory.outcomes import AI, lookup_prior, record_outcome, should_auto_suppress
from app.models.state import AgentStatus, InvestigationState
@@ -562,6 +563,18 @@ class FusedAlertTriageWorker:
"""
if not _escalation_enabled():
return
+ # Pre-fetch the same context an analyst-initiated investigation gets.
+ # The manual orchestrator has built a ContextBundle since T2.1; the
+ # escalation path never did, so the high-volume automatic route
+ # investigated with strictly less context — no graph neighbourhood, no
+ # blast radius, no historical verdicts for the same entities — than a
+ # human clicking "investigate" on the identical alert.
+ state.context_bundle = await prefetch_context_bundle_dict(
+ case_id=str(state.incident_id),
+ tenant_id=str(state.tenant_id),
+ alert_summary=state.alert_summary or "",
+ raw_alert=state.raw_alert or {},
+ )
try:
async with CostTracker(run_id=str(state.run_id), tenant_id=str(state.tenant_id)):
await run_escalation(state, budget=default_budget(), seq_start=1)
diff --git a/services/agents/tests/test_escalation_context_depth.py b/services/agents/tests/test_escalation_context_depth.py
new file mode 100644
index 000000000..b533c3d55
--- /dev/null
+++ b/services/agents/tests/test_escalation_context_depth.py
@@ -0,0 +1,201 @@
+"""An auto-escalated alert must be investigated as deeply as a manual one.
+
+Three gaps in investigation depth, all the same shape: a capability was built,
+and the path that needed it did not use it.
+
+1. The manual investigator orchestrator has built a `ContextBundle` — graph
+ neighbourhood, blast radius, historical verdicts for the same entities,
+ UEBA baselines — since T2.1. The escalation path the auto-triage worker
+ uses never did, so the high-volume automatic route investigated with
+ strictly less context than a human clicking "investigate" on the identical
+ alert.
+
+2. `run_with_tools` is described in its own module docstring as "the primitive
+ that lets specialist agents use tools instead of reasoning over a single
+ pre-serialised blob", and called `bound.ainvoke` directly — bypassing the
+ LLM input contract on the one place in the system where untrusted content
+ is fed back into a prompt.
+
+3. QRadar's AQL translator was written, exported and tested, and the connector
+ type was missing from the API's federated-capable set.
+"""
+
+from __future__ import annotations
+
+import uuid
+
+import pytest
+from app.agents.investigation_agent import _bundle_findings
+from app.models.state import AgentStatus, InvestigationState
+from app.workers.fused_alert_consumer import FusedAlertTriageWorker
+
+
+def _state() -> InvestigationState:
+ return InvestigationState(
+ run_id=uuid.uuid4(),
+ incident_id=uuid.uuid4(),
+ tenant_id=uuid.uuid4(),
+ alert_summary="Lateral movement from WIN-DC01",
+ raw_alert={"hostname": "WIN-DC01", "username": "svc_backup"},
+ verdict="true_positive",
+ status=AgentStatus.RUNNING,
+ )
+
+
+# ── context on the escalation path ────────────────────────────────────────
+
+
+async def test_escalation_prefetches_the_context_bundle(monkeypatch: pytest.MonkeyPatch):
+ """The regression: the automatic path got no pre-fetched context at all."""
+ import app.workers.fused_alert_consumer as mod
+
+ seen: dict[str, object] = {}
+
+ async def _prefetch(*, case_id, tenant_id, alert_summary, raw_alert): # noqa: ANN001
+ seen.update(case_id=case_id, tenant_id=tenant_id, alert_summary=alert_summary)
+ return {"entities": [], "blast_radius": {}}
+
+ async def _run_escalation(state, **kwargs): # noqa: ANN001, ANN003
+ seen["bundle_at_run"] = state.context_bundle
+ return state
+
+ monkeypatch.setattr(mod, "prefetch_context_bundle_dict", _prefetch)
+ monkeypatch.setattr(mod, "run_escalation", _run_escalation)
+ monkeypatch.setenv("AISOC_AGENT_ESCALATION", "1")
+
+ state = _state()
+ await FusedAlertTriageWorker(bootstrap_servers="localhost:9092")._maybe_escalate(state)
+
+ assert seen["case_id"] == str(state.incident_id)
+ assert seen["tenant_id"] == str(state.tenant_id)
+ # Built *before* the subgraph runs, or the nodes cannot use it.
+ assert seen["bundle_at_run"] is not None
+
+
+async def test_a_context_failure_never_blocks_the_escalation(
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """Context is additive. Losing it must not lose the investigation."""
+ import app.workers.fused_alert_consumer as mod
+
+ ran = False
+
+ async def _prefetch(**kwargs): # noqa: ANN003
+ return {} # prefetch_context_bundle_dict never raises; it returns {}
+
+ async def _run_escalation(state, **kwargs): # noqa: ANN001, ANN003
+ nonlocal ran
+ ran = True
+ return state
+
+ monkeypatch.setattr(mod, "prefetch_context_bundle_dict", _prefetch)
+ monkeypatch.setattr(mod, "run_escalation", _run_escalation)
+ monkeypatch.setenv("AISOC_AGENT_ESCALATION", "1")
+
+ await FusedAlertTriageWorker(bootstrap_servers="localhost:9092")._maybe_escalate(_state())
+ assert ran
+
+
+# ── the bundle reaches the investigation ──────────────────────────────────
+
+
+def _bundle(**overrides) -> dict:
+ """A ContextBundle as `model_dump(mode="json")` produces it."""
+ payload = {"incident_id": str(uuid.uuid4()), "tenant_id": "t", "entities": []}
+ payload.update(overrides)
+ return payload
+
+
+def test_historical_verdicts_become_findings():
+ """How identical alerts previously resolved is the highest-value context."""
+ findings = _bundle_findings(
+ _bundle(
+ historical_similar_cases=[
+ {"key": "sig-a", "verdict": "false_positive", "similarity_score": 0.94},
+ ]
+ )
+ )
+ assert any("false_positive" in f for f in findings)
+ assert any("similar historical" in f.lower() for f in findings)
+
+
+def test_blast_radius_becomes_a_finding():
+ findings = _bundle_findings(
+ _bundle(
+ entity_neighborhoods={
+ "host:WIN-DC01": {
+ "entity": {"type": "host", "key": "host:WIN-DC01", "value": "WIN-DC01"},
+ "blast_radius_score": 0.82,
+ }
+ }
+ )
+ )
+ assert any("blast radius" in f.lower() for f in findings)
+
+
+def test_no_bundle_produces_no_findings():
+ assert _bundle_findings(None) == []
+ assert _bundle_findings({}) == []
+
+
+def test_an_unreadable_bundle_is_ignored_not_fatal():
+ """A shape change in the bundle must not break every investigation."""
+ assert _bundle_findings({"entities": "not-a-list"}) == []
+ assert _bundle_findings({"no": "incident_id"}) == []
+
+
+def test_raw_payloads_are_never_surfaced():
+ """Only the bundle's declared-safe summary fields reach the findings."""
+ findings = _bundle_findings(
+ _bundle(
+ entity_neighborhoods={
+ "host:h": {
+ "entity": {"type": "host", "key": "host:h", "value": "h"},
+ "blast_radius_score": 0.5,
+ "nodes": [{"vaulted": "do-not-leak"}],
+ }
+ }
+ )
+ )
+ assert not any("do-not-leak" in f for f in findings)
+
+
+# ── the tool loop honours the LLM contract ────────────────────────────────
+
+
+async def test_the_tool_loop_routes_through_the_llm_contract(
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """Tool output is untrusted: a log row can read as an instruction.
+
+ Calling `bound.ainvoke` directly skipped injection validation on precisely
+ the highest-risk content in the system, and lost token/cost telemetry for
+ every tool-calling turn.
+ """
+ import app.agents.tool_loop as loop
+ from app.tools.registry import ToolRegistry
+
+ called = False
+
+ async def _safe(llm, messages, **kwargs): # noqa: ANN001, ANN003
+ nonlocal called
+ called = True
+
+ class _Resp:
+ content = "done"
+ tool_calls: list = []
+
+ return _Resp()
+
+ monkeypatch.setattr(loop, "safe_ainvoke", _safe)
+
+ class _LLM:
+ def bind_tools(self, schemas): # noqa: ANN001
+ return self
+
+ out = await loop.run_with_tools(_LLM(), system="s", user="u", registry=ToolRegistry())
+ assert called, "the loop bypassed safe_ainvoke"
+ assert out["content"] == "done"
diff --git a/services/api/app/api/v1/endpoints/federated.py b/services/api/app/api/v1/endpoints/federated.py
index 39328f4ce..4362694f4 100644
--- a/services/api/app/api/v1/endpoints/federated.py
+++ b/services/api/app/api/v1/endpoints/federated.py
@@ -65,6 +65,13 @@ FEDERATED_CAPABLE_TYPES: frozenset[str] = frozenset(
"splunk",
"microsoft_sentinel",
"elastic",
+ # QRadar's AQL translator has been written, exported from
+ # app.federated.translators and covered by tests since the federated
+ # search shipped, but the type was never added here. A tenant with an
+ # enabled QRadar connector was silently excluded from every federated
+ # search, and naming it explicitly in `connector_ids` returned "not
+ # federated-capable".
+ "qradar",
}
)
diff --git a/services/api/tests/test_federated_capable_types.py b/services/api/tests/test_federated_capable_types.py
new file mode 100644
index 000000000..d201ef525
--- /dev/null
+++ b/services/api/tests/test_federated_capable_types.py
@@ -0,0 +1,50 @@
+"""Which connector types federated search will actually fan out to.
+
+`FEDERATED_CAPABLE_TYPES` is the API's allow-list, and it had drifted from the
+translators that exist. `to_aql` has been written, exported from
+`app.federated.translators`, covered by tests in
+`services/connectors/tests/test_federated_aql.py`, and used by the QRadar
+connector's `federated_search` since federated search shipped — but `qradar`
+was never added to the allow-list.
+
+The effect was quiet: a tenant with an enabled QRadar connector was excluded
+from every federated search with no error, and naming it explicitly in
+`connector_ids` returned "not found, not enabled, or not federated-capable",
+which reads like a misconfigured connector rather than a missing allow-list
+entry.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+federated = pytest.importorskip(
+ "app.api.v1.endpoints.federated",
+ reason="API dependencies not installed",
+)
+
+#: The dialect translator each federated-capable type dispatches to. Kept here
+#: rather than imported so a type added to the allow-list without a translator
+#: fails in this test, not at query time against a customer's connector.
+_EXPECTED_TRANSLATORS = {
+ "splunk": "to_spl",
+ "microsoft_sentinel": "to_kql",
+ "elastic": "to_esql",
+ "qradar": "to_aql",
+}
+
+
+def test_qradar_is_federated_capable():
+ assert "qradar" in federated.FEDERATED_CAPABLE_TYPES
+
+
+def test_every_federated_capable_type_has_a_declared_translator():
+ """Guards the same drift in the other direction."""
+ missing = sorted(set(federated.FEDERATED_CAPABLE_TYPES) - set(_EXPECTED_TRANSLATORS))
+ assert not missing, f"federated-capable types with no declared translator: {missing}"
+
+
+def test_every_declared_translator_is_federated_capable():
+ """A written, tested translator that no tenant can reach is the bug above."""
+ unreachable = sorted(set(_EXPECTED_TRANSLATORS) - set(federated.FEDERATED_CAPABLE_TYPES))
+ assert not unreachable, f"translators exist but the type is not allow-listed: {unreachable}"