-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: apply assistant-prefill guard to Anthropic models in interactive mode #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
octo-patch
wants to merge
3
commits into
usestrix:main
Choose a base branch
from
octo-patch:fix/issue-416-anthropic-prefill-in-interactive-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Tests for LLM._prepare_messages trailing-assistant-message handling.""" | ||
| from strix.llm.config import LLMConfig | ||
| from strix.llm.llm import LLM | ||
|
|
||
|
|
||
| def _make_llm(monkeypatch, model_name: str, interactive: bool) -> LLM: | ||
| monkeypatch.setenv("STRIX_LLM", model_name) | ||
| config = LLMConfig(model_name=model_name, interactive=interactive, enable_prompt_caching=False) | ||
| return LLM(config, agent_name=None) | ||
|
|
||
|
|
||
| def _history_ending_with_assistant() -> list[dict]: | ||
| return [ | ||
| {"role": "user", "content": "Scan this target."}, | ||
| {"role": "assistant", "content": "I found a vulnerability."}, | ||
| ] | ||
|
|
||
|
|
||
| def test_non_interactive_anthropic_adds_user_message(monkeypatch) -> None: | ||
| """Non-interactive mode always appends a user message when history ends with assistant.""" | ||
| llm = _make_llm(monkeypatch, "claude-sonnet-4-6", interactive=False) | ||
| history = _history_ending_with_assistant() | ||
| messages = llm._prepare_messages(history) | ||
| assert messages[-1]["role"] == "user" | ||
| assert messages[-1]["content"] == "<meta>Continue the task.</meta>" | ||
|
|
||
|
|
||
| def test_interactive_anthropic_adds_user_message(monkeypatch) -> None: | ||
| """Interactive mode with Anthropic model must also append a user message. | ||
|
|
||
| Anthropic API rejects messages where the last entry has role 'assistant' | ||
| (no assistant prefill support). This should hold regardless of interactive mode. | ||
| """ | ||
| llm = _make_llm(monkeypatch, "claude-sonnet-4-6", interactive=True) | ||
| history = _history_ending_with_assistant() | ||
| messages = llm._prepare_messages(history) | ||
| assert messages[-1]["role"] == "user" | ||
| assert messages[-1]["content"] == "<meta>Continue the task.</meta>" | ||
|
|
||
|
|
||
| def test_interactive_non_anthropic_does_not_add_user_message(monkeypatch) -> None: | ||
| """Interactive mode with a non-Anthropic model keeps the trailing assistant message. | ||
|
|
||
| Non-Anthropic models may support assistant prefill; in interactive mode the | ||
| caller (TUI) is responsible for appending the next user message. | ||
| """ | ||
| llm = _make_llm(monkeypatch, "openai/gpt-5.4", interactive=True) | ||
| history = _history_ending_with_assistant() | ||
| messages = llm._prepare_messages(history) | ||
| assert messages[-1]["role"] == "assistant" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, if the model provided empty strings for required report fields, the function returned a structured error (
{"success": False, ...}), giving the model a chance to retry with actual content. After this change, empty fields are silently replaced with"[Not provided by model]"and the scan is markedsuccess: True. The model never learns that its output was incomplete, so it cannot retry — the user receives a scan report with placeholder text and no in-session opportunity for the model to fill in the missing data.If the intent is to prevent infinite retry loops, a middle path would be to return a soft warning in the success response, or to log/surface the placeholder substitution to the user at completion time.
Prompt To Fix With AI