---
name: batch
description: Internal skill for batch sub-agents. Generates 5 episodes with fresh context.
allowed-tools: Read, Write, Glob, Bash
argument-hint: "[project] [batch_number] [instructions_json]"
internal: true
---

# /batch - Batch Generation (Internal)

**This is an internal skill called by the orchestrator.** Users should use `/generate-script-orchestrated` instead.

Generate exactly 5 episodes for a single batch with fresh context and orchestrator instructions.

---

## Invocation

```
[Called internally by orchestrator]

/batch [project] [batch_number] [instructions_json]
```

The orchestrator spawns this via the Task tool with batch-specific instructions.

---

## Execution Instructions

### Step 1: Load Context via /load-context

**FIRST ACTION: Invoke `/load-context` using the Skill tool:**

```
Skill tool → skill: "load-context", args: "[project] generate"
```

This ensures all required files are loaded consistently:
- `/skills/format_v12/SKILL.md` (V12 format rules)
- `/CONSTANTS.md` (numeric constraints)
- `/[project]/scripting/bible/characters.md` (voice patterns)
- `/[project]/ORCHESTRATION.md` (project rules)
- `/[project]/treatment.md` (master generation input)
- Last 2 episodes for continuity

**Do NOT skip this step or load files manually.** The skill ensures consistent context loading and writes the `.context_loaded` flag that the enforcement hook checks.

### Step 2: Extract Treatment Batch

```bash
python3 /tools/extract_treatment_batch.py ./[project] [batch_number]
```

This outputs the treatment content for the 5 episodes in this batch.

### Step 3: Load Continuity

If batch > 1, read the last 2 completed episodes:

```
./[project]/scripting/episodes/ep_[N-1].md
./[project]/scripting/episodes/ep_[N].md
```

Where N = (batch - 1) * 5

### Step 4: Parse Orchestrator Instructions

The orchestrator provides:

```json
{
  "thread_instructions": {
    "plant_in_this_batch": ["THREAD_A"],
    "advance_in_this_batch": ["THREAD_B"],
    "payoff_in_this_batch": []
  },
  "emotional_beat_instructions": {
    "beats_due_this_batch": ["THRESHOLD (Ep 15)"]
  },
  "pattern_instructions": {
    "hook_constraint": "Need 1+ dialogue hook",
    "cliffhanger_constraint": null
  },
  "continuity": {...}
}
```

### Step 5: Generate Episodes

For each of the 5 episodes:

#### 5a: Read Treatment Entry

From extracted treatment, get:
- Prose paragraph (what happens)
- THE MOMENT (must land)
- Cliffhanger image (end visual)
- Hook type, cliffhanger type

#### 5b: Generate Episode

```markdown
[[EPISODE X: TITLE]]

# [00:00-00:05] THE HOOK — [type]

[Scene heading]

[Visual hook, ~25 words]

# [00:05-00:15] THE SETUP

[Establish situation, ~50 words]

# [00:15-00:40] THE ESCALATION

[Build tension, ~100 words]
[Behavioral DNA in action blocks]
[Character voice per characters.md]

# [00:40-00:70] THE TURN

[THE MOMENT from treatment lands here]
[~100 words]

# [00:70-00:90] THE CLIFFHANGER — [type]

[End on image from treatment, ~50 words]

---

**Hook:** [type]
**Cliffhanger:** [type]
**THE MOMENT:** [brief description]
[THREAD: NAME] ← if thread activity
[BEAT: NAME] ← if emotional beat
```

#### 5c: Add Thread Markers

Per orchestrator instructions, add markers:
- `[PLANT: THREAD_NAME]` — first introduction
- `[ADVANCE: THREAD_NAME]` — development
- `[PAYOFF: THREAD_NAME]` — resolution

#### 5d: Add Beat Marker

If this episode should hit an emotional beat:
- `[BEAT: BEAT_NAME]`

#### 5e: Respect Pattern Constraints

If orchestrator says "Need 1+ dialogue hook":
- Ensure at least one episode in batch has dialogue hook
- Plan which episode (usually not first or last)

#### 5f: Save Episode

```bash
# Write to file
./[project]/scripting/episodes/ep_XXX.md
```

#### 5g: Validate Immediately

```bash
python3 /tools/episode_metrics.py ./[project]/scripting/episodes/ep_XXX.md --json
```

Check `is_valid` in output:

**If true:** Proceed to next episode

**If false:**
1. Run with `--prompt` for fix instructions:
   ```bash
   python3 /tools/episode_metrics.py ./[project]/scripting/episodes/ep_XXX.md --prompt
   ```
2. Apply targeted fix (don't rewrite from scratch)
3. Re-validate (max 3 attempts)
4. Do NOT proceed until valid

### Step 6: Run Batch Checkpoint

After all 5 episodes generated and validated:

```bash
python3 .claude/hooks/save_checkpoint.py ./[project] [batch_number]
```

**If fails:**
- Read failure output
- Fix identified issues
- Re-run checkpoint
- Repeat until passes

### Step 7: Report Completion

Output completion report:

```
BATCH [N] COMPLETE

Episodes: [start]-[end] generated and validated
Summary: ./[project]/_pipeline/state/batch_[N]_summary.json

Thread activity:
  Planted: [list]
  Advanced: [list]
  Paid off: [list]

Emotional beats hit: [list or "none"]

Pattern distribution:
  Hooks: [X] silent / [Y] dialogue
  Cliffhangers: [X] mid-action / [Y] aftermath

Continuity for next batch:
  Last location: [location]
  Open tension: [cliffhanger setup]
```

---

## Validation Fix Reference

| Issue | Fix |
|-------|-----|
| Word count HIGH by N | Remove ~N words from action blocks |
| Word count LOW by N | Add ~N words of sensory detail |
| Dialogue % HIGH | Convert expository dialogue to action |
| Exchanges HIGH | Consolidate consecutive same-character lines |

**CRITICAL:** Trust the validator. Your word count estimate is wrong.

---

## Thread Marker Examples

**Planting a thread:**
```markdown
The vial gleams blue under the emergency lights. Marcus palms it without looking—old habit from his clinic days.

---
[PLANT: BLUE_MEDICINE]
```

**Advancing a thread:**
```markdown
Kai notices the bulge in Marcus's pocket. "What's that?" Marcus's hand moves to cover it. "Nothing useful. Not yet."

---
[ADVANCE: BLUE_MEDICINE]
```

**Paying off a thread:**
```markdown
Marcus pulls out the vial. "This is why I kept it." He injects the compound directly into the creature's eye. It screams—and the door unlocks.

---
[PAYOFF: BLUE_MEDICINE]
```

---

## Pattern Constraint Examples

**Orchestrator says:** "Need 1+ dialogue hook (3 consecutive silent)"

**Response:** Plan Episode 12 or 13 (middle of batch) to have dialogue hook:

```markdown
# [00:00-00:05] THE HOOK — dialogue

INT. BUNKER - CONTINUOUS

MARCUS
(whispering)
Don't. Move.
```

---

## Termination

After reporting completion, this batch agent terminates. Do not:
- Retain state for next batch
- Assume context carries over
- Continue to next batch

The orchestrator will spawn a fresh agent for the next batch.

---

*This internal skill is executed by batch sub-agents spawned by the orchestrator.*
