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
import {
  LlmCallerTransport,
  __normalize_llm_caller_transport,
  __validate_llm_caller,
} from "std/agent/caller_transport"
import { MonologueActuationOptions } from "std/agent/monologue_actuation_types"
import "std/agent/options_formats"
import "std/agent/options_types"
import { completion_judge_feedback_prompt, completion_judge_system_prompt } from "std/agent/prompts"
import { agent_reasoning_apply } from "std/agent/reasoning"
import { AgentLoopRetryOptions, agent_apply_default_retry } from "std/agent/retry"
import { agent_scratchpad_options } from "std/agent/scratchpad"
import { pack_for } from "std/llm/defaults"
import { project_context_profile } from "std/project"

pub fn __native_tools_complete_naturally(opts) {
  return opts?.tools != nil && opts?.tool_format == "native"
}

pub fn __task_ledger_deliverable_text(value) {
  const text = if type_of(value) == "string" {
    value
  } else {
    to_string(value)
  }
  return trim(text)
}

pub fn __task_ledger_from_shorthand(opts) {
  const root_task = trim(opts?.root_task ?? "")
  let deliverables = []
  const shorthand = opts?.deliverables
  if type_of(shorthand) == "list" {
    for (index, item) in iter(shorthand).enumerate() {
      const text = __task_ledger_deliverable_text(item)
      if text != "" {
        deliverables = deliverables.push(
          {id: "deliverable-" + to_string(index + 1), text: text, status: "open", note: nil},
        )
      }
    }
  }
  if root_task == "" && len(deliverables) == 0 {
    return nil
  }
  return {root_task: root_task, deliverables: deliverables, rationale: "", observations: []}
}

pub fn __with_task_ledger_shorthand(opts) {
  if __has_key(opts, "task_ledger") {
    return opts
  }
  const ledger = __task_ledger_from_shorthand(opts)
  if ledger == nil {
    return opts
  }
  return opts + {task_ledger: ledger}
}

pub fn __context_profile_root(opts) {
  return opts?.context_profile_root ?? opts?.project_root ?? opts?.root ?? opts?.cwd ?? "."
}

pub fn __context_profile_options(opts) {
  let profile_opts = opts?.context_profile_options ?? {}
  if opts?.context_signals != nil {
    profile_opts = profile_opts + {signals: opts.context_signals}
  }
  if opts?.code_librarian_signals != nil {
    profile_opts = profile_opts + {signals: opts.code_librarian_signals}
  }
  return profile_opts
}

pub fn __context_profile_disabled(opts) {
  const value = opts?.auto_context_profile
  return type_of(value) == "bool" && !value
}

pub fn __with_project_context_profile(opts) {
  if __has_key(opts, "context_profile") || __has_key(opts, "project_context_profile")
    || __context_profile_disabled(
    opts,
  ) {
    return opts
  }
  const resolved = try {
    project_context_profile(__context_profile_root(opts), __context_profile_options(opts))
  }
  if is_err(resolved) {
    return opts + {_context_profile_error: to_string(unwrap_err(resolved))}
  }
  return opts + {context_profile: unwrap(resolved)}
}

pub fn __validate_done_sentinel(value) {
  if value == nil {
    return
  }
  const kind = type_of(value)
  if kind != "string" {
    throw "agent_loop: `done_sentinel` must be a non-empty string or nil; got " + kind
  }
  if trim(value) == "" {
    throw "agent_loop: `done_sentinel` must be a non-empty string or nil; got empty string"
  }
}

pub fn __validate_verify_completion(value) {
  if value == nil {
    return
  }
  if type_of(value) != "closure" {
    throw "agent_loop: `verify_completion` must be a closure or nil; got " + type_of(value)
  }
}

pub fn __validate_judge_dict_or_bool(label, value) {
  if value == nil {
    return
  }
  const kind = type_of(value)
  if kind != "dict" && kind != "bool" {
    throw "agent_loop: `" + label + "` must be a dict, bool, or nil; got " + kind
  }
}

pub fn __validate_optional_positive_int(label, field, value) {
  if value == nil {
    return
  }
  if type_of(value) != "int" {
    throw "agent_loop: `" + label + "." + field + "` must be an integer or nil; got "
      + type_of(value)
  }
  if value < 1 {
    throw "agent_loop: `" + label + "." + field + "` must be >= 1"
  }
}

pub fn __validate_optional_nonnegative_int(label, field, value) {
  if value == nil {
    return
  }
  if type_of(value) != "int" {
    throw "agent_loop: `" + label + "." + field + "` must be an integer or nil; got "
      + type_of(value)
  }
  if value < 0 {
    throw "agent_loop: `" + label + "." + field + "` must be >= 0"
  }
}

pub fn __validate_judge_cadence(label, value) {
  if value == nil {
    return
  }
  if type_of(value) != "dict" {
    throw "agent_loop: `" + label + "` must be a dict or nil; got " + type_of(value)
  }
  __validate_optional_positive_int(label, "every", value?.every)
  __validate_optional_nonnegative_int(label, "max_invocations", value?.max_invocations)
  __validate_optional_nonnegative_int(
    label,
    "min_iterations_before_first",
    value?.min_iterations_before_first,
  )
  const when = value?.when
  if when == nil {
    return
  }
  const kind = type_of(when)
  if kind == "closure" {
    return
  }
  if kind == "string" && (when == "always" || when == "stalled") {
    return
  }
  throw "agent_loop: `" + label + ".when` must be \"always\", \"stalled\", a closure, or nil; got "
    + to_string(
    when,
  )
}

pub fn __validate_done_judge_cadence(value) {
  if type_of(value) == "dict" {
    __validate_optional_nonnegative_int("done_judge", "max_invocations", value?.max_invocations)
    __validate_optional_nonnegative_int("done_judge", "max_feedback", value?.max_feedback)
    __validate_judge_cadence("done_judge.cadence", value?.cadence)
  }
}

pub fn __positive_int_budget_field(value, fallback, label) {
  if value == nil {
    return fallback
  }
  if type_of(value) != "int" {
    throw "agent_loop: `" + label + "` must be a positive integer; got " + type_of(value)
  }
  if value < 1 {
    throw "agent_loop: `" + label + "` must be a positive integer; got " + to_string(value)
  }
  return value
}

pub fn __nonnegative_int_budget_field(value, fallback, label) {
  if value == nil {
    return fallback
  }
  if type_of(value) != "int" {
    throw "agent_loop: `" + label + "` must be a non-negative integer; got " + type_of(value)
  }
  if value < 0 {
    throw "agent_loop: `" + label + "` must be a non-negative integer; got " + to_string(value)
  }
  return value
}

pub fn __positive_float_budget_field(value, fallback, label) {
  if value == nil {
    return fallback
  }
  const kind = type_of(value)
  if kind != "float" && kind != "int" {
    throw "agent_loop: `" + label + "` must be a positive number; got " + kind
  }
  const parsed = to_float(value)
  if parsed <= 0.0 {
    throw "agent_loop: `" + label + "` must be a positive number; got " + to_string(value)
  }
  return parsed
}

pub fn __normalize_consecutive_failure_budget(value) {
  if value == nil {
    return nil
  }
  if type_of(value) != "dict" {
    throw "agent_loop: `iteration_budget.consecutive_failures` must be a dict or nil; got "
      + type_of(
      value,
    )
  }
  if value?.max == nil {
    throw "agent_loop: `iteration_budget.consecutive_failures.max` is required"
  }
  const max_count = __positive_int_budget_field(
    value?.max,
    nil,
    "iteration_budget.consecutive_failures.max",
  )
  const kinds = value?.kinds ?? ["transient", "rate_limit", "provider_5xx"]
  if type_of(kinds) != "list" {
    throw "agent_loop: `iteration_budget.consecutive_failures.kinds` must be a list of strings; got "
      + type_of(
      kinds,
    )
  }
  if len(kinds) == 0 {
    throw "agent_loop: `iteration_budget.consecutive_failures.kinds` must not be empty"
  }
  for kind in kinds {
    if type_of(kind) != "string" || kind == "" {
      throw "agent_loop: `iteration_budget.consecutive_failures.kinds` must contain non-empty strings"
    }
  }
  return {
    max: max_count,
    kinds: kinds,
    paused_for_ms: __nonnegative_int_budget_field(
      value?.paused_for_ms,
      0,
      "iteration_budget.consecutive_failures.paused_for_ms",
    ),
  }
}

pub fn __validate_loop_control(value) {
  if value == nil {
    return
  }
  if type_of(value) != "closure" {
    throw "agent_loop: `loop_control` must be a closure or nil; got " + type_of(value)
  }
}

pub fn __validate_terminal_callback(value) {
  if value == nil {
    return
  }
  if type_of(value) != "closure" {
    throw "agent_loop: `terminal_callback` must be a closure or nil; got " + type_of(value)
  }
}

pub fn __validate_on_delta(value) {
  if value == nil {
    return
  }
  if type_of(value) != "closure" {
    throw "agent_loop: `on_delta` must be a closure or nil; got " + type_of(value)
  }
}

pub fn __validate_tool_caller(value) {
  if value == nil {
    return
  }
  if type_of(value) != "closure" {
    throw "agent_loop: `tool_caller` must be a closure or nil; got " + type_of(value)
  }
}

pub fn __validate_structural_validator(value) {
  if value == nil {
    return
  }
  if type_of(value) != "closure" {
    throw "agent_loop: `structural_validator` must be a closure or nil; got " + type_of(value)
  }
}

pub fn __validate_pre_turn_scope_classifier(value) {
  if value == nil {
    return
  }
  if type_of(value) != "closure" {
    throw "agent_loop: `pre_turn_scope_classifier` must be a closure or nil; got " + type_of(value)
  }
}

pub fn __validate_input_guardrail(value) {
  if value == nil {
    return
  }
  if type_of(value) != "closure" {
    throw "agent_loop: `input_guardrail` must be a closure or nil; got " + type_of(value)
  }
}

pub fn __validate_missing_tool_call_recovery(value) {
  if value == nil {
    return
  }
  if type_of(value) == "bool" {
    return
  }
  if type_of(value) != "dict" {
    throw "agent_loop: `missing_tool_call_recovery` must be a bool, dict, or nil; got "
      + type_of(
      value,
    )
  }
  if value?.enabled != nil && type_of(value.enabled) != "bool" {
    throw "agent_loop: `missing_tool_call_recovery.enabled` must be a bool or nil; got "
      + type_of(
      value.enabled,
    )
  }
  const classifier = value?.classifier
  if classifier != nil && type_of(classifier) != "closure" {
    throw "agent_loop: `missing_tool_call_recovery.classifier` must be a closure or nil; got "
      + type_of(
      classifier,
    )
  }
}

pub fn __validate_max_concurrent_tools(value) {
  if value == nil {
    return
  }
  if type_of(value) != "int" {
    throw "agent_loop: `max_concurrent_tools` must be an integer or nil; got " + type_of(value)
  }
  if value < 1 {
    throw "agent_loop: `max_concurrent_tools` must be >= 1; got " + to_string(value)
  }
}

pub fn __validate_prefetch_next_turn(value) {
  if value == nil {
    return
  }
  if type_of(value) != "bool" {
    throw "agent_loop: `prefetch_next_turn` must be a bool or nil; got " + type_of(value)
  }
}

pub fn __validate_string_list(label, value) {
  if value == nil {
    return
  }
  if type_of(value) != "list" {
    throw label + " must be a list of strings or nil; got " + type_of(value)
  }
  for item in value {
    if type_of(item) != "string" || item == "" {
      throw label + " entries must be non-empty strings"
    }
  }
}

pub fn __default_tool_surface_hard_keep() {
  return [
    "look",
    "search",
    "lookup",
    "replace_symbol",
    "done_sentinel",
    "load_skill",
    "agent_await_resumption",
  ]
}

pub fn __tool_surface_safe_defaults() {
  return {
    enabled: true,
    window_turns: 5,
    mode: "safe",
    hard_keep: __default_tool_surface_hard_keep(),
    prune_classes: ["read_only"],
    keep_classes: [
      "mutating",
      "approval",
      "session_control",
      "progress",
      "result_polling",
      "unknown",
    ],
    unknown_tool_policy: "keep",
  }
}

pub fn __tool_surface_aggressive_defaults() {
  return {
    enabled: true,
    window_turns: 5,
    mode: "aggressive",
    hard_keep: __default_tool_surface_hard_keep(),
    prune_classes: [
      "read_only",
      "mutating",
      "approval",
      "session_control",
      "progress",
      "result_polling",
      "unknown",
    ],
    keep_classes: [],
    unknown_tool_policy: "prune",
  }
}

pub fn __validate_tool_surface_mode(mode) {
  if mode == "safe" || mode == "aggressive" {
    return
  }
  throw "agent_loop: `tool_surface_narrowing.mode` must be \"safe\" or \"aggressive\""
}

pub fn __validate_tool_surface_unknown_policy(policy) {
  if policy == "keep" || policy == "prune" {
    return
  }
  throw "agent_loop: `tool_surface_narrowing.unknown_tool_policy` must be \"keep\" or \"prune\""
}

pub fn __default_read_only_stance() {
  return {
    enabled: false,
    armed: nil,
    escape_tool: "request_write_access",
    consent_check: nil,
    classifier: nil,
    min_confidence: 0.8,
    hard_keep: [],
  }
}

pub fn __normalize_read_only_stance(value) {
  const defaults = __default_read_only_stance()
  if value == nil {
    return defaults
  }
  if type_of(value) == "bool" {
    return defaults + {enabled: value}
  }
  if type_of(value) != "dict" {
    throw "agent_loop: `read_only_stance` must be a dict, bool, or nil; got " + type_of(value)
  }
  const enabled = value?.enabled ?? true
  if type_of(enabled) != "bool" {
    throw "agent_loop: `read_only_stance.enabled` must be a bool"
  }
  const armed = value?.armed
  if armed != nil && type_of(armed) != "bool" {
    throw "agent_loop: `read_only_stance.armed` must be a bool or nil"
  }
  const escape_tool = value?.escape_tool ?? defaults.escape_tool
  if type_of(escape_tool) != "string" || escape_tool == "" {
    throw "agent_loop: `read_only_stance.escape_tool` must be a non-empty string"
  }
  const min_confidence = value?.min_confidence ?? defaults.min_confidence
  if type_of(min_confidence) != "float" && type_of(min_confidence) != "int" {
    throw "agent_loop: `read_only_stance.min_confidence` must be a number"
  }
  const hard_keep = value?.hard_keep ?? defaults.hard_keep
  __validate_string_list("agent_loop: `read_only_stance.hard_keep`", hard_keep)
  return defaults
    + value
    + {
    enabled: enabled,
    armed: armed,
    escape_tool: escape_tool,
    min_confidence: min_confidence,
    hard_keep: hard_keep,
  }
}

pub fn __normalize_tool_surface_narrowing(value) {
  const safe_defaults = __tool_surface_safe_defaults()
  if value == nil {
    return safe_defaults
  }
  if type_of(value) == "bool" {
    return safe_defaults + {enabled: value}
  }
  if type_of(value) != "dict" {
    throw "agent_loop: `tool_surface_narrowing` must be a dict, bool, or nil; got " + type_of(value)
  }
  const requested_mode = value?.mode ?? safe_defaults.mode
  __validate_tool_surface_mode(requested_mode)
  const defaults = if requested_mode == "aggressive" {
    __tool_surface_aggressive_defaults()
  } else {
    safe_defaults
  }
  const enabled = value?.enabled ?? defaults.enabled
  if type_of(enabled) != "bool" {
    throw "agent_loop: `tool_surface_narrowing.enabled` must be a bool"
  }
  const window_turns = value?.window_turns ?? value?.narrow_window_turns ?? defaults.window_turns
  if type_of(window_turns) != "int" {
    throw "agent_loop: `tool_surface_narrowing.window_turns` must be an integer"
  }
  if window_turns < 1 {
    throw "agent_loop: `tool_surface_narrowing.window_turns` must be >= 1"
  }
  const hard_keep = value?.hard_keep ?? defaults.hard_keep
  __validate_string_list("agent_loop: `tool_surface_narrowing.hard_keep`", hard_keep)
  const prune_classes = value?.prune_classes ?? defaults.prune_classes
  __validate_string_list("agent_loop: `tool_surface_narrowing.prune_classes`", prune_classes)
  const keep_classes = value?.keep_classes ?? defaults.keep_classes
  __validate_string_list("agent_loop: `tool_surface_narrowing.keep_classes`", keep_classes)
  const unknown_tool_policy = value?.unknown_tool_policy ?? defaults.unknown_tool_policy
  __validate_tool_surface_unknown_policy(unknown_tool_policy)
  return defaults
    + value
    + {
    enabled: enabled,
    window_turns: window_turns,
    mode: requested_mode,
    hard_keep: hard_keep,
    prune_classes: prune_classes,
    keep_classes: keep_classes,
    unknown_tool_policy: unknown_tool_policy,
  }
}

pub fn __validate_removed_agent_loop_options(opts) {
  if __has_key(opts, "persistent") {
    throw "agent_loop: `persistent` was removed; use `loop_until_done` for completion looping and `session_id` for transcript persistence"
  }
}

pub fn __validate_agent_loop_completion_options(opts) {
  if __has_key(opts, "done_sentinel") {
    __validate_done_sentinel(opts.done_sentinel)
  }
  if __has_key(opts, "verify_completion") {
    __validate_verify_completion(opts.verify_completion)
  }
  if __has_key(opts, "verify_completion_judge") {
    __validate_judge_dict_or_bool("verify_completion_judge", opts.verify_completion_judge)
    if type_of(opts.verify_completion_judge) == "dict" {
      __validate_optional_nonnegative_int(
        "verify_completion_judge",
        "max_invocations",
        opts.verify_completion_judge?.max_invocations,
      )
      __validate_optional_nonnegative_int(
        "verify_completion_judge",
        "max_feedback",
        opts.verify_completion_judge?.max_feedback,
      )
    }
  }
  if __has_key(opts, "done_judge") {
    __validate_judge_dict_or_bool("done_judge", opts.done_judge)
    __validate_done_judge_cadence(opts.done_judge)
  }
}

pub fn __is_transcript_projection_policy(policy) {
  return policy == "raw" || policy == "clean_tool_repair" || policy == "squash_failed_calls"
    || policy
    == "summary_prefix"
    || policy == "reachability_gc"
    || policy == "context_gc"
    || policy == "tool_result_gc"
    || policy == "custom"
}

pub fn __is_reachability_projection_policy(policy) {
  return policy == "reachability_gc" || policy == "context_gc" || policy == "tool_result_gc"
}

pub fn __validate_non_negative_int_option(label, value) {
  if value != nil && (type_of(value) != "int" || value < 0) {
    throw "agent_loop: `" + label + "` must be a non-negative integer"
  }
}

pub fn __validate_transcript_projection(value) {
  if value == nil {
    return
  }
  if type_of(value) == "string" {
    const policy = value
    if !__is_transcript_projection_policy(policy) {
      throw "agent_loop: `transcript_projection` policy must be one of raw, clean_tool_repair, squash_failed_calls, summary_prefix, reachability_gc, custom; got "
        + policy
    }
    return
  }
  if type_of(value) != "dict" {
    throw "agent_loop: `transcript_projection` must be a string, dict, or nil; got "
      + type_of(value)
  }
  const policy = value?.policy ?? "raw"
  if type_of(policy) != "string" {
    throw "agent_loop: `transcript_projection.policy` must be a string"
  }
  if !__is_transcript_projection_policy(policy) {
    throw "agent_loop: `transcript_projection.policy` must be one of raw, clean_tool_repair, squash_failed_calls, summary_prefix, reachability_gc, custom; got "
      + policy
  }
  if policy == "custom" {
    const projector = value?.projector ?? value?.custom
    if projector == nil || type_of(projector) != "closure" {
      throw "agent_loop: `transcript_projection.policy = \"custom\"` requires a `projector` closure"
    }
  }
  if policy == "summary_prefix" {
    __validate_non_negative_int_option("transcript_projection.keep_last", value?.keep_last)
  }
  if __is_reachability_projection_policy(policy) {
    const root_window = value?.root_window ?? value?.recent_messages ?? value?.keep_last
    __validate_non_negative_int_option("transcript_projection.root_window", root_window)
    const min_chars = value?.min_chars ?? value?.min_reclaim_chars ?? value?.tool_result_min_chars
    __validate_non_negative_int_option("transcript_projection.min_chars", min_chars)
    const require_barrier = value?.require_write_barrier ?? value?.require_barrier
    if require_barrier != nil && type_of(require_barrier) != "bool" {
      throw "agent_loop: `transcript_projection.require_write_barrier` must be a bool"
    }
  }
}

pub fn __validate_agent_loop_callback_options(opts) {
  if __has_key(opts, "loop_control") {
    __validate_loop_control(opts.loop_control)
  }
  if __has_key(opts, "terminal_callback") {
    __validate_terminal_callback(opts.terminal_callback)
  }
  if __has_key(opts, "llm_caller") {
    __validate_llm_caller(opts.llm_caller)
  }
  if __has_key(opts, "on_delta") {
    __validate_on_delta(opts.on_delta)
  }
  if __has_key(opts, "tool_caller") {
    __validate_tool_caller(opts.tool_caller)
  }
  if __has_key(opts, "structural_validator") {
    __validate_structural_validator(opts.structural_validator)
  }
  if __has_key(opts, "input_guardrail") {
    __validate_input_guardrail(opts.input_guardrail)
  }
  if __has_key(opts, "pre_turn_scope_classifier") {
    __validate_pre_turn_scope_classifier(opts.pre_turn_scope_classifier)
  }
  if __has_key(opts, "missing_tool_call_recovery") {
    __validate_missing_tool_call_recovery(opts.missing_tool_call_recovery)
  }
  if __has_key(opts, "max_concurrent_tools") {
    __validate_max_concurrent_tools(opts.max_concurrent_tools)
  }
  if __has_key(opts, "prefetch_next_turn") {
    __validate_prefetch_next_turn(opts.prefetch_next_turn)
  }
  if __has_key(opts, "tool_surface_narrowing") {
    __normalize_tool_surface_narrowing(opts.tool_surface_narrowing)
  }
  if __has_key(opts, "transcript_projection") {
    __validate_transcript_projection(opts.transcript_projection)
  }
  if __has_key(opts, "scratchpad") {
    agent_scratchpad_options(opts)
  }
}

pub fn __normalize_reminder_provider_options(opts) {
  if !__has_key(opts, "reminders") {
    return opts + {reminders: {}}
  }
  if type_of(opts.reminders) == "list" {
    return opts + {reminders: {providers: opts.reminders}}
  }
  return opts
}

pub fn __with_resolved_tool_format(opts, resolution) {
  if resolution?.tool_format == nil {
    return opts
  }
  let next = opts
    + {
    tool_format: resolution.tool_format,
    _tool_format_source: resolution?.source ?? "unresolved",
  }
  if resolution?.capability_gap_event != nil {
    next = next + {_tool_format_capability_gap: resolution.capability_gap_event}
  }
  if resolution?.tool_format_override_event != nil {
    next = next + {_tool_format_override: resolution.tool_format_override_event}
  }
  return next
}

pub fn __auto_tool_format_source(value) {
  const source = to_string(value ?? "")
  return source == "capabilities" || source == "fallback" || source == "unresolved"
}

pub fn __clear_tool_format_resolution(opts) {
  return opts.remove("tool_format").remove("_tool_format_source").remove(
    "_tool_format_capability_gap",
  )
    .remove("_tool_format_override")
}
/**
 * Merge per-turn LLM overrides while preserving the difference between a
 * caller-explicit `tool_format` and an auto-resolved format carried from an
 * earlier route.
 *
 * @effects: []
 * @errors: []
 * @api_stability: experimental
 */