principles-disciple 1.93.0 → 1.95.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.
@@ -2,7 +2,7 @@
2
2
  "id": "principles-disciple",
3
3
  "name": "Principles Disciple",
4
4
  "description": "Evolutionary programming agent framework with strategic guardrails and reflection loops.",
5
- "version": "1.93.0",
5
+ "version": "1.95.0",
6
6
  "activation": {
7
7
  "onCapabilities": [
8
8
  "hook"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "principles-disciple",
3
- "version": "1.93.0",
3
+ "version": "1.95.0",
4
4
  "description": "Native OpenClaw plugin for Principles Disciple",
5
5
  "type": "module",
6
6
  "main": "./dist/bundle.js",
@@ -349,10 +349,31 @@ export async function handlePainReportCommand(ctx: PluginCommandContext): Promis
349
349
  };
350
350
  }
351
351
 
352
+ if (result.status === 'retried') {
353
+ const errorInfo = result.failureCategory
354
+ ? (isZh ? `\n⚠️ **错误类别**: ${result.failureCategory}` : `\n⚠️ **Error category**: ${result.failureCategory}`)
355
+ : '';
356
+ const messageInfo = result.message
357
+ ? (isZh ? `\n📝 **详情**: ${result.message}` : `\n📝 **Detail**: ${result.message}`)
358
+ : '';
359
+ return {
360
+ text: isZh
361
+ ? `✅ Pain 已记录,诊断任务已进入重试\n\n📋 **Pain ID**: ${result.painId}\n🔧 **Task ID**: ${result.taskId}${errorInfo}${messageInfo}\n\n诊断任务将在后台自动重试。使用 \`/pd-status\` 查看任务状态。`
362
+ : `✅ Pain recorded, diagnosis task entered retry\n\n📋 **Pain ID**: ${result.painId}\n🔧 **Task ID**: ${result.taskId}${errorInfo}${messageInfo}\n\nThe diagnosis task will retry automatically in the background. Use \`/pd-status\` to check task status.`,
363
+ };
364
+ }
365
+
366
+ // status === 'failed' | 'skipped' | 'degraded' — pain was NOT accepted
367
+ const reasonInfo = result.failureCategory
368
+ ? (isZh ? `\n⚠️ **原因**: ${result.failureCategory}` : `\n⚠️ **Reason**: ${result.failureCategory}`)
369
+ : '';
370
+ const messageInfo = result.message
371
+ ? (isZh ? `\n📝 **详情**: ${result.message}` : `\n📝 **Detail**: ${result.message}`)
372
+ : '';
352
373
  return {
353
374
  text: isZh
354
- ? `⚠️ Pain 记录未成功 (status: ${result.status})。请检查系统日志或使用 \`/pd-status\` 查看状态。`
355
- : `⚠️ Pain recording not accepted (status: ${result.status}). Check system logs or use \`/pd-status\` for status.`,
375
+ ? `❌ Pain 记录未成功 (status: ${result.status})${reasonInfo}${messageInfo}\n\n请检查系统日志或使用 \`/pd-status\` 查看状态。`
376
+ : `❌ Pain recording not accepted (status: ${result.status})${reasonInfo}${messageInfo}\n\nCheck system logs or use \`/pd-status\` for status.`,
356
377
  };
357
378
  } catch (err) {
358
379
  return {
@@ -0,0 +1,577 @@
1
+ /**
2
+ * After-Tool-Call Decomposition Helpers — PRI-326
3
+ *
4
+ * Extracted functions from handleAfterToolCall that implement
5
+ * individual pipeline stages. Each function has a focused responsibility.
6
+ *
7
+ * Pipeline order: classify → record → triage → gate → emit
8
+ *
9
+ * ERR checklist:
10
+ * - ERR-001: No `as` casts on untrusted runtime values.
11
+ * - ERR-002: Every skip/rejection includes structured reason + nextAction.
12
+ * - EP-01: Runtime values validated before use.
13
+ * - EP-03: No silent failures — every path is logged or structured.
14
+ */
15
+
16
+ import { isRisky, normalizePath } from '../utils/io.js';
17
+ import { normalizeProfile } from '../core/profile.js';
18
+ import { computePainScore, trackPrincipleValue } from '../core/pain.js';
19
+ import { getSession, trackFriction, resetFriction, getInjectedProbationIds, clearInjectedProbationIds, type SessionState } from '../core/session-tracker.js';
20
+ import { denoiseError, computeHash } from '../utils/hashing.js';
21
+ import { SystemLogger } from '../core/system-logger.js';
22
+ import { WorkspaceContext } from '../core/workspace-context.js';
23
+ import { getEvolutionLogger, createTraceId } from '../core/evolution-logger.js';
24
+ import { recordEvolutionSuccess, recordEvolutionFailure } from '../core/evolution-engine.js';
25
+ import type { PluginHookAfterToolCallEvent } from '../openclaw-sdk.js';
26
+ import { evaluatePainDiagnosticGate } from '../core/pain-diagnostic-gate.js';
27
+ import { sanitizeForEvidence, sanitizeToolParamsForEvidence } from './message-sanitize.js';
28
+ import { loadFeatureFlagFromConfig } from '../core/pd-config-loader.js';
29
+ import { resolveSourceKindFromToolFailure, evaluateEvidenceTriage } from './triage-adapter.js';
30
+ import { buildTrajectoryEvidence } from './trajectory-evidence.js';
31
+ import type { ToolCallOutcome, ToolCallObservation, PainAdmissionDecision } from './after-tool-call-types.js';
32
+
33
+ // ── Stage 1: Classify ───────────────────────────────────────────────────────
34
+
35
+ /**
36
+ * Classify the outcome of a tool call event.
37
+ *
38
+ * Pure function — no I/O, no side effects.
39
+ * Extracts exitCode logic, determines failure/success, classifies failure source.
40
+ */
41
+ export function classifyToolCallOutcome(event: PluginHookAfterToolCallEvent): ToolCallOutcome {
42
+ const resultObj = (event.result && typeof event.result === 'object') ? event.result as Record<string, unknown> : null;
43
+ const details = resultObj?.details && typeof resultObj.details === 'object' ? resultObj.details as Record<string, unknown> : null;
44
+ const topExitCode = resultObj?.exitCode;
45
+ const detailExitCode = details?.exitCode;
46
+
47
+ // Prefer the first *numeric* exit code
48
+ const exitCode = typeof topExitCode === 'number' ? topExitCode
49
+ : typeof detailExitCode === 'number' ? detailExitCode
50
+ : 0;
51
+ const isFailure = !!event.error || exitCode !== 0;
52
+
53
+ return {
54
+ isFailure,
55
+ exitCode,
56
+ failureSource: isFailure ? classifyToolFailureSource(event.toolName, event.error) : undefined,
57
+ };
58
+ }
59
+
60
+ // ── Stage 2: Build Observation ──────────────────────────────────────────────
61
+
62
+ /**
63
+ * Interface for tool parameters — avoids `any`.
64
+ */
65
+ interface ToolParams {
66
+ file_path?: string;
67
+ path?: string;
68
+ file?: string;
69
+ content?: string;
70
+ new_string?: string;
71
+ text?: string;
72
+ query?: string;
73
+ input?: string;
74
+ arguments?: string;
75
+ }
76
+
77
+ /**
78
+ * Build a normalized observation from the tool call event and workspace context.
79
+ *
80
+ * Combines file path normalization, risk classification, and error analysis.
81
+ * The profile parameter is passed in (loaded once at function scope).
82
+ */
83
+ export function buildToolCallObservation(
84
+ event: PluginHookAfterToolCallEvent,
85
+ outcome: ToolCallOutcome,
86
+ workspaceDir: string,
87
+ profile: ReturnType<typeof normalizeProfile>,
88
+ ): ToolCallObservation {
89
+ const rawParams = event.params;
90
+ const params: ToolParams = (rawParams && typeof rawParams === 'object' && !Array.isArray(rawParams))
91
+ ? rawParams as ToolParams
92
+ : {};
93
+ const filePath = params.file_path || params.path || params.file;
94
+ const relPath = typeof filePath === 'string' ? normalizePath(filePath, workspaceDir) : 'unknown';
95
+ const isRisk = isRisky(relPath, profile.risk_paths);
96
+ let errorText: string;
97
+ if (event.error) {
98
+ errorText = String(event.error);
99
+ } else if (typeof event.result === 'string') {
100
+ errorText = event.result;
101
+ } else {
102
+ try {
103
+ errorText = JSON.stringify(event.result);
104
+ } catch {
105
+ errorText = `[unserializable result: ${typeof event.result}]`;
106
+ }
107
+ }
108
+ const denoised = denoiseError(errorText);
109
+ const hash = computeHash(denoised);
110
+ const painScore = computePainScore(1, false, false, isRisk ? 20 : 0, workspaceDir);
111
+
112
+ return {
113
+ params: {
114
+ filePath,
115
+ content: params.content,
116
+ text: params.text,
117
+ newString: params.new_string,
118
+ query: params.query,
119
+ input: params.input,
120
+ arguments: params.arguments,
121
+ },
122
+ relPath,
123
+ isRisk,
124
+ errorType: extractErrorType(event.error || errorText),
125
+ errorHash: hash,
126
+ errorText,
127
+ painScore,
128
+ traceId: createTraceId(),
129
+ };
130
+ }
131
+
132
+ // ── Stage 3: Friction + Recording ──────────────────────────────────────────
133
+
134
+ /**
135
+ * Handle friction tracking for a tool failure.
136
+ *
137
+ * Updates GFI, records to event log and trajectory.
138
+ * Returns the updated session state and friction info.
139
+ */
140
+ export function handleFrictionTrackingForFailure(
141
+ sessionId: string,
142
+ event: PluginHookAfterToolCallEvent,
143
+ outcome: ToolCallOutcome,
144
+ observation: ToolCallObservation,
145
+ gfiBefore: number,
146
+ workspaceDir: string,
147
+ config: { get: (key: string) => unknown },
148
+ wctx: WorkspaceContext,
149
+ ): SessionState {
150
+ const deltaF = (config.get('scores.tool_failure_friction') as number) || 30;
151
+ const updatedState = trackFriction(sessionId, deltaF, observation.errorHash, workspaceDir, { source: outcome.failureSource });
152
+
153
+ recordEvolutionFailure(workspaceDir, event.toolName, {
154
+ filePath: observation.relPath,
155
+ reason: observation.isRisk ? 'risky' : 'tool',
156
+ sessionId,
157
+ });
158
+
159
+ // Record tool call failure event
160
+ wctx.eventLog.recordToolCall(sessionId, {
161
+ toolName: event.toolName,
162
+ filePath: observation.params.filePath,
163
+ error: event.error ? String(event.error).substring(0, 200) : undefined,
164
+ errorType: observation.errorType,
165
+ gfi: updatedState.currentGfi,
166
+ consecutiveErrors: updatedState.consecutiveErrors,
167
+ exitCode: outcome.exitCode as number | undefined,
168
+ gfiBefore,
169
+ gfiAfter: updatedState.currentGfi,
170
+ });
171
+
172
+ wctx.trajectory?.recordToolCall?.({
173
+ sessionId,
174
+ toolName: event.toolName,
175
+ outcome: 'failure',
176
+ durationMs: event.durationMs,
177
+ exitCode: outcome.exitCode as number | undefined,
178
+ errorType: observation.errorType,
179
+ errorMessage: event.error ? String(event.error) : undefined,
180
+ gfiBefore,
181
+ gfiAfter: updatedState.currentGfi,
182
+ paramsJson: sanitizeToolParamsForEvidence(event.params, workspaceDir),
183
+ });
184
+
185
+ return updatedState;
186
+ }
187
+
188
+ /**
189
+ * Handle friction relief and recording for a tool success.
190
+ *
191
+ * Relieves both tool_failure and dispatch_error GFI sources proportionally.
192
+ */
193
+ export function handleFrictionTrackingForSuccess(
194
+ sessionId: string,
195
+ event: PluginHookAfterToolCallEvent,
196
+ outcome: ToolCallOutcome,
197
+ observation: ToolCallObservation,
198
+ gfiBefore: number,
199
+ workspaceDir: string,
200
+ wctx: WorkspaceContext,
201
+ ): SessionState {
202
+ const session = getSession(sessionId);
203
+ const toolFailureGfi = session?.gfiBySource?.tool_failure || 0;
204
+ const dispatchErrorGfi = session?.gfiBySource?.dispatch_error || 0;
205
+
206
+ let resetState: SessionState = session || resetFriction(sessionId, workspaceDir);
207
+ if (toolFailureGfi > 0) {
208
+ resetState = resetFriction(sessionId, workspaceDir, {
209
+ source: 'tool_failure',
210
+ amount: toolFailureGfi * 0.5,
211
+ });
212
+ }
213
+ if (dispatchErrorGfi > 0) {
214
+ resetState = resetFriction(sessionId, workspaceDir, {
215
+ source: 'dispatch_error',
216
+ amount: dispatchErrorGfi * 0.5,
217
+ });
218
+ }
219
+
220
+ recordEvolutionSuccess(workspaceDir, event.toolName, {
221
+ sessionId,
222
+ reason: 'tool_success',
223
+ });
224
+
225
+ wctx.trajectory?.recordToolCall?.({
226
+ sessionId,
227
+ toolName: event.toolName,
228
+ outcome: 'success',
229
+ durationMs: event.durationMs,
230
+ exitCode: outcome.exitCode,
231
+ gfiBefore,
232
+ gfiAfter: resetState.currentGfi,
233
+ paramsJson: sanitizeToolParamsForEvidence(event.params, workspaceDir),
234
+ });
235
+
236
+ wctx.eventLog.recordToolCall(sessionId, {
237
+ toolName: event.toolName,
238
+ filePath: observation.params.filePath,
239
+ gfi: resetState.currentGfi,
240
+ gfiBefore,
241
+ gfiAfter: resetState.currentGfi,
242
+ });
243
+
244
+ return resetState;
245
+ }
246
+
247
+ // ── Stage 4: Hygiene Tracking ───────────────────────────────────────────────
248
+
249
+ /**
250
+ * Record hygiene tracking for memory/plan persistence actions on success.
251
+ */
252
+ export function recordHygieneTracking(
253
+ event: PluginHookAfterToolCallEvent,
254
+ observation: ToolCallObservation,
255
+ wctx: WorkspaceContext,
256
+ ): void {
257
+ const normalized = typeof observation.params.filePath === 'string' ? observation.params.filePath.replace(/\\/g, '/') : '';
258
+ const isMemory = /(?:^|\/)memory\//.test(normalized) || normalized.endsWith('/MEMORY.md') || normalized === 'MEMORY.md';
259
+ const isPlan = normalized === 'PLAN.md' || normalized.endsWith('/PLAN.md');
260
+
261
+ if (isMemory || isPlan) {
262
+ wctx.hygiene.recordPersistence({
263
+ ts: new Date().toISOString(),
264
+ tool: event.toolName,
265
+ path: observation.params.filePath ?? 'unknown',
266
+ type: isMemory ? 'memory' : 'plan',
267
+ contentLength: observation.params.content?.length ?? observation.params.newString?.length ?? 0,
268
+ });
269
+ }
270
+
271
+ // Special case for memory_store tool (Success only)
272
+ if (event.toolName === 'memory_store') {
273
+ wctx.hygiene.recordPersistence({
274
+ ts: new Date().toISOString(),
275
+ tool: event.toolName,
276
+ path: 'DATABASE',
277
+ type: 'memory',
278
+ contentLength: observation.params.text?.length ?? 0,
279
+ });
280
+ }
281
+ }
282
+
283
+ // ── Stage 5: Probation Feedback ─────────────────────────────────────────────
284
+
285
+ function shouldAttributePrincipleToTool(principle: { contextTags: string[]; trigger: string; }, toolName: string): boolean {
286
+ return principle.contextTags.includes(toolName) || principle.trigger.includes(toolName);
287
+ }
288
+
289
+ /**
290
+ * Record probation feedback for injected probation IDs.
291
+ *
292
+ * On success: positive feedback for matching principles.
293
+ * On failure: negative feedback for matching principles.
294
+ */
295
+ export function handleProbationFeedback(
296
+ sessionId: string,
297
+ toolName: string,
298
+ workspaceDir: string,
299
+ wctx: WorkspaceContext,
300
+ isSuccess: boolean,
301
+ ): void {
302
+ const injectedProbationIds = getInjectedProbationIds(sessionId, workspaceDir);
303
+ for (const id of injectedProbationIds) {
304
+ const principle = wctx.evolutionReducer.getPrincipleById(id);
305
+ const shouldAttribute = !!principle && shouldAttributePrincipleToTool(principle, toolName);
306
+ if (shouldAttribute) {
307
+ wctx.evolutionReducer.recordProbationFeedback(id, isSuccess);
308
+ }
309
+ }
310
+ clearInjectedProbationIds(sessionId, workspaceDir);
311
+ }
312
+
313
+ // ── Stage 6: Pain Admission ─────────────────────────────────────────────────
314
+
315
+ const WRITE_TOOLS = ['write', 'edit', 'apply_patch', 'write_file', 'edit_file', 'replace'];
316
+
317
+ /**
318
+ * Evaluate whether a tool failure should trigger pain diagnosis.
319
+ *
320
+ * Combines:
321
+ * 1. Write-tool check — only write tools on failures enter this path
322
+ * 2. PEAT-B1 triage — if feature flag is on, check evidence triage
323
+ * 3. PainDiagnosticGate — cooldown + threshold check
324
+ *
325
+ * Returns a structured decision with reason and stage.
326
+ */
327
+ export function evaluatePainAdmissionForToolCall(
328
+ event: PluginHookAfterToolCallEvent,
329
+ observation: ToolCallObservation,
330
+ outcome: ToolCallOutcome,
331
+ latestFailureState: SessionState | undefined,
332
+ sessionState: SessionState | undefined,
333
+ sessionId: string,
334
+ workspaceDir: string,
335
+ config: { get: (key: string) => unknown },
336
+ ): PainAdmissionDecision {
337
+ // Only write-tool failures enter the pain path
338
+ if (!WRITE_TOOLS.includes(event.toolName) || !outcome.isFailure) {
339
+ return {
340
+ admitted: false,
341
+ stage: 'not_applicable',
342
+ reason: 'not_a_write_tool_failure',
343
+ detail: `tool=${event.toolName}, isFailure=${outcome.isFailure}`,
344
+ };
345
+ }
346
+
347
+ const failureSource = outcome.failureSource ?? 'tool_failure';
348
+
349
+ // PEAT-B1: Evidence triage (feature-flagged)
350
+ const painTriageFlag = loadFeatureFlagFromConfig(workspaceDir, 'painEvidenceAdmission');
351
+ if (painTriageFlag.enabled) {
352
+ const sourceKind = resolveSourceKindFromToolFailure(event.toolName, failureSource);
353
+ const triage = evaluateEvidenceTriage(sourceKind, observation.painScore);
354
+ if (triage.decision !== 'admit') {
355
+ SystemLogger.log(workspaceDir, 'TRIAGE_EVIDENCE_ONLY', JSON.stringify({
356
+ sourceKind: triage.sourceKind,
357
+ decision: triage.decision,
358
+ reason: triage.reason,
359
+ nextAction: triage.nextAction,
360
+ tool: event.toolName,
361
+ path: observation.relPath,
362
+ }));
363
+ return {
364
+ admitted: false,
365
+ stage: 'triage_evidence_only',
366
+ reason: triage.reason,
367
+ detail: `sourceKind=${triage.sourceKind}, decision=${triage.decision}, nextAction=${triage.nextAction}`,
368
+ };
369
+ }
370
+ }
371
+
372
+ // PainDiagnosticGate evaluation
373
+ const diagnosticGate = evaluatePainDiagnosticGate({
374
+ source: failureSource,
375
+ score: observation.painScore,
376
+ currentGfi: (latestFailureState ?? sessionState)?.currentGfi ?? 0,
377
+ consecutiveErrors: (latestFailureState ?? sessionState)?.consecutiveErrors ?? 0,
378
+ isRisky: observation.isRisk,
379
+ errorHash: latestFailureState?.lastErrorHash,
380
+ sessionId,
381
+ thresholds: {
382
+ painTrigger: (config.get('thresholds.pain_trigger') as number) || 40,
383
+ highSeverity: (config.get('severity_thresholds.high') as number) || 70,
384
+ repeatedFailure: (config.get('thresholds.stuck_loops_trigger') as number) || 4,
385
+ },
386
+ });
387
+
388
+ if (!diagnosticGate.shouldDiagnose) {
389
+ SystemLogger.log(workspaceDir, 'PAIN_DIAGNOSE_SKIPPED', `Tool failure recorded as friction only: ${diagnosticGate.detail}; tool=${event.toolName}; path=${observation.relPath}`);
390
+ let rejectPayload: string;
391
+ try {
392
+ rejectPayload = JSON.stringify({
393
+ reason: diagnosticGate.reason,
394
+ detail: diagnosticGate.detail,
395
+ source: failureSource,
396
+ sessionId,
397
+ gfi: (latestFailureState ?? sessionState)?.currentGfi ?? 0,
398
+ score: observation.painScore,
399
+ });
400
+ } catch (e) {
401
+ SystemLogger.log(workspaceDir, 'PAYLOAD_SERIALIZE_FAILED', String(e));
402
+ rejectPayload = JSON.stringify({ reason: diagnosticGate.reason, detail: '(log serialization failed)' });
403
+ }
404
+ SystemLogger.log(workspaceDir, 'PAIN_GATE_REJECTED', rejectPayload);
405
+
406
+ return {
407
+ admitted: false,
408
+ stage: 'gate_rejected',
409
+ reason: diagnosticGate.reason,
410
+ detail: diagnosticGate.detail,
411
+ gateResult: { shouldDiagnose: false, reason: diagnosticGate.reason, detail: diagnosticGate.detail },
412
+ };
413
+ }
414
+
415
+ return {
416
+ admitted: true,
417
+ stage: 'gate_admitted',
418
+ reason: diagnosticGate.reason,
419
+ detail: diagnosticGate.detail,
420
+ gateResult: { shouldDiagnose: true, reason: diagnosticGate.reason, detail: diagnosticGate.detail },
421
+ };
422
+ }
423
+
424
+ // ── Stage 7: Emit Pain ─────────────────────────────────────────────────────
425
+
426
+ /**
427
+ * Emit pain signal after admission.
428
+ *
429
+ * Records to trajectory, event log, evolution logger, principle value tracker,
430
+ * and emits the pain_detected event.
431
+ *
432
+ * Only called when the admission decision is 'admitted'.
433
+ */
434
+ export function emitPainIfAdmitted(
435
+ wctx: WorkspaceContext,
436
+ event: PluginHookAfterToolCallEvent,
437
+ observation: ToolCallObservation,
438
+ outcome: ToolCallOutcome,
439
+ admission: PainAdmissionDecision,
440
+ sessionId: string,
441
+ agentId: string | undefined,
442
+ workspaceDir: string,
443
+ emitPainDetectedEvent: (wctx: WorkspaceContext, event: import('../core/evolution-types.js').EvolutionLoopEvent) => Promise<void>,
444
+ ): void {
445
+ if (!admission.admitted) return;
446
+
447
+ const failureSource = outcome.failureSource ?? 'tool_failure';
448
+
449
+ // Record to trajectory before Runtime V2 diagnosis
450
+ wctx.trajectory?.recordPainEvent({
451
+ sessionId,
452
+ source: failureSource,
453
+ score: observation.painScore,
454
+ reason: `Tool ${event.toolName} failed on ${observation.relPath}`,
455
+ severity: observation.painScore >= 70 ? 'severe' : observation.painScore >= 40 ? 'moderate' : 'mild',
456
+ origin: 'system_infer',
457
+ text: sanitizeForEvidence(observation.params.text ?? observation.params.content, workspaceDir) || undefined,
458
+ });
459
+
460
+ // Observe: track which principles would have prevented this pain (observation-only)
461
+ try {
462
+ trackPrincipleValue(
463
+ workspaceDir,
464
+ {
465
+ reason: `Tool ${event.toolName} failed on ${observation.relPath}. Error: ${event.error ?? 'Non-zero exit code'}`,
466
+ source: failureSource,
467
+ score: String(observation.painScore),
468
+ },
469
+ () => wctx.evolutionReducer.getActivePrinciples().map((p) => ({
470
+ id: p.id,
471
+ trigger: p.trigger,
472
+ valueMetrics: p.valueMetrics,
473
+ })),
474
+ (id, metrics) => {
475
+ const principle = wctx.evolutionReducer.getPrincipleById(id);
476
+ if (principle) {
477
+ principle.valueMetrics = metrics;
478
+ try {
479
+ wctx.principleTreeLedger.updatePrincipleValueMetrics(id, {
480
+ principleId: id,
481
+ painPreventedCount: metrics.painPreventedCount,
482
+ lastPainPreventedAt: metrics.lastPainPreventedAt,
483
+ calculatedAt: metrics.calculatedAt,
484
+ avgPainSeverityPrevented: 0,
485
+ totalOpportunities: 0,
486
+ adheredCount: 0,
487
+ violatedCount: 0,
488
+ implementationCost: 0,
489
+ benefitScore: 0,
490
+ });
491
+ } catch (e) {
492
+ SystemLogger.log(workspaceDir, 'METRICS_UPDATE_SKIP', String(e));
493
+ }
494
+ }
495
+ },
496
+ );
497
+ } catch (e) {
498
+ SystemLogger.log(workspaceDir, 'PRINCIPLE_TRACK_SKIP', String(e));
499
+ }
500
+
501
+ wctx.eventLog.recordPainSignal(sessionId, {
502
+ score: observation.painScore,
503
+ source: failureSource,
504
+ reason: `Tool ${event.toolName} failed on ${observation.relPath}`,
505
+ isRisky: observation.isRisk,
506
+ });
507
+
508
+ const evoLogger = getEvolutionLogger(workspaceDir, wctx.trajectory);
509
+ evoLogger.logPainDetected({
510
+ traceId: observation.traceId,
511
+ source: failureSource,
512
+ reason: `Tool ${event.toolName} failed on ${observation.relPath}`,
513
+ score: observation.painScore,
514
+ toolName: event.toolName,
515
+ filePath: observation.relPath,
516
+ sessionId,
517
+ });
518
+
519
+ // Create painId inline (matches original createPainId)
520
+ const painId = `pain_${Date.now()}_${observation.errorHash.slice(0, 8)}`;
521
+
522
+ emitPainDetectedEvent(wctx, {
523
+ ts: new Date().toISOString(),
524
+ type: 'pain_detected',
525
+ data: {
526
+ painId,
527
+ painType: failureSource,
528
+ source: event.toolName,
529
+ reason: `Tool ${event.toolName} failed on ${observation.relPath}; diagnosticGate=${admission.reason}`,
530
+ score: observation.painScore,
531
+ sessionId,
532
+ traceId: observation.traceId,
533
+ agentId,
534
+ provenance: 'automatic_hook',
535
+ evidence: buildTrajectoryEvidence(wctx, sessionId),
536
+ },
537
+ });
538
+ }
539
+
540
+ // ── Shared Helpers ──────────────────────────────────────────────────────────
541
+
542
+ export { buildTrajectoryEvidence } from './trajectory-evidence.js';
543
+
544
+ // ── Source Classification ────────────────────────────────────────────────────
545
+
546
+ /**
547
+ * Classify tool failure source.
548
+ *
549
+ * Pure function — no I/O, no side effects.
550
+ * Determines whether a tool failure is a dispatch error (tool not found)
551
+ * or a regular tool execution failure.
552
+ */
553
+ export function classifyToolFailureSource(toolName: string | undefined, error: unknown): 'dispatch_error' | 'tool_failure' {
554
+ if (!toolName || toolName.trim() === '') return 'dispatch_error';
555
+ const msg = String(error ?? '');
556
+ if (/\btool\s+(?:\S+\s+)?not\s+found\b/i.test(msg)) return 'dispatch_error';
557
+ if (/\bunknown\s+tool\b/i.test(msg)) return 'dispatch_error';
558
+ return 'tool_failure';
559
+ }
560
+
561
+ /**
562
+ * Extract error type classification from error value.
563
+ */
564
+ function extractErrorType(error: unknown): string {
565
+ if (!error) return 'Unknown';
566
+ const msg = String(error);
567
+ if (msg.includes('EACCES') || msg.includes('permission denied')) return 'EACCES';
568
+ if (msg.includes('ENOENT') || msg.includes('no such file')) return 'ENOENT';
569
+ if (msg.includes('EISDIR')) return 'EISDIR';
570
+ if (msg.includes('ENOSPC')) return 'ENOSPC';
571
+ if (msg.includes('SyntaxError')) return 'SyntaxError';
572
+ if (msg.includes('TypeError')) return 'TypeError';
573
+ if (msg.includes('ReferenceError')) return 'ReferenceError';
574
+ if (msg.includes('timeout') || msg.includes('ETIMEDOUT')) return 'Timeout';
575
+ if (msg.includes('network') || msg.includes('ECONNREFUSED')) return 'Network';
576
+ return 'Other';
577
+ }