@lumenflow/core 1.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 (263) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +119 -0
  3. package/dist/active-wu-detector.d.ts +33 -0
  4. package/dist/active-wu-detector.js +106 -0
  5. package/dist/adapters/filesystem-metrics.adapter.d.ts +108 -0
  6. package/dist/adapters/filesystem-metrics.adapter.js +519 -0
  7. package/dist/adapters/terminal-renderer.adapter.d.ts +106 -0
  8. package/dist/adapters/terminal-renderer.adapter.js +337 -0
  9. package/dist/arg-parser.d.ts +63 -0
  10. package/dist/arg-parser.js +560 -0
  11. package/dist/backlog-editor.d.ts +98 -0
  12. package/dist/backlog-editor.js +179 -0
  13. package/dist/backlog-generator.d.ts +111 -0
  14. package/dist/backlog-generator.js +381 -0
  15. package/dist/backlog-parser.d.ts +45 -0
  16. package/dist/backlog-parser.js +102 -0
  17. package/dist/backlog-sync-validator.d.ts +78 -0
  18. package/dist/backlog-sync-validator.js +294 -0
  19. package/dist/branch-drift.d.ts +34 -0
  20. package/dist/branch-drift.js +51 -0
  21. package/dist/cleanup-install-config.d.ts +33 -0
  22. package/dist/cleanup-install-config.js +37 -0
  23. package/dist/cleanup-lock.d.ts +139 -0
  24. package/dist/cleanup-lock.js +313 -0
  25. package/dist/code-path-validator.d.ts +146 -0
  26. package/dist/code-path-validator.js +537 -0
  27. package/dist/code-paths-overlap.d.ts +55 -0
  28. package/dist/code-paths-overlap.js +245 -0
  29. package/dist/commands-logger.d.ts +77 -0
  30. package/dist/commands-logger.js +254 -0
  31. package/dist/commit-message-utils.d.ts +25 -0
  32. package/dist/commit-message-utils.js +41 -0
  33. package/dist/compliance-parser.d.ts +150 -0
  34. package/dist/compliance-parser.js +507 -0
  35. package/dist/constants/backlog-patterns.d.ts +20 -0
  36. package/dist/constants/backlog-patterns.js +23 -0
  37. package/dist/constants/dora-constants.d.ts +49 -0
  38. package/dist/constants/dora-constants.js +53 -0
  39. package/dist/constants/gate-constants.d.ts +15 -0
  40. package/dist/constants/gate-constants.js +15 -0
  41. package/dist/constants/linter-constants.d.ts +16 -0
  42. package/dist/constants/linter-constants.js +16 -0
  43. package/dist/constants/tokenizer-constants.d.ts +15 -0
  44. package/dist/constants/tokenizer-constants.js +15 -0
  45. package/dist/core/scope-checker.d.ts +97 -0
  46. package/dist/core/scope-checker.js +163 -0
  47. package/dist/core/tool-runner.d.ts +161 -0
  48. package/dist/core/tool-runner.js +393 -0
  49. package/dist/core/tool.constants.d.ts +105 -0
  50. package/dist/core/tool.constants.js +101 -0
  51. package/dist/core/tool.schemas.d.ts +226 -0
  52. package/dist/core/tool.schemas.js +226 -0
  53. package/dist/core/worktree-guard.d.ts +130 -0
  54. package/dist/core/worktree-guard.js +242 -0
  55. package/dist/coverage-gate.d.ts +108 -0
  56. package/dist/coverage-gate.js +196 -0
  57. package/dist/date-utils.d.ts +75 -0
  58. package/dist/date-utils.js +140 -0
  59. package/dist/dependency-graph.d.ts +142 -0
  60. package/dist/dependency-graph.js +550 -0
  61. package/dist/dependency-guard.d.ts +54 -0
  62. package/dist/dependency-guard.js +142 -0
  63. package/dist/dependency-validator.d.ts +105 -0
  64. package/dist/dependency-validator.js +154 -0
  65. package/dist/docs-path-validator.d.ts +36 -0
  66. package/dist/docs-path-validator.js +95 -0
  67. package/dist/domain/orchestration.constants.d.ts +99 -0
  68. package/dist/domain/orchestration.constants.js +97 -0
  69. package/dist/domain/orchestration.schemas.d.ts +280 -0
  70. package/dist/domain/orchestration.schemas.js +211 -0
  71. package/dist/domain/orchestration.types.d.ts +133 -0
  72. package/dist/domain/orchestration.types.js +12 -0
  73. package/dist/error-handler.d.ts +116 -0
  74. package/dist/error-handler.js +136 -0
  75. package/dist/file-classifiers.d.ts +62 -0
  76. package/dist/file-classifiers.js +108 -0
  77. package/dist/gates-agent-mode.d.ts +81 -0
  78. package/dist/gates-agent-mode.js +94 -0
  79. package/dist/generate-traceability.d.ts +107 -0
  80. package/dist/generate-traceability.js +411 -0
  81. package/dist/git-adapter.d.ts +395 -0
  82. package/dist/git-adapter.js +649 -0
  83. package/dist/git-staged-validator.d.ts +32 -0
  84. package/dist/git-staged-validator.js +48 -0
  85. package/dist/hardcoded-strings.d.ts +61 -0
  86. package/dist/hardcoded-strings.js +270 -0
  87. package/dist/incremental-lint.d.ts +78 -0
  88. package/dist/incremental-lint.js +129 -0
  89. package/dist/incremental-test.d.ts +39 -0
  90. package/dist/incremental-test.js +61 -0
  91. package/dist/index.d.ts +42 -0
  92. package/dist/index.js +61 -0
  93. package/dist/invariants/check-automated-tests.d.ts +50 -0
  94. package/dist/invariants/check-automated-tests.js +166 -0
  95. package/dist/invariants-runner.d.ts +103 -0
  96. package/dist/invariants-runner.js +527 -0
  97. package/dist/lane-checker.d.ts +50 -0
  98. package/dist/lane-checker.js +319 -0
  99. package/dist/lane-inference.d.ts +39 -0
  100. package/dist/lane-inference.js +195 -0
  101. package/dist/lane-lock.d.ts +211 -0
  102. package/dist/lane-lock.js +474 -0
  103. package/dist/lane-validator.d.ts +48 -0
  104. package/dist/lane-validator.js +114 -0
  105. package/dist/logs-lib.d.ts +104 -0
  106. package/dist/logs-lib.js +207 -0
  107. package/dist/lumenflow-config-schema.d.ts +272 -0
  108. package/dist/lumenflow-config-schema.js +207 -0
  109. package/dist/lumenflow-config.d.ts +95 -0
  110. package/dist/lumenflow-config.js +236 -0
  111. package/dist/manual-test-validator.d.ts +80 -0
  112. package/dist/manual-test-validator.js +200 -0
  113. package/dist/merge-lock.d.ts +115 -0
  114. package/dist/merge-lock.js +251 -0
  115. package/dist/micro-worktree.d.ts +159 -0
  116. package/dist/micro-worktree.js +427 -0
  117. package/dist/migration-deployer.d.ts +69 -0
  118. package/dist/migration-deployer.js +151 -0
  119. package/dist/orchestration-advisory-loader.d.ts +28 -0
  120. package/dist/orchestration-advisory-loader.js +87 -0
  121. package/dist/orchestration-advisory.d.ts +58 -0
  122. package/dist/orchestration-advisory.js +94 -0
  123. package/dist/orchestration-di.d.ts +48 -0
  124. package/dist/orchestration-di.js +57 -0
  125. package/dist/orchestration-rules.d.ts +57 -0
  126. package/dist/orchestration-rules.js +201 -0
  127. package/dist/orphan-detector.d.ts +131 -0
  128. package/dist/orphan-detector.js +226 -0
  129. package/dist/path-classifiers.d.ts +57 -0
  130. package/dist/path-classifiers.js +93 -0
  131. package/dist/piped-command-detector.d.ts +34 -0
  132. package/dist/piped-command-detector.js +64 -0
  133. package/dist/ports/dashboard-renderer.port.d.ts +112 -0
  134. package/dist/ports/dashboard-renderer.port.js +25 -0
  135. package/dist/ports/metrics-collector.port.d.ts +132 -0
  136. package/dist/ports/metrics-collector.port.js +26 -0
  137. package/dist/process-detector.d.ts +84 -0
  138. package/dist/process-detector.js +172 -0
  139. package/dist/prompt-linter.d.ts +72 -0
  140. package/dist/prompt-linter.js +312 -0
  141. package/dist/prompt-monitor.d.ts +15 -0
  142. package/dist/prompt-monitor.js +205 -0
  143. package/dist/rebase-artifact-cleanup.d.ts +145 -0
  144. package/dist/rebase-artifact-cleanup.js +433 -0
  145. package/dist/retry-strategy.d.ts +189 -0
  146. package/dist/retry-strategy.js +283 -0
  147. package/dist/risk-detector.d.ts +108 -0
  148. package/dist/risk-detector.js +252 -0
  149. package/dist/rollback-utils.d.ts +76 -0
  150. package/dist/rollback-utils.js +104 -0
  151. package/dist/section-headings.d.ts +43 -0
  152. package/dist/section-headings.js +49 -0
  153. package/dist/spawn-escalation.d.ts +90 -0
  154. package/dist/spawn-escalation.js +253 -0
  155. package/dist/spawn-monitor.d.ts +229 -0
  156. package/dist/spawn-monitor.js +672 -0
  157. package/dist/spawn-recovery.d.ts +82 -0
  158. package/dist/spawn-recovery.js +298 -0
  159. package/dist/spawn-registry-schema.d.ts +98 -0
  160. package/dist/spawn-registry-schema.js +108 -0
  161. package/dist/spawn-registry-store.d.ts +146 -0
  162. package/dist/spawn-registry-store.js +273 -0
  163. package/dist/spawn-tree.d.ts +121 -0
  164. package/dist/spawn-tree.js +285 -0
  165. package/dist/stamp-status-validator.d.ts +84 -0
  166. package/dist/stamp-status-validator.js +134 -0
  167. package/dist/stamp-utils.d.ts +100 -0
  168. package/dist/stamp-utils.js +229 -0
  169. package/dist/state-machine.d.ts +26 -0
  170. package/dist/state-machine.js +83 -0
  171. package/dist/system-map-validator.d.ts +80 -0
  172. package/dist/system-map-validator.js +272 -0
  173. package/dist/telemetry.d.ts +80 -0
  174. package/dist/telemetry.js +213 -0
  175. package/dist/token-counter.d.ts +51 -0
  176. package/dist/token-counter.js +145 -0
  177. package/dist/usecases/get-dashboard-data.usecase.d.ts +52 -0
  178. package/dist/usecases/get-dashboard-data.usecase.js +61 -0
  179. package/dist/usecases/get-suggestions.usecase.d.ts +100 -0
  180. package/dist/usecases/get-suggestions.usecase.js +153 -0
  181. package/dist/user-normalizer.d.ts +41 -0
  182. package/dist/user-normalizer.js +141 -0
  183. package/dist/validators/phi-constants.d.ts +97 -0
  184. package/dist/validators/phi-constants.js +152 -0
  185. package/dist/validators/phi-scanner.d.ts +58 -0
  186. package/dist/validators/phi-scanner.js +215 -0
  187. package/dist/worktree-ownership.d.ts +50 -0
  188. package/dist/worktree-ownership.js +74 -0
  189. package/dist/worktree-scanner.d.ts +103 -0
  190. package/dist/worktree-scanner.js +168 -0
  191. package/dist/worktree-symlink.d.ts +99 -0
  192. package/dist/worktree-symlink.js +359 -0
  193. package/dist/wu-backlog-updater.d.ts +17 -0
  194. package/dist/wu-backlog-updater.js +37 -0
  195. package/dist/wu-checkpoint.d.ts +124 -0
  196. package/dist/wu-checkpoint.js +233 -0
  197. package/dist/wu-claim-helpers.d.ts +26 -0
  198. package/dist/wu-claim-helpers.js +63 -0
  199. package/dist/wu-claim-resume.d.ts +106 -0
  200. package/dist/wu-claim-resume.js +276 -0
  201. package/dist/wu-consistency-checker.d.ts +95 -0
  202. package/dist/wu-consistency-checker.js +567 -0
  203. package/dist/wu-constants.d.ts +1275 -0
  204. package/dist/wu-constants.js +1382 -0
  205. package/dist/wu-create-validators.d.ts +42 -0
  206. package/dist/wu-create-validators.js +93 -0
  207. package/dist/wu-done-branch-only.d.ts +63 -0
  208. package/dist/wu-done-branch-only.js +191 -0
  209. package/dist/wu-done-messages.d.ts +119 -0
  210. package/dist/wu-done-messages.js +185 -0
  211. package/dist/wu-done-pr.d.ts +72 -0
  212. package/dist/wu-done-pr.js +174 -0
  213. package/dist/wu-done-retry-helpers.d.ts +85 -0
  214. package/dist/wu-done-retry-helpers.js +172 -0
  215. package/dist/wu-done-ui.d.ts +37 -0
  216. package/dist/wu-done-ui.js +69 -0
  217. package/dist/wu-done-validators.d.ts +411 -0
  218. package/dist/wu-done-validators.js +1229 -0
  219. package/dist/wu-done-worktree.d.ts +182 -0
  220. package/dist/wu-done-worktree.js +1097 -0
  221. package/dist/wu-helpers.d.ts +128 -0
  222. package/dist/wu-helpers.js +248 -0
  223. package/dist/wu-lint.d.ts +70 -0
  224. package/dist/wu-lint.js +234 -0
  225. package/dist/wu-paths.d.ts +171 -0
  226. package/dist/wu-paths.js +178 -0
  227. package/dist/wu-preflight-validators.d.ts +86 -0
  228. package/dist/wu-preflight-validators.js +251 -0
  229. package/dist/wu-recovery.d.ts +138 -0
  230. package/dist/wu-recovery.js +341 -0
  231. package/dist/wu-repair-core.d.ts +131 -0
  232. package/dist/wu-repair-core.js +669 -0
  233. package/dist/wu-schema-normalization.d.ts +17 -0
  234. package/dist/wu-schema-normalization.js +82 -0
  235. package/dist/wu-schema.d.ts +793 -0
  236. package/dist/wu-schema.js +881 -0
  237. package/dist/wu-spawn-helpers.d.ts +121 -0
  238. package/dist/wu-spawn-helpers.js +271 -0
  239. package/dist/wu-spawn.d.ts +158 -0
  240. package/dist/wu-spawn.js +1306 -0
  241. package/dist/wu-state-schema.d.ts +213 -0
  242. package/dist/wu-state-schema.js +156 -0
  243. package/dist/wu-state-store.d.ts +264 -0
  244. package/dist/wu-state-store.js +691 -0
  245. package/dist/wu-status-transition.d.ts +63 -0
  246. package/dist/wu-status-transition.js +382 -0
  247. package/dist/wu-status-updater.d.ts +25 -0
  248. package/dist/wu-status-updater.js +116 -0
  249. package/dist/wu-transaction-collectors.d.ts +116 -0
  250. package/dist/wu-transaction-collectors.js +272 -0
  251. package/dist/wu-transaction.d.ts +170 -0
  252. package/dist/wu-transaction.js +273 -0
  253. package/dist/wu-validation-constants.d.ts +60 -0
  254. package/dist/wu-validation-constants.js +66 -0
  255. package/dist/wu-validation.d.ts +118 -0
  256. package/dist/wu-validation.js +243 -0
  257. package/dist/wu-validator.d.ts +62 -0
  258. package/dist/wu-validator.js +325 -0
  259. package/dist/wu-yaml-fixer.d.ts +97 -0
  260. package/dist/wu-yaml-fixer.js +264 -0
  261. package/dist/wu-yaml.d.ts +86 -0
  262. package/dist/wu-yaml.js +222 -0
  263. package/package.json +114 -0
@@ -0,0 +1,213 @@
1
+ /**
2
+ * WU State Schema (WU-1570)
3
+ *
4
+ * Zod schemas for WU state event validation.
5
+ * Defines event types for WU lifecycle: create, claim, block, unblock, complete.
6
+ *
7
+ * @see {@link tools/lib/__tests__/wu-state-store.test.mjs} - Tests
8
+ */
9
+ import { z } from 'zod';
10
+ /**
11
+ * WU event types
12
+ *
13
+ * - create: WU created (transitions to ready)
14
+ * - claim: WU claimed (transitions to in_progress)
15
+ * - block: WU blocked (transitions to blocked)
16
+ * - unblock: WU unblocked (transitions back to in_progress)
17
+ * - complete: WU completed (transitions to done)
18
+ * - checkpoint: Progress checkpoint (WU-1748: cross-agent visibility)
19
+ * - spawn: WU spawned from parent (WU-1947: parent-child relationships)
20
+ */
21
+ export declare const WU_EVENT_TYPES: readonly ["create", "claim", "block", "unblock", "complete", "checkpoint", "spawn"];
22
+ /** Type for WU event types */
23
+ export type WUEventType = (typeof WU_EVENT_TYPES)[number];
24
+ /**
25
+ * WU status values (matches LumenFlow state machine)
26
+ */
27
+ export declare const WU_STATUSES: readonly ["ready", "in_progress", "blocked", "waiting", "done"];
28
+ /** Type for WU status values */
29
+ export type WUStatus = (typeof WU_STATUSES)[number];
30
+ /**
31
+ * Regex patterns for WU validation
32
+ */
33
+ export declare const WU_PATTERNS: {
34
+ /** WU ID format: WU-{digits} */
35
+ WU_ID: RegExp;
36
+ };
37
+ /**
38
+ * Create event schema
39
+ */
40
+ export declare const CreateEventSchema: z.ZodObject<{
41
+ wuId: z.ZodString;
42
+ timestamp: z.ZodString;
43
+ type: z.ZodLiteral<"create">;
44
+ lane: z.ZodString;
45
+ title: z.ZodString;
46
+ }, z.core.$strip>;
47
+ /**
48
+ * Claim event schema
49
+ */
50
+ export declare const ClaimEventSchema: z.ZodObject<{
51
+ wuId: z.ZodString;
52
+ timestamp: z.ZodString;
53
+ type: z.ZodLiteral<"claim">;
54
+ lane: z.ZodString;
55
+ title: z.ZodString;
56
+ }, z.core.$strip>;
57
+ /**
58
+ * Block event schema
59
+ */
60
+ export declare const BlockEventSchema: z.ZodObject<{
61
+ wuId: z.ZodString;
62
+ timestamp: z.ZodString;
63
+ type: z.ZodLiteral<"block">;
64
+ reason: z.ZodString;
65
+ }, z.core.$strip>;
66
+ /**
67
+ * Unblock event schema
68
+ */
69
+ export declare const UnblockEventSchema: z.ZodObject<{
70
+ wuId: z.ZodString;
71
+ timestamp: z.ZodString;
72
+ type: z.ZodLiteral<"unblock">;
73
+ }, z.core.$strip>;
74
+ /**
75
+ * Complete event schema
76
+ */
77
+ export declare const CompleteEventSchema: z.ZodObject<{
78
+ wuId: z.ZodString;
79
+ timestamp: z.ZodString;
80
+ type: z.ZodLiteral<"complete">;
81
+ }, z.core.$strip>;
82
+ /**
83
+ * Checkpoint event schema (WU-1748: cross-agent visibility)
84
+ * Records progress checkpoints for abandoned WU detection
85
+ */
86
+ export declare const CheckpointEventSchema: z.ZodObject<{
87
+ wuId: z.ZodString;
88
+ timestamp: z.ZodString;
89
+ type: z.ZodLiteral<"checkpoint">;
90
+ note: z.ZodString;
91
+ sessionId: z.ZodOptional<z.ZodString>;
92
+ progress: z.ZodOptional<z.ZodString>;
93
+ nextSteps: z.ZodOptional<z.ZodString>;
94
+ }, z.core.$strip>;
95
+ /**
96
+ * Spawn event schema (WU-1947: parent-child relationships)
97
+ * Records WU spawn relationships for tracking parent-child WUs
98
+ */
99
+ export declare const SpawnEventSchema: z.ZodObject<{
100
+ wuId: z.ZodString;
101
+ timestamp: z.ZodString;
102
+ type: z.ZodLiteral<"spawn">;
103
+ parentWuId: z.ZodString;
104
+ spawnId: z.ZodString;
105
+ }, z.core.$strip>;
106
+ /**
107
+ * Union schema for all event types
108
+ */
109
+ export declare const WUEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
110
+ wuId: z.ZodString;
111
+ timestamp: z.ZodString;
112
+ type: z.ZodLiteral<"create">;
113
+ lane: z.ZodString;
114
+ title: z.ZodString;
115
+ }, z.core.$strip>, z.ZodObject<{
116
+ wuId: z.ZodString;
117
+ timestamp: z.ZodString;
118
+ type: z.ZodLiteral<"claim">;
119
+ lane: z.ZodString;
120
+ title: z.ZodString;
121
+ }, z.core.$strip>, z.ZodObject<{
122
+ wuId: z.ZodString;
123
+ timestamp: z.ZodString;
124
+ type: z.ZodLiteral<"block">;
125
+ reason: z.ZodString;
126
+ }, z.core.$strip>, z.ZodObject<{
127
+ wuId: z.ZodString;
128
+ timestamp: z.ZodString;
129
+ type: z.ZodLiteral<"unblock">;
130
+ }, z.core.$strip>, z.ZodObject<{
131
+ wuId: z.ZodString;
132
+ timestamp: z.ZodString;
133
+ type: z.ZodLiteral<"complete">;
134
+ }, z.core.$strip>, z.ZodObject<{
135
+ wuId: z.ZodString;
136
+ timestamp: z.ZodString;
137
+ type: z.ZodLiteral<"checkpoint">;
138
+ note: z.ZodString;
139
+ sessionId: z.ZodOptional<z.ZodString>;
140
+ progress: z.ZodOptional<z.ZodString>;
141
+ nextSteps: z.ZodOptional<z.ZodString>;
142
+ }, z.core.$strip>, z.ZodObject<{
143
+ wuId: z.ZodString;
144
+ timestamp: z.ZodString;
145
+ type: z.ZodLiteral<"spawn">;
146
+ parentWuId: z.ZodString;
147
+ spawnId: z.ZodString;
148
+ }, z.core.$strip>], "type">;
149
+ /**
150
+ * TypeScript types inferred from schemas
151
+ */
152
+ export type CreateEvent = z.infer<typeof CreateEventSchema>;
153
+ export type ClaimEvent = z.infer<typeof ClaimEventSchema>;
154
+ export type BlockEvent = z.infer<typeof BlockEventSchema>;
155
+ export type UnblockEvent = z.infer<typeof UnblockEventSchema>;
156
+ export type CompleteEvent = z.infer<typeof CompleteEventSchema>;
157
+ export type CheckpointEvent = z.infer<typeof CheckpointEventSchema>;
158
+ export type SpawnEvent = z.infer<typeof SpawnEventSchema>;
159
+ export type WUEvent = z.infer<typeof WUEventSchema>;
160
+ /**
161
+ * Validates WU event data against schema
162
+ *
163
+ * @param {unknown} data - Data to validate
164
+ * @returns {z.SafeParseReturnType<WUEvent, WUEvent>} Validation result
165
+ *
166
+ * @example
167
+ * const result = validateWUEvent(eventData);
168
+ * if (!result.success) {
169
+ * result.error.issues.forEach(issue => {
170
+ * console.error(`${issue.path.join('.')}: ${issue.message}`);
171
+ * });
172
+ * }
173
+ */
174
+ export declare function validateWUEvent(data: any): z.ZodSafeParseResult<{
175
+ wuId: string;
176
+ timestamp: string;
177
+ type: "create";
178
+ lane: string;
179
+ title: string;
180
+ } | {
181
+ wuId: string;
182
+ timestamp: string;
183
+ type: "claim";
184
+ lane: string;
185
+ title: string;
186
+ } | {
187
+ wuId: string;
188
+ timestamp: string;
189
+ type: "block";
190
+ reason: string;
191
+ } | {
192
+ wuId: string;
193
+ timestamp: string;
194
+ type: "unblock";
195
+ } | {
196
+ wuId: string;
197
+ timestamp: string;
198
+ type: "complete";
199
+ } | {
200
+ wuId: string;
201
+ timestamp: string;
202
+ type: "checkpoint";
203
+ note: string;
204
+ sessionId?: string;
205
+ progress?: string;
206
+ nextSteps?: string;
207
+ } | {
208
+ wuId: string;
209
+ timestamp: string;
210
+ type: "spawn";
211
+ parentWuId: string;
212
+ spawnId: string;
213
+ }>;
@@ -0,0 +1,156 @@
1
+ /**
2
+ * WU State Schema (WU-1570)
3
+ *
4
+ * Zod schemas for WU state event validation.
5
+ * Defines event types for WU lifecycle: create, claim, block, unblock, complete.
6
+ *
7
+ * @see {@link tools/lib/__tests__/wu-state-store.test.mjs} - Tests
8
+ */
9
+ import { z } from 'zod';
10
+ /**
11
+ * WU event types
12
+ *
13
+ * - create: WU created (transitions to ready)
14
+ * - claim: WU claimed (transitions to in_progress)
15
+ * - block: WU blocked (transitions to blocked)
16
+ * - unblock: WU unblocked (transitions back to in_progress)
17
+ * - complete: WU completed (transitions to done)
18
+ * - checkpoint: Progress checkpoint (WU-1748: cross-agent visibility)
19
+ * - spawn: WU spawned from parent (WU-1947: parent-child relationships)
20
+ */
21
+ export const WU_EVENT_TYPES = [
22
+ 'create',
23
+ 'claim',
24
+ 'block',
25
+ 'unblock',
26
+ 'complete',
27
+ 'checkpoint',
28
+ 'spawn',
29
+ ];
30
+ /**
31
+ * WU status values (matches LumenFlow state machine)
32
+ */
33
+ export const WU_STATUSES = ['ready', 'in_progress', 'blocked', 'waiting', 'done'];
34
+ /**
35
+ * Regex patterns for WU validation
36
+ */
37
+ export const WU_PATTERNS = {
38
+ /** WU ID format: WU-{digits} */
39
+ WU_ID: /^WU-\d+$/,
40
+ };
41
+ /**
42
+ * Error messages for schema validation
43
+ */
44
+ const ERROR_MESSAGES = {
45
+ EVENT_TYPE: `Event type must be one of: ${WU_EVENT_TYPES.join(', ')}`,
46
+ WU_ID: 'WU ID must match pattern WU-XXX (e.g., WU-1570)',
47
+ LANE_REQUIRED: 'Lane is required',
48
+ TITLE_REQUIRED: 'Title is required',
49
+ REASON_REQUIRED: 'Reason is required',
50
+ TIMESTAMP_REQUIRED: 'Timestamp is required',
51
+ };
52
+ /**
53
+ * Base event schema (common fields for all events)
54
+ */
55
+ const BaseEventSchema = z.object({
56
+ /** Event type */
57
+ type: z.enum(WU_EVENT_TYPES, {
58
+ error: ERROR_MESSAGES.EVENT_TYPE,
59
+ }),
60
+ /** WU ID */
61
+ wuId: z.string().regex(WU_PATTERNS.WU_ID, { message: ERROR_MESSAGES.WU_ID }),
62
+ /** Event timestamp (ISO 8601 datetime) */
63
+ timestamp: z.string().datetime({ message: ERROR_MESSAGES.TIMESTAMP_REQUIRED }),
64
+ });
65
+ /**
66
+ * Create event schema
67
+ */
68
+ export const CreateEventSchema = BaseEventSchema.extend({
69
+ type: z.literal('create'),
70
+ lane: z.string().min(1, { message: ERROR_MESSAGES.LANE_REQUIRED }),
71
+ title: z.string().min(1, { message: ERROR_MESSAGES.TITLE_REQUIRED }),
72
+ });
73
+ /**
74
+ * Claim event schema
75
+ */
76
+ export const ClaimEventSchema = BaseEventSchema.extend({
77
+ type: z.literal('claim'),
78
+ lane: z.string().min(1, { message: ERROR_MESSAGES.LANE_REQUIRED }),
79
+ title: z.string().min(1, { message: ERROR_MESSAGES.TITLE_REQUIRED }),
80
+ });
81
+ /**
82
+ * Block event schema
83
+ */
84
+ export const BlockEventSchema = BaseEventSchema.extend({
85
+ type: z.literal('block'),
86
+ reason: z.string().min(1, { message: ERROR_MESSAGES.REASON_REQUIRED }),
87
+ });
88
+ /**
89
+ * Unblock event schema
90
+ */
91
+ export const UnblockEventSchema = BaseEventSchema.extend({
92
+ type: z.literal('unblock'),
93
+ });
94
+ /**
95
+ * Complete event schema
96
+ */
97
+ export const CompleteEventSchema = BaseEventSchema.extend({
98
+ type: z.literal('complete'),
99
+ });
100
+ /**
101
+ * Checkpoint event schema (WU-1748: cross-agent visibility)
102
+ * Records progress checkpoints for abandoned WU detection
103
+ */
104
+ export const CheckpointEventSchema = BaseEventSchema.extend({
105
+ type: z.literal('checkpoint'),
106
+ /** Checkpoint note/description */
107
+ note: z.string().min(1, { message: 'Checkpoint note is required' }),
108
+ /** Optional session ID */
109
+ sessionId: z.string().optional(),
110
+ /** Optional progress summary */
111
+ progress: z.string().optional(),
112
+ /** Optional next steps */
113
+ nextSteps: z.string().optional(),
114
+ });
115
+ /**
116
+ * Spawn event schema (WU-1947: parent-child relationships)
117
+ * Records WU spawn relationships for tracking parent-child WUs
118
+ */
119
+ export const SpawnEventSchema = BaseEventSchema.extend({
120
+ type: z.literal('spawn'),
121
+ /** Parent WU ID that spawned this WU */
122
+ parentWuId: z
123
+ .string()
124
+ .regex(WU_PATTERNS.WU_ID, { message: 'Parent WU ID must match pattern WU-XXX' }),
125
+ /** Unique spawn identifier */
126
+ spawnId: z.string().min(1, { message: 'Spawn ID is required' }),
127
+ });
128
+ /**
129
+ * Union schema for all event types
130
+ */
131
+ export const WUEventSchema = z.discriminatedUnion('type', [
132
+ CreateEventSchema,
133
+ ClaimEventSchema,
134
+ BlockEventSchema,
135
+ UnblockEventSchema,
136
+ CompleteEventSchema,
137
+ CheckpointEventSchema,
138
+ SpawnEventSchema,
139
+ ]);
140
+ /**
141
+ * Validates WU event data against schema
142
+ *
143
+ * @param {unknown} data - Data to validate
144
+ * @returns {z.SafeParseReturnType<WUEvent, WUEvent>} Validation result
145
+ *
146
+ * @example
147
+ * const result = validateWUEvent(eventData);
148
+ * if (!result.success) {
149
+ * result.error.issues.forEach(issue => {
150
+ * console.error(`${issue.path.join('.')}: ${issue.message}`);
151
+ * });
152
+ * }
153
+ */
154
+ export function validateWUEvent(data) {
155
+ return WUEventSchema.safeParse(data);
156
+ }
@@ -0,0 +1,264 @@
1
+ /**
2
+ * WU State Store (WU-1570, WU-2240)
3
+ *
4
+ * Event-sourced state store for WU lifecycle following INIT-007 pattern.
5
+ * Stores events in .beacon/state/wu-events.jsonl (append-only, git-friendly).
6
+ *
7
+ * Features:
8
+ * - Event sourcing with replay for current state
9
+ * - Atomic append operations (WU-2240: temp file + fsync + rename)
10
+ * - O(1) queries by status and lane via in-memory indexes
11
+ * - State machine validation for legal transitions
12
+ * - File locking with stale detection (WU-2240)
13
+ * - Corruption recovery via repairStateFile (WU-2240)
14
+ *
15
+ * @see {@link tools/__tests__/state-store-concurrent.test.mjs} - Concurrent access tests
16
+ * @see {@link tools/lib/wu-state-schema.mjs} - Schema definitions
17
+ */
18
+ import { type WUEvent } from './wu-state-schema.js';
19
+ /**
20
+ * WU events file name constant
21
+ */
22
+ export declare const WU_EVENTS_FILE_NAME = "wu-events.jsonl";
23
+ /**
24
+ * WU state entry in the in-memory store
25
+ */
26
+ export interface WUStateEntry {
27
+ status: string;
28
+ lane: string;
29
+ title: string;
30
+ completedAt?: string;
31
+ lastCheckpoint?: string;
32
+ lastCheckpointNote?: string;
33
+ }
34
+ /**
35
+ * Lock file data structure
36
+ */
37
+ export interface LockData {
38
+ pid: number;
39
+ timestamp: number;
40
+ hostname: string;
41
+ }
42
+ /**
43
+ * Checkpoint options
44
+ */
45
+ export interface CheckpointOptions {
46
+ sessionId?: string;
47
+ progress?: string;
48
+ nextSteps?: string;
49
+ }
50
+ /**
51
+ * Repair result
52
+ */
53
+ export interface RepairResult {
54
+ success: boolean;
55
+ linesKept: number;
56
+ linesRemoved: number;
57
+ backupPath: string | null;
58
+ warnings: string[];
59
+ }
60
+ /**
61
+ * WU State Store class
62
+ *
63
+ * Manages WU lifecycle state via event sourcing pattern.
64
+ * Events are appended to JSONL file, state is rebuilt by replaying events.
65
+ */
66
+ export declare class WUStateStore {
67
+ private readonly baseDir;
68
+ private readonly eventsFilePath;
69
+ private wuState;
70
+ private byStatus;
71
+ private byLane;
72
+ private byParent;
73
+ constructor(baseDir: string);
74
+ /**
75
+ * Loads and replays events from JSONL file into current state.
76
+ *
77
+ * Handles:
78
+ * - Missing file: returns empty state
79
+ * - Empty file: returns empty state
80
+ * - Empty lines: skipped gracefully
81
+ * - Malformed JSON: throws error with line info
82
+ * - Invalid events: throws validation error
83
+ *
84
+ * @throws Error If file contains malformed JSON or invalid events
85
+ *
86
+ * @example
87
+ * const store = new WUStateStore('/path/to/project');
88
+ * await store.load();
89
+ * const inProgress = store.getByStatus('in_progress');
90
+ */
91
+ load(): Promise<void>;
92
+ /**
93
+ * Transition WU to a new status if it exists.
94
+ */
95
+ private _transitionToStatus;
96
+ /**
97
+ * Applies an event to the in-memory state.
98
+ */
99
+ private _applyEvent;
100
+ /**
101
+ * Sets WU state and updates indexes.
102
+ */
103
+ private _setState;
104
+ /**
105
+ * Appends an event to the events file.
106
+ *
107
+ * Uses append mode to avoid full file rewrite.
108
+ * Creates file and parent directories if they don't exist.
109
+ * Validates event before appending.
110
+ *
111
+ * @throws Error If event fails validation
112
+ */
113
+ private _appendEvent;
114
+ /**
115
+ * Claims a WU (transitions to in_progress).
116
+ *
117
+ * @throws Error If WU is already in_progress
118
+ *
119
+ * @example
120
+ * await store.claim('WU-1570', 'Operations: Tooling', 'Test WU');
121
+ */
122
+ claim(wuId: string, lane: string, title: string): Promise<void>;
123
+ /**
124
+ * Completes a WU (transitions to done).
125
+ *
126
+ * @throws Error If WU is not in_progress
127
+ *
128
+ * @example
129
+ * await store.complete('WU-1570');
130
+ */
131
+ complete(wuId: string): Promise<void>;
132
+ /**
133
+ * Get current in-memory state for a WU.
134
+ */
135
+ getWUState(wuId: string): WUStateEntry | undefined;
136
+ /**
137
+ * Create a complete event without writing to disk.
138
+ *
139
+ * Used by transactional flows where event log writes are staged and committed atomically.
140
+ *
141
+ * @throws Error If WU is not in_progress or event fails validation
142
+ */
143
+ createCompleteEvent(wuId: string, timestamp?: string): WUEvent;
144
+ /**
145
+ * Apply a validated event to in-memory state without writing to disk.
146
+ *
147
+ * @throws Error If event fails validation
148
+ */
149
+ applyEvent(event: WUEvent): void;
150
+ /**
151
+ * Blocks a WU (transitions to blocked).
152
+ *
153
+ * @throws Error If WU is not in_progress
154
+ *
155
+ * @example
156
+ * await store.block('WU-1570', 'Blocked by dependency');
157
+ */
158
+ block(wuId: string, reason: string): Promise<void>;
159
+ /**
160
+ * Unblocks a WU (transitions back to in_progress).
161
+ *
162
+ * @throws Error If WU is not blocked
163
+ *
164
+ * @example
165
+ * await store.unblock('WU-1570');
166
+ */
167
+ unblock(wuId: string): Promise<void>;
168
+ /**
169
+ * Records a checkpoint for a WU (WU-1748: cross-agent visibility).
170
+ *
171
+ * Checkpoints are recorded for visibility but don't change WU state.
172
+ * Used to track progress and detect abandoned WUs.
173
+ *
174
+ * @example
175
+ * await store.checkpoint('WU-1748', 'Completed worktree scanner', {
176
+ * progress: 'Scanner implemented and tests passing',
177
+ * nextSteps: 'Integrate into orchestrate:status'
178
+ * });
179
+ */
180
+ checkpoint(wuId: string, note: string, options?: CheckpointOptions): Promise<void>;
181
+ /**
182
+ * Gets WU IDs by status (O(1) lookup).
183
+ *
184
+ * @example
185
+ * const inProgress = store.getByStatus('in_progress');
186
+ * for (const wuId of inProgress) {
187
+ * console.log(wuId);
188
+ * }
189
+ */
190
+ getByStatus(status: string): Set<string>;
191
+ /**
192
+ * Gets WU IDs by lane (O(1) lookup).
193
+ *
194
+ * @example
195
+ * const tooling = store.getByLane('Operations: Tooling');
196
+ * for (const wuId of tooling) {
197
+ * console.log(wuId);
198
+ * }
199
+ */
200
+ getByLane(lane: string): Set<string>;
201
+ /**
202
+ * Gets child WU IDs spawned from a parent WU (O(1) lookup).
203
+ * WU-1947: Parent-child relationship tracking.
204
+ *
205
+ * @example
206
+ * const children = store.getChildWUs('WU-100');
207
+ * for (const childId of children) {
208
+ * console.log(`Child WU: ${childId}`);
209
+ * }
210
+ */
211
+ getChildWUs(parentWuId: string): Set<string>;
212
+ /**
213
+ * Records a spawn relationship between parent and child WUs.
214
+ * WU-1947: Parent-child relationship tracking.
215
+ *
216
+ * @example
217
+ * await store.spawn('WU-200', 'WU-100', 'spawn-abc123');
218
+ */
219
+ spawn(childWuId: string, parentWuId: string, spawnId: string): Promise<void>;
220
+ }
221
+ /**
222
+ * Check if a lock is stale (expired or dead process)
223
+ *
224
+ * WU-2240: Prepared for proper-lockfile integration
225
+ */
226
+ export declare function isLockStale(lockData: LockData): boolean;
227
+ /**
228
+ * Acquire a file lock for the events file
229
+ *
230
+ * Uses a JSON lock file containing PID, timestamp, and hostname.
231
+ * Implements stale lock detection via:
232
+ * - PID check (on same host)
233
+ * - 5-minute timeout (across hosts)
234
+ *
235
+ * WU-2240: Prepared for proper-lockfile integration
236
+ *
237
+ * @throws Error If lock cannot be acquired after retries
238
+ */
239
+ export declare function acquireLock(lockPath: string): Promise<void>;
240
+ /**
241
+ * Release a file lock
242
+ *
243
+ * WU-2240: Prepared for proper-lockfile integration
244
+ */
245
+ export declare function releaseLock(lockPath: string): void;
246
+ /**
247
+ * Repair a corrupted state file by removing invalid lines.
248
+ *
249
+ * WU-2240: Corruption recovery for wu-events.jsonl
250
+ *
251
+ * Features:
252
+ * - Creates backup before repair
253
+ * - Removes malformed JSON lines
254
+ * - Removes lines that fail schema validation
255
+ * - Returns detailed repair statistics
256
+ *
257
+ * @example
258
+ * const stateFilePath = path.join(process.cwd(), '.beacon', 'state', 'wu-events.jsonl');
259
+ * const result = await repairStateFile(stateFilePath);
260
+ * if (result.success) {
261
+ * console.log(`Repaired: kept ${result.linesKept}, removed ${result.linesRemoved}`);
262
+ * }
263
+ */
264
+ export declare function repairStateFile(filePath: string): Promise<RepairResult>;