4.4 KiB
RP07 — Download a validated retained dataset for local research
Parent E12. Priority P1 / High workflow gap. Complexity M. Dependencies RP05 and R05. Owner's "commit and continue" authorizes this next useful research slice. Conditional performance, monitoring and paper/live work remain deferred.
Evidence: existing canonical dataset GET endpoints return descriptors without bars. readCanonicalDataset already loads and validates an eligible retained v2 snapshot, while scripts/import-research-dataset.ts accepts the full {manifest,bars} representation. A user currently needs direct database access to inspect the frozen observations behind a saved run in VS Code, notebooks or local AI tools.
Frozen contract: GET /api/v2/datasets/canonical/:hash/export returns the existing full HistoricalDatasetV2 object, with no wrapper or new format. Reuse readCanonicalDataset once; preserve its supported-single-asset eligibility gate, values, manifest hash and converted parent identity. Add a minimal CanonicalDatasetReadError extends HistoricalDataRequestError for read rejection statuses while preserving current messages and base-class catches: 400 invalid hash, 404 missing, 409 corrupt content or identity mismatch, 422 unsupported. Unexpected failures return sanitized 500. Check cancellation before/after reading; cancelled route work returns499. Route-handled responses are no-store/nosniff; only validated success gets Content-Disposition: attachment; filename="dataset-<hash>.json" and JSON content type. No source queries, writes, slicing, renormalization, financial changes, cache, streaming framework or migration. Exact means identical validated values/hash, not original JSONB formatting or file byte hash.
Authorization: preserve existing read/operator/service and browser-session access. Research credentials still cannot use dataset routes; test the actual ResearchAgentsService resolver instead of a permissive fake. This does not give autonomous agents new data or trading capabilities. Users may supply downloaded files to local tools.
Frontend: a small shared DatasetDownload action in verified CanonicalSetup selection and full CanonicalResults provenance, using that saved result's immutable request.datasetHash. Loading, failure/retry and duplicate-click guards; selection change/unmount fences late bodies and download effects. Use the existing cookie-aware apiFetch path. Require status200, JSON type and response manifest shape/hash plus bar-array count before creating a Blob from the received text; the server remains the full content/hash validator. No frontend financial calculations or Node hashing. Use the known hash for the filename; no cross-origin custom-header dependency, raw link fallback or automatic downloads. Release Blob URLs. Never save settings or start a run. Keep the fills-only view unchanged.
Ownership: backend agent owns canonical-research.service.ts, datasets.routes.ts and focused API/read tests. Frontend agent owns DatasetDownload.tsx, backtestApiV2.ts, the two insertion components and focused download UI tests. No shared implementation files. Root owns PostgreSQL/browser tests, integration, docs and commits. Independent reviewer checks both halves against this contract.
Acceptance/tests: full downloaded JSON validates with identical manifest and every bar; converted parent, delayed availableAt, source ordinals and null VWAP survive. Invalid hashes avoid storage; missing/corrupt/mismatched/unsupported data never attach; internal errors are sanitized; cancellation fences delivery. Existing role access and research-token denial hold. Actual PostgreSQL export followed by the existing CLI import is idempotent, with unchanged unrelated tables. Real browser downloads from a selected dataset and an original run whose editable template now points elsewhere; file identity must match each source. Malformed/HTML/mismatched responses and delayed selection changes cannot create a download. No run/template/source writes from export.
Gates: focused API/UI/read tests, app/authored-test types, strict lint and production build; root full repository check, relevant disposable PostgreSQL tests and Chromium workbench. Document JSON inspection and existing CLI reimport; the file is a retained v2 snapshot, distinct from the source-input preparation uploader. Risk: exporting a mutable template's new dataset instead of original run input, or presenting a transport error as a data file. No broad research framework is needed.