@lumenflow/core 2.2.1 → 2.3.1

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 (221) hide show
  1. package/dist/active-wu-detector.d.ts +1 -1
  2. package/dist/active-wu-detector.js +1 -1
  3. package/dist/arg-parser.js +51 -18
  4. package/dist/backlog-generator.d.ts +4 -4
  5. package/dist/backlog-generator.js +4 -4
  6. package/dist/backlog-sync-validator.js +1 -1
  7. package/dist/cleanup-lock.d.ts +9 -2
  8. package/dist/cleanup-lock.js +17 -7
  9. package/dist/code-path-validator.d.ts +3 -3
  10. package/dist/code-path-validator.js +3 -3
  11. package/dist/compliance-parser.d.ts +1 -1
  12. package/dist/compliance-parser.js +1 -1
  13. package/dist/constants/backlog-patterns.d.ts +1 -1
  14. package/dist/constants/backlog-patterns.js +1 -1
  15. package/dist/constants/dora-constants.d.ts +1 -1
  16. package/dist/constants/dora-constants.js +1 -1
  17. package/dist/constants/gate-constants.d.ts +1 -1
  18. package/dist/constants/gate-constants.js +1 -1
  19. package/dist/constants/linter-constants.d.ts +1 -1
  20. package/dist/constants/linter-constants.js +1 -1
  21. package/dist/constants/tokenizer-constants.d.ts +1 -1
  22. package/dist/constants/tokenizer-constants.js +1 -1
  23. package/dist/context/location-resolver.js +2 -1
  24. package/dist/context-validation-integration.d.ts +1 -0
  25. package/dist/core/scope-checker.d.ts +3 -3
  26. package/dist/core/scope-checker.js +3 -3
  27. package/dist/core/tool-runner.d.ts +5 -5
  28. package/dist/core/tool-runner.js +5 -5
  29. package/dist/core/tool.constants.d.ts +1 -1
  30. package/dist/core/tool.constants.js +1 -1
  31. package/dist/core/tool.schemas.d.ts +2 -2
  32. package/dist/core/tool.schemas.js +1 -1
  33. package/dist/core/worktree-guard.d.ts +1 -1
  34. package/dist/core/worktree-guard.js +1 -1
  35. package/dist/coverage-gate.d.ts +12 -3
  36. package/dist/coverage-gate.js +15 -8
  37. package/dist/date-utils.d.ts +4 -4
  38. package/dist/date-utils.js +4 -4
  39. package/dist/dependency-graph.d.ts +6 -0
  40. package/dist/dependency-graph.js +43 -2
  41. package/dist/dependency-guard.d.ts +2 -2
  42. package/dist/dependency-guard.js +3 -3
  43. package/dist/dependency-validator.d.ts +4 -4
  44. package/dist/dependency-validator.js +4 -7
  45. package/dist/domain/orchestration.constants.d.ts +31 -10
  46. package/dist/domain/orchestration.constants.js +45 -16
  47. package/dist/domain/orchestration.schemas.d.ts +54 -28
  48. package/dist/domain/orchestration.schemas.js +2 -2
  49. package/dist/domain/orchestration.types.d.ts +2 -2
  50. package/dist/domain/orchestration.types.js +2 -2
  51. package/dist/error-handler.d.ts +10 -10
  52. package/dist/error-handler.js +10 -10
  53. package/dist/file-classifiers.d.ts +6 -6
  54. package/dist/file-classifiers.js +6 -6
  55. package/dist/gates-config.d.ts +74 -0
  56. package/dist/gates-config.js +209 -2
  57. package/dist/git-adapter.d.ts +11 -11
  58. package/dist/git-adapter.js +11 -11
  59. package/dist/git-context-extractor.d.ts +112 -0
  60. package/dist/git-context-extractor.js +559 -0
  61. package/dist/hardcoded-strings.d.ts +1 -1
  62. package/dist/hardcoded-strings.js +1 -1
  63. package/dist/incremental-lint.d.ts +1 -1
  64. package/dist/incremental-lint.js +2 -2
  65. package/dist/incremental-test.d.ts +1 -1
  66. package/dist/incremental-test.js +1 -1
  67. package/dist/index.d.ts +13 -0
  68. package/dist/index.js +25 -0
  69. package/dist/invariants/check-automated-tests.d.ts +2 -2
  70. package/dist/invariants/check-automated-tests.js +3 -3
  71. package/dist/lane-checker.d.ts +28 -7
  72. package/dist/lane-checker.js +316 -159
  73. package/dist/lane-suggest-prompt.d.ts +108 -0
  74. package/dist/lane-suggest-prompt.js +359 -0
  75. package/dist/lane-validator.d.ts +3 -3
  76. package/dist/lane-validator.js +3 -3
  77. package/dist/logs-lib.d.ts +1 -1
  78. package/dist/logs-lib.js +1 -1
  79. package/dist/lumenflow-config-schema.d.ts +162 -0
  80. package/dist/lumenflow-config-schema.js +180 -0
  81. package/dist/manual-test-validator.d.ts +2 -2
  82. package/dist/manual-test-validator.js +3 -3
  83. package/dist/merge-lock.d.ts +8 -1
  84. package/dist/merge-lock.js +16 -7
  85. package/dist/micro-worktree.d.ts +81 -13
  86. package/dist/micro-worktree.js +98 -17
  87. package/dist/migration-deployer.d.ts +1 -1
  88. package/dist/migration-deployer.js +1 -1
  89. package/dist/orchestration-advisory-loader.d.ts +2 -2
  90. package/dist/orchestration-advisory-loader.js +10 -6
  91. package/dist/orchestration-advisory.d.ts +3 -3
  92. package/dist/orchestration-advisory.js +4 -4
  93. package/dist/orchestration-di.d.ts +4 -4
  94. package/dist/orchestration-di.js +4 -4
  95. package/dist/orchestration-rules.d.ts +4 -4
  96. package/dist/orchestration-rules.js +18 -10
  97. package/dist/orphan-detector.d.ts +3 -3
  98. package/dist/orphan-detector.js +3 -3
  99. package/dist/patrol-loop.d.ts +170 -0
  100. package/dist/patrol-loop.js +186 -0
  101. package/dist/process-detector.d.ts +5 -5
  102. package/dist/process-detector.js +5 -5
  103. package/dist/rebase-artifact-cleanup.d.ts +3 -3
  104. package/dist/rebase-artifact-cleanup.js +3 -3
  105. package/dist/resolve-policy.d.ts +195 -0
  106. package/dist/resolve-policy.js +203 -0
  107. package/dist/risk-detector.d.ts +2 -2
  108. package/dist/risk-detector.js +2 -2
  109. package/dist/rollback-utils.d.ts +1 -1
  110. package/dist/rollback-utils.js +1 -1
  111. package/dist/section-headings.d.ts +1 -1
  112. package/dist/section-headings.js +1 -1
  113. package/dist/spawn-escalation.d.ts +4 -4
  114. package/dist/spawn-escalation.js +3 -3
  115. package/dist/spawn-monitor.d.ts +4 -4
  116. package/dist/spawn-monitor.js +4 -4
  117. package/dist/spawn-recovery.d.ts +3 -3
  118. package/dist/spawn-recovery.js +3 -3
  119. package/dist/spawn-registry-schema.d.ts +2 -2
  120. package/dist/spawn-registry-schema.js +2 -2
  121. package/dist/spawn-registry-store.d.ts +2 -2
  122. package/dist/spawn-registry-store.js +2 -2
  123. package/dist/spawn-strategy.d.ts +17 -11
  124. package/dist/spawn-strategy.js +47 -44
  125. package/dist/spawn-tree.d.ts +3 -3
  126. package/dist/spawn-tree.js +3 -3
  127. package/dist/state-cleanup-core.d.ts +205 -0
  128. package/dist/state-cleanup-core.js +240 -0
  129. package/dist/state-doctor-core.d.ts +168 -0
  130. package/dist/state-doctor-core.js +251 -0
  131. package/dist/stream-error-handler.d.ts +67 -0
  132. package/dist/stream-error-handler.js +94 -0
  133. package/dist/system-map-validator.d.ts +18 -0
  134. package/dist/system-map-validator.js +50 -16
  135. package/dist/telemetry.d.ts +1 -1
  136. package/dist/telemetry.js +1 -1
  137. package/dist/template-loader.d.ts +162 -0
  138. package/dist/template-loader.js +372 -0
  139. package/dist/test-baseline.d.ts +176 -0
  140. package/dist/test-baseline.js +282 -0
  141. package/dist/usecases/get-suggestions.usecase.d.ts +1 -1
  142. package/dist/validation/command-registry.js +37 -0
  143. package/dist/validators/backlog-sync.d.ts +14 -0
  144. package/dist/validators/backlog-sync.js +62 -0
  145. package/dist/validators/supabase-docs-linter.d.ts +18 -0
  146. package/dist/validators/supabase-docs-linter.js +42 -0
  147. package/dist/validators/wu-tasks.d.ts +24 -0
  148. package/dist/validators/wu-tasks.js +90 -0
  149. package/dist/worktree-scanner.d.ts +1 -1
  150. package/dist/worktree-scanner.js +1 -1
  151. package/dist/worktree-symlink.d.ts +3 -3
  152. package/dist/worktree-symlink.js +3 -3
  153. package/dist/wu-backlog-updater.d.ts +1 -1
  154. package/dist/wu-backlog-updater.js +1 -1
  155. package/dist/wu-claim-helpers.d.ts +1 -1
  156. package/dist/wu-claim-helpers.js +1 -1
  157. package/dist/wu-claim-resume.d.ts +1 -1
  158. package/dist/wu-claim-resume.js +1 -1
  159. package/dist/wu-consistency-checker.d.ts +1 -1
  160. package/dist/wu-consistency-checker.js +17 -11
  161. package/dist/wu-constants.d.ts +73 -36
  162. package/dist/wu-constants.js +65 -92
  163. package/dist/wu-done-branch-only.d.ts +1 -1
  164. package/dist/wu-done-branch-only.js +1 -1
  165. package/dist/wu-done-docs-generate.d.ts +1 -1
  166. package/dist/wu-done-docs-generate.js +1 -1
  167. package/dist/wu-done-messages.d.ts +2 -2
  168. package/dist/wu-done-messages.js +2 -2
  169. package/dist/wu-done-metadata.d.ts +3 -3
  170. package/dist/wu-done-metadata.js +3 -3
  171. package/dist/wu-done-pr.d.ts +1 -1
  172. package/dist/wu-done-pr.js +4 -2
  173. package/dist/wu-done-preflight.d.ts +20 -10
  174. package/dist/wu-done-preflight.js +48 -47
  175. package/dist/wu-done-ui.d.ts +3 -3
  176. package/dist/wu-done-ui.js +3 -3
  177. package/dist/wu-done-validation.d.ts +30 -0
  178. package/dist/wu-done-validation.js +106 -1
  179. package/dist/wu-done-validators.d.ts +1 -1
  180. package/dist/wu-done-worktree.d.ts +1 -1
  181. package/dist/wu-done-worktree.js +11 -1
  182. package/dist/wu-events-cleanup.d.ts +148 -0
  183. package/dist/wu-events-cleanup.js +401 -0
  184. package/dist/wu-helpers.d.ts +2 -2
  185. package/dist/wu-helpers.js +2 -2
  186. package/dist/wu-id-generator.d.ts +58 -0
  187. package/dist/wu-id-generator.js +103 -0
  188. package/dist/wu-lint.js +1 -1
  189. package/dist/wu-preflight-validators.d.ts +13 -1
  190. package/dist/wu-preflight-validators.js +56 -1
  191. package/dist/wu-recovery.d.ts +2 -2
  192. package/dist/wu-recovery.js +4 -4
  193. package/dist/wu-repair-core.d.ts +5 -5
  194. package/dist/wu-repair-core.js +6 -6
  195. package/dist/wu-schema-normalization.d.ts +1 -1
  196. package/dist/wu-schema-normalization.js +1 -1
  197. package/dist/wu-schema.d.ts +7 -7
  198. package/dist/wu-schema.js +8 -8
  199. package/dist/wu-spawn-context.d.ts +87 -0
  200. package/dist/wu-spawn-context.js +175 -0
  201. package/dist/wu-spawn-helpers.d.ts +1 -1
  202. package/dist/wu-spawn-helpers.js +1 -1
  203. package/dist/wu-spawn.d.ts +177 -4
  204. package/dist/wu-spawn.js +694 -72
  205. package/dist/wu-state-schema.d.ts +1 -1
  206. package/dist/wu-state-schema.js +1 -1
  207. package/dist/wu-state-store.d.ts +3 -3
  208. package/dist/wu-state-store.js +3 -3
  209. package/dist/wu-status-transition.d.ts +1 -1
  210. package/dist/wu-status-transition.js +1 -1
  211. package/dist/wu-status-updater.d.ts +3 -3
  212. package/dist/wu-status-updater.js +3 -3
  213. package/dist/wu-validation-constants.d.ts +2 -2
  214. package/dist/wu-validation-constants.js +2 -2
  215. package/dist/wu-validation.d.ts +3 -3
  216. package/dist/wu-validation.js +3 -3
  217. package/dist/wu-yaml-fixer.d.ts +2 -2
  218. package/dist/wu-yaml-fixer.js +3 -3
  219. package/dist/wu-yaml.d.ts +23 -0
  220. package/dist/wu-yaml.js +76 -2
  221. package/package.json +5 -2
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Stream Error Handler
3
+ *
4
+ * WU-1233: Centralized EPIPE protection for CLI commands.
5
+ *
6
+ * When CLI output is piped through head/tail, the pipe may close before
7
+ * all output is written. Node.js throws EPIPE errors in this case.
8
+ * This handler catches those errors and exits gracefully.
9
+ *
10
+ * Unix convention: Exit with code 0 on EPIPE (the consumer got what it needed).
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { StreamErrorHandler } from '@lumenflow/core';
15
+ *
16
+ * // In CLI entry point:
17
+ * const handler = StreamErrorHandler.createWithDefaults();
18
+ * handler.attach();
19
+ * ```
20
+ */
21
+ import type { WriteStream } from 'node:tty';
22
+ /**
23
+ * Dependencies for StreamErrorHandler
24
+ *
25
+ * Uses dependency injection for testability.
26
+ */
27
+ export interface StreamErrorHandlerDeps {
28
+ /** Standard output stream */
29
+ stdout: WriteStream;
30
+ /** Standard error stream */
31
+ stderr: WriteStream;
32
+ /** Exit function (defaults to process.exit) */
33
+ exitFn: (code: number) => void;
34
+ }
35
+ /**
36
+ * StreamErrorHandler
37
+ *
38
+ * Attaches error listeners to stdout/stderr to handle EPIPE errors gracefully.
39
+ * Follows single responsibility principle - only handles stream errors.
40
+ */
41
+ export declare class StreamErrorHandler {
42
+ private readonly deps;
43
+ private attached;
44
+ private stdoutHandler;
45
+ private stderrHandler;
46
+ constructor(deps: StreamErrorHandlerDeps);
47
+ /**
48
+ * Create a handler with default dependencies (process.stdout, process.stderr, process.exit)
49
+ */
50
+ static createWithDefaults(): StreamErrorHandler;
51
+ /**
52
+ * Attach error listeners to stdout and stderr
53
+ *
54
+ * Safe to call multiple times - will not attach duplicate listeners.
55
+ */
56
+ attach(): void;
57
+ /**
58
+ * Detach error listeners from stdout and stderr
59
+ *
60
+ * Safe to call even if not attached.
61
+ */
62
+ detach(): void;
63
+ /**
64
+ * Create an error handler that catches EPIPE and exits gracefully
65
+ */
66
+ private createErrorHandler;
67
+ }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Stream Error Handler
3
+ *
4
+ * WU-1233: Centralized EPIPE protection for CLI commands.
5
+ *
6
+ * When CLI output is piped through head/tail, the pipe may close before
7
+ * all output is written. Node.js throws EPIPE errors in this case.
8
+ * This handler catches those errors and exits gracefully.
9
+ *
10
+ * Unix convention: Exit with code 0 on EPIPE (the consumer got what it needed).
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { StreamErrorHandler } from '@lumenflow/core';
15
+ *
16
+ * // In CLI entry point:
17
+ * const handler = StreamErrorHandler.createWithDefaults();
18
+ * handler.attach();
19
+ * ```
20
+ */
21
+ import { STREAM_ERRORS, EXIT_CODES } from './wu-constants.js';
22
+ /**
23
+ * StreamErrorHandler
24
+ *
25
+ * Attaches error listeners to stdout/stderr to handle EPIPE errors gracefully.
26
+ * Follows single responsibility principle - only handles stream errors.
27
+ */
28
+ export class StreamErrorHandler {
29
+ deps;
30
+ attached = false;
31
+ stdoutHandler = null;
32
+ stderrHandler = null;
33
+ constructor(deps) {
34
+ this.deps = deps;
35
+ }
36
+ /**
37
+ * Create a handler with default dependencies (process.stdout, process.stderr, process.exit)
38
+ */
39
+ static createWithDefaults() {
40
+ return new StreamErrorHandler({
41
+ stdout: process.stdout,
42
+ stderr: process.stderr,
43
+ exitFn: (code) => process.exit(code),
44
+ });
45
+ }
46
+ /**
47
+ * Attach error listeners to stdout and stderr
48
+ *
49
+ * Safe to call multiple times - will not attach duplicate listeners.
50
+ */
51
+ attach() {
52
+ if (this.attached) {
53
+ return;
54
+ }
55
+ this.stdoutHandler = this.createErrorHandler();
56
+ this.stderrHandler = this.createErrorHandler();
57
+ this.deps.stdout.on('error', this.stdoutHandler);
58
+ this.deps.stderr.on('error', this.stderrHandler);
59
+ this.attached = true;
60
+ }
61
+ /**
62
+ * Detach error listeners from stdout and stderr
63
+ *
64
+ * Safe to call even if not attached.
65
+ */
66
+ detach() {
67
+ if (!this.attached) {
68
+ return;
69
+ }
70
+ if (this.stdoutHandler) {
71
+ this.deps.stdout.removeListener('error', this.stdoutHandler);
72
+ this.stdoutHandler = null;
73
+ }
74
+ if (this.stderrHandler) {
75
+ this.deps.stderr.removeListener('error', this.stderrHandler);
76
+ this.stderrHandler = null;
77
+ }
78
+ this.attached = false;
79
+ }
80
+ /**
81
+ * Create an error handler that catches EPIPE and exits gracefully
82
+ */
83
+ createErrorHandler() {
84
+ return (err) => {
85
+ // Only handle EPIPE errors
86
+ if (err.code === STREAM_ERRORS.EPIPE) {
87
+ // Exit gracefully with success code (Unix convention)
88
+ // The consumer of the pipe got what it needed
89
+ this.deps.exitFn(EXIT_CODES.SUCCESS);
90
+ }
91
+ // Non-EPIPE errors are not handled here - let them propagate
92
+ };
93
+ }
94
+ }
@@ -78,3 +78,21 @@ export declare function validateSystemMap(systemMap: any, deps: any): Promise<{
78
78
  queryErrors: any[];
79
79
  classificationErrors: any[];
80
80
  }>;
81
+ export interface SystemMapValidationResult {
82
+ valid: boolean;
83
+ skipped: boolean;
84
+ pathErrors: string[];
85
+ orphanDocs: string[];
86
+ audienceErrors: string[];
87
+ queryErrors: string[];
88
+ classificationErrors: string[];
89
+ }
90
+ export declare function runSystemMapValidation(options?: {
91
+ cwd?: string;
92
+ systemMapPath?: string;
93
+ logger?: {
94
+ log: (message: string) => void;
95
+ warn?: (message: string) => void;
96
+ error?: (message: string) => void;
97
+ };
98
+ }): Promise<SystemMapValidationResult>;
@@ -12,6 +12,7 @@
12
12
  * @module system-map-validator
13
13
  */
14
14
  import { existsSync, readFileSync } from 'node:fs';
15
+ import path from 'node:path';
15
16
  import fg from 'fast-glob';
16
17
  import { parseYAML } from './wu-yaml.js';
17
18
  /**
@@ -274,34 +275,64 @@ export async function validateSystemMap(systemMap, deps) {
274
275
  };
275
276
  }
276
277
  const DEFAULT_SYSTEM_MAP_PATH = 'SYSTEM-MAP.yaml';
277
- function emitErrors(label, errors) {
278
- if (!errors || errors.length === 0)
279
- return;
280
- console.error(`\n${label}:`);
281
- for (const error of errors) {
282
- console.error(` - ${error}`);
283
- }
284
- }
285
- async function runCLI() {
286
- const systemMapPath = process.env.SYSTEM_MAP_PATH || DEFAULT_SYSTEM_MAP_PATH;
287
- if (!existsSync(systemMapPath)) {
288
- console.warn(`[system-map] ${systemMapPath} not found; skipping validation.`);
289
- process.exit(0);
278
+ export async function runSystemMapValidation(options = {}) {
279
+ const { cwd = process.cwd(), systemMapPath, logger = console } = options;
280
+ const resolvedPath = systemMapPath ?? path.join(cwd, DEFAULT_SYSTEM_MAP_PATH);
281
+ if (!existsSync(resolvedPath)) {
282
+ logger.warn?.(`[system-map] ${resolvedPath} not found; skipping validation.`);
283
+ return {
284
+ valid: true,
285
+ skipped: true,
286
+ pathErrors: [],
287
+ orphanDocs: [],
288
+ audienceErrors: [],
289
+ queryErrors: [],
290
+ classificationErrors: [],
291
+ };
290
292
  }
291
293
  let systemMap;
292
294
  try {
293
- const raw = readFileSync(systemMapPath, 'utf-8');
295
+ const raw = readFileSync(resolvedPath, 'utf-8');
294
296
  systemMap = parseYAML(raw);
295
297
  }
296
298
  catch (error) {
297
299
  const message = error instanceof Error ? error.message : String(error);
298
- console.error(`[system-map] Failed to read or parse ${systemMapPath}: ${message}`);
299
- process.exit(1);
300
+ logger.error?.(`[system-map] Failed to read or parse ${resolvedPath}: ${message}`);
301
+ return {
302
+ valid: false,
303
+ skipped: false,
304
+ pathErrors: [message],
305
+ orphanDocs: [],
306
+ audienceErrors: [],
307
+ queryErrors: [],
308
+ classificationErrors: [],
309
+ };
300
310
  }
301
311
  const result = await validateSystemMap(systemMap, {
302
312
  exists: (path) => existsSync(path),
303
313
  glob: (pattern) => fg(pattern, { dot: false }),
304
314
  });
315
+ return {
316
+ valid: result.valid,
317
+ skipped: false,
318
+ pathErrors: result.pathErrors,
319
+ orphanDocs: result.orphanDocs,
320
+ audienceErrors: result.audienceErrors,
321
+ queryErrors: result.queryErrors,
322
+ classificationErrors: result.classificationErrors,
323
+ };
324
+ }
325
+ function emitErrors(label, errors) {
326
+ if (!errors || errors.length === 0)
327
+ return;
328
+ console.error(`\n${label}:`);
329
+ for (const error of errors) {
330
+ console.error(` - ${error}`);
331
+ }
332
+ }
333
+ async function runCLI() {
334
+ const systemMapPath = process.env.SYSTEM_MAP_PATH || DEFAULT_SYSTEM_MAP_PATH;
335
+ const result = await runSystemMapValidation({ systemMapPath });
305
336
  if (!result.valid) {
306
337
  console.error('\n[system-map] Validation failed');
307
338
  emitErrors('Missing paths', result.pathErrors);
@@ -311,6 +342,9 @@ async function runCLI() {
311
342
  emitErrors('Classification routing violations', result.classificationErrors);
312
343
  process.exit(1);
313
344
  }
345
+ if (result.skipped) {
346
+ process.exit(0);
347
+ }
314
348
  console.log('[system-map] Validation passed');
315
349
  process.exit(0);
316
350
  }
@@ -3,7 +3,7 @@
3
3
  * Telemetry Module - DORA/SPACE Metrics Emission
4
4
  *
5
5
  * Emits structured NDJSON telemetry for gates execution and WU flow metrics.
6
- * Used by gates-local.mjs and flow-report.mjs.
6
+ * Used by gates-local.ts and flow-report.ts.
7
7
  */
8
8
  /**
9
9
  * Emit a telemetry event as NDJSON
package/dist/telemetry.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * Telemetry Module - DORA/SPACE Metrics Emission
4
4
  *
5
5
  * Emits structured NDJSON telemetry for gates execution and WU flow metrics.
6
- * Used by gates-local.mjs and flow-report.mjs.
6
+ * Used by gates-local.ts and flow-report.ts.
7
7
  */
8
8
  import { appendFileSync, mkdirSync, existsSync } from 'node:fs';
9
9
  import { execSync } from 'node:child_process';
@@ -0,0 +1,162 @@
1
+ /**
2
+ * Template Loader (WU-1253)
3
+ *
4
+ * Loads, parses, and assembles prompt templates from .lumenflow/templates/
5
+ * with YAML frontmatter support. Enables extraction of hardcoded templates
6
+ * from wu-spawn.ts into maintainable markdown files.
7
+ *
8
+ * Features:
9
+ * - YAML frontmatter parsing via gray-matter
10
+ * - Manifest-driven assembly order
11
+ * - Client-specific overrides (templates.claude/, templates.cursor/)
12
+ * - Token replacement ({WU_ID}, {LANE}, etc.)
13
+ * - Conditional template inclusion
14
+ *
15
+ * @see {@link https://lumenflow.dev/reference/template-system/} - Template documentation
16
+ */
17
+ /**
18
+ * Template frontmatter parsed from YAML.
19
+ * All templates must include these fields in their frontmatter block.
20
+ */
21
+ export interface TemplateFrontmatter {
22
+ /** Unique identifier matching manifest entry (e.g., 'tdd-directive') */
23
+ id: string;
24
+ /** Human-readable name for debugging and documentation */
25
+ name: string;
26
+ /** Whether this template must be present for assembly to succeed */
27
+ required: boolean;
28
+ /** Assembly position - lower numbers appear first in output */
29
+ order: number;
30
+ /** Token names for replacement (e.g., ['WU_ID', 'LANE']) */
31
+ tokens?: string[];
32
+ /** Conditional expression for inclusion (e.g., "type === 'feature'") */
33
+ condition?: string;
34
+ }
35
+ /**
36
+ * Loaded template with content and metadata.
37
+ * Returned by loadTemplate() after parsing.
38
+ */
39
+ export interface LoadedTemplate {
40
+ /** Parsed frontmatter fields */
41
+ frontmatter: TemplateFrontmatter;
42
+ /** Markdown content after frontmatter block */
43
+ content: string;
44
+ /** Original file path for debugging */
45
+ sourcePath: string;
46
+ }
47
+ /**
48
+ * Manifest entry defining a template's inclusion in assembly.
49
+ */
50
+ export interface ManifestEntry {
51
+ /** Template identifier matching frontmatter id */
52
+ id: string;
53
+ /** Relative path within templates/spawn-prompt/ */
54
+ path: string;
55
+ /** Must be present for assembly to succeed */
56
+ required: boolean;
57
+ /** Assembly position (ascending order) */
58
+ order: number;
59
+ /** Optional condition expression */
60
+ condition?: string;
61
+ }
62
+ /**
63
+ * Complete manifest structure parsed from manifest.yaml.
64
+ */
65
+ export interface TemplateManifest {
66
+ /** Manifest format version */
67
+ version: string;
68
+ /** Default settings for template processing */
69
+ defaults: {
70
+ /** Token format: '{TOKEN}' or '{{TOKEN}}' */
71
+ tokenFormat: string;
72
+ };
73
+ /** Ordered list of template entries */
74
+ templates: ManifestEntry[];
75
+ }
76
+ /**
77
+ * Context for token replacement during assembly.
78
+ * Values are substituted for {TOKEN} placeholders.
79
+ */
80
+ export interface TemplateContext {
81
+ /** Work Unit identifier (e.g., 'WU-1253') */
82
+ WU_ID: string;
83
+ /** Lane name (e.g., 'Framework: Core') */
84
+ LANE: string;
85
+ /** WU type (e.g., 'feature', 'bug', 'documentation') */
86
+ TYPE: string;
87
+ /** Optional WU title */
88
+ TITLE?: string;
89
+ /** Optional WU description */
90
+ DESCRIPTION?: string;
91
+ /** Optional worktree path */
92
+ WORKTREE_PATH?: string;
93
+ /** Optional parent lane extracted from LANE */
94
+ laneParent?: string;
95
+ /** Allow additional context properties */
96
+ [key: string]: string | undefined;
97
+ }
98
+ /**
99
+ * Load and parse the template manifest.
100
+ *
101
+ * @param baseDir - Project root directory containing .lumenflow/
102
+ * @returns Parsed manifest with validated structure
103
+ * @throws If manifest is missing or has invalid structure
104
+ */
105
+ export declare function loadManifest(baseDir: string): TemplateManifest;
106
+ /**
107
+ * Load a single template file with frontmatter parsing.
108
+ *
109
+ * Uses gray-matter with the yaml engine for robust parsing,
110
+ * matching the pattern established in backlog-parser.ts.
111
+ *
112
+ * @param templatePath - Absolute path to template file
113
+ * @returns Parsed template with frontmatter and content
114
+ * @throws If file is missing or frontmatter is invalid
115
+ */
116
+ export declare function loadTemplate(templatePath: string): LoadedTemplate;
117
+ /**
118
+ * Load all templates from a directory, respecting client overrides.
119
+ *
120
+ * Override resolution order:
121
+ * 1. .lumenflow/templates.{client}/spawn-prompt/{template}.md (highest priority)
122
+ * 2. .lumenflow/templates/spawn-prompt/{template}.md (fallback)
123
+ *
124
+ * @param baseDir - Project root directory
125
+ * @param clientName - Client name for overrides (e.g., 'claude', 'cursor')
126
+ * @returns Map of template id to loaded template
127
+ */
128
+ export declare function loadTemplatesWithOverrides(baseDir: string, clientName: string): Map<string, LoadedTemplate>;
129
+ /**
130
+ * Assemble templates in manifest order with token replacement.
131
+ *
132
+ * @param templates - Map of loaded templates by id
133
+ * @param manifest - Manifest defining assembly order
134
+ * @param context - Token values for replacement
135
+ * @returns Assembled content with all tokens replaced
136
+ * @throws If required template is missing
137
+ */
138
+ export declare function assembleTemplates(templates: Map<string, LoadedTemplate>, manifest: TemplateManifest, context: TemplateContext): string;
139
+ /**
140
+ * Replace {TOKEN} placeholders with context values.
141
+ *
142
+ * @param content - Template content with placeholders
143
+ * @param tokens - Token name to value mapping
144
+ * @returns Content with tokens replaced
145
+ */
146
+ export declare function replaceTokens(content: string, tokens: Record<string, string | undefined>): string;
147
+ /**
148
+ * Evaluate a simple condition expression against context.
149
+ *
150
+ * Supports:
151
+ * - Equality: type === 'feature'
152
+ * - Inequality: type !== 'documentation'
153
+ * - Truthy: worktreePath
154
+ * - AND: type === 'feature' && lane === 'Core'
155
+ * - OR: type === 'feature' || type === 'bug'
156
+ * - Dotted paths: policy.testing === 'tdd' (WU-1260)
157
+ *
158
+ * @param condition - Condition expression string
159
+ * @param context - Context values for evaluation
160
+ * @returns Whether condition evaluates to true
161
+ */
162
+ export declare function evaluateCondition(condition: string | undefined, context: Record<string, unknown>): boolean;