[BLOCKING] Execute skill steps in declared order. NEVER skip, reorder, or merge steps without explicit user approval.
[BLOCKING] Before each step or sub-skill call, update task tracking: set in_progress when step starts, set completed when step ends.
[BLOCKING] Every completed/skipped step MUST include brief evidence or explicit skip reason.
[BLOCKING] If Task tools are unavailable, create and maintain an equivalent step-by-step plan tracker with the same status transitions.
Quick Summary
Goal: Lower the cost of the next change — cut coupling, hidden state, duplicated knowledge, unclear intent — by simplifying and refining code for clarity, consistency, and maintainability without altering any observable behavior. — why: every simplification serves future change cost, not aesthetics.
Summary: (read-this-if-nothing-else digest — purpose + every main step)
- Purpose — skeptical-first MUTATOR, not a suggester: grep all usages + trace consumers (graph downstream when graph.db exists) and cite
file:line BEFORE touching anything; apply a simplification ONLY when certain it preserves behavior, never when unsure. — why: an unverified "safe" rewrite silently breaks a downstream consumer.
- Main steps, run in order: (1) Phase 0 Detect artifact type (backend/frontend/test/config) + scope; (2) Identify Targets — recent git changes or named files, HARD-SKIP generated/migration/vendor; (3) Analyze via the 5 Simplification Dimensions; (4) Apply one refactoring type at a time (KISS/DRY/YAGNI, behavior-preserving); (5) Verify related tests after EACH change; (6) Self-Recursive Loop (analyze→simplify→verify) until zero findings or a no-progress/unsafe/owner-decision stop hits — do NOT spawn a fresh-context reviewer for your own findings; (7) Self-Review Gate.
- The 5 Simplification Dimensions (step 3): readability · DRY/abstraction (≥3 occurrences, YAGNI gate) · right-responsibility-lowest-layer (Entity > Domain Service > App Service > Controller) · complexity reduction (flatten nesting, extract >20-line methods) · DB paging+indexes — every technique answers ONE test: does this make the next change cheaper?
- Self-Review Gate (step 7) — this skill owns review of its own output: when it changed any file, self-invoke
/code-review scoped to ONLY those changed files (recursion-safe leaf — NEVER /changes-review); skip + log the reason when nothing changed. — why: the simplifier rewrites code after the main review batch, so its output ships unreviewed without this gate.
- Read FIRST:
docs/project-reference/code-review-rules.md (anti-patterns/checklists) then project-structure-reference.md — before any modification.
MANDATORY IMPORTANT MUST ATTENTION Plan task to READ:
docs/project-reference/code-review-rules.md — anti-patterns, review checklists (READ FIRST)
project-structure-reference.md — project patterns/structure
If not found, search for: project documentation, coding standards, architecture docs.
Workflow:
- Phase 0: Detect — Classify artifact type (backend/frontend/test/config) and scope
- Identify Targets — Recent git changes or specified files (skip generated/vendor)
- Analyze — Apply simplification dimensions (see below)
- Apply — One refactoring type at a time following KISS/DRY/YAGNI
- Verify — Run related tests, confirm no behavior changes
- Self-Recursive Check — Re-run this skill's simplification analysis until no simplification findings remain
- Self-Review Gate (MANDATORY when code changed) — If this skill modified any files, self-invoke
/code-review scoped to ONLY those changed files; skip + log if nothing changed
Key Rules:
- Preserve all existing functionality — no behavior changes
- Follow the project's documented patterns (entity expressions, fluent helpers, store base, BEM)
- Easy to Change is the primary simplification goal for source files; DRY, SOLID, abstraction, and patterns are valid only when they lower future edit sites or cognitive load
- Tests pass after every change
- Apply simplification only when certain it preserves behavior — NEVER apply when unsure
Phase 0: Artifact Detection
MUST ATTENTION classify before simplifying — detection drives focus and optional escalation only:
| Artifact Type |
Detection |
Key Focus |
| Backend |
Backend source files for the current stack (e.g. .cs) |
Domain-model expressions, fluent API, DRY via OOP, SOLID |
| Frontend |
Frontend source files for the current stack (e.g. .ts, .html, .scss) |
BEM, store base, subscription cleanup, component base |
| Tests |
Test source files for the current stack (e.g. *Test.cs, *.spec.ts) |
Assertions, async-assertion helpers (e.g. an await-until-condition poll helper), data isolation |
| Config/Generated |
Migrations, generated/vendor files (e.g. *.generated.*) |
SKIP — NEVER simplify generated/migration code |
Optional escalation by artifact:
| Artifact |
Escalate only when |
| Source code/diffs |
Broad review is requested after simplifier loop is clean |
| Security-sensitive |
Security-specific risk is present |
| Performance-critical |
Performance behavior is part of the change |
| Plans/docs/specs |
Artifact review is explicitly requested |
First Principle — Easy to Change
The success metric of every coding decision is future change cost.
DRY, SRP, abstraction, design patterns, naming, layering, tests — every
technique exists to serve one goal: making the next change cheaper.
When evaluating code, a refactor, a test, or an abstraction, ask:
does this make the next change cheaper or more expensive?
- Reject "best practices" that raise change cost (premature abstraction,
speculative generality, leaky indirection, ceremony without payoff).
- Name the real enemies in findings: coupling, hidden state, duplicated
knowledge, unclear intent, irreversible decisions exposed too early.
- Favor project-owned boundaries around external libraries, for example
component/service input-output contracts, when they localize future library
changes; reject pass-through wrappers that add ceremony without lowering
change cost.
- A simpler design that is easy to change beats a sophisticated design that
isn't.
Apply this lens before invoking any specific rule, pattern, or checklist
below — if a downstream rule would raise change cost, this principle wins.
Simplification Mindset
Skeptical-first: Verify before simplifying. Every change needs proof preserving behavior.
- NEVER assume code redundant — trace call paths and read implementations first
- Before removing/replacing: grep all usages confirming nothing depends on current form
- Before flagging convention violation: grep 3+ existing examples — codebase convention wins
- Every simplification requires
file:line evidence of what was verified
- Apply simplification only when certain it preserves behavior; if unsure → DO NOT apply
Simplification Dimensions
Dimension-based reasoning replaces fixed checklists. Each dimension has a Think: prompt forcing first-principles reasoning.
Dimension 1: Readability
Think: Would a new engineer understand this in 30 seconds? What forces multiple file traces?
- Schema visibility: functions computing data structures need output-shape comment
- Non-obvious pipelines: A→B→C transformations need brief pipeline explanation
- Self-documenting signatures: params explain role; remove unused params
- Magic values: replace unexplained numbers/strings with named constants
- Naming clarity: names reveal intent without reading implementation
Dimension 2: DRY & Abstraction
Think: Pattern appearing ≥3 places? What base class/generic eliminates duplication?
- Same-suffix classes (
*Entity, *Dto, *Service) → shared base
- Repeated logic blocks → extract to helper/extension method
- YAGNI gate: NEVER extract for hypothetical future use — 3+ occurrences required
Dimension 3: Right Responsibility
Think: Logic in lowest layer that can own it? Could moving it down enable reuse?
Entity/Model → Domain Service → Application Service → Controller (logic belongs lowest)
- Business logic in controllers → move down
- Mapping in handlers → move to DTO methods
Dimension 4: Complexity Reduction
Think: What is cognitive load? Can nesting/conditionals flatten?
- Nesting >3 → refactor (early returns, extract methods)
- Methods >20 lines → extract
- Complex conditionals → flatten or Strategy pattern (3+ branching occurrences only)
Dimension 5: Database Performance
MANDATORY IMPORTANT MUST ATTENTION
- Paging: ALL list queries MUST use pagination. NEVER unbounded
GetAll(), ToList(), Find() without Skip/Take or cursor-based paging.
- Indexes: ALL filter fields, foreign keys, sort columns MUST have database indexes. Entity expressions must match index field order. Collections need index management methods.
Project Patterns
Backend
- Extract entity static expressions (search: entity expression pattern)
- Use fluent helpers (search: fluent helper pattern in
docs/project-reference/backend-patterns-reference.md)
- Move mapping to DTO mapping methods (search: DTO mapping pattern)
- Use project validation fluent API (see
docs/project-reference/backend-patterns-reference.md)
- Verify entity expressions have database indexes
- Verify document DB collections have index management methods
Frontend
- Use project store base (search: store base class) for state management
- Apply subscription cleanup (search: subscription cleanup pattern) to all subscriptions
- BEM class naming on ALL template elements
- Use the project's base classes (search: base component class, store component base class)
Graph Intelligence (MANDATORY if graph.db exists)
Before simplifying, trace what depends on target:
python .claude/scripts/code_graph trace <file> --direction downstream --json
Verify simplified code preserves same interface for all traced consumers. Cross-service MESSAGE_BUS consumers are especially fragile — may depend on exact message shape.
Additional queries:
- Verify no callers break:
python .claude/scripts/code_graph query callers_of <function> --json
- Check dependents:
python .claude/scripts/code_graph query importers_of <module> --json
- Batch analysis:
python .claude/scripts/code_graph batch-query file1 file2 --json
Execution
Agent(subagent_type="code-simplifier", prompt="Review and simplify [target files]")
Example:
// Before
function getData() {
const result = fetchData();
if (result !== null && result !== undefined) {
return result;
} else {
return null;
}
}
// After
function getData() {
return fetchData() ?? null;
}
Constraints
- Preserve functionality — no behavior changes
- Tests passing — verify after every change
- Follow patterns — use the project's conventions, never invent
- Doc staleness — cross-ref changed files against feature docs, test specs, READMEs; flag updates needed
- Preserve ALL invariants; never weaken a property/mutation test — a refactor MUST keep every
[HARD] §4 rule / §5 invariant intact and MUST NOT delete, relax, or trivialize any property test or mutation test that guards them. If a simplification changes observable behavior, that is NOT a silent change — it is a Dual-Feedback finding (feed the spec AND the tests, then re-review), report it and stop, never ship it. After simplifying, the package MUST still pass the SAME property/mutation bar it passed before — green tests on a weakened bar are not a pass.
Self-Recursive Verification (MANDATORY after simplifications)
After simplifications applied, verification requires a self-recursive simplification pass over the updated diff. Do NOT spawn fresh-context reviewer to re-review this skill's own findings. Repeat analyze → simplify → verify until this skill finds no further simplification opportunities, or stop on unsafe/no-progress/user-decision blocker.
Self-Review Gate (MANDATORY when this skill changed code)
This skill is a code MUTATOR. It owns the review of its own output. Once the self-recursive simplification loop above is clean, gate the result:
- Did this skill modify any files? Determine the exact set of files this skill changed (its own edits — not the whole working tree).
- No files changed → SKIP this gate and log the skip reason ("code-simplifier made no changes — no self-review needed"). Done.
- Files changed → continue.
- Self-invoke
/code-review scoped to ONLY the changed files. Pass the explicit changed-file set as the review target — not the full diff, not unrelated files.
- Integrate the
/code-review findings. If it surfaces blocking issues caused by the simplification, fix them (behavior-preserving only) and re-run the self-recursive loop + this gate. If issues are out of simplification scope, report them up — do not silently drop.
Recursion safety: /code-review is a LEAF review skill — it does NOT invoke /code-simplifier back, so there is no cycle. Use /code-review here, NEVER /changes-review (the heavyweight workflow that itself contains /code-simplifier and would recurse).
Why this gate exists: /code-simplifier rewrites code after the main review batch has already run. Without this gate, the simplifier's output would ship unreviewed. This gate moves that review responsibility into the mutator itself — so the workflow-review-changes workflow no longer needs a separate /code-review step after /code-simplifier.
Used standalone (outside a review workflow), this self-review gate is sufficient for the simplifier's own changes; you may still finish with /changes-review or the active workflow's review gate for broader, whole-changeset coverage.
Workflow Recommendation
MANDATORY — NO EXCEPTIONS: If NOT already in workflow, use AskUserQuestion to ask user. Do NOT decide this is "simple enough to skip" — the user decides:
- Activate
workflow-review-changes workflow (Recommended) — full changes-review restart gate → validated fix cycle (plan → plan-review → feature-implement) → re-review → docs
- Execute
/code-simplifier directly — run standalone (this skill self-reviews its own changes via the Self-Review Gate)
Next Steps
MANDATORY — NO EXCEPTIONS after completing, use AskUserQuestion:
- "/workflow-review-changes (Recommended)" — Review all changes before commit
- "/code-review" — Full code review
- "Skip, continue manually" — user decides
AI Agent Integrity Gate (NON-NEGOTIABLE)
Completion ≠ Correctness. Before reporting work done, prove it:
- Grep every removed name. Extraction/rename/delete → grep confirms 0 dangling refs across ALL file types.
- Ask WHY before changing. Existing values intentional until proven otherwise. No "fix" without traced rationale.
- Verify ALL outputs. One build passing ≠ all builds passing. Check every affected stack.
- Evaluate pattern fit. Copying nearby code? Verify preconditions match — same scope, lifetime, base class, constraints.
- New artifact = wired artifact. Created? Prove registered, imported, reachable by all consumers.
[IMPORTANT] Use TaskCreate to break ALL work into small tasks BEFORE starting — including tasks for each file read. For simple tasks, ask user whether to skip.
Prerequisites: MUST ATTENTION READ before executing:
docs/project-reference/domain-entities-reference.md — domain entity catalog, relationships, cross-service sync (read when task involves business entities/models)
External Memory: Complex/lengthy work → write findings to plans/reports/. Prevents context loss, serves as deliverable.
Evidence Gate: MANDATORY — every claim, finding, recommendation requires file:line proof or traced evidence (confidence >80% to act, <80% verify first).
OOP & DRY Enforcement: MANDATORY — flag duplicated patterns for base class extraction. Same-suffix classes (*Entity, *Dto, *Service) inherit common base. Verify stack has linting/analyzer configured.
Self-Recursive Simplification Loop
Purpose: Avoid spending tokens on a fresh-context review of this skill's own findings. The simplifier owns its own convergence loop; broader review workflows can still run after the simplifier reports clean.
Loop:
- Analyze the current target/diff for simplification findings with
file:line evidence.
- Apply only behavior-preserving simplifications that satisfy the evidence gate.
- Run targeted verification after each change set.
- Re-read the updated diff and re-run this skill's simplification dimensions.
- Repeat until this skill finds zero simplification findings.
Stop conditions:
- The same simplification finding repeats for 3 passes with no progress.
- A simplification needs product/owner input or has behavior-change risk.
- Verification cannot run or cannot prove behavior preservation.
Rules:
- Do not spawn a fresh-context reviewer just because simplifications were applied.
- Do not re-review known findings in a fresh context before fixing them.
- Do not hand off as clean until the self-recursive pass finds zero simplification findings.
- After the self-recursive loop is clean, run the Self-Review Gate — if any files were changed, self-invoke
/code-review scoped to those files (recursion-safe leaf skill); skip + log if nothing changed.
Sub-Agent Return Contract — When this skill spawns a sub-agent, the sub-agent MUST return ONLY this structure. Main agent reads only this summary — NEVER requests full sub-agent output inline.
## Sub-Agent Result: [skill-name]
Status: ✅ PASS | ⚠️ PARTIAL | ❌ FAIL
Confidence: [0-100]%
### Findings (Critical/High only — max 10 bullets)
- [severity] [file:line] [finding]
### Actions Taken
- [file changed] [what changed]
### Blockers (if any)
- [blocker description]
Full report: plans/reports/[skill-name]-[date]-[slug].md
Main agent reads Full report file ONLY when: (a) resolving a specific blocker, or (b) building a fix plan.
Sub-agent writes full report incrementally (per SYNC:incremental-persistence) — not held in memory.
Context budget — the return payload is a SUMMARY, not a transcript: ≤10 finding bullets, no raw file contents / full diffs / verbatim logs inline, no re-pasted source. Everything beyond the summary lives in the Full report on disk. A sub-agent that would exceed the summary shape MUST write the detail to its report and return only the pointer — the orchestrator's context is the scarce resource the whole map-reduce protects.
UI System Context — For ANY task touching .ts, .html, .scss, or .css files:
MUST ATTENTION READ before implementing:
docs/project-reference/frontend-patterns-reference.md — component base classes, stores, forms
docs/project-reference/scss-styling-guide.md — BEM methodology, SCSS variables, mixins, responsive
docs/project-reference/design-system/README.md — design tokens, component inventory, icons
Reference docs/project-config.json for project-specific paths.
Shared Protocol Duplication Policy — Inline protocol content in skills (wrapped in <!-- SYNC:tag -->) is INTENTIONAL duplication. Do NOT extract, deduplicate, or replace with file references. AI compliance drops significantly when protocols are behind file-read indirection. To update: edit .claude/skills/shared/sync-inline-versions.md first, then grep SYNC:protocol-name and update all occurrences.
Source/test drift check. For coding, fix, debug, investigation, test, or review work: when source behavior changes, inspect affected unit/integration/E2E tests and decide from evidence whether tests should change to match intended behavior or the source change is an unintended bug to fix. Do not write tests for migration code; schema/data migrations are one-time execution paths, not core application logic.
AI Mistake Prevention — Failure modes to avoid on every task:
Re-read files after context changes. Context compaction, resume, or long-running work can make memory stale; verify current files before acting.
Verify generated content against source evidence. AI hallucinates APIs, names, claims, and document facts. Check the relevant source before documenting or referencing.
Check downstream references before deleting or renaming. Removing an artifact can stale docs, generated mirrors, configs, and callers; map references first.
Trace the full impact chain after edits. Changing a definition can miss derived outputs and consumers. Follow the affected chain before declaring done.
Verify ALL affected outputs, not just the first. One green check is not all green checks; validate every output surface the change can affect.
Assume existing values are intentional — ask WHY before changing OR flagging one as a defect. Before changing or reporting a constant, limit, flag, cutoff, wording, or pattern, read nearby context and history, the CALLER's ordering, and 2+ sibling call sites of the same convention. A doc stating WHAT without WHY is missing rationale, not proof of a missing guard.
Surface ambiguity before acting — don't pick silently. Multiple valid interpretations require an explicit question or stated assumption with risk.
Assert the outcome your system owns, not the intermediate state your infrastructure owns. When verifying async work, assert the final business state — never the delivery/retry bookkeeping held in shared infrastructure that any co-running process can write. Such a check passes when run alone and flakes the moment anything else shares that infrastructure.
Keep shared guidance role-relevant. Universal guidance must help every receiving skill or agent; code-specific obligations belong only in code-specific protocols.
Critical Thinking Mindset — Apply critical thinking, sequential thinking. Every claim needs traced proof, confidence >80% to act.
Anti-hallucination: Never present guess as fact — cite sources for every claim, admit uncertainty freely, self-check output for errors, cross-reference independently, stay skeptical of own confidence — certainty without evidence root of all hallucination.
Understand Code First — HARD-GATE: Do NOT write, plan, or fix until you READ existing code.
- Search 3+ similar patterns (
grep/glob) — cite file:line evidence
- Read existing files in target area — understand structure, base classes, conventions
- Run
python .claude/scripts/code_graph trace <file> --direction both --json when .code-graph/graph.db exists
- Map dependencies via
connections or callers_of — know what depends on your target
- Write investigation to
.ai/workspace/analysis/ for non-trivial tasks (3+ files)
- Re-read analysis file before implementing — never work from memory alone. — why: long context drifts from the file; the file is ground truth
- NEVER invent new patterns when existing ones work — match exactly or document deviation. — why: divergent patterns fragment the codebase and slow every future reader
BLOCKED until: - [ ] Read target files - [ ] Grep 3+ patterns - [ ] Graph trace (if graph.db exists) - [ ] Assumptions verified with evidence
Design Patterns Quality — Priority checks for every code change:
- DRY via OOP: Identify classes/modules with the same purpose, naming pattern, or lifecycle. Apply your knowledge of the project's language/framework to determine the idiomatic abstraction (base class, mixin, trait, protocol, decorator). 3+ similar patterns → extract to shared abstraction.
- Right Responsibility: Logic in LOWEST layer (Entity > Domain Service > Application Service > Controller). Never business logic in controllers.
- SOLID: Single responsibility (one reason to change). Open-closed (extend, don't modify). Liskov (subtypes substitutable). Interface segregation (small interfaces). Dependency inversion (depend on abstractions).
- After extraction/move/rename: Grep ENTIRE scope for dangling references. Zero tolerance.
- YAGNI gate: NEVER recommend patterns unless 3+ occurrences exist. Don't extract for hypothetical future use.
Anti-patterns to flag: God Object, Copy-Paste inheritance, Circular Dependency, Leaky Abstraction.
Serial Attention for Design Quality — Scan one quality dimension at a time (serial passes), not all concerns at once. — why: split attention misses violations that single-focus passes catch.
- Identify applicable dimensions — Based on the code's language, domain, and patterns, determine which quality dimensions apply: DRY, SOLID principles (SRP/OCP/LSP/ISP/DIP), OOP idioms, cohesion/coupling, GRASP, Law of Demeter, CQRS invariants, etc. Your list is NOT fixed — derive from what the code actually does.
- One focused pass per dimension — Dedicate single-focus attention to EACH dimension in sequence. Do NOT mix concerns across passes.
- Threshold: 3+ similar patterns = MANDATORY extraction — Not optional suggestion. Flag as mandatory structural fix requiring action.
- 2+ violations of same kind = structural finding — Report as "pattern problem" needing architectural resolution, not a list of individual instances.
Complexity Prevention (Ousterhout) — MANDATORY. Measure code by cost of change: one business change should map to one code change. Flag ALL of the following in review:
- Change amplification — small business change forces edits in >3 places → structural flaw. Count edit sites for a plausible future change (add variant, add field, add authorization). >3 = reject.
- Cognitive load — reader must hold too much context to safely modify. Flag deep inheritance, long parameter lists, boolean traps, implicit ordering dependencies.
- Cross-cutting duplication at entry points — logging, error handling, validation, auth, transactions reimplemented per controller/handler/route. Lift to middleware / interceptor / filter / decorator / aspect.
- Leaked implementation technology — repos returning
IQueryable/QuerySet/Criteria/raw cursors/ORM entities to callers. Return finished results + intent-revealing methods (GetActiveVipUsers() not Query()).
- Type-switch scattering —
switch/if-chains on enum/discriminator in >1 place. New variant = new file, not N edits. One factory/registry switch at the boundary OK; scattered switches = reject.
- Anemic models — domain objects with only getters/setters, logic floats in services. Move invariants/behavior onto the object (
order.Checkout(), not order.Status = ...).
- Primitive obsession — raw
string/int/decimal for account numbers, emails, money, percentages, date ranges, with re-validation at every entry. Wrap in value objects / records / structs that validate once at construction.
- Inline cross-cutting concerns — authorization/tenant isolation/audit/sanitization hand-written at top of every handler. Flag intent with declarative markers (
@RequirePermission("Order.Delete")), enforce once centrally.
- Shallow modules — tiny class, big interface (many public methods, many flags, many ctor params) wrapping little logic. A module is deep when a small interface hides a lot of implementation. If interface ≈ implementation cost to learn → inline.
- Missing base class for repeated component/handler lifecycle — 3+ forms/CRUD handlers/list views reimplementing loading/dirty/submit/pagination → extract to base class / hook / composable / mixin / trait.
- Premature vs delayed abstraction — rule-of-three. First occurrence: write it. Second: notice duplication. Third: extract. Don't build generic frameworks before real variation; don't copy-paste for the 4th time.
- Embedded utility logic not extracted to helpers — inline paging loops (
while (hasMore) { skip += take; ... }), ad-hoc datetime math, string parsing/formatting, collection partitioning, retry/backoff loops, URL/query-string building. If the algorithm is non-trivial AND stack-generic (not business-specific), extract to util/helper/extensions and let consumers call one line. Inline duplicates → duplicated bug surface.
- Logic in wrong (higher) layer — downshift to callee — business/derivation logic written in the caller when the callee owns the data. Defaults: Controller code that should be App Service. App Service code that should be Domain Service or Entity. Component code that should be ViewModel/Store/Service. Caller reaching into callee's data shape to compute something → move the computation behind an intent-revealing method on the callee. Lowest responsible layer wins (Entity > Domain Service > App Service > Controller · Model/VM > Store > Component). Higher-layer placement = duplicated logic when a sibling caller needs the same thing.
- Owner owns the rule — extract on first write — if a caller inlines logic that derives, normalizes, validates, or computes from another type's data, MOVE it to the owning type. Single use is sufficient — the trigger is wrong responsibility, not duplication. Sibling callers always arrive; inline copies drift silently with no compile error and no name to grep. Common offenders: Backend — inlined rules in application-layer handlers / commands / queries / services / controllers that belong on the domain entity / value object / domain service. Frontend — inlined derivations / formatting / validation in components that belong on the model / store / view-model / API service. Fix: name the rule once as a method (static or instance) on the owning type; callers invoke by name. Future variant → SECOND named method on the owner, never an inline near-duplicate. Right responsibility first; reuse is the consequence.
Extraction target — where the named rule lives:
| Shape of the rule |
Goes to |
| Pure function over an entity's own data |
static method on the entity |
| Behavior that mutates / guards entity state |
instance method on the entity |
| Always-true invariant on a primitive value |
value object constructor |
| Needs DI (repo / settings / clock) |
helper class registered in DI |
| Domain-agnostic algorithm reused across types |
util / extension method |
| Pure shape / projection conversion |
DTO mapping |
Pre-commit edit-site test (reject if answer is "many"):
| Change Scenario |
Should touch |
| Add new variant (customer type, payment method) |
1 new file |
| Change HTTP error response format |
1 middleware/filter |
| Add timestamp field to every persisted entity |
1 base entity/interceptor |
| Add authorization to a new endpoint |
1 declarative marker |
| Swap database/ORM |
Data layer only |
| Change business calculation rule |
1 method on owning entity |
| Add loading indicator pattern to forms |
1 base component/hook |
| Add validation rule to a domain primitive |
1 value-object ctor |
| Change paging/retry/datetime algorithm |
1 helper/util function |
| Change a derivation of entity data |
1 method on the entity |
Operating heuristics:
- Write the call site first.
- Count edit sites for plausible future change.
- Prefer removing code over adding it.
- Surface assumptions at boundaries, hide details inside.
- Pre-reuse scan — before writing a non-trivial block, grep for similar algorithms (
while.*skip, DateTime.*Add, split/join chains, paging loops, retry loops). Match existing helper → call it. None exists but pattern is stack-generic → extract to util before second caller appears.
- Layer placement test — ask "if a sibling caller needed this tomorrow, would they re-derive it?" If yes, the logic is in the wrong layer. Move it down.
- Open-case-for-future-reuse — if reviewer spots a block that is likely to appear in another feature (domain-agnostic algorithm, shared lifecycle, recurring derivation), do NOT rationalize with pure YAGNI. Either extract now (if cheap) or create a tracked TODO with the exact extraction target so the second caller does not duplicate silently. Silent duplication is the default failure mode.
- When in doubt ask: "What would need to change if the requirement shifts?"
The measure of good code is the cost of change. Not shortest. Not cleverest. Not most abstracted. Cheapest to safely modify having read a small local portion.
Severity Rubric — Classify every finding by consequence, not by how easy it is to fix. One scale across all reviews so a "High" means the same thing everywhere.
| Severity |
Action |
Definition |
| CRITICAL |
Block merge |
Silent runtime failure, data corruption, validation bypass, security hole |
| HIGH |
Must fix |
Incorrect behavior, invariant gap, architectural violation |
| MEDIUM |
Should fix |
Design debt, maintainability, likely future bug |
| LOW |
Nice to fix |
Convention, documentation, minor clarity |
Score-based skills map their numeric scale onto these tiers — do not invent a parallel vocabulary:
- 0-2 criterion scoring (e.g. production-readiness-review):
0 = CRITICAL/HIGH (criterion unmet, blocks production readiness), 1 = MEDIUM (partial, should fix), 2 = pass (no finding).
- Two-axis scoring (e.g. performance-review, impact × likelihood): map the resulting cell to the nearest tier — high-impact + high-likelihood → CRITICAL/HIGH; low-impact OR low-likelihood → MEDIUM/LOW.
A finding's tier drives the gate: CRITICAL/HIGH must be resolved or explicitly accepted by the owner before PASS; MEDIUM/LOW may ship with a tracked follow-up.
- MANDATORY IMPORTANT MUST ATTENTION cite
file:line evidence for every claim. Confidence >80% to act, <60% = do NOT recommend.
MUST ATTENTION apply critical + sequential thinking — every claim needs appropriate traced evidence (file:line for repo/code claims; source URL or artifact section for research, product, content, and docs claims); confidence >80% to act, <60% DO NOT recommend. Anti-hallucination: never present guess as fact, admit uncertainty freely, cross-reference independently, stay skeptical of own confidence.
MUST ATTENTION apply complexity prevention — one business change = one code change. Flag change amplification (>3 edit sites for future change), scattered type-switches, anemic models, primitive obsession, leaked technology through abstractions, shallow modules, un-extracted utility logic (paging/datetime/string/retry → helpers), and logic in the wrong higher layer (downshift to callee/entity/VM). Don't rationalize silent duplication with pure YAGNI.
MUST ATTENTION apply AI mistake prevention — verify generated content against evidence, trace downstream references before deleting or renaming, verify all affected outputs, re-read files after context loss, and surface ambiguity before acting.
- MANDATORY Classify findings Critical/High/Medium/Low by consequence; Critical/High block PASS until fixed or owner-accepted.
- MANDATORY Score-based skills (sre 0-2, perf two-axis) map onto the same four tiers — no parallel severity vocabulary.
Prompt-Enhance Closing Anchors
IMPORTANT MUST ATTENTION follow declared step order for this skill; NEVER skip, reorder, or merge steps without explicit user approval
IMPORTANT MUST ATTENTION for every step/sub-skill call: set in_progress before execution, set completed after execution
IMPORTANT MUST ATTENTION every skipped step MUST include explicit reason; every completed step MUST include concise evidence
IMPORTANT MUST ATTENTION if Task tools unavailable, maintain an equivalent step-by-step plan tracker with synchronized statuses
Parallel Sub-Agent Dispatch — Plan parallelism the moment a task breakdown exists, BEFORE executing it — running provably independent tasks sequentially wastes wall-clock. Applies to every multi-step job: workflow steps, planning, batch updates, investigation, research, scans, reviews, doc sync. Plan execution is metadata-gated, NEVER default-parallel — fan-out follows ONLY what the plan declares (PAR/SEQ tags + per-phase write set); an untagged plan runs sequentially — why: a derived write set cannot see cascade or generated writes.
- Tag every task
PAR or SEQ. PAR = inputs exclude every pending task's output AND write set disjoint from every other PAR. Else SEQ — MUST ATTENTION name the dependency forcing it.
- Group
PAR into waves. No edge between members. Two writers of one file NEVER share a wave. Read-only work (search, investigation, review, research) paral
…(truncated)
1---2name: code-simplifier-33description: [Code Quality] Use when you need to simplify and refine code for clarity, consistency, and maintainability while preserving all functionality.4---56<!-- PROMPT-ENHANCE:STEP-TASK-ANCHOR:START -->78> **[BLOCKING]** Execute skill steps in declared order. NEVER skip, reorder, or merge steps without explicit user approval.9> **[BLOCKING]** Before each step or sub-skill call, update task tracking: set `in_progress` when step starts, set `completed` when step ends.10> **[BLOCKING]** Every completed/skipped step MUST include brief evidence or explicit skip reason.11> **[BLOCKING]** If Task tools are unavailable, create and maintain an equivalent step-by-step plan tracker with the same status transitions.1213<!-- PROMPT-ENHANCE:STEP-TASK-ANCHOR:END -->1415## Quick Summary1617**Goal:** Lower the cost of the next change — cut coupling, hidden state, duplicated knowledge, unclear intent — by simplifying and refining code for clarity, consistency, and maintainability without altering any observable behavior. — why: every simplification serves future change cost, not aesthetics.1819**Summary:** (read-this-if-nothing-else digest — purpose + every main step)2021- **Purpose — skeptical-first MUTATOR, not a suggester:** grep all usages + trace consumers (graph downstream when graph.db exists) and cite `file:line` BEFORE touching anything; apply a simplification ONLY when certain it preserves behavior, never when unsure. — why: an unverified "safe" rewrite silently breaks a downstream consumer.22- **Main steps, run in order:** (1) **Phase 0 Detect** artifact type (backend/frontend/test/config) + scope; (2) **Identify Targets** — recent git changes or named files, HARD-SKIP generated/migration/vendor; (3) **Analyze** via the 5 Simplification Dimensions; (4) **Apply** one refactoring type at a time (KISS/DRY/YAGNI, behavior-preserving); (5) **Verify** related tests after EACH change; (6) **Self-Recursive Loop** (analyze→simplify→verify) until zero findings or a no-progress/unsafe/owner-decision stop hits — do NOT spawn a fresh-context reviewer for your own findings; (7) **Self-Review Gate**.23- **The 5 Simplification Dimensions (step 3):** readability · DRY/abstraction (≥3 occurrences, YAGNI gate) · right-responsibility-lowest-layer (Entity > Domain Service > App Service > Controller) · complexity reduction (flatten nesting, extract >20-line methods) · DB paging+indexes — every technique answers ONE test: does this make the next change cheaper?24- **Self-Review Gate (step 7) — this skill owns review of its own output:** when it changed any file, self-invoke `/code-review` scoped to ONLY those changed files (recursion-safe leaf — NEVER `/changes-review`); skip + log the reason when nothing changed. — why: the simplifier rewrites code after the main review batch, so its output ships unreviewed without this gate.25- **Read FIRST:** `docs/project-reference/code-review-rules.md` (anti-patterns/checklists) then `project-structure-reference.md` — before any modification.2627> **MANDATORY IMPORTANT MUST ATTENTION** Plan task to READ:28>29> - `docs/project-reference/code-review-rules.md` — anti-patterns, review checklists **(READ FIRST)**30> - `project-structure-reference.md` — project patterns/structure31>32> If not found, search for: project documentation, coding standards, architecture docs.3334**Workflow:**35361. **Phase 0: Detect** — Classify artifact type (backend/frontend/test/config) and scope372. **Identify Targets** — Recent git changes or specified files (skip generated/vendor)383. **Analyze** — Apply simplification dimensions (see below)394. **Apply** — One refactoring type at a time following KISS/DRY/YAGNI405. **Verify** — Run related tests, confirm no behavior changes416. **Self-Recursive Check** — Re-run this skill's simplification analysis until no simplification findings remain427. **Self-Review Gate (MANDATORY when code changed)** — If this skill modified any files, self-invoke `/code-review` scoped to ONLY those changed files; skip + log if nothing changed4344**Key Rules:**4546- Preserve all existing functionality — no behavior changes47- Follow the project's documented patterns (entity expressions, fluent helpers, store base, BEM)48- Easy to Change is the primary simplification goal for source files; DRY, SOLID, abstraction, and patterns are valid only when they lower future edit sites or cognitive load49- Tests pass after every change50- Apply simplification only when certain it preserves behavior — NEVER apply when unsure5152## Phase 0: Artifact Detection5354**MUST ATTENTION** classify before simplifying — detection drives focus and optional escalation only:5556| Artifact Type | Detection | Key Focus |57| ---------------- | -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |58| Backend | Backend source files for the current stack (e.g. `.cs`) | Domain-model expressions, fluent API, DRY via OOP, SOLID |59| Frontend | Frontend source files for the current stack (e.g. `.ts`, `.html`, `.scss`) | BEM, store base, subscription cleanup, component base |60| Tests | Test source files for the current stack (e.g. `*Test.cs`, `*.spec.ts`) | Assertions, async-assertion helpers (e.g. an await-until-condition poll helper), data isolation |61| Config/Generated | Migrations, generated/vendor files (e.g. `*.generated.*`) | **SKIP** — NEVER simplify generated/migration code |6263Optional escalation by artifact:6465| Artifact | Escalate only when |66| -------------------- | -------------------------------------------------------- |67| Source code/diffs | Broad review is requested after simplifier loop is clean |68| Security-sensitive | Security-specific risk is present |69| Performance-critical | Performance behavior is part of the change |70| Plans/docs/specs | Artifact review is explicitly requested |7172## First Principle — Easy to Change7374> **The success metric of every coding decision is _future change cost_.**75> DRY, SRP, abstraction, design patterns, naming, layering, tests — every76> technique exists to serve one goal: **making the next change cheaper**.7778When evaluating code, a refactor, a test, or an abstraction, ask:79**does this make the next change cheaper or more expensive?**8081- Reject "best practices" that raise change cost (premature abstraction,82 speculative generality, leaky indirection, ceremony without payoff).83- Name the real enemies in findings: **coupling, hidden state, duplicated84 knowledge, unclear intent, irreversible decisions exposed too early**.85- Favor project-owned boundaries around external libraries, for example86 component/service input-output contracts, when they localize future library87 changes; reject pass-through wrappers that add ceremony without lowering88 change cost.89- A simpler design that is easy to change beats a sophisticated design that90 isn't.9192Apply this lens **before** invoking any specific rule, pattern, or checklist93below — if a downstream rule would raise change cost, this principle wins.9495---9697## Simplification Mindset9899**Skeptical-first:** Verify before simplifying. Every change needs proof preserving behavior.100101- NEVER assume code redundant — trace call paths and read implementations first102- Before removing/replacing: grep all usages confirming nothing depends on current form103- Before flagging convention violation: grep 3+ existing examples — codebase convention wins104- Every simplification requires `file:line` evidence of what was verified105- Apply simplification only when certain it preserves behavior; if unsure → DO NOT apply106107## Simplification Dimensions108109Dimension-based reasoning replaces fixed checklists. Each dimension has a `Think:` prompt forcing first-principles reasoning.110111### Dimension 1: Readability112113> **Think:** Would a new engineer understand this in 30 seconds? What forces multiple file traces?114115- Schema visibility: functions computing data structures need output-shape comment116- Non-obvious pipelines: A→B→C transformations need brief pipeline explanation117- Self-documenting signatures: params explain role; remove unused params118- Magic values: replace unexplained numbers/strings with named constants119- Naming clarity: names reveal intent without reading implementation120121### Dimension 2: DRY & Abstraction122123> **Think:** Pattern appearing ≥3 places? What base class/generic eliminates duplication?124125- Same-suffix classes (`*Entity`, `*Dto`, `*Service`) → shared base126- Repeated logic blocks → extract to helper/extension method127- YAGNI gate: NEVER extract for hypothetical future use — 3+ occurrences required128129### Dimension 3: Right Responsibility130131> **Think:** Logic in lowest layer that can own it? Could moving it down enable reuse?132133- `Entity/Model → Domain Service → Application Service → Controller` (logic belongs lowest)134- Business logic in controllers → move down135- Mapping in handlers → move to DTO methods136137### Dimension 4: Complexity Reduction138139> **Think:** What is cognitive load? Can nesting/conditionals flatten?140141- Nesting >3 → refactor (early returns, extract methods)142- Methods >20 lines → extract143- Complex conditionals → flatten or Strategy pattern (3+ branching occurrences only)144145### Dimension 5: Database Performance146147> **MANDATORY IMPORTANT MUST ATTENTION**148>149> 1. **Paging:** ALL list queries MUST use pagination. NEVER unbounded `GetAll()`, `ToList()`, `Find()` without `Skip/Take` or cursor-based paging.150> 2. **Indexes:** ALL filter fields, foreign keys, sort columns MUST have database indexes. Entity expressions must match index field order. Collections need index management methods.151152## Project Patterns153154### Backend155156- Extract entity static expressions (search: entity expression pattern)157- Use fluent helpers (search: fluent helper pattern in `docs/project-reference/backend-patterns-reference.md`)158- Move mapping to DTO mapping methods (search: DTO mapping pattern)159- Use project validation fluent API (see `docs/project-reference/backend-patterns-reference.md`)160- Verify entity expressions have database indexes161- Verify document DB collections have index management methods162163### Frontend164165- Use project store base (search: store base class) for state management166- Apply subscription cleanup (search: subscription cleanup pattern) to all subscriptions167- BEM class naming on ALL template elements168- Use the project's base classes (search: base component class, store component base class)169170## Graph Intelligence (MANDATORY if graph.db exists)171172Before simplifying, trace what depends on target:173174```175python .claude/scripts/code_graph trace <file> --direction downstream --json176```177178Verify simplified code preserves same interface for all traced consumers. Cross-service MESSAGE_BUS consumers are especially fragile — may depend on exact message shape.179180Additional queries:181182- Verify no callers break: `python .claude/scripts/code_graph query callers_of <function> --json`183- Check dependents: `python .claude/scripts/code_graph query importers_of <module> --json`184- Batch analysis: `python .claude/scripts/code_graph batch-query file1 file2 --json`185186## Execution187188```189Agent(subagent_type="code-simplifier", prompt="Review and simplify [target files]")190```191192**Example:**193194```typescript195// Before196function getData() {197 const result = fetchData();198 if (result !== null && result !== undefined) {199 return result;200 } else {201 return null;202 }203}204205// After206function getData() {207 return fetchData() ?? null;208}209```210211## Constraints212213- **Preserve functionality** — no behavior changes214- **Tests passing** — verify after every change215- **Follow patterns** — use the project's conventions, never invent216- **Doc staleness** — cross-ref changed files against feature docs, test specs, READMEs; flag updates needed217- **Preserve ALL invariants; never weaken a property/mutation test** — a refactor MUST keep every `[HARD]` §4 rule / §5 invariant intact and MUST NOT delete, relax, or trivialize any property test or mutation test that guards them. If a simplification changes observable behavior, that is NOT a silent change — it is a **Dual-Feedback finding** (feed the spec AND the tests, then re-review), report it and stop, never ship it. After simplifying, the package MUST still pass the SAME property/mutation bar it passed before — green tests on a weakened bar are not a pass.218219---220221## Self-Recursive Verification (MANDATORY after simplifications)222223After simplifications applied, verification requires a **self-recursive simplification pass** over the updated diff. Do NOT spawn fresh-context reviewer to re-review this skill's own findings. Repeat analyze → simplify → verify until this skill finds no further simplification opportunities, or stop on unsafe/no-progress/user-decision blocker.224225## Self-Review Gate (MANDATORY when this skill changed code)226227> **This skill is a code MUTATOR. It owns the review of its own output.** Once the self-recursive simplification loop above is clean, gate the result:228>229> 1. **Did this skill modify any files?** Determine the exact set of files this skill changed (its own edits — not the whole working tree).230> - **No files changed** → SKIP this gate and **log the skip reason** ("code-simplifier made no changes — no self-review needed"). Done.231> - **Files changed** → continue.232> 2. **Self-invoke `/code-review` scoped to ONLY the changed files.** Pass the explicit changed-file set as the review target — not the full diff, not unrelated files.233> 3. **Integrate the `/code-review` findings.** If it surfaces blocking issues caused by the simplification, fix them (behavior-preserving only) and re-run the self-recursive loop + this gate. If issues are out of simplification scope, report them up — do not silently drop.234>235> **Recursion safety:** `/code-review` is a LEAF review skill — it does NOT invoke `/code-simplifier` back, so there is no cycle. Use `/code-review` here, NEVER `/changes-review` (the heavyweight workflow that itself contains `/code-simplifier` and would recurse).236>237> **Why this gate exists:** `/code-simplifier` rewrites code after the main review batch has already run. Without this gate, the simplifier's output would ship unreviewed. This gate moves that review responsibility into the mutator itself — so the `workflow-review-changes` workflow no longer needs a separate `/code-review` step after `/code-simplifier`.238239Used standalone (outside a review workflow), this self-review gate is sufficient for the simplifier's own changes; you may still finish with `/changes-review` or the active workflow's review gate for broader, whole-changeset coverage.240241## Workflow Recommendation242243> **MANDATORY — NO EXCEPTIONS:** If NOT already in workflow, use `AskUserQuestion` to ask user. Do NOT decide this is "simple enough to skip" — the user decides:244>245> 1. **Activate `workflow-review-changes` workflow** (Recommended) — full changes-review restart gate → validated fix cycle (plan → plan-review → feature-implement) → re-review → docs246> 2. **Execute `/code-simplifier` directly** — run standalone (this skill self-reviews its own changes via the Self-Review Gate)247248---249250## Next Steps251252**MANDATORY — NO EXCEPTIONS** after completing, use `AskUserQuestion`:253254- **"/workflow-review-changes (Recommended)"** — Review all changes before commit255- **"/code-review"** — Full code review256- **"Skip, continue manually"** — user decides257258## AI Agent Integrity Gate (NON-NEGOTIABLE)259260> **Completion ≠ Correctness.** Before reporting work done, prove it:261>262> 1. **Grep every removed name.** Extraction/rename/delete → grep confirms 0 dangling refs across ALL file types.263> 2. **Ask WHY before changing.** Existing values intentional until proven otherwise. No "fix" without traced rationale.264> 3. **Verify ALL outputs.** One build passing ≠ all builds passing. Check every affected stack.265> 4. **Evaluate pattern fit.** Copying nearby code? Verify preconditions match — same scope, lifetime, base class, constraints.266> 5. **New artifact = wired artifact.** Created? Prove registered, imported, reachable by all consumers.267268> **[IMPORTANT]** Use `TaskCreate` to break ALL work into small tasks BEFORE starting — including tasks for each file read. For simple tasks, ask user whether to skip.269270**Prerequisites:** **MUST ATTENTION READ** before executing:271272- `docs/project-reference/domain-entities-reference.md` — domain entity catalog, relationships, cross-service sync (read when task involves business entities/models)273274> **External Memory:** Complex/lengthy work → write findings to `plans/reports/`. Prevents context loss, serves as deliverable.275276> **Evidence Gate:** MANDATORY — every claim, finding, recommendation requires `file:line` proof or traced evidence (confidence >80% to act, <80% verify first).277278> **OOP & DRY Enforcement:** MANDATORY — flag duplicated patterns for base class extraction. Same-suffix classes (`*Entity`, `*Dto`, `*Service`) inherit common base. Verify stack has linting/analyzer configured.279280## Self-Recursive Simplification Loop281282**Purpose:** Avoid spending tokens on a fresh-context review of this skill's own findings. The simplifier owns its own convergence loop; broader review workflows can still run after the simplifier reports clean.283284Loop:2852861. Analyze the current target/diff for simplification findings with `file:line` evidence.2872. Apply only behavior-preserving simplifications that satisfy the evidence gate.2883. Run targeted verification after each change set.2894. Re-read the updated diff and re-run this skill's simplification dimensions.2905. Repeat until this skill finds zero simplification findings.291292Stop conditions:293294- The same simplification finding repeats for 3 passes with no progress.295- A simplification needs product/owner input or has behavior-change risk.296- Verification cannot run or cannot prove behavior preservation.297298Rules:299300- Do not spawn a fresh-context reviewer just because simplifications were applied.301- Do not re-review known findings in a fresh context before fixing them.302- Do not hand off as clean until the self-recursive pass finds zero simplification findings.303- After the self-recursive loop is clean, run the **Self-Review Gate** — if any files were changed, self-invoke `/code-review` scoped to those files (recursion-safe leaf skill); skip + log if nothing changed.304305<!-- SYNC:subagent-return-contract -->306307> **Sub-Agent Return Contract** — When this skill spawns a sub-agent, the sub-agent MUST return ONLY this structure. Main agent reads only this summary — NEVER requests full sub-agent output inline.308>309> ```markdown310> ## Sub-Agent Result: [skill-name]311>312> Status: ✅ PASS | ⚠️ PARTIAL | ❌ FAIL313> Confidence: [0-100]%314>315> ### Findings (Critical/High only — max 10 bullets)316>317> - [severity] [file:line] [finding]318>319> ### Actions Taken320>321> - [file changed] [what changed]322>323> ### Blockers (if any)324>325> - [blocker description]326>327> Full report: plans/reports/[skill-name]-[date]-[slug].md328> ```329>330> Main agent reads `Full report` file ONLY when: (a) resolving a specific blocker, or (b) building a fix plan.331> Sub-agent writes full report incrementally (per SYNC:incremental-persistence) — not held in memory.332>333> **Context budget** — the return payload is a SUMMARY, not a transcript: ≤10 finding bullets, no raw file contents / full diffs / verbatim logs inline, no re-pasted source. Everything beyond the summary lives in the `Full report` on disk. A sub-agent that would exceed the summary shape MUST write the detail to its report and return only the pointer — the orchestrator's context is the scarce resource the whole map-reduce protects.334335<!-- /SYNC:subagent-return-contract -->336337<!-- SYNC:ui-system-context -->338339> **UI System Context** — For ANY task touching `.ts`, `.html`, `.scss`, or `.css` files:340>341> **MUST ATTENTION READ before implementing:**342>343> 1. `docs/project-reference/frontend-patterns-reference.md` — component base classes, stores, forms344> 2. `docs/project-reference/scss-styling-guide.md` — BEM methodology, SCSS variables, mixins, responsive345> 3. `docs/project-reference/design-system/README.md` — design tokens, component inventory, icons346>347> Reference `docs/project-config.json` for project-specific paths.348349<!-- /SYNC:ui-system-context -->350351<!-- SYNC:shared-protocol-duplication-policy -->352353> **Shared Protocol Duplication Policy** — Inline protocol content in skills (wrapped in `<!-- SYNC:tag -->`) is INTENTIONAL duplication. Do NOT extract, deduplicate, or replace with file references. AI compliance drops significantly when protocols are behind file-read indirection. To update: edit `.claude/skills/shared/sync-inline-versions.md` first, then grep `SYNC:protocol-name` and update all occurrences.354355<!-- /SYNC:shared-protocol-duplication-policy -->356357<!-- SYNC:source-test-drift-check -->358359> **Source/test drift check.** For coding, fix, debug, investigation, test, or review work: when source behavior changes, inspect affected unit/integration/E2E tests and decide from evidence whether tests should change to match intended behavior or the source change is an unintended bug to fix. Do not write tests for migration code; schema/data migrations are one-time execution paths, not core application logic.360361<!-- /SYNC:source-test-drift-check -->362363<!-- SYNC:ai-mistake-prevention -->364365> **AI Mistake Prevention** — Failure modes to avoid on every task:366>367> **Re-read files after context changes.** Context compaction, resume, or long-running work can make memory stale; verify current files before acting.368> **Verify generated content against source evidence.** AI hallucinates APIs, names, claims, and document facts. Check the relevant source before documenting or referencing.369> **Check downstream references before deleting or renaming.** Removing an artifact can stale docs, generated mirrors, configs, and callers; map references first.370> **Trace the full impact chain after edits.** Changing a definition can miss derived outputs and consumers. Follow the affected chain before declaring done.371> **Verify ALL affected outputs, not just the first.** One green check is not all green checks; validate every output surface the change can affect.372> **Assume existing values are intentional — ask WHY before changing OR flagging one as a defect.** Before changing or reporting a constant, limit, flag, cutoff, wording, or pattern, read nearby context and history, the CALLER's ordering, and 2+ sibling call sites of the same convention. A doc stating WHAT without WHY is missing rationale, not proof of a missing guard.373> **Surface ambiguity before acting — don't pick silently.** Multiple valid interpretations require an explicit question or stated assumption with risk.374> **Assert the outcome your system owns, not the intermediate state your infrastructure owns.** When verifying async work, assert the final business state — never the delivery/retry bookkeeping held in shared infrastructure that any co-running process can write. Such a check passes when run alone and flakes the moment anything else shares that infrastructure.375> **Keep shared guidance role-relevant.** Universal guidance must help every receiving skill or agent; code-specific obligations belong only in code-specific protocols.376377<!-- /SYNC:ai-mistake-prevention -->378379<!-- SYNC:critical-thinking-mindset -->380381> **Critical Thinking Mindset** — Apply critical thinking, sequential thinking. Every claim needs traced proof, confidence >80% to act.382> **Anti-hallucination:** Never present guess as fact — cite sources for every claim, admit uncertainty freely, self-check output for errors, cross-reference independently, stay skeptical of own confidence — certainty without evidence root of all hallucination.383384<!-- /SYNC:critical-thinking-mindset -->385386<!-- SYNC:understand-code-first -->387388> **Understand Code First** — HARD-GATE: Do NOT write, plan, or fix until you READ existing code.389>390> 1. Search 3+ similar patterns (`grep`/`glob`) — cite `file:line` evidence391> 2. Read existing files in target area — understand structure, base classes, conventions392> 3. Run `python .claude/scripts/code_graph trace <file> --direction both --json` when `.code-graph/graph.db` exists393> 4. Map dependencies via `connections` or `callers_of` — know what depends on your target394> 5. Write investigation to `.ai/workspace/analysis/` for non-trivial tasks (3+ files)395> 6. Re-read analysis file before implementing — never work from memory alone. — why: long context drifts from the file; the file is ground truth396> 7. NEVER invent new patterns when existing ones work — match exactly or document deviation. — why: divergent patterns fragment the codebase and slow every future reader397>398> **BLOCKED until:** `- [ ]` Read target files `- [ ]` Grep 3+ patterns `- [ ]` Graph trace (if graph.db exists) `- [ ]` Assumptions verified with evidence399400<!-- /SYNC:understand-code-first -->401402<!-- SYNC:design-patterns-quality -->403404> **Design Patterns Quality** — Priority checks for every code change:405>406> 1. **DRY via OOP:** Identify classes/modules with the same purpose, naming pattern, or lifecycle. Apply your knowledge of the project's language/framework to determine the idiomatic abstraction (base class, mixin, trait, protocol, decorator). 3+ similar patterns → extract to shared abstraction.407> 2. **Right Responsibility:** Logic in LOWEST layer (Entity > Domain Service > Application Service > Controller). Never business logic in controllers.408> 3. **SOLID:** Single responsibility (one reason to change). Open-closed (extend, don't modify). Liskov (subtypes substitutable). Interface segregation (small interfaces). Dependency inversion (depend on abstractions).409> 4. **After extraction/move/rename:** Grep ENTIRE scope for dangling references. Zero tolerance.410> 5. **YAGNI gate:** NEVER recommend patterns unless 3+ occurrences exist. Don't extract for hypothetical future use.411>412> **Anti-patterns to flag:** God Object, Copy-Paste inheritance, Circular Dependency, Leaky Abstraction.413>414> **Serial Attention for Design Quality** — Scan one quality dimension at a time (serial passes), not all concerns at once. — why: split attention misses violations that single-focus passes catch.415>416> 1. **Identify applicable dimensions** — Based on the code's language, domain, and patterns, determine which quality dimensions apply: DRY, SOLID principles (SRP/OCP/LSP/ISP/DIP), OOP idioms, cohesion/coupling, GRASP, Law of Demeter, CQRS invariants, etc. Your list is NOT fixed — derive from what the code actually does.417> 2. **One focused pass per dimension** — Dedicate single-focus attention to EACH dimension in sequence. Do NOT mix concerns across passes.418> 3. **Threshold: 3+ similar patterns = MANDATORY extraction** — Not optional suggestion. Flag as mandatory structural fix requiring action.419> 4. **2+ violations of same kind = structural finding** — Report as "pattern problem" needing architectural resolution, not a list of individual instances.420421<!-- /SYNC:design-patterns-quality -->422423<!-- SYNC:complexity-prevention -->424425> **Complexity Prevention (Ousterhout)** — MANDATORY. Measure code by cost of change: one business change should map to one code change. Flag ALL of the following in review:426>427> 1. **Change amplification** — small business change forces edits in >3 places → structural flaw. Count edit sites for a plausible future change (add variant, add field, add authorization). >3 = reject.428> 2. **Cognitive load** — reader must hold too much context to safely modify. Flag deep inheritance, long parameter lists, boolean traps, implicit ordering dependencies.429> 3. **Cross-cutting duplication at entry points** — logging, error handling, validation, auth, transactions reimplemented per controller/handler/route. Lift to middleware / interceptor / filter / decorator / aspect.430> 4. **Leaked implementation technology** — repos returning `IQueryable`/`QuerySet`/`Criteria`/raw cursors/ORM entities to callers. Return finished results + intent-revealing methods (`GetActiveVipUsers()` not `Query()`).431> 5. **Type-switch scattering** — `switch`/`if`-chains on enum/discriminator in >1 place. New variant = new file, not N edits. One factory/registry switch at the boundary OK; scattered switches = reject.432> 6. **Anemic models** — domain objects with only getters/setters, logic floats in services. Move invariants/behavior onto the object (`order.Checkout()`, not `order.Status = ...`).433> 7. **Primitive obsession** — raw `string`/`int`/`decimal` for account numbers, emails, money, percentages, date ranges, with re-validation at every entry. Wrap in value objects / records / structs that validate once at construction.434> 8. **Inline cross-cutting concerns** — authorization/tenant isolation/audit/sanitization hand-written at top of every handler. Flag intent with declarative markers (`@RequirePermission("Order.Delete")`), enforce once centrally.435> 9. **Shallow modules** — tiny class, big interface (many public methods, many flags, many ctor params) wrapping little logic. A module is deep when a small interface hides a lot of implementation. If interface ≈ implementation cost to learn → inline.436> 10. **Missing base class for repeated component/handler lifecycle** — 3+ forms/CRUD handlers/list views reimplementing loading/dirty/submit/pagination → extract to base class / hook / composable / mixin / trait.437> 11. **Premature vs delayed abstraction** — rule-of-three. First occurrence: write it. Second: notice duplication. Third: extract. Don't build generic frameworks before real variation; don't copy-paste for the 4th time.438> 12. **Embedded utility logic not extracted to helpers** — inline paging loops (`while (hasMore) { skip += take; ... }`), ad-hoc datetime math, string parsing/formatting, collection partitioning, retry/backoff loops, URL/query-string building. If the algorithm is non-trivial AND stack-generic (not business-specific), extract to `util`/`helper`/`extensions` and let consumers call one line. Inline duplicates → duplicated bug surface.439> 13. **Logic in wrong (higher) layer — downshift to callee** — business/derivation logic written in the caller when the callee owns the data. Defaults: Controller code that should be App Service. App Service code that should be Domain Service or Entity. Component code that should be ViewModel/Store/Service. Caller reaching into callee's data shape to compute something → move the computation behind an intent-revealing method on the callee. Lowest responsible layer wins (Entity > Domain Service > App Service > Controller · Model/VM > Store > Component). Higher-layer placement = duplicated logic when a sibling caller needs the same thing.440> 14. **Owner owns the rule — extract on first write** — if a caller inlines logic that derives, normalizes, validates, or computes from another type's data, MOVE it to the owning type. Single use is sufficient — the trigger is wrong responsibility, not duplication. Sibling callers always arrive; inline copies drift silently with no compile error and no name to grep. **Common offenders:** _Backend_ — inlined rules in application-layer handlers / commands / queries / services / controllers that belong on the domain entity / value object / domain service. _Frontend_ — inlined derivations / formatting / validation in components that belong on the model / store / view-model / API service. **Fix:** name the rule once as a method (static or instance) on the owning type; callers invoke by name. Future variant → SECOND named method on the owner, never an inline near-duplicate. **Right responsibility first; reuse is the consequence.**441>442> **Extraction target — where the named rule lives:**443>444> | Shape of the rule | Goes to |445> | --------------------------------------------- | ----------------------------- |446> | Pure function over an entity's own data | static method on the entity |447> | Behavior that mutates / guards entity state | instance method on the entity |448> | Always-true invariant on a primitive value | value object constructor |449> | Needs DI (repo / settings / clock) | helper class registered in DI |450> | Domain-agnostic algorithm reused across types | util / extension method |451> | Pure shape / projection conversion | DTO mapping |452>453> **Pre-commit edit-site test (reject if answer is "many"):**454>455> | Change Scenario | Should touch |456> | ----------------------------------------------- | ------------------------- |457> | Add new variant (customer type, payment method) | 1 new file |458> | Change HTTP error response format | 1 middleware/filter |459> | Add timestamp field to every persisted entity | 1 base entity/interceptor |460> | Add authorization to a new endpoint | 1 declarative marker |461> | Swap database/ORM | Data layer only |462> | Change business calculation rule | 1 method on owning entity |463> | Add loading indicator pattern to forms | 1 base component/hook |464> | Add validation rule to a domain primitive | 1 value-object ctor |465> | Change paging/retry/datetime algorithm | 1 helper/util function |466> | Change a derivation of entity data | 1 method on the entity |467>468> **Operating heuristics:**469>470> - Write the call site first.471> - Count edit sites for plausible future change.472> - Prefer removing code over adding it.473> - Surface assumptions at boundaries, hide details inside.474> - **Pre-reuse scan** — before writing a non-trivial block, grep for similar algorithms (`while.*skip`, `DateTime.*Add`, `split`/`join` chains, paging loops, retry loops). Match existing helper → call it. None exists but pattern is stack-generic → extract to util before second caller appears.475> - **Layer placement test** — ask "if a sibling caller needed this tomorrow, would they re-derive it?" If yes, the logic is in the wrong layer. Move it down.476> - **Open-case-for-future-reuse** — if reviewer spots a block that is likely to appear in another feature (domain-agnostic algorithm, shared lifecycle, recurring derivation), do NOT rationalize with pure YAGNI. Either extract now (if cheap) or create a tracked TODO with the exact extraction target so the second caller does not duplicate silently. Silent duplication is the default failure mode.477> - When in doubt ask: "What would need to change if the requirement shifts?"478>479> **The measure of good code is the cost of change.** Not shortest. Not cleverest. Not most abstracted. Cheapest to safely modify having read a small local portion.480481<!-- /SYNC:complexity-prevention -->482483<!-- SYNC:severity-rubric -->484485> **Severity Rubric** — Classify every finding by consequence, not by how easy it is to fix. One scale across all reviews so a "High" means the same thing everywhere.486>487> | Severity | Action | Definition |488> | -------- | ----------- | ------------------------------------------------------------------------- |489> | CRITICAL | Block merge | Silent runtime failure, data corruption, validation bypass, security hole |490> | HIGH | Must fix | Incorrect behavior, invariant gap, architectural violation |491> | MEDIUM | Should fix | Design debt, maintainability, likely future bug |492> | LOW | Nice to fix | Convention, documentation, minor clarity |493>494> **Score-based skills** map their numeric scale onto these tiers — do not invent a parallel vocabulary:495>496> - **0-2 criterion scoring** (e.g. production-readiness-review): `0` = CRITICAL/HIGH (criterion unmet, blocks production readiness), `1` = MEDIUM (partial, should fix), `2` = pass (no finding).497> - **Two-axis scoring** (e.g. performance-review, impact × likelihood): map the resulting cell to the nearest tier — high-impact + high-likelihood → CRITICAL/HIGH; low-impact OR low-likelihood → MEDIUM/LOW.498>499> A finding's tier drives the gate: CRITICAL/HIGH must be resolved or explicitly accepted by the owner before PASS; MEDIUM/LOW may ship with a tracked follow-up.500501<!-- /SYNC:severity-rubric -->502503<!-- SYNC:evidence-based-reasoning:reminder -->504505- **MANDATORY IMPORTANT MUST ATTENTION** cite `file:line` evidence for every claim. Confidence >80% to act, <60% = do NOT recommend.506 <!-- /SYNC:evidence-based-reasoning:reminder -->507508<!-- SYNC:critical-thinking-mindset:reminder -->509510**MUST ATTENTION** apply critical + sequential thinking — every claim needs appropriate traced evidence (`file:line` for repo/code claims; source URL or artifact section for research, product, content, and docs claims); confidence >80% to act, <60% DO NOT recommend. Anti-hallucination: never present guess as fact, admit uncertainty freely, cross-reference independently, stay skeptical of own confidence.511512<!-- /SYNC:critical-thinking-mindset:reminder -->513514<!-- SYNC:complexity-prevention:reminder -->515516**MUST ATTENTION** apply complexity prevention — one business change = one code change. Flag change amplification (>3 edit sites for future change), scattered type-switches, anemic models, primitive obsession, leaked technology through abstractions, shallow modules, un-extracted utility logic (paging/datetime/string/retry → helpers), and logic in the wrong higher layer (downshift to callee/entity/VM). Don't rationalize silent duplication with pure YAGNI.517518<!-- /SYNC:complexity-prevention:reminder -->519520<!-- SYNC:ai-mistake-prevention:reminder -->521522**MUST ATTENTION** apply AI mistake prevention — verify generated content against evidence, trace downstream references before deleting or renaming, verify all affected outputs, re-read files after context loss, and surface ambiguity before acting.523524<!-- /SYNC:ai-mistake-prevention:reminder -->525526<!-- SYNC:severity-rubric:reminder -->527528- **MANDATORY** Classify findings Critical/High/Medium/Low by consequence; Critical/High block PASS until fixed or owner-accepted.529- **MANDATORY** Score-based skills (sre 0-2, perf two-axis) map onto the same four tiers — no parallel severity vocabulary.530531<!-- /SYNC:severity-rubric:reminder -->532533<!-- PROMPT-ENHANCE:STEP-TASK-CLOSING:START -->534535## Prompt-Enhance Closing Anchors536537**IMPORTANT MUST ATTENTION** follow declared step order for this skill; NEVER skip, reorder, or merge steps without explicit user approval538**IMPORTANT MUST ATTENTION** for every step/sub-skill call: set `in_progress` before execution, set `completed` after execution539**IMPORTANT MUST ATTENTION** every skipped step MUST include explicit reason; every completed step MUST include concise evidence540**IMPORTANT MUST ATTENTION** if Task tools unavailable, maintain an equivalent step-by-step plan tracker with synchronized statuses541542<!-- PROMPT-ENHANCE:STEP-TASK-CLOSING:END -->543544<!-- SYNC:parallel-subagent-dispatch -->545546> **Parallel Sub-Agent Dispatch** — Plan parallelism the moment a task breakdown exists, BEFORE executing it — running provably independent tasks sequentially wastes wall-clock. Applies to every multi-step job: workflow steps, planning, batch updates, investigation, research, scans, reviews, doc sync. **Plan execution is metadata-gated, NEVER default-parallel** — fan-out follows ONLY what the plan declares (`PAR`/`SEQ` tags + per-phase write set); an untagged plan runs sequentially — why: a derived write set cannot see cascade or generated writes.547>548> 1. **Tag every task `PAR` or `SEQ`.** `PAR` = inputs exclude every pending task's output AND write set disjoint from every other `PAR`. Else `SEQ` — MUST ATTENTION name the dependency forcing it.549> 2. **Group `PAR` into waves.** No edge between members. Two writers of one file NEVER share a wave. Read-only work (search, investigation, review, research) paral550551…(truncated)