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.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
"""Recursive, tool-driven investigation. The first production caller of the loop.
``run_with_tools`` — a working ReAct loop with tool execution, injection
guarding and cost telemetry — shipped with zero production callers. Every
investigation path fed the model one pre-serialised blob and asked for a
verdict, so the platform's deepest capability was reachable only from tests.
This module closes that. It selects an investigation strategy for the alert,
binds the investigation toolset, and runs the loop, so the model pivots
through the estate rather than summarising one payload.
What this deliberately is not: a workflow engine. The strategy supplies the
plan as guidance and the model chooses the tools. A hard-coded pivot sequence
would break on the first alert that did not match its shape, and would make
the loop decorative.
Three guardrails, because a loop that calls tools is a loop that spends money
and time on the hot path of every escalated alert:
* an iteration cap, inherited from ``run_with_tools``
* a wall-clock budget, since a slow pivot chain is worse than a shallow one
* a depth *record* on the result, so the investigation can be graded rather
than assumed deep
"""
from __future__ import annotations
import asyncio
import os
import time
from dataclasses import dataclass, field
from typing import Any
import structlog
from app.context import tenant_skills
from app.context.tenant_skills import ResolvedSkill
from app.core.cost_governor import get_governor
from app.investigator import ledger
from app.investigator.strategies import Strategy, select_strategy
from app.llm.factory import make_chat_model
from app.llm.prompt_registry import prompt_text
from app.llm.tool_loop import run_with_tools
from app.mcp.tools import build_mcp_toolset
from app.prompting.envelope import system_rule
from app.prompting.tool_results import unverified_tool_claims
from app.security.agent_identity import issue_identity
from app.tools.customer_tools import scoped_customer_tools
from app.tools.investigation import investigation_tools
from app.tools.registry import default_registry
from .limits import max_completion_tokens
logger = structlog.get_logger()
#: Off by default is wrong here — a shipped capability nobody enables is the
#: state this module exists to fix — but it must be disableable for air-gapped
#: and eval deployments that have no LLM.
ENABLED = os.getenv("AISOC_DEEP_INVESTIGATION", "true").strip().lower() not in (
"0",
"false",
"off",
"no",
)
MAX_ITERATIONS = int(os.getenv("AISOC_DEEP_INVESTIGATION_MAX_ITERS", "6"))
BUDGET_SECONDS = float(os.getenv("AISOC_DEEP_INVESTIGATION_BUDGET_SECONDS", "120"))
#: Per-incident spend ceiling. The cost governor already enforces a rolling
#: per-tenant budget, which is the wrong granularity for this: one runaway
#: investigation consuming a quarter of the day's allowance looks identical
#: to a hundred well-behaved ones until the budget runs out mid-afternoon
#: and every subsequent alert goes untriaged.
#:
#: A chain that cannot conclude within this is not one more turn away from
#: concluding, and the honest output is a partial investigation that says so.
MAX_USD_PER_INVESTIGATION = float(os.getenv("AISOC_DEEP_INVESTIGATION_MAX_USD", "0.50"))
MAX_TOKENS_PER_INVESTIGATION = int(os.getenv("AISOC_DEEP_INVESTIGATION_MAX_TOKENS", "60000"))
@dataclass
class DeepInvestigationResult:
"""What the loop found, and how hard it actually looked.
The depth fields are the point. An investigation that called one tool and
wrote three paragraphs is alert-enrich-summarise wearing a different
name, and without a record of pivots taken there is no way to tell the
two apart after the fact.
"""
strategy_id: str
narrative: str = ""
pivots: list[str] = field(default_factory=list)
distinct_pivots: int = 0
tool_trace: list[dict[str, Any]] = field(default_factory=list)
iterations: int = 0
truncated: bool = False
latency_ms: int = 0
over_budget: bool = False
#: Spend attributable to this one investigation. A fifteen-step pivot
#: chain is an order of magnitude more expensive than enrich-and-
#: summarise, and per-tenant daily budgets do not surface that: one alert
#: consuming a quarter of the day's budget looks the same as a hundred
#: alerts consuming it evenly, right up until the day ends early.
tokens: int = 0
usd_cost: float = 0.0
cost_capped: bool = False
unavailable_data: list[str] = field(default_factory=list)
#: Which of the customer's own tools this investigation was offered.
#: Recorded because "the agent reached the customer's SIEM" is a claim
#: about one run, and without the list there is no way to tell a run that
#: had the tools and did not use them from one that never had them.
customer_tools: list[str] = field(default_factory=list)
#: What was NOT connected, in prose, as it went into the prompt. Kept on
#: the result as well so the finding says which sources were unavailable
#: rather than leaving a reader to infer it from an absence.
coverage_notes: list[str] = field(default_factory=list)
#: How many tool calls reached the Investigation Ledger. Reported rather
#: than assumed: "every call is in the ledger" is a claim, and a run with
#: four pivots and zero rows written is the failure it would hide.
ledger_rows: int = 0
#: Namespaced MCP tools bound for this run, and what was refused and why.
#: Recorded on the result as well as in the ledger so an operator reading
#: one investigation can see that a tool they configured was not offered,
#: without going to the ledger to find out.
mcp_tools: list[str] = field(default_factory=list)
mcp_refusals: list[str] = field(default_factory=list)
#: The tenant skill that supplied this run's plan, if one matched, and the
#: version of it. Recorded on the result rather than derivable from
#: ``strategy_id`` alone, because the id names which skill and a verdict
#: disputed six months later needs to know which *text*, and the version
#: history is what turns the pair back into text.
tenant_skill: dict[str, Any] | None = None
#: Tools the narrative credits with a result that no call in this run
#: produced. Empty on an honest run, and a claim with no `tool_call` row
#: behind it on any other — which `distinct_pivots` cannot distinguish,
#: because it counts what the loop did and not what the narrative says.
unverified_tool_claims: list[str] = field(default_factory=list)
error: str | None = None
@property
def reached_depth(self) -> bool:
"""Whether the run met the strategy's floor for having investigated."""
strategy = _STRATEGY_CACHE.get(self.strategy_id)
floor = strategy.min_pivots if strategy else 2
return self.distinct_pivots >= floor
def as_dict(self) -> dict[str, Any]:
return {
"strategy_id": self.strategy_id,
"narrative": self.narrative,
"pivots": self.pivots,
"distinct_pivots": self.distinct_pivots,
"iterations": self.iterations,
"truncated": self.truncated,
"latency_ms": self.latency_ms,
"over_budget": self.over_budget,
"tokens": self.tokens,
"usd_cost": round(self.usd_cost, 6),
"cost_capped": self.cost_capped,
"reached_depth": self.reached_depth,
"unavailable_data": self.unavailable_data,
"customer_tools": self.customer_tools,
"coverage_notes": self.coverage_notes,
"ledger_rows": self.ledger_rows,
"mcp_tools": self.mcp_tools,
"mcp_refusals": self.mcp_refusals,
"tenant_skill": self.tenant_skill,
"error": self.error,
}
def findings(self) -> list[str]:
"""Render as investigation findings for the deterministic agent."""
out: list[str] = []
if self.error:
# A failed investigation must not read as a completed one.
out.append(f"Deep investigation did not complete ({self.error}). Findings below are from the deterministic path only.")
return out
if self.narrative:
out.append(self.narrative.strip())
if self.tenant_skill:
out.append(
f"Investigation plan came from this organisation's own skill "
f"{self.tenant_skill.get('ref')} (owner: {self.tenant_skill.get('owner') or 'unrecorded'}), "
f"which outranks the built-in strategy for alerts of this shape."
)
if self.pivots:
out.append(
f"Investigation depth: {self.distinct_pivots} distinct pivots "
f"across {self.iterations} reasoning steps "
f"(strategy: {self.strategy_id}) — {', '.join(sorted(set(self.pivots)))}."
)
if self.unavailable_data:
# Stated as a coverage gap, never as an absence of activity.
out.append(
"Coverage gap: the following data classes are not ingested, so this "
"investigation could not check them — "
f"{', '.join(sorted(set(self.unavailable_data)))}. "
"Treat them as unknown rather than clear."
)
if self.coverage_notes:
# Which of the customer's own products were reachable, and which
# were not. Reported even when everything was available, because a
# reader deciding how much to trust a verdict needs to know what it
# was based on, and an absent sentence is not an answer.
out.append("Sources consulted beyond the AiSOC event lake: " + " ".join(self.coverage_notes))
if self.truncated:
out.append(f"Investigation hit its {MAX_ITERATIONS}-step cap; the line of enquiry was not exhausted.")
if self.over_budget:
out.append(
f"Investigation exceeded its {BUDGET_SECONDS:.0f}s budget and was cut short; conclusions are based on partial evidence."
)
return out
_STRATEGY_CACHE: dict[str, Strategy] = {}
async def _resolve_tenant_skill(
state: Any,
*,
tenant_id: str,
summary: str,
techniques: list[str],
) -> ResolvedSkill | None:
"""The tenant skill matching this alert, or ``None``.
Prefers a skill the triage path already resolved onto the state, so one
investigation cannot be steered by one version of a skill and triaged
under another: the fetch is cached, but a cache expiry between the two
reads is exactly the window in which an activation would split them. When
the state carries no skill the resolver runs here, because deep
investigation is also reachable from the case path, which never went
through triage.
Failure is a ``None`` and a log line. The built-in strategy library is the
fallback, which is the behaviour before this phase.
"""
carried = getattr(state, "tenant_skill", None)
rows: list[dict[str, Any]]
try:
rows = await tenant_skills.fetch_skills(tenant_id)
except Exception as exc: # noqa: BLE001 - guidance is advisory, the investigation is not
logger.warning("deep_investigation.skill_lookup_failed", error=str(exc)[:200])
return None
if isinstance(carried, dict) and carried.get("skill_id"):
pinned = [
row
for row in rows
if str(row.get("skill_id")) == str(carried["skill_id"]) and int(row.get("version") or 0) == int(carried.get("version") or 0)
]
if pinned:
rows = pinned
else:
# The version triage used is no longer served. Saying so is the
# point: the alternative is silently investigating under a
# different version than the verdict will be explained by.
logger.warning(
"deep_investigation.skill_version_moved",
skill=carried.get("ref"),
hint="triage and investigation would otherwise run under different versions of this skill",
)
raw = getattr(state, "raw_alert", {}) or {}
return tenant_skills.select_skill(
rows,
summary=summary,
techniques=techniques,
rule_id=str(raw.get("rule_id") or "") or None,
source=str(raw.get("connector_type") or raw.get("source") or "") or None,
)
def _summarise_alert(state: Any) -> str:
"""The alert as a prompt, without dumping raw payloads into context."""
raw = getattr(state, "raw_alert", {}) or {}
parts = [f"Alert: {getattr(state, 'alert_summary', '') or raw.get('title') or 'unknown'}"]
for key, label in (
("severity", "Severity"),
("source", "Source"),
("connector_type", "Connector"),
):
if raw.get(key):
parts.append(f"{label}: {raw[key]}")
for key, label in (
("hostname", "Host"),
("src_hostname", "Host"),
("user_name", "User"),
("source_ip", "Source IP"),
("dest_ip", "Destination IP"),
("process_name", "Process"),
("hash_sha256", "SHA-256"),
):
if raw.get(key):
parts.append(f"{label}: {raw[key]}")
techniques = getattr(state, "mitre_mappings", None) or []
if techniques:
parts.append(f"Mapped ATT&CK techniques: {', '.join(techniques)}")
return "\n".join(parts)
def _classify_pivots(trace: list[dict[str, Any]]) -> tuple[list[str], list[str]]:
"""Split the trace into pivots taken and data classes that were missing.
A tool that reported ``available: false`` is not a pivot — counting it
would let an investigation reach its depth floor by calling four tools
that all answered "not ingested".
Neither is a tool that **raised**. ``app/tools/registry.py`` returns
``{"error": f"{type(exc).__name__}: {exc}"}`` when a tool throws, and that
shape carries no ``available`` key at all, so it used to fall into the
``else`` and count. Four tools that all timed out therefore looked like
four pivots, and an investigation could report the depth it was required to
reach while having learned nothing.
"""
pivots: list[str] = []
unavailable: list[str] = []
for entry in trace:
name = entry.get("tool", "")
preview = str(entry.get("result_preview", ""))
unavailable_flag = "'available': False" in preview or '"available": false' in preview
# Matched on the key rather than on the word, so a *result* mentioning
# an error — a SIEM row whose message contains "error" — is still a
# pivot. Only the registry's own failure envelope has the key at the
# top of the mapping.
errored = "'error':" in preview or '"error":' in preview
if unavailable_flag or errored:
unavailable.append(name)
else:
pivots.append(name)
return pivots, unavailable
#: Where the tool-call sequence numbers start.
#:
#: The graph runner numbers its own ``graph_step`` events from 0 upward, and
#: ``record_event`` has ``ON CONFLICT (run_id, seq) DO NOTHING``, so a
#: colliding sequence number is **silently dropped**. That is the worst
#: available failure: the ledger would look complete and be missing rows.
#: Offset high enough that a graph would have to run ten thousand nodes to
#: reach it.
_TOOL_CALL_SEQ_BASE = 10_000
async def _record_tool_calls(state: Any, trace: list[dict[str, Any]]) -> int:
"""Write every tool call the loop made to the Investigation Ledger.
``run_with_tools`` builds a trace and returns it, and nothing carried that
trace anywhere durable. The trace lived for the length of one function
call, so an investigation's own record held a narrative and a pivot count
with no evidence of which tools produced them, which is exactly what makes
a shallow run indistinguishable from a deep one after the fact.
Written here rather than through ``InvestigatorState.log_tool_call``,
which was the obvious route and is the wrong one. Two state classes exist:
``app.investigator.state.InvestigatorState``, which has an ``audit_log``
the orchestrator drains into the ledger, and
``app.models.state.InvestigationState``, which has neither. The production
caller of this module (``agents/investigation_agent.py``) passes the
**second**, so a version of this that called ``log_tool_call`` behind a
``hasattr`` guard would have been a no-op on every real investigation
while passing a test that constructed the first. That is this
repository's most-repeated defect shape and it very nearly landed again.
Best-effort, and deliberately so: a ledger write failing must not take a
completed investigation down with it. Returns how many rows were written
so a caller can tell "nothing to record" from "the ledger is not
configured", which a bare ``None`` would not.
"""
if not trace:
return 0
run_id = getattr(state, "run_id", None)
if run_id is None:
logger.warning("deep_investigation.no_run_id", reason="tool calls could not be attributed to a run")
return 0
tenant_ref = str(getattr(state, "tenant_id", "") or "")
try:
tenant_uuid = await ledger.resolve_tenant(tenant_ref)
except Exception: # noqa: BLE001
tenant_uuid = None
if tenant_uuid is None:
# Not an error on a deployment with no ledger database, which is every
# unit test and every air-gapped eval run. Logged at debug so a real
# deployment can find it without the noise.
logger.debug("deep_investigation.ledger_unavailable", tenant_ref=tenant_ref[:64])
return 0
written = 0
for offset, entry in enumerate(trace):
tool = str(entry.get("tool") or "unknown")
args = entry.get("args") or {}
try:
event_id = await ledger.record_event(
run_id=run_id,
tenant_id=tenant_uuid,
seq=_TOOL_CALL_SEQ_BASE + offset,
kind="tool_call",
agent="deep-investigator",
summary=f"{tool}({', '.join(sorted(args))})",
payload={
"tool": tool,
"args": args,
# The preview rather than the whole result. The loop
# already caps each tool message at 4000 characters, and a
# ledger row is not the place for a second copy of a
# customer's SIEM rows.
"result_preview": entry.get("result_preview"),
},
)
except Exception: # noqa: BLE001 - a ledger write must not fail an investigation
logger.warning("deep_investigation.tool_call_not_recorded", tool=tool)
continue
if event_id is not None:
written += 1
return written
async def run_deep_investigation(
state: Any,
*,
llm: Any | None = None,
max_iterations: int | None = None,
) -> DeepInvestigationResult:
"""Investigate by pivoting through the estate, not by summarising the alert.
Returns a result even on failure: the deterministic path still runs, and
a raised exception here would take a working investigation down with a
non-essential enhancement.
"""
tenant_id = str(getattr(state, "tenant_id", "") or "default")
summary = getattr(state, "alert_summary", "") or ""
techniques = list(getattr(state, "mitre_mappings", None) or [])
# Phase 6.2: a tenant skill outranks a built-in strategy when it matches.
# The two are different kinds of claim: a built-in encodes how an attack
# behaves in general, a skill encodes what is true in this estate, and the
# specific one wins. When nothing matches, selection is exactly what it
# was, which is what keeps the existing strategy-selection tests honest.
skill = await _resolve_tenant_skill(state, tenant_id=tenant_id, summary=summary, techniques=techniques)
if skill is not None:
strategy = skill.strategy()
system_guidance = skill.system_guidance()
else:
strategy = select_strategy(summary=summary, techniques=techniques)
system_guidance = strategy.system_guidance()
_STRATEGY_CACHE[strategy.id] = strategy
result = DeepInvestigationResult(strategy_id=strategy.id)
if skill is not None:
result.tenant_skill = skill.as_provenance()
if not ENABLED:
result.error = "deep investigation disabled (AISOC_DEEP_INVESTIGATION)"
return result
started = time.monotonic()
governor = get_governor()
spend_before = governor.spent_usd(tenant_id)
tokens_before = governor.spent_tokens(tenant_id)
try:
model = llm if llm is not None else make_chat_model("investigation", max_tokens=max_completion_tokens())
registry = default_registry(tenant_id)
for tool in investigation_tools(tenant_id):
registry.register(tool)
# Gap-closure Phase 4. Until now the only tools bound here reached
# AiSOC's own event lake, so anything the customer's estate held and
# AiSOC never ingested was invisible, and on the default CORE profile
# there is no lake at all.
#
# Scoped rather than bound wholesale: a model offered an EDR tool on a
# tenant with no EDR will call it, burn a turn of a bounded loop on
# `no_integration`, and some models will narrate the attempt as though
# it returned something. `coverage_notes` is the other half, and it is
# the honest half: what is *not* connected goes into the prompt as a
# gap in visibility rather than being left for the model to discover
# by the absence of a tool.
customer_tools, coverage_notes = await scoped_customer_tools(tenant_id)
for tool in customer_tools:
registry.register(tool)
result.customer_tools = [tool.name for tool in customer_tools]
result.coverage_notes = coverage_notes
coverage = ("\n\n" + "\n".join(f"- {note}" for note in coverage_notes)) if coverage_notes else ""
# One `system` string that both of the bindings below extend. Two
# f-strings each composing their own would mean whichever ran second
# silently dropped the first one's addition.
system = f"{prompt_text('deep_investigation.preamble')}\n\n{system_guidance}{coverage}"
# Third-party MCP servers this tenant registered, if any. The toolset
# carries its own nonce, and the standing data-only rule for that
# nonce is appended to the system message here: an MCP result is
# fenced with it, and a fence the system prompt never explains is a
# delimiter rather than a boundary.
run_id = str(getattr(state, "run_id", None) or "unknown-run")
# Wave-2 7.3. The investigator acts under its own short-lived
# identity, scoped to the pivots it is allowed to reach, and every
# tool call it makes writes an audit row naming the agent and run.
identity = issue_identity("investigation", run_id=run_id)
mcp = await build_mcp_toolset(tenant_id, run_id=getattr(state, "run_id", None))
if mcp.tools:
for tool in mcp.tools:
registry.register(tool)
# `build_mcp_toolset` has already classified each tool and
# refused the destructive ones (`app/mcp/policy.py`), so these
# names arrive approved. Extending the scope with them is one
# control handing its verdict to the other; the alternative —
# a static table naming a tenant's MCP tools — cannot exist,
# because they are discovered at run time.
identity = identity.with_granted_tools({t.name for t in mcp.tools})
system = f"{system}\n\n{system_rule(mcp.nonce)}"
result.mcp_tools = [t.name for t in mcp.tools]
result.mcp_refusals = [f"{name}: {classification}" for name, classification, _ in mcp.refusals]
loop = await asyncio.wait_for(
run_with_tools(
model,
system=system,
user=_summarise_alert(state),
registry=registry,
identity=identity,
max_iters=max_iterations or MAX_ITERATIONS,
),
timeout=BUDGET_SECONDS,
)
except TimeoutError:
result.over_budget = True
result.error = f"exceeded the {BUDGET_SECONDS:.0f}s budget"
result.latency_ms = int((time.monotonic() - started) * 1000)
logger.warning("deep_investigation.over_budget", strategy=strategy.id)
return result
except Exception as exc:
result.error = f"{type(exc).__name__}: {exc}"
result.latency_ms = int((time.monotonic() - started) * 1000)
logger.warning("deep_investigation.failed", strategy=strategy.id, error=type(exc).__name__)
return result
result.latency_ms = int((time.monotonic() - started) * 1000)
# Attributed by difference rather than self-reported: the loop does not
# know what it cost, and the governor is the only place that does.
result.usd_cost = max(0.0, governor.spent_usd(tenant_id) - spend_before)
result.tokens = max(0, governor.spent_tokens(tenant_id) - tokens_before)
result.cost_capped = result.usd_cost >= MAX_USD_PER_INVESTIGATION or result.tokens >= MAX_TOKENS_PER_INVESTIGATION
if result.cost_capped:
logger.warning(
"deep_investigation.cost_capped",
strategy=strategy.id,
usd=round(result.usd_cost, 4),
tokens=result.tokens,
)
result.narrative = loop.get("content", "") or ""
result.tool_trace = loop.get("tool_trace", []) or []
result.iterations = int(loop.get("iterations", 0) or 0)
result.truncated = bool(loop.get("truncated"))
result.pivots, result.unavailable_data = _classify_pivots(result.tool_trace)
result.distinct_pivots = len(set(result.pivots))
result.ledger_rows = await _record_tool_calls(state, result.tool_trace)
# Depth plan 1.3. The narrative is graded against the trace that just
# became `tool_call` rows, so a tool the model credits with a result it
# never called is named rather than read as a pivot that happened. The
# depth fields above cannot see this: `distinct_pivots` counts calls the
# loop made, and a narrative describing calls it did not make scores the
# same as an honest one.
#
# Recorded, not demoted. There is no verdict here to demote and no closure
# to block — this function returns a narrative — so the finding travels on
# the result to whoever does hold a verdict. `auto_triage_agent` is where
# the same check blocks an auto-close.
result.unverified_tool_claims = unverified_tool_claims(
result.narrative,
called_tools=[str(entry.get("tool") or "") for entry in result.tool_trace],
)
if result.unverified_tool_claims:
logger.warning(
"deep_investigation.unverified_tool_claim",
strategy=strategy.id,
claimed=result.unverified_tool_claims[:10],
tools_called=sorted({str(entry.get("tool") or "") for entry in result.tool_trace}),
)
logger.info(
"deep_investigation.complete",
strategy=strategy.id,
distinct_pivots=result.distinct_pivots,
iterations=result.iterations,
latency_ms=result.latency_ms,
reached_depth=result.reached_depth,
)
return result