Edge-testing an agent sim: two gotchas that only surface under stress
Stress/edge testing a tick-based simulation (many entities, long runs) surfaced two bugs that unit tests missed. (1) O(history) perf leak: a per-tick pass that SCANS all records of a type (here contracts) and never garbage-collects dead/expired/broken ones makes each tick progressively slower — cost grows with history, so long runs hang. Fix: GC terminal-state records, or index/skip them. Diagnose by timing batches of ticks (e.g. 5/15/30) — accelerating per-tick time is the tell. (2) Multi-pass ordering: a teardown action in an early tick pass (dissolve a relationship on contract breach) got silently UNDONE by a later pass in the SAME tick (the agent loop re-formed the relationship because the underlying attraction still scored high). Any state a later pass can recreate needs a cooldown/flag, or the teardown is a no-op. General lesson: emergent sims need soak/edge runs, not just single-step assertions — and watch for passes that fight each other within one tick.