harn-modules 0.7.30

Cross-file module graph and import resolution utilities for Harn
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
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
import "std/collections"
import "std/context"
import "std/json"

/** workflow_model_spec. */
pub fn workflow_model_spec(config) {
  let target = config?.model ?? config?.model_alias ?? config?.model_tier ?? config?.tier
  if target == nil || target == "" {
    return nil
  }
  return llm_pick_model(target, filter_nil({provider: config?.provider}))
}

/** workflow_options. */
pub fn workflow_options(config) {
  let spec = workflow_model_spec(config)
  return filter_nil(
    {
    provider: config?.provider ?? spec?.provider,
    model: config?.model ?? spec?.id,
    max_tokens: config?.max_tokens,
    temperature: config?.temperature,
    top_p: config?.top_p,
    top_k: config?.top_k,
    stop: config?.stop,
    seed: config?.seed,
    frequency_penalty: config?.frequency_penalty,
    presence_penalty: config?.presence_penalty,
    response_format: config?.response_format,
    schema: config?.schema,
    thinking: config?.thinking,
    tools: config?.tools,
    tool_choice: config?.tool_choice,
    cache: config?.cache,
    timeout: config?.timeout,
    structural_experiment: config?.structural_experiment,
    persistent: config?.persistent,
    max_iterations: config?.max_iterations,
    max_nudges: config?.max_nudges,
    nudge: config?.nudge,
    turn_policy: config?.turn_policy,
    stop_after_successful_tools: config?.stop_after_successful_tools,
    require_successful_tools: config?.require_successful_tools,
    tool_retries: config?.tool_retries,
    tool_backoff_ms: config?.tool_backoff_ms,
    tool_format: config?.tool_format,
    native_tool_fallback: config?.native_tool_fallback,
    context_callback: config?.context_callback ?? config?.context_filter,
    transcript: config?.transcript,
  },
  )
}

/** stage_config. */
pub fn stage_config(flow, overrides, stage_name) {
  let combined = flow ?? {} + overrides ?? {}
  let stage = combined[stage_name] ?? {}
  let merged = combined + stage
  return filter_nil(merged + {_stage: stage_name})
}

/** stage_prompt. */
pub fn stage_prompt(task, config) {
  let ctx = config?.context
  return prompt_compose(
    task,
    ctx,
    filter_nil(
    {
    system: config?.system,
    task_label: config?.task_label,
    separator: config?.context_separator,
    max_chars: config?.context_max_chars,
    section_max_chars: config?.context_section_max_chars,
  },
  ),
  )
}

/** run_stage. */
pub fn run_stage(task, config) {
  let prompt = stage_prompt(task, config)
  let opts = workflow_options(config)
  if config?.mode == "completion" {
    return llm_completion(prompt?.prompt, config?.suffix, prompt?.system, opts)
  }
  if config?.mode == "llm" || config?.persistent == false {
    return llm_call(prompt?.prompt, prompt?.system, opts)
  }
  return agent_loop(prompt?.prompt, prompt?.system, opts + {persistent: config?.persistent ?? true})
}

/** verify_command. */
pub fn verify_command(config) {
  if config?.command == nil || config?.command == "" {
    return nil
  }
  let output = host_call("process.exec", {command: config?.command})
  var ok = output?.success == true
  if config?.expect_status != nil {
    ok = output?.status == config?.expect_status
  }
  if config?.expect_text != nil {
    ok = ok && contains(output?.combined ?? "", config?.expect_text)
  }
  return {kind: "command", ok: ok, output: output}
}

/** verify_result. */
pub fn verify_result(task, result, config) {
  if config == nil {
    return nil
  }
  if config?.command != nil {
    return verify_command(config)
  }
  if config?.assert_text != nil {
    return {kind: "text", ok: contains(result?.text ?? "", config?.assert_text), output: result?.text ?? ""}
  }
  return nil
}

/** repair_task. */
pub fn repair_task(original_task, verification, config) {
  if config?.prompt != nil {
    return config?.prompt
  }
  let summary = json_stringify(verification)
  let message = "The previous attempt did not pass verification.\n\nVerification:\n" + summary
    + "\n\nContinue working on the original task and fix the problem.\n\nOriginal task:\n"
    + original_task
  return message
}

/** workflow. */
pub fn workflow(config) {
  return workflow_graph(config ?? {})
}

fn __action_graph_parse_input(raw) {
  if type_of(raw) != "string" {
    return raw
  }
  let text = trim(raw)
  if text == "" {
    return []
  }
  if starts_with(text, "{") || starts_with(text, "[") {
    return safe_parse(text) ?? raw
  }
  return raw
}

fn __action_graph_non_empty_string(value) {
  if type_of(value) != "string" {
    return nil
  }
  let text = trim(value)
  if text == "" {
    return nil
  }
  return text
}

fn __action_graph_first_string(candidates) {
  for value in candidates {
    let text = __action_graph_non_empty_string(value)
    if text != nil {
      return text
    }
  }
  return nil
}

fn __action_graph_listify(value) {
  if value == nil {
    return []
  }
  if type_of(value) == "list" {
    return value
  }
  return [value]
}

fn __action_graph_tool_list(raw) {
  let value = raw?.tools ?? raw?.tool ?? raw?.allowed_tools ?? raw?.capabilities
  if value == nil {
    return []
  }
  var tools = []
  if type_of(value) == "string" {
    return [trim(value)]
  }
  for item in __action_graph_listify(value) {
    let tool_name = if type_of(item) == "string" {
      trim(item)
    } else {
      trim(item?.name ?? item?.tool ?? item?.id ?? "")
    }
    if tool_name != "" && !contains(tools, tool_name) {
      tools = tools + [tool_name]
    }
  }
  return tools
}

fn __action_graph_tool_class(tools, phase) {
  if phase == "verify" {
    return "verify"
  }
  if len(tools) == 0 {
    return if phase == "research" {
      "read"
    } else {
      "generic"
    }
  }
  var saw_read = false
  var saw_write = false
  var saw_exec = false
  for tool_name in tools {
    let name = lowercase(trim(tool_name))
    if contains(["read", "search", "fetch", "query", "list", "inspect", "grep"], name) {
      saw_read = true
      continue
    }
    if contains(["edit", "write", "create", "update", "delete", "apply_patch"], name) {
      saw_write = true
      continue
    }
    if contains(["run", "exec", "shell", "test"], name) {
      saw_exec = true
      continue
    }
    return "generic"
  }
  if saw_write && !saw_read && !saw_exec {
    return "write"
  }
  if saw_exec && !saw_read && !saw_write {
    return "exec"
  }
  if saw_read && !saw_write && !saw_exec {
    return "read"
  }
  return "mixed"
}

fn __action_graph_phase(raw, fallback_phase, tools, instruction) {
  let explicit = lowercase(
    trim(raw?.phase ?? raw?.kind ?? raw?.type ?? raw?.category ?? raw?.mode ?? fallback_phase ?? ""),
  )
  if contains(["research", "discover", "analyze", "analysis", "investigate"], explicit) {
    return "research"
  }
  if contains(["verify", "verification", "test", "check", "evaluate", "evaluator", "qa"], explicit) {
    return "verify"
  }
  if contains(["execute", "act", "implementation", "implement", "apply", "fix", "edit"], explicit) {
    return "execute"
  }
  let text = lowercase(trim(instruction ?? ""))
  if starts_with(text, "research ")
    || starts_with(text, "inspect ")
    || starts_with(text, "analyze ")
    || starts_with(text, "investigate ") {
    return "research"
  }
  if starts_with(text, "verify ")
    || starts_with(text, "test ")
    || starts_with(text, "check ")
    || starts_with(text, "evaluate ") {
    return "verify"
  }
  let tool_class = __action_graph_tool_class(tools, fallback_phase)
  if tool_class == "read" && explicit != "execute" {
    return "research"
  }
  return "execute"
}

fn __action_graph_dependency_refs(raw) {
  let source = raw?.depends_on ?? raw?.dependsOn ?? raw?.dependencies ?? raw?.needs ?? raw?.after
  if source == nil {
    return []
  }
  var refs = []
  for item in __action_graph_listify(source) {
    if item == nil {
      continue
    }
    if type_of(item) == "int" || type_of(item) == "float" {
      refs = refs + [format("{}", item)]
      continue
    }
    if type_of(item) == "dict" {
      let ref_text = __action_graph_first_string(
        [item?.id, item?.action_id, item?.title, item?.name, item?.label, item?.task],
      )
      if ref_text != nil {
        refs = refs + [ref_text]
      }
      continue
    }
    let text = __action_graph_non_empty_string(item)
    if text != nil {
      refs = refs + [text]
    }
  }
  return refs
}

fn __action_graph_collect_items(parsed) {
  if parsed == nil {
    return []
  }
  if type_of(parsed) == "list" {
    return parsed
  }
  if type_of(parsed) != "dict" {
    return [parsed]
  }
  for key in ["actions", "plan", "steps", "items", "tasks"] {
    let value = parsed[key]
    if type_of(value) == "list" {
      return value
    }
    if type_of(value) == "dict" {
      let nested = __action_graph_collect_items(value)
      if len(nested) > 0 {
        return nested
      }
    }
  }
  if type_of(parsed?.batches) == "list" {
    var flattened = []
    for batch in parsed.batches {
      let batch_phase = batch?.phase ?? batch?.kind ?? batch?.type
      let batch_items = batch?.actions ?? batch?.items ?? batch?.steps ?? []
      for item in __action_graph_listify(batch_items) {
        if type_of(item) == "dict" {
          flattened = flattened + [item + {phase: item?.phase ?? batch_phase}]
        } else {
          flattened = flattened + [item]
        }
      }
    }
    if len(flattened) > 0 {
      return flattened
    }
  }
  var grouped = []
  for phase in ["research", "execute", "verify"] {
    let value = parsed[phase]
    if value == nil {
      continue
    }
    for item in __action_graph_listify(value) {
      if type_of(item) == "dict" {
        grouped = grouped + [item + {phase: item?.phase ?? phase}]
      } else {
        grouped = grouped + [{title: item, instruction: item, phase: phase}]
      }
    }
  }
  if len(grouped) > 0 {
    return grouped
  }
  return [parsed]
}

fn __action_graph_aliases_for(canonical_id, source_id, title, index) {
  var aliases = {}
  let numeric = format("{}", index + 1)
  aliases[canonical_id] = canonical_id
  aliases[numeric] = canonical_id
  if source_id != nil {
    aliases[lowercase(trim(source_id))] = canonical_id
  }
  if title != nil {
    aliases[lowercase(trim(title))] = canonical_id
  }
  return aliases
}

fn __action_graph_merge_aliases(base, extra) {
  var merged = base ?? {}
  for entry in extra ?? {} {
    merged[entry.key] = entry.value
  }
  return merged
}

fn __action_graph_append_unique_strings(items, values) {
  var result = items ?? []
  for value in __action_graph_listify(values) {
    let text = __action_graph_non_empty_string(value)
    if text != nil && !contains(result, text) {
      result = result + [text]
    }
  }
  return result
}

fn __action_graph_batch_target_paths(batch) {
  var target_paths = []
  for action in batch.actions {
    target_paths = __action_graph_append_unique_strings(
      target_paths,
      action?.target_paths ?? action?.metadata?.target_paths ?? [],
    )
  }
  return target_paths
}

fn __action_graph_batch_verification(batch) {
  let target_paths = __action_graph_batch_target_paths(batch)
  if len(target_paths) == 0 {
    return nil
  }
  return {
    summary: "Current batch scope",
    required_paths: target_paths,
    notes: [
    "Only the current batch paths are in scope for this stage.",
    "Future verifier checks run in later stages; do not satisfy them early unless this batch explicitly names that work.",
  ],
  }
}

fn __action_graph_batch_prompt(batch) {
  var available_tools = []
  for action in batch.actions {
    for tool_name in action?.tools ?? [] {
      if !contains(available_tools, tool_name) {
        available_tools = available_tools + [tool_name]
      }
    }
  }
  let target_paths = __action_graph_batch_target_paths(batch)
  var create_direct_write = false
  var has_exact_command = false
  for action in batch.actions {
    let instruction = lowercase(trim(action?.instruction ?? action?.title ?? ""))
    if starts_with(instruction, "create ") || starts_with(instruction, "write new ") {
      create_direct_write = true
    }
    if __action_graph_non_empty_string(action?.command ?? action?.metadata?.command) != nil {
      has_exact_command = true
    }
  }
  var lines = [
    "You are executing a scheduled action-graph batch.",
    "",
    "Phase: " + batch.phase,
    "Tool class: " + batch.tool_class,
    "Available tools this batch: "
    + if len(available_tools) > 0 {
    join(available_tools, ", ")
  } else {
    "(none declared)"
  },
    "Target paths this batch: "
    + if len(target_paths) > 0 {
    join(target_paths, ", ")
  } else {
    "(none declared)"
  },
    "",
    "Authority order: obey the current batch action list and the injected verifier contract before any broader workflow context.",
    "Complete every action in this batch before finishing.",
    "Scope boundary: perform only the action ids listed below for this batch.",
    "If you write, keep writes inside the current batch target paths unless a tool result proves a listed path needs to move.",
    "The runtime rejects mutating path-based tool calls outside the current batch target paths.",
    "You may read supporting workspace files outside the current batch target paths only when that context is immediately necessary to complete this batch.",
    "Do not inspect sibling files outside the current batch target paths just to prepare future batches.",
    "Treat broader plan/context artifacts as background evidence only, not as permission to continue with later actions.",
    "If workflow context mentions future batches or terminal verification, ignore that work until this batch explicitly names it.",
    "Do not preempt later batches, edit their target files early, or run final verification unless this batch explicitly lists those steps.",
    "If a tool result rejects another path or scope, treat that as evidence you attempted future-batch work; return to the current batch instead of retrying adjacent files.",
    "Do not issue a second write to the same target path unless a subsequent read or run result exposed a concrete problem with the first write.",
    "After a successful write satisfies the current batch target paths, finish immediately unless this batch explicitly includes a verification command.",
    "Once the listed actions are satisfied, stop immediately instead of continuing with adjacent work from the overall plan.",
    "Actions:",
  ]
  if create_direct_write {
    lines = lines
      + [
      "If an action explicitly says to create a target file, write that file directly instead of reading unrelated files first.",
      "After a successful write creates the target file, do not reread or rewrite that file unless a tool result exposes a concrete problem or this batch explicitly requires validation.",
    ]
  }
  if has_exact_command {
    lines = lines
      + [
      "If an action includes an exact command string, run that exact command before trying alternatives or discovery commands.",
    ]
    if batch.phase == "verify" && len(available_tools) == 1 && available_tools[0] == "run" {
      lines = lines
        + [
        "This verify batch is observation-only.",
        "After the exact command returns, report whether it passed or failed and stop.",
        "Do not attempt repairs, shell edits, fallback commands, or extra discovery in this batch.",
      ]
    }
  }
  for action in batch.actions {
    let title = action?.title ?? action?.instruction ?? action?.id
    let exact_command = __action_graph_non_empty_string(action?.command ?? action?.metadata?.command)
    let detail = if exact_command != nil {
      action?.instruction ?? title + " Run exactly `${exact_command}`."
    } else {
      action?.instruction ?? title
    }
    lines = lines + ["- [" + action.id + "] " + title + ": " + detail]
  }
  return join(lines, "\n")
}

fn __action_graph_batch(batch_index, phase, tool_class, actions) {
  var action_ids = []
  var titles = []
  var depends_on = []
  for action in actions {
    action_ids = action_ids + [action.id]
    titles = titles + [action?.title ?? action?.instruction ?? action.id]
    for dep in action?.depends_on ?? [] {
      if !contains(action_ids, dep) && !contains(depends_on, dep) {
        depends_on = depends_on + [dep]
      }
    }
  }
  let batch = {
    _type: "action_batch",
    id: format("batch-{}", batch_index + 1),
    phase: phase,
    tool_class: tool_class,
    actions: actions,
    action_ids: action_ids,
    action_titles: titles,
    depends_on: depends_on,
    count: len(actions),
    prompt: nil,
  }
  return batch + {prompt: __action_graph_batch_prompt(batch)}
}

fn __action_graph_batch_output_kind(phase) {
  if phase == "research" {
    return "summary"
  }
  if phase == "verify" {
    return "verification_result"
  }
  return "summary"
}

fn __action_graph_output_kind(template, default_output) {
  let outputs = template?.output_contract?.output_kinds ?? []
  if len(outputs) > 0 {
    return outputs[0]
  }
  return default_output
}

fn __action_graph_stage_template(config, phase) {
  if phase == "research" {
    return config?.research ?? config?.stage ?? {}
  }
  if phase == "verify" {
    return config?.verify_phase ?? config?.execute ?? config?.stage ?? {}
  }
  return config?.execute ?? config?.stage ?? {}
}

fn __action_graph_terminal_template(config, phase) {
  if phase == "verify" {
    return config?.verify ?? {}
  }
  if phase == "evaluate" {
    return config?.evaluate ?? {}
  }
  return {}
}

fn __action_graph_terminal_kind(template, phase) {
  if template?.kind != nil {
    return template.kind
  }
  if phase == "verify" || template?.command != nil || template?.assert_text != nil {
    return "verify"
  }
  return "stage"
}

fn __action_graph_node_system(base_system, prompt) {
  let prefix = __action_graph_non_empty_string(base_system)
  if prefix == nil {
    return prompt
  }
  return prefix + "\n\n" + prompt
}

fn __action_graph_batch_tools(batch, template) {
  let registry = template?.tools
  if registry == nil {
    return nil
  }
  var selected = []
  for action in batch.actions {
    for tool_name in action?.tools ?? [] {
      if !contains(selected, tool_name) {
        selected = selected + [tool_name]
      }
    }
  }
  if contains(selected, "write") && !contains(selected, "read") {
    selected = ["read"] + selected
  }
  if len(selected) == 0 {
    return registry
  }
  return tool_select(registry, selected)
}

fn __action_graph_batch_context_kinds(batch, template) {
  let explicit = template?.context_policy?.include_kinds ?? []
  if len(explicit) > 0 {
    return explicit
  }
  if batch.phase == "research" {
    return ["summary"]
  }
  if batch.phase == "verify" {
    return ["summary", "artifact", "verification_result"]
  }
  return ["summary"]
}

fn __action_graph_batch_node(batch, template) {
  let local_verification = __action_graph_batch_verification(batch)
  let model_policy = template?.model_policy ?? {}
  let output_kind = __action_graph_output_kind(template, __action_graph_batch_output_kind(batch.phase))
  let output_contract = template?.output_contract ?? {} + {output_kinds: [output_kind]}
  let target_paths = __action_graph_batch_target_paths(batch)
  let context_policy = template?.context_policy ?? {}
    + {include_kinds: __action_graph_batch_context_kinds(batch, template)}
  var metadata = template?.metadata ?? {}
  if local_verification != nil {
    metadata = metadata + {workflow_verification_scope: "local_only"}
  }
  let approval_policy = if len(target_paths) > 0 {
    template?.approval_policy ?? {} + {write_path_allowlist: target_paths}
  } else {
    template?.approval_policy ?? {}
  }
  return template ?? {}
    + {
    kind: "stage",
    task_label: format("{} batch {}", uppercase(batch.phase), batch.id),
    system: __action_graph_node_system(template?.system, batch.prompt),
    tools: __action_graph_batch_tools(batch, template),
    model_policy: model_policy,
    approval_policy: approval_policy,
    verify: if local_verification != nil {
    local_verification
  } else {
    template?.verify
  },
    output_contract: output_contract,
    context_policy: context_policy,
    metadata: metadata,
  }
}

fn __action_graph_terminal_prompt(plan, phase) {
  if phase == "verify" {
    return "Review the action-graph plan and produced artifacts, then verify whether the requested work is complete."
  }
  return "Evaluate the completed action-graph run and summarize whether the plan satisfied the task."
}

fn __action_graph_terminal_node(plan, phase, template) {
  let default_output = if phase == "verify" {
    "verification_result"
  } else {
    "summary"
  }
  let node_kind = __action_graph_terminal_kind(template, phase)
  var verify_payload = nil
  if node_kind == "verify" {
    verify_payload = template?.verify \
      ?? filter_nil(
      {
      command: template?.command,
      assert_text: template?.assert_text,
      expect_status: template?.expect_status,
    },
    )
  }
  let output_contract = template?.output_contract ?? {}
    + {output_kinds: [__action_graph_output_kind(template, default_output)]}
  let context_policy = template?.context_policy ?? {}
    + {
    include_kinds: template?.context_policy?.include_kinds ?? ["plan", "summary", "artifact", "verification_result"],
  }
  return template ?? {}
    + filter_nil(
    {
    kind: node_kind,
    task_label: uppercase(phase),
    system: __action_graph_node_system(template?.system, __action_graph_terminal_prompt(plan, phase)),
    verify: verify_payload,
    output_contract: output_contract,
    context_policy: context_policy,
  },
  )
}

/**
 * Normalize planner output into a canonical action-graph envelope.
 * action_graph.
 */
pub fn action_graph(raw, options = nil) {
  let parsed = __action_graph_parse_input(raw)
  let items = __action_graph_collect_items(parsed)
  var staged = []
  var aliases = {}
  for (index, item) in iter(items).enumerate() {
    let dict = if type_of(item) == "dict" {
      item
    } else {
      {title: item, instruction: item}
    }
    let tools = __action_graph_tool_list(dict)
    let title = __action_graph_first_string(
      [dict?.title, dict?.name, dict?.label, dict?.action, dict?.task, dict?.instruction, dict?.summary],
    ) \
      ?? format("Action {}", index + 1)
    let instruction = __action_graph_first_string(
      [
      dict?.instruction,
      dict?.prompt,
      dict?.task,
      dict?.action,
      dict?.goal,
      dict?.description,
      dict?.text,
      title,
    ],
    ) \
      ?? title
    let source_id = __action_graph_first_string([dict?.id, dict?.action_id, dict?.name, dict?.label])
    let phase = __action_graph_phase(dict, dict?.phase ?? options?.phase, tools, instruction)
    let canonical_id = format("action-{}", index + 1)
    staged = staged
      + [
      {
      _type: "action_item",
      id: canonical_id,
      title: title,
      instruction: instruction,
      phase: phase,
      depends_on: __action_graph_dependency_refs(dict),
      tools: tools,
      tool_class: dict?.tool_class ?? __action_graph_tool_class(tools, phase),
      metadata: filter_nil({source_id: source_id, source_phase: dict?.phase ?? dict?.kind ?? dict?.type}),
    },
    ]
    aliases = __action_graph_merge_aliases(
      aliases,
      __action_graph_aliases_for(canonical_id, source_id, title, index),
    )
  }
  var normalized = []
  var research_ids = []
  var execute_ids = []
  for action in staged {
    var resolved = []
    for ref in action.depends_on {
      let lookup = lowercase(trim(ref))
      let target = aliases[lookup] ?? aliases[ref]
      if target != nil && target != action.id && !contains(resolved, target) {
        resolved = resolved + [target]
      }
    }
    if len(resolved) == 0 && action.phase == "execute" && len(research_ids) > 0 {
      resolved = research_ids
    }
    if len(resolved) == 0 && action.phase == "verify" {
      resolved = if len(execute_ids) > 0 {
        execute_ids
      } else {
        research_ids
      }
    }
    let repaired = action + {depends_on: resolved}
    normalized = normalized + [repaired]
    if repaired.phase == "research" {
      research_ids = research_ids + [repaired.id]
    }
    if repaired.phase == "execute" {
      execute_ids = execute_ids + [repaired.id]
    }
  }
  return {
    _type: "action_graph",
    task: options?.task ?? parsed?.task ?? parsed?.goal,
    summary: options?.summary ?? parsed?.summary ?? parsed?.rationale,
    actions: normalized,
    metadata: filter_nil(
    {
    source: if type_of(parsed) == "dict" {
    parsed?._type ?? "planner_output"
  } else {
    "planner_output"
  },
  },
  ),
  }
}

/**
 * Compute dependency-ready execution batches from a canonical action graph.
 * action_graph_batches.
 */
pub fn action_graph_batches(graph, completed = nil) {
  let plan = if graph?._type == "action_graph" {
    graph
  } else {
    action_graph(graph, nil)
  }
  let done = __action_graph_listify(completed)
  var remaining = []
  for action in plan.actions {
    if !contains(done, action.id) {
      remaining = remaining + [action]
    }
  }
  var finished = done
  var batches = []
  var loop_count = 0
  while len(remaining) > 0 && loop_count < len(plan.actions) + 4 {
    var ready = []
    var blocked = []
    for action in remaining {
      var can_run = true
      for dep in action.depends_on {
        if !contains(finished, dep) {
          can_run = false
        }
      }
      if can_run {
        ready = ready + [action]
      } else {
        blocked = blocked + [action]
      }
    }
    if len(ready) == 0 {
      ready = [remaining[0]]
      blocked = remaining[1:]
    }
    let phase = if ready[0]?.phase == "research" || ready[0]?.phase == "execute" || ready[0]?.phase == "verify" {
      ready[0].phase
    } else {
      "execute"
    }
    var current_phase = []
    var next_remaining = blocked
    for action in ready {
      if action.phase == phase {
        current_phase = current_phase + [action]
      } else {
        next_remaining = next_remaining + [action]
      }
    }
    var grouped = {}
    var order = []
    for action in current_phase {
      let key = action.tool_class ?? "generic"
      if grouped[key] == nil {
        grouped[key] = []
        order = order + [key]
      }
      grouped[key] = grouped[key] + [action]
    }
    for key in order {
      let batch = __action_graph_batch(len(batches), phase, key, grouped[key])
      batches = batches + [batch]
      for action in grouped[key] {
        finished = finished + [action.id]
      }
    }
    remaining = next_remaining
    loop_count = loop_count + 1
  }
  return batches
}

/**
 * Render a canonical action graph into readable markdown.
 * action_graph_render.
 */
pub fn action_graph_render(graph) {
  let plan = if graph?._type == "action_graph" {
    graph
  } else {
    action_graph(graph, nil)
  }
  var lines = ["# Action Graph"]
  if plan?.task != nil {
    lines = lines + ["", "Task: " + plan.task]
  }
  if plan?.summary != nil {
    lines = lines + ["", "Summary: " + plan.summary]
  }
  for phase in ["research", "execute", "verify"] {
    var phase_lines = []
    for action in plan.actions {
      if action.phase != phase {
        continue
      }
      let deps = if len(action.depends_on) > 0 {
        " (depends on: " + join(action.depends_on, ", ") + ")"
      } else {
        ""
      }
      let label = action?.title ?? action?.instruction ?? action.id
      phase_lines = phase_lines
        + ["- [" + action.id + "] " + label + " [" + action.tool_class + "]" + deps]
    }
    if len(phase_lines) > 0 {
      lines = lines + ["", "## " + uppercase(phase)]
      lines = lines + phase_lines
    }
  }
  return join(lines, "\n")
}

/**
 * Convert a canonical action graph into a typed workflow graph.
 * action_graph_flow.
 */
pub fn action_graph_flow(graph, config = nil) {
  let plan = if graph?._type == "action_graph" {
    graph
  } else {
    action_graph(graph, config)
  }
  let batches = action_graph_batches(plan)
  var nodes = {}
  var edges = []
  var entry = ""
  var previous = ""
  for batch in batches {
    let node_id = replace(batch.id, "-", "_")
    let template = __action_graph_stage_template(config, batch.phase)
    nodes[node_id] = __action_graph_batch_node(batch, template)
    if entry == "" {
      entry = node_id
    }
    if previous != "" {
      edges = edges + [{from: previous, to: node_id}]
    }
    previous = node_id
  }
  for phase in ["verify", "evaluate"] {
    let template = __action_graph_terminal_template(config, phase)
    if len(keys(template ?? {})) == 0 {
      continue
    }
    let node_id = if phase == "verify" {
      "final_verify"
    } else {
      "final_evaluate"
    }
    nodes[node_id] = __action_graph_terminal_node(plan, phase, template)
    if entry == "" {
      entry = node_id
    }
    if previous != "" {
      edges = edges + [{from: previous, to: node_id}]
    }
    previous = node_id
  }
  return workflow_graph(
    {
    name: config?.name ?? plan?.task ?? "action_graph",
    entry: if entry == "" {
    "final_verify"
  } else {
    entry
  },
    nodes: nodes,
    edges: edges,
  },
  )
}

/**
 * Execute an action graph through the shared workflow runtime.
 * action_graph_run.
 */
pub fn action_graph_run(task, graph, config = nil, overrides = nil) {
  let plan = if graph?._type == "action_graph" {
    graph
  } else {
    action_graph(graph, config ?? {} + {task: task})
  }
  let flow = action_graph_flow(plan, config)
  let plan_artifact = artifact(
    {
    kind: "plan",
    title: config?.plan_title ?? "Action graph",
    text: action_graph_render(plan),
    data: plan,
    source: "action_graph",
    freshness: "fresh",
    relevance: 1.0,
  },
  )
  let artifacts = overrides?.artifacts ?? [] + [plan_artifact]
  let runtime = workflow_execute(task, flow, artifacts, overrides)
  return {
    _type: "action_graph_run",
    task: task,
    status: runtime?.status,
    plan: plan,
    batches: action_graph_batches(plan),
    flow: flow,
    runtime: runtime,
    run: runtime?.run,
    artifacts: runtime?.artifacts ?? [],
    transcript: runtime?.transcript,
    visible_text: workflow_result_text(runtime),
  }
}

/** task_run. */
pub fn task_run(task, flow, overrides) {
  let graph = workflow_graph(flow ?? {})
  let runtime = workflow_execute(task, graph, overrides?.artifacts ?? [], overrides)
  let stages = runtime?.run?.stages ?? []
  var act = nil
  var verify = nil
  var repair = nil
  let artifacts = runtime?.artifacts ?? []
  let latest_text = if len(artifacts) > 0 {
    artifacts[len(artifacts) - 1]?.text
  } else {
    act?.visible_text
  }
  for stage in stages {
    if act == nil {
      act = stage
      continue
    }
    if verify == nil && (stage?.node_id == "verify" || stage?.kind == "verify") {
      verify = stage
      continue
    }
    if repair == nil && stage?.node_id == "repair" {
      repair = stage
    }
  }
  let status = if verify?.status == "failed" || repair != nil {
    "needs_attention"
  } else {
    runtime?.status ?? "completed"
  }
  return {
    task: task,
    workflow: graph?.name ?? "workflow",
    status: status,
    text: latest_text,
    result: runtime,
    act: act,
    repair: repair,
    verification: verify,
    final_verification: verify,
    transcript: runtime?.transcript,
    run: runtime?.run,
    artifacts: artifacts,
  }
}

/** workflow_continue. */
pub fn workflow_continue(prev, task, flow, overrides) {
  let transcript = prev?.transcript ?? prev
  let merged = overrides ?? {} + {transcript: transcript}
  return task_run(task, flow, merged)
}

/** workflow_result_text. */
pub fn workflow_result_text(result) {
  let raw = if type_of(result) == "string" {
    result
  } else {
    result?.visible_text ?? result?.text ?? result?.result?.text ?? ""
  }
  if raw == nil || raw == "" {
    return ""
  }
  if type_of(raw) == "string" {
    return raw
  }
  return json_stringify(raw)
}

/** workflow_result_transcript. */
pub fn workflow_result_transcript(task, workflow_name, result, metadata) {
  let existing = result?.run?.transcript ?? result?.transcript ?? result?.result?.transcript
  if existing {
    return existing
  }
  var built = transcript(metadata ?? {} + {workflow: workflow_name})
  if task {
    built = add_user(built, [{type: "text", text: task, visibility: "public"}])
  }
  let text = workflow_result_text(result)
  if text {
    built = add_assistant(built, [{type: "output_text", text: text, visibility: "public"}])
  }
  return built
}

/** workflow_result_run. */
pub fn workflow_result_run(task, workflow_name, result, artifacts, options) {
  let base_run = if result?._type == "run_record" {
    result
  } else {
    result?.run ?? {}
  }
  let text = workflow_result_text(result)
  let metadata = base_run?.metadata ?? {} + options?.metadata ?? {}
  let transcript = if result?.run?.transcript ?? result?.transcript ?? result?.result?.transcript {
    result?.run?.transcript ?? result?.transcript ?? result?.result?.transcript
  } else {
    var built = transcript(metadata ?? {} + {workflow: workflow_name})
    if task {
      built = add_user(built, [{type: "text", text: task, visibility: "public"}])
    }
    if text {
      built = add_assistant(built, [{type: "output_text", text: text, visibility: "public"}])
    }
    built
  }
  var run_artifacts = base_run?.artifacts ?? result?.artifacts ?? artifacts ?? []
  var has_visible_result_artifact = false
  if text {
    for item in run_artifacts {
      if item?.text == text {
        has_visible_result_artifact = true
      }
    }
    if !has_visible_result_artifact {
      run_artifacts = run_artifacts
        + [
        artifact(
        {
        kind: "summary",
        title: "Visible result",
        text: text,
        source: workflow_name,
        freshness: "fresh",
        relevance: 1.0,
        metadata: {visibility: "public"},
      },
      ),
      ]
    }
  }
  return run_record(
    base_run
    + {
    workflow_id: base_run?.workflow_id ?? workflow_name,
    workflow_name: base_run?.workflow_name ?? workflow_name,
    task: task,
    status: options?.status ?? base_run?.status ?? "completed",
    transcript: transcript,
    artifacts: run_artifacts,
    trace_spans: trace_spans(),
    metadata: metadata,
  },
  )
}

/** workflow_result_persist. */
pub fn workflow_result_persist(task, workflow_name, result, artifacts, options) {
  let base_run = if result?._type == "run_record" {
    result
  } else {
    result?.run ?? {}
  }
  let text = workflow_result_text(result)
  let metadata = base_run?.metadata ?? {} + options?.metadata ?? {}
  let transcript = if result?.run?.transcript ?? result?.transcript ?? result?.result?.transcript {
    result?.run?.transcript ?? result?.transcript ?? result?.result?.transcript
  } else {
    var built = transcript(metadata ?? {} + {workflow: workflow_name})
    if task {
      built = add_user(built, [{type: "text", text: task, visibility: "public"}])
    }
    if text {
      built = add_assistant(built, [{type: "output_text", text: text, visibility: "public"}])
    }
    built
  }
  var run_artifacts = base_run?.artifacts ?? result?.artifacts ?? artifacts ?? []
  var has_visible_result_artifact = false
  if text {
    for item in run_artifacts {
      if item?.text == text {
        has_visible_result_artifact = true
      }
    }
    if !has_visible_result_artifact {
      run_artifacts = run_artifacts
        + [
        artifact(
        {
        kind: "summary",
        title: "Visible result",
        text: text,
        source: workflow_name,
        freshness: "fresh",
        relevance: 1.0,
        metadata: {visibility: "public"},
      },
      ),
      ]
    }
  }
  let run = run_record(
    base_run
    + {
    workflow_id: base_run?.workflow_id ?? workflow_name,
    workflow_name: base_run?.workflow_name ?? workflow_name,
    task: task,
    status: options?.status ?? base_run?.status ?? "completed",
    transcript: transcript,
    artifacts: run_artifacts,
    trace_spans: trace_spans(),
    metadata: metadata,
  },
  )
  let persisted = run_record_save(run, options?.path)
  return {
    path: persisted?.path,
    persisted_path: persisted?.path,
    run: persisted?.run,
    status: persisted?.run?.status ?? "completed",
    transcript: persisted?.run?.transcript,
    artifacts: persisted?.run?.artifacts ?? [],
    visible_text: text,
  }
}

/** workflow_session. */
pub fn workflow_session(prev) {
  let transcript = prev?.transcript ?? prev?.result?.transcript ?? prev
  let run = prev?.run ?? prev?.result?.run
  let persisted_path = prev?.persisted_path ?? prev?.path ?? run?.persisted_path
  let workflow_id = prev?.workflow_id ?? run?.workflow_id ?? prev?.workflow ?? run?.workflow_name
  return {
    _type: "workflow_session",
    workflow_id: workflow_id,
    transcript: transcript,
    run: run,
    persisted_path: persisted_path,
    status: prev?.status ?? run?.status ?? transcript?.state ?? "active",
    workflow: prev?.workflow ?? run?.workflow?.name,
    task: prev?.task ?? run?.task,
    summary: transcript_summary(transcript),
    transcript_state: transcript?.state ?? "active",
    message_count: len(transcript_messages(transcript)),
    event_count: len(transcript_events(transcript)),
    asset_count: len(transcript_assets(transcript)),
    usage: run?.usage ?? prev?.usage,
  }
}

/** workflow_session_new. */
pub fn workflow_session_new(metadata) {
  let raw = metadata ?? {}
  let workflow_id = raw?.workflow_id ?? raw?.workflow
  return workflow_session(
    {status: "active", workflow_id: workflow_id, transcript: transcript(raw)},
  )
}

/** workflow_session_restore. */
pub fn workflow_session_restore(source) {
  let run = if type_of(source) == "string" {
    run_record_load(source)
  } else {
    source?.run ?? source
  }
  return workflow_session(
    {
    status: run?.status ?? "restored",
    workflow_id: run?.workflow_id,
    run: run,
    persisted_path: run?.persisted_path,
    transcript: run?.transcript ?? transcript(),
  },
  )
}

/** workflow_session_fork. */
pub fn workflow_session_fork(prev) {
  let session = workflow_session(prev)
  return workflow_session(session + {transcript: transcript_fork(session?.transcript)})
}

/** workflow_session_archive. */
pub fn workflow_session_archive(prev) {
  let session = workflow_session(prev)
  return workflow_session(
    session + {status: "archived", transcript: transcript_archive(session?.transcript)},
  )
}

/** workflow_session_resume. */
pub fn workflow_session_resume(prev) {
  let session = workflow_session(prev)
  return workflow_session(session + {status: "active", transcript: transcript_resume(session?.transcript)})
}

/** workflow_compact. */
pub fn workflow_compact(prev, options) {
  let transcript = prev?.transcript ?? prev
  return transcript_summarize(transcript, options)
}

/** workflow_session_compact. */
pub fn workflow_session_compact(prev, options) {
  let session = workflow_session(prev)
  return workflow_session(session + {transcript: transcript_summarize(session?.transcript, options)})
}

/** workflow_reset. */
pub fn workflow_reset(prev, carry_summary) {
  let transcript = prev?.transcript ?? prev
  if carry_summary && transcript_summary(transcript) {
    return transcript_compact(transcript, {keep_last: 0})
  }
  return transcript()
}

/** workflow_session_reset. */
pub fn workflow_session_reset(prev, carry_summary) {
  let session = workflow_session(prev)
  return workflow_session(
    {
    workflow_id: session?.workflow_id,
    workflow: session?.workflow,
    status: "active",
    transcript: workflow_reset(session?.transcript, carry_summary),
  },
  )
}

/** continue_as_new. */
pub fn continue_as_new(prev, options = nil) {
  let session = workflow_session(prev)
  let carry_summary = options?.carry_summary ?? true
  let _state = workflow.continue_as_new(session)
  return workflow_session(
    {
    workflow_id: session?.workflow_id,
    workflow: session?.workflow,
    status: "active",
    persisted_path: session?.persisted_path,
    run: session?.run,
    transcript: workflow_reset(session?.transcript, carry_summary),
  },
  )
}

/** workflow_session_persist. */
pub fn workflow_session_persist(prev, path) {
  let session = workflow_session(prev)
  if session?.run == nil {
    return session
  }
  let persisted = run_record_save(session?.run, path)
  return workflow_session(session + {run: persisted?.run, persisted_path: persisted?.path})
}

/** worker_request. */
pub fn worker_request(worker) {
  return worker?.request ?? worker?.worker?.request ?? worker?.data?.request
}

/** worker_result. */
pub fn worker_result(worker) {
  return worker?.result ?? worker?.data?.payload ?? worker?.worker?.result
}

/** worker_provenance. */
pub fn worker_provenance(worker) {
  return worker?.provenance ?? worker?.worker?.provenance ?? worker?.data?.provenance
}

/** worker_research_questions. */
pub fn worker_research_questions(worker) {
  return worker_request(worker)?.research_questions ?? []
}

/** worker_action_items. */
pub fn worker_action_items(worker) {
  return worker_request(worker)?.action_items ?? []
}

/** worker_workflow_stages. */
pub fn worker_workflow_stages(worker) {
  return worker_request(worker)?.workflow_stages ?? []
}

/** worker_verification_steps. */
pub fn worker_verification_steps(worker) {
  return worker_request(worker)?.verification_steps ?? []
}