Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: dynamic file config values in tests + sentinel chunk for cancellation (#10223)
Kian Jonesc6c87501eb9f
diff --git a/tests/integration_test_human_in_the_loop.py b/tests/integration_test_human_in_the_loop.pyindex 9ec7f783e..c05f9d149 100644--- a/tests/integration_test_human_in_the_loop.py+++ b/tests/integration_test_human_in_the_loop.py@@ -1345,8 +1345,6 @@ def test_approve_with_cancellation(Test that when approval and cancellation happen simultaneously,the stream returns stop_reason: cancelled and stream_was_cancelled is set."""- import threading- import timelast_message_cursor = client.agents.messages.list(agent_id=agent.id, limit=1).items[0].id@@ -1357,15 +1355,8 @@ def test_approve_with_cancellation()tool_call_id = response.messages[-1].tool_call.tool_call_id- # Step 2: Start cancellation in background thread- def cancel_after_delay():- time.sleep(0.3) # Wait for stream to start- client.agents.messages.cancel(agent_id=agent.id)-- cancel_thread = threading.Thread(target=cancel_after_delay, daemon=True)- cancel_thread.start()-- # Step 3: Start approval stream (will be cancelled during processing)+ # Step 2: Start approval stream and wait for first chunk (sentinel approach)+ # This avoids race conditions with fixed delays in CI environmentsresponse = client.agents.messages.stream(agent_id=agent.id,messages=[@@ -1385,8 +1376,23 @@ def test_approve_with_cancellation(stream_tokens=True,)- # Step 4: Accumulate chunks- messages = accumulate_chunks(response)+ # Step 3: Wait for first chunk before triggering cancellation+ messages = []+ cancel_triggered = False++ for chunk in response:+ # Handle chunks that might not have message_type (like pings)+ if not hasattr(chunk, "message_type") or chunk.message_type == "ping":+ continue++ # Trigger cancellation after receiving first meaningful chunk+ if not cancel_triggered:+ cancel_triggered = True+ client.agents.messages.cancel(agent_id=agent.id)++ messages.append(chunk)++ # Step 4: Accumulate remaining chunks (already collected above)# Step 5: Verify we got chunks AND a cancelled stop reasonassert len(messages) > 1, "Should receive at least some chunks before cancellation"@@ -1401,9 +1407,6 @@ def test_approve_with_cancellation(latest_run = runs.items[0]assert latest_run.status == "cancelled", f"Expected run status 'cancelled', got '{latest_run.status}'"- # Wait for cancel thread to finish- cancel_thread.join(timeout=1.0)-logger.info(f"✅ Test passed: approval with cancellation handled correctly, received {len(messages)} chunks")# Step 7: Verify that approval response message is persisted@@ -1464,8 +1467,6 @@ def test_retry_with_summarization(4. Verify only system and summary messages remain in context5. Retry the original approval response - should succeed via idempotency check"""- import threading- import time# Step 1: Send message that triggers approval requestresponse = client.agents.messages.create(@@ -1474,15 +1475,8 @@ def test_retry_with_summarization()tool_call_id = response.messages[-1].tool_call.tool_call_id- # Step 2: Start cancellation in background thread- def cancel_after_delay():- time.sleep(0.3) # Wait for stream to start- client.agents.messages.cancel(agent_id=agent.id)-- cancel_thread = threading.Thread(target=cancel_after_delay, daemon=True)- cancel_thread.start()-- # Step 3: Start approval stream (will be cancelled during processing)+ # Step 2: Start approval stream and wait for first chunk (sentinel approach)+ # This avoids race conditions with fixed delays in CI environmentsresponse = client.agents.messages.stream(agent_id=agent.id,messages=[@@ -1502,16 +1496,27 @@ def test_retry_with_summarization(stream_tokens=True,)- # Step 4: Accumulate chunks (stream will be cancelled)- messages = accumulate_chunks(response)+ # Step 3: Wait for first chunk before triggering cancellation+ messages = []+ cancel_triggered = False++ for chunk in response:+ # Handle chunks that might not have message_type (like pings)+ if not hasattr(chunk, "message_type") or chunk.message_type == "ping":+ continue++ # Trigger cancellation after receiving first meaningful chunk+ if not cancel_triggered:+ cancel_triggered = True+ client.agents.messages.cancel(agent_id=agent.id)- # Step 5: Verify we got cancelled+ messages.append(chunk)++ # Step 4: Verify we got cancelledstop_reasons = [msg for msg in messages if hasattr(msg, "message_type") and msg.message_type == "stop_reason"]assert len(stop_reasons) == 1, f"Expected exactly 1 stop_reason, got {len(stop_reasons)}"assert stop_reasons[0].stop_reason == "cancelled", f"Expected stop_reason 'cancelled', got '{stop_reasons[0].stop_reason}'"- cancel_thread.join(timeout=1.0)-# Step 6: Verify tool return message is persistedall_messages = client.agents.messages.list(agent_id=agent.id, limit=100).itemstool_return_messages = [m for m in all_messages if m.message_type == "tool_return_message"]diff --git a/tests/test_sources.py b/tests/test_sources.pyindex f89f30876..594c665e4 100644--- a/tests/test_sources.py+++ b/tests/test_sources.py@@ -278,20 +278,26 @@ def test_attach_existing_files_creates_source_blocks_correctly(# Attach after uploading the fileclient.agents.folders.attach(folder_id=source.id, agent_id=agent_state.id)++ # Get agent state to retrieve file config defaults (dynamic based on model context window)+ agent_state = client.agents.retrieve(agent_id=agent_state.id)+ max_files_open = agent_state.max_files_open+ chars_limit = agent_state.per_file_view_window_char_limit+raw_system_message = get_raw_system_message(client, agent_state.id, recompile=True)# Assert that the expected chunk is in the raw system message- expected_chunk = """<directories>+ expected_chunk = f"""<directories><file_limits>- current_files_open=1-- max_files_open=5+- max_files_open={max_files_open}</file_limits><directory name="test_source"><file status="open" name="test_source/test.txt"><metadata>- read_only=true- chars_current=45-- chars_limit=15000+- chars_limit={chars_limit}</metadata><value>[Viewing file start (out of 1 lines)]@@ -342,19 +348,25 @@ def test_delete_source_removes_source_blocks_correctly(# Upload the filesupload_file_and_wait(client, source.id, file_path)++ # Get agent state to retrieve file config defaults (dynamic based on model context window)+ agent_state = client.agents.retrieve(agent_id=agent_state.id)+ max_files_open = agent_state.max_files_open+ chars_limit = agent_state.per_file_view_window_char_limit+raw_system_message = get_raw_system_message(client, agent_state.id, recompile=True)# Assert that the expected chunk is in the raw system message- expected_chunk = """<directories>+ expected_chunk = f"""<directories><file_limits>- current_files_open=1-- max_files_open=5+- max_files_open={max_files_open}</file_limits><directory name="test_source"><file status="open" name="test_source/test.txt"><metadata>- read_only=true- chars_current=45-- chars_limit=15000+- chars_limit={chars_limit}</metadata><value>[Viewing file start (out of 1 lines)]