@cleocode/core 2026.3.57 → 2026.3.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/agent-registry.d.ts +206 -0
- package/dist/agents/agent-registry.d.ts.map +1 -0
- package/dist/agents/agent-schema.d.ts.map +1 -1
- package/dist/agents/execution-learning.d.ts +223 -0
- package/dist/agents/execution-learning.d.ts.map +1 -0
- package/dist/agents/health-monitor.d.ts +161 -0
- package/dist/agents/health-monitor.d.ts.map +1 -0
- package/dist/agents/index.d.ts +4 -1
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/retry.d.ts +57 -4
- package/dist/agents/retry.d.ts.map +1 -1
- package/dist/backfill/index.d.ts +83 -0
- package/dist/backfill/index.d.ts.map +1 -0
- package/dist/bootstrap.d.ts +1 -1
- package/dist/config.d.ts +47 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6985 -5068
- package/dist/index.js.map +4 -4
- package/dist/intelligence/adaptive-validation.d.ts +151 -0
- package/dist/intelligence/adaptive-validation.d.ts.map +1 -0
- package/dist/intelligence/impact.d.ts +34 -1
- package/dist/intelligence/impact.d.ts.map +1 -1
- package/dist/intelligence/index.d.ts +7 -2
- package/dist/intelligence/index.d.ts.map +1 -1
- package/dist/intelligence/types.d.ts +60 -0
- package/dist/intelligence/types.d.ts.map +1 -1
- package/dist/internal.d.ts +8 -4
- package/dist/internal.d.ts.map +1 -1
- package/dist/lib/index.d.ts +10 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/retry.d.ts +128 -0
- package/dist/lib/retry.d.ts.map +1 -0
- package/dist/nexus/sharing/index.d.ts +48 -2
- package/dist/nexus/sharing/index.d.ts.map +1 -1
- package/dist/sessions/session-enforcement.d.ts.map +1 -1
- package/dist/stats/index.d.ts +1 -0
- package/dist/stats/index.d.ts.map +1 -1
- package/dist/stats/workflow-telemetry.d.ts +89 -0
- package/dist/stats/workflow-telemetry.d.ts.map +1 -0
- package/dist/store/brain-schema.d.ts.map +1 -1
- package/dist/store/converters.d.ts.map +1 -1
- package/dist/store/cross-db-cleanup.d.ts +93 -0
- package/dist/store/cross-db-cleanup.d.ts.map +1 -0
- package/dist/store/db-helpers.d.ts.map +1 -1
- package/dist/store/migration-sqlite.d.ts.map +1 -1
- package/dist/store/sqlite-data-accessor.d.ts.map +1 -1
- package/dist/store/sqlite.d.ts.map +1 -1
- package/dist/store/task-store.d.ts.map +1 -1
- package/dist/store/tasks-schema.d.ts +18 -3
- package/dist/store/tasks-schema.d.ts.map +1 -1
- package/dist/store/validation-schemas.d.ts +32 -0
- package/dist/store/validation-schemas.d.ts.map +1 -1
- package/dist/tasks/add.d.ts +10 -1
- package/dist/tasks/add.d.ts.map +1 -1
- package/dist/tasks/complete.d.ts.map +1 -1
- package/dist/tasks/enforcement.d.ts +22 -0
- package/dist/tasks/enforcement.d.ts.map +1 -0
- package/dist/tasks/epic-enforcement.d.ts +199 -0
- package/dist/tasks/epic-enforcement.d.ts.map +1 -0
- package/dist/tasks/index.d.ts +1 -1
- package/dist/tasks/index.d.ts.map +1 -1
- package/dist/tasks/pipeline-stage.d.ts +181 -0
- package/dist/tasks/pipeline-stage.d.ts.map +1 -0
- package/dist/tasks/update.d.ts +2 -0
- package/dist/tasks/update.d.ts.map +1 -1
- package/migrations/drizzle-brain/20260321000001_t033-brain-indexes/migration.sql +12 -0
- package/migrations/drizzle-brain/20260321000001_t033-brain-indexes/snapshot.json +1232 -0
- package/migrations/drizzle-tasks/20260321000000_t033-connection-health/migration.sql +518 -0
- package/migrations/drizzle-tasks/20260321000000_t033-connection-health/snapshot.json +4312 -0
- package/migrations/drizzle-tasks/20260321000002_t060-pipeline-stage-binding/migration.sql +82 -0
- package/migrations/drizzle-tasks/20260321000002_t060-pipeline-stage-binding/snapshot.json +9 -0
- package/package.json +5 -5
- package/schemas/config.schema.json +37 -1547
- package/src/__tests__/sharing.test.ts +24 -0
- package/src/agents/__tests__/agent-registry.test.ts +351 -0
- package/src/agents/__tests__/execution-learning.test.ts +684 -0
- package/src/agents/__tests__/health-monitor.test.ts +332 -0
- package/src/agents/__tests__/registry.test.ts +30 -2
- package/src/agents/agent-registry.ts +394 -0
- package/src/agents/agent-schema.ts +5 -0
- package/src/agents/execution-learning.ts +675 -0
- package/src/agents/health-monitor.ts +279 -0
- package/src/agents/index.ts +37 -1
- package/src/agents/retry.ts +57 -4
- package/src/backfill/index.ts +309 -0
- package/src/bootstrap.ts +1 -1
- package/src/config.ts +126 -0
- package/src/index.ts +8 -1
- package/src/intelligence/__tests__/adaptive-validation.test.ts +694 -0
- package/src/intelligence/__tests__/impact.test.ts +165 -1
- package/src/intelligence/adaptive-validation.ts +764 -0
- package/src/intelligence/impact.ts +203 -0
- package/src/intelligence/index.ts +19 -0
- package/src/intelligence/types.ts +76 -0
- package/src/internal.ts +39 -0
- package/src/lib/__tests__/retry.test.ts +321 -0
- package/src/lib/index.ts +16 -0
- package/src/lib/retry.ts +224 -0
- package/src/lifecycle/__tests__/chain-store.test.ts +7 -0
- package/src/lifecycle/__tests__/tessera-engine.test.ts +52 -0
- package/src/nexus/sharing/index.ts +142 -2
- package/src/sessions/__tests__/session-edge-cases.test.ts +24 -1
- package/src/sessions/session-enforcement.ts +13 -2
- package/src/stats/index.ts +7 -0
- package/src/stats/workflow-telemetry.ts +502 -0
- package/src/store/__tests__/migration-safety.test.ts +3 -0
- package/src/store/__tests__/session-store.test.ts +132 -1
- package/src/store/__tests__/task-store.test.ts +22 -1
- package/src/store/__tests__/test-db-helper.ts +29 -2
- package/src/store/brain-schema.ts +4 -1
- package/src/store/converters.ts +2 -0
- package/src/store/cross-db-cleanup.ts +192 -0
- package/src/store/db-helpers.ts +2 -0
- package/src/store/migration-sqlite.ts +6 -0
- package/src/store/sqlite-data-accessor.ts +20 -28
- package/src/store/sqlite.ts +14 -2
- package/src/store/task-store.ts +6 -0
- package/src/store/tasks-schema.ts +59 -20
- package/src/tasks/__tests__/add.test.ts +16 -0
- package/src/tasks/__tests__/complete-unblocks.test.ts +10 -1
- package/src/tasks/__tests__/complete.test.ts +11 -2
- package/src/tasks/__tests__/epic-enforcement.test.ts +909 -0
- package/src/tasks/__tests__/minimal-test.test.ts +28 -0
- package/src/tasks/__tests__/pipeline-stage.test.ts +403 -0
- package/src/tasks/__tests__/update.test.ts +40 -6
- package/src/tasks/add.ts +128 -2
- package/src/tasks/complete.ts +29 -17
- package/src/tasks/enforcement.ts +127 -0
- package/src/tasks/epic-enforcement.ts +364 -0
- package/src/tasks/index.ts +1 -0
- package/src/tasks/pipeline-stage.ts +293 -0
- package/src/tasks/update.ts +62 -0
- package/templates/config.template.json +34 -111
- package/templates/global-config.template.json +24 -40
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adaptive Validation module for the CLEO Intelligence dimension.
|
|
3
|
+
*
|
|
4
|
+
* Extends the prediction layer with:
|
|
5
|
+
* - Gate-level failure tracking: which verification gates fail most for which task types
|
|
6
|
+
* - Adaptive focus suggestions: recommend which gates to pay attention to given task attributes
|
|
7
|
+
* - Confidence scoring: compute and persist a 0-1 confidence score after task verification
|
|
8
|
+
* - Prediction storage: persist quality predictions back to brain as observations so
|
|
9
|
+
* subsequent sessions can learn from them
|
|
10
|
+
*
|
|
11
|
+
* All storage goes to existing brain tables (brain_observations, brain_learnings,
|
|
12
|
+
* brain_patterns). No new tables required.
|
|
13
|
+
*
|
|
14
|
+
* @task T035
|
|
15
|
+
* @epic T029
|
|
16
|
+
* @module intelligence
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { randomBytes } from 'node:crypto';
|
|
20
|
+
import type { Task, TaskVerification, VerificationGate } from '@cleocode/contracts';
|
|
21
|
+
import type { BrainDataAccessor } from '../store/brain-accessor.js';
|
|
22
|
+
import type { DataAccessor } from '../store/data-accessor.js';
|
|
23
|
+
import { predictValidationOutcome } from './prediction.js';
|
|
24
|
+
import type { ValidationPrediction } from './types.js';
|
|
25
|
+
|
|
26
|
+
// ============================================================================
|
|
27
|
+
// Constants
|
|
28
|
+
// ============================================================================
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Ordered list of all supported verification gates.
|
|
32
|
+
* Order reflects typical RCASD pipeline sequence.
|
|
33
|
+
*/
|
|
34
|
+
const ALL_GATES: VerificationGate[] = [
|
|
35
|
+
'implemented',
|
|
36
|
+
'testsPassed',
|
|
37
|
+
'qaPassed',
|
|
38
|
+
'cleanupDone',
|
|
39
|
+
'securityPassed',
|
|
40
|
+
'documented',
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/** Minimum confidence threshold to consider a gate "high-risk" for focus. */
|
|
44
|
+
const GATE_RISK_THRESHOLD = 0.3;
|
|
45
|
+
|
|
46
|
+
// ============================================================================
|
|
47
|
+
// Types
|
|
48
|
+
// ============================================================================
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A gate-level focus recommendation produced by adaptive validation.
|
|
52
|
+
*/
|
|
53
|
+
export interface GateFocusRecommendation {
|
|
54
|
+
/** The verification gate this recommendation applies to. */
|
|
55
|
+
gate: VerificationGate;
|
|
56
|
+
/**
|
|
57
|
+
* Priority level: high gates should be checked first; low gates are lower
|
|
58
|
+
* risk for this task type and can be reviewed last.
|
|
59
|
+
*/
|
|
60
|
+
priority: 'high' | 'medium' | 'low';
|
|
61
|
+
/** Human-readable rationale for this priority level. */
|
|
62
|
+
rationale: string;
|
|
63
|
+
/**
|
|
64
|
+
* Estimated pass likelihood for this gate (0-1) based on historical
|
|
65
|
+
* patterns and task metadata. Null when no historical data is available.
|
|
66
|
+
*/
|
|
67
|
+
estimatedPassLikelihood: number | null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Full adaptive validation suggestion set for a task.
|
|
72
|
+
*/
|
|
73
|
+
export interface AdaptiveValidationSuggestion {
|
|
74
|
+
/** Task ID this suggestion applies to. */
|
|
75
|
+
taskId: string;
|
|
76
|
+
/** Ordered gate recommendations (highest priority first). */
|
|
77
|
+
gateFocus: GateFocusRecommendation[];
|
|
78
|
+
/** Overall confidence that the task will pass all required gates. */
|
|
79
|
+
overallConfidence: number;
|
|
80
|
+
/**
|
|
81
|
+
* Actionable tips derived from gate focus analysis.
|
|
82
|
+
* Includes failure-pattern mitigations where available.
|
|
83
|
+
*/
|
|
84
|
+
tips: string[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Result of scoring and persisting a completed verification round.
|
|
89
|
+
*/
|
|
90
|
+
export interface VerificationConfidenceScore {
|
|
91
|
+
/** Task ID. */
|
|
92
|
+
taskId: string;
|
|
93
|
+
/**
|
|
94
|
+
* Computed confidence score (0-1).
|
|
95
|
+
*
|
|
96
|
+
* Score derivation:
|
|
97
|
+
* - Gates passed vs required gates: up to 0.6
|
|
98
|
+
* - Failure log length (fewer failures = higher confidence): up to 0.2
|
|
99
|
+
* - Round number (fewer rounds = higher confidence): up to 0.2
|
|
100
|
+
*/
|
|
101
|
+
confidenceScore: number;
|
|
102
|
+
/** Whether the overall verification passed. */
|
|
103
|
+
passed: boolean;
|
|
104
|
+
/** IDs of gates that passed. */
|
|
105
|
+
gatesPassed: VerificationGate[];
|
|
106
|
+
/** IDs of gates that failed or are missing. */
|
|
107
|
+
gatesFailed: VerificationGate[];
|
|
108
|
+
/** Brain observation ID if the score was persisted (may be undefined on dry run). */
|
|
109
|
+
observationId?: string;
|
|
110
|
+
/** Brain learning ID if a learning was extracted (may be undefined). */
|
|
111
|
+
learningId?: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Parameters for storing a quality prediction as a brain observation.
|
|
116
|
+
*/
|
|
117
|
+
export interface StorePredictionOptions {
|
|
118
|
+
/** Whether to skip persisting to brain (useful in tests). Default: false. */
|
|
119
|
+
dryRun?: boolean;
|
|
120
|
+
/** Session ID to attach to the observation (optional). */
|
|
121
|
+
sessionId?: string;
|
|
122
|
+
/** Project identifier to attach to the observation (optional). */
|
|
123
|
+
project?: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ============================================================================
|
|
127
|
+
// Adaptive Validation Gate Focus
|
|
128
|
+
// ============================================================================
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Suggest which verification gates to focus on for a task, ordered by risk.
|
|
132
|
+
*
|
|
133
|
+
* Uses:
|
|
134
|
+
* - Historical failure patterns from brain_patterns filtered by task type/labels
|
|
135
|
+
* - Task characteristics (size, type, labels, priority) to weight gate risk
|
|
136
|
+
* - Existing gate state from task.verification to skip already-passed gates
|
|
137
|
+
*
|
|
138
|
+
* @param taskId - The task to analyze
|
|
139
|
+
* @param taskAccessor - DataAccessor for tasks.db
|
|
140
|
+
* @param brainAccessor - BrainDataAccessor for brain.db
|
|
141
|
+
* @returns Ordered gate focus recommendations and overall confidence
|
|
142
|
+
*/
|
|
143
|
+
export async function suggestGateFocus(
|
|
144
|
+
taskId: string,
|
|
145
|
+
taskAccessor: DataAccessor,
|
|
146
|
+
brainAccessor: BrainDataAccessor,
|
|
147
|
+
): Promise<AdaptiveValidationSuggestion> {
|
|
148
|
+
const task = await taskAccessor.loadSingleTask(taskId);
|
|
149
|
+
if (!task) {
|
|
150
|
+
return {
|
|
151
|
+
taskId,
|
|
152
|
+
gateFocus: [],
|
|
153
|
+
overallConfidence: 0,
|
|
154
|
+
tips: [`Task ${taskId} not found — cannot generate gate focus suggestions.`],
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const gateFocus = await computeGateFocusRecommendations(task, brainAccessor);
|
|
159
|
+
const overallConfidence = computeOverallConfidenceFromGates(gateFocus);
|
|
160
|
+
const tips = buildAdaptiveTips(task, gateFocus);
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
taskId,
|
|
164
|
+
gateFocus,
|
|
165
|
+
overallConfidence: Math.round(overallConfidence * 1000) / 1000,
|
|
166
|
+
tips,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ============================================================================
|
|
171
|
+
// Confidence Scoring for Verification Gates
|
|
172
|
+
// ============================================================================
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Compute a confidence score for a completed verification round and persist
|
|
176
|
+
* it to brain.db as an observation and learning.
|
|
177
|
+
*
|
|
178
|
+
* Called after `cleo verify` gates are set. Stores:
|
|
179
|
+
* - A `brain_observations` row (type: 'discovery') with score, gates, task metadata
|
|
180
|
+
* - A `brain_learnings` row if the result is notable (high pass or high failure)
|
|
181
|
+
*
|
|
182
|
+
* @param taskId - The task that was verified
|
|
183
|
+
* @param verification - The task's current verification state
|
|
184
|
+
* @param taskAccessor - DataAccessor for tasks.db
|
|
185
|
+
* @param brainAccessor - BrainDataAccessor for brain.db
|
|
186
|
+
* @param options - Persistence and session options
|
|
187
|
+
* @returns Computed confidence score with optional persisted observation/learning IDs
|
|
188
|
+
*/
|
|
189
|
+
export async function scoreVerificationConfidence(
|
|
190
|
+
taskId: string,
|
|
191
|
+
verification: TaskVerification,
|
|
192
|
+
taskAccessor: DataAccessor,
|
|
193
|
+
brainAccessor: BrainDataAccessor,
|
|
194
|
+
options: StorePredictionOptions = {},
|
|
195
|
+
): Promise<VerificationConfidenceScore> {
|
|
196
|
+
const task = await taskAccessor.loadSingleTask(taskId);
|
|
197
|
+
|
|
198
|
+
const gatesPassed: VerificationGate[] = [];
|
|
199
|
+
const gatesFailed: VerificationGate[] = [];
|
|
200
|
+
|
|
201
|
+
for (const gate of ALL_GATES) {
|
|
202
|
+
const gateValue = verification.gates[gate];
|
|
203
|
+
if (gateValue === true) {
|
|
204
|
+
gatesPassed.push(gate);
|
|
205
|
+
} else if (gateValue === false || gateValue === null) {
|
|
206
|
+
gatesFailed.push(gate);
|
|
207
|
+
}
|
|
208
|
+
// undefined gates are ignored (not required)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const confidenceScore = computeVerificationConfidence(verification, gatesPassed, gatesFailed);
|
|
212
|
+
|
|
213
|
+
if (options.dryRun) {
|
|
214
|
+
return {
|
|
215
|
+
taskId,
|
|
216
|
+
confidenceScore,
|
|
217
|
+
passed: verification.passed,
|
|
218
|
+
gatesPassed,
|
|
219
|
+
gatesFailed,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Persist observation to brain
|
|
224
|
+
const observationId = await persistVerificationObservation(
|
|
225
|
+
taskId,
|
|
226
|
+
task,
|
|
227
|
+
verification,
|
|
228
|
+
confidenceScore,
|
|
229
|
+
gatesPassed,
|
|
230
|
+
gatesFailed,
|
|
231
|
+
brainAccessor,
|
|
232
|
+
options,
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
// Extract learning if notable
|
|
236
|
+
const learningId = await maybeExtractLearning(
|
|
237
|
+
taskId,
|
|
238
|
+
task,
|
|
239
|
+
verification,
|
|
240
|
+
confidenceScore,
|
|
241
|
+
gatesPassed,
|
|
242
|
+
gatesFailed,
|
|
243
|
+
brainAccessor,
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
taskId,
|
|
248
|
+
confidenceScore,
|
|
249
|
+
passed: verification.passed,
|
|
250
|
+
gatesPassed,
|
|
251
|
+
gatesFailed,
|
|
252
|
+
observationId,
|
|
253
|
+
learningId,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ============================================================================
|
|
258
|
+
// Quality Prediction Storage
|
|
259
|
+
// ============================================================================
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Store a validation prediction as a brain observation for future learning.
|
|
263
|
+
*
|
|
264
|
+
* Saves the full `ValidationPrediction` to brain_observations so that
|
|
265
|
+
* accumulated predictions can later be analyzed to improve gate pass rates.
|
|
266
|
+
*
|
|
267
|
+
* @param prediction - The prediction to persist
|
|
268
|
+
* @param brainAccessor - BrainDataAccessor for brain.db
|
|
269
|
+
* @param options - Dry-run and session options
|
|
270
|
+
* @returns The created brain observation ID (or undefined on dry run)
|
|
271
|
+
*/
|
|
272
|
+
export async function storePrediction(
|
|
273
|
+
prediction: ValidationPrediction,
|
|
274
|
+
brainAccessor: BrainDataAccessor,
|
|
275
|
+
options: StorePredictionOptions = {},
|
|
276
|
+
): Promise<string | undefined> {
|
|
277
|
+
if (options.dryRun) return undefined;
|
|
278
|
+
|
|
279
|
+
const id = `O-pred-${randomBytes(4).toString('hex')}`;
|
|
280
|
+
const now = new Date().toISOString().replace('T', ' ').slice(0, 19);
|
|
281
|
+
|
|
282
|
+
const facts: string[] = [
|
|
283
|
+
`taskId: ${prediction.taskId}`,
|
|
284
|
+
`stage: ${prediction.stage}`,
|
|
285
|
+
`passLikelihood: ${prediction.passLikelihood}`,
|
|
286
|
+
`blockers: ${prediction.blockers.length}`,
|
|
287
|
+
`suggestions: ${prediction.suggestions.length}`,
|
|
288
|
+
];
|
|
289
|
+
|
|
290
|
+
await brainAccessor.addObservation({
|
|
291
|
+
id,
|
|
292
|
+
type: 'discovery',
|
|
293
|
+
title: `Quality prediction: ${prediction.taskId} at ${prediction.stage} stage`,
|
|
294
|
+
subtitle: `Pass likelihood: ${Math.round(prediction.passLikelihood * 100)}%`,
|
|
295
|
+
narrative:
|
|
296
|
+
prediction.blockers.length > 0
|
|
297
|
+
? `Blockers: ${prediction.blockers.join('; ')}`
|
|
298
|
+
: 'No blockers identified.',
|
|
299
|
+
factsJson: JSON.stringify(facts),
|
|
300
|
+
conceptsJson: JSON.stringify(['quality-prediction', 'adaptive-validation', prediction.stage]),
|
|
301
|
+
project: options.project ?? null,
|
|
302
|
+
filesReadJson: null,
|
|
303
|
+
filesModifiedJson: null,
|
|
304
|
+
sourceSessionId: options.sessionId ?? null,
|
|
305
|
+
sourceType: 'agent',
|
|
306
|
+
contentHash: null,
|
|
307
|
+
discoveryTokens: null,
|
|
308
|
+
createdAt: now,
|
|
309
|
+
updatedAt: null,
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
return id;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Compute a prediction for a task and immediately persist it to brain.
|
|
317
|
+
*
|
|
318
|
+
* Convenience wrapper combining `predictValidationOutcome` and `storePrediction`.
|
|
319
|
+
*
|
|
320
|
+
* @param taskId - The task to predict and record
|
|
321
|
+
* @param stage - The lifecycle stage being predicted
|
|
322
|
+
* @param taskAccessor - DataAccessor for tasks.db
|
|
323
|
+
* @param brainAccessor - BrainDataAccessor for brain.db
|
|
324
|
+
* @param options - Dry-run and session options
|
|
325
|
+
* @returns The prediction with optional stored observation ID
|
|
326
|
+
*/
|
|
327
|
+
export async function predictAndStore(
|
|
328
|
+
taskId: string,
|
|
329
|
+
stage: string,
|
|
330
|
+
taskAccessor: DataAccessor,
|
|
331
|
+
brainAccessor: BrainDataAccessor,
|
|
332
|
+
options: StorePredictionOptions = {},
|
|
333
|
+
): Promise<ValidationPrediction & { observationId?: string }> {
|
|
334
|
+
const prediction = await predictValidationOutcome(taskId, stage, taskAccessor, brainAccessor);
|
|
335
|
+
const observationId = await storePrediction(prediction, brainAccessor, options);
|
|
336
|
+
return { ...prediction, observationId };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// ============================================================================
|
|
340
|
+
// Internal: Gate Focus Computation
|
|
341
|
+
// ============================================================================
|
|
342
|
+
|
|
343
|
+
async function computeGateFocusRecommendations(
|
|
344
|
+
task: Task,
|
|
345
|
+
brainAccessor: BrainDataAccessor,
|
|
346
|
+
): Promise<GateFocusRecommendation[]> {
|
|
347
|
+
const existingGates = task.verification?.gates ?? {};
|
|
348
|
+
const taskLabels = new Set((task.labels ?? []).map((l) => l.toLowerCase()));
|
|
349
|
+
const taskTitle = task.title.toLowerCase();
|
|
350
|
+
|
|
351
|
+
// Load failure patterns to detect which gates commonly fail for similar tasks
|
|
352
|
+
const failurePatterns = await brainAccessor.findPatterns({ type: 'failure', limit: 100 });
|
|
353
|
+
const blockerPatterns = await brainAccessor.findPatterns({ type: 'blocker', limit: 50 });
|
|
354
|
+
const allNegativePatterns = [...failurePatterns, ...blockerPatterns];
|
|
355
|
+
|
|
356
|
+
const recommendations: GateFocusRecommendation[] = [];
|
|
357
|
+
|
|
358
|
+
for (const gate of ALL_GATES) {
|
|
359
|
+
// Skip gates already passed
|
|
360
|
+
if (existingGates[gate] === true) continue;
|
|
361
|
+
|
|
362
|
+
const gateRisk = computeGateRisk(gate, task, taskLabels, taskTitle, allNegativePatterns);
|
|
363
|
+
|
|
364
|
+
const rationale = buildGateRationale(gate, gateRisk, task, existingGates[gate]);
|
|
365
|
+
const mitigation = findGateMitigation(gate, taskLabels, taskTitle, allNegativePatterns);
|
|
366
|
+
|
|
367
|
+
const priority: GateFocusRecommendation['priority'] =
|
|
368
|
+
gateRisk >= 0.6 ? 'high' : gateRisk >= GATE_RISK_THRESHOLD ? 'medium' : 'low';
|
|
369
|
+
|
|
370
|
+
recommendations.push({
|
|
371
|
+
gate,
|
|
372
|
+
priority,
|
|
373
|
+
rationale: mitigation ? `${rationale} Suggestion: ${mitigation}` : rationale,
|
|
374
|
+
estimatedPassLikelihood: gateRisk > 0 ? Math.round((1 - gateRisk) * 1000) / 1000 : null,
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Sort: high first, then medium, then low; within same priority sort by risk desc
|
|
379
|
+
const priorityOrder: Record<GateFocusRecommendation['priority'], number> = {
|
|
380
|
+
high: 0,
|
|
381
|
+
medium: 1,
|
|
382
|
+
low: 2,
|
|
383
|
+
};
|
|
384
|
+
recommendations.sort(
|
|
385
|
+
(a, b) =>
|
|
386
|
+
priorityOrder[a.priority] - priorityOrder[b.priority] ||
|
|
387
|
+
(a.estimatedPassLikelihood ?? 0.5) - (b.estimatedPassLikelihood ?? 0.5),
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
return recommendations;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function computeGateRisk(
|
|
394
|
+
gate: VerificationGate,
|
|
395
|
+
task: Task,
|
|
396
|
+
taskLabels: Set<string>,
|
|
397
|
+
taskTitle: string,
|
|
398
|
+
negativePatterns: Array<{ pattern: string; context: string; successRate: number | null }>,
|
|
399
|
+
): number {
|
|
400
|
+
let risk = 0;
|
|
401
|
+
|
|
402
|
+
// Base risk by gate type + task attributes
|
|
403
|
+
risk += getBaseGateRisk(gate, task);
|
|
404
|
+
|
|
405
|
+
// Historical pattern risk: patterns that mention this gate and this task
|
|
406
|
+
let matchCount = 0;
|
|
407
|
+
let totalFailureRate = 0;
|
|
408
|
+
|
|
409
|
+
for (const p of negativePatterns) {
|
|
410
|
+
const patternText = p.pattern.toLowerCase();
|
|
411
|
+
const contextText = p.context.toLowerCase();
|
|
412
|
+
const gateText = gate.toLowerCase();
|
|
413
|
+
|
|
414
|
+
const gateMatch = patternText.includes(gateText) || contextText.includes(gateText);
|
|
415
|
+
const labelMatch =
|
|
416
|
+
taskLabels.size > 0 &&
|
|
417
|
+
[...taskLabels].some((l) => patternText.includes(l) || contextText.includes(l));
|
|
418
|
+
const titleMatch = taskTitle
|
|
419
|
+
.split(/\s+/)
|
|
420
|
+
.filter((w) => w.length > 3)
|
|
421
|
+
.some((w) => patternText.includes(w) || contextText.includes(w));
|
|
422
|
+
|
|
423
|
+
if (gateMatch && (labelMatch || titleMatch)) {
|
|
424
|
+
matchCount++;
|
|
425
|
+
const failureRate = p.successRate !== null ? 1 - p.successRate : 0.5;
|
|
426
|
+
totalFailureRate += failureRate;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (matchCount > 0) {
|
|
431
|
+
const avgHistoricalFailureRate = totalFailureRate / matchCount;
|
|
432
|
+
// Blend 30% base risk, 70% historical
|
|
433
|
+
risk = risk * 0.3 + avgHistoricalFailureRate * 0.7;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return Math.min(Math.max(risk, 0), 1);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Intrinsic risk per gate based on task characteristics alone (no history).
|
|
441
|
+
*/
|
|
442
|
+
function getBaseGateRisk(gate: VerificationGate, task: Task): number {
|
|
443
|
+
const size = task.size ?? 'medium';
|
|
444
|
+
const sizeFactor = size === 'large' ? 0.3 : size === 'medium' ? 0.2 : 0.1;
|
|
445
|
+
const hasAcceptance = (task.acceptance?.length ?? 0) > 0;
|
|
446
|
+
const isHighPriority = task.priority === 'critical' || task.priority === 'high';
|
|
447
|
+
|
|
448
|
+
switch (gate) {
|
|
449
|
+
case 'implemented':
|
|
450
|
+
// Low intrinsic risk if task is active/done
|
|
451
|
+
return task.status === 'active' ? 0.2 : 0.15;
|
|
452
|
+
|
|
453
|
+
case 'testsPassed':
|
|
454
|
+
// Higher risk for large tasks or tasks without acceptance criteria
|
|
455
|
+
return sizeFactor + (hasAcceptance ? 0 : 0.1) + (isHighPriority ? 0.1 : 0);
|
|
456
|
+
|
|
457
|
+
case 'qaPassed':
|
|
458
|
+
// Higher risk for large/complex tasks
|
|
459
|
+
return sizeFactor + (isHighPriority ? 0.15 : 0.05);
|
|
460
|
+
|
|
461
|
+
case 'cleanupDone':
|
|
462
|
+
// Moderate risk — often overlooked
|
|
463
|
+
return 0.15 + sizeFactor;
|
|
464
|
+
|
|
465
|
+
case 'securityPassed': // Security-related labels increase risk significantly
|
|
466
|
+
{
|
|
467
|
+
const securityLabels = ['auth', 'security', 'crypto', 'permission', 'access'];
|
|
468
|
+
const matchingLabels = (task.labels ?? []).filter((l) =>
|
|
469
|
+
securityLabels.includes(l.toLowerCase()),
|
|
470
|
+
);
|
|
471
|
+
// Each security-related label increases risk; 2+ labels → high risk (>= 0.6)
|
|
472
|
+
if (matchingLabels.length >= 2) return 0.65;
|
|
473
|
+
if (matchingLabels.length === 1) return 0.45;
|
|
474
|
+
return 0.1;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
case 'documented':
|
|
478
|
+
// Large tasks and those with many acceptance criteria need more docs
|
|
479
|
+
return sizeFactor + ((task.acceptance?.length ?? 0) >= 3 ? 0.1 : 0);
|
|
480
|
+
|
|
481
|
+
default:
|
|
482
|
+
return 0.2;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function buildGateRationale(
|
|
487
|
+
gate: VerificationGate,
|
|
488
|
+
risk: number,
|
|
489
|
+
task: Task,
|
|
490
|
+
currentValue: boolean | null | undefined,
|
|
491
|
+
): string {
|
|
492
|
+
if (currentValue === false) {
|
|
493
|
+
return `Gate "${gate}" previously failed. High priority to address before retry.`;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const riskPercent = Math.round(risk * 100);
|
|
497
|
+
|
|
498
|
+
switch (gate) {
|
|
499
|
+
case 'implemented':
|
|
500
|
+
return `Verify core implementation is complete (estimated ${riskPercent}% failure risk).`;
|
|
501
|
+
case 'testsPassed':
|
|
502
|
+
return `Confirm all tests pass; ${
|
|
503
|
+
(task.size ?? 'medium') === 'large' ? 'large task increases test surface area. ' : ''
|
|
504
|
+
}Estimated ${riskPercent}% failure risk.`;
|
|
505
|
+
case 'qaPassed':
|
|
506
|
+
return `QA review required${
|
|
507
|
+
task.priority === 'critical' ? ' — critical priority task needs thorough QA' : ''
|
|
508
|
+
}. Estimated ${riskPercent}% failure risk.`;
|
|
509
|
+
case 'cleanupDone':
|
|
510
|
+
return `Ensure code cleanup is complete (dead code, lint, formatting). Estimated ${riskPercent}% failure risk.`;
|
|
511
|
+
case 'securityPassed':
|
|
512
|
+
return `Security review${
|
|
513
|
+
(task.labels ?? []).some((l) => ['auth', 'security'].includes(l.toLowerCase()))
|
|
514
|
+
? ' — security-related labels detected; review is high priority'
|
|
515
|
+
: ''
|
|
516
|
+
}. Estimated ${riskPercent}% failure risk.`;
|
|
517
|
+
case 'documented':
|
|
518
|
+
return `Ensure documentation is updated. Estimated ${riskPercent}% failure risk.`;
|
|
519
|
+
default:
|
|
520
|
+
return `Gate "${gate}" check required. Estimated ${riskPercent}% failure risk.`;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function findGateMitigation(
|
|
525
|
+
gate: VerificationGate,
|
|
526
|
+
taskLabels: Set<string>,
|
|
527
|
+
taskTitle: string,
|
|
528
|
+
negativePatterns: Array<{
|
|
529
|
+
pattern: string;
|
|
530
|
+
context: string;
|
|
531
|
+
mitigation: string | null;
|
|
532
|
+
}>,
|
|
533
|
+
): string | null {
|
|
534
|
+
for (const p of negativePatterns) {
|
|
535
|
+
if (!p.mitigation) continue;
|
|
536
|
+
|
|
537
|
+
const patternText = p.pattern.toLowerCase();
|
|
538
|
+
const contextText = p.context.toLowerCase();
|
|
539
|
+
const gateText = gate.toLowerCase();
|
|
540
|
+
|
|
541
|
+
const gateMatch = patternText.includes(gateText) || contextText.includes(gateText);
|
|
542
|
+
const labelMatch =
|
|
543
|
+
taskLabels.size > 0 &&
|
|
544
|
+
[...taskLabels].some((l) => patternText.includes(l) || contextText.includes(l));
|
|
545
|
+
const titleMatch = taskTitle
|
|
546
|
+
.split(/\s+/)
|
|
547
|
+
.filter((w) => w.length > 3)
|
|
548
|
+
.some((w) => patternText.includes(w));
|
|
549
|
+
|
|
550
|
+
if (gateMatch && (labelMatch || titleMatch)) {
|
|
551
|
+
return p.mitigation;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return null;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function computeOverallConfidenceFromGates(recommendations: GateFocusRecommendation[]): number {
|
|
558
|
+
if (recommendations.length === 0) return 1.0;
|
|
559
|
+
|
|
560
|
+
// Weighted average: high-priority gates drag confidence down more
|
|
561
|
+
const weights: Record<GateFocusRecommendation['priority'], number> = {
|
|
562
|
+
high: 3,
|
|
563
|
+
medium: 2,
|
|
564
|
+
low: 1,
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
let totalWeight = 0;
|
|
568
|
+
let weightedLikelihood = 0;
|
|
569
|
+
|
|
570
|
+
for (const rec of recommendations) {
|
|
571
|
+
const w = weights[rec.priority];
|
|
572
|
+
const likelihood = rec.estimatedPassLikelihood ?? 0.5;
|
|
573
|
+
totalWeight += w;
|
|
574
|
+
weightedLikelihood += likelihood * w;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
return totalWeight > 0 ? weightedLikelihood / totalWeight : 0.5;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function buildAdaptiveTips(task: Task, recommendations: GateFocusRecommendation[]): string[] {
|
|
581
|
+
const tips: string[] = [];
|
|
582
|
+
|
|
583
|
+
const highPriority = recommendations.filter((r) => r.priority === 'high');
|
|
584
|
+
if (highPriority.length > 0) {
|
|
585
|
+
tips.push(`Focus first on high-risk gates: ${highPriority.map((r) => r.gate).join(', ')}.`);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if ((task.acceptance?.length ?? 0) === 0) {
|
|
589
|
+
tips.push('Add acceptance criteria to improve gate prediction accuracy.');
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
if ((task.labels?.length ?? 0) === 0) {
|
|
593
|
+
tips.push('Adding labels will improve historical pattern matching for future predictions.');
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const alreadyFailed = recommendations.filter((r) => r.rationale.includes('previously failed'));
|
|
597
|
+
if (alreadyFailed.length > 0) {
|
|
598
|
+
tips.push(
|
|
599
|
+
`${alreadyFailed.length} gate(s) failed in a previous round — address these before re-running verification.`,
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
return tips;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// ============================================================================
|
|
607
|
+
// Internal: Confidence Score Computation
|
|
608
|
+
// ============================================================================
|
|
609
|
+
|
|
610
|
+
function computeVerificationConfidence(
|
|
611
|
+
verification: TaskVerification,
|
|
612
|
+
gatesPassed: VerificationGate[],
|
|
613
|
+
gatesFailed: VerificationGate[],
|
|
614
|
+
): number {
|
|
615
|
+
// Component 1: Gates passed ratio (up to 0.6)
|
|
616
|
+
const totalGatesTracked = gatesPassed.length + gatesFailed.length;
|
|
617
|
+
const gateRatio = totalGatesTracked > 0 ? gatesPassed.length / totalGatesTracked : 0.5;
|
|
618
|
+
const gateScore = gateRatio * 0.6;
|
|
619
|
+
|
|
620
|
+
// Component 2: Failure log penalty (up to 0.2; fewer failures = higher score)
|
|
621
|
+
const failureCount = verification.failureLog?.length ?? 0;
|
|
622
|
+
// Score decreases 0.04 per logged failure, capped at 0.2 deduction
|
|
623
|
+
const failurePenalty = Math.min(failureCount * 0.04, 0.2);
|
|
624
|
+
const failureScore = 0.2 - failurePenalty;
|
|
625
|
+
|
|
626
|
+
// Component 3: Round penalty (up to 0.2; fewer rounds = higher score)
|
|
627
|
+
const round = verification.round ?? 1;
|
|
628
|
+
// Round 1 = full 0.2; each extra round deducts 0.05
|
|
629
|
+
const roundPenalty = Math.min((round - 1) * 0.05, 0.2);
|
|
630
|
+
const roundScore = 0.2 - roundPenalty;
|
|
631
|
+
|
|
632
|
+
const total = gateScore + failureScore + roundScore;
|
|
633
|
+
return Math.round(Math.min(Math.max(total, 0), 1) * 1000) / 1000;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// ============================================================================
|
|
637
|
+
// Internal: Brain Persistence Helpers
|
|
638
|
+
// ============================================================================
|
|
639
|
+
|
|
640
|
+
async function persistVerificationObservation(
|
|
641
|
+
taskId: string,
|
|
642
|
+
task: Task | null,
|
|
643
|
+
verification: TaskVerification,
|
|
644
|
+
confidenceScore: number,
|
|
645
|
+
gatesPassed: VerificationGate[],
|
|
646
|
+
gatesFailed: VerificationGate[],
|
|
647
|
+
brainAccessor: BrainDataAccessor,
|
|
648
|
+
options: StorePredictionOptions,
|
|
649
|
+
): Promise<string> {
|
|
650
|
+
const id = `O-vconf-${randomBytes(4).toString('hex')}`;
|
|
651
|
+
const now = new Date().toISOString().replace('T', ' ').slice(0, 19);
|
|
652
|
+
|
|
653
|
+
const facts: string[] = [
|
|
654
|
+
`taskId: ${taskId}`,
|
|
655
|
+
`confidenceScore: ${confidenceScore}`,
|
|
656
|
+
`passed: ${verification.passed}`,
|
|
657
|
+
`round: ${verification.round}`,
|
|
658
|
+
`gatesPassed: ${gatesPassed.join(', ') || 'none'}`,
|
|
659
|
+
`gatesFailed: ${gatesFailed.join(', ') || 'none'}`,
|
|
660
|
+
`failureLogEntries: ${verification.failureLog?.length ?? 0}`,
|
|
661
|
+
];
|
|
662
|
+
|
|
663
|
+
if (task) {
|
|
664
|
+
facts.push(`taskSize: ${task.size ?? 'unspecified'}`);
|
|
665
|
+
facts.push(`taskPriority: ${task.priority}`);
|
|
666
|
+
if (task.labels?.length) {
|
|
667
|
+
facts.push(`labels: ${task.labels.join(', ')}`);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
const title = verification.passed
|
|
672
|
+
? `Verification passed: ${taskId} (confidence: ${Math.round(confidenceScore * 100)}%)`
|
|
673
|
+
: `Verification failed: ${taskId} (confidence: ${Math.round(confidenceScore * 100)}%)`;
|
|
674
|
+
|
|
675
|
+
await brainAccessor.addObservation({
|
|
676
|
+
id,
|
|
677
|
+
type: 'discovery',
|
|
678
|
+
title,
|
|
679
|
+
subtitle: `Round ${verification.round} — gates: ${gatesPassed.length} passed, ${gatesFailed.length} failed`,
|
|
680
|
+
narrative:
|
|
681
|
+
gatesFailed.length > 0
|
|
682
|
+
? `Failed gates: ${gatesFailed.join(', ')}. Consider addressing these before next round.`
|
|
683
|
+
: 'All tracked gates passed.',
|
|
684
|
+
factsJson: JSON.stringify(facts),
|
|
685
|
+
conceptsJson: JSON.stringify([
|
|
686
|
+
'verification',
|
|
687
|
+
'confidence-score',
|
|
688
|
+
'gate-tracking',
|
|
689
|
+
verification.passed ? 'pass' : 'fail',
|
|
690
|
+
]),
|
|
691
|
+
project: options.project ?? null,
|
|
692
|
+
filesReadJson: null,
|
|
693
|
+
filesModifiedJson: null,
|
|
694
|
+
sourceSessionId: options.sessionId ?? null,
|
|
695
|
+
sourceType: 'agent',
|
|
696
|
+
contentHash: null,
|
|
697
|
+
discoveryTokens: null,
|
|
698
|
+
createdAt: now,
|
|
699
|
+
updatedAt: null,
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
return id;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
async function maybeExtractLearning(
|
|
706
|
+
taskId: string,
|
|
707
|
+
task: Task | null,
|
|
708
|
+
verification: TaskVerification,
|
|
709
|
+
confidenceScore: number,
|
|
710
|
+
gatesPassed: VerificationGate[],
|
|
711
|
+
gatesFailed: VerificationGate[],
|
|
712
|
+
brainAccessor: BrainDataAccessor,
|
|
713
|
+
): Promise<string | undefined> {
|
|
714
|
+
// Only extract a learning when the result is notable:
|
|
715
|
+
// - High confidence pass (>= 0.8) on first round: positive learning
|
|
716
|
+
// - Low confidence or multiple failures: negative learning for future avoidance
|
|
717
|
+
const isNotable =
|
|
718
|
+
(verification.passed && confidenceScore >= 0.8 && verification.round === 1) ||
|
|
719
|
+
(!verification.passed && gatesFailed.length >= 2) ||
|
|
720
|
+
(!verification.passed && (verification.failureLog?.length ?? 0) >= 3);
|
|
721
|
+
|
|
722
|
+
if (!isNotable) return undefined;
|
|
723
|
+
|
|
724
|
+
const id = `L-vconf-${randomBytes(4).toString('hex')}`;
|
|
725
|
+
const now = new Date().toISOString().replace('T', ' ').slice(0, 19);
|
|
726
|
+
|
|
727
|
+
const taskLabels = task?.labels ?? [];
|
|
728
|
+
const taskSize = task?.size ?? 'medium';
|
|
729
|
+
const taskType = task?.type ?? 'task';
|
|
730
|
+
|
|
731
|
+
let insight: string;
|
|
732
|
+
let actionable: boolean;
|
|
733
|
+
let confidence: number;
|
|
734
|
+
|
|
735
|
+
if (verification.passed && confidenceScore >= 0.8) {
|
|
736
|
+
insight = `High-confidence verification pass for ${taskSize} ${taskType} task${
|
|
737
|
+
taskLabels.length ? ` with labels [${taskLabels.join(', ')}]` : ''
|
|
738
|
+
}. Pattern: ${gatesPassed.join(', ')} all passed on round ${verification.round}.`;
|
|
739
|
+
actionable = false;
|
|
740
|
+
confidence = confidenceScore;
|
|
741
|
+
} else {
|
|
742
|
+
insight = `Verification failure pattern: ${gatesFailed.join(', ')} failed${
|
|
743
|
+
taskLabels.length ? ` for tasks with labels [${taskLabels.join(', ')}]` : ''
|
|
744
|
+
}. Failures logged: ${verification.failureLog?.length ?? 0}. Address these gates early.`;
|
|
745
|
+
actionable = true;
|
|
746
|
+
confidence = Math.max(0.5, 1 - confidenceScore);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
await brainAccessor.addLearning({
|
|
750
|
+
id,
|
|
751
|
+
insight,
|
|
752
|
+
source: `verification-confidence:${taskId}`,
|
|
753
|
+
confidence,
|
|
754
|
+
actionable,
|
|
755
|
+
application: actionable
|
|
756
|
+
? `Focus on ${gatesFailed.join(', ')} gates for similar ${taskSize} tasks`
|
|
757
|
+
: null,
|
|
758
|
+
applicableTypesJson: JSON.stringify([taskType]),
|
|
759
|
+
createdAt: now,
|
|
760
|
+
updatedAt: null,
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
return id;
|
|
764
|
+
}
|