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

@ -229,6 +229,13 @@ export interface SimulateRequest {
floor_gbp?: string | null;
n_paths?: number;
seed?: number;
life_events?: Array<{
year_start: number;
year_end?: number | null;
delta_gbp_per_year?: string;
one_time_amount_gbp?: string | null;
enabled?: boolean;
}>;
}
export interface SimulateResult {

View file

@ -7,7 +7,7 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Link, useNavigate, useParams } from 'react-router-dom';
import { api, type Scenario, type SimulateRequest } from '@/api/client';
import { api, lifeEventsApi, type Scenario, type SimulateRequest } from '@/api/client';
import { ApiError } from '@/api/client';
import { FanChart } from '@/components/FanChart';
import { GoalsSection } from '@/components/GoalsSection';
@ -53,7 +53,9 @@ export function ScenarioDetail() {
del.mutate();
};
const onRunNow = (s: Scenario) =>
const onRunNow = async (s: Scenario) => {
// Pull events fresh so the run reflects whatever the user just edited.
const events = await lifeEventsApi.list(s.id);
sim.mutate({
jurisdiction: s.jurisdiction,
strategy: s.strategy,
@ -65,7 +67,15 @@ export function ScenarioDetail() {
horizon_years: s.horizon_years,
n_paths: 5000,
seed: 42,
life_events: events.map((e) => ({
year_start: e.year_start,
year_end: e.year_end,
delta_gbp_per_year: e.delta_gbp_per_year,
one_time_amount_gbp: e.one_time_amount_gbp,
enabled: e.enabled,
})),
});
};
if (!Number.isFinite(id)) {
return <p className="text-red-700">Invalid scenario id.</p>;
@ -104,9 +114,10 @@ export function ScenarioDetail() {
<div className="flex items-center gap-2">
<button
type="button"
onClick={() => onRunNow(s)}
onClick={() => void onRunNow(s)}
disabled={sim.isPending}
className="rounded-md border border-slate-300 bg-white text-sm px-3 py-1.5 hover:bg-slate-50 disabled:opacity-60"
title="Run a fresh MC including this scenario's life events"
>
{sim.isPending ? 'Running…' : 'Run now'}
</button>