feat(research): download exact saved revisions (RP26)

This commit is contained in:
Boki 2026-10-27 23:11:22 -04:00
parent 6ce3b68b3d
commit 5434950263
7 changed files with 320 additions and 3 deletions

View file

@ -3,7 +3,7 @@ import { Line, LineChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'rec
import type { CanonicalRunResult } from '@stock-bot/stock-config/canonical-research';
import type { CanonicalMetrics } from '@stock-bot/types';
import { actionClass } from '@/features/strategies/components/StrategyParameterForm';
import { downloadJsonText } from '../services/downloadJsonText';
import { downloadJsonText } from '@/utils/downloadJsonText';
import { DatasetDownload } from './DatasetDownload';
const tableClass = 'w-full text-sm text-left border-collapse';

View file

@ -1,7 +1,7 @@
import { useEffect, useRef, useState } from 'react';
import { actionClass } from '@/features/strategies/components/StrategyParameterForm';
import { downloadJsonText } from '@/utils/downloadJsonText';
import { backtestApiV2 } from '../services/backtestApiV2';
import { downloadJsonText } from '../services/downloadJsonText';
/** Changing the dataset discards pending transport state before another download is possible. */
export function DatasetDownload({ hash }: { hash: string }) {

View file

@ -9,6 +9,7 @@ import type {
ResearchHypothesisRevision,
} from '@stock-bot/types';
import { Card } from '@/components/ui/Card';
import { downloadJsonText } from '@/utils/downloadJsonText';
import { actionClass, fieldClass } from '../../strategies/components/StrategyParameterForm';
import { confirmDiscard, useUnsavedWarning } from '../../strategies/components/useUnsavedWarning';
import { researchApi, ResearchApiError } from '../services/researchApi';
@ -129,6 +130,7 @@ export function ResearchEntry({
const [saving, setSaving] = useState(false);
const [error, setError] = useState('');
const [blocked, setBlocked] = useState(false);
const [downloadError, setDownloadError] = useState('');
useUnsavedWarning(editing);
useEffect(() => {
onDirty(editing);
@ -145,6 +147,7 @@ export function ResearchEntry({
const controller = new AbortController();
setLoading(true);
setLoadError('');
setDownloadError('');
setEntry(null);
const load = async (): Promise<Entry> => {
if (kind === 'hypothesis') {
@ -205,6 +208,32 @@ export function ResearchEntry({
const revision = revisionId
? entry?.revisions.find(item => item.id === revisionId)
: entry?.entity.latestRevision;
const download = () => {
if (loading || saving || !entry || !revision) {
return;
}
setDownloadError('');
try {
const record = {
kind: 'research-revision-export/v1',
projectId,
entry:
entry.kind === 'hypothesis'
? { id: entry.entity.id, kind: 'hypothesis' }
: {
id: entry.entity.id,
kind: entry.entity.kind,
hypothesisRevisionId: entry.entity.hypothesisRevisionId,
},
revision,
};
downloadJsonText(JSON.stringify(record), `research-${entry.entity.id}-${revision.id}.json`);
} catch (cause) {
setDownloadError(
cause instanceof Error ? cause.message : 'Could not download saved revision'
);
}
};
const readonly =
archived || Boolean(writeBlocked) || entry?.entity.archived || Boolean(revisionId);
const beginEdit = () => {
@ -338,6 +367,21 @@ export function ResearchEntry({
<p className="text-xs text-text-secondary">
Revision {revision.ordinal} · {revision.createdAt} · {revision.id}
</p>
<div className="space-y-2">
<button type="button" className={actionClass} onClick={download} disabled={saving}>
{downloadError ? 'Retry saved revision download' : 'Download saved revision'}
</button>
{editing && (
<p className="text-sm text-text-secondary">
Downloads the saved revision displayed above. Unsaved edits are excluded.
</p>
)}
{downloadError && (
<p role="alert" className="text-error text-sm">
{downloadError}
</p>
)}
</div>
{revisionId && <p role="status">Immutable history: this revision is read-only.</p>}
{writeBlocked && (
<p role="status">