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.

feat: close the loop (Wave 1) — outcomes compound, repeats auto-suppress (#573)

Beenu Arora303902040954
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8d0c4a00a..06c1914ce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
+- **Closed loop: outcomes compound, repeat alerts auto-suppress (Wave 1).** Every
+ durable auto-triage outcome is now written back as a per-signature institutional
+ prior (`services/agents/app/memory/outcomes.py`), and a later alert matching a
+ trusted prior benign/false-positive disposition is auto-resolved WITHOUT
+ re-triage — human priors are trusted immediately, AI priors require corroboration
+ (repeat count + high confidence), and a prior true-positive never auto-closes a
+ future alert. Suppressions are recorded to `aisoc_outcome_suppressions`
+ (migration 048) and surfaced as a measured `repeat_alerts_suppressed` /
+ `repeat_suppression_rate` on `GET /metrics/funnel`. Fusion now applies a bounded
+ (±0.10), per-tenant institutional-memory nudge to fuse-time confidence
+ (`MemoryPriorProvider` distils disposition history; refreshed on a cadence),
+ scheduled hunt findings open governed DRAFT `DetectionRuleProposal`s
+ (`hunt-finding` source), and the previously-orphaned disposition-history tuner is
+ wired via `POST /detection/tuning/auto-suggest`. Gates: `test_outcome_memory.py`,
+ `test_memory_nudge.py`, `test_wave1_loop_edges.py`; claim-to-gate matrix +4.
- **LLM gateway (LiteLLM) — task-based model routing + observability
([#478](https://github.com/beenuar/AiSOC/issues/478), PR1).** New `litellm`
service in `docker-compose.yml` as the single entry point for live LLM calls.
diff --git a/docs/audit/CLAIM_TO_GATE_MATRIX.md b/docs/audit/CLAIM_TO_GATE_MATRIX.md
index 5670fddae..d0e884044 100644
--- a/docs/audit/CLAIM_TO_GATE_MATRIX.md
+++ b/docs/audit/CLAIM_TO_GATE_MATRIX.md
@@ -48,11 +48,15 @@ Statuses: `GATED` (a CI job fails when the claim stops being true) · `PARTIAL`
| Autonomy posture is copilot-by-default with a visible per-action/blast scorecard | README (autopilot/copilot) | `ci.yml` web-test job (`AutonomyScorecard.test.tsx` — posture stays Copilot unless a high/critical-blast action auto-executes; distribution + override counts; rendered badge) | GATED | - |
| Broad SIEM/NDR/edge coverage (QRadar, Exabeam, Securonix, Devo, Netskope, Windows/Sysmon, Zeek/Suricata, syslog/CEF) | README (connector count) | `ci.yml` connectors job (`test_schemas.py` + `test_conformance.py` gate every connector; `test_siem_expansion.py` + `test_ndr_edge_expansion.py` pin each new connector's severity-ladder mapping) + `generate_connector_count.py --check` (registry-derived count, README phrase) | GATED | - |
| AI/LLM-usage governance: audit connector + detections + tiered lake storage | README (AI usage / storage) | `ci.yml` connectors job (`test_llm_usage.py` — OpenAI/Anthropic audit connector emits the dotted `event_type` the `llm-*` rules match) + `validate-detections.yml` (8 `llm-*` native rules replay) + api job (`test_storage_tiering.py` — hot/cold TTL-move policy + DDL gate; verified on live ClickHouse) | GATED | - |
+| Outcomes compound: repeat alerts auto-suppressed from prior dispositions (measured, not estimated) | README (closed loop) | `ci.yml` agents job (`test_outcome_memory.py` — every durable triage outcome is written back as a per-signature prior; a later alert matching a trusted prior benign/FP disposition is auto-resolved WITHOUT re-triage; human priors trusted immediately, AI priors require corroboration, a prior TP never auto-closes) + api funnel `repeat_alerts_suppressed`/`repeat_suppression_rate` measured from `aisoc_outcome_suppressions` | GATED | - |
+| Institutional memory nudges fuse-time confidence (bounded, per-tenant) | README (compounding memory) | `ci.yml` fusion job (`test_memory_nudge.py` — distilled per-signature priors nudge new alerts of that signature ±0.10 in `ConfidenceScorer`; unknown signatures unchanged; `MemoryPriorProvider` distils per-tenant priors from disposition history so one tenant never nudges another) | GATED | - |
+| Scheduled hunt findings flow into governed detection proposals | README (proactive hunting → detections) | `ci.yml` api job (`test_wave1_loop_edges.py` — a scheduled hunt that returns hits opens a DRAFT `DetectionRuleProposal` (source `hunt-finding`) into the propose→review→eval-gate→promote lifecycle; zero-hit / flag-off paths open nothing) | GATED | - |
+| Analyst disposition history drives human-approval-gated tuning proposals | README (self-improving detections) | `ci.yml` api job (`test_wave1_loop_edges.py` — the previously-orphaned `suggest_tuning` engine is wired to real per-rule disposition history via `/detection/tuning/auto-suggest`, opening DRAFT proposals; `test_detection_tuner.py` fixture-gates the engine) | PARTIAL (mapping + wiring gated; end-to-end proposal insert exercised in api integration, not a unit DB) | Phase 2 |
## Summary
-- GATED: 33
-- PARTIAL: 7
+- GATED: 36
+- PARTIAL: 8
- NO GATE: 0 (**every claim is now backed by a failing test.** The last NO GATE — the weekly benchmark scoreboard running live against `main` — closed in Phase E1: `scripts/check_scoreboard.py` ties the published scoreboard to a deterministic per-PR live-agent MITRE-accuracy run, while the funded weekly `wet-eval.yml` appends the LLM-tier rows. The full Fully-Operational roadmap (Phases A1–E1) is complete. The 7 remaining PARTIAL rows are honest, named deferrals to future phases outside the A–E scope — each states the specific gap and the phase that closes it — not unproven claims.)
The ratchet is enforced by `scripts/check_claim_gate_matrix.py` (wired into `security.yml`): the NO GATE count may only decrease.
diff --git a/docs/openapi.yaml b/docs/openapi.yaml
index 146bc13d7..8fa632630 100644
--- a/docs/openapi.yaml
+++ b/docs/openapi.yaml
@@ -3376,6 +3376,56 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
+ /api/v1/detection/tuning/auto-suggest:
+ post:
+ tags:
+ - detection
+ summary: Auto Suggest Tuning
+ description: 'Derive human-approval-gated tuning suggestions from disposition
+ history.
+
+
+ Wave 1 wiring: connects the previously-orphaned ``suggest_tuning`` engine
+ to
+
+ real per-rule disposition history and, optionally, opens DRAFT detection
+
+ proposals (source ``auto-tuner``) into the governed lifecycle. Nothing is
+
+ auto-applied — an engineer reviews + eval-gates before promotion.'
+ operationId: auto_suggest_tuning_api_v1_detection_tuning_auto_suggest_post
+ security:
+ - HTTPBearer: []
+ parameters:
+ - name: window_days
+ in: query
+ required: false
+ schema:
+ type: integer
+ maximum: 180
+ minimum: 1
+ default: 30
+ title: Window Days
+ - name: create_proposals
+ in: query
+ required: false
+ schema:
+ type: boolean
+ default: true
+ title: Create Proposals
+ responses:
+ '200':
+ description: Successful Response
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/AutoSuggestResponse'
+ '422':
+ description: Validation Error
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/HTTPValidationError'
/api/v1/detection/tuning:
get:
tags:
@@ -14993,6 +15043,32 @@ components:
- access_token
- refresh_token
title: AuthenticateFinishResponse
+ AutoSuggestResponse:
+ properties:
+ window_days:
+ type: integer
+ title: Window Days
+ records_considered:
+ type: integer
+ title: Records Considered
+ suggestions:
+ items:
+ additionalProperties: true
+ type: object
+ type: array
+ title: Suggestions
+ proposals_created:
+ type: integer
+ title: Proposals Created
+ type: object
+ required:
+ - window_days
+ - records_considered
+ - suggestions
+ - proposals_created
+ title: AutoSuggestResponse
+ description: Disposition-history tuning suggestions + how many became draft
+ proposals.
AutoTuneRequest:
properties:
enabled:
@@ -18541,6 +18617,14 @@ components:
type: string
format: date-time
title: Generated At
+ repeat_alerts_suppressed:
+ type: integer
+ title: Repeat Alerts Suppressed
+ default: 0
+ repeat_suppression_rate:
+ type: number
+ title: Repeat Suppression Rate
+ default: 0.0
type: object
required:
- period
diff --git a/services/agents/app/investigator/ledger.py b/services/agents/app/investigator/ledger.py
index dae564f1b..99900b985 100644
--- a/services/agents/app/investigator/ledger.py
+++ b/services/agents/app/investigator/ledger.py
@@ -493,3 +493,40 @@ async def persist_auto_triage(
except Exception as exc: # noqa: BLE001 — re-raised as a typed, retryable error
logger.warning("ledger.auto_triage_persist_failed", run_id=str(run_id), error=str(exc))
raise LedgerPersistError(str(exc)) from exc
+
+
+async def record_suppression(
+ *,
+ tenant_ref: str,
+ signature: str,
+ alert_id: Any,
+ disposition: str,
+ prior_author: str,
+) -> bool:
+ """Durably record that a repeat alert was auto-suppressed from prior outcome
+ memory (Wave 1), so the compounding-reduction metric is measured, not
+ estimated. Best-effort: no DB / unknown tenant => no-op (returns False)."""
+ pool = await get_pool()
+ if pool is None:
+ return False
+ try:
+ async with pool.acquire() as conn:
+ tenant_id = await _resolve_tenant_id(conn, tenant_ref)
+ if tenant_id is None:
+ return False
+ await conn.execute(
+ """
+ INSERT INTO aisoc_outcome_suppressions
+ (id, tenant_id, signature, alert_id, disposition, prior_author, created_at)
+ VALUES (uuid_generate_v4(), $1, $2, $3, $4, $5, now())
+ """,
+ tenant_id,
+ signature,
+ _coerce_uuid(alert_id),
+ disposition,
+ prior_author,
+ )
+ return True
+ except Exception as exc: # noqa: BLE001 — metric write is best-effort
+ logger.debug("ledger.record_suppression_failed", signature=signature, error=str(exc))
+ return False
diff --git a/services/agents/app/memory/outcomes.py b/services/agents/app/memory/outcomes.py
new file mode 100644
index 000000000..232fa2e28
--- /dev/null
+++ b/services/agents/app/memory/outcomes.py
@@ -0,0 +1,114 @@
+"""Durable outcome-memory priors — close the loop (Wave 1).
+
+Every triage outcome (autonomous OR human) is written back as a per-signature
+prior, so a later alert with the same evidence signature can be auto-suppressed
+instead of re-triaged. This is what makes alert volume actually shrink over
+time: confirmed-benign signatures stop reaching the queue.
+
+Honesty + safety:
+* AI-authored priors are trusted less than human-verified ones. A human prior
+ suppresses immediately; an AI prior only suppresses once it has been
+ corroborated by repeat outcomes (``count``) at high confidence.
+* Only auto-closeable dispositions (false_positive / benign / benign_true_positive)
+ ever suppress. A prior true-positive never auto-closes a future alert.
+* Human authorship is sticky: an AI prior that a human later overrode stays
+ ``human`` and never downgrades.
+"""
+
+from __future__ import annotations
+
+import os
+from datetime import UTC, datetime
+from typing import Any
+
+import structlog
+
+from app.agents.dispositions import AUTO_CLOSEABLE_DISPOSITIONS, normalize_disposition
+from app.memory.institutional import institutional_get, institutional_set
+
+logger = structlog.get_logger()
+
+OUTCOME_KEY_PREFIX = "outcome:"
+HUMAN = "human"
+AI = "ai"
+
+# Suppression gates for AI-authored priors (env-overridable). Human priors are
+# always trusted; AI priors need corroboration.
+_MIN_AI_COUNT = int(os.getenv("AISOC_MEMORY_MIN_AI_COUNT", "3"))
+_AI_CONF_THRESHOLD = float(os.getenv("AISOC_MEMORY_AI_CONFIDENCE", "0.90"))
+
+
+def outcome_key(signature: str) -> str:
+ return f"{OUTCOME_KEY_PREFIX}{signature}"
+
+
+async def record_outcome(
+ tenant_id: str,
+ signature: str,
+ *,
+ disposition: str,
+ confidence: float,
+ author: str = AI,
+ alert_id: Any = None,
+) -> dict[str, Any]:
+ """Write/refresh the per-signature outcome prior. Best-effort (never raises)."""
+ disposition = normalize_disposition(disposition, default="needs_review")
+ key = outcome_key(signature)
+ now = datetime.now(UTC).isoformat()
+
+ prior = await institutional_get(tenant_id, key)
+ if isinstance(prior, dict) and prior.get("disposition") == disposition:
+ count = int(prior.get("count", 0)) + 1
+ first_seen = prior.get("first_seen", now)
+ # Human authorship is sticky and never downgraded to AI.
+ if prior.get("author") == HUMAN:
+ author = HUMAN
+ else:
+ count = 1
+ first_seen = now
+
+ value: dict[str, Any] = {
+ "signature": signature,
+ "disposition": disposition,
+ "confidence": round(float(confidence or 0.0), 4),
+ "author": author,
+ "count": count,
+ "first_seen": first_seen,
+ "last_seen": now,
+ "last_alert_id": str(alert_id) if alert_id else None,
+ }
+ try:
+ await institutional_set(
+ tenant_id,
+ key,
+ value,
+ tags=["outcome_prior", disposition, author],
+ analyst_override=(author == HUMAN),
+ )
+ except Exception as exc: # noqa: BLE001 — memory write is best-effort
+ logger.debug("outcome_memory.write_failed", signature=signature, error=str(exc))
+ return value
+
+
+async def lookup_prior(tenant_id: str, signature: str) -> dict[str, Any] | None:
+ """Return the durable outcome prior for a signature, or None."""
+ prior = await institutional_get(tenant_id, outcome_key(signature))
+ return prior if isinstance(prior, dict) else None
+
+
+def should_auto_suppress(
+ prior: dict[str, Any] | None,
+ *,
+ min_ai_count: int = _MIN_AI_COUNT,
+ ai_confidence: float = _AI_CONF_THRESHOLD,
+) -> bool:
+ """Decide whether a prior is trusted enough to auto-suppress a repeat alert."""
+ if not isinstance(prior, dict):
+ return False
+ disposition = str(prior.get("disposition", ""))
+ if disposition not in AUTO_CLOSEABLE_DISPOSITIONS:
+ return False
+ if prior.get("author") == HUMAN:
+ return True
+ # AI-authored: require corroboration by repeat count + high confidence.
+ return int(prior.get("count", 0)) >= min_ai_count and float(prior.get("confidence", 0.0)) >= ai_confidence
diff --git a/services/agents/app/workers/fused_alert_consumer.py b/services/agents/app/workers/fused_alert_consumer.py
index 1a8f39cff..29b04681a 100644
--- a/services/agents/app/workers/fused_alert_consumer.py
+++ b/services/agents/app/workers/fused_alert_consumer.py
@@ -42,13 +42,14 @@ from typing import Any
import structlog
from app.agents.auto_triage_agent import run_auto_triage
-from app.agents.dispositions import NEEDS_REVIEW
+from app.agents.dispositions import NEEDS_REVIEW, normalize_disposition
from app.agents.triage_agent import run_triage
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.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
from app.routing.model_router import is_deterministic_mode
from app.security.llm_resolver import resolve_llm_config
@@ -77,6 +78,8 @@ _METRICS = {
"bc_mutated": 0,
"needs_review_fallback": 0,
"escalated": 0,
+ "outcome_written": 0,
+ "outcome_suppressed": 0,
"persist_retries": 0,
"dead_lettered": 0,
"errors": 0,
@@ -90,6 +93,22 @@ def _escalation_enabled() -> bool:
return os.getenv("AISOC_AGENT_ESCALATE_TO_GRAPH", "1").strip().lower() not in {"0", "false", "no", "off"}
+def _truthy(name: str, default: str = "1") -> bool:
+ return os.getenv(name, default).strip().lower() not in {"0", "false", "no", "off"}
+
+
+def _memory_suppression_enabled() -> bool:
+ """Auto-suppress a repeat alert that matches a trusted prior benign/FP
+ outcome (Wave 1). On by default; disable with AISOC_AGENT_MEMORY_SUPPRESSION=0."""
+ return _truthy("AISOC_AGENT_MEMORY_SUPPRESSION")
+
+
+def _memory_writeback_enabled() -> bool:
+ """Write every durable triage outcome back as a per-signature prior (Wave 1)
+ so autonomous closures compound. Disable with AISOC_AGENT_MEMORY_WRITEBACK=0."""
+ return _truthy("AISOC_AGENT_MEMORY_WRITEBACK")
+
+
def _coerce_uuid(value: Any, *, fallback: str) -> uuid.UUID:
try:
return uuid.UUID(str(value))
@@ -327,6 +346,16 @@ class FusedAlertTriageWorker:
governor = get_governor()
fingerprint = governor.evidence_fingerprint(str(state.tenant_id), state.raw_alert)
+
+ # Wave 1 — forward auto-suppression: if this signature has a trusted
+ # prior benign/FP outcome, auto-resolve the repeat WITHOUT re-triage
+ # (this is what makes alert volume actually shrink over time). Gated on
+ # trust (human prior, or corroborated high-confidence AI prior).
+ if _memory_suppression_enabled():
+ suppressed = await self._maybe_suppress_from_memory(state, fingerprint, bc_matched)
+ if suppressed is not None:
+ return suppressed
+
decision = governor.check(str(state.tenant_id), fingerprint)
tier: str
@@ -383,6 +412,20 @@ class FusedAlertTriageWorker:
_METRICS["triaged"] += 1
await self._record(state, tier=tier, verdict=verdict, confidence=confidence, tokens=tokens, cost_usd=cost_usd)
+ # Wave 1 — write the durable outcome back as a per-signature prior so
+ # autonomous closures compound (a later identical alert can suppress).
+ if _memory_writeback_enabled() and verdict:
+ with contextlib.suppress(Exception):
+ await record_outcome(
+ str(state.tenant_id),
+ fingerprint,
+ disposition=str(verdict),
+ confidence=float(confidence or 0.0),
+ author=AI,
+ alert_id=(state.raw_alert or {}).get("id"),
+ )
+ _METRICS["outcome_written"] += 1
+
# Issue #569: route escalations (anything NOT auto-closed — TP,
# low-confidence, needs_review) through the full investigation graph.
# High-confidence FP/BTP already terminated (status COMPLETED) and
@@ -404,6 +447,80 @@ class FusedAlertTriageWorker:
"proposed_actions": [{"action_type": a.action_type, "requires_approval": a.requires_approval} for a in state.proposed_actions],
}
+ async def _maybe_suppress_from_memory(
+ self,
+ state: InvestigationState,
+ signature: str,
+ bc_matched: list[str],
+ ) -> dict[str, Any] | None:
+ """Auto-resolve a repeat alert from a trusted prior outcome (Wave 1).
+
+ Returns a summary dict (and short-circuits triage) when suppressed, else
+ None. Best-effort: any lookup failure falls through to normal triage.
+ """
+ try:
+ prior = await lookup_prior(str(state.tenant_id), signature)
+ except Exception as exc: # noqa: BLE001 — memory read is advisory
+ logger.debug("auto_triage_worker.memory_lookup_failed", error=str(exc))
+ return None
+ if not should_auto_suppress(prior):
+ return None
+
+ assert prior is not None # narrowed by should_auto_suppress
+ disposition = normalize_disposition(prior.get("disposition"), default=NEEDS_REVIEW)
+ confidence = float(prior.get("confidence", 0.9))
+ count = int(prior.get("count", 1))
+ author = str(prior.get("author", AI))
+
+ state.verdict = disposition
+ state.confidence = confidence
+ state.status = AgentStatus.COMPLETED
+ state.confidence_basis = [f"outcome_memory: prior {disposition} seen {count}x (author={author})"]
+ state.add_finding(
+ f"Auto-resolved from prior outcome memory: matches a prior {disposition} disposition "
+ f"(seen {count}x, author={author}) — repeat suppressed without re-triage."
+ )
+
+ _METRICS["outcome_suppressed"] += 1
+ _METRICS["triaged"] += 1
+ await self._record(state, tier="memory", verdict=disposition, confidence=confidence)
+ with contextlib.suppress(Exception):
+ await record_outcome(
+ str(state.tenant_id),
+ signature,
+ disposition=disposition,
+ confidence=confidence,
+ author=author,
+ alert_id=(state.raw_alert or {}).get("id"),
+ )
+ with contextlib.suppress(Exception):
+ await ledger_module.record_suppression(
+ tenant_ref=str(state.tenant_id),
+ signature=signature,
+ alert_id=(state.raw_alert or {}).get("id"),
+ disposition=disposition,
+ prior_author=author,
+ )
+ logger.info(
+ "auto_triage_worker.memory_suppressed",
+ run_id=str(state.run_id),
+ disposition=disposition,
+ prior_count=count,
+ author=author,
+ )
+ return {
+ "run_id": str(state.run_id),
+ "incident_id": str(state.incident_id),
+ "tenant_id": str(state.tenant_id),
+ "verdict": disposition,
+ "confidence": confidence,
+ "tier": "memory",
+ "suppressed_by_memory": True,
+ "business_context_rules": bc_matched,
+ "response_dispatched": False,
+ "proposed_actions": [],
+ }
+
async def _maybe_escalate(self, state: InvestigationState) -> None:
"""Run the full investigation graph for an escalated alert (issue #569).
diff --git a/services/agents/tests/test_outcome_memory.py b/services/agents/tests/test_outcome_memory.py
new file mode 100644
index 000000000..195fecee5
--- /dev/null
+++ b/services/agents/tests/test_outcome_memory.py
@@ -0,0 +1,160 @@
+"""Wave 1 - outcome memory closes the loop.
+
+Proves durable outcome priors are written back on every triage, that a repeat
+alert matching a trusted prior benign/FP disposition is auto-suppressed WITHOUT
+re-triage (so volume shrinks), and that the trust gate distinguishes human vs
+corroborated-AI priors and never auto-closes a prior true-positive.
+"""
+
+from __future__ import annotations
+
+import pytest
+from app.core.cost_governor import get_governor
+from app.memory import institutional
+from app.memory.outcomes import (
+ AI,
+ HUMAN,
+ lookup_prior,
+ record_outcome,
+ should_auto_suppress,
+)
+from app.workers import fused_alert_consumer as worker_mod
+from app.workers.fused_alert_consumer import FusedAlertTriageWorker, build_state
+
+pytestmark = pytest.mark.asyncio
+
+TENANT = "11111111-1111-1111-1111-111111111111"
+
+
+def _fused(**alert_overrides) -> dict:
+ alert = {
+ "id": "22222222-2222-2222-2222-222222222222",
+ "tenant_id": TENANT,
+ "title": "Scheduled Nessus scan from 10.0.0.5",
+ "severity": "medium",
+ "hostname": "scanner01",
+ "src_ip": "10.0.0.5",
+ "mitre_techniques": ["T1046"],
+ "risk_score": 0.3,
+ "raw_event": {"a": 1},
+ }
+ alert.update(alert_overrides)
+ return {
+ "id": "22222222-2222-2222-2222-222222222222",
+ "tenant_id": TENANT,
+ "incident_id": "33333333-3333-3333-3333-333333333333",
+ "fusion_decision": "new_incident",
+ "confidence_score": 0.5,
+ "alert": alert,
+ }
+
+
[email protected](autouse=True)
+def _isolate(monkeypatch):
+ institutional._FALLBACK.clear()
+
+ async def _persist_noop(*args, **kwargs): # noqa: ANN002, ANN003
+ return False
+
+ async def _rec_supp(*args, **kwargs): # noqa: ANN002, ANN003
+ return True
+
+ monkeypatch.setattr(worker_mod.ledger_module, "persist_auto_triage", _persist_noop)
+ monkeypatch.setattr(worker_mod.ledger_module, "record_suppression", _rec_supp)
+ monkeypatch.setenv("AISOC_DETERMINISTIC", "1")
+ monkeypatch.setenv("AISOC_AGENT_ESCALATE_TO_GRAPH", "0")
+ monkeypatch.setattr(worker_mod, "resolve_llm_config", _fake_cfg)
+ yield
+ institutional._FALLBACK.clear()
+
+
+async def _fake_cfg(_tenant): # noqa: ANN001
+ class _Cfg:
+ allowed = False
+ api_key = None
+ base_url = None
+ model = "gpt-4o-mini"
+
+ return _Cfg()
+
+
+def _signature(message: dict) -> str:
+ state = build_state(message)
+ assert state is not None
+ return get_governor().evidence_fingerprint(str(state.tenant_id), state.raw_alert)
+
+
+# ── unit: trust gate ─────────────────────────────────────────────────────────
+
+
+async def test_should_auto_suppress_trust_gate():
+ # Human benign prior → always trusted.
+ assert should_auto_suppress({"disposition": "false_positive", "author": HUMAN, "count": 1, "confidence": 0.5})
+ # AI prior needs corroboration (count) + high confidence.
+ assert not should_auto_suppress({"disposition": "benign", "author": AI, "count": 1, "confidence": 0.99})
+ assert should_auto_suppress({"disposition": "benign", "author": AI, "count": 3, "confidence": 0.95})
+ # A prior true-positive NEVER auto-closes a future alert.
+ assert not should_auto_suppress({"disposition": "true_positive", "author": HUMAN, "count": 9, "confidence": 1.0})
+
+
+async def test_record_and_lookup_roundtrip_and_count_bump():
+ v1 = await record_outcome(TENANT, "sig-1", disposition="false_positive", confidence=0.9, author=AI)
+ assert v1["count"] == 1
+ v2 = await record_outcome(TENANT, "sig-1", disposition="false_positive", confidence=0.92, author=AI)
+ assert v2["count"] == 2 # same disposition bumps the count
+ prior = await lookup_prior(TENANT, "sig-1")
+ assert prior is not None and prior["count"] == 2
+ # Human authorship is sticky: a later AI write does not downgrade it.
+ await record_outcome(TENANT, "sig-1", disposition="false_positive", confidence=0.5, author=HUMAN)
+ assert (await lookup_prior(TENANT, "sig-1"))["author"] == HUMAN
+ await record_outcome(TENANT, "sig-1", disposition="false_positive", confidence=0.5, author=AI)
+ assert (await lookup_prior(TENANT, "sig-1"))["author"] == HUMAN
+
+
+# ── worker: forward suppression + write-back ─────────────────────────────────
+
+
+async def test_repeat_alert_is_suppressed_from_human_prior(monkeypatch):
+ msg = _fused()
+ sig = _signature(msg)
+ # A human previously dispositioned this signature benign.
+ await record_outcome(TENANT, sig, disposition="false_positive", confidence=0.95, author=HUMAN)
+
+ # run_triage must NOT be called — suppression short-circuits it.
+ called = {"triage": 0}
+
+ async def _no_triage(state):
+ called["triage"] += 1
+ return state
+
+ monkeypatch.setattr(worker_mod, "run_triage", _no_triage)
+
+ result = await FusedAlertTriageWorker(bootstrap_servers="unused").triage(msg)
+ assert result is not None
+ assert result["suppressed_by_memory"] is True
+ assert result["tier"] == "memory"
+ assert result["verdict"] == "false_positive"
+ assert called["triage"] == 0 # no re-triage → volume shrinks
+ assert FusedAlertTriageWorker.get_metrics()["outcome_suppressed"] >= 1
+
+
+async def test_no_suppression_without_trusted_prior():
+ # A single low-confidence AI prior is NOT enough to suppress.
+ msg = _fused()
+ sig = _signature(msg)
+ await record_outcome(TENANT, sig, disposition="benign", confidence=0.5, author=AI)
+ result = await FusedAlertTriageWorker(bootstrap_servers="unused").triage(msg)
+ assert result is not None
+ assert result.get("suppressed_by_memory") is not True # normal triage ran
+
+
+async def test_triage_writes_outcome_prior_back(monkeypatch):
+ # Deterministic triage produces a verdict; it must be written back as a prior.
+ msg = _fused()
+ sig = _signature(msg)
+ assert await lookup_prior(TENANT, sig) is None
+ await FusedAlertTriageWorker(bootstrap_servers="unused").triage(msg)
+ prior = await lookup_prior(TENANT, sig)
+ assert prior is not None
+ assert prior["author"] == AI
+ assert prior["disposition"] # a real verdict was recorded
diff --git a/services/api/app/api/v1/endpoints/metrics.py b/services/api/app/api/v1/endpoints/metrics.py
index a72f324bd..cb561d109 100644
--- a/services/api/app/api/v1/endpoints/metrics.py
+++ b/services/api/app/api/v1/endpoints/metrics.py
@@ -25,7 +25,7 @@ from datetime import UTC, datetime, timedelta
from fastapi import APIRouter, Query
from pydantic import BaseModel
-from sqlalchemy import and_, func, select
+from sqlalchemy import and_, func, select, text
from app.api.v1.deps import AuthUser, DBSession
from app.core.config import settings
@@ -153,6 +153,11 @@ class FunnelMetrics(BaseModel):
mitre_coverage: MitreCoverage
deltas: FunnelDeltas
generated_at: datetime
+ # Wave 1 (close the loop): measured count of repeat alerts auto-suppressed
+ # from prior outcome memory in this window (compounding alert reduction).
+ # ``repeat_suppression_rate`` = suppressed / (alerts_generated + suppressed).
+ repeat_alerts_suppressed: int = 0
+ repeat_suppression_rate: float = 0.0
# ────────────────────────── v1.5 Pipeline-health models ───────────────────────
@@ -772,6 +777,25 @@ async def _events_of_interest_from_alerts(db, tenant_id, start, end) -> int:
return int(val or 0)
+async def _repeat_alerts_suppressed(db, tenant_id, start, end) -> int:
+ """Measured count of repeat alerts auto-suppressed from prior outcome memory
+ in [start, end). Tenant-scoped at the query layer. Returns 0 when the table
+ doesn't exist yet (pre-migration) rather than failing the funnel."""
+ try:
+ result = await db.scalar(
+ text(
+ """
+ SELECT count(*) FROM aisoc_outcome_suppressions
+ WHERE tenant_id = :tid AND created_at >= :start AND created_at < :end
+ """
+ ),
+ {"tid": tenant_id, "start": start, "end": end},
+ )
+ return int(result or 0)
+ except Exception: # noqa: BLE001 — analytics table optional; never break the funnel
+ return 0
+
+
async def _funnel_window(db, tenant_id, start, end, *, mitre_total: int) -> dict:
"""Compute the funnel for a single [start, end) window."""
events_of_interest = await _events_of_interest(db, tenant_id, start, end)
@@ -891,6 +915,12 @@ async def _funnel_window(db, tenant_id, start, end, *, mitre_total: int) -> dict
ratio = round(min(covered / mitre_total, 1.0), 4) if mitre_total > 0 else 0.0
coverage = MitreCoverage(covered=covered, total=mitre_total, ratio=ratio)
+ suppressed = await _repeat_alerts_suppressed(db, tenant_id, start, end)
+ # Compounding reduction: of everything the loop *could* have queued
+ # (generated + suppressed), what share did prior outcomes suppress?
+ denom = alerts_generated + suppressed
+ suppression_rate = round(suppressed / denom, 4) if denom else 0.0
+
return {
"events_of_interest": int(events_of_interest),
"correlation_instances": int(correlation_instances),
@@ -901,6 +931,8 @@ async def _funnel_window(db, tenant_id, start, end, *, mitre_total: int) -> dict
"correlation_efficiency": correlation_efficiency,
"alert_yield": alert_yield,
"mitre_coverage": coverage,
+ "repeat_alerts_suppressed": int(suppressed),
+ "repeat_suppression_rate": suppression_rate,
}
@@ -997,4 +1029,6 @@ async def get_funnel_metrics(
mitre_coverage=current["mitre_coverage"],
deltas=deltas,
generated_at=now,
+ repeat_alerts_suppressed=current.get("repeat_alerts_suppressed", 0),
+ repeat_suppression_rate=current.get("repeat_suppression_rate", 0.0),
)
diff --git a/services/api/app/api/v1/endpoints/rule_tuning.py b/services/api/app/api/v1/endpoints/rule_tuning.py
index c6311ff51..51c1c1411 100644
--- a/services/api/app/api/v1/endpoints/rule_tuning.py
+++ b/services/api/app/api/v1/endpoints/rule_tuning.py
@@ -13,12 +13,17 @@ the existing ``/detection`` URL tree without colliding with
from __future__ import annotations
import uuid
+from dataclasses import asdict
+from datetime import UTC, datetime
from typing import Annotated, Literal
from fastapi import APIRouter, Depends, Query, Request
+from pydantic import BaseModel
+from sqlalchemy import text
from app.api.v1.deps import AuthUser, require_permission
from app.db.rls import TenantDBSession
+from app.services.detection_tuner import DispositionRecord, suggest_tuning
from app.services.rule_tuning import (
ApplyTuningRequest,
AutoTuneRequest,
@@ -36,6 +41,117 @@ from app.services.rule_tuning import (
router = APIRouter(prefix="/detection/tuning", tags=["detection"])
+class AutoSuggestResponse(BaseModel):
+ """Disposition-history tuning suggestions + how many became draft proposals."""
+
+ window_days: int
+ records_considered: int
+ suggestions: list[dict]
+ proposals_created: int
+
+
+def _disposition_records(rows) -> list[DispositionRecord]: # noqa: ANN001
+ """Map alert-disposition rows into the tuner's input records.
+
+ ``human_verified`` is conservatively False here (these are stored
+ dispositions, not confirmed human overrides), so the tuner only proposes
+ scoped exceptions / severity reductions — never a bare ``suppress`` — from
+ this surface. Human-confirmed suppression still flows via the override path.
+ """
+ out: list[DispositionRecord] = []
+ for r in rows:
+ rid = getattr(r, "rule_id", None)
+ if not rid:
+ continue
+ out.append(
+ DispositionRecord(
+ rule_id=str(rid),
+ disposition=str(getattr(r, "disposition", "") or ""),
+ entity=(getattr(r, "entity", None) or None),
+ human_verified=False,
+ )
+ )
+ return out
+
+
[email protected]("/auto-suggest", response_model=AutoSuggestResponse)
+async def auto_suggest_tuning(
+ current_user: Annotated[AuthUser, Depends(require_permission("rules:write"))],
+ db: TenantDBSession,
+ window_days: int = Query(default=30, ge=1, le=180),
+ create_proposals: bool = Query(default=True),
+) -> AutoSuggestResponse:
+ """Derive human-approval-gated tuning suggestions from disposition history.
+
+ Wave 1 wiring: connects the previously-orphaned ``suggest_tuning`` engine to
+ real per-rule disposition history and, optionally, opens DRAFT detection
+ proposals (source ``auto-tuner``) into the governed lifecycle. Nothing is
+ auto-applied — an engineer reviews + eval-gates before promotion.
+ """
+ rows = (
+ await db.execute(
+ text(
+ """
+ SELECT rule_id,
+ disposition,
+ COALESCE(affected_hosts->>0, affected_users->>0, affected_ips->>0) AS entity
+ FROM alerts
+ WHERE tenant_id = :tid
+ AND rule_id IS NOT NULL
+ AND disposition IS NOT NULL
+ AND created_at > now() - make_interval(days => :days)
+ """
+ ).bindparams(tid=current_user.tenant_id, days=window_days)
+ )
+ ).fetchall()
+
+ records = _disposition_records(rows)
+ suggestions = suggest_tuning(records)
+
+ created = 0
+ if create_proposals and suggestions:
+ now = datetime.now(UTC)
+ for s in suggestions:
+ body = (
+ f"# Auto-tuner proposal for rule {s.rule_id}\n"
+ f"# Action: {s.action}\n"
+ f"# {s.rationale}\n"
+ f"# FP rate {s.fp_rate:.0%} over {s.sample_count} decided alerts"
+ + (f"; cluster entity: {s.entity}\n" if s.entity else "\n")
+ + "# TODO(analyst): review + attach fixtures before promotion.\n"
+ )
+ await db.execute(
+ text(
+ """
+ INSERT INTO aisoc_detection_rule_proposals
+ (id, tenant_id, base_rule_id, name, description,
+ rule_language, rule_body, category, severity, confidence,
+ status, source, created_at, updated_at)
+ VALUES
+ (:id, :tid, NULL, :name, :desc,
+ 'sigma', :body, 'tuning', 'low', 60,
+ 'draft', 'auto-tuner', :now, :now)
+ """
+ ).bindparams(
+ id=uuid.uuid4(),
+ tid=current_user.tenant_id,
+ name=f"tune {s.action}: {s.rule_id}"[:200],
+ desc=s.rationale[:2000],
+ body=body,
+ now=now,
+ )
+ )
+ created += 1
+ await db.commit()
+
+ return AutoSuggestResponse(
+ window_days=window_days,
+ records_considered=len(records),
+ suggestions=[asdict(s) for s in suggestions],
+ proposals_created=created,
+ )
+
+
@router.get("", response_model=TuningResponse)
async def get_tuning_workbench(
current_user: Annotated[AuthUser, Depends(require_permission("rules:read"))],
diff --git a/services/api/app/workers/hunt_scheduler.py b/services/api/app/workers/hunt_scheduler.py
index 57bcf2e6d..020249280 100644
--- a/services/api/app/workers/hunt_scheduler.py
+++ b/services/api/app/workers/hunt_scheduler.py
@@ -55,7 +55,10 @@ the flag the same way it gates ``oauth_refresh`` and
from __future__ import annotations
import asyncio
+import contextlib
import logging
+import os
+import uuid
from collections.abc import Awaitable, Callable
from datetime import UTC, datetime, timedelta
@@ -225,6 +228,65 @@ async def _open_case_for_hits(db: AsyncSession, hunt: SavedHunt, hit_count: int)
)
+def _hunt_to_detection_enabled() -> bool:
+ """Bridge scheduled-hunt findings into governed detection proposals (Wave 1).
+ On by default; disable with AISOC_HUNT_TO_DETECTION=false."""
+ return os.getenv("AISOC_HUNT_TO_DETECTION", "true").strip().lower() not in {"0", "false", "no", "off"}
+
+
+async def _propose_detection_from_hunt(db: AsyncSession, hunt: SavedHunt, hit_count: int) -> None:
+ """Open a DRAFT detection proposal from a scheduled hunt that found hits.
+
+ Closes the "hunt finding -> new detection" loop: a recurring, productive
+ hunt becomes a candidate detection in the governed propose -> review ->
+ eval-gate -> promote lifecycle. The proposal is a DRAFT scaffold (source
+ ``hunt-finding``) that an engineer converts into detection logic + fixtures
+ before promotion — findings never auto-promote.
+ """
+ if hit_count <= 0 or not _hunt_to_detection_enabled():
+ return
+ query = hunt.translated_query if isinstance(hunt.translated_query, dict) else {}
+ lang = str(getattr(hunt, "language", "esql") or "esql")
+ q_text = str(query.get(lang) or query.get("esql") or query.get("spl") or query.get("kql") or "").strip()
+ scaffold = (
+ f"# Draft detection proposed from scheduled hunt {hunt.name!r}.\n"
+ f"# It returned {hit_count} hit(s) on its scheduled run.\n"
+ f"# TODO(analyst): convert the hunt query below into Sigma detection logic\n"
+ f"# and attach positive/negative fixtures before promotion.\n"
+ f"# Original question: {hunt.nl_query}\n"
+ f"# {lang} query: {q_text or '(none captured)'}\n"
+ )
+ await db.execute(
+ text(
+ """
+ INSERT INTO aisoc_detection_rule_proposals
+ (id, tenant_id, base_rule_id, name, description,
+ rule_language, rule_body, category, severity, confidence,
+ status, source, created_at, updated_at)
+ VALUES
+ (:id, :tid, NULL, :name, :desc,
+ 'sigma', :body, 'hunt', 'medium', 60,
+ 'draft', 'hunt-finding', :now, :now)
+ """
+ ).bindparams(
+ id=uuid.uuid4(),
+ tid=hunt.tenant_id,
+ name=f"hunt: {hunt.name}"[:200],
+ desc=(f"Auto-proposed from scheduled hunt {hunt.name!r} ({hit_count} hit(s)). {hunt.nl_query}")[:2000],
+ body=scaffold,
+ now=datetime.now(UTC),
+ )
+ )
+ logger.info("hunt_scheduler.detection_proposed hunt_id=%s tenant=%s hits=%d", hunt.id, hunt.tenant_id, hit_count)
+
+
+async def _on_hunt_hits(db: AsyncSession, hunt: SavedHunt, hit_count: int) -> None:
+ """Default hit handler: open a case AND propose a detection (Wave 1)."""
+ await _open_case_for_hits(db, hunt, hit_count)
+ with contextlib.suppress(Exception):
+ await _propose_detection_from_hunt(db, hunt, hit_count)
+
+
# ---------------------------------------------------------------------------
# Hunt execution — routes through the warehouse provider registry
# ---------------------------------------------------------------------------
@@ -315,7 +377,7 @@ async def run_once(
db = AsyncSessionLocal()
assert db is not None # for type narrowing
now = now or datetime.now(UTC)
- callback = hit_callback or _open_case_for_hits
+ callback = hit_callback or _on_hunt_hits
runner = executor or _execute_hunt
fired = 0
try:
@@ -394,5 +456,7 @@ __all__ = [
"_is_due",
"_interval_seconds_for",
"_next_fire_at",
+ "_on_hunt_hits",
"_open_case_for_hits",
+ "_propose_detection_from_hunt",
]
diff --git a/services/api/migrations/048_outcome_suppressions.sql b/services/api/migrations/048_outcome_suppressions.sql
new file mode 100644
index 000000000..eb65bda36
--- /dev/null
+++ b/services/api/migrations/048_outcome_suppressions.sql
@@ -0,0 +1,25 @@
+-- Wave 1 (close the loop): measured record of repeat alerts auto-suppressed
+-- from prior outcome memory. The auto-triage worker inserts one row each time a
+-- new alert matches a trusted prior benign/false-positive disposition and is
+-- auto-resolved without re-triage, so the compounding alert-reduction metric is
+-- measured from real events rather than estimated.
+--
+-- Tenant isolation is at the query layer (every read filters by tenant_id),
+-- consistent with the /hunts + /cases pattern; no RLS on this analytics table.
+-- Idempotent + transactional.
+BEGIN;
+
+CREATE TABLE IF NOT EXISTS aisoc_outcome_suppressions (
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ tenant_id UUID NOT NULL,
+ signature TEXT NOT NULL,
+ alert_id UUID,
+ disposition TEXT NOT NULL,
+ prior_author TEXT NOT NULL DEFAULT 'ai',
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now()
+);
+
+CREATE INDEX IF NOT EXISTS idx_outcome_suppressions_tenant_time
+ ON aisoc_outcome_suppressions (tenant_id, created_at DESC);
+
+COMMIT;
diff --git a/services/api/tests/test_wave1_loop_edges.py b/services/api/tests/test_wave1_loop_edges.py
new file mode 100644
index 000000000..0bfe1c9a3
--- /dev/null
+++ b/services/api/tests/test_wave1_loop_edges.py
@@ -0,0 +1,82 @@
+"""Wave 1 - close-the-loop edges on the API side.
+
+Covers the hunt-finding -> detection-proposal bridge (W1.5) and the
+disposition-history -> tuning-record mapping that gives the previously-orphaned
+``suggest_tuning`` a real caller (W1.4). Both use lightweight mocks (no DB) so
+they run fast and deterministically.
+"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+from uuid import uuid4
+
+import pytest
+from app.api.v1.endpoints.rule_tuning import _disposition_records
+from app.workers import hunt_scheduler
+
+
+def _hunt():
+ return SimpleNamespace(
+ id=uuid4(),
+ tenant_id=uuid4(),
+ name="Iran inbound auth failures",
+ nl_query="did we get auth failures from Iran?",
+ language="esql",
+ translated_query={"esql": "FROM logs | WHERE country == 'IR'"},
+ )
+
+
+# ── W1.5: hunt finding -> detection proposal ────────────────────────────────
+
+
+async def test_hunt_hits_open_a_detection_proposal(monkeypatch):
+ monkeypatch.setenv("AISOC_HUNT_TO_DETECTION", "true")
+ db = AsyncMock()
+ await hunt_scheduler._propose_detection_from_hunt(db, _hunt(), 5)
+ assert db.execute.await_count == 1
+ sql = str(db.execute.await_args.args[0])
+ assert "aisoc_detection_rule_proposals" in sql
+
+
+async def test_no_proposal_without_hits(monkeypatch):
+ monkeypatch.setenv("AISOC_HUNT_TO_DETECTION", "true")
+ db = AsyncMock()
+ await hunt_scheduler._propose_detection_from_hunt(db, _hunt(), 0)
+ db.execute.assert_not_awaited()
+
+
+async def test_hunt_to_detection_flag_disables_bridge(monkeypatch):
+ monkeypatch.setenv("AISOC_HUNT_TO_DETECTION", "false")
+ db = AsyncMock()
+ await hunt_scheduler._propose_detection_from_hunt(db, _hunt(), 9)
+ db.execute.assert_not_awaited()
+
+
+async def test_default_callback_opens_case_and_proposal(monkeypatch):
+ monkeypatch.setenv("AISOC_HUNT_TO_DETECTION", "true")
+ db = AsyncMock()
+ db.add = lambda *_a, **_k: None # sync add
+ await hunt_scheduler._on_hunt_hits(db, _hunt(), 3)
+ # the proposal INSERT went through db.execute
+ assert db.execute.await_count == 1
+
+
+# ── W1.4: disposition history -> tuner input ────────────────────────────────
+
+
+def test_disposition_records_mapping_and_skips_null_rule():
+ rows = [
+ SimpleNamespace(rule_id="det-1", disposition="false_positive", entity="host-a"),
+ SimpleNamespace(rule_id="det-1", disposition="true_positive", entity="host-b"),
+ SimpleNamespace(rule_id=None, disposition="false_positive", entity="host-c"), # skipped
+ ]
+ records = _disposition_records(rows)
+ assert len(records) == 2 # the rule_id=None row is dropped
+ assert {r.rule_id for r in records} == {"det-1"}
+ assert all(r.human_verified is False for r in records)
diff --git a/services/fusion/app/main.py b/services/fusion/app/main.py
index 5231893ac..7ff6e749d 100644
--- a/services/fusion/app/main.py
+++ b/services/fusion/app/main.py
@@ -1,4 +1,5 @@
import asyncio
+import os
from contextlib import asynccontextmanager
import redis.asyncio as aioredis
@@ -8,6 +9,7 @@ from app._health import install_health_routes
from app.api.router import router, set_worker
from app.core.config import settings
from app.core.logging import configure_logging, logger
+from app.memory.provider import MemoryPriorProvider
from app.services.alert_enricher import AlertEnricher
from app.services.alert_sink import AlertSink
from app.services.attack_chain_grouper import AttackChainGrouper
@@ -53,6 +55,11 @@ async def lifespan(app: FastAPI):
if settings.fuse_enrichment_enabled
else None
)
+ # Wave 1 — live institutional-memory nudge: distil disposition history into
+ # per-tenant per-signature priors and feed them to the confidence scorer.
+ memory_provider = (
+ MemoryPriorProvider() if os.getenv("AISOC_FUSION_MEMORY_NUDGE", "1").strip().lower() not in {"0", "false", "no", "off"} else None
+ )
engine = FusionEngine(
dedup,
correlator,
@@ -61,6 +68,7 @@ async def lifespan(app: FastAPI):
ueba_cache=ueba_cache,
chain_grouper=chain_grouper,
enricher=enricher,
+ memory_provider=memory_provider,
)
# Phase 3.1 — fused alerts land in the Postgres alert store so the spine
# is continuous (raw event → alert row). Fail-soft: a missing/unreachable
@@ -99,6 +107,23 @@ async def lifespan(app: FastAPI):
app.state.worker_task = worker_task
app.state.redis = redis_client
+ # Wave 1 — periodically distil disposition history into memory priors so
+ # the confidence nudge stays current (default every 6h). Best-effort.
+ async def _refresh_memory_priors() -> None:
+ interval = max(int(os.getenv("AISOC_FUSION_MEMORY_REFRESH_SECONDS", "21600")), 300)
+ while True:
+ try:
+ if memory_provider is not None and sink is not None:
+ pool = await sink._ensure_pool() # noqa: SLF001 — reuse the sink's pool
+ if pool is not None:
+ await memory_provider.refresh(pool)
+ except Exception as exc: # noqa: BLE001 — never crash the service on a refresh
+ logger.warning("memory_prior_refresh_failed", error=str(exc))
+ await asyncio.sleep(interval)
+
+ memory_task = asyncio.create_task(_refresh_memory_priors()) if memory_provider is not None else None
+ app.state.memory_task = memory_task
+
# Phase 2.6 — flip /readyz to 200 now that Redis is open + the
# Kafka consumer is running.
app.state.mark_ready()
@@ -114,6 +139,8 @@ async def lifespan(app: FastAPI):
logger.info("Shutting down Alert Fusion Service")
await worker.stop()
worker_task.cancel()
+ if memory_task is not None:
+ memory_task.cancel()
await redis_client.aclose()
logger.info("Alert Fusion Service stopped")
diff --git a/services/fusion/app/memory/provider.py b/services/fusion/app/memory/provider.py
new file mode 100644
index 000000000..854c14f84
--- /dev/null
+++ b/services/fusion/app/memory/provider.py
@@ -0,0 +1,89 @@
+"""Live per-tenant memory priors for the confidence scorer (Wave 1).
+
+Closes the "distill exists but never reaches the live verdict path" gap: this
+provider periodically distills the tenant's alert-disposition history into
+per-signature priors (:func:`app.memory.distill.distill`) and hands them to the
+``ConfidenceScorer`` so a signature analysts keep correcting to benign nudges
+new alerts of that signature down (bounded ±0.10), and vice-versa.
+
+Priors are keyed per tenant so one tenant's history never nudges another's
+alerts (tenant isolation). Best-effort: no DB / no history => empty priors and
+the scorer is unchanged.
+"""
+
+from __future__ import annotations
+
+import json
+from collections import defaultdict
+from typing import Any
+
+import structlog
+
+from app.memory.distill import SignaturePrior, distill
+
+logger = structlog.get_logger()
+
+# Look-back window + per-signature cap for the distillation query.
+_WINDOW_DAYS = 90
+_MAX_ROWS = 50_000
+
+
+def _primary_technique(value: Any) -> str:
+ if isinstance(value, str):
+ try:
+ value = json.loads(value)
+ except (ValueError, TypeError):
+ return ""
+ if isinstance(value, list) and value:
+ return str(value[0])
+ return ""
+
+
+class MemoryPriorProvider:
+ """In-process cache of per-tenant signature priors, refreshed on a cadence."""
+
+ def __init__(self) -> None:
+ self._by_tenant: dict[str, dict[str, SignaturePrior]] = {}
+
+ def get_priors(self, tenant_id: str) -> dict[str, SignaturePrior]:
+ return self._by_tenant.get(str(tenant_id), {})
+
+ async def refresh(self, pool: Any) -> int:
+ """Rebuild the per-tenant prior cache from disposition history.
+
+ Returns the number of tenants refreshed. Never raises — a query failure
+ keeps the previous cache in place.
+ """
+ if pool is None:
+ return 0
+ try:
+ async with pool.acquire() as conn:
+ rows = await conn.fetch(
+ """
+ SELECT tenant_id, connector_type, mitre_techniques, disposition
+ FROM alerts
+ WHERE disposition IS NOT NULL
+ AND created_at > now() - make_interval(days => $1)
+ LIMIT $2
+ """,
+ _WINDOW_DAYS,
+ _MAX_ROWS,
+ )
+ except Exception as exc: # noqa: BLE001 — keep the last good cache
+ logger.debug("memory_provider.refresh_failed", error=str(exc))
+ return 0
+
+ by_tenant_rows: dict[str, list[dict[str, Any]]] = defaultdict(list)
+ for r in rows:
+ by_tenant_rows[str(r["tenant_id"])].append(
+ {
+ "category": "",
+ "connector_type": r["connector_type"] or "",
+ "primary_technique": _primary_technique(r["mitre_techniques"]),
+ "corrected_verdict": r["disposition"],
+ }
+ )
+
+ self._by_tenant = {tid: distill(rws).priors for tid, rws in by_tenant_rows.items()}
+ logger.info("memory_provider.refreshed", tenants=len(self._by_tenant))
+ return len(self._by_tenant)
diff --git a/services/fusion/app/services/confidence.py b/services/fusion/app/services/confidence.py
index 7219d4272..2e94cc239 100644
--- a/services/fusion/app/services/confidence.py
+++ b/services/fusion/app/services/confidence.py
@@ -47,6 +47,8 @@ from __future__ import annotations
import structlog
+from app.memory.distill import SignaturePrior, signature_key
+from app.memory.stage import MEMORY_CAP, memory_contribution
from app.models.alert import (
AlertSeverity,
ConfidenceFactor,
@@ -179,7 +181,7 @@ class ConfidenceScorer:
def enabled(self) -> bool:
return self._enabled
- def score(self, fused: FusedAlert) -> FusedAlert:
+ def score(self, fused: FusedAlert, priors: dict[str, SignaturePrior] | None = None) -> FusedAlert:
if not self._enabled:
# Break-glass path. Leave the model defaults in place so the
# frontend can degrade gracefully (no chip, no rationale panel)
@@ -277,6 +279,30 @@ class ConfidenceScorer:
)
)
+ # 8. Institutional memory nudge (Wave 1) — a bounded ±MEMORY_CAP
+ # adjustment from the distilled per-signature prior. A signature the
+ # analysts have repeatedly corrected to benign pulls new alerts of that
+ # signature down; one repeatedly confirmed nudges up. Unknown
+ # signatures contribute nothing (no factor added).
+ if priors:
+ sig = signature_key(
+ category="",
+ connector_type=(alert.connector_type or ""),
+ primary_technique=(alert.mitre_techniques[0] if alert.mitre_techniques else ""),
+ )
+ mem = memory_contribution(sig, priors)
+ if mem.sample_count > 0:
+ rationale.append(
+ ConfidenceFactor(
+ factor="institutional_memory",
+ label="Institutional memory",
+ value=mem.basis,
+ # contribution*weight == mem.delta (bounded ±MEMORY_CAP).
+ contribution=round(mem.delta / MEMORY_CAP, 4) if MEMORY_CAP else 0.0,
+ weight=MEMORY_CAP,
+ )
+ )
+
# Combine: score = 0.5 + Σ(w_i * c_i), clamped to [0, 1]
delta = sum(f.contribution * f.weight for f in rationale)
# Map [-0.5, +0.5] possible range onto a full [0, 1] using the same
diff --git a/services/fusion/app/services/fusion_engine.py b/services/fusion/app/services/fusion_engine.py
index 2b0bf3d79..99d9e2eaa 100644
--- a/services/fusion/app/services/fusion_engine.py
+++ b/services/fusion/app/services/fusion_engine.py
@@ -4,6 +4,7 @@ Core fusion engine: orchestrates deduplication → correlation → ML scoring.
from __future__ import annotations
+from typing import Any
from uuid import UUID
import structlog
@@ -124,11 +125,16 @@ class FusionEngine:
ueba_cache: UebaSignalCache | None = None,
chain_grouper: AttackChainGrouper | None = None,
enricher: AlertEnricher | None = None,
+ memory_provider: Any = None,
) -> None:
self._dedup = deduplicator
self._correlator = correlator
self._ml_scorer = ml_scorer or MLScorer()
self._entity_risk = entity_risk
+ # Wave 1 — live institutional-memory priors for the confidence nudge
+ # (optional; None = no nudge). Refreshed out-of-band from disposition
+ # history by app.memory.provider.MemoryPriorProvider.
+ self._memory_provider = memory_provider
# Phase A4 — behavioral-model signal (optional; None = no UEBA fusion).
self._ueba_cache = ueba_cache
# Phase C4 — fuse-time attack-chain former/extender (optional).
@@ -206,7 +212,8 @@ class FusionEngine:
# Pure, synchronous projection of the values already on ``fused``.
# Runs after ML scoring so the rationale picks up anomaly / priority.
try:
- fused = self._confidence_scorer.score(fused)
+ priors = self._memory_provider.get_priors(str(fused.tenant_id)) if self._memory_provider is not None else None
+ fused = self._confidence_scorer.score(fused, priors=priors)
except Exception as exc:
logger.warning("confidence_scoring_failed", error=str(exc))
diff --git a/services/fusion/tests/test_memory_nudge.py b/services/fusion/tests/test_memory_nudge.py
new file mode 100644
index 000000000..bbe4acdef
--- /dev/null
+++ b/services/fusion/tests/test_memory_nudge.py
@@ -0,0 +1,105 @@
+"""Wave 1 - live institutional-memory nudge in the confidence scorer.
+
+Proves a distilled per-signature prior nudges new alerts of that signature
+(benign history pulls down, confirmed history pushes up), that the nudge is
+bounded (never dominates), that an unknown signature changes nothing, and that
+the provider distils per-tenant priors from disposition history.
+"""
+
+from __future__ import annotations
+
+import uuid
+from contextlib import asynccontextmanager
+
+import pytest
+from app.memory.distill import SignaturePrior, signature_key
+from app.memory.provider import MemoryPriorProvider
+from app.memory.stage import MEMORY_CAP
+from app.models.alert import AlertSeverity, FusedAlert, FusionDecision, RawAlert
+from app.services.confidence import ConfidenceScorer
+
+CONNECTOR = "crowdstrike_falcon"
+TECH = "T1003"
+
+
+def _fused() -> FusedAlert:
+ raw = RawAlert(
+ tenant_id=uuid.uuid4(),
+ source="x",
+ title="Credential dumping",
+ severity=AlertSeverity.HIGH,
+ connector_type=CONNECTOR,
+ mitre_techniques=[TECH],
+ )
+ return FusedAlert(id=raw.id, tenant_id=raw.tenant_id, fusion_decision=FusionDecision.NEW_ALERT, alert=raw)
+
+
+def _priors(prior_val: float, *, connector: str = CONNECTOR, tech: str = TECH, n: int = 20):
+ sig = signature_key("", connector, tech)
+ return {sig: SignaturePrior(signature_key=sig, category="", sample_count=n, fp_rate=round(1.0 - prior_val, 4), prior=prior_val)}
+
+
+def test_benign_prior_nudges_confidence_down_bounded():
+ scorer = ConfidenceScorer()
+ base = scorer.score(_fused()).confidence_score
+ nudged = scorer.score(_fused(), priors=_priors(0.0)).confidence_score # always benign
+ assert nudged < base
+ assert (base - nudged) <= MEMORY_CAP + 1e-6 # bounded — never dominates
+
+
+def test_confirmed_prior_nudges_confidence_up_bounded():
+ scorer = ConfidenceScorer()
+ base = scorer.score(_fused()).confidence_score
+ nudged = scorer.score(_fused(), priors=_priors(1.0)).confidence_score # always TP
+ assert nudged > base
+ assert (nudged - base) <= MEMORY_CAP + 1e-6
+
+
+def test_unknown_signature_leaves_score_unchanged():
+ scorer = ConfidenceScorer()
+ base = scorer.score(_fused()).confidence_score
+ # A prior for a *different* signature must not touch this alert.
+ other = scorer.score(_fused(), priors=_priors(0.0, connector="okta", tech="T1078")).confidence_score
+ assert other == base
+ factors = {f.factor for f in scorer.score(_fused(), priors=_priors(0.0, connector="okta")).confidence_rationale}
+ assert "institutional_memory" not in factors
+
+
+class _FakeConn:
+ def __init__(self, rows):
+ self._rows = rows
+
+ async def fetch(self, *_a):
+ return self._rows
+
+
+class _FakePool:
+ def __init__(self, rows):
+ self._conn = _FakeConn(rows)
+
+ def acquire(self):
+ @asynccontextmanager
+ async def _cm():
+ yield self._conn
+
+ return _cm()
+
+
+async def test_provider_distils_per_tenant_priors():
+ t1 = str(uuid.uuid4())
+ t2 = str(uuid.uuid4())
+ rows = [
+ {"tenant_id": t1, "connector_type": CONNECTOR, "mitre_techniques": '["T1003"]', "disposition": "false_positive"},
+ {"tenant_id": t1, "connector_type": CONNECTOR, "mitre_techniques": '["T1003"]', "disposition": "false_positive"},
+ {"tenant_id": t2, "connector_type": "okta", "mitre_techniques": ["T1078"], "disposition": "true_positive"},
+ ]
+ provider = MemoryPriorProvider()
+ await provider.refresh(_FakePool(rows))
+
+ p1 = provider.get_priors(t1)
+ assert p1 # tenant 1 has a benign prior for the cred-dump signature
+ sig1 = signature_key("", CONNECTOR, "T1003")
+ assert p1[sig1].fp_rate == 1.0
+ # tenant isolation: tenant 2's priors never appear under tenant 1.
+ assert sig1 not in provider.get_priors(t2)