strawberry/tests/test_repeat_aggregator.cpp
trvon 4664ae4458
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Detection: unify the two repeat-aggregation sinks + IOC severity resolution
The scan path (handle_process) and the exec-guard recording path
(record_exec_decision) had grown two near-identical repeat-aggregation maps
(repeats_ / exec_repeats_) plus three copies of the IOC-feed severity idiom.
Refactor to one implementation; no behavior change (the existing aggregation and
IOC-severity tests are the oracle and pass unchanged).

- New include/strawberry/repeat_aggregator.hpp: RepeatAggregator owns the
  per-key window map, the 20-full-then-summary cadence and the aggregated count;
  observe(key, now, seen_ns, pid) returns {full, at_threshold, aggregated,
  emit_summary, notify, state}. Each caller keeps its own key, summary-id prefix,
  summary shape and notify behaviour. Both pipeline sinks now hold one instance
  (scan notifies at most hourly; exec passes notify_ns=0). status
  repeats_aggregated sums both totals (atomic, lock-free read).
- ioc_hit_severity(feed): one source for the three IOC match sites (scan_image,
  assess_exec, handle_process) that was the policy_->ioc_feed_severity ternary.
- Leaves the latency-critical assess_exec/decide path untouched; both sinks still
  run under mutex_, off the FAN response.

Unit test tests/test_repeat_aggregator.cpp (window reset, threshold, summary and
notify cadence, per-key independence). Suite 51/51; clang static analyzer and
clang-tidy clean on detection_pipeline.cpp; fold's 7 int-overflow are the accepted
monotonic-timestamp FP class (unchanged by the move); perf-kpi PASS, no regression
(exec_decision p50 unchanged, exec_assess/db_commit within noise or faster).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-28 17:39:48 -06:00

90 lines
3.9 KiB
C++

// RepeatAggregator: window reset, full-then-summary threshold, summary/notify
// cadence, per-key independence, eviction. Deterministic `now` values (not real
// time) so the cadence arithmetic is exact. The pipeline's
// test_repeated_detections_are_summarized / test_repeated_would_block_is_summarized
// cover it end-to-end; this pins the unit behaviour.
#include "strawberry/repeat_aggregator.hpp"
#include <cstdlib>
#include <iostream>
#include <string>
using namespace strawberry;
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::cerr << "CHECK FAILED: " #cond " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \
std::abort(); \
} \
} while (0)
int main() {
// full=3, window=10000, summary=100, notify=200 (ns).
RepeatAggregator agg(3, 10000, 100, 200);
const std::string k = "would_block|hash|rule";
// counts 1..3 are kept in full; the 3rd is exactly at the threshold.
for (int i = 1; i <= 3; ++i) {
const auto o = agg.observe(k, 1000, 1000, 100 + i);
CHECK(o.full && !o.aggregated && !o.emit_summary && !o.notify);
CHECK(o.at_threshold == (i == 3));
CHECK(o.state->count == static_cast<uint64_t>(i));
}
CHECK(agg.aggregated_total() == 0);
// count 4: first aggregated -> summary due (now-0>=100) and notify due (>=200).
{
const auto o = agg.observe(k, 1000, 1000, 200);
CHECK(o.aggregated && !o.full && o.emit_summary && o.notify);
CHECK(o.state->aggregated == 1 && o.state->count == 4);
CHECK(o.state->first_pid == 200 && o.state->first_seen_ns == 1000);
CHECK(o.state->last_pid == 200 && o.state->last_seen_ns == 1000);
}
CHECK(agg.aggregated_total() == 1);
// count 5 at +50: neither summary (50<100) nor notify (50<200) due.
{
const auto o = agg.observe(k, 1050, 1050, 201);
CHECK(o.aggregated && !o.emit_summary && !o.notify);
CHECK(o.state->aggregated == 2 && o.state->last_pid == 201 && o.state->last_seen_ns == 1050);
CHECK(o.state->first_pid == 200 && o.state->first_seen_ns == 1000); // first_* unchanged
}
// count 6 at +100 since last summary: summary due, notify still not (100<200).
{
const auto o = agg.observe(k, 1100, 1100, 202);
CHECK(o.aggregated && o.emit_summary && !o.notify);
}
// count 7: notify now due (300>=200) and summary due (200>=100).
{
const auto o = agg.observe(k, 1300, 1300, 203);
CHECK(o.aggregated && o.emit_summary && o.notify);
}
CHECK(agg.aggregated_total() == 4);
// Window reset: a gap larger than the window starts a fresh full record.
{
const auto o = agg.observe(k, 20000, 20000, 300);
CHECK(o.full && !o.aggregated && o.state->count == 1 && o.state->window_start_ns == 20000);
}
// notify_ns == 0: never notifies, still summarizes.
RepeatAggregator nonotify(1, 10000, 100, 0);
CHECK(nonotify.observe("x", 1000, 1000, 1).full);
{
const auto o = nonotify.observe("x", 2000, 2000, 2);
CHECK(o.aggregated && o.emit_summary && !o.notify);
}
// Per-key independence; aggregated_total sums across keys.
RepeatAggregator multi(1, 10000, 1, 0);
CHECK(multi.observe("a", 1000, 1000, 1).full);
CHECK(multi.observe("a", 1001, 1001, 1).aggregated);
CHECK(multi.observe("b", 1000, 1000, 1).full);
CHECK(multi.observe("b", 1001, 1001, 1).aggregated);
CHECK(multi.aggregated_total() == 2);
std::cout << "RepeatAggregator: PASSED" << std::endl;
return 0;
}