feat(research): run durable chronological validation and retain reports

This commit is contained in:
Boki 2026-10-24 12:03:41 -04:00
parent 67ab30cb7a
commit abd1c5b83d
42 changed files with 5345 additions and 74 deletions

View file

@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { z } from 'zod';
import {
@ -49,6 +49,28 @@ export function ExperimentEditor({
const previewRequest = useRef<AbortController | null>(null);
const pending = useRef(false);
const intent = useRef<{ body: ExperimentSubmission; key: string } | null>(null);
const submissionOwner = useRef<'ordinary' | 'chronological' | null>(null);
const [lockedBy, setLockedBy] = useState<'ordinary' | 'chronological' | null>(null);
const claimSubmission = useCallback((owner: 'ordinary' | 'chronological') => {
if (submissionOwner.current && submissionOwner.current !== owner) {
return false;
}
submissionOwner.current = owner;
setLockedBy(owner);
if (owner === 'chronological') {
generation.current++;
previewRequest.current?.abort();
setPreview(null);
setPreviewLoading(false);
}
return true;
}, []);
const releaseSubmission = useCallback((owner: 'ordinary' | 'chronological') => {
if (submissionOwner.current === owner) {
submissionOwner.current = null;
setLockedBy(null);
}
}, []);
const [hasIntent, setHasIntent] = useState(false);
const [backtestId, setBacktestId] = useState(initialBacktestId);
const [name, setName] = useState('');
@ -117,7 +139,7 @@ export function ExperimentEditor({
}, [initialBacktestId, templateReload, limit]);
const invalidate = (change: () => void) => {
if (intent.current) {
if (submissionOwner.current) {
return;
}
generation.current++;
@ -128,7 +150,7 @@ export function ExperimentEditor({
change();
};
const prepare = async () => {
if (intent.current || previewLoading) {
if (submissionOwner.current || previewLoading) {
return;
}
const current = ++generation.current;
@ -155,7 +177,7 @@ export function ExperimentEditor({
}
};
const submit = async () => {
if (pending.current || (!intent.current && !preview)) {
if (pending.current || (!intent.current && !preview) || !claimSubmission('ordinary')) {
return;
}
if (!intent.current) {
@ -177,6 +199,7 @@ export function ExperimentEditor({
if (alive.current) {
intent.current = null;
setHasIntent(false);
releaseSubmission('ordinary');
navigate(`/experiments/${encodeURIComponent(accepted.id)}`);
}
} catch (cause) {
@ -202,6 +225,7 @@ export function ExperimentEditor({
}
intent.current = null;
setHasIntent(false);
releaseSubmission('ordinary');
setAmbiguous(false);
setPreview(null);
setError('');
@ -240,7 +264,7 @@ export function ExperimentEditor({
<select
className={fieldClass}
value={backtestId}
disabled={hasIntent || templateLoading}
disabled={Boolean(lockedBy) || templateLoading}
onChange={event => invalidate(() => setBacktestId(event.target.value))}
>
<option value="">Select a saved canonical backtest</option>
@ -259,7 +283,7 @@ export function ExperimentEditor({
<input
className={fieldClass}
value={name}
disabled={hasIntent}
disabled={Boolean(lockedBy)}
onChange={event => invalidate(() => setName(event.target.value))}
/>
</label>
@ -268,7 +292,7 @@ export function ExperimentEditor({
<Button
variant="outline"
size="sm"
disabled={templateLoading || hasIntent}
disabled={templateLoading || Boolean(lockedBy)}
onClick={() => setLimit(value => value + 50)}
>
Load more backtests
@ -280,7 +304,7 @@ export function ExperimentEditor({
className={`${fieldClass} font-mono`}
rows={4}
value={grid}
disabled={hasIntent}
disabled={Boolean(lockedBy)}
onChange={event => invalidate(() => setGrid(event.target.value))}
aria-describedby="experiment-grid-help"
/>
@ -299,7 +323,7 @@ export function ExperimentEditor({
<Button
variant="outline"
disabled={
hasIntent ||
Boolean(lockedBy) ||
previewLoading ||
!backtests.some(item => item.id === backtestId) ||
!name.trim()
@ -324,7 +348,10 @@ export function ExperimentEditor({
)}
{(preview || hasIntent) && (
<div className="flex flex-wrap gap-2">
<Button disabled={submitting} onClick={() => void submit()}>
<Button
disabled={submitting || lockedBy === 'chronological'}
onClick={() => void submit()}
>
{submitting
? 'Submitting experiment...'
: hasIntent
@ -342,9 +369,11 @@ export function ExperimentEditor({
backtestId={backtestId}
name={name}
grid={grid}
disabled={hasIntent}
disabled={lockedBy === 'ordinary'}
eligible={!templateLoading && backtests.some(item => item.id === backtestId)}
onDirty={setValidationDirty}
claimSubmission={claimSubmission}
releaseSubmission={releaseSubmission}
/>
</section>
);