forked from openai/symphony
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.ex
More file actions
998 lines (847 loc) · 30.5 KB
/
config.ex
File metadata and controls
998 lines (847 loc) · 30.5 KB
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
defmodule SymphonyElixir.Config do
@moduledoc """
Runtime configuration loaded from `WORKFLOW.md`.
"""
alias NimbleOptions
alias SymphonyElixir.Workflow
@default_active_states ["Todo", "In Progress"]
@default_terminal_states ["Closed", "Cancelled", "Canceled", "Duplicate", "Done"]
@default_linear_endpoint "https://api.linear.app/graphql"
@default_prompt_template """
You are working on a Linear issue.
Identifier: {{ issue.identifier }}
Title: {{ issue.title }}
Body:
{% if issue.description %}
{{ issue.description }}
{% else %}
No description provided.
{% endif %}
"""
@default_poll_interval_ms 30_000
@default_workspace_root Path.join(System.tmp_dir!(), "symphony_workspaces")
@default_hook_timeout_ms 60_000
@default_max_concurrent_agents 10
@default_agent_max_turns 20
@default_max_retry_backoff_ms 300_000
@default_codex_command "codex app-server"
@default_codex_turn_timeout_ms 3_600_000
@default_codex_read_timeout_ms 5_000
@default_codex_stall_timeout_ms 300_000
@default_codex_approval_policy %{
"reject" => %{
"sandbox_approval" => true,
"rules" => true,
"mcp_elicitations" => true
}
}
@default_codex_thread_sandbox "workspace-write"
@default_observability_enabled true
@default_observability_refresh_ms 1_000
@default_observability_render_interval_ms 16
@default_server_host "127.0.0.1"
@workflow_options_schema NimbleOptions.new!(
tracker: [
type: :map,
default: %{},
keys: [
kind: [type: {:or, [:string, nil]}, default: nil],
endpoint: [type: :string, default: @default_linear_endpoint],
api_key: [type: {:or, [:string, nil]}, default: nil],
project_slug: [type: {:or, [:string, nil]}, default: nil],
team_key: [type: {:or, [:string, nil]}, default: nil],
assignee: [type: {:or, [:string, nil]}, default: nil],
labels: [type: {:list, :string}, default: []],
dispatch_states: [type: {:list, :string}, default: []],
active_states: [
type: {:list, :string},
default: @default_active_states
],
terminal_states: [
type: {:list, :string},
default: @default_terminal_states
]
]
],
polling: [
type: :map,
default: %{},
keys: [
interval_ms: [type: :integer, default: @default_poll_interval_ms]
]
],
workspace: [
type: :map,
default: %{},
keys: [
root: [type: {:or, [:string, nil]}, default: @default_workspace_root]
]
],
agent: [
type: :map,
default: %{},
keys: [
max_concurrent_agents: [
type: :integer,
default: @default_max_concurrent_agents
],
max_turns: [
type: :pos_integer,
default: @default_agent_max_turns
],
max_retry_backoff_ms: [
type: :pos_integer,
default: @default_max_retry_backoff_ms
],
max_concurrent_agents_by_state: [
type: {:map, :string, :pos_integer},
default: %{}
]
]
],
codex: [
type: :map,
default: %{},
keys: [
command: [type: :string, default: @default_codex_command],
turn_timeout_ms: [
type: :integer,
default: @default_codex_turn_timeout_ms
],
read_timeout_ms: [
type: :integer,
default: @default_codex_read_timeout_ms
],
stall_timeout_ms: [
type: :integer,
default: @default_codex_stall_timeout_ms
]
]
],
hooks: [
type: :map,
default: %{},
keys: [
after_create: [type: {:or, [:string, nil]}, default: nil],
before_run: [type: {:or, [:string, nil]}, default: nil],
after_run: [type: {:or, [:string, nil]}, default: nil],
before_remove: [type: {:or, [:string, nil]}, default: nil],
timeout_ms: [type: :pos_integer, default: @default_hook_timeout_ms]
]
],
observability: [
type: :map,
default: %{},
keys: [
dashboard_enabled: [
type: :boolean,
default: @default_observability_enabled
],
refresh_ms: [
type: :integer,
default: @default_observability_refresh_ms
],
render_interval_ms: [
type: :integer,
default: @default_observability_render_interval_ms
]
]
],
server: [
type: :map,
default: %{},
keys: [
port: [type: {:or, [:non_neg_integer, nil]}, default: nil],
host: [type: :string, default: @default_server_host]
]
]
)
@type workflow_payload :: Workflow.loaded_workflow()
@type tracker_kind :: String.t() | nil
@type codex_runtime_settings :: %{
approval_policy: String.t() | map(),
thread_sandbox: String.t(),
turn_sandbox_policy: map()
}
@type workspace_hooks :: %{
after_create: String.t() | nil,
before_run: String.t() | nil,
after_run: String.t() | nil,
before_remove: String.t() | nil,
timeout_ms: pos_integer()
}
@spec current_workflow() :: {:ok, workflow_payload()} | {:error, term()}
def current_workflow do
Workflow.current()
end
@spec tracker_kind() :: tracker_kind()
def tracker_kind do
get_in(validated_workflow_options(), [:tracker, :kind])
end
@spec linear_endpoint() :: String.t()
def linear_endpoint do
get_in(validated_workflow_options(), [:tracker, :endpoint])
end
@spec linear_api_token() :: String.t() | nil
def linear_api_token do
validated_workflow_options()
|> get_in([:tracker, :api_key])
|> resolve_env_value(System.get_env("LINEAR_API_KEY"))
|> normalize_secret_value()
end
@spec openai_api_token() :: String.t() | nil
def openai_api_token do
System.get_env("OPENAI_API_KEY")
|> normalize_secret_value()
end
@spec linear_project_slug() :: String.t() | nil
def linear_project_slug do
get_in(validated_workflow_options(), [:tracker, :project_slug])
end
@spec linear_team_key() :: String.t() | nil
def linear_team_key do
get_in(validated_workflow_options(), [:tracker, :team_key])
end
@spec linear_assignee() :: String.t() | nil
def linear_assignee do
validated_workflow_options()
|> get_in([:tracker, :assignee])
|> resolve_env_value(System.get_env("LINEAR_ASSIGNEE"))
|> normalize_secret_value()
end
@spec linear_labels() :: [String.t()]
def linear_labels do
get_in(validated_workflow_options(), [:tracker, :labels])
end
@spec linear_dispatch_states() :: [String.t()]
def linear_dispatch_states do
get_in(validated_workflow_options(), [:tracker, :dispatch_states])
end
@spec linear_active_states() :: [String.t()]
def linear_active_states do
get_in(validated_workflow_options(), [:tracker, :active_states])
end
@spec linear_terminal_states() :: [String.t()]
def linear_terminal_states do
get_in(validated_workflow_options(), [:tracker, :terminal_states])
end
@spec poll_interval_ms() :: pos_integer()
def poll_interval_ms do
get_in(validated_workflow_options(), [:polling, :interval_ms])
end
@spec workspace_root() :: Path.t()
def workspace_root do
validated_workflow_options()
|> get_in([:workspace, :root])
|> resolve_path_value(@default_workspace_root)
end
@spec workspace_hooks() :: workspace_hooks()
def workspace_hooks do
hooks = get_in(validated_workflow_options(), [:hooks])
%{
after_create: Map.get(hooks, :after_create),
before_run: Map.get(hooks, :before_run),
after_run: Map.get(hooks, :after_run),
before_remove: Map.get(hooks, :before_remove),
timeout_ms: Map.get(hooks, :timeout_ms)
}
end
@spec hook_timeout_ms() :: pos_integer()
def hook_timeout_ms do
get_in(validated_workflow_options(), [:hooks, :timeout_ms])
end
@spec max_concurrent_agents() :: pos_integer()
def max_concurrent_agents do
get_in(validated_workflow_options(), [:agent, :max_concurrent_agents])
end
@spec max_retry_backoff_ms() :: pos_integer()
def max_retry_backoff_ms do
get_in(validated_workflow_options(), [:agent, :max_retry_backoff_ms])
end
@spec agent_max_turns() :: pos_integer()
def agent_max_turns do
get_in(validated_workflow_options(), [:agent, :max_turns])
end
@spec max_concurrent_agents_for_state(term()) :: pos_integer()
def max_concurrent_agents_for_state(state_name) when is_binary(state_name) do
state_limits = get_in(validated_workflow_options(), [:agent, :max_concurrent_agents_by_state])
global_limit = max_concurrent_agents()
Map.get(state_limits, normalize_issue_state(state_name), global_limit)
end
def max_concurrent_agents_for_state(_state_name), do: max_concurrent_agents()
@spec codex_command() :: String.t()
def codex_command do
get_in(validated_workflow_options(), [:codex, :command])
end
@spec codex_turn_timeout_ms() :: pos_integer()
def codex_turn_timeout_ms do
get_in(validated_workflow_options(), [:codex, :turn_timeout_ms])
end
@spec codex_approval_policy() :: String.t() | map()
def codex_approval_policy do
case resolve_codex_approval_policy() do
{:ok, approval_policy} -> approval_policy
{:error, _reason} -> @default_codex_approval_policy
end
end
@spec codex_thread_sandbox() :: String.t()
def codex_thread_sandbox do
case resolve_codex_thread_sandbox() do
{:ok, thread_sandbox} -> thread_sandbox
{:error, _reason} -> @default_codex_thread_sandbox
end
end
@spec codex_turn_sandbox_policy(Path.t() | nil) :: map()
def codex_turn_sandbox_policy(workspace \\ nil) do
case resolve_codex_turn_sandbox_policy(workspace) do
{:ok, turn_sandbox_policy} -> turn_sandbox_policy
{:error, _reason} -> default_codex_turn_sandbox_policy(workspace)
end
end
@spec codex_read_timeout_ms() :: pos_integer()
def codex_read_timeout_ms do
get_in(validated_workflow_options(), [:codex, :read_timeout_ms])
end
@spec codex_stall_timeout_ms() :: non_neg_integer()
def codex_stall_timeout_ms do
validated_workflow_options()
|> get_in([:codex, :stall_timeout_ms])
|> max(0)
end
@spec workflow_prompt() :: String.t()
def workflow_prompt do
case current_workflow() do
{:ok, %{prompt_template: prompt}} ->
if String.trim(prompt) == "", do: @default_prompt_template, else: prompt
_ ->
@default_prompt_template
end
end
@spec observability_enabled?() :: boolean()
def observability_enabled? do
get_in(validated_workflow_options(), [:observability, :dashboard_enabled])
end
@spec observability_refresh_ms() :: pos_integer()
def observability_refresh_ms do
get_in(validated_workflow_options(), [:observability, :refresh_ms])
end
@spec observability_render_interval_ms() :: pos_integer()
def observability_render_interval_ms do
get_in(validated_workflow_options(), [:observability, :render_interval_ms])
end
@spec server_port() :: non_neg_integer() | nil
def server_port do
case Application.get_env(:symphony_elixir, :server_port_override) do
port when is_integer(port) and port >= 0 ->
port
_ ->
case get_in(validated_workflow_options(), [:server, :port]) do
port when is_integer(port) and port >= 0 -> port
_ -> env_server_port()
end
end
end
defp env_server_port do
case System.get_env("SYMPHONY_SERVER_PORT") do
nil ->
nil
val ->
case Integer.parse(val) do
{port, _} when port >= 0 -> port
_ -> nil
end
end
end
@spec server_host() :: String.t()
def server_host do
get_in(validated_workflow_options(), [:server, :host])
end
@spec validate!() :: :ok | {:error, term()}
def validate! do
with {:ok, _workflow} <- current_workflow(),
:ok <- require_tracker_kind(),
:ok <- require_linear_token(),
:ok <- require_openai_token(),
:ok <- require_linear_target(),
:ok <- require_valid_codex_runtime_settings() do
require_codex_command()
end
end
@spec codex_runtime_settings(Path.t() | nil) :: {:ok, codex_runtime_settings()} | {:error, term()}
def codex_runtime_settings(workspace \\ nil) do
with {:ok, approval_policy} <- resolve_codex_approval_policy(),
{:ok, thread_sandbox} <- resolve_codex_thread_sandbox(),
{:ok, turn_sandbox_policy} <- resolve_codex_turn_sandbox_policy(workspace) do
{:ok,
%{
approval_policy: approval_policy,
thread_sandbox: thread_sandbox,
turn_sandbox_policy: turn_sandbox_policy
}}
end
end
defp require_tracker_kind do
case tracker_kind() do
"linear" -> :ok
"memory" -> :ok
nil -> {:error, :missing_tracker_kind}
other -> {:error, {:unsupported_tracker_kind, other}}
end
end
defp require_linear_token do
case tracker_kind() do
"linear" ->
if is_binary(linear_api_token()) do
:ok
else
{:error, :missing_linear_api_token}
end
_ ->
:ok
end
end
defp require_openai_token do
if is_binary(openai_api_token()) do
:ok
else
{:error, :missing_openai_api_token}
end
end
defp require_linear_target do
case tracker_kind() do
"linear" ->
if present_tracker_target?(linear_project_slug()) or present_tracker_target?(linear_team_key()) do
:ok
else
{:error, :missing_linear_project_or_team_key}
end
_ ->
:ok
end
end
defp require_codex_command do
if byte_size(String.trim(codex_command())) > 0 do
:ok
else
{:error, :missing_codex_command}
end
end
defp require_valid_codex_runtime_settings do
case codex_runtime_settings() do
{:ok, _settings} -> :ok
{:error, reason} -> {:error, reason}
end
end
defp validated_workflow_options do
workflow_config()
|> extract_workflow_options()
|> NimbleOptions.validate!(@workflow_options_schema)
end
defp extract_workflow_options(config) do
%{
tracker: extract_tracker_options(section_map(config, "tracker")),
polling: extract_polling_options(section_map(config, "polling")),
workspace: extract_workspace_options(section_map(config, "workspace")),
agent: extract_agent_options(section_map(config, "agent")),
codex: extract_codex_options(section_map(config, "codex")),
hooks: extract_hooks_options(section_map(config, "hooks")),
observability: extract_observability_options(section_map(config, "observability")),
server: extract_server_options(section_map(config, "server"))
}
end
defp extract_tracker_options(section) do
%{}
|> put_if_present(:kind, normalize_tracker_kind(scalar_string_value(Map.get(section, "kind"))))
|> put_if_present(:endpoint, scalar_string_value(Map.get(section, "endpoint")))
|> put_if_present(:api_key, binary_value(Map.get(section, "api_key"), allow_empty: true))
|> put_if_present(:project_slug, scalar_string_value(Map.get(section, "project_slug")))
|> put_if_present(:team_key, scalar_string_value(Map.get(section, "team_key")))
|> put_if_present(:labels, csv_value(Map.get(section, "labels")))
|> put_if_present(:dispatch_states, csv_value(Map.get(section, "dispatch_states")))
|> put_if_present(:active_states, csv_value(Map.get(section, "active_states")))
|> put_if_present(:terminal_states, csv_value(Map.get(section, "terminal_states")))
end
defp extract_polling_options(section) do
%{}
|> put_if_present(:interval_ms, integer_value(Map.get(section, "interval_ms")))
end
defp extract_workspace_options(section) do
%{}
|> put_if_present(:root, binary_value(Map.get(section, "root")))
end
defp extract_agent_options(section) do
%{}
|> put_if_present(:max_concurrent_agents, integer_value(Map.get(section, "max_concurrent_agents")))
|> put_if_present(:max_turns, positive_integer_value(Map.get(section, "max_turns")))
|> put_if_present(:max_retry_backoff_ms, positive_integer_value(Map.get(section, "max_retry_backoff_ms")))
|> put_if_present(
:max_concurrent_agents_by_state,
state_limits_value(Map.get(section, "max_concurrent_agents_by_state"))
)
end
defp extract_codex_options(section) do
%{}
|> put_if_present(:command, command_value(Map.get(section, "command")))
|> put_if_present(:turn_timeout_ms, integer_value(Map.get(section, "turn_timeout_ms")))
|> put_if_present(:read_timeout_ms, integer_value(Map.get(section, "read_timeout_ms")))
|> put_if_present(:stall_timeout_ms, integer_value(Map.get(section, "stall_timeout_ms")))
end
defp extract_hooks_options(section) do
%{}
|> put_if_present(:after_create, hook_command_value(Map.get(section, "after_create")))
|> put_if_present(:before_run, hook_command_value(Map.get(section, "before_run")))
|> put_if_present(:after_run, hook_command_value(Map.get(section, "after_run")))
|> put_if_present(:before_remove, hook_command_value(Map.get(section, "before_remove")))
|> put_if_present(:timeout_ms, positive_integer_value(Map.get(section, "timeout_ms")))
end
defp extract_observability_options(section) do
%{}
|> put_if_present(:dashboard_enabled, boolean_value(Map.get(section, "dashboard_enabled")))
|> put_if_present(:refresh_ms, integer_value(Map.get(section, "refresh_ms")))
|> put_if_present(:render_interval_ms, integer_value(Map.get(section, "render_interval_ms")))
end
defp extract_server_options(section) do
%{}
|> put_if_present(:port, non_negative_integer_value(Map.get(section, "port")))
|> put_if_present(:host, scalar_string_value(Map.get(section, "host")))
end
defp section_map(config, key) do
case Map.get(config, key) do
section when is_map(section) -> section
_ -> %{}
end
end
defp put_if_present(map, _key, :omit), do: map
defp put_if_present(map, key, value), do: Map.put(map, key, value)
defp scalar_string_value(nil), do: :omit
defp scalar_string_value(value) when is_binary(value), do: String.trim(value)
defp scalar_string_value(value) when is_boolean(value), do: to_string(value)
defp scalar_string_value(value) when is_integer(value), do: to_string(value)
defp scalar_string_value(value) when is_float(value), do: to_string(value)
defp scalar_string_value(value) when is_atom(value), do: Atom.to_string(value)
defp scalar_string_value(_value), do: :omit
defp binary_value(value, opts \\ [])
defp binary_value(value, opts) when is_binary(value) do
allow_empty = Keyword.get(opts, :allow_empty, false)
if value == "" and not allow_empty do
:omit
else
value
end
end
defp binary_value(_value, _opts), do: :omit
defp command_value(value) when is_binary(value) do
case String.trim(value) do
"" -> :omit
trimmed -> trimmed
end
end
defp command_value(_value), do: :omit
defp hook_command_value(value) when is_binary(value) do
case String.trim(value) do
"" -> :omit
_ -> String.trim_trailing(value)
end
end
defp hook_command_value(_value), do: :omit
defp csv_value(values) when is_list(values) do
values
|> Enum.reduce([], fn value, acc -> maybe_append_csv_value(acc, value) end)
|> Enum.reverse()
|> case do
[] -> :omit
normalized_values -> normalized_values
end
end
defp csv_value(value) when is_binary(value) do
value
|> String.split(",", trim: true)
|> Enum.map(&String.trim/1)
|> Enum.reject(&(&1 == ""))
|> case do
[] -> :omit
normalized_values -> normalized_values
end
end
defp csv_value(_value), do: :omit
defp maybe_append_csv_value(acc, value) do
case scalar_string_value(value) do
:omit ->
acc
normalized ->
append_csv_value_if_present(acc, normalized)
end
end
defp append_csv_value_if_present(acc, value) do
trimmed = String.trim(value)
if trimmed == "" do
acc
else
[trimmed | acc]
end
end
defp integer_value(value) do
case parse_integer(value) do
{:ok, parsed} -> parsed
:error -> :omit
end
end
defp positive_integer_value(value) do
case parse_positive_integer(value) do
{:ok, parsed} -> parsed
:error -> :omit
end
end
defp non_negative_integer_value(value) do
case parse_non_negative_integer(value) do
{:ok, parsed} -> parsed
:error -> :omit
end
end
defp boolean_value(value) when is_boolean(value), do: value
defp boolean_value(value) when is_binary(value) do
case String.downcase(String.trim(value)) do
"true" -> true
"false" -> false
_ -> :omit
end
end
defp boolean_value(_value), do: :omit
defp state_limits_value(value) when is_map(value) do
value
|> Enum.reduce(%{}, fn {state_name, limit}, acc ->
case parse_positive_integer(limit) do
{:ok, parsed} ->
Map.put(acc, normalize_issue_state(to_string(state_name)), parsed)
:error ->
acc
end
end)
end
defp state_limits_value(_value), do: :omit
defp parse_integer(value) when is_integer(value), do: {:ok, value}
defp parse_integer(value) when is_binary(value) do
case Integer.parse(String.trim(value)) do
{parsed, _} -> {:ok, parsed}
:error -> :error
end
end
defp parse_integer(_value), do: :error
defp parse_positive_integer(value) do
case parse_integer(value) do
{:ok, parsed} when parsed > 0 -> {:ok, parsed}
_ -> :error
end
end
defp parse_non_negative_integer(value) do
case parse_integer(value) do
{:ok, parsed} when parsed >= 0 -> {:ok, parsed}
_ -> :error
end
end
@doc false
@spec present_tracker_target?(term()) :: boolean()
def present_tracker_target?(value) when is_binary(value) do
String.trim(value) != ""
end
def present_tracker_target?(_value), do: false
defp fetch_value(paths, default) do
config = workflow_config()
case resolve_config_value(config, paths) do
:missing -> default
value -> value
end
end
defp resolve_codex_approval_policy do
case fetch_value([["codex", "approval_policy"]], :missing) do
:missing ->
{:ok, @default_codex_approval_policy}
nil ->
{:ok, @default_codex_approval_policy}
value when is_binary(value) ->
approval_policy = String.trim(value)
if approval_policy == "" do
{:error, {:invalid_codex_approval_policy, value}}
else
{:ok, approval_policy}
end
value when is_map(value) ->
{:ok, value}
value ->
{:error, {:invalid_codex_approval_policy, value}}
end
end
defp resolve_codex_thread_sandbox do
case fetch_value([["codex", "thread_sandbox"]], :missing) do
:missing ->
{:ok, @default_codex_thread_sandbox}
nil ->
{:ok, @default_codex_thread_sandbox}
value when is_binary(value) ->
thread_sandbox = String.trim(value)
if thread_sandbox == "" do
{:error, {:invalid_codex_thread_sandbox, value}}
else
{:ok, thread_sandbox}
end
value ->
{:error, {:invalid_codex_thread_sandbox, value}}
end
end
defp resolve_codex_turn_sandbox_policy(workspace) do
case fetch_value([["codex", "turn_sandbox_policy"]], :missing) do
:missing ->
{:ok, default_codex_turn_sandbox_policy(workspace)}
nil ->
{:ok, default_codex_turn_sandbox_policy(workspace)}
value when is_map(value) ->
{:ok, value}
value ->
{:error, {:invalid_codex_turn_sandbox_policy, {:unsupported_value, value}}}
end
end
defp default_codex_turn_sandbox_policy(workspace) do
writable_root =
if is_binary(workspace) and String.trim(workspace) != "" do
Path.expand(workspace)
else
Path.expand(workspace_root())
end
%{
"type" => "workspaceWrite",
"writableRoots" => [writable_root],
"readOnlyAccess" => %{"type" => "fullAccess"},
"networkAccess" => false,
"excludeTmpdirEnvVar" => false,
"excludeSlashTmp" => false
}
end
defp normalize_issue_state(state_name) when is_binary(state_name) do
state_name
|> String.trim()
|> String.downcase()
end
defp normalize_tracker_kind(kind) when is_binary(kind) do
kind
|> String.trim()
|> String.downcase()
|> case do
"" -> nil
normalized -> normalized
end
end
defp normalize_tracker_kind(_kind), do: nil
defp workflow_config do
case current_workflow() do
{:ok, %{config: config}} when is_map(config) ->
normalize_keys(config)
_ ->
%{}
end
end
defp resolve_config_value(%{} = config, paths) do
Enum.reduce_while(paths, :missing, fn path, _acc ->
case get_in_path(config, path) do
:missing -> {:cont, :missing}
value -> {:halt, value}
end
end)
end
defp get_in_path(config, path) when is_list(path) and is_map(config) do
get_in_path(config, path, 0)
end
defp get_in_path(_, _), do: :missing
defp get_in_path(config, [], _depth), do: config
defp get_in_path(%{} = current, [segment | rest], _depth) do
case Map.fetch(current, normalize_key(segment)) do
{:ok, value} -> get_in_path(value, rest, 0)
:error -> :missing
end
end
defp get_in_path(_, _, _depth), do: :missing
defp normalize_keys(value) when is_map(value) do
Enum.reduce(value, %{}, fn {key, raw_value}, normalized ->
Map.put(normalized, normalize_key(key), normalize_keys(raw_value))
end)
end
defp normalize_keys(value) when is_list(value), do: Enum.map(value, &normalize_keys/1)
defp normalize_keys(value), do: value
defp normalize_key(value) when is_atom(value), do: Atom.to_string(value)
defp normalize_key(value), do: to_string(value)
defp resolve_path_value(:missing, default), do: default
defp resolve_path_value(nil, default), do: default
defp resolve_path_value(value, default) when is_binary(value) do
case normalize_path_token(value) do
:missing ->
default
path ->
path
|> String.trim()
|> preserve_command_name()
|> then(fn
"" -> default
resolved -> resolved
end)
end
end
defp resolve_path_value(_value, default), do: default
defp preserve_command_name(path) do
cond do
uri_path?(path) ->
path
String.contains?(path, "/") or String.contains?(path, "\\") ->
Path.expand(path)
true ->
path
end
end
defp uri_path?(path) do
String.match?(to_string(path), ~r/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//)
end
defp resolve_env_value(:missing, fallback), do: fallback
defp resolve_env_value(nil, fallback), do: fallback
defp resolve_env_value(value, fallback) when is_binary(value) do
trimmed = String.trim(value)
case env_reference_name(trimmed) do
{:ok, env_name} ->
env_name
|> System.get_env()
|> then(fn
nil -> fallback
"" -> nil
env_value -> env_value
end)
:error ->
trimmed
end
end
defp resolve_env_value(_value, fallback), do: fallback
defp normalize_path_token(value) when is_binary(value) do
trimmed = String.trim(value)
case env_reference_name(trimmed) do
{:ok, env_name} -> resolve_env_token(env_name)
:error -> trimmed
end
end
defp env_reference_name("$" <> env_name) do
if String.match?(env_name, ~r/^[A-Za-z_][A-Za-z0-9_]*$/) do
{:ok, env_name}
else
:error
end
end
defp env_reference_name(_value), do: :error
defp resolve_env_token(value) do
case System.get_env(value) do
nil -> :missing
env_value -> env_value
end
end
defp normalize_secret_value(value) when is_binary(value) do
case String.trim(value) do
"" -> nil
trimmed -> trimmed
end
end
defp normalize_secret_value(_value), do: nil
end