@lumenflow/packs-software-delivery 4.23.0 → 5.0.0

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 (286) hide show
  1. package/dist/manifest-schema.d.ts +12 -0
  2. package/dist/manifest-schema.d.ts.map +1 -1
  3. package/dist/manifest-schema.js +10 -0
  4. package/dist/manifest-schema.js.map +1 -1
  5. package/dist/manifest.d.ts +21 -0
  6. package/dist/manifest.d.ts.map +1 -1
  7. package/dist/manifest.js +92 -1
  8. package/dist/manifest.js.map +1 -1
  9. package/dist/src/commands/index.d.ts +2 -0
  10. package/dist/src/commands/index.d.ts.map +1 -0
  11. package/dist/src/commands/index.js +5 -0
  12. package/dist/src/commands/index.js.map +1 -0
  13. package/dist/src/config/delivery-review-contract.d.ts +17 -0
  14. package/dist/src/config/delivery-review-contract.d.ts.map +1 -0
  15. package/dist/src/config/delivery-review-contract.js +19 -0
  16. package/dist/src/config/delivery-review-contract.js.map +1 -0
  17. package/dist/src/config/env-accessors.d.ts +16 -0
  18. package/dist/src/config/env-accessors.d.ts.map +1 -0
  19. package/dist/src/config/env-accessors.js +18 -0
  20. package/dist/src/config/env-accessors.js.map +1 -0
  21. package/dist/src/config/index.d.ts +3 -0
  22. package/dist/src/config/index.d.ts.map +1 -0
  23. package/dist/src/config/index.js +8 -0
  24. package/dist/src/config/index.js.map +1 -0
  25. package/dist/src/config/normalize-config-keys.d.ts +16 -0
  26. package/dist/src/config/normalize-config-keys.d.ts.map +1 -0
  27. package/dist/src/config/normalize-config-keys.js +18 -0
  28. package/dist/src/config/normalize-config-keys.js.map +1 -0
  29. package/dist/src/config/schemas/lumenflow-config-schema-types.d.ts +190 -0
  30. package/dist/src/config/schemas/lumenflow-config-schema-types.d.ts.map +1 -0
  31. package/dist/src/config/schemas/lumenflow-config-schema-types.js +182 -0
  32. package/dist/src/config/schemas/lumenflow-config-schema-types.js.map +1 -0
  33. package/dist/src/config/schemas/lumenflow-config-schema.d.ts +190 -0
  34. package/dist/src/config/schemas/lumenflow-config-schema.d.ts.map +1 -0
  35. package/dist/src/config/schemas/lumenflow-config-schema.js +182 -0
  36. package/dist/src/config/schemas/lumenflow-config-schema.js.map +1 -0
  37. package/dist/src/config/workspace-reader.d.ts +56 -0
  38. package/dist/src/config/workspace-reader.d.ts.map +1 -0
  39. package/dist/src/config/workspace-reader.js +209 -0
  40. package/dist/src/config/workspace-reader.js.map +1 -0
  41. package/dist/src/constants/backlog-patterns.d.ts +21 -0
  42. package/dist/src/constants/backlog-patterns.d.ts.map +1 -0
  43. package/dist/src/constants/backlog-patterns.js +26 -0
  44. package/dist/src/constants/backlog-patterns.js.map +1 -0
  45. package/dist/src/constants/client-ids.d.ts +16 -0
  46. package/dist/src/constants/client-ids.d.ts.map +1 -0
  47. package/dist/src/constants/client-ids.js +16 -0
  48. package/dist/src/constants/client-ids.js.map +1 -0
  49. package/dist/src/constants/config-contract.d.ts +2 -0
  50. package/dist/src/constants/config-contract.d.ts.map +1 -0
  51. package/dist/src/constants/config-contract.js +7 -0
  52. package/dist/src/constants/config-contract.js.map +1 -0
  53. package/dist/src/constants/docs-layout-presets.d.ts +31 -0
  54. package/dist/src/constants/docs-layout-presets.d.ts.map +1 -0
  55. package/dist/src/constants/docs-layout-presets.js +41 -0
  56. package/dist/src/constants/docs-layout-presets.js.map +1 -0
  57. package/dist/src/constants/duration-constants.d.ts +11 -0
  58. package/dist/src/constants/duration-constants.d.ts.map +1 -0
  59. package/dist/src/constants/duration-constants.js +13 -0
  60. package/dist/src/constants/duration-constants.js.map +1 -0
  61. package/dist/src/constants/gate-constants.d.ts +24 -0
  62. package/dist/src/constants/gate-constants.d.ts.map +1 -0
  63. package/dist/src/constants/gate-constants.js +26 -0
  64. package/dist/src/constants/gate-constants.js.map +1 -0
  65. package/dist/src/constants/index.d.ts +18 -0
  66. package/dist/src/constants/index.d.ts.map +1 -0
  67. package/dist/src/constants/index.js +29 -0
  68. package/dist/src/constants/index.js.map +1 -0
  69. package/dist/src/constants/lock-constants.d.ts +29 -0
  70. package/dist/src/constants/lock-constants.d.ts.map +1 -0
  71. package/dist/src/constants/lock-constants.js +31 -0
  72. package/dist/src/constants/lock-constants.js.map +1 -0
  73. package/dist/src/constants/object-guards.d.ts +9 -0
  74. package/dist/src/constants/object-guards.d.ts.map +1 -0
  75. package/dist/src/constants/object-guards.js +11 -0
  76. package/dist/src/constants/object-guards.js.map +1 -0
  77. package/dist/src/constants/section-headings.d.ts +35 -0
  78. package/dist/src/constants/section-headings.d.ts.map +1 -0
  79. package/dist/src/constants/section-headings.js +82 -0
  80. package/dist/src/constants/section-headings.js.map +1 -0
  81. package/dist/src/constants/wu-cli-constants.d.ts +434 -0
  82. package/dist/src/constants/wu-cli-constants.d.ts.map +1 -0
  83. package/dist/src/constants/wu-cli-constants.js +439 -0
  84. package/dist/src/constants/wu-cli-constants.js.map +1 -0
  85. package/dist/src/constants/wu-domain-constants.d.ts +296 -0
  86. package/dist/src/constants/wu-domain-constants.d.ts.map +1 -0
  87. package/dist/src/constants/wu-domain-constants.js +400 -0
  88. package/dist/src/constants/wu-domain-constants.js.map +1 -0
  89. package/dist/src/constants/wu-git-constants.d.ts +2 -0
  90. package/dist/src/constants/wu-git-constants.d.ts.map +1 -0
  91. package/dist/src/constants/wu-git-constants.js +7 -0
  92. package/dist/src/constants/wu-git-constants.js.map +1 -0
  93. package/dist/src/constants/wu-id-format.d.ts +138 -0
  94. package/dist/src/constants/wu-id-format.d.ts.map +1 -0
  95. package/dist/src/constants/wu-id-format.js +265 -0
  96. package/dist/src/constants/wu-id-format.js.map +1 -0
  97. package/dist/src/constants/wu-paths-constants.d.ts +254 -0
  98. package/dist/src/constants/wu-paths-constants.d.ts.map +1 -0
  99. package/dist/src/constants/wu-paths-constants.js +276 -0
  100. package/dist/src/constants/wu-paths-constants.js.map +1 -0
  101. package/dist/src/constants/wu-statuses.d.ts +209 -0
  102. package/dist/src/constants/wu-statuses.d.ts.map +1 -0
  103. package/dist/src/constants/wu-statuses.js +245 -0
  104. package/dist/src/constants/wu-statuses.js.map +1 -0
  105. package/dist/src/constants/wu-type-helpers.d.ts +28 -0
  106. package/dist/src/constants/wu-type-helpers.d.ts.map +1 -0
  107. package/dist/src/constants/wu-type-helpers.js +49 -0
  108. package/dist/src/constants/wu-type-helpers.js.map +1 -0
  109. package/dist/src/constants/wu-ui-constants.d.ts +236 -0
  110. package/dist/src/constants/wu-ui-constants.d.ts.map +1 -0
  111. package/dist/src/constants/wu-ui-constants.js +238 -0
  112. package/dist/src/constants/wu-ui-constants.js.map +1 -0
  113. package/dist/src/constants/wu-validation-constants.d.ts +61 -0
  114. package/dist/src/constants/wu-validation-constants.d.ts.map +1 -0
  115. package/dist/src/constants/wu-validation-constants.js +69 -0
  116. package/dist/src/constants/wu-validation-constants.js.map +1 -0
  117. package/dist/src/domain/index.d.ts +4 -0
  118. package/dist/src/domain/index.d.ts.map +1 -0
  119. package/dist/src/domain/index.js +6 -0
  120. package/dist/src/domain/index.js.map +1 -0
  121. package/dist/src/domain/orchestration.constants.d.ts +111 -0
  122. package/dist/src/domain/orchestration.constants.d.ts.map +1 -0
  123. package/dist/src/domain/orchestration.constants.js +130 -0
  124. package/dist/src/domain/orchestration.constants.js.map +1 -0
  125. package/dist/src/domain/orchestration.schemas.d.ts +307 -0
  126. package/dist/src/domain/orchestration.schemas.d.ts.map +1 -0
  127. package/dist/src/domain/orchestration.schemas.js +214 -0
  128. package/dist/src/domain/orchestration.schemas.js.map +1 -0
  129. package/dist/src/domain/orchestration.types.d.ts +134 -0
  130. package/dist/src/domain/orchestration.types.d.ts.map +1 -0
  131. package/dist/src/domain/orchestration.types.js +5 -0
  132. package/dist/src/domain/orchestration.types.js.map +1 -0
  133. package/dist/src/methodology/incremental-test.d.ts +33 -0
  134. package/dist/src/methodology/incremental-test.d.ts.map +1 -0
  135. package/dist/src/methodology/incremental-test.js +73 -0
  136. package/dist/src/methodology/incremental-test.js.map +1 -0
  137. package/dist/src/methodology/index.d.ts +3 -0
  138. package/dist/src/methodology/index.d.ts.map +1 -0
  139. package/dist/src/methodology/index.js +6 -0
  140. package/dist/src/methodology/index.js.map +1 -0
  141. package/dist/src/methodology/manual-test-validator.d.ts +97 -0
  142. package/dist/src/methodology/manual-test-validator.d.ts.map +1 -0
  143. package/dist/src/methodology/manual-test-validator.js +248 -0
  144. package/dist/src/methodology/manual-test-validator.js.map +1 -0
  145. package/dist/src/policy/coverage-gate.d.ts +127 -0
  146. package/dist/src/policy/coverage-gate.d.ts.map +1 -0
  147. package/dist/src/policy/coverage-gate.js +211 -0
  148. package/dist/src/policy/coverage-gate.js.map +1 -0
  149. package/dist/src/policy/gates-agent-mode.d.ts +107 -0
  150. package/dist/src/policy/gates-agent-mode.d.ts.map +1 -0
  151. package/dist/src/policy/gates-agent-mode.js +138 -0
  152. package/dist/src/policy/gates-agent-mode.js.map +1 -0
  153. package/dist/src/policy/gates-config-internal.d.ts +54 -0
  154. package/dist/src/policy/gates-config-internal.d.ts.map +1 -0
  155. package/dist/src/policy/gates-config-internal.js +108 -0
  156. package/dist/src/policy/gates-config-internal.js.map +1 -0
  157. package/dist/src/policy/gates-config.d.ts +67 -0
  158. package/dist/src/policy/gates-config.d.ts.map +1 -0
  159. package/dist/src/policy/gates-config.js +193 -0
  160. package/dist/src/policy/gates-config.js.map +1 -0
  161. package/dist/src/policy/gates-coverage.d.ts +48 -0
  162. package/dist/src/policy/gates-coverage.d.ts.map +1 -0
  163. package/dist/src/policy/gates-coverage.js +182 -0
  164. package/dist/src/policy/gates-coverage.js.map +1 -0
  165. package/dist/src/policy/gates-presets.d.ts +51 -0
  166. package/dist/src/policy/gates-presets.d.ts.map +1 -0
  167. package/dist/src/policy/gates-presets.js +117 -0
  168. package/dist/src/policy/gates-presets.js.map +1 -0
  169. package/dist/src/policy/gates-schemas.d.ts +142 -0
  170. package/dist/src/policy/gates-schemas.d.ts.map +1 -0
  171. package/dist/src/policy/gates-schemas.js +67 -0
  172. package/dist/src/policy/gates-schemas.js.map +1 -0
  173. package/dist/src/policy/index.d.ts +19 -0
  174. package/dist/src/policy/index.d.ts.map +1 -0
  175. package/dist/src/policy/index.js +21 -0
  176. package/dist/src/policy/index.js.map +1 -0
  177. package/dist/src/policy/package-manager-resolver.d.ts +79 -0
  178. package/dist/src/policy/package-manager-resolver.d.ts.map +1 -0
  179. package/dist/src/policy/package-manager-resolver.js +245 -0
  180. package/dist/src/policy/package-manager-resolver.js.map +1 -0
  181. package/dist/src/policy/resolve-policy.d.ts +337 -0
  182. package/dist/src/policy/resolve-policy.d.ts.map +1 -0
  183. package/dist/src/policy/resolve-policy.js +353 -0
  184. package/dist/src/policy/resolve-policy.js.map +1 -0
  185. package/dist/src/ports/config.ports.d.ts +83 -0
  186. package/dist/src/ports/config.ports.d.ts.map +1 -0
  187. package/dist/src/ports/config.ports.js +4 -0
  188. package/dist/src/ports/config.ports.js.map +1 -0
  189. package/dist/src/ports/dashboard-renderer.port.d.ts +113 -0
  190. package/dist/src/ports/dashboard-renderer.port.d.ts.map +1 -0
  191. package/dist/src/ports/dashboard-renderer.port.js +4 -0
  192. package/dist/src/ports/dashboard-renderer.port.js.map +1 -0
  193. package/dist/src/ports/index.d.ts +5 -0
  194. package/dist/src/ports/index.d.ts.map +1 -0
  195. package/dist/src/ports/index.js +10 -0
  196. package/dist/src/ports/index.js.map +1 -0
  197. package/dist/src/ports/sync-validator.ports.d.ts +52 -0
  198. package/dist/src/ports/sync-validator.ports.d.ts.map +1 -0
  199. package/dist/src/ports/sync-validator.ports.js +4 -0
  200. package/dist/src/ports/sync-validator.ports.js.map +1 -0
  201. package/dist/src/ports/wu-helpers.ports.d.ts +157 -0
  202. package/dist/src/ports/wu-helpers.ports.d.ts.map +1 -0
  203. package/dist/src/ports/wu-helpers.ports.js +4 -0
  204. package/dist/src/ports/wu-helpers.ports.js.map +1 -0
  205. package/dist/src/ports/wu-state.ports.d.ts +209 -0
  206. package/dist/src/ports/wu-state.ports.d.ts.map +1 -0
  207. package/dist/src/ports/wu-state.ports.js +4 -0
  208. package/dist/src/ports/wu-state.ports.js.map +1 -0
  209. package/dist/src/primitives/index.d.ts +2 -0
  210. package/dist/src/primitives/index.d.ts.map +1 -0
  211. package/dist/src/primitives/index.js +5 -0
  212. package/dist/src/primitives/index.js.map +1 -0
  213. package/dist/src/runtime/index.d.ts +2 -0
  214. package/dist/src/runtime/index.d.ts.map +1 -0
  215. package/dist/src/runtime/index.js +6 -0
  216. package/dist/src/runtime/index.js.map +1 -0
  217. package/dist/src/runtime/work-classifier.d.ts +103 -0
  218. package/dist/src/runtime/work-classifier.d.ts.map +1 -0
  219. package/dist/src/runtime/work-classifier.js +427 -0
  220. package/dist/src/runtime/work-classifier.js.map +1 -0
  221. package/dist/src/sandbox/index.d.ts +6 -0
  222. package/dist/src/sandbox/index.d.ts.map +1 -0
  223. package/dist/src/sandbox/index.js +10 -0
  224. package/dist/src/sandbox/index.js.map +1 -0
  225. package/dist/src/sandbox/sandbox-allowlist.d.ts +16 -0
  226. package/dist/src/sandbox/sandbox-allowlist.d.ts.map +1 -0
  227. package/dist/src/sandbox/sandbox-allowlist.js +77 -0
  228. package/dist/src/sandbox/sandbox-allowlist.js.map +1 -0
  229. package/dist/src/sandbox/sandbox-backend-linux.d.ts +6 -0
  230. package/dist/src/sandbox/sandbox-backend-linux.d.ts.map +1 -0
  231. package/dist/src/sandbox/sandbox-backend-linux.js +67 -0
  232. package/dist/src/sandbox/sandbox-backend-linux.js.map +1 -0
  233. package/dist/src/sandbox/sandbox-backend-macos.d.ts +6 -0
  234. package/dist/src/sandbox/sandbox-backend-macos.d.ts.map +1 -0
  235. package/dist/src/sandbox/sandbox-backend-macos.js +112 -0
  236. package/dist/src/sandbox/sandbox-backend-macos.js.map +1 -0
  237. package/dist/src/sandbox/sandbox-backend-windows.d.ts +6 -0
  238. package/dist/src/sandbox/sandbox-backend-windows.d.ts.map +1 -0
  239. package/dist/src/sandbox/sandbox-backend-windows.js +30 -0
  240. package/dist/src/sandbox/sandbox-backend-windows.js.map +1 -0
  241. package/dist/src/sandbox/sandbox-profile.d.ts +58 -0
  242. package/dist/src/sandbox/sandbox-profile.d.ts.map +1 -0
  243. package/dist/src/sandbox/sandbox-profile.js +69 -0
  244. package/dist/src/sandbox/sandbox-profile.js.map +1 -0
  245. package/dist/src/schemas/index.d.ts +2 -0
  246. package/dist/src/schemas/index.d.ts.map +1 -0
  247. package/dist/src/schemas/index.js +5 -0
  248. package/dist/src/schemas/index.js.map +1 -0
  249. package/dist/src/state/date-utils.d.ts +66 -0
  250. package/dist/src/state/date-utils.d.ts.map +1 -0
  251. package/dist/src/state/date-utils.js +143 -0
  252. package/dist/src/state/date-utils.js.map +1 -0
  253. package/dist/src/state/index.d.ts +8 -0
  254. package/dist/src/state/index.d.ts.map +1 -0
  255. package/dist/src/state/index.js +15 -0
  256. package/dist/src/state/index.js.map +1 -0
  257. package/dist/src/state/state-machine.d.ts +14 -0
  258. package/dist/src/state/state-machine.d.ts.map +1 -0
  259. package/dist/src/state/state-machine.js +92 -0
  260. package/dist/src/state/state-machine.js.map +1 -0
  261. package/dist/src/state/wu-doc-types.d.ts +48 -0
  262. package/dist/src/state/wu-doc-types.d.ts.map +1 -0
  263. package/dist/src/state/wu-doc-types.js +4 -0
  264. package/dist/src/state/wu-doc-types.js.map +1 -0
  265. package/dist/src/state/wu-paths.d.ts +275 -0
  266. package/dist/src/state/wu-paths.d.ts.map +1 -0
  267. package/dist/src/state/wu-paths.js +335 -0
  268. package/dist/src/state/wu-paths.js.map +1 -0
  269. package/dist/src/state/wu-schema.d.ts +831 -0
  270. package/dist/src/state/wu-schema.d.ts.map +1 -0
  271. package/dist/src/state/wu-schema.js +934 -0
  272. package/dist/src/state/wu-schema.js.map +1 -0
  273. package/dist/src/state/wu-state-schema.d.ts +292 -0
  274. package/dist/src/state/wu-state-schema.d.ts.map +1 -0
  275. package/dist/src/state/wu-state-schema.js +215 -0
  276. package/dist/src/state/wu-state-schema.js.map +1 -0
  277. package/dist/src/state/wu-yaml.d.ts +113 -0
  278. package/dist/src/state/wu-yaml.d.ts.map +1 -0
  279. package/dist/src/state/wu-yaml.js +307 -0
  280. package/dist/src/state/wu-yaml.js.map +1 -0
  281. package/dist/tool-impl/wu-lifecycle-tools.d.ts +11 -0
  282. package/dist/tool-impl/wu-lifecycle-tools.d.ts.map +1 -1
  283. package/dist/tool-impl/wu-lifecycle-tools.js +17 -0
  284. package/dist/tool-impl/wu-lifecycle-tools.js.map +1 -1
  285. package/manifest.yaml +46 -0
  286. package/package.json +88 -3
@@ -0,0 +1,934 @@
1
+ // Copyright (c) 2026 Hellmai Ltd
2
+ // SPDX-License-Identifier: AGPL-3.0-only
3
+ /**
4
+ * Work Unit YAML Schema
5
+ *
6
+ * Zod schema for runtime validation of WU YAML structure.
7
+ * Provides compile-time type inference and semantic validation.
8
+ *
9
+ * Part of WU-1162: Add Zod schema validation to prevent placeholder WU completions
10
+ * Part of WU-1539: Add BaseWUSchema pattern for create/edit validation
11
+ *
12
+ * Schema Architecture (DRY pattern):
13
+ * - BaseWUSchema: Structural validation only (field types, formats, lengths)
14
+ * - WUSchema: Extends base + placeholder rejection (for wu:claim, wu:done)
15
+ * - ReadyWUSchema: Alias for BaseWUSchema (for wu:create, wu:edit)
16
+ *
17
+ * @see {@link packages/@lumenflow/cli/src/wu-done.ts} - Consumer (validates spec completeness, uses WUSchema)
18
+ * @see {@link packages/@lumenflow/cli/src/wu-claim.ts} - Consumer (validates spec completeness, uses WUSchema)
19
+ * @see {@link packages/@lumenflow/cli/src/wu-create.ts} - Consumer (structural validation, uses ReadyWUSchema)
20
+ * @see {@link packages/@lumenflow/cli/src/wu-edit.ts} - Consumer (structural validation, uses ReadyWUSchema)
21
+ * @see {@link packages/@lumenflow/cli/src/validate.ts} - Consumer (CI validation)
22
+ * @see {@link apps/web/src/lib/llm/schemas/orchestrator.ts} - Pattern reference
23
+ */
24
+ import { z } from 'zod';
25
+ import { WU_STATUS_GROUPS, WU_EXPOSURE_VALUES, WU_TYPES, WU_TYPE_VALUES, } from '../constants/wu-statuses.js';
26
+ import { WU_DEFAULTS } from '../constants/wu-domain-constants.js';
27
+ import { STRING_LITERALS } from '../constants/wu-ui-constants.js';
28
+ import { isDocsOrProcessType, isWUType } from '../constants/wu-type-helpers.js';
29
+ import { createWuPaths } from './wu-paths.js';
30
+ import { normalizeISODateTime } from './date-utils.js';
31
+ // WU-2225: getConfig import removed (getEscalationEmail was the only consumer)
32
+ /**
33
+ * Valid WU status values derived from WU_STATUS constant (DRY principle)
34
+ * Used for Zod enum validation with improved error messages
35
+ * Note: Defined as tuple for Zod enum compatibility
36
+ */
37
+ const VALID_STATUSES = [
38
+ 'todo',
39
+ 'ready',
40
+ 'backlog',
41
+ 'in_progress',
42
+ 'blocked',
43
+ 'done',
44
+ 'completed',
45
+ 'cancelled',
46
+ 'abandoned',
47
+ 'deferred',
48
+ 'closed',
49
+ 'superseded',
50
+ ];
51
+ /**
52
+ * Placeholder sentinel constant
53
+ *
54
+ * Used in wu:create template generation and validation.
55
+ * Single source of truth for placeholder detection (DRY principle).
56
+ *
57
+ * @example
58
+ * // tools/wu-create.ts
59
+ * description: `${PLACEHOLDER_SENTINEL} Describe the work...`
60
+ *
61
+ * @example
62
+ * // tools/validate.ts
63
+ * if (doc.description.includes(PLACEHOLDER_SENTINEL)) { error(); }
64
+ */
65
+ export const PLACEHOLDER_SENTINEL = '[PLACEHOLDER]';
66
+ /**
67
+ * Minimum description length requirement
68
+ * Stored as constant for DRY error message generation
69
+ */
70
+ const MIN_DESCRIPTION_LENGTH = 50;
71
+ /**
72
+ * WU ID format validation message (DRY principle)
73
+ * Used across blocks, blocked_by, and ui_pairing_wus fields
74
+ */
75
+ const WU_ID_FORMAT_MESSAGE = 'Must be WU-XXX format';
76
+ /**
77
+ * Acceptance criterion error message
78
+ * Stored as constant for DRY error message generation (sonarjs/no-duplicate-string)
79
+ */
80
+ const ACCEPTANCE_REQUIRED_MSG = 'At least one acceptance criterion required';
81
+ const CONTEXT_REQUIRED_TYPES = [WU_TYPES.FEATURE, WU_TYPES.BUG, WU_TYPES.REFACTOR];
82
+ // =============================================================================
83
+ // WU-1750: NORMALIZATION TRANSFORMS (Watertight YAML validation)
84
+ // =============================================================================
85
+ /**
86
+ * Regex pattern matching embedded newlines (both literal and escaped)
87
+ * Handles: "a\nb" (literal newline) and "a\\nb" (escaped backslash-n)
88
+ */
89
+ const NEWLINE_PATTERN = /\\n|\n/;
90
+ /**
91
+ * Transform: Normalize string arrays by splitting embedded newlines
92
+ *
93
+ * WU-1750: Agents sometimes pass multi-item content as single strings with \n.
94
+ * This transform auto-repairs: ["a\nb\nc"] → ["a", "b", "c"]
95
+ *
96
+ * @example
97
+ * // Input: ["tools/a.ts\ntools/b.js"]
98
+ * // Output: ["tools/a.js", "tools/b.js"]
99
+ */
100
+ const normalizedStringArray = z.array(z.string()).transform((arr) => arr
101
+ .flatMap((s) => s.split(NEWLINE_PATTERN))
102
+ .map((s) => s.trim())
103
+ .filter(Boolean));
104
+ /**
105
+ * Transform: Normalize description/notes strings by converting escaped newlines
106
+ *
107
+ * WU-1750: YAML quoted strings preserve literal \\n as two characters.
108
+ * This transform converts them to actual newlines: "a\\n\\nb" → "a\n\nb"
109
+ *
110
+ * @example
111
+ * // Input: "Problem:\\n\\n1. First issue"
112
+ * // Output: "Problem:\n\n1. First issue"
113
+ */
114
+ const _normalizedMultilineString = z.string().transform((s) => s.replace(/\\n/g, '\n'));
115
+ /**
116
+ * Refinement: File path cannot contain newlines (post-normalization safety check)
117
+ *
118
+ * WU-1750: After normalization, paths should be clean. This catches UnsafeAny edge cases.
119
+ */
120
+ const filePathItem = z.string().refine((s) => !s.includes('\n') && !s.includes('\\n'), {
121
+ message: 'File path cannot contain newlines - split into separate array items',
122
+ });
123
+ /**
124
+ * Normalized code_paths: split embedded newlines + validate each path
125
+ */
126
+ const normalizedCodePaths = normalizedStringArray.pipe(z.array(filePathItem)).default([]);
127
+ /**
128
+ * Normalized test paths object: all test arrays normalized
129
+ */
130
+ const normalizedTestPaths = z
131
+ .object({
132
+ manual: normalizedStringArray.optional(),
133
+ unit: normalizedStringArray.optional(),
134
+ integration: normalizedStringArray.optional(),
135
+ e2e: normalizedStringArray.optional(),
136
+ })
137
+ .optional();
138
+ // =============================================================================
139
+ // BASE FIELD DEFINITIONS (DRY - shared between BaseWUSchema and WUSchema)
140
+ // =============================================================================
141
+ /**
142
+ * Base description field (structural validation only)
143
+ * WU-1539: Fixed template string bug (single quotes → function message)
144
+ * WU-1750: Added normalization of escaped newlines (\\n → actual newlines)
145
+ */
146
+ const baseDescriptionField = z
147
+ .string()
148
+ .min(1, 'Description is required')
149
+ .transform((s) => s.replace(/\\n/g, '\n')) // WU-1750: Normalize escaped newlines
150
+ .refine((val) => val.trim().length >= MIN_DESCRIPTION_LENGTH, {
151
+ // WU-1539 fix: Use function message for dynamic interpolation
152
+ message: `Description must be at least ${MIN_DESCRIPTION_LENGTH} characters`,
153
+ });
154
+ /**
155
+ * Strict description field (with placeholder rejection)
156
+ * Used by wu:claim and wu:done to ensure placeholders are filled
157
+ * WU-1750: Added normalization of escaped newlines (\\n → actual newlines)
158
+ */
159
+ const strictDescriptionField = z
160
+ .string()
161
+ .min(1, 'Description is required')
162
+ .transform((s) => s.replace(/\\n/g, '\n')) // WU-1750: Normalize escaped newlines
163
+ .refine((val) => !val.includes(PLACEHOLDER_SENTINEL), {
164
+ message: `Description cannot contain ${PLACEHOLDER_SENTINEL} marker`,
165
+ })
166
+ .refine((val) => val.trim().length >= MIN_DESCRIPTION_LENGTH, {
167
+ // WU-1539 fix: Use function message for dynamic interpolation
168
+ message: `Description must be at least ${MIN_DESCRIPTION_LENGTH} characters`,
169
+ });
170
+ /**
171
+ * Recursive helper: Check all nested values for at least one item
172
+ * Shared between base and strict acceptance schemas
173
+ */
174
+ const hasItems = (value) => {
175
+ if (Array.isArray(value)) {
176
+ return value.length > 0;
177
+ }
178
+ if (typeof value === 'object' && value !== null) {
179
+ return Object.values(value).some(hasItems);
180
+ }
181
+ return false;
182
+ };
183
+ /**
184
+ * Recursive helper: Check all strings for PLACEHOLDER_SENTINEL
185
+ * Used only by strict acceptance schema
186
+ */
187
+ const checkStringsForPlaceholder = (value) => {
188
+ if (typeof value === 'string') {
189
+ return !value.includes(PLACEHOLDER_SENTINEL);
190
+ }
191
+ if (Array.isArray(value)) {
192
+ return value.every(checkStringsForPlaceholder);
193
+ }
194
+ if (typeof value === 'object' && value !== null) {
195
+ return Object.values(value).every(checkStringsForPlaceholder);
196
+ }
197
+ return true;
198
+ };
199
+ /**
200
+ * Base acceptance field (structural validation only)
201
+ * Validates format but allows placeholder markers
202
+ * WU-1750: Added normalization of embedded newlines in array items
203
+ */
204
+ const baseAcceptanceField = z.union([
205
+ // Flat array format (legacy): acceptance: ["item1", "item2"]
206
+ // WU-1750: Normalize embedded newlines: ["1. a\n2. b"] → ["1. a", "2. b"]
207
+ normalizedStringArray.pipe(z.array(z.string()).min(1, ACCEPTANCE_REQUIRED_MSG)),
208
+ // Nested object format (structured): acceptance: { category1: ["item1"], category2: ["item2"] }
209
+ z.record(z.string(), normalizedStringArray).refine((obj) => Object.values(obj).some(hasItems), {
210
+ message: ACCEPTANCE_REQUIRED_MSG,
211
+ }),
212
+ ]);
213
+ /**
214
+ * Strict acceptance field (with placeholder rejection)
215
+ * Used by wu:claim and wu:done to ensure placeholders are filled
216
+ * WU-1750: Added normalization of embedded newlines in array items
217
+ */
218
+ const strictAcceptanceField = z.union([
219
+ // Flat array format (legacy): acceptance: ["item1", "item2"]
220
+ // WU-1750: Normalize embedded newlines: ["1. a\n2. b"] → ["1. a", "2. b"]
221
+ normalizedStringArray
222
+ .pipe(z.array(z.string()).min(1, ACCEPTANCE_REQUIRED_MSG))
223
+ .refine((arr) => !arr.some((item) => item.includes(PLACEHOLDER_SENTINEL)), {
224
+ message: `Acceptance criteria cannot contain ${PLACEHOLDER_SENTINEL} markers`,
225
+ }),
226
+ // Nested object format (structured): acceptance: { category1: ["item1"], category2: ["item2"] }
227
+ z
228
+ .record(z.string(), normalizedStringArray)
229
+ .refine((obj) => Object.values(obj).some(hasItems), {
230
+ message: ACCEPTANCE_REQUIRED_MSG,
231
+ })
232
+ .refine((obj) => checkStringsForPlaceholder(obj), {
233
+ message: `Acceptance criteria cannot contain ${PLACEHOLDER_SENTINEL} markers`,
234
+ }),
235
+ ]);
236
+ /**
237
+ * Shared field definitions (same for both base and strict schemas)
238
+ * DRY: Defined once, used in both schema variants
239
+ */
240
+ const sharedFields = {
241
+ /** WU identifier (e.g., WU-1162) */
242
+ id: z.string().regex(/^WU-\d+$/, 'ID must match pattern WU-XXX'),
243
+ /** Short title describing the work */
244
+ title: z.string().min(1, 'Title is required'),
245
+ /** Lane assignment (parent or sub-lane) */
246
+ lane: z.string().min(1, 'Lane is required'),
247
+ /** Work type classification */
248
+ type: z
249
+ .enum(WU_TYPE_VALUES, {
250
+ error: `Invalid type. Valid values: ${WU_TYPE_VALUES.join(', ')}`,
251
+ })
252
+ .default(WU_DEFAULTS.type),
253
+ /** Current status in workflow */
254
+ status: z
255
+ .enum(VALID_STATUSES, {
256
+ error: `Invalid status. Valid values: ${VALID_STATUSES.join(', ')}`,
257
+ })
258
+ .default(WU_DEFAULTS.status),
259
+ /** Priority level */
260
+ priority: z
261
+ .enum(['P0', 'P1', 'P2', 'P3'], {
262
+ error: 'Invalid priority',
263
+ })
264
+ .default(WU_DEFAULTS.priority),
265
+ /** Creation date (YYYY-MM-DD) */
266
+ created: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Created must be YYYY-MM-DD'),
267
+ /** Files modified by this WU - WU-1750: Normalized to split embedded newlines */
268
+ code_paths: normalizedCodePaths,
269
+ /** Test specifications - WU-1750: All test arrays normalized */
270
+ tests: normalizedTestPaths.default(WU_DEFAULTS.tests),
271
+ /** Output artifacts (stamps, docs, etc.) - WU-1750: Normalized */
272
+ artifacts: normalizedStringArray.optional().default(WU_DEFAULTS.artifacts),
273
+ /** Upstream WU dependencies (informational, legacy field) - WU-1750: Normalized */
274
+ dependencies: normalizedStringArray.optional().default(WU_DEFAULTS.dependencies),
275
+ // === Initiative System Fields (WU-1246) ===
276
+ /** Parent initiative reference (format: INIT-{number} or slug) */
277
+ initiative: z.string().optional(),
278
+ /** Phase number within parent initiative */
279
+ phase: z.number().int().positive().optional(),
280
+ /** WU IDs that this WU blocks (downstream dependencies) - WU-1750: Normalized + validated */
281
+ blocks: normalizedStringArray
282
+ .pipe(z.array(z.string().regex(/^WU-\d+$/, WU_ID_FORMAT_MESSAGE)))
283
+ .optional(),
284
+ /** WU IDs that block this WU (upstream dependencies) - WU-1750: Normalized + validated */
285
+ blocked_by: normalizedStringArray
286
+ .pipe(z.array(z.string().regex(/^WU-\d+$/, WU_ID_FORMAT_MESSAGE)))
287
+ .optional(),
288
+ /** Cross-cutting tags (orthogonal to initiative) - WU-1750: Normalized */
289
+ labels: normalizedStringArray.optional(),
290
+ // === End Initiative System Fields ===
291
+ /**
292
+ * WU-1683: First-class plan field, symmetric with initiative `related_plan`.
293
+ * Set via wu:create --plan, wu:edit --plan, or plan:link --id WU-XXX.
294
+ */
295
+ plan: z.string().optional(),
296
+ /**
297
+ * WU-1833: References to plans, design docs, external specifications
298
+ * WU-1834: Supports both flat string array AND nested object format for backwards compatibility
299
+ *
300
+ * Flat format (WU-1833+): ['docs/plans/WU-XXX-plan.md']
301
+ * Nested format (legacy): [{file: 'docs/path.md', section: 'heading'}]
302
+ * Mixed format allowed: ['path.md', {section: 'heading'}]
303
+ * Bare object (WU-428): {file: 'docs/path.md', section: 'heading'}
304
+ */
305
+ spec_refs: z
306
+ .union([
307
+ // Single object format (WU-428 style): {file: '...', section: '...'}
308
+ z.object({
309
+ file: z.string().optional(),
310
+ section: z.string(),
311
+ }),
312
+ // Array format (WU-1833+): strings, objects, or mixed
313
+ z.array(z.union([
314
+ z.string(), // Flat format: 'docs/path.md'
315
+ z.object({
316
+ // Nested format: {file: 'path', section: 'heading'}
317
+ file: z.string().optional(),
318
+ section: z.string(),
319
+ }),
320
+ ])),
321
+ ])
322
+ .optional(),
323
+ /** Known risks or constraints - WU-1750: Normalized */
324
+ risks: normalizedStringArray.optional().default(WU_DEFAULTS.risks),
325
+ /**
326
+ * Free-form notes - supports string or array (auto-converted to string)
327
+ * WU-1750: Normalizes escaped newlines (\\n → actual newlines)
328
+ */
329
+ notes: z
330
+ .union([
331
+ z.string(),
332
+ z.array(z.string()), // Legacy array format - will be converted
333
+ ])
334
+ .optional()
335
+ .transform((val) => {
336
+ // Convert array to newline-joined string (legacy format)
337
+ if (Array.isArray(val)) {
338
+ return val.filter((s) => s.trim().length > 0).join(STRING_LITERALS.NEWLINE);
339
+ }
340
+ // WU-1750: Normalize escaped newlines in string format
341
+ if (typeof val === 'string') {
342
+ return val.replace(/\\n/g, '\n');
343
+ }
344
+ return val ?? WU_DEFAULTS.notes;
345
+ }),
346
+ /** Requires human review before merge */
347
+ requires_review: z.boolean().optional().default(WU_DEFAULTS.requires_review),
348
+ /** Locked state (done WUs only) */
349
+ locked: z.boolean().optional(),
350
+ /** Completion date (done WUs only) - auto-normalized to ISO datetime */
351
+ completed_at: z
352
+ .string()
353
+ .optional()
354
+ .transform((val) => normalizeISODateTime(val)),
355
+ /** Claimed mode (worktree/branch-only/worktree-pr/branch-pr) */
356
+ claimed_mode: z.enum(['worktree', 'branch-only', 'worktree-pr', 'branch-pr']).optional(),
357
+ /**
358
+ * WU-1589: Canonical branch name for this WU claim.
359
+ *
360
+ * Set at claim time to record the actual branch used.
361
+ * Used by defaultBranchFrom() as highest-priority source for branch resolution.
362
+ * Essential for branch-pr mode where cloud agents may use non-lane-derived branch names.
363
+ * Cleared on rollback/release/recover when resetting to ready.
364
+ */
365
+ claimed_branch: z.string().optional(),
366
+ /** Assigned agent email */
367
+ assigned_to: z.string().email().optional(),
368
+ /** Claim timestamp - auto-normalized to ISO datetime */
369
+ claimed_at: z
370
+ .string()
371
+ .optional()
372
+ .transform((val) => normalizeISODateTime(val)),
373
+ /** Block reason (blocked WUs only) */
374
+ blocked_reason: z.string().optional(),
375
+ /** Worktree path (claimed WUs only) */
376
+ worktree_path: z.string().optional(),
377
+ /** Current active session ID (WU-1438: auto-set on claim, cleared on done) */
378
+ session_id: z.string().uuid().optional(),
379
+ /** Agent sessions (issue logging metadata, WU-1231) */
380
+ agent_sessions: z
381
+ .array(z.object({
382
+ session_id: z.string().uuid(),
383
+ started: z.string().datetime(),
384
+ completed: z.string().datetime().optional(),
385
+ agent_type: z.enum([
386
+ 'claude-code',
387
+ 'codex-cli',
388
+ 'cursor',
389
+ 'gemini-cli',
390
+ 'windsurf',
391
+ 'copilot',
392
+ 'other',
393
+ ]),
394
+ context_tier: z.union([z.literal(1), z.literal(2), z.literal(3)]),
395
+ incidents_logged: z.number().int().min(0).default(0),
396
+ incidents_major: z.number().int().min(0).default(0),
397
+ artifacts: z.array(z.string()).optional(),
398
+ }))
399
+ .optional(),
400
+ // === Exposure System Fields (WU-1998) ===
401
+ /**
402
+ * WU-1998: Exposure level - defines how the WU exposes functionality to users
403
+ *
404
+ * Valid values:
405
+ * - 'ui': User-facing UI changes (pages, components, widgets)
406
+ * - 'api': API endpoints called by UI or external clients
407
+ * - 'backend-only': Backend-only changes (no user visibility)
408
+ * - 'documentation': Documentation changes only
409
+ *
410
+ * Optional during transition period, will become required after backlog update.
411
+ */
412
+ exposure: z
413
+ .enum(WU_EXPOSURE_VALUES, {
414
+ error: `Invalid exposure value. Valid values: ${WU_EXPOSURE_VALUES.join(', ')}`,
415
+ })
416
+ .optional(),
417
+ /**
418
+ * WU-1998: User journey description for user-facing WUs
419
+ *
420
+ * Recommended for exposure: 'ui' and 'api'.
421
+ * Describes the end-user interaction flow affected by this WU.
422
+ */
423
+ user_journey: z.string().optional(),
424
+ /**
425
+ * WU-1998: Related UI WUs for backend/API changes
426
+ *
427
+ * For WUs with exposure: 'api', this field lists UI WUs that consume the API.
428
+ * Ensures backend features have corresponding UI coverage.
429
+ * Each entry must match WU-XXX format.
430
+ */
431
+ ui_pairing_wus: normalizedStringArray
432
+ .pipe(z.array(z.string().regex(/^WU-\d+$/, WU_ID_FORMAT_MESSAGE)))
433
+ .optional(),
434
+ /**
435
+ * WU-2022: Navigation path for UI-exposed features
436
+ *
437
+ * For WUs with exposure: 'ui', specifies the route where the feature is accessible.
438
+ * Used by wu:done to verify that UI features are actually navigable.
439
+ * Prevents "orphaned code" where features exist but users cannot access them.
440
+ *
441
+ * Example: '/dashboard', '/settings/preferences', '/space'
442
+ */
443
+ navigation_path: z.string().optional(),
444
+ // === End Exposure System Fields ===
445
+ // === Sizing Estimate Fields (WU-2141) ===
446
+ /**
447
+ * WU-2141: Optional sizing estimate metadata.
448
+ *
449
+ * Records expected WU complexity for tooling-backed sizing enforcement.
450
+ * Absent for historical WUs (backward compatible).
451
+ *
452
+ * Fields:
453
+ * - estimated_files: Expected number of files to modify
454
+ * - estimated_tool_calls: Expected tool call count
455
+ * - strategy: Execution strategy from wu-sizing-guide.md
456
+ * - exception_type: Override type when thresholds intentionally exceeded
457
+ * - exception_reason: Justification for the exception (required with exception_type)
458
+ */
459
+ sizing_estimate: z
460
+ .object({
461
+ estimated_files: z.number().int().min(0),
462
+ estimated_tool_calls: z.number().int().min(0),
463
+ strategy: z.enum([
464
+ 'single-session',
465
+ 'checkpoint-resume',
466
+ 'orchestrator-worker',
467
+ 'decomposition',
468
+ ]),
469
+ exception_type: z.enum(['docs-only', 'shallow-multi-file']).optional(),
470
+ exception_reason: z.string().optional(),
471
+ })
472
+ .refine((data) => {
473
+ if (data.exception_type !== undefined) {
474
+ return data.exception_reason !== undefined && data.exception_reason.trim().length > 0;
475
+ }
476
+ return true;
477
+ }, {
478
+ message: 'sizing_estimate.exception_reason is required and must be non-empty when exception_type is set',
479
+ path: ['exception_reason'],
480
+ })
481
+ .optional(),
482
+ // === End Sizing Estimate Fields ===
483
+ // === Agent-First Approval Fields (WU-2079 → WU-2080) ===
484
+ /**
485
+ * WU-2080: Escalation triggers detected for this WU
486
+ *
487
+ * Agent-first model: agents auto-approve by default.
488
+ * Human escalation only when these triggers are detected:
489
+ * - sensitive_data: Changes to sensitive data handling
490
+ * - security_p0: P0 security incident or vulnerability
491
+ * - budget: Budget/resource allocation above threshold
492
+ * - cross_lane_arch: Cross-lane architectural decision
493
+ *
494
+ * Empty array = no escalation needed, agent proceeds autonomously.
495
+ */
496
+ escalation_triggers: z
497
+ .array(z.enum(['sensitive_data', 'security_p0', 'budget', 'cross_lane_arch']))
498
+ .optional()
499
+ .default([]),
500
+ /**
501
+ * WU-2080: Human escalation required flag
502
+ *
503
+ * Auto-set to true when escalation_triggers is non-empty.
504
+ * When true, wu:done requires human confirmation before completion.
505
+ */
506
+ requires_human_escalation: z.boolean().optional().default(false),
507
+ /**
508
+ * WU-2080: Email(s) of approvers who signed off
509
+ *
510
+ * Auto-populated with claiming agent at wu:claim.
511
+ * Additional human approvers added when escalation is resolved.
512
+ */
513
+ approved_by: z.array(z.string().email()).optional(),
514
+ /**
515
+ * WU-2080: Timestamp when approval was granted
516
+ *
517
+ * Auto-set at wu:claim for agent auto-approval.
518
+ * Updated when human escalation is resolved.
519
+ */
520
+ approved_at: z
521
+ .string()
522
+ .optional()
523
+ .transform((val) => normalizeISODateTime(val)),
524
+ /**
525
+ * WU-2080: Human who resolved escalation (if UnsafeAny)
526
+ *
527
+ * Only set when requires_human_escalation was true and resolved.
528
+ */
529
+ escalation_resolved_by: z.string().email().optional(),
530
+ /**
531
+ * WU-2080: Timestamp when human resolved escalation
532
+ */
533
+ escalation_resolved_at: z
534
+ .string()
535
+ .optional()
536
+ .transform((val) => normalizeISODateTime(val)),
537
+ // Legacy fields (deprecated, kept for backwards compatibility)
538
+ /** @deprecated Use escalation_triggers instead */
539
+ requires_cso_approval: z.boolean().optional().default(false),
540
+ /** @deprecated Use escalation_triggers instead */
541
+ requires_cto_approval: z.boolean().optional().default(false),
542
+ /** @deprecated Use escalation_triggers instead */
543
+ requires_design_approval: z.boolean().optional().default(false),
544
+ // === End Agent-First Approval Fields ===
545
+ };
546
+ // =============================================================================
547
+ // SCHEMA DEFINITIONS
548
+ // =============================================================================
549
+ /**
550
+ * Base WU Schema (structural validation only)
551
+ *
552
+ * WU-1539: Used by wu:create and wu:edit for fail-fast structural validation.
553
+ * Allows placeholder markers - only checks field types, formats, and lengths.
554
+ *
555
+ * Use case: Validate WU structure at creation/edit time before placeholders are filled.
556
+ */
557
+ export const BaseWUSchema = z.object({
558
+ ...sharedFields,
559
+ description: baseDescriptionField,
560
+ acceptance: baseAcceptanceField,
561
+ });
562
+ /**
563
+ * Ready WU Schema (alias for BaseWUSchema)
564
+ *
565
+ * WU-1539: Semantic alias for clarity in wu:create and wu:edit.
566
+ * Same validation as BaseWUSchema - allows placeholders, enforces structure.
567
+ */
568
+ export const ReadyWUSchema = BaseWUSchema;
569
+ /**
570
+ * Strict WU Schema (structural + placeholder rejection)
571
+ *
572
+ * Validates WU files against LumenFlow requirements:
573
+ * - No placeholder text in done WUs
574
+ * - Minimum description length (50 chars)
575
+ * - Code paths present for non-documentation WUs
576
+ * - Proper status/lane/type enums
577
+ *
578
+ * Used by wu:claim and wu:done to ensure specs are complete.
579
+ * Provides runtime validation and TypeScript type inference.
580
+ */
581
+ export const WUSchema = z.object({
582
+ ...sharedFields,
583
+ description: strictDescriptionField,
584
+ acceptance: strictAcceptanceField,
585
+ });
586
+ /**
587
+ * TypeScript type inferred from schema
588
+ *
589
+ * Single source of truth for both runtime validation and compile-time types.
590
+ * Replaces manual WU interfaces (DRY principle).
591
+ *
592
+ * Note: Type inference available in TypeScript via z.infer<typeof WUSchema>
593
+ * This is a JavaScript file, so the type export is not needed here.
594
+ *
595
+ * @typedef {import('zod').z.infer<typeof WUSchema>} WU
596
+ */
597
+ /**
598
+ * Validates WU data against strict schema (placeholder rejection)
599
+ *
600
+ * Used by wu:claim and wu:done to ensure specs are complete.
601
+ * Rejects WUs with placeholder markers.
602
+ *
603
+ * @param {unknown} data - Parsed YAML data to validate
604
+ * @returns {z.SafeParseReturnType<WU, WU>} Validation result
605
+ *
606
+ * @example
607
+ * const result = validateWU(yamlData);
608
+ * if (!result.success) {
609
+ * result.error.issues.forEach(issue => {
610
+ * console.error(`${issue.path.join('.')}: ${issue.message}`);
611
+ * });
612
+ * }
613
+ */
614
+ export function validateWU(data) {
615
+ return WUSchema.safeParse(data);
616
+ }
617
+ /**
618
+ * Validates WU data against base schema (structural only)
619
+ *
620
+ * WU-1539: Used by wu:create and wu:edit for fail-fast structural validation.
621
+ * Allows placeholder markers - only checks field types, formats, and lengths.
622
+ *
623
+ * @param {unknown} data - Parsed YAML data to validate
624
+ * @returns {z.SafeParseReturnType<WU, WU>} Validation result
625
+ *
626
+ * @example
627
+ * const result = validateReadyWU(yamlData);
628
+ * if (!result.success) {
629
+ * const errors = result.error.issues
630
+ * .map(issue => ` • ${issue.path.join('.')}: ${issue.message}`)
631
+ * .join('\n');
632
+ * die(`WU YAML validation failed:\n\n${errors}`);
633
+ * }
634
+ */
635
+ export function validateReadyWU(data) {
636
+ return ReadyWUSchema.safeParse(data);
637
+ }
638
+ /**
639
+ * Validates WU spec completeness for done status
640
+ *
641
+ * Additional validation beyond schema for WUs marked as done:
642
+ * - Code paths required for non-documentation WUs
643
+ * - Locked must be true
644
+ * - Completed timestamp must be present
645
+ *
646
+ * @param {WU} wu - Validated WU data
647
+ * @returns {{valid: boolean, errors: string[]}} Validation result
648
+ *
649
+ * @example
650
+ * const schemaResult = validateWU(data);
651
+ * if (schemaResult.success && data.status === 'done') {
652
+ * const completenessResult = validateDoneWU(schemaResult.data);
653
+ * if (!completenessResult.valid) {
654
+ * console.error(completenessResult.errors);
655
+ * }
656
+ * }
657
+ */
658
+ export function validateDoneWU(wu) {
659
+ const errors = [];
660
+ // Check code_paths for non-documentation WUs
661
+ if (!isDocsOrProcessType(wu.type)) {
662
+ if (!wu.code_paths || wu.code_paths.length === 0) {
663
+ errors.push('Code paths required for non-documentation WUs');
664
+ }
665
+ }
666
+ // Note: locked and completed_at are set automatically by wu:done
667
+ // No need to validate them here (they don't exist yet at validation time)
668
+ return {
669
+ valid: errors.length === 0,
670
+ errors,
671
+ };
672
+ }
673
+ /**
674
+ * WU-2080: Valid escalation trigger types
675
+ *
676
+ * These are the only conditions that require human intervention.
677
+ * Everything else is auto-approved by agents.
678
+ */
679
+ export const ESCALATION_TRIGGER_TYPES = [
680
+ 'sensitive_data', // Sensitive data handling changes
681
+ 'security_p0', // P0 security incident
682
+ 'budget', // Budget/resource above threshold
683
+ 'cross_lane_arch', // Cross-lane architectural decision
684
+ ];
685
+ /**
686
+ * WU-2080: Agent-first approval validation
687
+ *
688
+ * AGENT-FIRST MODEL: Agents auto-approve by default.
689
+ * Human escalation only when escalation_triggers is non-empty
690
+ * AND requires_human_escalation is true AND not yet resolved.
691
+ *
692
+ * Returns:
693
+ * - valid: true if agent can proceed (no unresolved escalation)
694
+ * - errors: blocking issues requiring human resolution
695
+ * - warnings: advisory messages (non-blocking)
696
+ *
697
+ * @param {object} wu - Validated WU data
698
+ * @returns {{valid: boolean, errors: string[], warnings: string[]}}
699
+ */
700
+ export function validateApprovalGates(wu) {
701
+ const errors = [];
702
+ const warnings = [];
703
+ // Agent-first: check for unresolved escalation triggers
704
+ const triggers = wu.escalation_triggers || [];
705
+ const requiresEscalation = wu.requires_human_escalation || triggers.length > 0;
706
+ if (requiresEscalation) {
707
+ // Check if escalation was resolved by human
708
+ const resolved = wu.escalation_resolved_by && wu.escalation_resolved_at;
709
+ if (!resolved) {
710
+ errors.push(`Human escalation required for: ${triggers.join(', ')}\n` +
711
+ ` To resolve: pnpm wu:escalate --resolve --id ${wu.id}`);
712
+ }
713
+ }
714
+ // Legacy backwards compatibility: map old fields to new model
715
+ if (wu.requires_cso_approval || wu.requires_cto_approval || wu.requires_design_approval) {
716
+ warnings.push('Using deprecated requires_X_approval fields. Migrate to escalation_triggers model.');
717
+ }
718
+ return {
719
+ valid: errors.length === 0,
720
+ errors,
721
+ warnings,
722
+ };
723
+ }
724
+ /**
725
+ * WU-2080: Detect escalation triggers from WU content
726
+ *
727
+ * Analyzes WU metadata to detect conditions requiring human escalation.
728
+ * Called by wu:claim to auto-set escalation_triggers.
729
+ *
730
+ * @param {object} wu - WU data with lane, type, code_paths
731
+ * @returns {string[]} Array of triggered escalation types
732
+ */
733
+ export function detectEscalationTriggers(wu) {
734
+ const triggers = [];
735
+ const lane = (wu.lane || '').toLowerCase();
736
+ const codePaths = wu.code_paths || [];
737
+ // Sensitive data: Changes to user data or auth
738
+ const sensitivePatterns = ['pii', 'user-data', 'auth', 'credentials'];
739
+ const touchesSensitive = codePaths.some((p) => sensitivePatterns.some((pat) => p.toLowerCase().includes(pat)));
740
+ if (touchesSensitive || lane.includes('pii')) {
741
+ triggers.push('sensitive_data');
742
+ }
743
+ // Security P0: Explicit security lane or auth changes
744
+ if (wu.priority === 'P0' && lane.includes('security')) {
745
+ triggers.push('security_p0');
746
+ }
747
+ return triggers;
748
+ }
749
+ /**
750
+ * WU-2080: Generate auto-approval metadata for wu:claim
751
+ *
752
+ * Called by wu:claim to auto-approve agents within policy.
753
+ * Sets approved_by and approved_at, detects escalation triggers.
754
+ *
755
+ * @param {object} wu - WU data
756
+ * @param {string} agentEmail - Email of claiming agent
757
+ * @returns {{approved_by: string[], approved_at: string, escalation_triggers: string[], requires_human_escalation: boolean}}
758
+ */
759
+ export function generateAutoApproval(wu, agentEmail) {
760
+ const triggers = detectEscalationTriggers(wu);
761
+ const now = new Date().toISOString();
762
+ return {
763
+ approved_by: [agentEmail],
764
+ approved_at: now,
765
+ escalation_triggers: triggers,
766
+ requires_human_escalation: triggers.length > 0,
767
+ };
768
+ }
769
+ /**
770
+ * @deprecated Use detectEscalationTriggers instead
771
+ * WU-2079: Legacy function for backwards compatibility
772
+ */
773
+ export function determineRequiredApprovals(wu) {
774
+ const triggers = detectEscalationTriggers(wu);
775
+ return {
776
+ requires_cso_approval: triggers.includes('security_p0') || triggers.includes('sensitive_data'),
777
+ requires_cto_approval: triggers.includes('cross_lane_arch'),
778
+ requires_design_approval: false, // Design no longer requires human escalation
779
+ };
780
+ }
781
+ /**
782
+ * WU-1811: Validates and normalizes WU YAML data with auto-fixable normalisations
783
+ *
784
+ * This function validates the WU YAML schema and applies fixable normalisations:
785
+ * - Trimming whitespace from string fields
786
+ * - Normalizing escaped newlines (\\n → \n)
787
+ * - Splitting embedded newlines in arrays (["a\nb"] → ["a", "b"])
788
+ *
789
+ * Returns:
790
+ * - valid: true if schema validation passes (after normalisations)
791
+ * - normalized: the normalized data (even if validation fails, partial normalization is returned)
792
+ * - errors: validation errors if UnsafeAny
793
+ * - wasNormalized: true if UnsafeAny normalisations were applied
794
+ *
795
+ * @param {unknown} data - Parsed YAML data to validate and normalize
796
+ * @returns {{valid: boolean, normalized: object|null, errors: string[], wasNormalized: boolean}}
797
+ *
798
+ * @example
799
+ * const { valid, normalized, errors, wasNormalized } = validateAndNormalizeWUYAML(yamlData);
800
+ * if (valid && wasNormalized) {
801
+ * // Write normalized data back to YAML file
802
+ * writeWU(wuPath, normalized);
803
+ * }
804
+ * if (!valid) {
805
+ * die(`Validation failed:\n${errors.join('\n')}`);
806
+ * }
807
+ */
808
+ export function validateAndNormalizeWUYAML(data) {
809
+ // First try to parse with schema (which applies normalizations)
810
+ const result = WUSchema.safeParse(data);
811
+ if (!result.success) {
812
+ // Schema validation failed - return errors
813
+ const errors = result.error.issues.map((issue) => `${issue.path.join('.')}: ${issue.message}`);
814
+ return {
815
+ valid: false,
816
+ normalized: null,
817
+ errors,
818
+ wasNormalized: false,
819
+ };
820
+ }
821
+ // Schema passed - check if data was normalized (compare key fields)
822
+ const normalized = result.data;
823
+ const original = typeof data === 'object' && data !== null ? data : {};
824
+ const wasNormalized = detectNormalizationChanges(original, normalized);
825
+ return {
826
+ valid: true,
827
+ normalized,
828
+ errors: [],
829
+ wasNormalized,
830
+ };
831
+ }
832
+ /**
833
+ * WU-1833: Validate WU spec completeness with advisory warnings
834
+ *
835
+ * Provides soft validation that warns (doesn't fail) when recommended fields are missing.
836
+ * Used by wu:validate command to surface quality issues without blocking workflow.
837
+ *
838
+ * Feature and bug WUs should have:
839
+ * - notes (implementation context, deployment instructions)
840
+ * - tests.manual (verification steps)
841
+ * - spec_refs (links to plans, design docs) - for features only
842
+ *
843
+ * @param {object} wu - Validated WU data (must pass WUSchema first)
844
+ * @returns {{warnings: string[]}} Array of warning messages
845
+ *
846
+ * @example
847
+ * const schemaResult = validateWU(data);
848
+ * if (schemaResult.success) {
849
+ * const { warnings } = validateWUCompleteness(schemaResult.data);
850
+ * if (warnings.length > 0) {
851
+ * console.warn('Quality warnings:');
852
+ * warnings.forEach(w => console.warn(` ⚠️ ${w}`));
853
+ * }
854
+ * }
855
+ */
856
+ export function validateWUCompleteness(wu) {
857
+ const warnings = [];
858
+ // WU-1384: Skip completeness checks for terminal WUs (done, cancelled, etc.)
859
+ // These are immutable historical records - enforcing completeness is pointless
860
+ const status = wu.status ?? '';
861
+ const isTerminal = WU_STATUS_GROUPS.TERMINAL.includes(status);
862
+ if (isTerminal) {
863
+ return { warnings };
864
+ }
865
+ const type = isWUType(wu.type) ? wu.type : WU_TYPES.FEATURE;
866
+ // Only feature, bug, and refactor WUs require completeness context.
867
+ const requiresContext = CONTEXT_REQUIRED_TYPES.includes(type);
868
+ if (!requiresContext) {
869
+ return { warnings };
870
+ }
871
+ // Check for notes (implementation context)
872
+ if (!wu.notes || wu.notes.trim().length === 0) {
873
+ warnings.push(`${wu.id}: Missing 'notes' field. Add implementation context, deployment instructions, or plan links.`);
874
+ }
875
+ // Check for manual tests
876
+ const hasManualTests = wu.tests?.manual && wu.tests.manual.length > 0;
877
+ if (!hasManualTests) {
878
+ warnings.push(`${wu.id}: Missing 'tests.manual' field. Add manual verification steps for acceptance criteria.`);
879
+ }
880
+ // Check for spec_refs (features should link to plans/specs)
881
+ // WU-1062: Accepts both repo-relative paths (<configured plansDir>/) and
882
+ // external paths (~/.lumenflow/plans/, $LUMENFLOW_HOME/plans/, lumenflow://plans/)
883
+ if (type === 'feature') {
884
+ const specRefs = wu.spec_refs;
885
+ const hasSpecRefs = !!specRefs && (specRefs.length ?? 0) > 0;
886
+ if (!hasSpecRefs) {
887
+ const plansDirHint = `${createWuPaths().PLANS_DIR().replace(/\/+$/, '')}/`;
888
+ warnings.push(`${wu.id}: Missing 'spec_refs' field. Link to plan file (${plansDirHint}, lumenflow://plans/, or ~/.lumenflow/plans/) for traceability.`);
889
+ }
890
+ }
891
+ return { warnings };
892
+ }
893
+ /**
894
+ * WU-1811: Detect if normalizations were applied by comparing original and normalized data
895
+ *
896
+ * Compares fields that are commonly normalized:
897
+ * - description (escaped newlines)
898
+ * - code_paths (embedded newlines split)
899
+ * - acceptance (embedded newlines split)
900
+ *
901
+ * @param {object} original - Original parsed YAML data
902
+ * @param {object} normalized - Schema-normalized data
903
+ * @returns {boolean} True if UnsafeAny normalisations were applied
904
+ */
905
+ function detectNormalizationChanges(original, normalized) {
906
+ // Compare description (newline normalization)
907
+ if (original.description !== normalized.description) {
908
+ return true;
909
+ }
910
+ // Compare code_paths (array splitting)
911
+ const origPaths = Array.isArray(original.code_paths) ? original.code_paths : [];
912
+ const normPaths = Array.isArray(normalized.code_paths) ? normalized.code_paths : [];
913
+ if (origPaths.length !== normPaths.length) {
914
+ return true;
915
+ }
916
+ for (let i = 0; i < origPaths.length; i++) {
917
+ if (origPaths[i] !== normPaths[i]) {
918
+ return true;
919
+ }
920
+ }
921
+ // Compare acceptance if both are arrays (most common case)
922
+ if (Array.isArray(original.acceptance) && Array.isArray(normalized.acceptance)) {
923
+ if (original.acceptance.length !== normalized.acceptance.length) {
924
+ return true;
925
+ }
926
+ for (let i = 0; i < original.acceptance.length; i++) {
927
+ if (original.acceptance[i] !== normalized.acceptance[i]) {
928
+ return true;
929
+ }
930
+ }
931
+ }
932
+ return false;
933
+ }
934
+ //# sourceMappingURL=wu-schema.js.map