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.
66set -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
1168mkdir -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
1470pass () { printf ' PASS %s\n' " $* " ; }
1571fail () { 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
1886SRV=$!
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"
36117EOF
118+ } > " $W /cloud-transport"
37119chmod +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
40123export " ${MODEL_ENV[@]} "
41124unset 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
45128mkdir -p " $REPO " ; cd " $REPO " ; git init -q -b main
46129git 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"
51134SID=$( python3 -c ' import json,sys;print(json.load(open(sys.argv[1]))["session_id"])' " $W /t1.json" )
52135echo " 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
55138echo " == 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"
57140pass " moved"
58141
59142echo " == local copy refuses to run"
143+ REQS_BEFORE=$( wc -l < " $W /model.log" )
60144if " ${RUN[@]} " --resume " $SID " run --json " should be blocked" > " $W /blocked.json" 2> " $W /blocked.err" ; then
61145 fail " local run was allowed after move"
62146fi
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
66154echo " == 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
69161python3 - " $W /model.log" << 'PY ' || fail "cloud context"
70162import json, sys
71163last = [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
74166assert "continue the task" in flat
75167print(" PASS cloud turn saw the full earlier conversation")
76168PY
77- CLOUD " cd $REPO && echo 'cloud edit' > b.txt"
169+ CLOUD " cd $REPO_Q && echo 'cloud edit' > b.txt"
78170
79171echo " == 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
84180echo " == local turn after return"
85181" ${RUN[@]} " --resume " $SID " run --json " back on the laptop" > " $W /t3.json"
0 commit comments