engine+api: plumb life events into the simulator
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled

Until now life events were stored but ignored by the engine — pure
metadata. Now they actually move portfolios.

Engine:
- simulator.simulate() takes optional cashflow_adjustments: a (n_years,)
  real-GBP array applied each year *after* savings + return but
  *before* withdrawal. Positive = inflow, negative = outflow.
- New fire_planner/life_events.py with EventInput dataclass +
  events_to_cashflow_array(events, horizon). Handles ranged deltas,
  one-time amounts, disabled events, year clipping past horizon,
  negative year_start (clipped to 0), and summing multiple events.

API:
- /simulate accepts optional life_events list. Server converts each
  to EventInput, builds cashflow_adjustments, passes to simulate().
- Frontend Run-now on scenario detail now fetches the scenario's
  life events and includes them in the request — projections finally
  reflect "retire at 50, kid born at y3, inheritance at y22".

Tests: 11 events helper + 4 end-to-end engine + 1 API integration =
16 new tests. 188 total (was 172). mypy strict + ruff clean.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Viktor Barzin 2026-05-09 22:30:33 +00:00
parent b82770b5c4
commit 2fc92c12f5
9 changed files with 335 additions and 4 deletions

View file

@ -113,6 +113,42 @@ async def test_compare_runs_two_scenarios(client: AsyncClient) -> None:
assert all(len(r["yearly"]) == 20 for r in results)
async def test_simulate_with_life_events_changes_outcome(client: AsyncClient) -> None:
"""Same params with vs without a £500k inheritance at year 5 — the
inheritance run must end with strictly more median NW."""
base_req = {
"jurisdiction": "uk",
"strategy": "trinity",
"leave_uk_year": 0,
"glide_path": "static_60_40",
"spending_gbp": "60000",
"nw_seed_gbp": "1500000",
"horizon_years": 30,
"n_paths": 200,
"seed": 42,
}
base = await client.post("/simulate", json=base_req)
assert base.status_code == 200, base.text
enhanced = await client.post(
"/simulate",
json={
**base_req,
"life_events": [
{
"year_start": 5,
"one_time_amount_gbp": "500000",
}
],
},
)
assert enhanced.status_code == 200, enhanced.text
base_p50 = float(base.json()["p50_ending_gbp"])
enhanced_p50 = float(enhanced.json()["p50_ending_gbp"])
assert enhanced_p50 > base_p50
async def test_compare_rejects_single_scenario(client: AsyncClient) -> None:
resp = await client.post(
"/compare",