## Apply: {{ instructions.changeName }}
Schema: {{ instructions.schemaName }}

{% if instructions.state == "blocked" and instructions.missingArtifacts %}
### ⚠️ Blocked

Missing artifacts: {{ instructions.missingArtifacts|join(", ") }}
Use the ito-continue-change skill to create these first.

{% endif %}
{% if context_files and context_files|length > 0 %}
### Context Files
{% for entry in context_files %}
- {{ entry.id }}: {{ entry.path }}
{% endfor %}

{% endif %}
{% if worktree.enabled and worktree.apply_enabled %}
### Worktree Setup

Strategy: `{{ worktree.strategy }}` | Branch: `{{ worktree.default_branch }}` | Dir: `{{ worktree.layout_dir_name }}`

For full layout diagrams and setup commands, run:

```bash
ito agent instruction worktrees
```

**Quick start for this change:**

```bash
CHANGE_NAME='{{ instructions.changeName | replace("'", "'\"'\"'") }}'
CHANGE_DIR="$(ito path worktree --change "$CHANGE_NAME")"

if [ ! -d "$CHANGE_DIR" ]; then
{% if worktree.strategy == "bare_control_siblings" %}
  PROJECT_ROOT="$(ito path project-root)"
  mkdir -p "$(ito path worktrees-root)"
  git -C "$PROJECT_ROOT" worktree add "$CHANGE_DIR" -b "$CHANGE_NAME"
{% else %}
  WORKTREE_ROOT="$(ito path worktree-root)"
  mkdir -p "$(ito path worktrees-root)"
  git -C "$WORKTREE_ROOT" worktree add "$CHANGE_DIR" -b "$CHANGE_NAME"
{% endif %}
fi

echo "Working directory: $CHANGE_DIR"
```
{% if worktree.copy_from_main and worktree.copy_from_main|length > 0 %}

Copy local setup files into the change worktree (missing files are skipped):

```bash
{% if worktree.strategy == "bare_control_siblings" %}
SOURCE_ROOT="$(ito path worktree --main)"
{% else %}
SOURCE_ROOT="$(ito path worktree-root)"
{% endif %}
{% for pattern in worktree.copy_from_main %}
for match in "$SOURCE_ROOT"/{{ pattern }}; do
  [ -e "$match" ] && cp "$match" "$CHANGE_DIR/" 2>/dev/null || true
done
{% endfor %}
```
{% endif %}
{% if worktree.setup_commands and worktree.setup_commands|length > 0 %}

Run in the change worktree before starting:

```bash
cd "$CHANGE_DIR"
{% for cmd in worktree.setup_commands %}
{{ cmd }}
{% endfor %}
```
{% endif %}

{% elif worktree.enabled and not worktree.apply_enabled %}
### Worktree Mode

Worktrees are enabled but apply-time setup is disabled. Work in your current directory.

{% endif %}
{% if instructions.tracksFile and instructions.tracksPath %}
### Task Tracking
- file: {{ instructions.tracksFile }}
{% if instructions.tracksFormat %}
- format: {{ instructions.tracksFormat }}
{% endif %}
- path: {{ instructions.tracksPath }}
{% if tracking_errors %}
- errors: {{ tracking_errors }}
{% endif %}
{% if tracking_warnings %}
- warnings: {{ tracking_warnings }}
{% endif %}

{% endif %}
### Testing Policy
- TDD workflow: {{ testing_policy.tdd_workflow }} (RED -> GREEN -> REFACTOR)
- TDD loop: write a failing test (RED), implement the minimum to pass (GREEN), then refactor (REFACTOR)
- Coverage target: {{ testing_policy.coverage_target_percent }}% (guidance; override per project)
- Override keys: defaults.testing.tdd.workflow, defaults.testing.coverage.target_percent
- Override files (low -> high): ito.json, .ito.json, .ito/config.json, $PROJECT_DIR/config.json

{% if instructions.progress.total > 0 or (instructions.tasks and instructions.tasks|length > 0) %}
### Progress
{% if instructions.state == "all_done" %}
{{ instructions.progress.complete }}/{{ instructions.progress.total }} complete ✓
{% else %}
{{ instructions.progress.complete }}/{{ instructions.progress.total }} complete
{% endif %}

{% endif %}
{% if instructions.tasks and instructions.tasks|length > 0 %}
### Tasks
{% for task in instructions.tasks %}
- [{% if task.done %}x{% else %} {% endif %}] {{ task.description }}
{% endfor %}

{% endif %}
### Audit

Use `ito tasks` CLI commands for all task state changes (emits audit events automatically).
Avoid editing `tasks.md` directly, but if you do, run `ito audit reconcile --change {{ instructions.changeName }} --fix` immediately after.
Run `ito audit reconcile --change {{ instructions.changeName }}` before archiving.

### Instruction
{{ instructions.instruction }}
{% if worktree.enabled and worktree.apply_enabled %}

### Integration

{% if worktree.integration_mode == "commit_pr" %}
Mode: **Commit & PR** — commit on the change branch, push, open a PR into `{{ worktree.default_branch }}`.

```bash
CHANGE_NAME='{{ instructions.changeName | replace("'", "'\"'\"'") }}'
CHANGE_DIR="$(ito path worktree --change "$CHANGE_NAME")"
CHANGE_ID="${CHANGE_NAME%%_*}"
cd "$CHANGE_DIR"
git add -A && git commit -m "feat: $CHANGE_NAME"
git push -u origin "$CHANGE_NAME"
gh pr create --title "feat($CHANGE_ID): <short summary>" --body "Implements change $CHANGE_NAME"
```
{% elif worktree.integration_mode == "merge_parent" %}
Mode: **Merge into Parent** — commit on the change branch, then merge into `{{ worktree.default_branch }}`.

```bash
CHANGE_NAME='{{ instructions.changeName | replace("'", "'\"'\"'") }}'
CHANGE_DIR="$(ito path worktree --change "$CHANGE_NAME")"
cd "$CHANGE_DIR"
git add -A && git commit -m "feat: $CHANGE_NAME"
cd "$(ito path worktree --main)"
git merge "$CHANGE_NAME"
```
{% else %}
Mode: `{{ worktree.integration_mode }}` (unrecognized). Set to `commit_pr` or `merge_parent`:
```bash
ito config set worktrees.apply.integration_mode commit_pr
```
{% endif %}

### Cleanup

After the change branch is merged:

```bash
CHANGE_NAME='{{ instructions.changeName | replace("'", "'\"'\"'") }}'
CHANGE_DIR="$(ito path worktree --change "$CHANGE_NAME")"
git worktree remove "$CHANGE_DIR" 2>/dev/null || true
git branch -d "$CHANGE_NAME" 2>/dev/null || true
git worktree prune
```
{% endif %}

{% if user_guidance %}
### User Guidance

{{ user_guidance }}

{% endif %}
