Pool baseline state across streams sharing an entity concept

Completes the last deferred item from ROADMAP §0: entities recognized as
the same real-world concept across enabled streams (e.g. srcip in both a
firewall and an IIS stream) now share baseline knowledge of entity
presence/age and known relationship-target values, instead of each stream
learning it cold. Implemented additively (synthetic shared_entity:<group>
namespace in the existing baseline.py tables, no schema migration), scoped
to entity presence + relationship known-values only — per-stream
distribution baselines stay untouched. Also fixes a latent alias-map
collision bug found while building this (ALIAS_CANONICAL_BY_FIELD let
"context" silently steal "hostname"/"dns_query" from their own canonical
groups).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
larssand
2026-09-23 18:50:14 +02:00
parent 8c155ec9cf
commit 83ffef3570
12 changed files with 305 additions and 41 deletions

View File

@@ -183,6 +183,57 @@ class BaselineTests(unittest.TestCase):
self.assertNotIn("alice", store.profile_deviations(burst, profiles, min_training_days=7))
self.assertIn("alice", store.profile_deviations(burst, profiles, min_training_days=0))
def test_entity_pools_share_known_relationship_values_across_streams(self):
with tempfile.TemporaryDirectory() as directory:
store = BaselineStore(str(Path(directory) / "baseline.sqlite3"))
profiles = parse_profiles([
{
"stream_id": "firewall",
"entity_field": "srcip",
"entity_fields": ["srcip"],
"categorical_fields": ["action"],
"relationship_fields": [{"left": "srcip", "right": "dstip", "name": "src to dst"}],
},
{
"stream_id": "iis",
"entity_field": "client_ip",
"entity_fields": ["client_ip"],
"categorical_fields": ["action"],
"relationship_fields": [{"left": "client_ip", "right": "dstip", "name": "client to dst"}],
},
])
entity_pools = {
"firewall": {"srcip": "shared_entity:srcip"},
"iis": {"client_ip": "shared_entity:srcip"},
}
for index in range(12):
# firewall establishes pooled knowledge of srcip=10.0.0.10 -> dstip=8.8.8.8
fw_event = parse_log_line(f"fgai_stream_id=firewall srcip=10.0.0.10 dstip=8.8.8.8 action=allow baseline={index}")
store.ingest_profile_fields([fw_event], profiles, observed_at=1_700_000_000 + index * 300, entity_pools=entity_pools)
# iis independently establishes its own (unpooled) history for a different destination
iis_history_event = parse_log_line(f"fgai_stream_id=iis client_ip=10.0.0.10 dstip=9.9.9.9 action=allow baseline={index}")
store.ingest_profile_fields([iis_history_event], profiles, observed_at=1_700_000_000 + index * 300)
iis_event = [parse_log_line("fgai_stream_id=iis client_ip=10.0.0.10 dstip=8.8.8.8 action=allow current=1")]
# Pooled: dstip=8.8.8.8 is already known via the firewall stream's contribution to the shared pool.
pooled = store.profile_deviations(iis_event, profiles, entity_pools=entity_pools)
self.assertNotIn("new_relationship", [item["detector"] for item in pooled.get("10.0.0.10", [])])
# Unpooled: iis's own history only knows dstip=9.9.9.9, so 8.8.8.8 is still reported new — proves
# the feature is additive and today's per-stream behavior is unchanged without entity_pools.
unpooled = store.profile_deviations(iis_event, profiles)
relation = next(item for item in unpooled["10.0.0.10"] if item["detector"] == "new_relationship")
self.assertEqual(relation["field"], "relationship:client_ip->dstip")
self.assertEqual(relation["value"], "8.8.8.8")
# Genuinely new everywhere: pooling never suppresses a value nobody has ever seen.
genuinely_new = [parse_log_line("fgai_stream_id=iis client_ip=10.0.0.10 dstip=1.2.3.4 current=1")]
still_new = store.profile_deviations(genuinely_new, profiles, entity_pools=entity_pools)
relation = next(item for item in still_new["10.0.0.10"] if item["detector"] == "new_relationship")
self.assertEqual(relation["value"], "1.2.3.4")
def test_custom_relationship_flags_new_user_source_ip(self):
with tempfile.TemporaryDirectory() as directory:
store = BaselineStore(str(Path(directory) / "baseline.sqlite3"))