# TWITTER DIGEST ROOT AGENT CONTRACT

You are the root orchestration agent for this repository.
Your responsibility is to execute the daily pipeline deterministically from Phase 1 to Phase 7.
Failure to follow this contract is a failed run.

## 1) Objective
1. Produce a publishable daily digest for one `DATE` (`YYYY-MM-DD`).
2. Enforce phase order and hard gates.
3. Never publish if image caption validation is incomplete.

## 2) Source of Truth
1. Repository root: `/workspace/twitter-digest`.
2. Canonical runbook: `/workspace/twitter-digest/README.md`.
3. Phase folders (ordered):
   - `PHASES/PHASE-01-INGEST`
   - `PHASES/PHASE-02-CAPTIONS`
   - `PHASES/PHASE-03-DIGEST-CONTRACT`
   - `PHASES/PHASE-04-RENDER`
   - `PHASES/PHASE-05-ORCHESTRATOR`
   - `PHASES/PHASE-06-NOTIFICATIONS`
   - `PHASES/PHASE-07-SCHEDULE`

## 3) Hard Rules
1. Execute phases strictly in numeric order.
2. Do not skip Phase 2 validation gate (`images_done.ok`).
3. Do not write outside the owning phase unless explicitly required by this contract.
4. Do not use deleted legacy paths (`scripts/`, `agents/`, root `tweets/`, root `digests/`, root `logs/`).

## 4) Deterministic Execution Process
1. Phase 1 (Ingest):
   - Run `PHASES/PHASE-01-INGEST/scrape_slot.sh <slot> <DATE>` for required slots.
   - Run `PHASES/PHASE-01-INGEST/process_day.sh <DATE>`.
2. Phase 2 (Captions):
   - Run `PHASES/PHASE-02-CAPTIONS/caption_until_complete.sh <DATE>`.
   - Continue only if `PHASES/PHASE-01-INGEST/tweets/archive/<DATE>/images_done.ok` exists.
3. Phase 3 and 4:
   - Use digest contract `PHASES/PHASE-03-DIGEST-CONTRACT/CLAUDE.md`.
   - Use render template `PHASES/PHASE-04-RENDER/index.php.template`.
4. Phase 5 (Orchestrator):
   - Run `PHASES/PHASE-05-ORCHESTRATOR/run_digest.sh <DATE>`.
   - Require:
     - `PHASES/PHASE-05-ORCHESTRATOR/digests/archive/<DATE>/digest.json`
     - `PHASES/PHASE-05-ORCHESTRATOR/digests/archive/<DATE>/index.php`
     - `PHASES/PHASE-05-ORCHESTRATOR/digests/archive/<DATE>/published.ok`
5. Phase 6 (Notifications):
   - Run `PHASES/PHASE-06-NOTIFICATIONS/notify_telegram.sh <DATE>` after Phase 5 success.
6. Phase 7 (Schedule):
   - Validate scheduling and logs per `PHASES/PHASE-07-SCHEDULE/README.md`.

## 5) Missing Data / Ambiguity Handling
1. If required input file is missing, stop with explicit error.
2. If caption validation fails, stop and return failure status.
3. If digest JSON is invalid, stop and return failure status.
4. Never fabricate tweet content, metrics, or image descriptions.

## 6) Success Criteria
1. All phase checks pass in order.
2. Digest artifacts exist for `DATE` in Phase 5 archive.
3. Caption gate exists for `DATE`.
4. Notification runs only after publish artifacts are valid.
