Skip to content

Commit 4091865

Browse files
authored
Merge pull request #377 from Extra-Chill/fix/375-bounded-external-retry
Retry stalled external graph reads
2 parents 554be3d + 8dfe623 commit 4091865

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

lib/opencode-subagents.sh

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
#!/bin/bash
22
# Project Data Machine's persisted portable agent graph for OpenCode.
33

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+
439
opencode_project_subagents() {
540
[ "${DRY_RUN:-false}" = true ] && return 0
641
local runtime has_opencode=false
@@ -29,7 +64,7 @@ opencode_project_subagents() {
2964
slug_payload="$(printf '%s' "$AGENT_SLUG" | base64 | tr -d '\n')"
3065
while [ -z "$expected_size" ] || [ "$offset" -lt "$expected_size" ]; do
3166
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")" || {
3368
warn "Could not read the Agents API subagent graph for coordinator '$AGENT_SLUG'"
3469
return 1
3570
}

tests/external-wordpress-runtime.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ case "$5:$6" in
6464
esac
6565
printf '%s' "$4" | grep -F "\$args=array(base64_decode('" >/dev/null || { echo "wp eval did not initialize reader arguments" >&2; exit 9; }
6666
printf '%s' "$4" | grep -F "'),'embedded');eval('?>'.base64_decode('" >/dev/null || { echo "wp eval did not execute embedded source" >&2; exit 9; }
67+
if [ "${WP_TEST_GRAPH_HANG_ONCE:-}" = 1 ] && [ ! -e "$WP_TEST_GRAPH_HANG_MARKER" ]; then
68+
touch "$WP_TEST_GRAPH_HANG_MARKER"
69+
sleep 10
70+
fi
6771
python3 - "$WP_TEST_GRAPH" "$4" <<'PY'
6872
import base64, json, os, re, sys
6973
value = open(sys.argv[1], "rb").read()
@@ -195,6 +199,13 @@ unset WP_TEST_GRAPH_SIZE_DRIFT
195199
after_drift="$(cksum "$RUNTIME_PROJECT_ROOT/.opencode/agents/writer.md" "$RUNTIME_PROJECT_ROOT/.opencode/.wp-coding-agents-subagents.json")"
196200
[ "$before_drift" = "$after_drift" ] || { echo "FAIL: graph size drift mutated projected files"; exit 1; }
197201

202+
export WP_TEST_GRAPH_HANG_ONCE=1
203+
export WP_TEST_GRAPH_HANG_MARKER="$TMP/graph-hung-once"
204+
export OPENCODE_EXTERNAL_GRAPH_TIMEOUT_SECONDS=1
205+
opencode_project_subagents
206+
unset WP_TEST_GRAPH_HANG_ONCE WP_TEST_GRAPH_HANG_MARKER OPENCODE_EXTERNAL_GRAPH_TIMEOUT_SECONDS
207+
[ -e "$TMP/graph-hung-once" ] || { echo "FAIL: graph retry fixture did not exercise its timeout"; exit 1; }
208+
198209
while IFS= read -r argument; do
199210
case "$argument" in
200211
"--path=$WORDPRESS_PATH"|"--user=$WORDPRESS_USER"|"secret value with spaces") ;;

0 commit comments

Comments
 (0)