lithermes-ai 0.5.0

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 (133) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +245 -0
  3. package/README_Ko-KR.md +245 -0
  4. package/assets/lithermes-plugin/NOTICE.md +37 -0
  5. package/assets/lithermes-plugin/README.md +40 -0
  6. package/assets/lithermes-plugin/__init__.py +179 -0
  7. package/assets/lithermes-plugin/core.py +853 -0
  8. package/assets/lithermes-plugin/litgoal/__init__.py +10 -0
  9. package/assets/lithermes-plugin/litgoal/cli.py +133 -0
  10. package/assets/lithermes-plugin/litgoal/hook.py +48 -0
  11. package/assets/lithermes-plugin/litgoal/model.py +171 -0
  12. package/assets/lithermes-plugin/litgoal/runtime.py +273 -0
  13. package/assets/lithermes-plugin/litgoal/store.py +93 -0
  14. package/assets/lithermes-plugin/litgoal/tools.py +228 -0
  15. package/assets/lithermes-plugin/payload-version.json +471 -0
  16. package/assets/lithermes-plugin/plugin.yaml +9 -0
  17. package/assets/lithermes-plugin/skills/ai-slop-remover/SKILL.md +142 -0
  18. package/assets/lithermes-plugin/skills/comment-checker/SKILL.md +50 -0
  19. package/assets/lithermes-plugin/skills/debugging/SKILL.md +116 -0
  20. package/assets/lithermes-plugin/skills/debugging/references/methodology/00-setup.md +108 -0
  21. package/assets/lithermes-plugin/skills/debugging/references/methodology/02-investigate.md +121 -0
  22. package/assets/lithermes-plugin/skills/debugging/references/methodology/04-oracle-triple.md +136 -0
  23. package/assets/lithermes-plugin/skills/debugging/references/methodology/05-escalate.md +69 -0
  24. package/assets/lithermes-plugin/skills/debugging/references/methodology/06-fix.md +116 -0
  25. package/assets/lithermes-plugin/skills/debugging/references/methodology/08-qa.md +94 -0
  26. package/assets/lithermes-plugin/skills/debugging/references/methodology/09-cleanup.md +164 -0
  27. package/assets/lithermes-plugin/skills/debugging/references/methodology/partial-runtime-evidence.md +229 -0
  28. package/assets/lithermes-plugin/skills/debugging/references/runtimes/bundled-js-binary.md +415 -0
  29. package/assets/lithermes-plugin/skills/debugging/references/runtimes/go.md +252 -0
  30. package/assets/lithermes-plugin/skills/debugging/references/runtimes/native-binary.md +484 -0
  31. package/assets/lithermes-plugin/skills/debugging/references/runtimes/node.md +260 -0
  32. package/assets/lithermes-plugin/skills/debugging/references/runtimes/python.md +248 -0
  33. package/assets/lithermes-plugin/skills/debugging/references/runtimes/rust.md +234 -0
  34. package/assets/lithermes-plugin/skills/debugging/references/tools/ghidra.md +212 -0
  35. package/assets/lithermes-plugin/skills/debugging/references/tools/playwright-cli.md +194 -0
  36. package/assets/lithermes-plugin/skills/debugging/references/tools/pwndbg.md +263 -0
  37. package/assets/lithermes-plugin/skills/debugging/references/tools/pwntools.md +265 -0
  38. package/assets/lithermes-plugin/skills/frontend-ui-ux/SKILL.md +77 -0
  39. package/assets/lithermes-plugin/skills/lit-plan/SKILL.md +374 -0
  40. package/assets/lithermes-plugin/skills/litgoal/.gitkeep +0 -0
  41. package/assets/lithermes-plugin/skills/litgoal/SKILL.md +207 -0
  42. package/assets/lithermes-plugin/skills/litwork/SKILL.md +262 -0
  43. package/assets/lithermes-plugin/skills/lsp/SKILL.md +53 -0
  44. package/assets/lithermes-plugin/skills/programming/SKILL.md +463 -0
  45. package/assets/lithermes-plugin/skills/programming/references/go/README.md +90 -0
  46. package/assets/lithermes-plugin/skills/programming/references/go/backend-stack.md +641 -0
  47. package/assets/lithermes-plugin/skills/programming/references/go/bootstrap.md +328 -0
  48. package/assets/lithermes-plugin/skills/programming/references/go/bubbletea-v2.md +360 -0
  49. package/assets/lithermes-plugin/skills/programming/references/go/cobra-stack.md +468 -0
  50. package/assets/lithermes-plugin/skills/programming/references/go/concurrency.md +362 -0
  51. package/assets/lithermes-plugin/skills/programming/references/go/data-modeling.md +329 -0
  52. package/assets/lithermes-plugin/skills/programming/references/go/error-handling.md +359 -0
  53. package/assets/lithermes-plugin/skills/programming/references/go/golangci-strict.md +236 -0
  54. package/assets/lithermes-plugin/skills/programming/references/go/grpc-connect.md +375 -0
  55. package/assets/lithermes-plugin/skills/programming/references/go/libraries.md +337 -0
  56. package/assets/lithermes-plugin/skills/programming/references/go/one-liners.md +202 -0
  57. package/assets/lithermes-plugin/skills/programming/references/go/sqlc-pgx.md +471 -0
  58. package/assets/lithermes-plugin/skills/programming/references/go/testing.md +467 -0
  59. package/assets/lithermes-plugin/skills/programming/references/go/type-patterns.md +298 -0
  60. package/assets/lithermes-plugin/skills/programming/references/python/README.md +314 -0
  61. package/assets/lithermes-plugin/skills/programming/references/python/async-anyio.md +442 -0
  62. package/assets/lithermes-plugin/skills/programming/references/python/data-modeling.md +233 -0
  63. package/assets/lithermes-plugin/skills/programming/references/python/data-processing.md +133 -0
  64. package/assets/lithermes-plugin/skills/programming/references/python/error-handling.md +218 -0
  65. package/assets/lithermes-plugin/skills/programming/references/python/fastapi-stack.md +316 -0
  66. package/assets/lithermes-plugin/skills/programming/references/python/httpx2-optimization.md +360 -0
  67. package/assets/lithermes-plugin/skills/programming/references/python/libraries.md +307 -0
  68. package/assets/lithermes-plugin/skills/programming/references/python/one-liners.md +268 -0
  69. package/assets/lithermes-plugin/skills/programming/references/python/orjson-stack.md +378 -0
  70. package/assets/lithermes-plugin/skills/programming/references/python/pydantic-ai.md +285 -0
  71. package/assets/lithermes-plugin/skills/programming/references/python/pyproject-strict.md +232 -0
  72. package/assets/lithermes-plugin/skills/programming/references/python/textual-tui.md +201 -0
  73. package/assets/lithermes-plugin/skills/programming/references/python/type-patterns.md +176 -0
  74. package/assets/lithermes-plugin/skills/programming/references/rust/README.md +317 -0
  75. package/assets/lithermes-plugin/skills/programming/references/rust/async-tokio.md +299 -0
  76. package/assets/lithermes-plugin/skills/programming/references/rust/axum-stack.md +467 -0
  77. package/assets/lithermes-plugin/skills/programming/references/rust/cargo-strict.md +317 -0
  78. package/assets/lithermes-plugin/skills/programming/references/rust/clap-stack.md +409 -0
  79. package/assets/lithermes-plugin/skills/programming/references/rust/concurrency.md +375 -0
  80. package/assets/lithermes-plugin/skills/programming/references/rust/libraries.md +439 -0
  81. package/assets/lithermes-plugin/skills/programming/references/rust/one-liners.md +291 -0
  82. package/assets/lithermes-plugin/skills/programming/references/rust/proptest-insta.md +429 -0
  83. package/assets/lithermes-plugin/skills/programming/references/rust/type-state.md +354 -0
  84. package/assets/lithermes-plugin/skills/programming/references/rust/unsafe-discipline.md +250 -0
  85. package/assets/lithermes-plugin/skills/programming/references/rust/zero-cost-safety.md +527 -0
  86. package/assets/lithermes-plugin/skills/programming/references/rust-ub/README.md +289 -0
  87. package/assets/lithermes-plugin/skills/programming/references/rust-ub/miri-sanitizers-loom.md +411 -0
  88. package/assets/lithermes-plugin/skills/programming/references/rust-ub/ub-taxonomy.md +269 -0
  89. package/assets/lithermes-plugin/skills/programming/references/typescript/README.md +195 -0
  90. package/assets/lithermes-plugin/skills/programming/references/typescript/backend-hono.md +672 -0
  91. package/assets/lithermes-plugin/skills/programming/references/typescript/bootstrap.md +199 -0
  92. package/assets/lithermes-plugin/skills/programming/references/typescript/data-modeling.md +202 -0
  93. package/assets/lithermes-plugin/skills/programming/references/typescript/error-handling.md +169 -0
  94. package/assets/lithermes-plugin/skills/programming/references/typescript/tsconfig-strict.md +152 -0
  95. package/assets/lithermes-plugin/skills/programming/references/typescript/type-patterns.md +196 -0
  96. package/assets/lithermes-plugin/skills/programming/scripts/go/check-no-excuse-rules.sh +173 -0
  97. package/assets/lithermes-plugin/skills/programming/scripts/go/new-project.py +138 -0
  98. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/.editorconfig +13 -0
  99. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/.golangci.yml +95 -0
  100. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/AGENTS.md.tmpl +24 -0
  101. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/README.md.tmpl +12 -0
  102. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/Taskfile.yml +40 -0
  103. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/ci.yml +37 -0
  104. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/config.go +24 -0
  105. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/gitignore +15 -0
  106. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/main.go.tmpl +22 -0
  107. package/assets/lithermes-plugin/skills/programming/scripts/go/templates/run.go +15 -0
  108. package/assets/lithermes-plugin/skills/programming/scripts/python/check-no-excuse-rules.py +687 -0
  109. package/assets/lithermes-plugin/skills/programming/scripts/python/new-project.py +172 -0
  110. package/assets/lithermes-plugin/skills/programming/scripts/python/new-script.py +116 -0
  111. package/assets/lithermes-plugin/skills/programming/scripts/rust/check-no-excuse-rules.py +296 -0
  112. package/assets/lithermes-plugin/skills/programming/scripts/rust/check-no-excuse-rules.sh +158 -0
  113. package/assets/lithermes-plugin/skills/programming/scripts/rust/new-project.py +175 -0
  114. package/assets/lithermes-plugin/skills/programming/scripts/typescript/check-no-excuse-rules.ts +282 -0
  115. package/assets/lithermes-plugin/skills/programming/scripts/typescript/new-project.ts +177 -0
  116. package/assets/lithermes-plugin/skills/refactor/SKILL.md +770 -0
  117. package/assets/lithermes-plugin/skills/remove-ai-slops/SKILL.md +335 -0
  118. package/assets/lithermes-plugin/skills/review-work/SKILL.md +562 -0
  119. package/assets/lithermes-plugin/skills/rules/SKILL.md +41 -0
  120. package/assets/lithermes-plugin/skills/start-work/SKILL.md +332 -0
  121. package/bin/lithermes.js +8 -0
  122. package/cover.png +0 -0
  123. package/package.json +39 -0
  124. package/src/cli.js +129 -0
  125. package/src/lib/check.js +94 -0
  126. package/src/lib/config.js +170 -0
  127. package/src/lib/files.js +65 -0
  128. package/src/lib/hermesDiscovery.js +50 -0
  129. package/src/lib/hud.js +121 -0
  130. package/src/lib/install.js +159 -0
  131. package/src/lib/patch.js +153 -0
  132. package/src/lib/skins.js +113 -0
  133. package/src/lib/spinner.js +104 -0
@@ -0,0 +1,770 @@
1
+ ---
2
+ name: refactor
3
+ description: "Intelligent refactor command. Triggers: refactor, refactoring, cleanup, restructure, extract, simplify, modernize."
4
+ ---
5
+
6
+ ## LitHermes Hermes-native execution (authoritative — overrides any harness examples below)
7
+
8
+ In Hermes there is **one** way to fan out refactor lanes: the native **`delegate_task`**
9
+ tool. It takes a batch of child tasks — `delegate_task(tasks:[{goal, context, toolsets?, role?}])`
10
+ — that run in **parallel**; the parent blocks until every child returns, then receives all
11
+ results together. Roles are `leaf` or `orchestrator`. There is no single-agent spawn
12
+ primitive, no `task()`, no background-output polling call, no named-agent registry, and no
13
+ per-child model selection. The prose and code blocks further down were copied from a
14
+ different harness and use foreign delegation names; translate every one of them to a
15
+ `delegate_task` child whose `goal`/`context` **inline** the role's full mandate.
16
+
17
+ | Legacy prose says | Do this in Hermes |
18
+ | --- | --- |
19
+ | "fire N explore agents in background" | call `delegate_task` **once** with a batch of N children (they run in parallel) |
20
+ | any "call the explore agent" / `subagent_type="explore"` call | a `delegate_task` child, `role:"leaf"`, whose `goal` is "codebase-search role: find …" and `context` carries the target/diff inline |
21
+ | any "call the librarian agent" call | a `delegate_task` child, `role:"leaf"`, `goal` "external-research role: find the modern API replacement for …" |
22
+ | any `subagent_type="plan"` call | a `delegate_task` child, `role:"leaf"`, `goal` "plan-generation role: produce the atomic refactor plan …" with the codemap inlined in `context` |
23
+ | any `subagent_type="oracle"` verification call | a `delegate_task` child, `role:"leaf"`, `goal` "plan-review / verification role: confirm step <N> introduced no regression …" |
24
+ | any background-output / wait-for-agent poll | nothing to call — the `delegate_task` batch already blocked until every child returned |
25
+ | any persistent-team primitive ("team" tools) | there is no persistent team; re-dispatch a fresh `delegate_task` batch of `leaf` children each time |
26
+ | `load_skills=[...]` | name the skills to load inside the child's `goal`/`context` text |
27
+
28
+ When you need refactor lanes worked in parallel, dispatch them all in a **single**
29
+ `delegate_task` batch and inline each lane's mandate. The plugin's `subagent_stop` hook
30
+ logs each child as it finishes. If a code block below conflicts with this section, this
31
+ section wins — every foreign delegation call in the prose below is a LEGACY illustration
32
+ copied from another harness; never call any of them literally, map each to a
33
+ `delegate_task` child.
34
+
35
+ export const REFACTOR_TEMPLATE = `# Intelligent Refactor Command
36
+
37
+ ## Usage
38
+ \`\`\`
39
+ /refactor <refactoring-target> [--scope=<file|module|project>] [--strategy=<safe|aggressive>]
40
+
41
+ Arguments:
42
+ refactoring-target: What to refactor. Can be:
43
+ - File path: src/auth/handler.ts
44
+ - Symbol name: "AuthService class"
45
+ - Pattern: "all functions using deprecated API"
46
+ - Description: "extract validation logic into separate module"
47
+
48
+ Options:
49
+ --scope: Refactoring scope (default: module)
50
+ - file: Single file only
51
+ - module: Module/directory scope
52
+ - project: Entire codebase
53
+
54
+ --strategy: Risk tolerance (default: safe)
55
+ - safe: Conservative, maximum test coverage required
56
+ - aggressive: Allow broader changes with adequate coverage
57
+ \`\`\`
58
+
59
+ ## What This Command Does
60
+
61
+ Performs intelligent, deterministic refactoring with full codebase awareness. Unlike blind search-and-replace, this command:
62
+
63
+ 1. **Understands your intent** - Analyzes what you actually want to achieve
64
+ 2. **Maps the codebase** - Builds a definitive codemap before touching anything
65
+ 3. **Assesses risk** - Evaluates test coverage and determines verification strategy
66
+ 4. **Plans meticulously** - Creates a detailed plan with Plan agent
67
+ 5. **Executes precisely** - Step-by-step refactoring with LSP and AST-grep
68
+ 6. **Verifies constantly** - Runs tests after each change to ensure zero regression
69
+
70
+ ---
71
+
72
+ # PHASE 0: INTENT GATE (MANDATORY FIRST STEP)
73
+
74
+ **BEFORE ANY ACTION, classify and validate the request.**
75
+
76
+ ## Step 0.1: Parse Request Type
77
+
78
+ | Signal | Classification | Action |
79
+ |--------|----------------|--------|
80
+ | Specific file/symbol | Explicit | Proceed to codebase analysis |
81
+ | "Refactor X to Y" | Clear transformation | Proceed to codebase analysis |
82
+ | "Improve", "Clean up" | Open-ended | **MUST ask**: "What specific improvement?" |
83
+ | Ambiguous scope | Uncertain | **MUST ask**: "Which modules/files?" |
84
+ | Missing context | Incomplete | **MUST ask**: "What's the desired outcome?" |
85
+
86
+ ## Step 0.2: Validate Understanding
87
+
88
+ Before proceeding, confirm:
89
+ - [ ] Target is clearly identified
90
+ - [ ] Desired outcome is understood
91
+ - [ ] Scope is defined (file/module/project)
92
+ - [ ] Success criteria can be articulated
93
+
94
+ **If ANY of above is unclear, ASK CLARIFYING QUESTION:**
95
+
96
+ \`\`\`
97
+ I want to make sure I understand the refactoring goal correctly.
98
+
99
+ **What I understood**: [interpretation]
100
+ **What I'm unsure about**: [specific ambiguity]
101
+
102
+ Options I see:
103
+ 1. [Option A] - [implications]
104
+ 2. [Option B] - [implications]
105
+
106
+ **My recommendation**: [suggestion with reasoning]
107
+
108
+ Should I proceed with [recommendation], or would you prefer differently?
109
+ \`\`\`
110
+
111
+ ## Step 0.3: Create Initial Todos
112
+
113
+ **IMMEDIATELY after understanding the request, create todos:**
114
+
115
+ \`\`\`
116
+ TodoWrite([
117
+ {"id": "phase-1", "content": "PHASE 1: Codebase Analysis - launch parallel explore agents", "status": "pending", "priority": "high"},
118
+ {"id": "phase-2", "content": "PHASE 2: Build Codemap - map dependencies and impact zones", "status": "pending", "priority": "high"},
119
+ {"id": "phase-3", "content": "PHASE 3: Test Assessment - analyze test coverage and verification strategy", "status": "pending", "priority": "high"},
120
+ {"id": "phase-4", "content": "PHASE 4: Plan Generation - invoke Plan agent for detailed refactoring plan", "status": "pending", "priority": "high"},
121
+ {"id": "phase-5", "content": "PHASE 5: Execute Refactoring - step-by-step with continuous verification", "status": "pending", "priority": "high"},
122
+ {"id": "phase-6", "content": "PHASE 6: Final Verification - full test suite and regression check", "status": "pending", "priority": "high"}
123
+ ])
124
+ \`\`\`
125
+
126
+ ---
127
+
128
+ # PHASE 1: CODEBASE ANALYSIS (PARALLEL EXPLORATION)
129
+
130
+ **Mark phase-1 as in_progress.**
131
+
132
+ ## 1.1: Launch Parallel Codebase-Search Lanes
133
+
134
+ Fan ALL of these out in **one** \`delegate_task\` batch — five \`leaf\` children that run in
135
+ parallel. Each child plays the **codebase-search role**; inline its mandate in \`goal\`/\`context\`:
136
+
137
+ \`\`\`
138
+ delegate_task(tasks=[
139
+ // Lane 1: Find the refactoring target
140
+ {
141
+ role: "leaf",
142
+ goal: "codebase-search role: find all occurrences and definitions of [TARGET].",
143
+ context: "Report: file paths, line numbers, usage patterns."
144
+ },
145
+ // Lane 2: Find related code
146
+ {
147
+ role: "leaf",
148
+ goal: "codebase-search role: find all code that imports, uses, or depends on [TARGET].",
149
+ context: "Report: dependency chains, import graphs."
150
+ },
151
+ // Lane 3: Find similar patterns
152
+ {
153
+ role: "leaf",
154
+ goal: "codebase-search role: find similar code patterns to [TARGET] in the codebase.",
155
+ context: "Report: analogous implementations, established conventions."
156
+ },
157
+ // Lane 4: Find tests
158
+ {
159
+ role: "leaf",
160
+ goal: "codebase-search role: find all test files related to [TARGET].",
161
+ context: "Report: test file paths, test case names, coverage indicators."
162
+ },
163
+ // Lane 5: Architecture context
164
+ {
165
+ role: "leaf",
166
+ goal: "codebase-search role: find architectural patterns and module organization around [TARGET].",
167
+ context: "Report: module boundaries, layer structure, design patterns in use."
168
+ }
169
+ ])
170
+ \`\`\`
171
+
172
+ The batch blocks until all five children return; you receive every result together.
173
+
174
+ ## 1.2: Direct Tool Exploration (BEFORE OR AFTER THE BATCH)
175
+
176
+ The \`delegate_task\` batch blocks the parent, so run these direct tools either before you
177
+ dispatch the batch or after it returns — not concurrently with it:
178
+
179
+ ### LSP Tools for Precise Analysis:
180
+
181
+ \`\`\`typescript
182
+ // Find definition(s)
183
+ LspGotoDefinition(filePath, line, character) // Where is it defined?
184
+
185
+ // Find ALL usages across workspace
186
+ LspFindReferences(filePath, line, character, includeDeclaration=true)
187
+
188
+ // Get file structure
189
+ LspDocumentSymbols(filePath) // Hierarchical outline
190
+ LspWorkspaceSymbols(filePath, query="[target_symbol]") // Search by name
191
+
192
+ // Get current diagnostics
193
+ lsp_diagnostics(filePath) // Errors, warnings before we start
194
+ \`\`\`
195
+
196
+ ### AST-Grep for Pattern Analysis:
197
+
198
+ \`\`\`typescript
199
+ // Find structural patterns
200
+ ast_grep_search(
201
+ pattern="function $NAME($$$) { $$$ }", // or relevant pattern
202
+ lang="typescript", // or relevant language
203
+ paths=["src/"]
204
+ )
205
+
206
+ // Preview refactoring (DRY RUN)
207
+ ast_grep_replace(
208
+ pattern="[old_pattern]",
209
+ rewrite="[new_pattern]",
210
+ lang="[language]",
211
+ dryRun=true // ALWAYS preview first
212
+ )
213
+ \`\`\`
214
+
215
+ ### Grep for Text Patterns:
216
+
217
+ \`\`\`
218
+ grep(pattern="[search_term]", path="src/", include="*.ts")
219
+ \`\`\`
220
+
221
+ ## 1.3: Collect Lane Results
222
+
223
+ The single \`delegate_task\` batch from 1.1 already returned every lane's result together —
224
+ there is nothing extra to poll. Read each child's findings (target locations, dependency
225
+ chains, similar patterns, tests, architecture) straight from the batch result.
226
+
227
+ **Mark phase-1 as completed after all results collected.**
228
+
229
+ ---
230
+
231
+ # PHASE 2: BUILD CODEMAP (DEPENDENCY MAPPING)
232
+
233
+ **Mark phase-2 as in_progress.**
234
+
235
+ ## 2.1: Construct Definitive Codemap
236
+
237
+ Based on Phase 1 results, build:
238
+
239
+ \`\`\`
240
+ ## CODEMAP: [TARGET]
241
+
242
+ ### Core Files (Direct Impact)
243
+ - \`path/to/file.ts:L10-L50\` - Primary definition
244
+ - \`path/to/file2.ts:L25\` - Key usage
245
+
246
+ ### Dependency Graph
247
+ \`\`\`
248
+ [TARGET]
249
+ ├── imports from:
250
+ │ ├── module-a (types)
251
+ │ └── module-b (utils)
252
+ ├── imported by:
253
+ │ ├── consumer-1.ts
254
+ │ ├── consumer-2.ts
255
+ │ └── consumer-3.ts
256
+ └── used by:
257
+ ├── handler.ts (direct call)
258
+ └── service.ts (dependency injection)
259
+ \`\`\`
260
+
261
+ ### Impact Zones
262
+ | Zone | Risk Level | Files Affected | Test Coverage |
263
+ |------|------------|----------------|---------------|
264
+ | Core | HIGH | 3 files | 85% covered |
265
+ | Consumers | MEDIUM | 8 files | 70% covered |
266
+ | Edge | LOW | 2 files | 50% covered |
267
+
268
+ ### Established Patterns
269
+ - Pattern A: [description] - used in N places
270
+ - Pattern B: [description] - established convention
271
+ \`\`\`
272
+
273
+ ## 2.2: Identify Refactoring Constraints
274
+
275
+ Based on codemap:
276
+ - **MUST follow**: [existing patterns identified]
277
+ - **MUST NOT break**: [critical dependencies]
278
+ - **Safe to change**: [isolated code zones]
279
+ - **Requires migration**: [breaking changes impact]
280
+
281
+ **Mark phase-2 as completed.**
282
+
283
+ ---
284
+
285
+ # PHASE 3: TEST ASSESSMENT (VERIFICATION STRATEGY)
286
+
287
+ **Mark phase-3 as in_progress.**
288
+
289
+ ## 3.1: Detect Test Infrastructure
290
+
291
+ \`\`\`bash
292
+ # Check for test commands
293
+ cat package.json | jq '.scripts | keys[] | select(test("test"))'
294
+
295
+ # Or for Python
296
+ ls -la pytest.ini pyproject.toml setup.cfg
297
+
298
+ # Or for Go
299
+ ls -la *_test.go
300
+ \`\`\`
301
+
302
+ ## 3.2: Analyze Test Coverage
303
+
304
+ \`\`\`
305
+ // One codebase-search child; the batch blocks, so the result is available inline.
306
+ delegate_task(tasks=[
307
+ {
308
+ role: "leaf",
309
+ goal: "codebase-search role: analyze test coverage for [TARGET].",
310
+ context: "Answer: 1. Which test files cover this code? 2. What test cases exist? "
311
+ + "3. Are there integration tests? 4. What edge cases are tested? "
312
+ + "5. Estimated coverage percentage?"
313
+ }
314
+ ])
315
+ \`\`\`
316
+
317
+ ## 3.3: Determine Verification Strategy
318
+
319
+ Based on test analysis:
320
+
321
+ | Coverage Level | Strategy |
322
+ |----------------|----------|
323
+ | HIGH (>80%) | Run existing tests after each step |
324
+ | MEDIUM (50-80%) | Run tests + add safety assertions |
325
+ | LOW (<50%) | **PAUSE**: Propose adding tests first |
326
+ | NONE | **BLOCK**: Refuse aggressive refactoring |
327
+
328
+ **If coverage is LOW or NONE, ask user:**
329
+
330
+ \`\`\`
331
+ Test coverage for [TARGET] is [LEVEL].
332
+
333
+ **Risk Assessment**: Refactoring without adequate tests is dangerous.
334
+
335
+ Options:
336
+ 1. Add tests first, then refactor (RECOMMENDED)
337
+ 2. Proceed with extra caution, manual verification required
338
+ 3. Abort refactoring
339
+
340
+ Which approach do you prefer?
341
+ \`\`\`
342
+
343
+ ## 3.4: Document Verification Plan
344
+
345
+ \`\`\`
346
+ ## VERIFICATION PLAN
347
+
348
+ ### Test Commands
349
+ - Unit: \`bun test\` / \`npm test\` / \`pytest\` / etc.
350
+ - Integration: [command if exists]
351
+ - Type check: \`tsc --noEmit\` / \`pyright\` / etc.
352
+
353
+ ### Verification Checkpoints
354
+ After each refactoring step:
355
+ 1. lsp_diagnostics → zero new errors
356
+ 2. Run test command → all pass
357
+ 3. Type check → clean
358
+
359
+ ### Regression Indicators
360
+ - [Specific test that must pass]
361
+ - [Behavior that must be preserved]
362
+ - [API contract that must not change]
363
+ \`\`\`
364
+
365
+ **Mark phase-3 as completed.**
366
+
367
+ ---
368
+
369
+ # PHASE 4: PLAN GENERATION (PLAN-GENERATION ROLE)
370
+
371
+ **Mark phase-4 as in_progress.**
372
+
373
+ ## 4.1: Dispatch the plan-generation lane
374
+
375
+ \`\`\`
376
+ delegate_task(tasks=[{
377
+ role: "leaf",
378
+ goal: "plan-generation role: create a detailed refactoring plan.",
379
+ context: "Create a detailed refactoring plan:
380
+
381
+ ## Refactoring Goal
382
+ [User's original request]
383
+
384
+ ## Codemap (from Phase 2)
385
+ [Insert codemap here]
386
+
387
+ ## Test Coverage (from Phase 3)
388
+ [Insert verification plan here]
389
+
390
+ ## Constraints
391
+ - MUST follow existing patterns: [list]
392
+ - MUST NOT break: [critical paths]
393
+ - MUST run tests after each step
394
+
395
+ ## Requirements
396
+ 1. Break down into atomic refactoring steps
397
+ 2. Each step must be independently verifiable
398
+ 3. Order steps by dependency (what must happen first)
399
+ 4. Specify exact files and line ranges for each step
400
+ 5. Include rollback strategy for each step
401
+ 6. Define commit checkpoints"
402
+ }])
403
+ \`\`\`
404
+
405
+ ## 4.2: Review and Validate Plan
406
+
407
+ After the plan-generation lane returns:
408
+
409
+ 1. **Verify completeness**: All identified files addressed?
410
+ 2. **Verify safety**: Each step reversible?
411
+ 3. **Verify order**: Dependencies respected?
412
+ 4. **Verify verification**: Test commands specified?
413
+
414
+ ## 4.3: Register Detailed Todos
415
+
416
+ Convert the plan-generation lane's output into granular todos:
417
+
418
+ \`\`\`
419
+ TodoWrite([
420
+ // Each step from the plan becomes a todo
421
+ {"id": "refactor-1", "content": "Step 1: [description]", "status": "pending", "priority": "high"},
422
+ {"id": "verify-1", "content": "Verify Step 1: run tests", "status": "pending", "priority": "high"},
423
+ {"id": "refactor-2", "content": "Step 2: [description]", "status": "pending", "priority": "medium"},
424
+ {"id": "verify-2", "content": "Verify Step 2: run tests", "status": "pending", "priority": "medium"},
425
+ // ... continue for all steps
426
+ ])
427
+ \`\`\`
428
+
429
+ **Mark phase-4 as completed.**
430
+
431
+ ---
432
+
433
+ # PHASE 5: EXECUTE REFACTORING (DETERMINISTIC EXECUTION)
434
+
435
+ **Mark phase-5 as in_progress.**
436
+
437
+ ## 5.1: Execution Protocol
438
+
439
+ For EACH refactoring step:
440
+
441
+ ### Pre-Step
442
+ 1. Mark step todo as \`in_progress\`
443
+ 2. Read current file state
444
+ 3. Verify lsp_diagnostics is baseline
445
+
446
+ ### Execute Step
447
+ Use appropriate tool:
448
+
449
+ **For Symbol Renames:**
450
+ \`\`\`typescript
451
+ lsp_prepare_rename(filePath, line, character) // Validate rename is possible
452
+ lsp_rename(filePath, line, character, newName) // Execute rename
453
+ \`\`\`
454
+
455
+ **For Pattern Transformations:**
456
+ \`\`\`typescript
457
+ // Preview first
458
+ ast_grep_replace(pattern, rewrite, lang, dryRun=true)
459
+
460
+ // If preview looks good, execute
461
+ ast_grep_replace(pattern, rewrite, lang, dryRun=false)
462
+ \`\`\`
463
+
464
+ **For Structural Changes:**
465
+ \`\`\`typescript
466
+ // Use Edit tool for precise changes
467
+ edit(filePath, oldString, newString)
468
+ \`\`\`
469
+
470
+ ### Post-Step Verification (MANDATORY)
471
+
472
+ \`\`\`typescript
473
+ // 1. Check diagnostics
474
+ lsp_diagnostics(filePath) // Must be clean or same as baseline
475
+
476
+ // 2. Run tests
477
+ bash("bun test") // Or appropriate test command
478
+
479
+ // 3. Type check
480
+ bash("tsc --noEmit") // Or appropriate type check
481
+ \`\`\`
482
+
483
+ ### Step Completion
484
+ 1. If verification passes → Mark step todo as \`completed\`
485
+ 2. If verification fails → **STOP AND FIX**
486
+
487
+ ## 5.2: Failure Recovery Protocol
488
+
489
+ If ANY verification fails:
490
+
491
+ 1. **STOP** immediately
492
+ 2. **REVERT** the failed change
493
+ 3. **DIAGNOSE** what went wrong
494
+ 4. **OPTIONS**:
495
+ - Fix the issue and retry
496
+ - Skip this step (if optional)
497
+ - Dispatch a plan-review lane (a \`delegate_task\` \`leaf\` child whose \`goal\` inlines the failure + ask for a fix strategy)
498
+ - Ask user for guidance
499
+
500
+ **NEVER proceed to next step with broken tests.**
501
+
502
+ ## 5.3: Commit Checkpoints
503
+
504
+ After each logical group of changes:
505
+
506
+ \`\`\`bash
507
+ git add [changed-files]
508
+ git commit -m "refactor(scope): description
509
+
510
+ [details of what was changed and why]"
511
+ \`\`\`
512
+
513
+ **Mark phase-5 as completed when all refactoring steps done.**
514
+
515
+ ---
516
+
517
+ # PHASE 6: FINAL VERIFICATION (REGRESSION CHECK)
518
+
519
+ **Mark phase-6 as in_progress.**
520
+
521
+ ## 6.1: Full Test Suite
522
+
523
+ \`\`\`bash
524
+ # Run complete test suite
525
+ bun test # or npm test, pytest, go test, etc.
526
+ \`\`\`
527
+
528
+ ## 6.2: Type Check
529
+
530
+ \`\`\`bash
531
+ # Full type check
532
+ tsc --noEmit # or equivalent
533
+ \`\`\`
534
+
535
+ ## 6.3: Lint Check
536
+
537
+ \`\`\`bash
538
+ # Run linter
539
+ eslint . # or equivalent
540
+ \`\`\`
541
+
542
+ ## 6.4: Build Verification (if applicable)
543
+
544
+ \`\`\`bash
545
+ # Ensure build still works
546
+ bun run build # or npm run build, etc.
547
+ \`\`\`
548
+
549
+ ## 6.5: Final Diagnostics
550
+
551
+ \`\`\`typescript
552
+ // Check all changed files
553
+ for (file of changedFiles) {
554
+ lsp_diagnostics(file) // Must all be clean
555
+ }
556
+ \`\`\`
557
+
558
+ ## 6.6: Generate Summary
559
+
560
+ \`\`\`markdown
561
+ ## Refactoring Complete
562
+
563
+ ### What Changed
564
+ - [List of changes made]
565
+
566
+ ### Files Modified
567
+ - \`path/to/file.ts\` - [what changed]
568
+ - \`path/to/file2.ts\` - [what changed]
569
+
570
+ ### Verification Results
571
+ - Tests: PASSED (X/Y passing)
572
+ - Type Check: CLEAN
573
+ - Lint: CLEAN
574
+ - Build: SUCCESS
575
+
576
+ ### No Regressions Detected
577
+ All existing tests pass. No new errors introduced.
578
+ \`\`\`
579
+
580
+ **Mark phase-6 as completed.**
581
+
582
+ ---
583
+
584
+ # CRITICAL RULES
585
+
586
+ ## NEVER DO
587
+ - Skip lsp_diagnostics check after changes
588
+ - Proceed with failing tests
589
+ - Make changes without understanding impact
590
+ - Use \`as any\`, \`@ts-ignore\`, \`@ts-expect-error\`
591
+ - Delete tests to make them pass
592
+ - Commit broken code
593
+ - Refactor without understanding existing patterns
594
+
595
+ ## ALWAYS DO
596
+ - Understand before changing
597
+ - Preview before applying (ast_grep dryRun=true)
598
+ - Verify after every change
599
+ - Follow existing codebase patterns
600
+ - Keep todos updated in real-time
601
+ - Commit at logical checkpoints
602
+ - Report issues immediately
603
+
604
+ ## ABORT CONDITIONS
605
+ If any of these occur, **STOP and consult user**:
606
+ - Test coverage is zero for target code
607
+ - Changes would break public API
608
+ - Refactoring scope is unclear
609
+ - 3 consecutive verification failures
610
+ - User-defined constraints violated
611
+
612
+ ---
613
+
614
+ # Tool Usage Philosophy
615
+
616
+ You already know these tools. Use them intelligently:
617
+
618
+ ## LSP Tools
619
+ Leverage LSP tools for precision analysis. Key patterns:
620
+ - **Understand before changing**: \`LspGotoDefinition\` to grasp context
621
+ - **Impact analysis**: \`LspFindReferences\` to map all usages before modification
622
+ - **Safe refactoring**: \`lsp_prepare_rename\` → \`lsp_rename\` for symbol renames
623
+ - **Continuous verification**: \`lsp_diagnostics\` after every change
624
+
625
+ ## AST-Grep
626
+ Use \`ast_grep_search\` and \`ast_grep_replace\` for structural transformations.
627
+ **Critical**: Always \`dryRun=true\` first, review, then execute.
628
+
629
+ ## Delegation Roles
630
+
631
+ Every role below is a \`delegate_task\` child — there is no agent registry and no named
632
+ personas. Inline the role's mandate in the child's \`goal\`/\`context\`. Dispatch independent
633
+ roles together in **one** \`delegate_task\` batch so they run in parallel:
634
+
635
+ - **codebase-search role**: parallel codebase pattern discovery — locate the target, its
636
+ dependents, similar patterns, related tests, and architecture context.
637
+ - **plan-generation role**: produce the detailed atomic refactor plan with verifiable steps,
638
+ ordering, exact files/line ranges, rollback strategy, and commit checkpoints.
639
+ - **plan-review / verification role**: read-only consultation for complex architectural
640
+ decisions, debugging, and confirming a step introduced no regression.
641
+ - **external-research role**: use when encountering deprecated methods or library-migration
642
+ tasks — query official docs and OSS examples for the modern replacements.
643
+
644
+ ## Deprecated Code & Library Migration
645
+ When you encounter deprecated methods/APIs during refactoring:
646
+ 1. Dispatch an **external-research** lane (a \`delegate_task\` \`leaf\` child) to find the recommended modern alternative
647
+ 2. **DO NOT auto-upgrade to latest version** unless user explicitly requests migration
648
+ 3. If user requests library migration, use the **external-research** lane to fetch latest API docs before making changes
649
+
650
+ ---
651
+
652
+ **Remember: Refactoring without tests is reckless. Refactoring without understanding is destructive. This command ensures you do neither.**
653
+
654
+ <user-request>
655
+ $ARGUMENTS
656
+ </user-request>
657
+ `
658
+
659
+ export const REFACTOR_PARALLEL_DISPATCH_ADDENDUM = `
660
+ ---
661
+
662
+ # Parallel Refactor Dispatch (Hermes \`delegate_task\`)
663
+
664
+ When the plan has several file-independent steps, you can work them in parallel with the
665
+ native \`delegate_task\` batch instead of executing each step inline. This addendum **refines
666
+ Phase 4-6** — it does not introduce any new primitive. There is still only one orchestrator
667
+ (you) and a set of \`leaf\` children dispatched per \`delegate_task\` batch; the parent blocks
668
+ until every child in a batch returns. There is no persistent team, no team registry, no
669
+ named worker personas, and no per-child model selection.
670
+
671
+ ## Phase 4 refinement: parallelism analysis in the plan
672
+
673
+ When you dispatch the plan-generation lane in Phase 4.1, append this requirement to its
674
+ \`goal\`/\`context\` so the plan tells you what is safe to parallelize — missing fields block
675
+ Phase 5 batching:
676
+
677
+ \`\`\`
678
+ 7. Output a Parallelism Analysis section with these fields:
679
+ - total_atomic_steps: integer
680
+ - file_independent_steps: integer (parallelizable, no cross-file blocker)
681
+ - cross_file_dependent_steps: integer (has blockers)
682
+ - per_step_class: [{step_id, class: 'mechanical' | 'reasoning', blockedBy: [step_ids], rationale}]
683
+ - dispatch_recommendation: 'parallel-batch' | 'sequential' with reason
684
+ - rationale for the grouping
685
+ \`\`\`
686
+
687
+ **Step classification** the plan-generation lane must apply to each step:
688
+ - \`mechanical\`: LSP rename, extract variable, inline, simple move, signature change without call-site logic.
689
+ - \`reasoning\`: logic-preserving refactors that need thought — extract function, restructure conditional, pattern transformation, cross-file API change.
690
+ - Recommend \`parallel-batch\` when \`file_independent_steps >= 3\`; recommend \`sequential\` otherwise.
691
+
692
+ ## Phase 5 refinement: choose batch vs sequential
693
+
694
+ Read the Parallelism Analysis from Phase 4. If any required field is missing, STOP and
695
+ re-request the plan with the exact missing field names — do not batch from a partial plan.
696
+
697
+ Then choose:
698
+
699
+ - **Parallel-batch path (5.1-B)**: when the plan recommends \`parallel-batch\` AND \`file_independent_steps >= 3\`. Dispatch the independent steps as \`leaf\` children in one \`delegate_task\` batch.
700
+ - **Sequential path (5.1-S)**: otherwise. Use the original 5.1 / 5.2 / 5.3 flow above.
701
+
702
+ Record the chosen path in the TodoWrite list.
703
+
704
+ ## Phase 5.1-B: parallel-batch execution
705
+
706
+ **Preconditions** (fail hard if any check fails):
707
+
708
+ 1. Confirm every step you intend to batch is marked \`file_independent\` (empty \`blockedBy\`) in the plan. Steps with blockers run sequentially in dependency order.
709
+ 2. Group steps so that no two children in the same batch touch the same file. Children share no state; overlapping edits would race.
710
+ 3. Inline each child's mandate fully — children do not see prior conversation. Each child receives its own slim brief.
711
+
712
+ **Per-child brief** — every child you dispatch carries its full mandate inline in
713
+ \`goal\`/\`context\`:
714
+
715
+ - For a **mechanical** step: "mechanical-refactor role: apply this step verbatim — no scope
716
+ expansion. Use LSP tools for correctness. After edits, request \`lsp_diagnostics\` on
717
+ touched files. Return: files touched + diagnostics status + diff summary. Do not run
718
+ tests, do not git add."
719
+ - For a **reasoning** step: "reasoning-refactor role: read this plan step carefully. Preview
720
+ with \`ast_grep_replace\` dryRun=true first, review, then execute. If the step is ambiguous
721
+ or would require out-of-scope changes, STOP and return \`UNCLEAR\` with the reason rather
722
+ than guessing. Return: files touched + diagnostics status + diff summary. Do not run
723
+ tests."
724
+
725
+ **Dispatch the batch** — pass the shared refactor context (codemap summary, constraints,
726
+ and established patterns from Phase 2) and the verification spec (exact test/typecheck/lint
727
+ commands + expected pass counts + regression indicators from Phase 3.4) **inline in every
728
+ child's \`context\`**; children cannot read a broadcast channel:
729
+
730
+ \`\`\`
731
+ delegate_task(tasks=[
732
+ {
733
+ role: "leaf",
734
+ goal: "<mechanical|reasoning>-refactor role: refactor step <N> — <short>",
735
+ context: "<per-step instructions: target files + line ranges + rollback strategy> "
736
+ + "<codemap summary + constraints + established patterns> "
737
+ + "<verify context for reference only — do NOT run tests>"
738
+ },
739
+ // ... one child per file-independent step, no two touching the same file ...
740
+ ])
741
+ \`\`\`
742
+
743
+ The batch blocks until every child returns. You receive all of their reports together.
744
+
745
+ **After the batch returns** (orchestrator owns verification — children never run tests):
746
+
747
+ - For each completed step, run the verification yourself, or dispatch a **plan-review /
748
+ verification** lane as a \`leaf\` child whose \`goal\` inlines the touched files + the verify
749
+ commands and asks it to return \`PASS\` or \`FAIL:<failing test + specific error + suggested
750
+ revert hunks>\`. Do not create a commit checkpoint until verification is PASS.
751
+ - On PASS: make the commit checkpoint for that step (see original 5.3). Proceed.
752
+ - On FAIL: decide —
753
+ - **Retry with fix hint**: re-dispatch that one step as a fresh \`leaf\` child whose \`context\` inlines the specific failure from verification.
754
+ - **Escalate**: after three FAIL cycles on the same step, STOP and consult the user with full evidence.
755
+ - On a child returning \`UNCLEAR\`: re-harvest context with a targeted codebase-search lane, then re-dispatch that step with the clarified brief inlined.
756
+
757
+ Proceed to Phase 6 only when every batched step is committed AND every paired verification returned PASS.
758
+
759
+ ## Phase 6 refinement: report the dispatch path
760
+
761
+ Append to the 6.6 summary a "Dispatch path" line and, when the parallel-batch path was used,
762
+ the batch metrics (batches dispatched, children per batch, verification lanes run).
763
+
764
+ ## MUST NOT (parallel-batch)
765
+
766
+ - Never put two children that touch the same file in the same batch — they race.
767
+ - Do not assume a child can see prior conversation, a broadcast, or another child's output — inline everything each child needs.
768
+ - Children never run tests or commit — the orchestrator owns verification and the commit checkpoints.
769
+ - Do not invent a persistent team or a named-agent registry; every lane is a fresh \`delegate_task\` \`leaf\` child whose mandate is inlined.
770
+ `