# Phase 00 — Context Loading + Resume Detection

**Mode:** INLINE (orchestrator executes every step directly).
**Workflow CLI param:** `--workflow=migration`.
**Inputs (must exist on entry):** Context markers set by `/migrate-refs` — `$MIGRATION_ID`, `$FROM_PATTERN`, `$TO_PATTERN`, `$SCOPE`, `$EXCLUSIONS`, `$DRY_RUN`.
**Outputs (must exist on exit):** `tmp/${MIGRATION_ID}/` directory ready for migration artifacts; either a fresh phase-state (new migration) or a resumed `current_phase` value (existing migration) determined and recorded.

---

## Entry Gate

Phase 00 has no predecessor — the `phase-init` call in SKILL.md "Phase State Initialization" IS the entry gate for this workflow. Per DevForgeAI.md ("Phase 01 has no predecessor — phase-init IS its entry gate; skip `phase-check --from=00`"), this skill's analogous rule is **skip `phase-check --from=init --to=00`** here. Proceed directly to Step 0.1.

---

## Reference Loading [MANDATORY]

```
# No required references for this phase. Phase 00 performs resume detection
# via Glob on the checkpoint YAML, verifies the 6 context markers set by
# /migrate-refs, and ensures tmp/${MIGRATION_ID}/ exists. The references/
# parameter-extraction.md file is mentioned in prose as a forward pointer
# for the marker-extraction algorithm but is not loaded by an explicit Read
# in this phase; downstream phases that act on the markers load it as needed.
```

---

## Step 0.1: Resume Detection (Checkpoint Lookup)

A migration that aborted or paused leaves a checkpoint behind. Detect that first so the orchestrator never re-runs completed phases.

**EXECUTE:**
```
Glob(pattern="tmp/.migration-checkpoint-${SESSION_ID}.yaml")
```
- If Glob returns 0 paths → this is a NEW migration. Set `RESUME = false`.
- If Glob returns 1 path → this is a RESUMED migration. Then:
  ```
  Read(file_path="tmp/.migration-checkpoint-${SESSION_ID}.yaml")
  ```
  Parse the YAML and extract `current_phase` (the last completed phase) and `phase_completion` (the boolean map). The orchestrator routes the rest of the workflow loop to start at `current_phase + 1`. Set `RESUME = true`.

**VERIFY:**
- Exactly one of the two branches above executed.
- If RESUMED: `current_phase` is an integer in `{0, 1, 2, 3, 4, 5}` (Phase 06 = workflow complete; resuming a 06 means the migration is already done — HALT and AskUserQuestion whether to re-commit or close).
- If NEW: no stale artifacts under `tmp/${MIGRATION_ID}/` are silently inherited. (A pre-existing `tmp/${MIGRATION_ID}/intent.json` from a different session is suspect — HALT and AskUserQuestion before proceeding.)

**RECORD:**
```bash
devforgeai-validate phase-record ${SESSION_ID} --workflow=migration --phase=00 --step=resume-check
```

---

## Step 0.2: Confirm Context Markers Exist

The `/migrate-refs` command set six context markers in the conversation. Every downstream phase reads them. If any marker is unset, the migration cannot proceed deterministically.

**EXECUTE:**

Inspect conversation context for each of the 6 markers:
```
MIGRATION_ID = <from context>
FROM_PATTERN = <from context>
TO_PATTERN   = <from context>
SCOPE        = <from context>   # may default to a list when /migrate-refs omits it
EXCLUSIONS   = <from context>   # may be empty list
DRY_RUN      = <from context>   # bool; defaults to false
```

For the formal extraction algorithm (how each marker is parsed out of the command invocation, including the case where `/migrate-refs` is invoked without explicit `--scope` and the default corpus is computed), see `references/parameter-extraction.md`. (Forward-declared; the reference file is authored separately. Read it at this step if it exists.)

Cross-reference [[feedback_dual_path_mechanical_migration]] (project-side memory): when SCOPE/EXCLUSIONS are computed defaults, the orchestrator MUST still confirm them with the user in Phase 01 before committing. Defaults are starting points, not contracts.

**VERIFY:**
- `MIGRATION_ID`, `FROM_PATTERN`, `TO_PATTERN` are all non-empty strings. `MIGRATION_ID` is a kebab-case slug matching `^[a-z][a-z0-9-]*$`.
- `SCOPE` resolves to a non-empty list of directory paths (defaulted or explicit).
- `EXCLUSIONS` is a list (may be empty `[]`).
- `DRY_RUN` is a boolean (default `false` if absent).

IF any required marker is missing or malformed: HALT and AskUserQuestion — "Required migration markers are missing or malformed (list which). Was this skill invoked via `/migrate-refs`? Provide the missing parameters or re-invoke the command."

**RECORD:**
```bash
devforgeai-validate phase-record ${SESSION_ID} --workflow=migration --phase=00 --step=markers-verified
```

---

## Step 0.3: Ensure `tmp/${MIGRATION_ID}/` Directory Exists

All migration artifacts (`intent.json`, `classification.md`, `transform-rules.md`, `clusters.json`, `phase-04-cluster-reports.md`, `verification-report.md`, `session.md`) live under `tmp/${MIGRATION_ID}/`. Per operational-safety.md Rule 2, this MUST be the project-scoped `tmp/` (relative to the project root), not the system `/tmp/`.

**EXECUTE:**
```
Glob(pattern="tmp/${MIGRATION_ID}/.gitkeep")
```
- If Glob returns 1 path → directory already exists from a prior session.
- If Glob returns 0 paths → create the directory by writing a `.gitkeep` sentinel:
  ```
  Write(file_path="tmp/${MIGRATION_ID}/.gitkeep", content="")
  ```
  The Write tool creates intermediate directories as needed.

**VERIFY:**
- After the EXECUTE step, `Glob(pattern="tmp/${MIGRATION_ID}/.gitkeep")` returns exactly 1 path.
- The path is project-scoped (no leading `/tmp/`, no absolute path).

IF the Glob still returns 0 paths after the Write call: HALT — filesystem write failure (likely sandbox restriction on the project root). AskUserQuestion to obtain a fallback location.

**RECORD:**
```bash
devforgeai-validate phase-record ${SESSION_ID} --workflow=migration --phase=00 --step=tmp-ready
```

---

## Exit Gate [MANDATORY]

```bash
devforgeai-validate phase-complete ${SESSION_ID} --workflow=migration --phase=00 --checkpoint-passed
```
IF exit != 0: HALT.

---

## Phase 00 Success Criteria

- [ ] Resume detection ran — either a NEW migration was confirmed (no checkpoint) or a RESUMED migration's `current_phase` was extracted from the checkpoint YAML.
- [ ] All 6 context markers (`MIGRATION_ID`, `FROM_PATTERN`, `TO_PATTERN`, `SCOPE`, `EXCLUSIONS`, `DRY_RUN`) are present and well-formed.
- [ ] `tmp/${MIGRATION_ID}/` exists in the project tree (verified via Glob on `.gitkeep`); the system `/tmp/` was NOT used.
- [ ] No stale cross-session artifacts under `tmp/${MIGRATION_ID}/` were inherited without explicit user confirmation.
- [ ] `phase-record` was called for each of the 3 steps (`resume-check`, `markers-verified`, `tmp-ready`).
- [ ] The Exit Gate `phase-complete` returned exit 0.
