Skip to content

Commit 2db2a40

Browse files
committed
test: isolate repeatable cloud migration e2e runs
1 parent ea1d274 commit 2db2a40

2 files changed

Lines changed: 236 additions & 77 deletions

File tree

‎scripts/test_cloud_move_agent_e2e.sh‎

Lines changed: 130 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,126 @@
44
# run, `cloud return`, and a real local turn afterward. The model is a fake
55
# OpenAI-compatible server that logs what the agent sent.
66
set -euo pipefail
7-
JCODE_BIN=${JCODE_BIN:?}
8-
W=$(mktemp -d "${JCODE_SCRATCH_DIR:-/tmp}/cloud-agent-e2e.XXXXXX")
9-
REPO=/tmp/jcode-cloud-agent-e2e-repo
10-
PORT=${PORT:-18911}
7+
# Re-exec with an allowlisted environment before starting any client or daemon.
8+
# Even a caller-supplied W is only a parent: never reuse or delete an old run.
9+
if [[ ${1:-} != --isolated ]]; then
10+
JCODE_BIN=$(realpath -e "${JCODE_BIN:?set JCODE_BIN to the jcode binary under test}")
11+
SCRIPT=$(realpath -e "$0")
12+
W=$(mktemp -d "${W:-${JCODE_SCRATCH_DIR:-/tmp}}/cloud-test.XXXXXX")
13+
mkdir -m 700 "$W/home" "$W/runtime" "$W/tmp"
14+
EXTRA=()
15+
for name in CLOUD_JCODE_BIN RETURN_JCODE_BIN CLOUD_UPGRADE_TO; do
16+
if [[ -n ${!name:-} ]]; then EXTRA+=("$name=$(realpath -e "${!name}")"); fi
17+
done
18+
exec env -i PATH=/usr/local/bin:/usr/bin:/bin LANG=C.UTF-8 \
19+
HOME="$W/home" TMPDIR="$W/tmp" XDG_RUNTIME_DIR="$W/runtime" JCODE_RUNTIME_DIR="$W/runtime" \
20+
JCODE_TEST_WORKSPACE="$W" W="$W" JCODE_BIN="$JCODE_BIN" \
21+
"${EXTRA[@]}" bash "$SCRIPT" --isolated
22+
fi
23+
REPO=$W/repo
24+
printf -v REPO_Q %q "$REPO"
25+
# Stop only processes carrying this run's unguessable workspace marker. This
26+
# includes detached daemons and provider children, not the user's real server.
27+
cleanup() {
28+
local status=$?
29+
trap - EXIT INT TERM
30+
env -u JCODE_TEST_WORKSPACE timeout --kill-after=2s 12s python3 - "$W" "$$" <<'PY' || status=1
31+
import os, signal, sys, time
32+
marker = b'JCODE_TEST_WORKSPACE=' + os.fsencode(sys.argv[1])
33+
exclude = {os.getpid(), int(sys.argv[2])}
34+
def owned():
35+
found = []
36+
for name in os.listdir('/proc'):
37+
if not name.isdigit() or int(name) in exclude:
38+
continue
39+
try:
40+
if marker in open('/proc/' + name + '/environ', 'rb').read().split(b'\0'):
41+
found.append(int(name))
42+
except (OSError, ProcessLookupError):
43+
pass
44+
return found
45+
def stop(sig):
46+
for pid in owned():
47+
try: os.kill(pid, sig)
48+
except ProcessLookupError: pass
49+
stop(signal.SIGTERM)
50+
deadline = time.monotonic() + 5
51+
while owned() and time.monotonic() < deadline:
52+
time.sleep(.1)
53+
stop(signal.SIGKILL)
54+
deadline = time.monotonic() + 2
55+
while owned() and time.monotonic() < deadline:
56+
time.sleep(.1)
57+
remaining = owned()
58+
if remaining:
59+
print('Cleanup failed, surviving test processes:', remaining, file=sys.stderr)
60+
sys.exit(1)
61+
PY
62+
if (( status != 0 )); then echo "FAILED: artifacts retained at $W" >&2; fi
63+
exit "$status"
64+
}
65+
trap cleanup EXIT
66+
trap 'exit 130' INT
67+
trap 'exit 143' TERM
1168
mkdir -p "$W/local-home" "$W/cloud-fs/home" "$W/cloud-fs/repo"
12-
if [ -e "$REPO" ]; then rm -rf -- "/tmp/jcode-cloud-agent-e2e-repo"; fi
1369

1470
pass() { printf ' PASS %s\n' "$*"; }
1571
fail() { printf ' FAIL %s\n' "$*"; exit 1; }
1672

17-
FAKE_LOG=$W/model.log FAKE_LABEL=model python3 "$(dirname "$0")/fake_openai_server.py" "$PORT" &
73+
# Bind port zero inside the real fake server, then publish the selected port.
74+
# This avoids the pick-a-free-port / close / rebind race in concurrent runs.
75+
FAKE_LOG=$W/model.log FAKE_LABEL=model python3 - "$(dirname "$0")/fake_openai_server.py" "$W/port" > "$W/provider.log" 2>&1 <<'PY' &
76+
import http.server, pathlib, runpy, sys
77+
script, port_file = sys.argv[1:]
78+
original = http.server.HTTPServer.server_activate
79+
def activate(server):
80+
original(server)
81+
pathlib.Path(port_file).write_text(str(server.server_address[1]))
82+
http.server.HTTPServer.server_activate = activate
83+
sys.argv = [script, '0']
84+
runpy.run_path(script, run_name='__main__')
85+
PY
1886
SRV=$!
19-
trap 'kill $SRV 2>/dev/null || true' EXIT
20-
sleep 1
87+
for ((attempt=0; attempt<100; attempt++)); do
88+
[[ -s "$W/port" ]] && break
89+
kill -0 "$SRV" 2>/dev/null || { cat "$W/provider.log"; fail "provider exited during startup"; }
90+
sleep .1
91+
done
92+
[[ -s "$W/port" ]] || fail "provider startup timed out"
93+
PORT=$(cat "$W/port")
2194

22-
MODEL_ENV=(JCODE_OPENAI_COMPAT_API_BASE=http://127.0.0.1:$PORT/v1 OPENAI_COMPAT_API_KEY=x JCODE_OPENAI_COMPAT_DEFAULT_MODEL=fake-model JCODE_NO_TELEMETRY=1 JCODE_NO_AUTO_UPDATE=1)
95+
MODEL_ENV=("JCODE_OPENAI_COMPAT_API_BASE=http://127.0.0.1:$PORT/v1" OPENAI_COMPAT_API_KEY=x JCODE_OPENAI_COMPAT_DEFAULT_MODEL=fake-model JCODE_NO_TELEMETRY=1 JCODE_NO_AUTO_UPDATE=1)
2396

24-
cat > "$W/cloud-transport" <<EOF
25-
#!/usr/bin/env bash
26-
exec bwrap --die-with-parent \\
27-
--ro-bind /usr /usr --ro-bind /etc /etc --symlink usr/lib /lib --symlink usr/lib64 /lib64 \\
28-
--symlink usr/bin /bin --symlink usr/bin /sbin --proc /proc --dev /dev --tmpfs /tmp \\
29-
--bind "$W/cloud-fs/home" /home/cloud --bind "$W/cloud-fs/repo" $REPO \\
30-
--ro-bind "$JCODE_BIN" /opt/jcode/jcode \\
31-
--clearenv --setenv HOME /home/cloud --setenv PATH /opt/jcode:/usr/bin \\
32-
--setenv JCODE_HOME /home/cloud/.jcode --setenv JCODE_CLOUD_HOST_LABEL cloud-e2e \\
33-
$(printf -- '--setenv %s %s ' $(printf '%s\n' "${MODEL_ENV[@]}" | tr '=' ' ')) \\
34-
--unshare-pid --unshare-ipc --unshare-uts --hostname cloud-e2e \\
35-
bash -c "\$1"
97+
{
98+
printf '#!/usr/bin/env bash\n'
99+
printf 'CLOUD_HOME=%q\nCLOUD_REPO=%q\nREPO=%q\nBINARY=%q\nWORKSPACE=%q\n' \
100+
"$W/cloud-fs/home" "$W/cloud-fs/repo" "$REPO" "$JCODE_BIN" "$W"
101+
printf 'MODEL_PORT=%q\n' "$PORT"
102+
cat <<'EOF'
103+
exec bwrap --die-with-parent \
104+
--ro-bind /usr /usr --ro-bind /etc /etc --symlink usr/lib /lib --symlink usr/lib64 /lib64 \
105+
--symlink usr/bin /bin --symlink usr/bin /sbin --proc /proc --dev /dev --tmpfs /tmp \
106+
--bind "$CLOUD_HOME" /home/cloud --bind "$CLOUD_REPO" "$REPO" \
107+
--ro-bind "$BINARY" /opt/jcode/jcode \
108+
--clearenv --setenv HOME /home/cloud --setenv USER cloud --setenv PATH /opt/jcode:/usr/local/bin:/usr/bin \
109+
--setenv JCODE_HOME /home/cloud/.jcode --setenv JCODE_CLOUD_HOST_LABEL cloud-e2e \
110+
--setenv JCODE_RUNTIME_DIR /home/cloud/runtime --setenv XDG_RUNTIME_DIR /home/cloud/runtime \
111+
--setenv JCODE_TEST_WORKSPACE "$WORKSPACE" \
112+
--setenv JCODE_NO_AUTO_UPDATE 1 --setenv JCODE_NON_INTERACTIVE 1 --setenv JCODE_NO_TELEMETRY 1 \
113+
--setenv JCODE_OPENAI_COMPAT_API_BASE "http://127.0.0.1:$MODEL_PORT/v1" \
114+
--setenv OPENAI_COMPAT_API_KEY x --setenv JCODE_OPENAI_COMPAT_DEFAULT_MODEL fake-model \
115+
--unshare-pid --unshare-ipc --unshare-uts --hostname cloud-e2e \
116+
bash -c "$1"
36117
EOF
118+
} > "$W/cloud-transport"
37119
chmod +x "$W/cloud-transport"
38120

39-
export JCODE_HOME=$W/local-home JCODE_CLOUD_TRANSPORT=$W/cloud-transport JCODE_CLOUD_HOST_LABEL=laptop-e2e
121+
export PATH="$W:$PATH"
122+
export JCODE_HOME=$W/local-home JCODE_CLOUD_TRANSPORT=cloud-transport JCODE_CLOUD_HOST_LABEL=laptop-e2e
40123
export "${MODEL_ENV[@]}"
41124
unset JCODE_SESSION_ID JCODE_SOCKET
42-
RUN=("$JCODE_BIN" --no-update --no-selfdev --provider openai-compatible --model fake-model)
43-
CLOUD() { "$W/cloud-transport" "$1"; }
125+
RUN=(timeout --kill-after=5s 90s "$JCODE_BIN" --no-update --no-selfdev --provider openai-compatible --model fake-model)
126+
CLOUD() { timeout --kill-after=5s 90s "$W/cloud-transport" "$1"; }
44127

45128
mkdir -p "$REPO"; cd "$REPO"; git init -q -b main
46129
git config user.name T; git config user.email [email protected]
@@ -50,22 +133,31 @@ echo "== local turn"
50133
"${RUN[@]}" run --json "turn one on the laptop" > "$W/t1.json"
51134
SID=$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["session_id"])' "$W/t1.json")
52135
echo " session $SID"
53-
grep -q '\[model\] ack' "$W/t1.json" && pass "local turn produced a reply" || fail "local turn"
136+
if grep -q '\[model\] ack' "$W/t1.json"; then pass "local turn produced a reply"; else fail "local turn"; fi
54137

55138
echo "== move"
56-
"$JCODE_BIN" --no-update --no-selfdev cloud move --session "$SID" --host cloud-e2e --remote-binary /opt/jcode/jcode --json > "$W/move.json"
139+
timeout --kill-after=5s 90s "$JCODE_BIN" --no-update --no-selfdev cloud move --session "$SID" --host cloud-e2e --remote-binary /opt/jcode/jcode --json > "$W/move.json"
57140
pass "moved"
58141

59142
echo "== local copy refuses to run"
143+
REQS_BEFORE=$(wc -l < "$W/model.log")
60144
if "${RUN[@]}" --resume "$SID" run --json "should be blocked" > "$W/blocked.json" 2> "$W/blocked.err"; then
61145
fail "local run was allowed after move"
62146
fi
63-
grep -q "moved to \`cloud-e2e\`" "$W/blocked.err" && pass "local run blocked with a clear message" || { cat "$W/blocked.err"; fail "block message"; }
64-
REQS_BEFORE=$(wc -l < "$W/model.log")
147+
if grep -q "moved to \`cloud-e2e\`" "$W/blocked.err"; then
148+
pass "local run blocked with a clear message"
149+
else
150+
cat "$W/blocked.err"; fail "block message"
151+
fi
152+
[[ $(wc -l < "$W/model.log") == "$REQS_BEFORE" ]] || fail "blocked run contacted provider"
65153

66154
echo "== cloud turn (continues the same conversation)"
67-
CLOUD "cd $REPO && jcode --no-update --no-selfdev --provider openai-compatible --model fake-model --resume $SID run --json 'continue the task'" > "$W/t2.json"
68-
grep -q 'saw_cloud_notice=True' "$W/t2.json" && pass "cloud model saw the migration notice" || { cat "$W/t2.json"; fail "cloud notice"; }
155+
CLOUD "cd $REPO_Q && jcode --no-update --no-selfdev --provider openai-compatible --model fake-model --resume $SID run --json 'continue the task'" > "$W/t2.json"
156+
if grep -q 'saw_cloud_notice=True' "$W/t2.json"; then
157+
pass "cloud model saw the migration notice"
158+
else
159+
cat "$W/t2.json"; fail "cloud notice"
160+
fi
69161
python3 - "$W/model.log" <<'PY' || fail "cloud context"
70162
import json, sys
71163
last = [json.loads(l) for l in open(sys.argv[1])][-1]
@@ -74,12 +166,16 @@ assert "turn one on the laptop" in flat, "earlier turn missing from cloud contex
74166
assert "continue the task" in flat
75167
print(" PASS cloud turn saw the full earlier conversation")
76168
PY
77-
CLOUD "cd $REPO && echo 'cloud edit' > b.txt"
169+
CLOUD "cd $REPO_Q && echo 'cloud edit' > b.txt"
78170

79171
echo "== return"
80-
"$JCODE_BIN" --no-update --no-selfdev cloud return --session "$SID" --json > "$W/ret.json"
81-
grep -q '"result": "applied"' "$W/ret.json" && pass "cloud edit merged back" || { cat "$W/ret.json"; fail "merge"; }
82-
test -f "$REPO/b.txt" && pass "b.txt present locally" || fail "b.txt"
172+
timeout --kill-after=5s 90s "$JCODE_BIN" --no-update --no-selfdev cloud return --session "$SID" --json > "$W/ret.json"
173+
if grep -q '"result": "applied"' "$W/ret.json"; then
174+
pass "cloud edit merged back"
175+
else
176+
cat "$W/ret.json"; fail "merge"
177+
fi
178+
if test -f "$REPO/b.txt"; then pass "b.txt present locally"; else fail "b.txt"; fi
83179

84180
echo "== local turn after return"
85181
"${RUN[@]}" --resume "$SID" run --json "back on the laptop" > "$W/t3.json"

0 commit comments

Comments
 (0)