---
description: "Analyse the project: extract adapted best-practices, hunt real bugs + improvement areas, check upstream drift of derived skills, research the companion multi-agent MCP server against current MCP practice, draft one plan, take approval, develop, then ask whether to sync. Use when asked to review a project for bugs, gaps or improvements, or to check whether derived skills have drifted from upstream."
description-tr: "Projeyi analiz eder: uyarlanmış best-practice'leri çıkarır, gerçek bug + iyileştirme alanlarını avlar, türetilmiş skill'lerin upstream drift'ini kontrol eder, multi-agent MCP sunucusunu güncel MCP pratiklerine göre araştırır, tek plan taslağı çıkarır, onay alır, geliştirir, sonra sync isteyip istemediğini sorar."
argument-hint: "[backlog | <focus area>]  -  optional; backlog runs the backlog step alone"
parameters: [{"name":"focus","kind":"text"}]
gui: hidden
destructive: false
confirm: none
allowed-tools: Read, Write, Edit, Bash, Glob, Grep, Agent, AskUserQuestion, WebFetch, WebSearch
---

# Multi-Agent Refactor

**Access check, before anything else.** This command can write outside this machine, so a login the site closed is stopped here, before any question or write.

```bash
ACCESS_LINE=$(node "$HOME/.claude/scripts/usage-access.mjs"); ACCESS_RC=$?
```

`ACCESS_RC` 3: print `$ACCESS_LINE` as written and stop: no question, no write, attended or unattended. Any other exit continues (the check fails open). Contract: `$HOME/.claude/multi-agent-refs/features/usage-reporting.md` "The access check".

**One command. Best-practices + Bug hunt + Upstream drift + Dev-toolkit -> Score -> Plan -> Approval -> Develop -> Sync.**

Deep-analyses the current project, extracts the global best-practices worth adopting (adapted to our stack), hunts real bugs and improvement areas, checks whether any skills we derived from an upstream source have drifted, researches the companion multi-agent MCP server against current MCP practice, scores everything, drafts a single prioritized plan, asks the user for approval, applies the approved items, and asks about sync at the end.

**Input**: $ARGUMENTS (optional  -  `backlog` runs Step 0e alone; otherwise an area to focus on: "security", "performance", "tests", "bugs", "best-practices", "drift", "multi-agent-mcp", etc.)

Procedures, output tables, the band list and the focus filter live in `$HOME/.claude/multi-agent-refs/refactor/sweep.md`. Read it before Step 0; each step below names the section it uses.

## Flow

```
Step 0:  BEST-PRACTICES  Research the field, extract the best approaches, ADAPT them to our stack -> plan band A
Step 0b: DRIFT           Check upstream-derived skills for updates we have not pulled -> plan band D
Step 0c: DEV-TOOLKIT     Research current MCP practice + audit the companion multi-agent-mcp repo -> plan band E
Step 0d: RUN-ERRORS      Read the local run-error ledger, rank recurring failures -> plan band F
Step 0e: BACKLOG         Decide the open observations recorded mid-task -> plan band G
Step 1:  SCAN            Walk the project structure (files, LOC, dependencies, CI, tests)
Step 2:  ANALYZE         10 categories + an explicit BUG HUNT (real defects, not just scores) -> plan bands B, C
Step 3:  SCORE           Each category /10, overall /100
Step 4:  PLAN            Merge every band into one table
Step 5:  ASK             "Start the work?"  -  wait for user approval
Step 6:  IMPLEMENT       Apply approved items one by one (lint, test, commit)
Step 7:  VERIFY          Confirm every test + lint passes
Step 8:  ASK SYNC        "Run /multi-agent:sync?"  -  wait for user approval
```

## Step 0: BEST-PRACTICES

Do not copy features blindly. Identify the domain, research the best-in-class (through `ai-analyst-toolkit` skills when enabled), record each candidate with its source, benefit, concrete adaptation to OUR repo, effort and impact, and discard what does not fit with the reason stated. Procedure and band-A table: `sweep.md` "Step 0".

## Step 0b: DRIFT  -  upstream-derived skill check

Rules that hold for every entry:
- The upstream mapping is **configuration, never hardcoded**: `global.derivedSkillSources` in `~/.claude/multi-agent-preferences.json`. Missing or empty -> **skip** and report "no derived-skill sources configured". Never invent a source.
- **The plugin cache is a mirror, not the authority.** Never let the cache alone produce an "up to date" verdict; report it as **`unverified (cache only)`**. An unreachable upstream is recorded as "upstream unreachable" and the run continues.
- Each drifted entry becomes a band-D plan item. Do not auto-apply upstream changes  -  they go through Step 5 approval like everything else, and after porting, bump the entry's `derivedFromVersion`.

Full procedure: `$HOME/.claude/multi-agent-refs/refactor/drift.md`. Read it when `global.derivedSkillSources` has at least one entry.

## Step 0c: DEV-TOOLKIT

Audit the companion `multi-agent-mcp` repo against current MCP practice. Resolution is configuration first, never a hardcoded path; skip when nothing resolves or `enabled` is false. Read `$HOME/.claude/multi-agent-refs/refactor/toolkit-research.md` before running this step.

## Step 0d: RUN-ERRORS

Read `$HOME/.claude/logs/multi-agent/errors-ledger.jsonl`. Missing, unreadable or empty means skip band F silently  -  it is additive signal, never a gate. With a focus area, still read it. A tag that stopped recurring after a version bump is resolved: report it, do not re-raise it. Never quote a user's identity as blame. Read `$HOME/.claude/multi-agent-refs/refactor/run-errors.md` when the ledger exists.

## Step 0e: BACKLOG

```bash
node "$HOME/.claude/scripts/observations.mjs" scan --status open --json
```

Exit 3 is `SCAN BROKEN`  -  files on disk, none parsed. Halt the band and say so rather than reporting "0 open". Split every open observation into actionable / sibling-propagation / to-decline, and resolve sibling surfaces mechanically with `skill-siblings.mjs`, never from memory. Output as plan band G. `/multi-agent:refactor backlog` runs this band alone. Buckets, statuses and how to write an observation: `$HOME/.claude/multi-agent-refs/refactor/observations.md`.

## Step 1: SCAN

Walk files, LOC, manifests, dependencies, CI, tests, linters, repo docs and git state (`sweep.md` "Step 1").

## Step 2: ANALYZE  -  10 categories + BUG HUNT

2a scores the 10 categories (`sweep.md` "Step 2a"). 2b runs an explicit defect hunt IN ADDITION to the scoring (`sweep.md` "Step 2b"). **Verify before reporting**: only list a bug you can trace to a real failure path with `file:line` and a concrete failure scenario; drop the plausible-but-unprovable ones, at the bar you would apply to a third party's code. Confirmed defects are band B (P0/P1); non-bug gaps from 2a are band C.

## Step 3: SCORE

Each category /10, total /100 (`sweep.md` "Step 3").

## Step 4: PLAN

One merged table, every row tagged with its band, priorities P0 / P1 / P2 (`sweep.md` "Step 4").

## Step 5: ASK  -  user approval

> "Found X items (P0: N, P1: M, P2: K) across bugs, best-practices, improvements, upstream drift, and multi-agent-mcp practice. Should I start the work?"

Options: "Yes, do all" / "Only P0" / "P0 + P1 only" / "Only bugs (band B)" / "Only the multi-agent-mcp (band E)" / specific items.

**Never start the work without approval.**

## Step 6: IMPLEMENT

Apply approved items one by one, testing each and rolling back on failure (`sweep.md` "Step 6"). Band-E items are applied and committed inside the toolkit repo, never mirrored here, and never published from this step. Commit format: `refactor(scope): {short description}`.

## Step 7: VERIFY

Full lint + test must pass; band-E work also re-verifies the toolkit (`sweep.md` "Step 7").

## Step 8: ASK SYNC

> "Work is complete. Should I run /multi-agent:sync?"

Options:
- "Yes" -> run the `/multi-agent:sync` command (full ecosystem sync  -  its Step 3d ships any band-E work in the toolkit repo)
- "No" -> emit a report and stop
- "Commit + push only" -> push the current repo without sync

## Focus filter

If $ARGUMENTS is set, focus on that band/category only; the mapping is `sweep.md` "Focus filter". Empty runs everything.
