diff --git a/src/cai/cli.py b/src/cai/cli.py index fc731b21..c1694c7c 100644 --- a/src/cai/cli.py +++ b/src/cai/cli.py @@ -1500,6 +1500,10 @@ def run_cai_cli( {"role": "assistant", "content": f"{result.final_output}"} ) else: + # Capture user_input before runner calls so ContextCompactedError + # handlers can reference it even on the very first iteration. + _last_user_input = user_input if isinstance(user_input, str) else "" + # Disable streaming by default, unless specifically enabled cai_stream = os.getenv("CAI_STREAM", "false") # Handle empty string or None values @@ -1757,9 +1761,6 @@ def run_cai_cli( agent.model.message_history[:] = fix_message_list(agent.model.message_history) turn_count += 1 - # Capture user_input here so auto-compact can replay it after clearing history. - _last_user_input = user_input if isinstance(user_input, str) else "" - # Auto-compact: when CAI_SUPPORT_MODEL + CAI_SUPPORT_INTERVAL are both set, # compact the conversation every N LLM *responses* (assistant messages in # history) using the support model. Counting assistant messages rather