openhermes 4.3.0 → 4.9.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.
- package/CONTEXT.md +9 -0
- package/README.md +26 -15
- package/bootstrap.ts +161 -124
- package/harness/agents/oh-browser.md +97 -0
- package/harness/agents/oh-builder.md +78 -0
- package/harness/agents/oh-facade.md +75 -0
- package/harness/agents/oh-fusion.md +45 -0
- package/harness/agents/oh-gauntlet.md +71 -0
- package/harness/agents/oh-grill.md +71 -0
- package/harness/agents/oh-investigate.md +60 -0
- package/harness/agents/oh-manifest.md +95 -0
- package/harness/agents/oh-plan-review.md +40 -0
- package/harness/agents/oh-planner.md +50 -0
- package/harness/agents/oh-refactor.md +37 -0
- package/harness/agents/oh-retro.md +46 -0
- package/harness/agents/oh-review.md +85 -0
- package/harness/agents/oh-security.md +83 -0
- package/harness/agents/oh-ship.md +76 -0
- package/harness/agents/oh-skill-craft.md +38 -0
- package/harness/agents/openhermes.md +107 -53
- package/harness/codex/AUTOPILOT.md +143 -91
- package/harness/codex/CHARTER.md +81 -0
- package/harness/commands/oh-doctor.md +193 -14
- package/harness/instructions/SHELL.md +76 -0
- package/harness/skills/oh-ascii/DEEP.md +292 -0
- package/harness/skills/oh-ascii/SKILL.md +31 -0
- package/harness/skills/oh-ascii/scripts/check_ascii_alignment.py +596 -0
- package/harness/skills/oh-browser/DEEP.md +54 -0
- package/harness/skills/oh-browser/SKILL.md +30 -0
- package/harness/skills/oh-builder/DEEP.md +63 -0
- package/harness/skills/oh-builder/SKILL.md +12 -90
- package/harness/skills/oh-expert/DEEP.md +85 -0
- package/harness/skills/oh-expert/SKILL.md +13 -106
- package/harness/skills/oh-facade/DEEP.md +182 -0
- package/harness/skills/oh-facade/SKILL.md +15 -279
- package/harness/skills/oh-freeze/DEEP.md +18 -0
- package/harness/skills/oh-freeze/SKILL.md +10 -19
- package/harness/skills/oh-full-output/DEEP.md +25 -0
- package/harness/skills/oh-full-output/SKILL.md +12 -65
- package/harness/skills/oh-fusion/DEEP.md +120 -0
- package/harness/skills/oh-fusion/SKILL.md +17 -295
- package/harness/skills/oh-gauntlet/DEEP.md +77 -0
- package/harness/skills/oh-gauntlet/SKILL.md +13 -105
- package/harness/skills/oh-grill/DEEP.md +51 -0
- package/harness/skills/oh-grill/SKILL.md +12 -63
- package/harness/skills/oh-guard/DEEP.md +19 -0
- package/harness/skills/oh-guard/SKILL.md +10 -24
- package/harness/skills/oh-handoff/DEEP.md +48 -0
- package/harness/skills/oh-handoff/SKILL.md +13 -23
- package/harness/skills/oh-health/DEEP.md +74 -0
- package/harness/skills/oh-health/SKILL.md +13 -76
- package/harness/skills/oh-init/DEEP.md +85 -0
- package/harness/skills/oh-init/SKILL.md +13 -127
- package/harness/skills/oh-investigate/DEEP.md +171 -0
- package/harness/skills/oh-investigate/SKILL.md +13 -66
- package/harness/skills/oh-issue/DEEP.md +21 -0
- package/harness/skills/oh-issue/SKILL.md +11 -27
- package/harness/skills/oh-learn/DEEP.md +44 -0
- package/harness/skills/oh-learn/SKILL.md +12 -83
- package/harness/skills/oh-manifest/DEEP.md +92 -0
- package/harness/skills/oh-manifest/SKILL.md +11 -108
- package/harness/skills/oh-plan-review/DEEP.md +90 -0
- package/harness/skills/oh-plan-review/SKILL.md +13 -115
- package/harness/skills/oh-planner/DEEP.md +172 -0
- package/harness/skills/oh-planner/SKILL.md +12 -149
- package/harness/skills/oh-prd/DEEP.md +45 -0
- package/harness/skills/oh-prd/SKILL.md +10 -26
- package/harness/skills/oh-refactor/DEEP.md +122 -0
- package/harness/skills/oh-refactor/SKILL.md +17 -410
- package/harness/skills/oh-retro/DEEP.md +26 -0
- package/harness/skills/oh-retro/SKILL.md +12 -24
- package/harness/skills/oh-review/DEEP.md +87 -0
- package/harness/skills/oh-review/SKILL.md +11 -97
- package/harness/skills/oh-security/DEEP.md +83 -0
- package/harness/skills/oh-security/SKILL.md +14 -96
- package/harness/skills/oh-ship/DEEP.md +141 -0
- package/harness/skills/oh-ship/SKILL.md +13 -31
- package/harness/skills/oh-skill-craft/DEEP.md +369 -0
- package/harness/skills/oh-skill-craft/SKILL.md +17 -178
- package/harness/skills/oh-skills-link/DEEP.md +16 -0
- package/harness/skills/oh-skills-link/SKILL.md +10 -20
- package/harness/skills/oh-skills-list/DEEP.md +20 -0
- package/harness/skills/oh-skills-list/SKILL.md +9 -22
- package/harness/skills/oh-triage/DEEP.md +23 -0
- package/harness/skills/oh-triage/SKILL.md +8 -24
- package/harness/skills/oh-worktree/DEEP.md +169 -0
- package/harness/skills/oh-worktree/SKILL.md +32 -0
- package/lib/harness-resolver.ts +8 -10
- package/package.json +5 -3
- package/scripts/count-tokens.mjs +158 -0
- package/scripts/oh-doctor.ps1 +342 -0
- package/harness/codex/CONSTITUTION.md +0 -73
- package/harness/codex/ROUTING.md +0 -92
- package/harness/instructions/RUNTIME.md +0 -30
- package/harness/skills/oh-caveman/SKILL.md +0 -42
- package/lib/logger.ts +0 -75
|
@@ -1,19 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: oh-planner
|
|
3
|
-
description: "
|
|
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
|
-
|
|
13
|
+
ALL-arounder planner: brainstorm, architecture analysis, structured plan, autoplan.
|
|
26
14
|
|
|
27
|
-
##
|
|
15
|
+
## Steps
|
|
28
16
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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-review — verify 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
|
|
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: "
|
|
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
|
-
|
|
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
|
-
##
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
|
43
|
-
| fail | → oh-grill
|
|
44
|
-
| blocker | → surface
|
|
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)
|