claude-code-workflow 6.3.26 → 6.3.28

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 (129) hide show
  1. package/.claude/CLAUDE.md +7 -1
  2. package/.claude/agents/action-planning-agent.md +1 -0
  3. package/.claude/agents/cli-discuss-agent.md +391 -0
  4. package/.claude/agents/cli-execution-agent.md +2 -0
  5. package/.claude/agents/cli-explore-agent.md +2 -1
  6. package/.claude/agents/cli-lite-planning-agent.md +1 -0
  7. package/.claude/agents/cli-planning-agent.md +1 -0
  8. package/.claude/agents/code-developer.md +1 -0
  9. package/.claude/agents/conceptual-planning-agent.md +2 -0
  10. package/.claude/agents/context-search-agent.md +1 -0
  11. package/.claude/agents/debug-explore-agent.md +2 -0
  12. package/.claude/agents/doc-generator.md +1 -0
  13. package/.claude/agents/issue-plan-agent.md +2 -1
  14. package/.claude/agents/issue-queue-agent.md +2 -1
  15. package/.claude/agents/memory-bridge.md +2 -0
  16. package/.claude/agents/test-context-search-agent.md +2 -0
  17. package/.claude/agents/test-fix-agent.md +1 -0
  18. package/.claude/agents/ui-design-agent.md +2 -0
  19. package/.claude/agents/universal-executor.md +1 -0
  20. package/.claude/commands/issue/execute.md +141 -163
  21. package/.claude/commands/workflow/lite-lite-lite.md +798 -0
  22. package/.claude/commands/workflow/multi-cli-plan.md +510 -0
  23. package/.claude/skills/ccw/SKILL.md +262 -372
  24. package/.claude/skills/ccw/command.json +547 -0
  25. package/.claude/skills/ccw-help/SKILL.md +46 -107
  26. package/.claude/skills/ccw-help/command.json +511 -0
  27. package/.claude/skills/skill-tuning/SKILL.md +303 -0
  28. package/.claude/skills/skill-tuning/phases/actions/action-abort.md +164 -0
  29. package/.claude/skills/skill-tuning/phases/actions/action-analyze-requirements.md +406 -0
  30. package/.claude/skills/skill-tuning/phases/actions/action-apply-fix.md +206 -0
  31. package/.claude/skills/skill-tuning/phases/actions/action-complete.md +195 -0
  32. package/.claude/skills/skill-tuning/phases/actions/action-diagnose-agent.md +317 -0
  33. package/.claude/skills/skill-tuning/phases/actions/action-diagnose-context.md +243 -0
  34. package/.claude/skills/skill-tuning/phases/actions/action-diagnose-dataflow.md +318 -0
  35. package/.claude/skills/skill-tuning/phases/actions/action-diagnose-docs.md +299 -0
  36. package/.claude/skills/skill-tuning/phases/actions/action-diagnose-memory.md +269 -0
  37. package/.claude/skills/skill-tuning/phases/actions/action-diagnose-token-consumption.md +200 -0
  38. package/.claude/skills/skill-tuning/phases/actions/action-gemini-analysis.md +322 -0
  39. package/.claude/skills/skill-tuning/phases/actions/action-generate-report.md +228 -0
  40. package/.claude/skills/skill-tuning/phases/actions/action-init.md +149 -0
  41. package/.claude/skills/skill-tuning/phases/actions/action-propose-fixes.md +317 -0
  42. package/.claude/skills/skill-tuning/phases/actions/action-verify.md +222 -0
  43. package/.claude/skills/skill-tuning/phases/orchestrator.md +377 -0
  44. package/.claude/skills/skill-tuning/phases/state-schema.md +378 -0
  45. package/.claude/skills/skill-tuning/specs/category-mappings.json +284 -0
  46. package/.claude/skills/skill-tuning/specs/dimension-mapping.md +212 -0
  47. package/.claude/skills/skill-tuning/specs/problem-taxonomy.md +318 -0
  48. package/.claude/skills/skill-tuning/specs/quality-gates.md +263 -0
  49. package/.claude/skills/skill-tuning/specs/skill-authoring-principles.md +189 -0
  50. package/.claude/skills/skill-tuning/specs/tuning-strategies.md +1537 -0
  51. package/.claude/skills/skill-tuning/templates/diagnosis-report.md +153 -0
  52. package/.claude/skills/skill-tuning/templates/fix-proposal.md +204 -0
  53. package/.claude/workflows/cli-templates/schemas/multi-cli-discussion-schema.json +421 -0
  54. package/.claude/workflows/cli-tools-usage.md +0 -41
  55. package/ccw/dist/core/auth/csrf-middleware.d.ts.map +1 -1
  56. package/ccw/dist/core/auth/csrf-middleware.js +3 -1
  57. package/ccw/dist/core/auth/csrf-middleware.js.map +1 -1
  58. package/ccw/dist/core/data-aggregator.d.ts +2 -0
  59. package/ccw/dist/core/data-aggregator.d.ts.map +1 -1
  60. package/ccw/dist/core/data-aggregator.js +5 -2
  61. package/ccw/dist/core/data-aggregator.js.map +1 -1
  62. package/ccw/dist/core/lite-scanner.d.ts +2 -1
  63. package/ccw/dist/core/lite-scanner.d.ts.map +1 -1
  64. package/ccw/dist/core/lite-scanner.js +295 -6
  65. package/ccw/dist/core/lite-scanner.js.map +1 -1
  66. package/ccw/dist/core/routes/codexlens/config-handlers.d.ts.map +1 -1
  67. package/ccw/dist/core/routes/codexlens/config-handlers.js +5 -5
  68. package/ccw/dist/core/routes/codexlens/config-handlers.js.map +1 -1
  69. package/ccw/dist/core/routes/session-routes.d.ts.map +1 -1
  70. package/ccw/dist/core/routes/session-routes.js +166 -48
  71. package/ccw/dist/core/routes/session-routes.js.map +1 -1
  72. package/ccw/dist/core/routes/system-routes.d.ts.map +1 -1
  73. package/ccw/dist/core/routes/system-routes.js +87 -0
  74. package/ccw/dist/core/routes/system-routes.js.map +1 -1
  75. package/ccw/dist/core/server.js +2 -2
  76. package/ccw/dist/core/server.js.map +1 -1
  77. package/ccw/scripts/IMPLEMENTATION-SUMMARY.md +226 -0
  78. package/ccw/scripts/QUICK-REFERENCE.md +135 -0
  79. package/ccw/scripts/README-memory-embedder.md +157 -0
  80. package/ccw/scripts/__pycache__/memory_embedder.cpython-313.pyc +0 -0
  81. package/ccw/scripts/__pycache__/test_memory_embedder.cpython-313-pytest-8.4.2.pyc +0 -0
  82. package/ccw/scripts/memory-embedder-example.ts +184 -0
  83. package/ccw/scripts/memory_embedder.py +428 -0
  84. package/ccw/scripts/test_memory_embedder.py +245 -0
  85. package/ccw/src/core/auth/csrf-middleware.ts +3 -1
  86. package/ccw/src/core/data-aggregator.ts +7 -2
  87. package/ccw/src/core/lite-scanner.ts +440 -6
  88. package/ccw/src/core/routes/codexlens/config-handlers.ts +12 -9
  89. package/ccw/src/core/routes/session-routes.ts +201 -48
  90. package/ccw/src/core/routes/system-routes.ts +102 -0
  91. package/ccw/src/core/server.ts +2 -2
  92. package/ccw/src/templates/dashboard-css/01-base.css +8 -0
  93. package/ccw/src/templates/dashboard-css/02-session.css +81 -0
  94. package/ccw/src/templates/dashboard-css/04-lite-tasks.css +2442 -0
  95. package/ccw/src/templates/dashboard-css/21-cli-toolmgmt.css +157 -0
  96. package/ccw/src/templates/dashboard-css/32-issue-manager.css +23 -0
  97. package/ccw/src/templates/dashboard-js/components/cli-stream-viewer.js +38 -4
  98. package/ccw/src/templates/dashboard-js/components/hook-manager.js +38 -13
  99. package/ccw/src/templates/dashboard-js/components/navigation.js +24 -4
  100. package/ccw/src/templates/dashboard-js/i18n.js +194 -6
  101. package/ccw/src/templates/dashboard-js/views/api-settings.js +32 -0
  102. package/ccw/src/templates/dashboard-js/views/claude-manager.js +44 -3
  103. package/ccw/src/templates/dashboard-js/views/cli-manager.js +303 -31
  104. package/ccw/src/templates/dashboard-js/views/history.js +44 -6
  105. package/ccw/src/templates/dashboard-js/views/home.js +1 -0
  106. package/ccw/src/templates/dashboard-js/views/issue-manager.js +54 -7
  107. package/ccw/src/templates/dashboard-js/views/lite-tasks.js +1817 -4
  108. package/ccw/src/templates/dashboard.html +5 -0
  109. package/package.json +2 -1
  110. package/.claude/skills/ccw/index/command-capabilities.json +0 -127
  111. package/.claude/skills/ccw/index/intent-rules.json +0 -136
  112. package/.claude/skills/ccw/index/workflow-chains.json +0 -451
  113. package/.claude/skills/ccw/phases/actions/bugfix.md +0 -218
  114. package/.claude/skills/ccw/phases/actions/coupled.md +0 -194
  115. package/.claude/skills/ccw/phases/actions/docs.md +0 -93
  116. package/.claude/skills/ccw/phases/actions/full.md +0 -154
  117. package/.claude/skills/ccw/phases/actions/issue.md +0 -201
  118. package/.claude/skills/ccw/phases/actions/rapid.md +0 -104
  119. package/.claude/skills/ccw/phases/actions/review-fix.md +0 -84
  120. package/.claude/skills/ccw/phases/actions/tdd.md +0 -66
  121. package/.claude/skills/ccw/phases/actions/ui.md +0 -79
  122. package/.claude/skills/ccw/phases/orchestrator.md +0 -435
  123. package/.claude/skills/ccw/specs/intent-classification.md +0 -336
  124. package/.claude/skills/ccw-help/index/all-agents.json +0 -82
  125. package/.claude/skills/ccw-help/index/all-commands.json +0 -882
  126. package/.claude/skills/ccw-help/index/by-category.json +0 -914
  127. package/.claude/skills/ccw-help/index/by-use-case.json +0 -896
  128. package/.claude/skills/ccw-help/index/command-relationships.json +0 -160
  129. package/.claude/skills/ccw-help/index/essential-commands.json +0 -112
@@ -0,0 +1,1537 @@
1
+ # Tuning Strategies
2
+
3
+ Detailed fix strategies for each problem category with implementation guidance.
4
+
5
+ ## When to Use
6
+
7
+ | Phase | Usage | Section |
8
+ |-------|-------|---------|
9
+ | action-propose-fixes | Strategy selection | Strategy Details |
10
+ | action-apply-fix | Implementation guidance | Implementation |
11
+ | action-verify | Verification steps | Verification |
12
+
13
+ ---
14
+
15
+ ## Authoring Principles Strategies (P0 - 首要准则)
16
+
17
+ > **核心原则**:简洁高效 → 去除无关存储 → 去除中间存储 → 上下文流转
18
+
19
+ ### Strategy: eliminate_intermediate_files
20
+
21
+ **Purpose**: 删除所有中间文件,改用上下文流转。
22
+
23
+ **Implementation**:
24
+ ```javascript
25
+ // Before: 中间文件
26
+ async function process() {
27
+ const step1 = await analyze();
28
+ Write(`${workDir}/step1.json`, JSON.stringify(step1));
29
+
30
+ const step1Data = JSON.parse(Read(`${workDir}/step1.json`));
31
+ const step2 = await transform(step1Data);
32
+ Write(`${workDir}/step2.json`, JSON.stringify(step2));
33
+
34
+ const step2Data = JSON.parse(Read(`${workDir}/step2.json`));
35
+ return finalize(step2Data);
36
+ }
37
+
38
+ // After: 上下文流转
39
+ async function process() {
40
+ const step1 = await analyze();
41
+ const step2 = await transform(step1); // 直接传递
42
+ return finalize(step2); // 只返回最终结果
43
+ }
44
+ ```
45
+
46
+ **Risk**: Low
47
+ **Verification**: `ls ${workDir}` 无 temp/intermediate 文件
48
+
49
+ ---
50
+
51
+ ### Strategy: minimize_state
52
+
53
+ **Purpose**: 精简 State schema 至必要字段。
54
+
55
+ **Implementation**:
56
+ ```typescript
57
+ // Before: 膨胀的 State
58
+ interface State {
59
+ status: string;
60
+ target: TargetInfo;
61
+ user_input: string;
62
+ parsed_input: ParsedInput; // 删除 - 只在处理时用
63
+ intermediate_result: any; // 删除 - 中间结果
64
+ debug_info: DebugInfo; // 删除 - 调试信息
65
+ analysis_cache: any; // 删除 - 缓存
66
+ full_history: HistoryEntry[]; // 删除 - 无限增长
67
+ step1_output: any; // 删除 - 中间输出
68
+ step2_output: any; // 删除 - 中间输出
69
+ final_result: FinalResult;
70
+ }
71
+
72
+ // After: 精简的 State
73
+ interface State {
74
+ status: 'pending' | 'running' | 'completed' | 'failed';
75
+ target: { name: string; path: string };
76
+ result_path: string; // 最终结果路径
77
+ error?: string; // 仅失败时有
78
+ }
79
+ ```
80
+
81
+ **Rules**:
82
+ - State 字段 ≤ 15 个
83
+ - 删除所有 `debug_*`, `*_cache`, `*_temp` 字段
84
+ - `*_history` 数组设置上限或改用滚动窗口
85
+
86
+ **Risk**: Medium (需确保不丢失必要数据)
87
+ **Verification**: Count state fields ≤ 15
88
+
89
+ ---
90
+
91
+ ### Strategy: context_passing
92
+
93
+ **Purpose**: 用函数参数/返回值代替文件中转。
94
+
95
+ **Implementation**:
96
+ ```javascript
97
+ // 上下文流转模式
98
+ async function executeWorkflow(initialContext) {
99
+ let ctx = initialContext;
100
+
101
+ // Phase 1: 直接传递上下文
102
+ ctx = await executePhase1(ctx);
103
+
104
+ // Phase 2: 继续传递
105
+ ctx = await executePhase2(ctx);
106
+
107
+ // Phase 3: 最终处理
108
+ const result = await executePhase3(ctx);
109
+
110
+ // 只存最终结果
111
+ Write(`${ctx.workDir}/result.json`, JSON.stringify(result));
112
+
113
+ return result;
114
+ }
115
+
116
+ // Phase 函数模板
117
+ async function executePhaseN(ctx) {
118
+ const { previousResult, constraints } = ctx;
119
+
120
+ const result = await Task({
121
+ prompt: `
122
+ [CONTEXT]
123
+ ${JSON.stringify(previousResult)}
124
+
125
+ [TASK]
126
+ Process and return result.
127
+ `
128
+ });
129
+
130
+ // 返回更新后的上下文,不写文件
131
+ return {
132
+ ...ctx,
133
+ previousResult: result,
134
+ completed: [...ctx.completed, 'phase-n']
135
+ };
136
+ }
137
+ ```
138
+
139
+ **Risk**: Low
140
+ **Verification**: 无 Write→Read 紧邻模式
141
+
142
+ ---
143
+
144
+ ### Strategy: deduplicate_storage
145
+
146
+ **Purpose**: 消除重复数据存储。
147
+
148
+ **Implementation**:
149
+ ```javascript
150
+ // Before: 重复存储
151
+ state.user_request = userInput;
152
+ state.original_request = userInput;
153
+ state.input_text = userInput;
154
+
155
+ // After: 单一来源
156
+ state.input = userInput; // 唯一存储点
157
+ ```
158
+
159
+ **Risk**: Low
160
+ **Verification**: 无相同数据多字段存储
161
+
162
+ ---
163
+
164
+ ## Context Explosion Strategies
165
+
166
+ ### Strategy: sliding_window
167
+
168
+ **Purpose**: Limit context history to most recent N items.
169
+
170
+ **Implementation**:
171
+ ```javascript
172
+ // In orchestrator.md or phase files
173
+ const MAX_HISTORY_ITEMS = 5;
174
+
175
+ function updateHistory(state, newItem) {
176
+ const history = state.history || [];
177
+ const updated = [...history, newItem].slice(-MAX_HISTORY_ITEMS);
178
+ return { ...state, history: updated };
179
+ }
180
+ ```
181
+
182
+ **Files to Modify**:
183
+ - `phases/orchestrator.md` - Add history management
184
+ - `phases/state-schema.md` - Document history limit
185
+
186
+ **Risk**: Low
187
+ **Verification**:
188
+ - Run skill for 10+ iterations
189
+ - Verify history.length never exceeds MAX_HISTORY_ITEMS
190
+
191
+ ---
192
+
193
+ ### Strategy: path_reference
194
+
195
+ **Purpose**: Pass file paths instead of full content.
196
+
197
+ **Implementation**:
198
+ ```javascript
199
+ // Before
200
+ const content = Read('data.json');
201
+ const prompt = `Analyze: ${content}`;
202
+
203
+ // After
204
+ const dataPath = `${workDir}/data.json`;
205
+ const prompt = `Analyze file at: ${dataPath}. Read it first.`;
206
+ ```
207
+
208
+ **Files to Modify**:
209
+ - All phase files with `${content}` in prompts
210
+
211
+ **Risk**: Low
212
+ **Verification**:
213
+ - Verify agents can still access required data
214
+ - Check token count reduced
215
+
216
+ ---
217
+
218
+ ### Strategy: context_summarization
219
+
220
+ **Purpose**: Add summarization step before passing to next phase.
221
+
222
+ **Implementation**:
223
+ ```javascript
224
+ // Add summarization agent
225
+ const summarizeResult = await Task({
226
+ subagent_type: 'universal-executor',
227
+ prompt: `
228
+ Summarize the following in <100 words, preserving key facts:
229
+ ${fullContent}
230
+
231
+ Return JSON: { summary: "...", key_points: [...] }
232
+ `
233
+ });
234
+
235
+ // Pass summary instead of full content
236
+ nextPhasePrompt = `Previous phase summary: ${summarizeResult.summary}`;
237
+ ```
238
+
239
+ **Files to Modify**:
240
+ - Phase transition points
241
+ - Orchestrator (if autonomous)
242
+
243
+ **Risk**: Low
244
+ **Verification**:
245
+ - Compare output quality with/without summarization
246
+ - Verify key information preserved
247
+
248
+ ---
249
+
250
+ ### Strategy: structured_state
251
+
252
+ **Purpose**: Replace text-based context with structured JSON state.
253
+
254
+ **Implementation**:
255
+ ```javascript
256
+ // Before: Text-based context passing
257
+ const context = `
258
+ User requested: ${userRequest}
259
+ Previous output: ${previousOutput}
260
+ Current status: ${status}
261
+ `;
262
+
263
+ // After: Structured state
264
+ const state = {
265
+ original_request: userRequest,
266
+ previous_output_path: `${workDir}/output.md`,
267
+ previous_output_summary: "...",
268
+ status: status,
269
+ key_decisions: [...]
270
+ };
271
+ ```
272
+
273
+ **Files to Modify**:
274
+ - `phases/state-schema.md` - Define structure
275
+ - All phases - Use structured fields
276
+
277
+ **Risk**: Medium (requires refactoring)
278
+ **Verification**:
279
+ - Verify all phases can access required state fields
280
+ - Check backward compatibility
281
+
282
+ ---
283
+
284
+ ## Long-tail Forgetting Strategies
285
+
286
+ ### Strategy: constraint_injection
287
+
288
+ **Purpose**: Inject original constraints into every phase prompt.
289
+
290
+ **Implementation**:
291
+ ```javascript
292
+ // Add to every phase prompt template
293
+ const phasePrompt = `
294
+ [CONSTRAINTS - FROM ORIGINAL REQUEST]
295
+ ${state.original_requirements.map(r => `- ${r}`).join('\n')}
296
+
297
+ [CURRENT TASK]
298
+ ${taskDescription}
299
+
300
+ [REMINDER]
301
+ Output MUST satisfy all constraints listed above.
302
+ `;
303
+ ```
304
+
305
+ **Files to Modify**:
306
+ - All `phases/*.md` files
307
+ - `templates/agent-base.md` (if exists)
308
+
309
+ **Risk**: Low
310
+ **Verification**:
311
+ - Verify constraints visible in each phase
312
+ - Test with specific constraint, verify output respects it
313
+
314
+ ---
315
+
316
+ ### Strategy: state_constraints_field
317
+
318
+ **Purpose**: Add dedicated field in state schema for requirements.
319
+
320
+ **Implementation**:
321
+ ```typescript
322
+ // In state-schema.md
323
+ interface State {
324
+ // Add these fields
325
+ original_requirements: string[]; // User's original constraints
326
+ goal_summary: string; // One-line goal statement
327
+ constraint_violations: string[]; // Track any violations
328
+ }
329
+
330
+ // In action-init.md
331
+ function initState(userInput) {
332
+ return {
333
+ original_requirements: extractRequirements(userInput),
334
+ goal_summary: summarizeGoal(userInput),
335
+ constraint_violations: []
336
+ };
337
+ }
338
+ ```
339
+
340
+ **Files to Modify**:
341
+ - `phases/state-schema.md`
342
+ - `phases/actions/action-init.md`
343
+
344
+ **Risk**: Low
345
+ **Verification**:
346
+ - Verify state.json contains requirements after init
347
+ - Check requirements persist through all phases
348
+
349
+ ---
350
+
351
+ ### Strategy: checkpoint_restore
352
+
353
+ **Purpose**: Save state at key milestones for recovery and verification.
354
+
355
+ **Implementation**:
356
+ ```javascript
357
+ // Add checkpoint function
358
+ function createCheckpoint(state, workDir, checkpointName) {
359
+ const checkpointPath = `${workDir}/checkpoints/${checkpointName}.json`;
360
+ Write(checkpointPath, JSON.stringify({
361
+ state: state,
362
+ timestamp: new Date().toISOString(),
363
+ name: checkpointName
364
+ }, null, 2));
365
+ return checkpointPath;
366
+ }
367
+
368
+ // Use at key points
369
+ await executePhase2();
370
+ createCheckpoint(state, workDir, 'after-phase-2');
371
+ ```
372
+
373
+ **Files to Modify**:
374
+ - `phases/orchestrator.md`
375
+ - Key phase files
376
+
377
+ **Risk**: Low
378
+ **Verification**:
379
+ - Verify checkpoints created at expected points
380
+ - Test restore from checkpoint
381
+
382
+ ---
383
+
384
+ ### Strategy: goal_embedding
385
+
386
+ **Purpose**: Track semantic similarity to original goal throughout execution.
387
+
388
+ **Implementation**:
389
+ ```javascript
390
+ // Store goal embedding at init
391
+ state.goal_embedding = await embed(state.goal_summary);
392
+
393
+ // At each major phase, check alignment
394
+ const currentPlanEmbedding = await embed(currentPlan);
395
+ const similarity = cosineSimilarity(state.goal_embedding, currentPlanEmbedding);
396
+
397
+ if (similarity < 0.7) {
398
+ console.warn('Goal drift detected! Similarity:', similarity);
399
+ // Trigger re-alignment
400
+ }
401
+ ```
402
+
403
+ **Files to Modify**:
404
+ - State schema (add embedding field)
405
+ - Orchestrator (add similarity check)
406
+
407
+ **Risk**: Medium (requires embedding infrastructure)
408
+ **Verification**:
409
+ - Test with intentional drift, verify detection
410
+ - Verify false positive rate acceptable
411
+
412
+ ---
413
+
414
+ ## Data Flow Strategies
415
+
416
+ ### Strategy: state_centralization
417
+
418
+ **Purpose**: Use single state.json for all persistent data.
419
+
420
+ **Implementation**:
421
+ ```javascript
422
+ // Create state manager
423
+ const StateManager = {
424
+ read: (workDir) => JSON.parse(Read(`${workDir}/state.json`)),
425
+
426
+ update: (workDir, updates) => {
427
+ const current = StateManager.read(workDir);
428
+ const next = { ...current, ...updates, updated_at: new Date().toISOString() };
429
+ Write(`${workDir}/state.json`, JSON.stringify(next, null, 2));
430
+ return next;
431
+ },
432
+
433
+ get: (workDir, path) => {
434
+ const state = StateManager.read(workDir);
435
+ return path.split('.').reduce((obj, key) => obj?.[key], state);
436
+ }
437
+ };
438
+
439
+ // Replace direct writes
440
+ // Before: Write(`${workDir}/config.json`, config);
441
+ // After: StateManager.update(workDir, { config });
442
+ ```
443
+
444
+ **Files to Modify**:
445
+ - All phases that write state
446
+ - Create shared state manager
447
+
448
+ **Risk**: Medium (significant refactoring)
449
+ **Verification**:
450
+ - Verify single state.json after full run
451
+ - Check no orphan state files
452
+
453
+ ---
454
+
455
+ ### Strategy: schema_enforcement
456
+
457
+ **Purpose**: Add runtime validation using Zod or similar.
458
+
459
+ **Implementation**:
460
+ ```javascript
461
+ // Define schema (in state-schema.md)
462
+ const StateSchema = {
463
+ status: ['pending', 'running', 'completed', 'failed'],
464
+ target_skill: {
465
+ name: 'string',
466
+ path: 'string'
467
+ },
468
+ // ... full schema
469
+ };
470
+
471
+ function validateState(state) {
472
+ const errors = [];
473
+
474
+ if (!StateSchema.status.includes(state.status)) {
475
+ errors.push(`Invalid status: ${state.status}`);
476
+ }
477
+
478
+ if (typeof state.target_skill?.name !== 'string') {
479
+ errors.push('target_skill.name must be string');
480
+ }
481
+
482
+ if (errors.length > 0) {
483
+ throw new Error(`State validation failed:\n${errors.join('\n')}`);
484
+ }
485
+
486
+ return true;
487
+ }
488
+
489
+ // Use before state write
490
+ function updateState(workDir, updates) {
491
+ const newState = { ...currentState, ...updates };
492
+ validateState(newState); // Throws if invalid
493
+ Write(`${workDir}/state.json`, JSON.stringify(newState, null, 2));
494
+ }
495
+ ```
496
+
497
+ **Files to Modify**:
498
+ - `phases/state-schema.md` - Add validation function
499
+ - All state write locations
500
+
501
+ **Risk**: Low
502
+ **Verification**:
503
+ - Test with invalid state, verify rejection
504
+ - Verify valid state accepted
505
+
506
+ ---
507
+
508
+ ### Strategy: field_normalization
509
+
510
+ **Purpose**: Normalize field names across all phases.
511
+
512
+ **Implementation**:
513
+ ```javascript
514
+ // Create normalization mapping
515
+ const FIELD_NORMALIZATIONS = {
516
+ 'title': 'name',
517
+ 'identifier': 'id',
518
+ 'state': 'status',
519
+ 'error': 'errors'
520
+ };
521
+
522
+ function normalizeData(data) {
523
+ if (typeof data !== 'object' || data === null) return data;
524
+
525
+ const normalized = {};
526
+ for (const [key, value] of Object.entries(data)) {
527
+ const normalizedKey = FIELD_NORMALIZATIONS[key] || key;
528
+ normalized[normalizedKey] = normalizeData(value);
529
+ }
530
+ return normalized;
531
+ }
532
+
533
+ // Apply when reading external data
534
+ const rawData = JSON.parse(Read(filePath));
535
+ const normalizedData = normalizeData(rawData);
536
+ ```
537
+
538
+ **Files to Modify**:
539
+ - Data ingestion points
540
+ - State update functions
541
+
542
+ **Risk**: Low
543
+ **Verification**:
544
+ - Verify consistent field names in state
545
+ - Check no data loss during normalization
546
+
547
+ ---
548
+
549
+ ## Agent Coordination Strategies
550
+
551
+ ### Strategy: error_wrapping
552
+
553
+ **Purpose**: Add try-catch to all Task calls.
554
+
555
+ **Implementation**:
556
+ ```javascript
557
+ // Wrapper function
558
+ async function safeTask(config, state, updateState) {
559
+ const maxRetries = 3;
560
+
561
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
562
+ try {
563
+ const result = await Task(config);
564
+
565
+ // Validate result
566
+ if (!result) throw new Error('Empty result from agent');
567
+
568
+ return result;
569
+ } catch (error) {
570
+ console.log(`Task attempt ${attempt} failed: ${error.message}`);
571
+
572
+ if (attempt === maxRetries) {
573
+ updateState({
574
+ errors: [...state.errors, {
575
+ action: config.subagent_type,
576
+ message: error.message,
577
+ timestamp: new Date().toISOString()
578
+ }],
579
+ error_count: state.error_count + 1
580
+ });
581
+ throw error;
582
+ }
583
+
584
+ // Wait before retry
585
+ await new Promise(r => setTimeout(r, 1000 * attempt));
586
+ }
587
+ }
588
+ }
589
+ ```
590
+
591
+ **Files to Modify**:
592
+ - All files with Task calls
593
+
594
+ **Risk**: Low
595
+ **Verification**:
596
+ - Simulate agent failure, verify graceful handling
597
+ - Verify retry logic works
598
+
599
+ ---
600
+
601
+ ### Strategy: result_validation
602
+
603
+ **Purpose**: Validate agent returns before use.
604
+
605
+ **Implementation**:
606
+ ```javascript
607
+ function validateAgentResult(result, expectedSchema) {
608
+ // Try JSON parse
609
+ let parsed;
610
+ try {
611
+ parsed = typeof result === 'string' ? JSON.parse(result) : result;
612
+ } catch (e) {
613
+ throw new Error(`Agent result is not valid JSON: ${result.slice(0, 100)}`);
614
+ }
615
+
616
+ // Check required fields
617
+ for (const field of expectedSchema.required || []) {
618
+ if (!(field in parsed)) {
619
+ throw new Error(`Missing required field: ${field}`);
620
+ }
621
+ }
622
+
623
+ return parsed;
624
+ }
625
+
626
+ // Usage
627
+ const rawResult = await Task({...});
628
+ const validResult = validateAgentResult(rawResult, {
629
+ required: ['status', 'output_file']
630
+ });
631
+ ```
632
+
633
+ **Files to Modify**:
634
+ - All locations where agent results are used
635
+
636
+ **Risk**: Low
637
+ **Verification**:
638
+ - Test with invalid agent output
639
+ - Verify proper error messages
640
+
641
+ ---
642
+
643
+ ### Strategy: flatten_nesting
644
+
645
+ **Purpose**: Remove nested agent calls, use orchestrator coordination.
646
+
647
+ **Implementation**:
648
+ ```javascript
649
+ // Before: Agent A calls Agent B in its prompt
650
+ // Agent A prompt: "... then call Task({subagent_type: 'B', ...}) ..."
651
+
652
+ // After: Agent A returns signal, orchestrator handles
653
+ // Agent A prompt: "If you need further analysis, return: { needs_agent_b: true, context: ... }"
654
+
655
+ // Orchestrator handles:
656
+ const resultA = await Task({ subagent_type: 'A', ... });
657
+ const parsedA = JSON.parse(resultA);
658
+
659
+ if (parsedA.needs_agent_b) {
660
+ const resultB = await Task({
661
+ subagent_type: 'B',
662
+ prompt: `Continue analysis with context: ${JSON.stringify(parsedA.context)}`
663
+ });
664
+ }
665
+ ```
666
+
667
+ **Files to Modify**:
668
+ - Phase files with nested Task calls
669
+ - Orchestrator decision logic
670
+
671
+ **Risk**: Medium (may change agent behavior)
672
+ **Verification**:
673
+ - Verify no nested Task patterns
674
+ - Test agent chain via orchestrator
675
+
676
+ ---
677
+
678
+ ## Documentation Strategies
679
+
680
+ 文档去重和冲突解决策略。
681
+
682
+ ---
683
+
684
+ ### Strategy: consolidate_to_ssot
685
+
686
+ **Purpose**: 将重复定义合并到单一真相来源 (Single Source of Truth)。
687
+
688
+ **Implementation**:
689
+ ```javascript
690
+ // 合并流程
691
+ async function consolidateToSSOT(state, duplicates) {
692
+ // 1. 识别最完整的定义位置
693
+ const canonical = selectCanonicalSource(duplicates);
694
+
695
+ // 2. 确保规范位置包含完整定义
696
+ const fullDefinition = mergeDefinitions(duplicates);
697
+ Write(canonical.file, fullDefinition);
698
+
699
+ // 3. 替换其他位置为引用
700
+ for (const dup of duplicates.filter(d => d.file !== canonical.file)) {
701
+ const reference = generateReference(canonical.file, dup.type);
702
+ // 例如: "详见 [state-schema.md](phases/state-schema.md)"
703
+ replaceWithReference(dup.file, dup.location, reference);
704
+ }
705
+
706
+ return { canonical: canonical.file, removed: duplicates.length - 1 };
707
+ }
708
+
709
+ function selectCanonicalSource(duplicates) {
710
+ // 优先级: specs/ > phases/ > SKILL.md
711
+ const priority = ['specs/', 'phases/', 'SKILL.md'];
712
+ return duplicates.sort((a, b) => {
713
+ const aIdx = priority.findIndex(p => a.file.includes(p));
714
+ const bIdx = priority.findIndex(p => b.file.includes(p));
715
+ return aIdx - bIdx;
716
+ })[0];
717
+ }
718
+ ```
719
+
720
+ **Risk**: Low
721
+ **Verification**:
722
+ - 确认只有一处包含完整定义
723
+ - 其他位置包含有效引用链接
724
+
725
+ ---
726
+
727
+ ### Strategy: centralize_mapping_config
728
+
729
+ **Purpose**: 将硬编码配置提取到集中的 JSON 文件,代码改为加载配置。
730
+
731
+ **Implementation**:
732
+ ```javascript
733
+ // 1. 创建集中配置文件
734
+ const config = {
735
+ version: "1.0.0",
736
+ categories: {
737
+ context_explosion: {
738
+ pattern_ids: ["CTX-001", "CTX-002"],
739
+ strategies: ["sliding_window", "path_reference"]
740
+ }
741
+ // ... 从硬编码中提取
742
+ }
743
+ };
744
+ Write('specs/category-mappings.json', JSON.stringify(config, null, 2));
745
+
746
+ // 2. 重构代码加载配置
747
+ // Before:
748
+ function findTaxonomyMatch(category) {
749
+ const patternMapping = {
750
+ 'context_explosion': { category: 'context_explosion', pattern_ids: [...] }
751
+ // 硬编码
752
+ };
753
+ return patternMapping[category];
754
+ }
755
+
756
+ // After:
757
+ function findTaxonomyMatch(category) {
758
+ const mappings = JSON.parse(Read('specs/category-mappings.json'));
759
+ const config = mappings.categories[category];
760
+ if (!config) return null;
761
+ return { category, pattern_ids: config.pattern_ids, severity_hint: config.severity_hint };
762
+ }
763
+ ```
764
+
765
+ **Risk**: Medium (需要测试配置加载逻辑)
766
+ **Verification**:
767
+ - JSON 文件语法正确
768
+ - 所有原硬编码的值都已迁移
769
+ - 功能行为不变
770
+
771
+ ---
772
+
773
+ ### Strategy: reconcile_conflicting_definitions
774
+
775
+ **Purpose**: 调和冲突的定义,由用户选择正确版本后统一更新。
776
+
777
+ **Implementation**:
778
+ ```javascript
779
+ async function reconcileConflicts(conflicts) {
780
+ for (const conflict of conflicts) {
781
+ // 1. 展示冲突给用户
782
+ const options = conflict.definitions.map(def => ({
783
+ label: `${def.file}: ${def.value}`,
784
+ description: `来自 ${def.file}`
785
+ }));
786
+
787
+ const choice = await AskUserQuestion({
788
+ questions: [{
789
+ question: `发现冲突定义: "${conflict.key}",请选择正确版本`,
790
+ header: '冲突解决',
791
+ options: options,
792
+ multiSelect: false
793
+ }]
794
+ });
795
+
796
+ // 2. 更新所有文件为选中的版本
797
+ const selected = conflict.definitions[choice.index];
798
+ for (const def of conflict.definitions) {
799
+ if (def.file !== selected.file) {
800
+ updateDefinition(def.file, conflict.key, selected.value);
801
+ }
802
+ }
803
+ }
804
+
805
+ return { resolved: conflicts.length };
806
+ }
807
+ ```
808
+
809
+ **Risk**: Low (用户确认后执行)
810
+ **Verification**:
811
+ - 所有位置的定义一致
812
+ - 无新冲突产生
813
+
814
+ ---
815
+
816
+ ## Strategy Selection Guide
817
+
818
+ ```
819
+ Issue Type: Context Explosion
820
+ ├── history grows unbounded? → sliding_window
821
+ ├── full content in prompts? → path_reference
822
+ ├── no summarization? → context_summarization
823
+ └── text-based context? → structured_state
824
+
825
+ Issue Type: Long-tail Forgetting
826
+ ├── constraints not in phases? → constraint_injection
827
+ ├── no requirements in state? → state_constraints_field
828
+ ├── no recovery points? → checkpoint_restore
829
+ └── goal drift risk? → goal_embedding
830
+
831
+ Issue Type: Data Flow
832
+ ├── multiple state files? → state_centralization
833
+ ├── no validation? → schema_enforcement
834
+ └── inconsistent names? → field_normalization
835
+
836
+ Issue Type: Agent Coordination
837
+ ├── no error handling? → error_wrapping
838
+ ├── no result validation? → result_validation
839
+ └── nested agent calls? → flatten_nesting
840
+
841
+ Issue Type: Prompt Engineering
842
+ ├── vague instructions? → structured_prompt
843
+ ├── inconsistent output? → output_schema
844
+ ├── hallucination risk? → grounding_context
845
+ └── format drift? → format_enforcement
846
+
847
+ Issue Type: Architecture
848
+ ├── unclear responsibilities? → phase_decomposition
849
+ ├── tight coupling? → interface_contracts
850
+ ├── poor extensibility? → plugin_architecture
851
+ └── complex flow? → state_machine
852
+
853
+ Issue Type: Performance
854
+ ├── high token usage? → token_budgeting
855
+ ├── slow execution? → parallel_execution
856
+ ├── redundant computation? → result_caching
857
+ └── large files? → lazy_loading
858
+
859
+ Issue Type: Error Handling
860
+ ├── no recovery? → graceful_degradation
861
+ ├── silent failures? → error_propagation
862
+ ├── no logging? → structured_logging
863
+ └── unclear errors? → error_context
864
+
865
+ Issue Type: Output Quality
866
+ ├── inconsistent quality? → quality_gates
867
+ ├── no verification? → output_validation
868
+ ├── format issues? → template_enforcement
869
+ └── incomplete output? → completeness_check
870
+
871
+ Issue Type: User Experience
872
+ ├── no progress? → progress_tracking
873
+ ├── unclear status? → status_communication
874
+ ├── no feedback? → interactive_checkpoints
875
+ └── confusing flow? → guided_workflow
876
+
877
+ Issue Type: Documentation Redundancy
878
+ ├── 核心定义重复? → consolidate_to_ssot
879
+ └── 硬编码配置重复? → centralize_mapping_config
880
+
881
+ Issue Type: Documentation Conflict
882
+ └── 定义不一致? → reconcile_conflicting_definitions
883
+ ```
884
+
885
+ ---
886
+
887
+ ## General Tuning Strategies (按需 via Gemini CLI)
888
+
889
+ 以下策略针对更通用的优化场景,通常需要 Gemini CLI 进行深度分析后生成具体实现。
890
+
891
+ ---
892
+
893
+ ### Prompt Engineering Strategies
894
+
895
+ #### Strategy: structured_prompt
896
+
897
+ **Purpose**: 将模糊指令转换为结构化提示词。
898
+
899
+ **Implementation**:
900
+ ```javascript
901
+ // Before: Vague prompt
902
+ const prompt = "Please analyze the code and give suggestions";
903
+
904
+ // After: Structured prompt
905
+ const prompt = `
906
+ [ROLE]
907
+ You are a code analysis expert specializing in ${domain}.
908
+
909
+ [TASK]
910
+ Analyze the provided code for:
911
+ 1. Code quality issues
912
+ 2. Performance bottlenecks
913
+ 3. Security vulnerabilities
914
+
915
+ [INPUT]
916
+ File: ${filePath}
917
+ Context: ${context}
918
+
919
+ [OUTPUT FORMAT]
920
+ Return JSON:
921
+ {
922
+ "issues": [{ "type": "...", "severity": "...", "location": "...", "suggestion": "..." }],
923
+ "summary": "..."
924
+ }
925
+
926
+ [CONSTRAINTS]
927
+ - Focus on actionable issues only
928
+ - Limit to top 10 findings
929
+ `;
930
+ ```
931
+
932
+ **Risk**: Low
933
+ **Verification**: Check output consistency across multiple runs
934
+
935
+ ---
936
+
937
+ #### Strategy: output_schema
938
+
939
+ **Purpose**: 强制 LLM 输出符合特定 schema。
940
+
941
+ **Implementation**:
942
+ ```javascript
943
+ // Define expected schema
944
+ const outputSchema = {
945
+ type: 'object',
946
+ required: ['status', 'result'],
947
+ properties: {
948
+ status: { enum: ['success', 'error', 'partial'] },
949
+ result: { type: 'object' },
950
+ errors: { type: 'array' }
951
+ }
952
+ };
953
+
954
+ // Include in prompt
955
+ const prompt = `
956
+ ...task description...
957
+
958
+ [OUTPUT SCHEMA]
959
+ Your response MUST be valid JSON matching this schema:
960
+ ${JSON.stringify(outputSchema, null, 2)}
961
+
962
+ [VALIDATION]
963
+ Before returning, verify your output:
964
+ 1. Is it valid JSON?
965
+ 2. Does it have all required fields?
966
+ 3. Are field types correct?
967
+ `;
968
+ ```
969
+
970
+ **Risk**: Low
971
+ **Verification**: JSON.parse + schema validation
972
+
973
+ ---
974
+
975
+ #### Strategy: grounding_context
976
+
977
+ **Purpose**: 提供足够上下文减少幻觉。
978
+
979
+ **Implementation**:
980
+ ```javascript
981
+ // Gather grounding context
982
+ const groundingContext = {
983
+ codebase_patterns: await analyzePatterns(skillPath),
984
+ existing_examples: await findSimilarImplementations(taskType),
985
+ constraints: state.original_requirements
986
+ };
987
+
988
+ const prompt = `
989
+ [GROUNDING CONTEXT]
990
+ This skill follows these patterns:
991
+ ${JSON.stringify(groundingContext.codebase_patterns)}
992
+
993
+ Similar implementations exist at:
994
+ ${groundingContext.existing_examples.map(e => `- ${e.path}`).join('\n')}
995
+
996
+ [TASK]
997
+ ${taskDescription}
998
+
999
+ [IMPORTANT]
1000
+ - Only suggest patterns that exist in the codebase
1001
+ - Reference specific files when making suggestions
1002
+ - If unsure, indicate uncertainty level
1003
+ `;
1004
+ ```
1005
+
1006
+ **Risk**: Medium (requires context gathering)
1007
+ **Verification**: Check suggestions match existing patterns
1008
+
1009
+ ---
1010
+
1011
+ ### Architecture Strategies
1012
+
1013
+ #### Strategy: phase_decomposition
1014
+
1015
+ **Purpose**: 重新划分阶段以清晰化职责。
1016
+
1017
+ **Analysis via Gemini**:
1018
+ ```bash
1019
+ ccw cli -p "
1020
+ PURPOSE: Analyze phase decomposition for skill at ${skillPath}
1021
+ TASK: • Map current phase responsibilities • Identify overlapping concerns • Suggest cleaner boundaries
1022
+ MODE: analysis
1023
+ CONTEXT: @phases/**/*.md
1024
+ EXPECTED: { current_phases: [], overlaps: [], recommended_structure: [] }
1025
+ " --tool gemini --mode analysis
1026
+ ```
1027
+
1028
+ **Implementation Pattern**:
1029
+ ```
1030
+ Before: Monolithic phases
1031
+ Phase1: Collect + Analyze + Transform + Output
1032
+
1033
+ After: Single-responsibility phases
1034
+ Phase1: Collect (input gathering)
1035
+ Phase2: Analyze (processing)
1036
+ Phase3: Transform (conversion)
1037
+ Phase4: Output (delivery)
1038
+ ```
1039
+
1040
+ ---
1041
+
1042
+ #### Strategy: interface_contracts
1043
+
1044
+ **Purpose**: 定义阶段间的数据契约。
1045
+
1046
+ **Implementation**:
1047
+ ```typescript
1048
+ // Define contracts in state-schema.md
1049
+ interface PhaseContract {
1050
+ input: {
1051
+ required: string[];
1052
+ optional: string[];
1053
+ schema: object;
1054
+ };
1055
+ output: {
1056
+ guarantees: string[];
1057
+ schema: object;
1058
+ };
1059
+ }
1060
+
1061
+ // Phase 1 output contract
1062
+ const phase1Contract: PhaseContract = {
1063
+ input: {
1064
+ required: ['user_request'],
1065
+ optional: ['preferences'],
1066
+ schema: { /* ... */ }
1067
+ },
1068
+ output: {
1069
+ guarantees: ['parsed_requirements', 'validation_status'],
1070
+ schema: { /* ... */ }
1071
+ }
1072
+ };
1073
+ ```
1074
+
1075
+ ---
1076
+
1077
+ ### Performance Strategies
1078
+
1079
+ #### Strategy: token_budgeting
1080
+
1081
+ **Purpose**: 为每个阶段设置 Token 预算。
1082
+
1083
+ **Implementation**:
1084
+ ```javascript
1085
+ const TOKEN_BUDGETS = {
1086
+ 'phase-collect': 2000,
1087
+ 'phase-analyze': 5000,
1088
+ 'phase-generate': 8000,
1089
+ total: 15000
1090
+ };
1091
+
1092
+ function checkBudget(phase, estimatedTokens) {
1093
+ if (estimatedTokens > TOKEN_BUDGETS[phase]) {
1094
+ console.warn(`Phase ${phase} exceeds budget: ${estimatedTokens} > ${TOKEN_BUDGETS[phase]}`);
1095
+ // Trigger summarization or truncation
1096
+ return false;
1097
+ }
1098
+ return true;
1099
+ }
1100
+ ```
1101
+
1102
+ ---
1103
+
1104
+ #### Strategy: parallel_execution
1105
+
1106
+ **Purpose**: 并行执行独立任务。
1107
+
1108
+ **Implementation**:
1109
+ ```javascript
1110
+ // Before: Sequential
1111
+ const result1 = await Task({ subagent_type: 'analyzer', prompt: prompt1 });
1112
+ const result2 = await Task({ subagent_type: 'analyzer', prompt: prompt2 });
1113
+ const result3 = await Task({ subagent_type: 'analyzer', prompt: prompt3 });
1114
+
1115
+ // After: Parallel (when independent)
1116
+ const [result1, result2, result3] = await Promise.all([
1117
+ Task({ subagent_type: 'analyzer', prompt: prompt1, run_in_background: true }),
1118
+ Task({ subagent_type: 'analyzer', prompt: prompt2, run_in_background: true }),
1119
+ Task({ subagent_type: 'analyzer', prompt: prompt3, run_in_background: true })
1120
+ ]);
1121
+ ```
1122
+
1123
+ ---
1124
+
1125
+ #### Strategy: result_caching
1126
+
1127
+ **Purpose**: 缓存中间结果避免重复计算。
1128
+
1129
+ **Implementation**:
1130
+ ```javascript
1131
+ const cache = {};
1132
+
1133
+ async function cachedAnalysis(key, analysisFunc) {
1134
+ if (cache[key]) {
1135
+ console.log(`Cache hit: ${key}`);
1136
+ return cache[key];
1137
+ }
1138
+
1139
+ const result = await analysisFunc();
1140
+ cache[key] = result;
1141
+
1142
+ // Persist to disk for cross-session caching
1143
+ Write(`${workDir}/cache/${key}.json`, JSON.stringify(result));
1144
+
1145
+ return result;
1146
+ }
1147
+ ```
1148
+
1149
+ ---
1150
+
1151
+ ### Error Handling Strategies
1152
+
1153
+ #### Strategy: graceful_degradation
1154
+
1155
+ **Purpose**: 失败时降级而非崩溃。
1156
+
1157
+ **Implementation**:
1158
+ ```javascript
1159
+ async function executeWithDegradation(primaryTask, fallbackTask) {
1160
+ try {
1161
+ return await primaryTask();
1162
+ } catch (error) {
1163
+ console.warn(`Primary task failed: ${error.message}, using fallback`);
1164
+
1165
+ try {
1166
+ return await fallbackTask();
1167
+ } catch (fallbackError) {
1168
+ console.error(`Fallback also failed: ${fallbackError.message}`);
1169
+ return {
1170
+ status: 'degraded',
1171
+ partial_result: null,
1172
+ error: fallbackError.message
1173
+ };
1174
+ }
1175
+ }
1176
+ }
1177
+ ```
1178
+
1179
+ ---
1180
+
1181
+ #### Strategy: structured_logging
1182
+
1183
+ **Purpose**: 添加结构化日志便于调试。
1184
+
1185
+ **Implementation**:
1186
+ ```javascript
1187
+ function log(level, action, data) {
1188
+ const entry = {
1189
+ timestamp: new Date().toISOString(),
1190
+ level,
1191
+ action,
1192
+ ...data
1193
+ };
1194
+
1195
+ // Append to log file
1196
+ const logPath = `${workDir}/execution.log`;
1197
+ const existing = Read(logPath) || '';
1198
+ Write(logPath, existing + JSON.stringify(entry) + '\n');
1199
+
1200
+ // Console output
1201
+ console.log(`[${level}] ${action}:`, JSON.stringify(data));
1202
+ }
1203
+
1204
+ // Usage
1205
+ log('INFO', 'phase_start', { phase: 'analyze', input_size: 1000 });
1206
+ log('ERROR', 'agent_failure', { agent: 'universal-executor', error: err.message });
1207
+ ```
1208
+
1209
+ ---
1210
+
1211
+ ### Output Quality Strategies
1212
+
1213
+ #### Strategy: quality_gates
1214
+
1215
+ **Purpose**: 输出前进行质量检查。
1216
+
1217
+ **Implementation**:
1218
+ ```javascript
1219
+ const qualityGates = [
1220
+ {
1221
+ name: 'completeness',
1222
+ check: (output) => output.sections?.length >= 3,
1223
+ message: 'Output must have at least 3 sections'
1224
+ },
1225
+ {
1226
+ name: 'format',
1227
+ check: (output) => /^#\s/.test(output.content),
1228
+ message: 'Output must start with markdown heading'
1229
+ },
1230
+ {
1231
+ name: 'length',
1232
+ check: (output) => output.content?.length >= 500,
1233
+ message: 'Output must be at least 500 characters'
1234
+ }
1235
+ ];
1236
+
1237
+ function validateOutput(output) {
1238
+ const failures = qualityGates
1239
+ .filter(gate => !gate.check(output))
1240
+ .map(gate => gate.message);
1241
+
1242
+ if (failures.length > 0) {
1243
+ throw new Error(`Quality gate failures:\n${failures.join('\n')}`);
1244
+ }
1245
+
1246
+ return true;
1247
+ }
1248
+ ```
1249
+
1250
+ ---
1251
+
1252
+ ### User Experience Strategies
1253
+
1254
+ #### Strategy: progress_tracking
1255
+
1256
+ **Purpose**: 显示执行进度。
1257
+
1258
+ **Implementation**:
1259
+ ```javascript
1260
+ function updateProgress(current, total, description) {
1261
+ const percentage = Math.round((current / total) * 100);
1262
+ const progressBar = '█'.repeat(percentage / 5) + '░'.repeat(20 - percentage / 5);
1263
+
1264
+ console.log(`[${progressBar}] ${percentage}% - ${description}`);
1265
+
1266
+ // Update state for UI
1267
+ updateState({
1268
+ progress: {
1269
+ current,
1270
+ total,
1271
+ percentage,
1272
+ description
1273
+ }
1274
+ });
1275
+ }
1276
+
1277
+ // Usage
1278
+ updateProgress(1, 5, 'Initializing tuning session...');
1279
+ updateProgress(2, 5, 'Running context diagnosis...');
1280
+ ```
1281
+
1282
+ ---
1283
+
1284
+ #### Strategy: interactive_checkpoints
1285
+
1286
+ **Purpose**: 在关键点暂停获取用户确认。
1287
+
1288
+ **Implementation**:
1289
+ ```javascript
1290
+ async function checkpoint(name, summary, options) {
1291
+ console.log(`\n=== Checkpoint: ${name} ===`);
1292
+ console.log(summary);
1293
+
1294
+ const response = await AskUserQuestion({
1295
+ questions: [{
1296
+ question: `Review ${name} results. How to proceed?`,
1297
+ header: 'Checkpoint',
1298
+ options: options || [
1299
+ { label: 'Continue', description: 'Proceed with next step' },
1300
+ { label: 'Modify', description: 'Adjust parameters and retry' },
1301
+ { label: 'Skip', description: 'Skip this step' },
1302
+ { label: 'Abort', description: 'Stop the workflow' }
1303
+ ],
1304
+ multiSelect: false
1305
+ }]
1306
+ });
1307
+
1308
+ return response;
1309
+ }
1310
+ ```
1311
+
1312
+ ---
1313
+
1314
+ ## Token Consumption Strategies
1315
+
1316
+ Strategies for reducing token usage and simplifying skill outputs.
1317
+
1318
+ ---
1319
+
1320
+ ### Strategy: prompt_compression
1321
+
1322
+ **Purpose**: Reduce verbose prompts by extracting static text and using templates.
1323
+
1324
+ **Implementation**:
1325
+ ```javascript
1326
+ // Before: Long inline prompt
1327
+ const prompt = `
1328
+ You are an expert code analyzer specializing in identifying patterns.
1329
+ Your role is to examine the provided code and identify any issues.
1330
+ Please follow these detailed instructions carefully:
1331
+ 1. Read the code thoroughly
1332
+ 2. Identify any anti-patterns
1333
+ 3. Check for security vulnerabilities
1334
+ ... (continues for many lines)
1335
+
1336
+ Code to analyze:
1337
+ ${fullCodeContent}
1338
+ `;
1339
+
1340
+ // After: Compressed with template reference
1341
+ const PROMPT_TEMPLATE_PATH = 'templates/analyzer-prompt.md';
1342
+
1343
+ const prompt = `
1344
+ [TEMPLATE: ${PROMPT_TEMPLATE_PATH}]
1345
+ [CODE_PATH: ${codePath}]
1346
+ [FOCUS: patterns, security]
1347
+ `;
1348
+
1349
+ // Or use key instructions only
1350
+ const prompt = `
1351
+ Analyze ${codePath} for: patterns, security, performance.
1352
+ Return JSON: { issues: [], severity: string }
1353
+ `;
1354
+ ```
1355
+
1356
+ **Risk**: Low
1357
+ **Verification**: Compare token count before/after compression
1358
+
1359
+ ---
1360
+
1361
+ ### Strategy: lazy_loading
1362
+
1363
+ **Purpose**: Pass file paths instead of full content, let consumers load if needed.
1364
+
1365
+ **Implementation**:
1366
+ ```javascript
1367
+ // Before: Full content in prompt
1368
+ const content = Read(filePath);
1369
+ const prompt = `Analyze this content:\n${content}`;
1370
+
1371
+ // After: Path reference with lazy loading instruction
1372
+ const prompt = `
1373
+ Analyze file at: ${filePath}
1374
+ (Read the file content if you need to examine it)
1375
+ Return: { summary: string, issues: [] }
1376
+ `;
1377
+
1378
+ // For agent calls
1379
+ const result = await Task({
1380
+ subagent_type: 'universal-executor',
1381
+ prompt: `
1382
+ [FILE_PATH]: ${dataPath}
1383
+ [TASK]: Analyze the file and extract key metrics.
1384
+ [NOTE]: Read the file only if needed for your analysis.
1385
+ `
1386
+ });
1387
+ ```
1388
+
1389
+ **Risk**: Low
1390
+ **Verification**: Verify agents can still access required data
1391
+
1392
+ ---
1393
+
1394
+ ### Strategy: output_minimization
1395
+
1396
+ **Purpose**: Configure agents to return minimal, structured output instead of verbose text.
1397
+
1398
+ **Implementation**:
1399
+ ```javascript
1400
+ // Before: Verbose output expectation
1401
+ const prompt = `
1402
+ Analyze the code and provide a detailed report including:
1403
+ - Executive summary
1404
+ - Detailed findings with explanations
1405
+ - Code examples for each issue
1406
+ - Recommendations with rationale
1407
+ ...
1408
+ `;
1409
+
1410
+ // After: Minimal structured output
1411
+ const prompt = `
1412
+ Analyze the code. Return ONLY this JSON:
1413
+ {
1414
+ "status": "pass|review|fail",
1415
+ "issues": [{ "id": string, "severity": string, "file": string, "line": number }],
1416
+ "summary": "one sentence"
1417
+ }
1418
+ Do not include explanations or code examples.
1419
+ `;
1420
+
1421
+ // Validation
1422
+ const result = JSON.parse(agentOutput);
1423
+ if (!result.status || !Array.isArray(result.issues)) {
1424
+ throw new Error('Invalid output format');
1425
+ }
1426
+ ```
1427
+
1428
+ **Risk**: Low
1429
+ **Verification**: JSON.parse succeeds, output size reduced
1430
+
1431
+ ---
1432
+
1433
+ ### Strategy: state_field_reduction
1434
+
1435
+ **Purpose**: Audit and consolidate state fields to minimize serialization overhead.
1436
+
1437
+ **Implementation**:
1438
+ ```typescript
1439
+ // Before: Bloated state
1440
+ interface State {
1441
+ status: string;
1442
+ target: TargetInfo;
1443
+ user_input: string;
1444
+ parsed_input: ParsedInput; // Remove - temporary
1445
+ intermediate_result: any; // Remove - not persisted
1446
+ debug_info: DebugInfo; // Remove - debugging only
1447
+ analysis_cache: any; // Remove - session cache
1448
+ full_history: HistoryEntry[]; // Remove - unbounded
1449
+ step1_output: any; // Remove - intermediate
1450
+ step2_output: any; // Remove - intermediate
1451
+ step3_output: any; // Remove - intermediate
1452
+ final_result: FinalResult;
1453
+ error_log: string[]; // Remove - debugging
1454
+ metrics: Metrics; // Remove - optional
1455
+ }
1456
+
1457
+ // After: Minimal state (≤15 fields)
1458
+ interface State {
1459
+ status: 'pending' | 'running' | 'completed' | 'failed';
1460
+ target: { name: string; path: string };
1461
+ input_summary: string; // Summarized user input
1462
+ result_path: string; // Path to final result
1463
+ quality_gate: 'pass' | 'fail';
1464
+ error?: string; // Only if failed
1465
+ }
1466
+ ```
1467
+
1468
+ **Audit Checklist**:
1469
+ ```javascript
1470
+ function auditStateFields(stateSchema) {
1471
+ const removeCandidates = [];
1472
+
1473
+ for (const [key, type] of Object.entries(stateSchema)) {
1474
+ // Identify removal candidates
1475
+ if (key.startsWith('debug_')) removeCandidates.push(key);
1476
+ if (key.endsWith('_cache')) removeCandidates.push(key);
1477
+ if (key.endsWith('_temp')) removeCandidates.push(key);
1478
+ if (key.includes('intermediate')) removeCandidates.push(key);
1479
+ if (key.includes('step') && key.includes('output')) removeCandidates.push(key);
1480
+ }
1481
+
1482
+ return {
1483
+ total_fields: Object.keys(stateSchema).length,
1484
+ remove_candidates: removeCandidates,
1485
+ estimated_reduction: removeCandidates.length
1486
+ };
1487
+ }
1488
+ ```
1489
+
1490
+ **Risk**: Medium (ensure no essential data removed)
1491
+ **Verification**: State field count ≤ 15, all essential data preserved
1492
+
1493
+ ---
1494
+
1495
+ ### Strategy: in_memory_consolidation
1496
+
1497
+ **Purpose**: Consolidate outputs into single file, eliminate redundant report files.
1498
+
1499
+ **Implementation**:
1500
+ ```javascript
1501
+ // Before: Multiple output files
1502
+ Write(`${workDir}/diagnosis-report.md`, reportMarkdown);
1503
+ Write(`${workDir}/diagnosis-summary.json`, summaryJson);
1504
+ Write(`${workDir}/state.json`, JSON.stringify(state));
1505
+ Write(`${workDir}/tuning-report.md`, tuningReport);
1506
+ Write(`${workDir}/tuning-summary.md`, finalSummary);
1507
+
1508
+ // After: Single source of truth
1509
+ const consolidatedState = {
1510
+ ...state,
1511
+ final_report: {
1512
+ summary: summaryJson,
1513
+ details_available_in_state: true,
1514
+ generated_at: new Date().toISOString()
1515
+ }
1516
+ };
1517
+ Write(`${workDir}/state.json`, JSON.stringify(consolidatedState, null, 2));
1518
+
1519
+ // Report can be rendered from state on-demand
1520
+ function renderReport(state) {
1521
+ return `
1522
+ # Tuning Report: ${state.target_skill.name}
1523
+ Status: ${state.status}
1524
+ Quality: ${state.quality_gate}
1525
+ Issues: ${state.issues.length}
1526
+ ...
1527
+ `;
1528
+ }
1529
+ ```
1530
+
1531
+ **Benefits**:
1532
+ - Single file to read/write
1533
+ - No data duplication
1534
+ - On-demand rendering
1535
+
1536
+ **Risk**: Low
1537
+ **Verification**: Only state.json exists as output, rendering works correctly