import { useEffect, useRef, useState } from 'react'; import { Link, useSearchParams } from 'react-router-dom'; import type { ExperimentComparisonDimension, ExperimentComparisonInput, } from '@stock-bot/stock-config/experiment-comparison'; import { Button } from '@/components/ui/Button'; import { ExperimentCounts } from './components/ExperimentCounts'; import { ExperimentInputs } from './components/ExperimentInputs'; import { metricText } from './components/trialMetricText'; import { experimentComparisonApi, parseComparisonQuery, type ExperimentComparisonView, } from './services/experimentComparisonApi'; const dimensionLabels: Record = { dataset: 'Dataset and retained provenance', window: 'Warmup and evaluation window', capital: 'Initial capital and strategy budget', costs: 'Fees, spread or slippage', execution: 'Execution and liquidity settings', risk: 'Portfolio risk limits and ledger settings', clock: 'Account risk-session clock', analytics: 'Metric definitions and analytics settings', seed: 'Random seed', build: 'Engine, worker or runtime build', models: 'Model artifacts', }; export function ExperimentComparisonPage() { const [params, setParams] = useSearchParams(); let input: ExperimentComparisonInput; try { input = parseComparisonQuery(params); } catch (cause) { return (

Compare experiments

{cause instanceof Error ? cause.message : 'Invalid comparison URL'}

Select experiments
); } return ( setParams({ ...input, referenceTrialKey: key })} /> ); } function Comparison({ input, onReference, }: { input: ExperimentComparisonInput; onReference: (key: string) => void; }) { const [value, setValue] = useState(null), [loading, setLoading] = useState(true), [error, setError] = useState(''), [reload, setReload] = useState(0); const reading = useRef(true); useEffect(() => { let current = true; const controller = new AbortController(); reading.current = true; setLoading(true); setError(''); void experimentComparisonApi .get(input, controller.signal) .then(result => { if (current) { setValue(result); } }) .catch(cause => { if (current) { setError(cause instanceof Error ? cause.message : 'Could not load comparison'); } }) .finally(() => { if (current) { reading.current = false; setLoading(false); } }); return () => { current = false; controller.abort(); }; }, [input.leftId, input.rightId, input.referenceTrialKey, reload]); return (
All experiments

Compare experiments

Read-only comparison of recorded settings and saved summaries. Matching settings do not establish economic equivalence, full financial reconciliation, independence, statistical significance or out-of-sample validity. No ranking, deltas or aggregate performance are calculated.

{loading &&

Loading comparison...

} {error && (

{error} {value && ' Showing the last loaded comparison for this same selection.'}

)} {value && ( <>

Reference comes from {value.experiments[0].name}. Without an explicit URL reference, its frozen first candidate is used; completion status and performance never choose the reference.

{!value.reference.summaryAvailable && (

Reference saved summary unavailable:{' '} {value.reference.diagnostic ?? 'No comparable saved summary exists for this trial.'}{' '} Settings comparisons and every outcome remain visible.

)}
{value.limitations.map((text, index) => (

{text}

))}
{value.experiments.map((experiment, side) => (

{side === 0 ? 'Left' : 'Right'}: {experiment.name}

Open {side === 0 ? 'left' : 'right'} experiment

Strategy {experiment.preview.request.strategy.selection.strategyId} · Version{' '} {experiment.strategyVersionId} · Dataset {experiment.datasetHash}

{side === 0 ? 'Left' : 'Right'} frozen inputs
{[ 'Trial / parameters', 'Status', 'Effective costs (fractions)', 'Input settings', 'Saved summary', 'Total return (fraction)', 'Max drawdown (fraction)', 'Sharpe ratio', 'Diagnostics / evidence', ].map(label => ( ))} {experiment.trials.map(trial => { const row = value.rows.find( item => item.experimentId === experiment.id && item.trialKey === trial.trialKey ); if (!row) { throw new Error('Verified comparison row is missing'); } const matching = experiment.preview.request.policy.matching, costs = 'costs' in trial ? trial.costs : { feeRate: matching.risk.feeRate, slippageRate: matching.slippageRate, }; const reference = experiment.id === value.reference.experimentId && trial.trialKey === value.reference.trialKey; return ( ); })}
{label}

{'costs' in trial ? trial.index === 0 ? 'Baseline' : `Scenario ${trial.index}` : `Trial ${trial.index + 1}`} {reference ? ' · Reference' : ''}

                              {JSON.stringify(trial.parameters, null, 2)}
                            

{trial.status}

{trial.status === 'missing' ? 'Expected run missing' : `${trial.progress}% complete`}

{trial.cancelRequested &&

Cancellation requested

}

Fee: {costs.feeRate}

Half-spread: {matching.halfSpreadRate}

Slippage: {costs.slippageRate}

{row.inputCompatibility === 'match' ? 'Matching settings' : 'Settings differ'}

{row.differences.map(difference => (

{dimensionLabels[difference]}

))}

{row.summaryAvailable ? 'Saved summary available' : (trial.diagnostic ?? 'Saved summary unavailable')}

{row.ready ? 'Matching settings and both saved summaries available' : 'Comparison evidence not ready'}

{metricText(trial, 'totalReturn')} {metricText(trial, 'maxDrawdown')} {metricText(trial, 'sharpeRatio')} {trial.error &&

{trial.error}

} {trial.diagnostic &&

{trial.diagnostic}

} {trial.limitations.map((text, index) => (

{text}

))} {trial.runId && ( Open {side === 0 ? 'left' : 'right'} trial {trial.index + 1} run )}

Trial key {trial.trialKey}

))} )}
); }