Repository navigation
Expand file tree
/
Copy pathe2e_hosted_connect.sh
More file actions
executable file
·342 lines (313 loc) · 13.1 KB
/
Copy pathe2e_hosted_connect.sh
File metadata and controls
executable file
·342 lines (313 loc) · 13.1 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
#!/usr/bin/env bash
set -euo pipefail
# Real-process hosted-connect smoke. Everything is loopback-only and all
# configuration/state lives below one temporary directory removed by the trap.
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$REPO_DIR"
TIMEOUT_SECS="${E2E_TIMEOUT_SECS:-180}"
CARGO_BIN="${CARGO_BIN:-cargo}"
TMP_ROOT=""
SERVER_PID=""
PROFILE=""
STARTED_AT="$(date +%s)"
log() {
printf '[hosted-connect-e2e] %s\n' "$*"
}
die() {
printf '[hosted-connect-e2e][FAIL] %s\n' "$*" >&2
exit 1
}
cleanup() {
trap - EXIT INT TERM
if [ -n "$TMP_ROOT" ] && [ -n "$PROFILE" ] && [ -x "$REPO_DIR/target/debug/webcodex" ]; then
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" runner stop --profile "$PROFILE" >/dev/null 2>&1 || true
fi
if [ -n "$SERVER_PID" ] && kill -0 "$SERVER_PID" 2>/dev/null; then
kill "$SERVER_PID" 2>/dev/null || true
wait "$SERVER_PID" 2>/dev/null || true
fi
if [ -n "$TMP_ROOT" ]; then
case "$TMP_ROOT" in
/tmp/webcodex-hosted-connect-e2e.*)
rm -rf -- "$TMP_ROOT"
;;
*)
printf '[hosted-connect-e2e][WARN] refusing unexpected cleanup path: %s\n' "$TMP_ROOT" >&2
;;
esac
fi
}
trap cleanup EXIT INT TERM
deadline_ok() {
[ "$(( $(date +%s) - STARTED_AT ))" -lt "$TIMEOUT_SECS" ]
}
process_active() {
local pid="$1"
local state
kill -0 "$pid" 2>/dev/null || return 1
state="$(ps -p "$pid" -o stat= 2>/dev/null | tr -d '[:space:]')"
case "$state" in
""|Z*) return 1 ;;
*) return 0 ;;
esac
}
random_secret() {
python3 -c 'import secrets; print(secrets.token_urlsafe(32))'
}
free_port() {
python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1]); s.close()'
}
json_field() {
python3 -c '
import json, sys
value = json.load(sys.stdin)
for component in sys.argv[1].split("."):
if component:
value = value[int(component)] if isinstance(value, list) else value.get(component)
if value is None:
break
if isinstance(value, (dict, list)):
print(json.dumps(value, separators=(",", ":")))
elif value is not None:
print(value)
' "$1"
}
post() {
local token="$1"
local path="$2"
local body="${3:-}"
if [ -z "$body" ]; then
body='{}'
fi
curl --silent --show-error --max-time 10 \
-H "Authorization: Bearer ${token}" \
-H "Content-Type: application/json" \
-X POST "http://127.0.0.1:${PORT}${path}" \
-d "$body"
}
if ! command -v curl >/dev/null || ! command -v python3 >/dev/null \
|| ! command -v git >/dev/null || ! command -v timeout >/dev/null; then
die "curl, python3, git, and timeout are required"
fi
log "building Server, Runner, and CLI binaries"
"$CARGO_BIN" build --quiet \
-p webcodex --bin webcodex-server \
-p webcodex-runner --bin webcodex-runner \
-p webcodex-cli --bin webcodex
PORT="$(free_port)"
BOOTSTRAP_KEY="$(random_secret)"
SHARED_KEY_A="hosted-a-$(random_secret)"
SHARED_KEY_B="hosted-b-$(random_secret)"
TMP_ROOT="$(mktemp -d /tmp/webcodex-hosted-connect-e2e.XXXXXX)"
mkdir -p "$TMP_ROOT/data" "$TMP_ROOT/project" "$TMP_ROOT/second-project" "$TMP_ROOT/home"
(
cd "$TMP_ROOT/project"
git init -q -b main
git config user.email e2e@example.invalid
git config user.name "WebCodex E2E"
printf '# hosted connect smoke\n' > README.md
git add README.md
git commit -q -m init
)
printf '# second hosted project\n' >"$TMP_ROOT/second-project/README.md"
log "exercising the real bounded hosted log writer"
ROTATION_PROFILE="rotation-e2e"
ROTATION_STATE="$TMP_ROOT/state/webcodex/clients/$ROTATION_PROFILE"
mkdir -p "$ROTATION_STATE"
printf 'profile = "%s"\n' "$ROTATION_PROFILE" >"$ROTATION_STATE/hosted-connect"
python3 -c '
import sys
out = sys.stdout.buffer
for start in range(0, 350000, 1000):
out.write(b"".join(
f"{line:06d} ".encode() + b"x" * 96 + b"\n"
for line in range(start, min(start + 1000, 350000))
))
' | timeout 45 "$REPO_DIR/target/debug/webcodex" \
__hosted-log-writer "$ROTATION_STATE"
for log_file in runner.log runner.log.1 runner.log.2; do
[ -f "$ROTATION_STATE/$log_file" ] \
|| die "runtime rotation did not create $log_file"
[ "$(stat -c '%a' "$ROTATION_STATE/$log_file")" = "600" ] \
|| die "$log_file is not mode 0600"
[ "$(stat -c '%s' "$ROTATION_STATE/$log_file")" -le 10485760 ] \
|| die "$log_file exceeded the 10 MiB bound"
done
ROTATION_TOTAL="$(du -cb "$ROTATION_STATE"/runner.log* | tail -1 | awk '{print $1}')"
[ "$ROTATION_TOTAL" -le 31457280 ] \
|| die "runtime log rotation exceeded the three-file bound"
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" runner logs --profile "$ROTATION_PROFILE" --lines 100 \
>"$TMP_ROOT/rotation-tail.out"
[ "$(head -1 "$TMP_ROOT/rotation-tail.out" | cut -d' ' -f1)" = "349900" ] \
|| die "bounded runner logs did not return the expected first tail line"
[ "$(tail -1 "$TMP_ROOT/rotation-tail.out" | cut -d' ' -f1)" = "349999" ] \
|| die "bounded runner logs did not return the expected final tail line"
log "starting temporary shared-key-enabled Server"
WEBCODEX_ADDR="127.0.0.1:${PORT}" \
WEBCODEX_DATA="$TMP_ROOT/data" \
WEBCODEX_TOKEN="$BOOTSTRAP_KEY" \
WEBCODEX_SHARED_KEY_ENABLED=true \
RUST_LOG=warn \
"$REPO_DIR/target/debug/webcodex-server" >"$TMP_ROOT/server.log" 2>&1 &
SERVER_PID=$!
for _ in $(seq 1 80); do
deadline_ok || die "Server readiness exceeded the test deadline"
if post "$BOOTSTRAP_KEY" /api/runtime/status '{}' >/dev/null 2>&1; then
break
fi
sleep 0.25
done
post "$BOOTSTRAP_KEY" /api/runtime/status '{}' >/dev/null \
|| die "Server did not become ready"
log "running the real one-command connection"
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" connect "http://127.0.0.1:${PORT}" \
--key "$SHARED_KEY_A" \
--project "$TMP_ROOT/project" \
>"$TMP_ROOT/connect-first.out"
PROFILE="$(awk -F': *' '$1 == "Profile" {print $2}' "$TMP_ROOT/connect-first.out")"
CLIENT_ID="$(awk -F': *' '$1 == "Client" {print $2}' "$TMP_ROOT/connect-first.out")"
RUNTIME_PROJECT="$(awk '$0 ~ /^Runtime project:/ {sub(/^Runtime project:[[:space:]]*/, ""); print}' "$TMP_ROOT/connect-first.out")"
[ -n "$PROFILE" ] && [ -n "$CLIENT_ID" ] && [ -n "$RUNTIME_PROJECT" ] \
|| die "connect output did not include profile, client, and project"
[ "$RUNTIME_PROJECT" = "agent:${CLIENT_ID}:project" ] \
|| die "connect returned an unexpected runtime project id"
PROFILE_DIR="$TMP_ROOT/config/webcodex/clients/$PROFILE"
STATE_DIR="$TMP_ROOT/state/webcodex/clients/$PROFILE"
[ "$(stat -c '%a' "$PROFILE_DIR/runner.toml")" = "600" ] \
|| die "runner.toml is not mode 0600"
[ "$(stat -c '%a' "$STATE_DIR/runner.toml")" = "600" ] \
|| die "runner state is not mode 0600"
[ "$(stat -c '%a' "$STATE_DIR/runner.log")" = "600" ] \
|| die "runner log is not mode 0600"
[ -f "$PROFILE_DIR/project-registry/project.toml" ] \
|| die "connect did not register the project locally"
[ -f "$STATE_DIR/runner.toml" ] && [ -f "$STATE_DIR/runner.log" ] \
|| die "connect did not persist Runner state and logs"
FIRST_PID="$(python3 -c 'import sys,tomllib; print(tomllib.load(open(sys.argv[1],"rb"))["pid"])' "$STATE_DIR/runner.toml")"
LOG_WRITER_PID="$(python3 -c 'import sys,tomllib; print(tomllib.load(open(sys.argv[1],"rb"))["log_writer"]["pid"])' "$STATE_DIR/runner.toml")"
kill -0 "$FIRST_PID" 2>/dev/null || die "Runner did not survive connect command exit"
process_active "$LOG_WRITER_PID" \
|| die "hosted log writer did not survive connect command exit"
[ ! -e "$PROFILE_DIR/.hosted-key-disclosed" ] \
|| die "an explicitly supplied key created a disclosure marker"
PROJECTS_A="$(post "$SHARED_KEY_A" /api/tools/call '{"tool":"list_projects","params":{}}')"
[ "$(printf '%s' "$PROJECTS_A" | json_field output.projects.0.id)" = "$RUNTIME_PROJECT" ] \
|| die "same-key project visibility failed"
READ_RESPONSE="$(post "$SHARED_KEY_A" /api/tools/call \
"{\"tool\":\"read_files\",\"params\":{\"project\":\"${RUNTIME_PROJECT}\",\"items\":[{\"path\":\"README.md\"}]}}")"
if [ "$(printf '%s' "$READ_RESPONSE" | json_field success)" != "True" ]; then
READ_ERROR="$(printf '%s' "$READ_RESPONSE" | python3 -c '
import json,sys
value=json.load(sys.stdin)
print(json.dumps({
"success": value.get("success"),
"error": value.get("error"),
"output": value.get("output"),
}, separators=(",",":")))
')"
die "same-key read_files failed (${READ_ERROR})"
fi
printf '%s' "$READ_RESPONSE" | grep -q 'hosted connect smoke' \
|| die "same-key read_files returned unexpected content"
PROJECTS_B="$(post "$SHARED_KEY_B" /api/tools/call '{"tool":"list_projects","params":{}}')"
[ "$(printf '%s' "$PROJECTS_B" | json_field output.count)" = "0" ] \
|| die "a different key discovered the connected project"
log "verifying client-id collision failure cleans up the rejected Runner"
set +e
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" connect "http://127.0.0.1:${PORT}" \
--key "$SHARED_KEY_B" \
--client-id "$CLIENT_ID" \
--project "$TMP_ROOT/project" \
>"$TMP_ROOT/connect-collision.out" 2>"$TMP_ROOT/connect-collision.err"
COLLISION_STATUS=$?
set -e
[ "$COLLISION_STATUS" -ne 0 ] || die "cross-key client-id collision unexpectedly succeeded"
grep -q 'Runner logs:' "$TMP_ROOT/connect-collision.err" \
|| die "collision failure did not report the Runner log path"
ACTIVE_STATE_COUNT="$(find "$TMP_ROOT/state/webcodex/clients" -name runner.toml -type f | wc -l)"
[ "$ACTIVE_STATE_COUNT" = "1" ] \
|| die "collision failure left an extra active Runner state"
log "re-running connect to verify profile, identity, and process reuse"
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" connect "http://127.0.0.1:${PORT}" \
--key "$SHARED_KEY_A" \
--project "$TMP_ROOT/project" \
>"$TMP_ROOT/connect-second.out"
SECOND_PID="$(python3 -c 'import sys,tomllib; print(tomllib.load(open(sys.argv[1],"rb"))["pid"])' "$STATE_DIR/runner.toml")"
[ "$FIRST_PID" = "$SECOND_PID" ] || die "repeated connect started a duplicate Runner"
[ "$(awk -F': *' '$1 == "Profile" {print $2}' "$TMP_ROOT/connect-second.out")" = "$PROFILE" ] \
|| die "repeated connect changed the profile"
log "adding a second project without replacing the first"
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" connect "http://127.0.0.1:${PORT}" \
--key "$SHARED_KEY_A" \
--project "$TMP_ROOT/second-project" \
>"$TMP_ROOT/connect-third.out"
PROJECTS_TWO="$(post "$SHARED_KEY_A" /api/tools/call '{"tool":"list_projects","params":{}}')"
printf '%s' "$PROJECTS_TWO" | python3 -c '
import json,sys
projects=json.load(sys.stdin).get("output",{}).get("projects",[])
ids={project.get("id") for project in projects}
expected=set(sys.argv[1:])
raise SystemExit(0 if ids == expected else 1)
' "$RUNTIME_PROJECT" "agent:${CLIENT_ID}:second-project" \
|| die "adding a second project replaced or duplicated project registration"
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" runner status --profile "$PROFILE" \
>"$TMP_ROOT/status.out"
grep -q 'runner mode:.*hosted local process' "$TMP_ROOT/status.out" \
|| die "runner status did not recognize the hosted Runner"
grep -q 'client online:.*yes' "$TMP_ROOT/status.out" \
|| die "runner status did not confirm Server visibility"
for safe_output in "$TMP_ROOT/connect-first.out" "$TMP_ROOT/connect-second.out" \
"$TMP_ROOT/connect-third.out" "$TMP_ROOT/status.out" \
"$STATE_DIR"/runner.log* "$STATE_DIR/runner.toml"; do
if grep -F "$SHARED_KEY_A" "$safe_output" >/dev/null 2>&1; then
die "shared key leaked into $(basename "$safe_output")"
fi
done
[ -z "$(git -C "$TMP_ROOT/project" status --porcelain)" ] \
|| die "connect modified the user project"
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" runner stop --profile "$PROFILE" \
>"$TMP_ROOT/stop.out"
[ ! -f "$STATE_DIR/runner.toml" ] || die "runner stop left active Runner state"
for _ in $(seq 1 40); do
if ! process_active "$LOG_WRITER_PID"; then
break
fi
sleep 0.05
done
if process_active "$LOG_WRITER_PID"; then
die "runner stop left the hosted log writer running"
fi
HOME="$TMP_ROOT/home" \
XDG_CONFIG_HOME="$TMP_ROOT/config" \
XDG_STATE_HOME="$TMP_ROOT/state" \
"$REPO_DIR/target/debug/webcodex" runner status --profile "$PROFILE" \
>"$TMP_ROOT/status-stopped.out"
grep -q 'runner active:.*false' "$TMP_ROOT/status-stopped.out" \
|| die "runner status did not report the stopped Runner"
PROFILE=""
log "PASS"