claude-coder 1.9.0 → 1.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +214 -214
  2. package/bin/cli.js +155 -155
  3. package/package.json +55 -55
  4. package/recipes/_shared/roles/developer.md +11 -11
  5. package/recipes/_shared/roles/product.md +12 -12
  6. package/recipes/_shared/roles/tester.md +12 -12
  7. package/recipes/_shared/test/report-format.md +86 -86
  8. package/recipes/backend/base.md +27 -27
  9. package/recipes/backend/components/auth.md +18 -18
  10. package/recipes/backend/components/crud-api.md +18 -18
  11. package/recipes/backend/components/file-service.md +15 -15
  12. package/recipes/backend/manifest.json +20 -20
  13. package/recipes/backend/test/api-test.md +25 -25
  14. package/recipes/console/base.md +37 -37
  15. package/recipes/console/components/modal-form.md +20 -20
  16. package/recipes/console/components/pagination.md +17 -17
  17. package/recipes/console/components/search.md +17 -17
  18. package/recipes/console/components/table-list.md +18 -18
  19. package/recipes/console/components/tabs.md +14 -14
  20. package/recipes/console/components/tree.md +15 -15
  21. package/recipes/console/components/upload.md +15 -15
  22. package/recipes/console/manifest.json +24 -24
  23. package/recipes/console/test/crud-e2e.md +47 -47
  24. package/recipes/h5/base.md +26 -26
  25. package/recipes/h5/components/animation.md +11 -11
  26. package/recipes/h5/components/countdown.md +11 -11
  27. package/recipes/h5/components/share.md +11 -11
  28. package/recipes/h5/components/swiper.md +11 -11
  29. package/recipes/h5/manifest.json +21 -21
  30. package/recipes/h5/test/h5-e2e.md +20 -20
  31. package/src/commands/auth.js +420 -362
  32. package/src/commands/setup-modules/helpers.js +100 -100
  33. package/src/commands/setup-modules/index.js +25 -25
  34. package/src/commands/setup-modules/mcp.js +115 -115
  35. package/src/commands/setup-modules/provider.js +260 -260
  36. package/src/commands/setup-modules/safety.js +47 -47
  37. package/src/commands/setup-modules/simplify.js +52 -52
  38. package/src/commands/setup.js +172 -172
  39. package/src/common/assets.js +245 -245
  40. package/src/common/config.js +125 -125
  41. package/src/common/constants.js +55 -55
  42. package/src/common/indicator.js +260 -260
  43. package/src/common/interaction.js +170 -170
  44. package/src/common/logging.js +77 -77
  45. package/src/common/sdk.js +50 -50
  46. package/src/common/tasks.js +88 -88
  47. package/src/common/utils.js +213 -213
  48. package/src/core/coding.js +33 -33
  49. package/src/core/go.js +264 -264
  50. package/src/core/hooks.js +500 -500
  51. package/src/core/init.js +166 -165
  52. package/src/core/plan.js +188 -187
  53. package/src/core/prompts.js +247 -247
  54. package/src/core/repair.js +36 -36
  55. package/src/core/runner.js +471 -458
  56. package/src/core/scan.js +93 -93
  57. package/src/core/session.js +280 -271
  58. package/src/core/simplify.js +74 -74
  59. package/src/core/state.js +105 -105
  60. package/src/index.js +76 -76
  61. package/templates/bash-process.md +12 -12
  62. package/templates/codingSystem.md +65 -65
  63. package/templates/codingUser.md +17 -17
  64. package/templates/coreProtocol.md +29 -29
  65. package/templates/goSystem.md +130 -130
  66. package/templates/guidance.json +72 -72
  67. package/templates/planSystem.md +78 -78
  68. package/templates/planUser.md +8 -8
  69. package/templates/requirements.example.md +57 -57
  70. package/templates/scanSystem.md +120 -120
  71. package/templates/scanUser.md +10 -10
  72. package/templates/test_rule.md +194 -194
  73. package/templates/web-testing.md +17 -17
  74. package/types/index.d.ts +217 -217
package/src/core/hooks.js CHANGED
@@ -1,501 +1,501 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
- const { inferPhaseStep } = require('../common/indicator');
6
- const { log } = require('../common/config');
7
- const { EDIT_THRESHOLD } = require('../common/constants');
8
- const { createAskUserQuestionHook } = require('../common/interaction');
9
- const { assets } = require('../common/assets');
10
- const { localTimestamp } = require('../common/utils');
11
- // ─────────────────────────────────────────────────────────────
12
- // Constants
13
- // ─────────────────────────────────────────────────────────────
14
-
15
- const DEFAULT_EDIT_THRESHOLD = EDIT_THRESHOLD;
16
-
17
- // Feature name constants
18
- const FEATURES = Object.freeze({
19
- GUIDANCE: 'guidance',
20
- EDIT_GUARD: 'editGuard',
21
- STOP: 'stop',
22
- STALL: 'stall',
23
- INTERACTION: 'interaction',
24
- });
25
-
26
- // ─────────────────────────────────────────────────────────────
27
- // GuidanceInjector: JSON-based configurable guidance system
28
- // ─────────────────────────────────────────────────────────────
29
-
30
- class GuidanceInjector {
31
- constructor() {
32
- this.rules = [];
33
- this.cache = {};
34
- this.injectedRules = new Set(); // Track which rules have been injected once
35
- this.loaded = false;
36
- }
37
-
38
- /**
39
- * Load rules from user's guidance.json and pre-compile regex patterns.
40
- */
41
- load() {
42
- if (this.loaded) return;
43
-
44
- try {
45
- const content = assets.read('guidance');
46
- const config = JSON.parse(content);
47
- this.rules = config.rules || [];
48
- } catch {
49
- this.rules = [];
50
- }
51
-
52
- this._compiledMatchers = new Map();
53
- this._compiledConditions = new Map();
54
- for (const rule of this.rules) {
55
- try {
56
- this._compiledMatchers.set(rule.name, new RegExp(rule.matcher));
57
- } catch {
58
- this._compiledMatchers.set(rule.name, null);
59
- }
60
- if (rule.condition?.pattern !== undefined) {
61
- try {
62
- this._compiledConditions.set(rule.name, new RegExp(rule.condition.pattern, 'i'));
63
- } catch {
64
- this._compiledConditions.set(rule.name, null);
65
- }
66
- }
67
- }
68
-
69
- this.loaded = true;
70
- }
71
-
72
- /**
73
- * Get nested field value from object
74
- * @param {object} obj - Source object
75
- * @param {string} fieldPath - Dot-separated path (e.g., "tool_input.command")
76
- */
77
- getFieldValue(obj, fieldPath) {
78
- return fieldPath.split('.').reduce((o, k) => o?.[k], obj);
79
- }
80
-
81
- /**
82
- * Check if condition matches
83
- * Supports: { field, pattern } or { any: [...] }
84
- * @param {string} [ruleName] - Rule name for looking up pre-compiled regex
85
- */
86
- matchCondition(input, condition, ruleName) {
87
- if (!condition) return true;
88
-
89
- if (condition.field && condition.pattern !== undefined) {
90
- const value = this.getFieldValue(input, condition.field);
91
- const re = (ruleName && this._compiledConditions?.get(ruleName)) ||
92
- new RegExp(condition.pattern, 'i');
93
- return re.test(String(value || ''));
94
- }
95
-
96
- if (condition.any && Array.isArray(condition.any)) {
97
- return condition.any.some(c => this.matchCondition(input, c));
98
- }
99
-
100
- return true;
101
- }
102
-
103
- /**
104
- * Extract tool tip key from tool name
105
- * @param {string} toolName - Full tool name (e.g., "mcp__playwright__browser_snapshot")
106
- * @param {string} extractor - Regex pattern to extract key
107
- */
108
- extractToolTipKey(toolName, extractor) {
109
- if (!extractor) return null;
110
- const match = toolName.match(new RegExp(extractor));
111
- return match ? match[1] : null;
112
- }
113
-
114
- /**
115
- * Get rule file content
116
- * @param {object|string} file - File config or path string
117
- * @param {string} basePath - Base directory for relative paths
118
- */
119
- getFileContent(file, basePath) {
120
- if (!file) return null;
121
-
122
- const filePath = typeof file === 'string' ? file : file.path;
123
- const absolutePath = path.isAbsolute(filePath)
124
- ? filePath
125
- : path.join(basePath, filePath);
126
-
127
- try {
128
- return fs.readFileSync(absolutePath, 'utf8');
129
- } catch {
130
- return null;
131
- }
132
- }
133
-
134
- /**
135
- * Process a single rule and return guidance content
136
- */
137
- processRule(rule, input, basePath) {
138
- const matcherRe = this._compiledMatchers?.get(rule.name) ?? new RegExp(rule.matcher);
139
- if (!matcherRe.test(input.tool_name)) {
140
- return null;
141
- }
142
-
143
- if (!this.matchCondition(input, rule.condition, rule.name)) {
144
- return null;
145
- }
146
-
147
- const result = { guidance: '', tip: '' };
148
-
149
- // Process file content
150
- if (rule.file) {
151
- const fileConfig = typeof rule.file === 'object' ? rule.file : { path: rule.file };
152
- const injectOnce = fileConfig.injectOnce === true;
153
- const ruleKey = `${rule.name}_file`;
154
-
155
- // Skip if already injected and injectOnce is true
156
- if (injectOnce && this.injectedRules.has(ruleKey)) {
157
- // Don't inject file content again
158
- } else {
159
- if (injectOnce) this.injectedRules.add(ruleKey);
160
-
161
- // Get cached content or read file
162
- const cacheKey = `${rule.name}_content`;
163
- if (!this.cache[cacheKey]) {
164
- this.cache[cacheKey] = this.getFileContent(fileConfig.path, basePath);
165
- }
166
- result.guidance = this.cache[cacheKey] || '';
167
- }
168
- }
169
-
170
- // Process tool tips
171
- if (rule.toolTips && rule.toolTips.items) {
172
- const tipKey = this.extractToolTipKey(input.tool_name, rule.toolTips.extractor);
173
- if (tipKey && rule.toolTips.items[tipKey]) {
174
- const tipInjectOnce = rule.toolTips.injectOnce !== false; // Default true
175
- const tipRuleKey = `${rule.name}_tip_${tipKey}`;
176
-
177
- if (!tipInjectOnce || !this.injectedRules.has(tipRuleKey)) {
178
- if (tipInjectOnce) this.injectedRules.add(tipRuleKey);
179
- result.tip = rule.toolTips.items[tipKey];
180
- }
181
- }
182
- }
183
-
184
- return result;
185
- }
186
-
187
- /**
188
- * Reset per-session state for clean session boundaries.
189
- * Also clears loaded flag so guidance.json is re-read on next hook call.
190
- */
191
- reset() {
192
- this.injectedRules.clear();
193
- this.cache = {};
194
- this.loaded = false;
195
- }
196
-
197
- /**
198
- * Create hook function for PreToolUse
199
- */
200
- createHook() {
201
- const basePath = assets.dir('loop');
202
-
203
- return async (input, _toolUseID, _context) => {
204
- this.load();
205
-
206
- if (this.rules.length === 0) return {};
207
-
208
- const guidanceParts = [];
209
- const tipParts = [];
210
-
211
- for (const rule of this.rules) {
212
- const result = this.processRule(rule, input, basePath);
213
- if (result) {
214
- if (result.guidance) guidanceParts.push(result.guidance);
215
- if (result.tip) tipParts.push(result.tip);
216
- }
217
- }
218
-
219
- const allParts = [...guidanceParts, ...tipParts];
220
- if (allParts.length === 0) return {};
221
-
222
- return {
223
- hookSpecificOutput: {
224
- hookEventName: 'PreToolUse',
225
- additionalContext: allParts.join('\n\n'),
226
- }
227
- };
228
- };
229
- }
230
- }
231
-
232
- // Shared instance (reset per session via createGuidanceModule)
233
- const guidanceInjector = new GuidanceInjector();
234
-
235
- // ─────────────────────────────────────────────────────────────
236
- // Utility Functions
237
- // ─────────────────────────────────────────────────────────────
238
-
239
-
240
- function logToolCall(logStream, input) {
241
- if (!logStream) return;
242
- const target = input.tool_input?.file_path || input.tool_input?.path || '';
243
- const cmd = input.tool_input?.command || '';
244
- const pattern = input.tool_input?.pattern || '';
245
- const detail = target || cmd.slice(0, 200) || (pattern ? `pattern: ${pattern}` : '');
246
- if (detail) {
247
- logStream.write(`[${localTimestamp()}] ${input.tool_name}: ${detail}\n`);
248
- }
249
- }
250
-
251
- // ─────────────────────────────────────────────────────────────
252
- // Module Factories
253
- // ─────────────────────────────────────────────────────────────
254
-
255
- /**
256
- * Create guidance injection module.
257
- * Resets the shared injector's per-session state to prevent cross-session leaks.
258
- */
259
- function createGuidanceModule() {
260
- guidanceInjector.reset();
261
- return {
262
- hook: guidanceInjector.createHook()
263
- };
264
- }
265
-
266
- /**
267
- * Create edit guard module.
268
- * Uses a sliding time window: edits older than cooldownMs are decayed,
269
- * allowing the model to resume editing after a "thinking" break.
270
- */
271
- function createEditGuardModule(options) {
272
- const editTimestamps = {};
273
- const threshold = options.editThreshold || DEFAULT_EDIT_THRESHOLD;
274
- const cooldownMs = options.editCooldownMs || 60000;
275
-
276
- return {
277
- hook: async (input, _toolUseID, _context) => {
278
- if (!['Write', 'Edit', 'MultiEdit'].includes(input.tool_name)) return {};
279
- const target = input.tool_input?.file_path || input.tool_input?.path || '';
280
- if (!target) return {};
281
-
282
- const now = Date.now();
283
- if (!editTimestamps[target]) editTimestamps[target] = [];
284
-
285
- editTimestamps[target] = editTimestamps[target].filter(t => now - t < cooldownMs);
286
- editTimestamps[target].push(now);
287
-
288
- if (editTimestamps[target].length > threshold) {
289
- return {
290
- hookSpecificOutput: {
291
- hookEventName: 'PreToolUse',
292
- permissionDecision: 'deny',
293
- permissionDecisionReason:
294
- `${cooldownMs / 1000}s 内对 ${target} 编辑 ${editTimestamps[target].length} 次(上限 ${threshold}),疑似死循环。请重新审视方案后再继续。`
295
- }
296
- };
297
- }
298
- return {};
299
- }
300
- };
301
- }
302
-
303
- /**
304
- * Create stall detection module (idle timeout only)
305
- */
306
- function createStallModule(indicator, logStream, options) {
307
- let stallDetected = false;
308
- let stallChecker = null;
309
- const timeoutMs = options.stallTimeoutMs || 1200000;
310
- const abortController = options.abortController;
311
-
312
- const checkStall = () => {
313
- const now = Date.now();
314
- const idleMs = now - indicator.lastActivityTime;
315
-
316
- if (idleMs > timeoutMs && !stallDetected) {
317
- stallDetected = true;
318
- const idleMin = Math.floor(idleMs / 60000);
319
- log('warn', `\n无响应超过 ${idleMin} 分钟,自动中断 session`);
320
- if (logStream) {
321
- logStream.write(`\n[${localTimestamp()}] STALL: 无响应 ${idleMin} 分钟,自动中断\n`);
322
- }
323
- if (abortController) {
324
- abortController.abort();
325
- log('warn', '\n已发送中断信号');
326
- }
327
- }
328
- };
329
-
330
- stallChecker = setInterval(checkStall, 30000);
331
-
332
- return {
333
- cleanup: () => { if (stallChecker) clearInterval(stallChecker); },
334
- isStalled: () => stallDetected
335
- };
336
- }
337
-
338
- /**
339
- * Create Stop hook — per-turn activity logger.
340
- * Stop fires on EVERY model response turn (not just session end).
341
- * For session completion detection, use the result message (SDKResultMessage.subtype).
342
- */
343
- function createStopHook(logStream) {
344
- return async (_input) => {
345
- if (logStream?.writable) {
346
- logStream.write(`[${localTimestamp()}] STOP: turn completed\n`);
347
- }
348
- return {};
349
- };
350
- }
351
-
352
- /**
353
- * Create PostToolUse hook — resets tool running state and activity timer.
354
- * Unified for all session types (replaces the former createCompletionModule).
355
- */
356
- function createEndToolHook(indicator) {
357
- return async (_input, _toolUseID, _context) => {
358
- indicator.endTool();
359
- indicator.updatePhase('thinking');
360
- return {};
361
- };
362
- }
363
-
364
- /**
365
- * Create PostToolUseFailure hook to ensure endTool on tool errors
366
- */
367
- function createFailureHook(indicator) {
368
- return async (_input, _toolUseID, _context) => {
369
- indicator.endTool();
370
- return {};
371
- };
372
- }
373
-
374
- /**
375
- * Create logging hook
376
- */
377
- function createLoggingHook(indicator, logStream) {
378
- return async (input, _toolUseID, _context) => {
379
- inferPhaseStep(indicator, input.tool_name, input.tool_input);
380
- logToolCall(logStream, input);
381
- return {};
382
- };
383
- }
384
-
385
- // ─────────────────────────────────────────────────────────────
386
- // Hook Factory: createHooks
387
- // ─────────────────────────────────────────────────────────────
388
-
389
- const FEATURE_MAP = {
390
- coding: [FEATURES.GUIDANCE, FEATURES.EDIT_GUARD, FEATURES.STOP, FEATURES.STALL],
391
- plan: [FEATURES.STOP, FEATURES.STALL],
392
- plan_interactive: [FEATURES.STOP, FEATURES.STALL, FEATURES.INTERACTION],
393
- scan: [FEATURES.STOP, FEATURES.STALL],
394
- add: [FEATURES.STOP, FEATURES.STALL],
395
- simplify: [FEATURES.STOP, FEATURES.STALL, FEATURES.INTERACTION],
396
- go: [FEATURES.STOP, FEATURES.STALL, FEATURES.INTERACTION],
397
- custom: null
398
- };
399
-
400
- /**
401
- * Create hooks based on session type
402
- */
403
- function createHooks(type, indicator, logStream, options = {}) {
404
- const features = type === 'custom'
405
- ? (options.features || [FEATURES.STALL])
406
- : (FEATURE_MAP[type] || [FEATURES.STALL]);
407
-
408
- const modules = {};
409
-
410
- if (features.includes(FEATURES.STALL)) {
411
- modules.stall = createStallModule(indicator, logStream, options);
412
- }
413
-
414
- if (features.includes(FEATURES.STOP)) {
415
- modules.stopHook = createStopHook(logStream);
416
- }
417
-
418
- if (features.includes(FEATURES.EDIT_GUARD)) {
419
- modules.editGuard = createEditGuardModule(options);
420
- }
421
-
422
- if (features.includes(FEATURES.GUIDANCE)) {
423
- modules.guidance = createGuidanceModule();
424
- }
425
-
426
- if (features.includes(FEATURES.INTERACTION)) {
427
- modules.interaction = { hook: createAskUserQuestionHook(indicator) };
428
- }
429
-
430
- // Assemble PreToolUse hooks
431
- const preToolUseHooks = [];
432
- preToolUseHooks.push(createLoggingHook(indicator, logStream));
433
-
434
- if (modules.editGuard) {
435
- preToolUseHooks.push(modules.editGuard.hook);
436
- }
437
-
438
- if (modules.guidance) {
439
- preToolUseHooks.push(modules.guidance.hook);
440
- }
441
-
442
- if (modules.interaction) {
443
- preToolUseHooks.push(modules.interaction.hook);
444
- }
445
-
446
- // PostToolUse: unified endTool for all session types
447
- const endToolHook = createEndToolHook(indicator);
448
-
449
- // PostToolUseFailure: ensure endTool even on tool errors
450
- const failureHook = createFailureHook(indicator);
451
-
452
- // Build hooks object
453
- const hooks = {};
454
- if (preToolUseHooks.length > 0) {
455
- hooks.PreToolUse = [{ matcher: '*', hooks: preToolUseHooks }];
456
- }
457
- hooks.PostToolUse = [{ matcher: '*', hooks: [endToolHook] }];
458
- hooks.PostToolUseFailure = [{ matcher: '*', hooks: [failureHook] }];
459
-
460
- // Stop hook: per-turn activity logger
461
- if (modules.stopHook) {
462
- hooks.Stop = [{ hooks: [modules.stopHook] }];
463
- }
464
-
465
- // SessionStart hook: log query lifecycle
466
- const sessionStartHook = async (input) => {
467
- indicator.updateActivity();
468
- if (logStream?.writable) {
469
- logStream.write(`[${localTimestamp()}] SESSION_START: source=${input.source || 'unknown'}\n`);
470
- }
471
- return {};
472
- };
473
- hooks.SessionStart = [{ hooks: [sessionStartHook] }];
474
-
475
- // Cleanup functions
476
- const cleanupFns = [];
477
- if (modules.stall) {
478
- cleanupFns.push(modules.stall.cleanup);
479
- }
480
-
481
- return {
482
- hooks,
483
- cleanup: () => cleanupFns.forEach(fn => fn()),
484
- isStalled: () => modules.stall?.isStalled() || false,
485
- };
486
- }
487
-
488
- // ─────────────────────────────────────────────────────────────
489
- // Exports
490
- // ─────────────────────────────────────────────────────────────
491
-
492
- module.exports = {
493
- createHooks,
494
- GuidanceInjector,
495
- createGuidanceModule,
496
- createEditGuardModule,
497
- createStopHook,
498
- createEndToolHook,
499
- createStallModule,
500
- FEATURES,
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const { inferPhaseStep } = require('../common/indicator');
6
+ const { log } = require('../common/config');
7
+ const { EDIT_THRESHOLD } = require('../common/constants');
8
+ const { createAskUserQuestionHook } = require('../common/interaction');
9
+ const { assets } = require('../common/assets');
10
+ const { localTimestamp } = require('../common/utils');
11
+ // ─────────────────────────────────────────────────────────────
12
+ // Constants
13
+ // ─────────────────────────────────────────────────────────────
14
+
15
+ const DEFAULT_EDIT_THRESHOLD = EDIT_THRESHOLD;
16
+
17
+ // Feature name constants
18
+ const FEATURES = Object.freeze({
19
+ GUIDANCE: 'guidance',
20
+ EDIT_GUARD: 'editGuard',
21
+ STOP: 'stop',
22
+ STALL: 'stall',
23
+ INTERACTION: 'interaction',
24
+ });
25
+
26
+ // ─────────────────────────────────────────────────────────────
27
+ // GuidanceInjector: JSON-based configurable guidance system
28
+ // ─────────────────────────────────────────────────────────────
29
+
30
+ class GuidanceInjector {
31
+ constructor() {
32
+ this.rules = [];
33
+ this.cache = {};
34
+ this.injectedRules = new Set(); // Track which rules have been injected once
35
+ this.loaded = false;
36
+ }
37
+
38
+ /**
39
+ * Load rules from user's guidance.json and pre-compile regex patterns.
40
+ */
41
+ load() {
42
+ if (this.loaded) return;
43
+
44
+ try {
45
+ const content = assets.read('guidance');
46
+ const config = JSON.parse(content);
47
+ this.rules = config.rules || [];
48
+ } catch {
49
+ this.rules = [];
50
+ }
51
+
52
+ this._compiledMatchers = new Map();
53
+ this._compiledConditions = new Map();
54
+ for (const rule of this.rules) {
55
+ try {
56
+ this._compiledMatchers.set(rule.name, new RegExp(rule.matcher));
57
+ } catch {
58
+ this._compiledMatchers.set(rule.name, null);
59
+ }
60
+ if (rule.condition?.pattern !== undefined) {
61
+ try {
62
+ this._compiledConditions.set(rule.name, new RegExp(rule.condition.pattern, 'i'));
63
+ } catch {
64
+ this._compiledConditions.set(rule.name, null);
65
+ }
66
+ }
67
+ }
68
+
69
+ this.loaded = true;
70
+ }
71
+
72
+ /**
73
+ * Get nested field value from object
74
+ * @param {object} obj - Source object
75
+ * @param {string} fieldPath - Dot-separated path (e.g., "tool_input.command")
76
+ */
77
+ getFieldValue(obj, fieldPath) {
78
+ return fieldPath.split('.').reduce((o, k) => o?.[k], obj);
79
+ }
80
+
81
+ /**
82
+ * Check if condition matches
83
+ * Supports: { field, pattern } or { any: [...] }
84
+ * @param {string} [ruleName] - Rule name for looking up pre-compiled regex
85
+ */
86
+ matchCondition(input, condition, ruleName) {
87
+ if (!condition) return true;
88
+
89
+ if (condition.field && condition.pattern !== undefined) {
90
+ const value = this.getFieldValue(input, condition.field);
91
+ const re = (ruleName && this._compiledConditions?.get(ruleName)) ||
92
+ new RegExp(condition.pattern, 'i');
93
+ return re.test(String(value || ''));
94
+ }
95
+
96
+ if (condition.any && Array.isArray(condition.any)) {
97
+ return condition.any.some(c => this.matchCondition(input, c));
98
+ }
99
+
100
+ return true;
101
+ }
102
+
103
+ /**
104
+ * Extract tool tip key from tool name
105
+ * @param {string} toolName - Full tool name (e.g., "mcp__playwright__browser_snapshot")
106
+ * @param {string} extractor - Regex pattern to extract key
107
+ */
108
+ extractToolTipKey(toolName, extractor) {
109
+ if (!extractor) return null;
110
+ const match = toolName.match(new RegExp(extractor));
111
+ return match ? match[1] : null;
112
+ }
113
+
114
+ /**
115
+ * Get rule file content
116
+ * @param {object|string} file - File config or path string
117
+ * @param {string} basePath - Base directory for relative paths
118
+ */
119
+ getFileContent(file, basePath) {
120
+ if (!file) return null;
121
+
122
+ const filePath = typeof file === 'string' ? file : file.path;
123
+ const absolutePath = path.isAbsolute(filePath)
124
+ ? filePath
125
+ : path.join(basePath, filePath);
126
+
127
+ try {
128
+ return fs.readFileSync(absolutePath, 'utf8');
129
+ } catch {
130
+ return null;
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Process a single rule and return guidance content
136
+ */
137
+ processRule(rule, input, basePath) {
138
+ const matcherRe = this._compiledMatchers?.get(rule.name) ?? new RegExp(rule.matcher);
139
+ if (!matcherRe.test(input.tool_name)) {
140
+ return null;
141
+ }
142
+
143
+ if (!this.matchCondition(input, rule.condition, rule.name)) {
144
+ return null;
145
+ }
146
+
147
+ const result = { guidance: '', tip: '' };
148
+
149
+ // Process file content
150
+ if (rule.file) {
151
+ const fileConfig = typeof rule.file === 'object' ? rule.file : { path: rule.file };
152
+ const injectOnce = fileConfig.injectOnce === true;
153
+ const ruleKey = `${rule.name}_file`;
154
+
155
+ // Skip if already injected and injectOnce is true
156
+ if (injectOnce && this.injectedRules.has(ruleKey)) {
157
+ // Don't inject file content again
158
+ } else {
159
+ if (injectOnce) this.injectedRules.add(ruleKey);
160
+
161
+ // Get cached content or read file
162
+ const cacheKey = `${rule.name}_content`;
163
+ if (!this.cache[cacheKey]) {
164
+ this.cache[cacheKey] = this.getFileContent(fileConfig.path, basePath);
165
+ }
166
+ result.guidance = this.cache[cacheKey] || '';
167
+ }
168
+ }
169
+
170
+ // Process tool tips
171
+ if (rule.toolTips && rule.toolTips.items) {
172
+ const tipKey = this.extractToolTipKey(input.tool_name, rule.toolTips.extractor);
173
+ if (tipKey && rule.toolTips.items[tipKey]) {
174
+ const tipInjectOnce = rule.toolTips.injectOnce !== false; // Default true
175
+ const tipRuleKey = `${rule.name}_tip_${tipKey}`;
176
+
177
+ if (!tipInjectOnce || !this.injectedRules.has(tipRuleKey)) {
178
+ if (tipInjectOnce) this.injectedRules.add(tipRuleKey);
179
+ result.tip = rule.toolTips.items[tipKey];
180
+ }
181
+ }
182
+ }
183
+
184
+ return result;
185
+ }
186
+
187
+ /**
188
+ * Reset per-session state for clean session boundaries.
189
+ * Also clears loaded flag so guidance.json is re-read on next hook call.
190
+ */
191
+ reset() {
192
+ this.injectedRules.clear();
193
+ this.cache = {};
194
+ this.loaded = false;
195
+ }
196
+
197
+ /**
198
+ * Create hook function for PreToolUse
199
+ */
200
+ createHook() {
201
+ const basePath = assets.dir('loop');
202
+
203
+ return async (input, _toolUseID, _context) => {
204
+ this.load();
205
+
206
+ if (this.rules.length === 0) return {};
207
+
208
+ const guidanceParts = [];
209
+ const tipParts = [];
210
+
211
+ for (const rule of this.rules) {
212
+ const result = this.processRule(rule, input, basePath);
213
+ if (result) {
214
+ if (result.guidance) guidanceParts.push(result.guidance);
215
+ if (result.tip) tipParts.push(result.tip);
216
+ }
217
+ }
218
+
219
+ const allParts = [...guidanceParts, ...tipParts];
220
+ if (allParts.length === 0) return {};
221
+
222
+ return {
223
+ hookSpecificOutput: {
224
+ hookEventName: 'PreToolUse',
225
+ additionalContext: allParts.join('\n\n'),
226
+ }
227
+ };
228
+ };
229
+ }
230
+ }
231
+
232
+ // Shared instance (reset per session via createGuidanceModule)
233
+ const guidanceInjector = new GuidanceInjector();
234
+
235
+ // ─────────────────────────────────────────────────────────────
236
+ // Utility Functions
237
+ // ─────────────────────────────────────────────────────────────
238
+
239
+
240
+ function logToolCall(logStream, input) {
241
+ if (!logStream) return;
242
+ const target = input.tool_input?.file_path || input.tool_input?.path || '';
243
+ const cmd = input.tool_input?.command || '';
244
+ const pattern = input.tool_input?.pattern || '';
245
+ const detail = target || cmd.slice(0, 200) || (pattern ? `pattern: ${pattern}` : '');
246
+ if (detail) {
247
+ logStream.write(`[${localTimestamp()}] ${input.tool_name}: ${detail}\n`);
248
+ }
249
+ }
250
+
251
+ // ─────────────────────────────────────────────────────────────
252
+ // Module Factories
253
+ // ─────────────────────────────────────────────────────────────
254
+
255
+ /**
256
+ * Create guidance injection module.
257
+ * Resets the shared injector's per-session state to prevent cross-session leaks.
258
+ */
259
+ function createGuidanceModule() {
260
+ guidanceInjector.reset();
261
+ return {
262
+ hook: guidanceInjector.createHook()
263
+ };
264
+ }
265
+
266
+ /**
267
+ * Create edit guard module.
268
+ * Uses a sliding time window: edits older than cooldownMs are decayed,
269
+ * allowing the model to resume editing after a "thinking" break.
270
+ */
271
+ function createEditGuardModule(options) {
272
+ const editTimestamps = {};
273
+ const threshold = options.editThreshold || DEFAULT_EDIT_THRESHOLD;
274
+ const cooldownMs = options.editCooldownMs || 60000;
275
+
276
+ return {
277
+ hook: async (input, _toolUseID, _context) => {
278
+ if (!['Write', 'Edit', 'MultiEdit'].includes(input.tool_name)) return {};
279
+ const target = input.tool_input?.file_path || input.tool_input?.path || '';
280
+ if (!target) return {};
281
+
282
+ const now = Date.now();
283
+ if (!editTimestamps[target]) editTimestamps[target] = [];
284
+
285
+ editTimestamps[target] = editTimestamps[target].filter(t => now - t < cooldownMs);
286
+ editTimestamps[target].push(now);
287
+
288
+ if (editTimestamps[target].length > threshold) {
289
+ return {
290
+ hookSpecificOutput: {
291
+ hookEventName: 'PreToolUse',
292
+ permissionDecision: 'deny',
293
+ permissionDecisionReason:
294
+ `${cooldownMs / 1000}s 内对 ${target} 编辑 ${editTimestamps[target].length} 次(上限 ${threshold}),疑似死循环。请重新审视方案后再继续。`
295
+ }
296
+ };
297
+ }
298
+ return {};
299
+ }
300
+ };
301
+ }
302
+
303
+ /**
304
+ * Create stall detection module (idle timeout only)
305
+ */
306
+ function createStallModule(indicator, logStream, options) {
307
+ let stallDetected = false;
308
+ let stallChecker = null;
309
+ const timeoutMs = options.stallTimeoutMs || 1200000;
310
+ const abortController = options.abortController;
311
+
312
+ const checkStall = () => {
313
+ const now = Date.now();
314
+ const idleMs = now - indicator.lastActivityTime;
315
+
316
+ if (idleMs > timeoutMs && !stallDetected) {
317
+ stallDetected = true;
318
+ const idleMin = Math.floor(idleMs / 60000);
319
+ log('warn', `\n无响应超过 ${idleMin} 分钟,自动中断 session`);
320
+ if (logStream) {
321
+ logStream.write(`\n[${localTimestamp()}] STALL: 无响应 ${idleMin} 分钟,自动中断\n`);
322
+ }
323
+ if (abortController) {
324
+ abortController.abort();
325
+ log('warn', '\n已发送中断信号');
326
+ }
327
+ }
328
+ };
329
+
330
+ stallChecker = setInterval(checkStall, 30000);
331
+
332
+ return {
333
+ cleanup: () => { if (stallChecker) clearInterval(stallChecker); },
334
+ isStalled: () => stallDetected
335
+ };
336
+ }
337
+
338
+ /**
339
+ * Create Stop hook — per-turn activity logger.
340
+ * Stop fires on EVERY model response turn (not just session end).
341
+ * For session completion detection, use the result message (SDKResultMessage.subtype).
342
+ */
343
+ function createStopHook(logStream) {
344
+ return async (_input) => {
345
+ if (logStream?.writable) {
346
+ logStream.write(`[${localTimestamp()}] STOP: turn completed\n`);
347
+ }
348
+ return {};
349
+ };
350
+ }
351
+
352
+ /**
353
+ * Create PostToolUse hook — resets tool running state and activity timer.
354
+ * Unified for all session types (replaces the former createCompletionModule).
355
+ */
356
+ function createEndToolHook(indicator) {
357
+ return async (_input, _toolUseID, _context) => {
358
+ indicator.endTool();
359
+ indicator.updatePhase('thinking');
360
+ return {};
361
+ };
362
+ }
363
+
364
+ /**
365
+ * Create PostToolUseFailure hook to ensure endTool on tool errors
366
+ */
367
+ function createFailureHook(indicator) {
368
+ return async (_input, _toolUseID, _context) => {
369
+ indicator.endTool();
370
+ return {};
371
+ };
372
+ }
373
+
374
+ /**
375
+ * Create logging hook
376
+ */
377
+ function createLoggingHook(indicator, logStream) {
378
+ return async (input, _toolUseID, _context) => {
379
+ inferPhaseStep(indicator, input.tool_name, input.tool_input);
380
+ logToolCall(logStream, input);
381
+ return {};
382
+ };
383
+ }
384
+
385
+ // ─────────────────────────────────────────────────────────────
386
+ // Hook Factory: createHooks
387
+ // ─────────────────────────────────────────────────────────────
388
+
389
+ const FEATURE_MAP = {
390
+ coding: [FEATURES.GUIDANCE, FEATURES.EDIT_GUARD, FEATURES.STOP, FEATURES.STALL],
391
+ plan: [FEATURES.STOP, FEATURES.STALL],
392
+ plan_interactive: [FEATURES.STOP, FEATURES.STALL, FEATURES.INTERACTION],
393
+ scan: [FEATURES.STOP, FEATURES.STALL],
394
+ add: [FEATURES.STOP, FEATURES.STALL],
395
+ simplify: [FEATURES.STOP, FEATURES.STALL, FEATURES.INTERACTION],
396
+ go: [FEATURES.STOP, FEATURES.STALL, FEATURES.INTERACTION],
397
+ custom: null
398
+ };
399
+
400
+ /**
401
+ * Create hooks based on session type
402
+ */
403
+ function createHooks(type, indicator, logStream, options = {}) {
404
+ const features = type === 'custom'
405
+ ? (options.features || [FEATURES.STALL])
406
+ : (FEATURE_MAP[type] || [FEATURES.STALL]);
407
+
408
+ const modules = {};
409
+
410
+ if (features.includes(FEATURES.STALL)) {
411
+ modules.stall = createStallModule(indicator, logStream, options);
412
+ }
413
+
414
+ if (features.includes(FEATURES.STOP)) {
415
+ modules.stopHook = createStopHook(logStream);
416
+ }
417
+
418
+ if (features.includes(FEATURES.EDIT_GUARD)) {
419
+ modules.editGuard = createEditGuardModule(options);
420
+ }
421
+
422
+ if (features.includes(FEATURES.GUIDANCE)) {
423
+ modules.guidance = createGuidanceModule();
424
+ }
425
+
426
+ if (features.includes(FEATURES.INTERACTION)) {
427
+ modules.interaction = { hook: createAskUserQuestionHook(indicator) };
428
+ }
429
+
430
+ // Assemble PreToolUse hooks
431
+ const preToolUseHooks = [];
432
+ preToolUseHooks.push(createLoggingHook(indicator, logStream));
433
+
434
+ if (modules.editGuard) {
435
+ preToolUseHooks.push(modules.editGuard.hook);
436
+ }
437
+
438
+ if (modules.guidance) {
439
+ preToolUseHooks.push(modules.guidance.hook);
440
+ }
441
+
442
+ if (modules.interaction) {
443
+ preToolUseHooks.push(modules.interaction.hook);
444
+ }
445
+
446
+ // PostToolUse: unified endTool for all session types
447
+ const endToolHook = createEndToolHook(indicator);
448
+
449
+ // PostToolUseFailure: ensure endTool even on tool errors
450
+ const failureHook = createFailureHook(indicator);
451
+
452
+ // Build hooks object
453
+ const hooks = {};
454
+ if (preToolUseHooks.length > 0) {
455
+ hooks.PreToolUse = [{ matcher: '*', hooks: preToolUseHooks }];
456
+ }
457
+ hooks.PostToolUse = [{ matcher: '*', hooks: [endToolHook] }];
458
+ hooks.PostToolUseFailure = [{ matcher: '*', hooks: [failureHook] }];
459
+
460
+ // Stop hook: per-turn activity logger
461
+ if (modules.stopHook) {
462
+ hooks.Stop = [{ hooks: [modules.stopHook] }];
463
+ }
464
+
465
+ // SessionStart hook: log query lifecycle
466
+ const sessionStartHook = async (input) => {
467
+ indicator.updateActivity();
468
+ if (logStream?.writable) {
469
+ logStream.write(`[${localTimestamp()}] SESSION_START: source=${input.source || 'unknown'}\n`);
470
+ }
471
+ return {};
472
+ };
473
+ hooks.SessionStart = [{ hooks: [sessionStartHook] }];
474
+
475
+ // Cleanup functions
476
+ const cleanupFns = [];
477
+ if (modules.stall) {
478
+ cleanupFns.push(modules.stall.cleanup);
479
+ }
480
+
481
+ return {
482
+ hooks,
483
+ cleanup: () => cleanupFns.forEach(fn => fn()),
484
+ isStalled: () => modules.stall?.isStalled() || false,
485
+ };
486
+ }
487
+
488
+ // ─────────────────────────────────────────────────────────────
489
+ // Exports
490
+ // ─────────────────────────────────────────────────────────────
491
+
492
+ module.exports = {
493
+ createHooks,
494
+ GuidanceInjector,
495
+ createGuidanceModule,
496
+ createEditGuardModule,
497
+ createStopHook,
498
+ createEndToolHook,
499
+ createStallModule,
500
+ FEATURES,
501
501
  };