openhermes 4.3.0 → 4.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/CONTEXT.md +10 -1
  2. package/README.md +54 -42
  3. package/bootstrap.ts +396 -142
  4. package/harness/agents/oh-browser.md +97 -0
  5. package/harness/agents/oh-builder.md +78 -0
  6. package/harness/agents/oh-facade.md +75 -0
  7. package/harness/agents/oh-fusion.md +45 -0
  8. package/harness/agents/oh-gauntlet.md +71 -0
  9. package/harness/agents/oh-grill.md +71 -0
  10. package/harness/agents/oh-investigate.md +60 -0
  11. package/harness/agents/oh-manifest.md +95 -0
  12. package/harness/agents/oh-plan-review.md +40 -0
  13. package/harness/agents/oh-planner.md +50 -0
  14. package/harness/agents/oh-refactor.md +37 -0
  15. package/harness/agents/oh-retro.md +46 -0
  16. package/harness/agents/oh-review.md +85 -0
  17. package/harness/agents/oh-security.md +83 -0
  18. package/harness/agents/oh-ship.md +76 -0
  19. package/harness/agents/oh-skill-craft.md +38 -0
  20. package/harness/agents/openhermes.md +28 -73
  21. package/harness/codex/AUTOPILOT.md +235 -87
  22. package/harness/codex/CHARTER.md +80 -0
  23. package/harness/instructions/SHELL.md +76 -0
  24. package/harness/lib/background/background.test.ts +197 -0
  25. package/harness/lib/background/index.ts +7 -0
  26. package/harness/lib/background/interfaces.ts +31 -0
  27. package/harness/lib/background/manager.ts +320 -0
  28. package/harness/lib/composer/compose.test.ts +168 -0
  29. package/harness/lib/composer/compose.ts +65 -0
  30. package/harness/lib/composer/fragments/01-identity.md +1 -0
  31. package/harness/lib/composer/fragments/02-delegation.md +6 -0
  32. package/harness/lib/composer/fragments/03-permissions.md +13 -0
  33. package/harness/lib/composer/fragments/04-task-flow.md +15 -0
  34. package/harness/lib/composer/fragments/05-confidence.md +5 -0
  35. package/harness/lib/composer/fragments/06-parallelization.md +17 -0
  36. package/harness/lib/composer/fragments/07-shell.md +41 -0
  37. package/harness/lib/composer/fragments/08-routing.md +8 -0
  38. package/harness/lib/composer/fragments/09-guardrails.md +12 -0
  39. package/harness/lib/composer/index.ts +1 -0
  40. package/harness/lib/hooks/builtins/confidence-gate-hook.ts +70 -0
  41. package/harness/lib/hooks/builtins/delegation-depth-hook.ts +59 -0
  42. package/harness/lib/hooks/builtins/error-recovery-hook.ts +107 -0
  43. package/harness/lib/hooks/builtins/memory-sync-hook.ts +73 -0
  44. package/harness/lib/hooks/builtins/plan-check-hook.ts +43 -0
  45. package/harness/lib/hooks/builtins/route-tracking-hook.ts +147 -0
  46. package/harness/lib/hooks/builtins/sanity-check-hook.ts +52 -0
  47. package/harness/lib/hooks/builtins/shell-detect-hook.ts +96 -0
  48. package/harness/lib/hooks/hooks.test.ts +1016 -0
  49. package/harness/lib/hooks/index.ts +30 -0
  50. package/harness/lib/hooks/registry.ts +416 -0
  51. package/harness/lib/hooks/types.ts +71 -0
  52. package/harness/lib/memory/index.ts +18 -0
  53. package/harness/lib/memory/interfaces.ts +53 -0
  54. package/harness/lib/memory/memory-manager.ts +205 -0
  55. package/harness/lib/memory/memory.test.ts +491 -0
  56. package/harness/lib/memory/plan-store.ts +366 -0
  57. package/harness/lib/recovery/handler.ts +243 -0
  58. package/harness/lib/recovery/index.ts +14 -0
  59. package/harness/lib/recovery/interfaces.ts +48 -0
  60. package/harness/lib/recovery/patterns.ts +149 -0
  61. package/harness/lib/recovery/recovery.test.ts +312 -0
  62. package/harness/lib/sanity/anomaly-tracker.ts +127 -0
  63. package/harness/lib/sanity/checker.ts +178 -0
  64. package/harness/lib/sanity/index.ts +13 -0
  65. package/harness/lib/sanity/interfaces.ts +24 -0
  66. package/harness/lib/sanity/sanity.test.ts +472 -0
  67. package/harness/lib/sync/file-watcher.ts +174 -0
  68. package/harness/lib/sync/index.ts +11 -0
  69. package/harness/lib/sync/interfaces.ts +27 -0
  70. package/harness/lib/sync/plan-sync.ts +536 -0
  71. package/harness/lib/sync/sync.test.ts +832 -0
  72. package/harness/skills/oh-ascii/DEEP.md +292 -0
  73. package/harness/skills/oh-ascii/SKILL.md +31 -0
  74. package/harness/skills/oh-ascii/scripts/check_ascii_alignment.py +596 -0
  75. package/harness/skills/oh-browser/DEEP.md +54 -0
  76. package/harness/skills/oh-browser/SKILL.md +30 -0
  77. package/harness/skills/oh-builder/DEEP.md +63 -0
  78. package/harness/skills/oh-builder/SKILL.md +12 -90
  79. package/harness/skills/oh-expert/DEEP.md +85 -0
  80. package/harness/skills/oh-expert/SKILL.md +13 -106
  81. package/harness/skills/oh-facade/DEEP.md +182 -0
  82. package/harness/skills/oh-facade/SKILL.md +15 -279
  83. package/harness/skills/oh-freeze/DEEP.md +18 -0
  84. package/harness/skills/oh-freeze/SKILL.md +10 -19
  85. package/harness/skills/oh-full-output/DEEP.md +25 -0
  86. package/harness/skills/oh-full-output/SKILL.md +12 -65
  87. package/harness/skills/oh-fusion/DEEP.md +120 -0
  88. package/harness/skills/oh-fusion/SKILL.md +17 -295
  89. package/harness/skills/oh-gauntlet/DEEP.md +77 -0
  90. package/harness/skills/oh-gauntlet/SKILL.md +13 -105
  91. package/harness/skills/oh-grill/DEEP.md +51 -0
  92. package/harness/skills/oh-grill/SKILL.md +12 -63
  93. package/harness/skills/oh-guard/DEEP.md +19 -0
  94. package/harness/skills/oh-guard/SKILL.md +10 -24
  95. package/harness/skills/oh-handoff/DEEP.md +48 -0
  96. package/harness/skills/oh-handoff/SKILL.md +13 -23
  97. package/harness/skills/oh-health/DEEP.md +74 -0
  98. package/harness/skills/oh-health/SKILL.md +13 -76
  99. package/harness/skills/oh-init/DEEP.md +85 -0
  100. package/harness/skills/oh-init/SKILL.md +13 -127
  101. package/harness/skills/oh-investigate/DEEP.md +171 -0
  102. package/harness/skills/oh-investigate/SKILL.md +13 -66
  103. package/harness/skills/oh-issue/DEEP.md +21 -0
  104. package/harness/skills/oh-issue/SKILL.md +11 -27
  105. package/harness/skills/oh-manifest/DEEP.md +92 -0
  106. package/harness/skills/oh-manifest/SKILL.md +12 -109
  107. package/harness/skills/oh-plan-review/DEEP.md +90 -0
  108. package/harness/skills/oh-plan-review/SKILL.md +13 -115
  109. package/harness/skills/oh-planner/DEEP.md +172 -0
  110. package/harness/skills/oh-planner/SKILL.md +12 -149
  111. package/harness/skills/oh-prd/DEEP.md +45 -0
  112. package/harness/skills/oh-prd/SKILL.md +10 -26
  113. package/harness/skills/oh-refactor/DEEP.md +122 -0
  114. package/harness/skills/oh-refactor/SKILL.md +17 -410
  115. package/harness/skills/oh-retro/DEEP.md +26 -0
  116. package/harness/skills/oh-retro/SKILL.md +12 -24
  117. package/harness/skills/oh-review/DEEP.md +87 -0
  118. package/harness/skills/oh-review/SKILL.md +11 -97
  119. package/harness/skills/oh-security/DEEP.md +83 -0
  120. package/harness/skills/oh-security/SKILL.md +14 -96
  121. package/harness/skills/oh-ship/DEEP.md +141 -0
  122. package/harness/skills/oh-ship/SKILL.md +14 -32
  123. package/harness/skills/oh-skill-craft/DEEP.md +369 -0
  124. package/harness/skills/oh-skill-craft/SKILL.md +13 -177
  125. package/harness/skills/oh-skills-link/DEEP.md +16 -0
  126. package/harness/skills/oh-skills-link/SKILL.md +10 -20
  127. package/harness/skills/oh-skills-list/DEEP.md +20 -0
  128. package/harness/skills/oh-skills-list/SKILL.md +9 -22
  129. package/harness/skills/oh-triage/DEEP.md +23 -0
  130. package/harness/skills/oh-triage/SKILL.md +8 -24
  131. package/harness/skills/oh-worktree/DEEP.md +169 -0
  132. package/harness/skills/oh-worktree/SKILL.md +32 -0
  133. package/lib/harness-resolver.ts +8 -10
  134. package/package.json +7 -5
  135. package/tsconfig.json +1 -1
  136. package/harness/codex/CONSTITUTION.md +0 -73
  137. package/harness/codex/ROUTING.md +0 -92
  138. package/harness/commands/oh-doctor.md +0 -26
  139. package/harness/commands/oh-log.md +0 -18
  140. package/harness/instructions/RUNTIME.md +0 -30
  141. package/harness/skills/oh-caveman/SKILL.md +0 -42
  142. package/harness/skills/oh-learn/SKILL.md +0 -101
  143. package/lib/logger.ts +0 -75
@@ -1,19 +1,7 @@
1
1
  ---
2
2
  name: oh-planner
3
- description: "ALL-arounder planner brainstorm, architect, autoplan, decision pipeline. Produces a consumable plan artifact."
3
+ description: "Use when a feature, architecture, or idea needs structured planning — from brainstorming through formal plan artifact. Produces consumable plan documents."
4
4
  tier: 3
5
- benefits-from: [oh-expert, oh-grill]
6
- triggers:
7
- - "plan this"
8
- - "how should I build"
9
- - "plan the architecture for"
10
- - "design this feature"
11
- - "brainstorm"
12
- - "autoplan"
13
- - "strategy for this feature"
14
- - "scope this feature"
15
- - "create a plan for"
16
- - "whats the plan for"
17
5
  route:
18
6
  pass: oh-grill
19
7
  fail: oh-planner
@@ -22,143 +10,18 @@ route:
22
10
 
23
11
  # oh-planner
24
12
 
25
- The ALL-arounder planner. Merges brainstorm, architecture analysis, strategy review, and automatic plan review into one skill. Produces a plan file in OpenCode's canonical storage at `~/.local/share/opencode/openhermes/plans/`.
13
+ ALL-arounder planner: brainstorm, architecture analysis, structured plan, autoplan.
26
14
 
27
- ## Entry Modes
15
+ ## Steps
28
16
 
29
- Use the mode that matches the user's starting point:
30
-
31
- ### Mode A: Brainstorm (exploratory)
32
- When the idea is fuzzy and needs shaping.
33
-
34
- 1. **Demand reality**who specifically needs this?
35
- 2. **Status quo**what do they do today?
36
- 3. **Desperate specificity** what's the one concrete thing they can't do?
37
- 4. **Narrowest wedge** — what's the smallest useful version?
38
- 5. **Observation** — what will you see/hear when it works?
39
- 6. **Future-fit** — does this compound or plateau?
40
-
41
- Output: structured design doc.
42
-
43
- ### Mode B: Architecture Analysis (existing codebase)
44
- When the codebase feels messy or you need to understand the surface.
45
-
46
- 1. **Read the domain** — load CONTEXT.md, understand the language
47
- 2. **Map the surface** — identify modules, boundaries, dependencies
48
- 3. **Find deepening opportunities** — duplication, over-coupling, grown-beyond-purpose functions, missing abstractions
49
- 4. **Rank by impact** — effort vs value, dependencies, risk
50
-
51
- Output: ranked refactoring candidates.
52
-
53
- ### Mode C: Structured Plan (non-trivial feature)
54
- When requirements exist but need a plan document.
55
-
56
- 1. **Scope challenge** — before reviewing anything, answer:
57
- - What existing code already partially solves each sub-problem?
58
- - What is the minimum set of changes that achieves the stated goal?
59
- - **Complexity check:** 8+ files or 2+ new classes/services in a single phase → smell. Propose splitting or simplifying.
60
- - **Search check:** for each architectural pattern or infrastructure component the plan introduces, check whether the runtime/framework has a built-in. Search for: `{framework} {pattern} built-in`. Flag custom solutions where built-ins exist.
61
- - **Completeness check:** with AI-assisted coding, completeness is 10-100x cheaper than with human teams. If the plan shortcuts something to save human hours that only saves minutes with AI, recommend the complete version.
62
- 2. **Strategy review** — challenge premises, identify scope decisions, consider 10x alternatives
63
- 3. **Architecture review** — data flow, component boundaries, API surface, state model
64
- 4. **Edge case analysis** — error states, concurrency, failure modes, security implications
65
- 5. **Dependency mapping** — what blocks what, parallelizable work
66
- 6. **Write plan.md** — structured artifact with phases, deps, verification steps
67
-
68
- ### Mode D: Autoplan (plan exists, needs full review)
69
- When a plan file exists and needs the full gauntlet. Auto-decides 90% of questions using decision principles. Surfaces only taste decisions at a final approval gate.
70
-
71
- Runs in order: **Strategy → Architecture → Design → Engineering → DX**
72
- Each phase must complete before the next begins.
73
-
74
- ## Decision Principles
75
-
76
- Use these to auto-resolve intermediate questions. Only surface to the user when options are genuinely close (taste decisions):
77
-
78
- 1. **Completeness over cleverness** — Choose the option that covers more cases
79
- 2. **Boil the lake** — Fix the blast radius, not the symptom
80
- 3. **Pragmatic over perfect** — Cleaner option that ships today wins
81
- 4. **DRY but not premature** — Reuse over rebuild, but don't abstract before the third instance
82
- 5. **Explicit over implicit** — Clear code over magic
83
- 6. **Bias toward action** — When in doubt, make progress
84
-
85
- Never auto-decide: premises (need human judgment) or cases where both the plan and the alternative have strong arguments.
86
-
87
- ## Plan Artifact
88
-
89
- Output goes in `~/.local/share/opencode/openhermes/plans/<project-name>-plan-<nnn>.md` (OpenCode's canonical storage, sequence-numbered per-session/project). The plan ID matches the filename.
90
-
91
- ```markdown
92
- # PLAN: <project-name>
93
-
94
- Plan ID: <project-name>-plan-<nnn>
95
- Project: <project-name>
96
- Status: active
97
- Created: <local-date-time>
98
- Updated: <local-date-time>
99
- Project Path: <absolute-project-path>
100
- Plan Path: ~/.local/share/opencode/openhermes/plans/<project-name>-plan-<nnn>.md
101
- Objective: <short objective>
102
-
103
- ## Current State
104
-
105
- <what exists now, what's missing>
106
-
107
- ## Assumptions
108
-
109
- - <assumption 1>
110
- - <assumption 2>
111
-
112
- ## Tasks
113
-
114
- - [ ] Task 1
115
- - [ ] Subtask 1.1
116
-
117
- ## Active Task
118
-
119
- <what's being worked on now>
120
-
121
- ## Subagents
122
-
123
- | Agent | Purpose | Status | Findings |
124
- |---|---|---|---|
125
-
126
- ## Completed
127
-
128
- - <what's done>
129
-
130
- ## Work Log
131
-
132
- <timestamped entries for subagent handoffs>
133
-
134
- ## Blockers
135
-
136
- - None
137
-
138
- ## Validation
139
-
140
- - [ ] Static checks
141
- - [ ] Unit tests
142
- - [ ] Manual verification
143
-
144
- ## Decisions
145
-
146
- - <decision> — <rationale>
147
-
148
- ## Notes
149
-
150
- <anything else>
151
- ```
152
-
153
- The plan file is self-contained — Tasks, Completed, Subagents, and Work Log sections eliminate the need for separate todo.md or work-log.md files.
154
-
155
- ## Anti-patterns
156
- - Skipping strategy review for complex features (architecture mistakes compound)
157
- - Plans at wrong granularity — too vague to execute or too detailed to read
158
- - Re-opening already-decided debates ("what if we rewrite in Rust?")
159
- - Perfect being the enemy of shipped (progress > polish)
160
- - Failing to flag taste decisions to the user
161
- - Big bang rewrites — plan increments, not overhauls
17
+ 1. Determine mode Mode A (brainstorm vague idea), Mode B (architecture analysis), Mode C (structured plan), or Mode D (autoplan).
18
+ 2. Clarify scope — ask 6 clarifying questions (who needs this, current workflow, capability gap, smallest version, success signals, compound vs plateau).
19
+ 3. Map code surface if applicable — module boundaries, dependencies, find deepening opportunities, rank by effort/value/risk.
20
+ 4. Challenge premises check existing code reuse, minimum changes, complexity (8+ files = smell), framework built-ins, completeness.
21
+ 5. Analyze architecture — data flow, component boundaries, API surface, state model, edge cases (error, concurrency, failure, security), dependency mapping.
22
+ 6. Write structured plan canonical format: Current State, Assumptions, Tasks, Active Task, Subagents, Completed, Work Log, Blockers, Validation, Decisions, Notes.
23
+ 7. Apply auto-resolution principles completeness over cleverness, boil the lake, pragmatic over perfect, DRY at 3rd, explicit over implicit, bias toward action. Surface taste decisions.
24
+ 8. Self-reviewverify spec coverage, scan for placeholders (TBD/TODO), check type consistency across tasks.
162
25
 
163
26
  ## Routing
164
27
 
@@ -166,4 +29,4 @@ The plan file is self-contained — Tasks, Completed, Subagents, and Work Log se
166
29
  |---------|-------|
167
30
  | pass | → oh-grill (stress-test plan) |
168
31
  | fail | → oh-planner (revise gaps) |
169
- | blocker | → surface to user |
32
+ | blocker | → surface |
@@ -0,0 +1,45 @@
1
+ # oh-prd — Deep Reference
2
+
3
+ ## When to Use
4
+
5
+ Conversation has defined a feature well enough to write requirements. Produces a structured PRD and publishes as a GitHub issue.
6
+
7
+ Triggers: write a prd, product requirements, spec this feature, feature spec.
8
+
9
+ ## PRD Structure
10
+
11
+ ```markdown
12
+ # PRD: <feature name>
13
+
14
+ ## Problem Statement
15
+ <what problem does this solve, for whom>
16
+
17
+ ## Success Criteria
18
+ <measurable outcomes>
19
+
20
+ ## Scope
21
+ ### In scope
22
+ - <features included>
23
+
24
+ ### Out of scope
25
+ - <explicitly excluded — prevents scope creep>
26
+
27
+ ## Requirements
28
+ ### Functional
29
+ - <numbered behaviors>
30
+
31
+ ### Non-functional
32
+ - <performance, security, accessibility, observability>
33
+
34
+ ## Open Questions
35
+ - <unresolved items>
36
+
37
+ ## Dependencies
38
+ - <what must exist first>
39
+ ```
40
+
41
+ ## Anti-patterns
42
+
43
+ - Specifying solutions instead of problems (let the builder decide how)
44
+ - Too detailed (PRD sets direction, not implementation)
45
+ - No open questions section (there are always open questions)
@@ -1,11 +1,7 @@
1
1
  ---
2
2
  name: oh-prd
3
- description: "Turn conversation context into a PRD and publish as GitHub issue"
3
+ description: "Write structured PRDs from conversation context and publish as issues"
4
4
  tier: 2
5
- triggers:
6
- - "write a prd"
7
- - "product requirements"
8
- - "prd for"
9
5
  route:
10
6
  pass: oh-issue
11
7
  fail: oh-grill
@@ -14,31 +10,19 @@ route:
14
10
 
15
11
  # oh-prd
16
12
 
17
- ## When to Use
18
- When a feature discussion has produced enough context to write a product requirements document. Captures the decision tree and outputs a structured issue.
13
+ Turn conversation context into a structured PRD and publish as a GitHub issue.
19
14
 
20
- ## Workflow
21
- 1. Extract requirements from conversation history
22
- 2. Structure into PRD format: problem statement, target users, requirements (must/should/could), out of scope
23
- 3. Create as GitHub issue with `gh issue create`
24
- 4. Add triage label for prioritisation
15
+ ## Steps
25
16
 
26
- ## PRD Structure
27
- - **Problem** what problem does this solve?
28
- - **Target users** who benefits?
29
- - **Requirements** must have / should have / could have
30
- - **Out of scope** — explicitly what's NOT included
31
- - **Success metrics** — how will we know it works?
32
-
33
- ## Anti-patterns
34
- - Writing PRD before understanding the problem
35
- - Requirements that aren't testable ("fast" vs "loads in <200ms")
36
- - Gold-plating — every feature is "must have"
17
+ 1. Extract requirements from conversation context
18
+ 2. Structure into formal PRD format
19
+ 3. Surface open questions to user
20
+ 4. Publish as GitHub issue via `gh issue create`
37
21
 
38
22
  ## Routing
39
23
 
40
24
  | Outcome | Route |
41
25
  |---------|-------|
42
- | pass | → oh-issue (break PRD into actionable issues) |
43
- | fail | → oh-grill (stress-test unclear requirements) |
44
- | blocker | → surface to user |
26
+ | pass | → oh-issue |
27
+ | fail | → oh-grill |
28
+ | blocker | → surface |
@@ -0,0 +1,122 @@
1
+ # oh-refactor — Deep Reference
2
+
3
+ ## Golden Rules
4
+
5
+ 1. **Behavior preserved** — changes structure only. Changing behavior = feature, not refactor.
6
+ 2. **Small steps** — one change, verify, commit, repeat. Never batch.
7
+ 3. **Tests essential** — no safety net = editing blind. Write characterization tests first.
8
+ 4. **One thing per commit** — never mix refactoring with feature work.
9
+ 5. **Commit between safe states** — commit before starting, after each green run.
10
+
11
+ ## When NOT to Refactor
12
+ - Code that works and will never change
13
+ - Critical production code without tests (add tests first)
14
+ - Tight deadline with no test safety net
15
+ - "Just because" — every refactor needs a clear purpose
16
+
17
+ ## Workflow
18
+
19
+ ### Phase 1: Prepare
20
+ Check test coverage (thin → write characterization tests). Commit current state. Create feature branch.
21
+
22
+ ### Phase 2: Identify
23
+ Find code smell. Understand what code does. Plan smallest fix. If behavior unclear → delegate to oh-investigate.
24
+
25
+ ### Phase 3: Refactor (small steps)
26
+ One change → run tests → commit → repeat until smell is gone.
27
+
28
+ ### Phase 4: Verify
29
+ All tests pass. Manual smoke test if coverage missing. Performance unchanged or better. Diff shows structural changes only.
30
+
31
+ ### Phase 5: Clean Up
32
+ Remove commented-out code, stale imports, dead paths. Update docs only if semantics changed. Final commit.
33
+
34
+ ## Common Code Smells & Fixes (top 6)
35
+
36
+ ### 1. Long Method
37
+ ```diff
38
+ - async function processOrder(orderId) {
39
+ - // 50 lines: fetch // 30 lines: validate
40
+ - // 40 lines: pricing // 30 lines: inventory
41
+ - // 20 lines: shipment // 30 lines: notifications
42
+ - }
43
+ + async function processOrder(orderId) {
44
+ + const order = await fetchOrder(orderId);
45
+ + validateOrder(order);
46
+ + const pricing = calculatePricing(order);
47
+ + const shipment = await createShipment(order);
48
+ + await sendNotifications(order, pricing, shipment);
49
+ + return { order, pricing, shipment };
50
+ + }
51
+ ```
52
+
53
+ ### 2. Guard Clauses (Arrow Code)
54
+ ```diff
55
+ - if (order) { if (order.user) { if (order.total > 0) { return processOrder(order); }}}
56
+ + if (!order) return { error: 'No order' };
57
+ + if (!order.user) return { error: 'No user' };
58
+ + if (order.total <= 0) return { error: 'Invalid total' };
59
+ + return processOrder(order);
60
+ ```
61
+
62
+ ### 3. Duplicated Code
63
+ ```diff
64
+ - function calculateUserDiscount(user) { if (user.membership === 'gold') return user.total * 0.2; }
65
+ - function calculateOrderDiscount(order) { if (order.user.membership === 'gold') return order.total * 0.2; }
66
+ + function getDiscountRate(membership) { return { gold: 0.2, silver: 0.1 }[membership] || 0; }
67
+ ```
68
+
69
+ ### 4. Magic Numbers → Constants
70
+ ```diff
71
+ - if (user.status === 2) { }
72
+ - const discount = total * 0.15;
73
+ + const UserStatus = { ACTIVE: 1, INACTIVE: 2 } as const;
74
+ + const DISCOUNT_RATES = { PREMIUM: 0.15, VIP: 0.2 } as const;
75
+ ```
76
+
77
+ ### 5. Primitive Obsession
78
+ ```diff
79
+ - function sendEmail(to, subject, body) { }
80
+ - sendEmail('user@example.com', 'Hello', '...');
81
+ + class Email {
82
+ + private constructor(public readonly value: string) {
83
+ + if (!Email.isValid(value)) throw new Error('Invalid email');
84
+ + }
85
+ + static create(v: string) { return new Email(v); }
86
+ + static isValid(e: string) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e); }
87
+ + }
88
+ ```
89
+
90
+ ### 6. Feature Envy
91
+ ```diff
92
+ - class Order { calculateDiscount(user) { if (user.membershipLevel === 'gold') return this.total * 0.2; } }
93
+ + class User { getDiscountRate(orderTotal) { const rates = { gold: 0.2, silver: 0.1 }; return rates[this.membershipLevel] || 0; } }
94
+ ```
95
+
96
+ ## Common Operations Reference
97
+
98
+ | Operation | What it does |
99
+ |-----------|-------------|
100
+ | Extract Method | Turn fragment into named function |
101
+ | Extract Class | Move related behavior to new class |
102
+ | Rename Method/Variable | Improve clarity |
103
+ | Introduce Parameter Object | Group related params |
104
+ | Guard Clauses | Early returns flatten nesting |
105
+ | Replace Magic Number | Named constants for literals |
106
+ | Consolidate Conditional | Combine duplicate conditions |
107
+
108
+ ## Checklist
109
+
110
+ - [ ] Functions small (< 50 lines), single-purpose
111
+ - [ ] No duplicated code
112
+ - [ ] Descriptive names, no magic values
113
+ - [ ] No dead code, stale imports, commented-out code
114
+ - [ ] Clear module boundaries, no circular deps
115
+ - [ ] Types for all public APIs, no `any` without justification
116
+ - [ ] All tests pass, edge cases covered
117
+
118
+ ## Anti-patterns
119
+ - Refactoring without tests (behavior preservation is unverifiable)
120
+ - Mixing behavior changes with refactoring
121
+ - "While I'm here" scope creep
122
+ - Large batch refactors (commit between safe states)