@vorionsys/contracts 0.1.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/dist/aci/aci-string.d.ts +539 -0
  2. package/dist/aci/aci-string.d.ts.map +1 -0
  3. package/dist/aci/aci-string.js +563 -0
  4. package/dist/aci/aci-string.js.map +1 -0
  5. package/dist/aci/attestation.d.ts +648 -0
  6. package/dist/aci/attestation.d.ts.map +1 -0
  7. package/dist/aci/attestation.js +289 -0
  8. package/dist/aci/attestation.js.map +1 -0
  9. package/dist/aci/domains.d.ts +260 -0
  10. package/dist/aci/domains.d.ts.map +1 -0
  11. package/dist/aci/domains.js +322 -0
  12. package/dist/aci/domains.js.map +1 -0
  13. package/dist/aci/effective-permission.d.ts +371 -0
  14. package/dist/aci/effective-permission.d.ts.map +1 -0
  15. package/dist/aci/effective-permission.js +351 -0
  16. package/dist/aci/effective-permission.js.map +1 -0
  17. package/dist/aci/identity.d.ts +1100 -0
  18. package/dist/aci/identity.d.ts.map +1 -0
  19. package/dist/aci/identity.js +328 -0
  20. package/dist/aci/identity.js.map +1 -0
  21. package/dist/aci/index.d.ts +67 -0
  22. package/dist/aci/index.d.ts.map +1 -0
  23. package/dist/aci/index.js +157 -0
  24. package/dist/aci/index.js.map +1 -0
  25. package/dist/aci/jwt-claims.d.ts +756 -0
  26. package/dist/aci/jwt-claims.d.ts.map +1 -0
  27. package/dist/aci/jwt-claims.js +335 -0
  28. package/dist/aci/jwt-claims.js.map +1 -0
  29. package/dist/aci/levels.d.ts +279 -0
  30. package/dist/aci/levels.d.ts.map +1 -0
  31. package/dist/aci/levels.js +467 -0
  32. package/dist/aci/levels.js.map +1 -0
  33. package/dist/aci/mapping.d.ts +291 -0
  34. package/dist/aci/mapping.d.ts.map +1 -0
  35. package/dist/aci/mapping.js +427 -0
  36. package/dist/aci/mapping.js.map +1 -0
  37. package/dist/aci/skills.d.ts +314 -0
  38. package/dist/aci/skills.d.ts.map +1 -0
  39. package/dist/aci/skills.js +404 -0
  40. package/dist/aci/skills.js.map +1 -0
  41. package/dist/aci/tiers.d.ts +403 -0
  42. package/dist/aci/tiers.d.ts.map +1 -0
  43. package/dist/aci/tiers.js +659 -0
  44. package/dist/aci/tiers.js.map +1 -0
  45. package/dist/canonical/agent.d.ts +796 -0
  46. package/dist/canonical/agent.d.ts.map +1 -0
  47. package/dist/canonical/agent.js +527 -0
  48. package/dist/canonical/agent.js.map +1 -0
  49. package/dist/canonical/governance.d.ts +905 -0
  50. package/dist/canonical/governance.d.ts.map +1 -0
  51. package/dist/canonical/governance.js +454 -0
  52. package/dist/canonical/governance.js.map +1 -0
  53. package/dist/canonical/index.d.ts +17 -0
  54. package/dist/canonical/index.d.ts.map +1 -0
  55. package/dist/canonical/index.js +21 -0
  56. package/dist/canonical/index.js.map +1 -0
  57. package/dist/canonical/intent.d.ts +727 -0
  58. package/dist/canonical/intent.d.ts.map +1 -0
  59. package/dist/canonical/intent.js +203 -0
  60. package/dist/canonical/intent.js.map +1 -0
  61. package/dist/canonical/risk-level.d.ts +344 -0
  62. package/dist/canonical/risk-level.d.ts.map +1 -0
  63. package/dist/canonical/risk-level.js +472 -0
  64. package/dist/canonical/risk-level.js.map +1 -0
  65. package/dist/canonical/trust-band.d.ts +239 -0
  66. package/dist/canonical/trust-band.d.ts.map +1 -0
  67. package/dist/canonical/trust-band.js +298 -0
  68. package/dist/canonical/trust-band.js.map +1 -0
  69. package/dist/canonical/trust-score.d.ts +301 -0
  70. package/dist/canonical/trust-score.d.ts.map +1 -0
  71. package/dist/canonical/trust-score.js +390 -0
  72. package/dist/canonical/trust-score.js.map +1 -0
  73. package/dist/canonical/trust-signal.d.ts +617 -0
  74. package/dist/canonical/trust-signal.d.ts.map +1 -0
  75. package/dist/canonical/trust-signal.js +355 -0
  76. package/dist/canonical/trust-signal.js.map +1 -0
  77. package/dist/canonical/validation.d.ts +231 -0
  78. package/dist/canonical/validation.d.ts.map +1 -0
  79. package/dist/canonical/validation.js +558 -0
  80. package/dist/canonical/validation.js.map +1 -0
  81. package/dist/common/index.d.ts +7 -0
  82. package/dist/common/index.d.ts.map +1 -0
  83. package/dist/common/index.js +7 -0
  84. package/dist/common/index.js.map +1 -0
  85. package/dist/common/primitives.d.ts +56 -0
  86. package/dist/common/primitives.d.ts.map +1 -0
  87. package/dist/common/primitives.js +70 -0
  88. package/dist/common/primitives.js.map +1 -0
  89. package/dist/index.d.ts +11 -0
  90. package/dist/index.d.ts.map +1 -0
  91. package/dist/index.js +15 -0
  92. package/dist/index.js.map +1 -0
  93. package/dist/v2/canary-probe.d.ts +201 -0
  94. package/dist/v2/canary-probe.d.ts.map +1 -0
  95. package/dist/v2/canary-probe.js +99 -0
  96. package/dist/v2/canary-probe.js.map +1 -0
  97. package/dist/v2/component.d.ts +132 -0
  98. package/dist/v2/component.d.ts.map +1 -0
  99. package/dist/v2/component.js +5 -0
  100. package/dist/v2/component.js.map +1 -0
  101. package/dist/v2/decision.d.ts +310 -0
  102. package/dist/v2/decision.d.ts.map +1 -0
  103. package/dist/v2/decision.js +21 -0
  104. package/dist/v2/decision.js.map +1 -0
  105. package/dist/v2/enums.d.ts +185 -0
  106. package/dist/v2/enums.d.ts.map +1 -0
  107. package/dist/v2/enums.js +203 -0
  108. package/dist/v2/enums.js.map +1 -0
  109. package/dist/v2/evidence.d.ts +368 -0
  110. package/dist/v2/evidence.d.ts.map +1 -0
  111. package/dist/v2/evidence.js +152 -0
  112. package/dist/v2/evidence.js.map +1 -0
  113. package/dist/v2/execution.d.ts +190 -0
  114. package/dist/v2/execution.d.ts.map +1 -0
  115. package/dist/v2/execution.js +5 -0
  116. package/dist/v2/execution.js.map +1 -0
  117. package/dist/v2/index.d.ts +19 -0
  118. package/dist/v2/index.d.ts.map +1 -0
  119. package/dist/v2/index.js +32 -0
  120. package/dist/v2/index.js.map +1 -0
  121. package/dist/v2/intent.d.ts +89 -0
  122. package/dist/v2/intent.d.ts.map +1 -0
  123. package/dist/v2/intent.js +5 -0
  124. package/dist/v2/intent.js.map +1 -0
  125. package/dist/v2/policy-bundle.d.ts +166 -0
  126. package/dist/v2/policy-bundle.d.ts.map +1 -0
  127. package/dist/v2/policy-bundle.js +20 -0
  128. package/dist/v2/policy-bundle.js.map +1 -0
  129. package/dist/v2/pre-action-gate.d.ts +185 -0
  130. package/dist/v2/pre-action-gate.d.ts.map +1 -0
  131. package/dist/v2/pre-action-gate.js +64 -0
  132. package/dist/v2/pre-action-gate.js.map +1 -0
  133. package/dist/v2/proof-event.d.ts +201 -0
  134. package/dist/v2/proof-event.d.ts.map +1 -0
  135. package/dist/v2/proof-event.js +5 -0
  136. package/dist/v2/proof-event.js.map +1 -0
  137. package/dist/v2/retention.d.ts +329 -0
  138. package/dist/v2/retention.d.ts.map +1 -0
  139. package/dist/v2/retention.js +162 -0
  140. package/dist/v2/retention.js.map +1 -0
  141. package/dist/v2/trust-delta.d.ts +119 -0
  142. package/dist/v2/trust-delta.d.ts.map +1 -0
  143. package/dist/v2/trust-delta.js +28 -0
  144. package/dist/v2/trust-delta.js.map +1 -0
  145. package/dist/v2/trust-profile.d.ts +337 -0
  146. package/dist/v2/trust-profile.d.ts.map +1 -0
  147. package/dist/v2/trust-profile.js +96 -0
  148. package/dist/v2/trust-profile.js.map +1 -0
  149. package/dist/validators/decision.d.ts +430 -0
  150. package/dist/validators/decision.d.ts.map +1 -0
  151. package/dist/validators/decision.js +61 -0
  152. package/dist/validators/decision.js.map +1 -0
  153. package/dist/validators/enums.d.ts +24 -0
  154. package/dist/validators/enums.d.ts.map +1 -0
  155. package/dist/validators/enums.js +24 -0
  156. package/dist/validators/enums.js.map +1 -0
  157. package/dist/validators/index.d.ts +30 -0
  158. package/dist/validators/index.d.ts.map +1 -0
  159. package/dist/validators/index.js +42 -0
  160. package/dist/validators/index.js.map +1 -0
  161. package/dist/validators/intent.d.ts +229 -0
  162. package/dist/validators/intent.d.ts.map +1 -0
  163. package/dist/validators/intent.js +47 -0
  164. package/dist/validators/intent.js.map +1 -0
  165. package/dist/validators/proof-event.d.ts +981 -0
  166. package/dist/validators/proof-event.d.ts.map +1 -0
  167. package/dist/validators/proof-event.js +134 -0
  168. package/dist/validators/proof-event.js.map +1 -0
  169. package/dist/validators/trust-profile.d.ts +350 -0
  170. package/dist/validators/trust-profile.d.ts.map +1 -0
  171. package/dist/validators/trust-profile.js +65 -0
  172. package/dist/validators/trust-profile.js.map +1 -0
  173. package/package.json +65 -0
@@ -0,0 +1,301 @@
1
+ /**
2
+ * @fileoverview Canonical TrustScore type definitions for the Vorion Platform.
3
+ *
4
+ * This file provides the authoritative definition for trust scores, using a
5
+ * branded type pattern to ensure type safety at compile time. The canonical
6
+ * scale is 0-1000, with utilities for conversion to/from other scales.
7
+ *
8
+ * Trust scores represent an agent's accumulated trust level based on their
9
+ * behavioral history, credentials, and contextual factors.
10
+ *
11
+ * @module @vorion/contracts/canonical/trust-score
12
+ */
13
+ import { z } from 'zod';
14
+ /**
15
+ * Brand symbol for TrustScore type safety.
16
+ * @internal
17
+ */
18
+ export declare const TrustScoreBrand: unique symbol;
19
+ /**
20
+ * Branded type for trust scores on the canonical 0-1000 scale.
21
+ *
22
+ * Using a branded type ensures that raw numbers cannot be accidentally
23
+ * used where a validated TrustScore is expected. All TrustScores must
24
+ * be created via `createTrustScore()` to ensure validation.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * // Correct usage
29
+ * const score: TrustScore = createTrustScore(750);
30
+ *
31
+ * // Type error - cannot assign raw number to TrustScore
32
+ * const score: TrustScore = 750; // Error!
33
+ * ```
34
+ */
35
+ export type TrustScore = number & {
36
+ readonly [TrustScoreBrand]: typeof TrustScoreBrand;
37
+ };
38
+ /**
39
+ * Minimum valid trust score (0).
40
+ * Represents complete lack of trust.
41
+ */
42
+ export declare const MIN_TRUST_SCORE: TrustScore;
43
+ /**
44
+ * Maximum valid trust score (1000).
45
+ * Represents maximum achievable trust.
46
+ */
47
+ export declare const MAX_TRUST_SCORE: TrustScore;
48
+ /**
49
+ * Default trust score for new agents (500).
50
+ * Represents a neutral starting point at the T3_STANDARD threshold.
51
+ */
52
+ export declare const DEFAULT_TRUST_SCORE: TrustScore;
53
+ /**
54
+ * Initial trust score for completely new/unknown agents (250).
55
+ * Places new agents in the T1_OBSERVED band.
56
+ */
57
+ export declare const INITIAL_TRUST_SCORE: TrustScore;
58
+ /**
59
+ * Trust score representing the probationary floor (167).
60
+ * Minimum score to exit T0_SANDBOX band.
61
+ */
62
+ export declare const PROBATION_THRESHOLD: TrustScore;
63
+ /**
64
+ * Trust score representing critical threshold (100).
65
+ * Below this triggers circuit breaker protections.
66
+ */
67
+ export declare const CIRCUIT_BREAKER_THRESHOLD: TrustScore;
68
+ /**
69
+ * Creates a validated TrustScore from a numeric value.
70
+ *
71
+ * Validates that the value is within the canonical 0-1000 range,
72
+ * rounds to the nearest integer, and returns a branded TrustScore.
73
+ *
74
+ * @param value - Numeric value to convert to TrustScore
75
+ * @returns Validated TrustScore
76
+ * @throws {Error} If value is outside the 0-1000 range or not a valid number
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const score = createTrustScore(750); // TrustScore(750)
81
+ * const score = createTrustScore(750.7); // TrustScore(751) - rounded
82
+ * const score = createTrustScore(-10); // Throws Error
83
+ * const score = createTrustScore(1500); // Throws Error
84
+ * ```
85
+ */
86
+ export declare function createTrustScore(value: number): TrustScore;
87
+ /**
88
+ * Creates a TrustScore, clamping values to valid range instead of throwing.
89
+ *
90
+ * Useful for normalization scenarios where out-of-range values should
91
+ * be corrected rather than rejected.
92
+ *
93
+ * @param value - Numeric value to convert to TrustScore
94
+ * @returns Validated and clamped TrustScore
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * createTrustScoreClamped(750); // TrustScore(750)
99
+ * createTrustScoreClamped(-50); // TrustScore(0) - clamped to min
100
+ * createTrustScoreClamped(1500); // TrustScore(1000) - clamped to max
101
+ * ```
102
+ */
103
+ export declare function createTrustScoreClamped(value: number): TrustScore;
104
+ /**
105
+ * Safely parses a value to TrustScore, returning a result object.
106
+ *
107
+ * @param value - Value to parse
108
+ * @returns Object with success flag and either score or error
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * const result = parseTrustScore(750);
113
+ * if (result.success) {
114
+ * console.log(result.score); // TrustScore(750)
115
+ * } else {
116
+ * console.log(result.error); // Error message
117
+ * }
118
+ * ```
119
+ */
120
+ export declare function parseTrustScore(value: unknown): {
121
+ success: true;
122
+ score: TrustScore;
123
+ } | {
124
+ success: false;
125
+ error: string;
126
+ };
127
+ /**
128
+ * Normalizes a TrustScore (0-1000) to a 0-100 scale.
129
+ *
130
+ * Useful for display purposes or integration with systems using percentage-based trust.
131
+ *
132
+ * @param score - TrustScore on 0-1000 scale
133
+ * @returns Normalized value on 0-100 scale
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * normalizeTo100(createTrustScore(750)); // 75
138
+ * normalizeTo100(createTrustScore(500)); // 50
139
+ * ```
140
+ */
141
+ export declare function normalizeTo100(score: TrustScore): number;
142
+ /**
143
+ * Converts a 0-100 scale value to a TrustScore (0-1000).
144
+ *
145
+ * Useful for importing trust data from systems using percentage-based trust.
146
+ *
147
+ * @param value - Value on 0-100 scale
148
+ * @returns TrustScore on 0-1000 scale
149
+ * @throws {Error} If value is outside 0-100 range
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * normalizeFrom100(75); // TrustScore(750)
154
+ * normalizeFrom100(50); // TrustScore(500)
155
+ * ```
156
+ */
157
+ export declare function normalizeFrom100(value: number): TrustScore;
158
+ /**
159
+ * Normalizes a TrustScore (0-1000) to a 0-1 decimal scale.
160
+ *
161
+ * Useful for probabilistic calculations or ML integrations.
162
+ *
163
+ * @param score - TrustScore on 0-1000 scale
164
+ * @returns Normalized value on 0-1 scale
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * normalizeToDecimal(createTrustScore(750)); // 0.75
169
+ * normalizeToDecimal(createTrustScore(500)); // 0.5
170
+ * ```
171
+ */
172
+ export declare function normalizeToDecimal(score: TrustScore): number;
173
+ /**
174
+ * Converts a 0-1 decimal value to a TrustScore (0-1000).
175
+ *
176
+ * Useful for converting ML outputs or probability scores to TrustScore.
177
+ *
178
+ * @param value - Value on 0-1 scale
179
+ * @returns TrustScore on 0-1000 scale
180
+ * @throws {Error} If value is outside 0-1 range
181
+ *
182
+ * @example
183
+ * ```typescript
184
+ * normalizeFromDecimal(0.75); // TrustScore(750)
185
+ * normalizeFromDecimal(0.5); // TrustScore(500)
186
+ * ```
187
+ */
188
+ export declare function normalizeFromDecimal(value: number): TrustScore;
189
+ /**
190
+ * Adds a delta to a TrustScore, clamping to valid range.
191
+ *
192
+ * @param score - Current TrustScore
193
+ * @param delta - Amount to add (can be negative)
194
+ * @returns New TrustScore clamped to 0-1000
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * addToTrustScore(createTrustScore(500), 100); // TrustScore(600)
199
+ * addToTrustScore(createTrustScore(950), 100); // TrustScore(1000) - clamped
200
+ * addToTrustScore(createTrustScore(50), -100); // TrustScore(0) - clamped
201
+ * ```
202
+ */
203
+ export declare function addToTrustScore(score: TrustScore, delta: number): TrustScore;
204
+ /**
205
+ * Calculates the difference between two TrustScores.
206
+ *
207
+ * @param a - First TrustScore
208
+ * @param b - Second TrustScore
209
+ * @returns Difference (a - b) as a number
210
+ */
211
+ export declare function trustScoreDifference(a: TrustScore, b: TrustScore): number;
212
+ /**
213
+ * Calculates the weighted average of multiple TrustScores.
214
+ *
215
+ * @param scores - Array of TrustScores
216
+ * @param weights - Optional array of weights (defaults to equal weighting)
217
+ * @returns Weighted average as TrustScore
218
+ * @throws {Error} If arrays are empty or weights don't match scores
219
+ *
220
+ * @example
221
+ * ```typescript
222
+ * weightedAverage([createTrustScore(600), createTrustScore(800)]); // 700
223
+ * weightedAverage(
224
+ * [createTrustScore(600), createTrustScore(800)],
225
+ * [0.3, 0.7]
226
+ * ); // 740
227
+ * ```
228
+ */
229
+ export declare function weightedAverage(scores: TrustScore[], weights?: number[]): TrustScore;
230
+ /**
231
+ * Compares two TrustScores.
232
+ *
233
+ * @param a - First TrustScore
234
+ * @param b - Second TrustScore
235
+ * @returns -1 if a < b, 0 if equal, 1 if a > b
236
+ */
237
+ export declare function compareTrustScores(a: TrustScore, b: TrustScore): -1 | 0 | 1;
238
+ /**
239
+ * Checks if a TrustScore meets a minimum threshold.
240
+ *
241
+ * @param score - Score to check
242
+ * @param threshold - Minimum required score
243
+ * @returns True if score meets or exceeds threshold
244
+ */
245
+ export declare function meetsThreshold(score: TrustScore, threshold: TrustScore): boolean;
246
+ /**
247
+ * Checks if a TrustScore is below the circuit breaker threshold.
248
+ *
249
+ * @param score - Score to check
250
+ * @returns True if score is critically low
251
+ */
252
+ export declare function isCriticallyLow(score: TrustScore): boolean;
253
+ /**
254
+ * Type guard to check if a value could be a valid TrustScore.
255
+ *
256
+ * Note: This checks numeric validity, not brand. Use for runtime validation.
257
+ *
258
+ * @param value - Value to check
259
+ * @returns True if value is a number in the 0-1000 range
260
+ *
261
+ * @example
262
+ * ```typescript
263
+ * isTrustScoreValue(750); // true
264
+ * isTrustScoreValue(-10); // false
265
+ * isTrustScoreValue('750'); // false
266
+ * ```
267
+ */
268
+ export declare function isTrustScoreValue(value: unknown): value is number;
269
+ /**
270
+ * Zod schema for raw trust score validation (0-1000 range).
271
+ *
272
+ * Validates that a number is within the canonical trust score range.
273
+ */
274
+ export declare const trustScoreValueSchema: z.ZodNumber;
275
+ /**
276
+ * Zod schema that validates and transforms to TrustScore branded type.
277
+ *
278
+ * @example
279
+ * ```typescript
280
+ * const score = trustScoreSchema.parse(750); // Returns TrustScore(750)
281
+ * trustScoreSchema.parse(-10); // Throws ZodError
282
+ * ```
283
+ */
284
+ export declare const trustScoreSchema: z.ZodEffects<z.ZodNumber, TrustScore, number>;
285
+ /**
286
+ * Zod schema for trust score on 0-100 scale with transformation to 0-1000.
287
+ *
288
+ * Useful for parsing input from systems using percentage-based trust.
289
+ */
290
+ export declare const trustScore100Schema: z.ZodEffects<z.ZodNumber, TrustScore, number>;
291
+ /**
292
+ * Zod schema for trust score on 0-1 decimal scale with transformation to 0-1000.
293
+ *
294
+ * Useful for parsing ML outputs or probability scores.
295
+ */
296
+ export declare const trustScoreDecimalSchema: z.ZodEffects<z.ZodNumber, TrustScore, number>;
297
+ /**
298
+ * Zod schema for optional trust score with default value.
299
+ */
300
+ export declare const trustScoreWithDefaultSchema: z.ZodEffects<z.ZodDefault<z.ZodOptional<z.ZodNumber>>, TrustScore, number | undefined>;
301
+ //# sourceMappingURL=trust-score.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trust-score.d.ts","sourceRoot":"","sources":["../../src/canonical/trust-score.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,390 @@
1
+ /**
2
+ * @fileoverview Canonical TrustScore type definitions for the Vorion Platform.
3
+ *
4
+ * This file provides the authoritative definition for trust scores, using a
5
+ * branded type pattern to ensure type safety at compile time. The canonical
6
+ * scale is 0-1000, with utilities for conversion to/from other scales.
7
+ *
8
+ * Trust scores represent an agent's accumulated trust level based on their
9
+ * behavioral history, credentials, and contextual factors.
10
+ *
11
+ * @module @vorion/contracts/canonical/trust-score
12
+ */
13
+ import { z } from 'zod';
14
+ // ============================================================================
15
+ // Constants
16
+ // ============================================================================
17
+ /**
18
+ * Minimum valid trust score (0).
19
+ * Represents complete lack of trust.
20
+ */
21
+ export const MIN_TRUST_SCORE = 0;
22
+ /**
23
+ * Maximum valid trust score (1000).
24
+ * Represents maximum achievable trust.
25
+ */
26
+ export const MAX_TRUST_SCORE = 1000;
27
+ /**
28
+ * Default trust score for new agents (500).
29
+ * Represents a neutral starting point at the T3_STANDARD threshold.
30
+ */
31
+ export const DEFAULT_TRUST_SCORE = 500;
32
+ /**
33
+ * Initial trust score for completely new/unknown agents (250).
34
+ * Places new agents in the T1_OBSERVED band.
35
+ */
36
+ export const INITIAL_TRUST_SCORE = 250;
37
+ /**
38
+ * Trust score representing the probationary floor (167).
39
+ * Minimum score to exit T0_SANDBOX band.
40
+ */
41
+ export const PROBATION_THRESHOLD = 167;
42
+ /**
43
+ * Trust score representing critical threshold (100).
44
+ * Below this triggers circuit breaker protections.
45
+ */
46
+ export const CIRCUIT_BREAKER_THRESHOLD = 100;
47
+ // ============================================================================
48
+ // Factory Functions
49
+ // ============================================================================
50
+ /**
51
+ * Creates a validated TrustScore from a numeric value.
52
+ *
53
+ * Validates that the value is within the canonical 0-1000 range,
54
+ * rounds to the nearest integer, and returns a branded TrustScore.
55
+ *
56
+ * @param value - Numeric value to convert to TrustScore
57
+ * @returns Validated TrustScore
58
+ * @throws {Error} If value is outside the 0-1000 range or not a valid number
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const score = createTrustScore(750); // TrustScore(750)
63
+ * const score = createTrustScore(750.7); // TrustScore(751) - rounded
64
+ * const score = createTrustScore(-10); // Throws Error
65
+ * const score = createTrustScore(1500); // Throws Error
66
+ * ```
67
+ */
68
+ export function createTrustScore(value) {
69
+ if (typeof value !== 'number' || Number.isNaN(value)) {
70
+ throw new Error(`TrustScore must be a valid number, got ${typeof value}`);
71
+ }
72
+ if (!Number.isFinite(value)) {
73
+ throw new Error('TrustScore must be a finite number');
74
+ }
75
+ if (value < 0 || value > 1000) {
76
+ throw new Error(`TrustScore must be between 0 and 1000, got ${value}`);
77
+ }
78
+ return Math.round(value);
79
+ }
80
+ /**
81
+ * Creates a TrustScore, clamping values to valid range instead of throwing.
82
+ *
83
+ * Useful for normalization scenarios where out-of-range values should
84
+ * be corrected rather than rejected.
85
+ *
86
+ * @param value - Numeric value to convert to TrustScore
87
+ * @returns Validated and clamped TrustScore
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * createTrustScoreClamped(750); // TrustScore(750)
92
+ * createTrustScoreClamped(-50); // TrustScore(0) - clamped to min
93
+ * createTrustScoreClamped(1500); // TrustScore(1000) - clamped to max
94
+ * ```
95
+ */
96
+ export function createTrustScoreClamped(value) {
97
+ if (typeof value !== 'number' || Number.isNaN(value) || !Number.isFinite(value)) {
98
+ return DEFAULT_TRUST_SCORE;
99
+ }
100
+ const clamped = Math.max(0, Math.min(1000, value));
101
+ return Math.round(clamped);
102
+ }
103
+ /**
104
+ * Safely parses a value to TrustScore, returning a result object.
105
+ *
106
+ * @param value - Value to parse
107
+ * @returns Object with success flag and either score or error
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * const result = parseTrustScore(750);
112
+ * if (result.success) {
113
+ * console.log(result.score); // TrustScore(750)
114
+ * } else {
115
+ * console.log(result.error); // Error message
116
+ * }
117
+ * ```
118
+ */
119
+ export function parseTrustScore(value) {
120
+ if (typeof value !== 'number') {
121
+ return { success: false, error: `Expected number, got ${typeof value}` };
122
+ }
123
+ if (Number.isNaN(value) || !Number.isFinite(value)) {
124
+ return { success: false, error: 'Value must be a finite number' };
125
+ }
126
+ if (value < 0 || value > 1000) {
127
+ return { success: false, error: `Value must be between 0 and 1000, got ${value}` };
128
+ }
129
+ return { success: true, score: Math.round(value) };
130
+ }
131
+ // ============================================================================
132
+ // Conversion Functions
133
+ // ============================================================================
134
+ /**
135
+ * Normalizes a TrustScore (0-1000) to a 0-100 scale.
136
+ *
137
+ * Useful for display purposes or integration with systems using percentage-based trust.
138
+ *
139
+ * @param score - TrustScore on 0-1000 scale
140
+ * @returns Normalized value on 0-100 scale
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * normalizeTo100(createTrustScore(750)); // 75
145
+ * normalizeTo100(createTrustScore(500)); // 50
146
+ * ```
147
+ */
148
+ export function normalizeTo100(score) {
149
+ return Math.round(score / 10);
150
+ }
151
+ /**
152
+ * Converts a 0-100 scale value to a TrustScore (0-1000).
153
+ *
154
+ * Useful for importing trust data from systems using percentage-based trust.
155
+ *
156
+ * @param value - Value on 0-100 scale
157
+ * @returns TrustScore on 0-1000 scale
158
+ * @throws {Error} If value is outside 0-100 range
159
+ *
160
+ * @example
161
+ * ```typescript
162
+ * normalizeFrom100(75); // TrustScore(750)
163
+ * normalizeFrom100(50); // TrustScore(500)
164
+ * ```
165
+ */
166
+ export function normalizeFrom100(value) {
167
+ if (value < 0 || value > 100) {
168
+ throw new Error(`Value must be between 0 and 100, got ${value}`);
169
+ }
170
+ return createTrustScore(value * 10);
171
+ }
172
+ /**
173
+ * Normalizes a TrustScore (0-1000) to a 0-1 decimal scale.
174
+ *
175
+ * Useful for probabilistic calculations or ML integrations.
176
+ *
177
+ * @param score - TrustScore on 0-1000 scale
178
+ * @returns Normalized value on 0-1 scale
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * normalizeToDecimal(createTrustScore(750)); // 0.75
183
+ * normalizeToDecimal(createTrustScore(500)); // 0.5
184
+ * ```
185
+ */
186
+ export function normalizeToDecimal(score) {
187
+ return score / 1000;
188
+ }
189
+ /**
190
+ * Converts a 0-1 decimal value to a TrustScore (0-1000).
191
+ *
192
+ * Useful for converting ML outputs or probability scores to TrustScore.
193
+ *
194
+ * @param value - Value on 0-1 scale
195
+ * @returns TrustScore on 0-1000 scale
196
+ * @throws {Error} If value is outside 0-1 range
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * normalizeFromDecimal(0.75); // TrustScore(750)
201
+ * normalizeFromDecimal(0.5); // TrustScore(500)
202
+ * ```
203
+ */
204
+ export function normalizeFromDecimal(value) {
205
+ if (value < 0 || value > 1) {
206
+ throw new Error(`Value must be between 0 and 1, got ${value}`);
207
+ }
208
+ return createTrustScore(value * 1000);
209
+ }
210
+ // ============================================================================
211
+ // Arithmetic Operations
212
+ // ============================================================================
213
+ /**
214
+ * Adds a delta to a TrustScore, clamping to valid range.
215
+ *
216
+ * @param score - Current TrustScore
217
+ * @param delta - Amount to add (can be negative)
218
+ * @returns New TrustScore clamped to 0-1000
219
+ *
220
+ * @example
221
+ * ```typescript
222
+ * addToTrustScore(createTrustScore(500), 100); // TrustScore(600)
223
+ * addToTrustScore(createTrustScore(950), 100); // TrustScore(1000) - clamped
224
+ * addToTrustScore(createTrustScore(50), -100); // TrustScore(0) - clamped
225
+ * ```
226
+ */
227
+ export function addToTrustScore(score, delta) {
228
+ return createTrustScoreClamped(score + delta);
229
+ }
230
+ /**
231
+ * Calculates the difference between two TrustScores.
232
+ *
233
+ * @param a - First TrustScore
234
+ * @param b - Second TrustScore
235
+ * @returns Difference (a - b) as a number
236
+ */
237
+ export function trustScoreDifference(a, b) {
238
+ return a - b;
239
+ }
240
+ /**
241
+ * Calculates the weighted average of multiple TrustScores.
242
+ *
243
+ * @param scores - Array of TrustScores
244
+ * @param weights - Optional array of weights (defaults to equal weighting)
245
+ * @returns Weighted average as TrustScore
246
+ * @throws {Error} If arrays are empty or weights don't match scores
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * weightedAverage([createTrustScore(600), createTrustScore(800)]); // 700
251
+ * weightedAverage(
252
+ * [createTrustScore(600), createTrustScore(800)],
253
+ * [0.3, 0.7]
254
+ * ); // 740
255
+ * ```
256
+ */
257
+ export function weightedAverage(scores, weights) {
258
+ if (scores.length === 0) {
259
+ throw new Error('Cannot calculate average of empty array');
260
+ }
261
+ if (weights) {
262
+ if (weights.length !== scores.length) {
263
+ throw new Error('Weights array must match scores array length');
264
+ }
265
+ let normalizedWeights = weights;
266
+ const totalWeight = weights.reduce((sum, w) => sum + w, 0);
267
+ if (Math.abs(totalWeight - 1) > 0.001) {
268
+ // Normalize weights if they don't sum to 1
269
+ normalizedWeights = weights.map((w) => w / totalWeight);
270
+ }
271
+ const weighted = scores.reduce((sum, score, i) => sum + score * normalizedWeights[i], 0);
272
+ return createTrustScoreClamped(weighted);
273
+ }
274
+ const sum = scores.reduce((acc, score) => acc + score, 0);
275
+ return createTrustScoreClamped(sum / scores.length);
276
+ }
277
+ // ============================================================================
278
+ // Comparison Functions
279
+ // ============================================================================
280
+ /**
281
+ * Compares two TrustScores.
282
+ *
283
+ * @param a - First TrustScore
284
+ * @param b - Second TrustScore
285
+ * @returns -1 if a < b, 0 if equal, 1 if a > b
286
+ */
287
+ export function compareTrustScores(a, b) {
288
+ if (a < b)
289
+ return -1;
290
+ if (a > b)
291
+ return 1;
292
+ return 0;
293
+ }
294
+ /**
295
+ * Checks if a TrustScore meets a minimum threshold.
296
+ *
297
+ * @param score - Score to check
298
+ * @param threshold - Minimum required score
299
+ * @returns True if score meets or exceeds threshold
300
+ */
301
+ export function meetsThreshold(score, threshold) {
302
+ return score >= threshold;
303
+ }
304
+ /**
305
+ * Checks if a TrustScore is below the circuit breaker threshold.
306
+ *
307
+ * @param score - Score to check
308
+ * @returns True if score is critically low
309
+ */
310
+ export function isCriticallyLow(score) {
311
+ return score < CIRCUIT_BREAKER_THRESHOLD;
312
+ }
313
+ // ============================================================================
314
+ // Type Guards
315
+ // ============================================================================
316
+ /**
317
+ * Type guard to check if a value could be a valid TrustScore.
318
+ *
319
+ * Note: This checks numeric validity, not brand. Use for runtime validation.
320
+ *
321
+ * @param value - Value to check
322
+ * @returns True if value is a number in the 0-1000 range
323
+ *
324
+ * @example
325
+ * ```typescript
326
+ * isTrustScoreValue(750); // true
327
+ * isTrustScoreValue(-10); // false
328
+ * isTrustScoreValue('750'); // false
329
+ * ```
330
+ */
331
+ export function isTrustScoreValue(value) {
332
+ return (typeof value === 'number' &&
333
+ Number.isFinite(value) &&
334
+ value >= 0 &&
335
+ value <= 1000);
336
+ }
337
+ // ============================================================================
338
+ // Zod Schemas
339
+ // ============================================================================
340
+ /**
341
+ * Zod schema for raw trust score validation (0-1000 range).
342
+ *
343
+ * Validates that a number is within the canonical trust score range.
344
+ */
345
+ export const trustScoreValueSchema = z
346
+ .number({
347
+ required_error: 'Trust score is required',
348
+ invalid_type_error: 'Trust score must be a number',
349
+ })
350
+ .min(0, 'Trust score must be at least 0')
351
+ .max(1000, 'Trust score must be at most 1000')
352
+ .int('Trust score must be an integer');
353
+ /**
354
+ * Zod schema that validates and transforms to TrustScore branded type.
355
+ *
356
+ * @example
357
+ * ```typescript
358
+ * const score = trustScoreSchema.parse(750); // Returns TrustScore(750)
359
+ * trustScoreSchema.parse(-10); // Throws ZodError
360
+ * ```
361
+ */
362
+ export const trustScoreSchema = trustScoreValueSchema.transform((val) => val);
363
+ /**
364
+ * Zod schema for trust score on 0-100 scale with transformation to 0-1000.
365
+ *
366
+ * Useful for parsing input from systems using percentage-based trust.
367
+ */
368
+ export const trustScore100Schema = z
369
+ .number()
370
+ .min(0, 'Trust score must be at least 0')
371
+ .max(100, 'Trust score must be at most 100')
372
+ .transform((val) => (val * 10));
373
+ /**
374
+ * Zod schema for trust score on 0-1 decimal scale with transformation to 0-1000.
375
+ *
376
+ * Useful for parsing ML outputs or probability scores.
377
+ */
378
+ export const trustScoreDecimalSchema = z
379
+ .number()
380
+ .min(0, 'Trust score must be at least 0')
381
+ .max(1, 'Trust score must be at most 1')
382
+ .transform((val) => Math.round(val * 1000));
383
+ /**
384
+ * Zod schema for optional trust score with default value.
385
+ */
386
+ export const trustScoreWithDefaultSchema = trustScoreValueSchema
387
+ .optional()
388
+ .default(500)
389
+ .transform((val) => val);
390
+ //# sourceMappingURL=trust-score.js.map