@htilabs/platform-common-models 0.8.13 → 0.8.14

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 (72) hide show
  1. package/README.md +44 -0
  2. package/dist/config/index.cjs +4 -0
  3. package/dist/config/index.cjs.map +1 -1
  4. package/dist/config/index.d.cts +16 -1
  5. package/dist/config/index.d.ts +16 -1
  6. package/dist/config/index.js +2 -0
  7. package/dist/config/index.js.map +1 -1
  8. package/dist/constants/index.cjs +10 -0
  9. package/dist/constants/index.cjs.map +1 -1
  10. package/dist/constants/index.d.cts +1 -1
  11. package/dist/constants/index.d.ts +1 -1
  12. package/dist/constants/index.js +10 -0
  13. package/dist/constants/index.js.map +1 -1
  14. package/dist/cvcs/index.cjs +505 -2
  15. package/dist/cvcs/index.cjs.map +1 -1
  16. package/dist/cvcs/index.d.cts +2642 -2084
  17. package/dist/cvcs/index.d.ts +2642 -2084
  18. package/dist/cvcs/index.js +500 -2
  19. package/dist/cvcs/index.js.map +1 -1
  20. package/dist/{index-I-DiX9eQ.d.ts → index-CDoqrfc1.d.ts} +1 -1
  21. package/dist/{index-CoVr1jIV.d.cts → index-Zu_mOJRn.d.cts} +1 -1
  22. package/dist/index.cjs +582 -4
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +111 -7
  25. package/dist/index.d.ts +111 -7
  26. package/dist/index.js +571 -4
  27. package/dist/index.js.map +1 -1
  28. package/dist/models/index.d.cts +2 -2
  29. package/dist/models/index.d.ts +2 -2
  30. package/dist/mongo/index.cjs +383 -0
  31. package/dist/mongo/index.cjs.map +1 -0
  32. package/dist/mongo/index.d.cts +221 -0
  33. package/dist/mongo/index.d.ts +221 -0
  34. package/dist/mongo/index.js +351 -0
  35. package/dist/mongo/index.js.map +1 -0
  36. package/dist/{platform-auth-BfCMuAlC.d.cts → platform-auth-AsfnIXfK.d.cts} +2 -2
  37. package/dist/{platform-auth-BfCMuAlC.d.ts → platform-auth-AsfnIXfK.d.ts} +2 -2
  38. package/dist/schemas/index.cjs +5 -1
  39. package/dist/schemas/index.cjs.map +1 -1
  40. package/dist/schemas/index.d.cts +29 -25
  41. package/dist/schemas/index.d.ts +29 -25
  42. package/dist/schemas/index.js +5 -1
  43. package/dist/schemas/index.js.map +1 -1
  44. package/dist/types/index.d.cts +2 -2
  45. package/dist/types/index.d.ts +2 -2
  46. package/package.json +10 -1
  47. package/src/__sot__/coreIndicators.pin.json +6 -1
  48. package/src/__tests__/__snapshots__/cvc-sot.snap +18 -2
  49. package/src/__tests__/cvc-136-scoring-invariant.test.ts +6 -3
  50. package/src/__tests__/cvc-drift-gate.test.ts +14 -3
  51. package/src/__tests__/cvc-sot-snapshot.test.ts +13 -1
  52. package/src/__tests__/cvc-structural.test.ts +6 -0
  53. package/src/__tests__/mongo-verifier.test.ts +502 -0
  54. package/src/config/index.ts +8 -2
  55. package/src/constants/ic.ts +31 -2
  56. package/src/cvcs/assertionStatus.v1.0.0.json +80 -0
  57. package/src/cvcs/coreIndicators.v1.0.0.json +67 -2
  58. package/src/cvcs/createdBySystem.v1.0.0.json +89 -0
  59. package/src/cvcs/hmisSex.v1.0.0.json +89 -0
  60. package/src/cvcs/index.ts +18 -0
  61. package/src/cvcs/minorAdultStatus.v1.0.0.json +80 -0
  62. package/src/cvcs/subjectType.v1.0.0.json +80 -0
  63. package/src/generated/cvc-enums.ts +76 -2
  64. package/src/index.ts +20 -0
  65. package/src/mongo/catalog.ts +65 -0
  66. package/src/mongo/contract.ts +157 -0
  67. package/src/mongo/index.ts +343 -0
  68. package/src/mongo/similarity.ts +133 -0
  69. package/src/mongo/uri.ts +53 -0
  70. package/src/schemas/common.ts +4 -0
  71. package/dist/{cvcEntry-BCj1OWqL.d.cts → cvcEntry-0BIB9sen.d.cts} +8 -8
  72. package/dist/{cvcEntry-BCj1OWqL.d.ts → cvcEntry-0BIB9sen.d.ts} +8 -8
@@ -0,0 +1,133 @@
1
+ /**
2
+ * @htilabs/platform-common-models — name-similarity heuristic
3
+ *
4
+ * A missing database or collection is AMBIGUOUS. MongoDB creates namespaces
5
+ * lazily, on first write, so "absent" means either:
6
+ *
7
+ * (a) a typo — the name is wrong and the real one exists nearby, or
8
+ * (b) unseeded — the environment is new and nothing has been written yet.
9
+ *
10
+ * Guessing wrong in direction (b) is expensive: a fatal check would ground
11
+ * every cold-start ephemeral deploy. So absence alone is never fatal. Only
12
+ * absence WITH a plausible near-neighbour is treated as a typo.
13
+ */
14
+
15
+ /** Maximum edit distance at which a neighbouring name is considered a typo. */
16
+ export const TYPO_DISTANCE_THRESHOLD = 2;
17
+
18
+ /**
19
+ * Optimal String Alignment distance (Damerau-Levenshtein restricted to
20
+ * adjacent transpositions), bounded: returns `max + 1` as soon as the true
21
+ * distance is known to exceed `max`. Bounding keeps this cheap across the
22
+ * ~90-database catalogue scanned on each lookup.
23
+ *
24
+ * A transposition MUST cost 1, not 2. `platform-dve` is a transposition of
25
+ * `platform-dev`, but plain Levenshtein scores it 2 -- tying it with the
26
+ * unrelated PRODUCTION name `platform-db` and letting iteration order pick.
27
+ * Steering an operator toward a production database because they typo'd a dev
28
+ * one is a worse failure than suggesting nothing at all.
29
+ */
30
+ export function editDistance(a: string, b: string, max = Infinity): number {
31
+ if (a === b) return 0;
32
+ if (Math.abs(a.length - b.length) > max) return max + 1;
33
+
34
+ // Three rows: prev2 carries i-2 so an adjacent transposition costs 1.
35
+ let prev2 = new Array<number>(b.length + 1).fill(0);
36
+ let prev = Array.from({ length: b.length + 1 }, (_, i) => i);
37
+ let curr = new Array<number>(b.length + 1);
38
+
39
+ for (let i = 1; i <= a.length; i++) {
40
+ curr[0] = i;
41
+ let rowMin = curr[0];
42
+ for (let j = 1; j <= b.length; j++) {
43
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
44
+ let d = Math.min(
45
+ prev[j] + 1, // deletion
46
+ curr[j - 1] + 1, // insertion
47
+ prev[j - 1] + cost, // substitution
48
+ );
49
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
50
+ d = Math.min(d, prev2[j - 2] + 1); // adjacent transposition
51
+ }
52
+ curr[j] = d;
53
+ if (d < rowMin) rowMin = d;
54
+ }
55
+ // Every remaining row can only grow: bail out early.
56
+ if (rowMin > max) return max + 1;
57
+ [prev2, prev, curr] = [prev, curr, prev2];
58
+ }
59
+
60
+ return prev[b.length];
61
+ }
62
+
63
+ /** Strip a trailing ordinal token: `demo2` -> `demo`, `cypress-tests-3` -> `cypress-tests`. */
64
+ function stripOrdinal(name: string): string {
65
+ return name.replace(/[-_]?\d+$/, "");
66
+ }
67
+
68
+ /**
69
+ * True when two names look like sibling environments rather than a typo of
70
+ * each other — that is, they differ ONLY in a trailing ordinal.
71
+ *
72
+ * This guard is mandatory, not cosmetic. The live cluster contains
73
+ * `leads-platform-demo` beside `leads-platform-demo2` (edit distance 1) and
74
+ * `cypress-tests-1/2/3`. Without it, provisioning `leads-platform-demo3` would
75
+ * find `demo2` one edit away, be branded a typo, and hard-fail a healthy new
76
+ * environment — reintroducing exactly the cold-start breakage the severity
77
+ * model exists to prevent.
78
+ *
79
+ * The guard is deliberately biased toward WARN: a genuine typo that happens to
80
+ * differ only in a trailing digit is downgraded to a warning rather than
81
+ * risking a false fatal.
82
+ */
83
+ export function isOrdinalSibling(a: string, b: string): boolean {
84
+ if (a === b) return false;
85
+ const baseA = stripOrdinal(a);
86
+ const baseB = stripOrdinal(b);
87
+ // Both must reduce to the same base, and at least one must have carried an
88
+ // ordinal (otherwise stripping changed nothing and this is a real edit).
89
+ return baseA === baseB && (baseA !== a || baseB !== b);
90
+ }
91
+
92
+ /**
93
+ * Find the existing names most likely to be what `name` was meant to be.
94
+ *
95
+ * Returns EVERY candidate at the minimal distance, not one arbitrary winner.
96
+ * `thread-dv` is one edit from both `thread-dev` and `thread-db`; picking
97
+ * between them by iteration order could point an operator at production. When
98
+ * the evidence is ambiguous, say so.
99
+ *
100
+ * Returns an empty array when nothing is close enough, or when the only close
101
+ * matches are sibling environments (see {@link isOrdinalSibling}). Empty means
102
+ * "absent, but no evidence of a typo" — the caller warns rather than fails.
103
+ *
104
+ * Results are sorted for deterministic messages.
105
+ */
106
+ export function nearestNames(name: string, candidates: readonly string[]): string[] {
107
+ let best: string[] = [];
108
+ let bestDistance = TYPO_DISTANCE_THRESHOLD + 1;
109
+
110
+ for (const candidate of candidates) {
111
+ if (candidate === name) return []; // exact match: not missing at all
112
+ if (isOrdinalSibling(name, candidate)) continue;
113
+
114
+ const d = editDistance(name, candidate, TYPO_DISTANCE_THRESHOLD);
115
+ if (d > TYPO_DISTANCE_THRESHOLD) continue;
116
+
117
+ if (d < bestDistance) {
118
+ bestDistance = d;
119
+ best = [candidate];
120
+ } else if (d === bestDistance) {
121
+ best.push(candidate);
122
+ }
123
+ }
124
+
125
+ return best.sort();
126
+ }
127
+
128
+ /** Render one or more suggestions as `"a"`, or `"a" or "b"`, or `"a", "b" or "c"`. */
129
+ export function formatSuggestions(names: readonly string[]): string {
130
+ const quoted = names.map((n) => `"${n}"`);
131
+ if (quoted.length <= 1) return quoted[0] ?? "";
132
+ return `${quoted.slice(0, -1).join(", ")} or ${quoted[quoted.length - 1]}`;
133
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @htilabs/platform-common-models — MongoDB URI parsing
3
+ *
4
+ * Home of the connection-string parsing shared by the seed guard
5
+ * (`scripts/lib/confirmTargetDb.ts`) and the runtime verifier
6
+ * (`src/mongo/index.ts`). It lives in `src/` so both can import one
7
+ * implementation; `scripts/` cannot be imported from `src/` at build time.
8
+ */
9
+
10
+ /** Suffix that marks a database as a non-production scratch/test database. */
11
+ export const TEST_DB_SUFFIX = "-test";
12
+
13
+ /**
14
+ * Parse the target database name out of a MongoDB connection URI.
15
+ *
16
+ * The database name is the path segment after the host(s), before any query
17
+ * string (`?...`). Handles both `mongodb://` and `mongodb+srv://` URIs and any
18
+ * userinfo / multi-host / option components. Returns `null` when the URI
19
+ * carries no database name (e.g. `mongodb://localhost:27017` or a trailing
20
+ * slash with nothing after it) — callers must treat a `null` name as
21
+ * prod-shaped and never silently default to a production database.
22
+ *
23
+ * Implemented as manual string parsing rather than `new URL()` because Node's
24
+ * URL parser rejects multi-host `mongodb+srv` authorities and mishandles the
25
+ * comma-separated seed-list form that real connection strings use.
26
+ */
27
+ export function parseDbName(uri: string): string | null {
28
+ // Strip the scheme.
29
+ const schemeMatch = /^mongodb(\+srv)?:\/\//i.exec(uri);
30
+ if (!schemeMatch) return null;
31
+ let rest = uri.slice(schemeMatch[0].length);
32
+
33
+ // Drop query string and fragment — the DB name precedes both.
34
+ const qIdx = rest.search(/[?#]/);
35
+ if (qIdx !== -1) rest = rest.slice(0, qIdx);
36
+
37
+ // The path begins at the first '/' after the authority. Everything before
38
+ // that first slash is userinfo + host(s); everything after is the DB name.
39
+ const slashIdx = rest.indexOf("/");
40
+ if (slashIdx === -1) return null;
41
+
42
+ const dbName = rest.slice(slashIdx + 1).trim();
43
+ return dbName.length > 0 ? dbName : null;
44
+ }
45
+
46
+ /**
47
+ * Classify a database name. A name is a "test" database (no confirmation
48
+ * required) only when it is non-null and ends in {@link TEST_DB_SUFFIX}.
49
+ * Everything else — including a `null` name — is prod-shaped.
50
+ */
51
+ export function isTestDb(dbName: string | null): boolean {
52
+ return dbName !== null && dbName.endsWith(TEST_DB_SUFFIX);
53
+ }
@@ -42,6 +42,10 @@ export const personNameSchema = new Schema(
42
42
  alias: Boolean,
43
43
  isUnknown: Boolean,
44
44
  isAnonymous: Boolean,
45
+ // Shape flag: this person has only one name (e.g., Madonna). A mononym
46
+ // can itself be legal, a nickname, a trade name, etc. — it's orthogonal
47
+ // to nameType, not a value of it.
48
+ isMononym: Boolean,
45
49
  },
46
50
  { _id: false },
47
51
  );
@@ -233,18 +233,18 @@ type SectionQuestionRef = InferRawDocType<typeof sectionQuestionRefSchema>;
233
233
  declare const guidanceSchema: Schema<any, mongoose.Model<any, any, any, any, any, any, any>, {}, {}, {}, {}, {
234
234
  _id: false;
235
235
  }, {
236
- text: string;
237
236
  type: "tip" | "warning" | "prompt";
238
- }, mongoose.Document<unknown, {}, {
239
237
  text: string;
238
+ }, mongoose.Document<unknown, {}, {
240
239
  type: "tip" | "warning" | "prompt";
240
+ text: string;
241
241
  }, {
242
242
  id: string;
243
243
  }, Omit<mongoose.DefaultSchemaOptions, "_id"> & {
244
244
  _id: false;
245
245
  }> & Omit<{
246
- text: string;
247
246
  type: "tip" | "warning" | "prompt";
247
+ text: string;
248
248
  } & {
249
249
  _id: mongoose.Types.ObjectId;
250
250
  } & {
@@ -252,8 +252,8 @@ declare const guidanceSchema: Schema<any, mongoose.Model<any, any, any, any, any
252
252
  }, "id"> & {
253
253
  id: string;
254
254
  }, unknown, {
255
- text: string;
256
255
  type: "tip" | "warning" | "prompt";
256
+ text: string;
257
257
  } & {
258
258
  _id: mongoose.Types.ObjectId;
259
259
  } & {
@@ -294,18 +294,18 @@ declare const sectionSchemaDefinition: {
294
294
  type: Schema<any, mongoose.Model<any, any, any, any, any, any, any>, {}, {}, {}, {}, {
295
295
  _id: false;
296
296
  }, {
297
- text: string;
298
297
  type: "tip" | "warning" | "prompt";
299
- }, mongoose.Document<unknown, {}, {
300
298
  text: string;
299
+ }, mongoose.Document<unknown, {}, {
301
300
  type: "tip" | "warning" | "prompt";
301
+ text: string;
302
302
  }, {
303
303
  id: string;
304
304
  }, Omit<mongoose.DefaultSchemaOptions, "_id"> & {
305
305
  _id: false;
306
306
  }> & Omit<{
307
- text: string;
308
307
  type: "tip" | "warning" | "prompt";
308
+ text: string;
309
309
  } & {
310
310
  _id: mongoose.Types.ObjectId;
311
311
  } & {
@@ -313,8 +313,8 @@ declare const sectionSchemaDefinition: {
313
313
  }, "id"> & {
314
314
  id: string;
315
315
  }, unknown, {
316
- text: string;
317
316
  type: "tip" | "warning" | "prompt";
317
+ text: string;
318
318
  } & {
319
319
  _id: mongoose.Types.ObjectId;
320
320
  } & {
@@ -233,18 +233,18 @@ type SectionQuestionRef = InferRawDocType<typeof sectionQuestionRefSchema>;
233
233
  declare const guidanceSchema: Schema<any, mongoose.Model<any, any, any, any, any, any, any>, {}, {}, {}, {}, {
234
234
  _id: false;
235
235
  }, {
236
- text: string;
237
236
  type: "tip" | "warning" | "prompt";
238
- }, mongoose.Document<unknown, {}, {
239
237
  text: string;
238
+ }, mongoose.Document<unknown, {}, {
240
239
  type: "tip" | "warning" | "prompt";
240
+ text: string;
241
241
  }, {
242
242
  id: string;
243
243
  }, Omit<mongoose.DefaultSchemaOptions, "_id"> & {
244
244
  _id: false;
245
245
  }> & Omit<{
246
- text: string;
247
246
  type: "tip" | "warning" | "prompt";
247
+ text: string;
248
248
  } & {
249
249
  _id: mongoose.Types.ObjectId;
250
250
  } & {
@@ -252,8 +252,8 @@ declare const guidanceSchema: Schema<any, mongoose.Model<any, any, any, any, any
252
252
  }, "id"> & {
253
253
  id: string;
254
254
  }, unknown, {
255
- text: string;
256
255
  type: "tip" | "warning" | "prompt";
256
+ text: string;
257
257
  } & {
258
258
  _id: mongoose.Types.ObjectId;
259
259
  } & {
@@ -294,18 +294,18 @@ declare const sectionSchemaDefinition: {
294
294
  type: Schema<any, mongoose.Model<any, any, any, any, any, any, any>, {}, {}, {}, {}, {
295
295
  _id: false;
296
296
  }, {
297
- text: string;
298
297
  type: "tip" | "warning" | "prompt";
299
- }, mongoose.Document<unknown, {}, {
300
298
  text: string;
299
+ }, mongoose.Document<unknown, {}, {
301
300
  type: "tip" | "warning" | "prompt";
301
+ text: string;
302
302
  }, {
303
303
  id: string;
304
304
  }, Omit<mongoose.DefaultSchemaOptions, "_id"> & {
305
305
  _id: false;
306
306
  }> & Omit<{
307
- text: string;
308
307
  type: "tip" | "warning" | "prompt";
308
+ text: string;
309
309
  } & {
310
310
  _id: mongoose.Types.ObjectId;
311
311
  } & {
@@ -313,8 +313,8 @@ declare const sectionSchemaDefinition: {
313
313
  }, "id"> & {
314
314
  id: string;
315
315
  }, unknown, {
316
- text: string;
317
316
  type: "tip" | "warning" | "prompt";
317
+ text: string;
318
318
  } & {
319
319
  _id: mongoose.Types.ObjectId;
320
320
  } & {