netsec-scan: say how many tripwires a seed tick held, and test the path

The seed line now names the held count ('N current-state finding(s) held
for the next tick') so a silent seed tick is explainable without --verbose
log archaeology, and tests/test_seed.py::TestSeedHoldsCurrentStateTripwires
pins the whole contract: seed tick silent with a drop-spike firing, the
fingerprint stored with last_alert='' (due, not cooled), the first warm
tick reports it once, the second is quiet.
This commit is contained in:
Sirius DevOps 2026-11-27 02:21:34 -05:00
commit beb54afb89
3 changed files with 77 additions and 6 deletions

View file

@ -20,6 +20,7 @@ import json
import tempfile
import unittest
from pathlib import Path
from unittest import mock
from helpers import fixture_lines
@ -120,5 +121,66 @@ class TestSeedIsSilent(unittest.TestCase):
self.assertEqual(second, "", "a reported tuple does not repeat")
class TestSeedHoldsCurrentStateTripwires(unittest.TestCase):
"""A seed tick stays silent even when a PromQL tripwire is firing.
The tripwire is current state, not history, so it must not be swallowed: it
is recorded with no cooldown clock and reported by the first warm tick. The
silence costs one interval; a 24 h STATE_COOLDOWN would be a real miss.
"""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = Path(self.tmp.name)
self.flows = self.root / "flows"
self.flows.mkdir()
lines = fixture_lines("egress-sample.jsonl")
with gzip.open(self.flows / ARCHIVES[-1], "wt", encoding="utf-8") as handle:
handle.write("\n".join(lines) + "\n")
(self.flows / LIVE).write_text("\n".join(lines) + "\n")
self.state = self.root / "state.json"
self.reports = self.root / "reports"
def tearDown(self):
self.tmp.cleanup()
@staticmethod
def _scalar(expr: str) -> float:
"""drop-spike shape: 0.9/s now against a 0.01/s baseline."""
if "hubble_drop_total" in expr and "offset 5m" in expr:
return 0.01
if "hubble_drop_total" in expr:
return 0.9
return 0.0
def tick(self):
from netsec_scan import prom as prom_mod
out, err = io.StringIO(), io.StringIO()
with mock.patch.object(prom_mod, "query", return_value=[]), \
mock.patch.object(prom_mod, "scalar", side_effect=self._scalar):
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = cli.main(["--flows-dir", str(self.flows),
"--state", str(self.state), "--reports", str(self.reports)])
self.assertEqual(code, 0)
return out.getvalue(), err.getvalue()
def test_seed_tick_is_silent_and_the_next_tick_reports_the_tripwire(self):
out, err = self.tick()
self.assertEqual(out, "", "a firing tripwire must not break the seed's silence")
self.assertIn("held for the next tick", err)
st = state_mod.load(self.state)
self.assertTrue(st["seeded"])
drop = [v for v in st["seen"].values() if v["rule"] == "drop-spike"]
self.assertEqual(len(drop), 1)
self.assertEqual(drop[0]["last_alert"], "", "a held tripwire is due, not cooled down")
warm, _err = self.tick()
self.assertEqual(len(warm.strip().splitlines()), 1, warm)
self.assertIn("drop-spike", warm)
after, _err = self.tick()
self.assertEqual(after, "", "the state cooldown applies once it has been reported")
if __name__ == "__main__":
unittest.main()