fix(claude-profile): use --append-system-prompt-file to avoid ARG_MAX on large projects

Passing combined context inline as a CLI arg hits Linux ARG_MAX (~2 MB)
when project CONTEXT.md or MEMORY.md are large. Three changes:

1. Write combined_context to a mktemp file and pass via
   --append-system-prompt-file instead of the inline --append-system-prompt arg.

2. Drop exec in favour of a subprocess call so the EXIT trap fires
   after claude exits, properly resetting the WezTerm theme and cleaning
   up the temp file. (With exec, bash is replaced immediately and traps
   never fire on success — WezTerm reset was silently broken.)

3. Warn when context exceeds 150 KB and show context size (in KB) on
   the launch line and in dryrun output, so oversized CONTEXT.md files
   are visible before they become a crash.
This commit is contained in:
Paul O'Reilly
2026-06-22 10:17:31 +12:00
parent 864960cbbb
commit b76e56f750

View File

@@ -378,6 +378,11 @@ combined_context="${context}
${body}"
ctx_kb=$(( ${#combined_context} / 1024 ))
if (( ${#combined_context} > 150000 )); then
echo "Warning: context is ${ctx_kb}KB — large projects may benefit from trimming CONTEXT.md or MEMORY.md" >&2
fi
if (( DRYRUN )); then
dryrun_log "Profile: $PROFILE ($target)"
dryrun_log "Mode: $MODE"
@@ -396,11 +401,11 @@ if (( DRYRUN )); then
dryrun_log "Would write: $target/active-mode.env"
dryrun_log "Would write: $target/last-mode"
if (( ${#CLAUDE_ARGS[@]} > 0 )); then
dryrun_log "Would execute: claude --model $MODEL_ID --append-system-prompt <context+mode> ${CLAUDE_ARGS[*]}"
dryrun_log "Would execute: claude --model $MODEL_ID --append-system-prompt-file <tmpfile> ${CLAUDE_ARGS[*]}"
else
dryrun_log "Would execute: claude --model $MODEL_ID --append-system-prompt <context+mode>"
dryrun_log "Would execute: claude --model $MODEL_ID --append-system-prompt-file <tmpfile>"
fi
dryrun_log "Context length: ${#combined_context} chars (${#context} from context-load + ${#body} from mode body)"
dryrun_log "Context length: ${#combined_context} chars (~${ctx_kb}KB; ${#context} from context-load + ${#body} from mode body)"
dryrun_log "No changes made."
exit 0
fi
@@ -433,5 +438,13 @@ done
printf '%s\n' "$active_env" > "$target/active-mode.env"
printf '%s\n' "$MODE" > "$target/last-mode"
echo "Launching: $PROFILE / $MODE${PROJECT:+ / $PROJECT}"
exec claude --model "$MODEL_ID" --append-system-prompt "$combined_context" "${CLAUDE_ARGS[@]}"
# Write context to a temp file — passing it inline as an argument hits ARG_MAX
# on large projects (context-load output can exceed 2 MB).
# Dropping exec (→ subprocess) means the EXIT trap fires after claude exits,
# which properly resets the WezTerm theme and cleans up the temp file.
context_file=$(mktemp /tmp/claude-profile-ctx-XXXXXX.md)
printf '%s\n' "$combined_context" > "$context_file"
trap 'rm -f "${context_file:-}"; reset_wezterm_profile' EXIT
echo "Launching: $PROFILE / $MODE${PROJECT:+ / $PROJECT} (context: ${ctx_kb}KB)"
claude --model "$MODEL_ID" --append-system-prompt-file "$context_file" "${CLAUDE_ARGS[@]}"