@mandujs/core 0.12.2 → 0.13.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 (173) hide show
  1. package/README.ko.md +304 -304
  2. package/README.md +653 -653
  3. package/package.json +1 -1
  4. package/src/brain/architecture/analyzer.ts +28 -26
  5. package/src/brain/doctor/analyzer.ts +1 -1
  6. package/src/bundler/build.ts +91 -91
  7. package/src/bundler/css.ts +302 -302
  8. package/src/bundler/dev.ts +0 -1
  9. package/src/change/history.ts +3 -3
  10. package/src/change/snapshot.ts +10 -9
  11. package/src/change/transaction.ts +2 -2
  12. package/src/client/Link.tsx +227 -227
  13. package/src/client/globals.ts +44 -44
  14. package/src/client/hooks.ts +267 -267
  15. package/src/client/index.ts +5 -5
  16. package/src/client/island.ts +8 -8
  17. package/src/client/router.ts +435 -435
  18. package/src/client/runtime.ts +23 -23
  19. package/src/client/serialize.ts +404 -404
  20. package/src/client/window-state.ts +101 -101
  21. package/src/config/mandu.ts +94 -96
  22. package/src/config/validate.ts +213 -215
  23. package/src/config/watcher.ts +311 -311
  24. package/src/constants.ts +40 -40
  25. package/src/content/content-layer.ts +314 -314
  26. package/src/content/content.test.ts +433 -433
  27. package/src/content/data-store.ts +245 -245
  28. package/src/content/digest.ts +133 -133
  29. package/src/content/index.ts +164 -164
  30. package/src/content/loader-context.ts +172 -172
  31. package/src/content/loaders/api.ts +216 -216
  32. package/src/content/loaders/file.ts +169 -169
  33. package/src/content/loaders/glob.ts +252 -252
  34. package/src/content/loaders/index.ts +34 -34
  35. package/src/content/loaders/types.ts +137 -137
  36. package/src/content/meta-store.ts +209 -209
  37. package/src/content/types.ts +282 -282
  38. package/src/content/watcher.ts +135 -135
  39. package/src/contract/client-safe.test.ts +42 -42
  40. package/src/contract/client-safe.ts +114 -114
  41. package/src/contract/client.ts +16 -16
  42. package/src/contract/define.ts +459 -459
  43. package/src/contract/handler.ts +10 -10
  44. package/src/contract/normalize.test.ts +276 -276
  45. package/src/contract/normalize.ts +404 -404
  46. package/src/contract/registry.test.ts +206 -206
  47. package/src/contract/registry.ts +568 -568
  48. package/src/contract/schema.ts +48 -48
  49. package/src/contract/types.ts +58 -58
  50. package/src/contract/validator.ts +32 -32
  51. package/src/devtools/ai/context-builder.ts +375 -375
  52. package/src/devtools/ai/index.ts +25 -25
  53. package/src/devtools/ai/mcp-connector.ts +465 -465
  54. package/src/devtools/client/catchers/error-catcher.ts +327 -327
  55. package/src/devtools/client/catchers/index.ts +18 -18
  56. package/src/devtools/client/catchers/network-proxy.ts +363 -363
  57. package/src/devtools/client/components/index.ts +39 -39
  58. package/src/devtools/client/components/kitchen-root.tsx +362 -362
  59. package/src/devtools/client/components/mandu-character.tsx +241 -241
  60. package/src/devtools/client/components/overlay.tsx +368 -368
  61. package/src/devtools/client/components/panel/errors-panel.tsx +259 -259
  62. package/src/devtools/client/components/panel/guard-panel.tsx +244 -244
  63. package/src/devtools/client/components/panel/index.ts +32 -32
  64. package/src/devtools/client/components/panel/islands-panel.tsx +304 -304
  65. package/src/devtools/client/components/panel/network-panel.tsx +292 -292
  66. package/src/devtools/client/components/panel/panel-container.tsx +259 -259
  67. package/src/devtools/client/filters/context-filters.ts +282 -282
  68. package/src/devtools/client/filters/index.ts +16 -16
  69. package/src/devtools/client/index.ts +63 -63
  70. package/src/devtools/client/persistence.ts +335 -335
  71. package/src/devtools/client/state-manager.ts +478 -478
  72. package/src/devtools/design-tokens.ts +263 -263
  73. package/src/devtools/hook/create-hook.ts +207 -207
  74. package/src/devtools/hook/index.ts +13 -13
  75. package/src/devtools/index.ts +439 -439
  76. package/src/devtools/init.ts +266 -266
  77. package/src/devtools/protocol.ts +237 -237
  78. package/src/devtools/server/index.ts +17 -17
  79. package/src/devtools/server/source-context.ts +444 -444
  80. package/src/devtools/types.ts +319 -319
  81. package/src/devtools/worker/index.ts +25 -25
  82. package/src/devtools/worker/redaction-worker.ts +222 -222
  83. package/src/devtools/worker/worker-manager.ts +409 -409
  84. package/src/error/classifier.ts +2 -2
  85. package/src/error/domains.ts +265 -265
  86. package/src/error/formatter.ts +32 -32
  87. package/src/error/result.ts +46 -46
  88. package/src/error/stack-analyzer.ts +5 -0
  89. package/src/error/types.ts +6 -6
  90. package/src/errors/extractor.ts +409 -409
  91. package/src/errors/index.ts +19 -19
  92. package/src/filling/auth.ts +308 -308
  93. package/src/filling/context.ts +569 -569
  94. package/src/filling/deps.ts +238 -238
  95. package/src/generator/contract-glue.ts +2 -1
  96. package/src/generator/generate.ts +12 -10
  97. package/src/generator/index.ts +3 -3
  98. package/src/generator/templates.ts +80 -79
  99. package/src/guard/analyzer.ts +360 -360
  100. package/src/guard/ast-analyzer.ts +806 -806
  101. package/src/guard/auto-correct.ts +1 -1
  102. package/src/guard/check.ts +128 -128
  103. package/src/guard/contract-guard.ts +9 -9
  104. package/src/guard/file-type.test.ts +24 -24
  105. package/src/guard/presets/atomic.ts +70 -70
  106. package/src/guard/presets/clean.ts +77 -77
  107. package/src/guard/presets/cqrs.test.ts +35 -14
  108. package/src/guard/presets/fsd.ts +79 -79
  109. package/src/guard/presets/hexagonal.ts +68 -68
  110. package/src/guard/presets/index.ts +291 -291
  111. package/src/guard/reporter.ts +445 -445
  112. package/src/guard/rules.ts +12 -12
  113. package/src/guard/statistics.ts +578 -578
  114. package/src/guard/suggestions.ts +358 -358
  115. package/src/guard/types.ts +348 -348
  116. package/src/guard/validator.ts +834 -834
  117. package/src/guard/watcher.ts +404 -404
  118. package/src/index.ts +1 -0
  119. package/src/intent/index.ts +310 -310
  120. package/src/island/index.ts +304 -304
  121. package/src/logging/index.ts +22 -22
  122. package/src/logging/transports.ts +365 -365
  123. package/src/paths.test.ts +47 -0
  124. package/src/paths.ts +47 -0
  125. package/src/plugins/index.ts +38 -38
  126. package/src/plugins/registry.ts +377 -377
  127. package/src/plugins/types.ts +363 -363
  128. package/src/report/build.ts +1 -1
  129. package/src/report/index.ts +1 -1
  130. package/src/router/fs-patterns.ts +387 -387
  131. package/src/router/fs-routes.ts +344 -401
  132. package/src/router/fs-scanner.ts +497 -497
  133. package/src/router/fs-types.ts +270 -278
  134. package/src/router/index.ts +81 -81
  135. package/src/runtime/boundary.tsx +232 -232
  136. package/src/runtime/compose.ts +222 -222
  137. package/src/runtime/lifecycle.ts +381 -381
  138. package/src/runtime/logger.test.ts +345 -345
  139. package/src/runtime/logger.ts +677 -677
  140. package/src/runtime/router.test.ts +476 -476
  141. package/src/runtime/router.ts +105 -105
  142. package/src/runtime/security.ts +155 -155
  143. package/src/runtime/server.ts +24 -24
  144. package/src/runtime/session-key.ts +328 -328
  145. package/src/runtime/ssr.ts +367 -367
  146. package/src/runtime/streaming-ssr.ts +1245 -1245
  147. package/src/runtime/trace.ts +144 -144
  148. package/src/seo/index.ts +214 -214
  149. package/src/seo/integration/ssr.ts +307 -307
  150. package/src/seo/render/basic.ts +427 -427
  151. package/src/seo/render/index.ts +143 -143
  152. package/src/seo/render/jsonld.ts +539 -539
  153. package/src/seo/render/opengraph.ts +191 -191
  154. package/src/seo/render/robots.ts +116 -116
  155. package/src/seo/render/sitemap.ts +137 -137
  156. package/src/seo/render/twitter.ts +126 -126
  157. package/src/seo/resolve/index.ts +353 -353
  158. package/src/seo/resolve/opengraph.ts +143 -143
  159. package/src/seo/resolve/robots.ts +73 -73
  160. package/src/seo/resolve/title.ts +94 -94
  161. package/src/seo/resolve/twitter.ts +73 -73
  162. package/src/seo/resolve/url.ts +97 -97
  163. package/src/seo/routes/index.ts +290 -290
  164. package/src/seo/types.ts +575 -575
  165. package/src/slot/validator.ts +39 -39
  166. package/src/spec/index.ts +3 -3
  167. package/src/spec/load.ts +76 -76
  168. package/src/spec/lock.ts +56 -56
  169. package/src/utils/bun.ts +8 -8
  170. package/src/utils/lru-cache.ts +75 -75
  171. package/src/utils/safe-io.ts +188 -188
  172. package/src/utils/string-safe.ts +298 -298
  173. package/src/watcher/rules.ts +5 -5
@@ -1,568 +1,568 @@
1
- /**
2
- * Mandu Contract Registry
3
- * Build-time contract index for tooling, docs, and guard
4
- */
5
-
6
- import type { RoutesManifest } from "../spec/schema";
7
- import type { ContractSchema, MethodRequestSchema } from "./schema";
8
- import path from "path";
9
- import { createHash } from "crypto";
10
-
11
- const HTTP_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const;
12
-
13
- export interface ContractRegistryEntry {
14
- id: string;
15
- routeId: string;
16
- file: string;
17
- methods: string[];
18
- request: Record<string, {
19
- query: boolean;
20
- body: boolean;
21
- params: boolean;
22
- headers: boolean;
23
- }>;
24
- response: number[];
25
- schemas?: {
26
- request?: Record<string, {
27
- query?: SchemaSummary;
28
- body?: SchemaSummary;
29
- params?: SchemaSummary;
30
- headers?: SchemaSummary;
31
- }>;
32
- response?: Record<number, SchemaSummary | undefined>;
33
- };
34
- hash: string | null;
35
- description?: string;
36
- tags?: string[];
37
- normalize?: ContractSchema["normalize"];
38
- coerceQueryParams?: ContractSchema["coerceQueryParams"];
39
- version?: string;
40
- meta?: Record<string, unknown>;
41
- }
42
-
43
- export type SchemaSummary =
44
- | {
45
- type: "object";
46
- keys: string[];
47
- required: string[];
48
- }
49
- | {
50
- type: "enum";
51
- values: Array<string | number>;
52
- }
53
- | {
54
- type: "literal";
55
- value: string | number | boolean | null;
56
- }
57
- | {
58
- type: "other";
59
- typeName?: string;
60
- };
61
-
62
- export interface ContractRegistry {
63
- version: 1;
64
- generatedAt: string;
65
- contracts: ContractRegistryEntry[];
66
- }
67
-
68
- export interface ContractRegistryResult {
69
- registry: ContractRegistry;
70
- warnings: string[];
71
- }
72
-
73
- export interface ContractRegistryChange {
74
- id: string;
75
- routeId: string;
76
- severity: "major" | "minor" | "patch";
77
- changes: string[];
78
- before?: ContractRegistryEntry;
79
- after?: ContractRegistryEntry;
80
- }
81
-
82
- export interface ContractRegistryDiff {
83
- added: ContractRegistryEntry[];
84
- removed: ContractRegistryEntry[];
85
- changed: ContractRegistryChange[];
86
- summary: {
87
- major: number;
88
- minor: number;
89
- patch: number;
90
- };
91
- }
92
-
93
- async function loadContract(contractPath: string, rootDir: string): Promise<ContractSchema | null> {
94
- try {
95
- const fullPath = path.join(rootDir, contractPath);
96
- const module = await import(fullPath);
97
- return module.default ?? null;
98
- } catch {
99
- return null;
100
- }
101
- }
102
-
103
- async function computeFileHash(filePath: string): Promise<string | null> {
104
- try {
105
- const content = await Bun.file(filePath).text();
106
- return createHash("sha256").update(content).digest("hex");
107
- } catch {
108
- return null;
109
- }
110
- }
111
-
112
- function extractRequestInfo(contract: ContractSchema): ContractRegistryEntry["request"] {
113
- const requestInfo: ContractRegistryEntry["request"] = {};
114
-
115
- for (const method of HTTP_METHODS) {
116
- const methodSchema = contract.request[method] as MethodRequestSchema | undefined;
117
- if (!methodSchema) continue;
118
-
119
- requestInfo[method] = {
120
- query: Boolean(methodSchema.query),
121
- body: Boolean(methodSchema.body),
122
- params: Boolean(methodSchema.params),
123
- headers: Boolean(methodSchema.headers),
124
- };
125
- }
126
-
127
- return requestInfo;
128
- }
129
-
130
- function unwrapSchema(schema: any): any {
131
- let current = schema;
132
- let depth = 0;
133
-
134
- while (current && current._def && depth < 10) {
135
- const typeName = current._def.typeName as string | undefined;
136
- if (typeName === "ZodOptional" || typeName === "ZodDefault") {
137
- current = current._def.innerType;
138
- } else if (typeName === "ZodEffects") {
139
- current = current._def.schema;
140
- } else if (typeName === "ZodNullable") {
141
- current = current._def.innerType;
142
- } else if (typeName === "ZodBranded" || typeName === "ZodCatch") {
143
- current = current._def.type;
144
- } else {
145
- break;
146
- }
147
- depth += 1;
148
- }
149
-
150
- return current;
151
- }
152
-
153
- function isOptionalSchema(schema: any): boolean {
154
- let current = schema;
155
- let depth = 0;
156
- while (current && current._def && depth < 10) {
157
- const typeName = current._def.typeName as string | undefined;
158
- if (typeName === "ZodOptional" || typeName === "ZodDefault") {
159
- return true;
160
- }
161
- if (typeName === "ZodEffects" || typeName === "ZodNullable" || typeName === "ZodBranded" || typeName === "ZodCatch") {
162
- current = current._def.schema ?? current._def.innerType ?? current._def.type;
163
- depth += 1;
164
- continue;
165
- }
166
- break;
167
- }
168
- return false;
169
- }
170
-
171
- function summarizeSchema(schema: any): SchemaSummary | undefined {
172
- if (!schema || !schema._def) return undefined;
173
-
174
- const base = unwrapSchema(schema);
175
- const def = base?._def;
176
- if (!def) return undefined;
177
-
178
- const typeName = def.typeName as string | undefined;
179
-
180
- if (typeName === "ZodObject") {
181
- const shape = typeof def.shape === "function" ? def.shape() : def.shape;
182
- const keys = Object.keys(shape ?? {}).sort();
183
- const required = keys.filter((key) => !isOptionalSchema(shape[key]));
184
- return {
185
- type: "object",
186
- keys,
187
- required: required.sort(),
188
- };
189
- }
190
-
191
- if (typeName === "ZodEnum") {
192
- const values = Array.isArray(def.values) ? def.values.slice() : [];
193
- values.sort();
194
- return {
195
- type: "enum",
196
- values,
197
- };
198
- }
199
-
200
- if (typeName === "ZodNativeEnum") {
201
- const rawValues = def.values ? Object.values(def.values) : [];
202
- const values = rawValues.filter((v: any) => typeof v === "string" || typeof v === "number");
203
- values.sort();
204
- return {
205
- type: "enum",
206
- values,
207
- };
208
- }
209
-
210
- if (typeName === "ZodLiteral") {
211
- return {
212
- type: "literal",
213
- value: def.value as any,
214
- };
215
- }
216
-
217
- return {
218
- type: "other",
219
- typeName,
220
- };
221
- }
222
-
223
- function extractSchemaSummaries(contract: ContractSchema): ContractRegistryEntry["schemas"] {
224
- const request: Record<string, any> = {};
225
- const response: Record<number, SchemaSummary> = {};
226
-
227
- for (const method of HTTP_METHODS) {
228
- const methodSchema = contract.request[method] as MethodRequestSchema | undefined;
229
- if (!methodSchema) continue;
230
-
231
- request[method] = {
232
- query: methodSchema.query ? summarizeSchema(methodSchema.query) : undefined,
233
- body: methodSchema.body ? summarizeSchema(methodSchema.body) : undefined,
234
- params: methodSchema.params ? summarizeSchema(methodSchema.params) : undefined,
235
- headers: methodSchema.headers ? summarizeSchema(methodSchema.headers) : undefined,
236
- };
237
- }
238
-
239
- for (const [statusCode, schema] of Object.entries(contract.response)) {
240
- const code = Number(statusCode);
241
- if (Number.isNaN(code)) continue;
242
- if (!schema) continue;
243
-
244
- const actualSchema = (schema as any).schema ?? schema;
245
- response[code] = summarizeSchema(actualSchema);
246
- }
247
-
248
- return {
249
- request,
250
- response,
251
- };
252
- }
253
-
254
- function extractMethods(contract: ContractSchema): string[] {
255
- return HTTP_METHODS.filter((method) => Boolean(contract.request[method]));
256
- }
257
-
258
- function extractResponseCodes(contract: ContractSchema): number[] {
259
- return Object.keys(contract.response)
260
- .filter((key) => /^\d+$/.test(key))
261
- .map((key) => Number(key))
262
- .sort((a, b) => a - b);
263
- }
264
-
265
- export async function buildContractRegistry(
266
- manifest: RoutesManifest,
267
- rootDir: string
268
- ): Promise<ContractRegistryResult> {
269
- const warnings: string[] = [];
270
- const contracts: ContractRegistryEntry[] = [];
271
-
272
- for (const route of manifest.routes) {
273
- if (!route.contractModule) continue;
274
-
275
- const contract = await loadContract(route.contractModule, rootDir);
276
- if (!contract) {
277
- warnings.push(`Failed to load contract: ${route.contractModule} (routeId: ${route.id})`);
278
- continue;
279
- }
280
-
281
- const contractPath = path.join(rootDir, route.contractModule);
282
- const hash = await computeFileHash(contractPath);
283
- const id = contract.name ?? route.id;
284
-
285
- contracts.push({
286
- id,
287
- routeId: route.id,
288
- file: route.contractModule,
289
- methods: extractMethods(contract),
290
- request: extractRequestInfo(contract),
291
- response: extractResponseCodes(contract),
292
- schemas: extractSchemaSummaries(contract),
293
- hash,
294
- description: contract.description,
295
- tags: contract.tags,
296
- normalize: contract.normalize,
297
- coerceQueryParams: contract.coerceQueryParams,
298
- version: contract.version,
299
- meta: contract.meta,
300
- });
301
- }
302
-
303
- return {
304
- registry: {
305
- version: 1,
306
- generatedAt: new Date().toISOString(),
307
- contracts,
308
- },
309
- warnings,
310
- };
311
- }
312
-
313
- export async function writeContractRegistry(
314
- registryPath: string,
315
- registry: ContractRegistry
316
- ): Promise<void> {
317
- await Bun.write(registryPath, JSON.stringify(registry, null, 2));
318
- }
319
-
320
- export async function readContractRegistry(
321
- registryPath: string
322
- ): Promise<ContractRegistry | null> {
323
- try {
324
- const file = Bun.file(registryPath);
325
- const exists = await file.exists();
326
- if (!exists) return null;
327
- const content = await file.text();
328
- return JSON.parse(content) as ContractRegistry;
329
- } catch {
330
- return null;
331
- }
332
- }
333
-
334
- function diffArray<T>(prev: T[], next: T[]): { added: T[]; removed: T[] } {
335
- const prevSet = new Set(prev);
336
- const nextSet = new Set(next);
337
- const added = next.filter((item) => !prevSet.has(item));
338
- const removed = prev.filter((item) => !nextSet.has(item));
339
- return { added, removed };
340
- }
341
-
342
- function diffRequestShapes(
343
- prev: ContractRegistryEntry["request"],
344
- next: ContractRegistryEntry["request"]
345
- ): { major: string[]; minor: string[] } {
346
- const major: string[] = [];
347
- const minor: string[] = [];
348
- const methods = new Set([...Object.keys(prev), ...Object.keys(next)]);
349
-
350
- for (const method of methods) {
351
- const before = prev[method];
352
- const after = next[method];
353
- if (!before || !after) continue;
354
-
355
- for (const key of ["query", "body", "params", "headers"] as const) {
356
- const beforeHas = Boolean(before[key]);
357
- const afterHas = Boolean(after[key]);
358
- if (beforeHas && !afterHas) {
359
- major.push(`${method}.${key} removed`);
360
- } else if (!beforeHas && afterHas) {
361
- minor.push(`${method}.${key} added`);
362
- }
363
- }
364
- }
365
-
366
- return { major, minor };
367
- }
368
-
369
- function diffSchemaSummary(
370
- prev?: SchemaSummary,
371
- next?: SchemaSummary
372
- ): { major: string[]; minor: string[] } {
373
- if (!prev || !next) return { major: [], minor: [] };
374
-
375
- if (prev.type !== next.type) {
376
- return {
377
- major: [`schema type changed: ${prev.type} -> ${next.type}`],
378
- minor: [],
379
- };
380
- }
381
-
382
- if (prev.type === "object" && next.type === "object") {
383
- const prevKeys = new Set(prev.keys);
384
- const nextKeys = new Set(next.keys);
385
-
386
- const removed = prev.keys.filter((k) => !nextKeys.has(k));
387
- const added = next.keys.filter((k) => !prevKeys.has(k));
388
-
389
- const prevRequired = new Set(prev.required);
390
- const nextRequired = new Set(next.required);
391
-
392
- const major: string[] = [];
393
- const minor: string[] = [];
394
-
395
- if (removed.length > 0) {
396
- major.push(`fields removed: ${removed.join(", ")}`);
397
- }
398
- if (added.length > 0) {
399
- minor.push(`fields added: ${added.join(", ")}`);
400
- }
401
-
402
- for (const key of prev.keys) {
403
- if (!nextKeys.has(key)) continue;
404
- const wasRequired = prevRequired.has(key);
405
- const nowRequired = nextRequired.has(key);
406
- if (wasRequired && !nowRequired) {
407
- minor.push(`field optionalized: ${key}`);
408
- } else if (!wasRequired && nowRequired) {
409
- major.push(`field required: ${key}`);
410
- }
411
- }
412
-
413
- return { major, minor };
414
- }
415
-
416
- if (prev.type === "enum" && next.type === "enum") {
417
- const prevValues = new Set(prev.values.map(String));
418
- const nextValues = new Set(next.values.map(String));
419
- const removed = prev.values.filter((v) => !nextValues.has(String(v)));
420
- const added = next.values.filter((v) => !prevValues.has(String(v)));
421
- return {
422
- major: removed.length > 0 ? [`enum values removed: ${removed.join(", ")}`] : [],
423
- minor: added.length > 0 ? [`enum values added: ${added.join(", ")}`] : [],
424
- };
425
- }
426
-
427
- if (prev.type === "literal" && next.type === "literal") {
428
- if (prev.value !== next.value) {
429
- return { major: [`literal changed: ${String(prev.value)} -> ${String(next.value)}`], minor: [] };
430
- }
431
- }
432
-
433
- if (prev.type === "other" && next.type === "other") {
434
- if (prev.typeName !== next.typeName) {
435
- return { major: [`schema changed: ${prev.typeName ?? "unknown"} -> ${next.typeName ?? "unknown"}`], minor: [] };
436
- }
437
- }
438
-
439
- return { major: [], minor: [] };
440
- }
441
-
442
- export function diffContractRegistry(
443
- prev: ContractRegistry,
444
- next: ContractRegistry
445
- ): ContractRegistryDiff {
446
- const prevMap = new Map(prev.contracts.map((c) => [c.id, c]));
447
- const nextMap = new Map(next.contracts.map((c) => [c.id, c]));
448
-
449
- const added: ContractRegistryEntry[] = [];
450
- const removed: ContractRegistryEntry[] = [];
451
- const changed: ContractRegistryChange[] = [];
452
-
453
- for (const [id, nextEntry] of nextMap.entries()) {
454
- const prevEntry = prevMap.get(id);
455
- if (!prevEntry) {
456
- added.push(nextEntry);
457
- continue;
458
- }
459
-
460
- const changes: string[] = [];
461
- let severity: ContractRegistryChange["severity"] = "patch";
462
-
463
- const methodDiff = diffArray(prevEntry.methods, nextEntry.methods);
464
- if (methodDiff.removed.length > 0) {
465
- changes.push(`methods removed: ${methodDiff.removed.join(", ")}`);
466
- severity = "major";
467
- }
468
- if (methodDiff.added.length > 0) {
469
- changes.push(`methods added: ${methodDiff.added.join(", ")}`);
470
- if (severity !== "major") severity = "minor";
471
- }
472
-
473
- const responseDiff = diffArray(prevEntry.response, nextEntry.response);
474
- if (responseDiff.removed.length > 0) {
475
- changes.push(`responses removed: ${responseDiff.removed.join(", ")}`);
476
- severity = "major";
477
- }
478
- if (responseDiff.added.length > 0) {
479
- changes.push(`responses added: ${responseDiff.added.join(", ")}`);
480
- if (severity !== "major") severity = "minor";
481
- }
482
-
483
- const requestDiff = diffRequestShapes(prevEntry.request, nextEntry.request);
484
- if (requestDiff.major.length > 0) {
485
- changes.push(...requestDiff.major);
486
- severity = "major";
487
- }
488
- if (requestDiff.minor.length > 0) {
489
- changes.push(...requestDiff.minor);
490
- if (severity !== "major") severity = "minor";
491
- }
492
-
493
- const prevReqSchemas = prevEntry.schemas?.request ?? {};
494
- const nextReqSchemas = nextEntry.schemas?.request ?? {};
495
- for (const method of Object.keys(prevReqSchemas)) {
496
- if (!nextReqSchemas[method]) continue;
497
- const prevParts = prevReqSchemas[method] ?? {};
498
- const nextParts = nextReqSchemas[method] ?? {};
499
- for (const part of ["query", "body", "params", "headers"] as const) {
500
- if (!prevParts[part] || !nextParts[part]) continue;
501
- const diff = diffSchemaSummary(prevParts[part], nextParts[part]);
502
- if (diff.major.length > 0) {
503
- changes.push(...diff.major.map((msg) => `${method}.${part}: ${msg}`));
504
- severity = "major";
505
- }
506
- if (diff.minor.length > 0) {
507
- changes.push(...diff.minor.map((msg) => `${method}.${part}: ${msg}`));
508
- if (severity !== "major") severity = "minor";
509
- }
510
- }
511
- }
512
-
513
- const prevResSchemas = prevEntry.schemas?.response ?? {};
514
- const nextResSchemas = nextEntry.schemas?.response ?? {};
515
- for (const [code, prevSchema] of Object.entries(prevResSchemas)) {
516
- const status = Number(code);
517
- const nextSchema = nextResSchemas[status];
518
- if (!nextSchema) continue;
519
- const diff = diffSchemaSummary(prevSchema, nextSchema);
520
- if (diff.major.length > 0) {
521
- changes.push(...diff.major.map((msg) => `response.${status}: ${msg}`));
522
- severity = "major";
523
- }
524
- if (diff.minor.length > 0) {
525
- changes.push(...diff.minor.map((msg) => `response.${status}: ${msg}`));
526
- if (severity !== "major") severity = "minor";
527
- }
528
- }
529
-
530
- if (prevEntry.description !== nextEntry.description) {
531
- changes.push("description changed");
532
- }
533
- if (JSON.stringify(prevEntry.tags ?? []) !== JSON.stringify(nextEntry.tags ?? [])) {
534
- changes.push("tags changed");
535
- }
536
- if (prevEntry.version !== nextEntry.version) {
537
- changes.push("version changed");
538
- }
539
- if (prevEntry.hash !== nextEntry.hash && changes.length === 0) {
540
- changes.push("contract content changed");
541
- }
542
-
543
- if (changes.length > 0) {
544
- changed.push({
545
- id,
546
- routeId: nextEntry.routeId,
547
- severity,
548
- changes,
549
- before: prevEntry,
550
- after: nextEntry,
551
- });
552
- }
553
- }
554
-
555
- for (const [id, prevEntry] of prevMap.entries()) {
556
- if (!nextMap.has(id)) {
557
- removed.push(prevEntry);
558
- }
559
- }
560
-
561
- const summary = {
562
- major: changed.filter((c) => c.severity === "major").length + removed.length,
563
- minor: changed.filter((c) => c.severity === "minor").length + added.length,
564
- patch: changed.filter((c) => c.severity === "patch").length,
565
- };
566
-
567
- return { added, removed, changed, summary };
568
- }
1
+ /**
2
+ * Mandu Contract Registry
3
+ * Build-time contract index for tooling, docs, and guard
4
+ */
5
+
6
+ import type { RoutesManifest } from "../spec/schema";
7
+ import type { ContractSchema, MethodRequestSchema } from "./schema";
8
+ import path from "path";
9
+ import { createHash } from "crypto";
10
+
11
+ const HTTP_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const;
12
+
13
+ export interface ContractRegistryEntry {
14
+ id: string;
15
+ routeId: string;
16
+ file: string;
17
+ methods: string[];
18
+ request: Record<string, {
19
+ query: boolean;
20
+ body: boolean;
21
+ params: boolean;
22
+ headers: boolean;
23
+ }>;
24
+ response: number[];
25
+ schemas?: {
26
+ request?: Record<string, {
27
+ query?: SchemaSummary;
28
+ body?: SchemaSummary;
29
+ params?: SchemaSummary;
30
+ headers?: SchemaSummary;
31
+ }>;
32
+ response?: Record<number, SchemaSummary | undefined>;
33
+ };
34
+ hash: string | null;
35
+ description?: string;
36
+ tags?: string[];
37
+ normalize?: ContractSchema["normalize"];
38
+ coerceQueryParams?: ContractSchema["coerceQueryParams"];
39
+ version?: string;
40
+ meta?: Record<string, unknown>;
41
+ }
42
+
43
+ export type SchemaSummary =
44
+ | {
45
+ type: "object";
46
+ keys: string[];
47
+ required: string[];
48
+ }
49
+ | {
50
+ type: "enum";
51
+ values: Array<string | number>;
52
+ }
53
+ | {
54
+ type: "literal";
55
+ value: string | number | boolean | null;
56
+ }
57
+ | {
58
+ type: "other";
59
+ typeName?: string;
60
+ };
61
+
62
+ export interface ContractRegistry {
63
+ version: 1;
64
+ generatedAt: string;
65
+ contracts: ContractRegistryEntry[];
66
+ }
67
+
68
+ export interface ContractRegistryResult {
69
+ registry: ContractRegistry;
70
+ warnings: string[];
71
+ }
72
+
73
+ export interface ContractRegistryChange {
74
+ id: string;
75
+ routeId: string;
76
+ severity: "major" | "minor" | "patch";
77
+ changes: string[];
78
+ before?: ContractRegistryEntry;
79
+ after?: ContractRegistryEntry;
80
+ }
81
+
82
+ export interface ContractRegistryDiff {
83
+ added: ContractRegistryEntry[];
84
+ removed: ContractRegistryEntry[];
85
+ changed: ContractRegistryChange[];
86
+ summary: {
87
+ major: number;
88
+ minor: number;
89
+ patch: number;
90
+ };
91
+ }
92
+
93
+ async function loadContract(contractPath: string, rootDir: string): Promise<ContractSchema | null> {
94
+ try {
95
+ const fullPath = path.join(rootDir, contractPath);
96
+ const module = await import(fullPath);
97
+ return module.default ?? null;
98
+ } catch {
99
+ return null;
100
+ }
101
+ }
102
+
103
+ async function computeFileHash(filePath: string): Promise<string | null> {
104
+ try {
105
+ const content = await Bun.file(filePath).text();
106
+ return createHash("sha256").update(content).digest("hex");
107
+ } catch {
108
+ return null;
109
+ }
110
+ }
111
+
112
+ function extractRequestInfo(contract: ContractSchema): ContractRegistryEntry["request"] {
113
+ const requestInfo: ContractRegistryEntry["request"] = {};
114
+
115
+ for (const method of HTTP_METHODS) {
116
+ const methodSchema = contract.request[method] as MethodRequestSchema | undefined;
117
+ if (!methodSchema) continue;
118
+
119
+ requestInfo[method] = {
120
+ query: Boolean(methodSchema.query),
121
+ body: Boolean(methodSchema.body),
122
+ params: Boolean(methodSchema.params),
123
+ headers: Boolean(methodSchema.headers),
124
+ };
125
+ }
126
+
127
+ return requestInfo;
128
+ }
129
+
130
+ function unwrapSchema(schema: any): any {
131
+ let current = schema;
132
+ let depth = 0;
133
+
134
+ while (current && current._def && depth < 10) {
135
+ const typeName = current._def.typeName as string | undefined;
136
+ if (typeName === "ZodOptional" || typeName === "ZodDefault") {
137
+ current = current._def.innerType;
138
+ } else if (typeName === "ZodEffects") {
139
+ current = current._def.schema;
140
+ } else if (typeName === "ZodNullable") {
141
+ current = current._def.innerType;
142
+ } else if (typeName === "ZodBranded" || typeName === "ZodCatch") {
143
+ current = current._def.type;
144
+ } else {
145
+ break;
146
+ }
147
+ depth += 1;
148
+ }
149
+
150
+ return current;
151
+ }
152
+
153
+ function isOptionalSchema(schema: any): boolean {
154
+ let current = schema;
155
+ let depth = 0;
156
+ while (current && current._def && depth < 10) {
157
+ const typeName = current._def.typeName as string | undefined;
158
+ if (typeName === "ZodOptional" || typeName === "ZodDefault") {
159
+ return true;
160
+ }
161
+ if (typeName === "ZodEffects" || typeName === "ZodNullable" || typeName === "ZodBranded" || typeName === "ZodCatch") {
162
+ current = current._def.schema ?? current._def.innerType ?? current._def.type;
163
+ depth += 1;
164
+ continue;
165
+ }
166
+ break;
167
+ }
168
+ return false;
169
+ }
170
+
171
+ function summarizeSchema(schema: any): SchemaSummary | undefined {
172
+ if (!schema || !schema._def) return undefined;
173
+
174
+ const base = unwrapSchema(schema);
175
+ const def = base?._def;
176
+ if (!def) return undefined;
177
+
178
+ const typeName = def.typeName as string | undefined;
179
+
180
+ if (typeName === "ZodObject") {
181
+ const shape = typeof def.shape === "function" ? def.shape() : def.shape;
182
+ const keys = Object.keys(shape ?? {}).sort();
183
+ const required = keys.filter((key) => !isOptionalSchema(shape[key]));
184
+ return {
185
+ type: "object",
186
+ keys,
187
+ required: required.sort(),
188
+ };
189
+ }
190
+
191
+ if (typeName === "ZodEnum") {
192
+ const values = Array.isArray(def.values) ? def.values.slice() : [];
193
+ values.sort();
194
+ return {
195
+ type: "enum",
196
+ values,
197
+ };
198
+ }
199
+
200
+ if (typeName === "ZodNativeEnum") {
201
+ const rawValues = def.values ? Object.values(def.values) : [];
202
+ const values = rawValues.filter((v: any) => typeof v === "string" || typeof v === "number");
203
+ values.sort();
204
+ return {
205
+ type: "enum",
206
+ values,
207
+ };
208
+ }
209
+
210
+ if (typeName === "ZodLiteral") {
211
+ return {
212
+ type: "literal",
213
+ value: def.value as any,
214
+ };
215
+ }
216
+
217
+ return {
218
+ type: "other",
219
+ typeName,
220
+ };
221
+ }
222
+
223
+ function extractSchemaSummaries(contract: ContractSchema): ContractRegistryEntry["schemas"] {
224
+ const request: Record<string, any> = {};
225
+ const response: Record<number, SchemaSummary> = {};
226
+
227
+ for (const method of HTTP_METHODS) {
228
+ const methodSchema = contract.request[method] as MethodRequestSchema | undefined;
229
+ if (!methodSchema) continue;
230
+
231
+ request[method] = {
232
+ query: methodSchema.query ? summarizeSchema(methodSchema.query) : undefined,
233
+ body: methodSchema.body ? summarizeSchema(methodSchema.body) : undefined,
234
+ params: methodSchema.params ? summarizeSchema(methodSchema.params) : undefined,
235
+ headers: methodSchema.headers ? summarizeSchema(methodSchema.headers) : undefined,
236
+ };
237
+ }
238
+
239
+ for (const [statusCode, schema] of Object.entries(contract.response)) {
240
+ const code = Number(statusCode);
241
+ if (Number.isNaN(code)) continue;
242
+ if (!schema) continue;
243
+
244
+ const actualSchema = (schema as any).schema ?? schema;
245
+ response[code] = summarizeSchema(actualSchema);
246
+ }
247
+
248
+ return {
249
+ request,
250
+ response,
251
+ };
252
+ }
253
+
254
+ function extractMethods(contract: ContractSchema): string[] {
255
+ return HTTP_METHODS.filter((method) => Boolean(contract.request[method]));
256
+ }
257
+
258
+ function extractResponseCodes(contract: ContractSchema): number[] {
259
+ return Object.keys(contract.response)
260
+ .filter((key) => /^\d+$/.test(key))
261
+ .map((key) => Number(key))
262
+ .sort((a, b) => a - b);
263
+ }
264
+
265
+ export async function buildContractRegistry(
266
+ manifest: RoutesManifest,
267
+ rootDir: string
268
+ ): Promise<ContractRegistryResult> {
269
+ const warnings: string[] = [];
270
+ const contracts: ContractRegistryEntry[] = [];
271
+
272
+ for (const route of manifest.routes) {
273
+ if (!route.contractModule) continue;
274
+
275
+ const contract = await loadContract(route.contractModule, rootDir);
276
+ if (!contract) {
277
+ warnings.push(`Failed to load contract: ${route.contractModule} (routeId: ${route.id})`);
278
+ continue;
279
+ }
280
+
281
+ const contractPath = path.join(rootDir, route.contractModule);
282
+ const hash = await computeFileHash(contractPath);
283
+ const id = contract.name ?? route.id;
284
+
285
+ contracts.push({
286
+ id,
287
+ routeId: route.id,
288
+ file: route.contractModule,
289
+ methods: extractMethods(contract),
290
+ request: extractRequestInfo(contract),
291
+ response: extractResponseCodes(contract),
292
+ schemas: extractSchemaSummaries(contract),
293
+ hash,
294
+ description: contract.description,
295
+ tags: contract.tags,
296
+ normalize: contract.normalize,
297
+ coerceQueryParams: contract.coerceQueryParams,
298
+ version: contract.version,
299
+ meta: contract.meta,
300
+ });
301
+ }
302
+
303
+ return {
304
+ registry: {
305
+ version: 1,
306
+ generatedAt: new Date().toISOString(),
307
+ contracts,
308
+ },
309
+ warnings,
310
+ };
311
+ }
312
+
313
+ export async function writeContractRegistry(
314
+ registryPath: string,
315
+ registry: ContractRegistry
316
+ ): Promise<void> {
317
+ await Bun.write(registryPath, JSON.stringify(registry, null, 2));
318
+ }
319
+
320
+ export async function readContractRegistry(
321
+ registryPath: string
322
+ ): Promise<ContractRegistry | null> {
323
+ try {
324
+ const file = Bun.file(registryPath);
325
+ const exists = await file.exists();
326
+ if (!exists) return null;
327
+ const content = await file.text();
328
+ return JSON.parse(content) as ContractRegistry;
329
+ } catch {
330
+ return null;
331
+ }
332
+ }
333
+
334
+ function diffArray<T>(prev: T[], next: T[]): { added: T[]; removed: T[] } {
335
+ const prevSet = new Set(prev);
336
+ const nextSet = new Set(next);
337
+ const added = next.filter((item) => !prevSet.has(item));
338
+ const removed = prev.filter((item) => !nextSet.has(item));
339
+ return { added, removed };
340
+ }
341
+
342
+ function diffRequestShapes(
343
+ prev: ContractRegistryEntry["request"],
344
+ next: ContractRegistryEntry["request"]
345
+ ): { major: string[]; minor: string[] } {
346
+ const major: string[] = [];
347
+ const minor: string[] = [];
348
+ const methods = new Set([...Object.keys(prev), ...Object.keys(next)]);
349
+
350
+ for (const method of methods) {
351
+ const before = prev[method];
352
+ const after = next[method];
353
+ if (!before || !after) continue;
354
+
355
+ for (const key of ["query", "body", "params", "headers"] as const) {
356
+ const beforeHas = Boolean(before[key]);
357
+ const afterHas = Boolean(after[key]);
358
+ if (beforeHas && !afterHas) {
359
+ major.push(`${method}.${key} removed`);
360
+ } else if (!beforeHas && afterHas) {
361
+ minor.push(`${method}.${key} added`);
362
+ }
363
+ }
364
+ }
365
+
366
+ return { major, minor };
367
+ }
368
+
369
+ function diffSchemaSummary(
370
+ prev?: SchemaSummary,
371
+ next?: SchemaSummary
372
+ ): { major: string[]; minor: string[] } {
373
+ if (!prev || !next) return { major: [], minor: [] };
374
+
375
+ if (prev.type !== next.type) {
376
+ return {
377
+ major: [`schema type changed: ${prev.type} -> ${next.type}`],
378
+ minor: [],
379
+ };
380
+ }
381
+
382
+ if (prev.type === "object" && next.type === "object") {
383
+ const prevKeys = new Set(prev.keys);
384
+ const nextKeys = new Set(next.keys);
385
+
386
+ const removed = prev.keys.filter((k) => !nextKeys.has(k));
387
+ const added = next.keys.filter((k) => !prevKeys.has(k));
388
+
389
+ const prevRequired = new Set(prev.required);
390
+ const nextRequired = new Set(next.required);
391
+
392
+ const major: string[] = [];
393
+ const minor: string[] = [];
394
+
395
+ if (removed.length > 0) {
396
+ major.push(`fields removed: ${removed.join(", ")}`);
397
+ }
398
+ if (added.length > 0) {
399
+ minor.push(`fields added: ${added.join(", ")}`);
400
+ }
401
+
402
+ for (const key of prev.keys) {
403
+ if (!nextKeys.has(key)) continue;
404
+ const wasRequired = prevRequired.has(key);
405
+ const nowRequired = nextRequired.has(key);
406
+ if (wasRequired && !nowRequired) {
407
+ minor.push(`field optionalized: ${key}`);
408
+ } else if (!wasRequired && nowRequired) {
409
+ major.push(`field required: ${key}`);
410
+ }
411
+ }
412
+
413
+ return { major, minor };
414
+ }
415
+
416
+ if (prev.type === "enum" && next.type === "enum") {
417
+ const prevValues = new Set(prev.values.map(String));
418
+ const nextValues = new Set(next.values.map(String));
419
+ const removed = prev.values.filter((v) => !nextValues.has(String(v)));
420
+ const added = next.values.filter((v) => !prevValues.has(String(v)));
421
+ return {
422
+ major: removed.length > 0 ? [`enum values removed: ${removed.join(", ")}`] : [],
423
+ minor: added.length > 0 ? [`enum values added: ${added.join(", ")}`] : [],
424
+ };
425
+ }
426
+
427
+ if (prev.type === "literal" && next.type === "literal") {
428
+ if (prev.value !== next.value) {
429
+ return { major: [`literal changed: ${String(prev.value)} -> ${String(next.value)}`], minor: [] };
430
+ }
431
+ }
432
+
433
+ if (prev.type === "other" && next.type === "other") {
434
+ if (prev.typeName !== next.typeName) {
435
+ return { major: [`schema changed: ${prev.typeName ?? "unknown"} -> ${next.typeName ?? "unknown"}`], minor: [] };
436
+ }
437
+ }
438
+
439
+ return { major: [], minor: [] };
440
+ }
441
+
442
+ export function diffContractRegistry(
443
+ prev: ContractRegistry,
444
+ next: ContractRegistry
445
+ ): ContractRegistryDiff {
446
+ const prevMap = new Map(prev.contracts.map((c) => [c.id, c]));
447
+ const nextMap = new Map(next.contracts.map((c) => [c.id, c]));
448
+
449
+ const added: ContractRegistryEntry[] = [];
450
+ const removed: ContractRegistryEntry[] = [];
451
+ const changed: ContractRegistryChange[] = [];
452
+
453
+ for (const [id, nextEntry] of nextMap.entries()) {
454
+ const prevEntry = prevMap.get(id);
455
+ if (!prevEntry) {
456
+ added.push(nextEntry);
457
+ continue;
458
+ }
459
+
460
+ const changes: string[] = [];
461
+ let severity: ContractRegistryChange["severity"] = "patch";
462
+
463
+ const methodDiff = diffArray(prevEntry.methods, nextEntry.methods);
464
+ if (methodDiff.removed.length > 0) {
465
+ changes.push(`methods removed: ${methodDiff.removed.join(", ")}`);
466
+ severity = "major";
467
+ }
468
+ if (methodDiff.added.length > 0) {
469
+ changes.push(`methods added: ${methodDiff.added.join(", ")}`);
470
+ if (severity !== "major") severity = "minor";
471
+ }
472
+
473
+ const responseDiff = diffArray(prevEntry.response, nextEntry.response);
474
+ if (responseDiff.removed.length > 0) {
475
+ changes.push(`responses removed: ${responseDiff.removed.join(", ")}`);
476
+ severity = "major";
477
+ }
478
+ if (responseDiff.added.length > 0) {
479
+ changes.push(`responses added: ${responseDiff.added.join(", ")}`);
480
+ if (severity !== "major") severity = "minor";
481
+ }
482
+
483
+ const requestDiff = diffRequestShapes(prevEntry.request, nextEntry.request);
484
+ if (requestDiff.major.length > 0) {
485
+ changes.push(...requestDiff.major);
486
+ severity = "major";
487
+ }
488
+ if (requestDiff.minor.length > 0) {
489
+ changes.push(...requestDiff.minor);
490
+ if (severity !== "major") severity = "minor";
491
+ }
492
+
493
+ const prevReqSchemas = prevEntry.schemas?.request ?? {};
494
+ const nextReqSchemas = nextEntry.schemas?.request ?? {};
495
+ for (const method of Object.keys(prevReqSchemas)) {
496
+ if (!nextReqSchemas[method]) continue;
497
+ const prevParts = prevReqSchemas[method] ?? {};
498
+ const nextParts = nextReqSchemas[method] ?? {};
499
+ for (const part of ["query", "body", "params", "headers"] as const) {
500
+ if (!prevParts[part] || !nextParts[part]) continue;
501
+ const diff = diffSchemaSummary(prevParts[part], nextParts[part]);
502
+ if (diff.major.length > 0) {
503
+ changes.push(...diff.major.map((msg) => `${method}.${part}: ${msg}`));
504
+ severity = "major";
505
+ }
506
+ if (diff.minor.length > 0) {
507
+ changes.push(...diff.minor.map((msg) => `${method}.${part}: ${msg}`));
508
+ if (severity !== "major") severity = "minor";
509
+ }
510
+ }
511
+ }
512
+
513
+ const prevResSchemas = prevEntry.schemas?.response ?? {};
514
+ const nextResSchemas = nextEntry.schemas?.response ?? {};
515
+ for (const [code, prevSchema] of Object.entries(prevResSchemas)) {
516
+ const status = Number(code);
517
+ const nextSchema = nextResSchemas[status];
518
+ if (!nextSchema) continue;
519
+ const diff = diffSchemaSummary(prevSchema, nextSchema);
520
+ if (diff.major.length > 0) {
521
+ changes.push(...diff.major.map((msg) => `response.${status}: ${msg}`));
522
+ severity = "major";
523
+ }
524
+ if (diff.minor.length > 0) {
525
+ changes.push(...diff.minor.map((msg) => `response.${status}: ${msg}`));
526
+ if (severity !== "major") severity = "minor";
527
+ }
528
+ }
529
+
530
+ if (prevEntry.description !== nextEntry.description) {
531
+ changes.push("description changed");
532
+ }
533
+ if (JSON.stringify(prevEntry.tags ?? []) !== JSON.stringify(nextEntry.tags ?? [])) {
534
+ changes.push("tags changed");
535
+ }
536
+ if (prevEntry.version !== nextEntry.version) {
537
+ changes.push("version changed");
538
+ }
539
+ if (prevEntry.hash !== nextEntry.hash && changes.length === 0) {
540
+ changes.push("contract content changed");
541
+ }
542
+
543
+ if (changes.length > 0) {
544
+ changed.push({
545
+ id,
546
+ routeId: nextEntry.routeId,
547
+ severity,
548
+ changes,
549
+ before: prevEntry,
550
+ after: nextEntry,
551
+ });
552
+ }
553
+ }
554
+
555
+ for (const [id, prevEntry] of prevMap.entries()) {
556
+ if (!nextMap.has(id)) {
557
+ removed.push(prevEntry);
558
+ }
559
+ }
560
+
561
+ const summary = {
562
+ major: changed.filter((c) => c.severity === "major").length + removed.length,
563
+ minor: changed.filter((c) => c.severity === "minor").length + added.length,
564
+ patch: changed.filter((c) => c.severity === "patch").length,
565
+ };
566
+
567
+ return { added, removed, changed, summary };
568
+ }