harn-stdlib 0.8.0

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
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
// @harn-entrypoint-category agent.stdlib
import { agent_autocompact_if_needed } from "std/agent/autocompact"
import { agent_budget_post_call_blocked, agent_budget_pre_call_blocked } from "std/agent/budget"
import { agent_daemon_snapshot, agent_daemon_step } from "std/agent/daemon"
import { agent_verify_or_continue } from "std/agent/judge"
import { agent_mcp_bootstrap_if_needed } from "std/agent/mcp"
import { agent_loop_options } from "std/agent/options"
import { agent_compute_post_turn } from "std/agent/postturn"
import { agent_build_turn_messages, agent_build_turn_system } from "std/agent/preflight"
import { agent_dispatch_tool_batch, agent_dispatch_tool_call } from "std/agent/primitives"
import { native_tool_contract_feedback_prompt } from "std/agent/prompts"
import { agent_skills_match } from "std/agent/skills"
import {
  agent_emit_event,
  agent_record_native_tool_fallback,
  agent_session_drain_feedback,
  agent_session_finalize,
  agent_session_init,
  agent_session_inject_feedback,
  agent_session_record_assistant,
  agent_session_record_tool_results,
  agent_session_record_usage,
} from "std/agent/state"
import {
  agent_tool_search_emit_queries,
  agent_tool_search_inject_if_needed,
  agent_tool_search_record_results,
} from "std/agent/tool_search"

fn __default_invoke_llm(message, turn_system, llm_opts) {
  let result = try {
    llm_call(message, turn_system, llm_opts)
  }
  if !is_err(result) {
    return {ok: true, value: unwrap(result)}
  }
  let err = unwrap_err(result)
  let reason = if type_of(err) == "dict" {
    err?.reason ?? ""
  } else {
    ""
  }
  if reason == "budget_exceeded" {
    return {ok: false, status: "budget_exhausted"}
  }
  throw err
}

fn __validate_caller_result(r) {
  if type_of(r) != "dict" {
    throw "agent_loop: llm_caller must return a dict; got " + type_of(r)
  }
  if r?.ok == nil {
    throw "agent_loop: llm_caller result missing `ok`"
  }
  if r.ok && type_of(r?.value) != "dict" {
    throw "agent_loop: llm_caller returned ok=true but `value` is not a dict"
  }
  if !r.ok && type_of(r?.status) != "string" {
    throw "agent_loop: llm_caller returned ok=false but `status` is not a string"
  }
}

fn __invoke_llm(message, turn_system, llm_opts) {
  let caller = llm_opts?._llm_caller
  if caller == nil {
    return __default_invoke_llm(message, turn_system, llm_opts)
  }
  let call = {
    prompt: message,
    system: turn_system,
    opts: llm_opts,
    turn: {iteration: llm_opts?._turn_iteration ?? 0, session_id: llm_opts?.session_id ?? "", attempt: 1},
  }
  let result = try {
    caller(call)
  }
  if is_err(result) {
    throw unwrap_err(result)
  }
  let r = unwrap(result)
  __validate_caller_result(r)
  return r
}

// -------------------------------------------------------------------------------------------------

// Tool middleware seam — composable tool_caller (mirrors __invoke_llm).
//
// Each tool dispatch is funneled through `tool_caller(envelope, next)` when
// the agent_loop options carry one. The envelope normalizes the call shape
// so middleware doesn't have to peek at the underlying registry/schema:
//
//   envelope = {
//     tool_name, tool_args, call_id,
//     declared_executor?, schema?, description?,
//     turn: {iteration, session_id},
//   }
//
// The middleware returns a dispatch-shape dict. Calling `next(envelope)`
// runs the default dispatch (with any envelope mutations the middleware
// applied — typically `tool_args` rewrites or argument stripping). Callers
// can short-circuit by returning their own dict without invoking `next`.
//
// See std/llm/tool_middleware for the userspace primitives + the bundled
// middleware library (with_required_reason, with_audit_log, …).

// -------------------------------------------------------------------------------------------------

fn __tool_registry_entry(tools, tool_name) {
  if tools == nil {
    return nil
  }
  let entries = tools?.tools
  if type_of(entries) != "list" {
    return nil
  }
  for entry in entries {
    if type_of(entry) != "dict" {
      continue
    }
    let entry_name = if entry?.name != nil {
      to_string(entry.name)
    } else {
      let func = entry?.function
      if type_of(func) == "dict" {
        to_string(func?.name ?? "")
      } else {
        ""
      }
    }
    if entry_name == tool_name {
      return entry
    }
  }
  return nil
}

fn __tool_envelope(call, tools, options) {
  let tool_name = to_string(call?.name ?? call?.tool_name ?? "")
  let tool_args_raw = call?.arguments ?? call?.tool_args
  let tool_args = if type_of(tool_args_raw) == "dict" {
    tool_args_raw
  } else {
    {}
  }
  let entry = __tool_registry_entry(tools, tool_name)
  let declared_executor = if entry == nil {
    nil
  } else {
    let direct = entry?.executor
    if direct != nil {
      to_string(direct)
    } else {
      let func = entry?.function
      if type_of(func) == "dict" && func?.executor != nil {
        to_string(func.executor)
      } else {
        nil
      }
    }
  }
  let schema = if entry == nil {
    nil
  } else {
    entry?.parameters ?? entry?.input_schema ?? entry?.inputSchema
  }
  let description = if entry == nil {
    ""
  } else {
    let direct = entry?.description
    if direct != nil {
      to_string(direct)
    } else {
      let func = entry?.function
      if type_of(func) == "dict" && func?.description != nil {
        to_string(func.description)
      } else {
        ""
      }
    }
  }
  return {
    tool_name: tool_name,
    tool_args: tool_args,
    call_id: to_string(call?.id ?? call?.tool_call_id ?? ""),
    declared_executor: declared_executor,
    schema: schema,
    description: description,
    turn: {iteration: options?._turn_iteration ?? 0, session_id: to_string(options?.session_id ?? "")},
  }
}

fn __default_invoke_tool(envelope, original_call, tools, options) {
  let next_call = original_call
    + {name: envelope.tool_name, tool_name: envelope.tool_name, arguments: envelope.tool_args}
  return agent_dispatch_tool_call(next_call, tools, options)
}

fn __validate_tool_caller_result(r) {
  if type_of(r) != "dict" {
    throw "agent_loop: tool_caller must return a dict; got " + type_of(r)
  }
  let name = r?.tool_name ?? r?.name
  if name == nil || to_string(name) == "" {
    throw "agent_loop: tool_caller result missing `tool_name`"
  }
  let ok = r?.ok
  if ok == nil {
    let success = r?.success
    if success == nil {
      let status = r?.status
      if status == nil {
        throw "agent_loop: tool_caller result missing `ok`/`success`/`status`"
      }
    }
  } else if type_of(ok) != "bool" {
    throw "agent_loop: tool_caller result `ok` must be a bool; got " + type_of(ok)
  }
}

fn __middleware_exception_result(envelope, err) {
  let err_text = to_string(err)
  let observation = "[error from " + envelope.tool_name + "]\n" + err_text
    + "\n[end of "
    + envelope.tool_name
    + " error]\n"
  return {
    ok: false,
    status: "error",
    tool_name: envelope.tool_name,
    tool_call_id: envelope.call_id,
    arguments: envelope.tool_args,
    result: nil,
    rendered_result: err_text,
    observation: observation,
    error: err_text,
    error_category: "tool_middleware_exception",
    executor: nil,
  }
}

fn __invoke_tool(call, tools, options) {
  let caller = options?._tool_caller
  if caller == nil {
    return agent_dispatch_tool_call(call, tools, options)
  }
  let envelope = __tool_envelope(call, tools, options)
  let next = { env_in -> __default_invoke_tool(env_in, call, tools, options) }
  let outcome = try {
    caller(envelope, next)
  }
  if is_err(outcome) {
    let err = unwrap_err(outcome)
    __maybe_emit_tool_audit(
      envelope.turn.session_id,
      envelope,
      {layer: "tool_caller", status: "exception", error: to_string(err)},
    )
    return __middleware_exception_result(envelope, err)
  }
  let r = unwrap(outcome)
  __validate_tool_caller_result(r)
  __maybe_emit_tool_audit(envelope.turn.session_id, envelope, r?.audit)
  return r
}

fn __maybe_emit_tool_audit(session_id, envelope, audit) {
  if audit == nil {
    return
  }
  if session_id == "" {
    return
  }
  let _ = try {
    agent_emit_event(
      session_id,
      "tool_call_audit",
      {tool_call_id: envelope.call_id, tool_name: envelope.tool_name, audit: audit},
    )
  }
}

fn __visible_text(parsed, raw_text) {
  if parsed?.user_response != nil && parsed.user_response != "" {
    return parsed.user_response
  }
  if parsed?.prose != nil && parsed.prose != "" {
    return parsed.prose
  }
  return raw_text
}

fn __resolve_tool_calls(llm_result, parsed) {
  let native_calls = llm_result?.native_tool_calls ?? llm_result?.tool_calls ?? []
  if len(native_calls) > 0 {
    return native_calls
  }
  return parsed?.calls ?? []
}

fn __native_fallback_feedback(policy, fallback_index) {
  return native_tool_contract_feedback_prompt({policy: policy, fallback_index: fallback_index})
}

fn __detect_native_fallback(llm_result, parsed, turn_opts, fallback_index, session_id, turn_index) {
  let native_calls = llm_result?.native_tool_calls ?? []
  let parsed_calls = parsed?.calls ?? []
  let format = turn_opts?.tool_format ?? ""
  if format != "native" || len(native_calls) > 0 || len(parsed_calls) == 0 {
    return {triggered: false, accepted: false, fallback_index: fallback_index, calls: nil}
  }
  let new_index = fallback_index + 1
  let policy = turn_opts?.native_tool_fallback ?? "reject"
  let accepted = if policy == "allow" {
    true
  } else {
    if policy == "allow_once" {
      new_index == 1
    } else {
      false
    }
  }
  agent_record_native_tool_fallback(
    session_id,
    {
      iteration: turn_index + 1,
      accepted: accepted,
      policy: policy,
      fallback_index: new_index,
      tool_call_count: len(parsed_calls),
    },
  )
  if !accepted {
    agent_session_inject_feedback(
      session_id,
      "native_tool_contract",
      __native_fallback_feedback(policy, new_index),
    )
  }
  let resolved_calls = if accepted {
    parsed_calls
  } else {
    []
  }
  return {triggered: true, accepted: accepted, fallback_index: new_index, calls: resolved_calls}
}

fn __dispatch_tool_calls(session_id, tool_calls, turn_opts) {
  if len(tool_calls) == 0 {
    return {dispatch: nil, turn_opts: turn_opts}
  }
  agent_tool_search_emit_queries(session_id, tool_calls, turn_opts)
  let tools = turn_opts?.tools
  let dispatch_options = {
    session_id: session_id,
    tool_format: turn_opts.tool_format,
    policy: turn_opts?.policy,
    approval_policy: turn_opts?.approval_policy,
    command_policy: turn_opts?.command_policy,
    permissions: turn_opts?.permissions,
    _turn_iteration: turn_opts?._turn_iteration ?? 0,
    _tool_caller: turn_opts?._tool_caller,
  }
  let caller = turn_opts?._tool_caller
  let dispatch = if caller == nil {
    agent_dispatch_tool_batch(tool_calls, tools, dispatch_options)
  } else {
    __dispatch_tool_calls_with_middleware(tool_calls, tools, dispatch_options)
  }
  agent_session_record_tool_results(session_id, dispatch)
  return {
    dispatch: dispatch,
    turn_opts: agent_tool_search_record_results(session_id, tool_calls, dispatch, turn_opts),
  }
}

fn __dispatch_tool_calls_with_middleware(tool_calls, tools, options) {
  // Middleware-enabled path: dispatch sequentially so each call observes
  // the audit-log/consent/redaction order in source order. Authors that
  // want concurrency can wrap the inner call themselves.
  var results = []
  for call in tool_calls {
    results = results.push(__invoke_tool(call, tools, options))
  }
  return results
}

fn __sync_tool_search_state(opts, turn_opts) {
  if turn_opts?._tool_search_client == nil {
    return opts
  }
  return opts + {_tool_search_client: turn_opts._tool_search_client}
}

fn __dispatch_results_list(dispatch) {
  if dispatch == nil {
    return []
  }
  if type_of(dispatch) == "list" {
    return dispatch
  }
  return dispatch?.results ?? []
}

fn __tool_result_ok(result) {
  if result?.ok != nil {
    return result.ok ? true : false
  }
  if result?.success != nil {
    return result.success ? true : false
  }
  let status = result?.status ?? ""
  return status == "ok" || status == "success"
}

fn __tool_result_name(result) {
  return result?.tool_name ?? result?.name ?? ""
}

fn __tool_names_by_status(dispatch, want_ok) {
  let results = __dispatch_results_list(dispatch)
  var names = []
  for result in results {
    let name = __tool_result_name(result)
    if name != "" && __tool_result_ok(result) == want_ok {
      names = names.push(name)
    }
  }
  return names
}

fn __merge_tool_names(existing, additions) {
  var merged = existing ?? []
  let values = additions ?? []
  for name in values {
    if name != "" && !contains(merged, name) {
      merged = merged.push(name)
    }
  }
  return merged
}

fn __merge_hook_dict(base, patch, label) {
  if patch == nil {
    return base
  }
  if type_of(patch) != "dict" {
    throw "agent_loop: post_turn_callback `" + label + "` must be a dict"
  }
  return base + patch
}

fn __strip_internal_keys(patch) {
  if patch == nil {
    return patch
  }
  if type_of(patch) != "dict" {
    return patch
  }
  var clean = {}
  for key in patch.keys() {
    if !starts_with(key, "_") {
      clean = clean + {[key]: patch[key]}
    }
  }
  return clean
}

fn __apply_post_turn_options(opts, outcome) {
  var updated = opts
  let next_patch = __strip_internal_keys(outcome?.next_options)
  updated = __merge_hook_dict(updated, next_patch, "next_options")
  let llm_patch = outcome?.llm_options
  if llm_patch != nil {
    if type_of(llm_patch) != "dict" {
      throw "agent_loop: post_turn_callback `llm_options` must be a dict"
    }
    let base_llm_options = updated?.llm_options ?? {}
    updated = updated + {llm_options: base_llm_options + llm_patch}
  }
  return updated
}

fn __next_text_only_count(tool_count, consecutive_text_only) {
  if tool_count == 0 {
    return consecutive_text_only + 1
  }
  return 0
}

fn __requirement_satisfied(requirement, successful) {
  if type_of(requirement) == "list" {
    for candidate in requirement {
      if contains(successful, candidate) {
        return true
      }
    }
    return false
  }
  return contains(successful, requirement)
}

fn __requirement_label(requirement) {
  if type_of(requirement) == "list" {
    return join(requirement, "|")
  }
  return to_string(requirement)
}

fn __missing_required_successful_tools(result, opts) {
  let required = opts?.require_successful_tools
  if required == nil || len(required) == 0 {
    return []
  }
  let successful = result?.tools?.successful ?? []
  var missing = []
  for requirement in required {
    if !__requirement_satisfied(requirement, successful) {
      missing = missing.push(__requirement_label(requirement))
    }
  }
  return missing
}

fn __enforce_required_successful_tools(result, opts) {
  let missing = __missing_required_successful_tools(result, opts)
  if len(missing) == 0 {
    return result
  }
  return result
    + {
    status: "failed",
    final_status: "failed",
    stop_reason: "missing_required_tools",
    missing_required_tools: missing,
    error: "Required tools did not succeed: " + join(missing, ", "),
  }
}

fn __agent_loop_finalize_failed(session, iteration) {
  try {
    agent_session_finalize(
      session.session_id,
      {final_status: "failed", stop_reason: "error", iterations: iteration},
    )
  } catch (e) {
  }
}

fn __requirement_missing(requirement, successful) {
  if type_of(requirement) == "list" {
    for candidate in requirement {
      if contains(successful, candidate) {
        return false
      }
    }
    return true
  }
  return !contains(successful, requirement)
}

fn __required_tools_missing_list(opts, successful_tools_seen) {
  let required = opts?.require_successful_tools
  if required == nil || len(required) == 0 {
    return []
  }
  var missing = []
  for requirement in required {
    if __requirement_missing(requirement, successful_tools_seen) {
      let label = if type_of(requirement) == "list" {
        join(requirement, "|")
      } else {
        to_string(requirement)
      }
      missing = missing.push(label)
    }
  }
  return missing
}

fn __required_tools_feedback(missing) {
  return "Required tool(s) have not succeeded yet: "
    + join(missing, ", ")
    + ". Continue working and call the missing required tool(s) before finalizing."
}

fn __build_loop_state(args) {
  let current_limit = args.current_limit
  let iteration = args.iteration
  let remaining = if iteration >= current_limit {
    0
  } else {
    current_limit - iteration
  }
  let missing = args.missing_required_tools
  return {
    iteration: iteration,
    budget: {
      current_limit: current_limit,
      max: args.budget_max,
      remaining: remaining,
      extension_count: args.extensions_used,
    },
    turn: {
      tool_call_count: args.turn_tool_count,
      successful_tool_names: args.turn_successful,
      rejected_tool_names: args.turn_rejected,
      text_chars: args.turn_text_chars,
      native_fallback_used: args.turn_native_fallback_used,
    },
    session: {
      successful_tool_names: args.session_successful,
      rejected_tool_names: args.session_rejected,
      required_tools_satisfied: len(missing) == 0,
      required_tools_missing: missing,
    },
    completion: {
      proposed: args.completion_proposed,
      vetoed: args.completion_vetoed,
      verdict: args.completion_verdict,
      feedback: args.completion_feedback,
    },
    progress: {changed: args.progress_changed, summary: args.progress_summary},
  }
}

fn __default_loop_control(state) {
  if state.budget.remaining > 0 {
    return nil
  }
  if state.completion.vetoed {
    return {action: "extend", reason: "completion gate vetoed"}
  }
  if !state.session.required_tools_satisfied {
    return {action: "extend", reason: "required tools missing"}
  }
  if state.progress.changed && state.turn.tool_call_count > 0 {
    return {action: "extend", reason: "recent turn made progress"}
  }
  return nil
}

fn __interpret_loop_command(command) {
  if command == nil {
    return {action: "none", by: 0, until: 0, reason: "", status: ""}
  }
  if type_of(command) != "dict" {
    throw "agent_loop: loop_control must return nil or a dict; got " + type_of(command)
  }
  let action = command?.action ?? "none"
  if action != "none" && action != "extend" && action != "stop" {
    throw "agent_loop: loop_control action must be \"none\", \"extend\", or \"stop\"; got "
      + to_string(action)
  }
  let by = command?.by ?? 0
  if type_of(by) != "int" {
    throw "agent_loop: loop_control `by` must be an integer; got " + type_of(by)
  }
  let until = command?.until ?? 0
  if type_of(until) != "int" {
    throw "agent_loop: loop_control `until` must be an integer; got " + type_of(until)
  }
  return {action: action, by: by, until: until, reason: command?.reason ?? "", status: command?.status ?? ""}
}

fn __loop_control_invoke(opts, budget, state) {
  let policy = opts?.loop_control
  if policy != nil {
    return __interpret_loop_command(policy(state))
  }
  if budget?.mode == "adaptive" {
    return __interpret_loop_command(__default_loop_control(state))
  }
  return __interpret_loop_command(nil)
}

fn __apply_extension(command, budget, current_max) {
  let cap = budget?.max ?? current_max
  let target = if command.until > 0 {
    command.until
  } else {
    let by = if command.by > 0 {
      command.by
    } else {
      budget?.extend_by ?? 0
    }
    current_max + by
  }
  let bounded = if target > cap {
    cap
  } else {
    target
  }
  if bounded <= current_max {
    return {extended: false, new_limit: current_max, delta: 0}
  }
  return {extended: true, new_limit: bounded, delta: bounded - current_max}
}

fn __record_budget_decision(decisions, iteration, action, old_limit, new_limit, reason, status) {
  return decisions
    .push(
    {
      iteration: iteration,
      action: action,
      old_limit: old_limit,
      new_limit: new_limit,
      reason: reason,
      status: status,
    },
  )
}

fn __progress_summary_text(completion_vetoed, tool_count, visible_text) {
  if completion_vetoed {
    return "completion gate vetoed"
  }
  if tool_count > 0 {
    return "executed " + to_string(tool_count) + " tool call(s)"
  }
  if trim(visible_text) != "" {
    return "produced visible text"
  }
  return "no progress signal"
}

fn __snapshot_loop_state(args) {
  let verdict = args.verdict
  let completion_vetoed = verdict != nil && verdict?.vetoed
  let completion_verdict = if verdict == nil {
    nil
  } else {
    verdict?.verdict
  }
  let completion_feedback = if verdict == nil {
    nil
  } else {
    verdict?.feedback
  }
  let visible_text = args.visible_text
  let tool_count = args.tool_count
  let turn_successful = args.turn_successful
  let progress_changed = tool_count > 0 || len(turn_successful) > 0
    || trim(visible_text) != ""
  let progress_summary = __progress_summary_text(completion_vetoed, tool_count, visible_text)
  return __build_loop_state(
    {
      iteration: args.iteration,
      current_limit: args.current_limit,
      budget_max: args.budget_max,
      extensions_used: args.extensions_used,
      turn_tool_count: tool_count,
      turn_successful: turn_successful,
      turn_rejected: args.turn_rejected,
      turn_text_chars: len(visible_text),
      turn_native_fallback_used: args.turn_native_fallback_used,
      session_successful: args.session_successful,
      session_rejected: args.session_rejected,
      missing_required_tools: args.missing_required_tools,
      completion_proposed: args.completion_proposed,
      completion_vetoed: completion_vetoed,
      completion_verdict: completion_verdict,
      completion_feedback: completion_feedback,
      progress_changed: progress_changed,
      progress_summary: progress_summary,
    },
  )
}

fn __apply_loop_command(state) {
  let command = state.command
  let session_id = state.session_id
  let iteration = state.iteration
  let current_max = state.current_max
  let budget = state.budget
  if command.action == "stop" {
    let stop_status = if command.status != "" {
      command.status
    } else {
      "stopped"
    }
    let stop_reason = if command.reason != "" {
      command.reason
    } else {
      "loop_control"
    }
    let decisions = __record_budget_decision(
      state.decisions,
      iteration,
      "stop",
      current_max,
      current_max,
      command.reason,
      stop_status,
    )
    agent_emit_event(
      session_id,
      "loop_control_decision",
      {
        iteration: iteration,
        action: "stop",
        old_limit: current_max,
        new_limit: current_max,
        reason: command.reason,
        status: stop_status,
      },
    )
    return {
      stop: true,
      final_status: stop_status,
      stop_reason: stop_reason,
      current_max: current_max,
      extensions_used: state.extensions_used,
      decisions: decisions,
    }
  }
  if command.action == "extend" {
    let applied = __apply_extension(command, budget, current_max)
    if applied.extended {
      let old_limit = current_max
      let new_limit = applied.new_limit
      let extensions_used = state.extensions_used + 1
      let decisions = __record_budget_decision(
        state.decisions,
        iteration,
        "extend",
        old_limit,
        new_limit,
        command.reason,
        "",
      )
      agent_emit_event(
        session_id,
        "loop_control_decision",
        {
          iteration: iteration,
          action: "extend",
          old_limit: old_limit,
          new_limit: new_limit,
          reason: command.reason,
          status: "",
        },
      )
      return {
        stop: false,
        final_status: "",
        stop_reason: nil,
        current_max: new_limit,
        extensions_used: extensions_used,
        decisions: decisions,
      }
    }
  }
  return {
    stop: false,
    final_status: "",
    stop_reason: nil,
    current_max: current_max,
    extensions_used: state.extensions_used,
    decisions: state.decisions,
  }
}

@complexity(allow)
fn __agent_loop_run(message, session, initial_opts) {
  var opts = initial_opts
  var iteration = 0
  var session_finalized = false
  let run = try {
    opts = agent_mcp_bootstrap_if_needed(session, opts)
    var stop_reason = nil
    var final_status = ""
    var verify_attempts = 0
    var consecutive_text_only = 0
    var fallback_index = 0
    var successful_tools_seen = []
    var rejected_tools_seen = []
    let max_verify_attempts = opts?.max_verify_attempts ?? 20
    let budget = opts?.iteration_budget
      ?? {
      mode: "fixed",
      initial: opts?.max_iterations ?? 50,
      max: opts?.max_iterations ?? 50,
      extend_by: 0,
      expose_decisions: false,
    }
    var current_max = budget.initial
    var extensions_used = 0
    var budget_decisions = []
    var last_tool_count = 0
    while iteration < current_max {
      if agent_budget_pre_call_blocked(session, opts) {
        final_status = "budget_exhausted"
        break
      }
      let turn_index = iteration
      agent_emit_event(session.session_id, "turn_start", {iteration: turn_index + 1})
      var turn_opts = agent_skills_match(session, opts, turn_index)
      turn_opts = agent_tool_search_inject_if_needed(turn_opts)
      agent_autocompact_if_needed(session, turn_opts)
      let pending = agent_session_drain_feedback(session.session_id)
      for note in pending {
        agent_session_inject_feedback(session.session_id, note.kind, note.content)
      }
      let turn_system = agent_build_turn_system(session, turn_opts, turn_index)
      let turn_messages = agent_build_turn_messages(session, turn_opts, turn_index)
      let llm_overrides = turn_opts?.llm_options ?? {}
      let base_opts = turn_opts + llm_overrides
      let llm_opts = base_opts
        + {
        messages: turn_messages,
        session_id: session.session_id,
        tool_format: turn_opts.tool_format,
        _turn_iteration: turn_index + 1,
      }
      let call = __invoke_llm(message, turn_system, llm_opts)
      if !call.ok {
        final_status = call.status
        break
      }
      let llm_result = call.value
      iteration = iteration + 1
      let raw_text = llm_result?.text ?? ""
      let parsed = agent_parse_tool_calls(raw_text, turn_opts?.tools)
      let visible_text = __visible_text(parsed, raw_text)
      let normalized = llm_result + {text: visible_text, visible_text: visible_text}
      agent_session_record_assistant(session.session_id, normalized)
      let fallback_outcome = __detect_native_fallback(
        llm_result,
        parsed,
        turn_opts,
        fallback_index,
        session.session_id,
        turn_index,
      )
      fallback_index = fallback_outcome.fallback_index
      let tool_calls = if fallback_outcome.triggered {
        fallback_outcome.calls
      } else {
        __resolve_tool_calls(llm_result, parsed)
      }
      let dispatched = __dispatch_tool_calls(
        session.session_id,
        tool_calls,
        turn_opts + {_turn_iteration: turn_index + 1, _tool_caller: opts?._tool_caller},
      )
      let dispatch = dispatched.dispatch
      opts = __sync_tool_search_state(opts, dispatched.turn_opts)
      successful_tools_seen = __merge_tool_names(successful_tools_seen, __tool_names_by_status(dispatch, true))
      rejected_tools_seen = __merge_tool_names(rejected_tools_seen, __tool_names_by_status(dispatch, false))
      let totals = agent_session_record_usage(session.session_id, llm_result)
      let tool_count = len(tool_calls)
      agent_emit_event(
        session.session_id,
        "turn_end",
        {iteration: turn_index + 1, turn_info: {tool_count: tool_count, text: visible_text}},
      )
      if agent_budget_post_call_blocked(totals, turn_opts) {
        final_status = "budget_exhausted"
        break
      }
      consecutive_text_only = __next_text_only_count(tool_count, consecutive_text_only)
      last_tool_count = tool_count
      let turn_successful = __tool_names_by_status(dispatch, true)
      let turn_rejected = __tool_names_by_status(dispatch, false)
      let turn_max_nudges = turn_opts?.max_nudges ?? 8
      let turn_loop_until_done = turn_opts?.loop_until_done ?? false
      if turn_loop_until_done && tool_count == 0 && consecutive_text_only > turn_max_nudges {
        final_status = "stuck"
        stop_reason = "max_nudges"
        break
      }
      let post_turn_opts = turn_opts
        + {_session_successful_tools: successful_tools_seen, _session_rejected_tools: rejected_tools_seen}
      let outcome = agent_compute_post_turn(
        session,
        normalized + {raw_text: raw_text, parsed_done_marker: parsed?.done_marker ?? ""},
        dispatch,
        post_turn_opts,
        turn_index,
      )
      opts = __apply_post_turn_options(opts, outcome)
      var should_continue = outcome.kind == "continue"
      var verdict_record = nil
      if should_continue {
        if opts?.daemon && len(tool_calls) == 0 {
          agent_daemon_step(session, opts, iteration)
        }
      } else {
        if outcome.needs_verify {
          if verify_attempts >= max_verify_attempts {
            final_status = "verify_exhausted"
            stop_reason = outcome.stop_reason
            break
          }
          let verdict = agent_verify_or_continue(session, turn_opts, outcome.stop_reason, llm_result.text, turn_index)
          verdict_record = verdict
          if verdict.vetoed {
            verify_attempts = verify_attempts + 1
            should_continue = true
          }
        }
        let missing_now = __required_tools_missing_list(opts, successful_tools_seen)
        if !should_continue && len(missing_now) > 0 {
          agent_session_inject_feedback(
            session.session_id,
            "required_tools",
            __required_tools_feedback(missing_now),
          )
          should_continue = true
        }
        if !should_continue {
          stop_reason = outcome.stop_reason
          break
        }
      }
      let missing_required_for_loop = __required_tools_missing_list(opts, successful_tools_seen)
      let loop_state = __snapshot_loop_state(
        {
          iteration: iteration,
          current_limit: current_max,
          budget_max: budget.max,
          extensions_used: extensions_used,
          tool_count: tool_count,
          turn_successful: turn_successful,
          turn_rejected: turn_rejected,
          visible_text: visible_text,
          turn_native_fallback_used: fallback_outcome.triggered && fallback_outcome.accepted,
          session_successful: successful_tools_seen,
          session_rejected: rejected_tools_seen,
          missing_required_tools: missing_required_for_loop,
          completion_proposed: outcome.kind == "break",
          verdict: verdict_record,
        },
      )
      let command = __loop_control_invoke(opts, budget, loop_state)
      let applied = __apply_loop_command(
        {
          command: command,
          session_id: session.session_id,
          iteration: iteration,
          current_max: current_max,
          extensions_used: extensions_used,
          decisions: budget_decisions,
          budget: budget,
        },
      )
      current_max = applied.current_max
      extensions_used = applied.extensions_used
      budget_decisions = applied.decisions
      if applied.stop {
        final_status = applied.final_status
        stop_reason = applied.stop_reason
        break
      }
    }
    if final_status == "" && iteration >= current_max && stop_reason == nil {
      final_status = "budget_exhausted"
    }
    if opts?.daemon && final_status != "" {
      agent_daemon_snapshot(session, opts, final_status, iteration)
    }
    let result = agent_session_finalize(
      session.session_id,
      {final_status: final_status, stop_reason: stop_reason ?? "", iterations: iteration},
    )
    session_finalized = true
    let enforced = __enforce_required_successful_tools(result, opts)
    if budget.expose_decisions {
      enforced
        + {
        adaptive_budget: {
          mode: budget.mode,
          initial: budget.initial,
          max: budget.max,
          final_limit: current_max,
          extensions_used: extensions_used,
          decisions: budget_decisions,
        },
      }
    } else {
      enforced
    }
  }
  if is_err(run) {
    if !session_finalized {
      __agent_loop_finalize_failed(session, iteration)
    }
    throw unwrap_err(run)
  }
  return unwrap(run)
}

/** agent_loop. */
pub fn agent_loop(message, system_prompt = nil, options = nil) {
  var opts = agent_loop_options(options)
  if system_prompt != nil && system_prompt != "" {
    opts = opts + {system: system_prompt}
  }
  let session = agent_session_init(message, system_prompt, opts)
  if session?.done {
    return session.result
  }
  defer {
    try {
      __host_mcp_disconnect(session.session_id)
    } catch (e) {
    }
  }
  return __agent_loop_run(message, session, opts)
}