stock-bot/docs/development/backlog/tasks/R01.md

70 lines
5.4 KiB
Markdown

# R01 — Persist immutable strategy versions and parameter schemas
Parent epic: E6. Priority/severity: P1 / High. Relative complexity: M. Recommended specialization: Full-stack research product.
Scheduling source of truth: [manifest entry R01](../manifest.json). No task is authorized until the plan is approved. Dependencies and decision references below describe the design; the manifest controls current readiness.
## Evidence and root cause
- **WEB-01 — Verified:** The real backtest list/detail/API/SQL flow already exists. Strategy/project/experiment work should extend it; App strategy/analytics views are placeholders. [apps/stock/web-app/src/app/App.tsx:24](../../../../apps/stock/web-app/src/app/App.tsx#L24); [apps/stock/web-api/src/services/backtest-v2.service.ts:211](../../../../apps/stock/web-api/src/services/backtest-v2.service.ts#L211).
- **WEB-04 — Verified:** Existing results snapshot configuration and datasets but do not capture an immutable strategy artifact/engine build/model/seed bundle. Legacy projection can fall back to mutable definition fields. [libs/core/types/src/backtest-run.ts:4](../../../../libs/core/types/src/backtest-run.ts#L4); [apps/stock/web-api/src/services/backtest-result.ts:107](../../../../apps/stock/web-api/src/services/backtest-result.ts#L107).
- **STR-01 — Verified:** Registry is a useful small boundary, but its factories receive mode/execution services; BaseStrategy mixes author decisions, order submission and hidden signal filtering. [apps/stock/orchestrator/src/strategies/StrategyRegistry.ts:7](../../../../apps/stock/orchestrator/src/strategies/StrategyRegistry.ts#L7); [apps/stock/orchestrator/src/strategies/BaseStrategy.ts:315](../../../../apps/stock/orchestrator/src/strategies/BaseStrategy.ts#L315).
## Intended behavior and implementation scope
Add strategies and append-only strategy_versions; seed registered built-ins with source/bundle hash, parameter schema, checkpoint contract and review status. Store executable artifact bytes or an immutable retained artifact reference, dependency lock and source revision. Draft metadata can change; published executable versions cannot.
Measurable benefit: Pins executable strategy identity before adding experiments.
Project/hypothesis linkage is optional and must not require R03 tables or placeholder records. Add nullable foreign-key links only when both entity tables exist, in a coordinated additive migration; standalone versions/experiments work first.
## Exclusions
No general workflow framework, browser IDE, multi-tenant identity platform or second simulator.
## Likely files and ownership
- database/postgres/init/ (new migration)
- apps/stock/web-api/src/routes/ (strategy endpoints)
- apps/stock/web-api/src/services/ (strategy service)
- apps/stock/orchestrator/src/strategies/StrategyRegistry.ts
- libs/core/types/src/strategy-version.ts (new)
Write locks: sql-migrations, strategy-catalog-api. One agent owns this change. Lock names do not override actual overlapping files.
## Dependencies and blockers
Implementation requires [O01](O01.md) integrated.
Architecture decisions: ADR-006. Agents may not independently amend their policies.
Parallel eligibility: only with disjoint actual files and reserved write locks; read FIRST-WAVE.md for the initial exact ownership boundaries.
## Acceptance criteria
- Invalid versioned parameters reject before enqueue; only registered/reviewed supported artifacts run.
- An idempotency key plus content fingerprint makes identical publish retries return the same version; changed content with the same key returns409. An intentional new revision gets a new ID and lineage even if content matches.
- Published source/schema/checkpoint/dependency content is immutable and retained. Review/approval is separately audited metadata, not a mutation of executable content.
- Registration never executes candidate code in the API; arbitrary untrusted execution stays unavailable until A02.
## Mandatory tests
- strategy_version_immutability_postgres, schema_validation, artifact_hash_mismatch, clone_lineage.
- Registry aliases and old definition compatibility.
Apply the relevant [quality gates](../../QUALITY-GATES.md); submit exact commands, outcomes and skipped checks. Named cases above are required regression scenarios, not claims that those tests already exist.
## Regression risks and rollback
Schema/API backward compatibility, historical provenance and UI state regression.
Keep migrations additive and previous results readable. Do not add a switch that restores a correctness defect. Versioned cutover/migration is required only when persisted contracts or active engines change; never silently fall back to old economics.
## Web/API contract and user states
- API: GET/POST /api/v2/strategies; GET/POST /strategies/:id/versions; GET /strategy-versions/:id; POST clone. Parameters use a versioned JSON schema subset.
- Backend/data dependencies: New strategy/version tables; existing strategy registry; source-artifact storage retained with version.
- Loading/error/empty behavior: API typed validation errors,404 and409; UI arrives in R02.
- User-facing completion is demonstrated by the acceptance criteria and interaction tests above; a backend-only implementation is insufficient where a UI is in scope.
## Handoff
Report scope changes (if any), fixture expectations, API/schema changes, measurements, remaining limitations and commit/PR references once authorized. Request independent review; only the integration/planning owner may mark the manifest done.