From 0235e9a2845f803213250367dd6325f7c084fe99 Mon Sep 17 00:00:00 2001 From: giveen Date: Thu, 2 Apr 2026 16:21:36 -0600 Subject: [PATCH] fix: assign _last_user_input before Runner.run() to avoid UnboundLocalError on first compact --- src/cai/cli.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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