{{ message }}
Repository navigation
Expand file tree
/
Copy pathonboarding_sandbox.sh
More file actions
executable file
·398 lines (368 loc) · 11.4 KB
/
Copy pathonboarding_sandbox.sh
File metadata and controls
executable file
·398 lines (368 loc) · 11.4 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env bash
set -euo pipefail
umask 077
repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
command=${1:-help}
if [[ $# -gt 0 ]]; then
shift
fi
sandbox_name=${JCODE_ONBOARDING_SANDBOX:-default}
if [[ ! "$sandbox_name" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ || "$sandbox_name" == "." || "$sandbox_name" == ".." ]]; then
echo "Invalid onboarding sandbox name: $sandbox_name" >&2
exit 2
fi
scratch_root=${JCODE_SCRATCH_DIR:-$HOME/.jcode/scratch}
sandbox_parent="$scratch_root/onboarding"
sandbox_root_default="$sandbox_parent/$sandbox_name"
sandbox_root=${JCODE_ONBOARDING_DIR:-$sandbox_root_default}
jcode_home="$sandbox_root/home"
runtime_dir="$sandbox_root/runtime"
marker_file="$sandbox_root/.jcode-onboarding-sandbox"
ensure_dirs() {
mkdir -p "$jcode_home" "$runtime_dir"
chmod 700 "$sandbox_root" "$jcode_home" "$runtime_dir"
: > "$marker_file"
chmod 600 "$marker_file"
}
run_in_sandbox() {
ensure_dirs
(
cd "$repo_root"
# Strip any inherited self-dev markers so the sandbox behaves like a real
# first-run install. `--no-selfdev` only prevents *setting*
# JCODE_CLIENT_SELFDEV_MODE; it cannot unset one inherited from a parent
# self-dev shell, which would otherwise force every sandbox session canary
# (suppressing the new-session suggestion cards we are trying to verify).
env -u JCODE_CLIENT_SELFDEV_MODE -u JCODE_SELFDEV -u JCODE_CANARY \
JCODE_HOME="$jcode_home" \
JCODE_RUNTIME_DIR="$runtime_dir" \
"$@"
)
}
print_usage() {
cat <<EOF
Usage: $(basename "$0") <command> [args...]
Commands:
env Print the sandbox environment exports
status Show sandbox paths and current contents
reset Delete the sandbox entirely
purge-external Delete copied real credentials/transcripts only
shell Open a clean shell with sandbox env vars set
jcode [args...] Run jcode inside the sandbox
auth-status Run 'jcode auth status' inside the sandbox
fresh [args...] Reset sandbox, then launch jcode with args
seed-real-logins [--with-transcripts|--transcripts-only]
Copy your REAL external logins (Codex/Claude/Gemini/
Copilot/Cursor/OpenCode/pi) into the sandbox so the
onboarding import step can import them. Add
--with-transcripts to also copy Codex/Claude
transcripts (so "continue where you left off" has data).
Originals are never modified.
fresh-real [--with-transcripts]
Reset sandbox, seed your real logins, then launch jcode
login <provider> ... Run 'jcode --provider <provider> login ...' in sandbox
fixture-list List saved local auth fixtures
fixture-save <name> Save current sandbox auth state as a local fixture
fixture-load <name> Load a saved auth fixture into this sandbox
fixture-run <name> -- [args...]
Load a fixture, then run jcode with args
help Show this help
Environment overrides:
JCODE_ONBOARDING_SANDBOX Sandbox name (default: default)
JCODE_ONBOARDING_DIR Explicit sandbox directory
JCODE_AUTH_FIXTURE_DIR Fixture store (default: .tmp/auth-fixtures)
JCODE_ONBOARDING_KEEP_EXTERNAL=1
Keep copied real credentials after fresh-real exits
Examples:
$(basename "$0") fresh
$(basename "$0") login openai
$(basename "$0") fixture-save normal-openai
$(basename "$0") fixture-load normal-openai
$(basename "$0") auth-status
EOF
}
print_env() {
ensure_dirs
cat <<EOF
export JCODE_HOME="$jcode_home"
export JCODE_RUNTIME_DIR="$runtime_dir"
EOF
}
status() {
ensure_dirs
echo "Sandbox name: $sandbox_name"
echo "Sandbox root: $sandbox_root"
echo "JCODE_HOME: $jcode_home"
echo "RUNTIME_DIR: $runtime_dir"
echo
if [[ -d "$jcode_home" ]]; then
echo "Home contents:"
find "$jcode_home" -maxdepth 3 \( -type f -o -type d \) | sed "s#^$sandbox_root#.#" | sort
fi
if [[ -d "$jcode_home/external" ]]; then
echo
echo "WARNING: this sandbox contains copies of real credentials or transcripts."
echo "Remove them with: $(basename "$0") purge-external"
fi
}
reset() {
if [[ ! -e "$sandbox_root" ]]; then
echo "Onboarding sandbox is already absent: $sandbox_root"
return
fi
case "$sandbox_root" in
""|/|"$HOME"|"$repo_root")
echo "Refusing to delete unsafe sandbox path: $sandbox_root" >&2
return 1
;;
esac
if [[ ! -f "$marker_file" && "$sandbox_root" != "$sandbox_root_default" ]]; then
echo "Refusing to delete unmarked custom sandbox: $sandbox_root" >&2
return 1
fi
rm -rf "$sandbox_root"
echo "Removed onboarding sandbox: $sandbox_root"
}
purge_external() {
if [[ -d "$jcode_home/external" ]]; then
rm -rf "$jcode_home/external"
echo "Removed copied external credentials/transcripts: $jcode_home/external"
else
echo "No copied external credentials/transcripts found."
fi
}
open_shell() {
ensure_dirs
echo "Opening sandbox shell"
echo " JCODE_HOME=$jcode_home"
echo " JCODE_RUNTIME_DIR=$runtime_dir"
env JCODE_HOME="$jcode_home" JCODE_RUNTIME_DIR="$runtime_dir" bash --noprofile --norc
}
run_jcode() {
# The sandbox should behave like a real standalone install, not a self-dev
# client. Because we launch from inside the repo, jcode would otherwise
# auto-detect the repository and join the shared self-dev server (remote
# mode), which both breaks isolation and skips local-only first-run behavior
# like the new-session model validation. `--no-selfdev` keeps it standalone,
# spawning its own server under the sandbox's JCODE_RUNTIME_DIR. Set
# JCODE_SANDBOX_SELFDEV=1 to opt back into the shared-server behavior.
local prefix=()
if [[ "${JCODE_SANDBOX_SELFDEV:-0}" != "1" ]]; then
prefix=(--no-selfdev)
fi
# Allow pointing the sandbox at an already-built binary (e.g. the selfdev
# profile output) without rebuilding the debug binary. Falls back to the
# debug binary, then to `cargo run`.
if [[ -n "${JCODE_SANDBOX_BIN:-}" ]]; then
if [[ -x "$JCODE_SANDBOX_BIN" ]]; then
run_in_sandbox "$JCODE_SANDBOX_BIN" "${prefix[@]}" "$@"
return
fi
echo "JCODE_SANDBOX_BIN=$JCODE_SANDBOX_BIN is not executable" >&2
return 1
fi
local binary_path="$repo_root/target/debug/jcode"
if [[ -x "$binary_path" ]]; then
run_in_sandbox "$binary_path" "${prefix[@]}" "$@"
else
run_in_sandbox cargo run --bin jcode -- "${prefix[@]}" "$@"
fi
}
run_auth_fixture() {
JCODE_ONBOARDING_SANDBOX="$sandbox_name" \
JCODE_ONBOARDING_DIR="$sandbox_root" \
"$repo_root/scripts/auth_fixture.sh" "$@"
}
# Copy one real file from $HOME into the sandbox's external/ tree, preserving its
# relative path. jcode resolves every external credential/transcript lookup to
# $JCODE_HOME/external/<same-relative-path-as-$HOME> when JCODE_HOME is set, so
# seeding here makes your real logins/transcripts visible to the onboarding
# import + continue steps. Copies (never symlinks: jcode rejects symlinked auth
# files) and never touches the originals.
seed_one_file() {
local rel=$1
local src="$HOME/$rel"
local dst="$jcode_home/external/$rel"
if [[ ! -e "$src" ]]; then
return 1
fi
mkdir -p "$(dirname "$dst")"
cp -a "$src" "$dst"
chmod -R go-rwx "$jcode_home/external" 2>/dev/null || true
return 0
}
# Copy a real directory subtree (e.g. transcript stores) into external/.
seed_one_dir() {
local rel=$1
local src="$HOME/$rel"
local dst="$jcode_home/external/$rel"
if [[ ! -d "$src" ]]; then
return 1
fi
mkdir -p "$dst"
cp -a "$src/." "$dst/"
chmod -R go-rwx "$jcode_home/external" 2>/dev/null || true
return 0
}
# Seed the sandbox with copies of your real external logins (and, with
# --with-transcripts, your Codex/Claude transcripts) so onboarding's import and
# "continue where you left off" steps act on real data.
seed_real_logins() {
ensure_dirs
local with_transcripts=0
for arg in "$@"; do
case "$arg" in
--with-transcripts) with_transcripts=1 ;;
--transcripts-only) with_transcripts=2 ;;
*) echo "Unknown seed-real-logins option: $arg" >&2; return 2 ;;
esac
done
# Auth/credential files the external-import detectors read, each relative to
# $HOME (mirrors crate::storage::user_home_path and the per-provider paths).
local auth_files=(
".codex/auth.json"
".claude/.credentials.json"
".claude.json"
".local/share/opencode/auth.json"
".pi/agent/auth.json"
".gemini/oauth_creds.json"
".config/github-copilot/hosts.json"
".config/github-copilot/apps.json"
".cursor/auth.json"
".config/cursor/auth.json"
".config/Cursor/User/globalStorage/state.vscdb"
".config/cursor/User/globalStorage/state.vscdb"
)
# Transcript stores the "continue where you left off" picker reads.
local transcript_dirs=(
".codex/sessions"
".claude/projects"
)
local seeded=()
local skipped=()
if [[ $with_transcripts -ne 2 ]]; then
for rel in "${auth_files[@]}"; do
if seed_one_file "$rel"; then
seeded+=("$rel")
else
skipped+=("$rel")
fi
done
fi
if [[ $with_transcripts -ge 1 ]]; then
for rel in "${transcript_dirs[@]}"; do
if seed_one_dir "$rel"; then
seeded+=("$rel/ (transcripts)")
else
skipped+=("$rel/ (transcripts)")
fi
done
fi
echo "Seeded real logins into sandbox external dir:"
echo " $jcode_home/external"
echo
if [[ ${#seeded[@]} -gt 0 ]]; then
echo "Copied:"
for rel in "${seeded[@]}"; do
echo " + $rel"
done
else
echo "Copied nothing (no matching real files found under \$HOME)."
fi
if [[ ${#skipped[@]} -gt 0 ]]; then
echo
echo "Not present (skipped):"
for rel in "${skipped[@]}"; do
echo " - $rel"
done
fi
echo
echo "These are copies; your real \$HOME files are untouched."
echo "They contain sensitive data and persist until reset or purge-external."
echo "Onboarding will now offer to import them. Start it with:"
echo " $(basename "$0") jcode"
}
scenario_arg() {
if [[ $# -gt 0 ]]; then
printf '%s' "$1"
else
printf 'onboarding'
fi
}
case "$command" in
env)
print_env
;;
status)
status
;;
reset)
reset
;;
purge-external)
purge_external
;;
shell)
open_shell
;;
jcode)
run_jcode "$@"
;;
auth-status)
run_jcode auth status
;;
fresh)
reset
run_jcode "$@"
;;
seed-real-logins)
seed_real_logins "$@"
;;
fresh-real)
reset
seed_real_logins "$@"
echo
echo "Launching sandbox jcode with your real logins available to import..."
if run_jcode; then
rc=0
else
rc=$?
fi
if [[ "${JCODE_ONBOARDING_KEEP_EXTERNAL:-0}" != "1" ]]; then
purge_external
else
echo "Keeping copied external credentials because JCODE_ONBOARDING_KEEP_EXTERNAL=1."
fi
exit "$rc"
;;
login)
if [[ $# -lt 1 ]]; then
echo "login requires a provider, for example: $(basename "$0") login openai" >&2
exit 1
fi
provider=$1
shift
run_jcode --provider "$provider" login "$@"
;;
fixture-list)
run_auth_fixture list
;;
fixture-save)
run_auth_fixture save "$@"
;;
fixture-load)
run_auth_fixture load "$@"
;;
fixture-run)
run_auth_fixture run "$@"
;;
help|-h|--help)
print_usage
;;
*)
echo "Unknown command: $command" >&2
echo >&2
print_usage >&2
exit 1
;;
esac
You can’t perform that action at this time.
