-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.sh
More file actions
executable file
·1279 lines (1118 loc) · 34 KB
/
Copy pathagent.sh
File metadata and controls
executable file
·1279 lines (1118 loc) · 34 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
999
1000
#!/usr/bin/env bash
set -euo pipefail
readonly IMAGE_NAME="${IMAGE_NAME:-agent-plain}"
readonly DEFAULT_RUNTIME_FILE="/etc/agentctl/preferred-runtime"
readonly USER_CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/agentctl"
readonly USER_RUNTIME_FILE="${USER_CONFIG_DIR}/preferred-runtime"
readonly RUNTIME_CONFIG_JSON="${AGENTCTL_RUNTIME_CONFIG_JSON-}"
readonly RUNTIME_OVERRIDE="${AGENTCTL_RUNTIME_OVERRIDE-}"
readonly MODEL_OVERRIDE="${AGENTCTL_MODEL_OVERRIDE:-}"
readonly RUN_MODE="${AGENTCTL_RUN_MODE:-local}"
readonly RUNTIME_REGISTRY_DIR="${AGENTCTL_RUNTIME_REGISTRY_DIR:-/etc/agentctl/runtimes.d}"
readonly RUNTIME_ADAPTER_DIR="${AGENTCTL_RUNTIME_ADAPTER_DIR:-/usr/local/lib/agentctl/runtimes}"
readonly FEATURE_REGISTRY_DIR="${AGENTCTL_FEATURE_REGISTRY_DIR:-/etc/agentctl/features.d}"
readonly FEATURE_ADAPTER_DIR="${AGENTCTL_FEATURE_ADAPTER_DIR:-/usr/local/lib/agentctl/features}"
die() {
printf '%s\n' "$*" >&2
exit 1
}
runtime_default() {
if [ -f "$DEFAULT_RUNTIME_FILE" ]; then
tr -d '[:space:]' <"$DEFAULT_RUNTIME_FILE"
return
fi
printf '%s\n' codex
}
runtime_preferred() {
if [ -n "${AGENTCTL_PREFERRED_RUNTIME:-}" ]; then
printf '%s\n' "$AGENTCTL_PREFERRED_RUNTIME"
return
fi
if [ -f "$USER_RUNTIME_FILE" ]; then
tr -d '[:space:]' <"$USER_RUNTIME_FILE"
return
fi
runtime_default
}
has_explicit_runtime_model() {
local arg
for arg in "$@"; do
case "$arg" in
-m|-m=*|--model|--model=*) return 0 ;;
esac
done
return 1
}
runtime_model_arg_value() {
local arg
local next_is_model=0
for arg in "$@"; do
if [ "$next_is_model" -eq 1 ]; then
printf '%s\n' "$arg"
return 0
fi
case "$arg" in
-m|--model)
next_is_model=1
;;
-m=*|--model=*)
printf '%s\n' "${arg#*=}"
return 0
;;
esac
done
return 1
}
ensure_user_config_dir() {
mkdir -p "$USER_CONFIG_DIR"
if [ "$(id -u)" -eq 0 ]; then
chown "$(home_owner)" "$USER_CONFIG_DIR"
fi
}
home_owner() {
stat -c '%u:%g' "$HOME" 2>/dev/null \
|| stat -f '%u:%g' "$HOME" 2>/dev/null \
|| printf '%s\n' "coder:coder"
}
agent_tools_home() {
printf '%s\n' "${AGENTCTL_TOOLS_HOME:-/opt/agentctl}"
}
agent_tools_bin_dir() {
printf '%s\n' "${AGENTCTL_TOOLS_BIN_DIR:-$(agent_tools_home)/bin}"
}
runtime_tool_home() {
local runtime="$1"
local env_name=""
local value=""
env_name="AGENTCTL_$(printf '%s' "$runtime" | tr '[:lower:]-' '[:upper:]_')_TOOLS_HOME"
eval "value=\"\${$env_name:-}\""
if [ -n "$value" ]; then
printf '%s\n' "$value"
return 0
fi
printf '%s/%s\n' "$(agent_tools_home)" "$runtime"
}
agent_export_tools_path() {
local tools_bin=""
tools_bin="$(agent_tools_bin_dir)"
case ":$PATH:" in
*":$tools_bin:"*) ;;
*) export PATH="$tools_bin:$PATH" ;;
esac
}
runtime_tool_env() {
local runtime="$1"
shift
env \
"AGENTCTL_TOOLS_HOME=$(agent_tools_home)" \
"PATH=$(agent_tools_bin_dir):$PATH" \
"$@"
}
runtime_manifest_path() {
local runtime="$1"
local path="${RUNTIME_REGISTRY_DIR}/${runtime}.json"
[ -f "$path" ] || return 1
printf '%s\n' "$path"
}
runtime_adapter_path() {
local runtime="$1"
local path="${RUNTIME_ADAPTER_DIR}/${runtime}.sh"
[ -f "$path" ] || return 1
printf '%s\n' "$path"
}
feature_manifest_path() {
local feature="$1"
local path="${FEATURE_REGISTRY_DIR}/${feature}.json"
[ -f "$path" ] || return 1
printf '%s\n' "$path"
}
feature_adapter_path() {
local feature="$1"
local path="${FEATURE_ADAPTER_DIR}/${feature}.sh"
[ -f "$path" ] || return 1
printf '%s\n' "$path"
}
runtime_manifest_string() {
local runtime="$1" key="$2"
local manifest
manifest="$(runtime_manifest_path "$runtime")" || return 1
jq -er "$key // empty" "$manifest"
}
runtime_manifest_json() {
local runtime="$1" key="$2"
local manifest
manifest="$(runtime_manifest_path "$runtime")" || return 1
jq -c "$key // []" "$manifest"
}
feature_manifest_string() {
local feature="$1" key="$2"
local manifest
manifest="$(feature_manifest_path "$feature")" || return 1
jq -er "$key // empty" "$manifest"
}
feature_manifest_json() {
local feature="$1" key="$2"
local manifest
manifest="$(feature_manifest_path "$feature")" || return 1
jq -c "$key // []" "$manifest"
}
runtime_known() {
runtime_manifest_path "$1" >/dev/null 2>&1
}
feature_known() {
feature_manifest_path "$1" >/dev/null 2>&1
}
runtime_ids() {
local path
[ -d "$RUNTIME_REGISTRY_DIR" ] || return 0
for path in "$RUNTIME_REGISTRY_DIR"/*.json; do
[ -e "$path" ] || continue
basename "$path" .json
done | sort
}
feature_ids() {
local path
[ -d "$FEATURE_REGISTRY_DIR" ] || return 0
for path in "$FEATURE_REGISTRY_DIR"/*.json; do
[ -e "$path" ] || continue
basename "$path" .json
done | sort
}
runtime_ids_installed() {
local runtime
while IFS= read -r runtime; do
[ -n "$runtime" ] || continue
if runtime_command_exists "$runtime"; then
printf '%s\n' "$runtime"
fi
done < <(runtime_ids)
}
feature_ids_installed() {
local feature
while IFS= read -r feature; do
[ -n "$feature" ] || continue
if [ "$(feature_installed_json "$feature")" = "true" ]; then
printf '%s\n' "$feature"
fi
done < <(feature_ids)
}
runtime_command_name() {
runtime_manifest_string "$1" '.command'
}
runtime_command_path() {
local runtime="$1"
local command_name=""
local candidate=""
local command_path=""
command_name="$(runtime_command_name "$runtime")" || return 1
candidate="$(agent_tools_bin_dir)/$command_name"
if [ -x "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
command_path="$(command -v "$command_name" 2>/dev/null || true)"
if [ -n "$command_path" ] && [ -x "$command_path" ]; then
printf '%s\n' "$command_path"
return 0
fi
for candidate in "$HOME/.local/bin/$command_name" "$HOME/bin/$command_name"; do
if [ -x "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
return 1
}
runtime_install_method() {
runtime_manifest_string "$1" '.install_method'
}
runtime_default_config_dir() {
runtime_manifest_string "$1" '.default_config_dir'
}
runtime_auth_formats_json() {
runtime_manifest_json "$1" '.auth_formats'
}
runtime_capabilities_json() {
runtime_manifest_json "$1" '.capabilities'
}
runtime_commands_json() {
runtime_manifest_json "$1" '.commands'
}
runtime_launch_configs_json() {
local runtime="$1"
local manifest
manifest="$(runtime_manifest_path "$runtime")" || return 1
jq -c '.launch_configs // {}' "$manifest"
}
feature_display_name() {
feature_manifest_string "$1" '.display_name'
}
feature_install_method() {
feature_manifest_string "$1" '.install_method'
}
feature_description() {
feature_manifest_string "$1" '.description'
}
feature_capabilities_json() {
feature_manifest_json "$1" '.capabilities'
}
feature_commands_json() {
feature_manifest_json "$1" '.commands'
}
runtime_command_exists() {
runtime_command_path "$1" >/dev/null 2>&1
}
runtime_installed_json() {
local runtime="$1"
if runtime_command_exists "$runtime"; then
printf '%s\n' true
else
printf '%s\n' false
fi
}
ensure_runtime_known() {
local runtime="$1"
runtime_known "$runtime" || die "unsupported runtime: $runtime"
}
ensure_feature_known() {
local feature="$1"
feature_known "$feature" || die "unsupported feature: $feature"
}
reset_runtime_hooks() {
unset -f \
agent_runtime_run \
agent_runtime_install \
agent_runtime_update \
agent_runtime_reset_config \
agent_runtime_state_paths \
agent_runtime_auth_read \
agent_runtime_auth_write \
agent_runtime_auth_login \
agent_runtime_mcp_add \
agent_runtime_mcp_sync \
>/dev/null 2>&1 || true
}
reset_feature_hooks() {
unset -f \
agent_feature_installed \
agent_feature_install \
agent_feature_remove \
agent_feature_update \
>/dev/null 2>&1 || true
}
load_runtime_adapter() {
local runtime="$1"
local adapter
ensure_runtime_known "$runtime"
adapter="$(runtime_adapter_path "$runtime")" || die "missing runtime adapter: $runtime"
reset_runtime_hooks
# shellcheck source=/dev/null
. "$adapter"
}
load_feature_adapter() {
local feature="$1"
local adapter
ensure_feature_known "$feature"
adapter="$(feature_adapter_path "$feature")" || die "missing feature adapter: $feature"
reset_feature_hooks
# shellcheck source=/dev/null
. "$adapter"
}
ensure_runtime_installed() {
local runtime="$1"
ensure_runtime_known "$runtime"
runtime_command_exists "$runtime" && return 0
die "runtime not installed: $runtime (run: agent.sh runtime install $runtime)"
}
feature_installed_json() {
local feature="$1"
ensure_feature_known "$feature"
load_feature_adapter "$feature"
if agent_feature_installed "$feature"; then
printf '%s\n' true
else
printf '%s\n' false
fi
}
run_runtime() {
local runtime="$1"
shift
ensure_runtime_installed "$runtime"
load_runtime_adapter "$runtime"
agent_runtime_run "$runtime" "$@"
}
runtime_config_json() {
local config_json="$RUNTIME_CONFIG_JSON"
if [ -z "$config_json" ]; then
config_json='{}'
fi
printf '%s' "$config_json" | jq -c '
if type == "object" then
with_entries(.value |= if . == null then "" else tostring end)
else
error("runtime config must be an object")
end
' 2>/dev/null || die "invalid runtime launch config JSON"
}
runtime_config_value() {
local key="$1"
local default_value="${2:-}"
local value=""
value="$(runtime_config_json | jq -er --arg key "$key" '.[$key] // empty' 2>/dev/null || true)"
if [ -n "$value" ]; then
printf '%s\n' "$value"
return 0
fi
printf '%s\n' "$default_value"
}
runtime_config_enabled() {
local key="$1"
local value=""
value="$(runtime_config_value "$key")"
case "$value" in
1|true|TRUE|yes|YES|on|ON) return 0 ;;
*) return 1 ;;
esac
}
ollama_detect_gateway() {
local route_file="${AGENTCTL_OLLAMA_ROUTE_FILE:-/proc/net/route}"
awk '
function hex2dec(hex, i, c, n, v) {
n = 0
hex = toupper(hex)
for (i = 1; i <= length(hex); i++) {
c = substr(hex, i, 1)
v = index("0123456789ABCDEF", c) - 1
if (v < 0) {
exit 1
}
n = (n * 16) + v
}
return n
}
$2 == "00000000" && length($3) == 8 {
printf "%s.%s.%s.%s\n",
hex2dec(substr($3, 7, 2)),
hex2dec(substr($3, 5, 2)),
hex2dec(substr($3, 3, 2)),
hex2dec(substr($3, 1, 2))
exit
}
' "$route_file" 2>/dev/null || true
}
ollama_configured_base_url() {
local value=""
value="$(runtime_config_value ollama_host)"
if [ -z "$value" ]; then
value="${OLLAMA_HOST:-}"
fi
[ -n "$value" ] || return 1
while [ "${value%/}" != "$value" ]; do
value="${value%/}"
done
[ -n "$value" ] || die "invalid Ollama host: $value"
printf '%s\n' "$value"
}
ollama_resolve_base_url() {
local configured_base_url=""
local gateway=""
local api_url=""
local ollama_port="11434"
command -v curl >/dev/null 2>&1 || die "Missing curl required for local Ollama connectivity checks"
configured_base_url="$(ollama_configured_base_url || true)"
if [ -n "$configured_base_url" ]; then
api_url="${configured_base_url}/api/version"
if curl -fsS --max-time 3 "$api_url" >/dev/null 2>&1; then
printf '%s\n' "$configured_base_url"
return 0
fi
die "Local Ollama is not reachable from the container.
Tried:
- Configured Ollama host: $api_url
Use a URL reachable from inside the container, for example:
OLLAMA_HOST=http://host.container.internal:11439"
fi
gateway="$(ollama_detect_gateway)"
[ -n "$gateway" ] || die "Unable to determine the container host gateway for local Ollama"
api_url="http://${gateway}:${ollama_port}/api/version"
if curl -fsS --max-time 3 "$api_url" >/dev/null 2>&1; then
printf 'http://%s:%s\n' "$gateway" "$ollama_port"
return 0
fi
die "Local Ollama is not reachable from the container.
Tried:
- Detected host gateway: $api_url
Expose or proxy Ollama onto the container network.
See docs/networking.md.
Host-side fixes:
- Rerun the agent from the macOS host with a managed listener:
agentctl run --start-ollama
- Start a second Ollama listener:
OLLAMA_HOST=http://${gateway}:${ollama_port} ollama serve
- Proxy localhost with socat (needs \`brew install socat\`):
socat TCP-LISTEN:${ollama_port},fork,bind=${gateway} TCP:127.0.0.1:${ollama_port}"
}
ollama_show_model() {
local ollama_base_url="$1"
local model="$2"
local output_file="$3"
command -v curl >/dev/null 2>&1 || die "Missing curl required for local Ollama model checks"
command -v jq >/dev/null 2>&1 || die "Missing jq required for local Ollama model checks"
if ! jq -n --arg model "$model" '{model: $model}' \
| curl -fsS --max-time 10 \
-H 'Content-Type: application/json' \
-d @- \
"${ollama_base_url%/}/api/show" >"$output_file"; then
die "Local Ollama model is not available: $model
Pull it on the host first:
ollama pull $model"
fi
}
ollama_require_model() {
local ollama_base_url="$1"
local model="$2"
ollama_show_model "$ollama_base_url" "$model" /dev/null
}
json_runtime_info() {
local runtime="$1"
local installed_json preferred_json commands_json capabilities_json launch_configs_json command_path_json command_path
ensure_runtime_known "$runtime"
installed_json="$(runtime_installed_json "$runtime")"
commands_json="$(runtime_commands_json "$runtime")"
capabilities_json="$(runtime_capabilities_json "$runtime")"
launch_configs_json="$(runtime_launch_configs_json "$runtime")"
if command_path="$(runtime_command_path "$runtime" 2>/dev/null)"; then
command_path_json="$(printf '%s' "$command_path" | jq -R .)"
else
command_path_json='null'
fi
if [ "$(runtime_preferred)" = "$runtime" ]; then
preferred_json=true
else
preferred_json=false
fi
jq -n \
--arg runtime "$runtime" \
--arg image "$IMAGE_NAME" \
--arg preferred "$(runtime_preferred)" \
--arg launcher "/usr/local/bin/agent.sh run" \
--arg command_name "$(runtime_command_name "$runtime")" \
--arg tools_home "$(agent_tools_home)" \
--arg tools_bin_dir "$(agent_tools_bin_dir)" \
--arg runtime_tool_home "$(runtime_tool_home "$runtime")" \
--arg install_method "$(runtime_install_method "$runtime")" \
--arg config_dir "$(runtime_default_config_dir "$runtime")" \
--argjson auth_formats "$(runtime_auth_formats_json "$runtime")" \
--argjson capabilities "$capabilities_json" \
--argjson commands "$commands_json" \
--argjson launch_configs "$launch_configs_json" \
--argjson command_path "$command_path_json" \
--argjson installed "$installed_json" \
--argjson selected "$preferred_json" \
'{
runtime: $runtime,
image: $image,
installed: $installed,
preferred: $selected,
preferred_runtime: $preferred,
launcher: $launcher,
command: $command_name,
command_path: $command_path,
tools_home: $tools_home,
tools_bin_dir: $tools_bin_dir,
runtime_tool_home: $runtime_tool_home,
install_method: $install_method,
auth_formats: $auth_formats,
capabilities: $capabilities,
commands: $commands,
launch_configs: $launch_configs,
default_config_dir: $config_dir,
phase: 2
}'
}
json_runtime_capabilities() {
local runtime="$1"
local capabilities_json launch_configs_json
ensure_runtime_known "$runtime"
capabilities_json="$(runtime_capabilities_json "$runtime")"
launch_configs_json="$(runtime_launch_configs_json "$runtime")"
jq -n \
--arg runtime "$runtime" \
--argjson auth_formats "$(runtime_auth_formats_json "$runtime")" \
--argjson capabilities "$capabilities_json" \
--argjson commands "$(runtime_commands_json "$runtime")" \
--argjson launch_configs "$launch_configs_json" \
'{
runtime: $runtime,
auth_formats: $auth_formats,
capabilities: $capabilities,
commands: $commands,
launch_configs: $launch_configs
}'
}
json_feature_info() {
local feature="$1"
local installed_json commands_json capabilities_json
ensure_feature_known "$feature"
installed_json="$(feature_installed_json "$feature")"
commands_json="$(feature_commands_json "$feature")"
capabilities_json="$(feature_capabilities_json "$feature")"
jq -n \
--arg feature "$feature" \
--arg image "$IMAGE_NAME" \
--arg display_name "$(feature_display_name "$feature")" \
--arg install_method "$(feature_install_method "$feature")" \
--arg description "$(feature_description "$feature")" \
--argjson commands "$commands_json" \
--argjson capabilities "$capabilities_json" \
--argjson installed "$installed_json" \
'{
feature: $feature,
image: $image,
display_name: $display_name,
installed: $installed,
install_method: $install_method,
description: $description,
capabilities: $capabilities,
commands: $commands
}'
}
json_system_manifest() {
local package_manager packages_json requested_packages_json apk_repositories_json runtimes_json features_json default_runtime preferred_runtime image_version tooling_version
local apk_world_file="${AGENTCTL_APK_WORLD_FILE:-/etc/apk/world}"
local apk_repositories_file="${AGENTCTL_APK_REPOSITORIES_FILE:-/etc/apk/repositories}"
local image_version_file="${AGENTCTL_IMAGE_VERSION_FILE:-/etc/agentctl/image-version}"
local tooling_version_file="${AGENTCTL_TOOLING_VERSION_FILE:-/etc/agentctl/tooling-version}"
if command -v apk >/dev/null 2>&1; then
package_manager=apk
packages_json="$(apk info -q 2>/dev/null | sort -u | jq -R . | jq -s .)"
if [ -f "$apk_world_file" ]; then
requested_packages_json="$(sed '/^[[:space:]]*$/d' "$apk_world_file" 2>/dev/null | sort -u | jq -R . | jq -s .)"
else
requested_packages_json='[]'
fi
if [ -f "$apk_repositories_file" ]; then
apk_repositories_json="$(sed '/^[[:space:]]*#/d;/^[[:space:]]*$/d' "$apk_repositories_file" 2>/dev/null | sort -u | jq -R . | jq -s .)"
else
apk_repositories_json='[]'
fi
elif command -v dpkg-query >/dev/null 2>&1; then
package_manager=dpkg
packages_json="$(dpkg-query -W -f='${Package}\n' 2>/dev/null | sort -u | jq -R . | jq -s .)"
if command -v apt-mark >/dev/null 2>&1; then
requested_packages_json="$(apt-mark showmanual 2>/dev/null | sort -u | jq -R . | jq -s .)"
else
requested_packages_json='[]'
fi
apk_repositories_json='[]'
else
package_manager=unknown
packages_json='[]'
requested_packages_json='[]'
apk_repositories_json='[]'
fi
runtimes_json="$(runtime_ids_installed | jq -R . | jq -s .)"
features_json="$(feature_ids_installed | jq -R . | jq -s .)"
default_runtime="$(runtime_default)"
preferred_runtime="$(runtime_preferred)"
if [ -f "$image_version_file" ]; then
image_version="$(tr -d '[:space:]' <"$image_version_file")"
fi
if [ -f "$tooling_version_file" ]; then
tooling_version="$(tr -d '[:space:]' <"$tooling_version_file")"
fi
image_version="${image_version:-unknown}"
tooling_version="${tooling_version:-unknown}"
jq -n \
--arg package_manager "$package_manager" \
--arg tools_home "$(agent_tools_home)" \
--arg tools_bin_dir "$(agent_tools_bin_dir)" \
--argjson packages "$packages_json" \
--argjson requested_packages "$requested_packages_json" \
--argjson apk_repositories "$apk_repositories_json" \
--argjson installed_runtimes "$runtimes_json" \
--argjson installed_features "$features_json" \
--arg default_runtime "$default_runtime" \
--arg preferred_runtime "$preferred_runtime" \
--arg image_version "$image_version" \
--arg tooling_version "$tooling_version" \
'{
package_manager: $package_manager,
packages: $packages,
requested_packages: $requested_packages,
apk_repositories: $apk_repositories,
tools_home: $tools_home,
tools_bin_dir: $tools_bin_dir,
installed_runtimes: $installed_runtimes,
installed_features: $installed_features,
default_runtime: $default_runtime,
preferred_runtime: $preferred_runtime,
image_version: $image_version,
tooling_version: $tooling_version
}'
}
json_recovery_manifest() {
local system_manifest package_details='[]' apt_sources='[]' python_environment='null'
local venv_dir="${AGENTCTL_RECOVERY_VENV_DIR:-/opt/venv}"
local inspect_json='{}' freeze_text='' inspect_file=''
system_manifest="$(json_system_manifest | jq '.apk_repositories |= map(gsub("://[^/@[:space:]]+@"; "://") | split("?")[0])')"
case "$(printf '%s' "$system_manifest" | jq -r '.package_manager')" in
apk)
if command -v apk >/dev/null 2>&1; then
package_details="$(apk query --format json --installed 2>/dev/null || printf '[]')"
fi
;;
dpkg)
if command -v dpkg-query >/dev/null 2>&1; then
package_details="$(dpkg-query -W -f='${binary:Package}\t${Version}\n' 2>/dev/null | jq -R 'split("\t") | {name: .[0], version: .[1]}' | jq -s .)"
fi
if [ -r /etc/apt/sources.list ] || [ -d /etc/apt/sources.list.d ]; then
apt_sources="$({
[ ! -r /etc/apt/sources.list ] || sed -n '/^[[:space:]]*deb[[:space:]]/ { s#//[^/@[:space:]]*@#//#g; s/?[^[:space:]]*$//; p; }' /etc/apt/sources.list
for source_file in /etc/apt/sources.list.d/*.list /etc/apt/sources.list.d/*.sources; do
[ -r "$source_file" ] || continue
# deb822 (.sources) files are deliberately not flattened into a
# sources.list file; they require their structured key material.
case "$source_file" in
*.list) sed -n '/^[[:space:]]*deb[[:space:]]/ { s#//[^/@[:space:]]*@#//#g; s/?[^[:space:]]*$//; p; }' "$source_file" ;;
esac
done
} 2>/dev/null | sort -u | jq -R . | jq -s .)"
fi
;;
esac
if [ -x "$venv_dir/bin/python" ]; then
inspect_json="$("$venv_dir/bin/python" -m pip inspect --local 2>/dev/null || printf '{}')"
freeze_text="$("$venv_dir/bin/python" -m pip freeze --all 2>/dev/null | sed 's#//[^/@[:space:]]*@#//#g; s/?[^[:space:]]*$//' || true)"
inspect_file="$(mktemp)"
printf '%s' "$inspect_json" >"$inspect_file"
python_environment="$(jq -cn \
--arg path "$venv_dir" \
--arg python "$("$venv_dir/bin/python" --version 2>&1 || true)" \
--arg pip "$("$venv_dir/bin/python" -m pip --version 2>&1 || true)" \
--arg freeze "$freeze_text" \
--slurpfile inspect_file "$inspect_file" \
'{path: $path, python: $python, pip: $pip,
packages: [(($inspect_file[0] // {}).installed // [])[] | {
name: (.metadata.name // ""), version: (.metadata.version // ""),
requested: (.requested // false),
direct_url: ((.direct_url.url // null) | if . == null then null else gsub("://[^/@[:space:]]+@"; "://") | split("?")[0] end)
}],
freeze: ($freeze | split("\n") | map(select(length > 0)))}')"
rm -f "$inspect_file"
fi
jq -cn \
--argjson system "$system_manifest" \
--argjson package_details "$package_details" \
--argjson apt_sources "$apt_sources" \
--argjson python_environment "$python_environment" \
--arg architecture "$(uname -m)" \
'{manifest_schema_version: 1, kind: "agentctl-upgrade-recovery-source",
captured_at: (now | todateiso8601), architecture: $architecture,
system: $system, package_details: $package_details, apt_sources: $apt_sources,
python_environment: $python_environment}'
}
state_unique_paths() {
awk 'NF && !seen[$0]++'
}
state_runtime_paths() {
local runtime="$1"
ensure_runtime_known "$runtime"
load_runtime_adapter "$runtime"
if declare -F agent_runtime_state_paths >/dev/null 2>&1; then
agent_runtime_state_paths "$runtime"
fi
return 0
}
state_legacy_paths() {
local codex_home_dir="${HOME}/.codex"
local claude_home_dir="${HOME}/.claude"
local claude_home_state_file="${HOME}/.claude.json"
local path=""
if [ -e "$codex_home_dir" ]; then
while IFS= read -r path; do
[ -n "$path" ] || continue
printf '%s\n' ".codex/$path"
done < <(
cd "$codex_home_dir" && \
find . -mindepth 1 -maxdepth 1 ! -name packages -exec basename {} \;
)
fi
[ -e "$claude_home_dir" ] && printf '%s\n' ".claude"
[ -e "$claude_home_state_file" ] && printf '%s\n' ".claude.json"
return 0
}
state_shell_paths() {
local path=""
for path in \
.profile \
.bashrc \
.bash_profile \
.bash_login \
.bash_logout \
.bash_history \
.zshrc \
.zprofile \
.zlogin \
.zlogout \
.zshenv \
.zsh_history \
.ash_history \
.sh_history; do
[ -e "$HOME/$path" ] && printf '%s\n' "$path"
done
return 0
}
state_export_paths() {
local installed_runtimes=""
state_shell_paths
[ -e "$USER_CONFIG_DIR" ] && printf '%s\n' ".config/agentctl"
installed_runtimes="$(runtime_ids_installed)"
if [ -n "$installed_runtimes" ]; then
printf '%s\n' "$installed_runtimes" | while IFS= read -r runtime; do
[ -n "$runtime" ] || continue
state_runtime_paths "$runtime"
done
else
state_legacy_paths
fi
}
state_import_paths() {
local installed_runtimes=""
printf '%s\n' ".config/agentctl"
installed_runtimes="$(runtime_ids_installed)"
if [ -n "$installed_runtimes" ]; then
printf '%s\n' "$installed_runtimes" | while IFS= read -r runtime; do
[ -n "$runtime" ] || continue
state_runtime_paths "$runtime"
done
else
printf '%s\n' ".codex" ".claude" ".claude.json"
fi
}
state_export() {
local -a paths=()
local path=""
while IFS= read -r path; do
[ -n "$path" ] || continue
paths+=("$path")
done < <(state_export_paths | state_unique_paths)
if [ "${#paths[@]}" -eq 0 ]; then
return 0
fi
tar -C "$HOME" -cf - "${paths[@]}"
}
state_import() {
local import_file=""
local path=""
local packages_backup=""
if [ -t 0 ]; then
return 0
fi
import_file="$(mktemp)"
cat >"$import_file"
if [ ! -s "$import_file" ]; then
rm -f "$import_file"
return 0
fi
if ! tar -tf "$import_file" >/dev/null 2>&1; then
rm -f "$import_file"
die "invalid state import archive"
fi
mkdir -p "$HOME"
packages_backup="$(mktemp -d)"
if [ -e "$HOME/.codex/packages" ]; then
mkdir -p "$packages_backup/.codex"
mv "$HOME/.codex/packages" "$packages_backup/.codex/packages"
fi
while IFS= read -r path; do
[ -n "$path" ] || continue
rm -rf "$HOME/$path"
done < <(state_import_paths | state_unique_paths)
tar -C "$HOME" -xf "$import_file"
rm -rf "$HOME/.codex/packages"
if [ -e "$packages_backup/.codex/packages" ]; then
mkdir -p "$HOME/.codex"
mv "$packages_backup/.codex/packages" "$HOME/.codex/packages"
fi
rm -rf "$packages_backup"
rm -f "$import_file"
}
auth_read() {
local runtime="$1" key="$2"
ensure_runtime_known "$runtime"
load_runtime_adapter "$runtime"
agent_runtime_auth_read "$runtime" "$key"
}
auth_write() {
local runtime="$1" key="$2" value="${3:-}"
ensure_runtime_known "$runtime"
load_runtime_adapter "$runtime"
agent_runtime_auth_write "$runtime" "$key" "$value"
}
auth_login() {
local runtime="$1"
ensure_runtime_installed "$runtime"
load_runtime_adapter "$runtime"
agent_runtime_auth_login "$runtime"
}