/aide:refactor — Refactor Phase
Agent: This command is orchestrated by the
/aideorchestrator. It has two modes selected by the--specsflag — code-drift refactor (default) and spec-bloat sweep (--specs). Each mode delegates to different agents and follows a different flow.
Refactor existing artifacts to close drift from canonical contracts. The two modes target different artifact classes and use different agents:
| Mode | Target | Drift detected against | Agents |
|---|---|---|---|
| Default | Source code | Coding playbook + progressive disclosure | aide-auditor → aide-implementor → aide-qa |
--specs | .aide files | Brevity Contract | aide-aligner → aide-spec-writer and/or aide-strategist → aide-aligner |
This command requires a path argument. It does NOT perform full-app refactoring in one pass. Scope it to a directory and it will audit every .aide-defined section within that path.
Usage
/aide:refactor <path> # code-drift refactor (default)
/aide:refactor --specs <path> # spec-bloat sweep
Mode 1 — Code-drift refactor (default)
Audit existing code against the coding playbook and refactor to close convention drift. This is a post-QA mode — it runs on code that already works and already passed QA. The goal is conformance, not new functionality.
Flow
- Discover sections — run
aide_discoverwith the given path to find all.aidespecs in the subtree - Audit each section — spawn one
aide-auditoragent per.aidespec found. Each auditor:- Reads the implementation
- Consults the coding playbook via
study-playbook - Compares against progressive disclosure conventions
- Produces
plan.aidewith refactoring steps
- Pause for approval — present all plans to the user. Do not proceed until approved
- Execute refactoring — for each approved
plan.aide, delegate toaide-implementoragents (one per numbered step, same as build phase) - Re-validate — delegate to
aide-qaper section to verify the refactoring didn't break spec conformance
Checklist
- Require a path argument — refuse to run without one
- Run
aide_discoverscoped to the provided path - For each
.aidespec found, spawn oneaide-auditoragent - Collect all
plan.aideoutputs and present to user for review - After approval, execute each plan using
aide-implementoragents (one per numbered step) - After all plans are executed, run
aide-qaper section to verify spec outcomes still hold - Report completion with a summary of drift items found, fixed, and verified
Mode 2 — Spec-bloat sweep (--specs)
Audit .aide files against the Brevity Contract and slim them in batched, cascade-aware passes. This is the canonical fix flow when the aligner reports spec-bloat items — instead of running /aide:spec or /aide:synthesize per bloated spec one at a time, this mode handles them as a single coordinated sweep.
The motivation: cascading prose refactors are prohibitively expensive when handled one spec at a time. A parent edit that invalidates content in N children should land as one orchestrated pass with all N rewrites reviewed together, not N separate sessions each costing a fresh tree walk.
Flow
-
Run the aligner — delegate to
aide-alignerscoped to the provided path. The aligner walks the tree top-down, executing all three passes (cascade, brevity, sibling-redundancy) and writingtodo.aidefiles at every spec where drift is found. This step is mandatory even if a recent alignment already happened — bloat may have shifted since. -
Collect spec-bloat findings. Read every
todo.aidewritten or updated by step 1. For each, partition the items by which field is bloated:- Frontmatter bloat (
description,intent,outcomes.desired,outcomes.undesiredover caps or carrying forbidden content) → routes toaide-spec-writer - Body bloat (
## Context,## Strategy,## Good examples,## Bad examplesover caps) → routes toaide-strategist - Sibling redundancy flagged at children → routes the duplicated content up to the parent (rewrite the parent, slim the children)
- Frontmatter bloat (
-
Spawn rewriter agents in parallel, one per bloated spec. Each rewriter receives:
- The current bloated spec (full content)
- The ancestor chain (so it can prune content the parent already covers)
- Sibling specs (so it can identify and lift sibling-shared content)
- The relevant
todo.aideitems naming the specific bloat - The Brevity Contract caps (load-bearing — the rewriter must measure)
Frontmatter rewriters use
aide-spec-writer; body rewriters useaide-strategist. A single bloated spec may need both rewriters in sequence — frontmatter first, then body. When both are needed, run frontmatter first (its outcomes anchor the body), then body against the freshly-tightened frontmatter. -
Pause for batched diff review. Present every rewrite as a before/after diff, grouped by spec. The user approves, rejects, or edits per spec. Do not land any rewrite without explicit approval — bloat removal is a judgment call about what content was load-bearing vs. ornamental, and the user is the authority on that.
-
Land approved rewrites. Apply approved diffs to the on-disk specs. Skip rejected ones — they stay bloated; the user accepts the bloat as intentional. Mark rejected
todo.aideitems as resolved-by-acceptance with a brief note in the file. -
Re-run the aligner. A second alignment pass confirms which
spec-bloatitems cleared and surfaces any new drift the rewrites introduced (rare but possible — a tightened parent may now leave a child orphaned of context the child still needs). -
Report — total
.aidefiles audited, total bloat items found, items resolved per spec, items rejected as intentional, items remaining (if any), and the final aligner verdict.
Cascade-awareness rule
When a parent spec is in the rewrite set, rewrite the parent first. Children's slim rewrites depend on what the parent now carries — a child that prunes content already covered by the parent must know what the parent will carry after the rewrite, not before. The orchestrator sequences rewriter agents top-down through the tree, not in parallel across depth levels, even though specs at the same depth can run in parallel.
Checklist
- Require a path argument — refuse to run without one
- Run
aide-alignerscoped to the provided path; collect alltodo.aidefiles produced - Partition
spec-bloatitems per spec by field (frontmatter vs. body) and per parent vs. children for sibling-redundancy - Sequence rewrites top-down: parents before children, siblings in parallel within each depth
- For each spec needing frontmatter rewrite, spawn
aide-spec-writerwith the bloated frontmatter and the ancestor chain as delegation context - For each spec needing body rewrite, spawn
aide-strategistwith the bloated body and the ancestor chain as delegation context - Present every rewrite as a before/after diff, grouped by spec
- Pause for explicit user approval per spec — do not batch-approve silently
- Land approved rewrites; record rejections with one-line notes
- Re-run
aide-alignerto confirm clearance and surface any new drift - Report counts by category (frontmatter cleared / body cleared / rejected as intentional / remaining)
What this mode does NOT do
- It does not run on code. Code drift is the default mode's responsibility.
- It does not invent new outcomes or rewrite intent. The intent is what the user approved at spec time; the rewriter compresses what's there, it does not change what the spec says.
- It does not delete
todo.aidefiles. Those carry the audit trail. The aligner's next walk replaces them. - It does not skip the diff review. Bloat-removal is a judgment call; user approval is mandatory per spec.