# Phase 4 — Register

**Deliverable:** Versioning, manifest, caches, store entries, agent linking — fully registered.

This phase is deterministic and requires no LLM calls. Execute all 11 steps in order.

Set `$FORGE_ROOT` and resolve `$KB_PATH`:

```sh
KB_PATH=$(node -e "try{console.log(require('./.forge/config.json').paths.engineering)}catch{console.log('engineering')}")
```

---

## Steps (follow in order)

### Step 1 — Render phase header

```sh
node "$FORGE_ROOT/tools/banners.cjs" --phase 4 4 "Register" forge
```

### Step 2 — Tools registration

Read `$FORGE_ROOT/init/generation/generate-tools.md` and follow it.

**Input**: `.forge/config.json`
**Output**: `paths.forgeRoot` written to `.forge/config.json`, schema copies to `.forge/schemas/`

### Step 3 — Enhancement substrate (FR-007)

Materialize the substrate for post-init hooks:

```sh
mkdir -p .forge/enhancements/
cp "$FORGE_ROOT/schemas/project-overlay.schema.json" .forge/schemas/
```

### Step 4 — structure-versions.json

```sh
node "$FORGE_ROOT/tools/manage-versions.cjs" init
```

### Step 5 — generation-manifest.json

```sh
node "$FORGE_ROOT/tools/generation-manifest.cjs" record-all
```

### Step 6 — persona-pack.json

```sh
node "$FORGE_ROOT/tools/build-persona-pack.cjs" \
  --out .forge/cache/persona-pack.json
```

### Step 7 — context-pack.md / context-pack.json

```sh
node "$FORGE_ROOT/tools/build-context-pack.cjs" \
  --arch-dir "$KB_PATH/architecture" \
  --out-md .forge/cache/context-pack.md \
  --out-json .forge/cache/context-pack.json
```

### Step 8 — init-context.json / init-context.md (final rebuild)

```sh
node "$FORGE_ROOT/tools/build-init-context.cjs" \
  --config    .forge/config.json \
  --personas  .forge/personas \
  --templates .forge/templates \
  --kb        "$KB_PATH" \
  --out       .forge/init-context.md \
  --json-out  .forge/init-context.json
```

### Step 9 — Store seed

```sh
node "$FORGE_ROOT/tools/seed-store.cjs"
```

### Step 10 — Update-check cache

```sh
node -e "
const fs = require('fs');
const pluginPkg = JSON.parse(fs.readFileSync('$FORGE_ROOT/.claude-plugin/plugin.json', 'utf8'));
const cache = {
  lastChecked: new Date().toISOString(),
  installedVersion: pluginPkg.version,
  latestVersion: pluginPkg.version,
  upToDate: true
};
fs.mkdirSync('.forge', { recursive: true });
fs.writeFileSync('.forge/update-check-cache.json', JSON.stringify(cache, null, 2) + '\n', 'utf8');
console.log('ノ update-check-cache.json written');
"
```

### Step 11 — Tomoshibi (refresh-kb-links)

> **Orchestrator note:** This step is orchestrator-owned. When running under `wfl:init`, the
> register agent returns `pendingActions: ["refresh-kb-links"]` and the command wrapper
> invokes the Skill. Do NOT execute this step if your orchestrator owns it.

Use the Skill tool:
```
skill: "forge:refresh-kb-links"
```

Fallback if unavailable:
```
△ Run /forge:refresh-kb-links to wire KB links into your agent instruction files.
```

### Step 12 — Git hygiene — gitignore transient store paths

1. Check whether `.gitignore` exists at project root:
   ```sh
   ls .gitignore 2>/dev/null
   ```
   If absent, skip this step.

2. If `.gitignore` exists, check whether `.forge/store/events/` is already
   ignored (any line containing `.forge/store/events/`, `.forge/store/events`,
   `.forge/store/`, or `.forge/`).
   - Found: emit `〇 .forge/store/events/ already gitignored — skipped.`

3. If not already ignored, unconditionally append:
   ```
   # Forge — transient agent event logs (one file per phase, do not commit)
   .forge/store/events/
   ```
   Emit `〇 Appended .forge/store/events/ to .gitignore.`

### Step 13 — Agent instruction file linking

> **Orchestrator note:** If your orchestrator pre-answered the CLAUDE.md creation prompt
> (via `args.createClaudeMd`), use that value directly — skip the interactive prompt.
> Execute the file creation only if `createClaudeMd === true`.

Check whether any agent instruction file exists at the project root:
```sh
ls CLAUDE.md AGENTS.md CLAUDE.local.md .cursorrules 2>/dev/null
```

If NONE exists, prompt:
```
No agent instruction file found. Create a minimal CLAUDE.md with Forge KB links? [Y/n]
```

If Y (default), create `CLAUDE.md` with:
- Project name header
- Links to `{KB_PATH}/MASTER_INDEX.md`, `{KB_PATH}/architecture/INDEX.md`,
  `{KB_PATH}/business-domain/INDEX.md`
- Canonical Forge workflow table
- Footer: `Generated by /forge:init. Run /forge:refresh-kb-links to update.`

If one already exists: skip. Do NOT modify existing files.

Delete `.forge/init-progress.json` — init is complete:
```sh
rm -f .forge/init-progress.json
```
