---
phase: "25"
plan: "01"
type: implementation
autonomous: true
requirements: [LAZY-01, LAZY-02]
---

# Phase 25 Plan 01: Lazy Evaluation + Cache Efficiency

## Objective

Replace eager full-file loads with lazy scans and eliminate unnecessary copy operations in two hot paths.

## Context

- @python/opendeviationbar/storage/parquet.py (TickStorage.write_ticks dedup path)
- @python/opendeviationbar/sidecar/midnight.py (\_sync_flush_rest_bars Stathera diagnostic)

## Tasks

### Task 1: scan_parquet for dedup in write_ticks [type="auto"]

**LAZY-01**: Replace `pl.read_parquet(parquet_path)` in `write_ticks` dedup branch with `pl.scan_parquet(parquet_path).select("agg_trade_id").collect()` for ID-only comparison, then lazy-load full file only when new rows exist.

**Done when**: The dedup check reads only the `agg_trade_id` column (not all 7 columns) for the anti-join, and full file is loaded only when merge is needed.

### Task 2: In-place sort for Stathera diagnostic [type="auto"]

**LAZY-02**: Replace `sorted_bars = sorted(batch_bars, key=...)` with `batch_bars.sort(key=...)` in-place sort in `_sync_flush_rest_bars` Stathera diagnostic section. This avoids creating a full copy of the batch list.

**Done when**: The Stathera diagnostic uses `list.sort()` (in-place) instead of `sorted()` (copy).

## Verification

1. `write_ticks` dedup path uses `scan_parquet` with column projection
2. Stathera diagnostic uses in-place sort
3. No behavioral changes -- same dedup and diagnostic output

## Success Criteria

1. `TickStorage.write_ticks` dedup check uses `scan_parquet` with column projection for ID-only comparison (not full file read)
2. Stathera diagnostic in `_sync_flush_rest_bars` avoids `sorted()` copy -- uses in-place sort
