Formalize simulator system with manifest, input-policy, runner, and admin UI (#409)
Introduces three new schema tables (simulator_profiles,
sports_season_simulator_configs, season_participant_simulator_inputs),
a central model layer (app/models/simulator.ts), and a single runner
entry point so every simulator run follows the same prepare → simulate
→ persist → snapshot → recalculate flow.
Key additions:
- manifest.ts: per-simulator display names, default configs, required/
optional inputs, derivable-input declarations, and setup sections
- input-policy.ts: resolves sourceElo from projectedWins,
projectedTablePoints, or sourceOdds; resolves ratings from sourceOdds;
supports block / fallbackElo / averageKnown / worstKnownMinus strategies
- runner.ts: single entry point for admin simulation runs; materialises
derived inputs, normalises result columns, zeroes omitted participants,
snapshots EVs, and recalculates linked fantasy standings
- /admin/simulators: inventory page with per-season readiness and bulk run
- /admin/sports-seasons/:id/simulator: per-season setup page with readiness
summary, input-policy editor, raw JSON config override, and CSV bulk input
- NCAAM/NCAAW simulators now read ratings from season_participant_simulator_inputs,
falling back to the hardcoded name-keyed maps while DB data is being populated
- Clone flow copies simulator config by default; volatile inputs (odds, Elo)
only copied when explicitly requested
Code-review fixes included in this commit:
- source field in compatibility bridge checked with !== null instead of !== undefined
- sourceEloRequirementLabel no longer appends "configured fallback" when the
participant is already excluded from all resolved sources
- Duplicate inline label maps in input-policy.ts replaced with simulatorInputLabel
- save-config preserves existing inputPolicy when the submitted JSON omits it
- Input table truncation label added (Showing 20 of N)
- CSV description notes values must not contain commas
- N+1 comment added to listSportsSeasonSimulatorSummaries
- assertRegistrySchemaDriftFree called in manifest tests
- Runner test suite added covering happy path, already-running guard,
readiness failure, empty results, and error recovery with status reset
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:09:53 -07:00
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
|
|
|
|
import { DEFAULT_SCORING_RULES } from "~/lib/scoring-types";
|
|
|
|
|
import type { ScoringRules } from "~/services/ev-calculator";
|
|
|
|
|
import { batchUpsertParticipantEvSnapshots } from "~/models/ev-snapshot";
|
|
|
|
|
import { batchUpsertParticipantEVs } from "~/models/participant-expected-value";
|
|
|
|
|
import { recalculateStandings } from "~/models/scoring-calculator";
|
|
|
|
|
import { findParticipantsBySportsSeasonId } from "~/models/season-participant";
|
|
|
|
|
import { findSportsSeasonById, updateSportsSeason } from "~/models/sports-season";
|
|
|
|
|
import { calculateEV } from "~/services/ev-calculator";
|
|
|
|
|
import { getSimulator, type SimulatorType } from "~/services/simulations/registry";
|
|
|
|
|
import { normalizeSimulationResultColumns } from "~/services/simulations/simulation-probabilities";
|
|
|
|
|
import {
|
|
|
|
|
getSportsSeasonSimulatorConfig,
|
|
|
|
|
prepareSimulatorInputsForRun,
|
|
|
|
|
validateSimulatorReadiness,
|
|
|
|
|
} from "~/models/simulator";
|
Unify futures odds with the simulation system
Make the futures-vs-Elo relationship an explicit, configurable rule and fix
futures odds failing to override stored Elo.
Root causes addressed:
- resolveSourceElos/resolveRatings used a hardcoded precedence (direct Elo ->
projectedWins -> projectedTablePoints -> odds). The prior fix nulled sourceElo
on futures entry but not projections, which still outranked odds.
- The override relied on a destructive null-on-save hack that also wiped
manually entered Elo.
- Blended simulators buried their Elo/odds weight in module constants.
- Futures odds were not surfaced on the /admin/simulators inventory.
Changes:
- Add a configurable source policy: sourceEloPriority (ordered) and oddsWeight,
parsed/clamped in getSimulatorInputPolicy. resolveSourceElos/resolveRatings now
resolve each participant by the configured priority instead of a fixed order.
Futures-centric simulators (ncaam, ncaaw, world_cup, ncaa_football,
college_hockey) default to a futures-override priority.
- Drop the destructive nulling in batchSaveFuturesOddsForSimulator and
batchSaveSourceOdds; override is now governed by policy, preserving stored Elo.
- Thread an optional SimulationContext (oddsWeight) through the Simulator
interface so blended sims (UCL, World Cup, NCAA FB, MLB) read the blend weight
from the season policy; defaults preserve prior calibration when no context is
passed.
- Add a "Futures vs. Elo" strategy control and Odds Blend Weight input to the
Simulator Setup input-policy card, persisted via save-input-policy.
- Surface futures on /admin/simulators: a source badge and a Futures Odds quick
link; extend listSportsSeasonSimulatorSummaries with odds source info.
- Tests: configurable priority override (Elo/projections/rating), oddsWeight
parsing/clamping, prefersFuturesOdds, manifest defaults, an NCAA Football
context-blend behavioral test, and an updated non-destructive save test.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QhNeB7gN6VKene7sdbBVQT
2026-06-25 22:11:32 +00:00
|
|
|
import { getSimulatorInputPolicy } from "~/services/simulations/input-policy";
|
Formalize simulator system with manifest, input-policy, runner, and admin UI (#409)
Introduces three new schema tables (simulator_profiles,
sports_season_simulator_configs, season_participant_simulator_inputs),
a central model layer (app/models/simulator.ts), and a single runner
entry point so every simulator run follows the same prepare → simulate
→ persist → snapshot → recalculate flow.
Key additions:
- manifest.ts: per-simulator display names, default configs, required/
optional inputs, derivable-input declarations, and setup sections
- input-policy.ts: resolves sourceElo from projectedWins,
projectedTablePoints, or sourceOdds; resolves ratings from sourceOdds;
supports block / fallbackElo / averageKnown / worstKnownMinus strategies
- runner.ts: single entry point for admin simulation runs; materialises
derived inputs, normalises result columns, zeroes omitted participants,
snapshots EVs, and recalculates linked fantasy standings
- /admin/simulators: inventory page with per-season readiness and bulk run
- /admin/sports-seasons/:id/simulator: per-season setup page with readiness
summary, input-policy editor, raw JSON config override, and CSV bulk input
- NCAAM/NCAAW simulators now read ratings from season_participant_simulator_inputs,
falling back to the hardcoded name-keyed maps while DB data is being populated
- Clone flow copies simulator config by default; volatile inputs (odds, Elo)
only copied when explicitly requested
Code-review fixes included in this commit:
- source field in compatibility bridge checked with !== null instead of !== undefined
- sourceEloRequirementLabel no longer appends "configured fallback" when the
participant is already excluded from all resolved sources
- Duplicate inline label maps in input-policy.ts replaced with simulatorInputLabel
- save-config preserves existing inputPolicy when the submitted JSON omits it
- Input table truncation label added (Showing 20 of N)
- CSV description notes values must not contain commas
- N+1 comment added to listSportsSeasonSimulatorSummaries
- assertRegistrySchemaDriftFree called in manifest tests
- Runner test suite added covering happy path, already-running guard,
readiness failure, empty results, and error recovery with status reset
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:09:53 -07:00
|
|
|
|
|
|
|
|
const ZERO_PROBS = {
|
|
|
|
|
probFirst: 0,
|
|
|
|
|
probSecond: 0,
|
|
|
|
|
probThird: 0,
|
|
|
|
|
probFourth: 0,
|
|
|
|
|
probFifth: 0,
|
|
|
|
|
probSixth: 0,
|
|
|
|
|
probSeventh: 0,
|
|
|
|
|
probEighth: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function getPersistenceContext(
|
|
|
|
|
simulatorType: SimulatorType,
|
|
|
|
|
sportsSeason: Awaited<ReturnType<typeof findSportsSeasonById>>
|
|
|
|
|
): Promise<{ scoringRules: ScoringRules; source: "elo_simulation" | "performance_model" }> {
|
|
|
|
|
if (simulatorType !== "brackt") {
|
|
|
|
|
return { scoringRules: DEFAULT_SCORING_RULES, source: "elo_simulation" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sportsSeason?.fantasySeasonId) {
|
|
|
|
|
throw new Error("Brackt simulations must run against a private per-league sports season, not the global Brackt template.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fantasySeason = await database().query.seasons.findFirst({
|
|
|
|
|
where: eq(schema.seasons.id, sportsSeason.fantasySeasonId),
|
|
|
|
|
});
|
|
|
|
|
if (!fantasySeason) {
|
|
|
|
|
throw new Error("Linked fantasy season not found for Brackt simulation.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
source: "performance_model",
|
|
|
|
|
scoringRules: {
|
|
|
|
|
pointsFor1st: fantasySeason.pointsFor1st,
|
|
|
|
|
pointsFor2nd: fantasySeason.pointsFor2nd,
|
|
|
|
|
pointsFor3rd: fantasySeason.pointsFor3rd,
|
|
|
|
|
pointsFor4th: fantasySeason.pointsFor4th,
|
|
|
|
|
pointsFor5th: fantasySeason.pointsFor5th,
|
|
|
|
|
pointsFor6th: fantasySeason.pointsFor6th,
|
|
|
|
|
pointsFor7th: fantasySeason.pointsFor7th,
|
|
|
|
|
pointsFor8th: fantasySeason.pointsFor8th,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface RunSportsSeasonSimulationResult {
|
|
|
|
|
sportsSeasonId: string;
|
|
|
|
|
simulatorType: SimulatorType;
|
|
|
|
|
simulatedParticipants: number;
|
|
|
|
|
zeroedParticipants: number;
|
|
|
|
|
snapshotDate: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function runSportsSeasonSimulation(
|
|
|
|
|
sportsSeasonId: string
|
|
|
|
|
): Promise<RunSportsSeasonSimulationResult> {
|
|
|
|
|
const sportsSeason = await findSportsSeasonById(sportsSeasonId);
|
|
|
|
|
if (!sportsSeason) {
|
|
|
|
|
throw new Error("Sports season not found");
|
|
|
|
|
}
|
2026-05-12 16:52:26 -07:00
|
|
|
if (sportsSeason.status === "completed") {
|
|
|
|
|
throw new Error("Completed sports seasons cannot be simulated.");
|
|
|
|
|
}
|
Formalize simulator system with manifest, input-policy, runner, and admin UI (#409)
Introduces three new schema tables (simulator_profiles,
sports_season_simulator_configs, season_participant_simulator_inputs),
a central model layer (app/models/simulator.ts), and a single runner
entry point so every simulator run follows the same prepare → simulate
→ persist → snapshot → recalculate flow.
Key additions:
- manifest.ts: per-simulator display names, default configs, required/
optional inputs, derivable-input declarations, and setup sections
- input-policy.ts: resolves sourceElo from projectedWins,
projectedTablePoints, or sourceOdds; resolves ratings from sourceOdds;
supports block / fallbackElo / averageKnown / worstKnownMinus strategies
- runner.ts: single entry point for admin simulation runs; materialises
derived inputs, normalises result columns, zeroes omitted participants,
snapshots EVs, and recalculates linked fantasy standings
- /admin/simulators: inventory page with per-season readiness and bulk run
- /admin/sports-seasons/:id/simulator: per-season setup page with readiness
summary, input-policy editor, raw JSON config override, and CSV bulk input
- NCAAM/NCAAW simulators now read ratings from season_participant_simulator_inputs,
falling back to the hardcoded name-keyed maps while DB data is being populated
- Clone flow copies simulator config by default; volatile inputs (odds, Elo)
only copied when explicitly requested
Code-review fixes included in this commit:
- source field in compatibility bridge checked with !== null instead of !== undefined
- sourceEloRequirementLabel no longer appends "configured fallback" when the
participant is already excluded from all resolved sources
- Duplicate inline label maps in input-policy.ts replaced with simulatorInputLabel
- save-config preserves existing inputPolicy when the submitted JSON omits it
- Input table truncation label added (Showing 20 of N)
- CSV description notes values must not contain commas
- N+1 comment added to listSportsSeasonSimulatorSummaries
- assertRegistrySchemaDriftFree called in manifest tests
- Runner test suite added covering happy path, already-running guard,
readiness failure, empty results, and error recovery with status reset
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:09:53 -07:00
|
|
|
|
|
|
|
|
const simulatorConfig = await getSportsSeasonSimulatorConfig(sportsSeasonId);
|
|
|
|
|
if (!simulatorConfig) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
"This sport has no simulator type configured. Set a simulator type on the sport in the admin panel before running a simulation."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sportsSeason.simulationStatus === "running") {
|
|
|
|
|
throw new Error("A simulation is already running for this sports season. Please wait for it to complete.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const readiness = await validateSimulatorReadiness(sportsSeasonId);
|
|
|
|
|
if (!readiness.canRun) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Simulator is not ready: ${readiness.missingInputs.join(", ") || "missing required setup"}.`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await prepareSimulatorInputsForRun(sportsSeasonId);
|
|
|
|
|
|
|
|
|
|
await updateSportsSeason(sportsSeasonId, { simulationStatus: "running" });
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const simulator = getSimulator(simulatorConfig.simulatorType);
|
Unify futures odds with the simulation system
Make the futures-vs-Elo relationship an explicit, configurable rule and fix
futures odds failing to override stored Elo.
Root causes addressed:
- resolveSourceElos/resolveRatings used a hardcoded precedence (direct Elo ->
projectedWins -> projectedTablePoints -> odds). The prior fix nulled sourceElo
on futures entry but not projections, which still outranked odds.
- The override relied on a destructive null-on-save hack that also wiped
manually entered Elo.
- Blended simulators buried their Elo/odds weight in module constants.
- Futures odds were not surfaced on the /admin/simulators inventory.
Changes:
- Add a configurable source policy: sourceEloPriority (ordered) and oddsWeight,
parsed/clamped in getSimulatorInputPolicy. resolveSourceElos/resolveRatings now
resolve each participant by the configured priority instead of a fixed order.
Futures-centric simulators (ncaam, ncaaw, world_cup, ncaa_football,
college_hockey) default to a futures-override priority.
- Drop the destructive nulling in batchSaveFuturesOddsForSimulator and
batchSaveSourceOdds; override is now governed by policy, preserving stored Elo.
- Thread an optional SimulationContext (oddsWeight) through the Simulator
interface so blended sims (UCL, World Cup, NCAA FB, MLB) read the blend weight
from the season policy; defaults preserve prior calibration when no context is
passed.
- Add a "Futures vs. Elo" strategy control and Odds Blend Weight input to the
Simulator Setup input-policy card, persisted via save-input-policy.
- Surface futures on /admin/simulators: a source badge and a Futures Odds quick
link; extend listSportsSeasonSimulatorSummaries with odds source info.
- Tests: configurable priority override (Elo/projections/rating), oddsWeight
parsing/clamping, prefersFuturesOdds, manifest defaults, an NCAA Football
context-blend behavioral test, and an updated non-destructive save test.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QhNeB7gN6VKene7sdbBVQT
2026-06-25 22:11:32 +00:00
|
|
|
const results = await simulator.simulate(sportsSeasonId, {
|
|
|
|
|
config: simulatorConfig.config,
|
|
|
|
|
oddsWeight: getSimulatorInputPolicy(simulatorConfig.config).oddsWeight,
|
|
|
|
|
});
|
Formalize simulator system with manifest, input-policy, runner, and admin UI (#409)
Introduces three new schema tables (simulator_profiles,
sports_season_simulator_configs, season_participant_simulator_inputs),
a central model layer (app/models/simulator.ts), and a single runner
entry point so every simulator run follows the same prepare → simulate
→ persist → snapshot → recalculate flow.
Key additions:
- manifest.ts: per-simulator display names, default configs, required/
optional inputs, derivable-input declarations, and setup sections
- input-policy.ts: resolves sourceElo from projectedWins,
projectedTablePoints, or sourceOdds; resolves ratings from sourceOdds;
supports block / fallbackElo / averageKnown / worstKnownMinus strategies
- runner.ts: single entry point for admin simulation runs; materialises
derived inputs, normalises result columns, zeroes omitted participants,
snapshots EVs, and recalculates linked fantasy standings
- /admin/simulators: inventory page with per-season readiness and bulk run
- /admin/sports-seasons/:id/simulator: per-season setup page with readiness
summary, input-policy editor, raw JSON config override, and CSV bulk input
- NCAAM/NCAAW simulators now read ratings from season_participant_simulator_inputs,
falling back to the hardcoded name-keyed maps while DB data is being populated
- Clone flow copies simulator config by default; volatile inputs (odds, Elo)
only copied when explicitly requested
Code-review fixes included in this commit:
- source field in compatibility bridge checked with !== null instead of !== undefined
- sourceEloRequirementLabel no longer appends "configured fallback" when the
participant is already excluded from all resolved sources
- Duplicate inline label maps in input-policy.ts replaced with simulatorInputLabel
- save-config preserves existing inputPolicy when the submitted JSON omits it
- Input table truncation label added (Showing 20 of N)
- CSV description notes values must not contain commas
- N+1 comment added to listSportsSeasonSimulatorSummaries
- assertRegistrySchemaDriftFree called in manifest tests
- Runner test suite added covering happy path, already-running guard,
readiness failure, empty results, and error recovery with status reset
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 21:09:53 -07:00
|
|
|
|
|
|
|
|
if (results.length === 0) {
|
|
|
|
|
throw new Error("Simulation returned no results. Check that participants have simulator input data.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
normalizeSimulationResultColumns(results);
|
|
|
|
|
|
|
|
|
|
const allSeasonParticipants = await findParticipantsBySportsSeasonId(sportsSeasonId);
|
|
|
|
|
const simulatedIds = new Set(results.map((r) => r.participantId));
|
|
|
|
|
const zeroedParticipants = allSeasonParticipants.filter((p) => !simulatedIds.has(p.id));
|
|
|
|
|
const persistence = await getPersistenceContext(simulatorConfig.simulatorType, sportsSeason);
|
|
|
|
|
|
|
|
|
|
await batchUpsertParticipantEVs([
|
|
|
|
|
...results.map((r) => ({
|
|
|
|
|
participantId: r.participantId,
|
|
|
|
|
sportsSeasonId,
|
|
|
|
|
probabilities: r.probabilities,
|
|
|
|
|
scoringRules: persistence.scoringRules,
|
|
|
|
|
source: persistence.source,
|
|
|
|
|
})),
|
|
|
|
|
...zeroedParticipants.map((p) => ({
|
|
|
|
|
participantId: p.id,
|
|
|
|
|
sportsSeasonId,
|
|
|
|
|
probabilities: ZERO_PROBS,
|
|
|
|
|
scoringRules: persistence.scoringRules,
|
|
|
|
|
source: persistence.source,
|
|
|
|
|
})),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const seasonSports = await database().query.seasonSports.findMany({
|
|
|
|
|
where: eq(schema.seasonSports.sportsSeasonId, sportsSeasonId),
|
|
|
|
|
});
|
|
|
|
|
await Promise.all(seasonSports.map(({ seasonId }) => recalculateStandings(seasonId)));
|
|
|
|
|
|
|
|
|
|
const snapshotDate = new Date().toISOString().slice(0, 10);
|
|
|
|
|
await batchUpsertParticipantEvSnapshots(
|
|
|
|
|
results.map((r) => ({
|
|
|
|
|
participantId: r.participantId,
|
|
|
|
|
sportsSeasonId,
|
|
|
|
|
snapshotDate,
|
|
|
|
|
probFirst: r.probabilities.probFirst,
|
|
|
|
|
probSecond: r.probabilities.probSecond,
|
|
|
|
|
probThird: r.probabilities.probThird,
|
|
|
|
|
probFourth: r.probabilities.probFourth,
|
|
|
|
|
probFifth: r.probabilities.probFifth,
|
|
|
|
|
probSixth: r.probabilities.probSixth,
|
|
|
|
|
probSeventh: r.probabilities.probSeventh,
|
|
|
|
|
probEighth: r.probabilities.probEighth,
|
|
|
|
|
calculatedEV: calculateEV(r.probabilities, persistence.scoringRules),
|
|
|
|
|
source: r.source,
|
|
|
|
|
}))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await updateSportsSeason(sportsSeasonId, { simulationStatus: "idle" });
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
sportsSeasonId,
|
|
|
|
|
simulatorType: simulatorConfig.simulatorType,
|
|
|
|
|
simulatedParticipants: results.length,
|
|
|
|
|
zeroedParticipants: zeroedParticipants.length,
|
|
|
|
|
snapshotDate,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await updateSportsSeason(sportsSeasonId, { simulationStatus: "failed" });
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|