# Regenerate All Artifacts Protocol

This file states the procedure AI MUST follow when the user asks to regenerate all artifacts. The procedure performs a full rebuild and does not consider file modification times.

A *generated file* is a registered file with protocol `generated`. This procedure regenerates every generated file in the project.

The procedure has four steps, executed in order. Steps 1 and 2 prepare; Step 3 presents the plan and waits for user approval; Step 4 executes the regeneration.

## Step 1: Collect the Generated Files

- AI MUST identify every generated file.

- For each generated file, AI MUST read its `Dependencies` and `Instructions` cells from the **File Registry**.

  - Every entry in the `Dependencies` cell MUST resolve to a registered file via the **Reference resolution** rule. Unresolved entries MUST trigger the **Undefined References** rule.

  - The `Instructions` cell MUST be non-empty and MUST resolve to a registered `rules`-type file. An empty or unresolved `Instructions` cell on a generated file MUST be reported as a registry violation; the procedure halts.

## Step 2: Order the Generated Files

- AI MUST sort the generated files so that, for every file in the list, all the generated files listed in its `Dependencies` cell are placed before it in the sorted list.

- If no such ordering is possible because two or more generated files depend on each other directly or through a chain, AI MUST identify those files, present them to the user, abort the procedure, and not proceed to subsequent steps.

## Step 3: Present the Plan

- AI MUST present the regeneration plan to the user as a table — one row per generated file, in the order computed in Step 2 — with columns:

  - File: the file's registered name.
  - Instructions: the registered name of the file referenced by the file's `Instructions` cell.
  - Dependencies: the registered names of the files listed in the file's `Dependencies` cell.

- AI MUST wait for explicit user approval before proceeding to Step 4. Without approval, the procedure halts and no files are modified.

## Step 4: Regenerate

- After approval, AI MUST delete every file inside the `artifacts/` subfolder of the project root. Subfolders inside `artifacts/` are removed along with their contents.

- For each generated file, in the order computed in Step 2, AI MUST produce the generated file's content by applying the instructions in the file referenced by the generated file's `Instructions` cell, and record it at the generated file's registered location (its `Subfolder` and `File` cells).

- After every generated file is regenerated, AI MUST report each file that was regenerated, in the order they were regenerated.