@cleocode/core 2026.3.43 → 2026.3.44

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 (161) hide show
  1. package/dist/admin/export-tasks.d.ts.map +1 -1
  2. package/dist/agents/agent-schema.d.ts +358 -0
  3. package/dist/agents/agent-schema.d.ts.map +1 -0
  4. package/dist/agents/capacity.d.ts +57 -0
  5. package/dist/agents/capacity.d.ts.map +1 -0
  6. package/dist/agents/index.d.ts +17 -0
  7. package/dist/agents/index.d.ts.map +1 -0
  8. package/dist/agents/registry.d.ts +115 -0
  9. package/dist/agents/registry.d.ts.map +1 -0
  10. package/dist/agents/retry.d.ts +83 -0
  11. package/dist/agents/retry.d.ts.map +1 -0
  12. package/dist/hooks/index.d.ts +4 -1
  13. package/dist/hooks/index.d.ts.map +1 -1
  14. package/dist/hooks/payload-schemas.d.ts +214 -0
  15. package/dist/hooks/payload-schemas.d.ts.map +1 -0
  16. package/dist/index.d.ts +3 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +16443 -2160
  19. package/dist/index.js.map +4 -4
  20. package/dist/inject/index.d.ts.map +1 -1
  21. package/dist/intelligence/impact.d.ts +51 -0
  22. package/dist/intelligence/impact.d.ts.map +1 -0
  23. package/dist/intelligence/index.d.ts +15 -0
  24. package/dist/intelligence/index.d.ts.map +1 -0
  25. package/dist/intelligence/patterns.d.ts +66 -0
  26. package/dist/intelligence/patterns.d.ts.map +1 -0
  27. package/dist/intelligence/prediction.d.ts +51 -0
  28. package/dist/intelligence/prediction.d.ts.map +1 -0
  29. package/dist/intelligence/types.d.ts +221 -0
  30. package/dist/intelligence/types.d.ts.map +1 -0
  31. package/dist/internal.d.ts +9 -0
  32. package/dist/internal.d.ts.map +1 -1
  33. package/dist/issue/template-parser.d.ts +8 -2
  34. package/dist/issue/template-parser.d.ts.map +1 -1
  35. package/dist/lifecycle/pipeline.d.ts +2 -2
  36. package/dist/lifecycle/pipeline.d.ts.map +1 -1
  37. package/dist/lifecycle/state-machine.d.ts +1 -1
  38. package/dist/lifecycle/state-machine.d.ts.map +1 -1
  39. package/dist/memory/brain-lifecycle.d.ts.map +1 -1
  40. package/dist/memory/brain-retrieval.d.ts.map +1 -1
  41. package/dist/memory/brain-row-types.d.ts +40 -6
  42. package/dist/memory/brain-row-types.d.ts.map +1 -1
  43. package/dist/memory/brain-search.d.ts.map +1 -1
  44. package/dist/memory/brain-similarity.d.ts.map +1 -1
  45. package/dist/memory/claude-mem-migration.d.ts.map +1 -1
  46. package/dist/nexus/discover.d.ts.map +1 -1
  47. package/dist/orchestration/bootstrap.d.ts.map +1 -1
  48. package/dist/orchestration/skill-ops.d.ts +4 -4
  49. package/dist/orchestration/skill-ops.d.ts.map +1 -1
  50. package/dist/otel/index.d.ts +1 -1
  51. package/dist/otel/index.d.ts.map +1 -1
  52. package/dist/sessions/briefing.d.ts.map +1 -1
  53. package/dist/sessions/handoff.d.ts.map +1 -1
  54. package/dist/sessions/index.d.ts +1 -1
  55. package/dist/sessions/index.d.ts.map +1 -1
  56. package/dist/sessions/types.d.ts +8 -42
  57. package/dist/sessions/types.d.ts.map +1 -1
  58. package/dist/signaldock/signaldock-transport.d.ts +1 -1
  59. package/dist/signaldock/signaldock-transport.d.ts.map +1 -1
  60. package/dist/skills/injection/subagent.d.ts +3 -3
  61. package/dist/skills/injection/subagent.d.ts.map +1 -1
  62. package/dist/skills/manifests/contribution.d.ts +2 -2
  63. package/dist/skills/manifests/contribution.d.ts.map +1 -1
  64. package/dist/skills/orchestrator/spawn.d.ts +6 -6
  65. package/dist/skills/orchestrator/spawn.d.ts.map +1 -1
  66. package/dist/skills/orchestrator/startup.d.ts +1 -1
  67. package/dist/skills/orchestrator/startup.d.ts.map +1 -1
  68. package/dist/skills/orchestrator/validator.d.ts +2 -2
  69. package/dist/skills/orchestrator/validator.d.ts.map +1 -1
  70. package/dist/skills/precedence-types.d.ts +24 -1
  71. package/dist/skills/precedence-types.d.ts.map +1 -1
  72. package/dist/skills/types.d.ts +70 -4
  73. package/dist/skills/types.d.ts.map +1 -1
  74. package/dist/store/export.d.ts +5 -4
  75. package/dist/store/export.d.ts.map +1 -1
  76. package/dist/store/tasks-schema.d.ts +12 -2
  77. package/dist/store/tasks-schema.d.ts.map +1 -1
  78. package/dist/store/typed-query.d.ts +12 -0
  79. package/dist/store/typed-query.d.ts.map +1 -0
  80. package/dist/store/validation-schemas.d.ts +2422 -50
  81. package/dist/store/validation-schemas.d.ts.map +1 -1
  82. package/dist/system/inject-generate.d.ts.map +1 -1
  83. package/dist/validation/doctor/checks.d.ts +5 -0
  84. package/dist/validation/doctor/checks.d.ts.map +1 -1
  85. package/dist/validation/engine.d.ts +10 -10
  86. package/dist/validation/engine.d.ts.map +1 -1
  87. package/dist/validation/index.d.ts +6 -2
  88. package/dist/validation/index.d.ts.map +1 -1
  89. package/dist/validation/protocol-common.d.ts +10 -2
  90. package/dist/validation/protocol-common.d.ts.map +1 -1
  91. package/migrations/drizzle-tasks/20260320013731_wave0-schema-hardening/migration.sql +84 -0
  92. package/migrations/drizzle-tasks/20260320013731_wave0-schema-hardening/snapshot.json +4060 -0
  93. package/migrations/drizzle-tasks/20260320020000_agent-dimension/migration.sql +35 -0
  94. package/migrations/drizzle-tasks/20260320020000_agent-dimension/snapshot.json +4312 -0
  95. package/package.json +2 -2
  96. package/src/admin/export-tasks.ts +2 -5
  97. package/src/agents/__tests__/capacity.test.ts +219 -0
  98. package/src/agents/__tests__/registry.test.ts +457 -0
  99. package/src/agents/__tests__/retry.test.ts +289 -0
  100. package/src/agents/agent-schema.ts +107 -0
  101. package/src/agents/capacity.ts +151 -0
  102. package/src/agents/index.ts +68 -0
  103. package/src/agents/registry.ts +449 -0
  104. package/src/agents/retry.ts +255 -0
  105. package/src/hooks/index.ts +20 -1
  106. package/src/hooks/payload-schemas.ts +199 -0
  107. package/src/index.ts +69 -0
  108. package/src/inject/index.ts +14 -14
  109. package/src/intelligence/__tests__/impact.test.ts +453 -0
  110. package/src/intelligence/__tests__/patterns.test.ts +450 -0
  111. package/src/intelligence/__tests__/prediction.test.ts +418 -0
  112. package/src/intelligence/impact.ts +638 -0
  113. package/src/intelligence/index.ts +47 -0
  114. package/src/intelligence/patterns.ts +621 -0
  115. package/src/intelligence/prediction.ts +621 -0
  116. package/src/intelligence/types.ts +273 -0
  117. package/src/internal.ts +82 -1
  118. package/src/issue/template-parser.ts +65 -4
  119. package/src/lifecycle/pipeline.ts +14 -7
  120. package/src/lifecycle/state-machine.ts +6 -2
  121. package/src/memory/brain-lifecycle.ts +5 -11
  122. package/src/memory/brain-retrieval.ts +44 -38
  123. package/src/memory/brain-row-types.ts +43 -6
  124. package/src/memory/brain-search.ts +53 -32
  125. package/src/memory/brain-similarity.ts +9 -8
  126. package/src/memory/claude-mem-migration.ts +4 -3
  127. package/src/nexus/__tests__/nexus-e2e.test.ts +1481 -0
  128. package/src/nexus/discover.ts +1 -0
  129. package/src/orchestration/bootstrap.ts +11 -17
  130. package/src/orchestration/skill-ops.ts +52 -32
  131. package/src/otel/index.ts +48 -4
  132. package/src/sessions/__tests__/briefing.test.ts +31 -2
  133. package/src/sessions/briefing.ts +27 -42
  134. package/src/sessions/handoff.ts +52 -86
  135. package/src/sessions/index.ts +5 -1
  136. package/src/sessions/types.ts +9 -43
  137. package/src/signaldock/signaldock-transport.ts +5 -2
  138. package/src/skills/injection/subagent.ts +10 -16
  139. package/src/skills/manifests/contribution.ts +5 -13
  140. package/src/skills/orchestrator/__tests__/spawn-tier.test.ts +44 -30
  141. package/src/skills/orchestrator/spawn.ts +18 -31
  142. package/src/skills/orchestrator/startup.ts +78 -65
  143. package/src/skills/orchestrator/validator.ts +26 -31
  144. package/src/skills/precedence-types.ts +24 -1
  145. package/src/skills/types.ts +72 -5
  146. package/src/store/__tests__/test-db-helper.d.ts +4 -4
  147. package/src/store/__tests__/test-db-helper.js +5 -16
  148. package/src/store/__tests__/test-db-helper.ts +5 -18
  149. package/src/store/chain-schema.ts +1 -1
  150. package/src/store/export.ts +22 -12
  151. package/src/store/tasks-schema.ts +65 -8
  152. package/src/store/typed-query.ts +17 -0
  153. package/src/store/validation-schemas.ts +347 -23
  154. package/src/system/inject-generate.ts +9 -23
  155. package/src/validation/doctor/checks.ts +24 -2
  156. package/src/validation/engine.ts +11 -11
  157. package/src/validation/index.ts +131 -3
  158. package/src/validation/protocol-common.ts +54 -3
  159. package/dist/tasks/reparent.d.ts +0 -38
  160. package/dist/tasks/reparent.d.ts.map +0 -1
  161. package/src/tasks/reparent.ts +0 -134
@@ -0,0 +1,638 @@
1
+ /**
2
+ * Impact analysis module - dependency-aware prediction of downstream effects.
3
+ *
4
+ * Builds on the existing dependency graph infrastructure in phases/deps.ts
5
+ * and orchestration/analyze.ts to provide:
6
+ * - Task impact assessment (direct + transitive dependents)
7
+ * - Change impact prediction (cancel, block, complete, reprioritize)
8
+ * - Blast radius calculation (scope quantification)
9
+ *
10
+ * @module intelligence
11
+ */
12
+
13
+ import type { Task } from '@cleocode/contracts';
14
+ import type { DataAccessor } from '../store/data-accessor.js';
15
+ import { getAccessor } from '../store/data-accessor.js';
16
+ import { getCriticalPath } from '../tasks/graph-ops.js';
17
+ import { getParentChain } from '../tasks/hierarchy.js';
18
+ import type {
19
+ AffectedTask,
20
+ BlastRadius,
21
+ BlastRadiusSeverity,
22
+ ChangeImpact,
23
+ ChangeType,
24
+ ImpactAssessment,
25
+ } from './types.js';
26
+
27
+ // ============================================================================
28
+ // Internal Helpers
29
+ // ============================================================================
30
+
31
+ /**
32
+ * Load all tasks from the data store.
33
+ */
34
+ async function loadAllTasks(accessor: DataAccessor): Promise<Task[]> {
35
+ const { tasks } = await accessor.queryTasks({});
36
+ return tasks;
37
+ }
38
+
39
+ /**
40
+ * Build a reverse adjacency map: taskId -> set of tasks that depend on it.
41
+ * Reuses buildGraph from phases/deps.ts for the forward graph, then inverts.
42
+ */
43
+ function buildDependentsMap(tasks: Task[]): Map<string, Set<string>> {
44
+ const dependents = new Map<string, Set<string>>();
45
+
46
+ for (const task of tasks) {
47
+ if (!dependents.has(task.id)) {
48
+ dependents.set(task.id, new Set());
49
+ }
50
+ if (task.depends) {
51
+ for (const depId of task.depends) {
52
+ if (!dependents.has(depId)) {
53
+ dependents.set(depId, new Set());
54
+ }
55
+ dependents.get(depId)!.add(task.id);
56
+ }
57
+ }
58
+ }
59
+
60
+ return dependents;
61
+ }
62
+
63
+ /**
64
+ * Collect all transitive dependents via BFS.
65
+ * Returns set excluding the source task itself.
66
+ */
67
+ function collectTransitiveDependents(
68
+ taskId: string,
69
+ dependentsMap: Map<string, Set<string>>,
70
+ ): Set<string> {
71
+ const visited = new Set<string>();
72
+ const queue: string[] = [taskId];
73
+
74
+ while (queue.length > 0) {
75
+ const current = queue.shift()!;
76
+ const deps = dependentsMap.get(current);
77
+ if (!deps) continue;
78
+
79
+ for (const depId of deps) {
80
+ if (!visited.has(depId)) {
81
+ visited.add(depId);
82
+ queue.push(depId);
83
+ }
84
+ }
85
+ }
86
+
87
+ return visited;
88
+ }
89
+
90
+ /**
91
+ * Count tasks that would be blocked (have unmet dependencies) if
92
+ * the given task is not completed.
93
+ */
94
+ function countBlockedWork(
95
+ taskId: string,
96
+ transitiveDependents: Set<string>,
97
+ taskMap: Map<string, Task>,
98
+ ): number {
99
+ let count = 0;
100
+
101
+ for (const depId of transitiveDependents) {
102
+ if (depId === taskId) continue; // Exclude the source task itself
103
+ const task = taskMap.get(depId);
104
+ if (!task) continue;
105
+ // A task is considered blocked-work if it has a dependency on the
106
+ // source task (directly or transitively) and is not yet completed.
107
+ if (task.status !== 'done' && task.status !== 'cancelled') {
108
+ count++;
109
+ }
110
+ }
111
+
112
+ return count;
113
+ }
114
+
115
+ /**
116
+ * Find epic IDs whose pipelines are affected by changes to a task.
117
+ * A pipeline is affected if the task or any of its transitive dependents
118
+ * belong to that epic's hierarchy.
119
+ */
120
+ function findAffectedPipelines(
121
+ taskId: string,
122
+ transitiveDependents: Set<string>,
123
+ tasks: Task[],
124
+ ): string[] {
125
+ const affectedEpicIds = new Set<string>();
126
+ const allAffectedIds = new Set([taskId, ...transitiveDependents]);
127
+
128
+ for (const id of allAffectedIds) {
129
+ const task = tasks.find((t) => t.id === id);
130
+ if (!task) continue;
131
+
132
+ // Walk parent chain to find epics
133
+ const ancestors = getParentChain(id, tasks);
134
+ for (const ancestor of ancestors) {
135
+ if (ancestor.type === 'epic') {
136
+ affectedEpicIds.add(ancestor.id);
137
+ }
138
+ }
139
+
140
+ // The task itself might be an epic
141
+ if (task.type === 'epic') {
142
+ affectedEpicIds.add(task.id);
143
+ }
144
+ }
145
+
146
+ return Array.from(affectedEpicIds);
147
+ }
148
+
149
+ /**
150
+ * Check whether a task lies on the critical path.
151
+ * Reuses getCriticalPath from tasks/graph-ops.ts.
152
+ */
153
+ function isTaskOnCriticalPath(taskId: string, tasks: Task[]): boolean {
154
+ const criticalPath = getCriticalPath(tasks);
155
+ return criticalPath.includes(taskId);
156
+ }
157
+
158
+ /**
159
+ * Classify blast radius severity based on project percentage.
160
+ */
161
+ function classifySeverity(projectPercentage: number): BlastRadiusSeverity {
162
+ if (projectPercentage <= 1) return 'isolated';
163
+ if (projectPercentage <= 10) return 'moderate';
164
+ if (projectPercentage <= 30) return 'widespread';
165
+ return 'critical';
166
+ }
167
+
168
+ /**
169
+ * Compute the maximum cascade depth via DFS from the source task
170
+ * through its transitive dependents.
171
+ */
172
+ function computeCascadeDepth(taskId: string, dependentsMap: Map<string, Set<string>>): number {
173
+ const visited = new Set<string>();
174
+
175
+ function dfs(id: string): number {
176
+ if (visited.has(id)) return 0;
177
+ visited.add(id);
178
+
179
+ const deps = dependentsMap.get(id);
180
+ if (!deps || deps.size === 0) return 0;
181
+
182
+ let maxDepth = 0;
183
+ for (const depId of deps) {
184
+ const depth = dfs(depId);
185
+ if (depth > maxDepth) maxDepth = depth;
186
+ }
187
+
188
+ return maxDepth + 1;
189
+ }
190
+
191
+ return dfs(taskId);
192
+ }
193
+
194
+ // ============================================================================
195
+ // Public API
196
+ // ============================================================================
197
+
198
+ /**
199
+ * Analyze the full downstream impact of a task.
200
+ *
201
+ * Computes direct and transitive dependents, affected lifecycle pipelines,
202
+ * blocked work counts, critical path membership, and blast radius.
203
+ *
204
+ * @param taskId - The task to analyze
205
+ * @param accessor - DataAccessor instance (or auto-created from cwd)
206
+ * @param cwd - Working directory (used if accessor is not provided)
207
+ * @returns Full impact assessment
208
+ */
209
+ export async function analyzeTaskImpact(
210
+ taskId: string,
211
+ accessor?: DataAccessor,
212
+ cwd?: string,
213
+ ): Promise<ImpactAssessment> {
214
+ const acc = accessor ?? (await getAccessor(cwd));
215
+ const tasks = await loadAllTasks(acc);
216
+ const taskMap = new Map(tasks.map((t) => [t.id, t]));
217
+
218
+ if (!taskMap.has(taskId)) {
219
+ return {
220
+ taskId,
221
+ directDependents: [],
222
+ transitiveDependents: [],
223
+ affectedPipelines: [],
224
+ blockedWorkCount: 0,
225
+ isOnCriticalPath: false,
226
+ blastRadius: {
227
+ directCount: 0,
228
+ transitiveCount: 0,
229
+ epicCount: 0,
230
+ projectPercentage: 0,
231
+ severity: 'isolated',
232
+ },
233
+ };
234
+ }
235
+
236
+ const dependentsMap = buildDependentsMap(tasks);
237
+ const directDeps = dependentsMap.get(taskId) ?? new Set<string>();
238
+ const transitiveDeps = collectTransitiveDependents(taskId, dependentsMap);
239
+
240
+ const affectedPipelines = findAffectedPipelines(taskId, transitiveDeps, tasks);
241
+ const blockedWorkCount = countBlockedWork(taskId, transitiveDeps, taskMap);
242
+ const onCriticalPath = isTaskOnCriticalPath(taskId, tasks);
243
+ const blastRadius = calculateBlastRadiusFromData(taskId, directDeps, transitiveDeps, tasks);
244
+
245
+ return {
246
+ taskId,
247
+ directDependents: Array.from(directDeps),
248
+ transitiveDependents: Array.from(transitiveDeps),
249
+ affectedPipelines,
250
+ blockedWorkCount,
251
+ isOnCriticalPath: onCriticalPath,
252
+ blastRadius,
253
+ };
254
+ }
255
+
256
+ /**
257
+ * Analyze the downstream effects of a specific change to a task.
258
+ *
259
+ * Predicts what happens when a task is cancelled, blocked, completed,
260
+ * or reprioritized, including cascading status changes.
261
+ *
262
+ * @param taskId - The task being changed
263
+ * @param changeType - The type of change
264
+ * @param accessor - DataAccessor instance (or auto-created from cwd)
265
+ * @param cwd - Working directory (used if accessor is not provided)
266
+ * @returns Predicted change impact
267
+ */
268
+ export async function analyzeChangeImpact(
269
+ taskId: string,
270
+ changeType: ChangeType,
271
+ accessor?: DataAccessor,
272
+ cwd?: string,
273
+ ): Promise<ChangeImpact> {
274
+ const acc = accessor ?? (await getAccessor(cwd));
275
+ const tasks = await loadAllTasks(acc);
276
+ const taskMap = new Map(tasks.map((t) => [t.id, t]));
277
+
278
+ const sourceTask = taskMap.get(taskId);
279
+ if (!sourceTask) {
280
+ return {
281
+ taskId,
282
+ changeType,
283
+ affectedTasks: [],
284
+ cascadeDepth: 0,
285
+ recommendation: `Task ${taskId} not found.`,
286
+ };
287
+ }
288
+
289
+ const dependentsMap = buildDependentsMap(tasks);
290
+ const transitiveDeps = collectTransitiveDependents(taskId, dependentsMap);
291
+ const cascadeDepth = computeCascadeDepth(taskId, dependentsMap);
292
+ const affectedTasks: AffectedTask[] = [];
293
+
294
+ switch (changeType) {
295
+ case 'cancel':
296
+ affectedTasks.push(...predictCancelEffects(taskId, transitiveDeps, dependentsMap, taskMap));
297
+ break;
298
+ case 'block':
299
+ affectedTasks.push(...predictBlockEffects(taskId, transitiveDeps, dependentsMap, taskMap));
300
+ break;
301
+ case 'complete':
302
+ affectedTasks.push(...predictCompleteEffects(taskId, transitiveDeps, dependentsMap, taskMap));
303
+ break;
304
+ case 'reprioritize':
305
+ affectedTasks.push(...predictReprioritizeEffects(taskId, transitiveDeps, taskMap));
306
+ break;
307
+ }
308
+
309
+ const recommendation = generateRecommendation(
310
+ changeType,
311
+ affectedTasks.length,
312
+ cascadeDepth,
313
+ taskId,
314
+ );
315
+
316
+ return {
317
+ taskId,
318
+ changeType,
319
+ affectedTasks,
320
+ cascadeDepth,
321
+ recommendation,
322
+ };
323
+ }
324
+
325
+ /**
326
+ * Calculate the blast radius for a task.
327
+ *
328
+ * Quantifies how many tasks, epics, and what percentage of the project
329
+ * would be impacted by changes to this task.
330
+ *
331
+ * @param taskId - The task to analyze
332
+ * @param accessor - DataAccessor instance (or auto-created from cwd)
333
+ * @param cwd - Working directory (used if accessor is not provided)
334
+ * @returns Blast radius metrics
335
+ */
336
+ export async function calculateBlastRadius(
337
+ taskId: string,
338
+ accessor?: DataAccessor,
339
+ cwd?: string,
340
+ ): Promise<BlastRadius> {
341
+ const acc = accessor ?? (await getAccessor(cwd));
342
+ const tasks = await loadAllTasks(acc);
343
+ const taskMap = new Map(tasks.map((t) => [t.id, t]));
344
+
345
+ if (!taskMap.has(taskId)) {
346
+ return {
347
+ directCount: 0,
348
+ transitiveCount: 0,
349
+ epicCount: 0,
350
+ projectPercentage: 0,
351
+ severity: 'isolated',
352
+ };
353
+ }
354
+
355
+ const dependentsMap = buildDependentsMap(tasks);
356
+ const directDeps = dependentsMap.get(taskId) ?? new Set<string>();
357
+ const transitiveDeps = collectTransitiveDependents(taskId, dependentsMap);
358
+
359
+ return calculateBlastRadiusFromData(taskId, directDeps, transitiveDeps, tasks);
360
+ }
361
+
362
+ // ============================================================================
363
+ // Change Effect Predictors
364
+ // ============================================================================
365
+
366
+ /**
367
+ * Predict effects of cancelling a task.
368
+ * Direct dependents whose only unmet dependency is this task become orphaned.
369
+ * Transitive dependents that lose their last prerequisite also cascade.
370
+ */
371
+ function predictCancelEffects(
372
+ taskId: string,
373
+ transitiveDeps: Set<string>,
374
+ dependentsMap: Map<string, Set<string>>,
375
+ taskMap: Map<string, Task>,
376
+ ): AffectedTask[] {
377
+ const affected: AffectedTask[] = [];
378
+ const directDeps = dependentsMap.get(taskId) ?? new Set<string>();
379
+
380
+ // Direct dependents: they lose a dependency
381
+ for (const depId of directDeps) {
382
+ const task = taskMap.get(depId);
383
+ if (!task || task.status === 'done' || task.status === 'cancelled') continue;
384
+
385
+ const otherUnmetDeps = (task.depends ?? []).filter(
386
+ (d) =>
387
+ d !== taskId &&
388
+ taskMap.has(d) &&
389
+ taskMap.get(d)!.status !== 'done' &&
390
+ taskMap.get(d)!.status !== 'cancelled',
391
+ );
392
+
393
+ if (otherUnmetDeps.length === 0) {
394
+ // This was the only blocking dep -- task becomes unblocked but orphaned
395
+ affected.push({
396
+ id: depId,
397
+ title: task.title,
398
+ currentStatus: task.status,
399
+ newStatus: task.status === 'blocked' ? 'pending' : undefined,
400
+ reason: 'Direct dependency cancelled; dependency link becomes orphaned.',
401
+ });
402
+ } else {
403
+ affected.push({
404
+ id: depId,
405
+ title: task.title,
406
+ currentStatus: task.status,
407
+ reason: 'Direct dependency cancelled; other dependencies remain.',
408
+ });
409
+ }
410
+ }
411
+
412
+ // Transitive dependents (excluding direct)
413
+ for (const depId of transitiveDeps) {
414
+ if (directDeps.has(depId)) continue;
415
+ const task = taskMap.get(depId);
416
+ if (!task || task.status === 'done' || task.status === 'cancelled') continue;
417
+
418
+ affected.push({
419
+ id: depId,
420
+ title: task.title,
421
+ currentStatus: task.status,
422
+ reason: 'Transitive dependency cancelled; may cascade through dependency chain.',
423
+ });
424
+ }
425
+
426
+ return affected;
427
+ }
428
+
429
+ /**
430
+ * Predict effects of blocking a task.
431
+ * All downstream dependents that are not yet done become cascading-blocked.
432
+ */
433
+ function predictBlockEffects(
434
+ taskId: string,
435
+ transitiveDeps: Set<string>,
436
+ dependentsMap: Map<string, Set<string>>,
437
+ taskMap: Map<string, Task>,
438
+ ): AffectedTask[] {
439
+ const affected: AffectedTask[] = [];
440
+
441
+ for (const depId of transitiveDeps) {
442
+ const task = taskMap.get(depId);
443
+ if (!task || task.status === 'done' || task.status === 'cancelled') continue;
444
+
445
+ const isDirect = (dependentsMap.get(taskId) ?? new Set()).has(depId);
446
+
447
+ affected.push({
448
+ id: depId,
449
+ title: task.title,
450
+ currentStatus: task.status,
451
+ newStatus: 'blocked',
452
+ reason: isDirect
453
+ ? 'Direct dependency blocked; task cannot proceed.'
454
+ : 'Transitive dependency blocked; cascading block through dependency chain.',
455
+ });
456
+ }
457
+
458
+ return affected;
459
+ }
460
+
461
+ /**
462
+ * Predict effects of completing a task.
463
+ * Dependents whose last unmet dependency was this task become unblocked.
464
+ */
465
+ function predictCompleteEffects(
466
+ taskId: string,
467
+ transitiveDeps: Set<string>,
468
+ dependentsMap: Map<string, Set<string>>,
469
+ taskMap: Map<string, Task>,
470
+ ): AffectedTask[] {
471
+ const affected: AffectedTask[] = [];
472
+ const directDeps = dependentsMap.get(taskId) ?? new Set<string>();
473
+
474
+ for (const depId of directDeps) {
475
+ const task = taskMap.get(depId);
476
+ if (!task || task.status === 'done' || task.status === 'cancelled') continue;
477
+
478
+ const remainingUnmet = (task.depends ?? []).filter(
479
+ (d) =>
480
+ d !== taskId &&
481
+ taskMap.has(d) &&
482
+ taskMap.get(d)!.status !== 'done' &&
483
+ taskMap.get(d)!.status !== 'cancelled',
484
+ );
485
+
486
+ if (remainingUnmet.length === 0) {
487
+ affected.push({
488
+ id: depId,
489
+ title: task.title,
490
+ currentStatus: task.status,
491
+ newStatus: task.status === 'blocked' ? 'pending' : task.status,
492
+ reason: 'All dependencies met; task becomes unblocked.',
493
+ });
494
+ } else {
495
+ affected.push({
496
+ id: depId,
497
+ title: task.title,
498
+ currentStatus: task.status,
499
+ reason: `Dependency completed; ${remainingUnmet.length} other dependency(ies) still unmet.`,
500
+ });
501
+ }
502
+ }
503
+
504
+ // Note transitive downstream tasks that benefit indirectly
505
+ for (const depId of transitiveDeps) {
506
+ if (directDeps.has(depId)) continue; // Already handled above
507
+ const task = taskMap.get(depId);
508
+ if (!task || task.status === 'done' || task.status === 'cancelled') continue;
509
+
510
+ affected.push({
511
+ id: depId,
512
+ title: task.title,
513
+ currentStatus: task.status,
514
+ reason: 'Upstream dependency completed; may unblock cascading work.',
515
+ });
516
+ }
517
+
518
+ return affected;
519
+ }
520
+
521
+ /**
522
+ * Predict effects of reprioritizing a task.
523
+ * Downstream tasks may need reordering in execution waves.
524
+ */
525
+ function predictReprioritizeEffects(
526
+ taskId: string,
527
+ transitiveDeps: Set<string>,
528
+ taskMap: Map<string, Task>,
529
+ ): AffectedTask[] {
530
+ const affected: AffectedTask[] = [];
531
+
532
+ for (const depId of transitiveDeps) {
533
+ const task = taskMap.get(depId);
534
+ if (!task || task.status === 'done' || task.status === 'cancelled') continue;
535
+
536
+ const isDirect = (task.depends ?? []).includes(taskId);
537
+ affected.push({
538
+ id: depId,
539
+ title: task.title,
540
+ currentStatus: task.status,
541
+ reason: isDirect
542
+ ? `Direct dependency ${taskId} reprioritized; execution order may change.`
543
+ : `Upstream dependency ${taskId} reprioritized; cascading reorder possible.`,
544
+ });
545
+ }
546
+
547
+ return affected;
548
+ }
549
+
550
+ // ============================================================================
551
+ // Blast Radius Computation
552
+ // ============================================================================
553
+
554
+ /**
555
+ * Internal blast radius computation from pre-computed dependency data.
556
+ */
557
+ function calculateBlastRadiusFromData(
558
+ taskId: string,
559
+ directDeps: Set<string>,
560
+ transitiveDeps: Set<string>,
561
+ tasks: Task[],
562
+ ): BlastRadius {
563
+ const totalTasks = tasks.length;
564
+
565
+ // Find affected epics
566
+ const affectedEpicIds = new Set<string>();
567
+ const allAffectedIds = new Set([taskId, ...transitiveDeps]);
568
+
569
+ for (const id of allAffectedIds) {
570
+ const task = tasks.find((t) => t.id === id);
571
+ if (!task) continue;
572
+
573
+ const ancestors = getParentChain(id, tasks);
574
+ for (const ancestor of ancestors) {
575
+ if (ancestor.type === 'epic') {
576
+ affectedEpicIds.add(ancestor.id);
577
+ }
578
+ }
579
+
580
+ if (task.type === 'epic') {
581
+ affectedEpicIds.add(task.id);
582
+ }
583
+ }
584
+
585
+ const projectPercentage =
586
+ totalTasks > 0 ? Math.round((transitiveDeps.size / totalTasks) * 100 * 100) / 100 : 0;
587
+
588
+ return {
589
+ directCount: directDeps.size,
590
+ transitiveCount: transitiveDeps.size,
591
+ epicCount: affectedEpicIds.size,
592
+ projectPercentage,
593
+ severity: classifySeverity(projectPercentage),
594
+ };
595
+ }
596
+
597
+ // ============================================================================
598
+ // Recommendation Generator
599
+ // ============================================================================
600
+
601
+ /**
602
+ * Generate a human-readable recommendation based on impact analysis.
603
+ */
604
+ function generateRecommendation(
605
+ changeType: ChangeType,
606
+ affectedCount: number,
607
+ cascadeDepth: number,
608
+ taskId: string,
609
+ ): string {
610
+ if (affectedCount === 0) {
611
+ return `No downstream tasks affected. Safe to ${changeType} ${taskId}.`;
612
+ }
613
+
614
+ const severity = affectedCount > 10 ? 'High' : affectedCount > 3 ? 'Moderate' : 'Low';
615
+
616
+ switch (changeType) {
617
+ case 'cancel':
618
+ return (
619
+ `${severity} impact: cancelling ${taskId} affects ${affectedCount} downstream task(s) ` +
620
+ `with cascade depth ${cascadeDepth}. Review affected tasks for orphaned dependencies.`
621
+ );
622
+ case 'block':
623
+ return (
624
+ `${severity} impact: blocking ${taskId} would cascade-block ${affectedCount} downstream task(s) ` +
625
+ `across ${cascadeDepth} level(s). Consider resolving the blocker to unblock the pipeline.`
626
+ );
627
+ case 'complete':
628
+ return (
629
+ `Completing ${taskId} would unblock or partially unblock ${affectedCount} downstream task(s). ` +
630
+ `Cascade depth: ${cascadeDepth}.`
631
+ );
632
+ case 'reprioritize':
633
+ return (
634
+ `${severity} impact: reprioritizing ${taskId} may reorder ${affectedCount} downstream task(s) ` +
635
+ `across ${cascadeDepth} level(s) of dependencies.`
636
+ );
637
+ }
638
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * CLEO Intelligence dimension — Quality Prediction and Pattern Extraction.
3
+ *
4
+ * Provides risk scoring, validation outcome prediction, automatic pattern
5
+ * detection, pattern matching, and pattern storage backed by the existing
6
+ * brain_patterns and brain_learnings tables.
7
+ *
8
+ * @task Wave3A
9
+ * @epic T5149
10
+ */
11
+
12
+ // Impact analysis
13
+ export {
14
+ analyzeChangeImpact,
15
+ analyzeTaskImpact,
16
+ calculateBlastRadius,
17
+ } from './impact.js';
18
+ // Patterns
19
+ export {
20
+ extractPatternsFromHistory,
21
+ matchPatterns,
22
+ storeDetectedPattern,
23
+ updatePatternStats,
24
+ } from './patterns.js';
25
+ // Prediction
26
+ export {
27
+ calculateTaskRisk,
28
+ gatherLearningContext,
29
+ predictValidationOutcome,
30
+ } from './prediction.js';
31
+ // Types
32
+ export type {
33
+ AffectedTask,
34
+ BlastRadius,
35
+ BlastRadiusSeverity,
36
+ ChangeImpact,
37
+ ChangeType,
38
+ DetectedPattern,
39
+ ImpactAssessment,
40
+ LearningContext,
41
+ PatternExtractionOptions,
42
+ PatternMatch,
43
+ PatternStatsUpdate,
44
+ RiskAssessment,
45
+ RiskFactor,
46
+ ValidationPrediction,
47
+ } from './types.js';