@vibecheckai/cli 3.2.5 → 3.3.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 (197) hide show
  1. package/bin/.generated +25 -25
  2. package/bin/dev/run-v2-torture.js +30 -30
  3. package/bin/registry.js +192 -5
  4. package/bin/runners/lib/__tests__/entitlements-v2.test.js +295 -295
  5. package/bin/runners/lib/agent-firewall/change-packet/builder.js +280 -6
  6. package/bin/runners/lib/agent-firewall/critic/index.js +151 -0
  7. package/bin/runners/lib/agent-firewall/critic/judge.js +432 -0
  8. package/bin/runners/lib/agent-firewall/critic/prompts.js +305 -0
  9. package/bin/runners/lib/agent-firewall/lawbook/distributor.js +465 -0
  10. package/bin/runners/lib/agent-firewall/lawbook/evaluator.js +604 -0
  11. package/bin/runners/lib/agent-firewall/lawbook/index.js +304 -0
  12. package/bin/runners/lib/agent-firewall/lawbook/registry.js +514 -0
  13. package/bin/runners/lib/agent-firewall/lawbook/schema.js +420 -0
  14. package/bin/runners/lib/agent-firewall/logger.js +141 -0
  15. package/bin/runners/lib/agent-firewall/policy/loader.js +312 -4
  16. package/bin/runners/lib/agent-firewall/policy/rules/ghost-env.js +113 -1
  17. package/bin/runners/lib/agent-firewall/policy/rules/ghost-route.js +133 -6
  18. package/bin/runners/lib/agent-firewall/proposal/extractor.js +394 -0
  19. package/bin/runners/lib/agent-firewall/proposal/index.js +212 -0
  20. package/bin/runners/lib/agent-firewall/proposal/schema.js +251 -0
  21. package/bin/runners/lib/agent-firewall/proposal/validator.js +386 -0
  22. package/bin/runners/lib/agent-firewall/reality/index.js +332 -0
  23. package/bin/runners/lib/agent-firewall/reality/state.js +625 -0
  24. package/bin/runners/lib/agent-firewall/reality/watcher.js +322 -0
  25. package/bin/runners/lib/agent-firewall/risk/index.js +173 -0
  26. package/bin/runners/lib/agent-firewall/risk/scorer.js +328 -0
  27. package/bin/runners/lib/agent-firewall/risk/thresholds.js +321 -0
  28. package/bin/runners/lib/agent-firewall/risk/vectors.js +421 -0
  29. package/bin/runners/lib/agent-firewall/simulator/diff-simulator.js +472 -0
  30. package/bin/runners/lib/agent-firewall/simulator/import-resolver.js +346 -0
  31. package/bin/runners/lib/agent-firewall/simulator/index.js +181 -0
  32. package/bin/runners/lib/agent-firewall/simulator/route-validator.js +380 -0
  33. package/bin/runners/lib/agent-firewall/time-machine/incident-correlator.js +661 -0
  34. package/bin/runners/lib/agent-firewall/time-machine/index.js +267 -0
  35. package/bin/runners/lib/agent-firewall/time-machine/replay-engine.js +436 -0
  36. package/bin/runners/lib/agent-firewall/time-machine/state-reconstructor.js +490 -0
  37. package/bin/runners/lib/agent-firewall/time-machine/timeline-builder.js +530 -0
  38. package/bin/runners/lib/analyzers.js +81 -18
  39. package/bin/runners/lib/api-client.js +269 -0
  40. package/bin/runners/lib/auth-truth.js +193 -193
  41. package/bin/runners/lib/authority-badge.js +425 -0
  42. package/bin/runners/lib/backup.js +62 -62
  43. package/bin/runners/lib/billing.js +107 -107
  44. package/bin/runners/lib/claims.js +118 -118
  45. package/bin/runners/lib/cli-output.js +7 -1
  46. package/bin/runners/lib/cli-ui.js +540 -540
  47. package/bin/runners/lib/contracts/auth-contract.js +202 -202
  48. package/bin/runners/lib/contracts/env-contract.js +181 -181
  49. package/bin/runners/lib/contracts/external-contract.js +206 -206
  50. package/bin/runners/lib/contracts/guard.js +168 -168
  51. package/bin/runners/lib/contracts/index.js +89 -89
  52. package/bin/runners/lib/contracts/plan-validator.js +311 -311
  53. package/bin/runners/lib/contracts/route-contract.js +199 -199
  54. package/bin/runners/lib/contracts.js +804 -804
  55. package/bin/runners/lib/detect.js +89 -89
  56. package/bin/runners/lib/doctor/autofix.js +254 -254
  57. package/bin/runners/lib/doctor/index.js +37 -37
  58. package/bin/runners/lib/doctor/modules/dependencies.js +325 -325
  59. package/bin/runners/lib/doctor/modules/index.js +46 -46
  60. package/bin/runners/lib/doctor/modules/network.js +250 -250
  61. package/bin/runners/lib/doctor/modules/project.js +312 -312
  62. package/bin/runners/lib/doctor/modules/runtime.js +224 -224
  63. package/bin/runners/lib/doctor/modules/security.js +348 -348
  64. package/bin/runners/lib/doctor/modules/system.js +213 -213
  65. package/bin/runners/lib/doctor/modules/vibecheck.js +394 -394
  66. package/bin/runners/lib/doctor/reporter.js +262 -262
  67. package/bin/runners/lib/doctor/service.js +262 -262
  68. package/bin/runners/lib/doctor/types.js +113 -113
  69. package/bin/runners/lib/doctor/ui.js +263 -263
  70. package/bin/runners/lib/doctor-v2.js +608 -608
  71. package/bin/runners/lib/drift.js +425 -425
  72. package/bin/runners/lib/enforcement.js +72 -72
  73. package/bin/runners/lib/enterprise-detect.js +603 -603
  74. package/bin/runners/lib/enterprise-init.js +942 -942
  75. package/bin/runners/lib/env-resolver.js +417 -417
  76. package/bin/runners/lib/env-template.js +66 -66
  77. package/bin/runners/lib/env.js +189 -189
  78. package/bin/runners/lib/error-handler.js +16 -9
  79. package/bin/runners/lib/exit-codes.js +275 -0
  80. package/bin/runners/lib/extractors/client-calls.js +990 -990
  81. package/bin/runners/lib/extractors/fastify-route-dump.js +573 -573
  82. package/bin/runners/lib/extractors/fastify-routes.js +426 -426
  83. package/bin/runners/lib/extractors/index.js +363 -363
  84. package/bin/runners/lib/extractors/next-routes.js +524 -524
  85. package/bin/runners/lib/extractors/proof-graph.js +431 -431
  86. package/bin/runners/lib/extractors/route-matcher.js +451 -451
  87. package/bin/runners/lib/extractors/truthpack-v2.js +377 -377
  88. package/bin/runners/lib/extractors/ui-bindings.js +547 -547
  89. package/bin/runners/lib/findings-schema.js +281 -281
  90. package/bin/runners/lib/firewall-prompt.js +50 -50
  91. package/bin/runners/lib/global-flags.js +37 -0
  92. package/bin/runners/lib/graph/graph-builder.js +265 -265
  93. package/bin/runners/lib/graph/html-renderer.js +413 -413
  94. package/bin/runners/lib/graph/index.js +32 -32
  95. package/bin/runners/lib/graph/runtime-collector.js +215 -215
  96. package/bin/runners/lib/graph/static-extractor.js +518 -518
  97. package/bin/runners/lib/help-formatter.js +413 -0
  98. package/bin/runners/lib/html-report.js +650 -650
  99. package/bin/runners/lib/llm.js +75 -75
  100. package/bin/runners/lib/logger.js +38 -0
  101. package/bin/runners/lib/meter.js +61 -61
  102. package/bin/runners/lib/missions/evidence.js +126 -126
  103. package/bin/runners/lib/patch.js +40 -40
  104. package/bin/runners/lib/permissions/auth-model.js +213 -213
  105. package/bin/runners/lib/permissions/idor-prover.js +205 -205
  106. package/bin/runners/lib/permissions/index.js +45 -45
  107. package/bin/runners/lib/permissions/matrix-builder.js +198 -198
  108. package/bin/runners/lib/pkgjson.js +28 -28
  109. package/bin/runners/lib/policy.js +295 -295
  110. package/bin/runners/lib/preflight.js +142 -142
  111. package/bin/runners/lib/reality/correlation-detectors.js +359 -359
  112. package/bin/runners/lib/reality/index.js +318 -318
  113. package/bin/runners/lib/reality/request-hashing.js +416 -416
  114. package/bin/runners/lib/reality/request-mapper.js +453 -453
  115. package/bin/runners/lib/reality/safety-rails.js +463 -463
  116. package/bin/runners/lib/reality/semantic-snapshot.js +408 -408
  117. package/bin/runners/lib/reality/toast-detector.js +393 -393
  118. package/bin/runners/lib/reality-findings.js +84 -84
  119. package/bin/runners/lib/receipts.js +179 -179
  120. package/bin/runners/lib/redact.js +29 -29
  121. package/bin/runners/lib/replay/capsule-manager.js +154 -154
  122. package/bin/runners/lib/replay/index.js +263 -263
  123. package/bin/runners/lib/replay/player.js +348 -348
  124. package/bin/runners/lib/replay/recorder.js +331 -331
  125. package/bin/runners/lib/report.js +135 -135
  126. package/bin/runners/lib/route-detection.js +1140 -1140
  127. package/bin/runners/lib/sandbox/index.js +59 -59
  128. package/bin/runners/lib/sandbox/proof-chain.js +399 -399
  129. package/bin/runners/lib/sandbox/sandbox-runner.js +205 -205
  130. package/bin/runners/lib/sandbox/worktree.js +174 -174
  131. package/bin/runners/lib/schema-validator.js +350 -350
  132. package/bin/runners/lib/schemas/contracts.schema.json +160 -160
  133. package/bin/runners/lib/schemas/finding.schema.json +100 -100
  134. package/bin/runners/lib/schemas/mission-pack.schema.json +206 -206
  135. package/bin/runners/lib/schemas/proof-graph.schema.json +176 -176
  136. package/bin/runners/lib/schemas/reality-report.schema.json +162 -162
  137. package/bin/runners/lib/schemas/share-pack.schema.json +180 -180
  138. package/bin/runners/lib/schemas/ship-report.schema.json +117 -117
  139. package/bin/runners/lib/schemas/truthpack-v2.schema.json +303 -303
  140. package/bin/runners/lib/schemas/validator.js +438 -438
  141. package/bin/runners/lib/score-history.js +282 -282
  142. package/bin/runners/lib/share-pack.js +239 -239
  143. package/bin/runners/lib/snippets.js +67 -67
  144. package/bin/runners/lib/unified-cli-output.js +604 -0
  145. package/bin/runners/lib/upsell.js +658 -510
  146. package/bin/runners/lib/usage.js +153 -153
  147. package/bin/runners/lib/validate-patch.js +156 -156
  148. package/bin/runners/lib/verdict-engine.js +628 -628
  149. package/bin/runners/reality/engine.js +917 -917
  150. package/bin/runners/reality/flows.js +122 -122
  151. package/bin/runners/reality/report.js +378 -378
  152. package/bin/runners/reality/session.js +193 -193
  153. package/bin/runners/runAgent.d.ts +5 -0
  154. package/bin/runners/runApprove.js +1200 -0
  155. package/bin/runners/runAuth.js +324 -95
  156. package/bin/runners/runCheckpoint.js +39 -21
  157. package/bin/runners/runClassify.js +859 -0
  158. package/bin/runners/runContext.js +136 -24
  159. package/bin/runners/runDoctor.js +108 -68
  160. package/bin/runners/runFirewall.d.ts +5 -0
  161. package/bin/runners/runFirewallHook.d.ts +5 -0
  162. package/bin/runners/runFix.js +6 -5
  163. package/bin/runners/runGuard.js +262 -168
  164. package/bin/runners/runInit.js +3 -2
  165. package/bin/runners/runMcp.js +130 -52
  166. package/bin/runners/runPolish.js +43 -20
  167. package/bin/runners/runProve.js +1 -2
  168. package/bin/runners/runReport.js +3 -2
  169. package/bin/runners/runScan.js +145 -44
  170. package/bin/runners/runShip.js +3 -4
  171. package/bin/runners/runTruth.d.ts +5 -0
  172. package/bin/runners/runValidate.js +19 -2
  173. package/bin/runners/runWatch.js +104 -53
  174. package/bin/vibecheck.js +106 -19
  175. package/mcp-server/HARDENING_SUMMARY.md +299 -0
  176. package/mcp-server/agent-firewall-interceptor.js +367 -31
  177. package/mcp-server/authority-tools.js +569 -0
  178. package/mcp-server/conductor/conflict-resolver.js +588 -0
  179. package/mcp-server/conductor/execution-planner.js +544 -0
  180. package/mcp-server/conductor/index.js +377 -0
  181. package/mcp-server/conductor/lock-manager.js +615 -0
  182. package/mcp-server/conductor/request-queue.js +550 -0
  183. package/mcp-server/conductor/session-manager.js +500 -0
  184. package/mcp-server/conductor/tools.js +510 -0
  185. package/mcp-server/index.js +1199 -208
  186. package/mcp-server/lib/api-client.cjs +305 -0
  187. package/mcp-server/lib/logger.cjs +30 -0
  188. package/mcp-server/logger.js +173 -0
  189. package/mcp-server/package.json +2 -2
  190. package/mcp-server/premium-tools.js +2 -2
  191. package/mcp-server/tier-auth.js +351 -136
  192. package/mcp-server/tools/index.js +72 -72
  193. package/mcp-server/truth-firewall-tools.js +145 -15
  194. package/mcp-server/vibecheck-tools.js +2 -2
  195. package/package.json +2 -3
  196. package/mcp-server/index.old.js +0 -4137
  197. package/mcp-server/package-lock.json +0 -165
@@ -0,0 +1,465 @@
1
+ /**
2
+ * Lawbook Distributor
3
+ *
4
+ * Syncs org-level policies to projects via API.
5
+ * Manages version control and conflict resolution.
6
+ *
7
+ * Codename: Lawbook
8
+ */
9
+
10
+ "use strict";
11
+
12
+ const fs = require("fs");
13
+ const path = require("path");
14
+ const crypto = require("crypto");
15
+
16
+ const { parseLawbook, serializeLawbook, validateInvariant } = require("./schema.js");
17
+ const { lawbookLogger: log, getErrorMessage } = require("../logger.js");
18
+
19
+ /**
20
+ * @typedef {Object} SyncResult
21
+ * @property {boolean} success - Sync successful
22
+ * @property {number} added - Invariants added
23
+ * @property {number} updated - Invariants updated
24
+ * @property {number} removed - Invariants removed
25
+ * @property {string[]} conflicts - Conflict descriptions
26
+ * @property {string} version - Synced version
27
+ */
28
+
29
+ /**
30
+ * Merge strategies for conflict resolution
31
+ */
32
+ const MERGE_STRATEGIES = {
33
+ ORG_WINS: "org_wins", // Org policies override local
34
+ LOCAL_WINS: "local_wins", // Local policies override org
35
+ STRICT: "strict", // Fail on conflicts
36
+ MERGE: "merge", // Merge with local taking precedence on conflicts
37
+ };
38
+
39
+ /**
40
+ * Lawbook Distributor class
41
+ */
42
+ class LawbookDistributor {
43
+ constructor(options = {}) {
44
+ this.projectRoot = options.projectRoot || process.cwd();
45
+ this.apiEndpoint = options.apiEndpoint || process.env.VIBECHECK_API_URL || "https://api.vibecheck.ai";
46
+ this.apiKey = options.apiKey || process.env.VIBECHECK_API_KEY;
47
+ this.cacheDir = path.join(this.projectRoot, ".vibecheck", "lawbook-cache");
48
+ this.syncHistoryPath = path.join(this.projectRoot, ".vibecheck", "lawbook-sync.json");
49
+ }
50
+
51
+ /**
52
+ * Fetch org lawbook from API
53
+ * @param {string} orgId - Organization ID
54
+ * @param {string} libraryId - Library ID (optional)
55
+ * @returns {Object} Fetched lawbook
56
+ */
57
+ async fetchOrgLawbook(orgId, libraryId = "default") {
58
+ try {
59
+ const url = `${this.apiEndpoint}/v1/orgs/${orgId}/lawbooks/${libraryId}`;
60
+
61
+ const response = await fetch(url, {
62
+ headers: {
63
+ "Authorization": `Bearer ${this.apiKey}`,
64
+ "Content-Type": "application/json",
65
+ },
66
+ });
67
+
68
+ if (!response.ok) {
69
+ throw new Error(`Failed to fetch lawbook: ${response.status}`);
70
+ }
71
+
72
+ const data = await response.json();
73
+
74
+ // Cache the result
75
+ this.cacheLawbook(orgId, libraryId, data);
76
+
77
+ return data;
78
+ } catch (error) {
79
+ // Try to use cached version
80
+ const cached = this.getCachedLawbook(orgId, libraryId);
81
+ if (cached) {
82
+ log.warn(`Using cached lawbook: ${getErrorMessage(error)}`);
83
+ return cached;
84
+ }
85
+ throw error;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Sync org lawbook to local project
91
+ * @param {string} orgId - Organization ID
92
+ * @param {Object} options - Sync options
93
+ * @returns {SyncResult} Sync result
94
+ */
95
+ async sync(orgId, options = {}) {
96
+ const {
97
+ libraryId = "default",
98
+ strategy = MERGE_STRATEGIES.MERGE,
99
+ dryRun = false,
100
+ } = options;
101
+
102
+ const result = {
103
+ success: false,
104
+ added: 0,
105
+ updated: 0,
106
+ removed: 0,
107
+ conflicts: [],
108
+ version: null,
109
+ };
110
+
111
+ try {
112
+ // Fetch org lawbook
113
+ const orgLawbook = await this.fetchOrgLawbook(orgId, libraryId);
114
+ result.version = orgLawbook.version;
115
+
116
+ // Load local lawbook
117
+ const localPath = path.join(this.projectRoot, ".vibecheck", "invariants.yaml");
118
+ let localLawbook = null;
119
+
120
+ if (fs.existsSync(localPath)) {
121
+ const content = fs.readFileSync(localPath, "utf-8");
122
+ localLawbook = parseLawbook(content);
123
+ }
124
+
125
+ // Merge lawbooks
126
+ const merged = this.mergeLawbooks(orgLawbook, localLawbook, strategy, result);
127
+
128
+ // Apply changes if not dry run
129
+ if (!dryRun && result.conflicts.length === 0) {
130
+ const dir = path.dirname(localPath);
131
+ if (!fs.existsSync(dir)) {
132
+ fs.mkdirSync(dir, { recursive: true });
133
+ }
134
+
135
+ const content = serializeLawbook(merged);
136
+ fs.writeFileSync(localPath, content);
137
+
138
+ // Record sync
139
+ this.recordSync(orgId, libraryId, result.version);
140
+
141
+ result.success = true;
142
+ } else if (dryRun) {
143
+ result.success = true;
144
+ }
145
+
146
+ return result;
147
+ } catch (error) {
148
+ result.success = false;
149
+ result.conflicts.push(`Sync error: ${error.message}`);
150
+ return result;
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Merge org and local lawbooks
156
+ * @param {Object} orgLawbook - Org lawbook
157
+ * @param {Object} localLawbook - Local lawbook
158
+ * @param {string} strategy - Merge strategy
159
+ * @param {Object} result - Result object to update
160
+ * @returns {Object} Merged lawbook
161
+ */
162
+ mergeLawbooks(orgLawbook, localLawbook, strategy, result) {
163
+ const merged = {
164
+ version: orgLawbook.version,
165
+ name: localLawbook?.name || orgLawbook.name,
166
+ description: localLawbook?.description || orgLawbook.description,
167
+ extends: orgLawbook.name,
168
+ invariants: [],
169
+ };
170
+
171
+ // Index invariants by ID
172
+ const orgInvariants = new Map();
173
+ const localInvariants = new Map();
174
+
175
+ for (const inv of orgLawbook.invariants || []) {
176
+ inv._source = "org";
177
+ orgInvariants.set(inv.id, inv);
178
+ }
179
+
180
+ for (const inv of localLawbook?.invariants || []) {
181
+ inv._source = "local";
182
+ localInvariants.set(inv.id, inv);
183
+ }
184
+
185
+ // Process org invariants
186
+ for (const [id, orgInv] of orgInvariants) {
187
+ const localInv = localInvariants.get(id);
188
+
189
+ if (!localInv) {
190
+ // New from org
191
+ merged.invariants.push({ ...orgInv, _source: undefined });
192
+ result.added++;
193
+ } else {
194
+ // Exists in both - check for conflicts
195
+ const conflict = this.detectConflict(orgInv, localInv);
196
+
197
+ if (conflict) {
198
+ switch (strategy) {
199
+ case MERGE_STRATEGIES.ORG_WINS:
200
+ merged.invariants.push({ ...orgInv, _source: undefined });
201
+ result.updated++;
202
+ break;
203
+
204
+ case MERGE_STRATEGIES.LOCAL_WINS:
205
+ merged.invariants.push({ ...localInv, _source: undefined });
206
+ break;
207
+
208
+ case MERGE_STRATEGIES.STRICT:
209
+ result.conflicts.push(`Conflict on '${id}': ${conflict}`);
210
+ break;
211
+
212
+ case MERGE_STRATEGIES.MERGE:
213
+ default:
214
+ // Merge with local taking precedence
215
+ const mergedInv = { ...orgInv, ...localInv, _source: undefined };
216
+ merged.invariants.push(mergedInv);
217
+ result.updated++;
218
+ break;
219
+ }
220
+ } else {
221
+ // No conflict - use org version (might have updates)
222
+ merged.invariants.push({ ...orgInv, _source: undefined });
223
+ }
224
+
225
+ localInvariants.delete(id);
226
+ }
227
+ }
228
+
229
+ // Add remaining local-only invariants
230
+ for (const [, localInv] of localInvariants) {
231
+ merged.invariants.push({ ...localInv, _source: undefined });
232
+ }
233
+
234
+ return merged;
235
+ }
236
+
237
+ /**
238
+ * Detect conflict between org and local invariant
239
+ * @param {Object} orgInv - Org invariant
240
+ * @param {Object} localInv - Local invariant
241
+ * @returns {string|null} Conflict description or null
242
+ */
243
+ detectConflict(orgInv, localInv) {
244
+ // Check for rule type conflict
245
+ if (orgInv.rule !== localInv.rule) {
246
+ return `Rule type mismatch: org='${orgInv.rule}', local='${localInv.rule}'`;
247
+ }
248
+
249
+ // Check for severity conflict
250
+ if (orgInv.severity !== localInv.severity) {
251
+ return `Severity mismatch: org='${orgInv.severity}', local='${localInv.severity}'`;
252
+ }
253
+
254
+ // Check for scope conflict
255
+ if (orgInv.scope !== localInv.scope) {
256
+ return `Scope mismatch: org='${orgInv.scope}', local='${localInv.scope}'`;
257
+ }
258
+
259
+ return null;
260
+ }
261
+
262
+ /**
263
+ * Cache a lawbook locally
264
+ * @param {string} orgId - Org ID
265
+ * @param {string} libraryId - Library ID
266
+ * @param {Object} lawbook - Lawbook to cache
267
+ */
268
+ cacheLawbook(orgId, libraryId, lawbook) {
269
+ try {
270
+ if (!fs.existsSync(this.cacheDir)) {
271
+ fs.mkdirSync(this.cacheDir, { recursive: true });
272
+ }
273
+
274
+ const cachePath = path.join(this.cacheDir, `${orgId}-${libraryId}.json`);
275
+ fs.writeFileSync(cachePath, JSON.stringify({
276
+ lawbook,
277
+ cachedAt: new Date().toISOString(),
278
+ }, null, 2));
279
+ } catch (error) {
280
+ log.warn(`Failed to cache lawbook: ${getErrorMessage(error)}`);
281
+ }
282
+ }
283
+
284
+ /**
285
+ * Get cached lawbook
286
+ * @param {string} orgId - Org ID
287
+ * @param {string} libraryId - Library ID
288
+ * @returns {Object|null} Cached lawbook or null
289
+ */
290
+ getCachedLawbook(orgId, libraryId) {
291
+ try {
292
+ const cachePath = path.join(this.cacheDir, `${orgId}-${libraryId}.json`);
293
+
294
+ if (fs.existsSync(cachePath)) {
295
+ const content = fs.readFileSync(cachePath, "utf-8");
296
+ const cached = JSON.parse(content);
297
+
298
+ // Check cache age (24 hours max)
299
+ const cacheAge = Date.now() - new Date(cached.cachedAt).getTime();
300
+ if (cacheAge < 24 * 60 * 60 * 1000) {
301
+ return cached.lawbook;
302
+ }
303
+ }
304
+ } catch (error) {
305
+ log.warn(`Failed to read cached lawbook: ${getErrorMessage(error)}`);
306
+ }
307
+ return null;
308
+ }
309
+
310
+ /**
311
+ * Record a sync event
312
+ * @param {string} orgId - Org ID
313
+ * @param {string} libraryId - Library ID
314
+ * @param {string} version - Synced version
315
+ */
316
+ recordSync(orgId, libraryId, version) {
317
+ try {
318
+ let history = [];
319
+
320
+ if (fs.existsSync(this.syncHistoryPath)) {
321
+ const content = fs.readFileSync(this.syncHistoryPath, "utf-8");
322
+ history = JSON.parse(content);
323
+ }
324
+
325
+ history.push({
326
+ orgId,
327
+ libraryId,
328
+ version,
329
+ syncedAt: new Date().toISOString(),
330
+ });
331
+
332
+ // Keep last 50 entries
333
+ if (history.length > 50) {
334
+ history = history.slice(-50);
335
+ }
336
+
337
+ const dir = path.dirname(this.syncHistoryPath);
338
+ if (!fs.existsSync(dir)) {
339
+ fs.mkdirSync(dir, { recursive: true });
340
+ }
341
+
342
+ fs.writeFileSync(this.syncHistoryPath, JSON.stringify(history, null, 2));
343
+ } catch (error) {
344
+ log.warn(`Failed to record sync: ${getErrorMessage(error)}`);
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Get sync history
350
+ * @returns {Object[]} Sync history
351
+ */
352
+ getSyncHistory() {
353
+ try {
354
+ if (fs.existsSync(this.syncHistoryPath)) {
355
+ const content = fs.readFileSync(this.syncHistoryPath, "utf-8");
356
+ return JSON.parse(content);
357
+ }
358
+ } catch (error) {
359
+ log.warn(`Failed to read sync history: ${getErrorMessage(error)}`);
360
+ }
361
+ return [];
362
+ }
363
+
364
+ /**
365
+ * Check if sync is needed
366
+ * @param {string} orgId - Org ID
367
+ * @param {string} libraryId - Library ID
368
+ * @returns {Object} Sync status
369
+ */
370
+ async checkSyncStatus(orgId, libraryId = "default") {
371
+ const history = this.getSyncHistory();
372
+ const lastSync = history.filter(h => h.orgId === orgId && h.libraryId === libraryId).pop();
373
+
374
+ // Fetch current version from API
375
+ try {
376
+ const url = `${this.apiEndpoint}/v1/orgs/${orgId}/lawbooks/${libraryId}/version`;
377
+ const response = await fetch(url, {
378
+ headers: {
379
+ "Authorization": `Bearer ${this.apiKey}`,
380
+ },
381
+ });
382
+
383
+ if (response.ok) {
384
+ const data = await response.json();
385
+
386
+ return {
387
+ needsSync: !lastSync || lastSync.version !== data.version,
388
+ currentVersion: data.version,
389
+ lastSyncVersion: lastSync?.version || null,
390
+ lastSyncTime: lastSync?.syncedAt || null,
391
+ };
392
+ }
393
+ } catch (error) {
394
+ // Ignore - will use cached data
395
+ }
396
+
397
+ return {
398
+ needsSync: !lastSync,
399
+ currentVersion: null,
400
+ lastSyncVersion: lastSync?.version || null,
401
+ lastSyncTime: lastSync?.syncedAt || null,
402
+ };
403
+ }
404
+
405
+ /**
406
+ * Publish local invariants to org (requires admin)
407
+ * @param {string} orgId - Org ID
408
+ * @param {Object[]} invariants - Invariants to publish
409
+ * @returns {Object} Publish result
410
+ */
411
+ async publishToOrg(orgId, invariants, options = {}) {
412
+ const {
413
+ libraryId = "default",
414
+ message = "Published from local project",
415
+ } = options;
416
+
417
+ try {
418
+ // Validate all invariants first
419
+ for (const inv of invariants) {
420
+ const validation = validateInvariant(inv);
421
+ if (!validation.valid) {
422
+ throw new Error(`Invalid invariant '${inv.id}': ${validation.errors.map(e => e.message).join(", ")}`);
423
+ }
424
+ }
425
+
426
+ const url = `${this.apiEndpoint}/v1/orgs/${orgId}/lawbooks/${libraryId}/publish`;
427
+
428
+ const response = await fetch(url, {
429
+ method: "POST",
430
+ headers: {
431
+ "Authorization": `Bearer ${this.apiKey}`,
432
+ "Content-Type": "application/json",
433
+ },
434
+ body: JSON.stringify({
435
+ invariants,
436
+ message,
437
+ publishedAt: new Date().toISOString(),
438
+ }),
439
+ });
440
+
441
+ if (!response.ok) {
442
+ const error = await response.json().catch(() => ({}));
443
+ throw new Error(error.message || `Publish failed: ${response.status}`);
444
+ }
445
+
446
+ return await response.json();
447
+ } catch (error) {
448
+ return {
449
+ success: false,
450
+ error: error.message,
451
+ };
452
+ }
453
+ }
454
+ }
455
+
456
+ /**
457
+ * Create a distributor instance
458
+ * @param {Object} options - Options
459
+ * @returns {LawbookDistributor} Distributor instance
460
+ */
461
+ function createDistributor(options = {}) {
462
+ return new LawbookDistributor(options);
463
+ }
464
+
465
+ module.exports = { LawbookDistributor, createDistributor, MERGE_STRATEGIES };