# agent-launchers.zsh — the agent-launcher function families from the workshop. # Curated from the presenter's dotfiles for "Agentic Software Factory" # (2026-07-17), section 02: Custom zsh Functions. Adapt paths to your setup. # Companion page: https://sacred-ritual-w7as.here.now/ # # Placeholders (comments only — nothing below needs editing to run): # = the login that owns ~/.codex # = the login that owns the second-account dirs # Lines that differ from the private originals carry an "ADAPTED:" comment. # # Safety trade (deliberate, visible, yours to reject): # --dangerously-bypass-approvals-and-sandbox (Codex) and # --dangerously-skip-permissions (Claude Code) remove approval prompts. # Delete those flags if you do not accept unattended file and shell access. # ============================================================ Codex: the x family : "${CODEX_PRIMARY_HOME:=$HOME/.codex}" # ADAPTED: name the second home whatever fits your second account, e.g. ~/.codex-work : "${CODEX_EGGHEAD_HOME:=$HOME/.codex-egghead}" # Names that must never be shared between accounts (auth / login only). # Everything else under the primary home is eligible for a symlink into x2. _codex_auth_local_basenames() { case "$1" in auth.json|auth.json.*|*.auth.json) return 0 ;; codex-login.log|codex-login*.log) return 0 ;; esac return 1 } # Build/refresh the second home as a shared view of ~/.codex, minus auth. # Safety rules: # - never write into CODEX_PRIMARY_HOME # - never replace a real (non-symlink) file/dir in the second home # - if auth.json is accidentally a symlink into primary, remove only that symlink # - only create/repair symlinks we manage (existing real files stay put) _codex_egghead_ensure_home() { emulate -L zsh setopt null_glob extended_glob local primary="${CODEX_PRIMARY_HOME:-$HOME/.codex}" local secondary="${CODEX_EGGHEAD_HOME:-$HOME/.codex-egghead}" local src base dest rel current if [[ ! -d "$primary" ]]; then print -u2 -- "codex primary home missing: $primary" return 1 fi # Refuse pathological layouts. if [[ "$secondary" == "$primary" || "$secondary" == "$primary"/* ]]; then print -u2 -- "refusing second home inside primary: $secondary" return 1 fi mkdir -p "$secondary" || return 1 # If a previous setup linked auth into the primary account, break that only. dest="$secondary/auth.json" if [[ -L "$dest" ]]; then current="$(readlink "$dest" 2>/dev/null || true)" case "$current" in "$primary/auth.json"|"$primary"/auth.json) rm -f "$dest" || return 1 ;; esac fi # Symlink top-level primary entries except auth-local basenames. for src in "$primary"/* "$primary"/.[!.]* "$primary"/..?*; do [[ -e "$src" || -L "$src" ]] || continue base="${src:t}" [[ "$base" == "." || "$base" == ".." ]] && continue _codex_auth_local_basenames "$base" && continue dest="$secondary/$base" if [[ -L "$dest" ]]; then current="$(readlink "$dest" 2>/dev/null || true)" if [[ "$current" != "$src" ]]; then # Only retarget an existing symlink; never touch real files. ln -sfn "$src" "$dest" || return 1 fi continue fi if [[ -e "$dest" ]]; then # Real file/dir already present in the second home — leave it alone. continue fi ln -s "$src" "$dest" || return 1 done # Keep login diagnostics local even if parent `log` is shared. # When primary/log is a real directory and we symlinked it, login would # write into primary. Prefer a local log dir with non-auth log files linked. if [[ -d "$primary/log" && ! -L "$primary/log" ]]; then if [[ -L "$secondary/log" ]]; then rm -f "$secondary/log" || return 1 fi mkdir -p "$secondary/log" || return 1 for src in "$primary/log"/*(N) "$primary/log"/.[!.]*(N); do [[ -e "$src" || -L "$src" ]] || continue base="${src:t}" [[ "$base" == "." || "$base" == ".." ]] && continue _codex_auth_local_basenames "$base" && continue dest="$secondary/log/$base" if [[ -L "$dest" ]]; then current="$(readlink "$dest" 2>/dev/null || true)" [[ "$current" == "$src" ]] && continue ln -sfn "$src" "$dest" || return 1 continue fi [[ -e "$dest" ]] && continue ln -s "$src" "$dest" || return 1 done fi # Ensure auth.json is never a symlink after ensure. if [[ -L "$secondary/auth.json" ]]; then print -u2 -- "refusing to use symlinked auth.json in $secondary" return 1 fi return 0 } unalias x 2>/dev/null x() { # Default account: via ~/.codex (ADAPTED: email -> placeholder) CODEX_HOME="${CODEX_PRIMARY_HOME:-$HOME/.codex}" \ codex -c 'shell_environment_policy.set.CODEX_LOAD_ZSH_DSL="1"' \ --dangerously-bypass-approvals-and-sandbox "$@" } unalias x2 2>/dev/null x2() { # Secondary account: via ~/.codex-egghead (ADAPTED: email -> placeholder) # Shared config/skills/sessions/etc via symlinks; local auth.json only. _codex_egghead_ensure_home || return 1 CODEX_HOME="${CODEX_EGGHEAD_HOME:-$HOME/.codex-egghead}" \ codex \ -c 'cli_auth_credentials_store="file"' \ -c 'shell_environment_policy.set.CODEX_LOAD_ZSH_DSL="1"' \ --dangerously-bypass-approvals-and-sandbox "$@" } # The resume family: every helper routes through x, so a future fix # to x fixes the whole family for free. unalias xr 2>/dev/null xr() { x resume "$@"; } # pick a session to resume unalias xrl 2>/dev/null xrl() { x resume --last "$@"; } # jump into the most recent session unalias xrm 2>/dev/null xrm() { x resume m "$@"; } # resume the session named "m" — rename to your favorite # ====================================================== Claude Code: the clx family unalias clx clx2 clxw 2>/dev/null clx() { claude --dangerously-skip-permissions "$@"; } clx2() { # second account () — same UX, separate config dir local config_dir="${CLAUDE_SECOND_CONFIG_DIR:-$HOME/.claude-second}" mkdir -p "$config_dir" || return CLAUDE_CONFIG_DIR="$config_dir" claude --dangerously-skip-permissions "$@" } clxw() { # launch inside a named worktree: clxw [args] local name="$1"; shift CLAUDE_WORKTREE_NAME="$name" claude --worktree "$name" --dangerously-skip-permissions "$@" } # ============================================ Prompt-builder handoff: p-f and p-c # Requires the `p` prompt-builder CLI. Compose the prompt first; the handoff # hands the finished prompt to a zsh function that forks and resumes a session. _p_handoff_zsh() { local fn="${1:?Usage: _p_handoff_zsh [p-args...]}" shift p \ --handoff-command zsh \ --handoff-arg -lc \ --handoff-arg "source \"\$HOME/.zsh-wrappers/agent-launchers.zsh\" >/dev/null 2>&1; ${fn} \"\$@\"" \ --handoff-arg "$fn" \ "$@" # ADAPTED: sources this installed file; the original sources the presenter's # full dotfiles module instead. } _codex_prompt_builder_fork() { # ADAPTED: the private original forks through the presenter's app-server # helpers, which can also compact the fork before resuming; that machinery # is not portable. This version uses codex's built-in fork. --compact is # accepted for interface compatibility but performs a plain fork. local fork_from="last" while (( $# )); do case "$1" in --fork-from) [[ $# -ge 2 ]] || { print -u2 -- "missing value for --fork-from"; return 2; } fork_from="$2" shift 2 ;; --compact) shift ;; --) shift break ;; *) break ;; esac done if [[ -z "$fork_from" || "$fork_from" == "last" ]]; then x fork --last "$@" else x fork "$fork_from" "$@" fi } p-f() { local source="${CODEX_FORK_SESSION_ID:-last}" _p_handoff_zsh _codex_prompt_builder_fork --fork-from "$source" "$@" } p-c() { local source="${CODEX_FORK_SESSION_ID:-last}" _p_handoff_zsh _codex_prompt_builder_fork --fork-from "$source" --compact "$@" } # ==================================== The cheat sheet: every family documents itself x-() { # ADAPTED: lists only the functions in this curated file; the private # original covers ~35 helpers. local -a fn_name=(x x2 xr xrl xrm clx clx2 clxw p-f p-c) local -A fn_desc=( x 'Primary Codex ( / ~/.codex) with agent-safe zsh DSL marker.' x2 'Second-account Codex ( / ~/.codex-egghead); shared state via symlinks, local auth only.' xr 'Resume a Codex session through the base x wrapper.' xrl 'Resume the most recent Codex session through the base x wrapper.' xrm 'Resume the Codex conversation named m through the base x wrapper.' clx 'Claude Code with permission prompts skipped.' clx2 'Second-account Claude Code via CLAUDE_CONFIG_DIR=~/.claude-second.' clxw 'Claude Code inside a named worktree: clxw [args].' p-f 'Compose a prompt in p, then fork the last Codex session and resume with it.' p-c 'Compose a prompt in p, then fork and resume (the original also compacts).' ) local fn for fn in "${fn_name[@]}"; do printf '%-5s %s\n' "$fn" "${fn_desc[$fn]}" done }