@cleocode/core 2026.3.58 → 2026.3.60

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 (153) 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-registry.js +288 -0
  4. package/dist/agents/agent-registry.js.map +1 -0
  5. package/dist/agents/agent-schema.js +5 -0
  6. package/dist/agents/agent-schema.js.map +1 -1
  7. package/dist/agents/execution-learning.js +474 -0
  8. package/dist/agents/execution-learning.js.map +1 -0
  9. package/dist/agents/health-monitor.d.ts +161 -0
  10. package/dist/agents/health-monitor.d.ts.map +1 -0
  11. package/dist/agents/health-monitor.js +217 -0
  12. package/dist/agents/health-monitor.js.map +1 -0
  13. package/dist/agents/index.d.ts +3 -1
  14. package/dist/agents/index.d.ts.map +1 -1
  15. package/dist/agents/index.js +9 -1
  16. package/dist/agents/index.js.map +1 -1
  17. package/dist/agents/retry.d.ts +57 -4
  18. package/dist/agents/retry.d.ts.map +1 -1
  19. package/dist/agents/retry.js +57 -4
  20. package/dist/agents/retry.js.map +1 -1
  21. package/dist/backfill/index.d.ts +27 -0
  22. package/dist/backfill/index.d.ts.map +1 -1
  23. package/dist/backfill/index.js +229 -0
  24. package/dist/backfill/index.js.map +1 -0
  25. package/dist/bootstrap.d.ts +2 -1
  26. package/dist/bootstrap.d.ts.map +1 -1
  27. package/dist/bootstrap.js +135 -28
  28. package/dist/bootstrap.js.map +1 -1
  29. package/dist/cleo.d.ts +40 -0
  30. package/dist/cleo.d.ts.map +1 -1
  31. package/dist/config.js +83 -0
  32. package/dist/config.js.map +1 -1
  33. package/dist/index.d.ts +1 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +1036 -536
  36. package/dist/index.js.map +4 -4
  37. package/dist/intelligence/adaptive-validation.js +497 -0
  38. package/dist/intelligence/adaptive-validation.js.map +1 -0
  39. package/dist/intelligence/impact.d.ts +34 -1
  40. package/dist/intelligence/impact.d.ts.map +1 -1
  41. package/dist/intelligence/impact.js +176 -0
  42. package/dist/intelligence/impact.js.map +1 -1
  43. package/dist/intelligence/index.d.ts +2 -2
  44. package/dist/intelligence/index.d.ts.map +1 -1
  45. package/dist/intelligence/index.js +6 -1
  46. package/dist/intelligence/index.js.map +1 -1
  47. package/dist/intelligence/types.d.ts +60 -0
  48. package/dist/intelligence/types.d.ts.map +1 -1
  49. package/dist/internal.d.ts +5 -4
  50. package/dist/internal.d.ts.map +1 -1
  51. package/dist/internal.js +11 -2
  52. package/dist/internal.js.map +1 -1
  53. package/dist/lib/index.d.ts +10 -0
  54. package/dist/lib/index.d.ts.map +1 -0
  55. package/dist/lib/index.js +10 -0
  56. package/dist/lib/index.js.map +1 -0
  57. package/dist/lib/retry.d.ts +128 -0
  58. package/dist/lib/retry.d.ts.map +1 -0
  59. package/dist/lib/retry.js +152 -0
  60. package/dist/lib/retry.js.map +1 -0
  61. package/dist/nexus/sharing/index.d.ts +48 -2
  62. package/dist/nexus/sharing/index.d.ts.map +1 -1
  63. package/dist/nexus/sharing/index.js +110 -1
  64. package/dist/nexus/sharing/index.js.map +1 -1
  65. package/dist/scaffold.d.ts.map +1 -1
  66. package/dist/scaffold.js +22 -2
  67. package/dist/scaffold.js.map +1 -1
  68. package/dist/sessions/session-enforcement.js +4 -0
  69. package/dist/sessions/session-enforcement.js.map +1 -1
  70. package/dist/stats/index.js +2 -0
  71. package/dist/stats/index.js.map +1 -1
  72. package/dist/stats/workflow-telemetry.d.ts +15 -0
  73. package/dist/stats/workflow-telemetry.d.ts.map +1 -1
  74. package/dist/stats/workflow-telemetry.js +400 -0
  75. package/dist/stats/workflow-telemetry.js.map +1 -0
  76. package/dist/store/brain-schema.js +4 -1
  77. package/dist/store/brain-schema.js.map +1 -1
  78. package/dist/store/converters.js +2 -0
  79. package/dist/store/converters.js.map +1 -1
  80. package/dist/store/cross-db-cleanup.d.ts +35 -0
  81. package/dist/store/cross-db-cleanup.d.ts.map +1 -1
  82. package/dist/store/cross-db-cleanup.js +169 -0
  83. package/dist/store/cross-db-cleanup.js.map +1 -0
  84. package/dist/store/db-helpers.js +2 -0
  85. package/dist/store/db-helpers.js.map +1 -1
  86. package/dist/store/migration-sqlite.js +5 -0
  87. package/dist/store/migration-sqlite.js.map +1 -1
  88. package/dist/store/sqlite-data-accessor.js +20 -28
  89. package/dist/store/sqlite-data-accessor.js.map +1 -1
  90. package/dist/store/sqlite.js +13 -2
  91. package/dist/store/sqlite.js.map +1 -1
  92. package/dist/store/task-store.js +4 -0
  93. package/dist/store/task-store.js.map +1 -1
  94. package/dist/store/tasks-schema.js +50 -20
  95. package/dist/store/tasks-schema.js.map +1 -1
  96. package/dist/tasks/add.js +87 -3
  97. package/dist/tasks/add.js.map +1 -1
  98. package/dist/tasks/complete.d.ts.map +1 -1
  99. package/dist/tasks/complete.js +15 -4
  100. package/dist/tasks/complete.js.map +1 -1
  101. package/dist/tasks/enforcement.d.ts.map +1 -1
  102. package/dist/tasks/enforcement.js +8 -1
  103. package/dist/tasks/enforcement.js.map +1 -1
  104. package/dist/tasks/epic-enforcement.d.ts +61 -0
  105. package/dist/tasks/epic-enforcement.d.ts.map +1 -1
  106. package/dist/tasks/epic-enforcement.js +294 -0
  107. package/dist/tasks/epic-enforcement.js.map +1 -0
  108. package/dist/tasks/index.js +1 -1
  109. package/dist/tasks/index.js.map +1 -1
  110. package/dist/tasks/pipeline-stage.d.ts +70 -1
  111. package/dist/tasks/pipeline-stage.d.ts.map +1 -1
  112. package/dist/tasks/pipeline-stage.js +248 -0
  113. package/dist/tasks/pipeline-stage.js.map +1 -0
  114. package/dist/tasks/update.js +28 -0
  115. package/dist/tasks/update.js.map +1 -1
  116. package/package.json +5 -5
  117. package/schemas/config.schema.json +37 -1547
  118. package/src/__tests__/sharing.test.ts +24 -0
  119. package/src/agents/__tests__/agent-registry.test.ts +351 -0
  120. package/src/agents/__tests__/health-monitor.test.ts +332 -0
  121. package/src/agents/agent-registry.ts +394 -0
  122. package/src/agents/health-monitor.ts +279 -0
  123. package/src/agents/index.ts +24 -1
  124. package/src/agents/retry.ts +57 -4
  125. package/src/backfill/index.ts +27 -0
  126. package/src/bootstrap.ts +171 -30
  127. package/src/cleo.ts +103 -2
  128. package/src/config.ts +3 -3
  129. package/src/index.ts +1 -0
  130. package/src/intelligence/__tests__/impact.test.ts +165 -1
  131. package/src/intelligence/impact.ts +203 -0
  132. package/src/intelligence/index.ts +3 -0
  133. package/src/intelligence/types.ts +76 -0
  134. package/src/internal.ts +20 -0
  135. package/src/lib/__tests__/retry.test.ts +321 -0
  136. package/src/lib/index.ts +16 -0
  137. package/src/lib/retry.ts +224 -0
  138. package/src/nexus/sharing/index.ts +142 -2
  139. package/src/scaffold.ts +24 -2
  140. package/src/stats/workflow-telemetry.ts +15 -0
  141. package/src/store/__tests__/session-store.test.ts +43 -7
  142. package/src/store/__tests__/task-store.test.ts +1 -1
  143. package/src/store/__tests__/test-db-helper.ts +7 -3
  144. package/src/store/cross-db-cleanup.ts +35 -0
  145. package/src/tasks/__tests__/epic-enforcement.test.ts +9 -4
  146. package/src/tasks/__tests__/minimal-test.test.ts +2 -2
  147. package/src/tasks/__tests__/update.test.ts +25 -25
  148. package/src/tasks/complete.ts +11 -6
  149. package/src/tasks/enforcement.ts +6 -3
  150. package/src/tasks/epic-enforcement.ts +61 -0
  151. package/src/tasks/pipeline-stage.ts +70 -1
  152. package/templates/config.template.json +5 -116
  153. package/templates/global-config.template.json +2 -44
@@ -11,6 +11,17 @@
11
11
  * @module agents
12
12
  */
13
13
 
14
+ // Load-balancing registry: task-count capacity, specializations, performance recording
15
+ export {
16
+ type AgentCapacity,
17
+ type AgentPerformanceMetrics,
18
+ getAgentCapacity,
19
+ getAgentSpecializations,
20
+ getAgentsByCapacity,
21
+ MAX_TASKS_PER_AGENT,
22
+ recordAgentPerformance,
23
+ updateAgentSpecializations,
24
+ } from './agent-registry.js';
14
25
  // Schema & types
15
26
  export {
16
27
  AGENT_INSTANCE_STATUSES,
@@ -47,10 +58,22 @@ export {
47
58
  recordFailurePattern,
48
59
  storeHealingStrategy,
49
60
  } from './execution-learning.js';
61
+ // Health monitoring (T039)
62
+ export {
63
+ type AgentHealthStatus,
64
+ checkAgentHealth,
65
+ detectCrashedAgents,
66
+ detectStaleAgents,
67
+ HEARTBEAT_INTERVAL_MS,
68
+ recordHeartbeat,
69
+ STALE_THRESHOLD_MS,
70
+ } from './health-monitor.js';
50
71
  // Registry (CRUD, heartbeat, health, errors)
72
+ // Note: registry.checkAgentHealth (thresholdMs, cwd) -> AgentInstanceRow[] is exported
73
+ // as findStaleAgentRows to avoid conflict with health-monitor.checkAgentHealth (T039).
51
74
  export {
52
75
  type AgentHealthReport,
53
- checkAgentHealth,
76
+ checkAgentHealth as findStaleAgentRows,
54
77
  classifyError,
55
78
  deregisterAgent,
56
79
  generateAgentId,
@@ -47,6 +47,17 @@ export const DEFAULT_RETRY_POLICY: Readonly<RetryPolicy> = Object.freeze({
47
47
 
48
48
  /**
49
49
  * Create a retry policy by merging overrides with the default policy.
50
+ *
51
+ * @remarks
52
+ * Unspecified fields fall back to {@link DEFAULT_RETRY_POLICY}.
53
+ *
54
+ * @param overrides - Partial policy to merge with defaults
55
+ * @returns A complete RetryPolicy
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * const policy = createRetryPolicy({ maxRetries: 5 });
60
+ * ```
50
61
  */
51
62
  export function createRetryPolicy(overrides?: Partial<RetryPolicy>): RetryPolicy {
52
63
  return { ...DEFAULT_RETRY_POLICY, ...overrides };
@@ -55,8 +66,19 @@ export function createRetryPolicy(overrides?: Partial<RetryPolicy>): RetryPolicy
55
66
  /**
56
67
  * Calculate the delay for a given retry attempt using exponential backoff.
57
68
  *
58
- * Formula: min(baseDelay * multiplier^attempt, maxDelay) + jitter
69
+ * @remarks
70
+ * Formula: `min(baseDelay * multiplier^attempt, maxDelay) + jitter`.
59
71
  * Jitter adds 0-25% randomness to prevent thundering herd.
72
+ *
73
+ * @param attempt - Zero-based attempt index
74
+ * @param policy - Retry policy with delay configuration
75
+ * @returns Delay in milliseconds before the next attempt
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * const delay = calculateDelay(1, createRetryPolicy());
80
+ * // => ~2000ms (with jitter)
81
+ * ```
60
82
  */
61
83
  export function calculateDelay(attempt: number, policy: RetryPolicy): number {
62
84
  const exponentialDelay = policy.baseDelayMs * policy.backoffMultiplier ** attempt;
@@ -73,6 +95,20 @@ export function calculateDelay(attempt: number, policy: RetryPolicy): number {
73
95
  /**
74
96
  * Determine whether an error should be retried based on its classification
75
97
  * and the retry policy.
98
+ *
99
+ * @remarks
100
+ * Permanent errors are never retried. Retriable errors are always retried
101
+ * (within attempt limits). Unknown errors defer to `policy.retryOnUnknown`.
102
+ *
103
+ * @param error - The caught error to classify
104
+ * @param attempt - Current attempt number (0-based)
105
+ * @param policy - Retry policy with limits and classification rules
106
+ * @returns True if the error should be retried
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * if (shouldRetry(err, attempt, policy)) { /* retry *\/ }
111
+ * ```
76
112
  */
77
113
  export function shouldRetry(error: unknown, attempt: number, policy: RetryPolicy): boolean {
78
114
  if (attempt >= policy.maxRetries) return false;
@@ -102,13 +138,20 @@ export interface RetryResult<T> {
102
138
  /**
103
139
  * Wrap an async function with retry logic using configurable exponential backoff.
104
140
  *
105
- * The function will be retried according to the policy when retriable errors
106
- * occur. Permanent errors cause immediate failure. Unknown errors respect
107
- * the `retryOnUnknown` policy setting.
141
+ * @remarks
142
+ * Agent-specific variant that integrates with error classification from the
143
+ * agent registry. For a dependency-free generic retry, use `lib/retry.ts`.
108
144
  *
145
+ * @typeParam T - The resolved type of the async function
109
146
  * @param fn - The async function to execute with retries
110
147
  * @param policy - Retry policy (uses DEFAULT_RETRY_POLICY if not provided)
111
148
  * @returns The result of the operation with retry metadata
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * const result = await withRetry(() => fetchAgentTask(agentId));
153
+ * if (!result.success) console.error(result.error);
154
+ * ```
112
155
  */
113
156
  export async function withRetry<T>(
114
157
  fn: () => Promise<T>,
@@ -176,9 +219,19 @@ export interface AgentRecoveryResult {
176
219
  * classified as 'permanent' are abandoned. Agents with retriable errors
177
220
  * are reset to 'starting' for the orchestration layer to re-assign.
178
221
  *
222
+ * @remarks
223
+ * Two-phase process: first detects stale agents via heartbeat threshold,
224
+ * then evaluates each crashed agent's error history for recoverability.
225
+ *
179
226
  * @param thresholdMs - Heartbeat threshold for crash detection (default: 30000)
180
227
  * @param cwd - Working directory
181
228
  * @returns Recovery results for each crashed agent
229
+ *
230
+ * @example
231
+ * ```ts
232
+ * const results = await recoverCrashedAgents(60_000);
233
+ * results.filter(r => r.recovered).forEach(r => console.log(r.agentId));
234
+ * ```
182
235
  */
183
236
  export async function recoverCrashedAgents(
184
237
  thresholdMs: number = 30_000,
@@ -8,6 +8,7 @@
8
8
  * backfillTasks(root, {}) -- apply changes
9
9
  * backfillTasks(root, { rollback: true }) -- revert backfill
10
10
  *
11
+ * @packageDocumentation
11
12
  * @epic T056
12
13
  * @task T066
13
14
  */
@@ -60,6 +61,20 @@ export interface BackfillResult {
60
61
  /**
61
62
  * Generate 3 baseline acceptance criteria from a task description.
62
63
  * Uses simple text analysis — no LLM required.
64
+ *
65
+ * @remarks
66
+ * Extracts action verbs from the title + description to produce contextually
67
+ * relevant criteria. Falls back to generic criteria when no verbs match.
68
+ *
69
+ * @param title - The task title
70
+ * @param description - The task description
71
+ * @returns Array of 3 acceptance criteria strings
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * generateAcFromDescription('Fix login bug', 'Users cannot log in');
76
+ * // => ['The defect is resolved...', 'No breaking changes...', 'Changes verified...']
77
+ * ```
63
78
  */
64
79
  export function generateAcFromDescription(title: string, description: string): string[] {
65
80
  const text = `${title} ${description}`.toLowerCase();
@@ -144,8 +159,20 @@ function isBackfilledTask(task: Task): boolean {
144
159
  /**
145
160
  * Retroactively populate AC and verification metadata for tasks that lack them.
146
161
  *
162
+ * @remarks
163
+ * In dry-run mode, computes changes without writing to the database.
164
+ * Backfilled tasks are tagged with a note so they can be identified and
165
+ * optionally rolled back later.
166
+ *
147
167
  * @param projectRoot - Project root directory (cwd for CLEO operations)
148
168
  * @param options - Backfill options (dryRun, rollback, taskIds)
169
+ * @returns Summary of changes applied (or previewed in dry-run mode)
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * const result = await backfillTasks('/my/project', { dryRun: true });
174
+ * console.log(result.changed); // number of tasks that would be modified
175
+ * ```
149
176
  */
150
177
  export async function backfillTasks(
151
178
  projectRoot: string,
package/src/bootstrap.ts CHANGED
@@ -41,7 +41,8 @@ export interface BootstrapOptions {
41
41
  * Bootstrap the global CLEO directory structure and install templates.
42
42
  *
43
43
  * Creates:
44
- * - ~/.cleo/templates/CLEO-INJECTION.md (from bundled template or injection content)
44
+ * - ~/.local/share/cleo/templates/CLEO-INJECTION.md (XDG primary)
45
+ * - ~/.cleo/templates/CLEO-INJECTION.md (legacy sync)
45
46
  * - ~/.agents/AGENTS.md with CAAMP injection block
46
47
  *
47
48
  * This is idempotent — safe to call multiple times.
@@ -60,7 +61,7 @@ export async function bootstrapGlobalCleo(options?: BootstrapOptions): Promise<B
60
61
  // Best-effort — don't fail bootstrap if cleanup fails
61
62
  }
62
63
 
63
- // Step 1: Ensure global templates
64
+ // Step 1: Ensure global templates (XDG + legacy sync)
64
65
  await ensureGlobalTemplatesBootstrap(ctx, options?.packageRoot);
65
66
 
66
67
  // Step 2: CAAMP injection into ~/.agents/AGENTS.md
@@ -78,11 +79,30 @@ export async function bootstrapGlobalCleo(options?: BootstrapOptions): Promise<B
78
79
  // Step 6: Install provider adapters
79
80
  await installProviderAdapters(ctx, options?.packageRoot);
80
81
 
82
+ // Step 7: Verify injection chain health
83
+ await verifyBootstrapHealth(ctx);
84
+
81
85
  return ctx;
82
86
  }
83
87
 
84
88
  // ── Step 1: Global templates ─────────────────────────────────────────
85
89
 
90
+ /**
91
+ * Write template content to a destination path, creating parent dirs as needed.
92
+ * Returns true if written, false if dry-run.
93
+ */
94
+ async function writeTemplateTo(
95
+ content: string,
96
+ destPath: string,
97
+ isDryRun: boolean,
98
+ ): Promise<boolean> {
99
+ if (isDryRun) return false;
100
+ const { dirname } = await import('node:path');
101
+ await mkdir(dirname(destPath), { recursive: true });
102
+ await writeFile(destPath, content);
103
+ return true;
104
+ }
105
+
86
106
  async function ensureGlobalTemplatesBootstrap(
87
107
  ctx: BootstrapContext,
88
108
  packageRootOverride?: string,
@@ -93,43 +113,85 @@ async function ensureGlobalTemplatesBootstrap(
93
113
  await mkdir(globalTemplatesDir, { recursive: true });
94
114
  }
95
115
 
116
+ // Resolve template content from bundled file or embedded fallback
117
+ let templateContent: string | null = null;
118
+
96
119
  try {
97
120
  const pkgRoot = packageRootOverride ?? getPackageRoot();
98
121
  const templatePath = join(pkgRoot, 'templates', 'CLEO-INJECTION.md');
99
122
  if (existsSync(templatePath)) {
100
- const content = readFileSync(templatePath, 'utf-8');
101
- const destPath = join(globalTemplatesDir, 'CLEO-INJECTION.md');
102
- if (!ctx.isDryRun) {
103
- await writeFile(destPath, content);
104
- }
105
- ctx.created.push(
106
- `~/.cleo/templates/CLEO-INJECTION.md (${ctx.isDryRun ? 'would refresh' : 'refreshed'})`,
107
- );
108
- } else {
109
- // Fallback: try using the injection content generator
110
- try {
111
- const { getInjectionTemplateContent } = await import('./injection.js');
112
- const content = getInjectionTemplateContent();
113
- if (content) {
114
- const destPath = join(globalTemplatesDir, 'CLEO-INJECTION.md');
115
- if (!ctx.isDryRun) {
116
- await writeFile(destPath, content);
117
- }
118
- ctx.created.push(
119
- `~/.cleo/templates/CLEO-INJECTION.md (${ctx.isDryRun ? 'would refresh' : 'refreshed'} from embedded)`,
120
- );
121
- }
122
- } catch {
123
- ctx.warnings.push('Could not refresh CLEO-INJECTION.md template');
124
- }
123
+ templateContent = readFileSync(templatePath, 'utf-8');
125
124
  }
126
125
  } catch {
126
+ // Fall through to embedded fallback
127
+ }
128
+
129
+ if (!templateContent) {
130
+ try {
131
+ const { getInjectionTemplateContent } = await import('./injection.js');
132
+ templateContent = getInjectionTemplateContent() ?? null;
133
+ } catch {
134
+ ctx.warnings.push('Could not refresh CLEO-INJECTION.md template');
135
+ return;
136
+ }
137
+ }
138
+
139
+ if (!templateContent) {
127
140
  ctx.warnings.push('Could not refresh CLEO-INJECTION.md template');
141
+ return;
142
+ }
143
+
144
+ // Write to XDG primary path
145
+ const xdgDest = join(globalTemplatesDir, 'CLEO-INJECTION.md');
146
+ const xdgWritten = await writeTemplateTo(templateContent, xdgDest, ctx.isDryRun);
147
+ ctx.created.push(
148
+ `${getCleoTemplatesTildePath()}/CLEO-INJECTION.md (${xdgWritten ? 'refreshed' : 'would refresh'})`,
149
+ );
150
+
151
+ // Sync to legacy ~/.cleo/templates/ if it exists (backward compat for
152
+ // project AGENTS.md files that still reference the old path)
153
+ const home = homedir();
154
+ const legacyTemplatesDir = join(home, '.cleo', 'templates');
155
+ if (legacyTemplatesDir !== globalTemplatesDir && existsSync(join(home, '.cleo'))) {
156
+ const legacyDest = join(legacyTemplatesDir, 'CLEO-INJECTION.md');
157
+ const legacyWritten = await writeTemplateTo(templateContent, legacyDest, ctx.isDryRun);
158
+ if (legacyWritten) {
159
+ ctx.created.push('~/.cleo/templates/CLEO-INJECTION.md (legacy sync)');
160
+ }
128
161
  }
129
162
  }
130
163
 
131
164
  // ── Step 2: CAAMP injection into ~/.agents/AGENTS.md ─────────────────
132
165
 
166
+ /**
167
+ * Sanitize a CAAMP-managed file by removing orphaned content outside
168
+ * CAAMP blocks. This fixes corruption from failed CAAMP consolidation
169
+ * (e.g. partial old block removal leaving `TION.md` fragments).
170
+ *
171
+ * Strategy: keep ONLY content inside valid CAAMP blocks + any non-CAAMP
172
+ * user content that doesn't look like an orphaned reference fragment.
173
+ */
174
+ function sanitizeCaampFile(content: string): string {
175
+ // Remove any duplicate <!-- CAAMP:END --> markers
176
+ let cleaned = content.replace(/(<!-- CAAMP:END -->)\s*(<!-- CAAMP:END -->)/g, '$1');
177
+
178
+ // Remove orphaned content between CAAMP:END and the next CAAMP:START (or EOF)
179
+ // that looks like a fragment of a CLEO reference (e.g. "TION.md", "INJECTION.md")
180
+ cleaned = cleaned.replace(
181
+ /<!-- CAAMP:END -->\s*[A-Z][A-Za-z-]*\.md\s*(?:<!-- CAAMP:END -->)?/g,
182
+ '<!-- CAAMP:END -->',
183
+ );
184
+
185
+ // Remove any lines that are just orphaned .md filename fragments
186
+ // (leftover from partial CAAMP block removal)
187
+ cleaned = cleaned.replace(/^[A-Z][A-Za-z-]*\.md\s*$/gm, '');
188
+
189
+ // Collapse multiple blank lines
190
+ cleaned = cleaned.replace(/\n{3,}/g, '\n\n');
191
+
192
+ return cleaned.trim() + '\n';
193
+ }
194
+
133
195
  async function injectAgentsHub(ctx: BootstrapContext): Promise<void> {
134
196
  const globalAgentsDir = getAgentsHome();
135
197
  const globalAgentsMd = join(globalAgentsDir, 'AGENTS.md');
@@ -143,21 +205,39 @@ async function injectAgentsHub(ctx: BootstrapContext): Promise<void> {
143
205
  await mkdir(globalAgentsDir, { recursive: true });
144
206
 
145
207
  // Strip legacy CLEO blocks (versioned markers from pre-CAAMP era)
208
+ // AND sanitize CAAMP corruption (orphaned fragments from bad consolidation)
146
209
  if (existsSync(globalAgentsMd)) {
147
210
  const content = await readFile(globalAgentsMd, 'utf8');
211
+
212
+ // Step A: Remove legacy <!-- CLEO:START -->...<!-- CLEO:END --> blocks
148
213
  const stripped = content.replace(
149
214
  /\n?<!-- CLEO:START[^>]*-->[\s\S]*?<!-- CLEO:END -->\n?/g,
150
215
  '',
151
216
  );
152
- if (stripped !== content) {
153
- await writeFile(globalAgentsMd, stripped, 'utf8');
217
+
218
+ // Step B: Sanitize CAAMP corruption (orphaned fragments, duplicate markers)
219
+ const sanitized = sanitizeCaampFile(stripped);
220
+
221
+ if (sanitized !== content) {
222
+ await writeFile(globalAgentsMd, sanitized, 'utf8');
223
+ ctx.created.push('~/.agents/AGENTS.md (sanitized CAAMP corruption)');
154
224
  }
155
225
  }
156
226
 
157
- // CAAMP 1.8.1: inject() is idempotent AND consolidates duplicates
227
+ // CAAMP inject() is idempotent writes the current XDG template reference
158
228
  const templateRef = `@${getCleoTemplatesTildePath()}/CLEO-INJECTION.md`;
159
229
  const action = await inject(globalAgentsMd, templateRef);
160
230
  ctx.created.push(`~/.agents/AGENTS.md (${action})`);
231
+
232
+ // Post-inject validation: verify the file is clean
233
+ const postContent = await readFile(globalAgentsMd, 'utf8');
234
+ const caampBlocks = postContent.match(/<!-- CAAMP:START -->/g);
235
+ const caampEnds = postContent.match(/<!-- CAAMP:END -->/g);
236
+ if (caampBlocks && caampEnds && caampBlocks.length !== caampEnds.length) {
237
+ ctx.warnings.push(
238
+ `~/.agents/AGENTS.md has mismatched CAAMP markers (${caampBlocks.length} START vs ${caampEnds.length} END)`,
239
+ );
240
+ }
161
241
  } else {
162
242
  ctx.created.push('~/.agents/AGENTS.md (would create/update CAAMP block)');
163
243
  }
@@ -330,3 +410,64 @@ async function installProviderAdapters(
330
410
  );
331
411
  }
332
412
  }
413
+
414
+ // ── Step 7: Post-bootstrap health verification ───────────────────────
415
+
416
+ /**
417
+ * Verify the injection chain is intact after bootstrap.
418
+ * Checks:
419
+ * 1. XDG template exists and has a version header
420
+ * 2. Legacy template (if present) matches XDG version
421
+ * 3. ~/.agents/AGENTS.md references the correct template path
422
+ * 4. No orphaned content in AGENTS.md
423
+ */
424
+ async function verifyBootstrapHealth(ctx: BootstrapContext): Promise<void> {
425
+ if (ctx.isDryRun) return;
426
+
427
+ try {
428
+ const xdgTemplatePath = join(getCleoTemplatesDir(), 'CLEO-INJECTION.md');
429
+ const agentsMd = join(getAgentsHome(), 'AGENTS.md');
430
+
431
+ // Check 1: XDG template exists
432
+ if (!existsSync(xdgTemplatePath)) {
433
+ ctx.warnings.push('Health: XDG template missing after bootstrap');
434
+ return;
435
+ }
436
+
437
+ const xdgContent = await readFile(xdgTemplatePath, 'utf8');
438
+ const xdgVersion = xdgContent.match(/^Version:\s*(.+)$/m)?.[1]?.trim();
439
+
440
+ // Check 2: Legacy template version sync
441
+ const home = homedir();
442
+ const legacyTemplatePath = join(home, '.cleo', 'templates', 'CLEO-INJECTION.md');
443
+ if (existsSync(legacyTemplatePath)) {
444
+ const legacyContent = await readFile(legacyTemplatePath, 'utf8');
445
+ const legacyVersion = legacyContent.match(/^Version:\s*(.+)$/m)?.[1]?.trim();
446
+ if (legacyVersion !== xdgVersion) {
447
+ ctx.warnings.push(
448
+ `Health: Legacy template version (${legacyVersion}) != XDG version (${xdgVersion})`,
449
+ );
450
+ }
451
+ }
452
+
453
+ // Check 3: AGENTS.md references the correct path
454
+ if (existsSync(agentsMd)) {
455
+ const agentsContent = await readFile(agentsMd, 'utf8');
456
+ const expectedRef = `@${getCleoTemplatesTildePath()}/CLEO-INJECTION.md`;
457
+ if (!agentsContent.includes(expectedRef)) {
458
+ ctx.warnings.push(`Health: ~/.agents/AGENTS.md does not reference ${expectedRef}`);
459
+ }
460
+
461
+ // Check 4: No orphaned .md fragments outside CAAMP blocks
462
+ const outsideCaamp = agentsContent.replace(
463
+ /<!-- CAAMP:START -->[\s\S]*?<!-- CAAMP:END -->/g,
464
+ '',
465
+ );
466
+ if (/[A-Z][A-Za-z-]*\.md/.test(outsideCaamp)) {
467
+ ctx.warnings.push('Health: ~/.agents/AGENTS.md has orphaned content outside CAAMP blocks');
468
+ }
469
+ }
470
+ } catch {
471
+ // Health check is non-critical — don't fail bootstrap
472
+ }
473
+ }
package/src/cleo.ts CHANGED
@@ -31,6 +31,28 @@ import type {
31
31
  import { exportTasks } from './admin/export.js';
32
32
  import type { ImportParams } from './admin/import.js';
33
33
  import { importTasks } from './admin/import.js';
34
+ // Agents
35
+ import {
36
+ type AgentCapacity,
37
+ type AgentHealthStatus,
38
+ type AgentInstanceRow,
39
+ checkAgentHealth,
40
+ deregisterAgent,
41
+ detectCrashedAgents,
42
+ getAgentCapacity,
43
+ heartbeat,
44
+ isOverloaded,
45
+ listAgentInstances,
46
+ type RegisterAgentOptions,
47
+ registerAgent,
48
+ } from './agents/index.js';
49
+ // Intelligence
50
+ import {
51
+ type BlastRadius,
52
+ calculateBlastRadius,
53
+ type ImpactReport,
54
+ predictImpact,
55
+ } from './intelligence/index.js';
34
56
  // Lifecycle
35
57
  import {
36
58
  checkGate,
@@ -127,6 +149,8 @@ import {
127
149
  } from './sticky/index.js';
128
150
  // Store
129
151
  import { getAccessor } from './store/data-accessor.js';
152
+ // Task Work (start/stop/current)
153
+ import { currentTask, startTask, stopTask } from './task-work/index.js';
130
154
  // Tasks
131
155
  import { addTask } from './tasks/add.js';
132
156
  import { archiveTasks } from './tasks/archive.js';
@@ -179,10 +203,21 @@ export interface TasksAPI {
179
203
  complete(params: { taskId: string; notes?: string }): Promise<unknown>;
180
204
  delete(params: { taskId: string; force?: boolean }): Promise<unknown>;
181
205
  archive(params?: { before?: string; taskIds?: string[]; dryRun?: boolean }): Promise<unknown>;
206
+ /** Start working on a specific task (sets focus). */
207
+ start(taskId: string): Promise<unknown>;
208
+ /** Stop working on the current task (clears focus). */
209
+ stop(): Promise<{ previousTask: string | null }>;
210
+ /** Get the current task work state. */
211
+ current(): Promise<unknown>;
182
212
  }
183
213
 
184
214
  export interface SessionsAPI {
185
- start(params: { name: string; scope: string; agent?: string }): Promise<unknown>;
215
+ start(params: {
216
+ name: string;
217
+ scope: string;
218
+ agent?: string;
219
+ startTask?: string;
220
+ }): Promise<unknown>;
186
221
  end(params?: { note?: string }): Promise<unknown>;
187
222
  status(): Promise<unknown>;
188
223
  resume(sessionId: string): Promise<unknown>;
@@ -324,6 +359,32 @@ export interface SyncAPI {
324
359
  removeProviderLinks(providerId: string): Promise<number>;
325
360
  }
326
361
 
362
+ export interface AgentsAPI {
363
+ /** Register a new agent instance. */
364
+ register(options: RegisterAgentOptions): Promise<AgentInstanceRow>;
365
+ /** Deregister an agent instance. */
366
+ deregister(agentId: string): Promise<AgentInstanceRow | null>;
367
+ /** Get health status for a specific agent. */
368
+ health(agentId: string): Promise<AgentHealthStatus | null>;
369
+ /** Detect agents that have crashed (missed heartbeats). */
370
+ detectCrashed(thresholdMs?: number): Promise<AgentInstanceRow[]>;
371
+ /** Record a heartbeat for an agent. */
372
+ recordHeartbeat(agentId: string): Promise<unknown>;
373
+ /** Get capacity info for an agent. */
374
+ capacity(agentId: string): Promise<AgentCapacity | null>;
375
+ /** Check if system is overloaded (available capacity below threshold). */
376
+ isOverloaded(threshold?: number): Promise<boolean>;
377
+ /** List all agent instances with optional filters. */
378
+ list(params?: { status?: string; agentType?: string }): Promise<AgentInstanceRow[]>;
379
+ }
380
+
381
+ export interface IntelligenceAPI {
382
+ /** Predict impact of a change description on related tasks. */
383
+ predictImpact(change: string): Promise<ImpactReport>;
384
+ /** Calculate blast radius for a task change. */
385
+ blastRadius(taskId: string): Promise<BlastRadius>;
386
+ }
387
+
327
388
  // ============================================================================
328
389
  // Init options
329
390
  // ============================================================================
@@ -410,6 +471,9 @@ export class Cleo {
410
471
  delete: (p) => deleteTask({ taskId: p.taskId, force: p.force }, root, store),
411
472
  archive: (p) =>
412
473
  archiveTasks({ before: p?.before, taskIds: p?.taskIds, dryRun: p?.dryRun }, root, store),
474
+ start: (taskId) => startTask(taskId, root, store),
475
+ stop: () => stopTask(root, store),
476
+ current: () => currentTask(root, store),
413
477
  };
414
478
  }
415
479
 
@@ -418,7 +482,12 @@ export class Cleo {
418
482
  const root = this.projectRoot;
419
483
  const store = this._store ?? undefined;
420
484
  return {
421
- start: (p) => startSession({ name: p.name, scope: p.scope, agent: p.agent }, root, store),
485
+ start: (p) =>
486
+ startSession(
487
+ { name: p.name, scope: p.scope, agent: p.agent, startTask: p.startTask },
488
+ root,
489
+ store,
490
+ ),
422
491
  end: (p) => endSession({ note: p?.note }, root, store),
423
492
  status: () => sessionStatus(root, store),
424
493
  resume: (id) => resumeSession(id, root, store),
@@ -570,6 +639,38 @@ export class Cleo {
570
639
  };
571
640
  }
572
641
 
642
+ // === Agents ===
643
+ get agents(): AgentsAPI {
644
+ const root = this.projectRoot;
645
+ return {
646
+ register: (opts) => registerAgent(opts, root),
647
+ deregister: (agentId) => deregisterAgent(agentId, root),
648
+ health: (agentId) => checkAgentHealth(agentId, undefined, root),
649
+ detectCrashed: (thresholdMs) => detectCrashedAgents(thresholdMs, root),
650
+ recordHeartbeat: (agentId) => heartbeat(agentId, root),
651
+ capacity: (agentId) => getAgentCapacity(agentId, root),
652
+ isOverloaded: (threshold) => isOverloaded(threshold, root),
653
+ list: (p) =>
654
+ listAgentInstances(
655
+ {
656
+ status: p?.status as 'active' | 'idle' | 'crashed' | undefined,
657
+ agentType: p?.agentType as import('./agents/index.js').AgentType | undefined,
658
+ },
659
+ root,
660
+ ),
661
+ };
662
+ }
663
+
664
+ // === Intelligence ===
665
+ get intelligence(): IntelligenceAPI {
666
+ const root = this.projectRoot;
667
+ const store = this._store ?? undefined;
668
+ return {
669
+ predictImpact: (change) => predictImpact(change, root, store ?? undefined),
670
+ blastRadius: (taskId) => calculateBlastRadius(taskId, store ?? undefined, root),
671
+ };
672
+ }
673
+
573
674
  // === Sync (Task Reconciliation) ===
574
675
  get sync(): SyncAPI {
575
676
  const root = this.projectRoot;
package/src/config.ts CHANGED
@@ -348,7 +348,7 @@ export const STRICTNESS_PRESETS: Record<StrictnessPreset, PresetDefinition> = {
348
348
  'session.autoStart': false,
349
349
  'session.requireNotes': true,
350
350
  'session.multiSession': false,
351
- 'hierarchy.requireAcceptanceCriteria': true,
351
+ 'enforcement.acceptance.mode': 'block',
352
352
  'lifecycle.mode': 'strict',
353
353
  },
354
354
  },
@@ -359,7 +359,7 @@ export const STRICTNESS_PRESETS: Record<StrictnessPreset, PresetDefinition> = {
359
359
  'session.autoStart': false,
360
360
  'session.requireNotes': false,
361
361
  'session.multiSession': true,
362
- 'hierarchy.requireAcceptanceCriteria': false,
362
+ 'enforcement.acceptance.mode': 'warn',
363
363
  'lifecycle.mode': 'advisory',
364
364
  },
365
365
  },
@@ -369,7 +369,7 @@ export const STRICTNESS_PRESETS: Record<StrictnessPreset, PresetDefinition> = {
369
369
  'session.autoStart': false,
370
370
  'session.requireNotes': false,
371
371
  'session.multiSession': true,
372
- 'hierarchy.requireAcceptanceCriteria': false,
372
+ 'enforcement.acceptance.mode': 'off',
373
373
  'lifecycle.mode': 'off',
374
374
  },
375
375
  },
package/src/index.ts CHANGED
@@ -40,6 +40,7 @@ export * as coreHooks from './hooks/index.js';
40
40
  export * as inject from './inject/index.js';
41
41
  export * as intelligence from './intelligence/index.js';
42
42
  export * as issue from './issue/index.js';
43
+ export * as lib from './lib/index.js';
43
44
  export * as lifecycle from './lifecycle/index.js';
44
45
  export * as coreMcp from './mcp/index.js';
45
46
  export * as memory from './memory/index.js';