Create `scripts/sync-codex.sh` — the dev-time build script that generates the entire `plugins/flow-next/codex/` directory from canonical sources. This is the core of the dual-skills architecture.

## What the script generates

```
plugins/flow-next/codex/
├── skills/            # All 16 skills, patched for Codex
├── agents/            # All 20 agents as .toml configs
└── hooks.json         # Codex-format Ralph hooks
```

## Skill patching (canonical skills/ → codex/skills/)

Copy all skill directories, then apply these patches to all .md files:

### 1. PATH fixes (all skills)
```
${DROID_PLUGIN_ROOT:-${CLAUDE_PLUGIN_ROOT}}/scripts/flowctl
→
${DROID_PLUGIN_ROOT:-${CLAUDE_PLUGIN_ROOT:-$HOME/.codex}}/scripts/flowctl
```
Add fallback line after FLOWCTL assignment:
```
[ -x "$FLOWCTL" ] || FLOWCTL=".flow/bin/flowctl"
```

Also patch:
- `${CLAUDE_PLUGIN_ROOT}/skills/flow-next-ralph-init/templates` → `~/.codex/templates/flow-next-ralph-init`
- `${CLAUDE_PLUGIN_ROOT}/skills/flow-next-worktree-kit/scripts` → `~/.codex/scripts`
- `${CLAUDE_PLUGIN_ROOT}/.claude-plugin/plugin.json` → check `.codex-plugin/plugin.json` first, fallback `.claude-plugin/plugin.json`

### 2. STRUCTURAL: Task tool → agent invocation

**flow-next-work (phases.md + SKILL.md):**
- Replace section 3c "Spawn Worker" with Codex agent role invocation (same replacement logic as current install-codex.sh `patch_work_for_codex_agents`)
- `Task flow-next:worker` → "Use the worker agent"
- `spawn the plan-sync subagent` → "use the plan_sync agent"
- `quality auditor subagent` → "quality_auditor agent"
- All "subagent" → "agent" terminology

**flow-next-plan (steps.md + SKILL.md):**
- `flow-next:context-scout` → `context_scout` agent (all 9 scouts, hyphens→underscores, backtick format)
- `Task flow-next:flow-gap-analyst` → "Use the flow_gap_analyst agent"
- `launch ALL scouts in ONE parallel Task call` → `launch ALL scout agents in parallel`

**flow-next-prime (workflow.md):**
- All 9 `Task flow-next:X-scout` → `Use the X_scout agent`
- `Task flow-next:claude-md-scout` → `Use the agents_md_scout agent` (rename!)
- `Run all 9 scouts in parallel using the Task tool:` → `Run all 9 scouts in parallel (Codex spawns them as multi-agent threads):`

### 3. BEHAVIORAL: RP warnings (3 review skills)
For flow-next-impl-review, flow-next-plan-review, flow-next-epic-review:
- Insert "CRITICAL: RepoPrompt Commands Are SLOW - DO NOT RETRY" warning block after line 1 of workflow.md
- Patch SKILL.md: `setup-review` → `setup-review (5-15 min, DO NOT RETRY)`, same for `chat-send`

### 4. NAMING: claude-md-scout → agents-md-scout
- In any skill that references claude-md-scout, rename to agents-md-scout
- CLAUDE.md → AGENTS.md in agents-md-scout-related content

## Agent conversion (agents/*.md → codex/agents/*.toml)

Extract the conversion logic from install-codex.sh. Apply these rules:

**TOML format:**
```toml
# Auto-generated by sync-codex.sh from <name>.md — do not edit manually
name = "<codex_name>"
description = "<from frontmatter>"
model = "<mapped model>"
model_reasoning_effort = "<effort>"   # omit for mini and inherit
sandbox_mode = "<mode>"
nickname_candidates = ["<name1>", "<name2>", "<name3>"]  # scouts only

developer_instructions = """
<body content, backslashes escaped>
"""
```

**Model mapping:**

| Claude Code model | Codex model | Reasoning | Agents |
|-------------------|-------------|-----------|--------|
| opus | gpt-5.4 | high | quality-auditor, flow-gap-analyst, context-scout |
| sonnet (smart scouts) | gpt-5.4 | high | epic-scout, agents-md-scout, docs-gap-scout |
| sonnet (fast scouts) | gpt-5.4-mini | (omit) | build, env, testing, tooling, observability, security, workflow, memory scouts |
| inherit | (omit) | (omit) | worker, plan-sync |

**Sandbox modes:**
- All scouts + quality-auditor + flow-gap-analyst: `sandbox_mode = "read-only"`
- worker + plan-sync: `sandbox_mode = "workspace-write"`

**Nickname candidates** — add 3 per scout for parallel UX. Thematic names.

**Agent body patching:**
- claude-md-scout → agents-md-scout: CLAUDE.md → AGENTS.md, Claude Code → Codex
- Escape backslashes for TOML triple-quoted strings

## Hooks generation (codex/hooks.json)

Create Codex hooks from canonical hooks/hooks.json:
- Keep: PreToolUse (Bash only), PostToolUse (Bash only), Stop
- Drop: SubagentStop, Edit/Write matchers
- Add: SessionStart hook, statusMessage fields
- Use `python3` explicitly in commands

## Script requirements
- Idempotent: running twice produces identical output
- Clean: remove and recreate codex/ before regenerating (no stale files)
- Validate: verify expected file counts after generation
- Summary: print counts of generated skills, agents, hooks
- Exit 1 on any error
- Should be under 300 lines

## Acceptance criteria
- [ ] `scripts/sync-codex.sh` exists and is executable
- [ ] Running it generates `plugins/flow-next/codex/` with skills/, agents/, hooks.json
- [ ] 16 skill directories in codex/skills/
- [ ] 20 .toml files in codex/agents/
- [ ] All .toml files parse correctly
- [ ] codex/hooks.json is valid JSON
- [ ] Running twice produces identical output (idempotent)
- [ ] Path patches applied (grep for CLAUDE_PLUGIN_ROOT in codex/skills/ should find 0 bare refs without fallback)
- [ ] Task→agent patches applied (grep for "Task flow-next:" in codex/skills/ returns 0)
- [ ] claude-md-scout renamed to agents-md-scout everywhere in codex/

## Done summary
Created scripts/sync-codex.sh that generates plugins/flow-next/codex/ from canonical sources. Produces 16 patched skills, 20 .toml agent configs, and hooks.json. All patches applied: PATH fixes, Task→agent invocation, RP warnings, claude-md-scout→agents-md-scout rename. Idempotent and validated.
## Evidence
- Commits: 4cbc0d19ec7fa0536dea3c7a9b7230c43a5e9553
- Tests: bash scripts/sync-codex.sh (idempotency verified via md5 hash comparison)
- PRs: