feat(P02): SLICE-07 cohort aggregation pipeline — k-anon, hook, nightly

TASK-07-01: server/cohort/aggregator.py — aggregate_session with k-anon
  write-time suppression (D-034, K_ANON_THRESHOLD=10), idempotent upsert,
  7-day rolling window, multiple metrics (sessions_count, active_learners,
  gate_open_rate, median_mastery_score, rubric_criterion_means,
  failure_mode_frequency, branch distribution). No PII in aggregates (D-031).
TASK-07-02: server/cohort/hook.py — on_session_end fire-and-forget (D-054),
  no-op when no Postgres, failures log + nightly reconciles.
TASK-07-03: server/cohort/nightly.py — NightlyScheduler in-process asyncio
  loop, 03:00 CT (America/Winnipeg approx), reconcile from mastery_gate_events,
  R-DASH-04 failure handling.
TASK-07-04: session_recorder.py — chain aggregation hook after mastery flow
  via asyncio.create_task (parallel, off voice path, D-054).
TASK-07-05: tests/test_cohort_aggregation.py — k-anon threshold (9/10/11),
  idempotent, 7-day window, metrics, no PII.
TASK-07-06: tests/test_cohort_nightly.py — scheduler timing, reconciliation,
  hook-failure+nightly recovery, R-DASH-04.
G-038 (binding): differencing-attack test — 10 learners window A, 9 in B,
  verify dropped learner cannot be isolated (B suppressed, value=NULL).

---ci---
project: praxis
phase: 2
milestone: v0.4
status: execute
persona: backend-engineer
task: 07-01..07-06
requirements:
  covered: [REQ-MT-02, REQ-NFR-DASH-02, REQ-NFR-DASH-01]
---/ci---
This commit is contained in:
Praxis CI
2026-08-04 02:01:06 +00:00
parent d3a67511e5
commit c396ded395
8 changed files with 1003 additions and 0 deletions
+246
View File
@@ -0,0 +1,246 @@
"""Cohort aggregation unit tests (TASK-07-05) — mocked PgStore, no Postgres.
Covers: k-anonymity suppression (9 vs 10 vs 11 learners), idempotent upsert,
7-day window computation, multiple metrics, no PII in upsert calls.
G-038 (binding — differencing-attack test): seed 10 learners in window A and
9 in window B (one dropped), verify the API/aggregation cannot isolate the
dropped learner — both windows show k-anonymized aggregates with no
per-learner data leaks.
"""
from __future__ import annotations
import datetime as _dt
from unittest.mock import AsyncMock, MagicMock
import pytest
from server.cohort.aggregator import (
K_ANON_THRESHOLD,
_rolling_window,
aggregate_session,
)
from server.cohort.hook import on_session_end
def _mock_pg_store():
store = MagicMock()
store.upsert_cohort_aggregate = AsyncMock()
return store
def _session(learner_ref: str, path: str = "customer_service",
outcome: str = "pass", rubric_scores=None,
failure_mode=None, branch_path=None) -> dict:
return {
"learner_ref": learner_ref,
"path": path,
"scenario_id": f"{path}_v01",
"outcome": outcome,
"rubric_scores": rubric_scores or [
{"criterion_id": "empathy", "score": 4.0},
{"criterion_id": "resolution", "score": 3.5},
],
"failure_mode": failure_mode,
"branch_path": branch_path or ["accept"],
"timestamp": _dt.datetime.now(_dt.timezone.utc).isoformat(),
}
# ── k-anonymity threshold ───────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_k_anon_threshold_at_10():
assert K_ANON_THRESHOLD == 10
@pytest.mark.asyncio
async def test_9_learners_suppressed():
store = _mock_pg_store()
for i in range(9):
await aggregate_session(store, _session(f"learner-{i}"))
suppressed_calls = [
c for c in store.upsert_cohort_aggregate.call_args_list
if c.args[6] is True # cell_suppressed
]
non_suppressed = [
c for c in store.upsert_cohort_aggregate.call_args_list
if c.args[6] is False
]
assert suppressed_calls, "cells should be suppressed with <10 learners"
assert not non_suppressed, "no cell should be non-suppressed with 9 learners"
@pytest.mark.asyncio
async def test_10_learners_not_suppressed():
store = _mock_pg_store()
for i in range(10):
await aggregate_session(store, _session(f"learner-{i}"))
non_suppressed = [
c for c in store.upsert_cohort_aggregate.call_args_list
if c.args[6] is False
]
assert non_suppressed, "cells should NOT be suppressed at exactly 10 learners"
# value should be non-null for non-suppressed cells
for c in non_suppressed:
assert c.args[4] is not None, "non-suppressed cell value must not be None"
@pytest.mark.asyncio
async def test_11_learners_not_suppressed():
store = _mock_pg_store()
for i in range(11):
await aggregate_session(store, _session(f"learner-{i}"))
non_suppressed = [
c for c in store.upsert_cohort_aggregate.call_args_list
if c.args[6] is False
]
assert non_suppressed, "11 learners should NOT be suppressed"
# ── Idempotent upsert ──────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_idempotent_same_session_twice():
store = _mock_pg_store()
outcome = _session("learner-x")
await aggregate_session(store, outcome)
await aggregate_session(store, outcome)
# Re-running with the same outcome produces additional upsert calls but
# the ON CONFLICT in PgStore makes them idempotent at the DB layer. The
# hook itself is deterministic — the same learner produces the same
# distinct-count + counter state in the cache.
# Assert at least one upsert happened (the contract is DB-level idempotency).
assert store.upsert_cohort_aggregate.called
# ── 7-day window computation ───────────────────────────────────────────────
def test_rolling_window_7_days():
now = _dt.datetime(2026, 8, 4, 12, 0, tzinfo=_dt.timezone.utc)
start, end = _rolling_window(now)
assert (end - start).days == 6 # 7-day inclusive span
assert end == now.date()
assert start == _dt.date(2026, 7, 29)
# ── Multiple metrics ───────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_multiple_metrics_computed():
store = _mock_pg_store()
await aggregate_session(store, _session("learner-1", rubric_scores=[
{"criterion_id": "empathy", "score": 4.0},
{"criterion_id": "resolution", "score": 3.0},
], failure_mode="missed_apology", branch_path=["escalate"]))
metrics = {c.args[1] for c in store.upsert_cohort_aggregate.call_args_list}
assert "sessions_count" in metrics
assert "active_learners_count" in metrics
assert "gate_open_rate" in metrics
assert "median_mastery_score" in metrics
assert "rubric_criterion_mean:empathy" in metrics
assert "failure_mode:missed_apology" in metrics
assert "branch:escalate" in metrics
# ── No PII in upsert calls ─────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_no_pii_in_upsert_calls():
store = _mock_pg_store()
await aggregate_session(store, _session("learner-sensitive-id-1234"))
for c in store.upsert_cohort_aggregate.call_args_list:
# path, metric, window_start, window_end, value, cell_count, suppressed
# No argument should contain the raw learner_ref string as PII.
for arg in c.args:
assert "learner-sensitive-id-1234" not in str(arg), \
"raw learner_ref must not leak into aggregate cell args"
# cell_count is the distinct-learner count (an integer), not the ref.
assert isinstance(c.args[5], int)
# ── G-038: Differencing-attack test (binding) ──────────────────────────────
# Seed 10 learners in window A, 9 in window B (one dropped). Verify the
# aggregation/API cannot isolate the dropped learner — both windows produce
# k-anonymized aggregates with no per-learner data leaks.
@pytest.mark.asyncio
async def test_g038_differencing_attack_cannot_isolate_dropped_learner():
"""G-038 binding: 10 learners in window A, 9 in window B (one dropped).
A differencing attack tries to subtract window B's aggregate from
window A's to recover the dropped learner's contribution. With k-anon
write-time suppression, window B (9 learners) is FULLY suppressed
(value=NULL, cell_suppressed=TRUE), so the attacker cannot subtract
anything — the dropped learner's contribution is not recoverable.
"""
store_a = _mock_pg_store()
store_b = _mock_pg_store()
# Window A: 10 distinct learners → non-suppressed
for i in range(10):
await aggregate_session(store_a, _session(f"learner-{i}"))
# Window B: 9 distinct learners (learner-9 dropped) → suppressed
for i in range(9):
await aggregate_session(store_b, _session(f"learner-{i}"))
a_cells = list(store_a.upsert_cohort_aggregate.call_args_list)
b_cells = list(store_b.upsert_cohort_aggregate.call_args_list)
# Window A: at least some non-suppressed cells (10 >= threshold)
a_non_suppressed = [c for c in a_cells if c.args[6] is False]
assert a_non_suppressed, "window A (10 learners) should have non-suppressed cells"
# Window B: ALL cells suppressed (9 < threshold)
b_suppressed = [c for c in b_cells if c.args[6] is True]
b_non_suppressed = [c for c in b_cells if c.args[6] is False]
assert b_suppressed, "window B (9 learners) must have suppressed cells"
assert not b_non_suppressed, \
"window B (9 learners) must have NO non-suppressed cells (differencing blocked)"
# The critical differencing-attack defense: window B's suppressed cells
# have value=NULL, so subtracting B from A is not possible — the attacker
# cannot recover learner-9's contribution.
for c in b_suppressed:
assert c.args[4] is None, \
"suppressed cell value must be NULL (differencing-attack defense)"
# No per-learner data leaks in either window's aggregate cells.
for cells in (a_cells, b_cells):
for c in cells:
for arg in c.args:
assert "learner-9" not in str(arg), \
"dropped learner's ref must not appear in any aggregate cell"
# ── Hook (TASK-07-02) ──────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_hook_no_postgres_is_noop():
# No exception, just a warning log.
await on_session_end(None, _session("learner-1"))
@pytest.mark.asyncio
async def test_hook_failure_logs_does_not_raise(monkeypatch):
store = _mock_pg_store()
store.upsert_cohort_aggregate = AsyncMock(side_effect=RuntimeError("boom"))
# Must not raise — the hook swallows + logs; nightly reconciles.
await on_session_end(store, _session("learner-1"))
@pytest.mark.asyncio
async def test_hook_idempotent():
store = _mock_pg_store()
outcome = _session("learner-1")
await on_session_end(store, outcome)
await on_session_end(store, outcome)
assert store.upsert_cohort_aggregate.called
+199
View File
@@ -0,0 +1,199 @@
"""Nightly reconciliation + hook integration tests (TASK-07-06) — mocked PgStore.
Covers: scheduler timing (seconds until 03:00 CT), reconciliation recomputes
all windows, hook failure + nightly reconciliation = correct final state,
R-DASH-04 (nightly failure logs + retries next night).
"""
from __future__ import annotations
import datetime as _dt
from unittest.mock import AsyncMock, MagicMock
import pytest
from server.cohort.nightly import (
CT,
NightlyScheduler,
seconds_until_next_03_ct,
)
# ── Scheduler timing ───────────────────────────────────────────────────────
def test_seconds_until_next_03_ct_future_today():
# 01:00 CT → next 03:00 CT is in 2h
now = _dt.datetime(2026, 8, 4, 1, 0, tzinfo=CT)
secs = seconds_until_next_03_ct(now)
assert 7190 <= secs <= 7200 # ~2h
def test_seconds_until_next_03_ct_past_today_wraps_tomorrow():
# 04:00 CT → next 03:00 CT is tomorrow (23h)
now = _dt.datetime(2026, 8, 4, 4, 0, tzinfo=CT)
secs = seconds_until_next_03_ct(now)
assert 82790 <= secs <= 82810 # ~23h
def test_seconds_until_next_03_ct_exactly_03_rolls_to_tomorrow():
now = _dt.datetime(2026, 8, 4, 3, 0, 0, tzinfo=CT)
secs = seconds_until_next_03_ct(now)
# exactly 03:00:00 → next run is tomorrow (0 secs would mean "now", but
# the scheduler sleeps then runs, so it must be ~24h)
assert secs >= 86390 # ~24h
# ── Reconciliation recomputes all windows ──────────────────────────────────
class _FakeRecord(dict):
"""Mimics an asyncpg Record — dict(record) returns the dict."""
pass
def _mock_pg_store_with_events(events):
store = MagicMock()
store.upsert_cohort_aggregate = AsyncMock()
conn = MagicMock()
rows = [_FakeRecord(e) for e in events]
conn.fetch = AsyncMock(return_value=rows)
cm = MagicMock()
cm.__aenter__ = AsyncMock(return_value=conn)
cm.__aexit__ = AsyncMock(return_value=None)
store.pool = MagicMock()
store.pool.acquire = MagicMock(return_value=cm)
return store
@pytest.mark.asyncio
async def test_reconcile_recomputes_all_paths():
events = [
{"learner_ref": "l1", "path_id": "customer_service", "gate_outcome": "open",
"rubric_scores_jsonb": '[{"criterion_id":"empathy","score":4.0}]',
"recorded_at": _dt.datetime.now(_dt.timezone.utc)},
{"learner_ref": "l2", "path_id": "customer_service", "gate_outcome": "open",
"rubric_scores_jsonb": '[{"criterion_id":"empathy","score":3.0}]',
"recorded_at": _dt.datetime.now(_dt.timezone.utc)},
{"learner_ref": "l3", "path_id": "sales", "gate_outcome": "closed",
"rubric_scores_jsonb": '[]',
"recorded_at": _dt.datetime.now(_dt.timezone.utc)},
]
store = _mock_pg_store_with_events(events)
sched = NightlyScheduler()
await sched.reconcile_now(store)
# upserts should cover both paths × multiple metrics
paths = {c.args[0] for c in store.upsert_cohort_aggregate.call_args_list}
assert "customer_service" in paths
assert "sales" in paths
metrics = {c.args[1] for c in store.upsert_cohort_aggregate.call_args_list}
assert "sessions_count" in metrics
assert "active_learners_count" in metrics
assert "gate_open_rate" in metrics
@pytest.mark.asyncio
async def test_reconcile_suppresses_below_threshold():
# 3 distinct learners → suppressed
events = [
{"learner_ref": f"l{i}", "path_id": "p", "gate_outcome": "open",
"rubric_scores_jsonb": "[]",
"recorded_at": _dt.datetime.now(_dt.timezone.utc)}
for i in range(3)
]
store = _mock_pg_store_with_events(events)
sched = NightlyScheduler()
await sched.reconcile_now(store)
suppressed = [c for c in store.upsert_cohort_aggregate.call_args_list if c.args[6] is True]
non_suppressed = [c for c in store.upsert_cohort_aggregate.call_args_list if c.args[6] is False]
assert suppressed, "3 learners must be suppressed"
assert not non_suppressed, "no cell should be non-suppressed with 3 learners"
@pytest.mark.asyncio
async def test_reconcile_no_events_no_op():
store = _mock_pg_store_with_events([])
sched = NightlyScheduler()
await sched.reconcile_now(store)
store.upsert_cohort_aggregate.assert_not_called()
# ── Hook failure → nightly reconciles ──────────────────────────────────────
@pytest.mark.asyncio
async def test_hook_failure_then_nightly_reconciles_correct_state():
"""A hook failure leaves no aggregate; the nightly job recomputes from
mastery_gate_events and produces the correct final state."""
events = [
{"learner_ref": f"l{i}", "path_id": "p", "gate_outcome": "open",
"rubric_scores_jsonb": "[]",
"recorded_at": _dt.datetime.now(_dt.timezone.utc)}
for i in range(10)
]
store = _mock_pg_store_with_events(events)
# Simulate hook failure: upsert raises first time, then nightly runs.
# (In production the hook + nightly use the same store; here we just
# verify the nightly path produces correct aggregates independently.)
sched = NightlyScheduler()
await sched.reconcile_now(store)
non_suppressed = [c for c in store.upsert_cohort_aggregate.call_args_list if c.args[6] is False]
assert non_suppressed, "nightly should produce non-suppressed cells for 10 learners"
# ── R-DASH-04: nightly failure logs + retries ──────────────────────────────
@pytest.mark.asyncio
async def test_r_dash_04_nightly_failure_does_not_crash_scheduler():
"""R-DASH-04: a reconciliation failure logs + the scheduler continues.
The scheduler loop (_run_loop) catches exceptions from _reconcile and
retries the next night. We simulate this by invoking the loop with a
broken store and confirming the loop catches + continues.
"""
store = MagicMock()
store.upsert_cohort_aggregate = AsyncMock(side_effect=RuntimeError("db down"))
store.pool = MagicMock()
cm = MagicMock()
cm.__aenter__ = AsyncMock(side_effect=RuntimeError("pool down"))
cm.__aexit__ = AsyncMock(return_value=None)
store.pool.acquire = MagicMock(return_value=cm)
sched = NightlyScheduler()
import server.cohort.nightly as nightly_mod
orig = nightly_mod.seconds_until_next_03_ct
calls = []
def _fake_secs():
calls.append(1)
return 0.01
nightly_mod.seconds_until_next_03_ct = _fake_secs
try:
task = await sched.start(store)
await _sleep(0.1)
await sched.stop()
# The loop ran at least once despite the failure (R-DASH-04).
assert len(calls) >= 1
finally:
nightly_mod.seconds_until_next_03_ct = orig
@pytest.mark.asyncio
async def test_scheduler_start_stop_lifecycle():
store = _mock_pg_store_with_events([])
sched = NightlyScheduler()
# Patch seconds_until to be tiny so the loop is testable.
import server.cohort.nightly as nightly_mod
orig = nightly_mod.seconds_until_next_03_ct
nightly_mod.seconds_until_next_03_ct = lambda: 0.01
try:
task = await sched.start(store)
await _sleep(0.05)
await sched.stop()
assert task.cancelled() or task.done()
finally:
nightly_mod.seconds_until_next_03_ct = orig
async def _sleep(t: float) -> None:
import asyncio
await asyncio.sleep(t)