|
1 | 1 | #!/bin/bash |
2 | 2 | # Project Data Machine's persisted portable agent graph for OpenCode. |
3 | 3 |
|
| 4 | +opencode_external_graph_eval() { |
| 5 | + local reader_code="$1" |
| 6 | + local timeout_seconds="${OPENCODE_EXTERNAL_GRAPH_TIMEOUT_SECONDS:-120}" |
| 7 | + local response_file pid elapsed status attempt |
| 8 | + case "$timeout_seconds" in |
| 9 | + ''|*[!0-9]*) timeout_seconds=120 ;; |
| 10 | + esac |
| 11 | + [ "$timeout_seconds" -gt 0 ] || timeout_seconds=120 |
| 12 | + response_file="$(mktemp)" || return 1 |
| 13 | + for attempt in 1 2 3; do |
| 14 | + : > "$response_file" |
| 15 | + wp_cmd eval "$reader_code" > "$response_file" 2>/dev/null & |
| 16 | + pid=$! |
| 17 | + elapsed=0 |
| 18 | + while kill -0 "$pid" 2>/dev/null; do |
| 19 | + if [ "$elapsed" -ge "$timeout_seconds" ]; then |
| 20 | + kill "$pid" 2>/dev/null || true |
| 21 | + sleep 1 |
| 22 | + kill -9 "$pid" 2>/dev/null || true |
| 23 | + break |
| 24 | + fi |
| 25 | + sleep 1 |
| 26 | + elapsed=$((elapsed + 1)) |
| 27 | + done |
| 28 | + if wait "$pid" 2>/dev/null; then status=0; else status=$?; fi |
| 29 | + if [ "$status" -eq 0 ]; then |
| 30 | + cat "$response_file" |
| 31 | + rm -f "$response_file" |
| 32 | + return 0 |
| 33 | + fi |
| 34 | + done |
| 35 | + rm -f "$response_file" |
| 36 | + return 1 |
| 37 | +} |
| 38 | + |
4 | 39 | opencode_project_subagents() { |
5 | 40 | [ "${DRY_RUN:-false}" = true ] && return 0 |
6 | 41 | local runtime has_opencode=false |
@@ -29,7 +64,7 @@ opencode_project_subagents() { |
29 | 64 | slug_payload="$(printf '%s' "$AGENT_SLUG" | base64 | tr -d '\n')" |
30 | 65 | while [ -z "$expected_size" ] || [ "$offset" -lt "$expected_size" ]; do |
31 | 66 | reader_code="ob_start();\$args=array(base64_decode('$slug_payload'),'embedded');eval('?>'.base64_decode('$reader_payload'));\$graph=ob_get_clean();echo json_encode(array('size'=>strlen(\$graph),'chunk'=>base64_encode(substr(\$graph,$offset,$chunk_size))));" |
32 | | - response="$(wp_cmd eval "$reader_code" 2>/dev/null)" || { |
| 67 | + response="$(opencode_external_graph_eval "$reader_code")" || { |
33 | 68 | warn "Could not read the Agents API subagent graph for coordinator '$AGENT_SLUG'" |
34 | 69 | return 1 |
35 | 70 | } |
|
0 commit comments