# Workflow: Cleanup

## Model Tier

Haiku - mechanical file operations, no judgment required.

## When to Use

Final step of any orchestrated workflow execution. Runs after synthesis has completed and user-facing output is ready.

## Inputs

- **plan_path**: Path to execution plan (e.g., `.claude/plugin-data/cc-architect/plans/current-plan.md`)

## Outputs

- None (files deleted)
- Optional: Archived plan in `.claude/plugin-data/cc-architect/plans/archive/`

---

## Step 1: Read Execution Plan

Read the plan file at `plan_path` to identify:
- All temporary files listed in step outputs (paths containing `/tasks/`)
- The final output path (preserved)
- Any user-requested artifacts (preserved)

## Step 2: Identify Files to Delete

**Delete (temporary task files):**
- Any file in `.claude/plugin-data/cc-architect/tasks/`
- These are intermediate outputs between workflow steps

**Preserve (permanent artifacts):**
- Files in `.claude/plugin-data/cc-architect/output/` (synthesis results)
- Any paths explicitly listed under `## Artifacts` in the plan
- Files outside `.claude/plugin-data/` (never touch)

## Step 3: Safe Deletion

**Safety rules (enforced):**
1. ONLY delete files under `.claude/plugin-data/cc-architect/tasks/`
2. NEVER delete anything outside `.claude/plugin-data/`
3. NEVER delete the `output/` directory or its contents
4. Verify each path starts with expected prefix before deletion

**Deletion procedure:**
```
For each temp file identified in plan:
  1. Verify path contains `.claude/plugin-data/cc-architect/tasks/`
  2. Verify file exists
  3. Delete file
  4. Log deletion
```

## Step 4: Archive Execution Plan (Optional)

**When to archive:**
- Complex multi-step executions (debugging/audit trail)
- User requests plan preservation
- Execution had errors (for investigation)

**Archive procedure:**
1. Create archive directory: `.claude/plugin-data/cc-architect/plans/archive/`
2. Generate timestamp: `YYYY-MM-DD-HHMMSS`
3. Move plan: `current-plan.md` -> `archive/{timestamp}-{request-summary}.md`

**When to delete instead:**
- Simple single-workflow executions
- Successful completion with no issues
- User explicitly requests cleanup

## Step 5: Directory Cleanup

After file deletion:
1. Check if `tasks/` directory is empty
2. If empty, remove the directory
3. Leave `output/` and `plans/` directories intact

---

## File Classification Reference

| Path Pattern | Classification | Action |
|--------------|----------------|--------|
| `*/tasks/*.md` | Temporary | Delete |
| `*/output/*.md` | Final artifact | Preserve |
| `*/output/final.md` | Primary output | Preserve |
| `*/plans/current-plan.md` | Execution plan | Archive or delete |
| `*/plans/archive/*` | Archived plans | Preserve |
| Anything outside `plugin-data/` | External | Never touch |

## Error Handling

**File not found:**
- Log warning, continue (file may have been manually deleted)

**Permission denied:**
- Log error, skip file, continue with others
- Report at end: "N files could not be deleted"

**Path validation failure:**
- STOP immediately
- Report: "Unsafe path detected: {path}"
- Do not delete anything else

---

## Example Execution

Given plan with these steps:
```markdown
### Step 1: Gather Context
- Output: `.claude/plugin-data/cc-architect/tasks/step-1-context.md`

### Step 2: Draft Structure
- Output: `.claude/plugin-data/cc-architect/tasks/step-2-draft.md`

### Step 3: Synthesize
- Output: `.claude/plugin-data/cc-architect/output/final.md`
```

Cleanup actions:
1. Delete: `.claude/plugin-data/cc-architect/tasks/step-1-context.md`
2. Delete: `.claude/plugin-data/cc-architect/tasks/step-2-draft.md`
3. Preserve: `.claude/plugin-data/cc-architect/output/final.md`
4. Archive or delete: `.claude/plugin-data/cc-architect/plans/current-plan.md`
5. Remove empty `tasks/` directory
