harn-stdlib 0.10.41

Embedded Harn standard library source catalog
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/**
 * Closed contracts and frozen registration for generic controlled experiments.
 *
 * Arm configuration is deliberately opaque: Harn freezes it, while the
 * executing host validates and interprets its keys. Metric observations remain
 * in declared units; bounded ranges make anytime-valid inference possible.
 */
import { VersionedContract, versioned_contract, versioned_descriptor } from "std/artifacts/typed"
import { ArtifactDescriptor } from "std/run_artifacts"
import {
  SchemaContract,
  SchemaContractFailure,
  ValidationIssue,
  schema_any,
  schema_closed_object,
  schema_contract,
  schema_contract_check,
  schema_dict,
  schema_enum,
  schema_float,
  schema_int,
  schema_list,
  schema_literal,
  schema_string,
  validation_issue,
  validation_rule,
} from "std/schema"

pub type MetricDirection = "up" | "down"

pub type AlarmKind = "absolute" | "percentage"

pub type ExperimentPhase = "iterate" | "gate"

pub type ExperimentArm = {id: string, config: dict<string, unknown>, complexity: int}

pub type MetricBounds = {lo: float, hi: float}

pub type ExperimentMetric = {id: string, direction: MetricDirection, bounds: MetricBounds}

pub type GuardrailAlarm = {kind: AlarmKind, threshold: float}

pub type ExperimentGuardrail = {
  id: string,
  direction: MetricDirection,
  bounds: MetricBounds,
  alarm: GuardrailAlarm,
}

pub type ExperimentMetrics = {primary: ExperimentMetric, guardrails: list<ExperimentGuardrail>}

pub type ExperimentDecisionPolicy = {delta: float, epsilon: float, ladder: list<int>}

pub type ExperimentBudget = {max_spend_usd: float, max_trials_per_case: int}

pub type AssignmentPolicy = {mode: "randomized_block", seed: string, blocking_factors: list<string>}

pub type CaseSet = {id: string, digest: string, cases: list<string>}

pub type ExperimentSplitPolicy = {iterate: CaseSet, gate: CaseSet, promotion: "explicit"}

pub type ExperimentManifest = {
  schema: "harn.experiment.v1",
  experiment_id: string,
  hypothesis: string,
  owner: string,
  baseline: ExperimentArm,
  candidates: list<ExperimentArm>,
  decision: ExperimentDecisionPolicy,
  metrics: ExperimentMetrics,
  assignment: AssignmentPolicy,
  splits: ExperimentSplitPolicy,
  budget: ExperimentBudget,
}

pub type ExperimentValidationContext = {
  supported_blocking_factors: list<string>,
  host_identity_available: bool,
}

pub type ExperimentRegistration = {
  schema: "harn.experiment.registration.v1",
  schema_version: int,
  registration_id: string,
  manifest_digest: string,
  phase: ExperimentPhase,
  experiment_id: string,
  hypothesis: string,
  owner: string,
  baseline: ExperimentArm,
  candidates: list<ExperimentArm>,
  decision: ExperimentDecisionPolicy,
  metrics: ExperimentMetrics,
  assignment: AssignmentPolicy,
  case_set: CaseSet,
  gate_case_set: CaseSet,
  budget: ExperimentBudget,
  min_trials_per_case: int,
  prior_spend_usd: float,
  promoted_from?: string,
  promoted_arm?: string,
}

pub type PromotionReceipt = {
  schema: "harn.experiment.promotion.v1",
  promotion_id: string,
  source_registration_id: string,
  source_decision_id: string,
  promoted_arm: string,
  gate_registration_id: string,
}

fn __opaque_config_schema() -> dict {
  return schema_dict(schema_any())
}

fn __arm_schema() -> dict {
  return schema_closed_object(
    {id: schema_string(), config: __opaque_config_schema(), complexity: schema_int()},
  )
}

fn __bounds_schema() -> dict {
  return schema_closed_object({lo: schema_float(), hi: schema_float()})
}

fn __metric_schema() -> dict {
  return schema_closed_object(
    {id: schema_string(), direction: schema_enum(["up", "down"]), bounds: __bounds_schema()},
  )
}

fn __guardrail_schema() -> dict {
  return schema_closed_object(
    {
      id: schema_string(),
      direction: schema_enum(["up", "down"]),
      bounds: __bounds_schema(),
      alarm: schema_closed_object(
        {kind: schema_enum(["absolute", "percentage"]), threshold: schema_float()},
      ),
    },
  )
}

fn __case_set_schema() -> dict {
  return schema_closed_object(
    {id: schema_string(), digest: schema_string(), cases: schema_list(schema_string())},
  )
}

fn __manifest_schema() -> Schema<ExperimentManifest> {
  return schema_closed_object(
    {
      schema: schema_literal("harn.experiment.v1"),
      experiment_id: schema_string(),
      hypothesis: schema_string(),
      owner: schema_string(),
      baseline: __arm_schema(),
      candidates: schema_list(__arm_schema()),
      decision: schema_closed_object(
        {delta: schema_float(), epsilon: schema_float(), ladder: schema_list(schema_int())},
      ),
      metrics: schema_closed_object(
        {primary: __metric_schema(), guardrails: schema_list(__guardrail_schema())},
      ),
      assignment: schema_closed_object(
        {
          mode: schema_literal("randomized_block"),
          seed: schema_string(),
          blocking_factors: schema_list(schema_string()),
        },
      ),
      splits: schema_closed_object(
        {
          iterate: __case_set_schema(),
          gate: __case_set_schema(),
          promotion: schema_literal("explicit"),
        },
      ),
      budget: schema_closed_object(
        {max_spend_usd: schema_float(), max_trials_per_case: schema_int()},
      ),
    },
  )
}

fn __nonempty_issue(value: string, path: string) -> ValidationIssue? {
  if trim(value) != "" {
    return nil
  }
  return validation_issue("experiment.required", path + " must be non-empty", path)
}

fn __append_issue(issues: list<ValidationIssue>, issue: ValidationIssue?) -> list<ValidationIssue> {
  if issue == nil {
    return issues
  }
  return issues + [issue]
}

fn __bounds_issues(bounds: MetricBounds, path: string) -> list<ValidationIssue> {
  if bounds.lo < bounds.hi {
    return []
  }
  return [
    validation_issue(
      "experiment.metric_bounds",
      path + ".lo must be less than " + path + ".hi",
      path,
    ),
  ]
}

fn __metric_issues(manifest: ExperimentManifest) -> list<ValidationIssue> {
  let issues: list<ValidationIssue> = []
  issues = __append_issue(
    issues,
    __nonempty_issue(manifest.metrics.primary.id, "metrics.primary.id"),
  )
  issues = issues + __bounds_issues(manifest.metrics.primary.bounds, "metrics.primary.bounds")
  let seen: dict<string, bool> = {[manifest.metrics.primary.id]: true}
  let index = 0
  for guardrail in manifest.metrics.guardrails {
    const path = "metrics.guardrails[" + to_string(index) + "]"
    issues = __append_issue(issues, __nonempty_issue(guardrail.id, path + ".id"))
    issues = issues + __bounds_issues(guardrail.bounds, path + ".bounds")
    if seen[guardrail.id] ?? false {
      issues = issues
        + [
        validation_issue("experiment.duplicate_metric", "metric ids must be unique", path + ".id"),
      ]
    }
    seen = seen + {[guardrail.id]: true}
    if guardrail.alarm.threshold <= 0.0 {
      issues = issues
        + [
        validation_issue(
          "experiment.alarm_threshold",
          "guardrail alarm thresholds must be positive",
          path + ".alarm.threshold",
        ),
      ]
    }
    index = index + 1
  }
  return issues
}

fn __arm_issues(manifest: ExperimentManifest) -> list<ValidationIssue> {
  let issues: list<ValidationIssue> = []
  issues = __append_issue(issues, __nonempty_issue(manifest.baseline.id, "baseline.id"))
  if manifest.baseline.complexity < 0 {
    issues = issues
      + [
      validation_issue(
        "experiment.arm_complexity",
        "arm complexity must be nonnegative",
        "baseline.complexity",
      ),
    ]
  }
  if len(manifest.candidates) == 0 {
    issues = issues
      + [
      validation_issue(
        "experiment.candidates",
        "at least one candidate arm is required",
        "candidates",
      ),
    ]
  }
  let seen: dict<string, bool> = {[manifest.baseline.id]: true}
  let index = 0
  for arm in manifest.candidates {
    const path = "candidates[" + to_string(index) + "]"
    issues = __append_issue(issues, __nonempty_issue(arm.id, path + ".id"))
    if seen[arm.id] ?? false {
      issues = issues
        + [
        validation_issue(
          "experiment.duplicate_arm",
          "baseline and candidate arm ids must be unique",
          path + ".id",
        ),
      ]
    }
    seen = seen + {[arm.id]: true}
    if arm.complexity < 0 {
      issues = issues
        + [
        validation_issue(
          "experiment.arm_complexity",
          "arm complexity must be nonnegative",
          path + ".complexity",
        ),
      ]
    }
    index = index + 1
  }
  return issues
}

fn __policy_issues(manifest: ExperimentManifest) -> list<ValidationIssue> {
  let issues: list<ValidationIssue> = []
  if manifest.decision.delta <= 0.0 || manifest.decision.delta >= 1.0 {
    issues = issues
      + [
      validation_issue("experiment.delta", "decision.delta must be in (0, 1)", "decision.delta"),
    ]
  }
  const primary_width = manifest.metrics.primary.bounds.hi
  -manifest.metrics.primary.bounds.lo
  if manifest.decision.epsilon <= 0.0 || manifest.decision.epsilon >= primary_width {
    issues = issues
      + [
      validation_issue(
        "experiment.epsilon",
        "decision.epsilon must be positive and smaller than the primary metric range",
        "decision.epsilon",
      ),
    ]
  }
  let previous = 0
  for rung in manifest.decision.ladder {
    if rung <= previous {
      issues = issues
        + [
        validation_issue(
          "experiment.ladder",
          "decision.ladder must be non-empty, positive, and strictly increasing",
          "decision.ladder",
        ),
      ]
      break
    }
    previous = rung
  }
  if len(manifest.decision.ladder) == 0 {
    issues = issues
      + [
      validation_issue("experiment.ladder", "decision.ladder must be non-empty", "decision.ladder"),
    ]
  }
  if manifest.budget.max_spend_usd <= 0.0 {
    issues = issues
      + [
      validation_issue(
        "experiment.spend_budget",
        "budget.max_spend_usd must be positive",
        "budget.max_spend_usd",
      ),
    ]
  }
  if manifest.budget.max_trials_per_case < previous {
    issues = issues
      + [
      validation_issue(
        "experiment.trial_ceiling",
        "budget.max_trials_per_case must cover the deepest ladder rung",
        "budget.max_trials_per_case",
      ),
    ]
  }
  return issues
}

fn __case_set_issues(case_set: CaseSet, path: string) -> list<ValidationIssue> {
  let issues: list<ValidationIssue> = []
  issues = __append_issue(issues, __nonempty_issue(case_set.id, path + ".id"))
  issues = __append_issue(issues, __nonempty_issue(case_set.digest, path + ".digest"))
  if len(case_set.cases) == 0 {
    issues = issues
      + [
      validation_issue("experiment.case_set", path + ".cases must be non-empty", path + ".cases"),
    ]
  }
  let seen: dict<string, bool> = {}
  let index = 0
  for case_id in case_set.cases {
    if trim(case_id) == "" || (seen[case_id] ?? false) {
      issues = issues
        + [
        validation_issue(
          "experiment.case_set",
          path + ".cases must contain unique non-empty ids",
          path + ".cases[" + to_string(index) + "]",
        ),
      ]
    }
    seen = seen + {[case_id]: true}
    index = index + 1
  }
  return issues
}

fn __manifest_rules(manifest: ExperimentManifest) -> list<ValidationIssue> {
  let issues: list<ValidationIssue> = []
  issues = __append_issue(issues, __nonempty_issue(manifest.experiment_id, "experiment_id"))
  issues = __append_issue(issues, __nonempty_issue(manifest.hypothesis, "hypothesis"))
  issues = __append_issue(issues, __nonempty_issue(manifest.owner, "owner"))
  issues = issues + __arm_issues(manifest)
  issues = issues + __metric_issues(manifest)
  issues = issues + __policy_issues(manifest)
  issues = issues + __case_set_issues(manifest.splits.iterate, "splits.iterate")
  issues = issues + __case_set_issues(manifest.splits.gate, "splits.gate")
  if manifest.splits.iterate.digest == manifest.splits.gate.digest {
    issues = issues
      + [
      validation_issue(
        "experiment.split_alias",
        "iterate and gate case sets must have different digests",
        "splits",
      ),
    ]
  }
  for case_id in manifest.splits.iterate.cases {
    if manifest.splits.gate.cases.contains(case_id) {
      issues = issues
        + [
        validation_issue(
          "experiment.split_overlap",
          "iterate and gate case sets must be disjoint",
          "splits",
        ),
      ]
      break
    }
  }
  return issues
}

/**
 * Closed structural contract for `harn.experiment.v1`.
 *
 * Opaque arm config maps are the sole open objects. Every experiment-owned
 * record rejects unknown fields.
 *
 * @effects: []
 * @errors: []
 */
pub fn experiment_manifest_contract() -> SchemaContract<ExperimentManifest> {
  return schema_contract(
    __manifest_schema(),
    [validation_rule("experiment_manifest", __manifest_rules)],
  )
}

fn __blocking_issues(
  manifest: ExperimentManifest,
  context: ExperimentValidationContext,
) -> list<ValidationIssue> {
  let issues: list<ValidationIssue> = []
  let seen: dict<string, bool> = {}
  let index = 0
  for factor in manifest.assignment.blocking_factors {
    if trim(factor) == "" || (seen[factor] ?? false) {
      issues = issues
        + [
        validation_issue(
          "experiment.blocking_factor",
          "blocking factors must be unique non-empty ids",
          "assignment.blocking_factors[" + to_string(index) + "]",
        ),
      ]
    } else if !context.supported_blocking_factors.contains(factor) {
      issues = issues
        + [
        validation_issue(
          "experiment.unknown_blocking_factor",
          "the executing host does not support blocking factor '" + factor + "'",
          "assignment.blocking_factors[" + to_string(index) + "]",
        ),
      ]
    }
    seen = seen + {[factor]: true}
    index = index + 1
  }
  if context.host_identity_available && !manifest.assignment.blocking_factors.contains("host") {
    issues = issues
      + [
      validation_issue(
        "experiment.host_block_required",
        "host must be a blocking factor when host identity is available",
        "assignment.blocking_factors",
      ),
    ]
  }
  return issues
}

/**
 * Validate an untrusted manifest once at its owning boundary.
 *
 * The context is host capability, not experiment policy. It lets a portable
 * host reject factors it cannot realize without teaching Harn machine topology.
 *
 * @effects: []
 * @errors: []
 */
pub fn validate_experiment_manifest(
  value: unknown,
  context: ExperimentValidationContext,
) -> Result<ExperimentManifest, SchemaContractFailure> {
  const checked = schema_contract_check(value, experiment_manifest_contract())
  if !is_ok(checked) {
    return checked
  }
  const manifest = unwrap(checked)
  const issues = __blocking_issues(manifest, context)
  if len(issues) > 0 {
    return Err(
      {
        kind: "rule_failed",
        detail: to_string(len(issues)) + " validation issue(s)",
        issues: issues,
      },
    )
  }
  return Ok(manifest)
}

fn __digest(value: unknown) -> string {
  return "harn-value-v1:" + to_string(hash_value(value))
}

fn __case_set_for_phase(manifest: ExperimentManifest, phase: ExperimentPhase) -> CaseSet {
  if phase == "gate" {
    return manifest.splits.gate
  }
  return manifest.splits.iterate
}

/**
 * Freeze a validated manifest into immutable run state.
 *
 * The editable source manifest is never reread. `phase = "gate"` is rejected:
 * only `promote_experiment` can construct a gate registration.
 *
 * @effects: []
 * @errors: [validation]
 */
pub fn register_experiment(manifest: ExperimentManifest) -> ExperimentRegistration {
  const minimum_trials = manifest.decision.ladder[0]
  if minimum_trials == nil {
    throw "std/eval/experiment: validated decision ladder is empty"
  }
  const manifest_digest = __digest(manifest)
  const identity = {
    manifest_digest: manifest_digest,
    phase: "iterate",
    case_set: manifest.splits.iterate,
  }
  return {
    schema: "harn.experiment.registration.v1",
    schema_version: 1,
    registration_id: "expreg-" + to_string(hash_value(identity)),
    manifest_digest: manifest_digest,
    phase: "iterate",
    experiment_id: manifest.experiment_id,
    hypothesis: manifest.hypothesis,
    owner: manifest.owner,
    baseline: manifest.baseline,
    candidates: manifest.candidates,
    decision: manifest.decision,
    metrics: manifest.metrics,
    assignment: manifest.assignment,
    case_set: __case_set_for_phase(manifest, "iterate"),
    gate_case_set: manifest.splits.gate,
    budget: manifest.budget,
    min_trials_per_case: minimum_trials,
    prior_spend_usd: 0.0,
  }
}

/**
 * Durable typed contract for frozen registrations.
 *
 * @effects: []
 * @errors: []
 */
pub fn experiment_registration_contract() -> VersionedContract<ExperimentRegistration> {
  return versioned_contract(
    "harn.experiment.registration",
    1,
    schema_contract(schema_of(ExperimentRegistration), []),
  )
}

/**
 * Reusable descriptor for a frozen registration artifact.
 *
 * @effects: []
 * @errors: [validation]
 */
pub fn experiment_registration_descriptor(
  name: string = "experiment-registration.json",
) -> ArtifactDescriptor<ExperimentRegistration> {
  return versioned_descriptor(name, experiment_registration_contract())
}