popeye-cli 1.10.0 → 2.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 (253) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/CONTRIBUTING.md +15 -1
  3. package/README.md +57 -0
  4. package/dist/pipeline/artifact-manager.d.ts +47 -0
  5. package/dist/pipeline/artifact-manager.d.ts.map +1 -0
  6. package/dist/pipeline/artifact-manager.js +251 -0
  7. package/dist/pipeline/artifact-manager.js.map +1 -0
  8. package/dist/pipeline/artifact-validators.d.ts +29 -0
  9. package/dist/pipeline/artifact-validators.d.ts.map +1 -0
  10. package/dist/pipeline/artifact-validators.js +173 -0
  11. package/dist/pipeline/artifact-validators.js.map +1 -0
  12. package/dist/pipeline/change-request.d.ts +47 -0
  13. package/dist/pipeline/change-request.d.ts.map +1 -0
  14. package/dist/pipeline/change-request.js +91 -0
  15. package/dist/pipeline/change-request.js.map +1 -0
  16. package/dist/pipeline/check-runner.d.ts +47 -0
  17. package/dist/pipeline/check-runner.d.ts.map +1 -0
  18. package/dist/pipeline/check-runner.js +417 -0
  19. package/dist/pipeline/check-runner.js.map +1 -0
  20. package/dist/pipeline/command-resolver.d.ts +9 -0
  21. package/dist/pipeline/command-resolver.d.ts.map +1 -0
  22. package/dist/pipeline/command-resolver.js +140 -0
  23. package/dist/pipeline/command-resolver.js.map +1 -0
  24. package/dist/pipeline/consensus/consensus-runner.d.ts +44 -0
  25. package/dist/pipeline/consensus/consensus-runner.d.ts.map +1 -0
  26. package/dist/pipeline/consensus/consensus-runner.js +212 -0
  27. package/dist/pipeline/consensus/consensus-runner.js.map +1 -0
  28. package/dist/pipeline/constitution.d.ts +45 -0
  29. package/dist/pipeline/constitution.d.ts.map +1 -0
  30. package/dist/pipeline/constitution.js +82 -0
  31. package/dist/pipeline/constitution.js.map +1 -0
  32. package/dist/pipeline/gate-engine.d.ts +55 -0
  33. package/dist/pipeline/gate-engine.d.ts.map +1 -0
  34. package/dist/pipeline/gate-engine.js +270 -0
  35. package/dist/pipeline/gate-engine.js.map +1 -0
  36. package/dist/pipeline/index.d.ts +26 -0
  37. package/dist/pipeline/index.d.ts.map +1 -0
  38. package/dist/pipeline/index.js +35 -0
  39. package/dist/pipeline/index.js.map +1 -0
  40. package/dist/pipeline/migration.d.ts +15 -0
  41. package/dist/pipeline/migration.d.ts.map +1 -0
  42. package/dist/pipeline/migration.js +76 -0
  43. package/dist/pipeline/migration.js.map +1 -0
  44. package/dist/pipeline/orchestrator.d.ts +28 -0
  45. package/dist/pipeline/orchestrator.d.ts.map +1 -0
  46. package/dist/pipeline/orchestrator.js +238 -0
  47. package/dist/pipeline/orchestrator.js.map +1 -0
  48. package/dist/pipeline/packets/audit-report-builder.d.ts +11 -0
  49. package/dist/pipeline/packets/audit-report-builder.d.ts.map +1 -0
  50. package/dist/pipeline/packets/audit-report-builder.js +32 -0
  51. package/dist/pipeline/packets/audit-report-builder.js.map +1 -0
  52. package/dist/pipeline/packets/consensus-packet-builder.d.ts +35 -0
  53. package/dist/pipeline/packets/consensus-packet-builder.d.ts.map +1 -0
  54. package/dist/pipeline/packets/consensus-packet-builder.js +80 -0
  55. package/dist/pipeline/packets/consensus-packet-builder.js.map +1 -0
  56. package/dist/pipeline/packets/index.d.ts +12 -0
  57. package/dist/pipeline/packets/index.d.ts.map +1 -0
  58. package/dist/pipeline/packets/index.js +8 -0
  59. package/dist/pipeline/packets/index.js.map +1 -0
  60. package/dist/pipeline/packets/plan-packet-builder.d.ts +21 -0
  61. package/dist/pipeline/packets/plan-packet-builder.d.ts.map +1 -0
  62. package/dist/pipeline/packets/plan-packet-builder.js +27 -0
  63. package/dist/pipeline/packets/plan-packet-builder.js.map +1 -0
  64. package/dist/pipeline/packets/rca-packet-builder.d.ts +19 -0
  65. package/dist/pipeline/packets/rca-packet-builder.d.ts.map +1 -0
  66. package/dist/pipeline/packets/rca-packet-builder.js +22 -0
  67. package/dist/pipeline/packets/rca-packet-builder.js.map +1 -0
  68. package/dist/pipeline/phases/architecture.d.ts +7 -0
  69. package/dist/pipeline/phases/architecture.d.ts.map +1 -0
  70. package/dist/pipeline/phases/architecture.js +60 -0
  71. package/dist/pipeline/phases/architecture.js.map +1 -0
  72. package/dist/pipeline/phases/audit.d.ts +8 -0
  73. package/dist/pipeline/phases/audit.d.ts.map +1 -0
  74. package/dist/pipeline/phases/audit.js +144 -0
  75. package/dist/pipeline/phases/audit.js.map +1 -0
  76. package/dist/pipeline/phases/consensus-architecture.d.ts +7 -0
  77. package/dist/pipeline/phases/consensus-architecture.d.ts.map +1 -0
  78. package/dist/pipeline/phases/consensus-architecture.js +84 -0
  79. package/dist/pipeline/phases/consensus-architecture.js.map +1 -0
  80. package/dist/pipeline/phases/consensus-master-plan.d.ts +7 -0
  81. package/dist/pipeline/phases/consensus-master-plan.d.ts.map +1 -0
  82. package/dist/pipeline/phases/consensus-master-plan.js +81 -0
  83. package/dist/pipeline/phases/consensus-master-plan.js.map +1 -0
  84. package/dist/pipeline/phases/consensus-role-plans.d.ts +7 -0
  85. package/dist/pipeline/phases/consensus-role-plans.d.ts.map +1 -0
  86. package/dist/pipeline/phases/consensus-role-plans.js +85 -0
  87. package/dist/pipeline/phases/consensus-role-plans.js.map +1 -0
  88. package/dist/pipeline/phases/done.d.ts +7 -0
  89. package/dist/pipeline/phases/done.d.ts.map +1 -0
  90. package/dist/pipeline/phases/done.js +45 -0
  91. package/dist/pipeline/phases/done.js.map +1 -0
  92. package/dist/pipeline/phases/implementation.d.ts +8 -0
  93. package/dist/pipeline/phases/implementation.d.ts.map +1 -0
  94. package/dist/pipeline/phases/implementation.js +42 -0
  95. package/dist/pipeline/phases/implementation.js.map +1 -0
  96. package/dist/pipeline/phases/index.d.ts +20 -0
  97. package/dist/pipeline/phases/index.d.ts.map +1 -0
  98. package/dist/pipeline/phases/index.js +19 -0
  99. package/dist/pipeline/phases/index.js.map +1 -0
  100. package/dist/pipeline/phases/intake.d.ts +8 -0
  101. package/dist/pipeline/phases/intake.d.ts.map +1 -0
  102. package/dist/pipeline/phases/intake.js +40 -0
  103. package/dist/pipeline/phases/intake.js.map +1 -0
  104. package/dist/pipeline/phases/phase-context.d.ts +30 -0
  105. package/dist/pipeline/phases/phase-context.d.ts.map +1 -0
  106. package/dist/pipeline/phases/phase-context.js +33 -0
  107. package/dist/pipeline/phases/phase-context.js.map +1 -0
  108. package/dist/pipeline/phases/production-gate.d.ts +8 -0
  109. package/dist/pipeline/phases/production-gate.d.ts.map +1 -0
  110. package/dist/pipeline/phases/production-gate.js +84 -0
  111. package/dist/pipeline/phases/production-gate.js.map +1 -0
  112. package/dist/pipeline/phases/qa-validation.d.ts +7 -0
  113. package/dist/pipeline/phases/qa-validation.d.ts.map +1 -0
  114. package/dist/pipeline/phases/qa-validation.js +50 -0
  115. package/dist/pipeline/phases/qa-validation.js.map +1 -0
  116. package/dist/pipeline/phases/recovery-loop.d.ts +7 -0
  117. package/dist/pipeline/phases/recovery-loop.d.ts.map +1 -0
  118. package/dist/pipeline/phases/recovery-loop.js +91 -0
  119. package/dist/pipeline/phases/recovery-loop.js.map +1 -0
  120. package/dist/pipeline/phases/review.d.ts +8 -0
  121. package/dist/pipeline/phases/review.d.ts.map +1 -0
  122. package/dist/pipeline/phases/review.js +127 -0
  123. package/dist/pipeline/phases/review.js.map +1 -0
  124. package/dist/pipeline/phases/role-planning.d.ts +7 -0
  125. package/dist/pipeline/phases/role-planning.d.ts.map +1 -0
  126. package/dist/pipeline/phases/role-planning.js +75 -0
  127. package/dist/pipeline/phases/role-planning.js.map +1 -0
  128. package/dist/pipeline/phases/stuck.d.ts +7 -0
  129. package/dist/pipeline/phases/stuck.d.ts.map +1 -0
  130. package/dist/pipeline/phases/stuck.js +51 -0
  131. package/dist/pipeline/phases/stuck.js.map +1 -0
  132. package/dist/pipeline/repo-snapshot.d.ts +24 -0
  133. package/dist/pipeline/repo-snapshot.d.ts.map +1 -0
  134. package/dist/pipeline/repo-snapshot.js +343 -0
  135. package/dist/pipeline/repo-snapshot.js.map +1 -0
  136. package/dist/pipeline/role-execution-adapter.d.ts +59 -0
  137. package/dist/pipeline/role-execution-adapter.d.ts.map +1 -0
  138. package/dist/pipeline/role-execution-adapter.js +159 -0
  139. package/dist/pipeline/role-execution-adapter.js.map +1 -0
  140. package/dist/pipeline/skill-loader.d.ts +34 -0
  141. package/dist/pipeline/skill-loader.d.ts.map +1 -0
  142. package/dist/pipeline/skill-loader.js +156 -0
  143. package/dist/pipeline/skill-loader.js.map +1 -0
  144. package/dist/pipeline/skills/defaults.d.ts +16 -0
  145. package/dist/pipeline/skills/defaults.d.ts.map +1 -0
  146. package/dist/pipeline/skills/defaults.js +189 -0
  147. package/dist/pipeline/skills/defaults.js.map +1 -0
  148. package/dist/pipeline/type-defs/artifacts.d.ts +202 -0
  149. package/dist/pipeline/type-defs/artifacts.d.ts.map +1 -0
  150. package/dist/pipeline/type-defs/artifacts.js +66 -0
  151. package/dist/pipeline/type-defs/artifacts.js.map +1 -0
  152. package/dist/pipeline/type-defs/audit.d.ts +256 -0
  153. package/dist/pipeline/type-defs/audit.d.ts.map +1 -0
  154. package/dist/pipeline/type-defs/audit.js +54 -0
  155. package/dist/pipeline/type-defs/audit.js.map +1 -0
  156. package/dist/pipeline/type-defs/checks.d.ts +81 -0
  157. package/dist/pipeline/type-defs/checks.d.ts.map +1 -0
  158. package/dist/pipeline/type-defs/checks.js +38 -0
  159. package/dist/pipeline/type-defs/checks.js.map +1 -0
  160. package/dist/pipeline/type-defs/enums.d.ts +43 -0
  161. package/dist/pipeline/type-defs/enums.d.ts.map +1 -0
  162. package/dist/pipeline/type-defs/enums.js +55 -0
  163. package/dist/pipeline/type-defs/enums.js.map +1 -0
  164. package/dist/pipeline/type-defs/index.d.ts +12 -0
  165. package/dist/pipeline/type-defs/index.d.ts.map +1 -0
  166. package/dist/pipeline/type-defs/index.js +12 -0
  167. package/dist/pipeline/type-defs/index.js.map +1 -0
  168. package/dist/pipeline/type-defs/packets.d.ts +806 -0
  169. package/dist/pipeline/type-defs/packets.d.ts.map +1 -0
  170. package/dist/pipeline/type-defs/packets.js +109 -0
  171. package/dist/pipeline/type-defs/packets.js.map +1 -0
  172. package/dist/pipeline/type-defs/snapshot.d.ts +52 -0
  173. package/dist/pipeline/type-defs/snapshot.d.ts.map +1 -0
  174. package/dist/pipeline/type-defs/snapshot.js +35 -0
  175. package/dist/pipeline/type-defs/snapshot.js.map +1 -0
  176. package/dist/pipeline/type-defs/state.d.ts +449 -0
  177. package/dist/pipeline/type-defs/state.d.ts.map +1 -0
  178. package/dist/pipeline/type-defs/state.js +88 -0
  179. package/dist/pipeline/type-defs/state.js.map +1 -0
  180. package/dist/pipeline/types.d.ts +16 -0
  181. package/dist/pipeline/types.d.ts.map +1 -0
  182. package/dist/pipeline/types.js +16 -0
  183. package/dist/pipeline/types.js.map +1 -0
  184. package/dist/types/audit.d.ts +6 -6
  185. package/dist/workflow/index.d.ts.map +1 -1
  186. package/dist/workflow/index.js +48 -0
  187. package/dist/workflow/index.js.map +1 -1
  188. package/package.json +1 -1
  189. package/skills/PHASE_GATE_ENGINE_SPEC.md +113 -20
  190. package/skills/POPEYE_FULL_AUTONOMY_PIPELINE.md +66 -13
  191. package/src/pipeline/artifact-manager.ts +339 -0
  192. package/src/pipeline/artifact-validators.ts +224 -0
  193. package/src/pipeline/change-request.ts +119 -0
  194. package/src/pipeline/check-runner.ts +504 -0
  195. package/src/pipeline/command-resolver.ts +168 -0
  196. package/src/pipeline/consensus/consensus-runner.ts +317 -0
  197. package/src/pipeline/constitution.ts +109 -0
  198. package/src/pipeline/gate-engine.ts +347 -0
  199. package/src/pipeline/index.ts +82 -0
  200. package/src/pipeline/migration.ts +91 -0
  201. package/src/pipeline/orchestrator.ts +314 -0
  202. package/src/pipeline/packets/audit-report-builder.ts +47 -0
  203. package/src/pipeline/packets/consensus-packet-builder.ts +112 -0
  204. package/src/pipeline/packets/index.ts +15 -0
  205. package/src/pipeline/packets/plan-packet-builder.ts +52 -0
  206. package/src/pipeline/packets/rca-packet-builder.ts +38 -0
  207. package/src/pipeline/phases/architecture.ts +73 -0
  208. package/src/pipeline/phases/audit.ts +193 -0
  209. package/src/pipeline/phases/consensus-architecture.ts +104 -0
  210. package/src/pipeline/phases/consensus-master-plan.ts +100 -0
  211. package/src/pipeline/phases/consensus-role-plans.ts +105 -0
  212. package/src/pipeline/phases/done.ts +68 -0
  213. package/src/pipeline/phases/implementation.ts +48 -0
  214. package/src/pipeline/phases/index.ts +21 -0
  215. package/src/pipeline/phases/intake.ts +54 -0
  216. package/src/pipeline/phases/phase-context.ts +86 -0
  217. package/src/pipeline/phases/production-gate.ts +113 -0
  218. package/src/pipeline/phases/qa-validation.ts +63 -0
  219. package/src/pipeline/phases/recovery-loop.ts +118 -0
  220. package/src/pipeline/phases/review.ts +149 -0
  221. package/src/pipeline/phases/role-planning.ts +92 -0
  222. package/src/pipeline/phases/stuck.ts +62 -0
  223. package/src/pipeline/repo-snapshot.ts +395 -0
  224. package/src/pipeline/role-execution-adapter.ts +238 -0
  225. package/src/pipeline/skill-loader.ts +192 -0
  226. package/src/pipeline/skills/defaults.ts +215 -0
  227. package/src/pipeline/type-defs/artifacts.ts +81 -0
  228. package/src/pipeline/type-defs/audit.ts +67 -0
  229. package/src/pipeline/type-defs/checks.ts +47 -0
  230. package/src/pipeline/type-defs/enums.ts +62 -0
  231. package/src/pipeline/type-defs/index.ts +12 -0
  232. package/src/pipeline/type-defs/packets.ts +131 -0
  233. package/src/pipeline/type-defs/snapshot.ts +55 -0
  234. package/src/pipeline/type-defs/state.ts +165 -0
  235. package/src/pipeline/types.ts +16 -0
  236. package/src/workflow/index.ts +48 -0
  237. package/tests/pipeline/artifact-manager.test.ts +183 -0
  238. package/tests/pipeline/artifact-validators.test.ts +207 -0
  239. package/tests/pipeline/change-request.test.ts +180 -0
  240. package/tests/pipeline/check-runner.test.ts +157 -0
  241. package/tests/pipeline/command-resolver.test.ts +159 -0
  242. package/tests/pipeline/consensus-runner.test.ts +206 -0
  243. package/tests/pipeline/consensus-scoring.test.ts +163 -0
  244. package/tests/pipeline/constitution.test.ts +122 -0
  245. package/tests/pipeline/gate-engine.test.ts +195 -0
  246. package/tests/pipeline/migration.test.ts +133 -0
  247. package/tests/pipeline/orchestrator.test.ts +614 -0
  248. package/tests/pipeline/packets/builders.test.ts +347 -0
  249. package/tests/pipeline/repo-snapshot.test.ts +189 -0
  250. package/tests/pipeline/role-execution-adapter.test.ts +299 -0
  251. package/tests/pipeline/skill-loader.test.ts +186 -0
  252. package/tests/pipeline/start-env-checks.test.ts +123 -0
  253. package/tests/pipeline/types.test.ts +156 -0
@@ -0,0 +1,347 @@
1
+ /**
2
+ * Packet Builders tests — all 4 builders: auto-compute, version increment.
3
+ */
4
+
5
+ import { describe, it, expect } from 'vitest';
6
+ import {
7
+ buildPlanPacket,
8
+ buildConsensusPacket,
9
+ buildRCAPacket,
10
+ buildAuditReport,
11
+ } from '../../../src/pipeline/packets/index.js';
12
+ import type { ArtifactRef, ReviewerVote, AuditFinding } from '../../../src/pipeline/types.js';
13
+
14
+ function makeRef(type: string = 'master_plan'): ArtifactRef {
15
+ return {
16
+ artifact_id: `ref-${type}`,
17
+ path: `docs/${type}.md`,
18
+ sha256: 'abc123',
19
+ version: 1,
20
+ type: type as ArtifactRef['type'],
21
+ };
22
+ }
23
+
24
+ function makeVote(
25
+ vote: 'APPROVE' | 'REJECT' | 'CONDITIONAL',
26
+ reviewerId: string = 'reviewer-1',
27
+ ): ReviewerVote {
28
+ return {
29
+ reviewer_id: reviewerId,
30
+ provider: 'openai',
31
+ model: 'gpt-4o',
32
+ temperature: 0.3,
33
+ prompt_hash: 'hash123',
34
+ vote,
35
+ confidence: 0.9,
36
+ blocking_issues: vote === 'REJECT' ? ['issue'] : [],
37
+ suggestions: [],
38
+ evidence_refs: [],
39
+ };
40
+ }
41
+
42
+ describe('PacketBuilders', () => {
43
+ describe('buildPlanPacket', () => {
44
+ it('should create a valid plan packet', () => {
45
+ const packet = buildPlanPacket({
46
+ phase: 'INTAKE',
47
+ submittedBy: 'DISPATCHER',
48
+ masterPlanRef: makeRef('master_plan'),
49
+ constitutionRef: makeRef('constitution'),
50
+ repoSnapshotRef: makeRef('repo_snapshot'),
51
+ proposedArtifacts: [makeRef('architecture')],
52
+ acceptanceCriteria: ['All endpoints documented'],
53
+ dependencies: [],
54
+ constraints: [],
55
+ });
56
+
57
+ expect(packet.metadata.packet_id).toBeDefined();
58
+ expect(packet.metadata.timestamp).toBeDefined();
59
+ expect(packet.metadata.phase).toBe('INTAKE');
60
+ expect(packet.metadata.submitted_by).toBe('DISPATCHER');
61
+ expect(packet.metadata.version).toBe(1);
62
+ expect(packet.references.master_plan.artifact_id).toBe('ref-master_plan');
63
+ expect(packet.proposed_artifacts).toHaveLength(1);
64
+ expect(packet.acceptance_criteria).toHaveLength(1);
65
+ });
66
+
67
+ it('should accept version override', () => {
68
+ const packet = buildPlanPacket({
69
+ phase: 'ARCHITECTURE',
70
+ submittedBy: 'ARCHITECT',
71
+ masterPlanRef: makeRef(),
72
+ constitutionRef: makeRef(),
73
+ repoSnapshotRef: makeRef('repo_snapshot'),
74
+ proposedArtifacts: [],
75
+ acceptanceCriteria: [],
76
+ dependencies: [],
77
+ constraints: [],
78
+ version: 3,
79
+ });
80
+
81
+ expect(packet.metadata.version).toBe(3);
82
+ });
83
+
84
+ it('should include open questions when provided', () => {
85
+ const packet = buildPlanPacket({
86
+ phase: 'INTAKE',
87
+ submittedBy: 'DISPATCHER',
88
+ masterPlanRef: makeRef(),
89
+ constitutionRef: makeRef(),
90
+ repoSnapshotRef: makeRef('repo_snapshot'),
91
+ proposedArtifacts: [],
92
+ acceptanceCriteria: [],
93
+ dependencies: [],
94
+ constraints: [],
95
+ openQuestions: ['Which database?', 'Auth strategy?'],
96
+ });
97
+
98
+ expect(packet.open_questions).toHaveLength(2);
99
+ });
100
+ });
101
+
102
+ describe('buildConsensusPacket', () => {
103
+ it('should auto-compute APPROVED when all approve', () => {
104
+ const packet = buildConsensusPacket({
105
+ planPacketRef: makeRef(),
106
+ votes: [makeVote('APPROVE', 'r1'), makeVote('APPROVE', 'r2')],
107
+ rules: { threshold: 0.95, quorum: 2, min_reviewers: 2 },
108
+ });
109
+
110
+ expect(packet.consensus_result.approved).toBe(true);
111
+ expect(packet.consensus_result.score).toBe(1.0);
112
+ expect(packet.consensus_result.participating_reviewers).toBe(2);
113
+ expect(packet.final_status).toBe('APPROVED');
114
+ });
115
+
116
+ it('should auto-compute REJECTED when below threshold', () => {
117
+ const packet = buildConsensusPacket({
118
+ planPacketRef: makeRef(),
119
+ votes: [makeVote('APPROVE', 'r1'), makeVote('REJECT', 'r2')],
120
+ rules: { threshold: 0.95, quorum: 2, min_reviewers: 2 },
121
+ });
122
+
123
+ expect(packet.consensus_result.approved).toBe(false);
124
+ expect(packet.consensus_result.score).toBe(0.5);
125
+ expect(packet.final_status).toBe('REJECTED');
126
+ });
127
+
128
+ it('should set ARBITRATED when arbitrator present', () => {
129
+ const packet = buildConsensusPacket({
130
+ planPacketRef: makeRef(),
131
+ votes: [makeVote('APPROVE', 'r1'), makeVote('REJECT', 'r2')],
132
+ rules: { threshold: 0.95, quorum: 2, min_reviewers: 2 },
133
+ arbitratorResult: { decision: 'Approve with changes' },
134
+ });
135
+
136
+ expect(packet.final_status).toBe('ARBITRATED');
137
+ expect(packet.arbitrator_result?.decision).toBe('Approve with changes');
138
+ });
139
+
140
+ it('should reject when quorum not met', () => {
141
+ const packet = buildConsensusPacket({
142
+ planPacketRef: makeRef(),
143
+ votes: [makeVote('APPROVE', 'r1')],
144
+ rules: { threshold: 0.95, quorum: 2, min_reviewers: 2 },
145
+ });
146
+
147
+ // Score is 1.0 but quorum=2, only 1 voter
148
+ expect(packet.consensus_result.approved).toBe(false);
149
+ expect(packet.final_status).toBe('REJECTED');
150
+ });
151
+
152
+ it('should handle zero votes', () => {
153
+ const packet = buildConsensusPacket({
154
+ planPacketRef: makeRef(),
155
+ votes: [],
156
+ rules: { threshold: 0.95, quorum: 2, min_reviewers: 2 },
157
+ });
158
+
159
+ expect(packet.consensus_result.score).toBe(0);
160
+ expect(packet.consensus_result.approved).toBe(false);
161
+ });
162
+
163
+ it('should include weighted_score in consensus result (v1.1)', () => {
164
+ const packet = buildConsensusPacket({
165
+ planPacketRef: makeRef(),
166
+ votes: [makeVote('APPROVE', 'r1'), makeVote('APPROVE', 'r2')],
167
+ rules: { threshold: 0.5, quorum: 1, min_reviewers: 1 },
168
+ });
169
+
170
+ expect(packet.consensus_result.weighted_score).toBeDefined();
171
+ expect(typeof packet.consensus_result.weighted_score).toBe('number');
172
+ expect(packet.consensus_result.weighted_score).toBe(1.0);
173
+ });
174
+
175
+ it('should have weighted_score < 1 for mixed votes (v1.1)', () => {
176
+ const packet = buildConsensusPacket({
177
+ planPacketRef: makeRef(),
178
+ votes: [makeVote('APPROVE', 'r1'), makeVote('REJECT', 'r2')],
179
+ rules: { threshold: 0.5, quorum: 1, min_reviewers: 1 },
180
+ });
181
+
182
+ // weighted_score should be 0 since REJECT vote has blocking_issues
183
+ expect(packet.consensus_result.weighted_score).toBe(0);
184
+ });
185
+
186
+ it('should link to plan packet', () => {
187
+ const planRef = makeRef();
188
+ const packet = buildConsensusPacket({
189
+ planPacketRef: planRef,
190
+ votes: [makeVote('APPROVE')],
191
+ rules: { threshold: 0.5, quorum: 1, min_reviewers: 1 },
192
+ });
193
+
194
+ expect(packet.metadata.plan_packet_id).toBe(planRef.artifact_id);
195
+ expect(packet.plan_packet_reference).toBe(planRef);
196
+ });
197
+ });
198
+
199
+ describe('buildRCAPacket', () => {
200
+ it('should create RCA with all fields', () => {
201
+ const rca = buildRCAPacket({
202
+ incidentSummary: 'Tests failing on CI',
203
+ symptoms: ['test timeout', 'flaky assertions'],
204
+ rootCause: 'Race condition in async setup',
205
+ responsibleLayer: 'test infrastructure',
206
+ originPhase: 'QA_VALIDATION',
207
+ governanceGap: 'No test isolation check',
208
+ correctiveActions: ['Add mutex to shared resource'],
209
+ prevention: 'Enable parallel test isolation',
210
+ });
211
+
212
+ expect(rca.rca_id).toBeDefined();
213
+ expect(rca.timestamp).toBeDefined();
214
+ expect(rca.incident_summary).toBe('Tests failing on CI');
215
+ expect(rca.symptoms).toHaveLength(2);
216
+ expect(rca.root_cause).toContain('Race condition');
217
+ expect(rca.origin_phase).toBe('QA_VALIDATION');
218
+ expect(rca.corrective_actions).toHaveLength(1);
219
+ });
220
+
221
+ it('should include rewind target when provided', () => {
222
+ const rca = buildRCAPacket({
223
+ incidentSummary: 'Architecture mismatch',
224
+ symptoms: ['contract violation'],
225
+ rootCause: 'Wrong API design',
226
+ responsibleLayer: 'architecture',
227
+ originPhase: 'IMPLEMENTATION',
228
+ governanceGap: 'No contract validation',
229
+ correctiveActions: ['Redesign API'],
230
+ prevention: 'Add contract tests',
231
+ rewindTo: 'ARCHITECTURE',
232
+ requiresConsensusOn: ['ARCHITECTURE'],
233
+ });
234
+
235
+ expect(rca.requires_phase_rewind_to).toBe('ARCHITECTURE');
236
+ expect(rca.requires_consensus_on).toEqual(['ARCHITECTURE']);
237
+ });
238
+
239
+ it('should leave rewind undefined when not needed', () => {
240
+ const rca = buildRCAPacket({
241
+ incidentSummary: 'Minor fix',
242
+ symptoms: ['typo'],
243
+ rootCause: 'Spelling error',
244
+ responsibleLayer: 'documentation',
245
+ originPhase: 'REVIEW',
246
+ governanceGap: 'None',
247
+ correctiveActions: ['Fix typo'],
248
+ prevention: 'Add spell check',
249
+ });
250
+
251
+ expect(rca.requires_phase_rewind_to).toBeUndefined();
252
+ expect(rca.requires_consensus_on).toBeUndefined();
253
+ });
254
+ });
255
+
256
+ describe('buildAuditReport', () => {
257
+ it('should PASS with no findings', () => {
258
+ const report = buildAuditReport({
259
+ repoSnapshot: makeRef('repo_snapshot'),
260
+ findings: [],
261
+ });
262
+
263
+ expect(report.audit_id).toBeDefined();
264
+ expect(report.overall_status).toBe('PASS');
265
+ expect(report.system_risk_score).toBe(0);
266
+ expect(report.recovery_required).toBe(false);
267
+ expect(report.findings).toHaveLength(0);
268
+ });
269
+
270
+ it('should FAIL with blocking findings', () => {
271
+ const finding: AuditFinding = {
272
+ id: 'f1',
273
+ severity: 'P0',
274
+ category: 'security',
275
+ description: 'SQL injection in login',
276
+ evidence: [],
277
+ blocking: true,
278
+ suggested_owner: 'BACKEND_PROGRAMMER',
279
+ };
280
+
281
+ const report = buildAuditReport({
282
+ repoSnapshot: makeRef('repo_snapshot'),
283
+ findings: [finding],
284
+ });
285
+
286
+ expect(report.overall_status).toBe('FAIL');
287
+ expect(report.system_risk_score).toBe(40); // P0 = 40
288
+ expect(report.recovery_required).toBe(true);
289
+ });
290
+
291
+ it('should PASS with non-blocking findings', () => {
292
+ const finding: AuditFinding = {
293
+ id: 'f1',
294
+ severity: 'P3',
295
+ category: 'config',
296
+ description: 'Missing optional config',
297
+ evidence: [],
298
+ blocking: false,
299
+ suggested_owner: 'BACKEND_PROGRAMMER',
300
+ };
301
+
302
+ const report = buildAuditReport({
303
+ repoSnapshot: makeRef('repo_snapshot'),
304
+ findings: [finding],
305
+ });
306
+
307
+ expect(report.overall_status).toBe('PASS');
308
+ expect(report.system_risk_score).toBe(2); // P3 = 2
309
+ expect(report.recovery_required).toBe(false);
310
+ });
311
+
312
+ it('should compute cumulative risk score', () => {
313
+ const findings: AuditFinding[] = [
314
+ { id: 'f1', severity: 'P0', category: 'security', description: 'A', evidence: [], blocking: true, suggested_owner: 'DEBUGGER' },
315
+ { id: 'f2', severity: 'P1', category: 'tests', description: 'B', evidence: [], blocking: true, suggested_owner: 'QA_TESTER' },
316
+ { id: 'f3', severity: 'P2', category: 'config', description: 'C', evidence: [], blocking: false, suggested_owner: 'BACKEND_PROGRAMMER' },
317
+ ];
318
+
319
+ const report = buildAuditReport({
320
+ repoSnapshot: makeRef('repo_snapshot'),
321
+ findings,
322
+ });
323
+
324
+ expect(report.system_risk_score).toBe(68); // 40 + 20 + 8
325
+ expect(report.recovery_required).toBe(true);
326
+ });
327
+
328
+ it('should cap risk score at 100', () => {
329
+ const findings: AuditFinding[] = Array.from({ length: 5 }, (_, i) => ({
330
+ id: `f${i}`,
331
+ severity: 'P0' as const,
332
+ category: 'security' as const,
333
+ description: `Finding ${i}`,
334
+ evidence: [],
335
+ blocking: true,
336
+ suggested_owner: 'DEBUGGER' as const,
337
+ }));
338
+
339
+ const report = buildAuditReport({
340
+ repoSnapshot: makeRef('repo_snapshot'),
341
+ findings,
342
+ });
343
+
344
+ expect(report.system_risk_score).toBe(100); // 5 * 40 = 200, capped at 100
345
+ });
346
+ });
347
+ });
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Repo Snapshot tests — config detection, tree summary, diff.
3
+ */
4
+
5
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
6
+ import { mkdirSync, rmSync, writeFileSync, existsSync } from 'node:fs';
7
+ import { join } from 'node:path';
8
+ import { generateRepoSnapshot, diffSnapshots } from '../../src/pipeline/repo-snapshot.js';
9
+
10
+ const TEST_DIR = join(process.cwd(), '.test-repo-snapshot');
11
+
12
+ describe('RepoSnapshot', () => {
13
+ beforeEach(() => {
14
+ if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true });
15
+ mkdirSync(TEST_DIR, { recursive: true });
16
+ });
17
+
18
+ afterEach(() => {
19
+ if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true });
20
+ });
21
+
22
+ describe('generateRepoSnapshot', () => {
23
+ it('should generate snapshot with basic fields', async () => {
24
+ writeFileSync(join(TEST_DIR, 'index.ts'), 'console.log("hello");\n');
25
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
26
+
27
+ expect(snapshot.snapshot_id).toBeDefined();
28
+ expect(snapshot.snapshot_id.length).toBe(16);
29
+ expect(snapshot.timestamp).toBeDefined();
30
+ expect(snapshot.tree_summary).toBeDefined();
31
+ expect(Array.isArray(snapshot.config_files)).toBe(true);
32
+ expect(Array.isArray(snapshot.languages_detected)).toBe(true);
33
+ expect(typeof snapshot.total_files).toBe('number');
34
+ expect(typeof snapshot.total_lines).toBe('number');
35
+ });
36
+
37
+ it('should detect package.json config', async () => {
38
+ const pkg = { name: 'test', version: '1.0.0', scripts: { test: 'vitest' } };
39
+ writeFileSync(join(TEST_DIR, 'package.json'), JSON.stringify(pkg, null, 2));
40
+
41
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
42
+ const pkgConfig = snapshot.config_files.find((c) => c.type === 'package.json');
43
+
44
+ expect(pkgConfig).toBeDefined();
45
+ expect(pkgConfig!.content_hash).toBeDefined();
46
+ expect(pkgConfig!.key_fields).toHaveProperty('name', 'test');
47
+ expect(pkgConfig!.key_fields).toHaveProperty('scripts');
48
+ });
49
+
50
+ it('should detect languages from file extensions', async () => {
51
+ mkdirSync(join(TEST_DIR, 'src'), { recursive: true });
52
+ writeFileSync(join(TEST_DIR, 'src', 'app.ts'), 'export const x = 1;\n');
53
+ writeFileSync(join(TEST_DIR, 'src', 'util.py'), 'x = 1\n');
54
+
55
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
56
+ expect(snapshot.languages_detected).toContain('typescript');
57
+ expect(snapshot.languages_detected).toContain('python');
58
+ });
59
+
60
+ it('should detect package manager from lock files', async () => {
61
+ writeFileSync(join(TEST_DIR, 'package.json'), '{}');
62
+ writeFileSync(join(TEST_DIR, 'pnpm-lock.yaml'), '');
63
+
64
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
65
+ expect(snapshot.package_manager).toBe('pnpm');
66
+ });
67
+
68
+ it('should detect test framework from scripts', async () => {
69
+ const pkg = { name: 'test', scripts: { test: 'vitest run' } };
70
+ writeFileSync(join(TEST_DIR, 'package.json'), JSON.stringify(pkg));
71
+
72
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
73
+ expect(snapshot.test_framework).toBe('vitest');
74
+ });
75
+
76
+ it('should detect build tool from scripts', async () => {
77
+ const pkg = { name: 'test', scripts: { build: 'tsc -b' } };
78
+ writeFileSync(join(TEST_DIR, 'package.json'), JSON.stringify(pkg));
79
+
80
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
81
+ expect(snapshot.build_tool).toBe('tsc');
82
+ });
83
+
84
+ it('should detect env files', async () => {
85
+ writeFileSync(join(TEST_DIR, '.env'), 'KEY=val');
86
+ writeFileSync(join(TEST_DIR, '.env.example'), 'KEY=');
87
+
88
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
89
+ expect(snapshot.env_files).toContain('.env');
90
+ expect(snapshot.env_files).toContain('.env.example');
91
+ });
92
+
93
+ it('should detect migrations presence', async () => {
94
+ mkdirSync(join(TEST_DIR, 'migrations'), { recursive: true });
95
+
96
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
97
+ expect(snapshot.migrations_present).toBe(true);
98
+ });
99
+
100
+ it('should count files and lines', async () => {
101
+ mkdirSync(join(TEST_DIR, 'src'), { recursive: true });
102
+ writeFileSync(join(TEST_DIR, 'src', 'a.ts'), 'line1\nline2\nline3\n');
103
+ writeFileSync(join(TEST_DIR, 'src', 'b.ts'), 'one\ntwo\n');
104
+
105
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
106
+ expect(snapshot.total_files).toBe(2);
107
+ expect(snapshot.total_lines).toBeGreaterThan(0);
108
+ });
109
+
110
+ it('should exclude node_modules from scan', async () => {
111
+ mkdirSync(join(TEST_DIR, 'node_modules', 'dep'), { recursive: true });
112
+ writeFileSync(join(TEST_DIR, 'node_modules', 'dep', 'index.js'), 'x');
113
+ mkdirSync(join(TEST_DIR, 'src'), { recursive: true });
114
+ writeFileSync(join(TEST_DIR, 'src', 'app.ts'), 'const x = 1;\n');
115
+
116
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
117
+ expect(snapshot.total_files).toBe(1); // Only src/app.ts
118
+ });
119
+ });
120
+
121
+ describe('diffSnapshots', () => {
122
+ it('should detect no changes for identical snapshots', async () => {
123
+ writeFileSync(join(TEST_DIR, 'index.ts'), 'x');
124
+ const snapshot = await generateRepoSnapshot(TEST_DIR);
125
+ const diff = diffSnapshots(snapshot, snapshot);
126
+
127
+ expect(diff.has_changes).toBe(false);
128
+ expect(diff.files_delta).toBe(0);
129
+ expect(diff.lines_delta).toBe(0);
130
+ expect(diff.added_configs).toHaveLength(0);
131
+ expect(diff.removed_configs).toHaveLength(0);
132
+ expect(diff.changed_configs).toHaveLength(0);
133
+ });
134
+
135
+ it('should detect added config files', () => {
136
+ const before = {
137
+ snapshot_id: 'a', timestamp: '', tree_summary: '',
138
+ config_files: [], languages_detected: [],
139
+ scripts: {}, env_files: [],
140
+ migrations_present: false, ports_entrypoints: [],
141
+ total_files: 10, total_lines: 100,
142
+ };
143
+ const after = {
144
+ ...before,
145
+ snapshot_id: 'b',
146
+ config_files: [{ path: 'package.json', type: 'package.json', content_hash: 'abc', key_fields: {} }],
147
+ };
148
+
149
+ const diff = diffSnapshots(before, after);
150
+ expect(diff.added_configs).toContain('package.json');
151
+ expect(diff.has_changes).toBe(true);
152
+ });
153
+
154
+ it('should detect changed config files', () => {
155
+ const before = {
156
+ snapshot_id: 'a', timestamp: '', tree_summary: '',
157
+ config_files: [{ path: 'package.json', type: 'package.json', content_hash: 'old', key_fields: {} }],
158
+ languages_detected: [], scripts: {},
159
+ env_files: [], migrations_present: false,
160
+ ports_entrypoints: [], total_files: 10, total_lines: 100,
161
+ };
162
+ const after = {
163
+ ...before,
164
+ snapshot_id: 'b',
165
+ config_files: [{ path: 'package.json', type: 'package.json', content_hash: 'new', key_fields: {} }],
166
+ };
167
+
168
+ const diff = diffSnapshots(before, after);
169
+ expect(diff.changed_configs).toContain('package.json');
170
+ expect(diff.has_changes).toBe(true);
171
+ });
172
+
173
+ it('should detect file count delta', () => {
174
+ const before = {
175
+ snapshot_id: 'a', timestamp: '', tree_summary: '',
176
+ config_files: [], languages_detected: [],
177
+ scripts: {}, env_files: [],
178
+ migrations_present: false, ports_entrypoints: [],
179
+ total_files: 10, total_lines: 100,
180
+ };
181
+ const after = { ...before, snapshot_id: 'b', total_files: 15, total_lines: 200 };
182
+
183
+ const diff = diffSnapshots(before, after);
184
+ expect(diff.files_delta).toBe(5);
185
+ expect(diff.lines_delta).toBe(100);
186
+ expect(diff.has_changes).toBe(true);
187
+ });
188
+ });
189
+ });