-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex-profile
More file actions
executable file
·540 lines (413 loc) · 11.7 KB
/
Copy pathcodex-profile
File metadata and controls
executable file
·540 lines (413 loc) · 11.7 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env bash
set -euo pipefail
MAIN_HOME="${CODEX_MAIN_HOME:-$HOME/.codex}"
PROFILE_BASE="${CODEX_PROFILE_BASE:-$HOME/.codex-profiles}"
SHARED_HOME="${CODEX_SHARED_HOME:-$HOME/.codex-shared}"
SHARED_DIRS=(
"sessions"
"memories"
"rules"
"skills"
"shell_snapshots"
)
SHARED_FILES=(
"AGENTS.md"
"history.jsonl"
"session_index.jsonl"
"config.toml"
"models_cache.json"
)
usage() {
cat <<EOF
codex-profile - Multi-profile manager for OpenAI Codex CLI
Usage:
codex-profile init
codex-profile add <profile-name>
codex-profile list
codex-profile path <profile-name>
codex-profile login <profile-name>
codex-profile run <profile-name> [codex args...]
codex-profile resume <profile-name> [codex resume args...]
codex-profile status
codex-profile help
Overview:
codex-profile lets you use multiple Codex CLI profiles while sharing
selected local context files between them.
Each profile keeps its own authentication and runtime state:
- auth.json
- state_*.sqlite
- logs_*.sqlite
- cache/
- log/
- tmp/
Shared context items are linked from the main Codex home:
- sessions/
- session_index.jsonl
- history.jsonl
- memories/
- rules/
- skills/
- shell_snapshots/
- AGENTS.md
- config.toml
- models_cache.json
This allows another Codex profile to resume previous sessions and use the
same rules, memories, skills, and MCP configuration, while keeping auth files
isolated per profile.
Commands:
init
Initialize the shared context link directory.
Default main Codex home:
\$HOME/.codex
Default shared link directory:
\$HOME/.codex-shared
This command does not move files from the main Codex home.
It creates symlinks in the shared directory pointing back to the main home.
Example:
codex-profile init
add <profile-name>
Create a new Codex profile.
Default profile path:
\$HOME/.codex-profiles/<profile-name>
The new profile gets symlinks to shared context items.
Authentication and runtime state remain profile-specific.
Examples:
codex-profile add backup
codex-profile add work
codex-profile add api
list
List the main profile and additional profiles.
Example:
codex-profile list
path <profile-name>
Print the CODEX_HOME path for a profile.
Example:
codex-profile path backup
login <profile-name>
Run "codex login" with the selected profile's CODEX_HOME.
If the profile does not exist, it is created first.
Example:
codex-profile login backup
Equivalent:
CODEX_HOME="\$HOME/.codex-profiles/backup" codex login
run <profile-name> [codex args...]
Run Codex with the selected profile.
If the profile does not exist, it is created first.
Examples:
codex-profile run backup
codex-profile run backup exec "review this repository"
codex-profile run work --model gpt-5.1-codex
Equivalent:
CODEX_HOME="\$HOME/.codex-profiles/backup" codex [args...]
resume <profile-name> [codex resume args...]
Run "codex resume" with the selected profile.
If the profile does not exist, it is created first.
Examples:
codex-profile resume backup --last
codex-profile resume backup --all
Notes:
--last
Resume the latest session for the current working directory.
--all
Include sessions from other working directories in the picker.
Equivalent:
CODEX_HOME="\$HOME/.codex-profiles/backup" codex resume [args...]
status
Show main home, shared home, profiles, and shared symlinks.
Example:
codex-profile status
Environment variables:
CODEX_MAIN_HOME
Main Codex home directory.
Default:
\$HOME/.codex
CODEX_PROFILE_BASE
Base directory for additional profiles.
Default:
\$HOME/.codex-profiles
CODEX_SHARED_HOME
Shared symlink directory.
Default:
\$HOME/.codex-shared
Recommended aliases:
alias codex-main='CODEX_HOME="\$HOME/.codex" codex'
alias codex-backup='codex-profile run backup'
alias codex-backup-resume='codex-profile resume backup --last'
alias codex-backup-resume-all='codex-profile resume backup --all'
Typical workflow:
1. Initialize shared context:
codex-profile init
2. Create a new profile:
codex-profile add backup
3. Log in with the new profile:
codex-profile login backup
4. Run Codex with the profile:
codex-profile run backup
5. Resume the previous session with another profile:
codex-profile resume backup --last
Safety model:
Shared:
- sessions
- memories
- rules
- skills
- config.toml
- AGENTS.md
- history.jsonl
- session_index.jsonl
Not shared:
- auth.json
- installation_id
- state_*.sqlite
- logs_*.sqlite
- cache/
- log/
- tmp/
- version.json
Important:
This tool is designed for sequential profile switching.
Do not run multiple Codex profiles at the same time while sharing the same
sessions/history files. Concurrent writes may corrupt or desynchronize local
Codex state.
The script does not move files from the main Codex home. The shared directory
contains symlinks pointing back to the main profile.
Example symlink structure:
~/.codex/config.toml
^
~/.codex-shared/config.toml -> ~/.codex/config.toml
^
~/.codex-profiles/backup/config.toml -> ~/.codex-shared/config.toml
Authentication remains isolated:
~/.codex/auth.json
~/.codex-profiles/backup/auth.json
EOF
}
die() {
echo "ERROR: $*" >&2
exit 1
}
validate_profile_name() {
local profile_name="$1"
[[ -n "$profile_name" ]] || die "Profile name is required."
if [[ ! "$profile_name" =~ ^[a-zA-Z0-9._-]+$ ]]; then
die "Invalid profile name: $profile_name"
fi
if [[ "$profile_name" == "." || "$profile_name" == ".." ]]; then
die "Invalid profile name: $profile_name"
fi
case "$profile_name" in
main|shared|default)
die "Reserved profile name: $profile_name"
;;
esac
}
ensure_base_dirs() {
mkdir -p "$MAIN_HOME"
mkdir -p "$PROFILE_BASE"
mkdir -p "$SHARED_HOME"
}
path_exists_or_is_symlink() {
local target_path="$1"
[[ -e "$target_path" || -L "$target_path" ]]
}
safe_symlink() {
local target_path="$1"
local link_path="$2"
local parent_dir
parent_dir="$(dirname "$link_path")"
mkdir -p "$parent_dir"
if path_exists_or_is_symlink "$link_path" && [[ ! -L "$link_path" ]]; then
echo "exists and is not a symlink, skipped: $link_path"
return 1
fi
rm -f "$link_path"
ln -s "$target_path" "$link_path"
}
link_dir_from_main_to_shared() {
local shared_item="$1"
local main_path="$MAIN_HOME/$shared_item"
local shared_link="$SHARED_HOME/$shared_item"
if ! path_exists_or_is_symlink "$main_path"; then
mkdir -p "$main_path"
fi
if [[ ! -d "$main_path" && ! -L "$main_path" ]]; then
echo "not a directory, skipped: $main_path"
return
fi
safe_symlink "$main_path" "$shared_link" >/dev/null || return
echo "shared dir: $shared_link -> $main_path"
}
link_file_from_main_to_shared() {
local shared_item="$1"
local main_path="$MAIN_HOME/$shared_item"
local shared_link="$SHARED_HOME/$shared_item"
if ! path_exists_or_is_symlink "$main_path"; then
echo "missing main file, skipped: $shared_item"
return
fi
if [[ ! -f "$main_path" && ! -L "$main_path" ]]; then
echo "not a regular file, skipped: $main_path"
return
fi
safe_symlink "$main_path" "$shared_link" >/dev/null || return
echo "shared file: $shared_link -> $main_path"
}
link_shared_item_into_profile() {
local profile_home="$1"
local shared_item="$2"
local shared_path="$SHARED_HOME/$shared_item"
local profile_path="$profile_home/$shared_item"
if ! path_exists_or_is_symlink "$shared_path"; then
echo "missing shared item, skipped: $shared_path"
return
fi
safe_symlink "$shared_path" "$profile_path" >/dev/null || return
echo "profile link: $profile_path -> $shared_path"
}
init_shared() {
local shared_item
ensure_base_dirs
for shared_item in "${SHARED_DIRS[@]}"; do
link_dir_from_main_to_shared "$shared_item"
done
for shared_item in "${SHARED_FILES[@]}"; do
link_file_from_main_to_shared "$shared_item"
done
echo
echo "Initialized shared Codex context:"
echo " main: $MAIN_HOME"
echo " shared: $SHARED_HOME"
}
link_shared_into_profile() {
local profile_home="$1"
local shared_item
mkdir -p "$profile_home"
for shared_item in "${SHARED_DIRS[@]}"; do
link_shared_item_into_profile "$profile_home" "$shared_item"
done
for shared_item in "${SHARED_FILES[@]}"; do
link_shared_item_into_profile "$profile_home" "$shared_item"
done
}
add_profile() {
local profile_name="$1"
local profile_home
validate_profile_name "$profile_name"
profile_home="$PROFILE_BASE/$profile_name"
init_shared >/dev/null
mkdir -p "$profile_home"
link_shared_into_profile "$profile_home"
echo
echo "Profile created:"
echo " name: $profile_name"
echo " CODEX_HOME: $profile_home"
echo
echo "Login:"
echo " CODEX_HOME=\"$profile_home\" codex login"
echo
echo "Run:"
echo " CODEX_HOME=\"$profile_home\" codex"
echo
echo "Resume:"
echo " CODEX_HOME=\"$profile_home\" codex resume --last"
}
list_profiles() {
ensure_base_dirs
echo "main -> $MAIN_HOME"
if [[ -d "$PROFILE_BASE" ]]; then
find "$PROFILE_BASE" -mindepth 1 -maxdepth 1 -type d -printf "%f -> %p\n" | sort
fi
}
profile_path() {
local profile_name="$1"
validate_profile_name "$profile_name"
echo "$PROFILE_BASE/$profile_name"
}
login_profile() {
local profile_name="$1"
local profile_home
validate_profile_name "$profile_name"
profile_home="$PROFILE_BASE/$profile_name"
if [[ ! -d "$profile_home" ]]; then
add_profile "$profile_name" >/dev/null
fi
CODEX_HOME="$profile_home" codex login
}
run_profile() {
local profile_name="$1"
local profile_home
shift
validate_profile_name "$profile_name"
profile_home="$PROFILE_BASE/$profile_name"
if [[ ! -d "$profile_home" ]]; then
add_profile "$profile_name" >/dev/null
fi
CODEX_HOME="$profile_home" codex "$@"
}
resume_profile() {
local profile_name="$1"
local profile_home
shift
validate_profile_name "$profile_name"
profile_home="$PROFILE_BASE/$profile_name"
if [[ ! -d "$profile_home" ]]; then
add_profile "$profile_name" >/dev/null
fi
CODEX_HOME="$profile_home" codex resume "$@"
}
show_status() {
ensure_base_dirs
echo "Main:"
echo " $MAIN_HOME"
echo
echo "Shared:"
echo " $SHARED_HOME"
echo
echo "Profiles:"
list_profiles
echo
echo "Shared links:"
find "$SHARED_HOME" -maxdepth 1 -type l -printf " %f -> %l\n" | sort || true
}
main() {
local command="${1:-}"
shift || true
case "$command" in
init)
init_shared
;;
add)
[[ $# -eq 1 ]] || die "Usage: codex-profile add <profile-name>"
add_profile "$1"
;;
list)
list_profiles
;;
path)
[[ $# -eq 1 ]] || die "Usage: codex-profile path <profile-name>"
profile_path "$1"
;;
login)
[[ $# -eq 1 ]] || die "Usage: codex-profile login <profile-name>"
login_profile "$1"
;;
run)
[[ $# -ge 1 ]] || die "Usage: codex-profile run <profile-name> [codex args...]"
run_profile "$@"
;;
resume)
[[ $# -ge 1 ]] || die "Usage: codex-profile resume <profile-name> [codex resume args...]"
resume_profile "$@"
;;
status)
show_status
;;
-h|--help|help|"")
usage
;;
*)
die "Unknown command: $command"
;;
esac
}
main "$@"