From b76e56f7502ae0dabec670516ca856ff19ebda33 Mon Sep 17 00:00:00 2001 From: Paul O'Reilly Date: Mon, 22 Jun 2026 10:17:31 +1200 Subject: [PATCH] fix(claude-profile): use --append-system-prompt-file to avoid ARG_MAX on large projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- scripts/claude-profile | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/scripts/claude-profile b/scripts/claude-profile index 12c954f..120a6f5 100755 --- a/scripts/claude-profile +++ b/scripts/claude-profile @@ -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 ${CLAUDE_ARGS[*]}" + dryrun_log "Would execute: claude --model $MODEL_ID --append-system-prompt-file ${CLAUDE_ARGS[*]}" else - dryrun_log "Would execute: claude --model $MODEL_ID --append-system-prompt " + dryrun_log "Would execute: claude --model $MODEL_ID --append-system-prompt-file " 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[@]}"