fix: thinking block content leak in text extraction#10
Merged
Conversation
content_models.ThinkingContent has a .text attribute (distinct from message_models.ThinkingBlock which uses .thinking). The hasattr-based filter at both the main loop path and the max-iterations fallback was letting thinking text leak into final_response. Extracts a shared _extract_text_from_content helper and replaces both call sites. Same fix pattern as amplifier-module-loop-streaming PR #25 (merge df5c0e1) and amplifier-module-loop-basic PR #11. Adds regression tests verifying ThinkingContent is filtered at both code paths while TextBlock/TextContent pass through.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a thinking-block text leak at two locations in
amplifier_module_loop_events/__init__.py.Root Cause
There are two content block model systems in amplifier-core:
content_models.ThinkingContent→ has.text(was the bug hazard)message_models.ThinkingBlock→ has.thinking(already safe)The old
execute()inline text extraction usedhasattr(block, "text")at both the main loop path (line ~268) and the max-iterations fallback path (line ~621). BecauseThinkingContenthas a.textattribute, this guard let thinking text leak intofinal_response.Fix
Extracts a shared
_extract_text_from_contenthelper (module-level, following the existing_normalize_tool_callconvention) that uses an explicitblock.type == "text"check. Replaces both call sites with the helper — eliminating the duplication and the bug in one pass.Pattern
Same fix as:
_extract_text_from_contentpatternThis module has two locations with the bug, so extracting the shared helper is especially important for DRY compliance.
Tests Added
New file
tests/test_thinking_block_leak.pywith 8 tests:test_thinking_content_does_not_leak_into_final_responsetest_thinking_content_filtered_at_max_iterations_fallbacktest_helper_directly_filters_thinking_contenttest_text_content_passes_throughTextContentstill workstest_text_block_passes_throughTextBlockstill workstest_thinking_block_does_not_leak_into_final_responseThinkingBlockwas already safetest_dict_text_block_passes_through{"type": "text"}blocks pass throughtest_dict_thinking_block_is_filtered{"type": "thinking"}blocks filteredTDD Verification
RED: 4 tests failed before fix (bug tests + import error for helper not yet existing)
GREEN: All 8 tests pass after fix
Note:
test_tool_post_no_modify_uses_originalintest_hook_modify.pywas already failing onmainbefore this branch — not introduced by this change.