@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.
Files changed (136) hide show
  1. package/dist/agents/agent-registry.d.ts +206 -0
  2. package/dist/agents/agent-registry.d.ts.map +1 -0
  3. package/dist/agents/agent-schema.d.ts.map +1 -1
  4. package/dist/agents/execution-learning.d.ts +223 -0
  5. package/dist/agents/execution-learning.d.ts.map +1 -0
  6. package/dist/agents/health-monitor.d.ts +161 -0
  7. package/dist/agents/health-monitor.d.ts.map +1 -0
  8. package/dist/agents/index.d.ts +4 -1
  9. package/dist/agents/index.d.ts.map +1 -1
  10. package/dist/agents/retry.d.ts +57 -4
  11. package/dist/agents/retry.d.ts.map +1 -1
  12. package/dist/backfill/index.d.ts +83 -0
  13. package/dist/backfill/index.d.ts.map +1 -0
  14. package/dist/bootstrap.d.ts +1 -1
  15. package/dist/config.d.ts +47 -0
  16. package/dist/config.d.ts.map +1 -1
  17. package/dist/index.d.ts +2 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +6985 -5068
  20. package/dist/index.js.map +4 -4
  21. package/dist/intelligence/adaptive-validation.d.ts +151 -0
  22. package/dist/intelligence/adaptive-validation.d.ts.map +1 -0
  23. package/dist/intelligence/impact.d.ts +34 -1
  24. package/dist/intelligence/impact.d.ts.map +1 -1
  25. package/dist/intelligence/index.d.ts +7 -2
  26. package/dist/intelligence/index.d.ts.map +1 -1
  27. package/dist/intelligence/types.d.ts +60 -0
  28. package/dist/intelligence/types.d.ts.map +1 -1
  29. package/dist/internal.d.ts +8 -4
  30. package/dist/internal.d.ts.map +1 -1
  31. package/dist/lib/index.d.ts +10 -0
  32. package/dist/lib/index.d.ts.map +1 -0
  33. package/dist/lib/retry.d.ts +128 -0
  34. package/dist/lib/retry.d.ts.map +1 -0
  35. package/dist/nexus/sharing/index.d.ts +48 -2
  36. package/dist/nexus/sharing/index.d.ts.map +1 -1
  37. package/dist/sessions/session-enforcement.d.ts.map +1 -1
  38. package/dist/stats/index.d.ts +1 -0
  39. package/dist/stats/index.d.ts.map +1 -1
  40. package/dist/stats/workflow-telemetry.d.ts +89 -0
  41. package/dist/stats/workflow-telemetry.d.ts.map +1 -0
  42. package/dist/store/brain-schema.d.ts.map +1 -1
  43. package/dist/store/converters.d.ts.map +1 -1
  44. package/dist/store/cross-db-cleanup.d.ts +93 -0
  45. package/dist/store/cross-db-cleanup.d.ts.map +1 -0
  46. package/dist/store/db-helpers.d.ts.map +1 -1
  47. package/dist/store/migration-sqlite.d.ts.map +1 -1
  48. package/dist/store/sqlite-data-accessor.d.ts.map +1 -1
  49. package/dist/store/sqlite.d.ts.map +1 -1
  50. package/dist/store/task-store.d.ts.map +1 -1
  51. package/dist/store/tasks-schema.d.ts +18 -3
  52. package/dist/store/tasks-schema.d.ts.map +1 -1
  53. package/dist/store/validation-schemas.d.ts +32 -0
  54. package/dist/store/validation-schemas.d.ts.map +1 -1
  55. package/dist/tasks/add.d.ts +10 -1
  56. package/dist/tasks/add.d.ts.map +1 -1
  57. package/dist/tasks/complete.d.ts.map +1 -1
  58. package/dist/tasks/enforcement.d.ts +22 -0
  59. package/dist/tasks/enforcement.d.ts.map +1 -0
  60. package/dist/tasks/epic-enforcement.d.ts +199 -0
  61. package/dist/tasks/epic-enforcement.d.ts.map +1 -0
  62. package/dist/tasks/index.d.ts +1 -1
  63. package/dist/tasks/index.d.ts.map +1 -1
  64. package/dist/tasks/pipeline-stage.d.ts +181 -0
  65. package/dist/tasks/pipeline-stage.d.ts.map +1 -0
  66. package/dist/tasks/update.d.ts +2 -0
  67. package/dist/tasks/update.d.ts.map +1 -1
  68. package/migrations/drizzle-brain/20260321000001_t033-brain-indexes/migration.sql +12 -0
  69. package/migrations/drizzle-brain/20260321000001_t033-brain-indexes/snapshot.json +1232 -0
  70. package/migrations/drizzle-tasks/20260321000000_t033-connection-health/migration.sql +518 -0
  71. package/migrations/drizzle-tasks/20260321000000_t033-connection-health/snapshot.json +4312 -0
  72. package/migrations/drizzle-tasks/20260321000002_t060-pipeline-stage-binding/migration.sql +82 -0
  73. package/migrations/drizzle-tasks/20260321000002_t060-pipeline-stage-binding/snapshot.json +9 -0
  74. package/package.json +5 -5
  75. package/schemas/config.schema.json +37 -1547
  76. package/src/__tests__/sharing.test.ts +24 -0
  77. package/src/agents/__tests__/agent-registry.test.ts +351 -0
  78. package/src/agents/__tests__/execution-learning.test.ts +684 -0
  79. package/src/agents/__tests__/health-monitor.test.ts +332 -0
  80. package/src/agents/__tests__/registry.test.ts +30 -2
  81. package/src/agents/agent-registry.ts +394 -0
  82. package/src/agents/agent-schema.ts +5 -0
  83. package/src/agents/execution-learning.ts +675 -0
  84. package/src/agents/health-monitor.ts +279 -0
  85. package/src/agents/index.ts +37 -1
  86. package/src/agents/retry.ts +57 -4
  87. package/src/backfill/index.ts +309 -0
  88. package/src/bootstrap.ts +1 -1
  89. package/src/config.ts +126 -0
  90. package/src/index.ts +8 -1
  91. package/src/intelligence/__tests__/adaptive-validation.test.ts +694 -0
  92. package/src/intelligence/__tests__/impact.test.ts +165 -1
  93. package/src/intelligence/adaptive-validation.ts +764 -0
  94. package/src/intelligence/impact.ts +203 -0
  95. package/src/intelligence/index.ts +19 -0
  96. package/src/intelligence/types.ts +76 -0
  97. package/src/internal.ts +39 -0
  98. package/src/lib/__tests__/retry.test.ts +321 -0
  99. package/src/lib/index.ts +16 -0
  100. package/src/lib/retry.ts +224 -0
  101. package/src/lifecycle/__tests__/chain-store.test.ts +7 -0
  102. package/src/lifecycle/__tests__/tessera-engine.test.ts +52 -0
  103. package/src/nexus/sharing/index.ts +142 -2
  104. package/src/sessions/__tests__/session-edge-cases.test.ts +24 -1
  105. package/src/sessions/session-enforcement.ts +13 -2
  106. package/src/stats/index.ts +7 -0
  107. package/src/stats/workflow-telemetry.ts +502 -0
  108. package/src/store/__tests__/migration-safety.test.ts +3 -0
  109. package/src/store/__tests__/session-store.test.ts +132 -1
  110. package/src/store/__tests__/task-store.test.ts +22 -1
  111. package/src/store/__tests__/test-db-helper.ts +29 -2
  112. package/src/store/brain-schema.ts +4 -1
  113. package/src/store/converters.ts +2 -0
  114. package/src/store/cross-db-cleanup.ts +192 -0
  115. package/src/store/db-helpers.ts +2 -0
  116. package/src/store/migration-sqlite.ts +6 -0
  117. package/src/store/sqlite-data-accessor.ts +20 -28
  118. package/src/store/sqlite.ts +14 -2
  119. package/src/store/task-store.ts +6 -0
  120. package/src/store/tasks-schema.ts +59 -20
  121. package/src/tasks/__tests__/add.test.ts +16 -0
  122. package/src/tasks/__tests__/complete-unblocks.test.ts +10 -1
  123. package/src/tasks/__tests__/complete.test.ts +11 -2
  124. package/src/tasks/__tests__/epic-enforcement.test.ts +909 -0
  125. package/src/tasks/__tests__/minimal-test.test.ts +28 -0
  126. package/src/tasks/__tests__/pipeline-stage.test.ts +403 -0
  127. package/src/tasks/__tests__/update.test.ts +40 -6
  128. package/src/tasks/add.ts +128 -2
  129. package/src/tasks/complete.ts +29 -17
  130. package/src/tasks/enforcement.ts +127 -0
  131. package/src/tasks/epic-enforcement.ts +364 -0
  132. package/src/tasks/index.ts +1 -0
  133. package/src/tasks/pipeline-stage.ts +293 -0
  134. package/src/tasks/update.ts +62 -0
  135. package/templates/config.template.json +34 -111
  136. package/templates/global-config.template.json +24 -40
@@ -0,0 +1,364 @@
1
+ /**
2
+ * Epic lifecycle pipeline enforcement (RCASD-IVTR+C).
3
+ *
4
+ * Enforces three constraints specific to tasks of type "epic":
5
+ *
6
+ * 1. **Creation requirements** (strict mode only):
7
+ * - Minimum 5 acceptance criteria (vs. 3 for regular tasks).
8
+ * - Completion criteria must be defined (non-empty description field).
9
+ * - Initial pipeline stage defaults to "research" (inherited from
10
+ * pipeline-stage.ts, not re-asserted here).
11
+ *
12
+ * 2. **Child stage ceiling**: A child task's pipeline stage cannot advance
13
+ * past the epic's current pipeline stage. This is checked on child task
14
+ * update (pipelineStage change) and on child task creation.
15
+ *
16
+ * 3. **Epic stage advancement gate**: An epic cannot advance its pipeline
17
+ * stage while it has children whose status is not "done" AND whose
18
+ * pipeline stage equals the epic's current stage. In other words, all
19
+ * in-flight children at the current stage must be completed before the
20
+ * epic may move forward.
21
+ *
22
+ * Enforcement is conditional on `lifecycle.mode`:
23
+ * - "strict" → block (throw CleoError on violation)
24
+ * - "advisory" → warn (return error string but do not throw)
25
+ * - "off" → skip (all checks are no-ops)
26
+ *
27
+ * @task T062
28
+ * @epic T056
29
+ */
30
+
31
+ import type { DataAccessor, Task } from '@cleocode/contracts';
32
+ import { ExitCode } from '@cleocode/contracts';
33
+ import { loadConfig } from '../config.js';
34
+ import { CleoError } from '../errors.js';
35
+ import { getPipelineStageOrder, isValidPipelineStage } from './pipeline-stage.js';
36
+
37
+ // =============================================================================
38
+ // CONSTANTS
39
+ // =============================================================================
40
+
41
+ /** Minimum acceptance criteria count required for epic creation in strict mode. */
42
+ export const EPIC_MIN_AC = 5;
43
+
44
+ /** Minimum acceptance criteria count for regular tasks. */
45
+ export const TASK_MIN_AC = 3;
46
+
47
+ // =============================================================================
48
+ // TYPES
49
+ // =============================================================================
50
+
51
+ /** The resolved enforcement mode (from lifecycle.mode config key). */
52
+ export type LifecycleMode = 'strict' | 'advisory' | 'off';
53
+
54
+ /** Result of an enforcement check. `warning` is populated in advisory mode. */
55
+ export interface EpicEnforcementResult {
56
+ /** True unless a hard block was raised. */
57
+ valid: boolean;
58
+ /** Advisory message (non-blocking) or error message (blocked). */
59
+ warning?: string;
60
+ }
61
+
62
+ // =============================================================================
63
+ // HELPERS
64
+ // =============================================================================
65
+
66
+ /**
67
+ * Read `lifecycle.mode` from config. Falls back to "strict" when unset
68
+ * (matches the DEFAULTS in config.ts).
69
+ *
70
+ * @remarks
71
+ * In VITEST environments, returns "off" to avoid blocking tests.
72
+ *
73
+ * @param cwd - Working directory for config resolution
74
+ * @returns The resolved lifecycle mode
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const mode = await getLifecycleMode();
79
+ * // => 'strict' | 'advisory' | 'off'
80
+ * ```
81
+ *
82
+ * @task T062
83
+ */
84
+ export async function getLifecycleMode(cwd?: string): Promise<LifecycleMode> {
85
+ if (process.env.VITEST) return 'off';
86
+ const config = await loadConfig(cwd);
87
+ return config.lifecycle?.mode ?? 'strict';
88
+ }
89
+
90
+ // =============================================================================
91
+ // 1. EPIC CREATION REQUIREMENTS
92
+ // =============================================================================
93
+
94
+ /**
95
+ * Validate that a new epic satisfies creation requirements.
96
+ *
97
+ * In **strict** mode:
98
+ * - At least {@link EPIC_MIN_AC} acceptance criteria must be provided.
99
+ * - `description` must be non-empty (treated as completion criteria).
100
+ *
101
+ * In **advisory** mode the same checks are run but violations do not block —
102
+ * they are returned as `warning` text for the caller to surface.
103
+ *
104
+ * In **off** mode this function is a no-op.
105
+ *
106
+ * @remarks
107
+ * The description field serves as a proxy for completion criteria — epics
108
+ * without a description have no definition of "done" and should be blocked.
109
+ *
110
+ * @param options - Epic creation parameters
111
+ * @param options.acceptance - Acceptance criteria array supplied by the caller.
112
+ * @param options.description - Task description (used as completion criteria proxy).
113
+ * @param cwd - Working directory for config resolution.
114
+ * @returns EpicEnforcementResult — `valid: false` only in strict mode on error.
115
+ * @throws CleoError(VALIDATION_ERROR) in strict mode when constraints are violated.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * await validateEpicCreation({ acceptance: ['AC1','AC2','AC3','AC4','AC5'] });
120
+ * // => { valid: true }
121
+ * ```
122
+ *
123
+ * @task T062
124
+ */
125
+ export async function validateEpicCreation(
126
+ options: {
127
+ acceptance?: string[];
128
+ description?: string;
129
+ },
130
+ cwd?: string,
131
+ ): Promise<EpicEnforcementResult> {
132
+ const mode = await getLifecycleMode(cwd);
133
+ if (mode === 'off') return { valid: true };
134
+
135
+ const ac = options.acceptance ?? [];
136
+ const desc = (options.description ?? '').trim();
137
+
138
+ const violations: string[] = [];
139
+
140
+ if (ac.length < EPIC_MIN_AC) {
141
+ violations.push(
142
+ `Epic requires at least ${EPIC_MIN_AC} acceptance criteria (${ac.length} provided). Regular tasks need ${TASK_MIN_AC}.`,
143
+ );
144
+ }
145
+
146
+ if (!desc) {
147
+ violations.push('Epic must have a non-empty description (used as completion criteria).');
148
+ }
149
+
150
+ if (violations.length === 0) return { valid: true };
151
+
152
+ const message = violations.join(' | ');
153
+ const fix = `Add --acceptance "..." flags (need ${EPIC_MIN_AC}) and a --description "completion criteria"`;
154
+
155
+ if (mode === 'strict') {
156
+ throw new CleoError(ExitCode.VALIDATION_ERROR, message, { fix });
157
+ }
158
+
159
+ // advisory: warn but allow
160
+ return { valid: true, warning: message };
161
+ }
162
+
163
+ // =============================================================================
164
+ // 2. CHILD STAGE CEILING
165
+ // =============================================================================
166
+
167
+ /**
168
+ * Validate that a child task's pipeline stage does not exceed its epic's stage.
169
+ *
170
+ * Call this when:
171
+ * - A child task is **created** under an epic parent.
172
+ * - A child task's `pipelineStage` is **updated** and it has an epic ancestor.
173
+ *
174
+ * The check walks the task's ancestor chain to find the nearest epic ancestor.
175
+ * If none exists, the check is skipped.
176
+ *
177
+ * @remarks
178
+ * Skips the check if the epic has no pipeline stage set, or if the child
179
+ * stage is not a recognised value (those are handled by separate validation).
180
+ *
181
+ * @param options - Ceiling check parameters
182
+ * @param options.childStage - The proposed pipeline stage for the child.
183
+ * @param options.epicId - ID of the epic ancestor to check against.
184
+ * @param accessor - DataAccessor for task lookups.
185
+ * @param cwd - Working directory for config resolution.
186
+ * @returns EpicEnforcementResult
187
+ * @throws CleoError(VALIDATION_ERROR) in strict mode when the child stage exceeds the epic.
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * await validateChildStageCeiling(
192
+ * { childStage: 'testing', epicId: 'T001' },
193
+ * accessor,
194
+ * );
195
+ * ```
196
+ *
197
+ * @task T062
198
+ */
199
+ export async function validateChildStageCeiling(
200
+ options: {
201
+ childStage: string;
202
+ epicId: string;
203
+ },
204
+ accessor: DataAccessor,
205
+ cwd?: string,
206
+ ): Promise<EpicEnforcementResult> {
207
+ const mode = await getLifecycleMode(cwd);
208
+ if (mode === 'off') return { valid: true };
209
+
210
+ const epic = await accessor.loadSingleTask(options.epicId);
211
+ if (!epic || epic.type !== 'epic') return { valid: true };
212
+
213
+ const epicStage = epic.pipelineStage;
214
+ if (!epicStage || !isValidPipelineStage(epicStage)) return { valid: true };
215
+
216
+ const childStage = options.childStage;
217
+ if (!isValidPipelineStage(childStage)) return { valid: true }; // stage validation handled elsewhere
218
+
219
+ const epicOrder = getPipelineStageOrder(epicStage);
220
+ const childOrder = getPipelineStageOrder(childStage);
221
+
222
+ if (childOrder <= epicOrder) return { valid: true };
223
+
224
+ const message =
225
+ `Child task cannot be at pipeline stage "${childStage}" (order ${childOrder}) ` +
226
+ `because its parent epic ${options.epicId} is only at stage "${epicStage}" (order ${epicOrder}). ` +
227
+ `Children cannot exceed their epic's current stage.`;
228
+ const fix = `Use a stage at or before "${epicStage}". Advance the epic first if needed.`;
229
+
230
+ if (mode === 'strict') {
231
+ throw new CleoError(ExitCode.VALIDATION_ERROR, message, { fix });
232
+ }
233
+
234
+ return { valid: true, warning: message };
235
+ }
236
+
237
+ /**
238
+ * Find the nearest epic ancestor for a given task.
239
+ *
240
+ * Walks the ancestor chain (root-first) and returns the first task whose
241
+ * type is "epic", or null if no epic ancestor exists.
242
+ *
243
+ * @remarks
244
+ * Scans from closest ancestor to root so the *nearest* epic is returned,
245
+ * not the highest-level one.
246
+ *
247
+ * @param taskId - ID of the task whose ancestors to inspect.
248
+ * @param accessor - DataAccessor for the ancestor chain query.
249
+ * @returns The nearest epic ancestor, or null.
250
+ *
251
+ * @example
252
+ * ```ts
253
+ * const epic = await findEpicAncestor('T042', accessor);
254
+ * if (epic) console.log(epic.id); // e.g. 'T029'
255
+ * ```
256
+ *
257
+ * @task T062
258
+ */
259
+ export async function findEpicAncestor(
260
+ taskId: string,
261
+ accessor: DataAccessor,
262
+ ): Promise<Task | null> {
263
+ const ancestors = await accessor.getAncestorChain(taskId);
264
+ // ancestors is root-first; the last entry is the immediate parent
265
+ // We want the nearest epic, so scan from the end (closest ancestor first)
266
+ for (let i = ancestors.length - 1; i >= 0; i--) {
267
+ const ancestor = ancestors[i];
268
+ if (ancestor && ancestor.type === 'epic') return ancestor;
269
+ }
270
+ return null;
271
+ }
272
+
273
+ // =============================================================================
274
+ // 3. EPIC STAGE ADVANCEMENT GATE
275
+ // =============================================================================
276
+
277
+ /**
278
+ * Validate that an epic can advance its pipeline stage.
279
+ *
280
+ * An epic is **blocked** from advancing to a later stage when it has at least
281
+ * one child that:
282
+ * - Has a pipeline stage **equal to the epic's current stage**, AND
283
+ * - Has a status that is **not** "done" (i.e., is still in-flight).
284
+ *
285
+ * Rationale: the epic stage represents the stage the team is actively working
286
+ * in. Moving the epic forward while children are unfinished at the current
287
+ * stage violates the pipeline discipline.
288
+ *
289
+ * @remarks
290
+ * Only fires on genuine forward advancement — same-stage updates and
291
+ * backward moves are handled by {@link validatePipelineTransition}.
292
+ * Children with status "done", "cancelled", or "archived" are excluded
293
+ * from the blocker check.
294
+ *
295
+ * @param options - Advancement check parameters
296
+ * @param options.epicId - ID of the epic being advanced.
297
+ * @param options.currentStage - Epic's current pipeline stage (before the update).
298
+ * @param options.newStage - Proposed new pipeline stage.
299
+ * @param accessor - DataAccessor for children lookup.
300
+ * @param cwd - Working directory for config resolution.
301
+ * @returns EpicEnforcementResult
302
+ * @throws CleoError(VALIDATION_ERROR) in strict mode when incomplete children exist.
303
+ *
304
+ * @example
305
+ * ```ts
306
+ * await validateEpicStageAdvancement(
307
+ * { epicId: 'T029', currentStage: 'research', newStage: 'consensus' },
308
+ * accessor,
309
+ * );
310
+ * ```
311
+ *
312
+ * @task T062
313
+ */
314
+ export async function validateEpicStageAdvancement(
315
+ options: {
316
+ epicId: string;
317
+ currentStage: string;
318
+ newStage: string;
319
+ },
320
+ accessor: DataAccessor,
321
+ cwd?: string,
322
+ ): Promise<EpicEnforcementResult> {
323
+ const mode = await getLifecycleMode(cwd);
324
+ if (mode === 'off') return { valid: true };
325
+
326
+ const { epicId, currentStage, newStage } = options;
327
+
328
+ // Only enforce if actually advancing (not a same-stage no-op)
329
+ if (!isValidPipelineStage(currentStage) || !isValidPipelineStage(newStage)) {
330
+ return { valid: true };
331
+ }
332
+
333
+ const currentOrder = getPipelineStageOrder(currentStage);
334
+ const newOrder = getPipelineStageOrder(newStage);
335
+
336
+ if (newOrder <= currentOrder) {
337
+ // Same stage or backward — backward is caught by validatePipelineTransition
338
+ return { valid: true };
339
+ }
340
+
341
+ // Find all children whose stage equals the current epic stage and are not done
342
+ const children = await accessor.getChildren(epicId);
343
+ const blockers = children.filter((child) => {
344
+ if (child.status === 'done' || child.status === 'cancelled' || child.status === 'archived') {
345
+ return false;
346
+ }
347
+ return child.pipelineStage === currentStage;
348
+ });
349
+
350
+ if (blockers.length === 0) return { valid: true };
351
+
352
+ const blockerIds = blockers.map((t) => t.id).join(', ');
353
+ const message =
354
+ `Epic ${epicId} cannot advance from "${currentStage}" to "${newStage}" — ` +
355
+ `${blockers.length} child task(s) are still in-flight at stage "${currentStage}": ${blockerIds}. ` +
356
+ `Complete all children at the current stage before advancing the epic.`;
357
+ const fix = `Complete tasks ${blockerIds} first, then advance the epic stage.`;
358
+
359
+ if (mode === 'strict') {
360
+ throw new CleoError(ExitCode.VALIDATION_ERROR, message, { fix });
361
+ }
362
+
363
+ return { valid: true, warning: message };
364
+ }
@@ -8,6 +8,7 @@ export {
8
8
  type AddTaskOptions,
9
9
  type AddTaskResult,
10
10
  addTask,
11
+ buildDefaultVerification,
11
12
  findRecentDuplicate,
12
13
  getNextPosition,
13
14
  getTaskDepth,
@@ -0,0 +1,293 @@
1
+ /**
2
+ * Pipeline stage binding for tasks (RCASD-IVTR+C).
3
+ *
4
+ * Implements auto-assignment of pipeline stages on task creation and
5
+ * forward-only stage transition validation on task update.
6
+ *
7
+ * Stages (in order):
8
+ * 1. research
9
+ * 2. consensus
10
+ * 3. architecture_decision
11
+ * 4. specification
12
+ * 5. decomposition
13
+ * 6. implementation
14
+ * 7. validation
15
+ * 8. testing
16
+ * 9. release
17
+ * 10. contribution (cross-cutting, treated as terminal)
18
+ *
19
+ * @task T060
20
+ * @epic T056
21
+ */
22
+
23
+ import type { TaskType } from '@cleocode/contracts';
24
+ import { ExitCode } from '@cleocode/contracts';
25
+ import { CleoError } from '../errors.js';
26
+
27
+ /**
28
+ * Minimal parent task shape needed for pipeline stage resolution.
29
+ * @task T060
30
+ */
31
+ export interface ResolvedParent {
32
+ pipelineStage?: string | null;
33
+ type?: TaskType | null;
34
+ }
35
+
36
+ // =============================================================================
37
+ // CONSTANTS
38
+ // =============================================================================
39
+
40
+ /**
41
+ * Ordered pipeline stages (RCASD-IVTR+C).
42
+ * This matches lifecycle/stages.ts PIPELINE_STAGES but is kept local to avoid
43
+ * a circular dependency — tasks/ must not import from lifecycle/.
44
+ *
45
+ * @task T060
46
+ */
47
+ export const TASK_PIPELINE_STAGES = [
48
+ 'research',
49
+ 'consensus',
50
+ 'architecture_decision',
51
+ 'specification',
52
+ 'decomposition',
53
+ 'implementation',
54
+ 'validation',
55
+ 'testing',
56
+ 'release',
57
+ 'contribution',
58
+ ] as const;
59
+
60
+ /** Union type of all valid pipeline stage names. */
61
+ export type TaskPipelineStage = (typeof TASK_PIPELINE_STAGES)[number];
62
+
63
+ /** Order map for fast index lookups (1-based). */
64
+ const STAGE_ORDER: Record<TaskPipelineStage, number> = {
65
+ research: 1,
66
+ consensus: 2,
67
+ architecture_decision: 3,
68
+ specification: 4,
69
+ decomposition: 5,
70
+ implementation: 6,
71
+ validation: 7,
72
+ testing: 8,
73
+ release: 9,
74
+ contribution: 10,
75
+ };
76
+
77
+ // =============================================================================
78
+ // VALIDATION
79
+ // =============================================================================
80
+
81
+ /**
82
+ * Check whether a string is a valid pipeline stage name.
83
+ *
84
+ * @remarks
85
+ * Uses a type-narrowing signature so callers can safely use the value
86
+ * as {@link TaskPipelineStage} after a truthy check.
87
+ *
88
+ * @param stage - Raw string to test
89
+ * @returns True if it is a valid stage name
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * isValidPipelineStage('research'); // => true
94
+ * isValidPipelineStage('not_a_stage'); // => false
95
+ * ```
96
+ *
97
+ * @task T060
98
+ */
99
+ export function isValidPipelineStage(stage: string): stage is TaskPipelineStage {
100
+ return TASK_PIPELINE_STAGES.includes(stage as TaskPipelineStage);
101
+ }
102
+
103
+ /**
104
+ * Validate a pipeline stage name and throw a CleoError on failure.
105
+ *
106
+ * @remarks
107
+ * Uses an assertion signature — after a successful call the compiler
108
+ * narrows `stage` to {@link TaskPipelineStage}.
109
+ *
110
+ * @param stage - Stage name to validate
111
+ * @returns void (assertion function — narrows type on success)
112
+ * @throws CleoError(VALIDATION_ERROR) if invalid
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * validatePipelineStage('implementation'); // passes
117
+ * validatePipelineStage('invalid'); // throws CleoError
118
+ * ```
119
+ *
120
+ * @task T060
121
+ */
122
+ export function validatePipelineStage(stage: string): asserts stage is TaskPipelineStage {
123
+ if (!isValidPipelineStage(stage)) {
124
+ throw new CleoError(
125
+ ExitCode.VALIDATION_ERROR,
126
+ `Invalid pipeline stage: "${stage}". Valid stages: ${TASK_PIPELINE_STAGES.join(', ')}`,
127
+ { fix: `Use one of: ${TASK_PIPELINE_STAGES.join(', ')}` },
128
+ );
129
+ }
130
+ }
131
+
132
+ // =============================================================================
133
+ // AUTO-ASSIGNMENT
134
+ // =============================================================================
135
+
136
+ /**
137
+ * Determine the default pipeline stage for a new task.
138
+ *
139
+ * Rules (in priority order):
140
+ * 1. If an explicit stage is provided and valid, use it.
141
+ * 2. If the task has a parent, inherit the parent's pipelineStage.
142
+ * 3. If the task type is 'epic', default to 'research'.
143
+ * 4. Otherwise default to 'implementation'.
144
+ *
145
+ * @remarks
146
+ * Priority order ensures explicit caller intent wins, then parent
147
+ * inheritance, then type-based defaults. This avoids surprising
148
+ * overrides when parent stages differ from the default.
149
+ *
150
+ * @param options - Resolution inputs
151
+ * @param options.explicitStage - Stage explicitly provided by the caller
152
+ * @param options.taskType - Type of the task being created
153
+ * @param options.parentTask - Parent task (if any), for inheritance
154
+ * @returns The resolved pipeline stage name
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * resolveDefaultPipelineStage({ taskType: 'epic' });
159
+ * // => 'research'
160
+ *
161
+ * resolveDefaultPipelineStage({ taskType: 'task' });
162
+ * // => 'implementation'
163
+ * ```
164
+ *
165
+ * @task T060
166
+ */
167
+ export function resolveDefaultPipelineStage(options: {
168
+ explicitStage?: string | null;
169
+ taskType?: TaskType | null;
170
+ parentTask?: ResolvedParent | null;
171
+ }): TaskPipelineStage {
172
+ const { explicitStage, taskType, parentTask } = options;
173
+
174
+ // 1. Caller-supplied explicit stage (validated upstream)
175
+ if (explicitStage && isValidPipelineStage(explicitStage)) {
176
+ return explicitStage;
177
+ }
178
+
179
+ // 2. Inherit from parent
180
+ if (parentTask?.pipelineStage && isValidPipelineStage(parentTask.pipelineStage)) {
181
+ return parentTask.pipelineStage;
182
+ }
183
+
184
+ // 3. Epic → research
185
+ if (taskType === 'epic') {
186
+ return 'research';
187
+ }
188
+
189
+ // 4. Default
190
+ return 'implementation';
191
+ }
192
+
193
+ // =============================================================================
194
+ // TRANSITION VALIDATION
195
+ // =============================================================================
196
+
197
+ /**
198
+ * Get the numeric order of a pipeline stage (1-based).
199
+ *
200
+ * @remarks
201
+ * Returns -1 for unrecognised stage names so callers can distinguish
202
+ * "unknown" from a valid low-order stage.
203
+ *
204
+ * @param stage - Stage name (must be valid)
205
+ * @returns Numeric order (1–10), or -1 if not found
206
+ *
207
+ * @example
208
+ * ```ts
209
+ * getPipelineStageOrder('research'); // => 1
210
+ * getPipelineStageOrder('implementation'); // => 6
211
+ * getPipelineStageOrder('unknown'); // => -1
212
+ * ```
213
+ *
214
+ * @task T060
215
+ */
216
+ export function getPipelineStageOrder(stage: string): number {
217
+ return isValidPipelineStage(stage) ? STAGE_ORDER[stage] : -1;
218
+ }
219
+
220
+ /**
221
+ * Check whether transitioning from `currentStage` to `newStage` is forward-only.
222
+ *
223
+ * "Forward" means the new stage's order is greater than or equal to the current
224
+ * stage's order (same stage is a no-op and is considered valid).
225
+ *
226
+ * @remarks
227
+ * Unknown stages are treated as valid to avoid blocking tasks with
228
+ * legacy or custom stage names that predate the standard set.
229
+ *
230
+ * @param currentStage - The task's current pipeline stage
231
+ * @param newStage - The requested new pipeline stage
232
+ * @returns True if the transition is allowed (forward or same)
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * isPipelineTransitionForward('research', 'implementation'); // => true
237
+ * isPipelineTransitionForward('testing', 'research'); // => false
238
+ * ```
239
+ *
240
+ * @task T060
241
+ */
242
+ export function isPipelineTransitionForward(currentStage: string, newStage: string): boolean {
243
+ const currentOrder = getPipelineStageOrder(currentStage);
244
+ const newOrder = getPipelineStageOrder(newStage);
245
+ if (currentOrder === -1 || newOrder === -1) return true; // unknown stages: allow
246
+ return newOrder >= currentOrder;
247
+ }
248
+
249
+ /**
250
+ * Validate a pipeline stage transition and throw if it would move backward.
251
+ *
252
+ * @remarks
253
+ * Validates the new stage name first via {@link validatePipelineStage},
254
+ * then checks directionality. A null/undefined current stage accepts any
255
+ * valid new stage (first assignment).
256
+ *
257
+ * @param currentStage - The task's current pipeline stage (may be null/undefined)
258
+ * @param newStage - The new stage being requested
259
+ * @throws CleoError(VALIDATION_ERROR) if the transition is backward
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * validatePipelineTransition(null, 'research'); // passes (first assignment)
264
+ * validatePipelineTransition('research', 'implementation'); // passes (forward)
265
+ * validatePipelineTransition('testing', 'research'); // throws (backward)
266
+ * ```
267
+ *
268
+ * @task T060
269
+ */
270
+ export function validatePipelineTransition(
271
+ currentStage: string | null | undefined,
272
+ newStage: string,
273
+ ): void {
274
+ // Validate the new stage name first
275
+ validatePipelineStage(newStage);
276
+
277
+ if (!currentStage) {
278
+ // No current stage — any valid stage is allowed
279
+ return;
280
+ }
281
+
282
+ if (!isPipelineTransitionForward(currentStage, newStage)) {
283
+ const currentOrder = getPipelineStageOrder(currentStage);
284
+ const newOrder = getPipelineStageOrder(newStage);
285
+ throw new CleoError(
286
+ ExitCode.VALIDATION_ERROR,
287
+ `Pipeline stage transition rejected: cannot move backward from "${currentStage}" (order ${currentOrder}) to "${newStage}" (order ${newOrder}). Tasks can only move forward through pipeline stages.`,
288
+ {
289
+ fix: `Specify a stage at or after "${currentStage}". Valid forward stages: ${TASK_PIPELINE_STAGES.filter((s) => STAGE_ORDER[s] >= currentOrder).join(', ')}`,
290
+ },
291
+ );
292
+ }
293
+ }