---
phase: 32-historical-gap-cleanup
plan: 01
type: execute
wave: 1
depends_on: []
files_modified:
  - scripts/bulk_aion_repair.sh
autonomous: true
requirements:
  - CLEANUP-01

must_haves:
  truths:
    - "98K day-boundary gaps are eliminated by bulk Aion reprocessing from Parquet"
    - "Stathera audit shows fewer than 100 remaining gaps, all classified as exchange gaps"
    - "No new overlaps introduced by the reprocessing"
  artifacts:
    - path: "scripts/bulk_aion_repair.sh"
      provides: "Orchestration script for bulk Aion repair via pueue on bigblack"
      contains: "repair_direct_parquet.py"
  key_links:
    - from: "scripts/bulk_aion_repair.sh"
      to: "scripts/repair_direct_parquet.py"
      via: "uv run python invocation with --all --execute"
      pattern: "repair_direct_parquet.py.*--all.*--execute"
---

<objective>
Bulk reprocess 98K historical day-boundary gaps as continuous Aion streams by running
repair_direct_parquet.py for all 16 crypto symbols on bigblack via pueue. Verify with
Stathera audit that gaps drop to <100 (exchange gaps only).

Purpose: Eliminate legacy day-mode artifacts from the 42M+ bar dataset so Stathera
reports a clean pipeline with only genuine exchange gaps remaining.

Output: scripts/bulk_aion_repair.sh (runnable on bigblack via pueue)
</objective>

<execution_context>
@$HOME/.claude/get-shit-done/workflows/execute-plan.md
@$HOME/.claude/get-shit-done/templates/summary.md
</execution_context>

<context>
@.planning/PROJECT.md
@.planning/ROADMAP.md
@.planning/STATE.md
@scripts/repair_direct_parquet.py
@scripts/detect_gaps.py
</context>

<tasks>

<task type="auto">
  <name>Task 1: Create bulk_aion_repair.sh orchestration script</name>
  <files>scripts/bulk_aion_repair.sh</files>
  <read_first>
    - scripts/repair_direct_parquet.py (understand --all --execute --workers flags)
    - scripts/detect_gaps.py (understand Stathera audit invocation)
    - scripts/CLAUDE.md (bigblack capacity: 24 workers optimal for repair)
  </read_first>
  <action>
Create `scripts/bulk_aion_repair.sh` -- a shell script designed to run on bigblack via pueue.

The script must:

1. **Header and safety**: `#!/usr/bin/env bash`, `set -euo pipefail`, timestamp logging, trap for cleanup on failure.

2. **Pre-flight check**: Run `repair_direct_parquet.py --all` (dry run, no --execute) to log gap counts per symbol before repair. Capture output to a timestamped log file under `/tmp/bulk_aion_repair_YYYYMMDD_HHMMSS.log`.

3. **Execute repair**: Run `uv run --python 3.13 python scripts/repair_direct_parquet.py --all --execute --workers 24`. This processes all 16 crypto symbols sequentially (the script's `--all` mode handles sequential processing with inter-symbol cleanup, thread leak detection, and GC between symbols). 24 workers is the optimal setting for bigblack per scripts/CLAUDE.md capacity envelope.

4. **Post-repair Stathera audit**: Run `uv run --python 3.13 python scripts/detect_gaps.py --json` and log the total gap count. The script should parse the JSON output and print a clear PASS/FAIL summary: PASS if total gaps < 100, FAIL otherwise.

5. **Logging**: All output (stdout + stderr) tee'd to the log file. Print wall-clock duration at the end.

The script should be self-contained and runnable via: `pueue add -- bash scripts/bulk_aion_repair.sh`

Important constraints:

- Use `uv run --python 3.13` for all Python invocations (per project Python version policy)
- Working directory must be the repo root (add a `cd` to the script's directory parent)
- Do NOT add `--limit` -- process all days for all symbols
- The repair script already handles ouroboros_mode via the symbol registry (crypto = aion)
- overlap_strategy="warn" is already set in repair_direct_parquet.py (safe for gap-only repair)
  </action>
  <verify>
  <automated>bash -n scripts/bulk_aion_repair.sh && echo "Syntax OK"</automated>
  </verify>
  <acceptance_criteria>
  - scripts/bulk_aion_repair.sh exists, is executable, passes bash -n syntax check
  - Script runs dry-run (no --execute) first, then --execute, then Stathera audit
  - All Python calls use `uv run --python 3.13`
  - Workers set to 24 (bigblack optimal)
  - Output tee'd to timestamped log file
  - PASS/FAIL summary based on gap count < 100
    </acceptance_criteria>
    <done>scripts/bulk_aion_repair.sh is ready to deploy to bigblack and run via `pueue add -- bash scripts/bulk_aion_repair.sh`</done>
    </task>

</tasks>

<verification>
- `bash -n scripts/bulk_aion_repair.sh` passes (syntax valid)
- Script contains all 3 stages: dry-run, execute, Stathera audit
- Script uses correct Python version (3.13) and worker count (24)
</verification>

<success_criteria>

- scripts/bulk_aion_repair.sh created and syntax-validated
- Script orchestrates: pre-flight dry run, bulk repair (--all --execute --workers 24), post-repair Stathera audit with PASS/FAIL
- Ready to rsync to bigblack and run via pueue
  </success_criteria>

<output>
After completion, create `.planning/phases/32-historical-gap-cleanup/32-01-SUMMARY.md`
</output>
