harn-stdlib 0.10.27

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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
/**
 * std/verification — deterministic verification facts and helper shapes.
 *
 * Import: import "std/verification"
 *
 * This module is the Harn-owned home for reusable verification intelligence
 * primitives. It intentionally starts with fact capture, not policy: callers
 * can bind diagnostics and background check results to file snapshots without
 * duplicating hostlib plumbing or inventing product-specific hash logic.
 */
import { command_cancel, command_run, command_wait } from "std/command"
import { shell_quote } from "std/runtime"
import { regex_first_capture, truncate_text } from "std/text"
import "std/verification_types"

pub fn verification_file_hash_snapshot(paths: list<string>) -> VerificationFileHashSnapshot {
  return hostlib_code_index_file_hash_snapshot({paths: paths})
}

pub fn verification_snapshot_staleness(bound_snapshot, current_hashes) -> dict {
  const bound = __verification_snapshot_map(bound_snapshot)
  if type_of(bound) != "dict" || len(keys(bound)) == 0 {
    return {
      schema: "harn.verification.snapshot_staleness.v1",
      status: "unbound",
      stale: false,
      fresh: false,
      staleFiles: [],
      stale_files: [],
      changed_files: [],
      advisory: true,
      reason: "diagnostic carries no file-hash snapshot binding",
    }
  }
  const current = __verification_snapshot_map(current_hashes)
  const current_available = type_of(current) == "dict"
  let stale_files: list<string> = []
  for path in keys(bound).sort() {
    if !current_available || current[path] != bound[path] {
      stale_files = stale_files + [path]
    }
  }
  const stale = len(stale_files) > 0
  return {
    schema: "harn.verification.snapshot_staleness.v1",
    status: stale ? "bound_stale" : "bound_fresh",
    stale: stale,
    fresh: !stale,
    staleFiles: stale_files,
    stale_files: stale_files,
    changed_files: stale_files,
    advisory: stale,
    reason: stale ? "snapshot superseded or unverifiable for " + to_string(len(stale_files))
      + " file(s); re-check before trusting" : "snapshot matches current file hashes",
  }
}

pub fn __verification_opts(options) {
  return options ?? {}
}

pub fn __verification_result_ok(result) -> bool {
  if result?.ok != nil {
    return result.ok ? true : false
  }
  if result?.success != nil {
    return result.success ? true : false
  }
  const exit = to_int(result?.exit_code)
  const status = result?.status ?? "completed"
  return status == "completed" && exit == 0 && !(result?.timed_out ?? false)
}

pub fn __verification_duration_ms(result, opts) {
  if opts?.duration_ms != nil {
    return to_int(opts.duration_ms)
  }
  if result?.duration_ms != nil {
    return to_int(result.duration_ms)
  }
  if result?.execution_duration_ms != nil {
    return to_int(result.execution_duration_ms)
  }
  return nil
}

pub fn __verification_exit(result, opts) {
  if opts?.exit != nil {
    return to_int(opts.exit)
  }
  if result?.exit_code != nil {
    return to_int(result.exit_code)
  }
  if result?.status == "timed_out" || result?.timed_out == true {
    return 124
  }
  if result?.ok != nil || result?.success != nil {
    return __verification_result_ok(result) ? 0 : 1
  }
  return nil
}

pub fn __verification_warm(opts) -> bool {
  if opts?.warm != nil {
    return opts.warm ? true : false
  }
  if opts?.cold != nil {
    return !(opts.cold ? true : false)
  }
  return true
}

pub fn __verification_snapshot_map(value) {
  if value == nil {
    return nil
  }
  if type_of(value) == "dict" && value?.snapshot != nil {
    return value.snapshot
  }
  return value
}

pub fn __verification_capture_snapshot(opts) {
  let snapshot_fact = nil
  let snapshot = __verification_snapshot_map(opts?.snapshot)
  if opts?.snapshot_paths != nil {
    snapshot_fact = verification_file_hash_snapshot(opts.snapshot_paths)
    snapshot = snapshot_fact.snapshot
  }
  return {fact: snapshot_fact, snapshot: snapshot}
}

pub fn __verification_result_text(result, source: string) -> string {
  if source == "stderr" {
    return result?.stderr ?? ""
  }
  if source == "stdout" {
    return result?.stdout ?? ""
  }
  if source == "combined" {
    return result?.combined ?? ((result?.stdout ?? "") + (result?.stderr ?? ""))
  }
  return ""
}

pub fn __verification_failure_signature(result, opts) {
  if opts?.failure_signature != nil {
    const explicit = to_string(opts.failure_signature)
    return trim(explicit) == "" ? nil : explicit
  }
  if __verification_result_ok(result) {
    return nil
  }
  const source = opts?.failure_signature_from ?? "combined"
  if source == "none" {
    return nil
  }
  const max_chars = to_int(opts?.max_failure_signature_chars) ?? 4000
  const text = trim(__verification_result_text(result, source))
  if text != "" {
    return truncate_text(text, max_chars)
  }
  const status = result?.status ?? "failed"
  const exit = __verification_exit(result, opts)
  return "check " + to_string(status) + " exit=" + to_string(exit)
}

pub fn __verification_row_id(row, index: int) -> string {
  const id = trim(to_string(row?.id ?? row?.name ?? ""))
  if id != "" {
    return id
  }
  return "toolchain/" + to_string(index)
}

pub fn __verification_toolchain_name(row, id: string) -> string {
  const name = trim(to_string(row?.name ?? ""))
  return name == "" ? id : name
}

pub fn __verification_toolchain_probe(row) {
  const probe = row?.versionProbe ?? row?.probe
  if probe == nil {
    return nil
  }
  if type_of(probe) == "dict" && probe?.spec != nil {
    return probe.spec
  }
  return probe
}

pub fn __verification_toolchain_probe_options(row, opts) {
  const probe = row?.versionProbe ?? row?.probe ?? {}
  return (opts?.command_options ?? {}) + (probe?.command_options ?? row?.command_options ?? {})
}

pub fn __verification_toolchain_version_pattern(row, opts) -> string {
  const probe = row?.versionProbe ?? row?.probe ?? {}
  return to_string(
    probe?.versionPattern
      ?? probe?.version_pattern
      ?? row?.versionPattern
      ?? row?.version_pattern
      ?? opts?.version_pattern
      ?? "([0-9]+(\\.[0-9A-Za-z_+\\-]+)+)",
  )
}

pub fn __verification_toolchain_raw_version(result) -> string {
  const stdout = trim(result?.stdout ?? "")
  if stdout != "" {
    return stdout
  }
  return trim(result?.stderr ?? "")
}

pub fn __verification_toolchain_cache_identity(row, opts) -> dict {
  return (opts?.cache_identity ?? {}) + (row?.cacheIdentity ?? row?.cache_identity ?? {})
}

pub fn __verification_warm_state_mode(row) {
  const mode = row?.warmMode ?? row?.warm_mode ?? row?.warmState ?? row?.warm_state
  if mode == nil {
    return nil
  }
  if type_of(mode) == "dict" {
    return mode
  }
  return {warm: mode ? true : false}
}

pub fn __verification_warm_state_id(row, index: int) -> string {
  const mode = __verification_warm_state_mode(row) ?? {}
  const id = trim(to_string(mode?.id ?? row?.row_id ?? row?.id ?? row?.name ?? ""))
  if id != "" {
    return id
  }
  return "warm-state/" + to_string(index)
}

pub fn __verification_warm_state_name(row, id: string) -> string {
  const mode = __verification_warm_state_mode(row) ?? {}
  const name = trim(to_string(mode?.name ?? row?.name ?? ""))
  return name == "" ? id : name
}

pub fn __verification_warm_state_probe(row) {
  const mode = __verification_warm_state_mode(row) ?? {}
  const probe = mode?.readyProbe ?? mode?.ready_probe ?? mode?.readinessProbe
    ?? mode?.readiness_probe
    ?? mode?.probe
    ?? row?.readyProbe
    ?? row?.ready_probe
    ?? row?.readinessProbe
    ?? row?.readiness_probe
    ?? row?.probe
  if probe == nil {
    return nil
  }
  if type_of(probe) == "dict" && probe?.spec != nil {
    return probe.spec
  }
  return probe
}

pub fn __verification_warm_state_start_spec(row) {
  const mode = __verification_warm_state_mode(row) ?? {}
  const start = mode?.startCommand
    ?? mode?.start_command
    ?? mode?.spawnCommand
    ?? mode?.spawn_command
    ?? mode?.command
    ?? row?.startCommand
    ?? row?.start_command
    ?? row?.spawnCommand
    ?? row?.spawn_command
  if start == nil {
    return nil
  }
  if type_of(start) == "dict" && start?.spec != nil {
    return start.spec
  }
  return start
}

pub fn __verification_warm_state_teardown_spec(row) {
  const mode = __verification_warm_state_mode(row) ?? {}
  const stop = mode?.teardownCommand
    ?? mode?.teardown_command
    ?? mode?.stopCommand
    ?? mode?.stop_command
    ?? row?.teardownCommand
    ?? row?.teardown_command
    ?? row?.stopCommand
    ?? row?.stop_command
  if stop == nil {
    return nil
  }
  if type_of(stop) == "dict" && stop?.spec != nil {
    return stop.spec
  }
  return stop
}

pub fn __verification_warm_state_probe_options(row, opts) {
  const mode = __verification_warm_state_mode(row) ?? {}
  const probe = mode?.readyProbe ?? mode?.ready_probe ?? mode?.readinessProbe
    ?? mode?.readiness_probe
    ?? mode?.probe
    ?? row?.readyProbe
    ?? row?.ready_probe
    ?? row?.readinessProbe
    ?? row?.readiness_probe
    ?? row?.probe
    ?? {}
  return (opts?.command_options ?? {})
    + (probe?.command_options ?? mode?.command_options
    ?? row
    ?.command_options
    ?? {})
}

pub fn __verification_warm_state_start_options(row, opts) {
  const mode = __verification_warm_state_mode(row) ?? {}
  const start = mode?.startCommand
    ?? mode?.start_command
    ?? mode?.spawnCommand
    ?? mode?.spawn_command
    ?? mode?.command
    ?? row?.startCommand
    ?? row?.start_command
    ?? row?.spawnCommand
    ?? row?.spawn_command
    ?? {}
  return (opts?.command_options ?? {})
    + (start?.command_options ?? mode?.command_options
    ?? row
    ?.command_options
    ?? {})
}

pub fn __verification_warm_state_teardown_options(row, opts) {
  const mode = __verification_warm_state_mode(row) ?? {}
  const stop = mode?.teardownCommand
    ?? mode?.teardown_command
    ?? mode?.stopCommand
    ?? mode?.stop_command
    ?? row?.teardownCommand
    ?? row?.teardown_command
    ?? row?.stopCommand
    ?? row?.stop_command
    ?? {}
  return (opts?.command_options ?? {})
    + (stop?.command_options ?? mode?.command_options
    ?? row
    ?.command_options
    ?? {})
}

pub fn __verification_warm_state_cache_identity(row, opts) -> dict {
  const mode = __verification_warm_state_mode(row) ?? {}
  return (opts?.cache_identity ?? {})
    + (row?.cacheIdentity ?? row?.cache_identity ?? {})
    + (mode?.cacheIdentity ?? mode?.cache_identity
    ?? {})
}

pub fn __verification_warm_state_expected_delta_ms(row, mode) {
  return to_int(
    mode?.expectedWarmDeltaMs
      ?? mode?.expected_warm_delta_ms
      ?? row?.expectedWarmDeltaMs
      ?? row?.expected_warm_delta_ms,
  )
}

pub fn __verification_warm_state_timings(row, mode) {
  const timings = row?.timings ?? mode?.timings ?? {}
  const warm_ms = to_int(
    mode?.warmMs ?? mode?.warm_ms ?? timings?.warmMs?.p95 ?? timings?.warm_ms?.p95,
  )
  const cold_ms = to_int(
    mode?.coldMs ?? mode?.cold_ms ?? timings?.coldMs?.p95 ?? timings?.cold_ms?.p95,
  )
  return {warm_ms: warm_ms, cold_ms: cold_ms}
}

pub fn __verification_warm_state_mode_name(mode) -> string {
  const raw = mode?.mode ?? mode?.kind ?? mode?.type ?? "probe"
  const value = lowercase(trim(to_string(raw)))
  return value == "" ? "probe" : value
}

pub fn __verification_warm_state_probe_receipt(result) -> dict {
  return {
    status: result?.status ?? "not_configured",
    exit: result == nil ? nil : __verification_exit(result, {}),
    durationMs: result?.duration_ms,
    stdout: result?.stdout ?? "",
    stderr: result?.stderr ?? "",
  }
}

pub fn __verification_warm_state_first_fact(row, opts) {
  const facts = verification_warm_state_facts([row], opts)
  return len(facts) == 0 ? nil : facts[0]
}

pub fn __verification_warm_state_receipt_handle(value) {
  if value == nil {
    return nil
  }
  if value?.handle_id != nil {
    const handle = trim(to_string(value.handle_id))
    return handle == "" ? nil : handle
  }
  if value?.started?.handle_id != nil {
    const handle = trim(to_string(value.started.handle_id))
    return handle == "" ? nil : handle
  }
  return nil
}

pub fn __verification_warm_state_cancel_options(opts) -> dict {
  let out = {}
  if opts?.wait_result_ms != nil {
    out = out + {wait_result_ms: opts.wait_result_ms}
  } else {
    out = out + {wait_result_ms: 2000}
  }
  if opts?.timed_out != nil {
    out = out + {timed_out: opts.timed_out}
  }
  return out
}

pub fn __verification_string_list(value) -> list<string> {
  if value == nil {
    return []
  }
  const raw = type_of(value) == "list" ? value : [value]
  let out: list<string> = []
  for item in raw {
    const text = trim(to_string(item ?? ""))
    if text != "" && !contains(out, text) {
      out = out + [text]
    }
  }
  return out
}

pub fn __verification_delta_config_dict(value) -> dict {
  return type_of(value) == "dict" ? value : {}
}

pub fn __verification_delta_known_options(value) -> dict {
  if type_of(value) != "dict" {
    return {}
  }
  let out = {}
  for key in [
    "lowercase",
    "strip_ansi",
    "strip_locations",
    "strip_temp_paths",
    "collapse_whitespace",
    "include_path",
    "include_code",
    "include_severity",
    "max_chars",
    "replacements",
    "ignore_patterns",
    "strip_patterns",
    "stripPatterns",
  ] {
    if value.keys().contains(key) {
      out = out + {[key]: value[key]}
    }
  }
  return out
}

pub fn __verification_delta_normalization(options) -> dict {
  const opts = __verification_opts(options)
  const row = __verification_delta_config_dict(opts?.row ?? opts?.profile_row)
  const check = __verification_delta_config_dict(row?.check)
  const direct = __verification_delta_config_dict(
    opts?.diagnosticDelta
      ?? opts?.diagnostic_delta
      ?? opts?.signatureNormalization
      ?? opts?.signature_normalization,
  )
    + __verification_delta_known_options(opts)
  return {
    lowercase: true,
    strip_ansi: true,
    strip_locations: true,
    strip_temp_paths: true,
    collapse_whitespace: true,
    include_path: false,
    include_code: true,
    include_severity: false,
    max_chars: 4000,
    replacements: [],
    ignore_patterns: [],
    strip_patterns: [],
  }
    + __verification_delta_config_dict(row?.signatureNormalization)
    + __verification_delta_config_dict(
    row?.signature_normalization,
  )
    + __verification_delta_config_dict(row?.diagnosticDelta)
    + __verification_delta_config_dict(
    row?.diagnostic_delta,
  )
    + __verification_delta_config_dict(check?.signatureNormalization)
    + __verification_delta_config_dict(
    check?.signature_normalization,
  )
    + __verification_delta_config_dict(check?.diagnosticDelta)
    + __verification_delta_config_dict(
    check?.diagnostic_delta,
  )
    + direct
}

pub fn __verification_delta_raw_text(value) -> string {
  if value == nil {
    return ""
  }
  if type_of(value) != "dict" {
    return trim(to_string(value))
  }
  const raw = value?.failureSignature
    ?? value?.failure_signature
    ?? value?.signature
    ?? value?.message
    ?? value?.text
    ?? value?.error
    ?? value?.stderr
    ?? value?.stdout
    ?? value?.combined
    ?? value?.result
    ?? ""
  return trim(to_string(raw ?? ""))
}

pub fn __verification_delta_status(value) -> string {
  const direct = lowercase(trim(to_string(value?.status ?? "")))
  if direct != "" {
    return direct
  }
  return lowercase(
    trim(to_string(value?.classification?.status ?? value?.diagnostic?.status ?? "")),
  )
}

pub fn __verification_delta_feeds_gates(value) -> bool {
  if value == nil {
    return true
  }
  if value?.feedsGates != nil {
    return value.feedsGates ? true : false
  }
  if value?.feeds_gates != nil {
    return value.feeds_gates ? true : false
  }
  if value?.classification?.feedsGates != nil {
    return value.classification.feedsGates ? true : false
  }
  if value?.classification?.feeds_gates != nil {
    return value.classification.feeds_gates ? true : false
  }
  if value?.diagnostic?.feedsGates != nil {
    return value.diagnostic.feedsGates ? true : false
  }
  if value?.diagnostic?.feeds_gates != nil {
    return value.diagnostic.feeds_gates ? true : false
  }
  const status = __verification_delta_status(value)
  return !["advisory", "bound_stale", "stale", "unbound"].contains(status)
}

pub fn __verification_delta_explicit_success(value) -> bool {
  if value == nil {
    return false
  }
  if value?.ok != nil {
    return value.ok ? true : false
  }
  if value?.success != nil {
    return value.success ? true : false
  }
  const exit = to_int(value?.exit ?? value?.exit_code)
  if exit != nil {
    return exit == 0
  }
  const status = __verification_delta_status(value)
  return ["pass", "passed", "success", "succeeded"].contains(status)
}

pub fn __verification_delta_explicit_failure(value) -> bool {
  if value == nil {
    return false
  }
  if value?.ok != nil {
    return !(value.ok ? true : false)
  }
  if value?.success != nil {
    return !(value.success ? true : false)
  }
  const exit = to_int(value?.exit ?? value?.exit_code)
  if exit != nil {
    return exit != 0
  }
  const status = __verification_delta_status(value)
  return ["error", "fail", "failed", "failure", "timed_out", "timeout"].contains(status)
}

pub fn __verification_delta_has_collection(value) -> bool {
  return type_of(value?.diagnostics) == "list" || type_of(value?.errors) == "list"
}

pub fn __verification_delta_synthetic_failure(value) -> dict {
  const status = __verification_delta_status(value)
  const exit = to_int(value?.exit ?? value?.exit_code)
  const status_text = status == "" ? "failed" : status
  const exit_text = exit == nil ? "" : " exit=" + to_string(exit)
  return {message: "check " + status_text + exit_text, code: status_text}
}

pub fn __verification_delta_raw_entries(value) -> list {
  if value == nil {
    return []
  }
  const kind = type_of(value)
  if kind == "list" {
    return value
  }
  if kind == "string" {
    return [value]
  }
  if kind != "dict" {
    return [value]
  }
  if type_of(value?.diagnostics) == "list" {
    return value.diagnostics
  }
  if type_of(value?.errors) == "list" {
    return value.errors
  }
  if type_of(value?.diagnostic) == "dict" {
    return [value.diagnostic]
  }
  if __verification_delta_raw_text(value) != "" {
    return [value]
  }
  if __verification_delta_explicit_failure(value) {
    return [__verification_delta_synthetic_failure(value)]
  }
  return []
}

pub fn __verification_delta_location(value) -> string {
  if type_of(value) != "dict" {
    return ""
  }
  let path = trim(to_string(value?.path ?? value?.file ?? value?.filename ?? value?.uri ?? ""))
  const line = to_int(value?.line ?? value?.line_number)
  if path != "" && line != nil {
    path = path + ":" + to_string(line)
    const column = to_int(value?.column ?? value?.col)
    if column != nil {
      path = path + ":" + to_string(column)
    }
  }
  return path
}

pub fn __verification_delta_replacement_pattern(entry) -> string {
  if type_of(entry) == "dict" {
    return trim(to_string(entry?.pattern ?? entry?.regex ?? ""))
  }
  return trim(to_string(entry ?? ""))
}

pub fn __verification_delta_replacement_text(entry) -> string {
  if type_of(entry) == "dict" {
    return to_string(entry?.replacement ?? entry?.with ?? " ")
  }
  return " "
}

pub fn __verification_delta_normalize_text(text: string, cfg: dict) -> string {
  let out = to_string(text ?? "")
  if cfg?.strip_ansi != false {
    out = regex_replace_all("\\u{1b}\\[[0-9;]*m", "", out)
  }
  for replacement in cfg?.replacements ?? [] {
    const pattern = __verification_delta_replacement_pattern(replacement)
    if pattern != "" {
      out = regex_replace_all(pattern, __verification_delta_replacement_text(replacement), out)
    }
  }
  for pattern in __verification_string_list(cfg?.strip_patterns ?? cfg?.stripPatterns) {
    out = regex_replace_all(pattern, " ", out)
  }
  if cfg?.lowercase != false {
    out = lowercase(out)
  }
  if cfg?.strip_locations != false {
    out = regex_replace_all("[^\\s:]+:[0-9]+(?::[0-9]+)?", " ", out)
    out = regex_replace_all("\\b(?:line|col|column)\\s+[0-9]+\\b", " ", out)
  }
  if cfg?.strip_temp_paths != false {
    out = regex_replace_all("/tmp/[^\\s:]*", " ", out)
    out = regex_replace_all("/var/folders/[^\\s:]*", " ", out)
  }
  if cfg?.collapse_whitespace != false {
    out = trim(regex_replace_all("\\s+", " ", out))
  }
  const max_chars = to_int(cfg?.max_chars) ?? 4000
  if max_chars > 0 {
    out = truncate_text(out, max_chars)
  }
  return out
}

pub fn __verification_delta_signature(value, cfg: dict) -> string {
  let parts: list<string> = []
  if type_of(value) == "dict" {
    if cfg?.include_path == true {
      const location = __verification_delta_location(value)
      if location != "" {
        parts = parts.push(location)
      }
    }
    if cfg?.include_severity == true {
      const severity = trim(to_string(value?.severity ?? value?.level ?? ""))
      if severity != "" {
        parts = parts.push(severity)
      }
    }
    if cfg?.include_code != false {
      const code = trim(to_string(value?.code ?? value?.rule ?? value?.kind ?? ""))
      if code != "" {
        parts = parts.push(code)
      }
    }
  }
  const text = __verification_delta_raw_text(value)
  if text != "" {
    parts = parts.push(text)
  }
  return __verification_delta_normalize_text(join(parts, " "), cfg)
}

pub fn __verification_delta_ignored(signature: string, cfg: dict) -> bool {
  for pattern in __verification_string_list(cfg?.ignore_patterns ?? cfg?.ignorePatterns) {
    if regex_match(pattern, signature) != nil {
      return true
    }
  }
  return false
}

pub fn __verification_delta_direct_count(value) {
  return to_int(
    value?.error_count ?? value?.diagnostic_count ?? value?.diagnostics_count
      ?? value?.failure_count,
  )
}

pub fn __verification_delta_info(value, cfg: dict) -> dict {
  const entries = __verification_delta_raw_entries(value)
  let normalized: list<string> = []
  for entry in entries {
    const signature = __verification_delta_signature(entry, cfg)
    if signature != "" && !__verification_delta_ignored(signature, cfg) {
      normalized = normalized.push(signature)
    }
  }
  let signatures: list<string> = []
  for signature in normalized {
    if !signatures.contains(signature) {
      signatures = signatures.push(signature)
    }
  }
  const direct_count = __verification_delta_direct_count(value)
  const count = direct_count ?? len(normalized)
  const has_collection = __verification_delta_has_collection(value)
  const failed = len(normalized) > 0
    || (!has_collection && __verification_delta_explicit_failure(value))
  return {
    count: count,
    explicit_success: __verification_delta_explicit_success(value),
    failed: failed,
    feedsGates: __verification_delta_feeds_gates(value),
    feeds_gates: __verification_delta_feeds_gates(value),
    has_collection: has_collection,
    signatures: signatures.sort(),
  }
}

pub fn __verification_delta_set_minus(left: list<string>, right: list<string>) -> list<string> {
  let out: list<string> = []
  for item in left {
    if !right.contains(item) && !out.contains(item) {
      out = out.push(item)
    }
  }
  return out.sort()
}

pub fn __verification_delta_set_intersection(
  left: list<string>,
  right: list<string>,
) -> list<string> {
  let out: list<string> = []
  for item in left {
    if right.contains(item) && !out.contains(item) {
      out = out.push(item)
    }
  }
  return out.sort()
}

pub fn __verification_delta_gate_signatures(info: dict) -> list<string> {
  return info.feedsGates ? info.signatures : []
}

pub fn __verification_delta_snapshot(info: dict) -> dict {
  return {
    count: info.count,
    feedsGates: info.feedsGates,
    feeds_gates: info.feeds_gates,
    signatures: info.signatures,
  }
}

pub fn __verification_delta_result(
  status: string,
  progress_credit: bool,
  previous_info: dict,
  current_info: dict,
  removed: list<string>,
  added: list<string>,
  persisted: list<string>,
  normalization: dict,
  reason: string,
) -> VerificationDiagnosticDelta {
  let out = {
    schema: "harn.verification.diagnostic_delta.v1",
    status: status,
    progress_credit: progress_credit,
    feedsGates: current_info.feedsGates,
    feeds_gates: current_info.feeds_gates,
    advisory: !current_info.feedsGates,
    reason: reason,
    previous: __verification_delta_snapshot(previous_info),
    current: __verification_delta_snapshot(current_info),
    removed: removed,
    added: added,
    persisted: persisted,
    same_signature_set: len(removed) == 0 && len(added) == 0,
    normalization: normalization,
  }
  if previous_info.feedsGates && current_info.feedsGates {
    out = out + {count_delta: current_info.count - previous_info.count}
  }
  return out
}

pub fn __verification_gate_hashes(value) -> dict {
  const snapshot = __verification_snapshot_map(value)
  return type_of(snapshot) == "dict" ? snapshot : {}
}

pub fn __verification_gate_hashes_available(value) -> bool {
  if value == nil {
    return false
  }
  return type_of(__verification_snapshot_map(value)) == "dict"
}

pub fn __verification_gate_embedded_classification(value) {
  if type_of(value) != "dict" {
    return nil
  }
  if type_of(value?.classification) == "dict" {
    return value.classification
  }
  const status = to_string(value?.status ?? "")
  if ["bound_fresh", "bound_stale", "unbound"].contains(status) {
    return value
  }
  return nil
}

pub fn __verification_gate_unclassified(value) -> dict {
  const status = __verification_delta_status(value)
  const feeds_gates = __verification_delta_feeds_gates(value)
  return {
    status: status == "" ? "unclassified" : status,
    staleFiles: [],
    stale_files: [],
    feedsGates: feeds_gates,
    feeds_gates: feeds_gates,
    advisory: !feeds_gates,
    reason:
      feeds_gates ? "current file hashes unavailable; preserving diagnostic gate status" : "diagnostic status does not feed gates",
  }
}

pub fn __verification_gate_classification(
  value,
  current_hashes: dict,
  hashes_available: bool,
) -> dict {
  const embedded = __verification_gate_embedded_classification(value)
  if embedded != nil {
    return embedded
  }
  if !hashes_available {
    return __verification_gate_unclassified(value)
  }
  const envelope = type_of(value) == "dict" ? value : nil
  return verification_diagnostic_classify(envelope, current_hashes)
}

pub fn __verification_gate_delta_value(value, classification: dict) -> dict {
  const base = if type_of(value) == "dict" {
    value
  } else if value == nil {
    {}
  } else {
    {failureSignature: to_string(value)}
  }
  const feeds_gates = classification?.feedsGates ? true : false
  return base
    + {
    classification: classification,
    feedsGates: feeds_gates,
    feeds_gates: feeds_gates,
    advisory: !feeds_gates,
  }
}

pub fn __verification_gate_status(delta: dict, classification: dict) -> string {
  if !(classification?.feedsGates ? true : false) {
    return "advisory"
  }
  const status = to_string(delta?.status ?? "")
  return status == "" ? "initialized" : status
}

pub fn __verification_gate_reason(delta: dict, classification: dict) -> string {
  if !(classification?.feedsGates ? true : false) {
    return to_string(classification?.reason ?? "current diagnostic is advisory")
  }
  return to_string(delta?.reason ?? "")
}

pub fn verification_warm_state_facts(
  rows: list<dict>,
  options = nil,
) -> list<VerificationWarmStateFact> {
  const opts = __verification_opts(options)
  let facts = []
  let index = 0
  for row in rows {
    const mode = __verification_warm_state_mode(row)
    const id = __verification_warm_state_id(row, index)
    const name = __verification_warm_state_name(row, id)
    const spec = __verification_warm_state_probe(row)
    let result = nil
    if spec != nil {
      result = command_run(spec, __verification_warm_state_probe_options(row, opts))
    }
    const probe_ready = result != nil && __verification_result_ok(result)
    const configured = mode != nil || spec != nil
    const ready = if mode?.ready != nil {
      mode.ready ? true : false
    } else if mode?.warm != nil {
      mode.warm ? true : false
    } else {
      probe_ready
    }
    const timings = __verification_warm_state_timings(row, mode ?? {})
    facts = facts
      + [
      {
        id: id,
        name: name,
        configured: configured,
        available: probe_ready,
        ready: ready,
        warm: ready,
        timing_kind: ready ? "warm" : "cold",
        mode: __verification_warm_state_mode_name(mode ?? {}),
        expected_warm_delta_ms: __verification_warm_state_expected_delta_ms(row, mode ?? {}),
        warm_ms: timings.warm_ms,
        cold_ms: timings.cold_ms,
        cache_identity: __verification_warm_state_cache_identity(row, opts),
        probe: __verification_warm_state_probe_receipt(result),
      },
    ]
    index = index + 1
  }
  return facts
}