|
| 1 | +# Session / Memory / Summary Replay Consistency Harness — Design |
| 2 | + |
| 3 | +## Objective |
| 4 | + |
| 5 | +Validate that InMemory, SQL, and Redis backends for Session, Memory, and Summary |
| 6 | +services produce equivalent results when driven by identical operation sequences. |
| 7 | +This harness replays standardized input trajectories through multiple backends |
| 8 | +and generates a structured diff report. |
| 9 | + |
| 10 | +## Architecture |
| 11 | + |
| 12 | +``` |
| 13 | +replay_cases/*.json → ReplayEngine → BackendResult |
| 14 | + │ |
| 15 | + ┌─────────┴─────────┐ |
| 16 | + Backend A Backend B |
| 17 | + (InMemory) (SQL / Redis) |
| 18 | + │ │ |
| 19 | + BackendResult BackendResult |
| 20 | + │ │ |
| 21 | + └────────┬───────────┘ |
| 22 | + ▼ |
| 23 | + _normalizer.py |
| 24 | + (strip timestamps, IDs, sort keys) |
| 25 | + │ |
| 26 | + NormalizedResult A + B |
| 27 | + │ |
| 28 | + ▼ |
| 29 | + _comparator.py |
| 30 | + (pairwise diff events, state, memory, summary) |
| 31 | + │ |
| 32 | + ▼ |
| 33 | + DiffReport (JSON) |
| 34 | +``` |
| 35 | + |
| 36 | +Each replay case is a JSON file describing a sequence of operations: |
| 37 | +`create_session`, `append_event`, `update_state`, `inject_summary`, |
| 38 | +`store_memory`, `search_memory`, `read_back`. |
| 39 | + |
| 40 | +The ReplayEngine executes the same sequence against two backends in parallel, |
| 41 | +collecting raw results. These are then normalized and compared. |
| 42 | + |
| 43 | +## Normalization Strategy |
| 44 | + |
| 45 | +Fields that differ across backends by design are normalized before comparison: |
| 46 | + |
| 47 | +| Field | Strategy | |
| 48 | +|------------------------|----------------------------------------------------------| |
| 49 | +| `event.id` | Stripped (auto-generated UUID per backend) | |
| 50 | +| `event.timestamp` | Replaced with sequential index (0, 1, 2, ...) | |
| 51 | +| `session.last_update_time` | Replaced with sentinel `0.0` | |
| 52 | +| `summary_timestamp` | Replaced with sentinel `0.0` | |
| 53 | +| `memory_entry.timestamp` | Stripped | |
| 54 | +| dict key ordering | Re-serialized with `sort_keys=True` | |
| 55 | +| `invocation_id` | Stripped (invocation-scoped, not backend-scoped) | |
| 56 | +| `branch`, `request_id` | Stripped (runtime metadata, not persisted uniformly) | |
| 57 | + |
| 58 | +## Summary Comparison Strategy |
| 59 | + |
| 60 | +The harness distinguishes two levels of summary correctness: |
| 61 | + |
| 62 | +1. **Content semantic consistency** — the `summary_text` is compared after |
| 63 | + whitespace normalization. Minor formatting differences that do not change |
| 64 | + meaning are treated as allowed diffs on a per-backend-pair basis. |
| 65 | + |
| 66 | +2. **Metadata integrity** — `session_id`, `original_event_count`, and |
| 67 | + `compressed_event_count` must match exactly across backends. Any deviation |
| 68 | + in these fields is an unconditional failure. Three classes of summary bug |
| 69 | + are explicitly detected: |
| 70 | + - **Summary loss** — summary present in backend A, absent in backend B. |
| 71 | + - **Summary overwrite** — summary exists but with wrong `session_id`. |
| 72 | + - **Wrong session affiliation** — summary is stored under the wrong session |
| 73 | + key in the underlying cache or storage. |
| 74 | + |
| 75 | +Summary injection into the `SummarizerSessionManager._summarizer_cache` |
| 76 | +bypasses the LLM, since the harness tests storage consistency, not |
| 77 | +summarization model quality. |
| 78 | + |
| 79 | +## Allowed Differences |
| 80 | + |
| 81 | +Not all field-level differences indicate a bug. Known, documented divergences |
| 82 | +are captured in `AllowedDiff` rules: |
| 83 | + |
| 84 | +``` |
| 85 | +allowed_diffs = { |
| 86 | + "inmem_vs_sql": [ |
| 87 | + {"field": "events[*].function_calls[*].args", "reason": "SQL serializes |
| 88 | + JSON args differently from InMemory dict round-trip"}, |
| 89 | + ], |
| 90 | + "inmem_vs_redis": [ |
| 91 | + {"field": "events[*].timestamp_precision", |
| 92 | + "reason": "Redis stores timestamps as float strings with limited precision"}, |
| 93 | + ], |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Each rule includes the backend pair, the field path, and a justification. |
| 98 | +Diffs matching an allowed rule are suppressed from the failure count but |
| 99 | +still appear in the report tagged `allowed: true`. |
| 100 | + |
| 101 | +## Backend Integration |
| 102 | + |
| 103 | +| Backend | Availability | Activation | |
| 104 | +|----------|-----------------|---------------------------------------------| |
| 105 | +| InMemory | Always | Direct instantiation | |
| 106 | +| SQL | Always (sqlite) | `sqlite:///:memory:` — no external deps | |
| 107 | +| Redis | Opt-in | `TRPC_REDIS_URL` env var; skipped otherwise | |
| 108 | + |
| 109 | +The lightweight mode (InMemory + SQL) runs in CI without external services. |
| 110 | +Redis integration mode requires the environment variable to be set; when |
| 111 | +absent, Redis test pairs are skipped with a descriptive message. |
| 112 | + |
| 113 | +## Diff Report |
| 114 | + |
| 115 | +The report (`session_memory_summary_diff_report.json`) contains: |
| 116 | + |
| 117 | +- **run metadata**: run ID, timestamp, backends tested. |
| 118 | +- **per-case results**: status (pass/fail/error), list of `DiffEntry` objects. |
| 119 | +- **summary**: total/pass/fail counts and false-positive rate. |
| 120 | + |
| 121 | +Each `DiffEntry` pinpoints: backend pair, session ID, event index (or summary |
| 122 | +ID), category (events/state/memory/summary), full dotted field path, and the |
| 123 | +two conflicting values. |
| 124 | + |
| 125 | +## Testing the Harness Itself |
| 126 | + |
| 127 | +- `test_replay_normalizer.py` — unit tests for every normalization function. |
| 128 | +- `test_replay_comparator.py` — unit tests for diff logic, including targeted |
| 129 | + tests for summary loss, overwrite, and mis-affiliation detection. |
| 130 | +- `test_replay_report.py` — unit tests for report generation and serialization. |
| 131 | +- `test_replay_consistency.py` — E2E tests running all 10 replay cases through |
| 132 | + backend pairs, asserting normal cases produce ≤5% false positives and anomaly |
| 133 | + cases are 100% detected. |
0 commit comments