# CV6 Production-Readiness Validation

Status as of 2026-07-04 (Saturday — market closed, so live-session items are pending Monday).

## How to run

- **Offline suite (Phase A):** `python validate_cv6.py` → writes `validation_report.json`, exit 0 = all pass.
- **Server stability (A10):** start `python -m uvicorn main:app --host 127.0.0.1 --port 8000`, then `python validate_load.py`.

`validate_cv6.py` **supersedes** the old `integration_test.py`, which is stale
(hits endpoint paths that don't exist — `/market/quote`, `/signal/generate`,
`/risk/calculate`, `/strategy/list`, `/indicators/calculate`, `/dashboard/summary`
— and asserts the pre-refactor MM-01 "dashboard reads broker not paper", which is
no longer true). Do not trust `integration_test.py` as-is.

## Phase A — Offline results: 23 / 23 PASS + A10 PASS

| Section | Checks | Result |
|---|---|---|
| A1 Boot integrity | routes, TSM tables, sl_order_id migration | PASS |
| A2 Source of truth | lifecycle capital/PnL, one-symbol-one-position, restart recovery, broker reconcile (restore/out-of-band close/failed-fetch safety), DB idempotency | PASS |
| A3 Order execution | idempotency dedup, position guard, partial fill, ambiguous-timeout reconcile, broker-native SL-M, OCO close-action inputs | PASS |
| A4 Dashboard consistency | REAL→TSM (broker="live"), mode-aware paper fallback, dashboard PnL == /state PnL | PASS |
| A5 Risk engine | kill switch blocks manual path, market-hours + kill-switch gate | PASS |
| A6 Market-time rules | EOD square-off NSE 15:10 / MCX 23:00 | PASS |
| A7 Real indicators | Wilder RSI/ATR/EMA/Supertrend, unavailable-not-fabricated, real-candle path, honest prompt | PASS |
| A8 Resilience logic | Angel expiry→relogin+retry, feed reconnect refresh+respawn, disconnect halts supervisor | PASS |
| A9 Backtest realism | no random P&L fabrication in backtest core | PASS |
| A10 Server stability | 400 concurrent reqs: 0×5xx, 0 db-locks, 0 exceptions | PASS |

**Bug found + fixed during validation:** `app/execution/order_manager.py` had a
function-local `from loguru import logger` that shadowed the module logger,
causing `UnboundLocalError` on the ambiguous-timeout and SL-unprotected log
paths (both in the live order flow). Fixed; suite re-run green.

**Notes / non-blockers:**
- A10 latency avg ~1s / p95 ~3.5s under 24-way concurrency — SQLite serializes
  dashboard/state reads. Fine single-user; enable WAL + connect `timeout` if
  concurrency grows (per original audit).
- A10 memory/thread figure was psutil matching the wrong PID; the server did not
  crash/leak (0 exceptions, clean completion) but the RSS delta is inconclusive.

## Phase B — LIVE market-session checklist (execute Monday, market hours)

Run in PAPER mode first. A REAL order only with your explicit go-ahead, tiny size.

- [ ] **B1 Broker connect** — Angel One + MStock connect on startup; `/broker/status`
      shows connected; scrip master + feed token loaded (check startup logs).
- [ ] **B2 Live feed** — `/ws/status` shows `live_provider_registered: true` and
      symbol sources = `angelone` (not `simulation`); open a `/ws/state` socket and
      confirm real ticks; subscribe a non-watchlist symbol and confirm it goes LIVE.
- [ ] **B3 Order path** — start autonomous in PAPER, let it run a full cycle; confirm
      dashboard/`/state` reflect it and a trade lands in `/history`. Then (opt-in) one
      tiny REAL manual order via `/execution/place-order` with a `stop_loss` → confirm
      fill, TSM position, journal row, dashboard update, and **the SL-M order is
      visible in the Angel One terminal**.
- [ ] **B4 Reconciliation** — manually close that REAL position in the broker terminal;
      within ~10 min the periodic reconcile force-closes it in TSM and journals it
      (`BROKER_RECONCILE_FLAT`).
- [ ] **B5 Resilience** — drop the network briefly; confirm the feed supervisor
      reconnects + re-subscribes (logs: "supervisor... reconnect succeeded"); confirm
      `validate_session` runs each reconcile cycle.
- [ ] **B6 Square-off** — hold a PAPER intraday position past 15:10; confirm auto EOD
      square-off fires.
- [ ] **B7 Restart recovery** — with an open REAL position, restart the server; confirm
      TSM + dashboard restore true state from DB + broker reconcile (position, capital,
      SL id all intact).

## Exit criteria for production sign-off
Phase A green (done) + Phase B executed on a live session with evidence, especially
B3 (broker-side SL visible) and B4 (reconciliation). AMI remains frozen until this
completes successfully.
