refactor(web): isolate research workflows and shared form utilities (CL30)
This commit is contained in:
parent
7ae6ac21c4
commit
a411f9a665
41 changed files with 859 additions and 699 deletions
|
|
@ -1,215 +0,0 @@
|
|||
import {
|
||||
isCanonicalRunResult,
|
||||
type ResearchRunResult,
|
||||
} from '@stock-bot/stock-config/canonical-research';
|
||||
import type { ValidationResultEvidence } from '@stock-bot/stock-config/chronological-validation';
|
||||
import {
|
||||
canonicalResearchMetricPaths,
|
||||
legacyResearchMetricPaths,
|
||||
} from '@stock-bot/stock-config/research';
|
||||
import type { ResearchFindingSource } from '@stock-bot/types';
|
||||
import type { ValidationDetailView } from '@/features/experiments/services/chronologicalValidationDurableApi';
|
||||
import type { WorkbenchExperimentDetailView } from '@/features/experiments/services/experimentApi';
|
||||
import type { WalkForwardDetailView } from '@/features/experiments/services/walkForwardApi';
|
||||
import type { ArtifactInput } from '../services/researchApi';
|
||||
|
||||
type Reference = NonNullable<ArtifactInput['evidence']>[number];
|
||||
export interface FindingMetricChoice {
|
||||
reference: Reference;
|
||||
value: number | null;
|
||||
unavailableReason?: string | null;
|
||||
}
|
||||
export interface FindingRunChoice {
|
||||
key: string;
|
||||
label: string;
|
||||
status: string;
|
||||
diagnostic: string | null;
|
||||
metrics: FindingMetricChoice[];
|
||||
}
|
||||
export interface FindingDraftSource {
|
||||
key: string;
|
||||
label: string;
|
||||
context: ResearchFindingSource | null;
|
||||
legacy: boolean;
|
||||
limitations: string[];
|
||||
runs: FindingRunChoice[];
|
||||
initialSelection: string[];
|
||||
}
|
||||
export const findingMetricKey = (reference: Reference) =>
|
||||
`${reference.runId}:${reference.metricsVersion}:${reference.metricPath}`;
|
||||
const sourceMetrics = ['totalReturn', 'maxDrawdown', 'sharpeRatio'] as const;
|
||||
function choices(
|
||||
runId: string,
|
||||
strategyVersionId: string,
|
||||
metrics: Record<(typeof sourceMetrics)[number], number | null>,
|
||||
unavailable: Record<string, string>
|
||||
): FindingMetricChoice[] {
|
||||
return sourceMetrics.map(key => ({
|
||||
reference: {
|
||||
runId,
|
||||
strategyVersionId,
|
||||
metricsVersion: 'canonical-analytics/v1',
|
||||
metricPath: `analytics.metrics.${key}`,
|
||||
},
|
||||
value: metrics[key],
|
||||
unavailableReason: unavailable[key],
|
||||
}));
|
||||
}
|
||||
export function singleRunFindingSource(result: ResearchRunResult): FindingDraftSource {
|
||||
const canonical = isCanonicalRunResult(result);
|
||||
const references: Reference[] = canonical
|
||||
? canonicalResearchMetricPaths.map(metricPath => ({
|
||||
runId: result.runId,
|
||||
metricsVersion: 'canonical-analytics/v1',
|
||||
strategyVersionId: result.request.strategy.versionId,
|
||||
metricPath,
|
||||
}))
|
||||
: legacyResearchMetricPaths.map(metricPath => ({
|
||||
runId: result.runId,
|
||||
metricsVersion: 'legacy-display/v1',
|
||||
metricPath,
|
||||
}));
|
||||
const metrics =
|
||||
result.status === 'completed'
|
||||
? references.map(reference => {
|
||||
const value = reference.metricPath
|
||||
.split('.')
|
||||
.reduce<unknown>(
|
||||
(item, key) =>
|
||||
item && typeof item === 'object'
|
||||
? (item as Record<string, unknown>)[key]
|
||||
: undefined,
|
||||
result
|
||||
);
|
||||
return { reference, value: typeof value === 'number' ? value : null };
|
||||
})
|
||||
: [];
|
||||
return {
|
||||
key: `${result.backtestId}:${result.runId}`,
|
||||
label: `Run: ${result.runId}`,
|
||||
context: null,
|
||||
legacy: !canonical,
|
||||
limitations: canonical
|
||||
? [...result.financial.limitations, ...result.financial.modelLimitations]
|
||||
: (result.provenance?.limitations.map(item => item.message) ?? []),
|
||||
runs: [{ key: result.runId, label: 'Run', status: result.status, diagnostic: null, metrics }],
|
||||
initialSelection: metrics[0] ? [findingMetricKey(metrics[0].reference)] : [],
|
||||
};
|
||||
}
|
||||
function source(
|
||||
context: ResearchFindingSource,
|
||||
label: string,
|
||||
runs: FindingRunChoice[],
|
||||
limitations: string[]
|
||||
): FindingDraftSource {
|
||||
return {
|
||||
key: JSON.stringify(context),
|
||||
label,
|
||||
context,
|
||||
legacy: false,
|
||||
runs,
|
||||
limitations,
|
||||
initialSelection: [],
|
||||
};
|
||||
}
|
||||
export function experimentFindingSource(
|
||||
value: WorkbenchExperimentDetailView
|
||||
): FindingDraftSource | null {
|
||||
if (value.standalone !== true) {
|
||||
return null;
|
||||
}
|
||||
return source(
|
||||
{ kind: 'experiment/v1', experimentId: value.id, previewHash: value.previewHash },
|
||||
value.name,
|
||||
value.trials.map(trial => ({
|
||||
key: trial.trialKey,
|
||||
label:
|
||||
value.kind === 'cost-sensitivity'
|
||||
? trial.index === 0
|
||||
? 'Baseline'
|
||||
: `Scenario ${trial.index}`
|
||||
: `Trial ${trial.index + 1}`,
|
||||
status:
|
||||
trial.status === 'completed' && !trial.comparable
|
||||
? 'completed · incomparable'
|
||||
: trial.status,
|
||||
diagnostic: trial.error ?? trial.diagnostic,
|
||||
metrics:
|
||||
trial.status === 'completed' &&
|
||||
trial.comparable &&
|
||||
trial.runId &&
|
||||
trial.metrics &&
|
||||
trial.unavailable
|
||||
? choices(trial.runId, value.strategyVersionId, trial.metrics, trial.unavailable)
|
||||
: [],
|
||||
})),
|
||||
[
|
||||
'Selected contributing measurements do not endorse the whole experiment or establish out-of-sample validity.',
|
||||
]
|
||||
);
|
||||
}
|
||||
function reportRun(
|
||||
item: ValidationResultEvidence,
|
||||
strategyVersionId: string,
|
||||
prefix = ''
|
||||
): FindingRunChoice {
|
||||
return {
|
||||
key: `${prefix}${item.phase}:${item.runId}`,
|
||||
label: `${prefix}${item.phase} candidate ${item.sourceCandidateIndex + 1}`,
|
||||
status: 'completed',
|
||||
diagnostic: null,
|
||||
metrics: choices(item.runId, strategyVersionId, item.metrics, item.unavailable),
|
||||
};
|
||||
}
|
||||
export function chronologicalFindingSource(value: ValidationDetailView): FindingDraftSource | null {
|
||||
const report = value.report;
|
||||
if (!report) {
|
||||
return null;
|
||||
}
|
||||
return source(
|
||||
{
|
||||
kind: 'chronological-report/v1',
|
||||
validationId: value.id,
|
||||
previewHash: value.previewHash,
|
||||
reportId: report.id,
|
||||
reportHash: report.reportHash,
|
||||
},
|
||||
value.name,
|
||||
[...report.training, report.validation, report.test].map(item =>
|
||||
reportRun(item, value.preview.source.request.strategy.versionId)
|
||||
),
|
||||
[
|
||||
...report.limitations,
|
||||
'Selected contributing measurements do not endorse the whole report. Training, validation and test remain distinct reset accounts.',
|
||||
]
|
||||
);
|
||||
}
|
||||
export function walkForwardFindingSource(value: WalkForwardDetailView): FindingDraftSource | null {
|
||||
const report = value.report;
|
||||
if (!report) {
|
||||
return null;
|
||||
}
|
||||
return source(
|
||||
{
|
||||
kind: 'walk-forward-report/v1',
|
||||
walkForwardId: value.id,
|
||||
previewHash: value.previewHash,
|
||||
reportId: report.id,
|
||||
reportHash: report.reportHash,
|
||||
},
|
||||
value.name,
|
||||
report.segments.flatMap(segment =>
|
||||
[segment.validation, segment.test].map(item =>
|
||||
reportRun(
|
||||
item,
|
||||
value.preview.source.request.strategy.versionId,
|
||||
`Window ${segment.index + 1} `
|
||||
)
|
||||
)
|
||||
),
|
||||
[
|
||||
...report.limitations,
|
||||
'Selected contributing measurements do not endorse the whole report. Windows are independent reset segments, not continuous portfolio equity.',
|
||||
]
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue