Skip to content

Instantly share code, notes, and snippets.

@myobie
Created June 23, 2026 20:19
Show Gist options
  • Select an option

  • Save myobie/ee8d1dd8817086d8d3383ed12ef4ef9b to your computer and use it in GitHub Desktop.

Select an option

Save myobie/ee8d1dd8817086d8d3383ed12ef4ef9b to your computer and use it in GitHub Desktop.
Wrapper for launching Claude Code under detached pty (e.g. pty up) — bootstraps the session jsonl via --print on first run since detached pty interactive doesn't trigger persistence init. See https://gist.github.com/myobie/1505d47f9e805534d07ff721ca38557f for the bug context.
#!/bin/sh
# pty-claude-launcher.sh — start or resume a pinned-id claude session
# under a detached pty (e.g. `pty up`), bootstrapping the transcript
# jsonl via `claude --print` on first run.
#
# Why: under detached pty + scripted interaction, claude's interactive
# init never creates `~/.claude/projects/<sanitized-cwd>/<uuid>.jsonl`,
# so a `--session-id <new-uuid>` fallthrough silently runs in-memory
# only and loses everything on shutdown. `--print` hits a different
# code path that always writes the jsonl. Subsequent `--resume <uuid>`
# under detached pty appends to the existing jsonl normally.
#
# This script is idempotent: the bootstrap fires only when the jsonl
# is missing. Existing agents whose jsonl already exists go straight
# to --resume — no re-bootstrap, no API token cost.
#
# Args forwarded to interactive claude, e.g.
# "$HOME/bin/pty-claude-launcher.sh --dangerously-load-development-channels server:coord"
[ -f .claude-session-id ] || uuidgen > .claude-session-id
SID=$(cat .claude-session-id | tr -d '[:space:]')
ENCODED=$(pwd -P | tr '/.' '--')
JSONL="$HOME/.claude/projects/${ENCODED}/${SID}.jsonl"
if [ ! -f "$JSONL" ]; then
claude --print --dangerously-skip-permissions --session-id "$SID" </dev/null "session init" >/dev/null 2>&1 || true
fi
exec claude --dangerously-skip-permissions "$@" --resume "$SID"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment